2 Copyright (C) 2000-2013 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 "c-family/c-pragma.h"
33 #include "diagnostic-core.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
39 #include "tree-pretty-print.h"
41 #include "type-utils.h"
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 static cp_token eof_token
=
52 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant
{
58 /* floating-point literal */
62 /* %<__FUNCTION__%> */
64 /* %<__PRETTY_FUNCTION__%> */
72 /* %<typeid%> operator */
74 /* non-constant compound literals */
82 /* an array reference */
88 /* the address of a label */
102 /* calls to overloaded operators */
106 /* a comma operator */
108 /* a call to a constructor */
110 /* a transaction expression */
112 } non_integral_constant
;
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error
{
120 /* is not a class or namespace */
122 /* is not a class, namespace, or enumeration */
126 /* The various kinds of required token */
127 typedef enum required_token
{
129 RT_SEMICOLON
, /* ';' */
130 RT_OPEN_PAREN
, /* '(' */
131 RT_CLOSE_BRACE
, /* '}' */
132 RT_OPEN_BRACE
, /* '{' */
133 RT_CLOSE_SQUARE
, /* ']' */
134 RT_OPEN_SQUARE
, /* '[' */
138 RT_GREATER
, /* '>' */
140 RT_ELLIPSIS
, /* '...' */
144 RT_COLON_SCOPE
, /* ':' or '::' */
145 RT_CLOSE_PAREN
, /* ')' */
146 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
147 RT_PRAGMA_EOL
, /* end of line */
148 RT_NAME
, /* identifier */
150 /* The type is CPP_KEYWORD */
152 RT_DELETE
, /* delete */
153 RT_RETURN
, /* return */
154 RT_WHILE
, /* while */
155 RT_EXTERN
, /* extern */
156 RT_STATIC_ASSERT
, /* static_assert */
157 RT_DECLTYPE
, /* decltype */
158 RT_OPERATOR
, /* operator */
159 RT_CLASS
, /* class */
160 RT_TEMPLATE
, /* template */
161 RT_NAMESPACE
, /* namespace */
162 RT_USING
, /* using */
165 RT_CATCH
, /* catch */
166 RT_THROW
, /* throw */
167 RT_LABEL
, /* __label__ */
168 RT_AT_TRY
, /* @try */
169 RT_AT_SYNCHRONIZED
, /* @synchronized */
170 RT_AT_THROW
, /* @throw */
172 RT_SELECT
, /* selection-statement */
173 RT_INTERATION
, /* iteration-statement */
174 RT_JUMP
, /* jump-statement */
175 RT_CLASS_KEY
, /* class-key */
176 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
177 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
178 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
179 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
184 static cp_lexer
*cp_lexer_new_main
186 static cp_lexer
*cp_lexer_new_from_tokens
187 (cp_token_cache
*tokens
);
188 static void cp_lexer_destroy
190 static int cp_lexer_saving_tokens
192 static cp_token
*cp_lexer_token_at
193 (cp_lexer
*, cp_token_position
);
194 static void cp_lexer_get_preprocessor_token
195 (cp_lexer
*, cp_token
*);
196 static inline cp_token
*cp_lexer_peek_token
198 static cp_token
*cp_lexer_peek_nth_token
199 (cp_lexer
*, size_t);
200 static inline bool cp_lexer_next_token_is
201 (cp_lexer
*, enum cpp_ttype
);
202 static bool cp_lexer_next_token_is_not
203 (cp_lexer
*, enum cpp_ttype
);
204 static bool cp_lexer_next_token_is_keyword
205 (cp_lexer
*, enum rid
);
206 static cp_token
*cp_lexer_consume_token
208 static void cp_lexer_purge_token
210 static void cp_lexer_purge_tokens_after
211 (cp_lexer
*, cp_token_position
);
212 static void cp_lexer_save_tokens
214 static void cp_lexer_commit_tokens
216 static void cp_lexer_rollback_tokens
218 static void cp_lexer_print_token
219 (FILE *, cp_token
*);
220 static inline bool cp_lexer_debugging_p
222 static void cp_lexer_start_debugging
223 (cp_lexer
*) ATTRIBUTE_UNUSED
;
224 static void cp_lexer_stop_debugging
225 (cp_lexer
*) ATTRIBUTE_UNUSED
;
227 static cp_token_cache
*cp_token_cache_new
228 (cp_token
*, cp_token
*);
230 static void cp_parser_initial_pragma
233 static tree cp_literal_operator_id
236 static void cp_parser_cilk_simd
237 (cp_parser
*, cp_token
*);
238 static bool cp_parser_omp_declare_reduction_exprs
241 /* Manifest constants. */
242 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
243 #define CP_SAVED_TOKEN_STACK 5
247 /* The stream to which debugging output should be written. */
248 static FILE *cp_lexer_debug_stream
;
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand
;
254 /* Dump up to NUM tokens in BUFFER to FILE starting with token
255 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
256 first token in BUFFER. If NUM is 0, dump all the tokens. If
257 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
258 highlighted by surrounding it in [[ ]]. */
261 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
262 cp_token
*start_token
, unsigned num
,
263 cp_token
*curr_token
)
265 unsigned i
, nprinted
;
269 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
275 num
= buffer
->length ();
277 if (start_token
== NULL
)
278 start_token
= buffer
->address ();
280 if (start_token
> buffer
->address ())
282 cp_lexer_print_token (file
, &(*buffer
)[0]);
283 fprintf (file
, " ... ");
288 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
290 if (token
== start_token
)
297 if (token
== curr_token
)
298 fprintf (file
, "[[");
300 cp_lexer_print_token (file
, token
);
302 if (token
== curr_token
)
303 fprintf (file
, "]]");
309 case CPP_CLOSE_BRACE
:
319 if (i
== num
&& i
< buffer
->length ())
321 fprintf (file
, " ... ");
322 cp_lexer_print_token (file
, &buffer
->last ());
325 fprintf (file
, "\n");
329 /* Dump all tokens in BUFFER to stderr. */
332 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
334 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
338 debug (vec
<cp_token
, va_gc
> &ref
)
340 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
344 debug (vec
<cp_token
, va_gc
> *ptr
)
349 fprintf (stderr
, "<nil>\n");
353 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
354 description for T. */
357 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
361 fprintf (file
, "%s: ", desc
);
362 print_node_brief (file
, "", t
, 0);
367 /* Dump parser context C to FILE. */
370 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
372 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
373 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
374 print_node_brief (file
, "", c
->object_type
, 0);
375 fprintf (file
, "}\n");
379 /* Print the stack of parsing contexts to FILE starting with FIRST. */
382 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
385 cp_parser_context
*c
;
387 fprintf (file
, "Parsing context stack:\n");
388 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
390 fprintf (file
, "\t#%u: ", i
);
391 cp_debug_print_context (file
, c
);
396 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
399 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
402 fprintf (file
, "%s: true\n", desc
);
406 /* Print an unparsed function entry UF to FILE. */
409 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
412 cp_default_arg_entry
*default_arg_fn
;
415 fprintf (file
, "\tFunctions with default args:\n");
417 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
420 fprintf (file
, "\t\tClass type: ");
421 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
422 fprintf (file
, "\t\tDeclaration: ");
423 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
424 fprintf (file
, "\n");
427 fprintf (file
, "\n\tFunctions with definitions that require "
428 "post-processing\n\t\t");
429 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
431 print_node_brief (file
, "", fn
, 0);
434 fprintf (file
, "\n");
436 fprintf (file
, "\n\tNon-static data members with initializers that require "
437 "post-processing\n\t\t");
438 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
440 print_node_brief (file
, "", fn
, 0);
443 fprintf (file
, "\n");
447 /* Print the stack of unparsed member functions S to FILE. */
450 cp_debug_print_unparsed_queues (FILE *file
,
451 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
454 cp_unparsed_functions_entry
*uf
;
456 fprintf (file
, "Unparsed functions\n");
457 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
459 fprintf (file
, "#%u:\n", i
);
460 cp_debug_print_unparsed_function (file
, uf
);
465 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
466 the given PARSER. If FILE is NULL, the output is printed on stderr. */
469 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
471 cp_token
*next_token
, *first_token
, *start_token
;
476 next_token
= parser
->lexer
->next_token
;
477 first_token
= parser
->lexer
->buffer
->address ();
478 start_token
= (next_token
> first_token
+ window_size
/ 2)
479 ? next_token
- window_size
/ 2
481 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
486 /* Dump debugging information for the given PARSER. If FILE is NULL,
487 the output is printed on stderr. */
490 cp_debug_parser (FILE *file
, cp_parser
*parser
)
492 const size_t window_size
= 20;
494 expanded_location eloc
;
499 fprintf (file
, "Parser state\n\n");
500 fprintf (file
, "Number of tokens: %u\n",
501 vec_safe_length (parser
->lexer
->buffer
));
502 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
503 cp_debug_print_tree_if_set (file
, "Object scope",
504 parser
->object_scope
);
505 cp_debug_print_tree_if_set (file
, "Qualifying scope",
506 parser
->qualifying_scope
);
507 cp_debug_print_context_stack (file
, parser
->context
);
508 cp_debug_print_flag (file
, "Allow GNU extensions",
509 parser
->allow_gnu_extensions_p
);
510 cp_debug_print_flag (file
, "'>' token is greater-than",
511 parser
->greater_than_is_operator_p
);
512 cp_debug_print_flag (file
, "Default args allowed in current "
513 "parameter list", parser
->default_arg_ok_p
);
514 cp_debug_print_flag (file
, "Parsing integral constant-expression",
515 parser
->integral_constant_expression_p
);
516 cp_debug_print_flag (file
, "Allow non-constant expression in current "
517 "constant-expression",
518 parser
->allow_non_integral_constant_expression_p
);
519 cp_debug_print_flag (file
, "Seen non-constant expression",
520 parser
->non_integral_constant_expression_p
);
521 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
523 parser
->local_variables_forbidden_p
);
524 cp_debug_print_flag (file
, "In unbraced linkage specification",
525 parser
->in_unbraced_linkage_specification_p
);
526 cp_debug_print_flag (file
, "Parsing a declarator",
527 parser
->in_declarator_p
);
528 cp_debug_print_flag (file
, "In template argument list",
529 parser
->in_template_argument_list_p
);
530 cp_debug_print_flag (file
, "Parsing an iteration statement",
531 parser
->in_statement
& IN_ITERATION_STMT
);
532 cp_debug_print_flag (file
, "Parsing a switch statement",
533 parser
->in_statement
& IN_SWITCH_STMT
);
534 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
535 parser
->in_statement
& IN_OMP_BLOCK
);
536 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
537 parser
->in_statement
& IN_CILK_SIMD_FOR
);
538 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
539 parser
->in_statement
& IN_OMP_FOR
);
540 cp_debug_print_flag (file
, "Parsing an if statement",
541 parser
->in_statement
& IN_IF_STMT
);
542 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
543 "context", parser
->in_type_id_in_expr_p
);
544 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
545 parser
->implicit_extern_c
);
546 cp_debug_print_flag (file
, "String expressions should be translated "
547 "to execution character set",
548 parser
->translate_strings_p
);
549 cp_debug_print_flag (file
, "Parsing function body outside of a "
550 "local class", parser
->in_function_body
);
551 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
552 parser
->colon_corrects_to_scope_p
);
553 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
554 parser
->colon_doesnt_start_class_def_p
);
555 if (parser
->type_definition_forbidden_message
)
556 fprintf (file
, "Error message for forbidden type definitions: %s\n",
557 parser
->type_definition_forbidden_message
);
558 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
559 fprintf (file
, "Number of class definitions in progress: %u\n",
560 parser
->num_classes_being_defined
);
561 fprintf (file
, "Number of template parameter lists for the current "
562 "declaration: %u\n", parser
->num_template_parameter_lists
);
563 cp_debug_parser_tokens (file
, parser
, window_size
);
564 token
= parser
->lexer
->next_token
;
565 fprintf (file
, "Next token to parse:\n");
566 fprintf (file
, "\tToken: ");
567 cp_lexer_print_token (file
, token
);
568 eloc
= expand_location (token
->location
);
569 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
570 fprintf (file
, "\tLine: %d\n", eloc
.line
);
571 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
575 debug (cp_parser
&ref
)
577 cp_debug_parser (stderr
, &ref
);
581 debug (cp_parser
*ptr
)
586 fprintf (stderr
, "<nil>\n");
589 /* Allocate memory for a new lexer object and return it. */
592 cp_lexer_alloc (void)
596 c_common_no_more_pch ();
598 /* Allocate the memory. */
599 lexer
= ggc_alloc_cleared_cp_lexer ();
601 /* Initially we are not debugging. */
602 lexer
->debugging_p
= false;
604 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
606 /* Create the buffer. */
607 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
613 /* Create a new main C++ lexer, the lexer that gets tokens from the
617 cp_lexer_new_main (void)
622 /* It's possible that parsing the first pragma will load a PCH file,
623 which is a GC collection point. So we have to do that before
624 allocating any memory. */
625 cp_parser_initial_pragma (&token
);
627 lexer
= cp_lexer_alloc ();
629 /* Put the first token in the buffer. */
630 lexer
->buffer
->quick_push (token
);
632 /* Get the remaining tokens from the preprocessor. */
633 while (token
.type
!= CPP_EOF
)
635 cp_lexer_get_preprocessor_token (lexer
, &token
);
636 vec_safe_push (lexer
->buffer
, token
);
639 lexer
->last_token
= lexer
->buffer
->address ()
640 + lexer
->buffer
->length ()
642 lexer
->next_token
= lexer
->buffer
->length ()
643 ? lexer
->buffer
->address ()
646 /* Subsequent preprocessor diagnostics should use compiler
647 diagnostic functions to get the compiler source location. */
650 gcc_assert (!lexer
->next_token
->purged_p
);
654 /* Create a new lexer whose token stream is primed with the tokens in
655 CACHE. When these tokens are exhausted, no new tokens will be read. */
658 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
660 cp_token
*first
= cache
->first
;
661 cp_token
*last
= cache
->last
;
662 cp_lexer
*lexer
= ggc_alloc_cleared_cp_lexer ();
664 /* We do not own the buffer. */
665 lexer
->buffer
= NULL
;
666 lexer
->next_token
= first
== last
? &eof_token
: first
;
667 lexer
->last_token
= last
;
669 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
671 /* Initially we are not debugging. */
672 lexer
->debugging_p
= false;
674 gcc_assert (!lexer
->next_token
->purged_p
);
678 /* Frees all resources associated with LEXER. */
681 cp_lexer_destroy (cp_lexer
*lexer
)
683 vec_free (lexer
->buffer
);
684 lexer
->saved_tokens
.release ();
688 /* Returns nonzero if debugging information should be output. */
691 cp_lexer_debugging_p (cp_lexer
*lexer
)
693 return lexer
->debugging_p
;
697 static inline cp_token_position
698 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
700 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
702 return lexer
->next_token
- previous_p
;
705 static inline cp_token
*
706 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
712 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
714 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
717 static inline cp_token_position
718 cp_lexer_previous_token_position (cp_lexer
*lexer
)
720 if (lexer
->next_token
== &eof_token
)
721 return lexer
->last_token
- 1;
723 return cp_lexer_token_position (lexer
, true);
726 static inline cp_token
*
727 cp_lexer_previous_token (cp_lexer
*lexer
)
729 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
731 return cp_lexer_token_at (lexer
, tp
);
734 /* nonzero if we are presently saving tokens. */
737 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
739 return lexer
->saved_tokens
.length () != 0;
742 /* Store the next token from the preprocessor in *TOKEN. Return true
743 if we reach EOF. If LEXER is NULL, assume we are handling an
744 initial #pragma pch_preprocess, and thus want the lexer to return
745 processed strings. */
748 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
750 static int is_extern_c
= 0;
752 /* Get a new token from the preprocessor. */
754 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
755 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
756 token
->keyword
= RID_MAX
;
757 token
->pragma_kind
= PRAGMA_NONE
;
758 token
->purged_p
= false;
760 /* On some systems, some header files are surrounded by an
761 implicit extern "C" block. Set a flag in the token if it
762 comes from such a header. */
763 is_extern_c
+= pending_lang_change
;
764 pending_lang_change
= 0;
765 token
->implicit_extern_c
= is_extern_c
> 0;
767 /* Check to see if this token is a keyword. */
768 if (token
->type
== CPP_NAME
)
770 if (C_IS_RESERVED_WORD (token
->u
.value
))
772 /* Mark this token as a keyword. */
773 token
->type
= CPP_KEYWORD
;
774 /* Record which keyword. */
775 token
->keyword
= C_RID_CODE (token
->u
.value
);
779 if (warn_cxx0x_compat
780 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
781 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
783 /* Warn about the C++0x keyword (but still treat it as
785 warning (OPT_Wc__0x_compat
,
786 "identifier %qE is a keyword in C++11",
789 /* Clear out the C_RID_CODE so we don't warn about this
790 particular identifier-turned-keyword again. */
791 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
794 token
->ambiguous_p
= false;
795 token
->keyword
= RID_MAX
;
798 else if (token
->type
== CPP_AT_NAME
)
800 /* This only happens in Objective-C++; it must be a keyword. */
801 token
->type
= CPP_KEYWORD
;
802 switch (C_RID_CODE (token
->u
.value
))
804 /* Replace 'class' with '@class', 'private' with '@private',
805 etc. This prevents confusion with the C++ keyword
806 'class', and makes the tokens consistent with other
807 Objective-C 'AT' keywords. For example '@class' is
808 reported as RID_AT_CLASS which is consistent with
809 '@synchronized', which is reported as
812 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
813 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
814 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
815 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
816 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
817 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
818 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
819 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
822 else if (token
->type
== CPP_PRAGMA
)
824 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
825 token
->pragma_kind
= ((enum pragma_kind
)
826 TREE_INT_CST_LOW (token
->u
.value
));
827 token
->u
.value
= NULL_TREE
;
831 /* Update the globals input_location and the input file stack from TOKEN. */
833 cp_lexer_set_source_position_from_token (cp_token
*token
)
835 if (token
->type
!= CPP_EOF
)
837 input_location
= token
->location
;
841 /* Return a pointer to the next token in the token stream, but do not
844 static inline cp_token
*
845 cp_lexer_peek_token (cp_lexer
*lexer
)
847 if (cp_lexer_debugging_p (lexer
))
849 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
850 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
851 putc ('\n', cp_lexer_debug_stream
);
853 return lexer
->next_token
;
856 /* Return true if the next token has the indicated TYPE. */
859 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
861 return cp_lexer_peek_token (lexer
)->type
== type
;
864 /* Return true if the next token does not have the indicated TYPE. */
867 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
869 return !cp_lexer_next_token_is (lexer
, type
);
872 /* Return true if the next token is the indicated KEYWORD. */
875 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
877 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
881 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
883 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
886 /* Return true if the next token is not the indicated KEYWORD. */
889 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
891 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
894 /* Return true if the next token is a keyword for a decl-specifier. */
897 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
901 token
= cp_lexer_peek_token (lexer
);
902 switch (token
->keyword
)
904 /* auto specifier: storage-class-specifier in C++,
905 simple-type-specifier in C++0x. */
907 /* Storage classes. */
913 /* Elaborated type specifiers. */
919 /* Simple type specifiers. */
934 /* GNU extensions. */
937 /* C++0x extensions. */
939 case RID_UNDERLYING_TYPE
:
947 /* Returns TRUE iff the token T begins a decltype type. */
950 token_is_decltype (cp_token
*t
)
952 return (t
->keyword
== RID_DECLTYPE
953 || t
->type
== CPP_DECLTYPE
);
956 /* Returns TRUE iff the next token begins a decltype type. */
959 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
961 cp_token
*t
= cp_lexer_peek_token (lexer
);
962 return token_is_decltype (t
);
965 /* Return a pointer to the Nth token in the token stream. If N is 1,
966 then this is precisely equivalent to cp_lexer_peek_token (except
967 that it is not inline). One would like to disallow that case, but
968 there is one case (cp_parser_nth_token_starts_template_id) where
969 the caller passes a variable for N and it might be 1. */
972 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
976 /* N is 1-based, not zero-based. */
979 if (cp_lexer_debugging_p (lexer
))
980 fprintf (cp_lexer_debug_stream
,
981 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
984 token
= lexer
->next_token
;
985 gcc_assert (!n
|| token
!= &eof_token
);
989 if (token
== lexer
->last_token
)
995 if (!token
->purged_p
)
999 if (cp_lexer_debugging_p (lexer
))
1001 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1002 putc ('\n', cp_lexer_debug_stream
);
1008 /* Return the next token, and advance the lexer's next_token pointer
1009 to point to the next non-purged token. */
1012 cp_lexer_consume_token (cp_lexer
* lexer
)
1014 cp_token
*token
= lexer
->next_token
;
1016 gcc_assert (token
!= &eof_token
);
1017 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1021 lexer
->next_token
++;
1022 if (lexer
->next_token
== lexer
->last_token
)
1024 lexer
->next_token
= &eof_token
;
1029 while (lexer
->next_token
->purged_p
);
1031 cp_lexer_set_source_position_from_token (token
);
1033 /* Provide debugging output. */
1034 if (cp_lexer_debugging_p (lexer
))
1036 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1037 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1038 putc ('\n', cp_lexer_debug_stream
);
1044 /* Permanently remove the next token from the token stream, and
1045 advance the next_token pointer to refer to the next non-purged
1049 cp_lexer_purge_token (cp_lexer
*lexer
)
1051 cp_token
*tok
= lexer
->next_token
;
1053 gcc_assert (tok
!= &eof_token
);
1054 tok
->purged_p
= true;
1055 tok
->location
= UNKNOWN_LOCATION
;
1056 tok
->u
.value
= NULL_TREE
;
1057 tok
->keyword
= RID_MAX
;
1062 if (tok
== lexer
->last_token
)
1068 while (tok
->purged_p
);
1069 lexer
->next_token
= tok
;
1072 /* Permanently remove all tokens after TOK, up to, but not
1073 including, the token that will be returned next by
1074 cp_lexer_peek_token. */
1077 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1079 cp_token
*peek
= lexer
->next_token
;
1081 if (peek
== &eof_token
)
1082 peek
= lexer
->last_token
;
1084 gcc_assert (tok
< peek
);
1086 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1088 tok
->purged_p
= true;
1089 tok
->location
= UNKNOWN_LOCATION
;
1090 tok
->u
.value
= NULL_TREE
;
1091 tok
->keyword
= RID_MAX
;
1095 /* Begin saving tokens. All tokens consumed after this point will be
1099 cp_lexer_save_tokens (cp_lexer
* lexer
)
1101 /* Provide debugging output. */
1102 if (cp_lexer_debugging_p (lexer
))
1103 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1105 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1108 /* Commit to the portion of the token stream most recently saved. */
1111 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer
))
1115 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1117 lexer
->saved_tokens
.pop ();
1120 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1121 to the token stream. Stop saving tokens. */
1124 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1126 /* Provide debugging output. */
1127 if (cp_lexer_debugging_p (lexer
))
1128 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1130 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1133 /* Print a representation of the TOKEN on the STREAM. */
1136 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1138 /* We don't use cpp_type2name here because the parser defines
1139 a few tokens of its own. */
1140 static const char *const token_names
[] = {
1141 /* cpplib-defined token types */
1142 #define OP(e, s) #e,
1143 #define TK(e, s) #e,
1147 /* C++ parser token types - see "Manifest constants", above. */
1150 "NESTED_NAME_SPECIFIER",
1153 /* For some tokens, print the associated data. */
1154 switch (token
->type
)
1157 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1158 For example, `struct' is mapped to an INTEGER_CST. */
1159 if (!identifier_p (token
->u
.value
))
1161 /* else fall through */
1163 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1170 case CPP_UTF8STRING
:
1171 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1175 print_generic_expr (stream
, token
->u
.value
, 0);
1179 /* If we have a name for the token, print it out. Otherwise, we
1180 simply give the numeric code. */
1181 if (token
->type
< ARRAY_SIZE(token_names
))
1182 fputs (token_names
[token
->type
], stream
);
1184 fprintf (stream
, "[%d]", token
->type
);
1190 debug (cp_token
&ref
)
1192 cp_lexer_print_token (stderr
, &ref
);
1193 fprintf (stderr
, "\n");
1197 debug (cp_token
*ptr
)
1202 fprintf (stderr
, "<nil>\n");
1206 /* Start emitting debugging information. */
1209 cp_lexer_start_debugging (cp_lexer
* lexer
)
1211 lexer
->debugging_p
= true;
1212 cp_lexer_debug_stream
= stderr
;
1215 /* Stop emitting debugging information. */
1218 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1220 lexer
->debugging_p
= false;
1221 cp_lexer_debug_stream
= NULL
;
1224 /* Create a new cp_token_cache, representing a range of tokens. */
1226 static cp_token_cache
*
1227 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1229 cp_token_cache
*cache
= ggc_alloc_cp_token_cache ();
1230 cache
->first
= first
;
1235 /* Diagnose if #pragma omp declare simd isn't followed immediately
1236 by function declaration or definition. */
1239 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1241 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1243 error ("%<#pragma omp declare simd%> not immediately followed by "
1244 "function declaration or definition");
1245 parser
->omp_declare_simd
= NULL
;
1249 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1250 and put that into "omp declare simd" attribute. */
1253 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1255 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1257 if (fndecl
== error_mark_node
)
1259 parser
->omp_declare_simd
= NULL
;
1262 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1264 cp_ensure_no_omp_declare_simd (parser
);
1270 /* Decl-specifiers. */
1272 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1275 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1277 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1282 /* Nothing other than the parser should be creating declarators;
1283 declarators are a semi-syntactic representation of C++ entities.
1284 Other parts of the front end that need to create entities (like
1285 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1287 static cp_declarator
*make_call_declarator
1288 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1289 static cp_declarator
*make_array_declarator
1290 (cp_declarator
*, tree
);
1291 static cp_declarator
*make_pointer_declarator
1292 (cp_cv_quals
, cp_declarator
*, tree
);
1293 static cp_declarator
*make_reference_declarator
1294 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1295 static cp_parameter_declarator
*make_parameter_declarator
1296 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1297 static cp_declarator
*make_ptrmem_declarator
1298 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1300 /* An erroneous declarator. */
1301 static cp_declarator
*cp_error_declarator
;
1303 /* The obstack on which declarators and related data structures are
1305 static struct obstack declarator_obstack
;
1307 /* Alloc BYTES from the declarator memory pool. */
1309 static inline void *
1310 alloc_declarator (size_t bytes
)
1312 return obstack_alloc (&declarator_obstack
, bytes
);
1315 /* Allocate a declarator of the indicated KIND. Clear fields that are
1316 common to all declarators. */
1318 static cp_declarator
*
1319 make_declarator (cp_declarator_kind kind
)
1321 cp_declarator
*declarator
;
1323 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1324 declarator
->kind
= kind
;
1325 declarator
->attributes
= NULL_TREE
;
1326 declarator
->std_attributes
= NULL_TREE
;
1327 declarator
->declarator
= NULL
;
1328 declarator
->parameter_pack_p
= false;
1329 declarator
->id_loc
= UNKNOWN_LOCATION
;
1334 /* Make a declarator for a generalized identifier. If
1335 QUALIFYING_SCOPE is non-NULL, the identifier is
1336 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1337 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1340 static cp_declarator
*
1341 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1342 special_function_kind sfk
)
1344 cp_declarator
*declarator
;
1346 /* It is valid to write:
1348 class C { void f(); };
1352 The standard is not clear about whether `typedef const C D' is
1353 legal; as of 2002-09-15 the committee is considering that
1354 question. EDG 3.0 allows that syntax. Therefore, we do as
1356 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1357 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1359 gcc_assert (identifier_p (unqualified_name
)
1360 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1361 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1363 declarator
= make_declarator (cdk_id
);
1364 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1365 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1366 declarator
->u
.id
.sfk
= sfk
;
1371 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1372 of modifiers such as const or volatile to apply to the pointer
1373 type, represented as identifiers. ATTRIBUTES represent the attributes that
1374 appertain to the pointer or reference. */
1377 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1380 cp_declarator
*declarator
;
1382 declarator
= make_declarator (cdk_pointer
);
1383 declarator
->declarator
= target
;
1384 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1385 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1388 declarator
->id_loc
= target
->id_loc
;
1389 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1390 target
->parameter_pack_p
= false;
1393 declarator
->parameter_pack_p
= false;
1395 declarator
->std_attributes
= attributes
;
1400 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1401 represent the attributes that appertain to the pointer or
1405 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1406 bool rvalue_ref
, tree attributes
)
1408 cp_declarator
*declarator
;
1410 declarator
= make_declarator (cdk_reference
);
1411 declarator
->declarator
= target
;
1412 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1413 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1416 declarator
->id_loc
= target
->id_loc
;
1417 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1418 target
->parameter_pack_p
= false;
1421 declarator
->parameter_pack_p
= false;
1423 declarator
->std_attributes
= attributes
;
1428 /* Like make_pointer_declarator -- but for a pointer to a non-static
1429 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1430 appertain to the pointer or reference. */
1433 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1434 cp_declarator
*pointee
,
1437 cp_declarator
*declarator
;
1439 declarator
= make_declarator (cdk_ptrmem
);
1440 declarator
->declarator
= pointee
;
1441 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1442 declarator
->u
.pointer
.class_type
= class_type
;
1446 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1447 pointee
->parameter_pack_p
= false;
1450 declarator
->parameter_pack_p
= false;
1452 declarator
->std_attributes
= attributes
;
1457 /* Make a declarator for the function given by TARGET, with the
1458 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1459 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1460 indicates what exceptions can be thrown. */
1463 make_call_declarator (cp_declarator
*target
,
1465 cp_cv_quals cv_qualifiers
,
1466 cp_virt_specifiers virt_specifiers
,
1467 cp_ref_qualifier ref_qualifier
,
1468 tree exception_specification
,
1469 tree late_return_type
)
1471 cp_declarator
*declarator
;
1473 declarator
= make_declarator (cdk_function
);
1474 declarator
->declarator
= target
;
1475 declarator
->u
.function
.parameters
= parms
;
1476 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1477 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1478 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1479 declarator
->u
.function
.exception_specification
= exception_specification
;
1480 declarator
->u
.function
.late_return_type
= late_return_type
;
1483 declarator
->id_loc
= target
->id_loc
;
1484 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1485 target
->parameter_pack_p
= false;
1488 declarator
->parameter_pack_p
= false;
1493 /* Make a declarator for an array of BOUNDS elements, each of which is
1494 defined by ELEMENT. */
1497 make_array_declarator (cp_declarator
*element
, tree bounds
)
1499 cp_declarator
*declarator
;
1501 declarator
= make_declarator (cdk_array
);
1502 declarator
->declarator
= element
;
1503 declarator
->u
.array
.bounds
= bounds
;
1506 declarator
->id_loc
= element
->id_loc
;
1507 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1508 element
->parameter_pack_p
= false;
1511 declarator
->parameter_pack_p
= false;
1516 /* Determine whether the declarator we've seen so far can be a
1517 parameter pack, when followed by an ellipsis. */
1519 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1521 /* Search for a declarator name, or any other declarator that goes
1522 after the point where the ellipsis could appear in a parameter
1523 pack. If we find any of these, then this declarator can not be
1524 made into a parameter pack. */
1526 while (declarator
&& !found
)
1528 switch ((int)declarator
->kind
)
1539 declarator
= declarator
->declarator
;
1547 cp_parameter_declarator
*no_parameters
;
1549 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1550 DECLARATOR and DEFAULT_ARGUMENT. */
1552 cp_parameter_declarator
*
1553 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1554 cp_declarator
*declarator
,
1555 tree default_argument
)
1557 cp_parameter_declarator
*parameter
;
1559 parameter
= ((cp_parameter_declarator
*)
1560 alloc_declarator (sizeof (cp_parameter_declarator
)));
1561 parameter
->next
= NULL
;
1562 if (decl_specifiers
)
1563 parameter
->decl_specifiers
= *decl_specifiers
;
1565 clear_decl_specs (¶meter
->decl_specifiers
);
1566 parameter
->declarator
= declarator
;
1567 parameter
->default_argument
= default_argument
;
1568 parameter
->ellipsis_p
= false;
1573 /* Returns true iff DECLARATOR is a declaration for a function. */
1576 function_declarator_p (const cp_declarator
*declarator
)
1580 if (declarator
->kind
== cdk_function
1581 && declarator
->declarator
->kind
== cdk_id
)
1583 if (declarator
->kind
== cdk_id
1584 || declarator
->kind
== cdk_error
)
1586 declarator
= declarator
->declarator
;
1596 A cp_parser parses the token stream as specified by the C++
1597 grammar. Its job is purely parsing, not semantic analysis. For
1598 example, the parser breaks the token stream into declarators,
1599 expressions, statements, and other similar syntactic constructs.
1600 It does not check that the types of the expressions on either side
1601 of an assignment-statement are compatible, or that a function is
1602 not declared with a parameter of type `void'.
1604 The parser invokes routines elsewhere in the compiler to perform
1605 semantic analysis and to build up the abstract syntax tree for the
1608 The parser (and the template instantiation code, which is, in a
1609 way, a close relative of parsing) are the only parts of the
1610 compiler that should be calling push_scope and pop_scope, or
1611 related functions. The parser (and template instantiation code)
1612 keeps track of what scope is presently active; everything else
1613 should simply honor that. (The code that generates static
1614 initializers may also need to set the scope, in order to check
1615 access control correctly when emitting the initializers.)
1620 The parser is of the standard recursive-descent variety. Upcoming
1621 tokens in the token stream are examined in order to determine which
1622 production to use when parsing a non-terminal. Some C++ constructs
1623 require arbitrary look ahead to disambiguate. For example, it is
1624 impossible, in the general case, to tell whether a statement is an
1625 expression or declaration without scanning the entire statement.
1626 Therefore, the parser is capable of "parsing tentatively." When the
1627 parser is not sure what construct comes next, it enters this mode.
1628 Then, while we attempt to parse the construct, the parser queues up
1629 error messages, rather than issuing them immediately, and saves the
1630 tokens it consumes. If the construct is parsed successfully, the
1631 parser "commits", i.e., it issues any queued error messages and
1632 the tokens that were being preserved are permanently discarded.
1633 If, however, the construct is not parsed successfully, the parser
1634 rolls back its state completely so that it can resume parsing using
1635 a different alternative.
1640 The performance of the parser could probably be improved substantially.
1641 We could often eliminate the need to parse tentatively by looking ahead
1642 a little bit. In some places, this approach might not entirely eliminate
1643 the need to parse tentatively, but it might still speed up the average
1646 /* Flags that are passed to some parsing functions. These values can
1647 be bitwise-ored together. */
1652 CP_PARSER_FLAGS_NONE
= 0x0,
1653 /* The construct is optional. If it is not present, then no error
1654 should be issued. */
1655 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1656 /* When parsing a type-specifier, treat user-defined type-names
1657 as non-type identifiers. */
1658 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1659 /* When parsing a type-specifier, do not try to parse a class-specifier
1660 or enum-specifier. */
1661 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1662 /* When parsing a decl-specifier-seq, only allow type-specifier or
1664 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1667 /* This type is used for parameters and variables which hold
1668 combinations of the above flags. */
1669 typedef int cp_parser_flags
;
1671 /* The different kinds of declarators we want to parse. */
1673 typedef enum cp_parser_declarator_kind
1675 /* We want an abstract declarator. */
1676 CP_PARSER_DECLARATOR_ABSTRACT
,
1677 /* We want a named declarator. */
1678 CP_PARSER_DECLARATOR_NAMED
,
1679 /* We don't mind, but the name must be an unqualified-id. */
1680 CP_PARSER_DECLARATOR_EITHER
1681 } cp_parser_declarator_kind
;
1683 /* The precedence values used to parse binary expressions. The minimum value
1684 of PREC must be 1, because zero is reserved to quickly discriminate
1685 binary operators from other tokens. */
1690 PREC_LOGICAL_OR_EXPRESSION
,
1691 PREC_LOGICAL_AND_EXPRESSION
,
1692 PREC_INCLUSIVE_OR_EXPRESSION
,
1693 PREC_EXCLUSIVE_OR_EXPRESSION
,
1694 PREC_AND_EXPRESSION
,
1695 PREC_EQUALITY_EXPRESSION
,
1696 PREC_RELATIONAL_EXPRESSION
,
1697 PREC_SHIFT_EXPRESSION
,
1698 PREC_ADDITIVE_EXPRESSION
,
1699 PREC_MULTIPLICATIVE_EXPRESSION
,
1701 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1704 /* A mapping from a token type to a corresponding tree node type, with a
1705 precedence value. */
1707 typedef struct cp_parser_binary_operations_map_node
1709 /* The token type. */
1710 enum cpp_ttype token_type
;
1711 /* The corresponding tree code. */
1712 enum tree_code tree_type
;
1713 /* The precedence of this operator. */
1714 enum cp_parser_prec prec
;
1715 } cp_parser_binary_operations_map_node
;
1717 typedef struct cp_parser_expression_stack_entry
1719 /* Left hand side of the binary operation we are currently
1722 /* Original tree code for left hand side, if it was a binary
1723 expression itself (used for -Wparentheses). */
1724 enum tree_code lhs_type
;
1725 /* Tree code for the binary operation we are parsing. */
1726 enum tree_code tree_type
;
1727 /* Precedence of the binary operation we are parsing. */
1728 enum cp_parser_prec prec
;
1729 /* Location of the binary operation we are parsing. */
1731 } cp_parser_expression_stack_entry
;
1733 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1734 entries because precedence levels on the stack are monotonically
1736 typedef struct cp_parser_expression_stack_entry
1737 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1741 /* Constructors and destructors. */
1743 static cp_parser_context
*cp_parser_context_new
1744 (cp_parser_context
*);
1746 /* Class variables. */
1748 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1750 /* The operator-precedence table used by cp_parser_binary_expression.
1751 Transformed into an associative array (binops_by_token) by
1754 static const cp_parser_binary_operations_map_node binops
[] = {
1755 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1756 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1758 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1759 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1760 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1762 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1763 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1765 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1766 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1768 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1769 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1770 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1771 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1773 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1774 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1776 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1778 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1780 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1782 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1784 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1787 /* The same as binops, but initialized by cp_parser_new so that
1788 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1790 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1792 /* Constructors and destructors. */
1794 /* Construct a new context. The context below this one on the stack
1795 is given by NEXT. */
1797 static cp_parser_context
*
1798 cp_parser_context_new (cp_parser_context
* next
)
1800 cp_parser_context
*context
;
1802 /* Allocate the storage. */
1803 if (cp_parser_context_free_list
!= NULL
)
1805 /* Pull the first entry from the free list. */
1806 context
= cp_parser_context_free_list
;
1807 cp_parser_context_free_list
= context
->next
;
1808 memset (context
, 0, sizeof (*context
));
1811 context
= ggc_alloc_cleared_cp_parser_context ();
1813 /* No errors have occurred yet in this context. */
1814 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1815 /* If this is not the bottommost context, copy information that we
1816 need from the previous context. */
1819 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1820 expression, then we are parsing one in this context, too. */
1821 context
->object_type
= next
->object_type
;
1822 /* Thread the stack. */
1823 context
->next
= next
;
1829 /* Managing the unparsed function queues. */
1831 #define unparsed_funs_with_default_args \
1832 parser->unparsed_queues->last ().funs_with_default_args
1833 #define unparsed_funs_with_definitions \
1834 parser->unparsed_queues->last ().funs_with_definitions
1835 #define unparsed_nsdmis \
1836 parser->unparsed_queues->last ().nsdmis
1839 push_unparsed_function_queues (cp_parser
*parser
)
1841 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
};
1842 vec_safe_push (parser
->unparsed_queues
, e
);
1846 pop_unparsed_function_queues (cp_parser
*parser
)
1848 release_tree_vector (unparsed_funs_with_definitions
);
1849 parser
->unparsed_queues
->pop ();
1854 /* Constructors and destructors. */
1856 static cp_parser
*cp_parser_new
1859 /* Routines to parse various constructs.
1861 Those that return `tree' will return the error_mark_node (rather
1862 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1863 Sometimes, they will return an ordinary node if error-recovery was
1864 attempted, even though a parse error occurred. So, to check
1865 whether or not a parse error occurred, you should always use
1866 cp_parser_error_occurred. If the construct is optional (indicated
1867 either by an `_opt' in the name of the function that does the
1868 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1869 the construct is not present. */
1871 /* Lexical conventions [gram.lex] */
1873 static tree cp_parser_identifier
1875 static tree cp_parser_string_literal
1876 (cp_parser
*, bool, bool);
1877 static tree cp_parser_userdef_char_literal
1879 static tree cp_parser_userdef_string_literal
1881 static tree cp_parser_userdef_numeric_literal
1884 /* Basic concepts [gram.basic] */
1886 static bool cp_parser_translation_unit
1889 /* Expressions [gram.expr] */
1891 static tree cp_parser_primary_expression
1892 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1893 static tree cp_parser_id_expression
1894 (cp_parser
*, bool, bool, bool *, bool, bool);
1895 static tree cp_parser_unqualified_id
1896 (cp_parser
*, bool, bool, bool, bool);
1897 static tree cp_parser_nested_name_specifier_opt
1898 (cp_parser
*, bool, bool, bool, bool);
1899 static tree cp_parser_nested_name_specifier
1900 (cp_parser
*, bool, bool, bool, bool);
1901 static tree cp_parser_qualifying_entity
1902 (cp_parser
*, bool, bool, bool, bool, bool);
1903 static tree cp_parser_postfix_expression
1904 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1905 static tree cp_parser_postfix_open_square_expression
1906 (cp_parser
*, tree
, bool, bool);
1907 static tree cp_parser_postfix_dot_deref_expression
1908 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1909 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1910 (cp_parser
*, int, bool, bool, bool *);
1911 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1912 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1913 static void cp_parser_pseudo_destructor_name
1914 (cp_parser
*, tree
, tree
*, tree
*);
1915 static tree cp_parser_unary_expression
1916 (cp_parser
*, bool, bool, cp_id_kind
*);
1917 static enum tree_code cp_parser_unary_operator
1919 static tree cp_parser_new_expression
1921 static vec
<tree
, va_gc
> *cp_parser_new_placement
1923 static tree cp_parser_new_type_id
1924 (cp_parser
*, tree
*);
1925 static cp_declarator
*cp_parser_new_declarator_opt
1927 static cp_declarator
*cp_parser_direct_new_declarator
1929 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1931 static tree cp_parser_delete_expression
1933 static tree cp_parser_cast_expression
1934 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1935 static tree cp_parser_binary_expression
1936 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1937 static tree cp_parser_question_colon_clause
1938 (cp_parser
*, tree
);
1939 static tree cp_parser_assignment_expression
1940 (cp_parser
*, bool, cp_id_kind
*);
1941 static enum tree_code cp_parser_assignment_operator_opt
1943 static tree cp_parser_expression
1944 (cp_parser
*, bool, cp_id_kind
*);
1945 static tree cp_parser_expression
1946 (cp_parser
*, bool, bool, cp_id_kind
*);
1947 static tree cp_parser_constant_expression
1948 (cp_parser
*, bool, bool *);
1949 static tree cp_parser_builtin_offsetof
1951 static tree cp_parser_lambda_expression
1953 static void cp_parser_lambda_introducer
1954 (cp_parser
*, tree
);
1955 static bool cp_parser_lambda_declarator_opt
1956 (cp_parser
*, tree
);
1957 static void cp_parser_lambda_body
1958 (cp_parser
*, tree
);
1960 /* Statements [gram.stmt.stmt] */
1962 static void cp_parser_statement
1963 (cp_parser
*, tree
, bool, bool *);
1964 static void cp_parser_label_for_labeled_statement
1965 (cp_parser
*, tree
);
1966 static tree cp_parser_expression_statement
1967 (cp_parser
*, tree
);
1968 static tree cp_parser_compound_statement
1969 (cp_parser
*, tree
, bool, bool);
1970 static void cp_parser_statement_seq_opt
1971 (cp_parser
*, tree
);
1972 static tree cp_parser_selection_statement
1973 (cp_parser
*, bool *);
1974 static tree cp_parser_condition
1976 static tree cp_parser_iteration_statement
1977 (cp_parser
*, bool);
1978 static bool cp_parser_for_init_statement
1979 (cp_parser
*, tree
*decl
);
1980 static tree cp_parser_for
1981 (cp_parser
*, bool);
1982 static tree cp_parser_c_for
1983 (cp_parser
*, tree
, tree
, bool);
1984 static tree cp_parser_range_for
1985 (cp_parser
*, tree
, tree
, tree
, bool);
1986 static void do_range_for_auto_deduction
1988 static tree cp_parser_perform_range_for_lookup
1989 (tree
, tree
*, tree
*);
1990 static tree cp_parser_range_for_member_function
1992 static tree cp_parser_jump_statement
1994 static void cp_parser_declaration_statement
1997 static tree cp_parser_implicitly_scoped_statement
1998 (cp_parser
*, bool *);
1999 static void cp_parser_already_scoped_statement
2002 /* Declarations [gram.dcl.dcl] */
2004 static void cp_parser_declaration_seq_opt
2006 static void cp_parser_declaration
2008 static void cp_parser_block_declaration
2009 (cp_parser
*, bool);
2010 static void cp_parser_simple_declaration
2011 (cp_parser
*, bool, tree
*);
2012 static void cp_parser_decl_specifier_seq
2013 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2014 static tree cp_parser_storage_class_specifier_opt
2016 static tree cp_parser_function_specifier_opt
2017 (cp_parser
*, cp_decl_specifier_seq
*);
2018 static tree cp_parser_type_specifier
2019 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2021 static tree cp_parser_simple_type_specifier
2022 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2023 static tree cp_parser_type_name
2025 static tree cp_parser_nonclass_name
2026 (cp_parser
* parser
);
2027 static tree cp_parser_elaborated_type_specifier
2028 (cp_parser
*, bool, bool);
2029 static tree cp_parser_enum_specifier
2031 static void cp_parser_enumerator_list
2032 (cp_parser
*, tree
);
2033 static void cp_parser_enumerator_definition
2034 (cp_parser
*, tree
);
2035 static tree cp_parser_namespace_name
2037 static void cp_parser_namespace_definition
2039 static void cp_parser_namespace_body
2041 static tree cp_parser_qualified_namespace_specifier
2043 static void cp_parser_namespace_alias_definition
2045 static bool cp_parser_using_declaration
2046 (cp_parser
*, bool);
2047 static void cp_parser_using_directive
2049 static tree cp_parser_alias_declaration
2051 static void cp_parser_asm_definition
2053 static void cp_parser_linkage_specification
2055 static void cp_parser_static_assert
2056 (cp_parser
*, bool);
2057 static tree cp_parser_decltype
2060 /* Declarators [gram.dcl.decl] */
2062 static tree cp_parser_init_declarator
2063 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*);
2064 static cp_declarator
*cp_parser_declarator
2065 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
2066 static cp_declarator
*cp_parser_direct_declarator
2067 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
2068 static enum tree_code cp_parser_ptr_operator
2069 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2070 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2072 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2074 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2076 static tree cp_parser_late_return_type_opt
2077 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2078 static tree cp_parser_declarator_id
2079 (cp_parser
*, bool);
2080 static tree cp_parser_type_id
2082 static tree cp_parser_template_type_arg
2084 static tree
cp_parser_trailing_type_id (cp_parser
*);
2085 static tree cp_parser_type_id_1
2086 (cp_parser
*, bool, bool);
2087 static void cp_parser_type_specifier_seq
2088 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2089 static tree cp_parser_parameter_declaration_clause
2091 static tree cp_parser_parameter_declaration_list
2092 (cp_parser
*, bool *);
2093 static cp_parameter_declarator
*cp_parser_parameter_declaration
2094 (cp_parser
*, bool, bool *);
2095 static tree cp_parser_default_argument
2096 (cp_parser
*, bool);
2097 static void cp_parser_function_body
2098 (cp_parser
*, bool);
2099 static tree cp_parser_initializer
2100 (cp_parser
*, bool *, bool *);
2101 static tree cp_parser_initializer_clause
2102 (cp_parser
*, bool *);
2103 static tree cp_parser_braced_list
2104 (cp_parser
*, bool*);
2105 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2106 (cp_parser
*, bool *);
2108 static bool cp_parser_ctor_initializer_opt_and_function_body
2109 (cp_parser
*, bool);
2111 static tree cp_parser_late_parsing_omp_declare_simd
2112 (cp_parser
*, tree
);
2114 static tree synthesize_implicit_template_parm
2116 static tree finish_fully_implicit_template
2117 (cp_parser
*, tree
);
2119 /* Classes [gram.class] */
2121 static tree cp_parser_class_name
2122 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2123 static tree cp_parser_class_specifier
2125 static tree cp_parser_class_head
2126 (cp_parser
*, bool *);
2127 static enum tag_types cp_parser_class_key
2129 static void cp_parser_member_specification_opt
2131 static void cp_parser_member_declaration
2133 static tree cp_parser_pure_specifier
2135 static tree cp_parser_constant_initializer
2138 /* Derived classes [gram.class.derived] */
2140 static tree cp_parser_base_clause
2142 static tree cp_parser_base_specifier
2145 /* Special member functions [gram.special] */
2147 static tree cp_parser_conversion_function_id
2149 static tree cp_parser_conversion_type_id
2151 static cp_declarator
*cp_parser_conversion_declarator_opt
2153 static bool cp_parser_ctor_initializer_opt
2155 static void cp_parser_mem_initializer_list
2157 static tree cp_parser_mem_initializer
2159 static tree cp_parser_mem_initializer_id
2162 /* Overloading [gram.over] */
2164 static tree cp_parser_operator_function_id
2166 static tree cp_parser_operator
2169 /* Templates [gram.temp] */
2171 static void cp_parser_template_declaration
2172 (cp_parser
*, bool);
2173 static tree cp_parser_template_parameter_list
2175 static tree cp_parser_template_parameter
2176 (cp_parser
*, bool *, bool *);
2177 static tree cp_parser_type_parameter
2178 (cp_parser
*, bool *);
2179 static tree cp_parser_template_id
2180 (cp_parser
*, bool, bool, enum tag_types
, bool);
2181 static tree cp_parser_template_name
2182 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2183 static tree cp_parser_template_argument_list
2185 static tree cp_parser_template_argument
2187 static void cp_parser_explicit_instantiation
2189 static void cp_parser_explicit_specialization
2192 /* Exception handling [gram.exception] */
2194 static tree cp_parser_try_block
2196 static bool cp_parser_function_try_block
2198 static void cp_parser_handler_seq
2200 static void cp_parser_handler
2202 static tree cp_parser_exception_declaration
2204 static tree cp_parser_throw_expression
2206 static tree cp_parser_exception_specification_opt
2208 static tree cp_parser_type_id_list
2211 /* GNU Extensions */
2213 static tree cp_parser_asm_specification_opt
2215 static tree cp_parser_asm_operand_list
2217 static tree cp_parser_asm_clobber_list
2219 static tree cp_parser_asm_label_list
2221 static bool cp_next_tokens_can_be_attribute_p
2223 static bool cp_next_tokens_can_be_gnu_attribute_p
2225 static bool cp_next_tokens_can_be_std_attribute_p
2227 static bool cp_nth_tokens_can_be_std_attribute_p
2228 (cp_parser
*, size_t);
2229 static bool cp_nth_tokens_can_be_gnu_attribute_p
2230 (cp_parser
*, size_t);
2231 static bool cp_nth_tokens_can_be_attribute_p
2232 (cp_parser
*, size_t);
2233 static tree cp_parser_attributes_opt
2235 static tree cp_parser_gnu_attributes_opt
2237 static tree cp_parser_gnu_attribute_list
2239 static tree cp_parser_std_attribute
2241 static tree cp_parser_std_attribute_spec
2243 static tree cp_parser_std_attribute_spec_seq
2245 static bool cp_parser_extension_opt
2246 (cp_parser
*, int *);
2247 static void cp_parser_label_declaration
2250 /* Transactional Memory Extensions */
2252 static tree cp_parser_transaction
2253 (cp_parser
*, enum rid
);
2254 static tree cp_parser_transaction_expression
2255 (cp_parser
*, enum rid
);
2256 static bool cp_parser_function_transaction
2257 (cp_parser
*, enum rid
);
2258 static tree cp_parser_transaction_cancel
2261 enum pragma_context
{
2268 static bool cp_parser_pragma
2269 (cp_parser
*, enum pragma_context
);
2271 /* Objective-C++ Productions */
2273 static tree cp_parser_objc_message_receiver
2275 static tree cp_parser_objc_message_args
2277 static tree cp_parser_objc_message_expression
2279 static tree cp_parser_objc_encode_expression
2281 static tree cp_parser_objc_defs_expression
2283 static tree cp_parser_objc_protocol_expression
2285 static tree cp_parser_objc_selector_expression
2287 static tree cp_parser_objc_expression
2289 static bool cp_parser_objc_selector_p
2291 static tree cp_parser_objc_selector
2293 static tree cp_parser_objc_protocol_refs_opt
2295 static void cp_parser_objc_declaration
2296 (cp_parser
*, tree
);
2297 static tree cp_parser_objc_statement
2299 static bool cp_parser_objc_valid_prefix_attributes
2300 (cp_parser
*, tree
*);
2301 static void cp_parser_objc_at_property_declaration
2303 static void cp_parser_objc_at_synthesize_declaration
2305 static void cp_parser_objc_at_dynamic_declaration
2307 static tree cp_parser_objc_struct_declaration
2310 /* Utility Routines */
2312 static tree cp_parser_lookup_name
2313 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2314 static tree cp_parser_lookup_name_simple
2315 (cp_parser
*, tree
, location_t
);
2316 static tree cp_parser_maybe_treat_template_as_class
2318 static bool cp_parser_check_declarator_template_parameters
2319 (cp_parser
*, cp_declarator
*, location_t
);
2320 static bool cp_parser_check_template_parameters
2321 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2322 static tree cp_parser_simple_cast_expression
2324 static tree cp_parser_global_scope_opt
2325 (cp_parser
*, bool);
2326 static bool cp_parser_constructor_declarator_p
2327 (cp_parser
*, bool);
2328 static tree cp_parser_function_definition_from_specifiers_and_declarator
2329 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2330 static tree cp_parser_function_definition_after_declarator
2331 (cp_parser
*, bool);
2332 static void cp_parser_template_declaration_after_export
2333 (cp_parser
*, bool);
2334 static void cp_parser_perform_template_parameter_access_checks
2335 (vec
<deferred_access_check
, va_gc
> *);
2336 static tree cp_parser_single_declaration
2337 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2338 static tree cp_parser_functional_cast
2339 (cp_parser
*, tree
);
2340 static tree cp_parser_save_member_function_body
2341 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2342 static tree cp_parser_save_nsdmi
2344 static tree cp_parser_enclosed_template_argument_list
2346 static void cp_parser_save_default_args
2347 (cp_parser
*, tree
);
2348 static void cp_parser_late_parsing_for_member
2349 (cp_parser
*, tree
);
2350 static tree cp_parser_late_parse_one_default_arg
2351 (cp_parser
*, tree
, tree
, tree
);
2352 static void cp_parser_late_parsing_nsdmi
2353 (cp_parser
*, tree
);
2354 static void cp_parser_late_parsing_default_args
2355 (cp_parser
*, tree
);
2356 static tree cp_parser_sizeof_operand
2357 (cp_parser
*, enum rid
);
2358 static tree cp_parser_trait_expr
2359 (cp_parser
*, enum rid
);
2360 static bool cp_parser_declares_only_class_p
2362 static void cp_parser_set_storage_class
2363 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2364 static void cp_parser_set_decl_spec_type
2365 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2366 static void set_and_check_decl_spec_loc
2367 (cp_decl_specifier_seq
*decl_specs
,
2368 cp_decl_spec ds
, cp_token
*);
2369 static bool cp_parser_friend_p
2370 (const cp_decl_specifier_seq
*);
2371 static void cp_parser_required_error
2372 (cp_parser
*, required_token
, bool);
2373 static cp_token
*cp_parser_require
2374 (cp_parser
*, enum cpp_ttype
, required_token
);
2375 static cp_token
*cp_parser_require_keyword
2376 (cp_parser
*, enum rid
, required_token
);
2377 static bool cp_parser_token_starts_function_definition_p
2379 static bool cp_parser_next_token_starts_class_definition_p
2381 static bool cp_parser_next_token_ends_template_argument_p
2383 static bool cp_parser_nth_token_starts_template_argument_list_p
2384 (cp_parser
*, size_t);
2385 static enum tag_types cp_parser_token_is_class_key
2387 static void cp_parser_check_class_key
2388 (enum tag_types
, tree type
);
2389 static void cp_parser_check_access_in_redeclaration
2390 (tree type
, location_t location
);
2391 static bool cp_parser_optional_template_keyword
2393 static void cp_parser_pre_parsed_nested_name_specifier
2395 static bool cp_parser_cache_group
2396 (cp_parser
*, enum cpp_ttype
, unsigned);
2397 static tree cp_parser_cache_defarg
2398 (cp_parser
*parser
, bool nsdmi
);
2399 static void cp_parser_parse_tentatively
2401 static void cp_parser_commit_to_tentative_parse
2403 static void cp_parser_commit_to_topmost_tentative_parse
2405 static void cp_parser_abort_tentative_parse
2407 static bool cp_parser_parse_definitely
2409 static inline bool cp_parser_parsing_tentatively
2411 static bool cp_parser_uncommitted_to_tentative_parse_p
2413 static void cp_parser_error
2414 (cp_parser
*, const char *);
2415 static void cp_parser_name_lookup_error
2416 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2417 static bool cp_parser_simulate_error
2419 static bool cp_parser_check_type_definition
2421 static void cp_parser_check_for_definition_in_return_type
2422 (cp_declarator
*, tree
, location_t type_location
);
2423 static void cp_parser_check_for_invalid_template_id
2424 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2425 static bool cp_parser_non_integral_constant_expression
2426 (cp_parser
*, non_integral_constant
);
2427 static void cp_parser_diagnose_invalid_type_name
2428 (cp_parser
*, tree
, tree
, location_t
);
2429 static bool cp_parser_parse_and_diagnose_invalid_type_name
2431 static int cp_parser_skip_to_closing_parenthesis
2432 (cp_parser
*, bool, bool, bool);
2433 static void cp_parser_skip_to_end_of_statement
2435 static void cp_parser_consume_semicolon_at_end_of_statement
2437 static void cp_parser_skip_to_end_of_block_or_statement
2439 static bool cp_parser_skip_to_closing_brace
2441 static void cp_parser_skip_to_end_of_template_parameter_list
2443 static void cp_parser_skip_to_pragma_eol
2444 (cp_parser
*, cp_token
*);
2445 static bool cp_parser_error_occurred
2447 static bool cp_parser_allow_gnu_extensions_p
2449 static bool cp_parser_is_pure_string_literal
2451 static bool cp_parser_is_string_literal
2453 static bool cp_parser_is_keyword
2454 (cp_token
*, enum rid
);
2455 static tree cp_parser_make_typename_type
2456 (cp_parser
*, tree
, tree
, location_t location
);
2457 static cp_declarator
* cp_parser_make_indirect_declarator
2458 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2460 /* Returns nonzero if we are parsing tentatively. */
2463 cp_parser_parsing_tentatively (cp_parser
* parser
)
2465 return parser
->context
->next
!= NULL
;
2468 /* Returns nonzero if TOKEN is a string literal. */
2471 cp_parser_is_pure_string_literal (cp_token
* token
)
2473 return (token
->type
== CPP_STRING
||
2474 token
->type
== CPP_STRING16
||
2475 token
->type
== CPP_STRING32
||
2476 token
->type
== CPP_WSTRING
||
2477 token
->type
== CPP_UTF8STRING
);
2480 /* Returns nonzero if TOKEN is a string literal
2481 of a user-defined string literal. */
2484 cp_parser_is_string_literal (cp_token
* token
)
2486 return (cp_parser_is_pure_string_literal (token
) ||
2487 token
->type
== CPP_STRING_USERDEF
||
2488 token
->type
== CPP_STRING16_USERDEF
||
2489 token
->type
== CPP_STRING32_USERDEF
||
2490 token
->type
== CPP_WSTRING_USERDEF
||
2491 token
->type
== CPP_UTF8STRING_USERDEF
);
2494 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2497 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2499 return token
->keyword
== keyword
;
2502 /* If not parsing tentatively, issue a diagnostic of the form
2503 FILE:LINE: MESSAGE before TOKEN
2504 where TOKEN is the next token in the input stream. MESSAGE
2505 (specified by the caller) is usually of the form "expected
2509 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2511 if (!cp_parser_simulate_error (parser
))
2513 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2514 /* This diagnostic makes more sense if it is tagged to the line
2515 of the token we just peeked at. */
2516 cp_lexer_set_source_position_from_token (token
);
2518 if (token
->type
== CPP_PRAGMA
)
2520 error_at (token
->location
,
2521 "%<#pragma%> is not allowed here");
2522 cp_parser_skip_to_pragma_eol (parser
, token
);
2526 c_parse_error (gmsgid
,
2527 /* Because c_parser_error does not understand
2528 CPP_KEYWORD, keywords are treated like
2530 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2531 token
->u
.value
, token
->flags
);
2535 /* Issue an error about name-lookup failing. NAME is the
2536 IDENTIFIER_NODE DECL is the result of
2537 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2538 the thing that we hoped to find. */
2541 cp_parser_name_lookup_error (cp_parser
* parser
,
2544 name_lookup_error desired
,
2545 location_t location
)
2547 /* If name lookup completely failed, tell the user that NAME was not
2549 if (decl
== error_mark_node
)
2551 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2552 error_at (location
, "%<%E::%E%> has not been declared",
2553 parser
->scope
, name
);
2554 else if (parser
->scope
== global_namespace
)
2555 error_at (location
, "%<::%E%> has not been declared", name
);
2556 else if (parser
->object_scope
2557 && !CLASS_TYPE_P (parser
->object_scope
))
2558 error_at (location
, "request for member %qE in non-class type %qT",
2559 name
, parser
->object_scope
);
2560 else if (parser
->object_scope
)
2561 error_at (location
, "%<%T::%E%> has not been declared",
2562 parser
->object_scope
, name
);
2564 error_at (location
, "%qE has not been declared", name
);
2566 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2571 error_at (location
, "%<%E::%E%> is not a type",
2572 parser
->scope
, name
);
2575 error_at (location
, "%<%E::%E%> is not a class or namespace",
2576 parser
->scope
, name
);
2580 "%<%E::%E%> is not a class, namespace, or enumeration",
2581 parser
->scope
, name
);
2588 else if (parser
->scope
== global_namespace
)
2593 error_at (location
, "%<::%E%> is not a type", name
);
2596 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2600 "%<::%E%> is not a class, namespace, or enumeration",
2612 error_at (location
, "%qE is not a type", name
);
2615 error_at (location
, "%qE is not a class or namespace", name
);
2619 "%qE is not a class, namespace, or enumeration", name
);
2627 /* If we are parsing tentatively, remember that an error has occurred
2628 during this tentative parse. Returns true if the error was
2629 simulated; false if a message should be issued by the caller. */
2632 cp_parser_simulate_error (cp_parser
* parser
)
2634 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2636 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2642 /* This function is called when a type is defined. If type
2643 definitions are forbidden at this point, an error message is
2647 cp_parser_check_type_definition (cp_parser
* parser
)
2649 /* If types are forbidden here, issue a message. */
2650 if (parser
->type_definition_forbidden_message
)
2652 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2653 in the message need to be interpreted. */
2654 error (parser
->type_definition_forbidden_message
);
2660 /* This function is called when the DECLARATOR is processed. The TYPE
2661 was a type defined in the decl-specifiers. If it is invalid to
2662 define a type in the decl-specifiers for DECLARATOR, an error is
2663 issued. TYPE_LOCATION is the location of TYPE and is used
2664 for error reporting. */
2667 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2668 tree type
, location_t type_location
)
2670 /* [dcl.fct] forbids type definitions in return types.
2671 Unfortunately, it's not easy to know whether or not we are
2672 processing a return type until after the fact. */
2674 && (declarator
->kind
== cdk_pointer
2675 || declarator
->kind
== cdk_reference
2676 || declarator
->kind
== cdk_ptrmem
))
2677 declarator
= declarator
->declarator
;
2679 && declarator
->kind
== cdk_function
)
2681 error_at (type_location
,
2682 "new types may not be defined in a return type");
2683 inform (type_location
,
2684 "(perhaps a semicolon is missing after the definition of %qT)",
2689 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2690 "<" in any valid C++ program. If the next token is indeed "<",
2691 issue a message warning the user about what appears to be an
2692 invalid attempt to form a template-id. LOCATION is the location
2693 of the type-specifier (TYPE) */
2696 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2698 enum tag_types tag_type
,
2699 location_t location
)
2701 cp_token_position start
= 0;
2703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2706 error_at (location
, "%qT is not a template", type
);
2707 else if (identifier_p (type
))
2709 if (tag_type
!= none_type
)
2710 error_at (location
, "%qE is not a class template", type
);
2712 error_at (location
, "%qE is not a template", type
);
2715 error_at (location
, "invalid template-id");
2716 /* Remember the location of the invalid "<". */
2717 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2718 start
= cp_lexer_token_position (parser
->lexer
, true);
2719 /* Consume the "<". */
2720 cp_lexer_consume_token (parser
->lexer
);
2721 /* Parse the template arguments. */
2722 cp_parser_enclosed_template_argument_list (parser
);
2723 /* Permanently remove the invalid template arguments so that
2724 this error message is not issued again. */
2726 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2730 /* If parsing an integral constant-expression, issue an error message
2731 about the fact that THING appeared and return true. Otherwise,
2732 return false. In either case, set
2733 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2736 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2737 non_integral_constant thing
)
2739 parser
->non_integral_constant_expression_p
= true;
2740 if (parser
->integral_constant_expression_p
)
2742 if (!parser
->allow_non_integral_constant_expression_p
)
2744 const char *msg
= NULL
;
2748 error ("floating-point literal "
2749 "cannot appear in a constant-expression");
2752 error ("a cast to a type other than an integral or "
2753 "enumeration type cannot appear in a "
2754 "constant-expression");
2757 error ("%<typeid%> operator "
2758 "cannot appear in a constant-expression");
2761 error ("non-constant compound literals "
2762 "cannot appear in a constant-expression");
2765 error ("a function call "
2766 "cannot appear in a constant-expression");
2769 error ("an increment "
2770 "cannot appear in a constant-expression");
2773 error ("an decrement "
2774 "cannot appear in a constant-expression");
2777 error ("an array reference "
2778 "cannot appear in a constant-expression");
2780 case NIC_ADDR_LABEL
:
2781 error ("the address of a label "
2782 "cannot appear in a constant-expression");
2784 case NIC_OVERLOADED
:
2785 error ("calls to overloaded operators "
2786 "cannot appear in a constant-expression");
2788 case NIC_ASSIGNMENT
:
2789 error ("an assignment cannot appear in a constant-expression");
2792 error ("a comma operator "
2793 "cannot appear in a constant-expression");
2795 case NIC_CONSTRUCTOR
:
2796 error ("a call to a constructor "
2797 "cannot appear in a constant-expression");
2799 case NIC_TRANSACTION
:
2800 error ("a transaction expression "
2801 "cannot appear in a constant-expression");
2807 msg
= "__FUNCTION__";
2809 case NIC_PRETTY_FUNC
:
2810 msg
= "__PRETTY_FUNCTION__";
2830 case NIC_PREINCREMENT
:
2833 case NIC_PREDECREMENT
:
2846 error ("%qs cannot appear in a constant-expression", msg
);
2853 /* Emit a diagnostic for an invalid type name. SCOPE is the
2854 qualifying scope (or NULL, if none) for ID. This function commits
2855 to the current active tentative parse, if any. (Otherwise, the
2856 problematic construct might be encountered again later, resulting
2857 in duplicate error messages.) LOCATION is the location of ID. */
2860 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2861 tree scope
, tree id
,
2862 location_t location
)
2864 tree decl
, old_scope
;
2865 cp_parser_commit_to_tentative_parse (parser
);
2866 /* Try to lookup the identifier. */
2867 old_scope
= parser
->scope
;
2868 parser
->scope
= scope
;
2869 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2870 parser
->scope
= old_scope
;
2871 /* If the lookup found a template-name, it means that the user forgot
2872 to specify an argument list. Emit a useful error message. */
2873 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2875 "invalid use of template-name %qE without an argument list",
2877 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2878 error_at (location
, "invalid use of destructor %qD as a type", id
);
2879 else if (TREE_CODE (decl
) == TYPE_DECL
)
2880 /* Something like 'unsigned A a;' */
2881 error_at (location
, "invalid combination of multiple type-specifiers");
2882 else if (!parser
->scope
)
2884 /* Issue an error message. */
2885 error_at (location
, "%qE does not name a type", id
);
2886 /* If we're in a template class, it's possible that the user was
2887 referring to a type from a base class. For example:
2889 template <typename T> struct A { typedef T X; };
2890 template <typename T> struct B : public A<T> { X x; };
2892 The user should have said "typename A<T>::X". */
2893 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2894 inform (location
, "C++11 %<constexpr%> only available with "
2895 "-std=c++11 or -std=gnu++11");
2896 else if (processing_template_decl
&& current_class_type
2897 && TYPE_BINFO (current_class_type
))
2901 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2905 tree base_type
= BINFO_TYPE (b
);
2906 if (CLASS_TYPE_P (base_type
)
2907 && dependent_type_p (base_type
))
2910 /* Go from a particular instantiation of the
2911 template (which will have an empty TYPE_FIELDs),
2912 to the main version. */
2913 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2914 for (field
= TYPE_FIELDS (base_type
);
2916 field
= DECL_CHAIN (field
))
2917 if (TREE_CODE (field
) == TYPE_DECL
2918 && DECL_NAME (field
) == id
)
2921 "(perhaps %<typename %T::%E%> was intended)",
2922 BINFO_TYPE (b
), id
);
2931 /* Here we diagnose qualified-ids where the scope is actually correct,
2932 but the identifier does not resolve to a valid type name. */
2933 else if (parser
->scope
!= error_mark_node
)
2935 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2938 error_at (location_of (id
),
2939 "%qE in namespace %qE does not name a template type",
2942 error_at (location_of (id
),
2943 "%qE in namespace %qE does not name a type",
2946 else if (CLASS_TYPE_P (parser
->scope
)
2947 && constructor_name_p (id
, parser
->scope
))
2950 error_at (location
, "%<%T::%E%> names the constructor, not"
2951 " the type", parser
->scope
, id
);
2952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2953 error_at (location
, "and %qT has no template constructors",
2956 else if (TYPE_P (parser
->scope
)
2957 && dependent_scope_p (parser
->scope
))
2958 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2959 "%qT is a dependent scope",
2960 parser
->scope
, id
, parser
->scope
);
2961 else if (TYPE_P (parser
->scope
))
2963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2964 error_at (location_of (id
),
2965 "%qE in %q#T does not name a template type",
2968 error_at (location_of (id
),
2969 "%qE in %q#T does not name a type",
2977 /* Check for a common situation where a type-name should be present,
2978 but is not, and issue a sensible error message. Returns true if an
2979 invalid type-name was detected.
2981 The situation handled by this function are variable declarations of the
2982 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2983 Usually, `ID' should name a type, but if we got here it means that it
2984 does not. We try to emit the best possible error message depending on
2985 how exactly the id-expression looks like. */
2988 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2991 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2993 /* Avoid duplicate error about ambiguous lookup. */
2994 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2996 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2997 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
3001 cp_parser_parse_tentatively (parser
);
3002 id
= cp_parser_id_expression (parser
,
3003 /*template_keyword_p=*/false,
3004 /*check_dependency_p=*/true,
3005 /*template_p=*/NULL
,
3006 /*declarator_p=*/true,
3007 /*optional_p=*/false);
3008 /* If the next token is a (, this is a function with no explicit return
3009 type, i.e. constructor, destructor or conversion op. */
3010 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3011 || TREE_CODE (id
) == TYPE_DECL
)
3013 cp_parser_abort_tentative_parse (parser
);
3016 if (!cp_parser_parse_definitely (parser
))
3019 /* Emit a diagnostic for the invalid type. */
3020 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
3021 id
, token
->location
);
3023 /* If we aren't in the middle of a declarator (i.e. in a
3024 parameter-declaration-clause), skip to the end of the declaration;
3025 there's no point in trying to process it. */
3026 if (!parser
->in_declarator_p
)
3027 cp_parser_skip_to_end_of_block_or_statement (parser
);
3031 /* Consume tokens up to, and including, the next non-nested closing `)'.
3032 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3033 are doing error recovery. Returns -1 if OR_COMMA is true and we
3034 found an unnested comma. */
3037 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3042 unsigned paren_depth
= 0;
3043 unsigned brace_depth
= 0;
3044 unsigned square_depth
= 0;
3046 if (recovering
&& !or_comma
3047 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3052 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3054 switch (token
->type
)
3057 case CPP_PRAGMA_EOL
:
3058 /* If we've run out of tokens, then there is no closing `)'. */
3061 /* This is good for lambda expression capture-lists. */
3062 case CPP_OPEN_SQUARE
:
3065 case CPP_CLOSE_SQUARE
:
3066 if (!square_depth
--)
3071 /* This matches the processing in skip_to_end_of_statement. */
3076 case CPP_OPEN_BRACE
:
3079 case CPP_CLOSE_BRACE
:
3085 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3090 case CPP_OPEN_PAREN
:
3095 case CPP_CLOSE_PAREN
:
3096 if (!brace_depth
&& !paren_depth
--)
3099 cp_lexer_consume_token (parser
->lexer
);
3108 /* Consume the token. */
3109 cp_lexer_consume_token (parser
->lexer
);
3113 /* Consume tokens until we reach the end of the current statement.
3114 Normally, that will be just before consuming a `;'. However, if a
3115 non-nested `}' comes first, then we stop before consuming that. */
3118 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3120 unsigned nesting_depth
= 0;
3124 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3126 switch (token
->type
)
3129 case CPP_PRAGMA_EOL
:
3130 /* If we've run out of tokens, stop. */
3134 /* If the next token is a `;', we have reached the end of the
3140 case CPP_CLOSE_BRACE
:
3141 /* If this is a non-nested '}', stop before consuming it.
3142 That way, when confronted with something like:
3146 we stop before consuming the closing '}', even though we
3147 have not yet reached a `;'. */
3148 if (nesting_depth
== 0)
3151 /* If it is the closing '}' for a block that we have
3152 scanned, stop -- but only after consuming the token.
3158 we will stop after the body of the erroneously declared
3159 function, but before consuming the following `typedef'
3161 if (--nesting_depth
== 0)
3163 cp_lexer_consume_token (parser
->lexer
);
3167 case CPP_OPEN_BRACE
:
3175 /* Consume the token. */
3176 cp_lexer_consume_token (parser
->lexer
);
3180 /* This function is called at the end of a statement or declaration.
3181 If the next token is a semicolon, it is consumed; otherwise, error
3182 recovery is attempted. */
3185 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3187 /* Look for the trailing `;'. */
3188 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3190 /* If there is additional (erroneous) input, skip to the end of
3192 cp_parser_skip_to_end_of_statement (parser
);
3193 /* If the next token is now a `;', consume it. */
3194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3195 cp_lexer_consume_token (parser
->lexer
);
3199 /* Skip tokens until we have consumed an entire block, or until we
3200 have consumed a non-nested `;'. */
3203 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3205 int nesting_depth
= 0;
3207 while (nesting_depth
>= 0)
3209 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3211 switch (token
->type
)
3214 case CPP_PRAGMA_EOL
:
3215 /* If we've run out of tokens, stop. */
3219 /* Stop if this is an unnested ';'. */
3224 case CPP_CLOSE_BRACE
:
3225 /* Stop if this is an unnested '}', or closes the outermost
3228 if (nesting_depth
< 0)
3234 case CPP_OPEN_BRACE
:
3243 /* Consume the token. */
3244 cp_lexer_consume_token (parser
->lexer
);
3248 /* Skip tokens until a non-nested closing curly brace is the next
3249 token, or there are no more tokens. Return true in the first case,
3253 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3255 unsigned nesting_depth
= 0;
3259 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3261 switch (token
->type
)
3264 case CPP_PRAGMA_EOL
:
3265 /* If we've run out of tokens, stop. */
3268 case CPP_CLOSE_BRACE
:
3269 /* If the next token is a non-nested `}', then we have reached
3270 the end of the current block. */
3271 if (nesting_depth
-- == 0)
3275 case CPP_OPEN_BRACE
:
3276 /* If it the next token is a `{', then we are entering a new
3277 block. Consume the entire block. */
3285 /* Consume the token. */
3286 cp_lexer_consume_token (parser
->lexer
);
3290 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3291 parameter is the PRAGMA token, allowing us to purge the entire pragma
3295 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3299 parser
->lexer
->in_pragma
= false;
3302 token
= cp_lexer_consume_token (parser
->lexer
);
3303 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3305 /* Ensure that the pragma is not parsed again. */
3306 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3309 /* Require pragma end of line, resyncing with it as necessary. The
3310 arguments are as for cp_parser_skip_to_pragma_eol. */
3313 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3315 parser
->lexer
->in_pragma
= false;
3316 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3317 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3320 /* This is a simple wrapper around make_typename_type. When the id is
3321 an unresolved identifier node, we can provide a superior diagnostic
3322 using cp_parser_diagnose_invalid_type_name. */
3325 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3326 tree id
, location_t id_location
)
3329 if (identifier_p (id
))
3331 result
= make_typename_type (scope
, id
, typename_type
,
3332 /*complain=*/tf_none
);
3333 if (result
== error_mark_node
)
3334 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3337 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3340 /* This is a wrapper around the
3341 make_{pointer,ptrmem,reference}_declarator functions that decides
3342 which one to call based on the CODE and CLASS_TYPE arguments. The
3343 CODE argument should be one of the values returned by
3344 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3345 appertain to the pointer or reference. */
3347 static cp_declarator
*
3348 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3349 cp_cv_quals cv_qualifiers
,
3350 cp_declarator
*target
,
3353 if (code
== ERROR_MARK
)
3354 return cp_error_declarator
;
3356 if (code
== INDIRECT_REF
)
3357 if (class_type
== NULL_TREE
)
3358 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3360 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3361 target
, attributes
);
3362 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3363 return make_reference_declarator (cv_qualifiers
, target
,
3365 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3366 return make_reference_declarator (cv_qualifiers
, target
,
3371 /* Create a new C++ parser. */
3374 cp_parser_new (void)
3380 /* cp_lexer_new_main is called before doing GC allocation because
3381 cp_lexer_new_main might load a PCH file. */
3382 lexer
= cp_lexer_new_main ();
3384 /* Initialize the binops_by_token so that we can get the tree
3385 directly from the token. */
3386 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3387 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3389 parser
= ggc_alloc_cleared_cp_parser ();
3390 parser
->lexer
= lexer
;
3391 parser
->context
= cp_parser_context_new (NULL
);
3393 /* For now, we always accept GNU extensions. */
3394 parser
->allow_gnu_extensions_p
= 1;
3396 /* The `>' token is a greater-than operator, not the end of a
3398 parser
->greater_than_is_operator_p
= true;
3400 parser
->default_arg_ok_p
= true;
3402 /* We are not parsing a constant-expression. */
3403 parser
->integral_constant_expression_p
= false;
3404 parser
->allow_non_integral_constant_expression_p
= false;
3405 parser
->non_integral_constant_expression_p
= false;
3407 /* Local variable names are not forbidden. */
3408 parser
->local_variables_forbidden_p
= false;
3410 /* We are not processing an `extern "C"' declaration. */
3411 parser
->in_unbraced_linkage_specification_p
= false;
3413 /* We are not processing a declarator. */
3414 parser
->in_declarator_p
= false;
3416 /* We are not processing a template-argument-list. */
3417 parser
->in_template_argument_list_p
= false;
3419 /* We are not in an iteration statement. */
3420 parser
->in_statement
= 0;
3422 /* We are not in a switch statement. */
3423 parser
->in_switch_statement_p
= false;
3425 /* We are not parsing a type-id inside an expression. */
3426 parser
->in_type_id_in_expr_p
= false;
3428 /* Declarations aren't implicitly extern "C". */
3429 parser
->implicit_extern_c
= false;
3431 /* String literals should be translated to the execution character set. */
3432 parser
->translate_strings_p
= true;
3434 /* We are not parsing a function body. */
3435 parser
->in_function_body
= false;
3437 /* We can correct until told otherwise. */
3438 parser
->colon_corrects_to_scope_p
= true;
3440 /* The unparsed function queue is empty. */
3441 push_unparsed_function_queues (parser
);
3443 /* There are no classes being defined. */
3444 parser
->num_classes_being_defined
= 0;
3446 /* No template parameters apply. */
3447 parser
->num_template_parameter_lists
= 0;
3449 /* Not declaring an implicit function template. */
3450 parser
->auto_is_implicit_function_template_parm_p
= false;
3451 parser
->fully_implicit_function_template_p
= false;
3452 parser
->implicit_template_parms
= 0;
3453 parser
->implicit_template_scope
= 0;
3458 /* Create a cp_lexer structure which will emit the tokens in CACHE
3459 and push it onto the parser's lexer stack. This is used for delayed
3460 parsing of in-class method bodies and default arguments, and should
3461 not be confused with tentative parsing. */
3463 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3465 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3466 lexer
->next
= parser
->lexer
;
3467 parser
->lexer
= lexer
;
3469 /* Move the current source position to that of the first token in the
3471 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3474 /* Pop the top lexer off the parser stack. This is never used for the
3475 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3477 cp_parser_pop_lexer (cp_parser
*parser
)
3479 cp_lexer
*lexer
= parser
->lexer
;
3480 parser
->lexer
= lexer
->next
;
3481 cp_lexer_destroy (lexer
);
3483 /* Put the current source position back where it was before this
3484 lexer was pushed. */
3485 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3488 /* Lexical conventions [gram.lex] */
3490 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3494 cp_parser_identifier (cp_parser
* parser
)
3498 /* Look for the identifier. */
3499 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3500 /* Return the value. */
3501 return token
? token
->u
.value
: error_mark_node
;
3504 /* Parse a sequence of adjacent string constants. Returns a
3505 TREE_STRING representing the combined, nul-terminated string
3506 constant. If TRANSLATE is true, translate the string to the
3507 execution character set. If WIDE_OK is true, a wide string is
3510 C++98 [lex.string] says that if a narrow string literal token is
3511 adjacent to a wide string literal token, the behavior is undefined.
3512 However, C99 6.4.5p4 says that this results in a wide string literal.
3513 We follow C99 here, for consistency with the C front end.
3515 This code is largely lifted from lex_string() in c-lex.c.
3517 FUTURE: ObjC++ will need to handle @-strings here. */
3519 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3523 struct obstack str_ob
;
3524 cpp_string str
, istr
, *strs
;
3526 enum cpp_ttype type
, curr_type
;
3527 int have_suffix_p
= 0;
3529 tree suffix_id
= NULL_TREE
;
3530 bool curr_tok_is_userdef_p
= false;
3532 tok
= cp_lexer_peek_token (parser
->lexer
);
3533 if (!cp_parser_is_string_literal (tok
))
3535 cp_parser_error (parser
, "expected string-literal");
3536 return error_mark_node
;
3539 if (cpp_userdef_string_p (tok
->type
))
3541 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3542 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3543 curr_tok_is_userdef_p
= true;
3547 string_tree
= tok
->u
.value
;
3548 curr_type
= tok
->type
;
3552 /* Try to avoid the overhead of creating and destroying an obstack
3553 for the common case of just one string. */
3554 if (!cp_parser_is_string_literal
3555 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3557 cp_lexer_consume_token (parser
->lexer
);
3559 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3560 str
.len
= TREE_STRING_LENGTH (string_tree
);
3563 if (curr_tok_is_userdef_p
)
3565 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3567 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3570 curr_type
= tok
->type
;
3576 gcc_obstack_init (&str_ob
);
3581 cp_lexer_consume_token (parser
->lexer
);
3583 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3584 str
.len
= TREE_STRING_LENGTH (string_tree
);
3586 if (curr_tok_is_userdef_p
)
3588 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3589 if (have_suffix_p
== 0)
3591 suffix_id
= curr_suffix_id
;
3594 else if (have_suffix_p
== 1
3595 && curr_suffix_id
!= suffix_id
)
3597 error ("inconsistent user-defined literal suffixes"
3598 " %qD and %qD in string literal",
3599 suffix_id
, curr_suffix_id
);
3602 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3605 curr_type
= tok
->type
;
3607 if (type
!= curr_type
)
3609 if (type
== CPP_STRING
)
3611 else if (curr_type
!= CPP_STRING
)
3612 error_at (tok
->location
,
3613 "unsupported non-standard concatenation "
3614 "of string literals");
3617 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3619 tok
= cp_lexer_peek_token (parser
->lexer
);
3620 if (cpp_userdef_string_p (tok
->type
))
3622 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3623 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3624 curr_tok_is_userdef_p
= true;
3628 string_tree
= tok
->u
.value
;
3629 curr_type
= tok
->type
;
3630 curr_tok_is_userdef_p
= false;
3633 while (cp_parser_is_string_literal (tok
));
3635 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3638 if (type
!= CPP_STRING
&& !wide_ok
)
3640 cp_parser_error (parser
, "a wide string is invalid in this context");
3644 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3645 (parse_in
, strs
, count
, &istr
, type
))
3647 value
= build_string (istr
.len
, (const char *)istr
.text
);
3648 free (CONST_CAST (unsigned char *, istr
.text
));
3654 case CPP_UTF8STRING
:
3655 TREE_TYPE (value
) = char_array_type_node
;
3658 TREE_TYPE (value
) = char16_array_type_node
;
3661 TREE_TYPE (value
) = char32_array_type_node
;
3664 TREE_TYPE (value
) = wchar_array_type_node
;
3668 value
= fix_string_type (value
);
3672 tree literal
= build_userdef_literal (suffix_id
, value
,
3673 OT_NONE
, NULL_TREE
);
3674 tok
->u
.value
= literal
;
3675 return cp_parser_userdef_string_literal (tok
);
3679 /* cpp_interpret_string has issued an error. */
3680 value
= error_mark_node
;
3683 obstack_free (&str_ob
, 0);
3688 /* Look up a literal operator with the name and the exact arguments. */
3691 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3694 decl
= lookup_name (name
);
3695 if (!decl
|| !is_overloaded_fn (decl
))
3696 return error_mark_node
;
3698 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3702 tree fn
= OVL_CURRENT (fns
);
3703 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3704 if (parmtypes
!= NULL_TREE
)
3706 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3707 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3709 tree tparm
= TREE_VALUE (parmtypes
);
3710 tree targ
= TREE_TYPE ((*args
)[ix
]);
3711 bool ptr
= TYPE_PTR_P (tparm
);
3712 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3713 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3715 || !same_type_p (TREE_TYPE (tparm
),
3720 && ix
== vec_safe_length (args
)
3721 /* May be this should be sufficient_parms_p instead,
3722 depending on how exactly should user-defined literals
3723 work in presence of default arguments on the literal
3724 operator parameters. */
3725 && parmtypes
== void_list_node
)
3730 return error_mark_node
;
3733 /* Parse a user-defined char constant. Returns a call to a user-defined
3734 literal operator taking the character as an argument. */
3737 cp_parser_userdef_char_literal (cp_parser
*parser
)
3739 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3740 tree literal
= token
->u
.value
;
3741 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3742 tree value
= USERDEF_LITERAL_VALUE (literal
);
3743 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3746 /* Build up a call to the user-defined operator */
3747 /* Lookup the name we got back from the id-expression. */
3748 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3749 vec_safe_push (args
, value
);
3750 decl
= lookup_literal_operator (name
, args
);
3751 if (!decl
|| decl
== error_mark_node
)
3753 error ("unable to find character literal operator %qD with %qT argument",
3754 name
, TREE_TYPE (value
));
3755 release_tree_vector (args
);
3756 return error_mark_node
;
3758 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3759 release_tree_vector (args
);
3760 if (result
!= error_mark_node
)
3763 error ("unable to find character literal operator %qD with %qT argument",
3764 name
, TREE_TYPE (value
));
3765 return error_mark_node
;
3768 /* A subroutine of cp_parser_userdef_numeric_literal to
3769 create a char... template parameter pack from a string node. */
3772 make_char_string_pack (tree value
)
3775 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3776 const char *str
= TREE_STRING_POINTER (value
);
3777 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3778 tree argvec
= make_tree_vec (1);
3780 /* Fill in CHARVEC with all of the parameters. */
3781 charvec
= make_tree_vec (len
);
3782 for (i
= 0; i
< len
; ++i
)
3783 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3785 /* Build the argument packs. */
3786 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3787 TREE_TYPE (argpack
) = char_type_node
;
3789 TREE_VEC_ELT (argvec
, 0) = argpack
;
3794 /* A subroutine of cp_parser_userdef_numeric_literal to
3795 create a char... template parameter pack from a string node. */
3798 make_string_pack (tree value
)
3801 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3802 const char *str
= TREE_STRING_POINTER (value
);
3803 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3804 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3805 tree argvec
= make_tree_vec (2);
3807 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3808 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3810 /* First template parm is character type. */
3811 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3813 /* Fill in CHARVEC with all of the parameters. */
3814 charvec
= make_tree_vec (len
);
3817 for (int i
= 0; i
< len
; ++i
)
3818 TREE_VEC_ELT (charvec
, i
) = build_int_cst (str_char_type_node
, str
[i
]);
3822 const uint16_t *num
= (const uint16_t *)str
;
3823 for (int i
= 0; i
< len
; ++i
)
3824 TREE_VEC_ELT (charvec
, i
) = build_int_cst (str_char_type_node
, num
[i
]);
3828 const uint32_t *num
= (const uint32_t *)str
;
3829 for (int i
= 0; i
< len
; ++i
)
3830 TREE_VEC_ELT (charvec
, i
) = build_int_cst (str_char_type_node
, num
[i
]);
3833 /* Build the argument packs. */
3834 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3835 TREE_TYPE (argpack
) = str_char_type_node
;
3837 TREE_VEC_ELT (argvec
, 1) = argpack
;
3842 /* Parse a user-defined numeric constant. returns a call to a user-defined
3843 literal operator. */
3846 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3848 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3849 tree literal
= token
->u
.value
;
3850 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3851 tree value
= USERDEF_LITERAL_VALUE (literal
);
3852 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3853 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3854 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3856 vec
<tree
, va_gc
> *args
;
3858 /* Look for a literal operator taking the exact type of numeric argument
3859 as the literal value. */
3860 args
= make_tree_vector ();
3861 vec_safe_push (args
, value
);
3862 decl
= lookup_literal_operator (name
, args
);
3863 if (decl
&& decl
!= error_mark_node
)
3865 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3866 if (result
!= error_mark_node
)
3868 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3869 warning_at (token
->location
, OPT_Woverflow
,
3870 "integer literal exceeds range of %qT type",
3871 long_long_unsigned_type_node
);
3875 warning_at (token
->location
, OPT_Woverflow
,
3876 "floating literal exceeds range of %qT type",
3877 long_double_type_node
);
3878 else if (overflow
< 0)
3879 warning_at (token
->location
, OPT_Woverflow
,
3880 "floating literal truncated to zero");
3882 release_tree_vector (args
);
3886 release_tree_vector (args
);
3888 /* If the numeric argument didn't work, look for a raw literal
3889 operator taking a const char* argument consisting of the number
3890 in string format. */
3891 args
= make_tree_vector ();
3892 vec_safe_push (args
, num_string
);
3893 decl
= lookup_literal_operator (name
, args
);
3894 if (decl
&& decl
!= error_mark_node
)
3896 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3897 if (result
!= error_mark_node
)
3899 release_tree_vector (args
);
3903 release_tree_vector (args
);
3905 /* If the raw literal didn't work, look for a non-type template
3906 function with parameter pack char.... Call the function with
3907 template parameter characters representing the number. */
3908 args
= make_tree_vector ();
3909 decl
= lookup_literal_operator (name
, args
);
3910 if (decl
&& decl
!= error_mark_node
)
3912 tree tmpl_args
= make_char_string_pack (num_string
);
3913 decl
= lookup_template_function (decl
, tmpl_args
);
3914 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3915 if (result
!= error_mark_node
)
3917 release_tree_vector (args
);
3921 release_tree_vector (args
);
3923 error ("unable to find numeric literal operator %qD", name
);
3924 return error_mark_node
;
3927 /* Parse a user-defined string constant. Returns a call to a user-defined
3928 literal operator taking a character pointer and the length of the string
3932 cp_parser_userdef_string_literal (cp_token
*token
)
3934 tree literal
= token
->u
.value
;
3935 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3936 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3937 tree value
= USERDEF_LITERAL_VALUE (literal
);
3938 int len
= TREE_STRING_LENGTH (value
)
3939 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
3941 vec
<tree
, va_gc
> *args
;
3943 /* Look for a template function with typename parameter CharT
3944 and parameter pack CharT... Call the function with
3945 template parameter characters representing the string. */
3946 args
= make_tree_vector ();
3947 decl
= lookup_literal_operator (name
, args
);
3948 if (decl
&& decl
!= error_mark_node
)
3950 tree tmpl_args
= make_string_pack (value
);
3951 decl
= lookup_template_function (decl
, tmpl_args
);
3952 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3953 if (result
!= error_mark_node
)
3955 release_tree_vector (args
);
3959 release_tree_vector (args
);
3961 /* Build up a call to the user-defined operator */
3962 /* Lookup the name we got back from the id-expression. */
3963 args
= make_tree_vector ();
3964 vec_safe_push (args
, value
);
3965 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
3966 decl
= lookup_name (name
);
3967 if (!decl
|| decl
== error_mark_node
)
3969 error ("unable to find string literal operator %qD", name
);
3970 release_tree_vector (args
);
3971 return error_mark_node
;
3973 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3974 release_tree_vector (args
);
3975 if (result
!= error_mark_node
)
3978 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3979 name
, TREE_TYPE (value
), size_type_node
);
3980 return error_mark_node
;
3984 /* Basic concepts [gram.basic] */
3986 /* Parse a translation-unit.
3989 declaration-seq [opt]
3991 Returns TRUE if all went well. */
3994 cp_parser_translation_unit (cp_parser
* parser
)
3996 /* The address of the first non-permanent object on the declarator
3998 static void *declarator_obstack_base
;
4002 /* Create the declarator obstack, if necessary. */
4003 if (!cp_error_declarator
)
4005 gcc_obstack_init (&declarator_obstack
);
4006 /* Create the error declarator. */
4007 cp_error_declarator
= make_declarator (cdk_error
);
4008 /* Create the empty parameter list. */
4009 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4010 /* Remember where the base of the declarator obstack lies. */
4011 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4014 cp_parser_declaration_seq_opt (parser
);
4016 /* If there are no tokens left then all went well. */
4017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4019 /* Get rid of the token array; we don't need it any more. */
4020 cp_lexer_destroy (parser
->lexer
);
4021 parser
->lexer
= NULL
;
4023 /* This file might have been a context that's implicitly extern
4024 "C". If so, pop the lang context. (Only relevant for PCH.) */
4025 if (parser
->implicit_extern_c
)
4027 pop_lang_context ();
4028 parser
->implicit_extern_c
= false;
4032 finish_translation_unit ();
4038 cp_parser_error (parser
, "expected declaration");
4042 /* Make sure the declarator obstack was fully cleaned up. */
4043 gcc_assert (obstack_next_free (&declarator_obstack
)
4044 == declarator_obstack_base
);
4046 /* All went well. */
4050 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4051 decltype context. */
4053 static inline tsubst_flags_t
4054 complain_flags (bool decltype_p
)
4056 tsubst_flags_t complain
= tf_warning_or_error
;
4058 complain
|= tf_decltype
;
4062 /* Expressions [gram.expr] */
4064 /* Parse a primary-expression.
4075 ( compound-statement )
4076 __builtin_va_arg ( assignment-expression , type-id )
4077 __builtin_offsetof ( type-id , offsetof-expression )
4080 __has_nothrow_assign ( type-id )
4081 __has_nothrow_constructor ( type-id )
4082 __has_nothrow_copy ( type-id )
4083 __has_trivial_assign ( type-id )
4084 __has_trivial_constructor ( type-id )
4085 __has_trivial_copy ( type-id )
4086 __has_trivial_destructor ( type-id )
4087 __has_virtual_destructor ( type-id )
4088 __is_abstract ( type-id )
4089 __is_base_of ( type-id , type-id )
4090 __is_class ( type-id )
4091 __is_convertible_to ( type-id , type-id )
4092 __is_empty ( type-id )
4093 __is_enum ( type-id )
4094 __is_final ( type-id )
4095 __is_literal_type ( type-id )
4096 __is_pod ( type-id )
4097 __is_polymorphic ( type-id )
4098 __is_std_layout ( type-id )
4099 __is_trivial ( type-id )
4100 __is_union ( type-id )
4102 Objective-C++ Extension:
4110 ADDRESS_P is true iff this expression was immediately preceded by
4111 "&" and therefore might denote a pointer-to-member. CAST_P is true
4112 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4113 true iff this expression is a template argument.
4115 Returns a representation of the expression. Upon return, *IDK
4116 indicates what kind of id-expression (if any) was present. */
4119 cp_parser_primary_expression (cp_parser
*parser
,
4122 bool template_arg_p
,
4126 cp_token
*token
= NULL
;
4128 /* Assume the primary expression is not an id-expression. */
4129 *idk
= CP_ID_KIND_NONE
;
4131 /* Peek at the next token. */
4132 token
= cp_lexer_peek_token (parser
->lexer
);
4133 switch (token
->type
)
4142 user-defined-literal */
4148 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4149 return cp_parser_userdef_numeric_literal (parser
);
4150 token
= cp_lexer_consume_token (parser
->lexer
);
4151 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4153 error_at (token
->location
,
4154 "fixed-point types not supported in C++");
4155 return error_mark_node
;
4157 /* Floating-point literals are only allowed in an integral
4158 constant expression if they are cast to an integral or
4159 enumeration type. */
4160 if (TREE_CODE (token
->u
.value
) == REAL_CST
4161 && parser
->integral_constant_expression_p
4164 /* CAST_P will be set even in invalid code like "int(2.7 +
4165 ...)". Therefore, we have to check that the next token
4166 is sure to end the cast. */
4169 cp_token
*next_token
;
4171 next_token
= cp_lexer_peek_token (parser
->lexer
);
4172 if (/* The comma at the end of an
4173 enumerator-definition. */
4174 next_token
->type
!= CPP_COMMA
4175 /* The curly brace at the end of an enum-specifier. */
4176 && next_token
->type
!= CPP_CLOSE_BRACE
4177 /* The end of a statement. */
4178 && next_token
->type
!= CPP_SEMICOLON
4179 /* The end of the cast-expression. */
4180 && next_token
->type
!= CPP_CLOSE_PAREN
4181 /* The end of an array bound. */
4182 && next_token
->type
!= CPP_CLOSE_SQUARE
4183 /* The closing ">" in a template-argument-list. */
4184 && (next_token
->type
!= CPP_GREATER
4185 || parser
->greater_than_is_operator_p
)
4186 /* C++0x only: A ">>" treated like two ">" tokens,
4187 in a template-argument-list. */
4188 && (next_token
->type
!= CPP_RSHIFT
4189 || (cxx_dialect
== cxx98
)
4190 || parser
->greater_than_is_operator_p
))
4194 /* If we are within a cast, then the constraint that the
4195 cast is to an integral or enumeration type will be
4196 checked at that point. If we are not within a cast, then
4197 this code is invalid. */
4199 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4201 return token
->u
.value
;
4203 case CPP_CHAR_USERDEF
:
4204 case CPP_CHAR16_USERDEF
:
4205 case CPP_CHAR32_USERDEF
:
4206 case CPP_WCHAR_USERDEF
:
4207 return cp_parser_userdef_char_literal (parser
);
4213 case CPP_UTF8STRING
:
4214 case CPP_STRING_USERDEF
:
4215 case CPP_STRING16_USERDEF
:
4216 case CPP_STRING32_USERDEF
:
4217 case CPP_WSTRING_USERDEF
:
4218 case CPP_UTF8STRING_USERDEF
:
4219 /* ??? Should wide strings be allowed when parser->translate_strings_p
4220 is false (i.e. in attributes)? If not, we can kill the third
4221 argument to cp_parser_string_literal. */
4222 return cp_parser_string_literal (parser
,
4223 parser
->translate_strings_p
,
4226 case CPP_OPEN_PAREN
:
4229 bool saved_greater_than_is_operator_p
;
4231 /* Consume the `('. */
4232 cp_lexer_consume_token (parser
->lexer
);
4233 /* Within a parenthesized expression, a `>' token is always
4234 the greater-than operator. */
4235 saved_greater_than_is_operator_p
4236 = parser
->greater_than_is_operator_p
;
4237 parser
->greater_than_is_operator_p
= true;
4238 /* If we see `( { ' then we are looking at the beginning of
4239 a GNU statement-expression. */
4240 if (cp_parser_allow_gnu_extensions_p (parser
)
4241 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
4243 /* Statement-expressions are not allowed by the standard. */
4244 pedwarn (token
->location
, OPT_Wpedantic
,
4245 "ISO C++ forbids braced-groups within expressions");
4247 /* And they're not allowed outside of a function-body; you
4248 cannot, for example, write:
4250 int i = ({ int j = 3; j + 1; });
4252 at class or namespace scope. */
4253 if (!parser
->in_function_body
4254 || parser
->in_template_argument_list_p
)
4256 error_at (token
->location
,
4257 "statement-expressions are not allowed outside "
4258 "functions nor in template-argument lists");
4259 cp_parser_skip_to_end_of_block_or_statement (parser
);
4260 expr
= error_mark_node
;
4264 /* Start the statement-expression. */
4265 expr
= begin_stmt_expr ();
4266 /* Parse the compound-statement. */
4267 cp_parser_compound_statement (parser
, expr
, false, false);
4269 expr
= finish_stmt_expr (expr
, false);
4274 /* Parse the parenthesized expression. */
4275 expr
= cp_parser_expression (parser
, cast_p
, decltype_p
, idk
);
4276 /* Let the front end know that this expression was
4277 enclosed in parentheses. This matters in case, for
4278 example, the expression is of the form `A::B', since
4279 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4281 expr
= finish_parenthesized_expr (expr
);
4282 /* DR 705: Wrapping an unqualified name in parentheses
4283 suppresses arg-dependent lookup. We want to pass back
4284 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4285 (c++/37862), but none of the others. */
4286 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4287 *idk
= CP_ID_KIND_NONE
;
4289 /* The `>' token might be the end of a template-id or
4290 template-parameter-list now. */
4291 parser
->greater_than_is_operator_p
4292 = saved_greater_than_is_operator_p
;
4293 /* Consume the `)'. */
4294 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4295 cp_parser_skip_to_end_of_statement (parser
);
4300 case CPP_OPEN_SQUARE
:
4301 if (c_dialect_objc ())
4302 /* We have an Objective-C++ message. */
4303 return cp_parser_objc_expression (parser
);
4305 tree lam
= cp_parser_lambda_expression (parser
);
4306 /* Don't warn about a failed tentative parse. */
4307 if (cp_parser_error_occurred (parser
))
4308 return error_mark_node
;
4309 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4313 case CPP_OBJC_STRING
:
4314 if (c_dialect_objc ())
4315 /* We have an Objective-C++ string literal. */
4316 return cp_parser_objc_expression (parser
);
4317 cp_parser_error (parser
, "expected primary-expression");
4318 return error_mark_node
;
4321 switch (token
->keyword
)
4323 /* These two are the boolean literals. */
4325 cp_lexer_consume_token (parser
->lexer
);
4326 return boolean_true_node
;
4328 cp_lexer_consume_token (parser
->lexer
);
4329 return boolean_false_node
;
4331 /* The `__null' literal. */
4333 cp_lexer_consume_token (parser
->lexer
);
4336 /* The `nullptr' literal. */
4338 cp_lexer_consume_token (parser
->lexer
);
4339 return nullptr_node
;
4341 /* Recognize the `this' keyword. */
4343 cp_lexer_consume_token (parser
->lexer
);
4344 if (parser
->local_variables_forbidden_p
)
4346 error_at (token
->location
,
4347 "%<this%> may not be used in this context");
4348 return error_mark_node
;
4350 /* Pointers cannot appear in constant-expressions. */
4351 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4352 return error_mark_node
;
4353 return finish_this_expr ();
4355 /* The `operator' keyword can be the beginning of an
4360 case RID_FUNCTION_NAME
:
4361 case RID_PRETTY_FUNCTION_NAME
:
4362 case RID_C99_FUNCTION_NAME
:
4364 non_integral_constant name
;
4366 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4367 __func__ are the names of variables -- but they are
4368 treated specially. Therefore, they are handled here,
4369 rather than relying on the generic id-expression logic
4370 below. Grammatically, these names are id-expressions.
4372 Consume the token. */
4373 token
= cp_lexer_consume_token (parser
->lexer
);
4375 switch (token
->keyword
)
4377 case RID_FUNCTION_NAME
:
4378 name
= NIC_FUNC_NAME
;
4380 case RID_PRETTY_FUNCTION_NAME
:
4381 name
= NIC_PRETTY_FUNC
;
4383 case RID_C99_FUNCTION_NAME
:
4384 name
= NIC_C99_FUNC
;
4390 if (cp_parser_non_integral_constant_expression (parser
, name
))
4391 return error_mark_node
;
4393 /* Look up the name. */
4394 return finish_fname (token
->u
.value
);
4401 source_location type_location
;
4403 /* The `__builtin_va_arg' construct is used to handle
4404 `va_arg'. Consume the `__builtin_va_arg' token. */
4405 cp_lexer_consume_token (parser
->lexer
);
4406 /* Look for the opening `('. */
4407 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4408 /* Now, parse the assignment-expression. */
4409 expression
= cp_parser_assignment_expression (parser
,
4410 /*cast_p=*/false, NULL
);
4411 /* Look for the `,'. */
4412 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4413 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4414 /* Parse the type-id. */
4415 type
= cp_parser_type_id (parser
);
4416 /* Look for the closing `)'. */
4417 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4418 /* Using `va_arg' in a constant-expression is not
4420 if (cp_parser_non_integral_constant_expression (parser
,
4422 return error_mark_node
;
4423 return build_x_va_arg (type_location
, expression
, type
);
4427 return cp_parser_builtin_offsetof (parser
);
4429 case RID_HAS_NOTHROW_ASSIGN
:
4430 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4431 case RID_HAS_NOTHROW_COPY
:
4432 case RID_HAS_TRIVIAL_ASSIGN
:
4433 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4434 case RID_HAS_TRIVIAL_COPY
:
4435 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4436 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4437 case RID_IS_ABSTRACT
:
4438 case RID_IS_BASE_OF
:
4440 case RID_IS_CONVERTIBLE_TO
:
4444 case RID_IS_LITERAL_TYPE
:
4446 case RID_IS_POLYMORPHIC
:
4447 case RID_IS_STD_LAYOUT
:
4448 case RID_IS_TRIVIAL
:
4450 return cp_parser_trait_expr (parser
, token
->keyword
);
4452 /* Objective-C++ expressions. */
4454 case RID_AT_PROTOCOL
:
4455 case RID_AT_SELECTOR
:
4456 return cp_parser_objc_expression (parser
);
4459 if (parser
->in_function_body
4460 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4463 error_at (token
->location
,
4464 "a template declaration cannot appear at block scope");
4465 cp_parser_skip_to_end_of_block_or_statement (parser
);
4466 return error_mark_node
;
4469 cp_parser_error (parser
, "expected primary-expression");
4470 return error_mark_node
;
4473 /* An id-expression can start with either an identifier, a
4474 `::' as the beginning of a qualified-id, or the "operator"
4478 case CPP_TEMPLATE_ID
:
4479 case CPP_NESTED_NAME_SPECIFIER
:
4483 const char *error_msg
;
4486 cp_token
*id_expr_token
;
4489 /* Parse the id-expression. */
4491 = cp_parser_id_expression (parser
,
4492 /*template_keyword_p=*/false,
4493 /*check_dependency_p=*/true,
4495 /*declarator_p=*/false,
4496 /*optional_p=*/false);
4497 if (id_expression
== error_mark_node
)
4498 return error_mark_node
;
4499 id_expr_token
= token
;
4500 token
= cp_lexer_peek_token (parser
->lexer
);
4501 done
= (token
->type
!= CPP_OPEN_SQUARE
4502 && token
->type
!= CPP_OPEN_PAREN
4503 && token
->type
!= CPP_DOT
4504 && token
->type
!= CPP_DEREF
4505 && token
->type
!= CPP_PLUS_PLUS
4506 && token
->type
!= CPP_MINUS_MINUS
);
4507 /* If we have a template-id, then no further lookup is
4508 required. If the template-id was for a template-class, we
4509 will sometimes have a TYPE_DECL at this point. */
4510 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4511 || TREE_CODE (id_expression
) == TYPE_DECL
)
4512 decl
= id_expression
;
4513 /* Look up the name. */
4516 tree ambiguous_decls
;
4518 /* If we already know that this lookup is ambiguous, then
4519 we've already issued an error message; there's no reason
4521 if (id_expr_token
->type
== CPP_NAME
4522 && id_expr_token
->ambiguous_p
)
4524 cp_parser_simulate_error (parser
);
4525 return error_mark_node
;
4528 decl
= cp_parser_lookup_name (parser
, id_expression
,
4531 /*is_namespace=*/false,
4532 /*check_dependency=*/true,
4534 id_expr_token
->location
);
4535 /* If the lookup was ambiguous, an error will already have
4537 if (ambiguous_decls
)
4538 return error_mark_node
;
4540 /* In Objective-C++, we may have an Objective-C 2.0
4541 dot-syntax for classes here. */
4542 if (c_dialect_objc ()
4543 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4544 && TREE_CODE (decl
) == TYPE_DECL
4545 && objc_is_class_name (decl
))
4548 cp_lexer_consume_token (parser
->lexer
);
4549 component
= cp_parser_identifier (parser
);
4550 if (component
== error_mark_node
)
4551 return error_mark_node
;
4553 return objc_build_class_component_ref (id_expression
, component
);
4556 /* In Objective-C++, an instance variable (ivar) may be preferred
4557 to whatever cp_parser_lookup_name() found. */
4558 decl
= objc_lookup_ivar (decl
, id_expression
);
4560 /* If name lookup gives us a SCOPE_REF, then the
4561 qualifying scope was dependent. */
4562 if (TREE_CODE (decl
) == SCOPE_REF
)
4564 /* At this point, we do not know if DECL is a valid
4565 integral constant expression. We assume that it is
4566 in fact such an expression, so that code like:
4568 template <int N> struct A {
4572 is accepted. At template-instantiation time, we
4573 will check that B<N>::i is actually a constant. */
4576 /* Check to see if DECL is a local variable in a context
4577 where that is forbidden. */
4578 if (parser
->local_variables_forbidden_p
4579 && local_variable_p (decl
))
4581 /* It might be that we only found DECL because we are
4582 trying to be generous with pre-ISO scoping rules.
4583 For example, consider:
4587 for (int i = 0; i < 10; ++i) {}
4588 extern void f(int j = i);
4591 Here, name look up will originally find the out
4592 of scope `i'. We need to issue a warning message,
4593 but then use the global `i'. */
4594 decl
= check_for_out_of_scope_variable (decl
);
4595 if (local_variable_p (decl
))
4597 error_at (id_expr_token
->location
,
4598 "local variable %qD may not appear in this context",
4600 return error_mark_node
;
4605 decl
= (finish_id_expression
4606 (id_expression
, decl
, parser
->scope
,
4608 parser
->integral_constant_expression_p
,
4609 parser
->allow_non_integral_constant_expression_p
,
4610 &parser
->non_integral_constant_expression_p
,
4611 template_p
, done
, address_p
,
4614 id_expr_token
->location
));
4616 cp_parser_error (parser
, error_msg
);
4620 /* Anything else is an error. */
4622 cp_parser_error (parser
, "expected primary-expression");
4623 return error_mark_node
;
4628 cp_parser_primary_expression (cp_parser
*parser
,
4631 bool template_arg_p
,
4634 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4635 /*decltype*/false, idk
);
4638 /* Parse an id-expression.
4645 :: [opt] nested-name-specifier template [opt] unqualified-id
4647 :: operator-function-id
4650 Return a representation of the unqualified portion of the
4651 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4652 a `::' or nested-name-specifier.
4654 Often, if the id-expression was a qualified-id, the caller will
4655 want to make a SCOPE_REF to represent the qualified-id. This
4656 function does not do this in order to avoid wastefully creating
4657 SCOPE_REFs when they are not required.
4659 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4662 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4663 uninstantiated templates.
4665 If *TEMPLATE_P is non-NULL, it is set to true iff the
4666 `template' keyword is used to explicitly indicate that the entity
4667 named is a template.
4669 If DECLARATOR_P is true, the id-expression is appearing as part of
4670 a declarator, rather than as part of an expression. */
4673 cp_parser_id_expression (cp_parser
*parser
,
4674 bool template_keyword_p
,
4675 bool check_dependency_p
,
4680 bool global_scope_p
;
4681 bool nested_name_specifier_p
;
4683 /* Assume the `template' keyword was not used. */
4685 *template_p
= template_keyword_p
;
4687 /* Look for the optional `::' operator. */
4689 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4691 /* Look for the optional nested-name-specifier. */
4692 nested_name_specifier_p
4693 = (cp_parser_nested_name_specifier_opt (parser
,
4694 /*typename_keyword_p=*/false,
4699 /* If there is a nested-name-specifier, then we are looking at
4700 the first qualified-id production. */
4701 if (nested_name_specifier_p
)
4704 tree saved_object_scope
;
4705 tree saved_qualifying_scope
;
4706 tree unqualified_id
;
4709 /* See if the next token is the `template' keyword. */
4711 template_p
= &is_template
;
4712 *template_p
= cp_parser_optional_template_keyword (parser
);
4713 /* Name lookup we do during the processing of the
4714 unqualified-id might obliterate SCOPE. */
4715 saved_scope
= parser
->scope
;
4716 saved_object_scope
= parser
->object_scope
;
4717 saved_qualifying_scope
= parser
->qualifying_scope
;
4718 /* Process the final unqualified-id. */
4719 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4722 /*optional_p=*/false);
4723 /* Restore the SAVED_SCOPE for our caller. */
4724 parser
->scope
= saved_scope
;
4725 parser
->object_scope
= saved_object_scope
;
4726 parser
->qualifying_scope
= saved_qualifying_scope
;
4728 return unqualified_id
;
4730 /* Otherwise, if we are in global scope, then we are looking at one
4731 of the other qualified-id productions. */
4732 else if (global_scope_p
)
4737 /* Peek at the next token. */
4738 token
= cp_lexer_peek_token (parser
->lexer
);
4740 /* If it's an identifier, and the next token is not a "<", then
4741 we can avoid the template-id case. This is an optimization
4742 for this common case. */
4743 if (token
->type
== CPP_NAME
4744 && !cp_parser_nth_token_starts_template_argument_list_p
4746 return cp_parser_identifier (parser
);
4748 cp_parser_parse_tentatively (parser
);
4749 /* Try a template-id. */
4750 id
= cp_parser_template_id (parser
,
4751 /*template_keyword_p=*/false,
4752 /*check_dependency_p=*/true,
4755 /* If that worked, we're done. */
4756 if (cp_parser_parse_definitely (parser
))
4759 /* Peek at the next token. (Changes in the token buffer may
4760 have invalidated the pointer obtained above.) */
4761 token
= cp_lexer_peek_token (parser
->lexer
);
4763 switch (token
->type
)
4766 return cp_parser_identifier (parser
);
4769 if (token
->keyword
== RID_OPERATOR
)
4770 return cp_parser_operator_function_id (parser
);
4774 cp_parser_error (parser
, "expected id-expression");
4775 return error_mark_node
;
4779 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4780 /*check_dependency_p=*/true,
4785 /* Parse an unqualified-id.
4789 operator-function-id
4790 conversion-function-id
4794 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4795 keyword, in a construct like `A::template ...'.
4797 Returns a representation of unqualified-id. For the `identifier'
4798 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4799 production a BIT_NOT_EXPR is returned; the operand of the
4800 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4801 other productions, see the documentation accompanying the
4802 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4803 names are looked up in uninstantiated templates. If DECLARATOR_P
4804 is true, the unqualified-id is appearing as part of a declarator,
4805 rather than as part of an expression. */
4808 cp_parser_unqualified_id (cp_parser
* parser
,
4809 bool template_keyword_p
,
4810 bool check_dependency_p
,
4816 /* Peek at the next token. */
4817 token
= cp_lexer_peek_token (parser
->lexer
);
4819 switch (token
->type
)
4825 /* We don't know yet whether or not this will be a
4827 cp_parser_parse_tentatively (parser
);
4828 /* Try a template-id. */
4829 id
= cp_parser_template_id (parser
, template_keyword_p
,
4833 /* If it worked, we're done. */
4834 if (cp_parser_parse_definitely (parser
))
4836 /* Otherwise, it's an ordinary identifier. */
4837 return cp_parser_identifier (parser
);
4840 case CPP_TEMPLATE_ID
:
4841 return cp_parser_template_id (parser
, template_keyword_p
,
4849 tree qualifying_scope
;
4854 /* Consume the `~' token. */
4855 cp_lexer_consume_token (parser
->lexer
);
4856 /* Parse the class-name. The standard, as written, seems to
4859 template <typename T> struct S { ~S (); };
4860 template <typename T> S<T>::~S() {}
4862 is invalid, since `~' must be followed by a class-name, but
4863 `S<T>' is dependent, and so not known to be a class.
4864 That's not right; we need to look in uninstantiated
4865 templates. A further complication arises from:
4867 template <typename T> void f(T t) {
4871 Here, it is not possible to look up `T' in the scope of `T'
4872 itself. We must look in both the current scope, and the
4873 scope of the containing complete expression.
4875 Yet another issue is:
4884 The standard does not seem to say that the `S' in `~S'
4885 should refer to the type `S' and not the data member
4888 /* DR 244 says that we look up the name after the "~" in the
4889 same scope as we looked up the qualifying name. That idea
4890 isn't fully worked out; it's more complicated than that. */
4891 scope
= parser
->scope
;
4892 object_scope
= parser
->object_scope
;
4893 qualifying_scope
= parser
->qualifying_scope
;
4895 /* Check for invalid scopes. */
4896 if (scope
== error_mark_node
)
4898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4899 cp_lexer_consume_token (parser
->lexer
);
4900 return error_mark_node
;
4902 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4904 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4905 error_at (token
->location
,
4906 "scope %qT before %<~%> is not a class-name",
4908 cp_parser_simulate_error (parser
);
4909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4910 cp_lexer_consume_token (parser
->lexer
);
4911 return error_mark_node
;
4913 gcc_assert (!scope
|| TYPE_P (scope
));
4915 /* If the name is of the form "X::~X" it's OK even if X is a
4917 token
= cp_lexer_peek_token (parser
->lexer
);
4919 && token
->type
== CPP_NAME
4920 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4922 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4923 || (CLASS_TYPE_P (scope
)
4924 && constructor_name_p (token
->u
.value
, scope
))))
4926 cp_lexer_consume_token (parser
->lexer
);
4927 return build_nt (BIT_NOT_EXPR
, scope
);
4930 /* ~auto means the destructor of whatever the object is. */
4931 if (cp_parser_is_keyword (token
, RID_AUTO
))
4933 if (cxx_dialect
< cxx1y
)
4934 pedwarn (input_location
, 0,
4935 "%<~auto%> only available with "
4936 "-std=c++1y or -std=gnu++1y");
4937 cp_lexer_consume_token (parser
->lexer
);
4938 return build_nt (BIT_NOT_EXPR
, make_auto ());
4941 /* If there was an explicit qualification (S::~T), first look
4942 in the scope given by the qualification (i.e., S).
4944 Note: in the calls to cp_parser_class_name below we pass
4945 typename_type so that lookup finds the injected-class-name
4946 rather than the constructor. */
4948 type_decl
= NULL_TREE
;
4951 cp_parser_parse_tentatively (parser
);
4952 type_decl
= cp_parser_class_name (parser
,
4953 /*typename_keyword_p=*/false,
4954 /*template_keyword_p=*/false,
4956 /*check_dependency=*/false,
4957 /*class_head_p=*/false,
4959 if (cp_parser_parse_definitely (parser
))
4962 /* In "N::S::~S", look in "N" as well. */
4963 if (!done
&& scope
&& qualifying_scope
)
4965 cp_parser_parse_tentatively (parser
);
4966 parser
->scope
= qualifying_scope
;
4967 parser
->object_scope
= NULL_TREE
;
4968 parser
->qualifying_scope
= NULL_TREE
;
4970 = cp_parser_class_name (parser
,
4971 /*typename_keyword_p=*/false,
4972 /*template_keyword_p=*/false,
4974 /*check_dependency=*/false,
4975 /*class_head_p=*/false,
4977 if (cp_parser_parse_definitely (parser
))
4980 /* In "p->S::~T", look in the scope given by "*p" as well. */
4981 else if (!done
&& object_scope
)
4983 cp_parser_parse_tentatively (parser
);
4984 parser
->scope
= object_scope
;
4985 parser
->object_scope
= NULL_TREE
;
4986 parser
->qualifying_scope
= NULL_TREE
;
4988 = cp_parser_class_name (parser
,
4989 /*typename_keyword_p=*/false,
4990 /*template_keyword_p=*/false,
4992 /*check_dependency=*/false,
4993 /*class_head_p=*/false,
4995 if (cp_parser_parse_definitely (parser
))
4998 /* Look in the surrounding context. */
5001 parser
->scope
= NULL_TREE
;
5002 parser
->object_scope
= NULL_TREE
;
5003 parser
->qualifying_scope
= NULL_TREE
;
5004 if (processing_template_decl
)
5005 cp_parser_parse_tentatively (parser
);
5007 = cp_parser_class_name (parser
,
5008 /*typename_keyword_p=*/false,
5009 /*template_keyword_p=*/false,
5011 /*check_dependency=*/false,
5012 /*class_head_p=*/false,
5014 if (processing_template_decl
5015 && ! cp_parser_parse_definitely (parser
))
5017 /* We couldn't find a type with this name, so just accept
5018 it and check for a match at instantiation time. */
5019 type_decl
= cp_parser_identifier (parser
);
5020 if (type_decl
!= error_mark_node
)
5021 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5025 /* If an error occurred, assume that the name of the
5026 destructor is the same as the name of the qualifying
5027 class. That allows us to keep parsing after running
5028 into ill-formed destructor names. */
5029 if (type_decl
== error_mark_node
&& scope
)
5030 return build_nt (BIT_NOT_EXPR
, scope
);
5031 else if (type_decl
== error_mark_node
)
5032 return error_mark_node
;
5034 /* Check that destructor name and scope match. */
5035 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5037 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5038 error_at (token
->location
,
5039 "declaration of %<~%T%> as member of %qT",
5041 cp_parser_simulate_error (parser
);
5042 return error_mark_node
;
5047 A typedef-name that names a class shall not be used as the
5048 identifier in the declarator for a destructor declaration. */
5050 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5051 && !DECL_SELF_REFERENCE_P (type_decl
)
5052 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5053 error_at (token
->location
,
5054 "typedef-name %qD used as destructor declarator",
5057 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5061 if (token
->keyword
== RID_OPERATOR
)
5065 /* This could be a template-id, so we try that first. */
5066 cp_parser_parse_tentatively (parser
);
5067 /* Try a template-id. */
5068 id
= cp_parser_template_id (parser
, template_keyword_p
,
5069 /*check_dependency_p=*/true,
5072 /* If that worked, we're done. */
5073 if (cp_parser_parse_definitely (parser
))
5075 /* We still don't know whether we're looking at an
5076 operator-function-id or a conversion-function-id. */
5077 cp_parser_parse_tentatively (parser
);
5078 /* Try an operator-function-id. */
5079 id
= cp_parser_operator_function_id (parser
);
5080 /* If that didn't work, try a conversion-function-id. */
5081 if (!cp_parser_parse_definitely (parser
))
5082 id
= cp_parser_conversion_function_id (parser
);
5083 else if (UDLIT_OPER_P (id
))
5086 const char *name
= UDLIT_OP_SUFFIX (id
);
5087 if (name
[0] != '_' && !in_system_header
&& declarator_p
)
5088 warning (0, "literal operator suffixes not preceded by %<_%>"
5089 " are reserved for future standardization");
5099 cp_parser_error (parser
, "expected unqualified-id");
5100 return error_mark_node
;
5104 /* Parse an (optional) nested-name-specifier.
5106 nested-name-specifier: [C++98]
5107 class-or-namespace-name :: nested-name-specifier [opt]
5108 class-or-namespace-name :: template nested-name-specifier [opt]
5110 nested-name-specifier: [C++0x]
5113 nested-name-specifier identifier ::
5114 nested-name-specifier template [opt] simple-template-id ::
5116 PARSER->SCOPE should be set appropriately before this function is
5117 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5118 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5121 Sets PARSER->SCOPE to the class (TYPE) or namespace
5122 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5123 it unchanged if there is no nested-name-specifier. Returns the new
5124 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5126 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5127 part of a declaration and/or decl-specifier. */
5130 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5131 bool typename_keyword_p
,
5132 bool check_dependency_p
,
5134 bool is_declaration
)
5136 bool success
= false;
5137 cp_token_position start
= 0;
5140 /* Remember where the nested-name-specifier starts. */
5141 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5143 start
= cp_lexer_token_position (parser
->lexer
, false);
5144 push_deferring_access_checks (dk_deferred
);
5151 tree saved_qualifying_scope
;
5152 bool template_keyword_p
;
5154 /* Spot cases that cannot be the beginning of a
5155 nested-name-specifier. */
5156 token
= cp_lexer_peek_token (parser
->lexer
);
5158 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5159 the already parsed nested-name-specifier. */
5160 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5162 /* Grab the nested-name-specifier and continue the loop. */
5163 cp_parser_pre_parsed_nested_name_specifier (parser
);
5164 /* If we originally encountered this nested-name-specifier
5165 with IS_DECLARATION set to false, we will not have
5166 resolved TYPENAME_TYPEs, so we must do so here. */
5168 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5170 new_scope
= resolve_typename_type (parser
->scope
,
5171 /*only_current_p=*/false);
5172 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5173 parser
->scope
= new_scope
;
5179 /* Spot cases that cannot be the beginning of a
5180 nested-name-specifier. On the second and subsequent times
5181 through the loop, we look for the `template' keyword. */
5182 if (success
&& token
->keyword
== RID_TEMPLATE
)
5184 /* A template-id can start a nested-name-specifier. */
5185 else if (token
->type
== CPP_TEMPLATE_ID
)
5187 /* DR 743: decltype can be used in a nested-name-specifier. */
5188 else if (token_is_decltype (token
))
5192 /* If the next token is not an identifier, then it is
5193 definitely not a type-name or namespace-name. */
5194 if (token
->type
!= CPP_NAME
)
5196 /* If the following token is neither a `<' (to begin a
5197 template-id), nor a `::', then we are not looking at a
5198 nested-name-specifier. */
5199 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5201 if (token
->type
== CPP_COLON
5202 && parser
->colon_corrects_to_scope_p
5203 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5205 error_at (token
->location
,
5206 "found %<:%> in nested-name-specifier, expected %<::%>");
5207 token
->type
= CPP_SCOPE
;
5210 if (token
->type
!= CPP_SCOPE
5211 && !cp_parser_nth_token_starts_template_argument_list_p
5216 /* The nested-name-specifier is optional, so we parse
5218 cp_parser_parse_tentatively (parser
);
5220 /* Look for the optional `template' keyword, if this isn't the
5221 first time through the loop. */
5223 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5225 template_keyword_p
= false;
5227 /* Save the old scope since the name lookup we are about to do
5228 might destroy it. */
5229 old_scope
= parser
->scope
;
5230 saved_qualifying_scope
= parser
->qualifying_scope
;
5231 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5232 look up names in "X<T>::I" in order to determine that "Y" is
5233 a template. So, if we have a typename at this point, we make
5234 an effort to look through it. */
5236 && !typename_keyword_p
5238 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5239 parser
->scope
= resolve_typename_type (parser
->scope
,
5240 /*only_current_p=*/false);
5241 /* Parse the qualifying entity. */
5243 = cp_parser_qualifying_entity (parser
,
5249 /* Look for the `::' token. */
5250 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5252 /* If we found what we wanted, we keep going; otherwise, we're
5254 if (!cp_parser_parse_definitely (parser
))
5256 bool error_p
= false;
5258 /* Restore the OLD_SCOPE since it was valid before the
5259 failed attempt at finding the last
5260 class-or-namespace-name. */
5261 parser
->scope
= old_scope
;
5262 parser
->qualifying_scope
= saved_qualifying_scope
;
5264 /* If the next token is a decltype, and the one after that is a
5265 `::', then the decltype has failed to resolve to a class or
5266 enumeration type. Give this error even when parsing
5267 tentatively since it can't possibly be valid--and we're going
5268 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5269 won't get another chance.*/
5270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5271 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5274 token
= cp_lexer_consume_token (parser
->lexer
);
5275 error_at (token
->location
, "decltype evaluates to %qT, "
5276 "which is not a class or enumeration type",
5278 parser
->scope
= error_mark_node
;
5282 cp_lexer_consume_token (parser
->lexer
);
5285 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5287 /* If the next token is an identifier, and the one after
5288 that is a `::', then any valid interpretation would have
5289 found a class-or-namespace-name. */
5290 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5291 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5293 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5296 token
= cp_lexer_consume_token (parser
->lexer
);
5299 if (!token
->ambiguous_p
)
5302 tree ambiguous_decls
;
5304 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5306 /*is_template=*/false,
5307 /*is_namespace=*/false,
5308 /*check_dependency=*/true,
5311 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5312 error_at (token
->location
,
5313 "%qD used without template parameters",
5315 else if (ambiguous_decls
)
5317 // cp_parser_lookup_name has the same diagnostic,
5318 // thus make sure to emit it at most once.
5319 if (cp_parser_uncommitted_to_tentative_parse_p
5322 error_at (token
->location
,
5323 "reference to %qD is ambiguous",
5325 print_candidates (ambiguous_decls
);
5327 decl
= error_mark_node
;
5331 if (cxx_dialect
!= cxx98
)
5332 cp_parser_name_lookup_error
5333 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5336 cp_parser_name_lookup_error
5337 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5341 parser
->scope
= error_mark_node
;
5343 /* Treat this as a successful nested-name-specifier
5348 If the name found is not a class-name (clause
5349 _class_) or namespace-name (_namespace.def_), the
5350 program is ill-formed. */
5353 cp_lexer_consume_token (parser
->lexer
);
5357 /* We've found one valid nested-name-specifier. */
5359 /* Name lookup always gives us a DECL. */
5360 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5361 new_scope
= TREE_TYPE (new_scope
);
5362 /* Uses of "template" must be followed by actual templates. */
5363 if (template_keyword_p
5364 && !(CLASS_TYPE_P (new_scope
)
5365 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5366 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5367 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5368 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5369 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5370 == TEMPLATE_ID_EXPR
)))
5371 permerror (input_location
, TYPE_P (new_scope
)
5372 ? G_("%qT is not a template")
5373 : G_("%qD is not a template"),
5375 /* If it is a class scope, try to complete it; we are about to
5376 be looking up names inside the class. */
5377 if (TYPE_P (new_scope
)
5378 /* Since checking types for dependency can be expensive,
5379 avoid doing it if the type is already complete. */
5380 && !COMPLETE_TYPE_P (new_scope
)
5381 /* Do not try to complete dependent types. */
5382 && !dependent_type_p (new_scope
))
5384 new_scope
= complete_type (new_scope
);
5385 /* If it is a typedef to current class, use the current
5386 class instead, as the typedef won't have any names inside
5388 if (!COMPLETE_TYPE_P (new_scope
)
5389 && currently_open_class (new_scope
))
5390 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5392 /* Make sure we look in the right scope the next time through
5394 parser
->scope
= new_scope
;
5397 /* If parsing tentatively, replace the sequence of tokens that makes
5398 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5399 token. That way, should we re-parse the token stream, we will
5400 not have to repeat the effort required to do the parse, nor will
5401 we issue duplicate error messages. */
5402 if (success
&& start
)
5406 token
= cp_lexer_token_at (parser
->lexer
, start
);
5407 /* Reset the contents of the START token. */
5408 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5409 /* Retrieve any deferred checks. Do not pop this access checks yet
5410 so the memory will not be reclaimed during token replacing below. */
5411 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
5412 token
->u
.tree_check_value
->value
= parser
->scope
;
5413 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5414 token
->u
.tree_check_value
->qualifying_scope
=
5415 parser
->qualifying_scope
;
5416 token
->keyword
= RID_MAX
;
5418 /* Purge all subsequent tokens. */
5419 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5423 pop_to_parent_deferring_access_checks ();
5425 return success
? parser
->scope
: NULL_TREE
;
5428 /* Parse a nested-name-specifier. See
5429 cp_parser_nested_name_specifier_opt for details. This function
5430 behaves identically, except that it will an issue an error if no
5431 nested-name-specifier is present. */
5434 cp_parser_nested_name_specifier (cp_parser
*parser
,
5435 bool typename_keyword_p
,
5436 bool check_dependency_p
,
5438 bool is_declaration
)
5442 /* Look for the nested-name-specifier. */
5443 scope
= cp_parser_nested_name_specifier_opt (parser
,
5448 /* If it was not present, issue an error message. */
5451 cp_parser_error (parser
, "expected nested-name-specifier");
5452 parser
->scope
= NULL_TREE
;
5458 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5459 this is either a class-name or a namespace-name (which corresponds
5460 to the class-or-namespace-name production in the grammar). For
5461 C++0x, it can also be a type-name that refers to an enumeration
5462 type or a simple-template-id.
5464 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5465 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5466 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5467 TYPE_P is TRUE iff the next name should be taken as a class-name,
5468 even the same name is declared to be another entity in the same
5471 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5472 specified by the class-or-namespace-name. If neither is found the
5473 ERROR_MARK_NODE is returned. */
5476 cp_parser_qualifying_entity (cp_parser
*parser
,
5477 bool typename_keyword_p
,
5478 bool template_keyword_p
,
5479 bool check_dependency_p
,
5481 bool is_declaration
)
5484 tree saved_qualifying_scope
;
5485 tree saved_object_scope
;
5488 bool successful_parse_p
;
5490 /* DR 743: decltype can appear in a nested-name-specifier. */
5491 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5493 scope
= cp_parser_decltype (parser
);
5494 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5495 && !MAYBE_CLASS_TYPE_P (scope
))
5497 cp_parser_simulate_error (parser
);
5498 return error_mark_node
;
5500 if (TYPE_NAME (scope
))
5501 scope
= TYPE_NAME (scope
);
5505 /* Before we try to parse the class-name, we must save away the
5506 current PARSER->SCOPE since cp_parser_class_name will destroy
5508 saved_scope
= parser
->scope
;
5509 saved_qualifying_scope
= parser
->qualifying_scope
;
5510 saved_object_scope
= parser
->object_scope
;
5511 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5512 there is no need to look for a namespace-name. */
5513 only_class_p
= template_keyword_p
5514 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5516 cp_parser_parse_tentatively (parser
);
5517 scope
= cp_parser_class_name (parser
,
5520 type_p
? class_type
: none_type
,
5522 /*class_head_p=*/false,
5524 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5525 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5527 && cxx_dialect
!= cxx98
5528 && !successful_parse_p
)
5530 /* Restore the saved scope. */
5531 parser
->scope
= saved_scope
;
5532 parser
->qualifying_scope
= saved_qualifying_scope
;
5533 parser
->object_scope
= saved_object_scope
;
5535 /* Parse tentatively. */
5536 cp_parser_parse_tentatively (parser
);
5538 /* Parse a type-name */
5539 scope
= cp_parser_type_name (parser
);
5541 /* "If the name found does not designate a namespace or a class,
5542 enumeration, or dependent type, the program is ill-formed."
5544 We cover classes and dependent types above and namespaces below,
5545 so this code is only looking for enums. */
5546 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5547 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5548 cp_parser_simulate_error (parser
);
5550 successful_parse_p
= cp_parser_parse_definitely (parser
);
5552 /* If that didn't work, try for a namespace-name. */
5553 if (!only_class_p
&& !successful_parse_p
)
5555 /* Restore the saved scope. */
5556 parser
->scope
= saved_scope
;
5557 parser
->qualifying_scope
= saved_qualifying_scope
;
5558 parser
->object_scope
= saved_object_scope
;
5559 /* If we are not looking at an identifier followed by the scope
5560 resolution operator, then this is not part of a
5561 nested-name-specifier. (Note that this function is only used
5562 to parse the components of a nested-name-specifier.) */
5563 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5564 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5565 return error_mark_node
;
5566 scope
= cp_parser_namespace_name (parser
);
5572 /* Parse a postfix-expression.
5576 postfix-expression [ expression ]
5577 postfix-expression ( expression-list [opt] )
5578 simple-type-specifier ( expression-list [opt] )
5579 typename :: [opt] nested-name-specifier identifier
5580 ( expression-list [opt] )
5581 typename :: [opt] nested-name-specifier template [opt] template-id
5582 ( expression-list [opt] )
5583 postfix-expression . template [opt] id-expression
5584 postfix-expression -> template [opt] id-expression
5585 postfix-expression . pseudo-destructor-name
5586 postfix-expression -> pseudo-destructor-name
5587 postfix-expression ++
5588 postfix-expression --
5589 dynamic_cast < type-id > ( expression )
5590 static_cast < type-id > ( expression )
5591 reinterpret_cast < type-id > ( expression )
5592 const_cast < type-id > ( expression )
5593 typeid ( expression )
5599 ( type-id ) { initializer-list , [opt] }
5601 This extension is a GNU version of the C99 compound-literal
5602 construct. (The C99 grammar uses `type-name' instead of `type-id',
5603 but they are essentially the same concept.)
5605 If ADDRESS_P is true, the postfix expression is the operand of the
5606 `&' operator. CAST_P is true if this expression is the target of a
5609 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5610 class member access expressions [expr.ref].
5612 Returns a representation of the expression. */
5615 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5616 bool member_access_only_p
, bool decltype_p
,
5617 cp_id_kind
* pidk_return
)
5622 cp_id_kind idk
= CP_ID_KIND_NONE
;
5623 tree postfix_expression
= NULL_TREE
;
5624 bool is_member_access
= false;
5626 /* Peek at the next token. */
5627 token
= cp_lexer_peek_token (parser
->lexer
);
5628 loc
= token
->location
;
5629 /* Some of the productions are determined by keywords. */
5630 keyword
= token
->keyword
;
5640 const char *saved_message
;
5641 bool saved_in_type_id_in_expr_p
;
5643 /* All of these can be handled in the same way from the point
5644 of view of parsing. Begin by consuming the token
5645 identifying the cast. */
5646 cp_lexer_consume_token (parser
->lexer
);
5648 /* New types cannot be defined in the cast. */
5649 saved_message
= parser
->type_definition_forbidden_message
;
5650 parser
->type_definition_forbidden_message
5651 = G_("types may not be defined in casts");
5653 /* Look for the opening `<'. */
5654 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5655 /* Parse the type to which we are casting. */
5656 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5657 parser
->in_type_id_in_expr_p
= true;
5658 type
= cp_parser_type_id (parser
);
5659 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5660 /* Look for the closing `>'. */
5661 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5662 /* Restore the old message. */
5663 parser
->type_definition_forbidden_message
= saved_message
;
5665 bool saved_greater_than_is_operator_p
5666 = parser
->greater_than_is_operator_p
;
5667 parser
->greater_than_is_operator_p
= true;
5669 /* And the expression which is being cast. */
5670 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5671 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
5672 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5674 parser
->greater_than_is_operator_p
5675 = saved_greater_than_is_operator_p
;
5677 /* Only type conversions to integral or enumeration types
5678 can be used in constant-expressions. */
5679 if (!cast_valid_in_integral_constant_expression_p (type
)
5680 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5681 return error_mark_node
;
5687 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5691 = build_static_cast (type
, expression
, tf_warning_or_error
);
5695 = build_reinterpret_cast (type
, expression
,
5696 tf_warning_or_error
);
5700 = build_const_cast (type
, expression
, tf_warning_or_error
);
5711 const char *saved_message
;
5712 bool saved_in_type_id_in_expr_p
;
5714 /* Consume the `typeid' token. */
5715 cp_lexer_consume_token (parser
->lexer
);
5716 /* Look for the `(' token. */
5717 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5718 /* Types cannot be defined in a `typeid' expression. */
5719 saved_message
= parser
->type_definition_forbidden_message
;
5720 parser
->type_definition_forbidden_message
5721 = G_("types may not be defined in a %<typeid%> expression");
5722 /* We can't be sure yet whether we're looking at a type-id or an
5724 cp_parser_parse_tentatively (parser
);
5725 /* Try a type-id first. */
5726 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5727 parser
->in_type_id_in_expr_p
= true;
5728 type
= cp_parser_type_id (parser
);
5729 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5730 /* Look for the `)' token. Otherwise, we can't be sure that
5731 we're not looking at an expression: consider `typeid (int
5732 (3))', for example. */
5733 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5734 /* If all went well, simply lookup the type-id. */
5735 if (cp_parser_parse_definitely (parser
))
5736 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5737 /* Otherwise, fall back to the expression variant. */
5742 /* Look for an expression. */
5743 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
5744 /* Compute its typeid. */
5745 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5746 /* Look for the `)' token. */
5747 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5749 /* Restore the saved message. */
5750 parser
->type_definition_forbidden_message
= saved_message
;
5751 /* `typeid' may not appear in an integral constant expression. */
5752 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5753 return error_mark_node
;
5760 /* The syntax permitted here is the same permitted for an
5761 elaborated-type-specifier. */
5762 type
= cp_parser_elaborated_type_specifier (parser
,
5763 /*is_friend=*/false,
5764 /*is_declaration=*/false);
5765 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5769 case RID_BUILTIN_SHUFFLE
:
5771 vec
<tree
, va_gc
> *vec
;
5775 cp_lexer_consume_token (parser
->lexer
);
5776 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5777 /*cast_p=*/false, /*allow_expansion_p=*/true,
5778 /*non_constant_p=*/NULL
);
5780 return error_mark_node
;
5782 FOR_EACH_VEC_ELT (*vec
, i
, p
)
5785 if (vec
->length () == 2)
5786 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
5787 tf_warning_or_error
);
5788 else if (vec
->length () == 3)
5789 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
5790 tf_warning_or_error
);
5793 error_at (loc
, "wrong number of arguments to "
5794 "%<__builtin_shuffle%>");
5795 return error_mark_node
;
5804 /* If the next thing is a simple-type-specifier, we may be
5805 looking at a functional cast. We could also be looking at
5806 an id-expression. So, we try the functional cast, and if
5807 that doesn't work we fall back to the primary-expression. */
5808 cp_parser_parse_tentatively (parser
);
5809 /* Look for the simple-type-specifier. */
5810 type
= cp_parser_simple_type_specifier (parser
,
5811 /*decl_specs=*/NULL
,
5812 CP_PARSER_FLAGS_NONE
);
5813 /* Parse the cast itself. */
5814 if (!cp_parser_error_occurred (parser
))
5816 = cp_parser_functional_cast (parser
, type
);
5817 /* If that worked, we're done. */
5818 if (cp_parser_parse_definitely (parser
))
5821 /* If the functional-cast didn't work out, try a
5822 compound-literal. */
5823 if (cp_parser_allow_gnu_extensions_p (parser
)
5824 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5826 tree initializer
= NULL_TREE
;
5827 bool compound_literal_p
;
5829 cp_parser_parse_tentatively (parser
);
5830 /* Consume the `('. */
5831 cp_lexer_consume_token (parser
->lexer
);
5833 /* Avoid calling cp_parser_type_id pointlessly, see comment
5834 in cp_parser_cast_expression about c++/29234. */
5835 cp_lexer_save_tokens (parser
->lexer
);
5838 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5839 /*consume_paren=*/true)
5840 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5842 /* Roll back the tokens we skipped. */
5843 cp_lexer_rollback_tokens (parser
->lexer
);
5845 if (!compound_literal_p
)
5846 cp_parser_simulate_error (parser
);
5849 /* Parse the type. */
5850 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5851 parser
->in_type_id_in_expr_p
= true;
5852 type
= cp_parser_type_id (parser
);
5853 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5854 /* Look for the `)'. */
5855 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5858 /* If things aren't going well, there's no need to
5860 if (!cp_parser_error_occurred (parser
))
5862 bool non_constant_p
;
5863 /* Parse the brace-enclosed initializer list. */
5864 initializer
= cp_parser_braced_list (parser
,
5867 /* If that worked, we're definitely looking at a
5868 compound-literal expression. */
5869 if (cp_parser_parse_definitely (parser
))
5871 /* Warn the user that a compound literal is not
5872 allowed in standard C++. */
5873 pedwarn (input_location
, OPT_Wpedantic
,
5874 "ISO C++ forbids compound-literals");
5875 /* For simplicity, we disallow compound literals in
5876 constant-expressions. We could
5877 allow compound literals of integer type, whose
5878 initializer was a constant, in constant
5879 expressions. Permitting that usage, as a further
5880 extension, would not change the meaning of any
5881 currently accepted programs. (Of course, as
5882 compound literals are not part of ISO C++, the
5883 standard has nothing to say.) */
5884 if (cp_parser_non_integral_constant_expression (parser
,
5887 postfix_expression
= error_mark_node
;
5890 /* Form the representation of the compound-literal. */
5892 = finish_compound_literal (type
, initializer
,
5893 tf_warning_or_error
);
5898 /* It must be a primary-expression. */
5900 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5901 /*template_arg_p=*/false,
5908 /* Note that we don't need to worry about calling build_cplus_new on a
5909 class-valued CALL_EXPR in decltype when it isn't the end of the
5910 postfix-expression; unary_complex_lvalue will take care of that for
5913 /* Keep looping until the postfix-expression is complete. */
5916 if (idk
== CP_ID_KIND_UNQUALIFIED
5917 && identifier_p (postfix_expression
)
5918 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
5919 /* It is not a Koenig lookup function call. */
5921 = unqualified_name_lookup_error (postfix_expression
);
5923 /* Peek at the next token. */
5924 token
= cp_lexer_peek_token (parser
->lexer
);
5926 switch (token
->type
)
5928 case CPP_OPEN_SQUARE
:
5929 if (cp_next_tokens_can_be_std_attribute_p (parser
))
5931 cp_parser_error (parser
,
5932 "two consecutive %<[%> shall "
5933 "only introduce an attribute");
5934 return error_mark_node
;
5937 = cp_parser_postfix_open_square_expression (parser
,
5941 idk
= CP_ID_KIND_NONE
;
5942 is_member_access
= false;
5945 case CPP_OPEN_PAREN
:
5946 /* postfix-expression ( expression-list [opt] ) */
5949 bool is_builtin_constant_p
;
5950 bool saved_integral_constant_expression_p
= false;
5951 bool saved_non_integral_constant_expression_p
= false;
5952 tsubst_flags_t complain
= complain_flags (decltype_p
);
5953 vec
<tree
, va_gc
> *args
;
5955 is_member_access
= false;
5957 is_builtin_constant_p
5958 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
5959 if (is_builtin_constant_p
)
5961 /* The whole point of __builtin_constant_p is to allow
5962 non-constant expressions to appear as arguments. */
5963 saved_integral_constant_expression_p
5964 = parser
->integral_constant_expression_p
;
5965 saved_non_integral_constant_expression_p
5966 = parser
->non_integral_constant_expression_p
;
5967 parser
->integral_constant_expression_p
= false;
5969 args
= (cp_parser_parenthesized_expression_list
5971 /*cast_p=*/false, /*allow_expansion_p=*/true,
5972 /*non_constant_p=*/NULL
));
5973 if (is_builtin_constant_p
)
5975 parser
->integral_constant_expression_p
5976 = saved_integral_constant_expression_p
;
5977 parser
->non_integral_constant_expression_p
5978 = saved_non_integral_constant_expression_p
;
5983 postfix_expression
= error_mark_node
;
5987 /* Function calls are not permitted in
5988 constant-expressions. */
5989 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
5990 && cp_parser_non_integral_constant_expression (parser
,
5993 postfix_expression
= error_mark_node
;
5994 release_tree_vector (args
);
5999 if (idk
== CP_ID_KIND_UNQUALIFIED
6000 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6002 if (identifier_p (postfix_expression
))
6004 if (!args
->is_empty ())
6007 if (!any_type_dependent_arguments_p (args
))
6009 = perform_koenig_lookup (postfix_expression
, args
,
6010 /*include_std=*/false,
6015 = unqualified_fn_lookup_error (postfix_expression
);
6017 /* We do not perform argument-dependent lookup if
6018 normal lookup finds a non-function, in accordance
6019 with the expected resolution of DR 218. */
6020 else if (!args
->is_empty ()
6021 && is_overloaded_fn (postfix_expression
))
6023 tree fn
= get_first_fn (postfix_expression
);
6024 fn
= STRIP_TEMPLATE (fn
);
6026 /* Do not do argument dependent lookup if regular
6027 lookup finds a member function or a block-scope
6028 function declaration. [basic.lookup.argdep]/3 */
6029 if (!DECL_FUNCTION_MEMBER_P (fn
)
6030 && !DECL_LOCAL_FUNCTION_P (fn
))
6033 if (!any_type_dependent_arguments_p (args
))
6035 = perform_koenig_lookup (postfix_expression
, args
,
6036 /*include_std=*/false,
6042 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6044 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6045 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6047 if (processing_template_decl
6048 && (type_dependent_expression_p (instance
)
6049 || (!BASELINK_P (fn
)
6050 && TREE_CODE (fn
) != FIELD_DECL
)
6051 || type_dependent_expression_p (fn
)
6052 || any_type_dependent_arguments_p (args
)))
6055 = build_nt_call_vec (postfix_expression
, args
);
6056 release_tree_vector (args
);
6060 if (BASELINK_P (fn
))
6063 = (build_new_method_call
6064 (instance
, fn
, &args
, NULL_TREE
,
6065 (idk
== CP_ID_KIND_QUALIFIED
6066 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6073 = finish_call_expr (postfix_expression
, &args
,
6074 /*disallow_virtual=*/false,
6078 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6079 || TREE_CODE (postfix_expression
) == MEMBER_REF
6080 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6081 postfix_expression
= (build_offset_ref_call_from_tree
6082 (postfix_expression
, &args
,
6084 else if (idk
== CP_ID_KIND_QUALIFIED
)
6085 /* A call to a static class member, or a namespace-scope
6088 = finish_call_expr (postfix_expression
, &args
,
6089 /*disallow_virtual=*/true,
6093 /* All other function calls. */
6095 = finish_call_expr (postfix_expression
, &args
,
6096 /*disallow_virtual=*/false,
6100 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6101 idk
= CP_ID_KIND_NONE
;
6103 release_tree_vector (args
);
6109 /* postfix-expression . template [opt] id-expression
6110 postfix-expression . pseudo-destructor-name
6111 postfix-expression -> template [opt] id-expression
6112 postfix-expression -> pseudo-destructor-name */
6114 /* Consume the `.' or `->' operator. */
6115 cp_lexer_consume_token (parser
->lexer
);
6118 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6122 is_member_access
= true;
6126 /* postfix-expression ++ */
6127 /* Consume the `++' token. */
6128 cp_lexer_consume_token (parser
->lexer
);
6129 /* Generate a representation for the complete expression. */
6131 = finish_increment_expr (postfix_expression
,
6132 POSTINCREMENT_EXPR
);
6133 /* Increments may not appear in constant-expressions. */
6134 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6135 postfix_expression
= error_mark_node
;
6136 idk
= CP_ID_KIND_NONE
;
6137 is_member_access
= false;
6140 case CPP_MINUS_MINUS
:
6141 /* postfix-expression -- */
6142 /* Consume the `--' token. */
6143 cp_lexer_consume_token (parser
->lexer
);
6144 /* Generate a representation for the complete expression. */
6146 = finish_increment_expr (postfix_expression
,
6147 POSTDECREMENT_EXPR
);
6148 /* Decrements may not appear in constant-expressions. */
6149 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6150 postfix_expression
= error_mark_node
;
6151 idk
= CP_ID_KIND_NONE
;
6152 is_member_access
= false;
6156 if (pidk_return
!= NULL
)
6157 * pidk_return
= idk
;
6158 if (member_access_only_p
)
6159 return is_member_access
? postfix_expression
: error_mark_node
;
6161 return postfix_expression
;
6165 /* We should never get here. */
6167 return error_mark_node
;
6170 /* This function parses Cilk Plus array notations. If a normal array expr. is
6171 parsed then the array index is passed back to the caller through *INIT_INDEX
6172 and the function returns a NULL_TREE. If array notation expr. is parsed,
6173 then *INIT_INDEX is ignored by the caller and the function returns
6174 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6178 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6181 cp_token
*token
= NULL
;
6182 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6183 if (!array_value
|| array_value
== error_mark_node
)
6185 cp_parser_skip_to_end_of_statement (parser
);
6186 return error_mark_node
;
6189 array_type
= TREE_TYPE (array_value
);
6191 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6192 parser
->colon_corrects_to_scope_p
= false;
6193 token
= cp_lexer_peek_token (parser
->lexer
);
6197 cp_parser_error (parser
, "expected %<:%> or numeral");
6198 return error_mark_node
;
6200 else if (token
->type
== CPP_COLON
)
6202 /* Consume the ':'. */
6203 cp_lexer_consume_token (parser
->lexer
);
6205 /* If we are here, then we have a case like this A[:]. */
6206 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6208 cp_parser_error (parser
, "expected %<]%>");
6209 cp_parser_skip_to_end_of_statement (parser
);
6210 return error_mark_node
;
6212 *init_index
= NULL_TREE
;
6214 length_index
= NULL_TREE
;
6218 /* If we are here, then there are three valid possibilities:
6220 2. ARRAY [ EXP : EXP ]
6221 3. ARRAY [ EXP : EXP : EXP ] */
6223 *init_index
= cp_parser_expression (parser
, false, NULL
);
6224 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6226 /* This indicates that we have a normal array expression. */
6227 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6231 /* Consume the ':'. */
6232 cp_lexer_consume_token (parser
->lexer
);
6233 length_index
= cp_parser_expression (parser
, false, NULL
);
6234 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6236 cp_lexer_consume_token (parser
->lexer
);
6237 stride
= cp_parser_expression (parser
, false, NULL
);
6240 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6242 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6243 || stride
== error_mark_node
)
6245 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6246 cp_lexer_consume_token (parser
->lexer
);
6247 return error_mark_node
;
6249 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6251 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6252 length_index
, stride
, array_type
);
6256 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6257 by cp_parser_builtin_offsetof. We're looking for
6259 postfix-expression [ expression ]
6260 postfix-expression [ braced-init-list ] (C++11)
6262 FOR_OFFSETOF is set if we're being called in that context, which
6263 changes how we deal with integer constant expressions. */
6266 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6267 tree postfix_expression
,
6271 tree index
= NULL_TREE
;
6272 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6273 bool saved_greater_than_is_operator_p
;
6275 /* Consume the `[' token. */
6276 cp_lexer_consume_token (parser
->lexer
);
6278 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6279 parser
->greater_than_is_operator_p
= true;
6281 /* Parse the index expression. */
6282 /* ??? For offsetof, there is a question of what to allow here. If
6283 offsetof is not being used in an integral constant expression context,
6284 then we *could* get the right answer by computing the value at runtime.
6285 If we are in an integral constant expression context, then we might
6286 could accept any constant expression; hard to say without analysis.
6287 Rather than open the barn door too wide right away, allow only integer
6288 constant expressions here. */
6290 index
= cp_parser_constant_expression (parser
, false, NULL
);
6293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6295 bool expr_nonconst_p
;
6296 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6297 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6298 if (flag_enable_cilkplus
6299 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6301 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6302 "braced list index is not allowed with array "
6304 cp_parser_skip_to_end_of_statement (parser
);
6305 return error_mark_node
;
6308 else if (flag_enable_cilkplus
)
6310 /* Here are have these two options:
6311 ARRAY[EXP : EXP] - Array notation expr with default
6313 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6315 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6316 postfix_expression
);
6321 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6324 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6326 /* Look for the closing `]'. */
6327 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6329 /* Build the ARRAY_REF. */
6330 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6333 /* When not doing offsetof, array references are not permitted in
6334 constant-expressions. */
6336 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6337 postfix_expression
= error_mark_node
;
6339 return postfix_expression
;
6342 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6343 by cp_parser_builtin_offsetof. We're looking for
6345 postfix-expression . template [opt] id-expression
6346 postfix-expression . pseudo-destructor-name
6347 postfix-expression -> template [opt] id-expression
6348 postfix-expression -> pseudo-destructor-name
6350 FOR_OFFSETOF is set if we're being called in that context. That sorta
6351 limits what of the above we'll actually accept, but nevermind.
6352 TOKEN_TYPE is the "." or "->" token, which will already have been
6353 removed from the stream. */
6356 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6357 enum cpp_ttype token_type
,
6358 tree postfix_expression
,
6359 bool for_offsetof
, cp_id_kind
*idk
,
6360 location_t location
)
6364 bool pseudo_destructor_p
;
6365 tree scope
= NULL_TREE
;
6367 /* If this is a `->' operator, dereference the pointer. */
6368 if (token_type
== CPP_DEREF
)
6369 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6370 tf_warning_or_error
);
6371 /* Check to see whether or not the expression is type-dependent. */
6372 dependent_p
= type_dependent_expression_p (postfix_expression
);
6373 /* The identifier following the `->' or `.' is not qualified. */
6374 parser
->scope
= NULL_TREE
;
6375 parser
->qualifying_scope
= NULL_TREE
;
6376 parser
->object_scope
= NULL_TREE
;
6377 *idk
= CP_ID_KIND_NONE
;
6379 /* Enter the scope corresponding to the type of the object
6380 given by the POSTFIX_EXPRESSION. */
6381 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6383 scope
= TREE_TYPE (postfix_expression
);
6384 /* According to the standard, no expression should ever have
6385 reference type. Unfortunately, we do not currently match
6386 the standard in this respect in that our internal representation
6387 of an expression may have reference type even when the standard
6388 says it does not. Therefore, we have to manually obtain the
6389 underlying type here. */
6390 scope
= non_reference (scope
);
6391 /* The type of the POSTFIX_EXPRESSION must be complete. */
6392 if (scope
== unknown_type_node
)
6394 error_at (location
, "%qE does not have class type",
6395 postfix_expression
);
6398 /* Unlike the object expression in other contexts, *this is not
6399 required to be of complete type for purposes of class member
6400 access (5.2.5) outside the member function body. */
6401 else if (postfix_expression
!= current_class_ref
6402 && !(processing_template_decl
&& scope
== current_class_type
))
6403 scope
= complete_type_or_else (scope
, NULL_TREE
);
6404 /* Let the name lookup machinery know that we are processing a
6405 class member access expression. */
6406 parser
->context
->object_type
= scope
;
6407 /* If something went wrong, we want to be able to discern that case,
6408 as opposed to the case where there was no SCOPE due to the type
6409 of expression being dependent. */
6411 scope
= error_mark_node
;
6412 /* If the SCOPE was erroneous, make the various semantic analysis
6413 functions exit quickly -- and without issuing additional error
6415 if (scope
== error_mark_node
)
6416 postfix_expression
= error_mark_node
;
6419 /* Assume this expression is not a pseudo-destructor access. */
6420 pseudo_destructor_p
= false;
6422 /* If the SCOPE is a scalar type, then, if this is a valid program,
6423 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6424 is type dependent, it can be pseudo-destructor-name or something else.
6425 Try to parse it as pseudo-destructor-name first. */
6426 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6431 cp_parser_parse_tentatively (parser
);
6432 /* Parse the pseudo-destructor-name. */
6434 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6437 && (cp_parser_error_occurred (parser
)
6438 || !SCALAR_TYPE_P (type
)))
6439 cp_parser_abort_tentative_parse (parser
);
6440 else if (cp_parser_parse_definitely (parser
))
6442 pseudo_destructor_p
= true;
6444 = finish_pseudo_destructor_expr (postfix_expression
,
6449 if (!pseudo_destructor_p
)
6451 /* If the SCOPE is not a scalar type, we are looking at an
6452 ordinary class member access expression, rather than a
6453 pseudo-destructor-name. */
6455 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6456 /* Parse the id-expression. */
6457 name
= (cp_parser_id_expression
6459 cp_parser_optional_template_keyword (parser
),
6460 /*check_dependency_p=*/true,
6462 /*declarator_p=*/false,
6463 /*optional_p=*/false));
6464 /* In general, build a SCOPE_REF if the member name is qualified.
6465 However, if the name was not dependent and has already been
6466 resolved; there is no need to build the SCOPE_REF. For example;
6468 struct X { void f(); };
6469 template <typename T> void f(T* t) { t->X::f(); }
6471 Even though "t" is dependent, "X::f" is not and has been resolved
6472 to a BASELINK; there is no need to include scope information. */
6474 /* But we do need to remember that there was an explicit scope for
6475 virtual function calls. */
6477 *idk
= CP_ID_KIND_QUALIFIED
;
6479 /* If the name is a template-id that names a type, we will get a
6480 TYPE_DECL here. That is invalid code. */
6481 if (TREE_CODE (name
) == TYPE_DECL
)
6483 error_at (token
->location
, "invalid use of %qD", name
);
6484 postfix_expression
= error_mark_node
;
6488 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6490 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6492 error_at (token
->location
, "%<%D::%D%> is not a class member",
6493 parser
->scope
, name
);
6494 postfix_expression
= error_mark_node
;
6497 name
= build_qualified_name (/*type=*/NULL_TREE
,
6501 parser
->scope
= NULL_TREE
;
6502 parser
->qualifying_scope
= NULL_TREE
;
6503 parser
->object_scope
= NULL_TREE
;
6505 if (parser
->scope
&& name
&& BASELINK_P (name
))
6506 adjust_result_of_qualified_name_lookup
6507 (name
, parser
->scope
, scope
);
6509 = finish_class_member_access_expr (postfix_expression
, name
,
6511 tf_warning_or_error
);
6515 /* We no longer need to look up names in the scope of the object on
6516 the left-hand side of the `.' or `->' operator. */
6517 parser
->context
->object_type
= NULL_TREE
;
6519 /* Outside of offsetof, these operators may not appear in
6520 constant-expressions. */
6522 && (cp_parser_non_integral_constant_expression
6523 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6524 postfix_expression
= error_mark_node
;
6526 return postfix_expression
;
6529 /* Parse a parenthesized expression-list.
6532 assignment-expression
6533 expression-list, assignment-expression
6538 identifier, expression-list
6540 CAST_P is true if this expression is the target of a cast.
6542 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6545 Returns a vector of trees. Each element is a representation of an
6546 assignment-expression. NULL is returned if the ( and or ) are
6547 missing. An empty, but allocated, vector is returned on no
6548 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6549 if we are parsing an attribute list for an attribute that wants a
6550 plain identifier argument, normal_attr for an attribute that wants
6551 an expression, or non_attr if we aren't parsing an attribute list. If
6552 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6553 not all of the expressions in the list were constant. */
6555 static vec
<tree
, va_gc
> *
6556 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6557 int is_attribute_list
,
6559 bool allow_expansion_p
,
6560 bool *non_constant_p
)
6562 vec
<tree
, va_gc
> *expression_list
;
6563 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6564 tree identifier
= NULL_TREE
;
6565 bool saved_greater_than_is_operator_p
;
6567 /* Assume all the expressions will be constant. */
6569 *non_constant_p
= false;
6571 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6574 expression_list
= make_tree_vector ();
6576 /* Within a parenthesized expression, a `>' token is always
6577 the greater-than operator. */
6578 saved_greater_than_is_operator_p
6579 = parser
->greater_than_is_operator_p
;
6580 parser
->greater_than_is_operator_p
= true;
6582 /* Consume expressions until there are no more. */
6583 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6588 /* At the beginning of attribute lists, check to see if the
6589 next token is an identifier. */
6590 if (is_attribute_list
== id_attr
6591 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6595 /* Consume the identifier. */
6596 token
= cp_lexer_consume_token (parser
->lexer
);
6597 /* Save the identifier. */
6598 identifier
= token
->u
.value
;
6602 bool expr_non_constant_p
;
6604 /* Parse the next assignment-expression. */
6605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6607 /* A braced-init-list. */
6608 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6609 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6610 if (non_constant_p
&& expr_non_constant_p
)
6611 *non_constant_p
= true;
6613 else if (non_constant_p
)
6615 expr
= (cp_parser_constant_expression
6616 (parser
, /*allow_non_constant_p=*/true,
6617 &expr_non_constant_p
));
6618 if (expr_non_constant_p
)
6619 *non_constant_p
= true;
6622 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
6625 expr
= fold_non_dependent_expr (expr
);
6627 /* If we have an ellipsis, then this is an expression
6629 if (allow_expansion_p
6630 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6632 /* Consume the `...'. */
6633 cp_lexer_consume_token (parser
->lexer
);
6635 /* Build the argument pack. */
6636 expr
= make_pack_expansion (expr
);
6639 /* Add it to the list. We add error_mark_node
6640 expressions to the list, so that we can still tell if
6641 the correct form for a parenthesized expression-list
6642 is found. That gives better errors. */
6643 vec_safe_push (expression_list
, expr
);
6645 if (expr
== error_mark_node
)
6649 /* After the first item, attribute lists look the same as
6650 expression lists. */
6651 is_attribute_list
= non_attr
;
6654 /* If the next token isn't a `,', then we are done. */
6655 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6658 /* Otherwise, consume the `,' and keep going. */
6659 cp_lexer_consume_token (parser
->lexer
);
6662 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6667 /* We try and resync to an unnested comma, as that will give the
6668 user better diagnostics. */
6669 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6670 /*recovering=*/true,
6672 /*consume_paren=*/true);
6677 parser
->greater_than_is_operator_p
6678 = saved_greater_than_is_operator_p
;
6683 parser
->greater_than_is_operator_p
6684 = saved_greater_than_is_operator_p
;
6687 vec_safe_insert (expression_list
, 0, identifier
);
6689 return expression_list
;
6692 /* Parse a pseudo-destructor-name.
6694 pseudo-destructor-name:
6695 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6696 :: [opt] nested-name-specifier template template-id :: ~ type-name
6697 :: [opt] nested-name-specifier [opt] ~ type-name
6699 If either of the first two productions is used, sets *SCOPE to the
6700 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6701 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6702 or ERROR_MARK_NODE if the parse fails. */
6705 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6710 bool nested_name_specifier_p
;
6713 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
6714 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
6715 && !type_dependent_expression_p (object
))
6717 if (cxx_dialect
< cxx1y
)
6718 pedwarn (input_location
, 0,
6719 "%<~auto%> only available with "
6720 "-std=c++1y or -std=gnu++1y");
6721 cp_lexer_consume_token (parser
->lexer
);
6722 cp_lexer_consume_token (parser
->lexer
);
6724 *type
= TREE_TYPE (object
);
6728 /* Assume that things will not work out. */
6729 *type
= error_mark_node
;
6731 /* Look for the optional `::' operator. */
6732 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
6733 /* Look for the optional nested-name-specifier. */
6734 nested_name_specifier_p
6735 = (cp_parser_nested_name_specifier_opt (parser
,
6736 /*typename_keyword_p=*/false,
6737 /*check_dependency_p=*/true,
6739 /*is_declaration=*/false)
6741 /* Now, if we saw a nested-name-specifier, we might be doing the
6742 second production. */
6743 if (nested_name_specifier_p
6744 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
6746 /* Consume the `template' keyword. */
6747 cp_lexer_consume_token (parser
->lexer
);
6748 /* Parse the template-id. */
6749 cp_parser_template_id (parser
,
6750 /*template_keyword_p=*/true,
6751 /*check_dependency_p=*/false,
6753 /*is_declaration=*/true);
6754 /* Look for the `::' token. */
6755 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6757 /* If the next token is not a `~', then there might be some
6758 additional qualification. */
6759 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
6761 /* At this point, we're looking for "type-name :: ~". The type-name
6762 must not be a class-name, since this is a pseudo-destructor. So,
6763 it must be either an enum-name, or a typedef-name -- both of which
6764 are just identifiers. So, we peek ahead to check that the "::"
6765 and "~" tokens are present; if they are not, then we can avoid
6766 calling type_name. */
6767 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
6768 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
6769 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
6771 cp_parser_error (parser
, "non-scalar type");
6775 /* Look for the type-name. */
6776 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6777 if (*scope
== error_mark_node
)
6780 /* Look for the `::' token. */
6781 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6786 /* Look for the `~'. */
6787 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
6789 /* Once we see the ~, this has to be a pseudo-destructor. */
6790 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
6791 cp_parser_commit_to_topmost_tentative_parse (parser
);
6793 /* Look for the type-name again. We are not responsible for
6794 checking that it matches the first type-name. */
6795 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6798 /* Parse a unary-expression.
6804 unary-operator cast-expression
6805 sizeof unary-expression
6807 alignof ( type-id ) [C++0x]
6814 __extension__ cast-expression
6815 __alignof__ unary-expression
6816 __alignof__ ( type-id )
6817 alignof unary-expression [C++0x]
6818 __real__ cast-expression
6819 __imag__ cast-expression
6821 sizeof ( type-id ) { initializer-list , [opt] }
6822 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6823 __alignof__ ( type-id ) { initializer-list , [opt] }
6825 ADDRESS_P is true iff the unary-expression is appearing as the
6826 operand of the `&' operator. CAST_P is true if this expression is
6827 the target of a cast.
6829 Returns a representation of the expression. */
6832 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6833 bool decltype_p
, cp_id_kind
* pidk
)
6836 enum tree_code unary_operator
;
6838 /* Peek at the next token. */
6839 token
= cp_lexer_peek_token (parser
->lexer
);
6840 /* Some keywords give away the kind of expression. */
6841 if (token
->type
== CPP_KEYWORD
)
6843 enum rid keyword
= token
->keyword
;
6852 location_t first_loc
;
6854 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
6855 /* Consume the token. */
6856 cp_lexer_consume_token (parser
->lexer
);
6857 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6858 /* Parse the operand. */
6859 operand
= cp_parser_sizeof_operand (parser
, keyword
);
6861 if (TYPE_P (operand
))
6862 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
6865 /* ISO C++ defines alignof only with types, not with
6866 expressions. So pedwarn if alignof is used with a non-
6867 type expression. However, __alignof__ is ok. */
6868 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
6869 pedwarn (token
->location
, OPT_Wpedantic
,
6870 "ISO C++ does not allow %<alignof%> "
6873 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
6875 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6876 SIZEOF_EXPR with the original operand. */
6877 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
6879 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
6881 if (!processing_template_decl
&& TYPE_P (operand
))
6883 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
6884 build1 (NOP_EXPR
, operand
,
6886 SIZEOF_EXPR_TYPE_P (ret
) = 1;
6889 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
6890 TREE_SIDE_EFFECTS (ret
) = 0;
6891 TREE_READONLY (ret
) = 1;
6893 SET_EXPR_LOCATION (ret
, first_loc
);
6899 return cp_parser_new_expression (parser
);
6902 return cp_parser_delete_expression (parser
);
6906 /* The saved value of the PEDANTIC flag. */
6910 /* Save away the PEDANTIC flag. */
6911 cp_parser_extension_opt (parser
, &saved_pedantic
);
6912 /* Parse the cast-expression. */
6913 expr
= cp_parser_simple_cast_expression (parser
);
6914 /* Restore the PEDANTIC flag. */
6915 pedantic
= saved_pedantic
;
6925 /* Consume the `__real__' or `__imag__' token. */
6926 cp_lexer_consume_token (parser
->lexer
);
6927 /* Parse the cast-expression. */
6928 expression
= cp_parser_simple_cast_expression (parser
);
6929 /* Create the complete representation. */
6930 return build_x_unary_op (token
->location
,
6931 (keyword
== RID_REALPART
6932 ? REALPART_EXPR
: IMAGPART_EXPR
),
6934 tf_warning_or_error
);
6938 case RID_TRANSACTION_ATOMIC
:
6939 case RID_TRANSACTION_RELAXED
:
6940 return cp_parser_transaction_expression (parser
, keyword
);
6945 const char *saved_message
;
6946 bool saved_integral_constant_expression_p
;
6947 bool saved_non_integral_constant_expression_p
;
6948 bool saved_greater_than_is_operator_p
;
6950 cp_lexer_consume_token (parser
->lexer
);
6951 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6953 saved_message
= parser
->type_definition_forbidden_message
;
6954 parser
->type_definition_forbidden_message
6955 = G_("types may not be defined in %<noexcept%> expressions");
6957 saved_integral_constant_expression_p
6958 = parser
->integral_constant_expression_p
;
6959 saved_non_integral_constant_expression_p
6960 = parser
->non_integral_constant_expression_p
;
6961 parser
->integral_constant_expression_p
= false;
6963 saved_greater_than_is_operator_p
6964 = parser
->greater_than_is_operator_p
;
6965 parser
->greater_than_is_operator_p
= true;
6967 ++cp_unevaluated_operand
;
6968 ++c_inhibit_evaluation_warnings
;
6969 expr
= cp_parser_expression (parser
, false, NULL
);
6970 --c_inhibit_evaluation_warnings
;
6971 --cp_unevaluated_operand
;
6973 parser
->greater_than_is_operator_p
6974 = saved_greater_than_is_operator_p
;
6976 parser
->integral_constant_expression_p
6977 = saved_integral_constant_expression_p
;
6978 parser
->non_integral_constant_expression_p
6979 = saved_non_integral_constant_expression_p
;
6981 parser
->type_definition_forbidden_message
= saved_message
;
6983 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6984 return finish_noexcept_expr (expr
, tf_warning_or_error
);
6992 /* Look for the `:: new' and `:: delete', which also signal the
6993 beginning of a new-expression, or delete-expression,
6994 respectively. If the next token is `::', then it might be one of
6996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7000 /* See if the token after the `::' is one of the keywords in
7001 which we're interested. */
7002 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7003 /* If it's `new', we have a new-expression. */
7004 if (keyword
== RID_NEW
)
7005 return cp_parser_new_expression (parser
);
7006 /* Similarly, for `delete'. */
7007 else if (keyword
== RID_DELETE
)
7008 return cp_parser_delete_expression (parser
);
7011 /* Look for a unary operator. */
7012 unary_operator
= cp_parser_unary_operator (token
);
7013 /* The `++' and `--' operators can be handled similarly, even though
7014 they are not technically unary-operators in the grammar. */
7015 if (unary_operator
== ERROR_MARK
)
7017 if (token
->type
== CPP_PLUS_PLUS
)
7018 unary_operator
= PREINCREMENT_EXPR
;
7019 else if (token
->type
== CPP_MINUS_MINUS
)
7020 unary_operator
= PREDECREMENT_EXPR
;
7021 /* Handle the GNU address-of-label extension. */
7022 else if (cp_parser_allow_gnu_extensions_p (parser
)
7023 && token
->type
== CPP_AND_AND
)
7027 location_t loc
= token
->location
;
7029 /* Consume the '&&' token. */
7030 cp_lexer_consume_token (parser
->lexer
);
7031 /* Look for the identifier. */
7032 identifier
= cp_parser_identifier (parser
);
7033 /* Create an expression representing the address. */
7034 expression
= finish_label_address_expr (identifier
, loc
);
7035 if (cp_parser_non_integral_constant_expression (parser
,
7037 expression
= error_mark_node
;
7041 if (unary_operator
!= ERROR_MARK
)
7043 tree cast_expression
;
7044 tree expression
= error_mark_node
;
7045 non_integral_constant non_constant_p
= NIC_NONE
;
7046 location_t loc
= token
->location
;
7047 tsubst_flags_t complain
= complain_flags (decltype_p
);
7049 /* Consume the operator token. */
7050 token
= cp_lexer_consume_token (parser
->lexer
);
7051 /* Parse the cast-expression. */
7053 = cp_parser_cast_expression (parser
,
7054 unary_operator
== ADDR_EXPR
,
7058 /* Now, build an appropriate representation. */
7059 switch (unary_operator
)
7062 non_constant_p
= NIC_STAR
;
7063 expression
= build_x_indirect_ref (loc
, cast_expression
,
7069 non_constant_p
= NIC_ADDR
;
7072 expression
= build_x_unary_op (loc
, unary_operator
,
7077 case PREINCREMENT_EXPR
:
7078 case PREDECREMENT_EXPR
:
7079 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7080 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7082 case UNARY_PLUS_EXPR
:
7084 case TRUTH_NOT_EXPR
:
7085 expression
= finish_unary_op_expr (loc
, unary_operator
,
7086 cast_expression
, complain
);
7093 if (non_constant_p
!= NIC_NONE
7094 && cp_parser_non_integral_constant_expression (parser
,
7096 expression
= error_mark_node
;
7101 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7102 /*member_access_only_p=*/false,
7108 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7111 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7112 /*decltype*/false, pidk
);
7115 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7116 unary-operator, the corresponding tree code is returned. */
7118 static enum tree_code
7119 cp_parser_unary_operator (cp_token
* token
)
7121 switch (token
->type
)
7124 return INDIRECT_REF
;
7130 return UNARY_PLUS_EXPR
;
7136 return TRUTH_NOT_EXPR
;
7139 return BIT_NOT_EXPR
;
7146 /* Parse a new-expression.
7149 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7150 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7152 Returns a representation of the expression. */
7155 cp_parser_new_expression (cp_parser
* parser
)
7157 bool global_scope_p
;
7158 vec
<tree
, va_gc
> *placement
;
7160 vec
<tree
, va_gc
> *initializer
;
7161 tree nelts
= NULL_TREE
;
7164 /* Look for the optional `::' operator. */
7166 = (cp_parser_global_scope_opt (parser
,
7167 /*current_scope_valid_p=*/false)
7169 /* Look for the `new' operator. */
7170 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7171 /* There's no easy way to tell a new-placement from the
7172 `( type-id )' construct. */
7173 cp_parser_parse_tentatively (parser
);
7174 /* Look for a new-placement. */
7175 placement
= cp_parser_new_placement (parser
);
7176 /* If that didn't work out, there's no new-placement. */
7177 if (!cp_parser_parse_definitely (parser
))
7179 if (placement
!= NULL
)
7180 release_tree_vector (placement
);
7184 /* If the next token is a `(', then we have a parenthesized
7186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7189 const char *saved_message
= parser
->type_definition_forbidden_message
;
7191 /* Consume the `('. */
7192 cp_lexer_consume_token (parser
->lexer
);
7194 /* Parse the type-id. */
7195 parser
->type_definition_forbidden_message
7196 = G_("types may not be defined in a new-expression");
7197 type
= cp_parser_type_id (parser
);
7198 parser
->type_definition_forbidden_message
= saved_message
;
7200 /* Look for the closing `)'. */
7201 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7202 token
= cp_lexer_peek_token (parser
->lexer
);
7203 /* There should not be a direct-new-declarator in this production,
7204 but GCC used to allowed this, so we check and emit a sensible error
7205 message for this case. */
7206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7208 error_at (token
->location
,
7209 "array bound forbidden after parenthesized type-id");
7210 inform (token
->location
,
7211 "try removing the parentheses around the type-id");
7212 cp_parser_direct_new_declarator (parser
);
7215 /* Otherwise, there must be a new-type-id. */
7217 type
= cp_parser_new_type_id (parser
, &nelts
);
7219 /* If the next token is a `(' or '{', then we have a new-initializer. */
7220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7221 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7222 initializer
= cp_parser_new_initializer (parser
);
7226 /* A new-expression may not appear in an integral constant
7228 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7229 ret
= error_mark_node
;
7232 /* Create a representation of the new-expression. */
7233 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7234 tf_warning_or_error
);
7237 if (placement
!= NULL
)
7238 release_tree_vector (placement
);
7239 if (initializer
!= NULL
)
7240 release_tree_vector (initializer
);
7245 /* Parse a new-placement.
7250 Returns the same representation as for an expression-list. */
7252 static vec
<tree
, va_gc
> *
7253 cp_parser_new_placement (cp_parser
* parser
)
7255 vec
<tree
, va_gc
> *expression_list
;
7257 /* Parse the expression-list. */
7258 expression_list
= (cp_parser_parenthesized_expression_list
7259 (parser
, non_attr
, /*cast_p=*/false,
7260 /*allow_expansion_p=*/true,
7261 /*non_constant_p=*/NULL
));
7263 return expression_list
;
7266 /* Parse a new-type-id.
7269 type-specifier-seq new-declarator [opt]
7271 Returns the TYPE allocated. If the new-type-id indicates an array
7272 type, *NELTS is set to the number of elements in the last array
7273 bound; the TYPE will not include the last array bound. */
7276 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7278 cp_decl_specifier_seq type_specifier_seq
;
7279 cp_declarator
*new_declarator
;
7280 cp_declarator
*declarator
;
7281 cp_declarator
*outer_declarator
;
7282 const char *saved_message
;
7284 /* The type-specifier sequence must not contain type definitions.
7285 (It cannot contain declarations of new types either, but if they
7286 are not definitions we will catch that because they are not
7288 saved_message
= parser
->type_definition_forbidden_message
;
7289 parser
->type_definition_forbidden_message
7290 = G_("types may not be defined in a new-type-id");
7291 /* Parse the type-specifier-seq. */
7292 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7293 /*is_trailing_return=*/false,
7294 &type_specifier_seq
);
7295 /* Restore the old message. */
7296 parser
->type_definition_forbidden_message
= saved_message
;
7298 if (type_specifier_seq
.type
== error_mark_node
)
7299 return error_mark_node
;
7301 /* Parse the new-declarator. */
7302 new_declarator
= cp_parser_new_declarator_opt (parser
);
7304 /* Determine the number of elements in the last array dimension, if
7307 /* Skip down to the last array dimension. */
7308 declarator
= new_declarator
;
7309 outer_declarator
= NULL
;
7310 while (declarator
&& (declarator
->kind
== cdk_pointer
7311 || declarator
->kind
== cdk_ptrmem
))
7313 outer_declarator
= declarator
;
7314 declarator
= declarator
->declarator
;
7317 && declarator
->kind
== cdk_array
7318 && declarator
->declarator
7319 && declarator
->declarator
->kind
== cdk_array
)
7321 outer_declarator
= declarator
;
7322 declarator
= declarator
->declarator
;
7325 if (declarator
&& declarator
->kind
== cdk_array
)
7327 *nelts
= declarator
->u
.array
.bounds
;
7328 if (*nelts
== error_mark_node
)
7329 *nelts
= integer_one_node
;
7331 if (outer_declarator
)
7332 outer_declarator
->declarator
= declarator
->declarator
;
7334 new_declarator
= NULL
;
7337 return groktypename (&type_specifier_seq
, new_declarator
, false);
7340 /* Parse an (optional) new-declarator.
7343 ptr-operator new-declarator [opt]
7344 direct-new-declarator
7346 Returns the declarator. */
7348 static cp_declarator
*
7349 cp_parser_new_declarator_opt (cp_parser
* parser
)
7351 enum tree_code code
;
7352 tree type
, std_attributes
= NULL_TREE
;
7353 cp_cv_quals cv_quals
;
7355 /* We don't know if there's a ptr-operator next, or not. */
7356 cp_parser_parse_tentatively (parser
);
7357 /* Look for a ptr-operator. */
7358 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7359 /* If that worked, look for more new-declarators. */
7360 if (cp_parser_parse_definitely (parser
))
7362 cp_declarator
*declarator
;
7364 /* Parse another optional declarator. */
7365 declarator
= cp_parser_new_declarator_opt (parser
);
7367 declarator
= cp_parser_make_indirect_declarator
7368 (code
, type
, cv_quals
, declarator
, std_attributes
);
7373 /* If the next token is a `[', there is a direct-new-declarator. */
7374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7375 return cp_parser_direct_new_declarator (parser
);
7380 /* Parse a direct-new-declarator.
7382 direct-new-declarator:
7384 direct-new-declarator [constant-expression]
7388 static cp_declarator
*
7389 cp_parser_direct_new_declarator (cp_parser
* parser
)
7391 cp_declarator
*declarator
= NULL
;
7398 /* Look for the opening `['. */
7399 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7401 token
= cp_lexer_peek_token (parser
->lexer
);
7402 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7403 /* The standard requires that the expression have integral
7404 type. DR 74 adds enumeration types. We believe that the
7405 real intent is that these expressions be handled like the
7406 expression in a `switch' condition, which also allows
7407 classes with a single conversion to integral or
7408 enumeration type. */
7409 if (!processing_template_decl
)
7412 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7417 error_at (token
->location
,
7418 "expression in new-declarator must have integral "
7419 "or enumeration type");
7420 expression
= error_mark_node
;
7424 /* Look for the closing `]'. */
7425 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7427 /* Add this bound to the declarator. */
7428 declarator
= make_array_declarator (declarator
, expression
);
7430 /* If the next token is not a `[', then there are no more
7432 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7439 /* Parse a new-initializer.
7442 ( expression-list [opt] )
7445 Returns a representation of the expression-list. */
7447 static vec
<tree
, va_gc
> *
7448 cp_parser_new_initializer (cp_parser
* parser
)
7450 vec
<tree
, va_gc
> *expression_list
;
7452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7455 bool expr_non_constant_p
;
7456 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7457 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7458 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7459 expression_list
= make_tree_vector_single (t
);
7462 expression_list
= (cp_parser_parenthesized_expression_list
7463 (parser
, non_attr
, /*cast_p=*/false,
7464 /*allow_expansion_p=*/true,
7465 /*non_constant_p=*/NULL
));
7467 return expression_list
;
7470 /* Parse a delete-expression.
7473 :: [opt] delete cast-expression
7474 :: [opt] delete [ ] cast-expression
7476 Returns a representation of the expression. */
7479 cp_parser_delete_expression (cp_parser
* parser
)
7481 bool global_scope_p
;
7485 /* Look for the optional `::' operator. */
7487 = (cp_parser_global_scope_opt (parser
,
7488 /*current_scope_valid_p=*/false)
7490 /* Look for the `delete' keyword. */
7491 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7492 /* See if the array syntax is in use. */
7493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7495 /* Consume the `[' token. */
7496 cp_lexer_consume_token (parser
->lexer
);
7497 /* Look for the `]' token. */
7498 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7499 /* Remember that this is the `[]' construct. */
7505 /* Parse the cast-expression. */
7506 expression
= cp_parser_simple_cast_expression (parser
);
7508 /* A delete-expression may not appear in an integral constant
7510 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7511 return error_mark_node
;
7513 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7514 tf_warning_or_error
);
7517 /* Returns true if TOKEN may start a cast-expression and false
7521 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7523 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7524 switch (token
->type
)
7530 case CPP_CLOSE_SQUARE
:
7531 case CPP_CLOSE_PAREN
:
7532 case CPP_CLOSE_BRACE
:
7533 case CPP_OPEN_BRACE
:
7537 case CPP_DEREF_STAR
:
7545 case CPP_GREATER_EQ
:
7565 case CPP_OPEN_PAREN
:
7566 /* In ((type ()) () the last () isn't a valid cast-expression,
7567 so the whole must be parsed as postfix-expression. */
7568 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7571 /* '[' may start a primary-expression in obj-c++. */
7572 case CPP_OPEN_SQUARE
:
7573 return c_dialect_objc ();
7580 /* Parse a cast-expression.
7584 ( type-id ) cast-expression
7586 ADDRESS_P is true iff the unary-expression is appearing as the
7587 operand of the `&' operator. CAST_P is true if this expression is
7588 the target of a cast.
7590 Returns a representation of the expression. */
7593 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7594 bool decltype_p
, cp_id_kind
* pidk
)
7596 /* If it's a `(', then we might be looking at a cast. */
7597 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7599 tree type
= NULL_TREE
;
7600 tree expr
= NULL_TREE
;
7601 bool cast_expression_p
;
7602 const char *saved_message
;
7604 /* There's no way to know yet whether or not this is a cast.
7605 For example, `(int (3))' is a unary-expression, while `(int)
7606 3' is a cast. So, we resort to parsing tentatively. */
7607 cp_parser_parse_tentatively (parser
);
7608 /* Types may not be defined in a cast. */
7609 saved_message
= parser
->type_definition_forbidden_message
;
7610 parser
->type_definition_forbidden_message
7611 = G_("types may not be defined in casts");
7612 /* Consume the `('. */
7613 cp_lexer_consume_token (parser
->lexer
);
7614 /* A very tricky bit is that `(struct S) { 3 }' is a
7615 compound-literal (which we permit in C++ as an extension).
7616 But, that construct is not a cast-expression -- it is a
7617 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7618 is legal; if the compound-literal were a cast-expression,
7619 you'd need an extra set of parentheses.) But, if we parse
7620 the type-id, and it happens to be a class-specifier, then we
7621 will commit to the parse at that point, because we cannot
7622 undo the action that is done when creating a new class. So,
7623 then we cannot back up and do a postfix-expression.
7624 Another tricky case is the following (c++/29234):
7626 struct S { void operator () (); };
7633 As a type-id we parse the parenthesized S()() as a function
7634 returning a function, groktypename complains and we cannot
7635 back up in this case either.
7637 Therefore, we scan ahead to the closing `)', and check to see
7638 if the tokens after the `)' can start a cast-expression. Otherwise
7639 we are dealing with an unary-expression, a postfix-expression
7642 Save tokens so that we can put them back. */
7643 cp_lexer_save_tokens (parser
->lexer
);
7645 /* We may be looking at a cast-expression. */
7647 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7648 /*consume_paren=*/true)
7649 && cp_parser_tokens_start_cast_expression (parser
));
7651 /* Roll back the tokens we skipped. */
7652 cp_lexer_rollback_tokens (parser
->lexer
);
7653 /* If we aren't looking at a cast-expression, simulate an error so
7654 that the call to cp_parser_parse_definitely below will fail. */
7655 if (!cast_expression_p
)
7656 cp_parser_simulate_error (parser
);
7659 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7660 parser
->in_type_id_in_expr_p
= true;
7661 /* Look for the type-id. */
7662 type
= cp_parser_type_id (parser
);
7663 /* Look for the closing `)'. */
7664 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7665 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7668 /* Restore the saved message. */
7669 parser
->type_definition_forbidden_message
= saved_message
;
7671 /* At this point this can only be either a cast or a
7672 parenthesized ctor such as `(T ())' that looks like a cast to
7673 function returning T. */
7674 if (!cp_parser_error_occurred (parser
))
7676 cp_parser_parse_definitely (parser
);
7677 expr
= cp_parser_cast_expression (parser
,
7678 /*address_p=*/false,
7680 /*decltype_p=*/false,
7683 /* Warn about old-style casts, if so requested. */
7684 if (warn_old_style_cast
7685 && !in_system_header
7686 && !VOID_TYPE_P (type
)
7687 && current_lang_name
!= lang_name_c
)
7688 warning (OPT_Wold_style_cast
, "use of old-style cast");
7690 /* Only type conversions to integral or enumeration types
7691 can be used in constant-expressions. */
7692 if (!cast_valid_in_integral_constant_expression_p (type
)
7693 && cp_parser_non_integral_constant_expression (parser
,
7695 return error_mark_node
;
7697 /* Perform the cast. */
7698 expr
= build_c_cast (input_location
, type
, expr
);
7702 cp_parser_abort_tentative_parse (parser
);
7705 /* If we get here, then it's not a cast, so it must be a
7706 unary-expression. */
7707 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7711 /* Parse a binary expression of the general form:
7715 pm-expression .* cast-expression
7716 pm-expression ->* cast-expression
7718 multiplicative-expression:
7720 multiplicative-expression * pm-expression
7721 multiplicative-expression / pm-expression
7722 multiplicative-expression % pm-expression
7724 additive-expression:
7725 multiplicative-expression
7726 additive-expression + multiplicative-expression
7727 additive-expression - multiplicative-expression
7731 shift-expression << additive-expression
7732 shift-expression >> additive-expression
7734 relational-expression:
7736 relational-expression < shift-expression
7737 relational-expression > shift-expression
7738 relational-expression <= shift-expression
7739 relational-expression >= shift-expression
7743 relational-expression:
7744 relational-expression <? shift-expression
7745 relational-expression >? shift-expression
7747 equality-expression:
7748 relational-expression
7749 equality-expression == relational-expression
7750 equality-expression != relational-expression
7754 and-expression & equality-expression
7756 exclusive-or-expression:
7758 exclusive-or-expression ^ and-expression
7760 inclusive-or-expression:
7761 exclusive-or-expression
7762 inclusive-or-expression | exclusive-or-expression
7764 logical-and-expression:
7765 inclusive-or-expression
7766 logical-and-expression && inclusive-or-expression
7768 logical-or-expression:
7769 logical-and-expression
7770 logical-or-expression || logical-and-expression
7772 All these are implemented with a single function like:
7775 simple-cast-expression
7776 binary-expression <token> binary-expression
7778 CAST_P is true if this expression is the target of a cast.
7780 The binops_by_token map is used to get the tree codes for each <token> type.
7781 binary-expressions are associated according to a precedence table. */
7783 #define TOKEN_PRECEDENCE(token) \
7784 (((token->type == CPP_GREATER \
7785 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7786 && !parser->greater_than_is_operator_p) \
7787 ? PREC_NOT_OPERATOR \
7788 : binops_by_token[token->type].prec)
7791 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7792 bool no_toplevel_fold_p
,
7794 enum cp_parser_prec prec
,
7797 cp_parser_expression_stack stack
;
7798 cp_parser_expression_stack_entry
*sp
= &stack
[0];
7799 cp_parser_expression_stack_entry current
;
7802 enum tree_code rhs_type
;
7803 enum cp_parser_prec new_prec
, lookahead_prec
;
7806 /* Parse the first expression. */
7807 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
7808 cast_p
, decltype_p
, pidk
);
7809 current
.lhs_type
= ERROR_MARK
;
7810 current
.prec
= prec
;
7812 if (cp_parser_error_occurred (parser
))
7813 return error_mark_node
;
7817 /* Get an operator token. */
7818 token
= cp_lexer_peek_token (parser
->lexer
);
7820 if (warn_cxx0x_compat
7821 && token
->type
== CPP_RSHIFT
7822 && !parser
->greater_than_is_operator_p
)
7824 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
7825 "%<>>%> operator is treated"
7826 " as two right angle brackets in C++11"))
7827 inform (token
->location
,
7828 "suggest parentheses around %<>>%> expression");
7831 new_prec
= TOKEN_PRECEDENCE (token
);
7833 /* Popping an entry off the stack means we completed a subexpression:
7834 - either we found a token which is not an operator (`>' where it is not
7835 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7836 will happen repeatedly;
7837 - or, we found an operator which has lower priority. This is the case
7838 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7840 if (new_prec
<= current
.prec
)
7849 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
7850 current
.loc
= token
->location
;
7852 /* We used the operator token. */
7853 cp_lexer_consume_token (parser
->lexer
);
7855 /* For "false && x" or "true || x", x will never be executed;
7856 disable warnings while evaluating it. */
7857 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7858 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
7859 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7860 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
7862 /* Extract another operand. It may be the RHS of this expression
7863 or the LHS of a new, higher priority expression. */
7864 rhs
= cp_parser_simple_cast_expression (parser
);
7865 rhs_type
= ERROR_MARK
;
7867 /* Get another operator token. Look up its precedence to avoid
7868 building a useless (immediately popped) stack entry for common
7869 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7870 token
= cp_lexer_peek_token (parser
->lexer
);
7871 lookahead_prec
= TOKEN_PRECEDENCE (token
);
7872 if (lookahead_prec
> new_prec
)
7874 /* ... and prepare to parse the RHS of the new, higher priority
7875 expression. Since precedence levels on the stack are
7876 monotonically increasing, we do not have to care about
7881 current
.lhs_type
= rhs_type
;
7882 current
.prec
= new_prec
;
7883 new_prec
= lookahead_prec
;
7887 lookahead_prec
= new_prec
;
7888 /* If the stack is not empty, we have parsed into LHS the right side
7889 (`4' in the example above) of an expression we had suspended.
7890 We can use the information on the stack to recover the LHS (`3')
7891 from the stack together with the tree code (`MULT_EXPR'), and
7892 the precedence of the higher level subexpression
7893 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7894 which will be used to actually build the additive expression. */
7896 rhs_type
= current
.lhs_type
;
7901 /* Undo the disabling of warnings done above. */
7902 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7903 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
7904 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7905 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
7908 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7909 ERROR_MARK for everything that is not a binary expression.
7910 This makes warn_about_parentheses miss some warnings that
7911 involve unary operators. For unary expressions we should
7912 pass the correct tree_code unless the unary expression was
7913 surrounded by parentheses.
7915 if (no_toplevel_fold_p
7916 && lookahead_prec
<= current
.prec
7918 current
.lhs
= build2 (current
.tree_type
,
7919 TREE_CODE_CLASS (current
.tree_type
)
7921 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
7924 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
7925 current
.lhs
, current
.lhs_type
,
7926 rhs
, rhs_type
, &overload
,
7927 complain_flags (decltype_p
));
7928 current
.lhs_type
= current
.tree_type
;
7929 if (EXPR_P (current
.lhs
))
7930 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
7932 /* If the binary operator required the use of an overloaded operator,
7933 then this expression cannot be an integral constant-expression.
7934 An overloaded operator can be used even if both operands are
7935 otherwise permissible in an integral constant-expression if at
7936 least one of the operands is of enumeration type. */
7939 && cp_parser_non_integral_constant_expression (parser
,
7941 return error_mark_node
;
7948 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7949 bool no_toplevel_fold_p
,
7950 enum cp_parser_prec prec
,
7953 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
7954 /*decltype*/false, prec
, pidk
);
7957 /* Parse the `? expression : assignment-expression' part of a
7958 conditional-expression. The LOGICAL_OR_EXPR is the
7959 logical-or-expression that started the conditional-expression.
7960 Returns a representation of the entire conditional-expression.
7962 This routine is used by cp_parser_assignment_expression.
7964 ? expression : assignment-expression
7968 ? : assignment-expression */
7971 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
7974 tree assignment_expr
;
7975 struct cp_token
*token
;
7976 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7978 /* Consume the `?' token. */
7979 cp_lexer_consume_token (parser
->lexer
);
7980 token
= cp_lexer_peek_token (parser
->lexer
);
7981 if (cp_parser_allow_gnu_extensions_p (parser
)
7982 && token
->type
== CPP_COLON
)
7984 pedwarn (token
->location
, OPT_Wpedantic
,
7985 "ISO C++ does not allow ?: with omitted middle operand");
7986 /* Implicit true clause. */
7988 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
7989 warn_for_omitted_condop (token
->location
, logical_or_expr
);
7993 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
7994 parser
->colon_corrects_to_scope_p
= false;
7995 /* Parse the expression. */
7996 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
7997 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7998 c_inhibit_evaluation_warnings
+=
7999 ((logical_or_expr
== truthvalue_true_node
)
8000 - (logical_or_expr
== truthvalue_false_node
));
8001 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8004 /* The next token should be a `:'. */
8005 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8006 /* Parse the assignment-expression. */
8007 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
8008 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8010 /* Build the conditional-expression. */
8011 return build_x_conditional_expr (loc
, logical_or_expr
,
8014 tf_warning_or_error
);
8017 /* Parse an assignment-expression.
8019 assignment-expression:
8020 conditional-expression
8021 logical-or-expression assignment-operator assignment_expression
8024 CAST_P is true if this expression is the target of a cast.
8025 DECLTYPE_P is true if this expression is the operand of decltype.
8027 Returns a representation for the expression. */
8030 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
8031 bool decltype_p
, cp_id_kind
* pidk
)
8035 /* If the next token is the `throw' keyword, then we're looking at
8036 a throw-expression. */
8037 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8038 expr
= cp_parser_throw_expression (parser
);
8039 /* Otherwise, it must be that we are looking at a
8040 logical-or-expression. */
8043 /* Parse the binary expressions (logical-or-expression). */
8044 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8046 PREC_NOT_OPERATOR
, pidk
);
8047 /* If the next token is a `?' then we're actually looking at a
8048 conditional-expression. */
8049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8050 return cp_parser_question_colon_clause (parser
, expr
);
8053 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8055 /* If it's an assignment-operator, we're using the second
8057 enum tree_code assignment_operator
8058 = cp_parser_assignment_operator_opt (parser
);
8059 if (assignment_operator
!= ERROR_MARK
)
8061 bool non_constant_p
;
8062 location_t saved_input_location
;
8064 /* Parse the right-hand side of the assignment. */
8065 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8067 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8068 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8070 /* An assignment may not appear in a
8071 constant-expression. */
8072 if (cp_parser_non_integral_constant_expression (parser
,
8074 return error_mark_node
;
8075 /* Build the assignment expression. Its default
8076 location is the location of the '=' token. */
8077 saved_input_location
= input_location
;
8078 input_location
= loc
;
8079 expr
= build_x_modify_expr (loc
, expr
,
8080 assignment_operator
,
8082 complain_flags (decltype_p
));
8083 input_location
= saved_input_location
;
8092 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
8095 return cp_parser_assignment_expression (parser
, cast_p
,
8096 /*decltype*/false, pidk
);
8099 /* Parse an (optional) assignment-operator.
8101 assignment-operator: one of
8102 = *= /= %= += -= >>= <<= &= ^= |=
8106 assignment-operator: one of
8109 If the next token is an assignment operator, the corresponding tree
8110 code is returned, and the token is consumed. For example, for
8111 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8112 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8113 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8114 operator, ERROR_MARK is returned. */
8116 static enum tree_code
8117 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8122 /* Peek at the next token. */
8123 token
= cp_lexer_peek_token (parser
->lexer
);
8125 switch (token
->type
)
8136 op
= TRUNC_DIV_EXPR
;
8140 op
= TRUNC_MOD_EXPR
;
8172 /* Nothing else is an assignment operator. */
8176 /* If it was an assignment operator, consume it. */
8177 if (op
!= ERROR_MARK
)
8178 cp_lexer_consume_token (parser
->lexer
);
8183 /* Parse an expression.
8186 assignment-expression
8187 expression , assignment-expression
8189 CAST_P is true if this expression is the target of a cast.
8190 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8191 except possibly parenthesized or on the RHS of a comma (N3276).
8193 Returns a representation of the expression. */
8196 cp_parser_expression (cp_parser
* parser
, bool cast_p
, bool decltype_p
,
8199 tree expression
= NULL_TREE
;
8200 location_t loc
= UNKNOWN_LOCATION
;
8204 tree assignment_expression
;
8206 /* Parse the next assignment-expression. */
8207 assignment_expression
8208 = cp_parser_assignment_expression (parser
, cast_p
, decltype_p
, pidk
);
8210 /* We don't create a temporary for a call that is the immediate operand
8211 of decltype or on the RHS of a comma. But when we see a comma, we
8212 need to create a temporary for a call on the LHS. */
8213 if (decltype_p
&& !processing_template_decl
8214 && TREE_CODE (assignment_expression
) == CALL_EXPR
8215 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8216 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8217 assignment_expression
8218 = build_cplus_new (TREE_TYPE (assignment_expression
),
8219 assignment_expression
, tf_warning_or_error
);
8221 /* If this is the first assignment-expression, we can just
8224 expression
= assignment_expression
;
8226 expression
= build_x_compound_expr (loc
, expression
,
8227 assignment_expression
,
8228 complain_flags (decltype_p
));
8229 /* If the next token is not a comma, then we are done with the
8231 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8233 /* Consume the `,'. */
8234 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8235 cp_lexer_consume_token (parser
->lexer
);
8236 /* A comma operator cannot appear in a constant-expression. */
8237 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8238 expression
= error_mark_node
;
8245 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
8247 return cp_parser_expression (parser
, cast_p
, /*decltype*/false, pidk
);
8250 /* Parse a constant-expression.
8252 constant-expression:
8253 conditional-expression
8255 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8256 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8257 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8258 is false, NON_CONSTANT_P should be NULL. */
8261 cp_parser_constant_expression (cp_parser
* parser
,
8262 bool allow_non_constant_p
,
8263 bool *non_constant_p
)
8265 bool saved_integral_constant_expression_p
;
8266 bool saved_allow_non_integral_constant_expression_p
;
8267 bool saved_non_integral_constant_expression_p
;
8270 /* It might seem that we could simply parse the
8271 conditional-expression, and then check to see if it were
8272 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8273 one that the compiler can figure out is constant, possibly after
8274 doing some simplifications or optimizations. The standard has a
8275 precise definition of constant-expression, and we must honor
8276 that, even though it is somewhat more restrictive.
8282 is not a legal declaration, because `(2, 3)' is not a
8283 constant-expression. The `,' operator is forbidden in a
8284 constant-expression. However, GCC's constant-folding machinery
8285 will fold this operation to an INTEGER_CST for `3'. */
8287 /* Save the old settings. */
8288 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8289 saved_allow_non_integral_constant_expression_p
8290 = parser
->allow_non_integral_constant_expression_p
;
8291 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8292 /* We are now parsing a constant-expression. */
8293 parser
->integral_constant_expression_p
= true;
8294 parser
->allow_non_integral_constant_expression_p
8295 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8296 parser
->non_integral_constant_expression_p
= false;
8297 /* Although the grammar says "conditional-expression", we parse an
8298 "assignment-expression", which also permits "throw-expression"
8299 and the use of assignment operators. In the case that
8300 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8301 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8302 actually essential that we look for an assignment-expression.
8303 For example, cp_parser_initializer_clauses uses this function to
8304 determine whether a particular assignment-expression is in fact
8306 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
8307 /* Restore the old settings. */
8308 parser
->integral_constant_expression_p
8309 = saved_integral_constant_expression_p
;
8310 parser
->allow_non_integral_constant_expression_p
8311 = saved_allow_non_integral_constant_expression_p
;
8312 if (cxx_dialect
>= cxx11
)
8314 /* Require an rvalue constant expression here; that's what our
8315 callers expect. Reference constant expressions are handled
8316 separately in e.g. cp_parser_template_argument. */
8317 bool is_const
= potential_rvalue_constant_expression (expression
);
8318 parser
->non_integral_constant_expression_p
= !is_const
;
8319 if (!is_const
&& !allow_non_constant_p
)
8320 require_potential_rvalue_constant_expression (expression
);
8322 if (allow_non_constant_p
)
8323 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8324 parser
->non_integral_constant_expression_p
8325 = saved_non_integral_constant_expression_p
;
8330 /* Parse __builtin_offsetof.
8332 offsetof-expression:
8333 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8335 offsetof-member-designator:
8337 | offsetof-member-designator "." id-expression
8338 | offsetof-member-designator "[" expression "]"
8339 | offsetof-member-designator "->" id-expression */
8342 cp_parser_builtin_offsetof (cp_parser
*parser
)
8344 int save_ice_p
, save_non_ice_p
;
8349 /* We're about to accept non-integral-constant things, but will
8350 definitely yield an integral constant expression. Save and
8351 restore these values around our local parsing. */
8352 save_ice_p
= parser
->integral_constant_expression_p
;
8353 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8355 /* Consume the "__builtin_offsetof" token. */
8356 cp_lexer_consume_token (parser
->lexer
);
8357 /* Consume the opening `('. */
8358 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8359 /* Parse the type-id. */
8360 type
= cp_parser_type_id (parser
);
8361 /* Look for the `,'. */
8362 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8363 token
= cp_lexer_peek_token (parser
->lexer
);
8365 /* Build the (type *)null that begins the traditional offsetof macro. */
8366 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8367 tf_warning_or_error
);
8369 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8370 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8371 true, &dummy
, token
->location
);
8374 token
= cp_lexer_peek_token (parser
->lexer
);
8375 switch (token
->type
)
8377 case CPP_OPEN_SQUARE
:
8378 /* offsetof-member-designator "[" expression "]" */
8379 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8384 /* offsetof-member-designator "->" identifier */
8385 expr
= grok_array_decl (token
->location
, expr
,
8386 integer_zero_node
, false);
8390 /* offsetof-member-designator "." identifier */
8391 cp_lexer_consume_token (parser
->lexer
);
8392 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8397 case CPP_CLOSE_PAREN
:
8398 /* Consume the ")" token. */
8399 cp_lexer_consume_token (parser
->lexer
);
8403 /* Error. We know the following require will fail, but
8404 that gives the proper error message. */
8405 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8406 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8407 expr
= error_mark_node
;
8413 /* If we're processing a template, we can't finish the semantics yet.
8414 Otherwise we can fold the entire expression now. */
8415 if (processing_template_decl
)
8416 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
8418 expr
= finish_offsetof (expr
);
8421 parser
->integral_constant_expression_p
= save_ice_p
;
8422 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8427 /* Parse a trait expression.
8429 Returns a representation of the expression, the underlying type
8430 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8433 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8436 tree type1
, type2
= NULL_TREE
;
8437 bool binary
= false;
8438 cp_decl_specifier_seq decl_specs
;
8442 case RID_HAS_NOTHROW_ASSIGN
:
8443 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8445 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8446 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8448 case RID_HAS_NOTHROW_COPY
:
8449 kind
= CPTK_HAS_NOTHROW_COPY
;
8451 case RID_HAS_TRIVIAL_ASSIGN
:
8452 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8454 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8455 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8457 case RID_HAS_TRIVIAL_COPY
:
8458 kind
= CPTK_HAS_TRIVIAL_COPY
;
8460 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8461 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8463 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8464 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8466 case RID_IS_ABSTRACT
:
8467 kind
= CPTK_IS_ABSTRACT
;
8469 case RID_IS_BASE_OF
:
8470 kind
= CPTK_IS_BASE_OF
;
8474 kind
= CPTK_IS_CLASS
;
8476 case RID_IS_CONVERTIBLE_TO
:
8477 kind
= CPTK_IS_CONVERTIBLE_TO
;
8481 kind
= CPTK_IS_EMPTY
;
8484 kind
= CPTK_IS_ENUM
;
8487 kind
= CPTK_IS_FINAL
;
8489 case RID_IS_LITERAL_TYPE
:
8490 kind
= CPTK_IS_LITERAL_TYPE
;
8495 case RID_IS_POLYMORPHIC
:
8496 kind
= CPTK_IS_POLYMORPHIC
;
8498 case RID_IS_STD_LAYOUT
:
8499 kind
= CPTK_IS_STD_LAYOUT
;
8501 case RID_IS_TRIVIAL
:
8502 kind
= CPTK_IS_TRIVIAL
;
8505 kind
= CPTK_IS_UNION
;
8507 case RID_UNDERLYING_TYPE
:
8508 kind
= CPTK_UNDERLYING_TYPE
;
8513 case RID_DIRECT_BASES
:
8514 kind
= CPTK_DIRECT_BASES
;
8520 /* Consume the token. */
8521 cp_lexer_consume_token (parser
->lexer
);
8523 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8525 type1
= cp_parser_type_id (parser
);
8527 if (type1
== error_mark_node
)
8528 return error_mark_node
;
8530 /* Build a trivial decl-specifier-seq. */
8531 clear_decl_specs (&decl_specs
);
8532 decl_specs
.type
= type1
;
8534 /* Call grokdeclarator to figure out what type this is. */
8535 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8536 /*initialized=*/0, /*attrlist=*/NULL
);
8540 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8542 type2
= cp_parser_type_id (parser
);
8544 if (type2
== error_mark_node
)
8545 return error_mark_node
;
8547 /* Build a trivial decl-specifier-seq. */
8548 clear_decl_specs (&decl_specs
);
8549 decl_specs
.type
= type2
;
8551 /* Call grokdeclarator to figure out what type this is. */
8552 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8553 /*initialized=*/0, /*attrlist=*/NULL
);
8556 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8558 /* Complete the trait expression, which may mean either processing
8559 the trait expr now or saving it for template instantiation. */
8562 case CPTK_UNDERLYING_TYPE
:
8563 return finish_underlying_type (type1
);
8565 return finish_bases (type1
, false);
8566 case CPTK_DIRECT_BASES
:
8567 return finish_bases (type1
, true);
8569 return finish_trait_expr (kind
, type1
, type2
);
8573 /* Lambdas that appear in variable initializer or default argument scope
8574 get that in their mangling, so we need to record it. We might as well
8575 use the count for function and namespace scopes as well. */
8576 static GTY(()) tree lambda_scope
;
8577 static GTY(()) int lambda_count
;
8578 typedef struct GTY(()) tree_int
8583 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8586 start_lambda_scope (tree decl
)
8590 /* Once we're inside a function, we ignore other scopes and just push
8591 the function again so that popping works properly. */
8592 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8593 decl
= current_function_decl
;
8594 ti
.t
= lambda_scope
;
8595 ti
.i
= lambda_count
;
8596 vec_safe_push (lambda_scope_stack
, ti
);
8597 if (lambda_scope
!= decl
)
8599 /* Don't reset the count if we're still in the same function. */
8600 lambda_scope
= decl
;
8606 record_lambda_scope (tree lambda
)
8608 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8609 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8613 finish_lambda_scope (void)
8615 tree_int
*p
= &lambda_scope_stack
->last ();
8616 if (lambda_scope
!= p
->t
)
8618 lambda_scope
= p
->t
;
8619 lambda_count
= p
->i
;
8621 lambda_scope_stack
->pop ();
8624 /* Parse a lambda expression.
8627 lambda-introducer lambda-declarator [opt] compound-statement
8629 Returns a representation of the expression. */
8632 cp_parser_lambda_expression (cp_parser
* parser
)
8634 tree lambda_expr
= build_lambda_expr ();
8638 LAMBDA_EXPR_LOCATION (lambda_expr
)
8639 = cp_lexer_peek_token (parser
->lexer
)->location
;
8641 if (cp_unevaluated_operand
)
8642 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8643 "lambda-expression in unevaluated context");
8645 /* We may be in the middle of deferred access check. Disable
8647 push_deferring_access_checks (dk_no_deferred
);
8649 cp_parser_lambda_introducer (parser
, lambda_expr
);
8651 type
= begin_lambda_type (lambda_expr
);
8652 if (type
== error_mark_node
)
8653 return error_mark_node
;
8655 record_lambda_scope (lambda_expr
);
8657 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8658 determine_visibility (TYPE_NAME (type
));
8660 /* Now that we've started the type, add the capture fields for any
8661 explicit captures. */
8662 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8665 /* Inside the class, surrounding template-parameter-lists do not apply. */
8666 unsigned int saved_num_template_parameter_lists
8667 = parser
->num_template_parameter_lists
;
8668 unsigned char in_statement
= parser
->in_statement
;
8669 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
8670 bool fully_implicit_function_template_p
8671 = parser
->fully_implicit_function_template_p
;
8672 tree implicit_template_parms
= parser
->implicit_template_parms
;
8673 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
8675 parser
->num_template_parameter_lists
= 0;
8676 parser
->in_statement
= 0;
8677 parser
->in_switch_statement_p
= false;
8678 parser
->fully_implicit_function_template_p
= false;
8679 parser
->implicit_template_parms
= 0;
8680 parser
->implicit_template_scope
= 0;
8682 /* By virtue of defining a local class, a lambda expression has access to
8683 the private variables of enclosing classes. */
8685 ok
= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
8688 cp_parser_lambda_body (parser
, lambda_expr
);
8689 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8690 cp_parser_skip_to_end_of_block_or_statement (parser
);
8692 /* The capture list was built up in reverse order; fix that now. */
8693 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
8694 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8697 maybe_add_lambda_conv_op (type
);
8699 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
8701 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
8702 parser
->in_statement
= in_statement
;
8703 parser
->in_switch_statement_p
= in_switch_statement_p
;
8704 parser
->fully_implicit_function_template_p
8705 = fully_implicit_function_template_p
;
8706 parser
->implicit_template_parms
= implicit_template_parms
;
8707 parser
->implicit_template_scope
= implicit_template_scope
;
8710 pop_deferring_access_checks ();
8712 /* This field is only used during parsing of the lambda. */
8713 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
8715 /* This lambda shouldn't have any proxies left at this point. */
8716 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
8717 /* And now that we're done, push proxies for an enclosing lambda. */
8718 insert_pending_capture_proxies ();
8721 return build_lambda_object (lambda_expr
);
8723 return error_mark_node
;
8726 /* Parse the beginning of a lambda expression.
8729 [ lambda-capture [opt] ]
8731 LAMBDA_EXPR is the current representation of the lambda expression. */
8734 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
8736 /* Need commas after the first capture. */
8739 /* Eat the leading `['. */
8740 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8742 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8743 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
8744 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
8745 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
8746 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8747 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
8749 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
8751 cp_lexer_consume_token (parser
->lexer
);
8755 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
8757 cp_token
* capture_token
;
8759 tree capture_init_expr
;
8760 cp_id_kind idk
= CP_ID_KIND_NONE
;
8761 bool explicit_init_p
= false;
8763 enum capture_kind_type
8768 enum capture_kind_type capture_kind
= BY_COPY
;
8770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
8772 error ("expected end of capture-list");
8779 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8781 /* Possibly capture `this'. */
8782 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
8784 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8785 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
8786 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
8787 "with by-copy capture default");
8788 cp_lexer_consume_token (parser
->lexer
);
8789 add_capture (lambda_expr
,
8790 /*id=*/this_identifier
,
8791 /*initializer=*/finish_this_expr(),
8792 /*by_reference_p=*/false,
8797 /* Remember whether we want to capture as a reference or not. */
8798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
8800 capture_kind
= BY_REFERENCE
;
8801 cp_lexer_consume_token (parser
->lexer
);
8804 /* Get the identifier. */
8805 capture_token
= cp_lexer_peek_token (parser
->lexer
);
8806 capture_id
= cp_parser_identifier (parser
);
8808 if (capture_id
== error_mark_node
)
8809 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8810 delimiters, but I modified this to stop on unnested ']' as well. It
8811 was already changed to stop on unnested '}', so the
8812 "closing_parenthesis" name is no more misleading with my change. */
8814 cp_parser_skip_to_closing_parenthesis (parser
,
8815 /*recovering=*/true,
8817 /*consume_paren=*/true);
8821 /* Find the initializer for this capture. */
8822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
8823 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
8824 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8826 bool direct
, non_constant
;
8827 /* An explicit initializer exists. */
8828 if (cxx_dialect
< cxx1y
)
8829 pedwarn (input_location
, 0,
8830 "lambda capture initializers "
8831 "only available with -std=c++1y or -std=gnu++1y");
8832 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
8834 explicit_init_p
= true;
8838 const char* error_msg
;
8840 /* Turn the identifier into an id-expression. */
8842 = cp_parser_lookup_name_simple (parser
, capture_id
,
8843 capture_token
->location
);
8845 if (capture_init_expr
== error_mark_node
)
8847 unqualified_name_lookup_error (capture_id
);
8850 else if (DECL_P (capture_init_expr
)
8851 && (!VAR_P (capture_init_expr
)
8852 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
8854 error_at (capture_token
->location
,
8855 "capture of non-variable %qD ",
8857 inform (0, "%q+#D declared here", capture_init_expr
);
8860 if (VAR_P (capture_init_expr
)
8861 && decl_storage_duration (capture_init_expr
) != dk_auto
)
8863 pedwarn (capture_token
->location
, 0, "capture of variable "
8864 "%qD with non-automatic storage duration",
8866 inform (0, "%q+#D declared here", capture_init_expr
);
8871 = finish_id_expression
8876 /*integral_constant_expression_p=*/false,
8877 /*allow_non_integral_constant_expression_p=*/false,
8878 /*non_integral_constant_expression_p=*/NULL
,
8879 /*template_p=*/false,
8881 /*address_p=*/false,
8882 /*template_arg_p=*/false,
8884 capture_token
->location
);
8886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8888 cp_lexer_consume_token (parser
->lexer
);
8889 capture_init_expr
= make_pack_expansion (capture_init_expr
);
8892 check_for_bare_parameter_packs (capture_init_expr
);
8895 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
8896 && !explicit_init_p
)
8898 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
8899 && capture_kind
== BY_COPY
)
8900 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
8901 "of %qD redundant with by-copy capture default",
8903 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
8904 && capture_kind
== BY_REFERENCE
)
8905 pedwarn (capture_token
->location
, 0, "explicit by-reference "
8906 "capture of %qD redundant with by-reference capture "
8907 "default", capture_id
);
8910 add_capture (lambda_expr
,
8913 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
8917 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8920 /* Parse the (optional) middle of a lambda expression.
8923 < template-parameter-list [opt] >
8924 ( parameter-declaration-clause [opt] )
8925 attribute-specifier [opt]
8927 exception-specification [opt]
8928 lambda-return-type-clause [opt]
8930 LAMBDA_EXPR is the current representation of the lambda expression. */
8933 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
8935 /* 5.1.1.4 of the standard says:
8936 If a lambda-expression does not include a lambda-declarator, it is as if
8937 the lambda-declarator were ().
8938 This means an empty parameter list, no attributes, and no exception
8940 tree param_list
= void_list_node
;
8941 tree attributes
= NULL_TREE
;
8942 tree exception_spec
= NULL_TREE
;
8943 tree template_param_list
= NULL_TREE
;
8945 /* The template-parameter-list is optional, but must begin with
8946 an opening angle if present. */
8947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
8949 if (cxx_dialect
< cxx1y
)
8950 pedwarn (parser
->lexer
->next_token
->location
, 0,
8951 "lambda templates are only available with "
8952 "-std=c++1y or -std=gnu++1y");
8954 cp_lexer_consume_token (parser
->lexer
);
8956 template_param_list
= cp_parser_template_parameter_list (parser
);
8958 cp_parser_skip_to_end_of_template_parameter_list (parser
);
8960 /* We just processed one more parameter list. */
8961 ++parser
->num_template_parameter_lists
;
8964 /* The parameter-declaration-clause is optional (unless
8965 template-parameter-list was given), but must begin with an
8966 opening parenthesis if present. */
8967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8969 cp_lexer_consume_token (parser
->lexer
);
8971 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
8973 /* Parse parameters. */
8974 param_list
= cp_parser_parameter_declaration_clause (parser
);
8976 /* Default arguments shall not be specified in the
8977 parameter-declaration-clause of a lambda-declarator. */
8978 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
8979 if (TREE_PURPOSE (t
))
8980 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
8981 "default argument specified for lambda parameter");
8983 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8985 attributes
= cp_parser_attributes_opt (parser
);
8987 /* Parse optional `mutable' keyword. */
8988 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
8990 cp_lexer_consume_token (parser
->lexer
);
8991 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
8994 /* Parse optional exception specification. */
8995 exception_spec
= cp_parser_exception_specification_opt (parser
);
8997 /* Parse optional trailing return type. */
8998 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9000 cp_lexer_consume_token (parser
->lexer
);
9001 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9002 = cp_parser_trailing_type_id (parser
);
9005 /* The function parameters must be in scope all the way until after the
9006 trailing-return-type in case of decltype. */
9007 pop_bindings_and_leave_scope ();
9009 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9010 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9012 /* Create the function call operator.
9014 Messing with declarators like this is no uglier than building up the
9015 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9018 cp_decl_specifier_seq return_type_specs
;
9019 cp_declarator
* declarator
;
9024 clear_decl_specs (&return_type_specs
);
9025 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9026 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9028 /* Maybe we will deduce the return type later. */
9029 return_type_specs
.type
= make_auto ();
9031 p
= obstack_alloc (&declarator_obstack
, 0);
9033 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9036 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9037 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9038 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9039 VIRT_SPEC_UNSPECIFIED
,
9042 /*late_return_type=*/NULL_TREE
);
9043 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9045 fco
= grokmethod (&return_type_specs
,
9048 if (fco
!= error_mark_node
)
9050 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9051 DECL_ARTIFICIAL (fco
) = 1;
9052 /* Give the object parameter a different name. */
9053 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9054 if (template_param_list
)
9056 fco
= finish_member_template_decl (fco
);
9057 finish_template_decl (template_param_list
);
9058 --parser
->num_template_parameter_lists
;
9060 else if (parser
->fully_implicit_function_template_p
)
9061 fco
= finish_fully_implicit_template (parser
, fco
);
9064 finish_member_declaration (fco
);
9066 obstack_free (&declarator_obstack
, p
);
9068 return (fco
!= error_mark_node
);
9072 /* Parse the body of a lambda expression, which is simply
9076 but which requires special handling.
9077 LAMBDA_EXPR is the current representation of the lambda expression. */
9080 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9082 bool nested
= (current_function_decl
!= NULL_TREE
);
9083 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9085 push_function_context ();
9087 /* Still increment function_depth so that we don't GC in the
9088 middle of an expression. */
9090 /* Clear this in case we're in the middle of a default argument. */
9091 parser
->local_variables_forbidden_p
= false;
9093 /* Finish the function call operator
9095 + late_parsing_for_member
9096 + function_definition_after_declarator
9097 + ctor_initializer_opt_and_function_body */
9099 tree fco
= lambda_function (lambda_expr
);
9105 /* Let the front end know that we are going to be defining this
9107 start_preparsed_function (fco
,
9109 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9111 start_lambda_scope (fco
);
9112 body
= begin_function_body ();
9114 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9117 /* Push the proxies for any explicit captures. */
9118 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9119 cap
= TREE_CHAIN (cap
))
9120 build_capture_proxy (TREE_PURPOSE (cap
));
9122 compound_stmt
= begin_compound_stmt (0);
9124 /* 5.1.1.4 of the standard says:
9125 If a lambda-expression does not include a trailing-return-type, it
9126 is as if the trailing-return-type denotes the following type:
9127 * if the compound-statement is of the form
9128 { return attribute-specifier [opt] expression ; }
9129 the type of the returned expression after lvalue-to-rvalue
9130 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9131 (_conv.array_ 4.2), and function-to-pointer conversion
9133 * otherwise, void. */
9135 /* In a lambda that has neither a lambda-return-type-clause
9136 nor a deducible form, errors should be reported for return statements
9137 in the body. Since we used void as the placeholder return type, parsing
9138 the body as usual will give such desired behavior. */
9139 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9140 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9141 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9143 tree expr
= NULL_TREE
;
9144 cp_id_kind idk
= CP_ID_KIND_NONE
;
9146 /* Parse tentatively in case there's more after the initial return
9148 cp_parser_parse_tentatively (parser
);
9150 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9152 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
9154 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9155 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9157 if (cp_parser_parse_definitely (parser
))
9159 if (!processing_template_decl
)
9160 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9162 /* Will get error here if type not deduced yet. */
9163 finish_return_stmt (expr
);
9171 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9172 cp_parser_label_declaration (parser
);
9173 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9174 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9177 finish_compound_stmt (compound_stmt
);
9180 finish_function_body (body
);
9181 finish_lambda_scope ();
9183 /* Finish the function and generate code for it if necessary. */
9184 tree fn
= finish_function (/*inline*/2);
9186 /* Only expand if the call op is not a template. */
9187 if (!DECL_TEMPLATE_INFO (fco
))
9188 expand_or_defer_fn (fn
);
9191 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9193 pop_function_context();
9198 /* Statements [gram.stmt.stmt] */
9200 /* Parse a statement.
9204 expression-statement
9209 declaration-statement
9216 attribute-specifier-seq (opt) expression-statement
9217 attribute-specifier-seq (opt) compound-statement
9218 attribute-specifier-seq (opt) selection-statement
9219 attribute-specifier-seq (opt) iteration-statement
9220 attribute-specifier-seq (opt) jump-statement
9221 declaration-statement
9222 attribute-specifier-seq (opt) try-block
9229 IN_COMPOUND is true when the statement is nested inside a
9230 cp_parser_compound_statement; this matters for certain pragmas.
9232 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9233 is a (possibly labeled) if statement which is not enclosed in braces
9234 and has an else clause. This is used to implement -Wparentheses. */
9237 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9238 bool in_compound
, bool *if_p
)
9240 tree statement
, std_attrs
= NULL_TREE
;
9242 location_t statement_location
, attrs_location
;
9247 /* There is no statement yet. */
9248 statement
= NULL_TREE
;
9250 cp_lexer_save_tokens (parser
->lexer
);
9251 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9252 if (c_dialect_objc ())
9253 /* In obj-c++, seeing '[[' might be the either the beginning of
9254 c++11 attributes, or a nested objc-message-expression. So
9255 let's parse the c++11 attributes tentatively. */
9256 cp_parser_parse_tentatively (parser
);
9257 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9258 if (c_dialect_objc ())
9260 if (!cp_parser_parse_definitely (parser
))
9261 std_attrs
= NULL_TREE
;
9264 /* Peek at the next token. */
9265 token
= cp_lexer_peek_token (parser
->lexer
);
9266 /* Remember the location of the first token in the statement. */
9267 statement_location
= token
->location
;
9268 /* If this is a keyword, then that will often determine what kind of
9269 statement we have. */
9270 if (token
->type
== CPP_KEYWORD
)
9272 enum rid keyword
= token
->keyword
;
9278 /* Looks like a labeled-statement with a case label.
9279 Parse the label, and then use tail recursion to parse
9281 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9286 statement
= cp_parser_selection_statement (parser
, if_p
);
9292 statement
= cp_parser_iteration_statement (parser
, false);
9299 statement
= cp_parser_jump_statement (parser
);
9302 /* Objective-C++ exception-handling constructs. */
9305 case RID_AT_FINALLY
:
9306 case RID_AT_SYNCHRONIZED
:
9308 statement
= cp_parser_objc_statement (parser
);
9312 statement
= cp_parser_try_block (parser
);
9316 /* This must be a namespace alias definition. */
9317 cp_parser_declaration_statement (parser
);
9320 case RID_TRANSACTION_ATOMIC
:
9321 case RID_TRANSACTION_RELAXED
:
9322 statement
= cp_parser_transaction (parser
, keyword
);
9324 case RID_TRANSACTION_CANCEL
:
9325 statement
= cp_parser_transaction_cancel (parser
);
9329 /* It might be a keyword like `int' that can start a
9330 declaration-statement. */
9334 else if (token
->type
== CPP_NAME
)
9336 /* If the next token is a `:', then we are looking at a
9337 labeled-statement. */
9338 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9339 if (token
->type
== CPP_COLON
)
9341 /* Looks like a labeled-statement with an ordinary label.
9342 Parse the label, and then use tail recursion to parse
9345 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9349 /* Anything that starts with a `{' must be a compound-statement. */
9350 else if (token
->type
== CPP_OPEN_BRACE
)
9351 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9352 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9353 a statement all its own. */
9354 else if (token
->type
== CPP_PRAGMA
)
9356 /* Only certain OpenMP pragmas are attached to statements, and thus
9357 are considered statements themselves. All others are not. In
9358 the context of a compound, accept the pragma as a "statement" and
9359 return so that we can check for a close brace. Otherwise we
9360 require a real statement and must go back and read one. */
9362 cp_parser_pragma (parser
, pragma_compound
);
9363 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9367 else if (token
->type
== CPP_EOF
)
9369 cp_parser_error (parser
, "expected statement");
9373 /* Everything else must be a declaration-statement or an
9374 expression-statement. Try for the declaration-statement
9375 first, unless we are looking at a `;', in which case we know that
9376 we have an expression-statement. */
9379 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9381 if (std_attrs
!= NULL_TREE
)
9383 /* Attributes should be parsed as part of the the
9384 declaration, so let's un-parse them. */
9385 cp_lexer_rollback_tokens (parser
->lexer
);
9386 std_attrs
= NULL_TREE
;
9389 cp_parser_parse_tentatively (parser
);
9390 /* Try to parse the declaration-statement. */
9391 cp_parser_declaration_statement (parser
);
9392 /* If that worked, we're done. */
9393 if (cp_parser_parse_definitely (parser
))
9396 /* Look for an expression-statement instead. */
9397 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9400 /* Set the line number for the statement. */
9401 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9402 SET_EXPR_LOCATION (statement
, statement_location
);
9404 /* Note that for now, we don't do anything with c++11 statements
9405 parsed at this level. */
9406 if (std_attrs
!= NULL_TREE
)
9407 warning_at (attrs_location
,
9409 "attributes at the beginning of statement are ignored");
9412 /* Parse the label for a labeled-statement, i.e.
9415 case constant-expression :
9419 case constant-expression ... constant-expression : statement
9421 When a label is parsed without errors, the label is added to the
9422 parse tree by the finish_* functions, so this function doesn't
9423 have to return the label. */
9426 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9429 tree label
= NULL_TREE
;
9430 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9432 /* The next token should be an identifier. */
9433 token
= cp_lexer_peek_token (parser
->lexer
);
9434 if (token
->type
!= CPP_NAME
9435 && token
->type
!= CPP_KEYWORD
)
9437 cp_parser_error (parser
, "expected labeled-statement");
9441 parser
->colon_corrects_to_scope_p
= false;
9442 switch (token
->keyword
)
9449 /* Consume the `case' token. */
9450 cp_lexer_consume_token (parser
->lexer
);
9451 /* Parse the constant-expression. */
9452 expr
= cp_parser_constant_expression (parser
,
9453 /*allow_non_constant_p=*/false,
9456 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9457 if (ellipsis
->type
== CPP_ELLIPSIS
)
9459 /* Consume the `...' token. */
9460 cp_lexer_consume_token (parser
->lexer
);
9462 cp_parser_constant_expression (parser
,
9463 /*allow_non_constant_p=*/false,
9465 /* We don't need to emit warnings here, as the common code
9466 will do this for us. */
9469 expr_hi
= NULL_TREE
;
9471 if (parser
->in_switch_statement_p
)
9472 finish_case_label (token
->location
, expr
, expr_hi
);
9474 error_at (token
->location
,
9475 "case label %qE not within a switch statement",
9481 /* Consume the `default' token. */
9482 cp_lexer_consume_token (parser
->lexer
);
9484 if (parser
->in_switch_statement_p
)
9485 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9487 error_at (token
->location
, "case label not within a switch statement");
9491 /* Anything else must be an ordinary label. */
9492 label
= finish_label_stmt (cp_parser_identifier (parser
));
9496 /* Require the `:' token. */
9497 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9499 /* An ordinary label may optionally be followed by attributes.
9500 However, this is only permitted if the attributes are then
9501 followed by a semicolon. This is because, for backward
9502 compatibility, when parsing
9503 lab: __attribute__ ((unused)) int i;
9504 we want the attribute to attach to "i", not "lab". */
9505 if (label
!= NULL_TREE
9506 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9509 cp_parser_parse_tentatively (parser
);
9510 attrs
= cp_parser_gnu_attributes_opt (parser
);
9511 if (attrs
== NULL_TREE
9512 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9513 cp_parser_abort_tentative_parse (parser
);
9514 else if (!cp_parser_parse_definitely (parser
))
9517 attributes
= chainon (attributes
, attrs
);
9520 if (attributes
!= NULL_TREE
)
9521 cplus_decl_attributes (&label
, attributes
, 0);
9523 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9526 /* Parse an expression-statement.
9528 expression-statement:
9531 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9532 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9533 indicates whether this expression-statement is part of an
9534 expression statement. */
9537 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9539 tree statement
= NULL_TREE
;
9540 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9542 /* If the next token is a ';', then there is no expression
9544 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9546 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9547 if (statement
== error_mark_node
9548 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9550 cp_parser_skip_to_end_of_block_or_statement (parser
);
9551 return error_mark_node
;
9555 /* Give a helpful message for "A<T>::type t;" and the like. */
9556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9557 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9559 if (TREE_CODE (statement
) == SCOPE_REF
)
9560 error_at (token
->location
, "need %<typename%> before %qE because "
9561 "%qT is a dependent scope",
9562 statement
, TREE_OPERAND (statement
, 0));
9563 else if (is_overloaded_fn (statement
)
9564 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9567 tree fn
= get_first_fn (statement
);
9568 error_at (token
->location
,
9569 "%<%T::%D%> names the constructor, not the type",
9570 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9574 /* Consume the final `;'. */
9575 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9577 if (in_statement_expr
9578 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9579 /* This is the final expression statement of a statement
9581 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9583 statement
= finish_expr_stmt (statement
);
9588 /* Parse a compound-statement.
9591 { statement-seq [opt] }
9596 { label-declaration-seq [opt] statement-seq [opt] }
9598 label-declaration-seq:
9600 label-declaration-seq label-declaration
9602 Returns a tree representing the statement. */
9605 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
9606 bool in_try
, bool function_body
)
9610 /* Consume the `{'. */
9611 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9612 return error_mark_node
;
9613 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
9615 pedwarn (input_location
, OPT_Wpedantic
,
9616 "compound-statement in constexpr function");
9617 /* Begin the compound-statement. */
9618 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
9619 /* If the next keyword is `__label__' we have a label declaration. */
9620 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9621 cp_parser_label_declaration (parser
);
9622 /* Parse an (optional) statement-seq. */
9623 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
9624 /* Finish the compound-statement. */
9625 finish_compound_stmt (compound_stmt
);
9626 /* Consume the `}'. */
9627 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9629 return compound_stmt
;
9632 /* Parse an (optional) statement-seq.
9636 statement-seq [opt] statement */
9639 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
9641 /* Scan statements until there aren't any more. */
9644 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9646 /* If we are looking at a `}', then we have run out of
9647 statements; the same is true if we have reached the end
9648 of file, or have stumbled upon a stray '@end'. */
9649 if (token
->type
== CPP_CLOSE_BRACE
9650 || token
->type
== CPP_EOF
9651 || token
->type
== CPP_PRAGMA_EOL
9652 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
9655 /* If we are in a compound statement and find 'else' then
9656 something went wrong. */
9657 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
9659 if (parser
->in_statement
& IN_IF_STMT
)
9663 token
= cp_lexer_consume_token (parser
->lexer
);
9664 error_at (token
->location
, "%<else%> without a previous %<if%>");
9668 /* Parse the statement. */
9669 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
9673 /* Parse a selection-statement.
9675 selection-statement:
9676 if ( condition ) statement
9677 if ( condition ) statement else statement
9678 switch ( condition ) statement
9680 Returns the new IF_STMT or SWITCH_STMT.
9682 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9683 is a (possibly labeled) if statement which is not enclosed in
9684 braces and has an else clause. This is used to implement
9688 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
9696 /* Peek at the next token. */
9697 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
9699 /* See what kind of keyword it is. */
9700 keyword
= token
->keyword
;
9709 /* Look for the `('. */
9710 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
9712 cp_parser_skip_to_end_of_statement (parser
);
9713 return error_mark_node
;
9716 /* Begin the selection-statement. */
9717 if (keyword
== RID_IF
)
9718 statement
= begin_if_stmt ();
9720 statement
= begin_switch_stmt ();
9722 /* Parse the condition. */
9723 condition
= cp_parser_condition (parser
);
9724 /* Look for the `)'. */
9725 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
9726 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9727 /*consume_paren=*/true);
9729 if (keyword
== RID_IF
)
9732 unsigned char in_statement
;
9734 /* Add the condition. */
9735 finish_if_stmt_cond (condition
, statement
);
9737 /* Parse the then-clause. */
9738 in_statement
= parser
->in_statement
;
9739 parser
->in_statement
|= IN_IF_STMT
;
9740 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9742 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9743 add_stmt (build_empty_stmt (loc
));
9744 cp_lexer_consume_token (parser
->lexer
);
9745 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
9746 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
9747 "empty body in an %<if%> statement");
9751 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
9752 parser
->in_statement
= in_statement
;
9754 finish_then_clause (statement
);
9756 /* If the next token is `else', parse the else-clause. */
9757 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9760 /* Consume the `else' keyword. */
9761 cp_lexer_consume_token (parser
->lexer
);
9762 begin_else_clause (statement
);
9763 /* Parse the else-clause. */
9764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9767 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9769 OPT_Wempty_body
, "suggest braces around "
9770 "empty body in an %<else%> statement");
9771 add_stmt (build_empty_stmt (loc
));
9772 cp_lexer_consume_token (parser
->lexer
);
9775 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9777 finish_else_clause (statement
);
9779 /* If we are currently parsing a then-clause, then
9780 IF_P will not be NULL. We set it to true to
9781 indicate that this if statement has an else clause.
9782 This may trigger the Wparentheses warning below
9783 when we get back up to the parent if statement. */
9789 /* This if statement does not have an else clause. If
9790 NESTED_IF is true, then the then-clause is an if
9791 statement which does have an else clause. We warn
9792 about the potential ambiguity. */
9794 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
9795 "suggest explicit braces to avoid ambiguous"
9799 /* Now we're all done with the if-statement. */
9800 finish_if_stmt (statement
);
9804 bool in_switch_statement_p
;
9805 unsigned char in_statement
;
9807 /* Add the condition. */
9808 finish_switch_cond (condition
, statement
);
9810 /* Parse the body of the switch-statement. */
9811 in_switch_statement_p
= parser
->in_switch_statement_p
;
9812 in_statement
= parser
->in_statement
;
9813 parser
->in_switch_statement_p
= true;
9814 parser
->in_statement
|= IN_SWITCH_STMT
;
9815 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9816 parser
->in_switch_statement_p
= in_switch_statement_p
;
9817 parser
->in_statement
= in_statement
;
9819 /* Now we're all done with the switch-statement. */
9820 finish_switch_stmt (statement
);
9828 cp_parser_error (parser
, "expected selection-statement");
9829 return error_mark_node
;
9833 /* Parse a condition.
9837 type-specifier-seq declarator = initializer-clause
9838 type-specifier-seq declarator braced-init-list
9843 type-specifier-seq declarator asm-specification [opt]
9844 attributes [opt] = assignment-expression
9846 Returns the expression that should be tested. */
9849 cp_parser_condition (cp_parser
* parser
)
9851 cp_decl_specifier_seq type_specifiers
;
9852 const char *saved_message
;
9853 int declares_class_or_enum
;
9855 /* Try the declaration first. */
9856 cp_parser_parse_tentatively (parser
);
9857 /* New types are not allowed in the type-specifier-seq for a
9859 saved_message
= parser
->type_definition_forbidden_message
;
9860 parser
->type_definition_forbidden_message
9861 = G_("types may not be defined in conditions");
9862 /* Parse the type-specifier-seq. */
9863 cp_parser_decl_specifier_seq (parser
,
9864 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
9866 &declares_class_or_enum
);
9867 /* Restore the saved message. */
9868 parser
->type_definition_forbidden_message
= saved_message
;
9869 /* If all is well, we might be looking at a declaration. */
9870 if (!cp_parser_error_occurred (parser
))
9873 tree asm_specification
;
9875 cp_declarator
*declarator
;
9876 tree initializer
= NULL_TREE
;
9878 /* Parse the declarator. */
9879 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9880 /*ctor_dtor_or_conv_p=*/NULL
,
9881 /*parenthesized_p=*/NULL
,
9882 /*member_p=*/false);
9883 /* Parse the attributes. */
9884 attributes
= cp_parser_attributes_opt (parser
);
9885 /* Parse the asm-specification. */
9886 asm_specification
= cp_parser_asm_specification_opt (parser
);
9887 /* If the next token is not an `=' or '{', then we might still be
9888 looking at an expression. For example:
9892 looks like a decl-specifier-seq and a declarator -- but then
9893 there is no `=', so this is an expression. */
9894 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9895 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9896 cp_parser_simulate_error (parser
);
9898 /* If we did see an `=' or '{', then we are looking at a declaration
9900 if (cp_parser_parse_definitely (parser
))
9903 bool non_constant_p
;
9904 bool flags
= LOOKUP_ONLYCONVERTING
;
9906 /* Create the declaration. */
9907 decl
= start_decl (declarator
, &type_specifiers
,
9908 /*initialized_p=*/true,
9909 attributes
, /*prefix_attributes=*/NULL_TREE
,
9912 /* Parse the initializer. */
9913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9915 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
9916 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
9921 /* Consume the `='. */
9922 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
9923 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
9925 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
9926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9928 /* Process the initializer. */
9929 cp_finish_decl (decl
,
9930 initializer
, !non_constant_p
,
9935 pop_scope (pushed_scope
);
9937 return convert_from_reference (decl
);
9940 /* If we didn't even get past the declarator successfully, we are
9941 definitely not looking at a declaration. */
9943 cp_parser_abort_tentative_parse (parser
);
9945 /* Otherwise, we are looking at an expression. */
9946 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9949 /* Parses a for-statement or range-for-statement until the closing ')',
9953 cp_parser_for (cp_parser
*parser
, bool ivdep
)
9955 tree init
, scope
, decl
;
9958 /* Begin the for-statement. */
9959 scope
= begin_for_scope (&init
);
9961 /* Parse the initialization. */
9962 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
9965 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
9967 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
9971 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
9973 /* Normal for loop */
9974 tree condition
= NULL_TREE
;
9975 tree expression
= NULL_TREE
;
9978 stmt
= begin_for_stmt (scope
, init
);
9979 /* The for-init-statement has already been parsed in
9980 cp_parser_for_init_statement, so no work is needed here. */
9981 finish_for_init_stmt (stmt
);
9983 /* If there's a condition, process it. */
9984 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9985 condition
= cp_parser_condition (parser
);
9988 cp_parser_error (parser
, "missing loop condition in loop with "
9989 "%<GCC ivdep%> pragma");
9990 condition
= error_mark_node
;
9992 finish_for_cond (condition
, stmt
, ivdep
);
9993 /* Look for the `;'. */
9994 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9996 /* If there's an expression, process it. */
9997 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
9998 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9999 finish_for_expr (expression
, stmt
);
10004 /* Tries to parse a range-based for-statement:
10007 decl-specifier-seq declarator : expression
10009 The decl-specifier-seq declarator and the `:' are already parsed by
10010 cp_parser_for_init_statement. If processing_template_decl it returns a
10011 newly created RANGE_FOR_STMT; if not, it is converted to a
10012 regular FOR_STMT. */
10015 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10018 tree stmt
, range_expr
;
10020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10022 bool expr_non_constant_p
;
10023 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10026 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10028 /* If in template, STMT is converted to a normal for-statement
10029 at instantiation. If not, it is done just ahead. */
10030 if (processing_template_decl
)
10032 if (check_for_bare_parameter_packs (range_expr
))
10033 range_expr
= error_mark_node
;
10034 stmt
= begin_range_for_stmt (scope
, init
);
10036 RANGE_FOR_IVDEP (stmt
) = 1;
10037 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10038 if (!type_dependent_expression_p (range_expr
)
10039 /* do_auto_deduction doesn't mess with template init-lists. */
10040 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10041 do_range_for_auto_deduction (range_decl
, range_expr
);
10045 stmt
= begin_for_stmt (scope
, init
);
10046 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10051 /* Subroutine of cp_convert_range_for: given the initializer expression,
10052 builds up the range temporary. */
10055 build_range_temp (tree range_expr
)
10057 tree range_type
, range_temp
;
10059 /* Find out the type deduced by the declaration
10060 `auto &&__range = range_expr'. */
10061 range_type
= cp_build_reference_type (make_auto (), true);
10062 range_type
= do_auto_deduction (range_type
, range_expr
,
10063 type_uses_auto (range_type
));
10065 /* Create the __range variable. */
10066 range_temp
= build_decl (input_location
, VAR_DECL
,
10067 get_identifier ("__for_range"), range_type
);
10068 TREE_USED (range_temp
) = 1;
10069 DECL_ARTIFICIAL (range_temp
) = 1;
10074 /* Used by cp_parser_range_for in template context: we aren't going to
10075 do a full conversion yet, but we still need to resolve auto in the
10076 type of the for-range-declaration if present. This is basically
10077 a shortcut version of cp_convert_range_for. */
10080 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10082 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10085 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10086 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10087 iter_type
= (cp_parser_perform_range_for_lookup
10088 (range_temp
, &begin_dummy
, &end_dummy
));
10091 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10093 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10094 tf_warning_or_error
);
10095 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10096 iter_decl
, auto_node
);
10101 /* Converts a range-based for-statement into a normal
10102 for-statement, as per the definition.
10104 for (RANGE_DECL : RANGE_EXPR)
10107 should be equivalent to:
10110 auto &&__range = RANGE_EXPR;
10111 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10115 RANGE_DECL = *__begin;
10120 If RANGE_EXPR is an array:
10121 BEGIN_EXPR = __range
10122 END_EXPR = __range + ARRAY_SIZE(__range)
10123 Else if RANGE_EXPR has a member 'begin' or 'end':
10124 BEGIN_EXPR = __range.begin()
10125 END_EXPR = __range.end()
10127 BEGIN_EXPR = begin(__range)
10128 END_EXPR = end(__range);
10130 If __range has a member 'begin' but not 'end', or vice versa, we must
10131 still use the second alternative (it will surely fail, however).
10132 When calling begin()/end() in the third alternative we must use
10133 argument dependent lookup, but always considering 'std' as an associated
10137 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10141 tree iter_type
, begin_expr
, end_expr
;
10142 tree condition
, expression
;
10144 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10145 /* If an error happened previously do nothing or else a lot of
10146 unhelpful errors would be issued. */
10147 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10152 if (TREE_CODE (range_expr
) == VAR_DECL
10153 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10154 /* Can't bind a reference to an array of runtime bound. */
10155 range_temp
= range_expr
;
10158 range_temp
= build_range_temp (range_expr
);
10159 pushdecl (range_temp
);
10160 cp_finish_decl (range_temp
, range_expr
,
10161 /*is_constant_init*/false, NULL_TREE
,
10162 LOOKUP_ONLYCONVERTING
);
10163 range_temp
= convert_from_reference (range_temp
);
10165 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10166 &begin_expr
, &end_expr
);
10169 /* The new for initialization statement. */
10170 begin
= build_decl (input_location
, VAR_DECL
,
10171 get_identifier ("__for_begin"), iter_type
);
10172 TREE_USED (begin
) = 1;
10173 DECL_ARTIFICIAL (begin
) = 1;
10175 cp_finish_decl (begin
, begin_expr
,
10176 /*is_constant_init*/false, NULL_TREE
,
10177 LOOKUP_ONLYCONVERTING
);
10179 end
= build_decl (input_location
, VAR_DECL
,
10180 get_identifier ("__for_end"), iter_type
);
10181 TREE_USED (end
) = 1;
10182 DECL_ARTIFICIAL (end
) = 1;
10184 cp_finish_decl (end
, end_expr
,
10185 /*is_constant_init*/false, NULL_TREE
,
10186 LOOKUP_ONLYCONVERTING
);
10188 finish_for_init_stmt (statement
);
10190 /* The new for condition. */
10191 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10194 NULL
, tf_warning_or_error
);
10195 finish_for_cond (condition
, statement
, ivdep
);
10197 /* The new increment expression. */
10198 expression
= finish_unary_op_expr (input_location
,
10199 PREINCREMENT_EXPR
, begin
,
10200 tf_warning_or_error
);
10201 finish_for_expr (expression
, statement
);
10203 /* The declaration is initialized with *__begin inside the loop body. */
10204 cp_finish_decl (range_decl
,
10205 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10206 tf_warning_or_error
),
10207 /*is_constant_init*/false, NULL_TREE
,
10208 LOOKUP_ONLYCONVERTING
);
10213 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10214 We need to solve both at the same time because the method used
10215 depends on the existence of members begin or end.
10216 Returns the type deduced for the iterator expression. */
10219 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10221 if (error_operand_p (range
))
10223 *begin
= *end
= error_mark_node
;
10224 return error_mark_node
;
10227 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10229 error ("range-based %<for%> expression of type %qT "
10230 "has incomplete type", TREE_TYPE (range
));
10231 *begin
= *end
= error_mark_node
;
10232 return error_mark_node
;
10234 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10236 /* If RANGE is an array, we will use pointer arithmetic. */
10238 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10240 array_type_nelts_top (TREE_TYPE (range
)),
10242 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10246 /* If it is not an array, we must do a bit of magic. */
10247 tree id_begin
, id_end
;
10248 tree member_begin
, member_end
;
10250 *begin
= *end
= error_mark_node
;
10252 id_begin
= get_identifier ("begin");
10253 id_end
= get_identifier ("end");
10254 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10255 /*protect=*/2, /*want_type=*/false,
10256 tf_warning_or_error
);
10257 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10258 /*protect=*/2, /*want_type=*/false,
10259 tf_warning_or_error
);
10261 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10263 /* Use the member functions. */
10264 if (member_begin
!= NULL_TREE
)
10265 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10267 error ("range-based %<for%> expression of type %qT has an "
10268 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10270 if (member_end
!= NULL_TREE
)
10271 *end
= cp_parser_range_for_member_function (range
, id_end
);
10273 error ("range-based %<for%> expression of type %qT has a "
10274 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10278 /* Use global functions with ADL. */
10279 vec
<tree
, va_gc
> *vec
;
10280 vec
= make_tree_vector ();
10282 vec_safe_push (vec
, range
);
10284 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10285 /*include_std=*/true,
10286 tf_warning_or_error
);
10287 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10288 tf_warning_or_error
);
10289 member_end
= perform_koenig_lookup (id_end
, vec
,
10290 /*include_std=*/true,
10291 tf_warning_or_error
);
10292 *end
= finish_call_expr (member_end
, &vec
, false, true,
10293 tf_warning_or_error
);
10295 release_tree_vector (vec
);
10298 /* Last common checks. */
10299 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10301 /* If one of the expressions is an error do no more checks. */
10302 *begin
= *end
= error_mark_node
;
10303 return error_mark_node
;
10305 else if (type_dependent_expression_p (*begin
)
10306 || type_dependent_expression_p (*end
))
10307 /* Can happen, when, eg, in a template context, Koenig lookup
10308 can't resolve begin/end (c++/58503). */
10312 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10313 /* The unqualified type of the __begin and __end temporaries should
10314 be the same, as required by the multiple auto declaration. */
10315 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10316 error ("inconsistent begin/end types in range-based %<for%> "
10317 "statement: %qT and %qT",
10318 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10324 /* Helper function for cp_parser_perform_range_for_lookup.
10325 Builds a tree for RANGE.IDENTIFIER(). */
10328 cp_parser_range_for_member_function (tree range
, tree identifier
)
10331 vec
<tree
, va_gc
> *vec
;
10333 member
= finish_class_member_access_expr (range
, identifier
,
10334 false, tf_warning_or_error
);
10335 if (member
== error_mark_node
)
10336 return error_mark_node
;
10338 vec
= make_tree_vector ();
10339 res
= finish_call_expr (member
, &vec
,
10340 /*disallow_virtual=*/false,
10341 /*koenig_p=*/false,
10342 tf_warning_or_error
);
10343 release_tree_vector (vec
);
10347 /* Parse an iteration-statement.
10349 iteration-statement:
10350 while ( condition ) statement
10351 do statement while ( expression ) ;
10352 for ( for-init-statement condition [opt] ; expression [opt] )
10355 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10358 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10363 unsigned char in_statement
;
10365 /* Peek at the next token. */
10366 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10368 return error_mark_node
;
10370 /* Remember whether or not we are already within an iteration
10372 in_statement
= parser
->in_statement
;
10374 /* See what kind of keyword it is. */
10375 keyword
= token
->keyword
;
10382 /* Begin the while-statement. */
10383 statement
= begin_while_stmt ();
10384 /* Look for the `('. */
10385 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10386 /* Parse the condition. */
10387 condition
= cp_parser_condition (parser
);
10388 finish_while_stmt_cond (condition
, statement
, ivdep
);
10389 /* Look for the `)'. */
10390 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10391 /* Parse the dependent statement. */
10392 parser
->in_statement
= IN_ITERATION_STMT
;
10393 cp_parser_already_scoped_statement (parser
);
10394 parser
->in_statement
= in_statement
;
10395 /* We're done with the while-statement. */
10396 finish_while_stmt (statement
);
10404 /* Begin the do-statement. */
10405 statement
= begin_do_stmt ();
10406 /* Parse the body of the do-statement. */
10407 parser
->in_statement
= IN_ITERATION_STMT
;
10408 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10409 parser
->in_statement
= in_statement
;
10410 finish_do_body (statement
);
10411 /* Look for the `while' keyword. */
10412 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10413 /* Look for the `('. */
10414 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10415 /* Parse the expression. */
10416 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10417 /* We're done with the do-statement. */
10418 finish_do_stmt (expression
, statement
, ivdep
);
10419 /* Look for the `)'. */
10420 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10421 /* Look for the `;'. */
10422 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10428 /* Look for the `('. */
10429 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10431 statement
= cp_parser_for (parser
, ivdep
);
10433 /* Look for the `)'. */
10434 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10436 /* Parse the body of the for-statement. */
10437 parser
->in_statement
= IN_ITERATION_STMT
;
10438 cp_parser_already_scoped_statement (parser
);
10439 parser
->in_statement
= in_statement
;
10441 /* We're done with the for-statement. */
10442 finish_for_stmt (statement
);
10447 cp_parser_error (parser
, "expected iteration-statement");
10448 statement
= error_mark_node
;
10455 /* Parse a for-init-statement or the declarator of a range-based-for.
10456 Returns true if a range-based-for declaration is seen.
10458 for-init-statement:
10459 expression-statement
10460 simple-declaration */
10463 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10465 /* If the next token is a `;', then we have an empty
10466 expression-statement. Grammatically, this is also a
10467 simple-declaration, but an invalid one, because it does not
10468 declare anything. Therefore, if we did not handle this case
10469 specially, we would issue an error message about an invalid
10471 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10473 bool is_range_for
= false;
10474 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10476 parser
->colon_corrects_to_scope_p
= false;
10478 /* We're going to speculatively look for a declaration, falling back
10479 to an expression, if necessary. */
10480 cp_parser_parse_tentatively (parser
);
10481 /* Parse the declaration. */
10482 cp_parser_simple_declaration (parser
,
10483 /*function_definition_allowed_p=*/false,
10485 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10488 /* It is a range-for, consume the ':' */
10489 cp_lexer_consume_token (parser
->lexer
);
10490 is_range_for
= true;
10491 if (cxx_dialect
< cxx11
)
10493 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10494 "range-based %<for%> loops are not allowed "
10496 *decl
= error_mark_node
;
10500 /* The ';' is not consumed yet because we told
10501 cp_parser_simple_declaration not to. */
10502 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10504 if (cp_parser_parse_definitely (parser
))
10505 return is_range_for
;
10506 /* If the tentative parse failed, then we shall need to look for an
10507 expression-statement. */
10509 /* If we are here, it is an expression-statement. */
10510 cp_parser_expression_statement (parser
, NULL_TREE
);
10514 /* Parse a jump-statement.
10519 return expression [opt] ;
10520 return braced-init-list ;
10526 goto * expression ;
10528 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10531 cp_parser_jump_statement (cp_parser
* parser
)
10533 tree statement
= error_mark_node
;
10536 unsigned char in_statement
;
10538 /* Peek at the next token. */
10539 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10541 return error_mark_node
;
10543 /* See what kind of keyword it is. */
10544 keyword
= token
->keyword
;
10548 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
10549 switch (in_statement
)
10552 error_at (token
->location
, "break statement not within loop or switch");
10555 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
10556 || in_statement
== IN_ITERATION_STMT
);
10557 statement
= finish_break_stmt ();
10560 error_at (token
->location
, "invalid exit from OpenMP structured block");
10563 error_at (token
->location
, "break statement used with OpenMP for loop");
10565 case IN_CILK_SIMD_FOR
:
10566 error_at (token
->location
, "break statement used with Cilk Plus for loop");
10569 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10573 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10576 error_at (token
->location
, "continue statement not within a loop");
10578 case IN_CILK_SIMD_FOR
:
10579 error_at (token
->location
,
10580 "continue statement within %<#pragma simd%> loop body");
10581 /* Fall through. */
10582 case IN_ITERATION_STMT
:
10584 statement
= finish_continue_stmt ();
10587 error_at (token
->location
, "invalid exit from OpenMP structured block");
10590 gcc_unreachable ();
10592 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10598 bool expr_non_constant_p
;
10600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10602 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10603 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10605 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10606 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10608 /* If the next token is a `;', then there is no
10611 /* Build the return-statement. */
10612 statement
= finish_return_stmt (expr
);
10613 /* Look for the final `;'. */
10614 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10619 /* Create the goto-statement. */
10620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
10622 /* Issue a warning about this use of a GNU extension. */
10623 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
10624 /* Consume the '*' token. */
10625 cp_lexer_consume_token (parser
->lexer
);
10626 /* Parse the dependent expression. */
10627 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
10630 finish_goto_stmt (cp_parser_identifier (parser
));
10631 /* Look for the final `;'. */
10632 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10636 cp_parser_error (parser
, "expected jump-statement");
10643 /* Parse a declaration-statement.
10645 declaration-statement:
10646 block-declaration */
10649 cp_parser_declaration_statement (cp_parser
* parser
)
10653 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10654 p
= obstack_alloc (&declarator_obstack
, 0);
10656 /* Parse the block-declaration. */
10657 cp_parser_block_declaration (parser
, /*statement_p=*/true);
10659 /* Free any declarators allocated. */
10660 obstack_free (&declarator_obstack
, p
);
10663 /* Some dependent statements (like `if (cond) statement'), are
10664 implicitly in their own scope. In other words, if the statement is
10665 a single statement (as opposed to a compound-statement), it is
10666 none-the-less treated as if it were enclosed in braces. Any
10667 declarations appearing in the dependent statement are out of scope
10668 after control passes that point. This function parses a statement,
10669 but ensures that is in its own scope, even if it is not a
10670 compound-statement.
10672 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10673 is a (possibly labeled) if statement which is not enclosed in
10674 braces and has an else clause. This is used to implement
10677 Returns the new statement. */
10680 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
10687 /* Mark if () ; with a special NOP_EXPR. */
10688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10690 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10691 cp_lexer_consume_token (parser
->lexer
);
10692 statement
= add_stmt (build_empty_stmt (loc
));
10694 /* if a compound is opened, we simply parse the statement directly. */
10695 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10696 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
10697 /* If the token is not a `{', then we must take special action. */
10700 /* Create a compound-statement. */
10701 statement
= begin_compound_stmt (0);
10702 /* Parse the dependent-statement. */
10703 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
10704 /* Finish the dummy compound-statement. */
10705 finish_compound_stmt (statement
);
10708 /* Return the statement. */
10712 /* For some dependent statements (like `while (cond) statement'), we
10713 have already created a scope. Therefore, even if the dependent
10714 statement is a compound-statement, we do not want to create another
10718 cp_parser_already_scoped_statement (cp_parser
* parser
)
10720 /* If the token is a `{', then we must take special action. */
10721 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10722 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
10725 /* Avoid calling cp_parser_compound_statement, so that we
10726 don't create a new scope. Do everything else by hand. */
10727 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
10728 /* If the next keyword is `__label__' we have a label declaration. */
10729 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10730 cp_parser_label_declaration (parser
);
10731 /* Parse an (optional) statement-seq. */
10732 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10733 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10737 /* Declarations [gram.dcl.dcl] */
10739 /* Parse an optional declaration-sequence.
10743 declaration-seq declaration */
10746 cp_parser_declaration_seq_opt (cp_parser
* parser
)
10752 token
= cp_lexer_peek_token (parser
->lexer
);
10754 if (token
->type
== CPP_CLOSE_BRACE
10755 || token
->type
== CPP_EOF
10756 || token
->type
== CPP_PRAGMA_EOL
)
10759 if (token
->type
== CPP_SEMICOLON
)
10761 /* A declaration consisting of a single semicolon is
10762 invalid. Allow it unless we're being pedantic. */
10763 cp_lexer_consume_token (parser
->lexer
);
10764 if (!in_system_header
)
10765 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
10769 /* If we're entering or exiting a region that's implicitly
10770 extern "C", modify the lang context appropriately. */
10771 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
10773 push_lang_context (lang_name_c
);
10774 parser
->implicit_extern_c
= true;
10776 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
10778 pop_lang_context ();
10779 parser
->implicit_extern_c
= false;
10782 if (token
->type
== CPP_PRAGMA
)
10784 /* A top-level declaration can consist solely of a #pragma.
10785 A nested declaration cannot, so this is done here and not
10786 in cp_parser_declaration. (A #pragma at block scope is
10787 handled in cp_parser_statement.) */
10788 cp_parser_pragma (parser
, pragma_external
);
10792 /* Parse the declaration itself. */
10793 cp_parser_declaration (parser
);
10797 /* Parse a declaration.
10801 function-definition
10802 template-declaration
10803 explicit-instantiation
10804 explicit-specialization
10805 linkage-specification
10806 namespace-definition
10811 __extension__ declaration */
10814 cp_parser_declaration (cp_parser
* parser
)
10818 int saved_pedantic
;
10820 tree attributes
= NULL_TREE
;
10822 /* Check for the `__extension__' keyword. */
10823 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10825 /* Parse the qualified declaration. */
10826 cp_parser_declaration (parser
);
10827 /* Restore the PEDANTIC flag. */
10828 pedantic
= saved_pedantic
;
10833 /* Try to figure out what kind of declaration is present. */
10834 token1
= *cp_lexer_peek_token (parser
->lexer
);
10836 if (token1
.type
!= CPP_EOF
)
10837 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
10840 token2
.type
= CPP_EOF
;
10841 token2
.keyword
= RID_MAX
;
10844 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10845 p
= obstack_alloc (&declarator_obstack
, 0);
10847 /* If the next token is `extern' and the following token is a string
10848 literal, then we have a linkage specification. */
10849 if (token1
.keyword
== RID_EXTERN
10850 && cp_parser_is_pure_string_literal (&token2
))
10851 cp_parser_linkage_specification (parser
);
10852 /* If the next token is `template', then we have either a template
10853 declaration, an explicit instantiation, or an explicit
10855 else if (token1
.keyword
== RID_TEMPLATE
)
10857 /* `template <>' indicates a template specialization. */
10858 if (token2
.type
== CPP_LESS
10859 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
10860 cp_parser_explicit_specialization (parser
);
10861 /* `template <' indicates a template declaration. */
10862 else if (token2
.type
== CPP_LESS
)
10863 cp_parser_template_declaration (parser
, /*member_p=*/false);
10864 /* Anything else must be an explicit instantiation. */
10866 cp_parser_explicit_instantiation (parser
);
10868 /* If the next token is `export', then we have a template
10870 else if (token1
.keyword
== RID_EXPORT
)
10871 cp_parser_template_declaration (parser
, /*member_p=*/false);
10872 /* If the next token is `extern', 'static' or 'inline' and the one
10873 after that is `template', we have a GNU extended explicit
10874 instantiation directive. */
10875 else if (cp_parser_allow_gnu_extensions_p (parser
)
10876 && (token1
.keyword
== RID_EXTERN
10877 || token1
.keyword
== RID_STATIC
10878 || token1
.keyword
== RID_INLINE
)
10879 && token2
.keyword
== RID_TEMPLATE
)
10880 cp_parser_explicit_instantiation (parser
);
10881 /* If the next token is `namespace', check for a named or unnamed
10882 namespace definition. */
10883 else if (token1
.keyword
== RID_NAMESPACE
10884 && (/* A named namespace definition. */
10885 (token2
.type
== CPP_NAME
10886 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
10888 /* An unnamed namespace definition. */
10889 || token2
.type
== CPP_OPEN_BRACE
10890 || token2
.keyword
== RID_ATTRIBUTE
))
10891 cp_parser_namespace_definition (parser
);
10892 /* An inline (associated) namespace definition. */
10893 else if (token1
.keyword
== RID_INLINE
10894 && token2
.keyword
== RID_NAMESPACE
)
10895 cp_parser_namespace_definition (parser
);
10896 /* Objective-C++ declaration/definition. */
10897 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
10898 cp_parser_objc_declaration (parser
, NULL_TREE
);
10899 else if (c_dialect_objc ()
10900 && token1
.keyword
== RID_ATTRIBUTE
10901 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
10902 cp_parser_objc_declaration (parser
, attributes
);
10903 /* We must have either a block declaration or a function
10906 /* Try to parse a block-declaration, or a function-definition. */
10907 cp_parser_block_declaration (parser
, /*statement_p=*/false);
10909 /* Free any declarators allocated. */
10910 obstack_free (&declarator_obstack
, p
);
10913 /* Parse a block-declaration.
10918 namespace-alias-definition
10925 __extension__ block-declaration
10930 static_assert-declaration
10932 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10933 part of a declaration-statement. */
10936 cp_parser_block_declaration (cp_parser
*parser
,
10940 int saved_pedantic
;
10942 /* Check for the `__extension__' keyword. */
10943 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10945 /* Parse the qualified declaration. */
10946 cp_parser_block_declaration (parser
, statement_p
);
10947 /* Restore the PEDANTIC flag. */
10948 pedantic
= saved_pedantic
;
10953 /* Peek at the next token to figure out which kind of declaration is
10955 token1
= cp_lexer_peek_token (parser
->lexer
);
10957 /* If the next keyword is `asm', we have an asm-definition. */
10958 if (token1
->keyword
== RID_ASM
)
10961 cp_parser_commit_to_tentative_parse (parser
);
10962 cp_parser_asm_definition (parser
);
10964 /* If the next keyword is `namespace', we have a
10965 namespace-alias-definition. */
10966 else if (token1
->keyword
== RID_NAMESPACE
)
10967 cp_parser_namespace_alias_definition (parser
);
10968 /* If the next keyword is `using', we have a
10969 using-declaration, a using-directive, or an alias-declaration. */
10970 else if (token1
->keyword
== RID_USING
)
10975 cp_parser_commit_to_tentative_parse (parser
);
10976 /* If the token after `using' is `namespace', then we have a
10977 using-directive. */
10978 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10979 if (token2
->keyword
== RID_NAMESPACE
)
10980 cp_parser_using_directive (parser
);
10981 /* If the second token after 'using' is '=', then we have an
10982 alias-declaration. */
10983 else if (cxx_dialect
>= cxx11
10984 && token2
->type
== CPP_NAME
10985 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
10986 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
10987 cp_parser_alias_declaration (parser
);
10988 /* Otherwise, it's a using-declaration. */
10990 cp_parser_using_declaration (parser
,
10991 /*access_declaration_p=*/false);
10993 /* If the next keyword is `__label__' we have a misplaced label
10995 else if (token1
->keyword
== RID_LABEL
)
10997 cp_lexer_consume_token (parser
->lexer
);
10998 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
10999 cp_parser_skip_to_end_of_statement (parser
);
11000 /* If the next token is now a `;', consume it. */
11001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11002 cp_lexer_consume_token (parser
->lexer
);
11004 /* If the next token is `static_assert' we have a static assertion. */
11005 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11006 cp_parser_static_assert (parser
, /*member_p=*/false);
11007 /* Anything else must be a simple-declaration. */
11009 cp_parser_simple_declaration (parser
, !statement_p
,
11010 /*maybe_range_for_decl*/NULL
);
11013 /* Parse a simple-declaration.
11015 simple-declaration:
11016 decl-specifier-seq [opt] init-declarator-list [opt] ;
11018 init-declarator-list:
11020 init-declarator-list , init-declarator
11022 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11023 function-definition as a simple-declaration.
11025 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11026 parsed declaration if it is an uninitialized single declarator not followed
11027 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11028 if present, will not be consumed. */
11031 cp_parser_simple_declaration (cp_parser
* parser
,
11032 bool function_definition_allowed_p
,
11033 tree
*maybe_range_for_decl
)
11035 cp_decl_specifier_seq decl_specifiers
;
11036 int declares_class_or_enum
;
11037 bool saw_declarator
;
11039 if (maybe_range_for_decl
)
11040 *maybe_range_for_decl
= NULL_TREE
;
11042 /* Defer access checks until we know what is being declared; the
11043 checks for names appearing in the decl-specifier-seq should be
11044 done as if we were in the scope of the thing being declared. */
11045 push_deferring_access_checks (dk_deferred
);
11047 /* Parse the decl-specifier-seq. We have to keep track of whether
11048 or not the decl-specifier-seq declares a named class or
11049 enumeration type, since that is the only case in which the
11050 init-declarator-list is allowed to be empty.
11054 In a simple-declaration, the optional init-declarator-list can be
11055 omitted only when declaring a class or enumeration, that is when
11056 the decl-specifier-seq contains either a class-specifier, an
11057 elaborated-type-specifier, or an enum-specifier. */
11058 cp_parser_decl_specifier_seq (parser
,
11059 CP_PARSER_FLAGS_OPTIONAL
,
11061 &declares_class_or_enum
);
11062 /* We no longer need to defer access checks. */
11063 stop_deferring_access_checks ();
11065 /* In a block scope, a valid declaration must always have a
11066 decl-specifier-seq. By not trying to parse declarators, we can
11067 resolve the declaration/expression ambiguity more quickly. */
11068 if (!function_definition_allowed_p
11069 && !decl_specifiers
.any_specifiers_p
)
11071 cp_parser_error (parser
, "expected declaration");
11075 /* If the next two tokens are both identifiers, the code is
11076 erroneous. The usual cause of this situation is code like:
11080 where "T" should name a type -- but does not. */
11081 if (!decl_specifiers
.any_type_specifiers_p
11082 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11084 /* If parsing tentatively, we should commit; we really are
11085 looking at a declaration. */
11086 cp_parser_commit_to_tentative_parse (parser
);
11091 /* If we have seen at least one decl-specifier, and the next token
11092 is not a parenthesis, then we must be looking at a declaration.
11093 (After "int (" we might be looking at a functional cast.) */
11094 if (decl_specifiers
.any_specifiers_p
11095 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11096 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11097 && !cp_parser_error_occurred (parser
))
11098 cp_parser_commit_to_tentative_parse (parser
);
11100 /* Keep going until we hit the `;' at the end of the simple
11102 saw_declarator
= false;
11103 while (cp_lexer_next_token_is_not (parser
->lexer
,
11107 bool function_definition_p
;
11110 if (saw_declarator
)
11112 /* If we are processing next declarator, coma is expected */
11113 token
= cp_lexer_peek_token (parser
->lexer
);
11114 gcc_assert (token
->type
== CPP_COMMA
);
11115 cp_lexer_consume_token (parser
->lexer
);
11116 if (maybe_range_for_decl
)
11117 *maybe_range_for_decl
= error_mark_node
;
11120 saw_declarator
= true;
11122 /* Parse the init-declarator. */
11123 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11125 function_definition_allowed_p
,
11126 /*member_p=*/false,
11127 declares_class_or_enum
,
11128 &function_definition_p
,
11129 maybe_range_for_decl
);
11130 /* If an error occurred while parsing tentatively, exit quickly.
11131 (That usually happens when in the body of a function; each
11132 statement is treated as a declaration-statement until proven
11134 if (cp_parser_error_occurred (parser
))
11136 /* Handle function definitions specially. */
11137 if (function_definition_p
)
11139 /* If the next token is a `,', then we are probably
11140 processing something like:
11144 which is erroneous. */
11145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11147 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11148 error_at (token
->location
,
11150 " declarations and function-definitions is forbidden");
11152 /* Otherwise, we're done with the list of declarators. */
11155 pop_deferring_access_checks ();
11159 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11160 *maybe_range_for_decl
= decl
;
11161 /* The next token should be either a `,' or a `;'. */
11162 token
= cp_lexer_peek_token (parser
->lexer
);
11163 /* If it's a `,', there are more declarators to come. */
11164 if (token
->type
== CPP_COMMA
)
11165 /* will be consumed next time around */;
11166 /* If it's a `;', we are done. */
11167 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11169 /* Anything else is an error. */
11172 /* If we have already issued an error message we don't need
11173 to issue another one. */
11174 if (decl
!= error_mark_node
11175 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11176 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11177 /* Skip tokens until we reach the end of the statement. */
11178 cp_parser_skip_to_end_of_statement (parser
);
11179 /* If the next token is now a `;', consume it. */
11180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11181 cp_lexer_consume_token (parser
->lexer
);
11184 /* After the first time around, a function-definition is not
11185 allowed -- even if it was OK at first. For example:
11190 function_definition_allowed_p
= false;
11193 /* Issue an error message if no declarators are present, and the
11194 decl-specifier-seq does not itself declare a class or
11195 enumeration: [dcl.dcl]/3. */
11196 if (!saw_declarator
)
11198 if (cp_parser_declares_only_class_p (parser
))
11200 if (!declares_class_or_enum
11201 && decl_specifiers
.type
11202 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11203 /* Ensure an error is issued anyway when finish_decltype_type,
11204 called via cp_parser_decl_specifier_seq, returns a class or
11205 an enumeration (c++/51786). */
11206 decl_specifiers
.type
= NULL_TREE
;
11207 shadow_tag (&decl_specifiers
);
11209 /* Perform any deferred access checks. */
11210 perform_deferred_access_checks (tf_warning_or_error
);
11213 /* Consume the `;'. */
11214 if (!maybe_range_for_decl
)
11215 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11218 pop_deferring_access_checks ();
11221 /* Parse a decl-specifier-seq.
11223 decl-specifier-seq:
11224 decl-specifier-seq [opt] decl-specifier
11225 decl-specifier attribute-specifier-seq [opt] (C++11)
11228 storage-class-specifier
11239 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11241 The parser flags FLAGS is used to control type-specifier parsing.
11243 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11246 1: one of the decl-specifiers is an elaborated-type-specifier
11247 (i.e., a type declaration)
11248 2: one of the decl-specifiers is an enum-specifier or a
11249 class-specifier (i.e., a type definition)
11254 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11255 cp_parser_flags flags
,
11256 cp_decl_specifier_seq
*decl_specs
,
11257 int* declares_class_or_enum
)
11259 bool constructor_possible_p
= !parser
->in_declarator_p
;
11260 bool found_decl_spec
= false;
11261 cp_token
*start_token
= NULL
;
11264 /* Clear DECL_SPECS. */
11265 clear_decl_specs (decl_specs
);
11267 /* Assume no class or enumeration type is declared. */
11268 *declares_class_or_enum
= 0;
11270 /* Keep reading specifiers until there are no more to read. */
11273 bool constructor_p
;
11277 /* Peek at the next token. */
11278 token
= cp_lexer_peek_token (parser
->lexer
);
11280 /* Save the first token of the decl spec list for error
11283 start_token
= token
;
11284 /* Handle attributes. */
11285 if (cp_next_tokens_can_be_attribute_p (parser
))
11287 /* Parse the attributes. */
11288 tree attrs
= cp_parser_attributes_opt (parser
);
11290 /* In a sequence of declaration specifiers, c++11 attributes
11291 appertain to the type that precede them. In that case
11294 The attribute-specifier-seq affects the type only for
11295 the declaration it appears in, not other declarations
11296 involving the same type.
11298 But for now let's force the user to position the
11299 attribute either at the beginning of the declaration or
11300 after the declarator-id, which would clearly mean that it
11301 applies to the declarator. */
11302 if (cxx11_attribute_p (attrs
))
11304 if (!found_decl_spec
)
11305 /* The c++11 attribute is at the beginning of the
11306 declaration. It appertains to the entity being
11310 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11312 /* This is an attribute following a
11313 class-specifier. */
11314 if (decl_specs
->type_definition_p
)
11315 warn_misplaced_attr_for_class_type (token
->location
,
11321 decl_specs
->std_attributes
11322 = chainon (decl_specs
->std_attributes
,
11324 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11325 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11331 decl_specs
->attributes
11332 = chainon (decl_specs
->attributes
,
11334 if (decl_specs
->locations
[ds_attribute
] == 0)
11335 decl_specs
->locations
[ds_attribute
] = token
->location
;
11338 /* Assume we will find a decl-specifier keyword. */
11339 found_decl_spec
= true;
11340 /* If the next token is an appropriate keyword, we can simply
11341 add it to the list. */
11342 switch (token
->keyword
)
11348 if (!at_class_scope_p ())
11350 error_at (token
->location
, "%<friend%> used outside of class");
11351 cp_lexer_purge_token (parser
->lexer
);
11356 /* Consume the token. */
11357 cp_lexer_consume_token (parser
->lexer
);
11361 case RID_CONSTEXPR
:
11363 cp_lexer_consume_token (parser
->lexer
);
11366 /* function-specifier:
11373 cp_parser_function_specifier_opt (parser
, decl_specs
);
11380 /* Consume the token. */
11381 cp_lexer_consume_token (parser
->lexer
);
11382 /* A constructor declarator cannot appear in a typedef. */
11383 constructor_possible_p
= false;
11384 /* The "typedef" keyword can only occur in a declaration; we
11385 may as well commit at this point. */
11386 cp_parser_commit_to_tentative_parse (parser
);
11388 if (decl_specs
->storage_class
!= sc_none
)
11389 decl_specs
->conflicting_specifiers_p
= true;
11392 /* storage-class-specifier:
11402 if (cxx_dialect
== cxx98
)
11404 /* Consume the token. */
11405 cp_lexer_consume_token (parser
->lexer
);
11407 /* Complain about `auto' as a storage specifier, if
11408 we're complaining about C++0x compatibility. */
11409 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11410 " changes meaning in C++11; please remove it");
11412 /* Set the storage class anyway. */
11413 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11417 /* C++0x auto type-specifier. */
11418 found_decl_spec
= false;
11425 /* Consume the token. */
11426 cp_lexer_consume_token (parser
->lexer
);
11427 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11431 /* Consume the token. */
11433 cp_lexer_consume_token (parser
->lexer
);
11437 /* We did not yet find a decl-specifier yet. */
11438 found_decl_spec
= false;
11442 if (found_decl_spec
11443 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11444 && token
->keyword
!= RID_CONSTEXPR
)
11445 error ("decl-specifier invalid in condition");
11448 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11450 /* Constructors are a special case. The `S' in `S()' is not a
11451 decl-specifier; it is the beginning of the declarator. */
11453 = (!found_decl_spec
11454 && constructor_possible_p
11455 && (cp_parser_constructor_declarator_p
11456 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11458 /* If we don't have a DECL_SPEC yet, then we must be looking at
11459 a type-specifier. */
11460 if (!found_decl_spec
&& !constructor_p
)
11462 int decl_spec_declares_class_or_enum
;
11463 bool is_cv_qualifier
;
11467 = cp_parser_type_specifier (parser
, flags
,
11469 /*is_declaration=*/true,
11470 &decl_spec_declares_class_or_enum
,
11472 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11474 /* If this type-specifier referenced a user-defined type
11475 (a typedef, class-name, etc.), then we can't allow any
11476 more such type-specifiers henceforth.
11480 The longest sequence of decl-specifiers that could
11481 possibly be a type name is taken as the
11482 decl-specifier-seq of a declaration. The sequence shall
11483 be self-consistent as described below.
11487 As a general rule, at most one type-specifier is allowed
11488 in the complete decl-specifier-seq of a declaration. The
11489 only exceptions are the following:
11491 -- const or volatile can be combined with any other
11494 -- signed or unsigned can be combined with char, long,
11502 void g (const int Pc);
11504 Here, Pc is *not* part of the decl-specifier seq; it's
11505 the declarator. Therefore, once we see a type-specifier
11506 (other than a cv-qualifier), we forbid any additional
11507 user-defined types. We *do* still allow things like `int
11508 int' to be considered a decl-specifier-seq, and issue the
11509 error message later. */
11510 if (type_spec
&& !is_cv_qualifier
)
11511 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11512 /* A constructor declarator cannot follow a type-specifier. */
11515 constructor_possible_p
= false;
11516 found_decl_spec
= true;
11517 if (!is_cv_qualifier
)
11518 decl_specs
->any_type_specifiers_p
= true;
11522 /* If we still do not have a DECL_SPEC, then there are no more
11523 decl-specifiers. */
11524 if (!found_decl_spec
)
11527 decl_specs
->any_specifiers_p
= true;
11528 /* After we see one decl-specifier, further decl-specifiers are
11529 always optional. */
11530 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
11533 /* Don't allow a friend specifier with a class definition. */
11534 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
11535 && (*declares_class_or_enum
& 2))
11536 error_at (decl_specs
->locations
[ds_friend
],
11537 "class definition may not be declared a friend");
11540 /* Parse an (optional) storage-class-specifier.
11542 storage-class-specifier:
11551 storage-class-specifier:
11554 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11557 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
11559 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
11562 if (cxx_dialect
!= cxx98
)
11564 /* Fall through for C++98. */
11571 /* Consume the token. */
11572 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11579 /* Parse an (optional) function-specifier.
11581 function-specifier:
11586 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11587 Updates DECL_SPECS, if it is non-NULL. */
11590 cp_parser_function_specifier_opt (cp_parser
* parser
,
11591 cp_decl_specifier_seq
*decl_specs
)
11593 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11594 switch (token
->keyword
)
11597 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
11601 /* 14.5.2.3 [temp.mem]
11603 A member function template shall not be virtual. */
11604 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11605 error_at (token
->location
, "templates may not be %<virtual%>");
11607 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
11611 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
11618 /* Consume the token. */
11619 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11622 /* Parse a linkage-specification.
11624 linkage-specification:
11625 extern string-literal { declaration-seq [opt] }
11626 extern string-literal declaration */
11629 cp_parser_linkage_specification (cp_parser
* parser
)
11633 /* Look for the `extern' keyword. */
11634 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
11636 /* Look for the string-literal. */
11637 linkage
= cp_parser_string_literal (parser
, false, false);
11639 /* Transform the literal into an identifier. If the literal is a
11640 wide-character string, or contains embedded NULs, then we can't
11641 handle it as the user wants. */
11642 if (strlen (TREE_STRING_POINTER (linkage
))
11643 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
11645 cp_parser_error (parser
, "invalid linkage-specification");
11646 /* Assume C++ linkage. */
11647 linkage
= lang_name_cplusplus
;
11650 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
11652 /* We're now using the new linkage. */
11653 push_lang_context (linkage
);
11655 /* If the next token is a `{', then we're using the first
11657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11659 cp_ensure_no_omp_declare_simd (parser
);
11661 /* Consume the `{' token. */
11662 cp_lexer_consume_token (parser
->lexer
);
11663 /* Parse the declarations. */
11664 cp_parser_declaration_seq_opt (parser
);
11665 /* Look for the closing `}'. */
11666 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11668 /* Otherwise, there's just one declaration. */
11671 bool saved_in_unbraced_linkage_specification_p
;
11673 saved_in_unbraced_linkage_specification_p
11674 = parser
->in_unbraced_linkage_specification_p
;
11675 parser
->in_unbraced_linkage_specification_p
= true;
11676 cp_parser_declaration (parser
);
11677 parser
->in_unbraced_linkage_specification_p
11678 = saved_in_unbraced_linkage_specification_p
;
11681 /* We're done with the linkage-specification. */
11682 pop_lang_context ();
11685 /* Parse a static_assert-declaration.
11687 static_assert-declaration:
11688 static_assert ( constant-expression , string-literal ) ;
11690 If MEMBER_P, this static_assert is a class member. */
11693 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
11698 location_t saved_loc
;
11701 /* Peek at the `static_assert' token so we can keep track of exactly
11702 where the static assertion started. */
11703 token
= cp_lexer_peek_token (parser
->lexer
);
11704 saved_loc
= token
->location
;
11706 /* Look for the `static_assert' keyword. */
11707 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
11711 /* We know we are in a static assertion; commit to any tentative
11713 if (cp_parser_parsing_tentatively (parser
))
11714 cp_parser_commit_to_tentative_parse (parser
);
11716 /* Parse the `(' starting the static assertion condition. */
11717 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11719 /* Parse the constant-expression. Allow a non-constant expression
11720 here in order to give better diagnostics in finish_static_assert. */
11722 cp_parser_constant_expression (parser
,
11723 /*allow_non_constant_p=*/true,
11724 /*non_constant_p=*/&dummy
);
11726 /* Parse the separating `,'. */
11727 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11729 /* Parse the string-literal message. */
11730 message
= cp_parser_string_literal (parser
,
11731 /*translate=*/false,
11734 /* A `)' completes the static assertion. */
11735 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11736 cp_parser_skip_to_closing_parenthesis (parser
,
11737 /*recovering=*/true,
11738 /*or_comma=*/false,
11739 /*consume_paren=*/true);
11741 /* A semicolon terminates the declaration. */
11742 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11744 /* Complete the static assertion, which may mean either processing
11745 the static assert now or saving it for template instantiation. */
11746 finish_static_assert (condition
, message
, saved_loc
, member_p
);
11749 /* Parse the expression in decltype ( expression ). */
11752 cp_parser_decltype_expr (cp_parser
*parser
,
11753 bool &id_expression_or_member_access_p
)
11755 cp_token
*id_expr_start_token
;
11758 /* First, try parsing an id-expression. */
11759 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
11760 cp_parser_parse_tentatively (parser
);
11761 expr
= cp_parser_id_expression (parser
,
11762 /*template_keyword_p=*/false,
11763 /*check_dependency_p=*/true,
11764 /*template_p=*/NULL
,
11765 /*declarator_p=*/false,
11766 /*optional_p=*/false);
11768 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
11770 bool non_integral_constant_expression_p
= false;
11771 tree id_expression
= expr
;
11773 const char *error_msg
;
11775 if (identifier_p (expr
))
11776 /* Lookup the name we got back from the id-expression. */
11777 expr
= cp_parser_lookup_name_simple (parser
, expr
,
11778 id_expr_start_token
->location
);
11781 && expr
!= error_mark_node
11782 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
11783 && TREE_CODE (expr
) != TYPE_DECL
11784 && (TREE_CODE (expr
) != BIT_NOT_EXPR
11785 || !TYPE_P (TREE_OPERAND (expr
, 0)))
11786 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11788 /* Complete lookup of the id-expression. */
11789 expr
= (finish_id_expression
11790 (id_expression
, expr
, parser
->scope
, &idk
,
11791 /*integral_constant_expression_p=*/false,
11792 /*allow_non_integral_constant_expression_p=*/true,
11793 &non_integral_constant_expression_p
,
11794 /*template_p=*/false,
11796 /*address_p=*/false,
11797 /*template_arg_p=*/false,
11799 id_expr_start_token
->location
));
11801 if (expr
== error_mark_node
)
11802 /* We found an id-expression, but it was something that we
11803 should not have found. This is an error, not something
11804 we can recover from, so note that we found an
11805 id-expression and we'll recover as gracefully as
11807 id_expression_or_member_access_p
= true;
11811 && expr
!= error_mark_node
11812 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11813 /* We have an id-expression. */
11814 id_expression_or_member_access_p
= true;
11817 if (!id_expression_or_member_access_p
)
11819 /* Abort the id-expression parse. */
11820 cp_parser_abort_tentative_parse (parser
);
11822 /* Parsing tentatively, again. */
11823 cp_parser_parse_tentatively (parser
);
11825 /* Parse a class member access. */
11826 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
11827 /*cast_p=*/false, /*decltype*/true,
11828 /*member_access_only_p=*/true, NULL
);
11831 && expr
!= error_mark_node
11832 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11833 /* We have an id-expression. */
11834 id_expression_or_member_access_p
= true;
11837 if (id_expression_or_member_access_p
)
11838 /* We have parsed the complete id-expression or member access. */
11839 cp_parser_parse_definitely (parser
);
11842 /* Abort our attempt to parse an id-expression or member access
11844 cp_parser_abort_tentative_parse (parser
);
11846 /* Parse a full expression. */
11847 expr
= cp_parser_expression (parser
, /*cast_p=*/false,
11848 /*decltype*/true, NULL
);
11854 /* Parse a `decltype' type. Returns the type.
11856 simple-type-specifier:
11857 decltype ( expression )
11859 decltype ( auto ) */
11862 cp_parser_decltype (cp_parser
*parser
)
11865 bool id_expression_or_member_access_p
= false;
11866 const char *saved_message
;
11867 bool saved_integral_constant_expression_p
;
11868 bool saved_non_integral_constant_expression_p
;
11869 bool saved_greater_than_is_operator_p
;
11870 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11872 if (start_token
->type
== CPP_DECLTYPE
)
11874 /* Already parsed. */
11875 cp_lexer_consume_token (parser
->lexer
);
11876 return start_token
->u
.value
;
11879 /* Look for the `decltype' token. */
11880 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
11881 return error_mark_node
;
11883 /* Parse the opening `('. */
11884 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11885 return error_mark_node
;
11887 /* decltype (auto) */
11888 if (cxx_dialect
>= cxx1y
11889 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
11891 cp_lexer_consume_token (parser
->lexer
);
11892 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11893 return error_mark_node
;
11894 expr
= make_decltype_auto ();
11895 AUTO_IS_DECLTYPE (expr
) = true;
11899 /* Types cannot be defined in a `decltype' expression. Save away the
11901 saved_message
= parser
->type_definition_forbidden_message
;
11903 /* And create the new one. */
11904 parser
->type_definition_forbidden_message
11905 = G_("types may not be defined in %<decltype%> expressions");
11907 /* The restrictions on constant-expressions do not apply inside
11908 decltype expressions. */
11909 saved_integral_constant_expression_p
11910 = parser
->integral_constant_expression_p
;
11911 saved_non_integral_constant_expression_p
11912 = parser
->non_integral_constant_expression_p
;
11913 parser
->integral_constant_expression_p
= false;
11915 /* Within a parenthesized expression, a `>' token is always
11916 the greater-than operator. */
11917 saved_greater_than_is_operator_p
11918 = parser
->greater_than_is_operator_p
;
11919 parser
->greater_than_is_operator_p
= true;
11921 /* Do not actually evaluate the expression. */
11922 ++cp_unevaluated_operand
;
11924 /* Do not warn about problems with the expression. */
11925 ++c_inhibit_evaluation_warnings
;
11927 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
11929 /* Go back to evaluating expressions. */
11930 --cp_unevaluated_operand
;
11931 --c_inhibit_evaluation_warnings
;
11933 /* The `>' token might be the end of a template-id or
11934 template-parameter-list now. */
11935 parser
->greater_than_is_operator_p
11936 = saved_greater_than_is_operator_p
;
11938 /* Restore the old message and the integral constant expression
11940 parser
->type_definition_forbidden_message
= saved_message
;
11941 parser
->integral_constant_expression_p
11942 = saved_integral_constant_expression_p
;
11943 parser
->non_integral_constant_expression_p
11944 = saved_non_integral_constant_expression_p
;
11946 /* Parse to the closing `)'. */
11947 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11949 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11950 /*consume_paren=*/true);
11951 return error_mark_node
;
11954 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
11955 tf_warning_or_error
);
11958 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11960 start_token
->type
= CPP_DECLTYPE
;
11961 start_token
->u
.value
= expr
;
11962 start_token
->keyword
= RID_MAX
;
11963 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
11968 /* Special member functions [gram.special] */
11970 /* Parse a conversion-function-id.
11972 conversion-function-id:
11973 operator conversion-type-id
11975 Returns an IDENTIFIER_NODE representing the operator. */
11978 cp_parser_conversion_function_id (cp_parser
* parser
)
11982 tree saved_qualifying_scope
;
11983 tree saved_object_scope
;
11984 tree pushed_scope
= NULL_TREE
;
11986 /* Look for the `operator' token. */
11987 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
11988 return error_mark_node
;
11989 /* When we parse the conversion-type-id, the current scope will be
11990 reset. However, we need that information in able to look up the
11991 conversion function later, so we save it here. */
11992 saved_scope
= parser
->scope
;
11993 saved_qualifying_scope
= parser
->qualifying_scope
;
11994 saved_object_scope
= parser
->object_scope
;
11995 /* We must enter the scope of the class so that the names of
11996 entities declared within the class are available in the
11997 conversion-type-id. For example, consider:
12004 S::operator I() { ... }
12006 In order to see that `I' is a type-name in the definition, we
12007 must be in the scope of `S'. */
12009 pushed_scope
= push_scope (saved_scope
);
12010 /* Parse the conversion-type-id. */
12011 type
= cp_parser_conversion_type_id (parser
);
12012 /* Leave the scope of the class, if any. */
12014 pop_scope (pushed_scope
);
12015 /* Restore the saved scope. */
12016 parser
->scope
= saved_scope
;
12017 parser
->qualifying_scope
= saved_qualifying_scope
;
12018 parser
->object_scope
= saved_object_scope
;
12019 /* If the TYPE is invalid, indicate failure. */
12020 if (type
== error_mark_node
)
12021 return error_mark_node
;
12022 return mangle_conv_op_name_for_type (type
);
12025 /* Parse a conversion-type-id:
12027 conversion-type-id:
12028 type-specifier-seq conversion-declarator [opt]
12030 Returns the TYPE specified. */
12033 cp_parser_conversion_type_id (cp_parser
* parser
)
12036 cp_decl_specifier_seq type_specifiers
;
12037 cp_declarator
*declarator
;
12038 tree type_specified
;
12039 const char *saved_message
;
12041 /* Parse the attributes. */
12042 attributes
= cp_parser_attributes_opt (parser
);
12044 saved_message
= parser
->type_definition_forbidden_message
;
12045 parser
->type_definition_forbidden_message
12046 = G_("types may not be defined in a conversion-type-id");
12048 /* Parse the type-specifiers. */
12049 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12050 /*is_trailing_return=*/false,
12053 parser
->type_definition_forbidden_message
= saved_message
;
12055 /* If that didn't work, stop. */
12056 if (type_specifiers
.type
== error_mark_node
)
12057 return error_mark_node
;
12058 /* Parse the conversion-declarator. */
12059 declarator
= cp_parser_conversion_declarator_opt (parser
);
12061 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12062 /*initialized=*/0, &attributes
);
12064 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12066 /* Don't give this error when parsing tentatively. This happens to
12067 work because we always parse this definitively once. */
12068 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12069 && type_uses_auto (type_specified
))
12071 if (cxx_dialect
< cxx1y
)
12073 error ("invalid use of %<auto%> in conversion operator");
12074 return error_mark_node
;
12076 else if (template_parm_scope_p ())
12077 warning (0, "use of %<auto%> in member template "
12078 "conversion operator can never be deduced");
12081 return type_specified
;
12084 /* Parse an (optional) conversion-declarator.
12086 conversion-declarator:
12087 ptr-operator conversion-declarator [opt]
12091 static cp_declarator
*
12092 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12094 enum tree_code code
;
12095 tree class_type
, std_attributes
= NULL_TREE
;
12096 cp_cv_quals cv_quals
;
12098 /* We don't know if there's a ptr-operator next, or not. */
12099 cp_parser_parse_tentatively (parser
);
12100 /* Try the ptr-operator. */
12101 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12103 /* If it worked, look for more conversion-declarators. */
12104 if (cp_parser_parse_definitely (parser
))
12106 cp_declarator
*declarator
;
12108 /* Parse another optional declarator. */
12109 declarator
= cp_parser_conversion_declarator_opt (parser
);
12111 declarator
= cp_parser_make_indirect_declarator
12112 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12120 /* Parse an (optional) ctor-initializer.
12123 : mem-initializer-list
12125 Returns TRUE iff the ctor-initializer was actually present. */
12128 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12130 /* If the next token is not a `:', then there is no
12131 ctor-initializer. */
12132 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12134 /* Do default initialization of any bases and members. */
12135 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12136 finish_mem_initializers (NULL_TREE
);
12141 /* Consume the `:' token. */
12142 cp_lexer_consume_token (parser
->lexer
);
12143 /* And the mem-initializer-list. */
12144 cp_parser_mem_initializer_list (parser
);
12149 /* Parse a mem-initializer-list.
12151 mem-initializer-list:
12152 mem-initializer ... [opt]
12153 mem-initializer ... [opt] , mem-initializer-list */
12156 cp_parser_mem_initializer_list (cp_parser
* parser
)
12158 tree mem_initializer_list
= NULL_TREE
;
12159 tree target_ctor
= error_mark_node
;
12160 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12162 /* Let the semantic analysis code know that we are starting the
12163 mem-initializer-list. */
12164 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12165 error_at (token
->location
,
12166 "only constructors take member initializers");
12168 /* Loop through the list. */
12171 tree mem_initializer
;
12173 token
= cp_lexer_peek_token (parser
->lexer
);
12174 /* Parse the mem-initializer. */
12175 mem_initializer
= cp_parser_mem_initializer (parser
);
12176 /* If the next token is a `...', we're expanding member initializers. */
12177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12179 /* Consume the `...'. */
12180 cp_lexer_consume_token (parser
->lexer
);
12182 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12183 can be expanded but members cannot. */
12184 if (mem_initializer
!= error_mark_node
12185 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12187 error_at (token
->location
,
12188 "cannot expand initializer for member %<%D%>",
12189 TREE_PURPOSE (mem_initializer
));
12190 mem_initializer
= error_mark_node
;
12193 /* Construct the pack expansion type. */
12194 if (mem_initializer
!= error_mark_node
)
12195 mem_initializer
= make_pack_expansion (mem_initializer
);
12197 if (target_ctor
!= error_mark_node
12198 && mem_initializer
!= error_mark_node
)
12200 error ("mem-initializer for %qD follows constructor delegation",
12201 TREE_PURPOSE (mem_initializer
));
12202 mem_initializer
= error_mark_node
;
12204 /* Look for a target constructor. */
12205 if (mem_initializer
!= error_mark_node
12206 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12207 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12209 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12210 if (mem_initializer_list
)
12212 error ("constructor delegation follows mem-initializer for %qD",
12213 TREE_PURPOSE (mem_initializer_list
));
12214 mem_initializer
= error_mark_node
;
12216 target_ctor
= mem_initializer
;
12218 /* Add it to the list, unless it was erroneous. */
12219 if (mem_initializer
!= error_mark_node
)
12221 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12222 mem_initializer_list
= mem_initializer
;
12224 /* If the next token is not a `,', we're done. */
12225 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12227 /* Consume the `,' token. */
12228 cp_lexer_consume_token (parser
->lexer
);
12231 /* Perform semantic analysis. */
12232 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12233 finish_mem_initializers (mem_initializer_list
);
12236 /* Parse a mem-initializer.
12239 mem-initializer-id ( expression-list [opt] )
12240 mem-initializer-id braced-init-list
12245 ( expression-list [opt] )
12247 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12248 class) or FIELD_DECL (for a non-static data member) to initialize;
12249 the TREE_VALUE is the expression-list. An empty initialization
12250 list is represented by void_list_node. */
12253 cp_parser_mem_initializer (cp_parser
* parser
)
12255 tree mem_initializer_id
;
12256 tree expression_list
;
12258 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12260 /* Find out what is being initialized. */
12261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12263 permerror (token
->location
,
12264 "anachronistic old-style base class initializer");
12265 mem_initializer_id
= NULL_TREE
;
12269 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12270 if (mem_initializer_id
== error_mark_node
)
12271 return mem_initializer_id
;
12273 member
= expand_member_init (mem_initializer_id
);
12274 if (member
&& !DECL_P (member
))
12275 in_base_initializer
= 1;
12277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12279 bool expr_non_constant_p
;
12280 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12281 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12282 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12283 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12287 vec
<tree
, va_gc
> *vec
;
12288 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12290 /*allow_expansion_p=*/true,
12291 /*non_constant_p=*/NULL
);
12293 return error_mark_node
;
12294 expression_list
= build_tree_list_vec (vec
);
12295 release_tree_vector (vec
);
12298 if (expression_list
== error_mark_node
)
12299 return error_mark_node
;
12300 if (!expression_list
)
12301 expression_list
= void_type_node
;
12303 in_base_initializer
= 0;
12305 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12308 /* Parse a mem-initializer-id.
12310 mem-initializer-id:
12311 :: [opt] nested-name-specifier [opt] class-name
12314 Returns a TYPE indicating the class to be initializer for the first
12315 production. Returns an IDENTIFIER_NODE indicating the data member
12316 to be initialized for the second production. */
12319 cp_parser_mem_initializer_id (cp_parser
* parser
)
12321 bool global_scope_p
;
12322 bool nested_name_specifier_p
;
12323 bool template_p
= false;
12326 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12328 /* `typename' is not allowed in this context ([temp.res]). */
12329 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12331 error_at (token
->location
,
12332 "keyword %<typename%> not allowed in this context (a qualified "
12333 "member initializer is implicitly a type)");
12334 cp_lexer_consume_token (parser
->lexer
);
12336 /* Look for the optional `::' operator. */
12338 = (cp_parser_global_scope_opt (parser
,
12339 /*current_scope_valid_p=*/false)
12341 /* Look for the optional nested-name-specifier. The simplest way to
12346 The keyword `typename' is not permitted in a base-specifier or
12347 mem-initializer; in these contexts a qualified name that
12348 depends on a template-parameter is implicitly assumed to be a
12351 is to assume that we have seen the `typename' keyword at this
12353 nested_name_specifier_p
12354 = (cp_parser_nested_name_specifier_opt (parser
,
12355 /*typename_keyword_p=*/true,
12356 /*check_dependency_p=*/true,
12358 /*is_declaration=*/true)
12360 if (nested_name_specifier_p
)
12361 template_p
= cp_parser_optional_template_keyword (parser
);
12362 /* If there is a `::' operator or a nested-name-specifier, then we
12363 are definitely looking for a class-name. */
12364 if (global_scope_p
|| nested_name_specifier_p
)
12365 return cp_parser_class_name (parser
,
12366 /*typename_keyword_p=*/true,
12367 /*template_keyword_p=*/template_p
,
12369 /*check_dependency_p=*/true,
12370 /*class_head_p=*/false,
12371 /*is_declaration=*/true);
12372 /* Otherwise, we could also be looking for an ordinary identifier. */
12373 cp_parser_parse_tentatively (parser
);
12374 /* Try a class-name. */
12375 id
= cp_parser_class_name (parser
,
12376 /*typename_keyword_p=*/true,
12377 /*template_keyword_p=*/false,
12379 /*check_dependency_p=*/true,
12380 /*class_head_p=*/false,
12381 /*is_declaration=*/true);
12382 /* If we found one, we're done. */
12383 if (cp_parser_parse_definitely (parser
))
12385 /* Otherwise, look for an ordinary identifier. */
12386 return cp_parser_identifier (parser
);
12389 /* Overloading [gram.over] */
12391 /* Parse an operator-function-id.
12393 operator-function-id:
12396 Returns an IDENTIFIER_NODE for the operator which is a
12397 human-readable spelling of the identifier, e.g., `operator +'. */
12400 cp_parser_operator_function_id (cp_parser
* parser
)
12402 /* Look for the `operator' keyword. */
12403 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12404 return error_mark_node
;
12405 /* And then the name of the operator itself. */
12406 return cp_parser_operator (parser
);
12409 /* Return an identifier node for a user-defined literal operator.
12410 The suffix identifier is chained to the operator name identifier. */
12413 cp_literal_operator_id (const char* name
)
12416 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12417 + strlen (name
) + 10);
12418 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12419 identifier
= get_identifier (buffer
);
12424 /* Parse an operator.
12427 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12428 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12429 || ++ -- , ->* -> () []
12436 Returns an IDENTIFIER_NODE for the operator which is a
12437 human-readable spelling of the identifier, e.g., `operator +'. */
12440 cp_parser_operator (cp_parser
* parser
)
12442 tree id
= NULL_TREE
;
12444 bool bad_encoding_prefix
= false;
12446 /* Peek at the next token. */
12447 token
= cp_lexer_peek_token (parser
->lexer
);
12448 /* Figure out which operator we have. */
12449 switch (token
->type
)
12455 /* The keyword should be either `new' or `delete'. */
12456 if (token
->keyword
== RID_NEW
)
12458 else if (token
->keyword
== RID_DELETE
)
12463 /* Consume the `new' or `delete' token. */
12464 cp_lexer_consume_token (parser
->lexer
);
12466 /* Peek at the next token. */
12467 token
= cp_lexer_peek_token (parser
->lexer
);
12468 /* If it's a `[' token then this is the array variant of the
12470 if (token
->type
== CPP_OPEN_SQUARE
)
12472 /* Consume the `[' token. */
12473 cp_lexer_consume_token (parser
->lexer
);
12474 /* Look for the `]' token. */
12475 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12476 id
= ansi_opname (op
== NEW_EXPR
12477 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12479 /* Otherwise, we have the non-array variant. */
12481 id
= ansi_opname (op
);
12487 id
= ansi_opname (PLUS_EXPR
);
12491 id
= ansi_opname (MINUS_EXPR
);
12495 id
= ansi_opname (MULT_EXPR
);
12499 id
= ansi_opname (TRUNC_DIV_EXPR
);
12503 id
= ansi_opname (TRUNC_MOD_EXPR
);
12507 id
= ansi_opname (BIT_XOR_EXPR
);
12511 id
= ansi_opname (BIT_AND_EXPR
);
12515 id
= ansi_opname (BIT_IOR_EXPR
);
12519 id
= ansi_opname (BIT_NOT_EXPR
);
12523 id
= ansi_opname (TRUTH_NOT_EXPR
);
12527 id
= ansi_assopname (NOP_EXPR
);
12531 id
= ansi_opname (LT_EXPR
);
12535 id
= ansi_opname (GT_EXPR
);
12539 id
= ansi_assopname (PLUS_EXPR
);
12543 id
= ansi_assopname (MINUS_EXPR
);
12547 id
= ansi_assopname (MULT_EXPR
);
12551 id
= ansi_assopname (TRUNC_DIV_EXPR
);
12555 id
= ansi_assopname (TRUNC_MOD_EXPR
);
12559 id
= ansi_assopname (BIT_XOR_EXPR
);
12563 id
= ansi_assopname (BIT_AND_EXPR
);
12567 id
= ansi_assopname (BIT_IOR_EXPR
);
12571 id
= ansi_opname (LSHIFT_EXPR
);
12575 id
= ansi_opname (RSHIFT_EXPR
);
12578 case CPP_LSHIFT_EQ
:
12579 id
= ansi_assopname (LSHIFT_EXPR
);
12582 case CPP_RSHIFT_EQ
:
12583 id
= ansi_assopname (RSHIFT_EXPR
);
12587 id
= ansi_opname (EQ_EXPR
);
12591 id
= ansi_opname (NE_EXPR
);
12595 id
= ansi_opname (LE_EXPR
);
12598 case CPP_GREATER_EQ
:
12599 id
= ansi_opname (GE_EXPR
);
12603 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
12607 id
= ansi_opname (TRUTH_ORIF_EXPR
);
12610 case CPP_PLUS_PLUS
:
12611 id
= ansi_opname (POSTINCREMENT_EXPR
);
12614 case CPP_MINUS_MINUS
:
12615 id
= ansi_opname (PREDECREMENT_EXPR
);
12619 id
= ansi_opname (COMPOUND_EXPR
);
12622 case CPP_DEREF_STAR
:
12623 id
= ansi_opname (MEMBER_REF
);
12627 id
= ansi_opname (COMPONENT_REF
);
12630 case CPP_OPEN_PAREN
:
12631 /* Consume the `('. */
12632 cp_lexer_consume_token (parser
->lexer
);
12633 /* Look for the matching `)'. */
12634 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
12635 return ansi_opname (CALL_EXPR
);
12637 case CPP_OPEN_SQUARE
:
12638 /* Consume the `['. */
12639 cp_lexer_consume_token (parser
->lexer
);
12640 /* Look for the matching `]'. */
12641 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12642 return ansi_opname (ARRAY_REF
);
12647 case CPP_UTF8STRING
:
12648 bad_encoding_prefix
= true;
12649 /* Fall through. */
12652 if (cxx_dialect
== cxx98
)
12653 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12654 if (bad_encoding_prefix
)
12656 error ("invalid encoding prefix in literal operator");
12657 return error_mark_node
;
12659 if (TREE_STRING_LENGTH (token
->u
.value
) > 2)
12661 error ("expected empty string after %<operator%> keyword");
12662 return error_mark_node
;
12664 /* Consume the string. */
12665 cp_lexer_consume_token (parser
->lexer
);
12666 /* Look for the suffix identifier. */
12667 token
= cp_lexer_peek_token (parser
->lexer
);
12668 if (token
->type
== CPP_NAME
)
12670 id
= cp_parser_identifier (parser
);
12671 if (id
!= error_mark_node
)
12673 const char *name
= IDENTIFIER_POINTER (id
);
12674 return cp_literal_operator_id (name
);
12677 else if (token
->type
== CPP_KEYWORD
)
12679 error ("unexpected keyword;"
12680 " remove space between quotes and suffix identifier");
12681 return error_mark_node
;
12685 error ("expected suffix identifier");
12686 return error_mark_node
;
12689 case CPP_WSTRING_USERDEF
:
12690 case CPP_STRING16_USERDEF
:
12691 case CPP_STRING32_USERDEF
:
12692 case CPP_UTF8STRING_USERDEF
:
12693 bad_encoding_prefix
= true;
12694 /* Fall through. */
12696 case CPP_STRING_USERDEF
:
12697 if (cxx_dialect
== cxx98
)
12698 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12699 if (bad_encoding_prefix
)
12701 error ("invalid encoding prefix in literal operator");
12702 return error_mark_node
;
12705 tree string_tree
= USERDEF_LITERAL_VALUE (token
->u
.value
);
12706 if (TREE_STRING_LENGTH (string_tree
) > 2)
12708 error ("expected empty string after %<operator%> keyword");
12709 return error_mark_node
;
12711 id
= USERDEF_LITERAL_SUFFIX_ID (token
->u
.value
);
12712 /* Consume the user-defined string literal. */
12713 cp_lexer_consume_token (parser
->lexer
);
12714 if (id
!= error_mark_node
)
12716 const char *name
= IDENTIFIER_POINTER (id
);
12717 return cp_literal_operator_id (name
);
12720 return error_mark_node
;
12724 /* Anything else is an error. */
12728 /* If we have selected an identifier, we need to consume the
12731 cp_lexer_consume_token (parser
->lexer
);
12732 /* Otherwise, no valid operator name was present. */
12735 cp_parser_error (parser
, "expected operator");
12736 id
= error_mark_node
;
12742 /* Parse a template-declaration.
12744 template-declaration:
12745 export [opt] template < template-parameter-list > declaration
12747 If MEMBER_P is TRUE, this template-declaration occurs within a
12750 The grammar rule given by the standard isn't correct. What
12751 is really meant is:
12753 template-declaration:
12754 export [opt] template-parameter-list-seq
12755 decl-specifier-seq [opt] init-declarator [opt] ;
12756 export [opt] template-parameter-list-seq
12757 function-definition
12759 template-parameter-list-seq:
12760 template-parameter-list-seq [opt]
12761 template < template-parameter-list > */
12764 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
12766 /* Check for `export'. */
12767 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
12769 /* Consume the `export' token. */
12770 cp_lexer_consume_token (parser
->lexer
);
12771 /* Warn that we do not support `export'. */
12772 warning (0, "keyword %<export%> not implemented, and will be ignored");
12775 cp_parser_template_declaration_after_export (parser
, member_p
);
12778 /* Parse a template-parameter-list.
12780 template-parameter-list:
12782 template-parameter-list , template-parameter
12784 Returns a TREE_LIST. Each node represents a template parameter.
12785 The nodes are connected via their TREE_CHAINs. */
12788 cp_parser_template_parameter_list (cp_parser
* parser
)
12790 tree parameter_list
= NULL_TREE
;
12792 begin_template_parm_list ();
12794 /* The loop below parses the template parms. We first need to know
12795 the total number of template parms to be able to compute proper
12796 canonical types of each dependent type. So after the loop, when
12797 we know the total number of template parms,
12798 end_template_parm_list computes the proper canonical types and
12799 fixes up the dependent types accordingly. */
12804 bool is_parameter_pack
;
12805 location_t parm_loc
;
12807 /* Parse the template-parameter. */
12808 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12809 parameter
= cp_parser_template_parameter (parser
,
12811 &is_parameter_pack
);
12812 /* Add it to the list. */
12813 if (parameter
!= error_mark_node
)
12814 parameter_list
= process_template_parm (parameter_list
,
12818 is_parameter_pack
);
12821 tree err_parm
= build_tree_list (parameter
, parameter
);
12822 parameter_list
= chainon (parameter_list
, err_parm
);
12825 /* If the next token is not a `,', we're done. */
12826 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12828 /* Otherwise, consume the `,' token. */
12829 cp_lexer_consume_token (parser
->lexer
);
12832 return end_template_parm_list (parameter_list
);
12835 /* Parse a template-parameter.
12837 template-parameter:
12839 parameter-declaration
12841 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12842 the parameter. The TREE_PURPOSE is the default value, if any.
12843 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12844 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12845 set to true iff this parameter is a parameter pack. */
12848 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
12849 bool *is_parameter_pack
)
12852 cp_parameter_declarator
*parameter_declarator
;
12853 cp_declarator
*id_declarator
;
12856 /* Assume it is a type parameter or a template parameter. */
12857 *is_non_type
= false;
12858 /* Assume it not a parameter pack. */
12859 *is_parameter_pack
= false;
12860 /* Peek at the next token. */
12861 token
= cp_lexer_peek_token (parser
->lexer
);
12862 /* If it is `class' or `template', we have a type-parameter. */
12863 if (token
->keyword
== RID_TEMPLATE
)
12864 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12865 /* If it is `class' or `typename' we do not know yet whether it is a
12866 type parameter or a non-type parameter. Consider:
12868 template <typename T, typename T::X X> ...
12872 template <class C, class D*> ...
12874 Here, the first parameter is a type parameter, and the second is
12875 a non-type parameter. We can tell by looking at the token after
12876 the identifier -- if it is a `,', `=', or `>' then we have a type
12878 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
12880 /* Peek at the token after `class' or `typename'. */
12881 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12882 /* If it's an ellipsis, we have a template type parameter
12884 if (token
->type
== CPP_ELLIPSIS
)
12885 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12886 /* If it's an identifier, skip it. */
12887 if (token
->type
== CPP_NAME
)
12888 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
12889 /* Now, see if the token looks like the end of a template
12891 if (token
->type
== CPP_COMMA
12892 || token
->type
== CPP_EQ
12893 || token
->type
== CPP_GREATER
)
12894 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12897 /* Otherwise, it is a non-type parameter.
12901 When parsing a default template-argument for a non-type
12902 template-parameter, the first non-nested `>' is taken as the end
12903 of the template parameter-list rather than a greater-than
12905 *is_non_type
= true;
12906 parameter_declarator
12907 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
12908 /*parenthesized_p=*/NULL
);
12910 /* If the parameter declaration is marked as a parameter pack, set
12911 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12912 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12914 if (parameter_declarator
12915 && parameter_declarator
->declarator
12916 && parameter_declarator
->declarator
->parameter_pack_p
)
12918 *is_parameter_pack
= true;
12919 parameter_declarator
->declarator
->parameter_pack_p
= false;
12922 if (parameter_declarator
12923 && parameter_declarator
->default_argument
)
12925 /* Can happen in some cases of erroneous input (c++/34892). */
12926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12927 /* Consume the `...' for better error recovery. */
12928 cp_lexer_consume_token (parser
->lexer
);
12930 /* If the next token is an ellipsis, and we don't already have it
12931 marked as a parameter pack, then we have a parameter pack (that
12932 has no declarator). */
12933 else if (!*is_parameter_pack
12934 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12935 && (declarator_can_be_parameter_pack
12936 (parameter_declarator
->declarator
)))
12938 /* Consume the `...'. */
12939 cp_lexer_consume_token (parser
->lexer
);
12940 maybe_warn_variadic_templates ();
12942 *is_parameter_pack
= true;
12944 /* We might end up with a pack expansion as the type of the non-type
12945 template parameter, in which case this is a non-type template
12947 else if (parameter_declarator
12948 && parameter_declarator
->decl_specifiers
.type
12949 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
12951 *is_parameter_pack
= true;
12952 parameter_declarator
->decl_specifiers
.type
=
12953 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
12956 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12958 /* Parameter packs cannot have default arguments. However, a
12959 user may try to do so, so we'll parse them and give an
12960 appropriate diagnostic here. */
12962 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12964 /* Find the name of the parameter pack. */
12965 id_declarator
= parameter_declarator
->declarator
;
12966 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
12967 id_declarator
= id_declarator
->declarator
;
12969 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
12970 error_at (start_token
->location
,
12971 "template parameter pack %qD cannot have a default argument",
12972 id_declarator
->u
.id
.unqualified_name
);
12974 error_at (start_token
->location
,
12975 "template parameter pack cannot have a default argument");
12977 /* Parse the default argument, but throw away the result. */
12978 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
12981 parm
= grokdeclarator (parameter_declarator
->declarator
,
12982 ¶meter_declarator
->decl_specifiers
,
12983 TPARM
, /*initialized=*/0,
12984 /*attrlist=*/NULL
);
12985 if (parm
== error_mark_node
)
12986 return error_mark_node
;
12988 return build_tree_list (parameter_declarator
->default_argument
, parm
);
12991 /* Parse a type-parameter.
12994 class identifier [opt]
12995 class identifier [opt] = type-id
12996 typename identifier [opt]
12997 typename identifier [opt] = type-id
12998 template < template-parameter-list > class identifier [opt]
12999 template < template-parameter-list > class identifier [opt]
13002 GNU Extension (variadic templates):
13005 class ... identifier [opt]
13006 typename ... identifier [opt]
13008 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13009 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13010 the declaration of the parameter.
13012 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13015 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13020 /* Look for a keyword to tell us what kind of parameter this is. */
13021 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13023 return error_mark_node
;
13025 switch (token
->keyword
)
13031 tree default_argument
;
13033 /* If the next token is an ellipsis, we have a template
13035 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13037 /* Consume the `...' token. */
13038 cp_lexer_consume_token (parser
->lexer
);
13039 maybe_warn_variadic_templates ();
13041 *is_parameter_pack
= true;
13044 /* If the next token is an identifier, then it names the
13046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13047 identifier
= cp_parser_identifier (parser
);
13049 identifier
= NULL_TREE
;
13051 /* Create the parameter. */
13052 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13054 /* If the next token is an `=', we have a default argument. */
13055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13057 /* Consume the `=' token. */
13058 cp_lexer_consume_token (parser
->lexer
);
13059 /* Parse the default-argument. */
13060 push_deferring_access_checks (dk_no_deferred
);
13061 default_argument
= cp_parser_type_id (parser
);
13063 /* Template parameter packs cannot have default
13065 if (*is_parameter_pack
)
13068 error_at (token
->location
,
13069 "template parameter pack %qD cannot have a "
13070 "default argument", identifier
);
13072 error_at (token
->location
,
13073 "template parameter packs cannot have "
13074 "default arguments");
13075 default_argument
= NULL_TREE
;
13077 pop_deferring_access_checks ();
13080 default_argument
= NULL_TREE
;
13082 /* Create the combined representation of the parameter and the
13083 default argument. */
13084 parameter
= build_tree_list (default_argument
, parameter
);
13091 tree default_argument
;
13093 /* Look for the `<'. */
13094 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13095 /* Parse the template-parameter-list. */
13096 cp_parser_template_parameter_list (parser
);
13097 /* Look for the `>'. */
13098 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13099 /* Look for the `class' keyword. */
13100 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
13101 /* If the next token is an ellipsis, we have a template
13103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13105 /* Consume the `...' token. */
13106 cp_lexer_consume_token (parser
->lexer
);
13107 maybe_warn_variadic_templates ();
13109 *is_parameter_pack
= true;
13111 /* If the next token is an `=', then there is a
13112 default-argument. If the next token is a `>', we are at
13113 the end of the parameter-list. If the next token is a `,',
13114 then we are at the end of this parameter. */
13115 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13116 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13117 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13119 identifier
= cp_parser_identifier (parser
);
13120 /* Treat invalid names as if the parameter were nameless. */
13121 if (identifier
== error_mark_node
)
13122 identifier
= NULL_TREE
;
13125 identifier
= NULL_TREE
;
13127 /* Create the template parameter. */
13128 parameter
= finish_template_template_parm (class_type_node
,
13131 /* If the next token is an `=', then there is a
13132 default-argument. */
13133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13137 /* Consume the `='. */
13138 cp_lexer_consume_token (parser
->lexer
);
13139 /* Parse the id-expression. */
13140 push_deferring_access_checks (dk_no_deferred
);
13141 /* save token before parsing the id-expression, for error
13143 token
= cp_lexer_peek_token (parser
->lexer
);
13145 = cp_parser_id_expression (parser
,
13146 /*template_keyword_p=*/false,
13147 /*check_dependency_p=*/true,
13148 /*template_p=*/&is_template
,
13149 /*declarator_p=*/false,
13150 /*optional_p=*/false);
13151 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13152 /* If the id-expression was a template-id that refers to
13153 a template-class, we already have the declaration here,
13154 so no further lookup is needed. */
13157 /* Look up the name. */
13159 = cp_parser_lookup_name (parser
, default_argument
,
13161 /*is_template=*/is_template
,
13162 /*is_namespace=*/false,
13163 /*check_dependency=*/true,
13164 /*ambiguous_decls=*/NULL
,
13166 /* See if the default argument is valid. */
13168 = check_template_template_default_arg (default_argument
);
13170 /* Template parameter packs cannot have default
13172 if (*is_parameter_pack
)
13175 error_at (token
->location
,
13176 "template parameter pack %qD cannot "
13177 "have a default argument",
13180 error_at (token
->location
, "template parameter packs cannot "
13181 "have default arguments");
13182 default_argument
= NULL_TREE
;
13184 pop_deferring_access_checks ();
13187 default_argument
= NULL_TREE
;
13189 /* Create the combined representation of the parameter and the
13190 default argument. */
13191 parameter
= build_tree_list (default_argument
, parameter
);
13196 gcc_unreachable ();
13203 /* Parse a template-id.
13206 template-name < template-argument-list [opt] >
13208 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13209 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13210 returned. Otherwise, if the template-name names a function, or set
13211 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13212 names a class, returns a TYPE_DECL for the specialization.
13214 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13215 uninstantiated templates. */
13218 cp_parser_template_id (cp_parser
*parser
,
13219 bool template_keyword_p
,
13220 bool check_dependency_p
,
13221 enum tag_types tag_type
,
13222 bool is_declaration
)
13228 cp_token_position start_of_id
= 0;
13229 deferred_access_check
*chk
;
13230 vec
<deferred_access_check
, va_gc
> *access_check
;
13231 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13232 bool is_identifier
;
13234 /* If the next token corresponds to a template-id, there is no need
13236 next_token
= cp_lexer_peek_token (parser
->lexer
);
13237 if (next_token
->type
== CPP_TEMPLATE_ID
)
13239 struct tree_check
*check_value
;
13241 /* Get the stored value. */
13242 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13243 /* Perform any access checks that were deferred. */
13244 access_check
= check_value
->checks
;
13247 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13248 perform_or_defer_access_check (chk
->binfo
,
13251 tf_warning_or_error
);
13253 /* Return the stored value. */
13254 return check_value
->value
;
13257 /* Avoid performing name lookup if there is no possibility of
13258 finding a template-id. */
13259 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13260 || (next_token
->type
== CPP_NAME
13261 && !cp_parser_nth_token_starts_template_argument_list_p
13264 cp_parser_error (parser
, "expected template-id");
13265 return error_mark_node
;
13268 /* Remember where the template-id starts. */
13269 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13270 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13272 push_deferring_access_checks (dk_deferred
);
13274 /* Parse the template-name. */
13275 is_identifier
= false;
13276 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13277 check_dependency_p
,
13281 if (templ
== error_mark_node
|| is_identifier
)
13283 pop_deferring_access_checks ();
13287 /* If we find the sequence `[:' after a template-name, it's probably
13288 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13289 parse correctly the argument list. */
13290 next_token
= cp_lexer_peek_token (parser
->lexer
);
13291 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13292 if (next_token
->type
== CPP_OPEN_SQUARE
13293 && next_token
->flags
& DIGRAPH
13294 && next_token_2
->type
== CPP_COLON
13295 && !(next_token_2
->flags
& PREV_WHITE
))
13297 cp_parser_parse_tentatively (parser
);
13298 /* Change `:' into `::'. */
13299 next_token_2
->type
= CPP_SCOPE
;
13300 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13302 cp_lexer_consume_token (parser
->lexer
);
13304 /* Parse the arguments. */
13305 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13306 if (!cp_parser_parse_definitely (parser
))
13308 /* If we couldn't parse an argument list, then we revert our changes
13309 and return simply an error. Maybe this is not a template-id
13311 next_token_2
->type
= CPP_COLON
;
13312 cp_parser_error (parser
, "expected %<<%>");
13313 pop_deferring_access_checks ();
13314 return error_mark_node
;
13316 /* Otherwise, emit an error about the invalid digraph, but continue
13317 parsing because we got our argument list. */
13318 if (permerror (next_token
->location
,
13319 "%<<::%> cannot begin a template-argument list"))
13321 static bool hint
= false;
13322 inform (next_token
->location
,
13323 "%<<:%> is an alternate spelling for %<[%>."
13324 " Insert whitespace between %<<%> and %<::%>");
13325 if (!hint
&& !flag_permissive
)
13327 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13328 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13329 "accept your code)");
13336 /* Look for the `<' that starts the template-argument-list. */
13337 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13339 pop_deferring_access_checks ();
13340 return error_mark_node
;
13342 /* Parse the arguments. */
13343 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13346 /* Build a representation of the specialization. */
13347 if (identifier_p (templ
))
13348 template_id
= build_min_nt_loc (next_token
->location
,
13351 else if (DECL_TYPE_TEMPLATE_P (templ
)
13352 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13354 bool entering_scope
;
13355 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13356 template (rather than some instantiation thereof) only if
13357 is not nested within some other construct. For example, in
13358 "template <typename T> void f(T) { A<T>::", A<T> is just an
13359 instantiation of A. */
13360 entering_scope
= (template_parm_scope_p ()
13361 && cp_lexer_next_token_is (parser
->lexer
,
13364 = finish_template_type (templ
, arguments
, entering_scope
);
13368 /* If it's not a class-template or a template-template, it should be
13369 a function-template. */
13370 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13371 || TREE_CODE (templ
) == OVERLOAD
13372 || BASELINK_P (templ
)));
13374 template_id
= lookup_template_function (templ
, arguments
);
13377 /* If parsing tentatively, replace the sequence of tokens that makes
13378 up the template-id with a CPP_TEMPLATE_ID token. That way,
13379 should we re-parse the token stream, we will not have to repeat
13380 the effort required to do the parse, nor will we issue duplicate
13381 error messages about problems during instantiation of the
13385 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13387 /* Reset the contents of the START_OF_ID token. */
13388 token
->type
= CPP_TEMPLATE_ID
;
13389 /* Retrieve any deferred checks. Do not pop this access checks yet
13390 so the memory will not be reclaimed during token replacing below. */
13391 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
13392 token
->u
.tree_check_value
->value
= template_id
;
13393 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13394 token
->keyword
= RID_MAX
;
13396 /* Purge all subsequent tokens. */
13397 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13399 /* ??? Can we actually assume that, if template_id ==
13400 error_mark_node, we will have issued a diagnostic to the
13401 user, as opposed to simply marking the tentative parse as
13403 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13404 error_at (token
->location
, "parse error in template argument list");
13407 pop_to_parent_deferring_access_checks ();
13408 return template_id
;
13411 /* Parse a template-name.
13416 The standard should actually say:
13420 operator-function-id
13422 A defect report has been filed about this issue.
13424 A conversion-function-id cannot be a template name because they cannot
13425 be part of a template-id. In fact, looking at this code:
13427 a.operator K<int>()
13429 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13430 It is impossible to call a templated conversion-function-id with an
13431 explicit argument list, since the only allowed template parameter is
13432 the type to which it is converting.
13434 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13435 `template' keyword, in a construction like:
13439 In that case `f' is taken to be a template-name, even though there
13440 is no way of knowing for sure.
13442 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13443 name refers to a set of overloaded functions, at least one of which
13444 is a template, or an IDENTIFIER_NODE with the name of the template,
13445 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13446 names are looked up inside uninstantiated templates. */
13449 cp_parser_template_name (cp_parser
* parser
,
13450 bool template_keyword_p
,
13451 bool check_dependency_p
,
13452 bool is_declaration
,
13453 enum tag_types tag_type
,
13454 bool *is_identifier
)
13459 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13461 /* If the next token is `operator', then we have either an
13462 operator-function-id or a conversion-function-id. */
13463 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13465 /* We don't know whether we're looking at an
13466 operator-function-id or a conversion-function-id. */
13467 cp_parser_parse_tentatively (parser
);
13468 /* Try an operator-function-id. */
13469 identifier
= cp_parser_operator_function_id (parser
);
13470 /* If that didn't work, try a conversion-function-id. */
13471 if (!cp_parser_parse_definitely (parser
))
13473 cp_parser_error (parser
, "expected template-name");
13474 return error_mark_node
;
13477 /* Look for the identifier. */
13479 identifier
= cp_parser_identifier (parser
);
13481 /* If we didn't find an identifier, we don't have a template-id. */
13482 if (identifier
== error_mark_node
)
13483 return error_mark_node
;
13485 /* If the name immediately followed the `template' keyword, then it
13486 is a template-name. However, if the next token is not `<', then
13487 we do not treat it as a template-name, since it is not being used
13488 as part of a template-id. This enables us to handle constructs
13491 template <typename T> struct S { S(); };
13492 template <typename T> S<T>::S();
13494 correctly. We would treat `S' as a template -- if it were `S<T>'
13495 -- but we do not if there is no `<'. */
13497 if (processing_template_decl
13498 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13500 /* In a declaration, in a dependent context, we pretend that the
13501 "template" keyword was present in order to improve error
13502 recovery. For example, given:
13504 template <typename T> void f(T::X<int>);
13506 we want to treat "X<int>" as a template-id. */
13508 && !template_keyword_p
13509 && parser
->scope
&& TYPE_P (parser
->scope
)
13510 && check_dependency_p
13511 && dependent_scope_p (parser
->scope
)
13512 /* Do not do this for dtors (or ctors), since they never
13513 need the template keyword before their name. */
13514 && !constructor_name_p (identifier
, parser
->scope
))
13516 cp_token_position start
= 0;
13518 /* Explain what went wrong. */
13519 error_at (token
->location
, "non-template %qD used as template",
13521 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
13522 parser
->scope
, identifier
);
13523 /* If parsing tentatively, find the location of the "<" token. */
13524 if (cp_parser_simulate_error (parser
))
13525 start
= cp_lexer_token_position (parser
->lexer
, true);
13526 /* Parse the template arguments so that we can issue error
13527 messages about them. */
13528 cp_lexer_consume_token (parser
->lexer
);
13529 cp_parser_enclosed_template_argument_list (parser
);
13530 /* Skip tokens until we find a good place from which to
13531 continue parsing. */
13532 cp_parser_skip_to_closing_parenthesis (parser
,
13533 /*recovering=*/true,
13535 /*consume_paren=*/false);
13536 /* If parsing tentatively, permanently remove the
13537 template argument list. That will prevent duplicate
13538 error messages from being issued about the missing
13539 "template" keyword. */
13541 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
13543 *is_identifier
= true;
13547 /* If the "template" keyword is present, then there is generally
13548 no point in doing name-lookup, so we just return IDENTIFIER.
13549 But, if the qualifying scope is non-dependent then we can
13550 (and must) do name-lookup normally. */
13551 if (template_keyword_p
13553 || (TYPE_P (parser
->scope
)
13554 && dependent_type_p (parser
->scope
))))
13558 /* Look up the name. */
13559 decl
= cp_parser_lookup_name (parser
, identifier
,
13561 /*is_template=*/true,
13562 /*is_namespace=*/false,
13563 check_dependency_p
,
13564 /*ambiguous_decls=*/NULL
,
13567 /* If DECL is a template, then the name was a template-name. */
13568 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
13572 tree fn
= NULL_TREE
;
13574 /* The standard does not explicitly indicate whether a name that
13575 names a set of overloaded declarations, some of which are
13576 templates, is a template-name. However, such a name should
13577 be a template-name; otherwise, there is no way to form a
13578 template-id for the overloaded templates. */
13579 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
13580 if (TREE_CODE (fns
) == OVERLOAD
)
13581 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
13582 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
13587 /* The name does not name a template. */
13588 cp_parser_error (parser
, "expected template-name");
13589 return error_mark_node
;
13593 /* If DECL is dependent, and refers to a function, then just return
13594 its name; we will look it up again during template instantiation. */
13595 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
13597 tree scope
= ovl_scope (decl
);
13598 if (TYPE_P (scope
) && dependent_type_p (scope
))
13605 /* Parse a template-argument-list.
13607 template-argument-list:
13608 template-argument ... [opt]
13609 template-argument-list , template-argument ... [opt]
13611 Returns a TREE_VEC containing the arguments. */
13614 cp_parser_template_argument_list (cp_parser
* parser
)
13616 tree fixed_args
[10];
13617 unsigned n_args
= 0;
13618 unsigned alloced
= 10;
13619 tree
*arg_ary
= fixed_args
;
13621 bool saved_in_template_argument_list_p
;
13623 bool saved_non_ice_p
;
13625 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
13626 parser
->in_template_argument_list_p
= true;
13627 /* Even if the template-id appears in an integral
13628 constant-expression, the contents of the argument list do
13630 saved_ice_p
= parser
->integral_constant_expression_p
;
13631 parser
->integral_constant_expression_p
= false;
13632 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
13633 parser
->non_integral_constant_expression_p
= false;
13635 /* Parse the arguments. */
13641 /* Consume the comma. */
13642 cp_lexer_consume_token (parser
->lexer
);
13644 /* Parse the template-argument. */
13645 argument
= cp_parser_template_argument (parser
);
13647 /* If the next token is an ellipsis, we're expanding a template
13649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13651 if (argument
== error_mark_node
)
13653 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13654 error_at (token
->location
,
13655 "expected parameter pack before %<...%>");
13657 /* Consume the `...' token. */
13658 cp_lexer_consume_token (parser
->lexer
);
13660 /* Make the argument into a TYPE_PACK_EXPANSION or
13661 EXPR_PACK_EXPANSION. */
13662 argument
= make_pack_expansion (argument
);
13665 if (n_args
== alloced
)
13669 if (arg_ary
== fixed_args
)
13671 arg_ary
= XNEWVEC (tree
, alloced
);
13672 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
13675 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
13677 arg_ary
[n_args
++] = argument
;
13679 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
13681 vec
= make_tree_vec (n_args
);
13684 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
13686 if (arg_ary
!= fixed_args
)
13688 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
13689 parser
->integral_constant_expression_p
= saved_ice_p
;
13690 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
13691 #ifdef ENABLE_CHECKING
13692 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
13697 /* Parse a template-argument.
13700 assignment-expression
13704 The representation is that of an assignment-expression, type-id, or
13705 id-expression -- except that the qualified id-expression is
13706 evaluated, so that the value returned is either a DECL or an
13709 Although the standard says "assignment-expression", it forbids
13710 throw-expressions or assignments in the template argument.
13711 Therefore, we use "conditional-expression" instead. */
13714 cp_parser_template_argument (cp_parser
* parser
)
13719 bool maybe_type_id
= false;
13720 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
13721 location_t loc
= 0;
13724 /* There's really no way to know what we're looking at, so we just
13725 try each alternative in order.
13729 In a template-argument, an ambiguity between a type-id and an
13730 expression is resolved to a type-id, regardless of the form of
13731 the corresponding template-parameter.
13733 Therefore, we try a type-id first. */
13734 cp_parser_parse_tentatively (parser
);
13735 argument
= cp_parser_template_type_arg (parser
);
13736 /* If there was no error parsing the type-id but the next token is a
13737 '>>', our behavior depends on which dialect of C++ we're
13738 parsing. In C++98, we probably found a typo for '> >'. But there
13739 are type-id which are also valid expressions. For instance:
13741 struct X { int operator >> (int); };
13742 template <int V> struct Foo {};
13745 Here 'X()' is a valid type-id of a function type, but the user just
13746 wanted to write the expression "X() >> 5". Thus, we remember that we
13747 found a valid type-id, but we still try to parse the argument as an
13748 expression to see what happens.
13750 In C++0x, the '>>' will be considered two separate '>'
13752 if (!cp_parser_error_occurred (parser
)
13753 && cxx_dialect
== cxx98
13754 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
13756 maybe_type_id
= true;
13757 cp_parser_abort_tentative_parse (parser
);
13761 /* If the next token isn't a `,' or a `>', then this argument wasn't
13762 really finished. This means that the argument is not a valid
13764 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13765 cp_parser_error (parser
, "expected template-argument");
13766 /* If that worked, we're done. */
13767 if (cp_parser_parse_definitely (parser
))
13770 /* We're still not sure what the argument will be. */
13771 cp_parser_parse_tentatively (parser
);
13772 /* Try a template. */
13773 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
13774 argument
= cp_parser_id_expression (parser
,
13775 /*template_keyword_p=*/false,
13776 /*check_dependency_p=*/true,
13778 /*declarator_p=*/false,
13779 /*optional_p=*/false);
13780 /* If the next token isn't a `,' or a `>', then this argument wasn't
13781 really finished. */
13782 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13783 cp_parser_error (parser
, "expected template-argument");
13784 if (!cp_parser_error_occurred (parser
))
13786 /* Figure out what is being referred to. If the id-expression
13787 was for a class template specialization, then we will have a
13788 TYPE_DECL at this point. There is no need to do name lookup
13789 at this point in that case. */
13790 if (TREE_CODE (argument
) != TYPE_DECL
)
13791 argument
= cp_parser_lookup_name (parser
, argument
,
13793 /*is_template=*/template_p
,
13794 /*is_namespace=*/false,
13795 /*check_dependency=*/true,
13796 /*ambiguous_decls=*/NULL
,
13797 argument_start_token
->location
);
13798 if (TREE_CODE (argument
) != TEMPLATE_DECL
13799 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
13800 cp_parser_error (parser
, "expected template-name");
13802 if (cp_parser_parse_definitely (parser
))
13804 /* It must be a non-type argument. There permitted cases are given
13805 in [temp.arg.nontype]:
13807 -- an integral constant-expression of integral or enumeration
13810 -- the name of a non-type template-parameter; or
13812 -- the name of an object or function with external linkage...
13814 -- the address of an object or function with external linkage...
13816 -- a pointer to member... */
13817 /* Look for a non-type template parameter. */
13818 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13820 cp_parser_parse_tentatively (parser
);
13821 argument
= cp_parser_primary_expression (parser
,
13822 /*address_p=*/false,
13824 /*template_arg_p=*/true,
13826 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
13827 || !cp_parser_next_token_ends_template_argument_p (parser
))
13828 cp_parser_simulate_error (parser
);
13829 if (cp_parser_parse_definitely (parser
))
13833 /* If the next token is "&", the argument must be the address of an
13834 object or function with external linkage. */
13835 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
13838 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13839 cp_lexer_consume_token (parser
->lexer
);
13841 /* See if we might have an id-expression. */
13842 token
= cp_lexer_peek_token (parser
->lexer
);
13843 if (token
->type
== CPP_NAME
13844 || token
->keyword
== RID_OPERATOR
13845 || token
->type
== CPP_SCOPE
13846 || token
->type
== CPP_TEMPLATE_ID
13847 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
13849 cp_parser_parse_tentatively (parser
);
13850 argument
= cp_parser_primary_expression (parser
,
13853 /*template_arg_p=*/true,
13855 if (cp_parser_error_occurred (parser
)
13856 || !cp_parser_next_token_ends_template_argument_p (parser
))
13857 cp_parser_abort_tentative_parse (parser
);
13862 if (INDIRECT_REF_P (argument
))
13864 gcc_assert (REFERENCE_REF_P (argument
));
13865 argument
= TREE_OPERAND (argument
, 0);
13868 /* If we're in a template, we represent a qualified-id referring
13869 to a static data member as a SCOPE_REF even if the scope isn't
13870 dependent so that we can check access control later. */
13872 if (TREE_CODE (probe
) == SCOPE_REF
)
13873 probe
= TREE_OPERAND (probe
, 1);
13876 /* A variable without external linkage might still be a
13877 valid constant-expression, so no error is issued here
13878 if the external-linkage check fails. */
13879 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
13880 cp_parser_simulate_error (parser
);
13882 else if (is_overloaded_fn (argument
))
13883 /* All overloaded functions are allowed; if the external
13884 linkage test does not pass, an error will be issued
13888 && (TREE_CODE (argument
) == OFFSET_REF
13889 || TREE_CODE (argument
) == SCOPE_REF
))
13890 /* A pointer-to-member. */
13892 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
13895 cp_parser_simulate_error (parser
);
13897 if (cp_parser_parse_definitely (parser
))
13900 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
13901 tf_warning_or_error
);
13906 /* If the argument started with "&", there are no other valid
13907 alternatives at this point. */
13910 cp_parser_error (parser
, "invalid non-type template argument");
13911 return error_mark_node
;
13914 /* If the argument wasn't successfully parsed as a type-id followed
13915 by '>>', the argument can only be a constant expression now.
13916 Otherwise, we try parsing the constant-expression tentatively,
13917 because the argument could really be a type-id. */
13919 cp_parser_parse_tentatively (parser
);
13920 argument
= cp_parser_constant_expression (parser
,
13921 /*allow_non_constant_p=*/false,
13922 /*non_constant_p=*/NULL
);
13923 if (!maybe_type_id
)
13925 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13926 cp_parser_error (parser
, "expected template-argument");
13927 if (cp_parser_parse_definitely (parser
))
13929 /* We did our best to parse the argument as a non type-id, but that
13930 was the only alternative that matched (albeit with a '>' after
13931 it). We can assume it's just a typo from the user, and a
13932 diagnostic will then be issued. */
13933 return cp_parser_template_type_arg (parser
);
13936 /* Parse an explicit-instantiation.
13938 explicit-instantiation:
13939 template declaration
13941 Although the standard says `declaration', what it really means is:
13943 explicit-instantiation:
13944 template decl-specifier-seq [opt] declarator [opt] ;
13946 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13947 supposed to be allowed. A defect report has been filed about this
13952 explicit-instantiation:
13953 storage-class-specifier template
13954 decl-specifier-seq [opt] declarator [opt] ;
13955 function-specifier template
13956 decl-specifier-seq [opt] declarator [opt] ; */
13959 cp_parser_explicit_instantiation (cp_parser
* parser
)
13961 int declares_class_or_enum
;
13962 cp_decl_specifier_seq decl_specifiers
;
13963 tree extension_specifier
= NULL_TREE
;
13965 timevar_push (TV_TEMPLATE_INST
);
13967 /* Look for an (optional) storage-class-specifier or
13968 function-specifier. */
13969 if (cp_parser_allow_gnu_extensions_p (parser
))
13971 extension_specifier
13972 = cp_parser_storage_class_specifier_opt (parser
);
13973 if (!extension_specifier
)
13974 extension_specifier
13975 = cp_parser_function_specifier_opt (parser
,
13976 /*decl_specs=*/NULL
);
13979 /* Look for the `template' keyword. */
13980 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
13981 /* Let the front end know that we are processing an explicit
13983 begin_explicit_instantiation ();
13984 /* [temp.explicit] says that we are supposed to ignore access
13985 control while processing explicit instantiation directives. */
13986 push_deferring_access_checks (dk_no_check
);
13987 /* Parse a decl-specifier-seq. */
13988 cp_parser_decl_specifier_seq (parser
,
13989 CP_PARSER_FLAGS_OPTIONAL
,
13991 &declares_class_or_enum
);
13992 /* If there was exactly one decl-specifier, and it declared a class,
13993 and there's no declarator, then we have an explicit type
13995 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
13999 type
= check_tag_decl (&decl_specifiers
,
14000 /*explicit_type_instantiation_p=*/true);
14001 /* Turn access control back on for names used during
14002 template instantiation. */
14003 pop_deferring_access_checks ();
14005 do_type_instantiation (type
, extension_specifier
,
14006 /*complain=*/tf_error
);
14010 cp_declarator
*declarator
;
14013 /* Parse the declarator. */
14015 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14016 /*ctor_dtor_or_conv_p=*/NULL
,
14017 /*parenthesized_p=*/NULL
,
14018 /*member_p=*/false);
14019 if (declares_class_or_enum
& 2)
14020 cp_parser_check_for_definition_in_return_type (declarator
,
14021 decl_specifiers
.type
,
14022 decl_specifiers
.locations
[ds_type_spec
]);
14023 if (declarator
!= cp_error_declarator
)
14025 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14026 permerror (decl_specifiers
.locations
[ds_inline
],
14027 "explicit instantiation shall not use"
14028 " %<inline%> specifier");
14029 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14030 permerror (decl_specifiers
.locations
[ds_constexpr
],
14031 "explicit instantiation shall not use"
14032 " %<constexpr%> specifier");
14034 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14035 NORMAL
, 0, &decl_specifiers
.attributes
);
14036 /* Turn access control back on for names used during
14037 template instantiation. */
14038 pop_deferring_access_checks ();
14039 /* Do the explicit instantiation. */
14040 do_decl_instantiation (decl
, extension_specifier
);
14044 pop_deferring_access_checks ();
14045 /* Skip the body of the explicit instantiation. */
14046 cp_parser_skip_to_end_of_statement (parser
);
14049 /* We're done with the instantiation. */
14050 end_explicit_instantiation ();
14052 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14054 timevar_pop (TV_TEMPLATE_INST
);
14057 /* Parse an explicit-specialization.
14059 explicit-specialization:
14060 template < > declaration
14062 Although the standard says `declaration', what it really means is:
14064 explicit-specialization:
14065 template <> decl-specifier [opt] init-declarator [opt] ;
14066 template <> function-definition
14067 template <> explicit-specialization
14068 template <> template-declaration */
14071 cp_parser_explicit_specialization (cp_parser
* parser
)
14073 bool need_lang_pop
;
14074 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14076 /* Look for the `template' keyword. */
14077 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14078 /* Look for the `<'. */
14079 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14080 /* Look for the `>'. */
14081 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14082 /* We have processed another parameter list. */
14083 ++parser
->num_template_parameter_lists
;
14086 A template ... explicit specialization ... shall not have C
14088 if (current_lang_name
== lang_name_c
)
14090 error_at (token
->location
, "template specialization with C linkage");
14091 /* Give it C++ linkage to avoid confusing other parts of the
14093 push_lang_context (lang_name_cplusplus
);
14094 need_lang_pop
= true;
14097 need_lang_pop
= false;
14098 /* Let the front end know that we are beginning a specialization. */
14099 if (!begin_specialization ())
14101 end_specialization ();
14105 /* If the next keyword is `template', we need to figure out whether
14106 or not we're looking a template-declaration. */
14107 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14109 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14110 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14111 cp_parser_template_declaration_after_export (parser
,
14112 /*member_p=*/false);
14114 cp_parser_explicit_specialization (parser
);
14117 /* Parse the dependent declaration. */
14118 cp_parser_single_declaration (parser
,
14120 /*member_p=*/false,
14121 /*explicit_specialization_p=*/true,
14122 /*friend_p=*/NULL
);
14123 /* We're done with the specialization. */
14124 end_specialization ();
14125 /* For the erroneous case of a template with C linkage, we pushed an
14126 implicit C++ linkage scope; exit that scope now. */
14128 pop_lang_context ();
14129 /* We're done with this parameter list. */
14130 --parser
->num_template_parameter_lists
;
14133 /* Parse a type-specifier.
14136 simple-type-specifier
14139 elaborated-type-specifier
14147 Returns a representation of the type-specifier. For a
14148 class-specifier, enum-specifier, or elaborated-type-specifier, a
14149 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14151 The parser flags FLAGS is used to control type-specifier parsing.
14153 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14154 in a decl-specifier-seq.
14156 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14157 class-specifier, enum-specifier, or elaborated-type-specifier, then
14158 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14159 if a type is declared; 2 if it is defined. Otherwise, it is set to
14162 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14163 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14164 is set to FALSE. */
14167 cp_parser_type_specifier (cp_parser
* parser
,
14168 cp_parser_flags flags
,
14169 cp_decl_specifier_seq
*decl_specs
,
14170 bool is_declaration
,
14171 int* declares_class_or_enum
,
14172 bool* is_cv_qualifier
)
14174 tree type_spec
= NULL_TREE
;
14177 cp_decl_spec ds
= ds_last
;
14179 /* Assume this type-specifier does not declare a new type. */
14180 if (declares_class_or_enum
)
14181 *declares_class_or_enum
= 0;
14182 /* And that it does not specify a cv-qualifier. */
14183 if (is_cv_qualifier
)
14184 *is_cv_qualifier
= false;
14185 /* Peek at the next token. */
14186 token
= cp_lexer_peek_token (parser
->lexer
);
14188 /* If we're looking at a keyword, we can use that to guide the
14189 production we choose. */
14190 keyword
= token
->keyword
;
14194 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14195 goto elaborated_type_specifier
;
14197 /* Look for the enum-specifier. */
14198 type_spec
= cp_parser_enum_specifier (parser
);
14199 /* If that worked, we're done. */
14202 if (declares_class_or_enum
)
14203 *declares_class_or_enum
= 2;
14205 cp_parser_set_decl_spec_type (decl_specs
,
14208 /*type_definition_p=*/true);
14212 goto elaborated_type_specifier
;
14214 /* Any of these indicate either a class-specifier, or an
14215 elaborated-type-specifier. */
14219 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14220 goto elaborated_type_specifier
;
14222 /* Parse tentatively so that we can back up if we don't find a
14223 class-specifier. */
14224 cp_parser_parse_tentatively (parser
);
14225 /* Look for the class-specifier. */
14226 type_spec
= cp_parser_class_specifier (parser
);
14227 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14228 /* If that worked, we're done. */
14229 if (cp_parser_parse_definitely (parser
))
14231 if (declares_class_or_enum
)
14232 *declares_class_or_enum
= 2;
14234 cp_parser_set_decl_spec_type (decl_specs
,
14237 /*type_definition_p=*/true);
14241 /* Fall through. */
14242 elaborated_type_specifier
:
14243 /* We're declaring (not defining) a class or enum. */
14244 if (declares_class_or_enum
)
14245 *declares_class_or_enum
= 1;
14247 /* Fall through. */
14249 /* Look for an elaborated-type-specifier. */
14251 = (cp_parser_elaborated_type_specifier
14253 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14256 cp_parser_set_decl_spec_type (decl_specs
,
14259 /*type_definition_p=*/false);
14264 if (is_cv_qualifier
)
14265 *is_cv_qualifier
= true;
14270 if (is_cv_qualifier
)
14271 *is_cv_qualifier
= true;
14276 if (is_cv_qualifier
)
14277 *is_cv_qualifier
= true;
14281 /* The `__complex__' keyword is a GNU extension. */
14289 /* Handle simple keywords. */
14294 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14295 decl_specs
->any_specifiers_p
= true;
14297 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14300 /* If we do not already have a type-specifier, assume we are looking
14301 at a simple-type-specifier. */
14302 type_spec
= cp_parser_simple_type_specifier (parser
,
14306 /* If we didn't find a type-specifier, and a type-specifier was not
14307 optional in this context, issue an error message. */
14308 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14310 cp_parser_error (parser
, "expected type specifier");
14311 return error_mark_node
;
14317 /* Parse a simple-type-specifier.
14319 simple-type-specifier:
14320 :: [opt] nested-name-specifier [opt] type-name
14321 :: [opt] nested-name-specifier template template-id
14336 simple-type-specifier:
14338 decltype ( expression )
14341 __underlying_type ( type-id )
14345 simple-type-specifier:
14347 __typeof__ unary-expression
14348 __typeof__ ( type-id )
14349 __typeof__ ( type-id ) { initializer-list , [opt] }
14351 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14352 appropriately updated. */
14355 cp_parser_simple_type_specifier (cp_parser
* parser
,
14356 cp_decl_specifier_seq
*decl_specs
,
14357 cp_parser_flags flags
)
14359 tree type
= NULL_TREE
;
14362 /* Peek at the next token. */
14363 token
= cp_lexer_peek_token (parser
->lexer
);
14365 /* If we're looking at a keyword, things are easy. */
14366 switch (token
->keyword
)
14370 decl_specs
->explicit_char_p
= true;
14371 type
= char_type_node
;
14374 type
= char16_type_node
;
14377 type
= char32_type_node
;
14380 type
= wchar_type_node
;
14383 type
= boolean_type_node
;
14386 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14387 type
= short_integer_type_node
;
14391 decl_specs
->explicit_int_p
= true;
14392 type
= integer_type_node
;
14395 if (!int128_integer_type_node
)
14398 decl_specs
->explicit_int128_p
= true;
14399 type
= int128_integer_type_node
;
14403 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14404 type
= long_integer_type_node
;
14407 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14408 type
= integer_type_node
;
14411 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14412 type
= unsigned_type_node
;
14415 type
= float_type_node
;
14418 type
= double_type_node
;
14421 type
= void_type_node
;
14425 maybe_warn_cpp0x (CPP0X_AUTO
);
14426 if (parser
->auto_is_implicit_function_template_parm_p
)
14428 type
= synthesize_implicit_template_parm (parser
);
14430 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14432 if (cxx_dialect
< cxx1y
)
14433 pedwarn (location_of (type
), 0,
14434 "use of %<auto%> in lambda parameter declaration "
14435 "only available with "
14436 "-std=c++1y or -std=gnu++1y");
14438 else if (cxx_dialect
< cxx1y
)
14439 pedwarn (location_of (type
), 0,
14440 "use of %<auto%> in parameter declaration "
14441 "only available with "
14442 "-std=c++1y or -std=gnu++1y");
14444 pedwarn (location_of (type
), OPT_Wpedantic
,
14445 "ISO C++ forbids use of %<auto%> in parameter "
14449 type
= make_auto ();
14453 /* Since DR 743, decltype can either be a simple-type-specifier by
14454 itself or begin a nested-name-specifier. Parsing it will replace
14455 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14456 handling below decide what to do. */
14457 cp_parser_decltype (parser
);
14458 cp_lexer_set_token_position (parser
->lexer
, token
);
14462 /* Consume the `typeof' token. */
14463 cp_lexer_consume_token (parser
->lexer
);
14464 /* Parse the operand to `typeof'. */
14465 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14466 /* If it is not already a TYPE, take its type. */
14467 if (!TYPE_P (type
))
14468 type
= finish_typeof (type
);
14471 cp_parser_set_decl_spec_type (decl_specs
, type
,
14473 /*type_definition_p=*/false);
14477 case RID_UNDERLYING_TYPE
:
14478 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14480 cp_parser_set_decl_spec_type (decl_specs
, type
,
14482 /*type_definition_p=*/false);
14487 case RID_DIRECT_BASES
:
14488 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14490 cp_parser_set_decl_spec_type (decl_specs
, type
,
14492 /*type_definition_p=*/false);
14498 /* If token is an already-parsed decltype not followed by ::,
14499 it's a simple-type-specifier. */
14500 if (token
->type
== CPP_DECLTYPE
14501 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
14503 type
= token
->u
.value
;
14505 cp_parser_set_decl_spec_type (decl_specs
, type
,
14507 /*type_definition_p=*/false);
14508 cp_lexer_consume_token (parser
->lexer
);
14512 /* If the type-specifier was for a built-in type, we're done. */
14515 /* Record the type. */
14517 && (token
->keyword
!= RID_SIGNED
14518 && token
->keyword
!= RID_UNSIGNED
14519 && token
->keyword
!= RID_SHORT
14520 && token
->keyword
!= RID_LONG
))
14521 cp_parser_set_decl_spec_type (decl_specs
,
14524 /*type_definition_p=*/false);
14526 decl_specs
->any_specifiers_p
= true;
14528 /* Consume the token. */
14529 cp_lexer_consume_token (parser
->lexer
);
14531 /* There is no valid C++ program where a non-template type is
14532 followed by a "<". That usually indicates that the user thought
14533 that the type was a template. */
14534 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
14537 return TYPE_NAME (type
);
14540 /* The type-specifier must be a user-defined type. */
14541 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
14546 /* Don't gobble tokens or issue error messages if this is an
14547 optional type-specifier. */
14548 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
14549 cp_parser_parse_tentatively (parser
);
14551 /* Look for the optional `::' operator. */
14553 = (cp_parser_global_scope_opt (parser
,
14554 /*current_scope_valid_p=*/false)
14556 /* Look for the nested-name specifier. */
14558 = (cp_parser_nested_name_specifier_opt (parser
,
14559 /*typename_keyword_p=*/false,
14560 /*check_dependency_p=*/true,
14562 /*is_declaration=*/false)
14564 token
= cp_lexer_peek_token (parser
->lexer
);
14565 /* If we have seen a nested-name-specifier, and the next token
14566 is `template', then we are using the template-id production. */
14568 && cp_parser_optional_template_keyword (parser
))
14570 /* Look for the template-id. */
14571 type
= cp_parser_template_id (parser
,
14572 /*template_keyword_p=*/true,
14573 /*check_dependency_p=*/true,
14575 /*is_declaration=*/false);
14576 /* If the template-id did not name a type, we are out of
14578 if (TREE_CODE (type
) != TYPE_DECL
)
14580 cp_parser_error (parser
, "expected template-id for type");
14584 /* Otherwise, look for a type-name. */
14586 type
= cp_parser_type_name (parser
);
14587 /* Keep track of all name-lookups performed in class scopes. */
14591 && TREE_CODE (type
) == TYPE_DECL
14592 && identifier_p (DECL_NAME (type
)))
14593 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
14594 /* If it didn't work out, we don't have a TYPE. */
14595 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
14596 && !cp_parser_parse_definitely (parser
))
14598 if (type
&& decl_specs
)
14599 cp_parser_set_decl_spec_type (decl_specs
, type
,
14601 /*type_definition_p=*/false);
14604 /* If we didn't get a type-name, issue an error message. */
14605 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14607 cp_parser_error (parser
, "expected type-name");
14608 return error_mark_node
;
14611 if (type
&& type
!= error_mark_node
)
14613 /* See if TYPE is an Objective-C type, and if so, parse and
14614 accept any protocol references following it. Do this before
14615 the cp_parser_check_for_invalid_template_id() call, because
14616 Objective-C types can be followed by '<...>' which would
14617 enclose protocol names rather than template arguments, and so
14618 everything is fine. */
14619 if (c_dialect_objc () && !parser
->scope
14620 && (objc_is_id (type
) || objc_is_class_name (type
)))
14622 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14623 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
14625 /* Clobber the "unqualified" type previously entered into
14626 DECL_SPECS with the new, improved protocol-qualified version. */
14628 decl_specs
->type
= qual_type
;
14633 /* There is no valid C++ program where a non-template type is
14634 followed by a "<". That usually indicates that the user
14635 thought that the type was a template. */
14636 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
14644 /* Parse a type-name.
14650 simple-template-id [in c++0x]
14658 Returns a TYPE_DECL for the type. */
14661 cp_parser_type_name (cp_parser
* parser
)
14665 /* We can't know yet whether it is a class-name or not. */
14666 cp_parser_parse_tentatively (parser
);
14667 /* Try a class-name. */
14668 type_decl
= cp_parser_class_name (parser
,
14669 /*typename_keyword_p=*/false,
14670 /*template_keyword_p=*/false,
14672 /*check_dependency_p=*/true,
14673 /*class_head_p=*/false,
14674 /*is_declaration=*/false);
14675 /* If it's not a class-name, keep looking. */
14676 if (!cp_parser_parse_definitely (parser
))
14678 if (cxx_dialect
< cxx11
)
14679 /* It must be a typedef-name or an enum-name. */
14680 return cp_parser_nonclass_name (parser
);
14682 cp_parser_parse_tentatively (parser
);
14683 /* It is either a simple-template-id representing an
14684 instantiation of an alias template... */
14685 type_decl
= cp_parser_template_id (parser
,
14686 /*template_keyword_p=*/false,
14687 /*check_dependency_p=*/false,
14689 /*is_declaration=*/false);
14690 /* Note that this must be an instantiation of an alias template
14691 because [temp.names]/6 says:
14693 A template-id that names an alias template specialization
14696 Whereas [temp.names]/7 says:
14698 A simple-template-id that names a class template
14699 specialization is a class-name. */
14700 if (type_decl
!= NULL_TREE
14701 && TREE_CODE (type_decl
) == TYPE_DECL
14702 && TYPE_DECL_ALIAS_P (type_decl
))
14703 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
14705 cp_parser_simulate_error (parser
);
14707 if (!cp_parser_parse_definitely (parser
))
14708 /* ... Or a typedef-name or an enum-name. */
14709 return cp_parser_nonclass_name (parser
);
14715 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14723 Returns a TYPE_DECL for the type. */
14726 cp_parser_nonclass_name (cp_parser
* parser
)
14731 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14732 identifier
= cp_parser_identifier (parser
);
14733 if (identifier
== error_mark_node
)
14734 return error_mark_node
;
14736 /* Look up the type-name. */
14737 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
14739 if (TREE_CODE (type_decl
) == USING_DECL
)
14741 if (!DECL_DEPENDENT_P (type_decl
))
14742 type_decl
= strip_using_decl (type_decl
);
14743 else if (USING_DECL_TYPENAME_P (type_decl
))
14745 /* We have found a type introduced by a using
14746 declaration at class scope that refers to a dependent
14749 using typename :: [opt] nested-name-specifier unqualified-id ;
14751 type_decl
= make_typename_type (TREE_TYPE (type_decl
),
14752 DECL_NAME (type_decl
),
14753 typename_type
, tf_error
);
14754 if (type_decl
!= error_mark_node
)
14755 type_decl
= TYPE_NAME (type_decl
);
14759 if (TREE_CODE (type_decl
) != TYPE_DECL
14760 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
14762 /* See if this is an Objective-C type. */
14763 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14764 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
14766 type_decl
= TYPE_NAME (type
);
14769 /* Issue an error if we did not find a type-name. */
14770 if (TREE_CODE (type_decl
) != TYPE_DECL
14771 /* In Objective-C, we have the complication that class names are
14772 normally type names and start declarations (eg, the
14773 "NSObject" in "NSObject *object;"), but can be used in an
14774 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14775 is an expression. So, a classname followed by a dot is not a
14776 valid type-name. */
14777 || (objc_is_class_name (TREE_TYPE (type_decl
))
14778 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
14780 if (!cp_parser_simulate_error (parser
))
14781 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
14782 NLE_TYPE
, token
->location
);
14783 return error_mark_node
;
14785 /* Remember that the name was used in the definition of the
14786 current class so that we can check later to see if the
14787 meaning would have been different after the class was
14788 entirely defined. */
14789 else if (type_decl
!= error_mark_node
14791 maybe_note_name_used_in_class (identifier
, type_decl
);
14796 /* Parse an elaborated-type-specifier. Note that the grammar given
14797 here incorporates the resolution to DR68.
14799 elaborated-type-specifier:
14800 class-key :: [opt] nested-name-specifier [opt] identifier
14801 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14802 enum-key :: [opt] nested-name-specifier [opt] identifier
14803 typename :: [opt] nested-name-specifier identifier
14804 typename :: [opt] nested-name-specifier template [opt]
14809 elaborated-type-specifier:
14810 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14811 class-key attributes :: [opt] nested-name-specifier [opt]
14812 template [opt] template-id
14813 enum attributes :: [opt] nested-name-specifier [opt] identifier
14815 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14816 declared `friend'. If IS_DECLARATION is TRUE, then this
14817 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14818 something is being declared.
14820 Returns the TYPE specified. */
14823 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
14825 bool is_declaration
)
14827 enum tag_types tag_type
;
14829 tree type
= NULL_TREE
;
14830 tree attributes
= NULL_TREE
;
14832 cp_token
*token
= NULL
;
14834 /* See if we're looking at the `enum' keyword. */
14835 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
14837 /* Consume the `enum' token. */
14838 cp_lexer_consume_token (parser
->lexer
);
14839 /* Remember that it's an enumeration type. */
14840 tag_type
= enum_type
;
14841 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14842 enums) is used here. */
14843 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14844 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14846 pedwarn (input_location
, 0, "elaborated-type-specifier "
14847 "for a scoped enum must not use the %<%D%> keyword",
14848 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
14849 /* Consume the `struct' or `class' and parse it anyway. */
14850 cp_lexer_consume_token (parser
->lexer
);
14852 /* Parse the attributes. */
14853 attributes
= cp_parser_attributes_opt (parser
);
14855 /* Or, it might be `typename'. */
14856 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14859 /* Consume the `typename' token. */
14860 cp_lexer_consume_token (parser
->lexer
);
14861 /* Remember that it's a `typename' type. */
14862 tag_type
= typename_type
;
14864 /* Otherwise it must be a class-key. */
14867 tag_type
= cp_parser_class_key (parser
);
14868 if (tag_type
== none_type
)
14869 return error_mark_node
;
14870 /* Parse the attributes. */
14871 attributes
= cp_parser_attributes_opt (parser
);
14874 /* Look for the `::' operator. */
14875 globalscope
= cp_parser_global_scope_opt (parser
,
14876 /*current_scope_valid_p=*/false);
14877 /* Look for the nested-name-specifier. */
14878 if (tag_type
== typename_type
&& !globalscope
)
14880 if (!cp_parser_nested_name_specifier (parser
,
14881 /*typename_keyword_p=*/true,
14882 /*check_dependency_p=*/true,
14885 return error_mark_node
;
14888 /* Even though `typename' is not present, the proposed resolution
14889 to Core Issue 180 says that in `class A<T>::B', `B' should be
14890 considered a type-name, even if `A<T>' is dependent. */
14891 cp_parser_nested_name_specifier_opt (parser
,
14892 /*typename_keyword_p=*/true,
14893 /*check_dependency_p=*/true,
14896 /* For everything but enumeration types, consider a template-id.
14897 For an enumeration type, consider only a plain identifier. */
14898 if (tag_type
!= enum_type
)
14900 bool template_p
= false;
14903 /* Allow the `template' keyword. */
14904 template_p
= cp_parser_optional_template_keyword (parser
);
14905 /* If we didn't see `template', we don't know if there's a
14906 template-id or not. */
14908 cp_parser_parse_tentatively (parser
);
14909 /* Parse the template-id. */
14910 token
= cp_lexer_peek_token (parser
->lexer
);
14911 decl
= cp_parser_template_id (parser
, template_p
,
14912 /*check_dependency_p=*/true,
14915 /* If we didn't find a template-id, look for an ordinary
14917 if (!template_p
&& !cp_parser_parse_definitely (parser
))
14919 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14920 in effect, then we must assume that, upon instantiation, the
14921 template will correspond to a class. */
14922 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
14923 && tag_type
== typename_type
)
14924 type
= make_typename_type (parser
->scope
, decl
,
14926 /*complain=*/tf_error
);
14927 /* If the `typename' keyword is in effect and DECL is not a type
14928 decl, then type is non existent. */
14929 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
14931 else if (TREE_CODE (decl
) == TYPE_DECL
)
14932 type
= check_elaborated_type_specifier (tag_type
, decl
,
14933 /*allow_template_p=*/true);
14934 else if (decl
== error_mark_node
)
14935 type
= error_mark_node
;
14940 token
= cp_lexer_peek_token (parser
->lexer
);
14941 identifier
= cp_parser_identifier (parser
);
14943 if (identifier
== error_mark_node
)
14945 parser
->scope
= NULL_TREE
;
14946 return error_mark_node
;
14949 /* For a `typename', we needn't call xref_tag. */
14950 if (tag_type
== typename_type
14951 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
14952 return cp_parser_make_typename_type (parser
, parser
->scope
,
14955 /* Look up a qualified name in the usual way. */
14959 tree ambiguous_decls
;
14961 decl
= cp_parser_lookup_name (parser
, identifier
,
14963 /*is_template=*/false,
14964 /*is_namespace=*/false,
14965 /*check_dependency=*/true,
14969 /* If the lookup was ambiguous, an error will already have been
14971 if (ambiguous_decls
)
14972 return error_mark_node
;
14974 /* If we are parsing friend declaration, DECL may be a
14975 TEMPLATE_DECL tree node here. However, we need to check
14976 whether this TEMPLATE_DECL results in valid code. Consider
14977 the following example:
14980 template <class T> class C {};
14983 template <class T> friend class N::C; // #1, valid code
14985 template <class T> class Y {
14986 friend class N::C; // #2, invalid code
14989 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14990 name lookup of `N::C'. We see that friend declaration must
14991 be template for the code to be valid. Note that
14992 processing_template_decl does not work here since it is
14993 always 1 for the above two cases. */
14995 decl
= (cp_parser_maybe_treat_template_as_class
14996 (decl
, /*tag_name_p=*/is_friend
14997 && parser
->num_template_parameter_lists
));
14999 if (TREE_CODE (decl
) != TYPE_DECL
)
15001 cp_parser_diagnose_invalid_type_name (parser
,
15005 return error_mark_node
;
15008 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15010 bool allow_template
= (parser
->num_template_parameter_lists
15011 || DECL_SELF_REFERENCE_P (decl
));
15012 type
= check_elaborated_type_specifier (tag_type
, decl
,
15015 if (type
== error_mark_node
)
15016 return error_mark_node
;
15019 /* Forward declarations of nested types, such as
15024 are invalid unless all components preceding the final '::'
15025 are complete. If all enclosing types are complete, these
15026 declarations become merely pointless.
15028 Invalid forward declarations of nested types are errors
15029 caught elsewhere in parsing. Those that are pointless arrive
15032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15033 && !is_friend
&& !processing_explicit_instantiation
)
15034 warning (0, "declaration %qD does not declare anything", decl
);
15036 type
= TREE_TYPE (decl
);
15040 /* An elaborated-type-specifier sometimes introduces a new type and
15041 sometimes names an existing type. Normally, the rule is that it
15042 introduces a new type only if there is not an existing type of
15043 the same name already in scope. For example, given:
15046 void f() { struct S s; }
15048 the `struct S' in the body of `f' is the same `struct S' as in
15049 the global scope; the existing definition is used. However, if
15050 there were no global declaration, this would introduce a new
15051 local class named `S'.
15053 An exception to this rule applies to the following code:
15055 namespace N { struct S; }
15057 Here, the elaborated-type-specifier names a new type
15058 unconditionally; even if there is already an `S' in the
15059 containing scope this declaration names a new type.
15060 This exception only applies if the elaborated-type-specifier
15061 forms the complete declaration:
15065 A declaration consisting solely of `class-key identifier ;' is
15066 either a redeclaration of the name in the current scope or a
15067 forward declaration of the identifier as a class name. It
15068 introduces the name into the current scope.
15070 We are in this situation precisely when the next token is a `;'.
15072 An exception to the exception is that a `friend' declaration does
15073 *not* name a new type; i.e., given:
15075 struct S { friend struct T; };
15077 `T' is not a new type in the scope of `S'.
15079 Also, `new struct S' or `sizeof (struct S)' never results in the
15080 definition of a new type; a new type can only be declared in a
15081 declaration context. */
15087 /* Friends have special name lookup rules. */
15088 ts
= ts_within_enclosing_non_class
;
15089 else if (is_declaration
15090 && cp_lexer_next_token_is (parser
->lexer
,
15092 /* This is a `class-key identifier ;' */
15098 (parser
->num_template_parameter_lists
15099 && (cp_parser_next_token_starts_class_definition_p (parser
)
15100 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15101 /* An unqualified name was used to reference this type, so
15102 there were no qualifying templates. */
15103 if (!cp_parser_check_template_parameters (parser
,
15104 /*num_templates=*/0,
15106 /*declarator=*/NULL
))
15107 return error_mark_node
;
15108 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15112 if (type
== error_mark_node
)
15113 return error_mark_node
;
15115 /* Allow attributes on forward declarations of classes. */
15118 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15119 warning (OPT_Wattributes
,
15120 "attributes ignored on uninstantiated type");
15121 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15122 && ! processing_explicit_instantiation
)
15123 warning (OPT_Wattributes
,
15124 "attributes ignored on template instantiation");
15125 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15126 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15128 warning (OPT_Wattributes
,
15129 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15132 if (tag_type
!= enum_type
)
15134 /* Indicate whether this class was declared as a `class' or as a
15136 if (TREE_CODE (type
) == RECORD_TYPE
)
15137 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15138 cp_parser_check_class_key (tag_type
, type
);
15141 /* A "<" cannot follow an elaborated type specifier. If that
15142 happens, the user was probably trying to form a template-id. */
15143 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15149 /* Parse an enum-specifier.
15152 enum-head { enumerator-list [opt] }
15153 enum-head { enumerator-list , } [C++0x]
15156 enum-key identifier [opt] enum-base [opt]
15157 enum-key nested-name-specifier identifier enum-base [opt]
15162 enum struct [C++0x]
15165 : type-specifier-seq
15167 opaque-enum-specifier:
15168 enum-key identifier enum-base [opt] ;
15171 enum-key attributes[opt] identifier [opt] enum-base [opt]
15172 { enumerator-list [opt] }attributes[opt]
15173 enum-key attributes[opt] identifier [opt] enum-base [opt]
15174 { enumerator-list, }attributes[opt] [C++0x]
15176 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15177 if the token stream isn't an enum-specifier after all. */
15180 cp_parser_enum_specifier (cp_parser
* parser
)
15183 tree type
= NULL_TREE
;
15185 tree nested_name_specifier
= NULL_TREE
;
15187 bool scoped_enum_p
= false;
15188 bool has_underlying_type
= false;
15189 bool nested_being_defined
= false;
15190 bool new_value_list
= false;
15191 bool is_new_type
= false;
15192 bool is_anonymous
= false;
15193 tree underlying_type
= NULL_TREE
;
15194 cp_token
*type_start_token
= NULL
;
15195 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15197 parser
->colon_corrects_to_scope_p
= false;
15199 /* Parse tentatively so that we can back up if we don't find a
15201 cp_parser_parse_tentatively (parser
);
15203 /* Caller guarantees that the current token is 'enum', an identifier
15204 possibly follows, and the token after that is an opening brace.
15205 If we don't have an identifier, fabricate an anonymous name for
15206 the enumeration being defined. */
15207 cp_lexer_consume_token (parser
->lexer
);
15209 /* Parse the "class" or "struct", which indicates a scoped
15210 enumeration type in C++0x. */
15211 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15212 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15214 if (cxx_dialect
< cxx11
)
15215 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15217 /* Consume the `struct' or `class' token. */
15218 cp_lexer_consume_token (parser
->lexer
);
15220 scoped_enum_p
= true;
15223 attributes
= cp_parser_attributes_opt (parser
);
15225 /* Clear the qualification. */
15226 parser
->scope
= NULL_TREE
;
15227 parser
->qualifying_scope
= NULL_TREE
;
15228 parser
->object_scope
= NULL_TREE
;
15230 /* Figure out in what scope the declaration is being placed. */
15231 prev_scope
= current_scope ();
15233 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15235 push_deferring_access_checks (dk_no_check
);
15236 nested_name_specifier
15237 = cp_parser_nested_name_specifier_opt (parser
,
15238 /*typename_keyword_p=*/true,
15239 /*check_dependency_p=*/false,
15241 /*is_declaration=*/false);
15243 if (nested_name_specifier
)
15247 identifier
= cp_parser_identifier (parser
);
15248 name
= cp_parser_lookup_name (parser
, identifier
,
15250 /*is_template=*/false,
15251 /*is_namespace=*/false,
15252 /*check_dependency=*/true,
15253 /*ambiguous_decls=*/NULL
,
15255 if (name
&& name
!= error_mark_node
)
15257 type
= TREE_TYPE (name
);
15258 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15260 /* Are template enums allowed in ISO? */
15261 if (template_parm_scope_p ())
15262 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15263 "%qD is an enumeration template", name
);
15264 /* ignore a typename reference, for it will be solved by name
15269 else if (nested_name_specifier
== error_mark_node
)
15270 /* We already issued an error. */;
15272 error_at (type_start_token
->location
,
15273 "%qD is not an enumerator-name", identifier
);
15277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15278 identifier
= cp_parser_identifier (parser
);
15281 identifier
= make_anon_name ();
15282 is_anonymous
= true;
15284 error_at (type_start_token
->location
,
15285 "anonymous scoped enum is not allowed");
15288 pop_deferring_access_checks ();
15290 /* Check for the `:' that denotes a specified underlying type in C++0x.
15291 Note that a ':' could also indicate a bitfield width, however. */
15292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15294 cp_decl_specifier_seq type_specifiers
;
15296 /* Consume the `:'. */
15297 cp_lexer_consume_token (parser
->lexer
);
15299 /* Parse the type-specifier-seq. */
15300 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15301 /*is_trailing_return=*/false,
15304 /* At this point this is surely not elaborated type specifier. */
15305 if (!cp_parser_parse_definitely (parser
))
15308 if (cxx_dialect
< cxx11
)
15309 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15311 has_underlying_type
= true;
15313 /* If that didn't work, stop. */
15314 if (type_specifiers
.type
!= error_mark_node
)
15316 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15317 /*initialized=*/0, NULL
);
15318 if (underlying_type
== error_mark_node
)
15319 underlying_type
= NULL_TREE
;
15323 /* Look for the `{' but don't consume it yet. */
15324 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15326 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15328 cp_parser_error (parser
, "expected %<{%>");
15329 if (has_underlying_type
)
15335 /* An opaque-enum-specifier must have a ';' here. */
15336 if ((scoped_enum_p
|| underlying_type
)
15337 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15339 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15340 if (has_underlying_type
)
15348 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15351 if (nested_name_specifier
)
15353 if (CLASS_TYPE_P (nested_name_specifier
))
15355 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15356 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15357 push_scope (nested_name_specifier
);
15359 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15361 push_nested_namespace (nested_name_specifier
);
15365 /* Issue an error message if type-definitions are forbidden here. */
15366 if (!cp_parser_check_type_definition (parser
))
15367 type
= error_mark_node
;
15369 /* Create the new type. We do this before consuming the opening
15370 brace so the enum will be recorded as being on the line of its
15371 tag (or the 'enum' keyword, if there is no tag). */
15372 type
= start_enum (identifier
, type
, underlying_type
,
15373 scoped_enum_p
, &is_new_type
);
15375 /* If the next token is not '{' it is an opaque-enum-specifier or an
15376 elaborated-type-specifier. */
15377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15379 timevar_push (TV_PARSE_ENUM
);
15380 if (nested_name_specifier
15381 && nested_name_specifier
!= error_mark_node
)
15383 /* The following catches invalid code such as:
15384 enum class S<int>::E { A, B, C }; */
15385 if (!processing_specialization
15386 && CLASS_TYPE_P (nested_name_specifier
)
15387 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15388 error_at (type_start_token
->location
, "cannot add an enumerator "
15389 "list to a template instantiation");
15391 /* If that scope does not contain the scope in which the
15392 class was originally declared, the program is invalid. */
15393 if (prev_scope
&& !is_ancestor (prev_scope
, nested_name_specifier
))
15395 if (at_namespace_scope_p ())
15396 error_at (type_start_token
->location
,
15397 "declaration of %qD in namespace %qD which does not "
15399 type
, prev_scope
, nested_name_specifier
);
15401 error_at (type_start_token
->location
,
15402 "declaration of %qD in %qD which does not enclose %qD",
15403 type
, prev_scope
, nested_name_specifier
);
15404 type
= error_mark_node
;
15409 begin_scope (sk_scoped_enum
, type
);
15411 /* Consume the opening brace. */
15412 cp_lexer_consume_token (parser
->lexer
);
15414 if (type
== error_mark_node
)
15415 ; /* Nothing to add */
15416 else if (OPAQUE_ENUM_P (type
)
15417 || (cxx_dialect
> cxx98
&& processing_specialization
))
15419 new_value_list
= true;
15420 SET_OPAQUE_ENUM_P (type
, false);
15421 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15425 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
15426 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15427 "previous definition here");
15428 type
= error_mark_node
;
15431 if (type
== error_mark_node
)
15432 cp_parser_skip_to_end_of_block_or_statement (parser
);
15433 /* If the next token is not '}', then there are some enumerators. */
15434 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15436 if (is_anonymous
&& !scoped_enum_p
)
15437 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15438 "ISO C++ forbids empty anonymous enum");
15441 cp_parser_enumerator_list (parser
, type
);
15443 /* Consume the final '}'. */
15444 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15448 timevar_pop (TV_PARSE_ENUM
);
15452 /* If a ';' follows, then it is an opaque-enum-specifier
15453 and additional restrictions apply. */
15454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15457 error_at (type_start_token
->location
,
15458 "opaque-enum-specifier without name");
15459 else if (nested_name_specifier
)
15460 error_at (type_start_token
->location
,
15461 "opaque-enum-specifier must use a simple identifier");
15465 /* Look for trailing attributes to apply to this enumeration, and
15466 apply them if appropriate. */
15467 if (cp_parser_allow_gnu_extensions_p (parser
))
15469 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15470 trailing_attr
= chainon (trailing_attr
, attributes
);
15471 cplus_decl_attributes (&type
,
15473 (int) ATTR_FLAG_TYPE_IN_PLACE
);
15476 /* Finish up the enumeration. */
15477 if (type
!= error_mark_node
)
15479 if (new_value_list
)
15480 finish_enum_value_list (type
);
15482 finish_enum (type
);
15485 if (nested_name_specifier
)
15487 if (CLASS_TYPE_P (nested_name_specifier
))
15489 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
15490 pop_scope (nested_name_specifier
);
15492 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15494 pop_nested_namespace (nested_name_specifier
);
15498 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
15502 /* Parse an enumerator-list. The enumerators all have the indicated
15506 enumerator-definition
15507 enumerator-list , enumerator-definition */
15510 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
15514 /* Parse an enumerator-definition. */
15515 cp_parser_enumerator_definition (parser
, type
);
15517 /* If the next token is not a ',', we've reached the end of
15519 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15521 /* Otherwise, consume the `,' and keep going. */
15522 cp_lexer_consume_token (parser
->lexer
);
15523 /* If the next token is a `}', there is a trailing comma. */
15524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15526 if (cxx_dialect
< cxx11
&& !in_system_header
)
15527 pedwarn (input_location
, OPT_Wpedantic
,
15528 "comma at end of enumerator list");
15534 /* Parse an enumerator-definition. The enumerator has the indicated
15537 enumerator-definition:
15539 enumerator = constant-expression
15545 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
15551 /* Save the input location because we are interested in the location
15552 of the identifier and not the location of the explicit value. */
15553 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15555 /* Look for the identifier. */
15556 identifier
= cp_parser_identifier (parser
);
15557 if (identifier
== error_mark_node
)
15560 /* If the next token is an '=', then there is an explicit value. */
15561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15563 /* Consume the `=' token. */
15564 cp_lexer_consume_token (parser
->lexer
);
15565 /* Parse the value. */
15566 value
= cp_parser_constant_expression (parser
,
15567 /*allow_non_constant_p=*/false,
15573 /* If we are processing a template, make sure the initializer of the
15574 enumerator doesn't contain any bare template parameter pack. */
15575 if (check_for_bare_parameter_packs (value
))
15576 value
= error_mark_node
;
15578 /* integral_constant_value will pull out this expression, so make sure
15579 it's folded as appropriate. */
15580 value
= fold_non_dependent_expr (value
);
15582 /* Create the enumerator. */
15583 build_enumerator (identifier
, value
, type
, loc
);
15586 /* Parse a namespace-name.
15589 original-namespace-name
15592 Returns the NAMESPACE_DECL for the namespace. */
15595 cp_parser_namespace_name (cp_parser
* parser
)
15598 tree namespace_decl
;
15600 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15602 /* Get the name of the namespace. */
15603 identifier
= cp_parser_identifier (parser
);
15604 if (identifier
== error_mark_node
)
15605 return error_mark_node
;
15607 /* Look up the identifier in the currently active scope. Look only
15608 for namespaces, due to:
15610 [basic.lookup.udir]
15612 When looking up a namespace-name in a using-directive or alias
15613 definition, only namespace names are considered.
15617 [basic.lookup.qual]
15619 During the lookup of a name preceding the :: scope resolution
15620 operator, object, function, and enumerator names are ignored.
15622 (Note that cp_parser_qualifying_entity only calls this
15623 function if the token after the name is the scope resolution
15625 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
15627 /*is_template=*/false,
15628 /*is_namespace=*/true,
15629 /*check_dependency=*/true,
15630 /*ambiguous_decls=*/NULL
,
15632 /* If it's not a namespace, issue an error. */
15633 if (namespace_decl
== error_mark_node
15634 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
15636 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15637 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
15638 cp_parser_error (parser
, "expected namespace-name");
15639 namespace_decl
= error_mark_node
;
15642 return namespace_decl
;
15645 /* Parse a namespace-definition.
15647 namespace-definition:
15648 named-namespace-definition
15649 unnamed-namespace-definition
15651 named-namespace-definition:
15652 original-namespace-definition
15653 extension-namespace-definition
15655 original-namespace-definition:
15656 namespace identifier { namespace-body }
15658 extension-namespace-definition:
15659 namespace original-namespace-name { namespace-body }
15661 unnamed-namespace-definition:
15662 namespace { namespace-body } */
15665 cp_parser_namespace_definition (cp_parser
* parser
)
15667 tree identifier
, attribs
;
15668 bool has_visibility
;
15671 cp_ensure_no_omp_declare_simd (parser
);
15672 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
15674 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
15676 cp_lexer_consume_token (parser
->lexer
);
15681 /* Look for the `namespace' keyword. */
15682 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15684 /* Get the name of the namespace. We do not attempt to distinguish
15685 between an original-namespace-definition and an
15686 extension-namespace-definition at this point. The semantic
15687 analysis routines are responsible for that. */
15688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15689 identifier
= cp_parser_identifier (parser
);
15691 identifier
= NULL_TREE
;
15693 /* Parse any specified attributes. */
15694 attribs
= cp_parser_attributes_opt (parser
);
15696 /* Look for the `{' to start the namespace. */
15697 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
15698 /* Start the namespace. */
15699 push_namespace (identifier
);
15701 /* "inline namespace" is equivalent to a stub namespace definition
15702 followed by a strong using directive. */
15705 tree name_space
= current_namespace
;
15706 /* Set up namespace association. */
15707 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
15708 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
15709 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
15710 /* Import the contents of the inline namespace. */
15712 do_using_directive (name_space
);
15713 push_namespace (identifier
);
15716 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
15718 /* Parse the body of the namespace. */
15719 cp_parser_namespace_body (parser
);
15721 if (has_visibility
)
15722 pop_visibility (1);
15724 /* Finish the namespace. */
15726 /* Look for the final `}'. */
15727 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15730 /* Parse a namespace-body.
15733 declaration-seq [opt] */
15736 cp_parser_namespace_body (cp_parser
* parser
)
15738 cp_parser_declaration_seq_opt (parser
);
15741 /* Parse a namespace-alias-definition.
15743 namespace-alias-definition:
15744 namespace identifier = qualified-namespace-specifier ; */
15747 cp_parser_namespace_alias_definition (cp_parser
* parser
)
15750 tree namespace_specifier
;
15752 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15754 /* Look for the `namespace' keyword. */
15755 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15756 /* Look for the identifier. */
15757 identifier
= cp_parser_identifier (parser
);
15758 if (identifier
== error_mark_node
)
15760 /* Look for the `=' token. */
15761 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
15762 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15764 error_at (token
->location
, "%<namespace%> definition is not allowed here");
15765 /* Skip the definition. */
15766 cp_lexer_consume_token (parser
->lexer
);
15767 if (cp_parser_skip_to_closing_brace (parser
))
15768 cp_lexer_consume_token (parser
->lexer
);
15771 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15772 /* Look for the qualified-namespace-specifier. */
15773 namespace_specifier
15774 = cp_parser_qualified_namespace_specifier (parser
);
15775 /* Look for the `;' token. */
15776 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15778 /* Register the alias in the symbol table. */
15779 do_namespace_alias (identifier
, namespace_specifier
);
15782 /* Parse a qualified-namespace-specifier.
15784 qualified-namespace-specifier:
15785 :: [opt] nested-name-specifier [opt] namespace-name
15787 Returns a NAMESPACE_DECL corresponding to the specified
15791 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
15793 /* Look for the optional `::'. */
15794 cp_parser_global_scope_opt (parser
,
15795 /*current_scope_valid_p=*/false);
15797 /* Look for the optional nested-name-specifier. */
15798 cp_parser_nested_name_specifier_opt (parser
,
15799 /*typename_keyword_p=*/false,
15800 /*check_dependency_p=*/true,
15802 /*is_declaration=*/true);
15804 return cp_parser_namespace_name (parser
);
15807 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15808 access declaration.
15811 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15812 using :: unqualified-id ;
15814 access-declaration:
15820 cp_parser_using_declaration (cp_parser
* parser
,
15821 bool access_declaration_p
)
15824 bool typename_p
= false;
15825 bool global_scope_p
;
15829 int oldcount
= errorcount
;
15830 cp_token
*diag_token
= NULL
;
15832 if (access_declaration_p
)
15834 diag_token
= cp_lexer_peek_token (parser
->lexer
);
15835 cp_parser_parse_tentatively (parser
);
15839 /* Look for the `using' keyword. */
15840 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15842 /* Peek at the next token. */
15843 token
= cp_lexer_peek_token (parser
->lexer
);
15844 /* See if it's `typename'. */
15845 if (token
->keyword
== RID_TYPENAME
)
15847 /* Remember that we've seen it. */
15849 /* Consume the `typename' token. */
15850 cp_lexer_consume_token (parser
->lexer
);
15854 /* Look for the optional global scope qualification. */
15856 = (cp_parser_global_scope_opt (parser
,
15857 /*current_scope_valid_p=*/false)
15860 /* If we saw `typename', or didn't see `::', then there must be a
15861 nested-name-specifier present. */
15862 if (typename_p
|| !global_scope_p
)
15863 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
15864 /*check_dependency_p=*/true,
15866 /*is_declaration=*/true);
15867 /* Otherwise, we could be in either of the two productions. In that
15868 case, treat the nested-name-specifier as optional. */
15870 qscope
= cp_parser_nested_name_specifier_opt (parser
,
15871 /*typename_keyword_p=*/false,
15872 /*check_dependency_p=*/true,
15874 /*is_declaration=*/true);
15876 qscope
= global_namespace
;
15878 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
15879 /* Something has already gone wrong; there's no need to parse
15880 further. Since an error has occurred, the return value of
15881 cp_parser_parse_definitely will be false, as required. */
15882 return cp_parser_parse_definitely (parser
);
15884 token
= cp_lexer_peek_token (parser
->lexer
);
15885 /* Parse the unqualified-id. */
15886 identifier
= cp_parser_unqualified_id (parser
,
15887 /*template_keyword_p=*/false,
15888 /*check_dependency_p=*/true,
15889 /*declarator_p=*/true,
15890 /*optional_p=*/false);
15892 if (access_declaration_p
)
15894 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15895 cp_parser_simulate_error (parser
);
15896 if (!cp_parser_parse_definitely (parser
))
15900 /* The function we call to handle a using-declaration is different
15901 depending on what scope we are in. */
15902 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
15904 else if (!identifier_p (identifier
)
15905 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
15906 /* [namespace.udecl]
15908 A using declaration shall not name a template-id. */
15909 error_at (token
->location
,
15910 "a template-id may not appear in a using-declaration");
15913 if (at_class_scope_p ())
15915 /* Create the USING_DECL. */
15916 decl
= do_class_using_decl (parser
->scope
, identifier
);
15918 if (decl
&& typename_p
)
15919 USING_DECL_TYPENAME_P (decl
) = 1;
15921 if (check_for_bare_parameter_packs (decl
))
15924 /* Add it to the list of members in this class. */
15925 finish_member_declaration (decl
);
15929 decl
= cp_parser_lookup_name_simple (parser
,
15932 if (decl
== error_mark_node
)
15933 cp_parser_name_lookup_error (parser
, identifier
,
15936 else if (check_for_bare_parameter_packs (decl
))
15938 else if (!at_namespace_scope_p ())
15939 do_local_using_decl (decl
, qscope
, identifier
);
15941 do_toplevel_using_decl (decl
, qscope
, identifier
);
15945 /* Look for the final `;'. */
15946 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15948 if (access_declaration_p
&& errorcount
== oldcount
)
15949 warning_at (diag_token
->location
, OPT_Wdeprecated
,
15950 "access declarations are deprecated "
15951 "in favour of using-declarations; "
15952 "suggestion: add the %<using%> keyword");
15957 /* Parse an alias-declaration.
15960 using identifier attribute-specifier-seq [opt] = type-id */
15963 cp_parser_alias_declaration (cp_parser
* parser
)
15965 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
15966 location_t id_location
;
15967 cp_declarator
*declarator
;
15968 cp_decl_specifier_seq decl_specs
;
15970 const char *saved_message
= NULL
;
15972 /* Look for the `using' keyword. */
15973 cp_token
*using_token
15974 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15975 if (using_token
== NULL
)
15976 return error_mark_node
;
15978 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
15979 id
= cp_parser_identifier (parser
);
15980 if (id
== error_mark_node
)
15981 return error_mark_node
;
15983 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
15984 attributes
= cp_parser_attributes_opt (parser
);
15985 if (attributes
== error_mark_node
)
15986 return error_mark_node
;
15988 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15990 if (cp_parser_error_occurred (parser
))
15991 return error_mark_node
;
15993 cp_parser_commit_to_tentative_parse (parser
);
15995 /* Now we are going to parse the type-id of the declaration. */
16000 "A type-specifier-seq shall not define a class or enumeration
16001 unless it appears in the type-id of an alias-declaration (7.1.3) that
16002 is not the declaration of a template-declaration."
16004 In other words, if we currently are in an alias template, the
16005 type-id should not define a type.
16007 So let's set parser->type_definition_forbidden_message in that
16008 case; cp_parser_check_type_definition (called by
16009 cp_parser_class_specifier) will then emit an error if a type is
16010 defined in the type-id. */
16011 if (parser
->num_template_parameter_lists
)
16013 saved_message
= parser
->type_definition_forbidden_message
;
16014 parser
->type_definition_forbidden_message
=
16015 G_("types may not be defined in alias template declarations");
16018 type
= cp_parser_type_id (parser
);
16020 /* Restore the error message if need be. */
16021 if (parser
->num_template_parameter_lists
)
16022 parser
->type_definition_forbidden_message
= saved_message
;
16024 if (type
== error_mark_node
)
16026 cp_parser_skip_to_end_of_block_or_statement (parser
);
16027 return error_mark_node
;
16030 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16032 if (cp_parser_error_occurred (parser
))
16034 cp_parser_skip_to_end_of_block_or_statement (parser
);
16035 return error_mark_node
;
16038 /* A typedef-name can also be introduced by an alias-declaration. The
16039 identifier following the using keyword becomes a typedef-name. It has
16040 the same semantics as if it were introduced by the typedef
16041 specifier. In particular, it does not define a new type and it shall
16042 not appear in the type-id. */
16044 clear_decl_specs (&decl_specs
);
16045 decl_specs
.type
= type
;
16046 if (attributes
!= NULL_TREE
)
16048 decl_specs
.attributes
= attributes
;
16049 set_and_check_decl_spec_loc (&decl_specs
,
16053 set_and_check_decl_spec_loc (&decl_specs
,
16056 set_and_check_decl_spec_loc (&decl_specs
,
16060 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16061 declarator
->id_loc
= id_location
;
16063 member_p
= at_class_scope_p ();
16065 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16066 NULL_TREE
, attributes
);
16068 decl
= start_decl (declarator
, &decl_specs
, 0,
16069 attributes
, NULL_TREE
, &pushed_scope
);
16070 if (decl
== error_mark_node
)
16073 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16076 pop_scope (pushed_scope
);
16078 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16079 added into the symbol table; otherwise, return the TYPE_DECL. */
16080 if (DECL_LANG_SPECIFIC (decl
)
16081 && DECL_TEMPLATE_INFO (decl
)
16082 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16084 decl
= DECL_TI_TEMPLATE (decl
);
16086 check_member_template (decl
);
16092 /* Parse a using-directive.
16095 using namespace :: [opt] nested-name-specifier [opt]
16096 namespace-name ; */
16099 cp_parser_using_directive (cp_parser
* parser
)
16101 tree namespace_decl
;
16104 /* Look for the `using' keyword. */
16105 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16106 /* And the `namespace' keyword. */
16107 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16108 /* Look for the optional `::' operator. */
16109 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16110 /* And the optional nested-name-specifier. */
16111 cp_parser_nested_name_specifier_opt (parser
,
16112 /*typename_keyword_p=*/false,
16113 /*check_dependency_p=*/true,
16115 /*is_declaration=*/true);
16116 /* Get the namespace being used. */
16117 namespace_decl
= cp_parser_namespace_name (parser
);
16118 /* And any specified attributes. */
16119 attribs
= cp_parser_attributes_opt (parser
);
16120 /* Update the symbol table. */
16121 parse_using_directive (namespace_decl
, attribs
);
16122 /* Look for the final `;'. */
16123 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16126 /* Parse an asm-definition.
16129 asm ( string-literal ) ;
16134 asm volatile [opt] ( string-literal ) ;
16135 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16136 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16137 : asm-operand-list [opt] ) ;
16138 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16139 : asm-operand-list [opt]
16140 : asm-clobber-list [opt] ) ;
16141 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16142 : asm-clobber-list [opt]
16143 : asm-goto-list ) ; */
16146 cp_parser_asm_definition (cp_parser
* parser
)
16149 tree outputs
= NULL_TREE
;
16150 tree inputs
= NULL_TREE
;
16151 tree clobbers
= NULL_TREE
;
16152 tree labels
= NULL_TREE
;
16154 bool volatile_p
= false;
16155 bool extended_p
= false;
16156 bool invalid_inputs_p
= false;
16157 bool invalid_outputs_p
= false;
16158 bool goto_p
= false;
16159 required_token missing
= RT_NONE
;
16161 /* Look for the `asm' keyword. */
16162 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16163 /* See if the next token is `volatile'. */
16164 if (cp_parser_allow_gnu_extensions_p (parser
)
16165 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16167 /* Remember that we saw the `volatile' keyword. */
16169 /* Consume the token. */
16170 cp_lexer_consume_token (parser
->lexer
);
16172 if (cp_parser_allow_gnu_extensions_p (parser
)
16173 && parser
->in_function_body
16174 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16176 /* Remember that we saw the `goto' keyword. */
16178 /* Consume the token. */
16179 cp_lexer_consume_token (parser
->lexer
);
16181 /* Look for the opening `('. */
16182 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16184 /* Look for the string. */
16185 string
= cp_parser_string_literal (parser
, false, false);
16186 if (string
== error_mark_node
)
16188 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16189 /*consume_paren=*/true);
16193 /* If we're allowing GNU extensions, check for the extended assembly
16194 syntax. Unfortunately, the `:' tokens need not be separated by
16195 a space in C, and so, for compatibility, we tolerate that here
16196 too. Doing that means that we have to treat the `::' operator as
16198 if (cp_parser_allow_gnu_extensions_p (parser
)
16199 && parser
->in_function_body
16200 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16201 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16203 bool inputs_p
= false;
16204 bool clobbers_p
= false;
16205 bool labels_p
= false;
16207 /* The extended syntax was used. */
16210 /* Look for outputs. */
16211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16213 /* Consume the `:'. */
16214 cp_lexer_consume_token (parser
->lexer
);
16215 /* Parse the output-operands. */
16216 if (cp_lexer_next_token_is_not (parser
->lexer
,
16218 && cp_lexer_next_token_is_not (parser
->lexer
,
16220 && cp_lexer_next_token_is_not (parser
->lexer
,
16223 outputs
= cp_parser_asm_operand_list (parser
);
16225 if (outputs
== error_mark_node
)
16226 invalid_outputs_p
= true;
16228 /* If the next token is `::', there are no outputs, and the
16229 next token is the beginning of the inputs. */
16230 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16231 /* The inputs are coming next. */
16234 /* Look for inputs. */
16236 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16238 /* Consume the `:' or `::'. */
16239 cp_lexer_consume_token (parser
->lexer
);
16240 /* Parse the output-operands. */
16241 if (cp_lexer_next_token_is_not (parser
->lexer
,
16243 && cp_lexer_next_token_is_not (parser
->lexer
,
16245 && cp_lexer_next_token_is_not (parser
->lexer
,
16247 inputs
= cp_parser_asm_operand_list (parser
);
16249 if (inputs
== error_mark_node
)
16250 invalid_inputs_p
= true;
16252 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16253 /* The clobbers are coming next. */
16256 /* Look for clobbers. */
16258 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16261 /* Consume the `:' or `::'. */
16262 cp_lexer_consume_token (parser
->lexer
);
16263 /* Parse the clobbers. */
16264 if (cp_lexer_next_token_is_not (parser
->lexer
,
16266 && cp_lexer_next_token_is_not (parser
->lexer
,
16268 clobbers
= cp_parser_asm_clobber_list (parser
);
16271 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16272 /* The labels are coming next. */
16275 /* Look for labels. */
16277 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16280 /* Consume the `:' or `::'. */
16281 cp_lexer_consume_token (parser
->lexer
);
16282 /* Parse the labels. */
16283 labels
= cp_parser_asm_label_list (parser
);
16286 if (goto_p
&& !labels_p
)
16287 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16290 missing
= RT_COLON_SCOPE
;
16292 /* Look for the closing `)'. */
16293 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16294 missing
? missing
: RT_CLOSE_PAREN
))
16295 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16296 /*consume_paren=*/true);
16297 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16299 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16301 /* Create the ASM_EXPR. */
16302 if (parser
->in_function_body
)
16304 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16305 inputs
, clobbers
, labels
);
16306 /* If the extended syntax was not used, mark the ASM_EXPR. */
16309 tree temp
= asm_stmt
;
16310 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16311 temp
= TREE_OPERAND (temp
, 0);
16313 ASM_INPUT_P (temp
) = 1;
16317 add_asm_node (string
);
16321 /* Declarators [gram.dcl.decl] */
16323 /* Parse an init-declarator.
16326 declarator initializer [opt]
16331 declarator asm-specification [opt] attributes [opt] initializer [opt]
16333 function-definition:
16334 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16336 decl-specifier-seq [opt] declarator function-try-block
16340 function-definition:
16341 __extension__ function-definition
16345 function-definition:
16346 decl-specifier-seq [opt] declarator function-transaction-block
16348 The DECL_SPECIFIERS apply to this declarator. Returns a
16349 representation of the entity declared. If MEMBER_P is TRUE, then
16350 this declarator appears in a class scope. The new DECL created by
16351 this declarator is returned.
16353 The CHECKS are access checks that should be performed once we know
16354 what entity is being declared (and, therefore, what classes have
16357 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16358 for a function-definition here as well. If the declarator is a
16359 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16360 be TRUE upon return. By that point, the function-definition will
16361 have been completely parsed.
16363 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16366 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16367 parsed declaration if it is an uninitialized single declarator not followed
16368 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16369 if present, will not be consumed. If returned, this declarator will be
16370 created with SD_INITIALIZED but will not call cp_finish_decl. */
16373 cp_parser_init_declarator (cp_parser
* parser
,
16374 cp_decl_specifier_seq
*decl_specifiers
,
16375 vec
<deferred_access_check
, va_gc
> *checks
,
16376 bool function_definition_allowed_p
,
16378 int declares_class_or_enum
,
16379 bool* function_definition_p
,
16380 tree
* maybe_range_for_decl
)
16382 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16383 *attributes_start_token
= NULL
;
16384 cp_declarator
*declarator
;
16385 tree prefix_attributes
;
16386 tree attributes
= NULL
;
16387 tree asm_specification
;
16389 tree decl
= NULL_TREE
;
16391 int is_initialized
;
16392 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16393 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16395 enum cpp_ttype initialization_kind
;
16396 bool is_direct_init
= false;
16397 bool is_non_constant_init
;
16398 int ctor_dtor_or_conv_p
;
16400 tree pushed_scope
= NULL_TREE
;
16401 bool range_for_decl_p
= false;
16402 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16404 /* Gather the attributes that were provided with the
16405 decl-specifiers. */
16406 prefix_attributes
= decl_specifiers
->attributes
;
16408 /* Assume that this is not the declarator for a function
16410 if (function_definition_p
)
16411 *function_definition_p
= false;
16413 /* Default arguments are only permitted for function parameters. */
16414 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16415 parser
->default_arg_ok_p
= false;
16417 /* Defer access checks while parsing the declarator; we cannot know
16418 what names are accessible until we know what is being
16420 resume_deferring_access_checks ();
16422 /* Parse the declarator. */
16423 token
= cp_lexer_peek_token (parser
->lexer
);
16425 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16426 &ctor_dtor_or_conv_p
,
16427 /*parenthesized_p=*/NULL
,
16429 /* Gather up the deferred checks. */
16430 stop_deferring_access_checks ();
16432 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16434 /* If the DECLARATOR was erroneous, there's no need to go
16436 if (declarator
== cp_error_declarator
)
16437 return error_mark_node
;
16439 /* Check that the number of template-parameter-lists is OK. */
16440 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16442 return error_mark_node
;
16444 if (declares_class_or_enum
& 2)
16445 cp_parser_check_for_definition_in_return_type (declarator
,
16446 decl_specifiers
->type
,
16447 decl_specifiers
->locations
[ds_type_spec
]);
16449 /* Figure out what scope the entity declared by the DECLARATOR is
16450 located in. `grokdeclarator' sometimes changes the scope, so
16451 we compute it now. */
16452 scope
= get_scope_of_declarator (declarator
);
16454 /* Perform any lookups in the declared type which were thought to be
16455 dependent, but are not in the scope of the declarator. */
16456 decl_specifiers
->type
16457 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
16459 /* If we're allowing GNU extensions, look for an
16460 asm-specification. */
16461 if (cp_parser_allow_gnu_extensions_p (parser
))
16463 /* Look for an asm-specification. */
16464 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
16465 asm_specification
= cp_parser_asm_specification_opt (parser
);
16468 asm_specification
= NULL_TREE
;
16470 /* Look for attributes. */
16471 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
16472 attributes
= cp_parser_attributes_opt (parser
);
16474 /* Peek at the next token. */
16475 token
= cp_lexer_peek_token (parser
->lexer
);
16477 if (function_declarator_p (declarator
))
16479 /* Check to see if the token indicates the start of a
16480 function-definition. */
16481 if (cp_parser_token_starts_function_definition_p (token
))
16483 if (!function_definition_allowed_p
)
16485 /* If a function-definition should not appear here, issue an
16487 cp_parser_error (parser
,
16488 "a function-definition is not allowed here");
16489 return error_mark_node
;
16492 location_t func_brace_location
16493 = cp_lexer_peek_token (parser
->lexer
)->location
;
16495 /* Neither attributes nor an asm-specification are allowed
16496 on a function-definition. */
16497 if (asm_specification
)
16498 error_at (asm_spec_start_token
->location
,
16499 "an asm-specification is not allowed "
16500 "on a function-definition");
16502 error_at (attributes_start_token
->location
,
16503 "attributes are not allowed "
16504 "on a function-definition");
16505 /* This is a function-definition. */
16506 *function_definition_p
= true;
16508 /* Parse the function definition. */
16510 decl
= cp_parser_save_member_function_body (parser
,
16513 prefix_attributes
);
16516 (cp_parser_function_definition_from_specifiers_and_declarator
16517 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
16519 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
16521 /* This is where the prologue starts... */
16522 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
16523 = func_brace_location
;
16532 Only in function declarations for constructors, destructors, and
16533 type conversions can the decl-specifier-seq be omitted.
16535 We explicitly postpone this check past the point where we handle
16536 function-definitions because we tolerate function-definitions
16537 that are missing their return types in some modes. */
16538 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
16540 cp_parser_error (parser
,
16541 "expected constructor, destructor, or type conversion");
16542 return error_mark_node
;
16545 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16546 if (token
->type
== CPP_EQ
16547 || token
->type
== CPP_OPEN_PAREN
16548 || token
->type
== CPP_OPEN_BRACE
)
16550 is_initialized
= SD_INITIALIZED
;
16551 initialization_kind
= token
->type
;
16552 if (maybe_range_for_decl
)
16553 *maybe_range_for_decl
= error_mark_node
;
16555 if (token
->type
== CPP_EQ
16556 && function_declarator_p (declarator
))
16558 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16559 if (t2
->keyword
== RID_DEFAULT
)
16560 is_initialized
= SD_DEFAULTED
;
16561 else if (t2
->keyword
== RID_DELETE
)
16562 is_initialized
= SD_DELETED
;
16567 /* If the init-declarator isn't initialized and isn't followed by a
16568 `,' or `;', it's not a valid init-declarator. */
16569 if (token
->type
!= CPP_COMMA
16570 && token
->type
!= CPP_SEMICOLON
)
16572 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
16573 range_for_decl_p
= true;
16576 cp_parser_error (parser
, "expected initializer");
16577 return error_mark_node
;
16580 is_initialized
= SD_UNINITIALIZED
;
16581 initialization_kind
= CPP_EOF
;
16584 /* Because start_decl has side-effects, we should only call it if we
16585 know we're going ahead. By this point, we know that we cannot
16586 possibly be looking at any other construct. */
16587 cp_parser_commit_to_tentative_parse (parser
);
16589 /* If the decl specifiers were bad, issue an error now that we're
16590 sure this was intended to be a declarator. Then continue
16591 declaring the variable(s), as int, to try to cut down on further
16593 if (decl_specifiers
->any_specifiers_p
16594 && decl_specifiers
->type
== error_mark_node
)
16596 cp_parser_error (parser
, "invalid type in declaration");
16597 decl_specifiers
->type
= integer_type_node
;
16600 /* Check to see whether or not this declaration is a friend. */
16601 friend_p
= cp_parser_friend_p (decl_specifiers
);
16603 /* Enter the newly declared entry in the symbol table. If we're
16604 processing a declaration in a class-specifier, we wait until
16605 after processing the initializer. */
16608 if (parser
->in_unbraced_linkage_specification_p
)
16609 decl_specifiers
->storage_class
= sc_extern
;
16610 decl
= start_decl (declarator
, decl_specifiers
,
16611 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
16612 attributes
, prefix_attributes
, &pushed_scope
);
16613 cp_finalize_omp_declare_simd (parser
, decl
);
16614 /* Adjust location of decl if declarator->id_loc is more appropriate:
16615 set, and decl wasn't merged with another decl, in which case its
16616 location would be different from input_location, and more accurate. */
16618 && declarator
->id_loc
!= UNKNOWN_LOCATION
16619 && DECL_SOURCE_LOCATION (decl
) == input_location
)
16620 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
16623 /* Enter the SCOPE. That way unqualified names appearing in the
16624 initializer will be looked up in SCOPE. */
16625 pushed_scope
= push_scope (scope
);
16627 /* Perform deferred access control checks, now that we know in which
16628 SCOPE the declared entity resides. */
16629 if (!member_p
&& decl
)
16631 tree saved_current_function_decl
= NULL_TREE
;
16633 /* If the entity being declared is a function, pretend that we
16634 are in its scope. If it is a `friend', it may have access to
16635 things that would not otherwise be accessible. */
16636 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16638 saved_current_function_decl
= current_function_decl
;
16639 current_function_decl
= decl
;
16642 /* Perform access checks for template parameters. */
16643 cp_parser_perform_template_parameter_access_checks (checks
);
16645 /* Perform the access control checks for the declarator and the
16646 decl-specifiers. */
16647 perform_deferred_access_checks (tf_warning_or_error
);
16649 /* Restore the saved value. */
16650 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16651 current_function_decl
= saved_current_function_decl
;
16654 /* Parse the initializer. */
16655 initializer
= NULL_TREE
;
16656 is_direct_init
= false;
16657 is_non_constant_init
= true;
16658 if (is_initialized
)
16660 if (function_declarator_p (declarator
))
16662 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
16663 if (initialization_kind
== CPP_EQ
)
16664 initializer
= cp_parser_pure_specifier (parser
);
16667 /* If the declaration was erroneous, we don't really
16668 know what the user intended, so just silently
16669 consume the initializer. */
16670 if (decl
!= error_mark_node
)
16671 error_at (initializer_start_token
->location
,
16672 "initializer provided for function");
16673 cp_parser_skip_to_closing_parenthesis (parser
,
16674 /*recovering=*/true,
16675 /*or_comma=*/false,
16676 /*consume_paren=*/true);
16681 /* We want to record the extra mangling scope for in-class
16682 initializers of class members and initializers of static data
16683 member templates. The former involves deferring
16684 parsing of the initializer until end of class as with default
16685 arguments. So right here we only handle the latter. */
16686 if (!member_p
&& processing_template_decl
)
16687 start_lambda_scope (decl
);
16688 initializer
= cp_parser_initializer (parser
,
16690 &is_non_constant_init
);
16691 if (!member_p
&& processing_template_decl
)
16692 finish_lambda_scope ();
16693 if (initializer
== error_mark_node
)
16694 cp_parser_skip_to_end_of_statement (parser
);
16698 /* The old parser allows attributes to appear after a parenthesized
16699 initializer. Mark Mitchell proposed removing this functionality
16700 on the GCC mailing lists on 2002-08-13. This parser accepts the
16701 attributes -- but ignores them. */
16702 if (cp_parser_allow_gnu_extensions_p (parser
)
16703 && initialization_kind
== CPP_OPEN_PAREN
)
16704 if (cp_parser_attributes_opt (parser
))
16705 warning (OPT_Wattributes
,
16706 "attributes after parenthesized initializer ignored");
16708 /* For an in-class declaration, use `grokfield' to create the
16714 pop_scope (pushed_scope
);
16715 pushed_scope
= NULL_TREE
;
16717 decl
= grokfield (declarator
, decl_specifiers
,
16718 initializer
, !is_non_constant_init
,
16719 /*asmspec=*/NULL_TREE
,
16720 chainon (attributes
, prefix_attributes
));
16721 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16722 cp_parser_save_default_args (parser
, decl
);
16723 cp_finalize_omp_declare_simd (parser
, decl
);
16726 /* Finish processing the declaration. But, skip member
16728 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
16730 cp_finish_decl (decl
,
16731 initializer
, !is_non_constant_init
,
16733 /* If the initializer is in parentheses, then this is
16734 a direct-initialization, which means that an
16735 `explicit' constructor is OK. Otherwise, an
16736 `explicit' constructor cannot be used. */
16737 ((is_direct_init
|| !is_initialized
)
16738 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
16740 else if ((cxx_dialect
!= cxx98
) && friend_p
16741 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16742 /* Core issue #226 (C++0x only): A default template-argument
16743 shall not be specified in a friend class template
16745 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
16746 /*is_partial=*/false, /*is_friend_decl=*/1);
16748 if (!friend_p
&& pushed_scope
)
16749 pop_scope (pushed_scope
);
16751 if (function_declarator_p (declarator
)
16752 && parser
->fully_implicit_function_template_p
)
16755 decl
= finish_fully_implicit_template (parser
, decl
);
16757 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
16763 /* Parse a declarator.
16767 ptr-operator declarator
16769 abstract-declarator:
16770 ptr-operator abstract-declarator [opt]
16771 direct-abstract-declarator
16776 attributes [opt] direct-declarator
16777 attributes [opt] ptr-operator declarator
16779 abstract-declarator:
16780 attributes [opt] ptr-operator abstract-declarator [opt]
16781 attributes [opt] direct-abstract-declarator
16783 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16784 detect constructor, destructor or conversion operators. It is set
16785 to -1 if the declarator is a name, and +1 if it is a
16786 function. Otherwise it is set to zero. Usually you just want to
16787 test for >0, but internally the negative value is used.
16789 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16790 a decl-specifier-seq unless it declares a constructor, destructor,
16791 or conversion. It might seem that we could check this condition in
16792 semantic analysis, rather than parsing, but that makes it difficult
16793 to handle something like `f()'. We want to notice that there are
16794 no decl-specifiers, and therefore realize that this is an
16795 expression, not a declaration.)
16797 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16798 the declarator is a direct-declarator of the form "(...)".
16800 MEMBER_P is true iff this declarator is a member-declarator. */
16802 static cp_declarator
*
16803 cp_parser_declarator (cp_parser
* parser
,
16804 cp_parser_declarator_kind dcl_kind
,
16805 int* ctor_dtor_or_conv_p
,
16806 bool* parenthesized_p
,
16809 cp_declarator
*declarator
;
16810 enum tree_code code
;
16811 cp_cv_quals cv_quals
;
16813 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
16815 /* Assume this is not a constructor, destructor, or type-conversion
16817 if (ctor_dtor_or_conv_p
)
16818 *ctor_dtor_or_conv_p
= 0;
16820 if (cp_parser_allow_gnu_extensions_p (parser
))
16821 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
16823 /* Check for the ptr-operator production. */
16824 cp_parser_parse_tentatively (parser
);
16825 /* Parse the ptr-operator. */
16826 code
= cp_parser_ptr_operator (parser
,
16831 /* If that worked, then we have a ptr-operator. */
16832 if (cp_parser_parse_definitely (parser
))
16834 /* If a ptr-operator was found, then this declarator was not
16836 if (parenthesized_p
)
16837 *parenthesized_p
= true;
16838 /* The dependent declarator is optional if we are parsing an
16839 abstract-declarator. */
16840 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16841 cp_parser_parse_tentatively (parser
);
16843 /* Parse the dependent declarator. */
16844 declarator
= cp_parser_declarator (parser
, dcl_kind
,
16845 /*ctor_dtor_or_conv_p=*/NULL
,
16846 /*parenthesized_p=*/NULL
,
16847 /*member_p=*/false);
16849 /* If we are parsing an abstract-declarator, we must handle the
16850 case where the dependent declarator is absent. */
16851 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
16852 && !cp_parser_parse_definitely (parser
))
16855 declarator
= cp_parser_make_indirect_declarator
16856 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
16858 /* Everything else is a direct-declarator. */
16861 if (parenthesized_p
)
16862 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
16864 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
16865 ctor_dtor_or_conv_p
,
16869 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
16870 declarator
->attributes
= gnu_attributes
;
16874 /* Parse a direct-declarator or direct-abstract-declarator.
16878 direct-declarator ( parameter-declaration-clause )
16879 cv-qualifier-seq [opt]
16880 ref-qualifier [opt]
16881 exception-specification [opt]
16882 direct-declarator [ constant-expression [opt] ]
16885 direct-abstract-declarator:
16886 direct-abstract-declarator [opt]
16887 ( parameter-declaration-clause )
16888 cv-qualifier-seq [opt]
16889 ref-qualifier [opt]
16890 exception-specification [opt]
16891 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16892 ( abstract-declarator )
16894 Returns a representation of the declarator. DCL_KIND is
16895 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16896 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16897 we are parsing a direct-declarator. It is
16898 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16899 of ambiguity we prefer an abstract declarator, as per
16900 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16901 cp_parser_declarator. */
16903 static cp_declarator
*
16904 cp_parser_direct_declarator (cp_parser
* parser
,
16905 cp_parser_declarator_kind dcl_kind
,
16906 int* ctor_dtor_or_conv_p
,
16910 cp_declarator
*declarator
= NULL
;
16911 tree scope
= NULL_TREE
;
16912 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16913 bool saved_in_declarator_p
= parser
->in_declarator_p
;
16915 tree pushed_scope
= NULL_TREE
;
16919 /* Peek at the next token. */
16920 token
= cp_lexer_peek_token (parser
->lexer
);
16921 if (token
->type
== CPP_OPEN_PAREN
)
16923 /* This is either a parameter-declaration-clause, or a
16924 parenthesized declarator. When we know we are parsing a
16925 named declarator, it must be a parenthesized declarator
16926 if FIRST is true. For instance, `(int)' is a
16927 parameter-declaration-clause, with an omitted
16928 direct-abstract-declarator. But `((*))', is a
16929 parenthesized abstract declarator. Finally, when T is a
16930 template parameter `(T)' is a
16931 parameter-declaration-clause, and not a parenthesized
16934 We first try and parse a parameter-declaration-clause,
16935 and then try a nested declarator (if FIRST is true).
16937 It is not an error for it not to be a
16938 parameter-declaration-clause, even when FIRST is
16944 The first is the declaration of a function while the
16945 second is the definition of a variable, including its
16948 Having seen only the parenthesis, we cannot know which of
16949 these two alternatives should be selected. Even more
16950 complex are examples like:
16955 The former is a function-declaration; the latter is a
16956 variable initialization.
16958 Thus again, we try a parameter-declaration-clause, and if
16959 that fails, we back out and return. */
16961 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16964 unsigned saved_num_template_parameter_lists
;
16965 bool is_declarator
= false;
16967 /* In a member-declarator, the only valid interpretation
16968 of a parenthesis is the start of a
16969 parameter-declaration-clause. (It is invalid to
16970 initialize a static data member with a parenthesized
16971 initializer; only the "=" form of initialization is
16974 cp_parser_parse_tentatively (parser
);
16976 /* Consume the `('. */
16977 cp_lexer_consume_token (parser
->lexer
);
16980 /* If this is going to be an abstract declarator, we're
16981 in a declarator and we can't have default args. */
16982 parser
->default_arg_ok_p
= false;
16983 parser
->in_declarator_p
= true;
16986 /* Inside the function parameter list, surrounding
16987 template-parameter-lists do not apply. */
16988 saved_num_template_parameter_lists
16989 = parser
->num_template_parameter_lists
;
16990 parser
->num_template_parameter_lists
= 0;
16992 begin_scope (sk_function_parms
, NULL_TREE
);
16994 /* Parse the parameter-declaration-clause. */
16995 params
= cp_parser_parameter_declaration_clause (parser
);
16997 /* Restore saved template parameter lists accounting for implicit
16998 template parameters. */
16999 parser
->num_template_parameter_lists
17000 += saved_num_template_parameter_lists
;
17002 /* Consume the `)'. */
17003 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17005 /* If all went well, parse the cv-qualifier-seq,
17006 ref-qualifier and the exception-specification. */
17007 if (member_p
|| cp_parser_parse_definitely (parser
))
17009 cp_cv_quals cv_quals
;
17010 cp_virt_specifiers virt_specifiers
;
17011 cp_ref_qualifier ref_qual
;
17012 tree exception_specification
;
17015 bool memfn
= (member_p
|| (pushed_scope
17016 && CLASS_TYPE_P (pushed_scope
)));
17018 is_declarator
= true;
17020 if (ctor_dtor_or_conv_p
)
17021 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17024 /* Parse the cv-qualifier-seq. */
17025 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17026 /* Parse the ref-qualifier. */
17027 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17028 /* And the exception-specification. */
17029 exception_specification
17030 = cp_parser_exception_specification_opt (parser
);
17032 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17034 late_return
= (cp_parser_late_return_type_opt
17035 (parser
, declarator
,
17036 memfn
? cv_quals
: -1));
17039 /* Parse the virt-specifier-seq. */
17040 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17042 /* Create the function-declarator. */
17043 declarator
= make_call_declarator (declarator
,
17048 exception_specification
,
17050 declarator
->std_attributes
= attrs
;
17051 /* Any subsequent parameter lists are to do with
17052 return type, so are not those of the declared
17054 parser
->default_arg_ok_p
= false;
17057 /* Remove the function parms from scope. */
17058 pop_bindings_and_leave_scope ();
17061 /* Repeat the main loop. */
17065 /* If this is the first, we can try a parenthesized
17069 bool saved_in_type_id_in_expr_p
;
17071 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17072 parser
->in_declarator_p
= saved_in_declarator_p
;
17074 /* Consume the `('. */
17075 cp_lexer_consume_token (parser
->lexer
);
17076 /* Parse the nested declarator. */
17077 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17078 parser
->in_type_id_in_expr_p
= true;
17080 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17081 /*parenthesized_p=*/NULL
,
17083 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17085 /* Expect a `)'. */
17086 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17087 declarator
= cp_error_declarator
;
17088 if (declarator
== cp_error_declarator
)
17091 goto handle_declarator
;
17093 /* Otherwise, we must be done. */
17097 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17098 && token
->type
== CPP_OPEN_SQUARE
17099 && !cp_next_tokens_can_be_attribute_p (parser
))
17101 /* Parse an array-declarator. */
17102 tree bounds
, attrs
;
17104 if (ctor_dtor_or_conv_p
)
17105 *ctor_dtor_or_conv_p
= 0;
17108 parser
->default_arg_ok_p
= false;
17109 parser
->in_declarator_p
= true;
17110 /* Consume the `['. */
17111 cp_lexer_consume_token (parser
->lexer
);
17112 /* Peek at the next token. */
17113 token
= cp_lexer_peek_token (parser
->lexer
);
17114 /* If the next token is `]', then there is no
17115 constant-expression. */
17116 if (token
->type
!= CPP_CLOSE_SQUARE
)
17118 bool non_constant_p
;
17120 = cp_parser_constant_expression (parser
,
17121 /*allow_non_constant=*/true,
17123 if (!non_constant_p
)
17125 else if (error_operand_p (bounds
))
17126 /* Already gave an error. */;
17127 else if (!parser
->in_function_body
17128 || current_binding_level
->kind
== sk_function_parms
)
17130 /* Normally, the array bound must be an integral constant
17131 expression. However, as an extension, we allow VLAs
17132 in function scopes as long as they aren't part of a
17133 parameter declaration. */
17134 cp_parser_error (parser
,
17135 "array bound is not an integer constant");
17136 bounds
= error_mark_node
;
17138 else if (processing_template_decl
)
17140 /* Remember this wasn't a constant-expression. */
17141 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17142 TREE_SIDE_EFFECTS (bounds
) = 1;
17146 bounds
= NULL_TREE
;
17147 /* Look for the closing `]'. */
17148 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17150 declarator
= cp_error_declarator
;
17154 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17155 declarator
= make_array_declarator (declarator
, bounds
);
17156 declarator
->std_attributes
= attrs
;
17158 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17161 tree qualifying_scope
;
17162 tree unqualified_name
;
17164 special_function_kind sfk
;
17166 bool pack_expansion_p
= false;
17167 cp_token
*declarator_id_start_token
;
17169 /* Parse a declarator-id */
17170 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17173 cp_parser_parse_tentatively (parser
);
17175 /* If we see an ellipsis, we should be looking at a
17177 if (token
->type
== CPP_ELLIPSIS
)
17179 /* Consume the `...' */
17180 cp_lexer_consume_token (parser
->lexer
);
17182 pack_expansion_p
= true;
17186 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17188 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17189 qualifying_scope
= parser
->scope
;
17194 if (!unqualified_name
&& pack_expansion_p
)
17196 /* Check whether an error occurred. */
17197 okay
= !cp_parser_error_occurred (parser
);
17199 /* We already consumed the ellipsis to mark a
17200 parameter pack, but we have no way to report it,
17201 so abort the tentative parse. We will be exiting
17202 immediately anyway. */
17203 cp_parser_abort_tentative_parse (parser
);
17206 okay
= cp_parser_parse_definitely (parser
);
17209 unqualified_name
= error_mark_node
;
17210 else if (unqualified_name
17211 && (qualifying_scope
17212 || (!identifier_p (unqualified_name
))))
17214 cp_parser_error (parser
, "expected unqualified-id");
17215 unqualified_name
= error_mark_node
;
17219 if (!unqualified_name
)
17221 if (unqualified_name
== error_mark_node
)
17223 declarator
= cp_error_declarator
;
17224 pack_expansion_p
= false;
17225 declarator
->parameter_pack_p
= false;
17229 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17231 if (qualifying_scope
&& at_namespace_scope_p ()
17232 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17234 /* In the declaration of a member of a template class
17235 outside of the class itself, the SCOPE will sometimes
17236 be a TYPENAME_TYPE. For example, given:
17238 template <typename T>
17239 int S<T>::R::i = 3;
17241 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17242 this context, we must resolve S<T>::R to an ordinary
17243 type, rather than a typename type.
17245 The reason we normally avoid resolving TYPENAME_TYPEs
17246 is that a specialization of `S' might render
17247 `S<T>::R' not a type. However, if `S' is
17248 specialized, then this `i' will not be used, so there
17249 is no harm in resolving the types here. */
17252 /* Resolve the TYPENAME_TYPE. */
17253 type
= resolve_typename_type (qualifying_scope
,
17254 /*only_current_p=*/false);
17255 /* If that failed, the declarator is invalid. */
17256 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17258 if (typedef_variant_p (type
))
17259 error_at (declarator_id_start_token
->location
,
17260 "cannot define member of dependent typedef "
17263 error_at (declarator_id_start_token
->location
,
17264 "%<%T::%E%> is not a type",
17265 TYPE_CONTEXT (qualifying_scope
),
17266 TYPE_IDENTIFIER (qualifying_scope
));
17268 qualifying_scope
= type
;
17273 if (unqualified_name
)
17277 if (qualifying_scope
17278 && CLASS_TYPE_P (qualifying_scope
))
17279 class_type
= qualifying_scope
;
17281 class_type
= current_class_type
;
17283 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17285 tree name_type
= TREE_TYPE (unqualified_name
);
17286 if (class_type
&& same_type_p (name_type
, class_type
))
17288 if (qualifying_scope
17289 && CLASSTYPE_USE_TEMPLATE (name_type
))
17291 error_at (declarator_id_start_token
->location
,
17292 "invalid use of constructor as a template");
17293 inform (declarator_id_start_token
->location
,
17294 "use %<%T::%D%> instead of %<%T::%D%> to "
17295 "name the constructor in a qualified name",
17297 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17298 class_type
, name_type
);
17299 declarator
= cp_error_declarator
;
17303 unqualified_name
= constructor_name (class_type
);
17307 /* We do not attempt to print the declarator
17308 here because we do not have enough
17309 information about its original syntactic
17311 cp_parser_error (parser
, "invalid declarator");
17312 declarator
= cp_error_declarator
;
17319 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17320 sfk
= sfk_destructor
;
17321 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17322 sfk
= sfk_conversion
;
17323 else if (/* There's no way to declare a constructor
17324 for an anonymous type, even if the type
17325 got a name for linkage purposes. */
17326 !TYPE_WAS_ANONYMOUS (class_type
)
17327 && constructor_name_p (unqualified_name
,
17330 unqualified_name
= constructor_name (class_type
);
17331 sfk
= sfk_constructor
;
17333 else if (is_overloaded_fn (unqualified_name
)
17334 && DECL_CONSTRUCTOR_P (get_first_fn
17335 (unqualified_name
)))
17336 sfk
= sfk_constructor
;
17338 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17339 *ctor_dtor_or_conv_p
= -1;
17342 declarator
= make_id_declarator (qualifying_scope
,
17345 declarator
->std_attributes
= attrs
;
17346 declarator
->id_loc
= token
->location
;
17347 declarator
->parameter_pack_p
= pack_expansion_p
;
17349 if (pack_expansion_p
)
17350 maybe_warn_variadic_templates ();
17353 handle_declarator
:;
17354 scope
= get_scope_of_declarator (declarator
);
17357 /* Any names that appear after the declarator-id for a
17358 member are looked up in the containing scope. */
17359 if (at_function_scope_p ())
17361 /* But declarations with qualified-ids can't appear in a
17363 cp_parser_error (parser
, "qualified-id in declaration");
17366 pushed_scope
= push_scope (scope
);
17368 parser
->in_declarator_p
= true;
17369 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17370 || (declarator
&& declarator
->kind
== cdk_id
))
17371 /* Default args are only allowed on function
17373 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17375 parser
->default_arg_ok_p
= false;
17384 /* For an abstract declarator, we might wind up with nothing at this
17385 point. That's an error; the declarator is not optional. */
17387 cp_parser_error (parser
, "expected declarator");
17389 /* If we entered a scope, we must exit it now. */
17391 pop_scope (pushed_scope
);
17393 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17394 parser
->in_declarator_p
= saved_in_declarator_p
;
17399 /* Parse a ptr-operator.
17402 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17403 * cv-qualifier-seq [opt]
17405 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17406 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17411 & cv-qualifier-seq [opt]
17413 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17414 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17415 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17416 filled in with the TYPE containing the member. *CV_QUALS is
17417 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17418 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17419 Note that the tree codes returned by this function have nothing
17420 to do with the types of trees that will be eventually be created
17421 to represent the pointer or reference type being parsed. They are
17422 just constants with suggestive names. */
17423 static enum tree_code
17424 cp_parser_ptr_operator (cp_parser
* parser
,
17426 cp_cv_quals
*cv_quals
,
17429 enum tree_code code
= ERROR_MARK
;
17431 tree attrs
= NULL_TREE
;
17433 /* Assume that it's not a pointer-to-member. */
17435 /* And that there are no cv-qualifiers. */
17436 *cv_quals
= TYPE_UNQUALIFIED
;
17438 /* Peek at the next token. */
17439 token
= cp_lexer_peek_token (parser
->lexer
);
17441 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17442 if (token
->type
== CPP_MULT
)
17443 code
= INDIRECT_REF
;
17444 else if (token
->type
== CPP_AND
)
17446 else if ((cxx_dialect
!= cxx98
) &&
17447 token
->type
== CPP_AND_AND
) /* C++0x only */
17448 code
= NON_LVALUE_EXPR
;
17450 if (code
!= ERROR_MARK
)
17452 /* Consume the `*', `&' or `&&'. */
17453 cp_lexer_consume_token (parser
->lexer
);
17455 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17456 `&', if we are allowing GNU extensions. (The only qualifier
17457 that can legally appear after `&' is `restrict', but that is
17458 enforced during semantic analysis. */
17459 if (code
== INDIRECT_REF
17460 || cp_parser_allow_gnu_extensions_p (parser
))
17461 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17463 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17464 if (attributes
!= NULL
)
17465 *attributes
= attrs
;
17469 /* Try the pointer-to-member case. */
17470 cp_parser_parse_tentatively (parser
);
17471 /* Look for the optional `::' operator. */
17472 cp_parser_global_scope_opt (parser
,
17473 /*current_scope_valid_p=*/false);
17474 /* Look for the nested-name specifier. */
17475 token
= cp_lexer_peek_token (parser
->lexer
);
17476 cp_parser_nested_name_specifier (parser
,
17477 /*typename_keyword_p=*/false,
17478 /*check_dependency_p=*/true,
17480 /*is_declaration=*/false);
17481 /* If we found it, and the next token is a `*', then we are
17482 indeed looking at a pointer-to-member operator. */
17483 if (!cp_parser_error_occurred (parser
)
17484 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
17486 /* Indicate that the `*' operator was used. */
17487 code
= INDIRECT_REF
;
17489 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
17490 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
17491 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
17492 error_at (token
->location
, "cannot form pointer to member of "
17493 "non-class %q#T", parser
->scope
);
17496 /* The type of which the member is a member is given by the
17498 *type
= parser
->scope
;
17499 /* The next name will not be qualified. */
17500 parser
->scope
= NULL_TREE
;
17501 parser
->qualifying_scope
= NULL_TREE
;
17502 parser
->object_scope
= NULL_TREE
;
17503 /* Look for optional c++11 attributes. */
17504 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17505 if (attributes
!= NULL
)
17506 *attributes
= attrs
;
17507 /* Look for the optional cv-qualifier-seq. */
17508 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17511 /* If that didn't work we don't have a ptr-operator. */
17512 if (!cp_parser_parse_definitely (parser
))
17513 cp_parser_error (parser
, "expected ptr-operator");
17519 /* Parse an (optional) cv-qualifier-seq.
17522 cv-qualifier cv-qualifier-seq [opt]
17533 Returns a bitmask representing the cv-qualifiers. */
17536 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
17538 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
17543 cp_cv_quals cv_qualifier
;
17545 /* Peek at the next token. */
17546 token
= cp_lexer_peek_token (parser
->lexer
);
17547 /* See if it's a cv-qualifier. */
17548 switch (token
->keyword
)
17551 cv_qualifier
= TYPE_QUAL_CONST
;
17555 cv_qualifier
= TYPE_QUAL_VOLATILE
;
17559 cv_qualifier
= TYPE_QUAL_RESTRICT
;
17563 cv_qualifier
= TYPE_UNQUALIFIED
;
17570 if (cv_quals
& cv_qualifier
)
17572 error_at (token
->location
, "duplicate cv-qualifier");
17573 cp_lexer_purge_token (parser
->lexer
);
17577 cp_lexer_consume_token (parser
->lexer
);
17578 cv_quals
|= cv_qualifier
;
17585 /* Parse an (optional) ref-qualifier
17591 Returns cp_ref_qualifier representing ref-qualifier. */
17593 static cp_ref_qualifier
17594 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
17596 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
17598 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17599 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
17604 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
17605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17607 switch (token
->type
)
17610 curr_ref_qual
= REF_QUAL_LVALUE
;
17614 curr_ref_qual
= REF_QUAL_RVALUE
;
17618 curr_ref_qual
= REF_QUAL_NONE
;
17622 if (!curr_ref_qual
)
17626 error_at (token
->location
, "multiple ref-qualifiers");
17627 cp_lexer_purge_token (parser
->lexer
);
17631 ref_qual
= curr_ref_qual
;
17632 cp_lexer_consume_token (parser
->lexer
);
17639 /* Parse an (optional) virt-specifier-seq.
17641 virt-specifier-seq:
17642 virt-specifier virt-specifier-seq [opt]
17648 Returns a bitmask representing the virt-specifiers. */
17650 static cp_virt_specifiers
17651 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
17653 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
17658 cp_virt_specifiers virt_specifier
;
17660 /* Peek at the next token. */
17661 token
= cp_lexer_peek_token (parser
->lexer
);
17662 /* See if it's a virt-specifier-qualifier. */
17663 if (token
->type
!= CPP_NAME
)
17665 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
17667 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17668 virt_specifier
= VIRT_SPEC_OVERRIDE
;
17670 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
17672 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17673 virt_specifier
= VIRT_SPEC_FINAL
;
17675 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
17677 virt_specifier
= VIRT_SPEC_FINAL
;
17682 if (virt_specifiers
& virt_specifier
)
17684 error_at (token
->location
, "duplicate virt-specifier");
17685 cp_lexer_purge_token (parser
->lexer
);
17689 cp_lexer_consume_token (parser
->lexer
);
17690 virt_specifiers
|= virt_specifier
;
17693 return virt_specifiers
;
17696 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17697 is in scope even though it isn't real. */
17700 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
17704 if (current_class_ptr
)
17706 /* We don't clear this between NSDMIs. Is it already what we want? */
17707 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
17708 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
17709 && cp_type_quals (type
) == quals
)
17713 this_parm
= build_this_parm (ctype
, quals
);
17714 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17715 current_class_ptr
= NULL_TREE
;
17717 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
17718 current_class_ptr
= this_parm
;
17721 /* Return true iff our current scope is a non-static data member
17725 parsing_nsdmi (void)
17727 /* We recognize NSDMI context by the context-less 'this' pointer set up
17728 by the function above. */
17729 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
17734 /* Parse a late-specified return type, if any. This is not a separate
17735 non-terminal, but part of a function declarator, which looks like
17737 -> trailing-type-specifier-seq abstract-declarator(opt)
17739 Returns the type indicated by the type-id.
17741 In addition to this this parses any queued up omp declare simd
17744 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17748 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
17752 tree type
= NULL_TREE
;
17753 bool declare_simd_p
= (parser
->omp_declare_simd
17755 && declarator
->kind
== cdk_id
);
17757 /* Peek at the next token. */
17758 token
= cp_lexer_peek_token (parser
->lexer
);
17759 /* A late-specified return type is indicated by an initial '->'. */
17760 if (token
->type
!= CPP_DEREF
&& !declare_simd_p
)
17763 tree save_ccp
= current_class_ptr
;
17764 tree save_ccr
= current_class_ref
;
17767 /* DR 1207: 'this' is in scope in the trailing return type. */
17768 inject_this_parameter (current_class_type
, quals
);
17771 if (token
->type
== CPP_DEREF
)
17773 /* Consume the ->. */
17774 cp_lexer_consume_token (parser
->lexer
);
17776 type
= cp_parser_trailing_type_id (parser
);
17779 if (declare_simd_p
)
17780 declarator
->std_attributes
17781 = cp_parser_late_parsing_omp_declare_simd (parser
,
17782 declarator
->std_attributes
);
17786 current_class_ptr
= save_ccp
;
17787 current_class_ref
= save_ccr
;
17793 /* Parse a declarator-id.
17797 :: [opt] nested-name-specifier [opt] type-name
17799 In the `id-expression' case, the value returned is as for
17800 cp_parser_id_expression if the id-expression was an unqualified-id.
17801 If the id-expression was a qualified-id, then a SCOPE_REF is
17802 returned. The first operand is the scope (either a NAMESPACE_DECL
17803 or TREE_TYPE), but the second is still just a representation of an
17807 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
17810 /* The expression must be an id-expression. Assume that qualified
17811 names are the names of types so that:
17814 int S<T>::R::i = 3;
17816 will work; we must treat `S<T>::R' as the name of a type.
17817 Similarly, assume that qualified names are templates, where
17821 int S<T>::R<T>::i = 3;
17824 id
= cp_parser_id_expression (parser
,
17825 /*template_keyword_p=*/false,
17826 /*check_dependency_p=*/false,
17827 /*template_p=*/NULL
,
17828 /*declarator_p=*/true,
17830 if (id
&& BASELINK_P (id
))
17831 id
= BASELINK_FUNCTIONS (id
);
17835 /* Parse a type-id.
17838 type-specifier-seq abstract-declarator [opt]
17840 Returns the TYPE specified. */
17843 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
17844 bool is_trailing_return
)
17846 cp_decl_specifier_seq type_specifier_seq
;
17847 cp_declarator
*abstract_declarator
;
17849 /* Parse the type-specifier-seq. */
17850 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17851 is_trailing_return
,
17852 &type_specifier_seq
);
17853 if (type_specifier_seq
.type
== error_mark_node
)
17854 return error_mark_node
;
17856 /* There might or might not be an abstract declarator. */
17857 cp_parser_parse_tentatively (parser
);
17858 /* Look for the declarator. */
17859 abstract_declarator
17860 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
17861 /*parenthesized_p=*/NULL
,
17862 /*member_p=*/false);
17863 /* Check to see if there really was a declarator. */
17864 if (!cp_parser_parse_definitely (parser
))
17865 abstract_declarator
= NULL
;
17867 if (type_specifier_seq
.type
17868 && cxx_dialect
< cxx1y
17869 && type_uses_auto (type_specifier_seq
.type
))
17871 /* A type-id with type 'auto' is only ok if the abstract declarator
17872 is a function declarator with a late-specified return type. */
17873 if (abstract_declarator
17874 && abstract_declarator
->kind
== cdk_function
17875 && abstract_declarator
->u
.function
.late_return_type
)
17879 error ("invalid use of %<auto%>");
17880 return error_mark_node
;
17884 return groktypename (&type_specifier_seq
, abstract_declarator
,
17888 static tree
cp_parser_type_id (cp_parser
*parser
)
17890 return cp_parser_type_id_1 (parser
, false, false);
17893 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
17896 const char *saved_message
= parser
->type_definition_forbidden_message
;
17897 parser
->type_definition_forbidden_message
17898 = G_("types may not be defined in template arguments");
17899 r
= cp_parser_type_id_1 (parser
, true, false);
17900 parser
->type_definition_forbidden_message
= saved_message
;
17904 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
17906 return cp_parser_type_id_1 (parser
, false, true);
17909 /* Parse a type-specifier-seq.
17911 type-specifier-seq:
17912 type-specifier type-specifier-seq [opt]
17916 type-specifier-seq:
17917 attributes type-specifier-seq [opt]
17919 If IS_DECLARATION is true, we are at the start of a "condition" or
17920 exception-declaration, so we might be followed by a declarator-id.
17922 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17923 i.e. we've just seen "->".
17925 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17928 cp_parser_type_specifier_seq (cp_parser
* parser
,
17929 bool is_declaration
,
17930 bool is_trailing_return
,
17931 cp_decl_specifier_seq
*type_specifier_seq
)
17933 bool seen_type_specifier
= false;
17934 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
17935 cp_token
*start_token
= NULL
;
17937 /* Clear the TYPE_SPECIFIER_SEQ. */
17938 clear_decl_specs (type_specifier_seq
);
17940 /* In the context of a trailing return type, enum E { } is an
17941 elaborated-type-specifier followed by a function-body, not an
17943 if (is_trailing_return
)
17944 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
17946 /* Parse the type-specifiers and attributes. */
17949 tree type_specifier
;
17950 bool is_cv_qualifier
;
17952 /* Check for attributes first. */
17953 if (cp_next_tokens_can_be_attribute_p (parser
))
17955 type_specifier_seq
->attributes
=
17956 chainon (type_specifier_seq
->attributes
,
17957 cp_parser_attributes_opt (parser
));
17961 /* record the token of the beginning of the type specifier seq,
17962 for error reporting purposes*/
17964 start_token
= cp_lexer_peek_token (parser
->lexer
);
17966 /* Look for the type-specifier. */
17967 type_specifier
= cp_parser_type_specifier (parser
,
17969 type_specifier_seq
,
17970 /*is_declaration=*/false,
17973 if (!type_specifier
)
17975 /* If the first type-specifier could not be found, this is not a
17976 type-specifier-seq at all. */
17977 if (!seen_type_specifier
)
17979 cp_parser_error (parser
, "expected type-specifier");
17980 type_specifier_seq
->type
= error_mark_node
;
17983 /* If subsequent type-specifiers could not be found, the
17984 type-specifier-seq is complete. */
17988 seen_type_specifier
= true;
17989 /* The standard says that a condition can be:
17991 type-specifier-seq declarator = assignment-expression
17998 we should treat the "S" as a declarator, not as a
17999 type-specifier. The standard doesn't say that explicitly for
18000 type-specifier-seq, but it does say that for
18001 decl-specifier-seq in an ordinary declaration. Perhaps it
18002 would be clearer just to allow a decl-specifier-seq here, and
18003 then add a semantic restriction that if any decl-specifiers
18004 that are not type-specifiers appear, the program is invalid. */
18005 if (is_declaration
&& !is_cv_qualifier
)
18006 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18010 /* Parse a parameter-declaration-clause.
18012 parameter-declaration-clause:
18013 parameter-declaration-list [opt] ... [opt]
18014 parameter-declaration-list , ...
18016 Returns a representation for the parameter declarations. A return
18017 value of NULL indicates a parameter-declaration-clause consisting
18018 only of an ellipsis. */
18021 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18030 int auto_is_implicit_function_template_parm_p
;
18032 parser
->auto_is_implicit_function_template_parm_p
18033 = auto_is_implicit_function_template_parm_p
;
18035 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18039 if (!processing_specialization
)
18040 parser
->auto_is_implicit_function_template_parm_p
= true;
18042 /* Peek at the next token. */
18043 token
= cp_lexer_peek_token (parser
->lexer
);
18044 /* Check for trivial parameter-declaration-clauses. */
18045 if (token
->type
== CPP_ELLIPSIS
)
18047 /* Consume the `...' token. */
18048 cp_lexer_consume_token (parser
->lexer
);
18051 else if (token
->type
== CPP_CLOSE_PAREN
)
18052 /* There are no parameters. */
18054 #ifndef NO_IMPLICIT_EXTERN_C
18055 if (in_system_header
&& current_class_type
== NULL
18056 && current_lang_name
== lang_name_c
)
18060 return void_list_node
;
18062 /* Check for `(void)', too, which is a special case. */
18063 else if (token
->keyword
== RID_VOID
18064 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18065 == CPP_CLOSE_PAREN
))
18067 /* Consume the `void' token. */
18068 cp_lexer_consume_token (parser
->lexer
);
18069 /* There are no parameters. */
18070 return void_list_node
;
18073 /* Parse the parameter-declaration-list. */
18074 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18075 /* If a parse error occurred while parsing the
18076 parameter-declaration-list, then the entire
18077 parameter-declaration-clause is erroneous. */
18081 /* Peek at the next token. */
18082 token
= cp_lexer_peek_token (parser
->lexer
);
18083 /* If it's a `,', the clause should terminate with an ellipsis. */
18084 if (token
->type
== CPP_COMMA
)
18086 /* Consume the `,'. */
18087 cp_lexer_consume_token (parser
->lexer
);
18088 /* Expect an ellipsis. */
18090 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18092 /* It might also be `...' if the optional trailing `,' was
18094 else if (token
->type
== CPP_ELLIPSIS
)
18096 /* Consume the `...' token. */
18097 cp_lexer_consume_token (parser
->lexer
);
18098 /* And remember that we saw it. */
18102 ellipsis_p
= false;
18104 /* Finish the parameter list. */
18106 parameters
= chainon (parameters
, void_list_node
);
18111 /* Parse a parameter-declaration-list.
18113 parameter-declaration-list:
18114 parameter-declaration
18115 parameter-declaration-list , parameter-declaration
18117 Returns a representation of the parameter-declaration-list, as for
18118 cp_parser_parameter_declaration_clause. However, the
18119 `void_list_node' is never appended to the list. Upon return,
18120 *IS_ERROR will be true iff an error occurred. */
18123 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18125 tree parameters
= NULL_TREE
;
18126 tree
*tail
= ¶meters
;
18127 bool saved_in_unbraced_linkage_specification_p
;
18130 /* Assume all will go well. */
18132 /* The special considerations that apply to a function within an
18133 unbraced linkage specifications do not apply to the parameters
18134 to the function. */
18135 saved_in_unbraced_linkage_specification_p
18136 = parser
->in_unbraced_linkage_specification_p
;
18137 parser
->in_unbraced_linkage_specification_p
= false;
18139 /* Look for more parameters. */
18142 cp_parameter_declarator
*parameter
;
18143 tree decl
= error_mark_node
;
18144 bool parenthesized_p
= false;
18145 int template_parm_idx
= (parser
->num_template_parameter_lists
?
18146 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18147 (current_template_parms
)) : 0);
18149 /* Parse the parameter. */
18151 = cp_parser_parameter_declaration (parser
,
18152 /*template_parm_p=*/false,
18155 /* We don't know yet if the enclosing context is deprecated, so wait
18156 and warn in grokparms if appropriate. */
18157 deprecated_state
= DEPRECATED_SUPPRESS
;
18161 /* If a function parameter pack was specified and an implicit template
18162 parameter was introduced during cp_parser_parameter_declaration,
18163 change any implicit parameters introduced into packs. */
18164 if (parser
->implicit_template_parms
18165 && parameter
->declarator
18166 && parameter
->declarator
->parameter_pack_p
)
18168 int latest_template_parm_idx
= TREE_VEC_LENGTH
18169 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18171 if (latest_template_parm_idx
!= template_parm_idx
)
18172 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18173 (parameter
->decl_specifiers
.type
,
18174 template_parm_idx
, latest_template_parm_idx
);
18177 decl
= grokdeclarator (parameter
->declarator
,
18178 ¶meter
->decl_specifiers
,
18180 parameter
->default_argument
!= NULL_TREE
,
18181 ¶meter
->decl_specifiers
.attributes
);
18184 deprecated_state
= DEPRECATED_NORMAL
;
18186 /* If a parse error occurred parsing the parameter declaration,
18187 then the entire parameter-declaration-list is erroneous. */
18188 if (decl
== error_mark_node
)
18191 parameters
= error_mark_node
;
18195 if (parameter
->decl_specifiers
.attributes
)
18196 cplus_decl_attributes (&decl
,
18197 parameter
->decl_specifiers
.attributes
,
18199 if (DECL_NAME (decl
))
18200 decl
= pushdecl (decl
);
18202 if (decl
!= error_mark_node
)
18204 retrofit_lang_decl (decl
);
18205 DECL_PARM_INDEX (decl
) = ++index
;
18206 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18209 /* Add the new parameter to the list. */
18210 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18211 tail
= &TREE_CHAIN (*tail
);
18213 /* Peek at the next token. */
18214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18215 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18216 /* These are for Objective-C++ */
18217 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18218 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18219 /* The parameter-declaration-list is complete. */
18221 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18225 /* Peek at the next token. */
18226 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18227 /* If it's an ellipsis, then the list is complete. */
18228 if (token
->type
== CPP_ELLIPSIS
)
18230 /* Otherwise, there must be more parameters. Consume the
18232 cp_lexer_consume_token (parser
->lexer
);
18233 /* When parsing something like:
18235 int i(float f, double d)
18237 we can tell after seeing the declaration for "f" that we
18238 are not looking at an initialization of a variable "i",
18239 but rather at the declaration of a function "i".
18241 Due to the fact that the parsing of template arguments
18242 (as specified to a template-id) requires backtracking we
18243 cannot use this technique when inside a template argument
18245 if (!parser
->in_template_argument_list_p
18246 && !parser
->in_type_id_in_expr_p
18247 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18248 /* However, a parameter-declaration of the form
18249 "float(f)" (which is a valid declaration of a
18250 parameter "f") can also be interpreted as an
18251 expression (the conversion of "f" to "float"). */
18252 && !parenthesized_p
)
18253 cp_parser_commit_to_tentative_parse (parser
);
18257 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18258 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18259 cp_parser_skip_to_closing_parenthesis (parser
,
18260 /*recovering=*/true,
18261 /*or_comma=*/false,
18262 /*consume_paren=*/false);
18267 parser
->in_unbraced_linkage_specification_p
18268 = saved_in_unbraced_linkage_specification_p
;
18270 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18271 if (current_binding_level
->level_chain
== its
)
18273 parser
->implicit_template_parms
= 0;
18274 parser
->implicit_template_scope
= 0;
18280 /* Parse a parameter declaration.
18282 parameter-declaration:
18283 decl-specifier-seq ... [opt] declarator
18284 decl-specifier-seq declarator = assignment-expression
18285 decl-specifier-seq ... [opt] abstract-declarator [opt]
18286 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18288 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18289 declares a template parameter. (In that case, a non-nested `>'
18290 token encountered during the parsing of the assignment-expression
18291 is not interpreted as a greater-than operator.)
18293 Returns a representation of the parameter, or NULL if an error
18294 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18295 true iff the declarator is of the form "(p)". */
18297 static cp_parameter_declarator
*
18298 cp_parser_parameter_declaration (cp_parser
*parser
,
18299 bool template_parm_p
,
18300 bool *parenthesized_p
)
18302 int declares_class_or_enum
;
18303 cp_decl_specifier_seq decl_specifiers
;
18304 cp_declarator
*declarator
;
18305 tree default_argument
;
18306 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18307 const char *saved_message
;
18309 /* In a template parameter, `>' is not an operator.
18313 When parsing a default template-argument for a non-type
18314 template-parameter, the first non-nested `>' is taken as the end
18315 of the template parameter-list rather than a greater-than
18318 /* Type definitions may not appear in parameter types. */
18319 saved_message
= parser
->type_definition_forbidden_message
;
18320 parser
->type_definition_forbidden_message
18321 = G_("types may not be defined in parameter types");
18323 /* Parse the declaration-specifiers. */
18324 cp_parser_decl_specifier_seq (parser
,
18325 CP_PARSER_FLAGS_NONE
,
18327 &declares_class_or_enum
);
18329 /* Complain about missing 'typename' or other invalid type names. */
18330 if (!decl_specifiers
.any_type_specifiers_p
18331 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18332 decl_specifiers
.type
= error_mark_node
;
18334 /* If an error occurred, there's no reason to attempt to parse the
18335 rest of the declaration. */
18336 if (cp_parser_error_occurred (parser
))
18338 parser
->type_definition_forbidden_message
= saved_message
;
18342 /* Peek at the next token. */
18343 token
= cp_lexer_peek_token (parser
->lexer
);
18345 /* If the next token is a `)', `,', `=', `>', or `...', then there
18346 is no declarator. However, when variadic templates are enabled,
18347 there may be a declarator following `...'. */
18348 if (token
->type
== CPP_CLOSE_PAREN
18349 || token
->type
== CPP_COMMA
18350 || token
->type
== CPP_EQ
18351 || token
->type
== CPP_GREATER
)
18354 if (parenthesized_p
)
18355 *parenthesized_p
= false;
18357 /* Otherwise, there should be a declarator. */
18360 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18361 parser
->default_arg_ok_p
= false;
18363 /* After seeing a decl-specifier-seq, if the next token is not a
18364 "(", there is no possibility that the code is a valid
18365 expression. Therefore, if parsing tentatively, we commit at
18367 if (!parser
->in_template_argument_list_p
18368 /* In an expression context, having seen:
18372 we cannot be sure whether we are looking at a
18373 function-type (taking a "char" as a parameter) or a cast
18374 of some object of type "char" to "int". */
18375 && !parser
->in_type_id_in_expr_p
18376 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18377 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
18378 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
18379 cp_parser_commit_to_tentative_parse (parser
);
18380 /* Parse the declarator. */
18381 declarator_token_start
= token
;
18382 declarator
= cp_parser_declarator (parser
,
18383 CP_PARSER_DECLARATOR_EITHER
,
18384 /*ctor_dtor_or_conv_p=*/NULL
,
18386 /*member_p=*/false);
18387 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18388 /* After the declarator, allow more attributes. */
18389 decl_specifiers
.attributes
18390 = chainon (decl_specifiers
.attributes
,
18391 cp_parser_attributes_opt (parser
));
18394 /* If the next token is an ellipsis, and we have not seen a
18395 declarator name, and the type of the declarator contains parameter
18396 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18397 a parameter pack expansion expression. Otherwise, leave the
18398 ellipsis for a C-style variadic function. */
18399 token
= cp_lexer_peek_token (parser
->lexer
);
18400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18402 tree type
= decl_specifiers
.type
;
18404 if (type
&& DECL_P (type
))
18405 type
= TREE_TYPE (type
);
18408 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
18409 && declarator_can_be_parameter_pack (declarator
)
18410 && (!declarator
|| !declarator
->parameter_pack_p
)
18411 && uses_parameter_packs (type
))
18413 /* Consume the `...'. */
18414 cp_lexer_consume_token (parser
->lexer
);
18415 maybe_warn_variadic_templates ();
18417 /* Build a pack expansion type */
18419 declarator
->parameter_pack_p
= true;
18421 decl_specifiers
.type
= make_pack_expansion (type
);
18425 /* The restriction on defining new types applies only to the type
18426 of the parameter, not to the default argument. */
18427 parser
->type_definition_forbidden_message
= saved_message
;
18429 /* If the next token is `=', then process a default argument. */
18430 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18432 token
= cp_lexer_peek_token (parser
->lexer
);
18433 /* If we are defining a class, then the tokens that make up the
18434 default argument must be saved and processed later. */
18435 if (!template_parm_p
&& at_class_scope_p ()
18436 && TYPE_BEING_DEFINED (current_class_type
)
18437 && !LAMBDA_TYPE_P (current_class_type
))
18438 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
18439 /* Outside of a class definition, we can just parse the
18440 assignment-expression. */
18443 = cp_parser_default_argument (parser
, template_parm_p
);
18445 if (!parser
->default_arg_ok_p
)
18447 if (flag_permissive
)
18448 warning (0, "deprecated use of default argument for parameter of non-function");
18451 error_at (token
->location
,
18452 "default arguments are only "
18453 "permitted for function parameters");
18454 default_argument
= NULL_TREE
;
18457 else if ((declarator
&& declarator
->parameter_pack_p
)
18458 || (decl_specifiers
.type
18459 && PACK_EXPANSION_P (decl_specifiers
.type
)))
18461 /* Find the name of the parameter pack. */
18462 cp_declarator
*id_declarator
= declarator
;
18463 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
18464 id_declarator
= id_declarator
->declarator
;
18466 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
18467 error_at (declarator_token_start
->location
,
18469 ? G_("template parameter pack %qD "
18470 "cannot have a default argument")
18471 : G_("parameter pack %qD cannot have "
18472 "a default argument"),
18473 id_declarator
->u
.id
.unqualified_name
);
18475 error_at (declarator_token_start
->location
,
18477 ? G_("template parameter pack cannot have "
18478 "a default argument")
18479 : G_("parameter pack cannot have a "
18480 "default argument"));
18482 default_argument
= NULL_TREE
;
18486 default_argument
= NULL_TREE
;
18488 return make_parameter_declarator (&decl_specifiers
,
18493 /* Parse a default argument and return it.
18495 TEMPLATE_PARM_P is true if this is a default argument for a
18496 non-type template parameter. */
18498 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
18500 tree default_argument
= NULL_TREE
;
18501 bool saved_greater_than_is_operator_p
;
18502 bool saved_local_variables_forbidden_p
;
18503 bool non_constant_p
, is_direct_init
;
18505 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18507 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
18508 parser
->greater_than_is_operator_p
= !template_parm_p
;
18509 /* Local variable names (and the `this' keyword) may not
18510 appear in a default argument. */
18511 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
18512 parser
->local_variables_forbidden_p
= true;
18513 /* Parse the assignment-expression. */
18514 if (template_parm_p
)
18515 push_deferring_access_checks (dk_no_deferred
);
18517 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
18518 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
18519 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18520 if (template_parm_p
)
18521 pop_deferring_access_checks ();
18522 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
18523 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
18525 return default_argument
;
18528 /* Parse a function-body.
18531 compound_statement */
18534 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
18536 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
18539 /* Parse a ctor-initializer-opt followed by a function-body. Return
18540 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18541 is true we are parsing a function-try-block. */
18544 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
18545 bool in_function_try_block
)
18548 bool ctor_initializer_p
;
18549 const bool check_body_p
=
18550 DECL_CONSTRUCTOR_P (current_function_decl
)
18551 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
18554 /* Begin the function body. */
18555 body
= begin_function_body ();
18556 /* Parse the optional ctor-initializer. */
18557 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
18559 /* If we're parsing a constexpr constructor definition, we need
18560 to check that the constructor body is indeed empty. However,
18561 before we get to cp_parser_function_body lot of junk has been
18562 generated, so we can't just check that we have an empty block.
18563 Rather we take a snapshot of the outermost block, and check whether
18564 cp_parser_function_body changed its state. */
18567 list
= cur_stmt_list
;
18568 if (STATEMENT_LIST_TAIL (list
))
18569 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
18571 /* Parse the function-body. */
18572 cp_parser_function_body (parser
, in_function_try_block
);
18574 check_constexpr_ctor_body (last
, list
);
18575 /* Finish the function body. */
18576 finish_function_body (body
);
18578 return ctor_initializer_p
;
18581 /* Parse an initializer.
18584 = initializer-clause
18585 ( expression-list )
18587 Returns an expression representing the initializer. If no
18588 initializer is present, NULL_TREE is returned.
18590 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18591 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18592 set to TRUE if there is no initializer present. If there is an
18593 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18594 is set to true; otherwise it is set to false. */
18597 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
18598 bool* non_constant_p
)
18603 /* Peek at the next token. */
18604 token
= cp_lexer_peek_token (parser
->lexer
);
18606 /* Let our caller know whether or not this initializer was
18608 *is_direct_init
= (token
->type
!= CPP_EQ
);
18609 /* Assume that the initializer is constant. */
18610 *non_constant_p
= false;
18612 if (token
->type
== CPP_EQ
)
18614 /* Consume the `='. */
18615 cp_lexer_consume_token (parser
->lexer
);
18616 /* Parse the initializer-clause. */
18617 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
18619 else if (token
->type
== CPP_OPEN_PAREN
)
18621 vec
<tree
, va_gc
> *vec
;
18622 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
18624 /*allow_expansion_p=*/true,
18627 return error_mark_node
;
18628 init
= build_tree_list_vec (vec
);
18629 release_tree_vector (vec
);
18631 else if (token
->type
== CPP_OPEN_BRACE
)
18633 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18634 init
= cp_parser_braced_list (parser
, non_constant_p
);
18635 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
18639 /* Anything else is an error. */
18640 cp_parser_error (parser
, "expected initializer");
18641 init
= error_mark_node
;
18647 /* Parse an initializer-clause.
18649 initializer-clause:
18650 assignment-expression
18653 Returns an expression representing the initializer.
18655 If the `assignment-expression' production is used the value
18656 returned is simply a representation for the expression.
18658 Otherwise, calls cp_parser_braced_list. */
18661 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
18665 /* Assume the expression is constant. */
18666 *non_constant_p
= false;
18668 /* If it is not a `{', then we are looking at an
18669 assignment-expression. */
18670 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
18673 = cp_parser_constant_expression (parser
,
18674 /*allow_non_constant_p=*/true,
18678 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
18680 return initializer
;
18683 /* Parse a brace-enclosed initializer list.
18686 { initializer-list , [opt] }
18689 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18690 the elements of the initializer-list (or NULL, if the last
18691 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18692 NULL_TREE. There is no way to detect whether or not the optional
18693 trailing `,' was provided. NON_CONSTANT_P is as for
18694 cp_parser_initializer. */
18697 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
18701 /* Consume the `{' token. */
18702 cp_lexer_consume_token (parser
->lexer
);
18703 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18704 initializer
= make_node (CONSTRUCTOR
);
18705 /* If it's not a `}', then there is a non-trivial initializer. */
18706 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
18708 /* Parse the initializer list. */
18709 CONSTRUCTOR_ELTS (initializer
)
18710 = cp_parser_initializer_list (parser
, non_constant_p
);
18711 /* A trailing `,' token is allowed. */
18712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18713 cp_lexer_consume_token (parser
->lexer
);
18716 *non_constant_p
= false;
18717 /* Now, there should be a trailing `}'. */
18718 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18719 TREE_TYPE (initializer
) = init_list_type_node
;
18720 return initializer
;
18723 /* Parse an initializer-list.
18726 initializer-clause ... [opt]
18727 initializer-list , initializer-clause ... [opt]
18732 designation initializer-clause ...[opt]
18733 initializer-list , designation initializer-clause ...[opt]
18738 [ constant-expression ] =
18740 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18741 for the initializer. If the INDEX of the elt is non-NULL, it is the
18742 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18743 as for cp_parser_initializer. */
18745 static vec
<constructor_elt
, va_gc
> *
18746 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
18748 vec
<constructor_elt
, va_gc
> *v
= NULL
;
18750 /* Assume all of the expressions are constant. */
18751 *non_constant_p
= false;
18753 /* Parse the rest of the list. */
18759 bool clause_non_constant_p
;
18761 /* If the next token is an identifier and the following one is a
18762 colon, we are looking at the GNU designated-initializer
18764 if (cp_parser_allow_gnu_extensions_p (parser
)
18765 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
18766 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
18768 /* Warn the user that they are using an extension. */
18769 pedwarn (input_location
, OPT_Wpedantic
,
18770 "ISO C++ does not allow designated initializers");
18771 /* Consume the identifier. */
18772 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18773 /* Consume the `:'. */
18774 cp_lexer_consume_token (parser
->lexer
);
18776 /* Also handle the C99 syntax, '. id ='. */
18777 else if (cp_parser_allow_gnu_extensions_p (parser
)
18778 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
18779 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
18780 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
18782 /* Warn the user that they are using an extension. */
18783 pedwarn (input_location
, OPT_Wpedantic
,
18784 "ISO C++ does not allow C99 designated initializers");
18785 /* Consume the `.'. */
18786 cp_lexer_consume_token (parser
->lexer
);
18787 /* Consume the identifier. */
18788 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18789 /* Consume the `='. */
18790 cp_lexer_consume_token (parser
->lexer
);
18792 /* Also handle C99 array designators, '[ const ] ='. */
18793 else if (cp_parser_allow_gnu_extensions_p (parser
)
18794 && !c_dialect_objc ()
18795 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
18797 /* In C++11, [ could start a lambda-introducer. */
18798 bool non_const
= false;
18800 cp_parser_parse_tentatively (parser
);
18801 cp_lexer_consume_token (parser
->lexer
);
18802 designator
= cp_parser_constant_expression (parser
, true, &non_const
);
18803 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
18804 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18805 if (!cp_parser_parse_definitely (parser
))
18806 designator
= NULL_TREE
;
18807 else if (non_const
)
18808 require_potential_rvalue_constant_expression (designator
);
18811 designator
= NULL_TREE
;
18813 /* Parse the initializer. */
18814 initializer
= cp_parser_initializer_clause (parser
,
18815 &clause_non_constant_p
);
18816 /* If any clause is non-constant, so is the entire initializer. */
18817 if (clause_non_constant_p
)
18818 *non_constant_p
= true;
18820 /* If we have an ellipsis, this is an initializer pack
18822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18824 /* Consume the `...'. */
18825 cp_lexer_consume_token (parser
->lexer
);
18827 /* Turn the initializer into an initializer expansion. */
18828 initializer
= make_pack_expansion (initializer
);
18831 /* Add it to the vector. */
18832 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
18834 /* If the next token is not a comma, we have reached the end of
18836 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18839 /* Peek at the next token. */
18840 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18841 /* If the next token is a `}', then we're still done. An
18842 initializer-clause can have a trailing `,' after the
18843 initializer-list and before the closing `}'. */
18844 if (token
->type
== CPP_CLOSE_BRACE
)
18847 /* Consume the `,' token. */
18848 cp_lexer_consume_token (parser
->lexer
);
18854 /* Classes [gram.class] */
18856 /* Parse a class-name.
18862 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18863 to indicate that names looked up in dependent types should be
18864 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18865 keyword has been used to indicate that the name that appears next
18866 is a template. TAG_TYPE indicates the explicit tag given before
18867 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18868 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18869 is the class being defined in a class-head.
18871 Returns the TYPE_DECL representing the class. */
18874 cp_parser_class_name (cp_parser
*parser
,
18875 bool typename_keyword_p
,
18876 bool template_keyword_p
,
18877 enum tag_types tag_type
,
18878 bool check_dependency_p
,
18880 bool is_declaration
)
18886 tree identifier
= NULL_TREE
;
18888 /* All class-names start with an identifier. */
18889 token
= cp_lexer_peek_token (parser
->lexer
);
18890 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
18892 cp_parser_error (parser
, "expected class-name");
18893 return error_mark_node
;
18896 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18897 to a template-id, so we save it here. */
18898 scope
= parser
->scope
;
18899 if (scope
== error_mark_node
)
18900 return error_mark_node
;
18902 /* Any name names a type if we're following the `typename' keyword
18903 in a qualified name where the enclosing scope is type-dependent. */
18904 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
18905 && dependent_type_p (scope
));
18906 /* Handle the common case (an identifier, but not a template-id)
18908 if (token
->type
== CPP_NAME
18909 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
18911 cp_token
*identifier_token
;
18914 /* Look for the identifier. */
18915 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
18916 ambiguous_p
= identifier_token
->ambiguous_p
;
18917 identifier
= cp_parser_identifier (parser
);
18918 /* If the next token isn't an identifier, we are certainly not
18919 looking at a class-name. */
18920 if (identifier
== error_mark_node
)
18921 decl
= error_mark_node
;
18922 /* If we know this is a type-name, there's no need to look it
18924 else if (typename_p
)
18928 tree ambiguous_decls
;
18929 /* If we already know that this lookup is ambiguous, then
18930 we've already issued an error message; there's no reason
18934 cp_parser_simulate_error (parser
);
18935 return error_mark_node
;
18937 /* If the next token is a `::', then the name must be a type
18940 [basic.lookup.qual]
18942 During the lookup for a name preceding the :: scope
18943 resolution operator, object, function, and enumerator
18944 names are ignored. */
18945 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18946 tag_type
= typename_type
;
18947 /* Look up the name. */
18948 decl
= cp_parser_lookup_name (parser
, identifier
,
18950 /*is_template=*/false,
18951 /*is_namespace=*/false,
18952 check_dependency_p
,
18954 identifier_token
->location
);
18955 if (ambiguous_decls
)
18957 if (cp_parser_parsing_tentatively (parser
))
18958 cp_parser_simulate_error (parser
);
18959 return error_mark_node
;
18965 /* Try a template-id. */
18966 decl
= cp_parser_template_id (parser
, template_keyword_p
,
18967 check_dependency_p
,
18970 if (decl
== error_mark_node
)
18971 return error_mark_node
;
18974 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
18976 /* If this is a typename, create a TYPENAME_TYPE. */
18977 if (typename_p
&& decl
!= error_mark_node
)
18979 decl
= make_typename_type (scope
, decl
, typename_type
,
18980 /*complain=*/tf_error
);
18981 if (decl
!= error_mark_node
)
18982 decl
= TYPE_NAME (decl
);
18985 decl
= strip_using_decl (decl
);
18987 /* Check to see that it is really the name of a class. */
18988 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18989 && identifier_p (TREE_OPERAND (decl
, 0))
18990 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18991 /* Situations like this:
18993 template <typename T> struct A {
18994 typename T::template X<int>::I i;
18997 are problematic. Is `T::template X<int>' a class-name? The
18998 standard does not seem to be definitive, but there is no other
18999 valid interpretation of the following `::'. Therefore, those
19000 names are considered class-names. */
19002 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19003 if (decl
!= error_mark_node
)
19004 decl
= TYPE_NAME (decl
);
19006 else if (TREE_CODE (decl
) != TYPE_DECL
19007 || TREE_TYPE (decl
) == error_mark_node
19008 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19009 /* In Objective-C 2.0, a classname followed by '.' starts a
19010 dot-syntax expression, and it's not a type-name. */
19011 || (c_dialect_objc ()
19012 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19013 && objc_is_class_name (decl
)))
19014 decl
= error_mark_node
;
19016 if (decl
== error_mark_node
)
19017 cp_parser_error (parser
, "expected class-name");
19018 else if (identifier
&& !parser
->scope
)
19019 maybe_note_name_used_in_class (identifier
, decl
);
19024 /* Parse a class-specifier.
19027 class-head { member-specification [opt] }
19029 Returns the TREE_TYPE representing the class. */
19032 cp_parser_class_specifier_1 (cp_parser
* parser
)
19035 tree attributes
= NULL_TREE
;
19036 bool nested_name_specifier_p
;
19037 unsigned saved_num_template_parameter_lists
;
19038 bool saved_in_function_body
;
19039 unsigned char in_statement
;
19040 bool in_switch_statement_p
;
19041 bool saved_in_unbraced_linkage_specification_p
;
19042 tree old_scope
= NULL_TREE
;
19043 tree scope
= NULL_TREE
;
19044 cp_token
*closing_brace
;
19046 push_deferring_access_checks (dk_no_deferred
);
19048 /* Parse the class-head. */
19049 type
= cp_parser_class_head (parser
,
19050 &nested_name_specifier_p
);
19051 /* If the class-head was a semantic disaster, skip the entire body
19055 cp_parser_skip_to_end_of_block_or_statement (parser
);
19056 pop_deferring_access_checks ();
19057 return error_mark_node
;
19060 /* Look for the `{'. */
19061 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19063 pop_deferring_access_checks ();
19064 return error_mark_node
;
19067 cp_ensure_no_omp_declare_simd (parser
);
19069 /* Issue an error message if type-definitions are forbidden here. */
19070 cp_parser_check_type_definition (parser
);
19071 /* Remember that we are defining one more class. */
19072 ++parser
->num_classes_being_defined
;
19073 /* Inside the class, surrounding template-parameter-lists do not
19075 saved_num_template_parameter_lists
19076 = parser
->num_template_parameter_lists
;
19077 parser
->num_template_parameter_lists
= 0;
19078 /* We are not in a function body. */
19079 saved_in_function_body
= parser
->in_function_body
;
19080 parser
->in_function_body
= false;
19081 /* Or in a loop. */
19082 in_statement
= parser
->in_statement
;
19083 parser
->in_statement
= 0;
19084 /* Or in a switch. */
19085 in_switch_statement_p
= parser
->in_switch_statement_p
;
19086 parser
->in_switch_statement_p
= false;
19087 /* We are not immediately inside an extern "lang" block. */
19088 saved_in_unbraced_linkage_specification_p
19089 = parser
->in_unbraced_linkage_specification_p
;
19090 parser
->in_unbraced_linkage_specification_p
= false;
19092 /* Start the class. */
19093 if (nested_name_specifier_p
)
19095 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19096 old_scope
= push_inner_scope (scope
);
19098 type
= begin_class_definition (type
);
19100 if (type
== error_mark_node
)
19101 /* If the type is erroneous, skip the entire body of the class. */
19102 cp_parser_skip_to_closing_brace (parser
);
19104 /* Parse the member-specification. */
19105 cp_parser_member_specification_opt (parser
);
19107 /* Look for the trailing `}'. */
19108 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19109 /* Look for trailing attributes to apply to this class. */
19110 if (cp_parser_allow_gnu_extensions_p (parser
))
19111 attributes
= cp_parser_gnu_attributes_opt (parser
);
19112 if (type
!= error_mark_node
)
19113 type
= finish_struct (type
, attributes
);
19114 if (nested_name_specifier_p
)
19115 pop_inner_scope (old_scope
, scope
);
19117 /* We've finished a type definition. Check for the common syntax
19118 error of forgetting a semicolon after the definition. We need to
19119 be careful, as we can't just check for not-a-semicolon and be done
19120 with it; the user might have typed:
19122 class X { } c = ...;
19123 class X { } *p = ...;
19125 and so forth. Instead, enumerate all the possible tokens that
19126 might follow this production; if we don't see one of them, then
19127 complain and silently insert the semicolon. */
19129 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19130 bool want_semicolon
= true;
19132 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19133 /* Don't try to parse c++11 attributes here. As per the
19134 grammar, that should be a task for
19135 cp_parser_decl_specifier_seq. */
19136 want_semicolon
= false;
19138 switch (token
->type
)
19141 case CPP_SEMICOLON
:
19144 case CPP_OPEN_PAREN
:
19145 case CPP_CLOSE_PAREN
:
19147 want_semicolon
= false;
19150 /* While it's legal for type qualifiers and storage class
19151 specifiers to follow type definitions in the grammar, only
19152 compiler testsuites contain code like that. Assume that if
19153 we see such code, then what we're really seeing is a case
19157 const <type> var = ...;
19162 static <type> func (...) ...
19164 i.e. the qualifier or specifier applies to the next
19165 declaration. To do so, however, we need to look ahead one
19166 more token to see if *that* token is a type specifier.
19168 This code could be improved to handle:
19171 static const <type> var = ...; */
19173 if (keyword_is_decl_specifier (token
->keyword
))
19175 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19177 /* Handling user-defined types here would be nice, but very
19180 = (lookahead
->type
== CPP_KEYWORD
19181 && keyword_begins_type_specifier (lookahead
->keyword
));
19188 /* If we don't have a type, then something is very wrong and we
19189 shouldn't try to do anything clever. Likewise for not seeing the
19191 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19193 cp_token_position prev
19194 = cp_lexer_previous_token_position (parser
->lexer
);
19195 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19196 location_t loc
= prev_token
->location
;
19198 if (CLASSTYPE_DECLARED_CLASS (type
))
19199 error_at (loc
, "expected %<;%> after class definition");
19200 else if (TREE_CODE (type
) == RECORD_TYPE
)
19201 error_at (loc
, "expected %<;%> after struct definition");
19202 else if (TREE_CODE (type
) == UNION_TYPE
)
19203 error_at (loc
, "expected %<;%> after union definition");
19205 gcc_unreachable ();
19207 /* Unget one token and smash it to look as though we encountered
19208 a semicolon in the input stream. */
19209 cp_lexer_set_token_position (parser
->lexer
, prev
);
19210 token
= cp_lexer_peek_token (parser
->lexer
);
19211 token
->type
= CPP_SEMICOLON
;
19212 token
->keyword
= RID_MAX
;
19216 /* If this class is not itself within the scope of another class,
19217 then we need to parse the bodies of all of the queued function
19218 definitions. Note that the queued functions defined in a class
19219 are not always processed immediately following the
19220 class-specifier for that class. Consider:
19223 struct B { void f() { sizeof (A); } };
19226 If `f' were processed before the processing of `A' were
19227 completed, there would be no way to compute the size of `A'.
19228 Note that the nesting we are interested in here is lexical --
19229 not the semantic nesting given by TYPE_CONTEXT. In particular,
19232 struct A { struct B; };
19233 struct A::B { void f() { } };
19235 there is no need to delay the parsing of `A::B::f'. */
19236 if (--parser
->num_classes_being_defined
== 0)
19239 tree class_type
= NULL_TREE
;
19240 tree pushed_scope
= NULL_TREE
;
19242 cp_default_arg_entry
*e
;
19243 tree save_ccp
, save_ccr
;
19245 /* In a first pass, parse default arguments to the functions.
19246 Then, in a second pass, parse the bodies of the functions.
19247 This two-phased approach handles cases like:
19255 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
19258 /* If there are default arguments that have not yet been processed,
19259 take care of them now. */
19260 if (class_type
!= e
->class_type
)
19263 pop_scope (pushed_scope
);
19264 class_type
= e
->class_type
;
19265 pushed_scope
= push_scope (class_type
);
19267 /* Make sure that any template parameters are in scope. */
19268 maybe_begin_member_template_processing (decl
);
19269 /* Parse the default argument expressions. */
19270 cp_parser_late_parsing_default_args (parser
, decl
);
19271 /* Remove any template parameters from the symbol table. */
19272 maybe_end_member_template_processing ();
19274 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
19275 /* Now parse any NSDMIs. */
19276 save_ccp
= current_class_ptr
;
19277 save_ccr
= current_class_ref
;
19278 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
19280 if (class_type
!= DECL_CONTEXT (decl
))
19283 pop_scope (pushed_scope
);
19284 class_type
= DECL_CONTEXT (decl
);
19285 pushed_scope
= push_scope (class_type
);
19287 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
19288 cp_parser_late_parsing_nsdmi (parser
, decl
);
19290 vec_safe_truncate (unparsed_nsdmis
, 0);
19291 current_class_ptr
= save_ccp
;
19292 current_class_ref
= save_ccr
;
19294 pop_scope (pushed_scope
);
19295 /* Now parse the body of the functions. */
19298 /* OpenMP UDRs need to be parsed before all other functions. */
19299 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19300 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
19301 cp_parser_late_parsing_for_member (parser
, decl
);
19302 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19303 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
19304 cp_parser_late_parsing_for_member (parser
, decl
);
19307 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19308 cp_parser_late_parsing_for_member (parser
, decl
);
19309 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
19312 /* Put back any saved access checks. */
19313 pop_deferring_access_checks ();
19315 /* Restore saved state. */
19316 parser
->in_switch_statement_p
= in_switch_statement_p
;
19317 parser
->in_statement
= in_statement
;
19318 parser
->in_function_body
= saved_in_function_body
;
19319 parser
->num_template_parameter_lists
19320 = saved_num_template_parameter_lists
;
19321 parser
->in_unbraced_linkage_specification_p
19322 = saved_in_unbraced_linkage_specification_p
;
19328 cp_parser_class_specifier (cp_parser
* parser
)
19331 timevar_push (TV_PARSE_STRUCT
);
19332 ret
= cp_parser_class_specifier_1 (parser
);
19333 timevar_pop (TV_PARSE_STRUCT
);
19337 /* Parse a class-head.
19340 class-key identifier [opt] base-clause [opt]
19341 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19342 class-key nested-name-specifier [opt] template-id
19345 class-virt-specifier:
19349 class-key attributes identifier [opt] base-clause [opt]
19350 class-key attributes nested-name-specifier identifier base-clause [opt]
19351 class-key attributes nested-name-specifier [opt] template-id
19354 Upon return BASES is initialized to the list of base classes (or
19355 NULL, if there are none) in the same form returned by
19356 cp_parser_base_clause.
19358 Returns the TYPE of the indicated class. Sets
19359 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19360 involving a nested-name-specifier was used, and FALSE otherwise.
19362 Returns error_mark_node if this is not a class-head.
19364 Returns NULL_TREE if the class-head is syntactically valid, but
19365 semantically invalid in a way that means we should skip the entire
19366 body of the class. */
19369 cp_parser_class_head (cp_parser
* parser
,
19370 bool* nested_name_specifier_p
)
19372 tree nested_name_specifier
;
19373 enum tag_types class_key
;
19374 tree id
= NULL_TREE
;
19375 tree type
= NULL_TREE
;
19378 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
19379 bool template_id_p
= false;
19380 bool qualified_p
= false;
19381 bool invalid_nested_name_p
= false;
19382 bool invalid_explicit_specialization_p
= false;
19383 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
19384 tree pushed_scope
= NULL_TREE
;
19385 unsigned num_templates
;
19386 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
19387 /* Assume no nested-name-specifier will be present. */
19388 *nested_name_specifier_p
= false;
19389 /* Assume no template parameter lists will be used in defining the
19392 parser
->colon_corrects_to_scope_p
= false;
19394 /* Look for the class-key. */
19395 class_key
= cp_parser_class_key (parser
);
19396 if (class_key
== none_type
)
19397 return error_mark_node
;
19399 /* Parse the attributes. */
19400 attributes
= cp_parser_attributes_opt (parser
);
19402 /* If the next token is `::', that is invalid -- but sometimes
19403 people do try to write:
19407 Handle this gracefully by accepting the extra qualifier, and then
19408 issuing an error about it later if this really is a
19409 class-head. If it turns out just to be an elaborated type
19410 specifier, remain silent. */
19411 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
19412 qualified_p
= true;
19414 push_deferring_access_checks (dk_no_check
);
19416 /* Determine the name of the class. Begin by looking for an
19417 optional nested-name-specifier. */
19418 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
19419 nested_name_specifier
19420 = cp_parser_nested_name_specifier_opt (parser
,
19421 /*typename_keyword_p=*/false,
19422 /*check_dependency_p=*/false,
19424 /*is_declaration=*/false);
19425 /* If there was a nested-name-specifier, then there *must* be an
19427 if (nested_name_specifier
)
19429 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19430 /* Although the grammar says `identifier', it really means
19431 `class-name' or `template-name'. You are only allowed to
19432 define a class that has already been declared with this
19435 The proposed resolution for Core Issue 180 says that wherever
19436 you see `class T::X' you should treat `X' as a type-name.
19438 It is OK to define an inaccessible class; for example:
19440 class A { class B; };
19443 We do not know if we will see a class-name, or a
19444 template-name. We look for a class-name first, in case the
19445 class-name is a template-id; if we looked for the
19446 template-name first we would stop after the template-name. */
19447 cp_parser_parse_tentatively (parser
);
19448 type
= cp_parser_class_name (parser
,
19449 /*typename_keyword_p=*/false,
19450 /*template_keyword_p=*/false,
19452 /*check_dependency_p=*/false,
19453 /*class_head_p=*/true,
19454 /*is_declaration=*/false);
19455 /* If that didn't work, ignore the nested-name-specifier. */
19456 if (!cp_parser_parse_definitely (parser
))
19458 invalid_nested_name_p
= true;
19459 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19460 id
= cp_parser_identifier (parser
);
19461 if (id
== error_mark_node
)
19464 /* If we could not find a corresponding TYPE, treat this
19465 declaration like an unqualified declaration. */
19466 if (type
== error_mark_node
)
19467 nested_name_specifier
= NULL_TREE
;
19468 /* Otherwise, count the number of templates used in TYPE and its
19469 containing scopes. */
19474 for (scope
= TREE_TYPE (type
);
19475 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
19476 scope
= get_containing_scope (scope
))
19478 && CLASS_TYPE_P (scope
)
19479 && CLASSTYPE_TEMPLATE_INFO (scope
)
19480 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
19481 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
19482 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
19486 /* Otherwise, the identifier is optional. */
19489 /* We don't know whether what comes next is a template-id,
19490 an identifier, or nothing at all. */
19491 cp_parser_parse_tentatively (parser
);
19492 /* Check for a template-id. */
19493 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19494 id
= cp_parser_template_id (parser
,
19495 /*template_keyword_p=*/false,
19496 /*check_dependency_p=*/true,
19498 /*is_declaration=*/true);
19499 /* If that didn't work, it could still be an identifier. */
19500 if (!cp_parser_parse_definitely (parser
))
19502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19504 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19505 id
= cp_parser_identifier (parser
);
19512 template_id_p
= true;
19517 pop_deferring_access_checks ();
19521 cp_parser_check_for_invalid_template_id (parser
, id
,
19523 type_start_token
->location
);
19525 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19527 /* If it's not a `:' or a `{' then we can't really be looking at a
19528 class-head, since a class-head only appears as part of a
19529 class-specifier. We have to detect this situation before calling
19530 xref_tag, since that has irreversible side-effects. */
19531 if (!cp_parser_next_token_starts_class_definition_p (parser
))
19533 cp_parser_error (parser
, "expected %<{%> or %<:%>");
19534 type
= error_mark_node
;
19538 /* At this point, we're going ahead with the class-specifier, even
19539 if some other problem occurs. */
19540 cp_parser_commit_to_tentative_parse (parser
);
19541 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
19543 cp_parser_error (parser
,
19544 "cannot specify %<override%> for a class");
19545 type
= error_mark_node
;
19548 /* Issue the error about the overly-qualified name now. */
19551 cp_parser_error (parser
,
19552 "global qualification of class name is invalid");
19553 type
= error_mark_node
;
19556 else if (invalid_nested_name_p
)
19558 cp_parser_error (parser
,
19559 "qualified name does not name a class");
19560 type
= error_mark_node
;
19563 else if (nested_name_specifier
)
19567 /* Reject typedef-names in class heads. */
19568 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
19570 error_at (type_start_token
->location
,
19571 "invalid class name in declaration of %qD",
19577 /* Figure out in what scope the declaration is being placed. */
19578 scope
= current_scope ();
19579 /* If that scope does not contain the scope in which the
19580 class was originally declared, the program is invalid. */
19581 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
19583 if (at_namespace_scope_p ())
19584 error_at (type_start_token
->location
,
19585 "declaration of %qD in namespace %qD which does not "
19587 type
, scope
, nested_name_specifier
);
19589 error_at (type_start_token
->location
,
19590 "declaration of %qD in %qD which does not enclose %qD",
19591 type
, scope
, nested_name_specifier
);
19597 A declarator-id shall not be qualified except for the
19598 definition of a ... nested class outside of its class
19599 ... [or] the definition or explicit instantiation of a
19600 class member of a namespace outside of its namespace. */
19601 if (scope
== nested_name_specifier
)
19603 permerror (nested_name_specifier_token_start
->location
,
19604 "extra qualification not allowed");
19605 nested_name_specifier
= NULL_TREE
;
19609 /* An explicit-specialization must be preceded by "template <>". If
19610 it is not, try to recover gracefully. */
19611 if (at_namespace_scope_p ()
19612 && parser
->num_template_parameter_lists
== 0
19615 error_at (type_start_token
->location
,
19616 "an explicit specialization must be preceded by %<template <>%>");
19617 invalid_explicit_specialization_p
= true;
19618 /* Take the same action that would have been taken by
19619 cp_parser_explicit_specialization. */
19620 ++parser
->num_template_parameter_lists
;
19621 begin_specialization ();
19623 /* There must be no "return" statements between this point and the
19624 end of this function; set "type "to the correct return value and
19625 use "goto done;" to return. */
19626 /* Make sure that the right number of template parameters were
19628 if (!cp_parser_check_template_parameters (parser
, num_templates
,
19629 type_start_token
->location
,
19630 /*declarator=*/NULL
))
19632 /* If something went wrong, there is no point in even trying to
19633 process the class-definition. */
19638 /* Look up the type. */
19641 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
19642 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
19643 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
19645 error_at (type_start_token
->location
,
19646 "function template %qD redeclared as a class template", id
);
19647 type
= error_mark_node
;
19651 type
= TREE_TYPE (id
);
19652 type
= maybe_process_partial_specialization (type
);
19654 if (nested_name_specifier
)
19655 pushed_scope
= push_scope (nested_name_specifier
);
19657 else if (nested_name_specifier
)
19663 template <typename T> struct S { struct T };
19664 template <typename T> struct S<T>::T { };
19666 we will get a TYPENAME_TYPE when processing the definition of
19667 `S::T'. We need to resolve it to the actual type before we
19668 try to define it. */
19669 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
19671 class_type
= resolve_typename_type (TREE_TYPE (type
),
19672 /*only_current_p=*/false);
19673 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
19674 type
= TYPE_NAME (class_type
);
19677 cp_parser_error (parser
, "could not resolve typename type");
19678 type
= error_mark_node
;
19682 if (maybe_process_partial_specialization (TREE_TYPE (type
))
19683 == error_mark_node
)
19689 class_type
= current_class_type
;
19690 /* Enter the scope indicated by the nested-name-specifier. */
19691 pushed_scope
= push_scope (nested_name_specifier
);
19692 /* Get the canonical version of this type. */
19693 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
19694 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19695 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
19697 type
= push_template_decl (type
);
19698 if (type
== error_mark_node
)
19705 type
= TREE_TYPE (type
);
19706 *nested_name_specifier_p
= true;
19708 else /* The name is not a nested name. */
19710 /* If the class was unnamed, create a dummy name. */
19712 id
= make_anon_name ();
19713 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
19714 parser
->num_template_parameter_lists
);
19717 /* Indicate whether this class was declared as a `class' or as a
19719 if (TREE_CODE (type
) == RECORD_TYPE
)
19720 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
19721 cp_parser_check_class_key (class_key
, type
);
19723 /* If this type was already complete, and we see another definition,
19724 that's an error. */
19725 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
19727 error_at (type_start_token
->location
, "redefinition of %q#T",
19729 error_at (type_start_token
->location
, "previous definition of %q+#T",
19734 else if (type
== error_mark_node
)
19739 /* Apply attributes now, before any use of the class as a template
19740 argument in its base list. */
19741 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
19742 fixup_attribute_variants (type
);
19745 /* We will have entered the scope containing the class; the names of
19746 base classes should be looked up in that context. For example:
19748 struct A { struct B {}; struct C; };
19749 struct A::C : B {};
19753 /* Get the list of base-classes, if there is one. */
19754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19755 bases
= cp_parser_base_clause (parser
);
19759 /* If we're really defining a class, process the base classes.
19760 If they're invalid, fail. */
19761 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
19762 && !xref_basetypes (type
, bases
))
19766 /* Leave the scope given by the nested-name-specifier. We will
19767 enter the class scope itself while processing the members. */
19769 pop_scope (pushed_scope
);
19771 if (invalid_explicit_specialization_p
)
19773 end_specialization ();
19774 --parser
->num_template_parameter_lists
;
19778 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
19779 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
19780 CLASSTYPE_FINAL (type
) = 1;
19782 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19786 /* Parse a class-key.
19793 Returns the kind of class-key specified, or none_type to indicate
19796 static enum tag_types
19797 cp_parser_class_key (cp_parser
* parser
)
19800 enum tag_types tag_type
;
19802 /* Look for the class-key. */
19803 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
19807 /* Check to see if the TOKEN is a class-key. */
19808 tag_type
= cp_parser_token_is_class_key (token
);
19810 cp_parser_error (parser
, "expected class-key");
19814 /* Parse an (optional) member-specification.
19816 member-specification:
19817 member-declaration member-specification [opt]
19818 access-specifier : member-specification [opt] */
19821 cp_parser_member_specification_opt (cp_parser
* parser
)
19828 /* Peek at the next token. */
19829 token
= cp_lexer_peek_token (parser
->lexer
);
19830 /* If it's a `}', or EOF then we've seen all the members. */
19831 if (token
->type
== CPP_CLOSE_BRACE
19832 || token
->type
== CPP_EOF
19833 || token
->type
== CPP_PRAGMA_EOL
)
19836 /* See if this token is a keyword. */
19837 keyword
= token
->keyword
;
19841 case RID_PROTECTED
:
19843 /* Consume the access-specifier. */
19844 cp_lexer_consume_token (parser
->lexer
);
19845 /* Remember which access-specifier is active. */
19846 current_access_specifier
= token
->u
.value
;
19847 /* Look for the `:'. */
19848 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
19852 /* Accept #pragmas at class scope. */
19853 if (token
->type
== CPP_PRAGMA
)
19855 cp_parser_pragma (parser
, pragma_member
);
19859 /* Otherwise, the next construction must be a
19860 member-declaration. */
19861 cp_parser_member_declaration (parser
);
19866 /* Parse a member-declaration.
19868 member-declaration:
19869 decl-specifier-seq [opt] member-declarator-list [opt] ;
19870 function-definition ; [opt]
19871 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19873 template-declaration
19876 member-declarator-list:
19878 member-declarator-list , member-declarator
19881 declarator pure-specifier [opt]
19882 declarator constant-initializer [opt]
19883 identifier [opt] : constant-expression
19887 member-declaration:
19888 __extension__ member-declaration
19891 declarator attributes [opt] pure-specifier [opt]
19892 declarator attributes [opt] constant-initializer [opt]
19893 identifier [opt] attributes [opt] : constant-expression
19897 member-declaration:
19898 static_assert-declaration */
19901 cp_parser_member_declaration (cp_parser
* parser
)
19903 cp_decl_specifier_seq decl_specifiers
;
19904 tree prefix_attributes
;
19906 int declares_class_or_enum
;
19908 cp_token
*token
= NULL
;
19909 cp_token
*decl_spec_token_start
= NULL
;
19910 cp_token
*initializer_token_start
= NULL
;
19911 int saved_pedantic
;
19912 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
19914 /* Check for the `__extension__' keyword. */
19915 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
19918 cp_parser_member_declaration (parser
);
19919 /* Restore the old value of the PEDANTIC flag. */
19920 pedantic
= saved_pedantic
;
19925 /* Check for a template-declaration. */
19926 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
19928 /* An explicit specialization here is an error condition, and we
19929 expect the specialization handler to detect and report this. */
19930 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
19931 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
19932 cp_parser_explicit_specialization (parser
);
19934 cp_parser_template_declaration (parser
, /*member_p=*/true);
19939 /* Check for a using-declaration. */
19940 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
19942 if (cxx_dialect
< cxx11
)
19944 /* Parse the using-declaration. */
19945 cp_parser_using_declaration (parser
,
19946 /*access_declaration_p=*/false);
19952 bool alias_decl_expected
;
19953 cp_parser_parse_tentatively (parser
);
19954 decl
= cp_parser_alias_declaration (parser
);
19955 /* Note that if we actually see the '=' token after the
19956 identifier, cp_parser_alias_declaration commits the
19957 tentative parse. In that case, we really expects an
19958 alias-declaration. Otherwise, we expect a using
19960 alias_decl_expected
=
19961 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
19962 cp_parser_parse_definitely (parser
);
19964 if (alias_decl_expected
)
19965 finish_member_declaration (decl
);
19967 cp_parser_using_declaration (parser
,
19968 /*access_declaration_p=*/false);
19973 /* Check for @defs. */
19974 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
19977 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
19978 ivar
= ivar_chains
;
19982 ivar
= TREE_CHAIN (member
);
19983 TREE_CHAIN (member
) = NULL_TREE
;
19984 finish_member_declaration (member
);
19989 /* If the next token is `static_assert' we have a static assertion. */
19990 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
19992 cp_parser_static_assert (parser
, /*member_p=*/true);
19996 parser
->colon_corrects_to_scope_p
= false;
19998 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20001 /* Parse the decl-specifier-seq. */
20002 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20003 cp_parser_decl_specifier_seq (parser
,
20004 CP_PARSER_FLAGS_OPTIONAL
,
20006 &declares_class_or_enum
);
20007 /* Check for an invalid type-name. */
20008 if (!decl_specifiers
.any_type_specifiers_p
20009 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20011 /* If there is no declarator, then the decl-specifier-seq should
20013 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20015 /* If there was no decl-specifier-seq, and the next token is a
20016 `;', then we have something like:
20022 Each member-declaration shall declare at least one member
20023 name of the class. */
20024 if (!decl_specifiers
.any_specifiers_p
)
20026 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20027 if (!in_system_header_at (token
->location
))
20028 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20034 /* See if this declaration is a friend. */
20035 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20036 /* If there were decl-specifiers, check to see if there was
20037 a class-declaration. */
20038 type
= check_tag_decl (&decl_specifiers
,
20039 /*explicit_type_instantiation_p=*/false);
20040 /* Nested classes have already been added to the class, but
20041 a `friend' needs to be explicitly registered. */
20044 /* If the `friend' keyword was present, the friend must
20045 be introduced with a class-key. */
20046 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20047 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20048 "in C++03 a class-key must be used "
20049 "when declaring a friend");
20052 template <typename T> struct A {
20053 friend struct A<T>::B;
20056 A<T>::B will be represented by a TYPENAME_TYPE, and
20057 therefore not recognized by check_tag_decl. */
20060 type
= decl_specifiers
.type
;
20061 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20062 type
= TREE_TYPE (type
);
20064 if (!type
|| !TYPE_P (type
))
20065 error_at (decl_spec_token_start
->location
,
20066 "friend declaration does not name a class or "
20069 make_friend_class (current_class_type
, type
,
20070 /*complain=*/true);
20072 /* If there is no TYPE, an error message will already have
20074 else if (!type
|| type
== error_mark_node
)
20076 /* An anonymous aggregate has to be handled specially; such
20077 a declaration really declares a data member (with a
20078 particular type), as opposed to a nested class. */
20079 else if (ANON_AGGR_TYPE_P (type
))
20082 if (decl_specifiers
.storage_class
!= sc_none
)
20083 error_at (decl_spec_token_start
->location
,
20084 "a storage class on an anonymous aggregate "
20085 "in class scope is not allowed");
20087 /* Remove constructors and such from TYPE, now that we
20088 know it is an anonymous aggregate. */
20089 fixup_anonymous_aggr (type
);
20090 /* And make the corresponding data member. */
20091 decl
= build_decl (decl_spec_token_start
->location
,
20092 FIELD_DECL
, NULL_TREE
, type
);
20093 /* Add it to the class. */
20094 finish_member_declaration (decl
);
20097 cp_parser_check_access_in_redeclaration
20099 decl_spec_token_start
->location
);
20104 bool assume_semicolon
= false;
20106 /* Clear attributes from the decl_specifiers but keep them
20107 around as prefix attributes that apply them to the entity
20109 prefix_attributes
= decl_specifiers
.attributes
;
20110 decl_specifiers
.attributes
= NULL_TREE
;
20112 /* See if these declarations will be friends. */
20113 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20115 /* Keep going until we hit the `;' at the end of the
20117 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20119 tree attributes
= NULL_TREE
;
20120 tree first_attribute
;
20122 /* Peek at the next token. */
20123 token
= cp_lexer_peek_token (parser
->lexer
);
20125 /* Check for a bitfield declaration. */
20126 if (token
->type
== CPP_COLON
20127 || (token
->type
== CPP_NAME
20128 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20134 /* Get the name of the bitfield. Note that we cannot just
20135 check TOKEN here because it may have been invalidated by
20136 the call to cp_lexer_peek_nth_token above. */
20137 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20138 identifier
= cp_parser_identifier (parser
);
20140 identifier
= NULL_TREE
;
20142 /* Consume the `:' token. */
20143 cp_lexer_consume_token (parser
->lexer
);
20144 /* Get the width of the bitfield. */
20146 = cp_parser_constant_expression (parser
,
20147 /*allow_non_constant=*/false,
20150 /* Look for attributes that apply to the bitfield. */
20151 attributes
= cp_parser_attributes_opt (parser
);
20152 /* Remember which attributes are prefix attributes and
20154 first_attribute
= attributes
;
20155 /* Combine the attributes. */
20156 attributes
= chainon (prefix_attributes
, attributes
);
20158 /* Create the bitfield declaration. */
20159 decl
= grokbitfield (identifier
20160 ? make_id_declarator (NULL_TREE
,
20170 cp_declarator
*declarator
;
20172 tree asm_specification
;
20173 int ctor_dtor_or_conv_p
;
20175 /* Parse the declarator. */
20177 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20178 &ctor_dtor_or_conv_p
,
20179 /*parenthesized_p=*/NULL
,
20180 /*member_p=*/true);
20182 /* If something went wrong parsing the declarator, make sure
20183 that we at least consume some tokens. */
20184 if (declarator
== cp_error_declarator
)
20186 /* Skip to the end of the statement. */
20187 cp_parser_skip_to_end_of_statement (parser
);
20188 /* If the next token is not a semicolon, that is
20189 probably because we just skipped over the body of
20190 a function. So, we consume a semicolon if
20191 present, but do not issue an error message if it
20193 if (cp_lexer_next_token_is (parser
->lexer
,
20195 cp_lexer_consume_token (parser
->lexer
);
20199 if (declares_class_or_enum
& 2)
20200 cp_parser_check_for_definition_in_return_type
20201 (declarator
, decl_specifiers
.type
,
20202 decl_specifiers
.locations
[ds_type_spec
]);
20204 /* Look for an asm-specification. */
20205 asm_specification
= cp_parser_asm_specification_opt (parser
);
20206 /* Look for attributes that apply to the declaration. */
20207 attributes
= cp_parser_attributes_opt (parser
);
20208 /* Remember which attributes are prefix attributes and
20210 first_attribute
= attributes
;
20211 /* Combine the attributes. */
20212 attributes
= chainon (prefix_attributes
, attributes
);
20214 /* If it's an `=', then we have a constant-initializer or a
20215 pure-specifier. It is not correct to parse the
20216 initializer before registering the member declaration
20217 since the member declaration should be in scope while
20218 its initializer is processed. However, the rest of the
20219 front end does not yet provide an interface that allows
20220 us to handle this correctly. */
20221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20225 A pure-specifier shall be used only in the declaration of
20226 a virtual function.
20228 A member-declarator can contain a constant-initializer
20229 only if it declares a static member of integral or
20232 Therefore, if the DECLARATOR is for a function, we look
20233 for a pure-specifier; otherwise, we look for a
20234 constant-initializer. When we call `grokfield', it will
20235 perform more stringent semantics checks. */
20236 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
20237 if (function_declarator_p (declarator
)
20238 || (decl_specifiers
.type
20239 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
20240 && declarator
->kind
== cdk_id
20241 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
20242 == FUNCTION_TYPE
)))
20243 initializer
= cp_parser_pure_specifier (parser
);
20244 else if (decl_specifiers
.storage_class
!= sc_static
)
20245 initializer
= cp_parser_save_nsdmi (parser
);
20246 else if (cxx_dialect
>= cxx11
)
20249 /* Don't require a constant rvalue in C++11, since we
20250 might want a reference constant. We'll enforce
20251 constancy later. */
20252 cp_lexer_consume_token (parser
->lexer
);
20253 /* Parse the initializer. */
20254 initializer
= cp_parser_initializer_clause (parser
,
20258 /* Parse the initializer. */
20259 initializer
= cp_parser_constant_initializer (parser
);
20261 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20262 && !function_declarator_p (declarator
))
20265 if (decl_specifiers
.storage_class
!= sc_static
)
20266 initializer
= cp_parser_save_nsdmi (parser
);
20268 initializer
= cp_parser_initializer (parser
, &x
, &x
);
20270 /* Otherwise, there is no initializer. */
20272 initializer
= NULL_TREE
;
20274 /* See if we are probably looking at a function
20275 definition. We are certainly not looking at a
20276 member-declarator. Calling `grokfield' has
20277 side-effects, so we must not do it unless we are sure
20278 that we are looking at a member-declarator. */
20279 if (cp_parser_token_starts_function_definition_p
20280 (cp_lexer_peek_token (parser
->lexer
)))
20282 /* The grammar does not allow a pure-specifier to be
20283 used when a member function is defined. (It is
20284 possible that this fact is an oversight in the
20285 standard, since a pure function may be defined
20286 outside of the class-specifier. */
20287 if (initializer
&& initializer_token_start
)
20288 error_at (initializer_token_start
->location
,
20289 "pure-specifier on function-definition");
20290 decl
= cp_parser_save_member_function_body (parser
,
20294 /* If the member was not a friend, declare it here. */
20297 if (parser
->fully_implicit_function_template_p
)
20298 decl
= finish_fully_implicit_template (parser
, decl
);
20299 finish_member_declaration (decl
);
20301 /* Peek at the next token. */
20302 token
= cp_lexer_peek_token (parser
->lexer
);
20303 /* If the next token is a semicolon, consume it. */
20304 if (token
->type
== CPP_SEMICOLON
)
20305 cp_lexer_consume_token (parser
->lexer
);
20309 if (declarator
->kind
== cdk_function
)
20310 declarator
->id_loc
= token
->location
;
20311 /* Create the declaration. */
20312 decl
= grokfield (declarator
, &decl_specifiers
,
20313 initializer
, /*init_const_expr_p=*/true,
20314 asm_specification
, attributes
);
20315 if (parser
->fully_implicit_function_template_p
)
20316 decl
= finish_fully_implicit_template (parser
, decl
);
20319 cp_finalize_omp_declare_simd (parser
, decl
);
20321 /* Reset PREFIX_ATTRIBUTES. */
20322 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
20323 attributes
= TREE_CHAIN (attributes
);
20325 TREE_CHAIN (attributes
) = NULL_TREE
;
20327 /* If there is any qualification still in effect, clear it
20328 now; we will be starting fresh with the next declarator. */
20329 parser
->scope
= NULL_TREE
;
20330 parser
->qualifying_scope
= NULL_TREE
;
20331 parser
->object_scope
= NULL_TREE
;
20332 /* If it's a `,', then there are more declarators. */
20333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20335 cp_lexer_consume_token (parser
->lexer
);
20336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20338 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
20339 error_at (token
->location
,
20340 "stray %<,%> at end of member declaration");
20343 /* If the next token isn't a `;', then we have a parse error. */
20344 else if (cp_lexer_next_token_is_not (parser
->lexer
,
20347 /* The next token might be a ways away from where the
20348 actual semicolon is missing. Find the previous token
20349 and use that for our error position. */
20350 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
20351 error_at (token
->location
,
20352 "expected %<;%> at end of member declaration");
20354 /* Assume that the user meant to provide a semicolon. If
20355 we were to cp_parser_skip_to_end_of_statement, we might
20356 skip to a semicolon inside a member function definition
20357 and issue nonsensical error messages. */
20358 assume_semicolon
= true;
20363 /* Add DECL to the list of members. */
20365 finish_member_declaration (decl
);
20367 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20368 cp_parser_save_default_args (parser
, decl
);
20369 else if (TREE_CODE (decl
) == FIELD_DECL
20370 && !DECL_C_BIT_FIELD (decl
)
20371 && DECL_INITIAL (decl
))
20372 /* Add DECL to the queue of NSDMI to be parsed later. */
20373 vec_safe_push (unparsed_nsdmis
, decl
);
20376 if (assume_semicolon
)
20381 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20383 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20386 /* Parse a pure-specifier.
20391 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20392 Otherwise, ERROR_MARK_NODE is returned. */
20395 cp_parser_pure_specifier (cp_parser
* parser
)
20399 /* Look for the `=' token. */
20400 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
20401 return error_mark_node
;
20402 /* Look for the `0' token. */
20403 token
= cp_lexer_peek_token (parser
->lexer
);
20405 if (token
->type
== CPP_EOF
20406 || token
->type
== CPP_PRAGMA_EOL
)
20407 return error_mark_node
;
20409 cp_lexer_consume_token (parser
->lexer
);
20411 /* Accept = default or = delete in c++0x mode. */
20412 if (token
->keyword
== RID_DEFAULT
20413 || token
->keyword
== RID_DELETE
)
20415 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
20416 return token
->u
.value
;
20419 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20420 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
20422 cp_parser_error (parser
,
20423 "invalid pure specifier (only %<= 0%> is allowed)");
20424 cp_parser_skip_to_end_of_statement (parser
);
20425 return error_mark_node
;
20427 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20429 error_at (token
->location
, "templates may not be %<virtual%>");
20430 return error_mark_node
;
20433 return integer_zero_node
;
20436 /* Parse a constant-initializer.
20438 constant-initializer:
20439 = constant-expression
20441 Returns a representation of the constant-expression. */
20444 cp_parser_constant_initializer (cp_parser
* parser
)
20446 /* Look for the `=' token. */
20447 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
20448 return error_mark_node
;
20450 /* It is invalid to write:
20452 struct S { static const int i = { 7 }; };
20455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20457 cp_parser_error (parser
,
20458 "a brace-enclosed initializer is not allowed here");
20459 /* Consume the opening brace. */
20460 cp_lexer_consume_token (parser
->lexer
);
20461 /* Skip the initializer. */
20462 cp_parser_skip_to_closing_brace (parser
);
20463 /* Look for the trailing `}'. */
20464 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20466 return error_mark_node
;
20469 return cp_parser_constant_expression (parser
,
20470 /*allow_non_constant=*/false,
20474 /* Derived classes [gram.class.derived] */
20476 /* Parse a base-clause.
20479 : base-specifier-list
20481 base-specifier-list:
20482 base-specifier ... [opt]
20483 base-specifier-list , base-specifier ... [opt]
20485 Returns a TREE_LIST representing the base-classes, in the order in
20486 which they were declared. The representation of each node is as
20487 described by cp_parser_base_specifier.
20489 In the case that no bases are specified, this function will return
20490 NULL_TREE, not ERROR_MARK_NODE. */
20493 cp_parser_base_clause (cp_parser
* parser
)
20495 tree bases
= NULL_TREE
;
20497 /* Look for the `:' that begins the list. */
20498 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20500 /* Scan the base-specifier-list. */
20505 bool pack_expansion_p
= false;
20507 /* Look for the base-specifier. */
20508 base
= cp_parser_base_specifier (parser
);
20509 /* Look for the (optional) ellipsis. */
20510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20512 /* Consume the `...'. */
20513 cp_lexer_consume_token (parser
->lexer
);
20515 pack_expansion_p
= true;
20518 /* Add BASE to the front of the list. */
20519 if (base
&& base
!= error_mark_node
)
20521 if (pack_expansion_p
)
20522 /* Make this a pack expansion type. */
20523 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
20525 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
20527 TREE_CHAIN (base
) = bases
;
20531 /* Peek at the next token. */
20532 token
= cp_lexer_peek_token (parser
->lexer
);
20533 /* If it's not a comma, then the list is complete. */
20534 if (token
->type
!= CPP_COMMA
)
20536 /* Consume the `,'. */
20537 cp_lexer_consume_token (parser
->lexer
);
20540 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20541 base class had a qualified name. However, the next name that
20542 appears is certainly not qualified. */
20543 parser
->scope
= NULL_TREE
;
20544 parser
->qualifying_scope
= NULL_TREE
;
20545 parser
->object_scope
= NULL_TREE
;
20547 return nreverse (bases
);
20550 /* Parse a base-specifier.
20553 :: [opt] nested-name-specifier [opt] class-name
20554 virtual access-specifier [opt] :: [opt] nested-name-specifier
20556 access-specifier virtual [opt] :: [opt] nested-name-specifier
20559 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20560 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20561 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20562 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20565 cp_parser_base_specifier (cp_parser
* parser
)
20569 bool virtual_p
= false;
20570 bool duplicate_virtual_error_issued_p
= false;
20571 bool duplicate_access_error_issued_p
= false;
20572 bool class_scope_p
, template_p
;
20573 tree access
= access_default_node
;
20576 /* Process the optional `virtual' and `access-specifier'. */
20579 /* Peek at the next token. */
20580 token
= cp_lexer_peek_token (parser
->lexer
);
20581 /* Process `virtual'. */
20582 switch (token
->keyword
)
20585 /* If `virtual' appears more than once, issue an error. */
20586 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
20588 cp_parser_error (parser
,
20589 "%<virtual%> specified more than once in base-specified");
20590 duplicate_virtual_error_issued_p
= true;
20595 /* Consume the `virtual' token. */
20596 cp_lexer_consume_token (parser
->lexer
);
20601 case RID_PROTECTED
:
20603 /* If more than one access specifier appears, issue an
20605 if (access
!= access_default_node
20606 && !duplicate_access_error_issued_p
)
20608 cp_parser_error (parser
,
20609 "more than one access specifier in base-specified");
20610 duplicate_access_error_issued_p
= true;
20613 access
= ridpointers
[(int) token
->keyword
];
20615 /* Consume the access-specifier. */
20616 cp_lexer_consume_token (parser
->lexer
);
20625 /* It is not uncommon to see programs mechanically, erroneously, use
20626 the 'typename' keyword to denote (dependent) qualified types
20627 as base classes. */
20628 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
20630 token
= cp_lexer_peek_token (parser
->lexer
);
20631 if (!processing_template_decl
)
20632 error_at (token
->location
,
20633 "keyword %<typename%> not allowed outside of templates");
20635 error_at (token
->location
,
20636 "keyword %<typename%> not allowed in this context "
20637 "(the base class is implicitly a type)");
20638 cp_lexer_consume_token (parser
->lexer
);
20641 /* Look for the optional `::' operator. */
20642 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
20643 /* Look for the nested-name-specifier. The simplest way to
20648 The keyword `typename' is not permitted in a base-specifier or
20649 mem-initializer; in these contexts a qualified name that
20650 depends on a template-parameter is implicitly assumed to be a
20653 is to pretend that we have seen the `typename' keyword at this
20655 cp_parser_nested_name_specifier_opt (parser
,
20656 /*typename_keyword_p=*/true,
20657 /*check_dependency_p=*/true,
20659 /*is_declaration=*/true);
20660 /* If the base class is given by a qualified name, assume that names
20661 we see are type names or templates, as appropriate. */
20662 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
20663 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
20666 && cp_lexer_next_token_is_decltype (parser
->lexer
))
20667 /* DR 950 allows decltype as a base-specifier. */
20668 type
= cp_parser_decltype (parser
);
20671 /* Otherwise, look for the class-name. */
20672 type
= cp_parser_class_name (parser
,
20676 /*check_dependency_p=*/true,
20677 /*class_head_p=*/false,
20678 /*is_declaration=*/true);
20679 type
= TREE_TYPE (type
);
20682 if (type
== error_mark_node
)
20683 return error_mark_node
;
20685 return finish_base_specifier (type
, access
, virtual_p
);
20688 /* Exception handling [gram.exception] */
20690 /* Parse an (optional) noexcept-specification.
20692 noexcept-specification:
20693 noexcept ( constant-expression ) [opt]
20695 If no noexcept-specification is present, returns NULL_TREE.
20696 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20697 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20698 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20699 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20700 in which case a boolean condition is returned instead. */
20703 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
20704 bool require_constexpr
,
20705 bool* consumed_expr
,
20709 const char *saved_message
;
20711 /* Peek at the next token. */
20712 token
= cp_lexer_peek_token (parser
->lexer
);
20714 /* Is it a noexcept-specification? */
20715 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
20718 cp_lexer_consume_token (parser
->lexer
);
20720 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
20722 cp_lexer_consume_token (parser
->lexer
);
20724 if (require_constexpr
)
20726 /* Types may not be defined in an exception-specification. */
20727 saved_message
= parser
->type_definition_forbidden_message
;
20728 parser
->type_definition_forbidden_message
20729 = G_("types may not be defined in an exception-specification");
20731 expr
= cp_parser_constant_expression (parser
, false, NULL
);
20733 /* Restore the saved message. */
20734 parser
->type_definition_forbidden_message
= saved_message
;
20738 expr
= cp_parser_expression (parser
, false, NULL
);
20739 *consumed_expr
= true;
20742 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20746 expr
= boolean_true_node
;
20747 if (!require_constexpr
)
20748 *consumed_expr
= false;
20751 /* We cannot build a noexcept-spec right away because this will check
20752 that expr is a constexpr. */
20754 return build_noexcept_spec (expr
, tf_warning_or_error
);
20762 /* Parse an (optional) exception-specification.
20764 exception-specification:
20765 throw ( type-id-list [opt] )
20767 Returns a TREE_LIST representing the exception-specification. The
20768 TREE_VALUE of each node is a type. */
20771 cp_parser_exception_specification_opt (cp_parser
* parser
)
20775 const char *saved_message
;
20777 /* Peek at the next token. */
20778 token
= cp_lexer_peek_token (parser
->lexer
);
20780 /* Is it a noexcept-specification? */
20781 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
20783 if (type_id_list
!= NULL_TREE
)
20784 return type_id_list
;
20786 /* If it's not `throw', then there's no exception-specification. */
20787 if (!cp_parser_is_keyword (token
, RID_THROW
))
20791 /* Enable this once a lot of code has transitioned to noexcept? */
20792 if (cxx_dialect
>= cxx11
&& !in_system_header
)
20793 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
20794 "deprecated in C++0x; use %<noexcept%> instead");
20797 /* Consume the `throw'. */
20798 cp_lexer_consume_token (parser
->lexer
);
20800 /* Look for the `('. */
20801 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20803 /* Peek at the next token. */
20804 token
= cp_lexer_peek_token (parser
->lexer
);
20805 /* If it's not a `)', then there is a type-id-list. */
20806 if (token
->type
!= CPP_CLOSE_PAREN
)
20808 /* Types may not be defined in an exception-specification. */
20809 saved_message
= parser
->type_definition_forbidden_message
;
20810 parser
->type_definition_forbidden_message
20811 = G_("types may not be defined in an exception-specification");
20812 /* Parse the type-id-list. */
20813 type_id_list
= cp_parser_type_id_list (parser
);
20814 /* Restore the saved message. */
20815 parser
->type_definition_forbidden_message
= saved_message
;
20818 type_id_list
= empty_except_spec
;
20820 /* Look for the `)'. */
20821 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20823 return type_id_list
;
20826 /* Parse an (optional) type-id-list.
20830 type-id-list , type-id ... [opt]
20832 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20833 in the order that the types were presented. */
20836 cp_parser_type_id_list (cp_parser
* parser
)
20838 tree types
= NULL_TREE
;
20845 /* Get the next type-id. */
20846 type
= cp_parser_type_id (parser
);
20847 /* Parse the optional ellipsis. */
20848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20850 /* Consume the `...'. */
20851 cp_lexer_consume_token (parser
->lexer
);
20853 /* Turn the type into a pack expansion expression. */
20854 type
= make_pack_expansion (type
);
20856 /* Add it to the list. */
20857 types
= add_exception_specifier (types
, type
, /*complain=*/1);
20858 /* Peek at the next token. */
20859 token
= cp_lexer_peek_token (parser
->lexer
);
20860 /* If it is not a `,', we are done. */
20861 if (token
->type
!= CPP_COMMA
)
20863 /* Consume the `,'. */
20864 cp_lexer_consume_token (parser
->lexer
);
20867 return nreverse (types
);
20870 /* Parse a try-block.
20873 try compound-statement handler-seq */
20876 cp_parser_try_block (cp_parser
* parser
)
20880 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
20881 try_block
= begin_try_block ();
20882 cp_parser_compound_statement (parser
, NULL
, true, false);
20883 finish_try_block (try_block
);
20884 cp_parser_handler_seq (parser
);
20885 finish_handler_sequence (try_block
);
20890 /* Parse a function-try-block.
20892 function-try-block:
20893 try ctor-initializer [opt] function-body handler-seq */
20896 cp_parser_function_try_block (cp_parser
* parser
)
20898 tree compound_stmt
;
20900 bool ctor_initializer_p
;
20902 /* Look for the `try' keyword. */
20903 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
20905 /* Let the rest of the front end know where we are. */
20906 try_block
= begin_function_try_block (&compound_stmt
);
20907 /* Parse the function-body. */
20908 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
20909 (parser
, /*in_function_try_block=*/true);
20910 /* We're done with the `try' part. */
20911 finish_function_try_block (try_block
);
20912 /* Parse the handlers. */
20913 cp_parser_handler_seq (parser
);
20914 /* We're done with the handlers. */
20915 finish_function_handler_sequence (try_block
, compound_stmt
);
20917 return ctor_initializer_p
;
20920 /* Parse a handler-seq.
20923 handler handler-seq [opt] */
20926 cp_parser_handler_seq (cp_parser
* parser
)
20932 /* Parse the handler. */
20933 cp_parser_handler (parser
);
20934 /* Peek at the next token. */
20935 token
= cp_lexer_peek_token (parser
->lexer
);
20936 /* If it's not `catch' then there are no more handlers. */
20937 if (!cp_parser_is_keyword (token
, RID_CATCH
))
20942 /* Parse a handler.
20945 catch ( exception-declaration ) compound-statement */
20948 cp_parser_handler (cp_parser
* parser
)
20953 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
20954 handler
= begin_handler ();
20955 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20956 declaration
= cp_parser_exception_declaration (parser
);
20957 finish_handler_parms (declaration
, handler
);
20958 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20959 cp_parser_compound_statement (parser
, NULL
, false, false);
20960 finish_handler (handler
);
20963 /* Parse an exception-declaration.
20965 exception-declaration:
20966 type-specifier-seq declarator
20967 type-specifier-seq abstract-declarator
20971 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20972 ellipsis variant is used. */
20975 cp_parser_exception_declaration (cp_parser
* parser
)
20977 cp_decl_specifier_seq type_specifiers
;
20978 cp_declarator
*declarator
;
20979 const char *saved_message
;
20981 /* If it's an ellipsis, it's easy to handle. */
20982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20984 /* Consume the `...' token. */
20985 cp_lexer_consume_token (parser
->lexer
);
20989 /* Types may not be defined in exception-declarations. */
20990 saved_message
= parser
->type_definition_forbidden_message
;
20991 parser
->type_definition_forbidden_message
20992 = G_("types may not be defined in exception-declarations");
20994 /* Parse the type-specifier-seq. */
20995 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
20996 /*is_trailing_return=*/false,
20998 /* If it's a `)', then there is no declarator. */
20999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21002 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21003 /*ctor_dtor_or_conv_p=*/NULL
,
21004 /*parenthesized_p=*/NULL
,
21005 /*member_p=*/false);
21007 /* Restore the saved message. */
21008 parser
->type_definition_forbidden_message
= saved_message
;
21010 if (!type_specifiers
.any_specifiers_p
)
21011 return error_mark_node
;
21013 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21016 /* Parse a throw-expression.
21019 throw assignment-expression [opt]
21021 Returns a THROW_EXPR representing the throw-expression. */
21024 cp_parser_throw_expression (cp_parser
* parser
)
21029 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21030 token
= cp_lexer_peek_token (parser
->lexer
);
21031 /* Figure out whether or not there is an assignment-expression
21032 following the "throw" keyword. */
21033 if (token
->type
== CPP_COMMA
21034 || token
->type
== CPP_SEMICOLON
21035 || token
->type
== CPP_CLOSE_PAREN
21036 || token
->type
== CPP_CLOSE_SQUARE
21037 || token
->type
== CPP_CLOSE_BRACE
21038 || token
->type
== CPP_COLON
)
21039 expression
= NULL_TREE
;
21041 expression
= cp_parser_assignment_expression (parser
,
21042 /*cast_p=*/false, NULL
);
21044 return build_throw (expression
);
21047 /* GNU Extensions */
21049 /* Parse an (optional) asm-specification.
21052 asm ( string-literal )
21054 If the asm-specification is present, returns a STRING_CST
21055 corresponding to the string-literal. Otherwise, returns
21059 cp_parser_asm_specification_opt (cp_parser
* parser
)
21062 tree asm_specification
;
21064 /* Peek at the next token. */
21065 token
= cp_lexer_peek_token (parser
->lexer
);
21066 /* If the next token isn't the `asm' keyword, then there's no
21067 asm-specification. */
21068 if (!cp_parser_is_keyword (token
, RID_ASM
))
21071 /* Consume the `asm' token. */
21072 cp_lexer_consume_token (parser
->lexer
);
21073 /* Look for the `('. */
21074 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21076 /* Look for the string-literal. */
21077 asm_specification
= cp_parser_string_literal (parser
, false, false);
21079 /* Look for the `)'. */
21080 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21082 return asm_specification
;
21085 /* Parse an asm-operand-list.
21089 asm-operand-list , asm-operand
21092 string-literal ( expression )
21093 [ string-literal ] string-literal ( expression )
21095 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21096 each node is the expression. The TREE_PURPOSE is itself a
21097 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21098 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21099 is a STRING_CST for the string literal before the parenthesis. Returns
21100 ERROR_MARK_NODE if any of the operands are invalid. */
21103 cp_parser_asm_operand_list (cp_parser
* parser
)
21105 tree asm_operands
= NULL_TREE
;
21106 bool invalid_operands
= false;
21110 tree string_literal
;
21114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21116 /* Consume the `[' token. */
21117 cp_lexer_consume_token (parser
->lexer
);
21118 /* Read the operand name. */
21119 name
= cp_parser_identifier (parser
);
21120 if (name
!= error_mark_node
)
21121 name
= build_string (IDENTIFIER_LENGTH (name
),
21122 IDENTIFIER_POINTER (name
));
21123 /* Look for the closing `]'. */
21124 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21128 /* Look for the string-literal. */
21129 string_literal
= cp_parser_string_literal (parser
, false, false);
21131 /* Look for the `('. */
21132 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21133 /* Parse the expression. */
21134 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
21135 /* Look for the `)'. */
21136 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21138 if (name
== error_mark_node
21139 || string_literal
== error_mark_node
21140 || expression
== error_mark_node
)
21141 invalid_operands
= true;
21143 /* Add this operand to the list. */
21144 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21147 /* If the next token is not a `,', there are no more
21149 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21151 /* Consume the `,'. */
21152 cp_lexer_consume_token (parser
->lexer
);
21155 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21158 /* Parse an asm-clobber-list.
21162 asm-clobber-list , string-literal
21164 Returns a TREE_LIST, indicating the clobbers in the order that they
21165 appeared. The TREE_VALUE of each node is a STRING_CST. */
21168 cp_parser_asm_clobber_list (cp_parser
* parser
)
21170 tree clobbers
= NULL_TREE
;
21174 tree string_literal
;
21176 /* Look for the string literal. */
21177 string_literal
= cp_parser_string_literal (parser
, false, false);
21178 /* Add it to the list. */
21179 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21180 /* If the next token is not a `,', then the list is
21182 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21184 /* Consume the `,' token. */
21185 cp_lexer_consume_token (parser
->lexer
);
21191 /* Parse an asm-label-list.
21195 asm-label-list , identifier
21197 Returns a TREE_LIST, indicating the labels in the order that they
21198 appeared. The TREE_VALUE of each node is a label. */
21201 cp_parser_asm_label_list (cp_parser
* parser
)
21203 tree labels
= NULL_TREE
;
21207 tree identifier
, label
, name
;
21209 /* Look for the identifier. */
21210 identifier
= cp_parser_identifier (parser
);
21211 if (!error_operand_p (identifier
))
21213 label
= lookup_label (identifier
);
21214 if (TREE_CODE (label
) == LABEL_DECL
)
21216 TREE_USED (label
) = 1;
21217 check_goto (label
);
21218 name
= build_string (IDENTIFIER_LENGTH (identifier
),
21219 IDENTIFIER_POINTER (identifier
));
21220 labels
= tree_cons (name
, label
, labels
);
21223 /* If the next token is not a `,', then the list is
21225 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21227 /* Consume the `,' token. */
21228 cp_lexer_consume_token (parser
->lexer
);
21231 return nreverse (labels
);
21234 /* Return TRUE iff the next tokens in the stream are possibly the
21235 beginning of a GNU extension attribute. */
21238 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
21240 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
21243 /* Return TRUE iff the next tokens in the stream are possibly the
21244 beginning of a standard C++-11 attribute specifier. */
21247 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
21249 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
21252 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21253 beginning of a standard C++-11 attribute specifier. */
21256 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
21258 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21260 return (cxx_dialect
>= cxx11
21261 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
21262 || (token
->type
== CPP_OPEN_SQUARE
21263 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
21264 && token
->type
== CPP_OPEN_SQUARE
)));
21267 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21268 beginning of a GNU extension attribute. */
21271 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
21273 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21275 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
21278 /* Return true iff the next tokens can be the beginning of either a
21279 GNU attribute list, or a standard C++11 attribute sequence. */
21282 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
21284 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
21285 || cp_next_tokens_can_be_std_attribute_p (parser
));
21288 /* Return true iff the next Nth tokens can be the beginning of either
21289 a GNU attribute list, or a standard C++11 attribute sequence. */
21292 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
21294 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
21295 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
21298 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21299 of GNU attributes, or return NULL. */
21302 cp_parser_attributes_opt (cp_parser
*parser
)
21304 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
21305 return cp_parser_gnu_attributes_opt (parser
);
21306 return cp_parser_std_attribute_spec_seq (parser
);
21309 /* Parse an (optional) series of attributes.
21312 attributes attribute
21315 __attribute__ (( attribute-list [opt] ))
21317 The return value is as for cp_parser_gnu_attribute_list. */
21320 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
21322 tree attributes
= NULL_TREE
;
21327 tree attribute_list
;
21330 /* Peek at the next token. */
21331 token
= cp_lexer_peek_token (parser
->lexer
);
21332 /* If it's not `__attribute__', then we're done. */
21333 if (token
->keyword
!= RID_ATTRIBUTE
)
21336 /* Consume the `__attribute__' keyword. */
21337 cp_lexer_consume_token (parser
->lexer
);
21338 /* Look for the two `(' tokens. */
21339 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21340 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21342 /* Peek at the next token. */
21343 token
= cp_lexer_peek_token (parser
->lexer
);
21344 if (token
->type
!= CPP_CLOSE_PAREN
)
21345 /* Parse the attribute-list. */
21346 attribute_list
= cp_parser_gnu_attribute_list (parser
);
21348 /* If the next token is a `)', then there is no attribute
21350 attribute_list
= NULL
;
21352 /* Look for the two `)' tokens. */
21353 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
21355 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
21358 cp_parser_skip_to_end_of_statement (parser
);
21360 /* Add these new attributes to the list. */
21361 attributes
= chainon (attributes
, attribute_list
);
21367 /* Parse a GNU attribute-list.
21371 attribute-list , attribute
21375 identifier ( identifier )
21376 identifier ( identifier , expression-list )
21377 identifier ( expression-list )
21379 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21380 to an attribute. The TREE_PURPOSE of each node is the identifier
21381 indicating which attribute is in use. The TREE_VALUE represents
21382 the arguments, if any. */
21385 cp_parser_gnu_attribute_list (cp_parser
* parser
)
21387 tree attribute_list
= NULL_TREE
;
21388 bool save_translate_strings_p
= parser
->translate_strings_p
;
21390 parser
->translate_strings_p
= false;
21397 /* Look for the identifier. We also allow keywords here; for
21398 example `__attribute__ ((const))' is legal. */
21399 token
= cp_lexer_peek_token (parser
->lexer
);
21400 if (token
->type
== CPP_NAME
21401 || token
->type
== CPP_KEYWORD
)
21403 tree arguments
= NULL_TREE
;
21405 /* Consume the token. */
21406 token
= cp_lexer_consume_token (parser
->lexer
);
21408 /* Save away the identifier that indicates which attribute
21410 identifier
= (token
->type
== CPP_KEYWORD
)
21411 /* For keywords, use the canonical spelling, not the
21412 parsed identifier. */
21413 ? ridpointers
[(int) token
->keyword
]
21416 attribute
= build_tree_list (identifier
, NULL_TREE
);
21418 /* Peek at the next token. */
21419 token
= cp_lexer_peek_token (parser
->lexer
);
21420 /* If it's an `(', then parse the attribute arguments. */
21421 if (token
->type
== CPP_OPEN_PAREN
)
21423 vec
<tree
, va_gc
> *vec
;
21424 int attr_flag
= (attribute_takes_identifier_p (identifier
)
21425 ? id_attr
: normal_attr
);
21426 vec
= cp_parser_parenthesized_expression_list
21427 (parser
, attr_flag
, /*cast_p=*/false,
21428 /*allow_expansion_p=*/false,
21429 /*non_constant_p=*/NULL
);
21431 arguments
= error_mark_node
;
21434 arguments
= build_tree_list_vec (vec
);
21435 release_tree_vector (vec
);
21437 /* Save the arguments away. */
21438 TREE_VALUE (attribute
) = arguments
;
21441 if (arguments
!= error_mark_node
)
21443 /* Add this attribute to the list. */
21444 TREE_CHAIN (attribute
) = attribute_list
;
21445 attribute_list
= attribute
;
21448 token
= cp_lexer_peek_token (parser
->lexer
);
21450 /* Now, look for more attributes. If the next token isn't a
21451 `,', we're done. */
21452 if (token
->type
!= CPP_COMMA
)
21455 /* Consume the comma and keep going. */
21456 cp_lexer_consume_token (parser
->lexer
);
21458 parser
->translate_strings_p
= save_translate_strings_p
;
21460 /* We built up the list in reverse order. */
21461 return nreverse (attribute_list
);
21464 /* Parse a standard C++11 attribute.
21466 The returned representation is a TREE_LIST which TREE_PURPOSE is
21467 the scoped name of the attribute, and the TREE_VALUE is its
21470 Note that the scoped name of the attribute is itself a TREE_LIST
21471 which TREE_PURPOSE is the namespace of the attribute, and
21472 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21473 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21474 and which TREE_PURPOSE is directly the attribute name.
21476 Clients of the attribute code should use get_attribute_namespace
21477 and get_attribute_name to get the actual namespace and name of
21478 attributes, regardless of their being GNU or C++11 attributes.
21481 attribute-token attribute-argument-clause [opt]
21485 attribute-scoped-token
21487 attribute-scoped-token:
21488 attribute-namespace :: identifier
21490 attribute-namespace:
21493 attribute-argument-clause:
21494 ( balanced-token-seq )
21496 balanced-token-seq:
21497 balanced-token [opt]
21498 balanced-token-seq balanced-token
21501 ( balanced-token-seq )
21502 [ balanced-token-seq ]
21503 { balanced-token-seq }. */
21506 cp_parser_std_attribute (cp_parser
*parser
)
21508 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
21511 /* First, parse name of the the attribute, a.k.a
21512 attribute-token. */
21514 token
= cp_lexer_peek_token (parser
->lexer
);
21515 if (token
->type
== CPP_NAME
)
21516 attr_id
= token
->u
.value
;
21517 else if (token
->type
== CPP_KEYWORD
)
21518 attr_id
= ridpointers
[(int) token
->keyword
];
21519 else if (token
->flags
& NAMED_OP
)
21520 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
21522 if (attr_id
== NULL_TREE
)
21525 cp_lexer_consume_token (parser
->lexer
);
21527 token
= cp_lexer_peek_token (parser
->lexer
);
21528 if (token
->type
== CPP_SCOPE
)
21530 /* We are seeing a scoped attribute token. */
21532 cp_lexer_consume_token (parser
->lexer
);
21535 token
= cp_lexer_consume_token (parser
->lexer
);
21536 if (token
->type
== CPP_NAME
)
21537 attr_id
= token
->u
.value
;
21538 else if (token
->type
== CPP_KEYWORD
)
21539 attr_id
= ridpointers
[(int) token
->keyword
];
21542 error_at (token
->location
,
21543 "expected an identifier for the attribute name");
21544 return error_mark_node
;
21546 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
21548 token
= cp_lexer_peek_token (parser
->lexer
);
21552 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
21554 /* C++11 noreturn attribute is equivalent to GNU's. */
21555 if (is_attribute_p ("noreturn", attr_id
))
21556 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21557 /* C++14 deprecated attribute is equivalent to GNU's. */
21558 else if (cxx_dialect
>= cxx1y
&& is_attribute_p ("deprecated", attr_id
))
21559 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21562 /* Now parse the optional argument clause of the attribute. */
21564 if (token
->type
!= CPP_OPEN_PAREN
)
21568 vec
<tree
, va_gc
> *vec
;
21569 int attr_flag
= normal_attr
;
21571 if (attr_ns
== get_identifier ("gnu")
21572 && attribute_takes_identifier_p (attr_id
))
21573 /* A GNU attribute that takes an identifier in parameter. */
21574 attr_flag
= id_attr
;
21576 vec
= cp_parser_parenthesized_expression_list
21577 (parser
, attr_flag
, /*cast_p=*/false,
21578 /*allow_expansion_p=*/true,
21579 /*non_constant_p=*/NULL
);
21581 arguments
= error_mark_node
;
21584 arguments
= build_tree_list_vec (vec
);
21585 release_tree_vector (vec
);
21588 if (arguments
== error_mark_node
)
21589 attribute
= error_mark_node
;
21591 TREE_VALUE (attribute
) = arguments
;
21597 /* Parse a list of standard C++-11 attributes.
21601 attribute-list , attribute[opt]
21603 attribute-list , attribute ...
21607 cp_parser_std_attribute_list (cp_parser
*parser
)
21609 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
21610 cp_token
*token
= NULL
;
21614 attribute
= cp_parser_std_attribute (parser
);
21615 if (attribute
== error_mark_node
)
21617 if (attribute
!= NULL_TREE
)
21619 TREE_CHAIN (attribute
) = attributes
;
21620 attributes
= attribute
;
21622 token
= cp_lexer_peek_token (parser
->lexer
);
21623 if (token
->type
!= CPP_COMMA
)
21625 cp_lexer_consume_token (parser
->lexer
);
21627 attributes
= nreverse (attributes
);
21631 /* Parse a standard C++-11 attribute specifier.
21633 attribute-specifier:
21634 [ [ attribute-list ] ]
21635 alignment-specifier
21637 alignment-specifier:
21638 alignas ( type-id ... [opt] )
21639 alignas ( alignment-expression ... [opt] ). */
21642 cp_parser_std_attribute_spec (cp_parser
*parser
)
21644 tree attributes
= NULL_TREE
;
21645 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21647 if (token
->type
== CPP_OPEN_SQUARE
21648 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
21650 cp_lexer_consume_token (parser
->lexer
);
21651 cp_lexer_consume_token (parser
->lexer
);
21653 attributes
= cp_parser_std_attribute_list (parser
);
21655 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
21656 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
21657 cp_parser_skip_to_end_of_statement (parser
);
21659 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21660 when we are sure that we have actually parsed them. */
21661 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21667 /* Look for an alignment-specifier. */
21669 token
= cp_lexer_peek_token (parser
->lexer
);
21671 if (token
->type
!= CPP_KEYWORD
21672 || token
->keyword
!= RID_ALIGNAS
)
21675 cp_lexer_consume_token (parser
->lexer
);
21676 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21678 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
21680 cp_parser_error (parser
, "expected %<(%>");
21681 return error_mark_node
;
21684 cp_parser_parse_tentatively (parser
);
21685 alignas_expr
= cp_parser_type_id (parser
);
21687 if (!cp_parser_parse_definitely (parser
))
21689 gcc_assert (alignas_expr
== error_mark_node
21690 || alignas_expr
== NULL_TREE
);
21693 cp_parser_assignment_expression (parser
, /*cast_p=*/false,
21694 /**cp_id_kind=*/NULL
);
21695 if (alignas_expr
== error_mark_node
)
21696 cp_parser_skip_to_end_of_statement (parser
);
21697 if (alignas_expr
== NULL_TREE
21698 || alignas_expr
== error_mark_node
)
21699 return alignas_expr
;
21702 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
21704 cp_parser_error (parser
, "expected %<)%>");
21705 return error_mark_node
;
21708 alignas_expr
= cxx_alignas_expr (alignas_expr
);
21710 /* Build the C++-11 representation of an 'aligned'
21713 build_tree_list (build_tree_list (get_identifier ("gnu"),
21714 get_identifier ("aligned")),
21715 build_tree_list (NULL_TREE
, alignas_expr
));
21721 /* Parse a standard C++-11 attribute-specifier-seq.
21723 attribute-specifier-seq:
21724 attribute-specifier-seq [opt] attribute-specifier
21728 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
21730 tree attr_specs
= NULL
;
21734 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
21735 if (attr_spec
== NULL_TREE
)
21737 if (attr_spec
== error_mark_node
)
21738 return error_mark_node
;
21740 TREE_CHAIN (attr_spec
) = attr_specs
;
21741 attr_specs
= attr_spec
;
21744 attr_specs
= nreverse (attr_specs
);
21748 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21749 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21750 current value of the PEDANTIC flag, regardless of whether or not
21751 the `__extension__' keyword is present. The caller is responsible
21752 for restoring the value of the PEDANTIC flag. */
21755 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
21757 /* Save the old value of the PEDANTIC flag. */
21758 *saved_pedantic
= pedantic
;
21760 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
21762 /* Consume the `__extension__' token. */
21763 cp_lexer_consume_token (parser
->lexer
);
21764 /* We're not being pedantic while the `__extension__' keyword is
21774 /* Parse a label declaration.
21777 __label__ label-declarator-seq ;
21779 label-declarator-seq:
21780 identifier , label-declarator-seq
21784 cp_parser_label_declaration (cp_parser
* parser
)
21786 /* Look for the `__label__' keyword. */
21787 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
21793 /* Look for an identifier. */
21794 identifier
= cp_parser_identifier (parser
);
21795 /* If we failed, stop. */
21796 if (identifier
== error_mark_node
)
21798 /* Declare it as a label. */
21799 finish_label_decl (identifier
);
21800 /* If the next token is a `;', stop. */
21801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21803 /* Look for the `,' separating the label declarations. */
21804 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
21807 /* Look for the final `;'. */
21808 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21811 /* Support Functions */
21813 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21814 NAME should have one of the representations used for an
21815 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21816 is returned. If PARSER->SCOPE is a dependent type, then a
21817 SCOPE_REF is returned.
21819 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21820 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21821 was formed. Abstractly, such entities should not be passed to this
21822 function, because they do not need to be looked up, but it is
21823 simpler to check for this special case here, rather than at the
21826 In cases not explicitly covered above, this function returns a
21827 DECL, OVERLOAD, or baselink representing the result of the lookup.
21828 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21831 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21832 (e.g., "struct") that was used. In that case bindings that do not
21833 refer to types are ignored.
21835 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21838 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21841 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21844 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21845 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21846 NULL_TREE otherwise. */
21849 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
21850 enum tag_types tag_type
,
21853 bool check_dependency
,
21854 tree
*ambiguous_decls
,
21855 location_t name_location
)
21858 tree object_type
= parser
->context
->object_type
;
21860 /* Assume that the lookup will be unambiguous. */
21861 if (ambiguous_decls
)
21862 *ambiguous_decls
= NULL_TREE
;
21864 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21865 no longer valid. Note that if we are parsing tentatively, and
21866 the parse fails, OBJECT_TYPE will be automatically restored. */
21867 parser
->context
->object_type
= NULL_TREE
;
21869 if (name
== error_mark_node
)
21870 return error_mark_node
;
21872 /* A template-id has already been resolved; there is no lookup to
21874 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
21876 if (BASELINK_P (name
))
21878 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
21879 == TEMPLATE_ID_EXPR
);
21883 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21884 it should already have been checked to make sure that the name
21885 used matches the type being destroyed. */
21886 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
21890 /* Figure out to which type this destructor applies. */
21892 type
= parser
->scope
;
21893 else if (object_type
)
21894 type
= object_type
;
21896 type
= current_class_type
;
21897 /* If that's not a class type, there is no destructor. */
21898 if (!type
|| !CLASS_TYPE_P (type
))
21899 return error_mark_node
;
21900 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
21901 lazily_declare_fn (sfk_destructor
, type
);
21902 if (!CLASSTYPE_DESTRUCTORS (type
))
21903 return error_mark_node
;
21904 /* If it was a class type, return the destructor. */
21905 return CLASSTYPE_DESTRUCTORS (type
);
21908 /* By this point, the NAME should be an ordinary identifier. If
21909 the id-expression was a qualified name, the qualifying scope is
21910 stored in PARSER->SCOPE at this point. */
21911 gcc_assert (identifier_p (name
));
21913 /* Perform the lookup. */
21918 if (parser
->scope
== error_mark_node
)
21919 return error_mark_node
;
21921 /* If the SCOPE is dependent, the lookup must be deferred until
21922 the template is instantiated -- unless we are explicitly
21923 looking up names in uninstantiated templates. Even then, we
21924 cannot look up the name if the scope is not a class type; it
21925 might, for example, be a template type parameter. */
21926 dependent_p
= (TYPE_P (parser
->scope
)
21927 && dependent_scope_p (parser
->scope
));
21928 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
21930 /* Defer lookup. */
21931 decl
= error_mark_node
;
21934 tree pushed_scope
= NULL_TREE
;
21936 /* If PARSER->SCOPE is a dependent type, then it must be a
21937 class type, and we must not be checking dependencies;
21938 otherwise, we would have processed this lookup above. So
21939 that PARSER->SCOPE is not considered a dependent base by
21940 lookup_member, we must enter the scope here. */
21942 pushed_scope
= push_scope (parser
->scope
);
21944 /* If the PARSER->SCOPE is a template specialization, it
21945 may be instantiated during name lookup. In that case,
21946 errors may be issued. Even if we rollback the current
21947 tentative parse, those errors are valid. */
21948 decl
= lookup_qualified_name (parser
->scope
, name
,
21949 tag_type
!= none_type
,
21950 /*complain=*/true);
21952 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21953 lookup result and the nested-name-specifier nominates a class C:
21954 * if the name specified after the nested-name-specifier, when
21955 looked up in C, is the injected-class-name of C (Clause 9), or
21956 * if the name specified after the nested-name-specifier is the
21957 same as the identifier or the simple-template-id's template-
21958 name in the last component of the nested-name-specifier,
21959 the name is instead considered to name the constructor of
21960 class C. [ Note: for example, the constructor is not an
21961 acceptable lookup result in an elaborated-type-specifier so
21962 the constructor would not be used in place of the
21963 injected-class-name. --end note ] Such a constructor name
21964 shall be used only in the declarator-id of a declaration that
21965 names a constructor or in a using-declaration. */
21966 if (tag_type
== none_type
21967 && DECL_SELF_REFERENCE_P (decl
)
21968 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
21969 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
21970 tag_type
!= none_type
,
21971 /*complain=*/true);
21973 /* If we have a single function from a using decl, pull it out. */
21974 if (TREE_CODE (decl
) == OVERLOAD
21975 && !really_overloaded_fn (decl
))
21976 decl
= OVL_FUNCTION (decl
);
21979 pop_scope (pushed_scope
);
21982 /* If the scope is a dependent type and either we deferred lookup or
21983 we did lookup but didn't find the name, rememeber the name. */
21984 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
21985 && dependent_type_p (parser
->scope
))
21991 /* The resolution to Core Issue 180 says that `struct
21992 A::B' should be considered a type-name, even if `A'
21994 type
= make_typename_type (parser
->scope
, name
, tag_type
,
21995 /*complain=*/tf_error
);
21996 if (type
!= error_mark_node
)
21997 decl
= TYPE_NAME (type
);
21999 else if (is_template
22000 && (cp_parser_next_token_ends_template_argument_p (parser
)
22001 || cp_lexer_next_token_is (parser
->lexer
,
22003 decl
= make_unbound_class_template (parser
->scope
,
22005 /*complain=*/tf_error
);
22007 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22008 parser
->scope
, name
,
22011 parser
->qualifying_scope
= parser
->scope
;
22012 parser
->object_scope
= NULL_TREE
;
22014 else if (object_type
)
22016 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22017 OBJECT_TYPE is not a class. */
22018 if (CLASS_TYPE_P (object_type
))
22019 /* If the OBJECT_TYPE is a template specialization, it may
22020 be instantiated during name lookup. In that case, errors
22021 may be issued. Even if we rollback the current tentative
22022 parse, those errors are valid. */
22023 decl
= lookup_member (object_type
,
22026 tag_type
!= none_type
,
22027 tf_warning_or_error
);
22032 /* Look it up in the enclosing context. */
22033 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22035 /*block_p=*/true, is_namespace
, 0);
22036 parser
->object_scope
= object_type
;
22037 parser
->qualifying_scope
= NULL_TREE
;
22041 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22043 /*block_p=*/true, is_namespace
, 0);
22044 parser
->qualifying_scope
= NULL_TREE
;
22045 parser
->object_scope
= NULL_TREE
;
22048 /* If the lookup failed, let our caller know. */
22049 if (!decl
|| decl
== error_mark_node
)
22050 return error_mark_node
;
22052 /* Pull out the template from an injected-class-name (or multiple). */
22054 decl
= maybe_get_template_decl_from_type_decl (decl
);
22056 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22057 if (TREE_CODE (decl
) == TREE_LIST
)
22059 if (ambiguous_decls
)
22060 *ambiguous_decls
= decl
;
22061 /* The error message we have to print is too complicated for
22062 cp_parser_error, so we incorporate its actions directly. */
22063 if (!cp_parser_simulate_error (parser
))
22065 error_at (name_location
, "reference to %qD is ambiguous",
22067 print_candidates (decl
);
22069 return error_mark_node
;
22072 gcc_assert (DECL_P (decl
)
22073 || TREE_CODE (decl
) == OVERLOAD
22074 || TREE_CODE (decl
) == SCOPE_REF
22075 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
22076 || BASELINK_P (decl
));
22078 /* If we have resolved the name of a member declaration, check to
22079 see if the declaration is accessible. When the name resolves to
22080 set of overloaded functions, accessibility is checked when
22081 overload resolution is done.
22083 During an explicit instantiation, access is not checked at all,
22084 as per [temp.explicit]. */
22086 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
22088 maybe_record_typedef_use (decl
);
22093 /* Like cp_parser_lookup_name, but for use in the typical case where
22094 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22095 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22098 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
22100 return cp_parser_lookup_name (parser
, name
,
22102 /*is_template=*/false,
22103 /*is_namespace=*/false,
22104 /*check_dependency=*/true,
22105 /*ambiguous_decls=*/NULL
,
22109 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22110 the current context, return the TYPE_DECL. If TAG_NAME_P is
22111 true, the DECL indicates the class being defined in a class-head,
22112 or declared in an elaborated-type-specifier.
22114 Otherwise, return DECL. */
22117 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22119 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22120 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22123 template <typename T> struct B;
22126 template <typename T> struct A::B {};
22128 Similarly, in an elaborated-type-specifier:
22130 namespace N { struct X{}; }
22133 template <typename T> friend struct N::X;
22136 However, if the DECL refers to a class type, and we are in
22137 the scope of the class, then the name lookup automatically
22138 finds the TYPE_DECL created by build_self_reference rather
22139 than a TEMPLATE_DECL. For example, in:
22141 template <class T> struct S {
22145 there is no need to handle such case. */
22147 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
22148 return DECL_TEMPLATE_RESULT (decl
);
22153 /* If too many, or too few, template-parameter lists apply to the
22154 declarator, issue an error message. Returns TRUE if all went well,
22155 and FALSE otherwise. */
22158 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
22159 cp_declarator
*declarator
,
22160 location_t declarator_location
)
22162 switch (declarator
->kind
)
22166 unsigned num_templates
= 0;
22167 tree scope
= declarator
->u
.id
.qualifying_scope
;
22170 num_templates
= num_template_headers_for_class (scope
);
22171 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
22172 == TEMPLATE_ID_EXPR
)
22173 /* If the DECLARATOR has the form `X<y>' then it uses one
22174 additional level of template parameters. */
22177 return cp_parser_check_template_parameters
22178 (parser
, num_templates
, declarator_location
, declarator
);
22184 case cdk_reference
:
22186 return (cp_parser_check_declarator_template_parameters
22187 (parser
, declarator
->declarator
, declarator_location
));
22193 gcc_unreachable ();
22198 /* NUM_TEMPLATES were used in the current declaration. If that is
22199 invalid, return FALSE and issue an error messages. Otherwise,
22200 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22201 declarator and we can print more accurate diagnostics. */
22204 cp_parser_check_template_parameters (cp_parser
* parser
,
22205 unsigned num_templates
,
22206 location_t location
,
22207 cp_declarator
*declarator
)
22209 /* If there are the same number of template classes and parameter
22210 lists, that's OK. */
22211 if (parser
->num_template_parameter_lists
== num_templates
)
22213 /* If there are more, but only one more, then we are referring to a
22214 member template. That's OK too. */
22215 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
22217 /* If there are more template classes than parameter lists, we have
22220 template <class T> void S<T>::R<T>::f (); */
22221 if (parser
->num_template_parameter_lists
< num_templates
)
22223 if (declarator
&& !current_function_decl
)
22224 error_at (location
, "specializing member %<%T::%E%> "
22225 "requires %<template<>%> syntax",
22226 declarator
->u
.id
.qualifying_scope
,
22227 declarator
->u
.id
.unqualified_name
);
22228 else if (declarator
)
22229 error_at (location
, "invalid declaration of %<%T::%E%>",
22230 declarator
->u
.id
.qualifying_scope
,
22231 declarator
->u
.id
.unqualified_name
);
22233 error_at (location
, "too few template-parameter-lists");
22236 /* Otherwise, there are too many template parameter lists. We have
22239 template <class T> template <class U> void S::f(); */
22240 error_at (location
, "too many template-parameter-lists");
22244 /* Parse an optional `::' token indicating that the following name is
22245 from the global namespace. If so, PARSER->SCOPE is set to the
22246 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22247 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22248 Returns the new value of PARSER->SCOPE, if the `::' token is
22249 present, and NULL_TREE otherwise. */
22252 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
22256 /* Peek at the next token. */
22257 token
= cp_lexer_peek_token (parser
->lexer
);
22258 /* If we're looking at a `::' token then we're starting from the
22259 global namespace, not our current location. */
22260 if (token
->type
== CPP_SCOPE
)
22262 /* Consume the `::' token. */
22263 cp_lexer_consume_token (parser
->lexer
);
22264 /* Set the SCOPE so that we know where to start the lookup. */
22265 parser
->scope
= global_namespace
;
22266 parser
->qualifying_scope
= global_namespace
;
22267 parser
->object_scope
= NULL_TREE
;
22269 return parser
->scope
;
22271 else if (!current_scope_valid_p
)
22273 parser
->scope
= NULL_TREE
;
22274 parser
->qualifying_scope
= NULL_TREE
;
22275 parser
->object_scope
= NULL_TREE
;
22281 /* Returns TRUE if the upcoming token sequence is the start of a
22282 constructor declarator. If FRIEND_P is true, the declarator is
22283 preceded by the `friend' specifier. */
22286 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
22288 bool constructor_p
;
22289 bool outside_class_specifier_p
;
22290 tree nested_name_specifier
;
22291 cp_token
*next_token
;
22293 /* The common case is that this is not a constructor declarator, so
22294 try to avoid doing lots of work if at all possible. It's not
22295 valid declare a constructor at function scope. */
22296 if (parser
->in_function_body
)
22298 /* And only certain tokens can begin a constructor declarator. */
22299 next_token
= cp_lexer_peek_token (parser
->lexer
);
22300 if (next_token
->type
!= CPP_NAME
22301 && next_token
->type
!= CPP_SCOPE
22302 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
22303 && next_token
->type
!= CPP_TEMPLATE_ID
)
22306 /* Parse tentatively; we are going to roll back all of the tokens
22308 cp_parser_parse_tentatively (parser
);
22309 /* Assume that we are looking at a constructor declarator. */
22310 constructor_p
= true;
22312 /* Look for the optional `::' operator. */
22313 cp_parser_global_scope_opt (parser
,
22314 /*current_scope_valid_p=*/false);
22315 /* Look for the nested-name-specifier. */
22316 nested_name_specifier
22317 = (cp_parser_nested_name_specifier_opt (parser
,
22318 /*typename_keyword_p=*/false,
22319 /*check_dependency_p=*/false,
22321 /*is_declaration=*/false));
22323 outside_class_specifier_p
= (!at_class_scope_p ()
22324 || !TYPE_BEING_DEFINED (current_class_type
)
22327 /* Outside of a class-specifier, there must be a
22328 nested-name-specifier. */
22329 if (!nested_name_specifier
&& outside_class_specifier_p
)
22330 constructor_p
= false;
22331 else if (nested_name_specifier
== error_mark_node
)
22332 constructor_p
= false;
22334 /* If we have a class scope, this is easy; DR 147 says that S::S always
22335 names the constructor, and no other qualified name could. */
22336 if (constructor_p
&& nested_name_specifier
22337 && CLASS_TYPE_P (nested_name_specifier
))
22339 tree id
= cp_parser_unqualified_id (parser
,
22340 /*template_keyword_p=*/false,
22341 /*check_dependency_p=*/false,
22342 /*declarator_p=*/true,
22343 /*optional_p=*/false);
22344 if (is_overloaded_fn (id
))
22345 id
= DECL_NAME (get_first_fn (id
));
22346 if (!constructor_name_p (id
, nested_name_specifier
))
22347 constructor_p
= false;
22349 /* If we still think that this might be a constructor-declarator,
22350 look for a class-name. */
22351 else if (constructor_p
)
22355 template <typename T> struct S {
22359 we must recognize that the nested `S' names a class. */
22361 type_decl
= cp_parser_class_name (parser
,
22362 /*typename_keyword_p=*/false,
22363 /*template_keyword_p=*/false,
22365 /*check_dependency_p=*/false,
22366 /*class_head_p=*/false,
22367 /*is_declaration=*/false);
22368 /* If there was no class-name, then this is not a constructor.
22369 Otherwise, if we are in a class-specifier and we aren't
22370 handling a friend declaration, check that its type matches
22371 current_class_type (c++/38313). Note: error_mark_node
22372 is left alone for error recovery purposes. */
22373 constructor_p
= (!cp_parser_error_occurred (parser
)
22374 && (outside_class_specifier_p
22375 || type_decl
== error_mark_node
22376 || same_type_p (current_class_type
,
22377 TREE_TYPE (type_decl
))));
22379 /* If we're still considering a constructor, we have to see a `(',
22380 to begin the parameter-declaration-clause, followed by either a
22381 `)', an `...', or a decl-specifier. We need to check for a
22382 type-specifier to avoid being fooled into thinking that:
22386 is a constructor. (It is actually a function named `f' that
22387 takes one parameter (of type `int') and returns a value of type
22390 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22391 constructor_p
= false;
22394 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
22395 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
22396 /* A parameter declaration begins with a decl-specifier,
22397 which is either the "attribute" keyword, a storage class
22398 specifier, or (usually) a type-specifier. */
22399 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
22402 tree pushed_scope
= NULL_TREE
;
22403 unsigned saved_num_template_parameter_lists
;
22405 /* Names appearing in the type-specifier should be looked up
22406 in the scope of the class. */
22407 if (current_class_type
)
22411 type
= TREE_TYPE (type_decl
);
22412 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22414 type
= resolve_typename_type (type
,
22415 /*only_current_p=*/false);
22416 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22418 cp_parser_abort_tentative_parse (parser
);
22422 pushed_scope
= push_scope (type
);
22425 /* Inside the constructor parameter list, surrounding
22426 template-parameter-lists do not apply. */
22427 saved_num_template_parameter_lists
22428 = parser
->num_template_parameter_lists
;
22429 parser
->num_template_parameter_lists
= 0;
22431 /* Look for the type-specifier. */
22432 cp_parser_type_specifier (parser
,
22433 CP_PARSER_FLAGS_NONE
,
22434 /*decl_specs=*/NULL
,
22435 /*is_declarator=*/true,
22436 /*declares_class_or_enum=*/NULL
,
22437 /*is_cv_qualifier=*/NULL
);
22439 parser
->num_template_parameter_lists
22440 = saved_num_template_parameter_lists
;
22442 /* Leave the scope of the class. */
22444 pop_scope (pushed_scope
);
22446 constructor_p
= !cp_parser_error_occurred (parser
);
22450 /* We did not really want to consume any tokens. */
22451 cp_parser_abort_tentative_parse (parser
);
22453 return constructor_p
;
22456 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22457 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22458 they must be performed once we are in the scope of the function.
22460 Returns the function defined. */
22463 cp_parser_function_definition_from_specifiers_and_declarator
22464 (cp_parser
* parser
,
22465 cp_decl_specifier_seq
*decl_specifiers
,
22467 const cp_declarator
*declarator
)
22472 /* Begin the function-definition. */
22473 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
22475 /* The things we're about to see are not directly qualified by any
22476 template headers we've seen thus far. */
22477 reset_specialization ();
22479 /* If there were names looked up in the decl-specifier-seq that we
22480 did not check, check them now. We must wait until we are in the
22481 scope of the function to perform the checks, since the function
22482 might be a friend. */
22483 perform_deferred_access_checks (tf_warning_or_error
);
22487 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
22488 parser
->omp_declare_simd
= NULL
;
22493 /* Skip the entire function. */
22494 cp_parser_skip_to_end_of_block_or_statement (parser
);
22495 fn
= error_mark_node
;
22497 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
22499 /* Seen already, skip it. An error message has already been output. */
22500 cp_parser_skip_to_end_of_block_or_statement (parser
);
22501 fn
= current_function_decl
;
22502 current_function_decl
= NULL_TREE
;
22503 /* If this is a function from a class, pop the nested class. */
22504 if (current_class_name
)
22505 pop_nested_class ();
22510 if (DECL_DECLARED_INLINE_P (current_function_decl
))
22511 tv
= TV_PARSE_INLINE
;
22513 tv
= TV_PARSE_FUNC
;
22515 fn
= cp_parser_function_definition_after_declarator (parser
,
22516 /*inline_p=*/false);
22523 /* Parse the part of a function-definition that follows the
22524 declarator. INLINE_P is TRUE iff this function is an inline
22525 function defined within a class-specifier.
22527 Returns the function defined. */
22530 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
22534 bool ctor_initializer_p
= false;
22535 bool saved_in_unbraced_linkage_specification_p
;
22536 bool saved_in_function_body
;
22537 unsigned saved_num_template_parameter_lists
;
22539 bool fully_implicit_function_template_p
22540 = parser
->fully_implicit_function_template_p
;
22541 parser
->fully_implicit_function_template_p
= false;
22542 tree implicit_template_parms
22543 = parser
->implicit_template_parms
;
22544 parser
->implicit_template_parms
= 0;
22545 cp_binding_level
* implicit_template_scope
22546 = parser
->implicit_template_scope
;
22547 parser
->implicit_template_scope
= 0;
22549 saved_in_function_body
= parser
->in_function_body
;
22550 parser
->in_function_body
= true;
22551 /* If the next token is `return', then the code may be trying to
22552 make use of the "named return value" extension that G++ used to
22554 token
= cp_lexer_peek_token (parser
->lexer
);
22555 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
22557 /* Consume the `return' keyword. */
22558 cp_lexer_consume_token (parser
->lexer
);
22559 /* Look for the identifier that indicates what value is to be
22561 cp_parser_identifier (parser
);
22562 /* Issue an error message. */
22563 error_at (token
->location
,
22564 "named return values are no longer supported");
22565 /* Skip tokens until we reach the start of the function body. */
22568 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22569 if (token
->type
== CPP_OPEN_BRACE
22570 || token
->type
== CPP_EOF
22571 || token
->type
== CPP_PRAGMA_EOL
)
22573 cp_lexer_consume_token (parser
->lexer
);
22576 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22577 anything declared inside `f'. */
22578 saved_in_unbraced_linkage_specification_p
22579 = parser
->in_unbraced_linkage_specification_p
;
22580 parser
->in_unbraced_linkage_specification_p
= false;
22581 /* Inside the function, surrounding template-parameter-lists do not
22583 saved_num_template_parameter_lists
22584 = parser
->num_template_parameter_lists
;
22585 parser
->num_template_parameter_lists
= 0;
22587 start_lambda_scope (current_function_decl
);
22589 /* If the next token is `try', `__transaction_atomic', or
22590 `__transaction_relaxed`, then we are looking at either function-try-block
22591 or function-transaction-block. Note that all of these include the
22593 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
22594 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22595 RID_TRANSACTION_ATOMIC
);
22596 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
22597 RID_TRANSACTION_RELAXED
))
22598 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22599 RID_TRANSACTION_RELAXED
);
22600 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
22601 ctor_initializer_p
= cp_parser_function_try_block (parser
);
22603 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
22604 (parser
, /*in_function_try_block=*/false);
22606 finish_lambda_scope ();
22608 /* Finish the function. */
22609 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
22610 (inline_p
? 2 : 0));
22611 /* Generate code for it, if necessary. */
22612 expand_or_defer_fn (fn
);
22613 /* Restore the saved values. */
22614 parser
->in_unbraced_linkage_specification_p
22615 = saved_in_unbraced_linkage_specification_p
;
22616 parser
->num_template_parameter_lists
22617 = saved_num_template_parameter_lists
;
22618 parser
->in_function_body
= saved_in_function_body
;
22620 parser
->fully_implicit_function_template_p
22621 = fully_implicit_function_template_p
;
22622 parser
->implicit_template_parms
22623 = implicit_template_parms
;
22624 parser
->implicit_template_scope
22625 = implicit_template_scope
;
22627 if (parser
->fully_implicit_function_template_p
)
22628 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
22633 /* Parse a template-declaration, assuming that the `export' (and
22634 `extern') keywords, if present, has already been scanned. MEMBER_P
22635 is as for cp_parser_template_declaration. */
22638 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
22640 tree decl
= NULL_TREE
;
22641 vec
<deferred_access_check
, va_gc
> *checks
;
22642 tree parameter_list
;
22643 bool friend_p
= false;
22644 bool need_lang_pop
;
22647 /* Look for the `template' keyword. */
22648 token
= cp_lexer_peek_token (parser
->lexer
);
22649 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
22653 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
22655 if (at_class_scope_p () && current_function_decl
)
22657 /* 14.5.2.2 [temp.mem]
22659 A local class shall not have member templates. */
22660 error_at (token
->location
,
22661 "invalid declaration of member template in local class");
22662 cp_parser_skip_to_end_of_block_or_statement (parser
);
22667 A template ... shall not have C linkage. */
22668 if (current_lang_name
== lang_name_c
)
22670 error_at (token
->location
, "template with C linkage");
22671 /* Give it C++ linkage to avoid confusing other parts of the
22673 push_lang_context (lang_name_cplusplus
);
22674 need_lang_pop
= true;
22677 need_lang_pop
= false;
22679 /* We cannot perform access checks on the template parameter
22680 declarations until we know what is being declared, just as we
22681 cannot check the decl-specifier list. */
22682 push_deferring_access_checks (dk_deferred
);
22684 /* If the next token is `>', then we have an invalid
22685 specialization. Rather than complain about an invalid template
22686 parameter, issue an error message here. */
22687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
22689 cp_parser_error (parser
, "invalid explicit specialization");
22690 begin_specialization ();
22691 parameter_list
= NULL_TREE
;
22695 /* Parse the template parameters. */
22696 parameter_list
= cp_parser_template_parameter_list (parser
);
22699 /* Get the deferred access checks from the parameter list. These
22700 will be checked once we know what is being declared, as for a
22701 member template the checks must be performed in the scope of the
22702 class containing the member. */
22703 checks
= get_deferred_access_checks ();
22705 /* Look for the `>'. */
22706 cp_parser_skip_to_end_of_template_parameter_list (parser
);
22707 /* We just processed one more parameter list. */
22708 ++parser
->num_template_parameter_lists
;
22709 /* If the next token is `template', there are more template
22711 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
22713 cp_parser_template_declaration_after_export (parser
, member_p
);
22714 else if (cxx_dialect
>= cxx11
22715 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
22716 decl
= cp_parser_alias_declaration (parser
);
22719 /* There are no access checks when parsing a template, as we do not
22720 know if a specialization will be a friend. */
22721 push_deferring_access_checks (dk_no_check
);
22722 token
= cp_lexer_peek_token (parser
->lexer
);
22723 decl
= cp_parser_single_declaration (parser
,
22726 /*explicit_specialization_p=*/false,
22728 pop_deferring_access_checks ();
22730 /* If this is a member template declaration, let the front
22732 if (member_p
&& !friend_p
&& decl
)
22734 if (TREE_CODE (decl
) == TYPE_DECL
)
22735 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
22737 decl
= finish_member_template_decl (decl
);
22739 else if (friend_p
&& decl
22740 && DECL_DECLARES_TYPE_P (decl
))
22741 make_friend_class (current_class_type
, TREE_TYPE (decl
),
22742 /*complain=*/true);
22744 /* We are done with the current parameter list. */
22745 --parser
->num_template_parameter_lists
;
22747 pop_deferring_access_checks ();
22750 finish_template_decl (parameter_list
);
22752 /* Check the template arguments for a literal operator template. */
22754 && DECL_DECLARES_FUNCTION_P (decl
)
22755 && UDLIT_OPER_P (DECL_NAME (decl
)))
22758 if (parameter_list
== NULL_TREE
)
22762 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
22763 if (num_parms
== 1)
22765 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
22766 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
22767 if (TREE_TYPE (parm
) != char_type_node
22768 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
22771 else if (num_parms
== 2 && cxx_dialect
>= cxx1y
)
22773 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
22774 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
22775 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
22776 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
22777 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
22778 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
22785 error ("literal operator template %qD has invalid parameter list."
22786 " Expected non-type template argument pack <char...>"
22787 " or <typename CharT, CharT...>",
22790 /* Register member declarations. */
22791 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
22792 finish_member_declaration (decl
);
22793 /* For the erroneous case of a template with C linkage, we pushed an
22794 implicit C++ linkage scope; exit that scope now. */
22796 pop_lang_context ();
22797 /* If DECL is a function template, we must return to parse it later.
22798 (Even though there is no definition, there might be default
22799 arguments that need handling.) */
22800 if (member_p
&& decl
22801 && DECL_DECLARES_FUNCTION_P (decl
))
22802 vec_safe_push (unparsed_funs_with_definitions
, decl
);
22805 /* Perform the deferred access checks from a template-parameter-list.
22806 CHECKS is a TREE_LIST of access checks, as returned by
22807 get_deferred_access_checks. */
22810 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
22812 ++processing_template_parmlist
;
22813 perform_access_checks (checks
, tf_warning_or_error
);
22814 --processing_template_parmlist
;
22817 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22818 `function-definition' sequence that follows a template header.
22819 If MEMBER_P is true, this declaration appears in a class scope.
22821 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22822 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22825 cp_parser_single_declaration (cp_parser
* parser
,
22826 vec
<deferred_access_check
, va_gc
> *checks
,
22828 bool explicit_specialization_p
,
22831 int declares_class_or_enum
;
22832 tree decl
= NULL_TREE
;
22833 cp_decl_specifier_seq decl_specifiers
;
22834 bool function_definition_p
= false;
22835 cp_token
*decl_spec_token_start
;
22837 /* This function is only used when processing a template
22839 gcc_assert (innermost_scope_kind () == sk_template_parms
22840 || innermost_scope_kind () == sk_template_spec
);
22842 /* Defer access checks until we know what is being declared. */
22843 push_deferring_access_checks (dk_deferred
);
22845 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22847 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22848 cp_parser_decl_specifier_seq (parser
,
22849 CP_PARSER_FLAGS_OPTIONAL
,
22851 &declares_class_or_enum
);
22853 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
22855 /* There are no template typedefs. */
22856 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
22858 error_at (decl_spec_token_start
->location
,
22859 "template declaration of %<typedef%>");
22860 decl
= error_mark_node
;
22863 /* Gather up the access checks that occurred the
22864 decl-specifier-seq. */
22865 stop_deferring_access_checks ();
22867 /* Check for the declaration of a template class. */
22868 if (declares_class_or_enum
)
22870 if (cp_parser_declares_only_class_p (parser
))
22872 decl
= shadow_tag (&decl_specifiers
);
22877 friend template <typename T> struct A<T>::B;
22880 A<T>::B will be represented by a TYPENAME_TYPE, and
22881 therefore not recognized by shadow_tag. */
22882 if (friend_p
&& *friend_p
22884 && decl_specifiers
.type
22885 && TYPE_P (decl_specifiers
.type
))
22886 decl
= decl_specifiers
.type
;
22888 if (decl
&& decl
!= error_mark_node
)
22889 decl
= TYPE_NAME (decl
);
22891 decl
= error_mark_node
;
22893 /* Perform access checks for template parameters. */
22894 cp_parser_perform_template_parameter_access_checks (checks
);
22898 /* Complain about missing 'typename' or other invalid type names. */
22899 if (!decl_specifiers
.any_type_specifiers_p
22900 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22902 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22903 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22904 the rest of this declaration. */
22905 decl
= error_mark_node
;
22909 /* If it's not a template class, try for a template function. If
22910 the next token is a `;', then this declaration does not declare
22911 anything. But, if there were errors in the decl-specifiers, then
22912 the error might well have come from an attempted class-specifier.
22913 In that case, there's no need to warn about a missing declarator. */
22915 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
22916 || decl_specifiers
.type
!= error_mark_node
))
22918 decl
= cp_parser_init_declarator (parser
,
22921 /*function_definition_allowed_p=*/true,
22923 declares_class_or_enum
,
22924 &function_definition_p
,
22927 /* 7.1.1-1 [dcl.stc]
22929 A storage-class-specifier shall not be specified in an explicit
22930 specialization... */
22932 && explicit_specialization_p
22933 && decl_specifiers
.storage_class
!= sc_none
)
22935 error_at (decl_spec_token_start
->location
,
22936 "explicit template specialization cannot have a storage class");
22937 decl
= error_mark_node
;
22940 if (decl
&& VAR_P (decl
))
22941 check_template_variable (decl
);
22944 /* Look for a trailing `;' after the declaration. */
22945 if (!function_definition_p
22946 && (decl
== error_mark_node
22947 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
22948 cp_parser_skip_to_end_of_block_or_statement (parser
);
22951 pop_deferring_access_checks ();
22953 /* Clear any current qualification; whatever comes next is the start
22954 of something new. */
22955 parser
->scope
= NULL_TREE
;
22956 parser
->qualifying_scope
= NULL_TREE
;
22957 parser
->object_scope
= NULL_TREE
;
22962 /* Parse a cast-expression that is not the operand of a unary "&". */
22965 cp_parser_simple_cast_expression (cp_parser
*parser
)
22967 return cp_parser_cast_expression (parser
, /*address_p=*/false,
22968 /*cast_p=*/false, /*decltype*/false, NULL
);
22971 /* Parse a functional cast to TYPE. Returns an expression
22972 representing the cast. */
22975 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
22977 vec
<tree
, va_gc
> *vec
;
22978 tree expression_list
;
22982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22985 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
22986 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
22987 if (TREE_CODE (type
) == TYPE_DECL
)
22988 type
= TREE_TYPE (type
);
22989 return finish_compound_literal (type
, expression_list
,
22990 tf_warning_or_error
);
22994 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22996 /*allow_expansion_p=*/true,
22997 /*non_constant_p=*/NULL
);
22999 expression_list
= error_mark_node
;
23002 expression_list
= build_tree_list_vec (vec
);
23003 release_tree_vector (vec
);
23006 cast
= build_functional_cast (type
, expression_list
,
23007 tf_warning_or_error
);
23008 /* [expr.const]/1: In an integral constant expression "only type
23009 conversions to integral or enumeration type can be used". */
23010 if (TREE_CODE (type
) == TYPE_DECL
)
23011 type
= TREE_TYPE (type
);
23012 if (cast
!= error_mark_node
23013 && !cast_valid_in_integral_constant_expression_p (type
)
23014 && cp_parser_non_integral_constant_expression (parser
,
23016 return error_mark_node
;
23020 /* Save the tokens that make up the body of a member function defined
23021 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23022 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23023 specifiers applied to the declaration. Returns the FUNCTION_DECL
23024 for the member function. */
23027 cp_parser_save_member_function_body (cp_parser
* parser
,
23028 cp_decl_specifier_seq
*decl_specifiers
,
23029 cp_declarator
*declarator
,
23036 /* Create the FUNCTION_DECL. */
23037 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
23038 cp_finalize_omp_declare_simd (parser
, fn
);
23039 /* If something went badly wrong, bail out now. */
23040 if (fn
== error_mark_node
)
23042 /* If there's a function-body, skip it. */
23043 if (cp_parser_token_starts_function_definition_p
23044 (cp_lexer_peek_token (parser
->lexer
)))
23045 cp_parser_skip_to_end_of_block_or_statement (parser
);
23046 return error_mark_node
;
23049 /* Remember it, if there default args to post process. */
23050 cp_parser_save_default_args (parser
, fn
);
23052 /* Save away the tokens that make up the body of the
23054 first
= parser
->lexer
->next_token
;
23055 /* Handle function try blocks. */
23056 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23057 cp_lexer_consume_token (parser
->lexer
);
23058 /* We can have braced-init-list mem-initializers before the fn body. */
23059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23061 cp_lexer_consume_token (parser
->lexer
);
23062 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23064 /* cache_group will stop after an un-nested { } pair, too. */
23065 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
23068 /* variadic mem-inits have ... after the ')'. */
23069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23070 cp_lexer_consume_token (parser
->lexer
);
23073 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23074 /* Handle function try blocks. */
23075 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
23076 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23077 last
= parser
->lexer
->next_token
;
23079 /* Save away the inline definition; we will process it when the
23080 class is complete. */
23081 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
23082 DECL_PENDING_INLINE_P (fn
) = 1;
23084 /* We need to know that this was defined in the class, so that
23085 friend templates are handled correctly. */
23086 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
23088 /* Add FN to the queue of functions to be parsed later. */
23089 vec_safe_push (unparsed_funs_with_definitions
, fn
);
23094 /* Save the tokens that make up the in-class initializer for a non-static
23095 data member. Returns a DEFAULT_ARG. */
23098 cp_parser_save_nsdmi (cp_parser
* parser
)
23100 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
23103 /* Parse a template-argument-list, as well as the trailing ">" (but
23104 not the opening "<"). See cp_parser_template_argument_list for the
23108 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
23112 tree saved_qualifying_scope
;
23113 tree saved_object_scope
;
23114 bool saved_greater_than_is_operator_p
;
23115 int saved_unevaluated_operand
;
23116 int saved_inhibit_evaluation_warnings
;
23120 When parsing a template-id, the first non-nested `>' is taken as
23121 the end of the template-argument-list rather than a greater-than
23123 saved_greater_than_is_operator_p
23124 = parser
->greater_than_is_operator_p
;
23125 parser
->greater_than_is_operator_p
= false;
23126 /* Parsing the argument list may modify SCOPE, so we save it
23128 saved_scope
= parser
->scope
;
23129 saved_qualifying_scope
= parser
->qualifying_scope
;
23130 saved_object_scope
= parser
->object_scope
;
23131 /* We need to evaluate the template arguments, even though this
23132 template-id may be nested within a "sizeof". */
23133 saved_unevaluated_operand
= cp_unevaluated_operand
;
23134 cp_unevaluated_operand
= 0;
23135 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23136 c_inhibit_evaluation_warnings
= 0;
23137 /* Parse the template-argument-list itself. */
23138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
23139 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23140 arguments
= NULL_TREE
;
23142 arguments
= cp_parser_template_argument_list (parser
);
23143 /* Look for the `>' that ends the template-argument-list. If we find
23144 a '>>' instead, it's probably just a typo. */
23145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23147 if (cxx_dialect
!= cxx98
)
23149 /* In C++0x, a `>>' in a template argument list or cast
23150 expression is considered to be two separate `>'
23151 tokens. So, change the current token to a `>', but don't
23152 consume it: it will be consumed later when the outer
23153 template argument list (or cast expression) is parsed.
23154 Note that this replacement of `>' for `>>' is necessary
23155 even if we are parsing tentatively: in the tentative
23156 case, after calling
23157 cp_parser_enclosed_template_argument_list we will always
23158 throw away all of the template arguments and the first
23159 closing `>', either because the template argument list
23160 was erroneous or because we are replacing those tokens
23161 with a CPP_TEMPLATE_ID token. The second `>' (which will
23162 not have been thrown away) is needed either to close an
23163 outer template argument list or to complete a new-style
23165 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23166 token
->type
= CPP_GREATER
;
23168 else if (!saved_greater_than_is_operator_p
)
23170 /* If we're in a nested template argument list, the '>>' has
23171 to be a typo for '> >'. We emit the error message, but we
23172 continue parsing and we push a '>' as next token, so that
23173 the argument list will be parsed correctly. Note that the
23174 global source location is still on the token before the
23175 '>>', so we need to say explicitly where we want it. */
23176 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23177 error_at (token
->location
, "%<>>%> should be %<> >%> "
23178 "within a nested template argument list");
23180 token
->type
= CPP_GREATER
;
23184 /* If this is not a nested template argument list, the '>>'
23185 is a typo for '>'. Emit an error message and continue.
23186 Same deal about the token location, but here we can get it
23187 right by consuming the '>>' before issuing the diagnostic. */
23188 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
23189 error_at (token
->location
,
23190 "spurious %<>>%>, use %<>%> to terminate "
23191 "a template argument list");
23195 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23196 /* The `>' token might be a greater-than operator again now. */
23197 parser
->greater_than_is_operator_p
23198 = saved_greater_than_is_operator_p
;
23199 /* Restore the SAVED_SCOPE. */
23200 parser
->scope
= saved_scope
;
23201 parser
->qualifying_scope
= saved_qualifying_scope
;
23202 parser
->object_scope
= saved_object_scope
;
23203 cp_unevaluated_operand
= saved_unevaluated_operand
;
23204 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23209 /* MEMBER_FUNCTION is a member function, or a friend. If default
23210 arguments, or the body of the function have not yet been parsed,
23214 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
23216 timevar_push (TV_PARSE_INMETH
);
23217 /* If this member is a template, get the underlying
23219 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
23220 member_function
= DECL_TEMPLATE_RESULT (member_function
);
23222 /* There should not be any class definitions in progress at this
23223 point; the bodies of members are only parsed outside of all class
23225 gcc_assert (parser
->num_classes_being_defined
== 0);
23226 /* While we're parsing the member functions we might encounter more
23227 classes. We want to handle them right away, but we don't want
23228 them getting mixed up with functions that are currently in the
23230 push_unparsed_function_queues (parser
);
23232 /* Make sure that any template parameters are in scope. */
23233 maybe_begin_member_template_processing (member_function
);
23235 /* If the body of the function has not yet been parsed, parse it
23237 if (DECL_PENDING_INLINE_P (member_function
))
23239 tree function_scope
;
23240 cp_token_cache
*tokens
;
23242 /* The function is no longer pending; we are processing it. */
23243 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
23244 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
23245 DECL_PENDING_INLINE_P (member_function
) = 0;
23247 /* If this is a local class, enter the scope of the containing
23249 function_scope
= current_function_decl
;
23250 if (function_scope
)
23251 push_function_context ();
23253 /* Push the body of the function onto the lexer stack. */
23254 cp_parser_push_lexer_for_tokens (parser
, tokens
);
23256 /* Let the front end know that we going to be defining this
23258 start_preparsed_function (member_function
, NULL_TREE
,
23259 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
23261 /* Don't do access checking if it is a templated function. */
23262 if (processing_template_decl
)
23263 push_deferring_access_checks (dk_no_check
);
23265 /* #pragma omp declare reduction needs special parsing. */
23266 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
23268 parser
->lexer
->in_pragma
= true;
23269 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
23270 finish_function (0);
23271 cp_check_omp_declare_reduction (member_function
);
23274 /* Now, parse the body of the function. */
23275 cp_parser_function_definition_after_declarator (parser
,
23276 /*inline_p=*/true);
23278 if (processing_template_decl
)
23279 pop_deferring_access_checks ();
23281 /* Leave the scope of the containing function. */
23282 if (function_scope
)
23283 pop_function_context ();
23284 cp_parser_pop_lexer (parser
);
23287 /* Remove any template parameters from the symbol table. */
23288 maybe_end_member_template_processing ();
23290 /* Restore the queue. */
23291 pop_unparsed_function_queues (parser
);
23292 timevar_pop (TV_PARSE_INMETH
);
23295 /* If DECL contains any default args, remember it on the unparsed
23296 functions queue. */
23299 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
23303 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
23305 probe
= TREE_CHAIN (probe
))
23306 if (TREE_PURPOSE (probe
))
23308 cp_default_arg_entry entry
= {current_class_type
, decl
};
23309 vec_safe_push (unparsed_funs_with_default_args
, entry
);
23314 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23315 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23316 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23317 from the parameter-type-list. */
23320 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
23321 tree default_arg
, tree parmtype
)
23323 cp_token_cache
*tokens
;
23327 if (default_arg
== error_mark_node
)
23328 return error_mark_node
;
23330 /* Push the saved tokens for the default argument onto the parser's
23332 tokens
= DEFARG_TOKENS (default_arg
);
23333 cp_parser_push_lexer_for_tokens (parser
, tokens
);
23335 start_lambda_scope (decl
);
23337 /* Parse the default argument. */
23338 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
23339 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
23340 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23342 finish_lambda_scope ();
23344 if (parsed_arg
== error_mark_node
)
23345 cp_parser_skip_to_end_of_statement (parser
);
23347 if (!processing_template_decl
)
23349 /* In a non-template class, check conversions now. In a template,
23350 we'll wait and instantiate these as needed. */
23351 if (TREE_CODE (decl
) == PARM_DECL
)
23352 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
23353 tf_warning_or_error
);
23356 int flags
= LOOKUP_IMPLICIT
;
23357 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
)
23358 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg
))
23359 flags
= LOOKUP_NORMAL
;
23360 parsed_arg
= digest_init_flags (TREE_TYPE (decl
), parsed_arg
, flags
);
23361 if (TREE_CODE (parsed_arg
) == TARGET_EXPR
)
23362 /* This represents the whole initialization. */
23363 TARGET_EXPR_DIRECT_INIT_P (parsed_arg
) = true;
23367 /* If the token stream has not been completely used up, then
23368 there was extra junk after the end of the default
23370 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
23372 if (TREE_CODE (decl
) == PARM_DECL
)
23373 cp_parser_error (parser
, "expected %<,%>");
23375 cp_parser_error (parser
, "expected %<;%>");
23378 /* Revert to the main lexer. */
23379 cp_parser_pop_lexer (parser
);
23384 /* FIELD is a non-static data member with an initializer which we saved for
23385 later; parse it now. */
23388 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
23392 maybe_begin_member_template_processing (field
);
23394 push_unparsed_function_queues (parser
);
23395 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
23396 DECL_INITIAL (field
),
23398 pop_unparsed_function_queues (parser
);
23400 maybe_end_member_template_processing ();
23402 DECL_INITIAL (field
) = def
;
23405 /* FN is a FUNCTION_DECL which may contains a parameter with an
23406 unparsed DEFAULT_ARG. Parse the default args now. This function
23407 assumes that the current scope is the scope in which the default
23408 argument should be processed. */
23411 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
23413 bool saved_local_variables_forbidden_p
;
23414 tree parm
, parmdecl
;
23416 /* While we're parsing the default args, we might (due to the
23417 statement expression extension) encounter more classes. We want
23418 to handle them right away, but we don't want them getting mixed
23419 up with default args that are currently in the queue. */
23420 push_unparsed_function_queues (parser
);
23422 /* Local variable names (and the `this' keyword) may not appear
23423 in a default argument. */
23424 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
23425 parser
->local_variables_forbidden_p
= true;
23427 push_defarg_context (fn
);
23429 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
23430 parmdecl
= DECL_ARGUMENTS (fn
);
23431 parm
&& parm
!= void_list_node
;
23432 parm
= TREE_CHAIN (parm
),
23433 parmdecl
= DECL_CHAIN (parmdecl
))
23435 tree default_arg
= TREE_PURPOSE (parm
);
23437 vec
<tree
, va_gc
> *insts
;
23444 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
23445 /* This can happen for a friend declaration for a function
23446 already declared with default arguments. */
23450 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
23452 TREE_VALUE (parm
));
23453 if (parsed_arg
== error_mark_node
)
23458 TREE_PURPOSE (parm
) = parsed_arg
;
23460 /* Update any instantiations we've already created. */
23461 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
23462 vec_safe_iterate (insts
, ix
, ©
); ix
++)
23463 TREE_PURPOSE (copy
) = parsed_arg
;
23466 pop_defarg_context ();
23468 /* Make sure no default arg is missing. */
23469 check_default_args (fn
);
23471 /* Restore the state of local_variables_forbidden_p. */
23472 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
23474 /* Restore the queue. */
23475 pop_unparsed_function_queues (parser
);
23478 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23480 sizeof ... ( identifier )
23482 where the 'sizeof' token has already been consumed. */
23485 cp_parser_sizeof_pack (cp_parser
*parser
)
23487 /* Consume the `...'. */
23488 cp_lexer_consume_token (parser
->lexer
);
23489 maybe_warn_variadic_templates ();
23491 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
23493 cp_lexer_consume_token (parser
->lexer
);
23495 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
23496 "%<sizeof...%> argument must be surrounded by parentheses");
23498 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23499 tree name
= cp_parser_identifier (parser
);
23500 if (name
== error_mark_node
)
23501 return error_mark_node
;
23502 /* The name is not qualified. */
23503 parser
->scope
= NULL_TREE
;
23504 parser
->qualifying_scope
= NULL_TREE
;
23505 parser
->object_scope
= NULL_TREE
;
23506 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
23507 if (expr
== error_mark_node
)
23508 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
23510 if (TREE_CODE (expr
) == TYPE_DECL
)
23511 expr
= TREE_TYPE (expr
);
23512 else if (TREE_CODE (expr
) == CONST_DECL
)
23513 expr
= DECL_INITIAL (expr
);
23514 expr
= make_pack_expansion (expr
);
23517 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23522 /* Parse the operand of `sizeof' (or a similar operator). Returns
23523 either a TYPE or an expression, depending on the form of the
23524 input. The KEYWORD indicates which kind of expression we have
23528 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
23530 tree expr
= NULL_TREE
;
23531 const char *saved_message
;
23533 bool saved_integral_constant_expression_p
;
23534 bool saved_non_integral_constant_expression_p
;
23536 /* If it's a `...', then we are computing the length of a parameter
23538 if (keyword
== RID_SIZEOF
23539 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23540 return cp_parser_sizeof_pack (parser
);
23542 /* Types cannot be defined in a `sizeof' expression. Save away the
23544 saved_message
= parser
->type_definition_forbidden_message
;
23545 /* And create the new one. */
23546 tmp
= concat ("types may not be defined in %<",
23547 IDENTIFIER_POINTER (ridpointers
[keyword
]),
23548 "%> expressions", NULL
);
23549 parser
->type_definition_forbidden_message
= tmp
;
23551 /* The restrictions on constant-expressions do not apply inside
23552 sizeof expressions. */
23553 saved_integral_constant_expression_p
23554 = parser
->integral_constant_expression_p
;
23555 saved_non_integral_constant_expression_p
23556 = parser
->non_integral_constant_expression_p
;
23557 parser
->integral_constant_expression_p
= false;
23559 /* Do not actually evaluate the expression. */
23560 ++cp_unevaluated_operand
;
23561 ++c_inhibit_evaluation_warnings
;
23562 /* If it's a `(', then we might be looking at the type-id
23564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23566 tree type
= NULL_TREE
;
23567 bool compound_literal_p
;
23569 /* We can't be sure yet whether we're looking at a type-id or an
23571 cp_parser_parse_tentatively (parser
);
23572 /* Consume the `('. */
23573 cp_lexer_consume_token (parser
->lexer
);
23574 /* Note: as a GNU Extension, compound literals are considered
23575 postfix-expressions as they are in C99, so they are valid
23576 arguments to sizeof. See comment in cp_parser_cast_expression
23578 cp_lexer_save_tokens (parser
->lexer
);
23579 /* Skip tokens until the next token is a closing parenthesis.
23580 If we find the closing `)', and the next token is a `{', then
23581 we are looking at a compound-literal. */
23583 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
23584 /*consume_paren=*/true)
23585 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
23586 /* Roll back the tokens we skipped. */
23587 cp_lexer_rollback_tokens (parser
->lexer
);
23588 /* If we were looking at a compound-literal, simulate an error
23589 so that the call to cp_parser_parse_definitely below will
23591 if (compound_literal_p
)
23592 cp_parser_simulate_error (parser
);
23595 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
23596 parser
->in_type_id_in_expr_p
= true;
23597 /* Look for the type-id. */
23598 type
= cp_parser_type_id (parser
);
23599 /* Look for the closing `)'. */
23600 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23601 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
23604 /* If all went well, then we're done. */
23605 if (cp_parser_parse_definitely (parser
))
23607 cp_decl_specifier_seq decl_specs
;
23609 /* Build a trivial decl-specifier-seq. */
23610 clear_decl_specs (&decl_specs
);
23611 decl_specs
.type
= type
;
23613 /* Call grokdeclarator to figure out what type this is. */
23614 expr
= grokdeclarator (NULL
,
23618 /*attrlist=*/NULL
);
23622 /* If the type-id production did not work out, then we must be
23623 looking at the unary-expression production. */
23625 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
23626 /*cast_p=*/false, NULL
);
23628 /* Go back to evaluating expressions. */
23629 --cp_unevaluated_operand
;
23630 --c_inhibit_evaluation_warnings
;
23632 /* Free the message we created. */
23634 /* And restore the old one. */
23635 parser
->type_definition_forbidden_message
= saved_message
;
23636 parser
->integral_constant_expression_p
23637 = saved_integral_constant_expression_p
;
23638 parser
->non_integral_constant_expression_p
23639 = saved_non_integral_constant_expression_p
;
23644 /* If the current declaration has no declarator, return true. */
23647 cp_parser_declares_only_class_p (cp_parser
*parser
)
23649 /* If the next token is a `;' or a `,' then there is no
23651 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
23652 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
23655 /* Update the DECL_SPECS to reflect the storage class indicated by
23659 cp_parser_set_storage_class (cp_parser
*parser
,
23660 cp_decl_specifier_seq
*decl_specs
,
23664 cp_storage_class storage_class
;
23666 if (parser
->in_unbraced_linkage_specification_p
)
23668 error_at (token
->location
, "invalid use of %qD in linkage specification",
23669 ridpointers
[keyword
]);
23672 else if (decl_specs
->storage_class
!= sc_none
)
23674 decl_specs
->conflicting_specifiers_p
= true;
23678 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
23679 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
23680 && decl_specs
->gnu_thread_keyword_p
)
23682 pedwarn (decl_specs
->locations
[ds_thread
], 0,
23683 "%<__thread%> before %qD", ridpointers
[keyword
]);
23689 storage_class
= sc_auto
;
23692 storage_class
= sc_register
;
23695 storage_class
= sc_static
;
23698 storage_class
= sc_extern
;
23701 storage_class
= sc_mutable
;
23704 gcc_unreachable ();
23706 decl_specs
->storage_class
= storage_class
;
23707 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
23709 /* A storage class specifier cannot be applied alongside a typedef
23710 specifier. If there is a typedef specifier present then set
23711 conflicting_specifiers_p which will trigger an error later
23712 on in grokdeclarator. */
23713 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
23714 decl_specs
->conflicting_specifiers_p
= true;
23717 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23718 is true, the type is a class or enum definition. */
23721 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
23724 bool type_definition_p
)
23726 decl_specs
->any_specifiers_p
= true;
23728 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23729 (with, for example, in "typedef int wchar_t;") we remember that
23730 this is what happened. In system headers, we ignore these
23731 declarations so that G++ can work with system headers that are not
23733 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
23734 && !type_definition_p
23735 && (type_spec
== boolean_type_node
23736 || type_spec
== char16_type_node
23737 || type_spec
== char32_type_node
23738 || type_spec
== wchar_type_node
)
23739 && (decl_specs
->type
23740 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
23741 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
23742 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
23743 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
23745 decl_specs
->redefined_builtin_type
= type_spec
;
23746 set_and_check_decl_spec_loc (decl_specs
,
23747 ds_redefined_builtin_type_spec
,
23749 if (!decl_specs
->type
)
23751 decl_specs
->type
= type_spec
;
23752 decl_specs
->type_definition_p
= false;
23753 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
23756 else if (decl_specs
->type
)
23757 decl_specs
->multiple_types_p
= true;
23760 decl_specs
->type
= type_spec
;
23761 decl_specs
->type_definition_p
= type_definition_p
;
23762 decl_specs
->redefined_builtin_type
= NULL_TREE
;
23763 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
23767 /* True iff TOKEN is the GNU keyword __thread. */
23770 token_is__thread (cp_token
*token
)
23772 gcc_assert (token
->keyword
== RID_THREAD
);
23773 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
23776 /* Set the location for a declarator specifier and check if it is
23779 DECL_SPECS is the sequence of declarator specifiers onto which to
23782 DS is the single declarator specifier to set which location is to
23783 be set onto the existing sequence of declarators.
23785 LOCATION is the location for the declarator specifier to
23789 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
23790 cp_decl_spec ds
, cp_token
*token
)
23792 gcc_assert (ds
< ds_last
);
23794 if (decl_specs
== NULL
)
23797 source_location location
= token
->location
;
23799 if (decl_specs
->locations
[ds
] == 0)
23801 decl_specs
->locations
[ds
] = location
;
23802 if (ds
== ds_thread
)
23803 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
23809 if (decl_specs
->locations
[ds_long_long
] != 0)
23810 error_at (location
,
23811 "%<long long long%> is too long for GCC");
23814 decl_specs
->locations
[ds_long_long
] = location
;
23815 pedwarn_cxx98 (location
,
23817 "ISO C++ 1998 does not support %<long long%>");
23820 else if (ds
== ds_thread
)
23822 bool gnu
= token_is__thread (token
);
23823 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
23824 error_at (location
,
23825 "both %<__thread%> and %<thread_local%> specified");
23827 error_at (location
, "duplicate %qD", token
->u
.value
);
23831 static const char *const decl_spec_names
[] = {
23848 error_at (location
,
23849 "duplicate %qs", decl_spec_names
[ds
]);
23854 /* Return true iff the declarator specifier DS is present in the
23855 sequence of declarator specifiers DECL_SPECS. */
23858 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
23861 gcc_assert (ds
< ds_last
);
23863 if (decl_specs
== NULL
)
23866 return decl_specs
->locations
[ds
] != 0;
23869 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23870 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23873 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
23875 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
23878 /* Issue an error message indicating that TOKEN_DESC was expected.
23879 If KEYWORD is true, it indicated this function is called by
23880 cp_parser_require_keword and the required token can only be
23881 a indicated keyword. */
23884 cp_parser_required_error (cp_parser
*parser
,
23885 required_token token_desc
,
23888 switch (token_desc
)
23891 cp_parser_error (parser
, "expected %<new%>");
23894 cp_parser_error (parser
, "expected %<delete%>");
23897 cp_parser_error (parser
, "expected %<return%>");
23900 cp_parser_error (parser
, "expected %<while%>");
23903 cp_parser_error (parser
, "expected %<extern%>");
23905 case RT_STATIC_ASSERT
:
23906 cp_parser_error (parser
, "expected %<static_assert%>");
23909 cp_parser_error (parser
, "expected %<decltype%>");
23912 cp_parser_error (parser
, "expected %<operator%>");
23915 cp_parser_error (parser
, "expected %<class%>");
23918 cp_parser_error (parser
, "expected %<template%>");
23921 cp_parser_error (parser
, "expected %<namespace%>");
23924 cp_parser_error (parser
, "expected %<using%>");
23927 cp_parser_error (parser
, "expected %<asm%>");
23930 cp_parser_error (parser
, "expected %<try%>");
23933 cp_parser_error (parser
, "expected %<catch%>");
23936 cp_parser_error (parser
, "expected %<throw%>");
23939 cp_parser_error (parser
, "expected %<__label__%>");
23942 cp_parser_error (parser
, "expected %<@try%>");
23944 case RT_AT_SYNCHRONIZED
:
23945 cp_parser_error (parser
, "expected %<@synchronized%>");
23948 cp_parser_error (parser
, "expected %<@throw%>");
23950 case RT_TRANSACTION_ATOMIC
:
23951 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
23953 case RT_TRANSACTION_RELAXED
:
23954 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
23961 switch (token_desc
)
23964 cp_parser_error (parser
, "expected %<;%>");
23966 case RT_OPEN_PAREN
:
23967 cp_parser_error (parser
, "expected %<(%>");
23969 case RT_CLOSE_BRACE
:
23970 cp_parser_error (parser
, "expected %<}%>");
23972 case RT_OPEN_BRACE
:
23973 cp_parser_error (parser
, "expected %<{%>");
23975 case RT_CLOSE_SQUARE
:
23976 cp_parser_error (parser
, "expected %<]%>");
23978 case RT_OPEN_SQUARE
:
23979 cp_parser_error (parser
, "expected %<[%>");
23982 cp_parser_error (parser
, "expected %<,%>");
23985 cp_parser_error (parser
, "expected %<::%>");
23988 cp_parser_error (parser
, "expected %<<%>");
23991 cp_parser_error (parser
, "expected %<>%>");
23994 cp_parser_error (parser
, "expected %<=%>");
23997 cp_parser_error (parser
, "expected %<...%>");
24000 cp_parser_error (parser
, "expected %<*%>");
24003 cp_parser_error (parser
, "expected %<~%>");
24006 cp_parser_error (parser
, "expected %<:%>");
24008 case RT_COLON_SCOPE
:
24009 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24011 case RT_CLOSE_PAREN
:
24012 cp_parser_error (parser
, "expected %<)%>");
24014 case RT_COMMA_CLOSE_PAREN
:
24015 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24017 case RT_PRAGMA_EOL
:
24018 cp_parser_error (parser
, "expected end of line");
24021 cp_parser_error (parser
, "expected identifier");
24024 cp_parser_error (parser
, "expected selection-statement");
24026 case RT_INTERATION
:
24027 cp_parser_error (parser
, "expected iteration-statement");
24030 cp_parser_error (parser
, "expected jump-statement");
24033 cp_parser_error (parser
, "expected class-key");
24035 case RT_CLASS_TYPENAME_TEMPLATE
:
24036 cp_parser_error (parser
,
24037 "expected %<class%>, %<typename%>, or %<template%>");
24040 gcc_unreachable ();
24044 gcc_unreachable ();
24049 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24050 issue an error message indicating that TOKEN_DESC was expected.
24052 Returns the token consumed, if the token had the appropriate type.
24053 Otherwise, returns NULL. */
24056 cp_parser_require (cp_parser
* parser
,
24057 enum cpp_ttype type
,
24058 required_token token_desc
)
24060 if (cp_lexer_next_token_is (parser
->lexer
, type
))
24061 return cp_lexer_consume_token (parser
->lexer
);
24064 /* Output the MESSAGE -- unless we're parsing tentatively. */
24065 if (!cp_parser_simulate_error (parser
))
24066 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
24071 /* An error message is produced if the next token is not '>'.
24072 All further tokens are skipped until the desired token is
24073 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24076 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
24078 /* Current level of '< ... >'. */
24079 unsigned level
= 0;
24080 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24081 unsigned nesting_depth
= 0;
24083 /* Are we ready, yet? If not, issue error message. */
24084 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
24087 /* Skip tokens until the desired token is found. */
24090 /* Peek at the next token. */
24091 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24094 if (!nesting_depth
)
24099 if (cxx_dialect
== cxx98
)
24100 /* C++0x views the `>>' operator as two `>' tokens, but
24103 else if (!nesting_depth
&& level
-- == 0)
24105 /* We've hit a `>>' where the first `>' closes the
24106 template argument list, and the second `>' is
24107 spurious. Just consume the `>>' and stop; we've
24108 already produced at least one error. */
24109 cp_lexer_consume_token (parser
->lexer
);
24112 /* Fall through for C++0x, so we handle the second `>' in
24116 if (!nesting_depth
&& level
-- == 0)
24118 /* We've reached the token we want, consume it and stop. */
24119 cp_lexer_consume_token (parser
->lexer
);
24124 case CPP_OPEN_PAREN
:
24125 case CPP_OPEN_SQUARE
:
24129 case CPP_CLOSE_PAREN
:
24130 case CPP_CLOSE_SQUARE
:
24131 if (nesting_depth
-- == 0)
24136 case CPP_PRAGMA_EOL
:
24137 case CPP_SEMICOLON
:
24138 case CPP_OPEN_BRACE
:
24139 case CPP_CLOSE_BRACE
:
24140 /* The '>' was probably forgotten, don't look further. */
24147 /* Consume this token. */
24148 cp_lexer_consume_token (parser
->lexer
);
24152 /* If the next token is the indicated keyword, consume it. Otherwise,
24153 issue an error message indicating that TOKEN_DESC was expected.
24155 Returns the token consumed, if the token had the appropriate type.
24156 Otherwise, returns NULL. */
24159 cp_parser_require_keyword (cp_parser
* parser
,
24161 required_token token_desc
)
24163 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
24165 if (token
&& token
->keyword
!= keyword
)
24167 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
24174 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24175 function-definition. */
24178 cp_parser_token_starts_function_definition_p (cp_token
* token
)
24180 return (/* An ordinary function-body begins with an `{'. */
24181 token
->type
== CPP_OPEN_BRACE
24182 /* A ctor-initializer begins with a `:'. */
24183 || token
->type
== CPP_COLON
24184 /* A function-try-block begins with `try'. */
24185 || token
->keyword
== RID_TRY
24186 /* A function-transaction-block begins with `__transaction_atomic'
24187 or `__transaction_relaxed'. */
24188 || token
->keyword
== RID_TRANSACTION_ATOMIC
24189 || token
->keyword
== RID_TRANSACTION_RELAXED
24190 /* The named return value extension begins with `return'. */
24191 || token
->keyword
== RID_RETURN
);
24194 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24198 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
24202 token
= cp_lexer_peek_token (parser
->lexer
);
24203 return (token
->type
== CPP_OPEN_BRACE
24204 || (token
->type
== CPP_COLON
24205 && !parser
->colon_doesnt_start_class_def_p
));
24208 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24209 C++0x) ending a template-argument. */
24212 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
24216 token
= cp_lexer_peek_token (parser
->lexer
);
24217 return (token
->type
== CPP_COMMA
24218 || token
->type
== CPP_GREATER
24219 || token
->type
== CPP_ELLIPSIS
24220 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
24223 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24224 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24227 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
24232 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24233 if (token
->type
== CPP_LESS
)
24235 /* Check for the sequence `<::' in the original code. It would be lexed as
24236 `[:', where `[' is a digraph, and there is no whitespace before
24238 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
24241 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
24242 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
24248 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24249 or none_type otherwise. */
24251 static enum tag_types
24252 cp_parser_token_is_class_key (cp_token
* token
)
24254 switch (token
->keyword
)
24259 return record_type
;
24268 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24271 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
24273 if (type
== error_mark_node
)
24275 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
24277 if (permerror (input_location
, "%qs tag used in naming %q#T",
24278 class_key
== union_type
? "union"
24279 : class_key
== record_type
? "struct" : "class",
24281 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
24282 "%q#T was previously declared here", type
);
24286 /* Issue an error message if DECL is redeclared with different
24287 access than its original declaration [class.access.spec/3].
24288 This applies to nested classes and nested class templates.
24292 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
24294 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
24297 if ((TREE_PRIVATE (decl
)
24298 != (current_access_specifier
== access_private_node
))
24299 || (TREE_PROTECTED (decl
)
24300 != (current_access_specifier
== access_protected_node
)))
24301 error_at (location
, "%qD redeclared with different access", decl
);
24304 /* Look for the `template' keyword, as a syntactic disambiguator.
24305 Return TRUE iff it is present, in which case it will be
24309 cp_parser_optional_template_keyword (cp_parser
*parser
)
24311 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24313 /* In C++98 the `template' keyword can only be used within templates;
24314 outside templates the parser can always figure out what is a
24315 template and what is not. In C++11, per the resolution of DR 468,
24316 `template' is allowed in cases where it is not strictly necessary. */
24317 if (!processing_template_decl
24318 && pedantic
&& cxx_dialect
== cxx98
)
24320 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24321 pedwarn (token
->location
, OPT_Wpedantic
,
24322 "in C++98 %<template%> (as a disambiguator) is only "
24323 "allowed within templates");
24324 /* If this part of the token stream is rescanned, the same
24325 error message would be generated. So, we purge the token
24326 from the stream. */
24327 cp_lexer_purge_token (parser
->lexer
);
24332 /* Consume the `template' keyword. */
24333 cp_lexer_consume_token (parser
->lexer
);
24340 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24341 set PARSER->SCOPE, and perform other related actions. */
24344 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
24347 struct tree_check
*check_value
;
24348 deferred_access_check
*chk
;
24349 vec
<deferred_access_check
, va_gc
> *checks
;
24351 /* Get the stored value. */
24352 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
24353 /* Perform any access checks that were deferred. */
24354 checks
= check_value
->checks
;
24357 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
24358 perform_or_defer_access_check (chk
->binfo
,
24360 chk
->diag_decl
, tf_warning_or_error
);
24362 /* Set the scope from the stored value. */
24363 parser
->scope
= check_value
->value
;
24364 parser
->qualifying_scope
= check_value
->qualifying_scope
;
24365 parser
->object_scope
= NULL_TREE
;
24368 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24369 encounter the end of a block before what we were looking for. */
24372 cp_parser_cache_group (cp_parser
*parser
,
24373 enum cpp_ttype end
,
24378 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24380 /* Abort a parenthesized expression if we encounter a semicolon. */
24381 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
24382 && token
->type
== CPP_SEMICOLON
)
24384 /* If we've reached the end of the file, stop. */
24385 if (token
->type
== CPP_EOF
24386 || (end
!= CPP_PRAGMA_EOL
24387 && token
->type
== CPP_PRAGMA_EOL
))
24389 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
24390 /* We've hit the end of an enclosing block, so there's been some
24391 kind of syntax error. */
24394 /* Consume the token. */
24395 cp_lexer_consume_token (parser
->lexer
);
24396 /* See if it starts a new group. */
24397 if (token
->type
== CPP_OPEN_BRACE
)
24399 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
24400 /* In theory this should probably check end == '}', but
24401 cp_parser_save_member_function_body needs it to exit
24402 after either '}' or ')' when called with ')'. */
24406 else if (token
->type
== CPP_OPEN_PAREN
)
24408 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
24409 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
24412 else if (token
->type
== CPP_PRAGMA
)
24413 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
24414 else if (token
->type
== end
)
24419 /* Like above, for caching a default argument or NSDMI. Both of these are
24420 terminated by a non-nested comma, but it can be unclear whether or not a
24421 comma is nested in a template argument list unless we do more parsing.
24422 In order to handle this ambiguity, when we encounter a ',' after a '<'
24423 we try to parse what follows as a parameter-declaration-list (in the
24424 case of a default argument) or a member-declarator (in the case of an
24425 NSDMI). If that succeeds, then we stop caching. */
24428 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
24430 unsigned depth
= 0;
24431 int maybe_template_id
= 0;
24432 cp_token
*first_token
;
24434 tree default_argument
;
24436 /* Add tokens until we have processed the entire default
24437 argument. We add the range [first_token, token). */
24438 first_token
= cp_lexer_peek_token (parser
->lexer
);
24439 if (first_token
->type
== CPP_OPEN_BRACE
)
24441 /* For list-initialization, this is straightforward. */
24442 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
24443 token
= cp_lexer_peek_token (parser
->lexer
);
24449 /* Peek at the next token. */
24450 token
= cp_lexer_peek_token (parser
->lexer
);
24451 /* What we do depends on what token we have. */
24452 switch (token
->type
)
24454 /* In valid code, a default argument must be
24455 immediately followed by a `,' `)', or `...'. */
24457 if (depth
== 0 && maybe_template_id
)
24459 /* If we've seen a '<', we might be in a
24460 template-argument-list. Until Core issue 325 is
24461 resolved, we don't know how this situation ought
24462 to be handled, so try to DTRT. We check whether
24463 what comes after the comma is a valid parameter
24464 declaration list. If it is, then the comma ends
24465 the default argument; otherwise the default
24466 argument continues. */
24467 bool error
= false;
24469 /* Set ITALP so cp_parser_parameter_declaration_list
24470 doesn't decide to commit to this parse. */
24471 bool saved_italp
= parser
->in_template_argument_list_p
;
24472 parser
->in_template_argument_list_p
= true;
24474 cp_parser_parse_tentatively (parser
);
24475 cp_lexer_consume_token (parser
->lexer
);
24479 int ctor_dtor_or_conv_p
;
24480 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24481 &ctor_dtor_or_conv_p
,
24482 /*parenthesized_p=*/NULL
,
24483 /*member_p=*/true);
24487 begin_scope (sk_function_parms
, NULL_TREE
);
24488 cp_parser_parameter_declaration_list (parser
, &error
);
24489 pop_bindings_and_leave_scope ();
24491 if (!cp_parser_error_occurred (parser
) && !error
)
24493 cp_parser_abort_tentative_parse (parser
);
24495 parser
->in_template_argument_list_p
= saved_italp
;
24498 case CPP_CLOSE_PAREN
:
24500 /* If we run into a non-nested `;', `}', or `]',
24501 then the code is invalid -- but the default
24502 argument is certainly over. */
24503 case CPP_SEMICOLON
:
24504 case CPP_CLOSE_BRACE
:
24505 case CPP_CLOSE_SQUARE
:
24507 /* Handle correctly int n = sizeof ... ( p ); */
24508 && !(nsdmi
&& token
->type
== CPP_ELLIPSIS
))
24510 /* Update DEPTH, if necessary. */
24511 else if (token
->type
== CPP_CLOSE_PAREN
24512 || token
->type
== CPP_CLOSE_BRACE
24513 || token
->type
== CPP_CLOSE_SQUARE
)
24517 case CPP_OPEN_PAREN
:
24518 case CPP_OPEN_SQUARE
:
24519 case CPP_OPEN_BRACE
:
24525 /* This might be the comparison operator, or it might
24526 start a template argument list. */
24527 ++maybe_template_id
;
24531 if (cxx_dialect
== cxx98
)
24533 /* Fall through for C++0x, which treats the `>>'
24534 operator like two `>' tokens in certain
24540 /* This might be an operator, or it might close a
24541 template argument list. But if a previous '<'
24542 started a template argument list, this will have
24543 closed it, so we can't be in one anymore. */
24544 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
24545 if (maybe_template_id
< 0)
24546 maybe_template_id
= 0;
24550 /* If we run out of tokens, issue an error message. */
24552 case CPP_PRAGMA_EOL
:
24553 error_at (token
->location
, "file ends in default argument");
24559 /* In these cases, we should look for template-ids.
24560 For example, if the default argument is
24561 `X<int, double>()', we need to do name lookup to
24562 figure out whether or not `X' is a template; if
24563 so, the `,' does not end the default argument.
24565 That is not yet done. */
24572 /* If we've reached the end, stop. */
24576 /* Add the token to the token block. */
24577 token
= cp_lexer_consume_token (parser
->lexer
);
24580 /* Create a DEFAULT_ARG to represent the unparsed default
24582 default_argument
= make_node (DEFAULT_ARG
);
24583 DEFARG_TOKENS (default_argument
)
24584 = cp_token_cache_new (first_token
, token
);
24585 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
24587 return default_argument
;
24590 /* Begin parsing tentatively. We always save tokens while parsing
24591 tentatively so that if the tentative parsing fails we can restore the
24595 cp_parser_parse_tentatively (cp_parser
* parser
)
24597 /* Enter a new parsing context. */
24598 parser
->context
= cp_parser_context_new (parser
->context
);
24599 /* Begin saving tokens. */
24600 cp_lexer_save_tokens (parser
->lexer
);
24601 /* In order to avoid repetitive access control error messages,
24602 access checks are queued up until we are no longer parsing
24604 push_deferring_access_checks (dk_deferred
);
24607 /* Commit to the currently active tentative parse. */
24610 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
24612 cp_parser_context
*context
;
24615 /* Mark all of the levels as committed. */
24616 lexer
= parser
->lexer
;
24617 for (context
= parser
->context
; context
->next
; context
= context
->next
)
24619 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24621 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24622 while (!cp_lexer_saving_tokens (lexer
))
24623 lexer
= lexer
->next
;
24624 cp_lexer_commit_tokens (lexer
);
24628 /* Commit to the topmost currently active tentative parse.
24630 Note that this function shouldn't be called when there are
24631 irreversible side-effects while in a tentative state. For
24632 example, we shouldn't create a permanent entry in the symbol
24633 table, or issue an error message that might not apply if the
24634 tentative parse is aborted. */
24637 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
24639 cp_parser_context
*context
= parser
->context
;
24640 cp_lexer
*lexer
= parser
->lexer
;
24644 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24646 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24648 while (!cp_lexer_saving_tokens (lexer
))
24649 lexer
= lexer
->next
;
24650 cp_lexer_commit_tokens (lexer
);
24654 /* Abort the currently active tentative parse. All consumed tokens
24655 will be rolled back, and no diagnostics will be issued. */
24658 cp_parser_abort_tentative_parse (cp_parser
* parser
)
24660 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
24661 || errorcount
> 0);
24662 cp_parser_simulate_error (parser
);
24663 /* Now, pretend that we want to see if the construct was
24664 successfully parsed. */
24665 cp_parser_parse_definitely (parser
);
24668 /* Stop parsing tentatively. If a parse error has occurred, restore the
24669 token stream. Otherwise, commit to the tokens we have consumed.
24670 Returns true if no error occurred; false otherwise. */
24673 cp_parser_parse_definitely (cp_parser
* parser
)
24675 bool error_occurred
;
24676 cp_parser_context
*context
;
24678 /* Remember whether or not an error occurred, since we are about to
24679 destroy that information. */
24680 error_occurred
= cp_parser_error_occurred (parser
);
24681 /* Remove the topmost context from the stack. */
24682 context
= parser
->context
;
24683 parser
->context
= context
->next
;
24684 /* If no parse errors occurred, commit to the tentative parse. */
24685 if (!error_occurred
)
24687 /* Commit to the tokens read tentatively, unless that was
24689 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
24690 cp_lexer_commit_tokens (parser
->lexer
);
24692 pop_to_parent_deferring_access_checks ();
24694 /* Otherwise, if errors occurred, roll back our state so that things
24695 are just as they were before we began the tentative parse. */
24698 cp_lexer_rollback_tokens (parser
->lexer
);
24699 pop_deferring_access_checks ();
24701 /* Add the context to the front of the free list. */
24702 context
->next
= cp_parser_context_free_list
;
24703 cp_parser_context_free_list
= context
;
24705 return !error_occurred
;
24708 /* Returns true if we are parsing tentatively and are not committed to
24709 this tentative parse. */
24712 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
24714 return (cp_parser_parsing_tentatively (parser
)
24715 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
24718 /* Returns nonzero iff an error has occurred during the most recent
24719 tentative parse. */
24722 cp_parser_error_occurred (cp_parser
* parser
)
24724 return (cp_parser_parsing_tentatively (parser
)
24725 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
24728 /* Returns nonzero if GNU extensions are allowed. */
24731 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
24733 return parser
->allow_gnu_extensions_p
;
24736 /* Objective-C++ Productions */
24739 /* Parse an Objective-C expression, which feeds into a primary-expression
24743 objc-message-expression
24744 objc-string-literal
24745 objc-encode-expression
24746 objc-protocol-expression
24747 objc-selector-expression
24749 Returns a tree representation of the expression. */
24752 cp_parser_objc_expression (cp_parser
* parser
)
24754 /* Try to figure out what kind of declaration is present. */
24755 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
24759 case CPP_OPEN_SQUARE
:
24760 return cp_parser_objc_message_expression (parser
);
24762 case CPP_OBJC_STRING
:
24763 kwd
= cp_lexer_consume_token (parser
->lexer
);
24764 return objc_build_string_object (kwd
->u
.value
);
24767 switch (kwd
->keyword
)
24769 case RID_AT_ENCODE
:
24770 return cp_parser_objc_encode_expression (parser
);
24772 case RID_AT_PROTOCOL
:
24773 return cp_parser_objc_protocol_expression (parser
);
24775 case RID_AT_SELECTOR
:
24776 return cp_parser_objc_selector_expression (parser
);
24782 error_at (kwd
->location
,
24783 "misplaced %<@%D%> Objective-C++ construct",
24785 cp_parser_skip_to_end_of_block_or_statement (parser
);
24788 return error_mark_node
;
24791 /* Parse an Objective-C message expression.
24793 objc-message-expression:
24794 [ objc-message-receiver objc-message-args ]
24796 Returns a representation of an Objective-C message. */
24799 cp_parser_objc_message_expression (cp_parser
* parser
)
24801 tree receiver
, messageargs
;
24803 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
24804 receiver
= cp_parser_objc_message_receiver (parser
);
24805 messageargs
= cp_parser_objc_message_args (parser
);
24806 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24808 return objc_build_message_expr (receiver
, messageargs
);
24811 /* Parse an objc-message-receiver.
24813 objc-message-receiver:
24815 simple-type-specifier
24817 Returns a representation of the type or expression. */
24820 cp_parser_objc_message_receiver (cp_parser
* parser
)
24824 /* An Objective-C message receiver may be either (1) a type
24825 or (2) an expression. */
24826 cp_parser_parse_tentatively (parser
);
24827 rcv
= cp_parser_expression (parser
, false, NULL
);
24829 if (cp_parser_parse_definitely (parser
))
24832 rcv
= cp_parser_simple_type_specifier (parser
,
24833 /*decl_specs=*/NULL
,
24834 CP_PARSER_FLAGS_NONE
);
24836 return objc_get_class_reference (rcv
);
24839 /* Parse the arguments and selectors comprising an Objective-C message.
24844 objc-selector-args , objc-comma-args
24846 objc-selector-args:
24847 objc-selector [opt] : assignment-expression
24848 objc-selector-args objc-selector [opt] : assignment-expression
24851 assignment-expression
24852 objc-comma-args , assignment-expression
24854 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24855 selector arguments and TREE_VALUE containing a list of comma
24859 cp_parser_objc_message_args (cp_parser
* parser
)
24861 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
24862 bool maybe_unary_selector_p
= true;
24863 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24865 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
24867 tree selector
= NULL_TREE
, arg
;
24869 if (token
->type
!= CPP_COLON
)
24870 selector
= cp_parser_objc_selector (parser
);
24872 /* Detect if we have a unary selector. */
24873 if (maybe_unary_selector_p
24874 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
24875 return build_tree_list (selector
, NULL_TREE
);
24877 maybe_unary_selector_p
= false;
24878 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24879 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
24882 = chainon (sel_args
,
24883 build_tree_list (selector
, arg
));
24885 token
= cp_lexer_peek_token (parser
->lexer
);
24888 /* Handle non-selector arguments, if any. */
24889 while (token
->type
== CPP_COMMA
)
24893 cp_lexer_consume_token (parser
->lexer
);
24894 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
24897 = chainon (addl_args
,
24898 build_tree_list (NULL_TREE
, arg
));
24900 token
= cp_lexer_peek_token (parser
->lexer
);
24903 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
24905 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
24906 return build_tree_list (error_mark_node
, error_mark_node
);
24909 return build_tree_list (sel_args
, addl_args
);
24912 /* Parse an Objective-C encode expression.
24914 objc-encode-expression:
24915 @encode objc-typename
24917 Returns an encoded representation of the type argument. */
24920 cp_parser_objc_encode_expression (cp_parser
* parser
)
24925 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
24926 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24927 token
= cp_lexer_peek_token (parser
->lexer
);
24928 type
= complete_type (cp_parser_type_id (parser
));
24929 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24933 error_at (token
->location
,
24934 "%<@encode%> must specify a type as an argument");
24935 return error_mark_node
;
24938 /* This happens if we find @encode(T) (where T is a template
24939 typename or something dependent on a template typename) when
24940 parsing a template. In that case, we can't compile it
24941 immediately, but we rather create an AT_ENCODE_EXPR which will
24942 need to be instantiated when the template is used.
24944 if (dependent_type_p (type
))
24946 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
24947 TREE_READONLY (value
) = 1;
24951 return objc_build_encode_expr (type
);
24954 /* Parse an Objective-C @defs expression. */
24957 cp_parser_objc_defs_expression (cp_parser
*parser
)
24961 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
24962 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24963 name
= cp_parser_identifier (parser
);
24964 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24966 return objc_get_class_ivars (name
);
24969 /* Parse an Objective-C protocol expression.
24971 objc-protocol-expression:
24972 @protocol ( identifier )
24974 Returns a representation of the protocol expression. */
24977 cp_parser_objc_protocol_expression (cp_parser
* parser
)
24981 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
24982 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24983 proto
= cp_parser_identifier (parser
);
24984 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24986 return objc_build_protocol_expr (proto
);
24989 /* Parse an Objective-C selector expression.
24991 objc-selector-expression:
24992 @selector ( objc-method-signature )
24994 objc-method-signature:
25000 objc-selector-seq objc-selector :
25002 Returns a representation of the method selector. */
25005 cp_parser_objc_selector_expression (cp_parser
* parser
)
25007 tree sel_seq
= NULL_TREE
;
25008 bool maybe_unary_selector_p
= true;
25010 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25012 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
25013 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25014 token
= cp_lexer_peek_token (parser
->lexer
);
25016 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
25017 || token
->type
== CPP_SCOPE
)
25019 tree selector
= NULL_TREE
;
25021 if (token
->type
!= CPP_COLON
25022 || token
->type
== CPP_SCOPE
)
25023 selector
= cp_parser_objc_selector (parser
);
25025 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
25026 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
25028 /* Detect if we have a unary selector. */
25029 if (maybe_unary_selector_p
)
25031 sel_seq
= selector
;
25032 goto finish_selector
;
25036 cp_parser_error (parser
, "expected %<:%>");
25039 maybe_unary_selector_p
= false;
25040 token
= cp_lexer_consume_token (parser
->lexer
);
25042 if (token
->type
== CPP_SCOPE
)
25045 = chainon (sel_seq
,
25046 build_tree_list (selector
, NULL_TREE
));
25048 = chainon (sel_seq
,
25049 build_tree_list (NULL_TREE
, NULL_TREE
));
25053 = chainon (sel_seq
,
25054 build_tree_list (selector
, NULL_TREE
));
25056 token
= cp_lexer_peek_token (parser
->lexer
);
25060 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25062 return objc_build_selector_expr (loc
, sel_seq
);
25065 /* Parse a list of identifiers.
25067 objc-identifier-list:
25069 objc-identifier-list , identifier
25071 Returns a TREE_LIST of identifier nodes. */
25074 cp_parser_objc_identifier_list (cp_parser
* parser
)
25080 identifier
= cp_parser_identifier (parser
);
25081 if (identifier
== error_mark_node
)
25082 return error_mark_node
;
25084 list
= build_tree_list (NULL_TREE
, identifier
);
25085 sep
= cp_lexer_peek_token (parser
->lexer
);
25087 while (sep
->type
== CPP_COMMA
)
25089 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25090 identifier
= cp_parser_identifier (parser
);
25091 if (identifier
== error_mark_node
)
25094 list
= chainon (list
, build_tree_list (NULL_TREE
,
25096 sep
= cp_lexer_peek_token (parser
->lexer
);
25102 /* Parse an Objective-C alias declaration.
25104 objc-alias-declaration:
25105 @compatibility_alias identifier identifier ;
25107 This function registers the alias mapping with the Objective-C front end.
25108 It returns nothing. */
25111 cp_parser_objc_alias_declaration (cp_parser
* parser
)
25115 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
25116 alias
= cp_parser_identifier (parser
);
25117 orig
= cp_parser_identifier (parser
);
25118 objc_declare_alias (alias
, orig
);
25119 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25122 /* Parse an Objective-C class forward-declaration.
25124 objc-class-declaration:
25125 @class objc-identifier-list ;
25127 The function registers the forward declarations with the Objective-C
25128 front end. It returns nothing. */
25131 cp_parser_objc_class_declaration (cp_parser
* parser
)
25133 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
25138 id
= cp_parser_identifier (parser
);
25139 if (id
== error_mark_node
)
25142 objc_declare_class (id
);
25144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25145 cp_lexer_consume_token (parser
->lexer
);
25149 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25152 /* Parse a list of Objective-C protocol references.
25154 objc-protocol-refs-opt:
25155 objc-protocol-refs [opt]
25157 objc-protocol-refs:
25158 < objc-identifier-list >
25160 Returns a TREE_LIST of identifiers, if any. */
25163 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
25165 tree protorefs
= NULL_TREE
;
25167 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
25169 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
25170 protorefs
= cp_parser_objc_identifier_list (parser
);
25171 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
25177 /* Parse a Objective-C visibility specification. */
25180 cp_parser_objc_visibility_spec (cp_parser
* parser
)
25182 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
25184 switch (vis
->keyword
)
25186 case RID_AT_PRIVATE
:
25187 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
25189 case RID_AT_PROTECTED
:
25190 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
25192 case RID_AT_PUBLIC
:
25193 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
25195 case RID_AT_PACKAGE
:
25196 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
25202 /* Eat '@private'/'@protected'/'@public'. */
25203 cp_lexer_consume_token (parser
->lexer
);
25206 /* Parse an Objective-C method type. Return 'true' if it is a class
25207 (+) method, and 'false' if it is an instance (-) method. */
25210 cp_parser_objc_method_type (cp_parser
* parser
)
25212 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
25218 /* Parse an Objective-C protocol qualifier. */
25221 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
25223 tree quals
= NULL_TREE
, node
;
25224 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25226 node
= token
->u
.value
;
25228 while (node
&& identifier_p (node
)
25229 && (node
== ridpointers
[(int) RID_IN
]
25230 || node
== ridpointers
[(int) RID_OUT
]
25231 || node
== ridpointers
[(int) RID_INOUT
]
25232 || node
== ridpointers
[(int) RID_BYCOPY
]
25233 || node
== ridpointers
[(int) RID_BYREF
]
25234 || node
== ridpointers
[(int) RID_ONEWAY
]))
25236 quals
= tree_cons (NULL_TREE
, node
, quals
);
25237 cp_lexer_consume_token (parser
->lexer
);
25238 token
= cp_lexer_peek_token (parser
->lexer
);
25239 node
= token
->u
.value
;
25245 /* Parse an Objective-C typename. */
25248 cp_parser_objc_typename (cp_parser
* parser
)
25250 tree type_name
= NULL_TREE
;
25252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25254 tree proto_quals
, cp_type
= NULL_TREE
;
25256 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
25257 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
25259 /* An ObjC type name may consist of just protocol qualifiers, in which
25260 case the type shall default to 'id'. */
25261 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
25263 cp_type
= cp_parser_type_id (parser
);
25265 /* If the type could not be parsed, an error has already
25266 been produced. For error recovery, behave as if it had
25267 not been specified, which will use the default type
25269 if (cp_type
== error_mark_node
)
25271 cp_type
= NULL_TREE
;
25272 /* We need to skip to the closing parenthesis as
25273 cp_parser_type_id() does not seem to do it for
25275 cp_parser_skip_to_closing_parenthesis (parser
,
25276 /*recovering=*/true,
25277 /*or_comma=*/false,
25278 /*consume_paren=*/false);
25282 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25283 type_name
= build_tree_list (proto_quals
, cp_type
);
25289 /* Check to see if TYPE refers to an Objective-C selector name. */
25292 cp_parser_objc_selector_p (enum cpp_ttype type
)
25294 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
25295 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
25296 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
25297 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
25298 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
25301 /* Parse an Objective-C selector. */
25304 cp_parser_objc_selector (cp_parser
* parser
)
25306 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
25308 if (!cp_parser_objc_selector_p (token
->type
))
25310 error_at (token
->location
, "invalid Objective-C++ selector name");
25311 return error_mark_node
;
25314 /* C++ operator names are allowed to appear in ObjC selectors. */
25315 switch (token
->type
)
25317 case CPP_AND_AND
: return get_identifier ("and");
25318 case CPP_AND_EQ
: return get_identifier ("and_eq");
25319 case CPP_AND
: return get_identifier ("bitand");
25320 case CPP_OR
: return get_identifier ("bitor");
25321 case CPP_COMPL
: return get_identifier ("compl");
25322 case CPP_NOT
: return get_identifier ("not");
25323 case CPP_NOT_EQ
: return get_identifier ("not_eq");
25324 case CPP_OR_OR
: return get_identifier ("or");
25325 case CPP_OR_EQ
: return get_identifier ("or_eq");
25326 case CPP_XOR
: return get_identifier ("xor");
25327 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
25328 default: return token
->u
.value
;
25332 /* Parse an Objective-C params list. */
25335 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
25337 tree params
= NULL_TREE
;
25338 bool maybe_unary_selector_p
= true;
25339 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25341 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25343 tree selector
= NULL_TREE
, type_name
, identifier
;
25344 tree parm_attr
= NULL_TREE
;
25346 if (token
->keyword
== RID_ATTRIBUTE
)
25349 if (token
->type
!= CPP_COLON
)
25350 selector
= cp_parser_objc_selector (parser
);
25352 /* Detect if we have a unary selector. */
25353 if (maybe_unary_selector_p
25354 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25356 params
= selector
; /* Might be followed by attributes. */
25360 maybe_unary_selector_p
= false;
25361 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
25363 /* Something went quite wrong. There should be a colon
25364 here, but there is not. Stop parsing parameters. */
25367 type_name
= cp_parser_objc_typename (parser
);
25368 /* New ObjC allows attributes on parameters too. */
25369 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
25370 parm_attr
= cp_parser_attributes_opt (parser
);
25371 identifier
= cp_parser_identifier (parser
);
25375 objc_build_keyword_decl (selector
,
25380 token
= cp_lexer_peek_token (parser
->lexer
);
25383 if (params
== NULL_TREE
)
25385 cp_parser_error (parser
, "objective-c++ method declaration is expected");
25386 return error_mark_node
;
25389 /* We allow tail attributes for the method. */
25390 if (token
->keyword
== RID_ATTRIBUTE
)
25392 *attributes
= cp_parser_attributes_opt (parser
);
25393 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25394 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25396 cp_parser_error (parser
,
25397 "method attributes must be specified at the end");
25398 return error_mark_node
;
25401 if (params
== NULL_TREE
)
25403 cp_parser_error (parser
, "objective-c++ method declaration is expected");
25404 return error_mark_node
;
25409 /* Parse the non-keyword Objective-C params. */
25412 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
25415 tree params
= make_node (TREE_LIST
);
25416 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25417 *ellipsisp
= false; /* Initially, assume no ellipsis. */
25419 while (token
->type
== CPP_COMMA
)
25421 cp_parameter_declarator
*parmdecl
;
25424 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25425 token
= cp_lexer_peek_token (parser
->lexer
);
25427 if (token
->type
== CPP_ELLIPSIS
)
25429 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
25431 token
= cp_lexer_peek_token (parser
->lexer
);
25435 /* TODO: parse attributes for tail parameters. */
25436 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
25437 parm
= grokdeclarator (parmdecl
->declarator
,
25438 &parmdecl
->decl_specifiers
,
25439 PARM
, /*initialized=*/0,
25440 /*attrlist=*/NULL
);
25442 chainon (params
, build_tree_list (NULL_TREE
, parm
));
25443 token
= cp_lexer_peek_token (parser
->lexer
);
25446 /* We allow tail attributes for the method. */
25447 if (token
->keyword
== RID_ATTRIBUTE
)
25449 if (*attributes
== NULL_TREE
)
25451 *attributes
= cp_parser_attributes_opt (parser
);
25452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25453 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25457 /* We have an error, but parse the attributes, so that we can
25459 *attributes
= cp_parser_attributes_opt (parser
);
25461 cp_parser_error (parser
,
25462 "method attributes must be specified at the end");
25463 return error_mark_node
;
25469 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25472 cp_parser_objc_interstitial_code (cp_parser
* parser
)
25474 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25476 /* If the next token is `extern' and the following token is a string
25477 literal, then we have a linkage specification. */
25478 if (token
->keyword
== RID_EXTERN
25479 && cp_parser_is_pure_string_literal
25480 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
25481 cp_parser_linkage_specification (parser
);
25482 /* Handle #pragma, if any. */
25483 else if (token
->type
== CPP_PRAGMA
)
25484 cp_parser_pragma (parser
, pragma_objc_icode
);
25485 /* Allow stray semicolons. */
25486 else if (token
->type
== CPP_SEMICOLON
)
25487 cp_lexer_consume_token (parser
->lexer
);
25488 /* Mark methods as optional or required, when building protocols. */
25489 else if (token
->keyword
== RID_AT_OPTIONAL
)
25491 cp_lexer_consume_token (parser
->lexer
);
25492 objc_set_method_opt (true);
25494 else if (token
->keyword
== RID_AT_REQUIRED
)
25496 cp_lexer_consume_token (parser
->lexer
);
25497 objc_set_method_opt (false);
25499 else if (token
->keyword
== RID_NAMESPACE
)
25500 cp_parser_namespace_definition (parser
);
25501 /* Other stray characters must generate errors. */
25502 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
25504 cp_lexer_consume_token (parser
->lexer
);
25505 error ("stray %qs between Objective-C++ methods",
25506 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
25508 /* Finally, try to parse a block-declaration, or a function-definition. */
25510 cp_parser_block_declaration (parser
, /*statement_p=*/false);
25513 /* Parse a method signature. */
25516 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
25518 tree rettype
, kwdparms
, optparms
;
25519 bool ellipsis
= false;
25520 bool is_class_method
;
25522 is_class_method
= cp_parser_objc_method_type (parser
);
25523 rettype
= cp_parser_objc_typename (parser
);
25524 *attributes
= NULL_TREE
;
25525 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
25526 if (kwdparms
== error_mark_node
)
25527 return error_mark_node
;
25528 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
25529 if (optparms
== error_mark_node
)
25530 return error_mark_node
;
25532 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
25536 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
25539 cp_lexer_save_tokens (parser
->lexer
);
25540 tattr
= cp_parser_attributes_opt (parser
);
25541 gcc_assert (tattr
) ;
25543 /* If the attributes are followed by a method introducer, this is not allowed.
25544 Dump the attributes and flag the situation. */
25545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
25546 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
25549 /* Otherwise, the attributes introduce some interstitial code, possibly so
25550 rewind to allow that check. */
25551 cp_lexer_rollback_tokens (parser
->lexer
);
25555 /* Parse an Objective-C method prototype list. */
25558 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
25560 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25562 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25564 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
25566 tree attributes
, sig
;
25567 bool is_class_method
;
25568 if (token
->type
== CPP_PLUS
)
25569 is_class_method
= true;
25571 is_class_method
= false;
25572 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
25573 if (sig
== error_mark_node
)
25575 cp_parser_skip_to_end_of_block_or_statement (parser
);
25576 token
= cp_lexer_peek_token (parser
->lexer
);
25579 objc_add_method_declaration (is_class_method
, sig
, attributes
);
25580 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25582 else if (token
->keyword
== RID_AT_PROPERTY
)
25583 cp_parser_objc_at_property_declaration (parser
);
25584 else if (token
->keyword
== RID_ATTRIBUTE
25585 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25586 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25588 "prefix attributes are ignored for methods");
25590 /* Allow for interspersed non-ObjC++ code. */
25591 cp_parser_objc_interstitial_code (parser
);
25593 token
= cp_lexer_peek_token (parser
->lexer
);
25596 if (token
->type
!= CPP_EOF
)
25597 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25599 cp_parser_error (parser
, "expected %<@end%>");
25601 objc_finish_interface ();
25604 /* Parse an Objective-C method definition list. */
25607 cp_parser_objc_method_definition_list (cp_parser
* parser
)
25609 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25611 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25615 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
25618 tree sig
, attribute
;
25619 bool is_class_method
;
25620 if (token
->type
== CPP_PLUS
)
25621 is_class_method
= true;
25623 is_class_method
= false;
25624 push_deferring_access_checks (dk_deferred
);
25625 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
25626 if (sig
== error_mark_node
)
25628 cp_parser_skip_to_end_of_block_or_statement (parser
);
25629 token
= cp_lexer_peek_token (parser
->lexer
);
25632 objc_start_method_definition (is_class_method
, sig
, attribute
,
25635 /* For historical reasons, we accept an optional semicolon. */
25636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25637 cp_lexer_consume_token (parser
->lexer
);
25639 ptk
= cp_lexer_peek_token (parser
->lexer
);
25640 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
25641 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
25643 perform_deferred_access_checks (tf_warning_or_error
);
25644 stop_deferring_access_checks ();
25645 meth
= cp_parser_function_definition_after_declarator (parser
,
25647 pop_deferring_access_checks ();
25648 objc_finish_method_definition (meth
);
25651 /* The following case will be removed once @synthesize is
25652 completely implemented. */
25653 else if (token
->keyword
== RID_AT_PROPERTY
)
25654 cp_parser_objc_at_property_declaration (parser
);
25655 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
25656 cp_parser_objc_at_synthesize_declaration (parser
);
25657 else if (token
->keyword
== RID_AT_DYNAMIC
)
25658 cp_parser_objc_at_dynamic_declaration (parser
);
25659 else if (token
->keyword
== RID_ATTRIBUTE
25660 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25661 warning_at (token
->location
, OPT_Wattributes
,
25662 "prefix attributes are ignored for methods");
25664 /* Allow for interspersed non-ObjC++ code. */
25665 cp_parser_objc_interstitial_code (parser
);
25667 token
= cp_lexer_peek_token (parser
->lexer
);
25670 if (token
->type
!= CPP_EOF
)
25671 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25673 cp_parser_error (parser
, "expected %<@end%>");
25675 objc_finish_implementation ();
25678 /* Parse Objective-C ivars. */
25681 cp_parser_objc_class_ivars (cp_parser
* parser
)
25683 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25685 if (token
->type
!= CPP_OPEN_BRACE
)
25686 return; /* No ivars specified. */
25688 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
25689 token
= cp_lexer_peek_token (parser
->lexer
);
25691 while (token
->type
!= CPP_CLOSE_BRACE
25692 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25694 cp_decl_specifier_seq declspecs
;
25695 int decl_class_or_enum_p
;
25696 tree prefix_attributes
;
25698 cp_parser_objc_visibility_spec (parser
);
25700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25703 cp_parser_decl_specifier_seq (parser
,
25704 CP_PARSER_FLAGS_OPTIONAL
,
25706 &decl_class_or_enum_p
);
25708 /* auto, register, static, extern, mutable. */
25709 if (declspecs
.storage_class
!= sc_none
)
25711 cp_parser_error (parser
, "invalid type for instance variable");
25712 declspecs
.storage_class
= sc_none
;
25715 /* thread_local. */
25716 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
25718 cp_parser_error (parser
, "invalid type for instance variable");
25719 declspecs
.locations
[ds_thread
] = 0;
25723 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
25725 cp_parser_error (parser
, "invalid type for instance variable");
25726 declspecs
.locations
[ds_typedef
] = 0;
25729 prefix_attributes
= declspecs
.attributes
;
25730 declspecs
.attributes
= NULL_TREE
;
25732 /* Keep going until we hit the `;' at the end of the
25734 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25736 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
25737 cp_declarator
*declarator
= NULL
;
25738 int ctor_dtor_or_conv_p
;
25740 /* Check for a (possibly unnamed) bitfield declaration. */
25741 token
= cp_lexer_peek_token (parser
->lexer
);
25742 if (token
->type
== CPP_COLON
)
25745 if (token
->type
== CPP_NAME
25746 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
25749 /* Get the name of the bitfield. */
25750 declarator
= make_id_declarator (NULL_TREE
,
25751 cp_parser_identifier (parser
),
25755 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
25756 /* Get the width of the bitfield. */
25758 = cp_parser_constant_expression (parser
,
25759 /*allow_non_constant=*/false,
25764 /* Parse the declarator. */
25766 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25767 &ctor_dtor_or_conv_p
,
25768 /*parenthesized_p=*/NULL
,
25769 /*member_p=*/false);
25772 /* Look for attributes that apply to the ivar. */
25773 attributes
= cp_parser_attributes_opt (parser
);
25774 /* Remember which attributes are prefix attributes and
25776 first_attribute
= attributes
;
25777 /* Combine the attributes. */
25778 attributes
= chainon (prefix_attributes
, attributes
);
25781 /* Create the bitfield declaration. */
25782 decl
= grokbitfield (declarator
, &declspecs
,
25786 decl
= grokfield (declarator
, &declspecs
,
25787 NULL_TREE
, /*init_const_expr_p=*/false,
25788 NULL_TREE
, attributes
);
25790 /* Add the instance variable. */
25791 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
25792 objc_add_instance_variable (decl
);
25794 /* Reset PREFIX_ATTRIBUTES. */
25795 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
25796 attributes
= TREE_CHAIN (attributes
);
25798 TREE_CHAIN (attributes
) = NULL_TREE
;
25800 token
= cp_lexer_peek_token (parser
->lexer
);
25802 if (token
->type
== CPP_COMMA
)
25804 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25810 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25811 token
= cp_lexer_peek_token (parser
->lexer
);
25814 if (token
->keyword
== RID_AT_END
)
25815 cp_parser_error (parser
, "expected %<}%>");
25817 /* Do not consume the RID_AT_END, so it will be read again as terminating
25818 the @interface of @implementation. */
25819 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25820 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
25822 /* For historical reasons, we accept an optional semicolon. */
25823 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25824 cp_lexer_consume_token (parser
->lexer
);
25827 /* Parse an Objective-C protocol declaration. */
25830 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
25832 tree proto
, protorefs
;
25835 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25836 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
25838 tok
= cp_lexer_peek_token (parser
->lexer
);
25839 error_at (tok
->location
, "identifier expected after %<@protocol%>");
25840 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25844 /* See if we have a forward declaration or a definition. */
25845 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25847 /* Try a forward declaration first. */
25848 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
25854 id
= cp_parser_identifier (parser
);
25855 if (id
== error_mark_node
)
25858 objc_declare_protocol (id
, attributes
);
25860 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25861 cp_lexer_consume_token (parser
->lexer
);
25865 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25868 /* Ok, we got a full-fledged definition (or at least should). */
25871 proto
= cp_parser_identifier (parser
);
25872 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
25873 objc_start_protocol (proto
, protorefs
, attributes
);
25874 cp_parser_objc_method_prototype_list (parser
);
25878 /* Parse an Objective-C superclass or category. */
25881 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
25884 tree
*categ
, bool *is_class_extension
)
25886 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
25888 *super
= *categ
= NULL_TREE
;
25889 *is_class_extension
= false;
25890 if (next
->type
== CPP_COLON
)
25892 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
25893 *super
= cp_parser_identifier (parser
);
25895 else if (next
->type
== CPP_OPEN_PAREN
)
25897 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
25899 /* If there is no category name, and this is an @interface, we
25900 have a class extension. */
25901 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25903 *categ
= NULL_TREE
;
25904 *is_class_extension
= true;
25907 *categ
= cp_parser_identifier (parser
);
25909 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25913 /* Parse an Objective-C class interface. */
25916 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
25918 tree name
, super
, categ
, protos
;
25919 bool is_class_extension
;
25921 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
25922 name
= cp_parser_identifier (parser
);
25923 if (name
== error_mark_node
)
25925 /* It's hard to recover because even if valid @interface stuff
25926 is to follow, we can't compile it (or validate it) if we
25927 don't even know which class it refers to. Let's assume this
25928 was a stray '@interface' token in the stream and skip it.
25932 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
25933 &is_class_extension
);
25934 protos
= cp_parser_objc_protocol_refs_opt (parser
);
25936 /* We have either a class or a category on our hands. */
25937 if (categ
|| is_class_extension
)
25938 objc_start_category_interface (name
, categ
, protos
, attributes
);
25941 objc_start_class_interface (name
, super
, protos
, attributes
);
25942 /* Handle instance variable declarations, if any. */
25943 cp_parser_objc_class_ivars (parser
);
25944 objc_continue_interface ();
25947 cp_parser_objc_method_prototype_list (parser
);
25950 /* Parse an Objective-C class implementation. */
25953 cp_parser_objc_class_implementation (cp_parser
* parser
)
25955 tree name
, super
, categ
;
25956 bool is_class_extension
;
25958 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
25959 name
= cp_parser_identifier (parser
);
25960 if (name
== error_mark_node
)
25962 /* It's hard to recover because even if valid @implementation
25963 stuff is to follow, we can't compile it (or validate it) if
25964 we don't even know which class it refers to. Let's assume
25965 this was a stray '@implementation' token in the stream and
25970 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
25971 &is_class_extension
);
25973 /* We have either a class or a category on our hands. */
25975 objc_start_category_implementation (name
, categ
);
25978 objc_start_class_implementation (name
, super
);
25979 /* Handle instance variable declarations, if any. */
25980 cp_parser_objc_class_ivars (parser
);
25981 objc_continue_implementation ();
25984 cp_parser_objc_method_definition_list (parser
);
25987 /* Consume the @end token and finish off the implementation. */
25990 cp_parser_objc_end_implementation (cp_parser
* parser
)
25992 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25993 objc_finish_implementation ();
25996 /* Parse an Objective-C declaration. */
25999 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
26001 /* Try to figure out what kind of declaration is present. */
26002 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26005 switch (kwd
->keyword
)
26010 error_at (kwd
->location
, "attributes may not be specified before"
26011 " the %<@%D%> Objective-C++ keyword",
26015 case RID_AT_IMPLEMENTATION
:
26016 warning_at (kwd
->location
, OPT_Wattributes
,
26017 "prefix attributes are ignored before %<@%D%>",
26024 switch (kwd
->keyword
)
26027 cp_parser_objc_alias_declaration (parser
);
26030 cp_parser_objc_class_declaration (parser
);
26032 case RID_AT_PROTOCOL
:
26033 cp_parser_objc_protocol_declaration (parser
, attributes
);
26035 case RID_AT_INTERFACE
:
26036 cp_parser_objc_class_interface (parser
, attributes
);
26038 case RID_AT_IMPLEMENTATION
:
26039 cp_parser_objc_class_implementation (parser
);
26042 cp_parser_objc_end_implementation (parser
);
26045 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26047 cp_parser_skip_to_end_of_block_or_statement (parser
);
26051 /* Parse an Objective-C try-catch-finally statement.
26053 objc-try-catch-finally-stmt:
26054 @try compound-statement objc-catch-clause-seq [opt]
26055 objc-finally-clause [opt]
26057 objc-catch-clause-seq:
26058 objc-catch-clause objc-catch-clause-seq [opt]
26061 @catch ( objc-exception-declaration ) compound-statement
26063 objc-finally-clause:
26064 @finally compound-statement
26066 objc-exception-declaration:
26067 parameter-declaration
26070 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26074 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26075 for C. Keep them in sync. */
26078 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
26080 location_t location
;
26083 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
26084 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26085 objc_maybe_warn_exceptions (location
);
26086 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26087 node, lest it get absorbed into the surrounding block. */
26088 stmt
= push_stmt_list ();
26089 cp_parser_compound_statement (parser
, NULL
, false, false);
26090 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
26092 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
26094 cp_parameter_declarator
*parm
;
26095 tree parameter_declaration
= error_mark_node
;
26096 bool seen_open_paren
= false;
26098 cp_lexer_consume_token (parser
->lexer
);
26099 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26100 seen_open_paren
= true;
26101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26103 /* We have "@catch (...)" (where the '...' are literally
26104 what is in the code). Skip the '...'.
26105 parameter_declaration is set to NULL_TREE, and
26106 objc_being_catch_clauses() knows that that means
26108 cp_lexer_consume_token (parser
->lexer
);
26109 parameter_declaration
= NULL_TREE
;
26113 /* We have "@catch (NSException *exception)" or something
26114 like that. Parse the parameter declaration. */
26115 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
26117 parameter_declaration
= error_mark_node
;
26119 parameter_declaration
= grokdeclarator (parm
->declarator
,
26120 &parm
->decl_specifiers
,
26121 PARM
, /*initialized=*/0,
26122 /*attrlist=*/NULL
);
26124 if (seen_open_paren
)
26125 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26128 /* If there was no open parenthesis, we are recovering from
26129 an error, and we are trying to figure out what mistake
26130 the user has made. */
26132 /* If there is an immediate closing parenthesis, the user
26133 probably forgot the opening one (ie, they typed "@catch
26134 NSException *e)". Parse the closing parenthesis and keep
26136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26137 cp_lexer_consume_token (parser
->lexer
);
26139 /* If these is no immediate closing parenthesis, the user
26140 probably doesn't know that parenthesis are required at
26141 all (ie, they typed "@catch NSException *e"). So, just
26142 forget about the closing parenthesis and keep going. */
26144 objc_begin_catch_clause (parameter_declaration
);
26145 cp_parser_compound_statement (parser
, NULL
, false, false);
26146 objc_finish_catch_clause ();
26148 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
26150 cp_lexer_consume_token (parser
->lexer
);
26151 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26152 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26153 node, lest it get absorbed into the surrounding block. */
26154 stmt
= push_stmt_list ();
26155 cp_parser_compound_statement (parser
, NULL
, false, false);
26156 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
26159 return objc_finish_try_stmt ();
26162 /* Parse an Objective-C synchronized statement.
26164 objc-synchronized-stmt:
26165 @synchronized ( expression ) compound-statement
26167 Returns NULL_TREE. */
26170 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
26172 location_t location
;
26175 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
26177 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26178 objc_maybe_warn_exceptions (location
);
26179 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
26180 lock
= cp_parser_expression (parser
, false, NULL
);
26181 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26183 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26184 node, lest it get absorbed into the surrounding block. */
26185 stmt
= push_stmt_list ();
26186 cp_parser_compound_statement (parser
, NULL
, false, false);
26188 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
26191 /* Parse an Objective-C throw statement.
26194 @throw assignment-expression [opt] ;
26196 Returns a constructed '@throw' statement. */
26199 cp_parser_objc_throw_statement (cp_parser
*parser
)
26201 tree expr
= NULL_TREE
;
26202 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26204 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
26206 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26207 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
26209 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26211 return objc_build_throw_stmt (loc
, expr
);
26214 /* Parse an Objective-C statement. */
26217 cp_parser_objc_statement (cp_parser
* parser
)
26219 /* Try to figure out what kind of declaration is present. */
26220 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26222 switch (kwd
->keyword
)
26225 return cp_parser_objc_try_catch_finally_statement (parser
);
26226 case RID_AT_SYNCHRONIZED
:
26227 return cp_parser_objc_synchronized_statement (parser
);
26229 return cp_parser_objc_throw_statement (parser
);
26231 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26233 cp_parser_skip_to_end_of_block_or_statement (parser
);
26236 return error_mark_node
;
26239 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26240 look ahead to see if an objc keyword follows the attributes. This
26241 is to detect the use of prefix attributes on ObjC @interface and
26245 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
26247 cp_lexer_save_tokens (parser
->lexer
);
26248 *attrib
= cp_parser_attributes_opt (parser
);
26249 gcc_assert (*attrib
);
26250 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
26252 cp_lexer_commit_tokens (parser
->lexer
);
26255 cp_lexer_rollback_tokens (parser
->lexer
);
26259 /* This routine is a minimal replacement for
26260 c_parser_struct_declaration () used when parsing the list of
26261 types/names or ObjC++ properties. For example, when parsing the
26264 @property (readonly) int a, b, c;
26266 this function is responsible for parsing "int a, int b, int c" and
26267 returning the declarations as CHAIN of DECLs.
26269 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26270 similar parsing. */
26272 cp_parser_objc_struct_declaration (cp_parser
*parser
)
26274 tree decls
= NULL_TREE
;
26275 cp_decl_specifier_seq declspecs
;
26276 int decl_class_or_enum_p
;
26277 tree prefix_attributes
;
26279 cp_parser_decl_specifier_seq (parser
,
26280 CP_PARSER_FLAGS_NONE
,
26282 &decl_class_or_enum_p
);
26284 if (declspecs
.type
== error_mark_node
)
26285 return error_mark_node
;
26287 /* auto, register, static, extern, mutable. */
26288 if (declspecs
.storage_class
!= sc_none
)
26290 cp_parser_error (parser
, "invalid type for property");
26291 declspecs
.storage_class
= sc_none
;
26294 /* thread_local. */
26295 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26297 cp_parser_error (parser
, "invalid type for property");
26298 declspecs
.locations
[ds_thread
] = 0;
26302 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26304 cp_parser_error (parser
, "invalid type for property");
26305 declspecs
.locations
[ds_typedef
] = 0;
26308 prefix_attributes
= declspecs
.attributes
;
26309 declspecs
.attributes
= NULL_TREE
;
26311 /* Keep going until we hit the `;' at the end of the declaration. */
26312 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26314 tree attributes
, first_attribute
, decl
;
26315 cp_declarator
*declarator
;
26318 /* Parse the declarator. */
26319 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26320 NULL
, NULL
, false);
26322 /* Look for attributes that apply to the ivar. */
26323 attributes
= cp_parser_attributes_opt (parser
);
26324 /* Remember which attributes are prefix attributes and
26326 first_attribute
= attributes
;
26327 /* Combine the attributes. */
26328 attributes
= chainon (prefix_attributes
, attributes
);
26330 decl
= grokfield (declarator
, &declspecs
,
26331 NULL_TREE
, /*init_const_expr_p=*/false,
26332 NULL_TREE
, attributes
);
26334 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
26335 return error_mark_node
;
26337 /* Reset PREFIX_ATTRIBUTES. */
26338 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26339 attributes
= TREE_CHAIN (attributes
);
26341 TREE_CHAIN (attributes
) = NULL_TREE
;
26343 DECL_CHAIN (decl
) = decls
;
26346 token
= cp_lexer_peek_token (parser
->lexer
);
26347 if (token
->type
== CPP_COMMA
)
26349 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26358 /* Parse an Objective-C @property declaration. The syntax is:
26360 objc-property-declaration:
26361 '@property' objc-property-attributes[opt] struct-declaration ;
26363 objc-property-attributes:
26364 '(' objc-property-attribute-list ')'
26366 objc-property-attribute-list:
26367 objc-property-attribute
26368 objc-property-attribute-list, objc-property-attribute
26370 objc-property-attribute
26371 'getter' = identifier
26372 'setter' = identifier
26381 @property NSString *name;
26382 @property (readonly) id object;
26383 @property (retain, nonatomic, getter=getTheName) id name;
26384 @property int a, b, c;
26386 PS: This function is identical to
26387 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26389 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
26391 /* The following variables hold the attributes of the properties as
26392 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26393 seen. When we see an attribute, we set them to 'true' (if they
26394 are boolean properties) or to the identifier (if they have an
26395 argument, ie, for getter and setter). Note that here we only
26396 parse the list of attributes, check the syntax and accumulate the
26397 attributes that we find. objc_add_property_declaration() will
26398 then process the information. */
26399 bool property_assign
= false;
26400 bool property_copy
= false;
26401 tree property_getter_ident
= NULL_TREE
;
26402 bool property_nonatomic
= false;
26403 bool property_readonly
= false;
26404 bool property_readwrite
= false;
26405 bool property_retain
= false;
26406 tree property_setter_ident
= NULL_TREE
;
26408 /* 'properties' is the list of properties that we read. Usually a
26409 single one, but maybe more (eg, in "@property int a, b, c;" there
26414 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26416 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
26418 /* Parse the optional attribute list... */
26419 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26422 cp_lexer_consume_token (parser
->lexer
);
26426 bool syntax_error
= false;
26427 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26430 if (token
->type
!= CPP_NAME
)
26432 cp_parser_error (parser
, "expected identifier");
26435 keyword
= C_RID_CODE (token
->u
.value
);
26436 cp_lexer_consume_token (parser
->lexer
);
26439 case RID_ASSIGN
: property_assign
= true; break;
26440 case RID_COPY
: property_copy
= true; break;
26441 case RID_NONATOMIC
: property_nonatomic
= true; break;
26442 case RID_READONLY
: property_readonly
= true; break;
26443 case RID_READWRITE
: property_readwrite
= true; break;
26444 case RID_RETAIN
: property_retain
= true; break;
26448 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
26450 if (keyword
== RID_GETTER
)
26451 cp_parser_error (parser
,
26452 "missing %<=%> (after %<getter%> attribute)");
26454 cp_parser_error (parser
,
26455 "missing %<=%> (after %<setter%> attribute)");
26456 syntax_error
= true;
26459 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
26460 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
26462 cp_parser_error (parser
, "expected identifier");
26463 syntax_error
= true;
26466 if (keyword
== RID_SETTER
)
26468 if (property_setter_ident
!= NULL_TREE
)
26470 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
26471 cp_lexer_consume_token (parser
->lexer
);
26474 property_setter_ident
= cp_parser_objc_selector (parser
);
26475 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26476 cp_parser_error (parser
, "setter name must terminate with %<:%>");
26478 cp_lexer_consume_token (parser
->lexer
);
26482 if (property_getter_ident
!= NULL_TREE
)
26484 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
26485 cp_lexer_consume_token (parser
->lexer
);
26488 property_getter_ident
= cp_parser_objc_selector (parser
);
26492 cp_parser_error (parser
, "unknown property attribute");
26493 syntax_error
= true;
26500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26501 cp_lexer_consume_token (parser
->lexer
);
26506 /* FIXME: "@property (setter, assign);" will generate a spurious
26507 "error: expected ‘)’ before ‘,’ token". This is because
26508 cp_parser_require, unlike the C counterpart, will produce an
26509 error even if we are in error recovery. */
26510 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26512 cp_parser_skip_to_closing_parenthesis (parser
,
26513 /*recovering=*/true,
26514 /*or_comma=*/false,
26515 /*consume_paren=*/true);
26519 /* ... and the property declaration(s). */
26520 properties
= cp_parser_objc_struct_declaration (parser
);
26522 if (properties
== error_mark_node
)
26524 cp_parser_skip_to_end_of_statement (parser
);
26525 /* If the next token is now a `;', consume it. */
26526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26527 cp_lexer_consume_token (parser
->lexer
);
26531 if (properties
== NULL_TREE
)
26532 cp_parser_error (parser
, "expected identifier");
26535 /* Comma-separated properties are chained together in
26536 reverse order; add them one by one. */
26537 properties
= nreverse (properties
);
26539 for (; properties
; properties
= TREE_CHAIN (properties
))
26540 objc_add_property_declaration (loc
, copy_node (properties
),
26541 property_readonly
, property_readwrite
,
26542 property_assign
, property_retain
,
26543 property_copy
, property_nonatomic
,
26544 property_getter_ident
, property_setter_ident
);
26547 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26550 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26552 objc-synthesize-declaration:
26553 @synthesize objc-synthesize-identifier-list ;
26555 objc-synthesize-identifier-list:
26556 objc-synthesize-identifier
26557 objc-synthesize-identifier-list, objc-synthesize-identifier
26559 objc-synthesize-identifier
26561 identifier = identifier
26564 @synthesize MyProperty;
26565 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26567 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26568 for C. Keep them in sync.
26571 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
26573 tree list
= NULL_TREE
;
26575 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26577 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
26580 tree property
, ivar
;
26581 property
= cp_parser_identifier (parser
);
26582 if (property
== error_mark_node
)
26584 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
26589 cp_lexer_consume_token (parser
->lexer
);
26590 ivar
= cp_parser_identifier (parser
);
26591 if (ivar
== error_mark_node
)
26593 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26599 list
= chainon (list
, build_tree_list (ivar
, property
));
26600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26601 cp_lexer_consume_token (parser
->lexer
);
26605 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26606 objc_add_synthesize_declaration (loc
, list
);
26609 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26611 objc-dynamic-declaration:
26612 @dynamic identifier-list ;
26615 @dynamic MyProperty;
26616 @dynamic MyProperty, AnotherProperty;
26618 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26619 for C. Keep them in sync.
26622 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
26624 tree list
= NULL_TREE
;
26626 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26628 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
26632 property
= cp_parser_identifier (parser
);
26633 if (property
== error_mark_node
)
26635 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26638 list
= chainon (list
, build_tree_list (NULL
, property
));
26639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26640 cp_lexer_consume_token (parser
->lexer
);
26644 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26645 objc_add_dynamic_declaration (loc
, list
);
26649 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26651 /* Returns name of the next clause.
26652 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26653 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26654 returned and the token is consumed. */
26656 static pragma_omp_clause
26657 cp_parser_omp_clause_name (cp_parser
*parser
)
26659 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
26661 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
26662 result
= PRAGMA_OMP_CLAUSE_IF
;
26663 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
26664 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
26665 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
26666 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
26667 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
26668 result
= PRAGMA_OMP_CLAUSE_FOR
;
26669 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26671 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26672 const char *p
= IDENTIFIER_POINTER (id
);
26677 if (!strcmp ("aligned", p
))
26678 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
26681 if (!strcmp ("collapse", p
))
26682 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
26683 else if (!strcmp ("copyin", p
))
26684 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
26685 else if (!strcmp ("copyprivate", p
))
26686 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
26689 if (!strcmp ("depend", p
))
26690 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
26691 else if (!strcmp ("device", p
))
26692 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
26693 else if (!strcmp ("dist_schedule", p
))
26694 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
26697 if (!strcmp ("final", p
))
26698 result
= PRAGMA_OMP_CLAUSE_FINAL
;
26699 else if (!strcmp ("firstprivate", p
))
26700 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
26701 else if (!strcmp ("from", p
))
26702 result
= PRAGMA_OMP_CLAUSE_FROM
;
26705 if (!strcmp ("inbranch", p
))
26706 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
26709 if (!strcmp ("lastprivate", p
))
26710 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
26711 else if (!strcmp ("linear", p
))
26712 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
26715 if (!strcmp ("map", p
))
26716 result
= PRAGMA_OMP_CLAUSE_MAP
;
26717 else if (!strcmp ("mergeable", p
))
26718 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
26721 if (!strcmp ("notinbranch", p
))
26722 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
26723 else if (!strcmp ("nowait", p
))
26724 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
26725 else if (!strcmp ("num_teams", p
))
26726 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
26727 else if (!strcmp ("num_threads", p
))
26728 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
26731 if (!strcmp ("ordered", p
))
26732 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
26735 if (!strcmp ("parallel", p
))
26736 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
26737 else if (!strcmp ("proc_bind", p
))
26738 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
26741 if (!strcmp ("reduction", p
))
26742 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
26745 if (!strcmp ("safelen", p
))
26746 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
26747 else if (!strcmp ("schedule", p
))
26748 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
26749 else if (!strcmp ("sections", p
))
26750 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
26751 else if (!strcmp ("shared", p
))
26752 result
= PRAGMA_OMP_CLAUSE_SHARED
;
26753 else if (!strcmp ("simdlen", p
))
26754 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
26757 if (!strcmp ("taskgroup", p
))
26758 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
26759 else if (!strcmp ("thread_limit", p
))
26760 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
26761 else if (!strcmp ("to", p
))
26762 result
= PRAGMA_OMP_CLAUSE_TO
;
26765 if (!strcmp ("uniform", p
))
26766 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
26767 else if (!strcmp ("untied", p
))
26768 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
26773 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
26774 cp_lexer_consume_token (parser
->lexer
);
26779 /* Validate that a clause of the given type does not already exist. */
26782 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
26783 const char *name
, location_t location
)
26787 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
26788 if (OMP_CLAUSE_CODE (c
) == code
)
26790 error_at (location
, "too many %qs clauses", name
);
26798 variable-list , identifier
26800 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
26801 colon). An opening parenthesis will have been consumed by the caller.
26803 If KIND is nonzero, create the appropriate node and install the decl
26804 in OMP_CLAUSE_DECL and add the node to the head of the list.
26806 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26807 return the list created.
26809 COLON can be NULL if only closing parenthesis should end the list,
26810 or pointer to bool which will receive false if the list is terminated
26811 by closing parenthesis or true if the list is terminated by colon. */
26814 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
26815 tree list
, bool *colon
)
26818 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
26821 parser
->colon_corrects_to_scope_p
= false;
26828 token
= cp_lexer_peek_token (parser
->lexer
);
26829 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
26830 /*check_dependency_p=*/true,
26831 /*template_p=*/NULL
,
26832 /*declarator_p=*/false,
26833 /*optional_p=*/false);
26834 if (name
== error_mark_node
)
26837 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
26838 if (decl
== error_mark_node
)
26839 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
26841 else if (kind
!= 0)
26845 case OMP_CLAUSE_MAP
:
26846 case OMP_CLAUSE_FROM
:
26847 case OMP_CLAUSE_TO
:
26848 case OMP_CLAUSE_DEPEND
:
26849 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
26851 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
26853 parser
->colon_corrects_to_scope_p
= false;
26854 cp_lexer_consume_token (parser
->lexer
);
26855 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26856 low_bound
= cp_parser_expression (parser
, /*cast_p=*/false,
26859 parser
->colon_corrects_to_scope_p
26860 = saved_colon_corrects_to_scope_p
;
26861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
26862 length
= integer_one_node
;
26865 /* Look for `:'. */
26866 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26868 if (!cp_lexer_next_token_is (parser
->lexer
,
26870 length
= cp_parser_expression (parser
,
26874 /* Look for the closing `]'. */
26875 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
26878 decl
= tree_cons (low_bound
, length
, decl
);
26885 tree u
= build_omp_clause (token
->location
, kind
);
26886 OMP_CLAUSE_DECL (u
) = decl
;
26887 OMP_CLAUSE_CHAIN (u
) = list
;
26891 list
= tree_cons (decl
, NULL_TREE
, list
);
26894 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26896 cp_lexer_consume_token (parser
->lexer
);
26900 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
26902 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26905 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
26909 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26913 /* Try to resync to an unnested comma. Copied from
26914 cp_parser_parenthesized_expression_list. */
26917 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
26918 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
26919 /*recovering=*/true,
26921 /*consume_paren=*/true);
26929 /* Similarly, but expect leading and trailing parenthesis. This is a very
26930 common case for omp clauses. */
26933 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
26935 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26936 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
26941 collapse ( constant-expression ) */
26944 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
26950 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26951 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26954 num
= cp_parser_constant_expression (parser
, false, NULL
);
26956 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26957 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26958 /*or_comma=*/false,
26959 /*consume_paren=*/true);
26961 if (num
== error_mark_node
)
26963 num
= fold_non_dependent_expr (num
);
26964 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
26965 || !tree_fits_shwi_p (num
)
26966 || (n
= tree_low_cst (num
, 0)) <= 0
26969 error_at (loc
, "collapse argument needs positive constant integer expression");
26973 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
26974 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
26975 OMP_CLAUSE_CHAIN (c
) = list
;
26976 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
26982 default ( shared | none ) */
26985 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
26987 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
26990 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26992 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26994 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26995 const char *p
= IDENTIFIER_POINTER (id
);
27000 if (strcmp ("none", p
) != 0)
27002 kind
= OMP_CLAUSE_DEFAULT_NONE
;
27006 if (strcmp ("shared", p
) != 0)
27008 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
27015 cp_lexer_consume_token (parser
->lexer
);
27020 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
27023 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27024 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27025 /*or_comma=*/false,
27026 /*consume_paren=*/true);
27028 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
27031 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
27032 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
27033 OMP_CLAUSE_CHAIN (c
) = list
;
27034 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
27040 final ( expression ) */
27043 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
27047 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27050 t
= cp_parser_condition (parser
);
27052 if (t
== error_mark_node
27053 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27054 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27055 /*or_comma=*/false,
27056 /*consume_paren=*/true);
27058 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
27060 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
27061 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
27062 OMP_CLAUSE_CHAIN (c
) = list
;
27068 if ( expression ) */
27071 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
27075 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27078 t
= cp_parser_condition (parser
);
27080 if (t
== error_mark_node
27081 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27082 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27083 /*or_comma=*/false,
27084 /*consume_paren=*/true);
27086 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
27088 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
27089 OMP_CLAUSE_IF_EXPR (c
) = t
;
27090 OMP_CLAUSE_CHAIN (c
) = list
;
27099 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
27100 tree list
, location_t location
)
27104 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
27107 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
27108 OMP_CLAUSE_CHAIN (c
) = list
;
27116 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
27117 tree list
, location_t location
)
27121 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
27123 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
27124 OMP_CLAUSE_CHAIN (c
) = list
;
27129 num_threads ( expression ) */
27132 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
27133 location_t location
)
27137 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27140 t
= cp_parser_expression (parser
, false, NULL
);
27142 if (t
== error_mark_node
27143 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27144 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27145 /*or_comma=*/false,
27146 /*consume_paren=*/true);
27148 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
27149 "num_threads", location
);
27151 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
27152 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
27153 OMP_CLAUSE_CHAIN (c
) = list
;
27162 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
27163 tree list
, location_t location
)
27167 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
27168 "ordered", location
);
27170 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
27171 OMP_CLAUSE_CHAIN (c
) = list
;
27176 reduction ( reduction-operator : variable-list )
27178 reduction-operator:
27179 One of: + * - & ^ | && ||
27183 reduction-operator:
27184 One of: + * - & ^ | && || min max
27188 reduction-operator:
27189 One of: + * - & ^ | && ||
27193 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
27195 enum tree_code code
= ERROR_MARK
;
27196 tree nlist
, c
, id
= NULL_TREE
;
27198 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27201 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27203 case CPP_PLUS
: code
= PLUS_EXPR
; break;
27204 case CPP_MULT
: code
= MULT_EXPR
; break;
27205 case CPP_MINUS
: code
= MINUS_EXPR
; break;
27206 case CPP_AND
: code
= BIT_AND_EXPR
; break;
27207 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
27208 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
27209 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
27210 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
27214 if (code
!= ERROR_MARK
)
27215 cp_lexer_consume_token (parser
->lexer
);
27218 bool saved_colon_corrects_to_scope_p
;
27219 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27220 parser
->colon_corrects_to_scope_p
= false;
27221 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
27222 /*check_dependency_p=*/true,
27223 /*template_p=*/NULL
,
27224 /*declarator_p=*/false,
27225 /*optional_p=*/false);
27226 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27227 if (identifier_p (id
))
27229 const char *p
= IDENTIFIER_POINTER (id
);
27231 if (strcmp (p
, "min") == 0)
27233 else if (strcmp (p
, "max") == 0)
27235 else if (id
== ansi_opname (PLUS_EXPR
))
27237 else if (id
== ansi_opname (MULT_EXPR
))
27239 else if (id
== ansi_opname (MINUS_EXPR
))
27241 else if (id
== ansi_opname (BIT_AND_EXPR
))
27242 code
= BIT_AND_EXPR
;
27243 else if (id
== ansi_opname (BIT_IOR_EXPR
))
27244 code
= BIT_IOR_EXPR
;
27245 else if (id
== ansi_opname (BIT_XOR_EXPR
))
27246 code
= BIT_XOR_EXPR
;
27247 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
27248 code
= TRUTH_ANDIF_EXPR
;
27249 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
27250 code
= TRUTH_ORIF_EXPR
;
27251 id
= omp_reduction_id (code
, id
, NULL_TREE
);
27252 tree scope
= parser
->scope
;
27254 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
27255 parser
->scope
= NULL_TREE
;
27256 parser
->qualifying_scope
= NULL_TREE
;
27257 parser
->object_scope
= NULL_TREE
;
27261 error ("invalid reduction-identifier");
27263 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27264 /*or_comma=*/false,
27265 /*consume_paren=*/true);
27270 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27273 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
27275 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27277 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
27278 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
27285 schedule ( schedule-kind )
27286 schedule ( schedule-kind , expression )
27289 static | dynamic | guided | runtime | auto */
27292 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
27296 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27299 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
27301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27303 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27304 const char *p
= IDENTIFIER_POINTER (id
);
27309 if (strcmp ("dynamic", p
) != 0)
27311 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
27315 if (strcmp ("guided", p
) != 0)
27317 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
27321 if (strcmp ("runtime", p
) != 0)
27323 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
27330 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
27331 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
27332 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
27333 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
27336 cp_lexer_consume_token (parser
->lexer
);
27338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27341 cp_lexer_consume_token (parser
->lexer
);
27343 token
= cp_lexer_peek_token (parser
->lexer
);
27344 t
= cp_parser_assignment_expression (parser
, false, NULL
);
27346 if (t
== error_mark_node
)
27348 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
27349 error_at (token
->location
, "schedule %<runtime%> does not take "
27350 "a %<chunk_size%> parameter");
27351 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
27352 error_at (token
->location
, "schedule %<auto%> does not take "
27353 "a %<chunk_size%> parameter");
27355 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
27357 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27360 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
27363 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
27364 OMP_CLAUSE_CHAIN (c
) = list
;
27368 cp_parser_error (parser
, "invalid schedule kind");
27370 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27371 /*or_comma=*/false,
27372 /*consume_paren=*/true);
27380 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
27381 tree list
, location_t location
)
27385 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
27387 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
27388 OMP_CLAUSE_CHAIN (c
) = list
;
27397 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
27398 tree list
, location_t location
)
27400 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
27401 tree c
= build_omp_clause (location
, code
);
27402 OMP_CLAUSE_CHAIN (c
) = list
;
27413 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
27414 enum omp_clause_code code
,
27415 tree list
, location_t location
)
27417 tree c
= build_omp_clause (location
, code
);
27418 OMP_CLAUSE_CHAIN (c
) = list
;
27423 num_teams ( expression ) */
27426 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
27427 location_t location
)
27431 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27434 t
= cp_parser_expression (parser
, false, NULL
);
27436 if (t
== error_mark_node
27437 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27438 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27439 /*or_comma=*/false,
27440 /*consume_paren=*/true);
27442 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
27443 "num_teams", location
);
27445 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
27446 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
27447 OMP_CLAUSE_CHAIN (c
) = list
;
27453 thread_limit ( expression ) */
27456 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
27457 location_t location
)
27461 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27464 t
= cp_parser_expression (parser
, false, NULL
);
27466 if (t
== error_mark_node
27467 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27468 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27469 /*or_comma=*/false,
27470 /*consume_paren=*/true);
27472 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
27473 "thread_limit", location
);
27475 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
27476 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
27477 OMP_CLAUSE_CHAIN (c
) = list
;
27483 aligned ( variable-list )
27484 aligned ( variable-list : constant-expression ) */
27487 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
27489 tree nlist
, c
, alignment
= NULL_TREE
;
27492 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27495 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
27500 alignment
= cp_parser_constant_expression (parser
, false, NULL
);
27502 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27503 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27504 /*or_comma=*/false,
27505 /*consume_paren=*/true);
27507 if (alignment
== error_mark_node
)
27508 alignment
= NULL_TREE
;
27511 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27512 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
27518 linear ( variable-list )
27519 linear ( variable-list : expression ) */
27522 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
)
27524 tree nlist
, c
, step
= integer_one_node
;
27527 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27530 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
27535 step
= cp_parser_expression (parser
, false, NULL
);
27537 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27538 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27539 /*or_comma=*/false,
27540 /*consume_paren=*/true);
27542 if (step
== error_mark_node
)
27546 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27547 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
27553 safelen ( constant-expression ) */
27556 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
27557 location_t location
)
27561 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27564 t
= cp_parser_constant_expression (parser
, false, NULL
);
27566 if (t
== error_mark_node
27567 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27568 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27569 /*or_comma=*/false,
27570 /*consume_paren=*/true);
27572 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
27574 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
27575 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
27576 OMP_CLAUSE_CHAIN (c
) = list
;
27582 simdlen ( constant-expression ) */
27585 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
27586 location_t location
)
27590 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27593 t
= cp_parser_constant_expression (parser
, false, NULL
);
27595 if (t
== error_mark_node
27596 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27597 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27598 /*or_comma=*/false,
27599 /*consume_paren=*/true);
27601 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
27603 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
27604 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
27605 OMP_CLAUSE_CHAIN (c
) = list
;
27611 depend ( depend-kind : variable-list )
27614 in | out | inout */
27617 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
27620 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
27622 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27627 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27628 const char *p
= IDENTIFIER_POINTER (id
);
27630 if (strcmp ("in", p
) == 0)
27631 kind
= OMP_CLAUSE_DEPEND_IN
;
27632 else if (strcmp ("inout", p
) == 0)
27633 kind
= OMP_CLAUSE_DEPEND_INOUT
;
27634 else if (strcmp ("out", p
) == 0)
27635 kind
= OMP_CLAUSE_DEPEND_OUT
;
27642 cp_lexer_consume_token (parser
->lexer
);
27643 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27646 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
27649 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27650 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
27655 cp_parser_error (parser
, "invalid depend kind");
27657 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27658 /*or_comma=*/false,
27659 /*consume_paren=*/true);
27664 map ( map-kind : variable-list )
27665 map ( variable-list )
27668 alloc | to | from | tofrom */
27671 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
27674 enum omp_clause_map_kind kind
= OMP_CLAUSE_MAP_TOFROM
;
27676 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
27680 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
27682 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27683 const char *p
= IDENTIFIER_POINTER (id
);
27685 if (strcmp ("alloc", p
) == 0)
27686 kind
= OMP_CLAUSE_MAP_ALLOC
;
27687 else if (strcmp ("to", p
) == 0)
27688 kind
= OMP_CLAUSE_MAP_TO
;
27689 else if (strcmp ("from", p
) == 0)
27690 kind
= OMP_CLAUSE_MAP_FROM
;
27691 else if (strcmp ("tofrom", p
) == 0)
27692 kind
= OMP_CLAUSE_MAP_TOFROM
;
27695 cp_parser_error (parser
, "invalid map kind");
27696 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27697 /*or_comma=*/false,
27698 /*consume_paren=*/true);
27701 cp_lexer_consume_token (parser
->lexer
);
27702 cp_lexer_consume_token (parser
->lexer
);
27705 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
27708 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27709 OMP_CLAUSE_MAP_KIND (c
) = kind
;
27715 device ( expression ) */
27718 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
27719 location_t location
)
27723 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27726 t
= cp_parser_expression (parser
, false, NULL
);
27728 if (t
== error_mark_node
27729 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27730 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27731 /*or_comma=*/false,
27732 /*consume_paren=*/true);
27734 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
27735 "device", location
);
27737 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
27738 OMP_CLAUSE_DEVICE_ID (c
) = t
;
27739 OMP_CLAUSE_CHAIN (c
) = list
;
27745 dist_schedule ( static )
27746 dist_schedule ( static , expression ) */
27749 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
27750 location_t location
)
27754 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27757 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
27759 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
27761 cp_lexer_consume_token (parser
->lexer
);
27763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27765 cp_lexer_consume_token (parser
->lexer
);
27767 t
= cp_parser_assignment_expression (parser
, false, NULL
);
27769 if (t
== error_mark_node
)
27771 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
27773 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27776 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
27779 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
27781 OMP_CLAUSE_CHAIN (c
) = list
;
27785 cp_parser_error (parser
, "invalid dist_schedule kind");
27787 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27788 /*or_comma=*/false,
27789 /*consume_paren=*/true);
27794 proc_bind ( proc-bind-kind )
27797 master | close | spread */
27800 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
27801 location_t location
)
27804 enum omp_clause_proc_bind_kind kind
;
27806 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27811 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27812 const char *p
= IDENTIFIER_POINTER (id
);
27814 if (strcmp ("master", p
) == 0)
27815 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
27816 else if (strcmp ("close", p
) == 0)
27817 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
27818 else if (strcmp ("spread", p
) == 0)
27819 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
27826 cp_lexer_consume_token (parser
->lexer
);
27827 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
27830 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
27831 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
27833 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
27834 OMP_CLAUSE_CHAIN (c
) = list
;
27838 cp_parser_error (parser
, "invalid depend kind");
27840 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27841 /*or_comma=*/false,
27842 /*consume_paren=*/true);
27846 /* Parse all OpenMP clauses. The set clauses allowed by the directive
27847 is a bitmask in MASK. Return the list of clauses found; the result
27848 of clause default goes in *pdefault. */
27851 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
27852 const char *where
, cp_token
*pragma_tok
,
27853 bool finish_p
= true)
27855 tree clauses
= NULL
;
27857 cp_token
*token
= NULL
;
27859 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
27861 pragma_omp_clause c_kind
;
27862 const char *c_name
;
27863 tree prev
= clauses
;
27865 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27866 cp_lexer_consume_token (parser
->lexer
);
27868 token
= cp_lexer_peek_token (parser
->lexer
);
27869 c_kind
= cp_parser_omp_clause_name (parser
);
27873 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
27874 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
27876 c_name
= "collapse";
27878 case PRAGMA_OMP_CLAUSE_COPYIN
:
27879 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
27882 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
27883 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
27885 c_name
= "copyprivate";
27887 case PRAGMA_OMP_CLAUSE_DEFAULT
:
27888 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
27890 c_name
= "default";
27892 case PRAGMA_OMP_CLAUSE_FINAL
:
27893 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
27896 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
27897 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
27899 c_name
= "firstprivate";
27901 case PRAGMA_OMP_CLAUSE_IF
:
27902 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
27905 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
27906 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
27908 c_name
= "lastprivate";
27910 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
27911 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
27913 c_name
= "mergeable";
27915 case PRAGMA_OMP_CLAUSE_NOWAIT
:
27916 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
27919 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
27920 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
27922 c_name
= "num_threads";
27924 case PRAGMA_OMP_CLAUSE_ORDERED
:
27925 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
27927 c_name
= "ordered";
27929 case PRAGMA_OMP_CLAUSE_PRIVATE
:
27930 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
27932 c_name
= "private";
27934 case PRAGMA_OMP_CLAUSE_REDUCTION
:
27935 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
27936 c_name
= "reduction";
27938 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
27939 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
27941 c_name
= "schedule";
27943 case PRAGMA_OMP_CLAUSE_SHARED
:
27944 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
27948 case PRAGMA_OMP_CLAUSE_UNTIED
:
27949 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
27953 case PRAGMA_OMP_CLAUSE_INBRANCH
:
27954 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
27955 clauses
, token
->location
);
27956 c_name
= "inbranch";
27958 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
27959 clauses
= cp_parser_omp_clause_branch (parser
,
27960 OMP_CLAUSE_NOTINBRANCH
,
27961 clauses
, token
->location
);
27962 c_name
= "notinbranch";
27964 case PRAGMA_OMP_CLAUSE_PARALLEL
:
27965 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
27966 clauses
, token
->location
);
27967 c_name
= "parallel";
27971 error_at (token
->location
, "%qs must be the first clause of %qs",
27976 case PRAGMA_OMP_CLAUSE_FOR
:
27977 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
27978 clauses
, token
->location
);
27981 goto clause_not_first
;
27983 case PRAGMA_OMP_CLAUSE_SECTIONS
:
27984 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
27985 clauses
, token
->location
);
27986 c_name
= "sections";
27988 goto clause_not_first
;
27990 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
27991 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
27992 clauses
, token
->location
);
27993 c_name
= "taskgroup";
27995 goto clause_not_first
;
27997 case PRAGMA_OMP_CLAUSE_TO
:
27998 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
28002 case PRAGMA_OMP_CLAUSE_FROM
:
28003 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
28007 case PRAGMA_OMP_CLAUSE_UNIFORM
:
28008 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
28010 c_name
= "uniform";
28012 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
28013 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
28015 c_name
= "num_teams";
28017 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
28018 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
28020 c_name
= "thread_limit";
28022 case PRAGMA_OMP_CLAUSE_ALIGNED
:
28023 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
28024 c_name
= "aligned";
28026 case PRAGMA_OMP_CLAUSE_LINEAR
:
28027 clauses
= cp_parser_omp_clause_linear (parser
, clauses
);
28030 case PRAGMA_OMP_CLAUSE_DEPEND
:
28031 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
28034 case PRAGMA_OMP_CLAUSE_MAP
:
28035 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
28038 case PRAGMA_OMP_CLAUSE_DEVICE
:
28039 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
28043 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
28044 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
28046 c_name
= "dist_schedule";
28048 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
28049 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
28051 c_name
= "proc_bind";
28053 case PRAGMA_OMP_CLAUSE_SAFELEN
:
28054 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
28056 c_name
= "safelen";
28058 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
28059 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
28061 c_name
= "simdlen";
28064 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
28070 if (((mask
>> c_kind
) & 1) == 0)
28072 /* Remove the invalid clause(s) from the list to avoid
28073 confusing the rest of the compiler. */
28075 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
28079 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
28081 return finish_omp_clauses (clauses
);
28089 In practice, we're also interested in adding the statement to an
28090 outer node. So it is convenient if we work around the fact that
28091 cp_parser_statement calls add_stmt. */
28094 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
28096 unsigned save
= parser
->in_statement
;
28098 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28099 This preserves the "not within loop or switch" style error messages
28100 for nonsense cases like
28106 if (parser
->in_statement
)
28107 parser
->in_statement
= IN_OMP_BLOCK
;
28113 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
28115 parser
->in_statement
= save
;
28119 cp_parser_omp_structured_block (cp_parser
*parser
)
28121 tree stmt
= begin_omp_structured_block ();
28122 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
28124 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
28126 cp_parser_end_omp_structured_block (parser
, save
);
28127 return finish_omp_structured_block (stmt
);
28131 # pragma omp atomic new-line
28135 x binop= expr | x++ | ++x | x-- | --x
28137 +, *, -, /, &, ^, |, <<, >>
28139 where x is an lvalue expression with scalar type.
28142 # pragma omp atomic new-line
28145 # pragma omp atomic read new-line
28148 # pragma omp atomic write new-line
28151 # pragma omp atomic update new-line
28154 # pragma omp atomic capture new-line
28157 # pragma omp atomic capture new-line
28165 expression-stmt | x = x binop expr
28167 v = expression-stmt
28169 { v = x; update-stmt; } | { update-stmt; v = x; }
28173 expression-stmt | x = x binop expr | x = expr binop x
28177 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28179 where x and v are lvalue expressions with scalar type. */
28182 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
28184 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
28185 tree rhs1
= NULL_TREE
, orig_lhs
;
28186 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
28187 bool structured_block
= false;
28188 bool seq_cst
= false;
28190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28192 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28193 const char *p
= IDENTIFIER_POINTER (id
);
28195 if (!strcmp (p
, "read"))
28196 code
= OMP_ATOMIC_READ
;
28197 else if (!strcmp (p
, "write"))
28199 else if (!strcmp (p
, "update"))
28201 else if (!strcmp (p
, "capture"))
28202 code
= OMP_ATOMIC_CAPTURE_NEW
;
28206 cp_lexer_consume_token (parser
->lexer
);
28209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28211 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28212 const char *p
= IDENTIFIER_POINTER (id
);
28214 if (!strcmp (p
, "seq_cst"))
28217 cp_lexer_consume_token (parser
->lexer
);
28220 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28224 case OMP_ATOMIC_READ
:
28225 case NOP_EXPR
: /* atomic write */
28226 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28227 /*cast_p=*/false, NULL
);
28228 if (v
== error_mark_node
)
28230 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28232 if (code
== NOP_EXPR
)
28233 lhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28235 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28236 /*cast_p=*/false, NULL
);
28237 if (lhs
== error_mark_node
)
28239 if (code
== NOP_EXPR
)
28241 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28249 case OMP_ATOMIC_CAPTURE_NEW
:
28250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28252 cp_lexer_consume_token (parser
->lexer
);
28253 structured_block
= true;
28257 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28258 /*cast_p=*/false, NULL
);
28259 if (v
== error_mark_node
)
28261 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28269 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28270 /*cast_p=*/false, NULL
);
28272 switch (TREE_CODE (lhs
))
28277 case POSTINCREMENT_EXPR
:
28278 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
28279 code
= OMP_ATOMIC_CAPTURE_OLD
;
28281 case PREINCREMENT_EXPR
:
28282 lhs
= TREE_OPERAND (lhs
, 0);
28283 opcode
= PLUS_EXPR
;
28284 rhs
= integer_one_node
;
28287 case POSTDECREMENT_EXPR
:
28288 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
28289 code
= OMP_ATOMIC_CAPTURE_OLD
;
28291 case PREDECREMENT_EXPR
:
28292 lhs
= TREE_OPERAND (lhs
, 0);
28293 opcode
= MINUS_EXPR
;
28294 rhs
= integer_one_node
;
28297 case COMPOUND_EXPR
:
28298 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
28299 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
28300 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
28301 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
28302 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28303 (TREE_OPERAND (lhs
, 1), 0), 0)))
28305 /* Undo effects of boolean_increment for post {in,de}crement. */
28306 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
28309 if (TREE_CODE (lhs
) == MODIFY_EXPR
28310 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
28312 /* Undo effects of boolean_increment. */
28313 if (integer_onep (TREE_OPERAND (lhs
, 1)))
28315 /* This is pre or post increment. */
28316 rhs
= TREE_OPERAND (lhs
, 1);
28317 lhs
= TREE_OPERAND (lhs
, 0);
28319 if (code
== OMP_ATOMIC_CAPTURE_NEW
28320 && !structured_block
28321 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
28322 code
= OMP_ATOMIC_CAPTURE_OLD
;
28328 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28331 opcode
= MULT_EXPR
;
28334 opcode
= TRUNC_DIV_EXPR
;
28337 opcode
= PLUS_EXPR
;
28340 opcode
= MINUS_EXPR
;
28342 case CPP_LSHIFT_EQ
:
28343 opcode
= LSHIFT_EXPR
;
28345 case CPP_RSHIFT_EQ
:
28346 opcode
= RSHIFT_EXPR
;
28349 opcode
= BIT_AND_EXPR
;
28352 opcode
= BIT_IOR_EXPR
;
28355 opcode
= BIT_XOR_EXPR
;
28358 enum cp_parser_prec oprec
;
28360 cp_lexer_consume_token (parser
->lexer
);
28361 cp_parser_parse_tentatively (parser
);
28362 rhs1
= cp_parser_simple_cast_expression (parser
);
28363 if (rhs1
== error_mark_node
)
28365 cp_parser_abort_tentative_parse (parser
);
28366 cp_parser_simple_cast_expression (parser
);
28369 token
= cp_lexer_peek_token (parser
->lexer
);
28370 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
28372 cp_parser_abort_tentative_parse (parser
);
28373 cp_parser_parse_tentatively (parser
);
28374 rhs
= cp_parser_binary_expression (parser
, false, true,
28375 PREC_NOT_OPERATOR
, NULL
);
28376 if (rhs
== error_mark_node
)
28378 cp_parser_abort_tentative_parse (parser
);
28379 cp_parser_binary_expression (parser
, false, true,
28380 PREC_NOT_OPERATOR
, NULL
);
28383 switch (TREE_CODE (rhs
))
28386 case TRUNC_DIV_EXPR
:
28394 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
28396 if (cp_parser_parse_definitely (parser
))
28398 opcode
= TREE_CODE (rhs
);
28399 rhs1
= TREE_OPERAND (rhs
, 0);
28400 rhs
= TREE_OPERAND (rhs
, 1);
28410 cp_parser_abort_tentative_parse (parser
);
28411 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
28413 rhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28414 if (rhs
== error_mark_node
)
28420 cp_parser_error (parser
,
28421 "invalid form of %<#pragma omp atomic%>");
28424 if (!cp_parser_parse_definitely (parser
))
28426 switch (token
->type
)
28428 case CPP_SEMICOLON
:
28429 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
28431 code
= OMP_ATOMIC_CAPTURE_OLD
;
28436 cp_lexer_consume_token (parser
->lexer
);
28439 else if (structured_block
)
28446 cp_parser_error (parser
,
28447 "invalid form of %<#pragma omp atomic%>");
28450 opcode
= MULT_EXPR
;
28453 opcode
= TRUNC_DIV_EXPR
;
28456 opcode
= PLUS_EXPR
;
28459 opcode
= MINUS_EXPR
;
28462 opcode
= LSHIFT_EXPR
;
28465 opcode
= RSHIFT_EXPR
;
28468 opcode
= BIT_AND_EXPR
;
28471 opcode
= BIT_IOR_EXPR
;
28474 opcode
= BIT_XOR_EXPR
;
28477 cp_parser_error (parser
,
28478 "invalid operator for %<#pragma omp atomic%>");
28481 oprec
= TOKEN_PRECEDENCE (token
);
28482 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
28483 if (commutative_tree_code (opcode
))
28484 oprec
= (enum cp_parser_prec
) (oprec
- 1);
28485 cp_lexer_consume_token (parser
->lexer
);
28486 rhs
= cp_parser_binary_expression (parser
, false, false,
28488 if (rhs
== error_mark_node
)
28493 cp_parser_error (parser
,
28494 "invalid operator for %<#pragma omp atomic%>");
28497 cp_lexer_consume_token (parser
->lexer
);
28499 rhs
= cp_parser_expression (parser
, false, NULL
);
28500 if (rhs
== error_mark_node
)
28505 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
28507 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
28509 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28510 /*cast_p=*/false, NULL
);
28511 if (v
== error_mark_node
)
28513 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28515 lhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28516 /*cast_p=*/false, NULL
);
28517 if (lhs1
== error_mark_node
)
28520 if (structured_block
)
28522 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28523 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
28526 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
28527 if (!structured_block
)
28528 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28532 cp_parser_skip_to_end_of_block_or_statement (parser
);
28533 if (structured_block
)
28535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28536 cp_lexer_consume_token (parser
->lexer
);
28537 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
28539 cp_parser_skip_to_end_of_block_or_statement (parser
);
28540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28541 cp_lexer_consume_token (parser
->lexer
);
28548 # pragma omp barrier new-line */
28551 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
28553 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28554 finish_omp_barrier ();
28558 # pragma omp critical [(name)] new-line
28559 structured-block */
28562 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
28564 tree stmt
, name
= NULL
;
28566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28568 cp_lexer_consume_token (parser
->lexer
);
28570 name
= cp_parser_identifier (parser
);
28572 if (name
== error_mark_node
28573 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28574 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28575 /*or_comma=*/false,
28576 /*consume_paren=*/true);
28577 if (name
== error_mark_node
)
28580 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28582 stmt
= cp_parser_omp_structured_block (parser
);
28583 return c_finish_omp_critical (input_location
, stmt
, name
);
28587 # pragma omp flush flush-vars[opt] new-line
28590 ( variable-list ) */
28593 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
28595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28596 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
28597 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28599 finish_omp_flush ();
28602 /* Helper function, to parse omp for increment expression. */
28605 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
28607 tree cond
= cp_parser_binary_expression (parser
, false, true,
28608 PREC_NOT_OPERATOR
, NULL
);
28609 if (cond
== error_mark_node
28610 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28612 cp_parser_skip_to_end_of_statement (parser
);
28613 return error_mark_node
;
28616 switch (TREE_CODE (cond
))
28624 if (code
== CILK_SIMD
)
28626 /* Fall through: OpenMP disallows NE_EXPR. */
28628 return error_mark_node
;
28631 /* If decl is an iterator, preserve LHS and RHS of the relational
28632 expr until finish_omp_for. */
28634 && (type_dependent_expression_p (decl
)
28635 || CLASS_TYPE_P (TREE_TYPE (decl
))))
28638 return build_x_binary_op (input_location
, TREE_CODE (cond
),
28639 TREE_OPERAND (cond
, 0), ERROR_MARK
,
28640 TREE_OPERAND (cond
, 1), ERROR_MARK
,
28641 /*overload=*/NULL
, tf_warning_or_error
);
28644 /* Helper function, to parse omp for increment expression. */
28647 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
28649 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28655 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
28657 op
= (token
->type
== CPP_PLUS_PLUS
28658 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
28659 cp_lexer_consume_token (parser
->lexer
);
28660 lhs
= cp_parser_simple_cast_expression (parser
);
28662 return error_mark_node
;
28663 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
28666 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
28668 return error_mark_node
;
28670 token
= cp_lexer_peek_token (parser
->lexer
);
28671 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
28673 op
= (token
->type
== CPP_PLUS_PLUS
28674 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
28675 cp_lexer_consume_token (parser
->lexer
);
28676 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
28679 op
= cp_parser_assignment_operator_opt (parser
);
28680 if (op
== ERROR_MARK
)
28681 return error_mark_node
;
28683 if (op
!= NOP_EXPR
)
28685 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
28686 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
28687 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
28690 lhs
= cp_parser_binary_expression (parser
, false, false,
28691 PREC_ADDITIVE_EXPRESSION
, NULL
);
28692 token
= cp_lexer_peek_token (parser
->lexer
);
28693 decl_first
= lhs
== decl
;
28696 if (token
->type
!= CPP_PLUS
28697 && token
->type
!= CPP_MINUS
)
28698 return error_mark_node
;
28702 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
28703 cp_lexer_consume_token (parser
->lexer
);
28704 rhs
= cp_parser_binary_expression (parser
, false, false,
28705 PREC_ADDITIVE_EXPRESSION
, NULL
);
28706 token
= cp_lexer_peek_token (parser
->lexer
);
28707 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
28709 if (lhs
== NULL_TREE
)
28711 if (op
== PLUS_EXPR
)
28714 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
28715 tf_warning_or_error
);
28718 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
28719 ERROR_MARK
, NULL
, tf_warning_or_error
);
28722 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
28726 if (rhs
!= decl
|| op
== MINUS_EXPR
)
28727 return error_mark_node
;
28728 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
28731 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
28733 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
28736 /* Parse the initialization statement of either an OpenMP for loop or
28737 a Cilk Plus for loop.
28739 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
28742 Return true if the resulting construct should have an
28743 OMP_CLAUSE_PRIVATE added to it. */
28746 cp_parser_omp_for_loop_init (cp_parser
*parser
,
28747 bool parsing_openmp
,
28748 tree
&this_pre_body
,
28749 vec
<tree
, va_gc
> *for_block
,
28754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28757 bool add_private_clause
= false;
28759 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
28763 integer-type var = lb
28764 random-access-iterator-type var = lb
28765 pointer-type var = lb
28767 cp_decl_specifier_seq type_specifiers
;
28769 /* First, try to parse as an initialized declaration. See
28770 cp_parser_condition, from whence the bulk of this is copied. */
28772 cp_parser_parse_tentatively (parser
);
28773 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
28774 /*is_trailing_return=*/false,
28776 if (cp_parser_parse_definitely (parser
))
28778 /* If parsing a type specifier seq succeeded, then this
28779 MUST be a initialized declaration. */
28780 tree asm_specification
, attributes
;
28781 cp_declarator
*declarator
;
28783 declarator
= cp_parser_declarator (parser
,
28784 CP_PARSER_DECLARATOR_NAMED
,
28785 /*ctor_dtor_or_conv_p=*/NULL
,
28786 /*parenthesized_p=*/NULL
,
28787 /*member_p=*/false);
28788 attributes
= cp_parser_attributes_opt (parser
);
28789 asm_specification
= cp_parser_asm_specification_opt (parser
);
28791 if (declarator
== cp_error_declarator
)
28792 cp_parser_skip_to_end_of_statement (parser
);
28796 tree pushed_scope
, auto_node
;
28798 decl
= start_decl (declarator
, &type_specifiers
,
28799 SD_INITIALIZED
, attributes
,
28800 /*prefix_attributes=*/NULL_TREE
,
28803 auto_node
= type_uses_auto (TREE_TYPE (decl
));
28804 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
28806 if (cp_lexer_next_token_is (parser
->lexer
,
28809 if (parsing_openmp
)
28810 error ("parenthesized initialization is not allowed in "
28811 "OpenMP %<for%> loop");
28813 error ("parenthesized initialization is "
28814 "not allowed in for-loop");
28817 /* Trigger an error. */
28818 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
28820 init
= error_mark_node
;
28821 cp_parser_skip_to_end_of_statement (parser
);
28823 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
28824 || type_dependent_expression_p (decl
)
28827 bool is_direct_init
, is_non_constant_init
;
28829 init
= cp_parser_initializer (parser
,
28831 &is_non_constant_init
);
28836 = do_auto_deduction (TREE_TYPE (decl
), init
,
28839 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
28840 && !type_dependent_expression_p (decl
))
28844 cp_finish_decl (decl
, init
, !is_non_constant_init
,
28846 LOOKUP_ONLYCONVERTING
);
28847 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
28849 vec_safe_push (for_block
, this_pre_body
);
28853 init
= pop_stmt_list (this_pre_body
);
28854 this_pre_body
= NULL_TREE
;
28859 cp_lexer_consume_token (parser
->lexer
);
28860 init
= cp_parser_assignment_expression (parser
, false, NULL
);
28863 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
28864 init
= error_mark_node
;
28866 cp_finish_decl (decl
, NULL_TREE
,
28867 /*init_const_expr_p=*/false,
28869 LOOKUP_ONLYCONVERTING
);
28873 pop_scope (pushed_scope
);
28879 /* If parsing a type specifier sequence failed, then
28880 this MUST be a simple expression. */
28881 cp_parser_parse_tentatively (parser
);
28882 decl
= cp_parser_primary_expression (parser
, false, false,
28884 if (!cp_parser_error_occurred (parser
)
28887 && CLASS_TYPE_P (TREE_TYPE (decl
)))
28891 cp_parser_parse_definitely (parser
);
28892 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
28893 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
28894 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
28897 tf_warning_or_error
));
28898 add_private_clause
= true;
28903 cp_parser_abort_tentative_parse (parser
);
28904 init
= cp_parser_expression (parser
, false, NULL
);
28907 if (TREE_CODE (init
) == MODIFY_EXPR
28908 || TREE_CODE (init
) == MODOP_EXPR
)
28909 real_decl
= TREE_OPERAND (init
, 0);
28913 return add_private_clause
;
28916 /* Parse the restricted form of the for statement allowed by OpenMP. */
28919 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
28922 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
28923 tree real_decl
, initv
, condv
, incrv
, declv
;
28924 tree this_pre_body
, cl
;
28925 location_t loc_first
;
28926 bool collapse_err
= false;
28927 int i
, collapse
= 1, nbraces
= 0;
28928 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
28930 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
28931 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
28932 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
28934 gcc_assert (collapse
>= 1);
28936 declv
= make_tree_vec (collapse
);
28937 initv
= make_tree_vec (collapse
);
28938 condv
= make_tree_vec (collapse
);
28939 incrv
= make_tree_vec (collapse
);
28941 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
28943 for (i
= 0; i
< collapse
; i
++)
28945 int bracecount
= 0;
28946 bool add_private_clause
= false;
28949 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
28951 cp_parser_error (parser
, "for statement expected");
28954 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
28956 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28959 init
= decl
= real_decl
= NULL
;
28960 this_pre_body
= push_stmt_list ();
28963 |= cp_parser_omp_for_loop_init (parser
,
28964 /*parsing_openmp=*/code
!= CILK_SIMD
,
28965 this_pre_body
, for_block
,
28966 init
, decl
, real_decl
);
28968 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
28971 this_pre_body
= pop_stmt_list (this_pre_body
);
28975 pre_body
= push_stmt_list ();
28977 add_stmt (this_pre_body
);
28978 pre_body
= pop_stmt_list (pre_body
);
28981 pre_body
= this_pre_body
;
28986 if (cclauses
!= NULL
28987 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
28988 && real_decl
!= NULL_TREE
)
28991 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
28992 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
28993 && OMP_CLAUSE_DECL (*c
) == real_decl
)
28995 error_at (loc
, "iteration variable %qD"
28996 " should not be firstprivate", real_decl
);
28997 *c
= OMP_CLAUSE_CHAIN (*c
);
28999 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
29000 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29002 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29003 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29004 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
29005 OMP_CLAUSE_DECL (l
) = real_decl
;
29006 OMP_CLAUSE_CHAIN (l
) = clauses
;
29007 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
29009 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
29010 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
29011 add_private_clause
= false;
29015 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
29016 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29017 add_private_clause
= false;
29018 c
= &OMP_CLAUSE_CHAIN (*c
);
29022 if (add_private_clause
)
29025 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
29027 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
29028 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
29029 && OMP_CLAUSE_DECL (c
) == decl
)
29031 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
29032 && OMP_CLAUSE_DECL (c
) == decl
)
29033 error_at (loc
, "iteration variable %qD "
29034 "should not be firstprivate",
29036 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
29037 && OMP_CLAUSE_DECL (c
) == decl
)
29038 error_at (loc
, "iteration variable %qD should not be reduction",
29043 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
29044 OMP_CLAUSE_DECL (c
) = decl
;
29045 c
= finish_omp_clauses (c
);
29048 OMP_CLAUSE_CHAIN (c
) = clauses
;
29055 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29056 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
29057 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29060 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29062 /* If decl is an iterator, preserve the operator on decl
29063 until finish_omp_for. */
29065 && ((processing_template_decl
29066 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
29067 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
29068 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
29070 incr
= cp_parser_expression (parser
, false, NULL
);
29071 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
29072 SET_EXPR_LOCATION (incr
, input_location
);
29075 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29076 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29077 /*or_comma=*/false,
29078 /*consume_paren=*/true);
29080 TREE_VEC_ELT (declv
, i
) = decl
;
29081 TREE_VEC_ELT (initv
, i
) = init
;
29082 TREE_VEC_ELT (condv
, i
) = cond
;
29083 TREE_VEC_ELT (incrv
, i
) = incr
;
29085 if (i
== collapse
- 1)
29088 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29089 in between the collapsed for loops to be still considered perfectly
29090 nested. Hopefully the final version clarifies this.
29091 For now handle (multiple) {'s and empty statements. */
29092 cp_parser_parse_tentatively (parser
);
29095 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29097 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29099 cp_lexer_consume_token (parser
->lexer
);
29102 else if (bracecount
29103 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29104 cp_lexer_consume_token (parser
->lexer
);
29107 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29108 error_at (loc
, "not enough collapsed for loops");
29109 collapse_err
= true;
29110 cp_parser_abort_tentative_parse (parser
);
29119 cp_parser_parse_definitely (parser
);
29120 nbraces
+= bracecount
;
29124 /* Note that we saved the original contents of this flag when we entered
29125 the structured block, and so we don't need to re-save it here. */
29126 if (code
== CILK_SIMD
)
29127 parser
->in_statement
= IN_CILK_SIMD_FOR
;
29129 parser
->in_statement
= IN_OMP_FOR
;
29131 /* Note that the grammar doesn't call for a structured block here,
29132 though the loop as a whole is a structured block. */
29133 body
= push_stmt_list ();
29134 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29135 body
= pop_stmt_list (body
);
29137 if (declv
== NULL_TREE
)
29140 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
29141 pre_body
, clauses
);
29145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29147 cp_lexer_consume_token (parser
->lexer
);
29150 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29151 cp_lexer_consume_token (parser
->lexer
);
29156 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29157 "collapsed loops not perfectly nested");
29159 collapse_err
= true;
29160 cp_parser_statement_seq_opt (parser
, NULL
);
29161 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
29166 while (!for_block
->is_empty ())
29167 add_stmt (pop_stmt_list (for_block
->pop ()));
29168 release_tree_vector (for_block
);
29173 /* Helper function for OpenMP parsing, split clauses and call
29174 finish_omp_clauses on each of the set of clauses afterwards. */
29177 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
29178 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
29181 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
29182 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
29184 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
29188 #pragma omp simd simd-clause[optseq] new-line
29191 #define OMP_SIMD_CLAUSE_MASK \
29192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29201 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
29202 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29204 tree clauses
, sb
, ret
;
29206 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29208 strcat (p_name
, " simd");
29209 mask
|= OMP_SIMD_CLAUSE_MASK
;
29210 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
29212 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29216 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
29217 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
29220 sb
= begin_omp_structured_block ();
29221 save
= cp_parser_begin_omp_structured_block (parser
);
29223 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
29225 cp_parser_end_omp_structured_block (parser
, save
);
29226 add_stmt (finish_omp_structured_block (sb
));
29232 #pragma omp for for-clause[optseq] new-line
29236 #pragma omp for simd for-simd-clause[optseq] new-line
29239 #define OMP_FOR_CLAUSE_MASK \
29240 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29250 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
29251 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29253 tree clauses
, sb
, ret
;
29255 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29257 strcat (p_name
, " for");
29258 mask
|= OMP_FOR_CLAUSE_MASK
;
29260 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
29262 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29264 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29265 const char *p
= IDENTIFIER_POINTER (id
);
29267 if (strcmp (p
, "simd") == 0)
29269 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29270 if (cclauses
== NULL
)
29271 cclauses
= cclauses_buf
;
29273 cp_lexer_consume_token (parser
->lexer
);
29274 if (!flag_openmp
) /* flag_openmp_simd */
29275 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29277 sb
= begin_omp_structured_block ();
29278 save
= cp_parser_begin_omp_structured_block (parser
);
29279 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29281 cp_parser_end_omp_structured_block (parser
, save
);
29282 tree body
= finish_omp_structured_block (sb
);
29285 ret
= make_node (OMP_FOR
);
29286 TREE_TYPE (ret
) = void_type_node
;
29287 OMP_FOR_BODY (ret
) = body
;
29288 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29289 SET_EXPR_LOCATION (ret
, loc
);
29294 if (!flag_openmp
) /* flag_openmp_simd */
29296 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29300 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29304 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
29305 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29308 sb
= begin_omp_structured_block ();
29309 save
= cp_parser_begin_omp_structured_block (parser
);
29311 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
29313 cp_parser_end_omp_structured_block (parser
, save
);
29314 add_stmt (finish_omp_structured_block (sb
));
29320 # pragma omp master new-line
29321 structured-block */
29324 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
29326 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29327 return c_finish_omp_master (input_location
,
29328 cp_parser_omp_structured_block (parser
));
29332 # pragma omp ordered new-line
29333 structured-block */
29336 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
29338 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29339 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29340 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
29346 { section-sequence }
29349 section-directive[opt] structured-block
29350 section-sequence section-directive structured-block */
29353 cp_parser_omp_sections_scope (cp_parser
*parser
)
29355 tree stmt
, substmt
;
29356 bool error_suppress
= false;
29359 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
29362 stmt
= push_stmt_list ();
29364 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
29366 substmt
= cp_parser_omp_structured_block (parser
);
29367 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
29368 add_stmt (substmt
);
29373 tok
= cp_lexer_peek_token (parser
->lexer
);
29374 if (tok
->type
== CPP_CLOSE_BRACE
)
29376 if (tok
->type
== CPP_EOF
)
29379 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
29381 cp_lexer_consume_token (parser
->lexer
);
29382 cp_parser_require_pragma_eol (parser
, tok
);
29383 error_suppress
= false;
29385 else if (!error_suppress
)
29387 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
29388 error_suppress
= true;
29391 substmt
= cp_parser_omp_structured_block (parser
);
29392 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
29393 add_stmt (substmt
);
29395 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29397 substmt
= pop_stmt_list (stmt
);
29399 stmt
= make_node (OMP_SECTIONS
);
29400 TREE_TYPE (stmt
) = void_type_node
;
29401 OMP_SECTIONS_BODY (stmt
) = substmt
;
29408 # pragma omp sections sections-clause[optseq] newline
29411 #define OMP_SECTIONS_CLAUSE_MASK \
29412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29419 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
29420 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29423 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29425 strcat (p_name
, " sections");
29426 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
29428 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
29430 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29434 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
29435 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
29438 ret
= cp_parser_omp_sections_scope (parser
);
29440 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
29446 # pragma parallel parallel-clause new-line
29447 # pragma parallel for parallel-for-clause new-line
29448 # pragma parallel sections parallel-sections-clause new-line
29451 # pragma parallel for simd parallel-for-simd-clause new-line */
29453 #define OMP_PARALLEL_CLAUSE_MASK \
29454 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29465 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
29466 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29468 tree stmt
, clauses
, block
;
29470 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29472 strcat (p_name
, " parallel");
29473 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
29475 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29477 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29478 if (cclauses
== NULL
)
29479 cclauses
= cclauses_buf
;
29481 cp_lexer_consume_token (parser
->lexer
);
29482 if (!flag_openmp
) /* flag_openmp_simd */
29483 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29484 block
= begin_omp_parallel ();
29485 save
= cp_parser_begin_omp_structured_block (parser
);
29486 cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29487 cp_parser_end_omp_structured_block (parser
, save
);
29488 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
29490 OMP_PARALLEL_COMBINED (stmt
) = 1;
29495 error_at (loc
, "expected %<for%> after %qs", p_name
);
29496 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29499 else if (!flag_openmp
) /* flag_openmp_simd */
29501 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29504 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29506 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29507 const char *p
= IDENTIFIER_POINTER (id
);
29508 if (strcmp (p
, "sections") == 0)
29510 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29511 cclauses
= cclauses_buf
;
29513 cp_lexer_consume_token (parser
->lexer
);
29514 block
= begin_omp_parallel ();
29515 save
= cp_parser_begin_omp_structured_block (parser
);
29516 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29517 cp_parser_end_omp_structured_block (parser
, save
);
29518 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
29520 OMP_PARALLEL_COMBINED (stmt
) = 1;
29525 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
29527 block
= begin_omp_parallel ();
29528 save
= cp_parser_begin_omp_structured_block (parser
);
29529 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29530 cp_parser_end_omp_structured_block (parser
, save
);
29531 stmt
= finish_omp_parallel (clauses
, block
);
29536 # pragma omp single single-clause[optseq] new-line
29537 structured-block */
29539 #define OMP_SINGLE_CLAUSE_MASK \
29540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29546 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
29548 tree stmt
= make_node (OMP_SINGLE
);
29549 TREE_TYPE (stmt
) = void_type_node
;
29551 OMP_SINGLE_CLAUSES (stmt
)
29552 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
29553 "#pragma omp single", pragma_tok
);
29554 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29556 return add_stmt (stmt
);
29560 # pragma omp task task-clause[optseq] new-line
29561 structured-block */
29563 #define OMP_TASK_CLAUSE_MASK \
29564 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29575 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
29577 tree clauses
, block
;
29580 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
29581 "#pragma omp task", pragma_tok
);
29582 block
= begin_omp_task ();
29583 save
= cp_parser_begin_omp_structured_block (parser
);
29584 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29585 cp_parser_end_omp_structured_block (parser
, save
);
29586 return finish_omp_task (clauses
, block
);
29590 # pragma omp taskwait new-line */
29593 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
29595 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29596 finish_omp_taskwait ();
29600 # pragma omp taskyield new-line */
29603 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
29605 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29606 finish_omp_taskyield ();
29610 # pragma omp taskgroup new-line
29611 structured-block */
29614 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
29616 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29617 return c_finish_omp_taskgroup (input_location
,
29618 cp_parser_omp_structured_block (parser
));
29623 # pragma omp threadprivate (variable-list) */
29626 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
29630 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29631 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29633 finish_omp_threadprivate (vars
);
29637 # pragma omp cancel cancel-clause[optseq] new-line */
29639 #define OMP_CANCEL_CLAUSE_MASK \
29640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29647 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
29649 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
29650 "#pragma omp cancel", pragma_tok
);
29651 finish_omp_cancel (clauses
);
29655 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
29657 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
29658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
29664 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
29667 bool point_seen
= false;
29669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29671 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29672 const char *p
= IDENTIFIER_POINTER (id
);
29674 if (strcmp (p
, "point") == 0)
29676 cp_lexer_consume_token (parser
->lexer
);
29682 cp_parser_error (parser
, "expected %<point%>");
29683 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29687 clauses
= cp_parser_omp_all_clauses (parser
,
29688 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
29689 "#pragma omp cancellation point",
29691 finish_omp_cancellation_point (clauses
);
29695 #pragma omp distribute distribute-clause[optseq] new-line
29698 #define OMP_DISTRIBUTE_CLAUSE_MASK \
29699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
29702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29705 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
29706 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29708 tree clauses
, sb
, ret
;
29710 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29712 strcat (p_name
, " distribute");
29713 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
29715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29717 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29718 const char *p
= IDENTIFIER_POINTER (id
);
29720 bool parallel
= false;
29722 if (strcmp (p
, "simd") == 0)
29725 parallel
= strcmp (p
, "parallel") == 0;
29726 if (parallel
|| simd
)
29728 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29729 if (cclauses
== NULL
)
29730 cclauses
= cclauses_buf
;
29731 cp_lexer_consume_token (parser
->lexer
);
29732 if (!flag_openmp
) /* flag_openmp_simd */
29735 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29738 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
29741 sb
= begin_omp_structured_block ();
29742 save
= cp_parser_begin_omp_structured_block (parser
);
29744 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29747 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
29749 cp_parser_end_omp_structured_block (parser
, save
);
29750 tree body
= finish_omp_structured_block (sb
);
29753 ret
= make_node (OMP_DISTRIBUTE
);
29754 TREE_TYPE (ret
) = void_type_node
;
29755 OMP_FOR_BODY (ret
) = body
;
29756 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
29757 SET_EXPR_LOCATION (ret
, loc
);
29762 if (!flag_openmp
) /* flag_openmp_simd */
29764 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29768 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29772 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
29773 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
29776 sb
= begin_omp_structured_block ();
29777 save
= cp_parser_begin_omp_structured_block (parser
);
29779 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
29781 cp_parser_end_omp_structured_block (parser
, save
);
29782 add_stmt (finish_omp_structured_block (sb
));
29788 # pragma omp teams teams-clause[optseq] new-line
29789 structured-block */
29791 #define OMP_TEAMS_CLAUSE_MASK \
29792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
29797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
29798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
29801 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
29802 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29804 tree clauses
, sb
, ret
;
29806 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29808 strcat (p_name
, " teams");
29809 mask
|= OMP_TEAMS_CLAUSE_MASK
;
29811 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29813 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29814 const char *p
= IDENTIFIER_POINTER (id
);
29815 if (strcmp (p
, "distribute") == 0)
29817 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29818 if (cclauses
== NULL
)
29819 cclauses
= cclauses_buf
;
29821 cp_lexer_consume_token (parser
->lexer
);
29822 if (!flag_openmp
) /* flag_openmp_simd */
29823 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
29825 sb
= begin_omp_structured_block ();
29826 save
= cp_parser_begin_omp_structured_block (parser
);
29827 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
29829 cp_parser_end_omp_structured_block (parser
, save
);
29830 tree body
= finish_omp_structured_block (sb
);
29833 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
29834 ret
= make_node (OMP_TEAMS
);
29835 TREE_TYPE (ret
) = void_type_node
;
29836 OMP_TEAMS_CLAUSES (ret
) = clauses
;
29837 OMP_TEAMS_BODY (ret
) = body
;
29838 return add_stmt (ret
);
29841 if (!flag_openmp
) /* flag_openmp_simd */
29843 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29847 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29851 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
29852 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
29855 tree stmt
= make_node (OMP_TEAMS
);
29856 TREE_TYPE (stmt
) = void_type_node
;
29857 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
29858 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29860 return add_stmt (stmt
);
29864 # pragma omp target data target-data-clause[optseq] new-line
29865 structured-block */
29867 #define OMP_TARGET_DATA_CLAUSE_MASK \
29868 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29873 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
29875 tree stmt
= make_node (OMP_TARGET_DATA
);
29876 TREE_TYPE (stmt
) = void_type_node
;
29878 OMP_TARGET_DATA_CLAUSES (stmt
)
29879 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
29880 "#pragma omp target data", pragma_tok
);
29881 keep_next_level (true);
29882 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29884 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
29885 return add_stmt (stmt
);
29889 # pragma omp target update target-update-clause[optseq] new-line */
29891 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
29892 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
29893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
29894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29898 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
29899 enum pragma_context context
)
29901 if (context
== pragma_stmt
)
29903 error_at (pragma_tok
->location
,
29904 "%<#pragma omp target update%> may only be "
29905 "used in compound statements");
29906 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29911 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
29912 "#pragma omp target update", pragma_tok
);
29913 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
29914 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
29916 error_at (pragma_tok
->location
,
29917 "%<#pragma omp target update must contain at least one "
29918 "%<from%> or %<to%> clauses");
29922 tree stmt
= make_node (OMP_TARGET_UPDATE
);
29923 TREE_TYPE (stmt
) = void_type_node
;
29924 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
29925 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
29931 # pragma omp target target-clause[optseq] new-line
29932 structured-block */
29934 #define OMP_TARGET_CLAUSE_MASK \
29935 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29940 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
29941 enum pragma_context context
)
29943 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
29945 cp_parser_error (parser
, "expected declaration specifiers");
29946 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29952 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29953 const char *p
= IDENTIFIER_POINTER (id
);
29955 if (strcmp (p
, "teams") == 0)
29957 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
29958 char p_name
[sizeof ("#pragma omp target teams distribute "
29959 "parallel for simd")];
29961 cp_lexer_consume_token (parser
->lexer
);
29962 strcpy (p_name
, "#pragma omp target");
29963 keep_next_level (true);
29964 if (!flag_openmp
) /* flag_openmp_simd */
29965 return cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
29966 OMP_TARGET_CLAUSE_MASK
, cclauses
);
29967 tree sb
= begin_omp_structured_block ();
29968 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
29969 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
29970 OMP_TARGET_CLAUSE_MASK
, cclauses
);
29971 cp_parser_end_omp_structured_block (parser
, save
);
29972 tree body
= finish_omp_structured_block (sb
);
29975 tree stmt
= make_node (OMP_TARGET
);
29976 TREE_TYPE (stmt
) = void_type_node
;
29977 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
29978 OMP_TARGET_BODY (stmt
) = body
;
29982 else if (!flag_openmp
) /* flag_openmp_simd */
29984 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29987 else if (strcmp (p
, "data") == 0)
29989 cp_lexer_consume_token (parser
->lexer
);
29990 cp_parser_omp_target_data (parser
, pragma_tok
);
29993 else if (strcmp (p
, "update") == 0)
29995 cp_lexer_consume_token (parser
->lexer
);
29996 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
30000 tree stmt
= make_node (OMP_TARGET
);
30001 TREE_TYPE (stmt
) = void_type_node
;
30003 OMP_TARGET_CLAUSES (stmt
)
30004 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
30005 "#pragma omp target", pragma_tok
);
30006 keep_next_level (true);
30007 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30009 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30015 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30017 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30026 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30027 enum pragma_context context
)
30029 bool first_p
= parser
->omp_declare_simd
== NULL
;
30030 cp_omp_declare_simd_data data
;
30033 data
.error_seen
= false;
30034 data
.fndecl_seen
= false;
30035 data
.tokens
= vNULL
;
30036 parser
->omp_declare_simd
= &data
;
30038 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30039 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30040 cp_lexer_consume_token (parser
->lexer
);
30041 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30042 parser
->omp_declare_simd
->error_seen
= true;
30043 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30044 struct cp_token_cache
*cp
30045 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
30046 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
30049 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
30050 cp_parser_pragma (parser
, context
);
30053 case pragma_external
:
30054 cp_parser_declaration (parser
);
30056 case pragma_member
:
30057 cp_parser_member_declaration (parser
);
30059 case pragma_objc_icode
:
30060 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30063 cp_parser_declaration_statement (parser
);
30066 if (parser
->omp_declare_simd
30067 && !parser
->omp_declare_simd
->error_seen
30068 && !parser
->omp_declare_simd
->fndecl_seen
)
30069 error_at (pragma_tok
->location
,
30070 "%<#pragma omp declare simd%> not immediately followed by "
30071 "function declaration or definition");
30072 data
.tokens
.release ();
30073 parser
->omp_declare_simd
= NULL
;
30077 /* Finalize #pragma omp declare simd clauses after direct declarator has
30078 been parsed, and put that into "omp declare simd" attribute. */
30081 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
30083 struct cp_token_cache
*ce
;
30084 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
30087 if (!data
->error_seen
&& data
->fndecl_seen
)
30089 error ("%<#pragma omp declare simd%> not immediately followed by "
30090 "a single function declaration or definition");
30091 data
->error_seen
= true;
30094 if (data
->error_seen
)
30097 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
30101 cp_parser_push_lexer_for_tokens (parser
, ce
);
30102 parser
->lexer
->in_pragma
= true;
30103 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
30104 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
30105 cp_lexer_consume_token (parser
->lexer
);
30106 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
30107 "#pragma omp declare simd", pragma_tok
);
30108 cp_parser_pop_lexer (parser
);
30110 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
30111 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
30112 TREE_CHAIN (c
) = attrs
;
30113 if (processing_template_decl
)
30114 ATTR_IS_DEPENDENT (c
) = 1;
30118 data
->fndecl_seen
= true;
30124 # pragma omp declare target new-line
30125 declarations and definitions
30126 # pragma omp end declare target new-line */
30129 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
30131 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30132 scope_chain
->omp_declare_target_attribute
++;
30136 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
30138 const char *p
= "";
30139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30141 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30142 p
= IDENTIFIER_POINTER (id
);
30144 if (strcmp (p
, "declare") == 0)
30146 cp_lexer_consume_token (parser
->lexer
);
30148 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30150 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30151 p
= IDENTIFIER_POINTER (id
);
30153 if (strcmp (p
, "target") == 0)
30154 cp_lexer_consume_token (parser
->lexer
);
30157 cp_parser_error (parser
, "expected %<target%>");
30158 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30164 cp_parser_error (parser
, "expected %<declare%>");
30165 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30168 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30169 if (!scope_chain
->omp_declare_target_attribute
)
30170 error_at (pragma_tok
->location
,
30171 "%<#pragma omp end declare target%> without corresponding "
30172 "%<#pragma omp declare target%>");
30174 scope_chain
->omp_declare_target_attribute
--;
30177 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30178 expression and optional initializer clause of
30179 #pragma omp declare reduction. We store the expression(s) as
30180 either 3, 6 or 7 special statements inside of the artificial function's
30181 body. The first two statements are DECL_EXPRs for the artificial
30182 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30183 expression that uses those variables.
30184 If there was any INITIALIZER clause, this is followed by further statements,
30185 the fourth and fifth statements are DECL_EXPRs for the artificial
30186 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30187 constructor variant (first token after open paren is not omp_priv),
30188 then the sixth statement is a statement with the function call expression
30189 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30190 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30191 to initialize the OMP_PRIV artificial variable and there is seventh
30192 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30195 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
30197 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
30198 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
30199 type
= TREE_TYPE (type
);
30200 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
30201 DECL_ARTIFICIAL (omp_out
) = 1;
30202 pushdecl (omp_out
);
30203 add_decl_expr (omp_out
);
30204 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
30205 DECL_ARTIFICIAL (omp_in
) = 1;
30207 add_decl_expr (omp_in
);
30209 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
30211 keep_next_level (true);
30212 tree block
= begin_omp_structured_block ();
30213 combiner
= cp_parser_expression (parser
, false, NULL
);
30214 finish_expr_stmt (combiner
);
30215 block
= finish_omp_structured_block (block
);
30218 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30221 const char *p
= "";
30222 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30224 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30225 p
= IDENTIFIER_POINTER (id
);
30228 if (strcmp (p
, "initializer") == 0)
30230 cp_lexer_consume_token (parser
->lexer
);
30231 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30237 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30238 p
= IDENTIFIER_POINTER (id
);
30241 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
30242 DECL_ARTIFICIAL (omp_priv
) = 1;
30243 pushdecl (omp_priv
);
30244 add_decl_expr (omp_priv
);
30245 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
30246 DECL_ARTIFICIAL (omp_orig
) = 1;
30247 pushdecl (omp_orig
);
30248 add_decl_expr (omp_orig
);
30250 keep_next_level (true);
30251 block
= begin_omp_structured_block ();
30254 if (strcmp (p
, "omp_priv") == 0)
30256 bool is_direct_init
, is_non_constant_init
;
30258 cp_lexer_consume_token (parser
->lexer
);
30259 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30260 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
30261 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
30262 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30264 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
30265 == CPP_CLOSE_PAREN
))
30267 finish_omp_structured_block (block
);
30268 error ("invalid initializer clause");
30271 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
30272 &is_non_constant_init
);
30273 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
30274 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
30278 cp_parser_parse_tentatively (parser
);
30279 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
30280 /*check_dependency_p=*/true,
30281 /*template_p=*/NULL
,
30282 /*declarator_p=*/false,
30283 /*optional_p=*/false);
30284 vec
<tree
, va_gc
> *args
;
30285 if (fn_name
== error_mark_node
30286 || cp_parser_error_occurred (parser
)
30287 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
30288 || ((args
= cp_parser_parenthesized_expression_list
30289 (parser
, non_attr
, /*cast_p=*/false,
30290 /*allow_expansion_p=*/true,
30291 /*non_constant_p=*/NULL
)),
30292 cp_parser_error_occurred (parser
)))
30294 finish_omp_structured_block (block
);
30295 cp_parser_abort_tentative_parse (parser
);
30296 cp_parser_error (parser
, "expected id-expression (arguments)");
30301 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
30302 if (arg
== omp_priv
30303 || (TREE_CODE (arg
) == ADDR_EXPR
30304 && TREE_OPERAND (arg
, 0) == omp_priv
))
30306 cp_parser_abort_tentative_parse (parser
);
30307 if (arg
== NULL_TREE
)
30308 error ("one of the initializer call arguments should be %<omp_priv%>"
30309 " or %<&omp_priv%>");
30310 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
30312 finish_expr_stmt (initializer
);
30315 block
= finish_omp_structured_block (block
);
30316 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
30317 finish_expr_stmt (block
);
30320 add_decl_expr (omp_orig
);
30322 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30326 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
30327 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
30333 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30334 initializer-clause[opt] new-line
30336 initializer-clause:
30337 initializer (omp_priv initializer)
30338 initializer (function-name (argument-list)) */
30341 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
30342 enum pragma_context
)
30344 vec
<tree
> types
= vNULL
;
30345 enum tree_code reduc_code
= ERROR_MARK
;
30346 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
30348 cp_token
*first_token
;
30349 cp_token_cache
*cp
;
30352 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30355 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
30358 reduc_code
= PLUS_EXPR
;
30361 reduc_code
= MULT_EXPR
;
30364 reduc_code
= MINUS_EXPR
;
30367 reduc_code
= BIT_AND_EXPR
;
30370 reduc_code
= BIT_XOR_EXPR
;
30373 reduc_code
= BIT_IOR_EXPR
;
30376 reduc_code
= TRUTH_ANDIF_EXPR
;
30379 reduc_code
= TRUTH_ORIF_EXPR
;
30382 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
30385 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30386 "%<|%>, %<&&%>, %<||%> or identifier");
30390 if (reduc_code
!= ERROR_MARK
)
30391 cp_lexer_consume_token (parser
->lexer
);
30393 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
30394 if (reduc_id
== error_mark_node
)
30397 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30400 /* Types may not be defined in declare reduction type list. */
30401 const char *saved_message
;
30402 saved_message
= parser
->type_definition_forbidden_message
;
30403 parser
->type_definition_forbidden_message
30404 = G_("types may not be defined in declare reduction type list");
30405 bool saved_colon_corrects_to_scope_p
;
30406 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30407 parser
->colon_corrects_to_scope_p
= false;
30408 bool saved_colon_doesnt_start_class_def_p
;
30409 saved_colon_doesnt_start_class_def_p
30410 = parser
->colon_doesnt_start_class_def_p
;
30411 parser
->colon_doesnt_start_class_def_p
= true;
30415 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30416 type
= cp_parser_type_id (parser
);
30417 if (type
== error_mark_node
)
30419 else if (ARITHMETIC_TYPE_P (type
)
30420 && (orig_reduc_id
== NULL_TREE
30421 || (TREE_CODE (type
) != COMPLEX_TYPE
30422 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
30424 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
30426 error_at (loc
, "predeclared arithmetic type %qT in "
30427 "%<#pragma omp declare reduction%>", type
);
30428 else if (TREE_CODE (type
) == FUNCTION_TYPE
30429 || TREE_CODE (type
) == METHOD_TYPE
30430 || TREE_CODE (type
) == ARRAY_TYPE
)
30431 error_at (loc
, "function or array type %qT in "
30432 "%<#pragma omp declare reduction%>", type
);
30433 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
30434 error_at (loc
, "reference type %qT in "
30435 "%<#pragma omp declare reduction%>", type
);
30436 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
30437 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
30438 "%<#pragma omp declare reduction%>", type
);
30440 types
.safe_push (type
);
30442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30443 cp_lexer_consume_token (parser
->lexer
);
30448 /* Restore the saved message. */
30449 parser
->type_definition_forbidden_message
= saved_message
;
30450 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30451 parser
->colon_doesnt_start_class_def_p
30452 = saved_colon_doesnt_start_class_def_p
;
30454 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
30455 || types
.is_empty ())
30458 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30463 first_token
= cp_lexer_peek_token (parser
->lexer
);
30466 FOR_EACH_VEC_ELT (types
, i
, type
)
30469 = build_function_type_list (void_type_node
,
30470 cp_build_reference_type (type
, false),
30472 tree this_reduc_id
= reduc_id
;
30473 if (!dependent_type_p (type
))
30474 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
30475 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
30476 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
30477 DECL_ARTIFICIAL (fndecl
) = 1;
30478 DECL_EXTERNAL (fndecl
) = 1;
30479 DECL_DECLARED_INLINE_P (fndecl
) = 1;
30480 DECL_IGNORED_P (fndecl
) = 1;
30481 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
30482 DECL_ATTRIBUTES (fndecl
)
30483 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
30484 DECL_ATTRIBUTES (fndecl
));
30485 if (processing_template_decl
)
30486 fndecl
= push_template_decl (fndecl
);
30487 bool block_scope
= false;
30488 tree block
= NULL_TREE
;
30489 if (current_function_decl
)
30491 block_scope
= true;
30492 DECL_CONTEXT (fndecl
) = global_namespace
;
30493 if (!processing_template_decl
)
30496 else if (current_class_type
)
30500 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30501 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30502 cp_lexer_consume_token (parser
->lexer
);
30503 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30505 cp
= cp_token_cache_new (first_token
,
30506 cp_lexer_peek_nth_token (parser
->lexer
,
30509 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
30510 finish_member_declaration (fndecl
);
30511 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
30512 DECL_PENDING_INLINE_P (fndecl
) = 1;
30513 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
30518 DECL_CONTEXT (fndecl
) = current_namespace
;
30522 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
30524 block
= begin_omp_structured_block ();
30527 cp_parser_push_lexer_for_tokens (parser
, cp
);
30528 parser
->lexer
->in_pragma
= true;
30530 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
30533 finish_function (0);
30535 DECL_CONTEXT (fndecl
) = current_function_decl
;
30537 cp_parser_pop_lexer (parser
);
30541 cp_parser_pop_lexer (parser
);
30543 finish_function (0);
30546 DECL_CONTEXT (fndecl
) = current_function_decl
;
30547 block
= finish_omp_structured_block (block
);
30548 if (TREE_CODE (block
) == BIND_EXPR
)
30549 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
30550 else if (TREE_CODE (block
) == STATEMENT_LIST
)
30551 DECL_SAVED_TREE (fndecl
) = block
;
30552 if (processing_template_decl
)
30553 add_decl_expr (fndecl
);
30555 cp_check_omp_declare_reduction (fndecl
);
30556 if (cp
== NULL
&& types
.length () > 1)
30557 cp
= cp_token_cache_new (first_token
,
30558 cp_lexer_peek_nth_token (parser
->lexer
, 2));
30559 if (errs
!= errorcount
)
30563 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30568 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30569 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30570 initializer-clause[opt] new-line
30571 #pragma omp declare target new-line */
30574 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
30575 enum pragma_context context
)
30577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30579 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30580 const char *p
= IDENTIFIER_POINTER (id
);
30582 if (strcmp (p
, "simd") == 0)
30584 cp_lexer_consume_token (parser
->lexer
);
30585 cp_parser_omp_declare_simd (parser
, pragma_tok
,
30589 cp_ensure_no_omp_declare_simd (parser
);
30590 if (strcmp (p
, "reduction") == 0)
30592 cp_lexer_consume_token (parser
->lexer
);
30593 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
30597 if (!flag_openmp
) /* flag_openmp_simd */
30599 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30602 if (strcmp (p
, "target") == 0)
30604 cp_lexer_consume_token (parser
->lexer
);
30605 cp_parser_omp_declare_target (parser
, pragma_tok
);
30609 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
30611 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30614 /* Main entry point to OpenMP statement pragmas. */
30617 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
30620 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
30621 omp_clause_mask
mask (0);
30623 switch (pragma_tok
->pragma_kind
)
30625 case PRAGMA_OMP_ATOMIC
:
30626 cp_parser_omp_atomic (parser
, pragma_tok
);
30628 case PRAGMA_OMP_CRITICAL
:
30629 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
30631 case PRAGMA_OMP_DISTRIBUTE
:
30632 strcpy (p_name
, "#pragma omp");
30633 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
30635 case PRAGMA_OMP_FOR
:
30636 strcpy (p_name
, "#pragma omp");
30637 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
30639 case PRAGMA_OMP_MASTER
:
30640 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
30642 case PRAGMA_OMP_ORDERED
:
30643 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
30645 case PRAGMA_OMP_PARALLEL
:
30646 strcpy (p_name
, "#pragma omp");
30647 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
30649 case PRAGMA_OMP_SECTIONS
:
30650 strcpy (p_name
, "#pragma omp");
30651 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
30653 case PRAGMA_OMP_SIMD
:
30654 strcpy (p_name
, "#pragma omp");
30655 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
30657 case PRAGMA_OMP_SINGLE
:
30658 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
30660 case PRAGMA_OMP_TASK
:
30661 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
30663 case PRAGMA_OMP_TASKGROUP
:
30664 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
30666 case PRAGMA_OMP_TEAMS
:
30667 strcpy (p_name
, "#pragma omp");
30668 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
30671 gcc_unreachable ();
30675 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30678 /* Transactional Memory parsing routines. */
30680 /* Parse a transaction attribute.
30686 ??? Simplify this when C++0x bracket attributes are
30687 implemented properly. */
30690 cp_parser_txn_attribute_opt (cp_parser
*parser
)
30693 tree attr_name
, attr
= NULL
;
30695 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30696 return cp_parser_attributes_opt (parser
);
30698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
30700 cp_lexer_consume_token (parser
->lexer
);
30701 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
30704 token
= cp_lexer_peek_token (parser
->lexer
);
30705 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
30707 token
= cp_lexer_consume_token (parser
->lexer
);
30709 attr_name
= (token
->type
== CPP_KEYWORD
30710 /* For keywords, use the canonical spelling,
30711 not the parsed identifier. */
30712 ? ridpointers
[(int) token
->keyword
]
30714 attr
= build_tree_list (attr_name
, NULL_TREE
);
30717 cp_parser_error (parser
, "expected identifier");
30719 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30721 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30725 /* Parse a __transaction_atomic or __transaction_relaxed statement.
30727 transaction-statement:
30728 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
30730 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
30734 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
30736 unsigned char old_in
= parser
->in_transaction
;
30737 unsigned char this_in
= 1, new_in
;
30739 tree stmt
, attrs
, noex
;
30741 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
30742 || keyword
== RID_TRANSACTION_RELAXED
);
30743 token
= cp_parser_require_keyword (parser
, keyword
,
30744 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
30745 : RT_TRANSACTION_RELAXED
));
30746 gcc_assert (token
!= NULL
);
30748 if (keyword
== RID_TRANSACTION_RELAXED
)
30749 this_in
|= TM_STMT_ATTR_RELAXED
;
30752 attrs
= cp_parser_txn_attribute_opt (parser
);
30754 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
30757 /* Parse a noexcept specification. */
30758 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
30760 /* Keep track if we're in the lexical scope of an outer transaction. */
30761 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
30763 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
30765 parser
->in_transaction
= new_in
;
30766 cp_parser_compound_statement (parser
, NULL
, false, false);
30767 parser
->in_transaction
= old_in
;
30769 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
30774 /* Parse a __transaction_atomic or __transaction_relaxed expression.
30776 transaction-expression:
30777 __transaction_atomic txn-noexcept-spec[opt] ( expression )
30778 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
30782 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
30784 unsigned char old_in
= parser
->in_transaction
;
30785 unsigned char this_in
= 1;
30790 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
30791 || keyword
== RID_TRANSACTION_RELAXED
);
30794 error (keyword
== RID_TRANSACTION_RELAXED
30795 ? G_("%<__transaction_relaxed%> without transactional memory "
30797 : G_("%<__transaction_atomic%> without transactional memory "
30798 "support enabled"));
30800 token
= cp_parser_require_keyword (parser
, keyword
,
30801 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
30802 : RT_TRANSACTION_RELAXED
));
30803 gcc_assert (token
!= NULL
);
30805 if (keyword
== RID_TRANSACTION_RELAXED
)
30806 this_in
|= TM_STMT_ATTR_RELAXED
;
30808 /* Set this early. This might mean that we allow transaction_cancel in
30809 an expression that we find out later actually has to be a constexpr.
30810 However, we expect that cxx_constant_value will be able to deal with
30811 this; also, if the noexcept has no constexpr, then what we parse next
30812 really is a transaction's body. */
30813 parser
->in_transaction
= this_in
;
30815 /* Parse a noexcept specification. */
30816 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
30819 if (!noex
|| !noex_expr
30820 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
30822 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
30824 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
30825 expr
= finish_parenthesized_expr (expr
);
30827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
30831 /* The only expression that is available got parsed for the noexcept
30832 already. noexcept is true then. */
30834 noex
= boolean_true_node
;
30837 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
30838 parser
->in_transaction
= old_in
;
30840 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
30841 return error_mark_node
;
30843 return (flag_tm
? expr
: error_mark_node
);
30846 /* Parse a function-transaction-block.
30848 function-transaction-block:
30849 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
30851 __transaction_atomic txn-attribute[opt] function-try-block
30852 __transaction_relaxed ctor-initializer[opt] function-body
30853 __transaction_relaxed function-try-block
30857 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
30859 unsigned char old_in
= parser
->in_transaction
;
30860 unsigned char new_in
= 1;
30861 tree compound_stmt
, stmt
, attrs
;
30862 bool ctor_initializer_p
;
30865 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
30866 || keyword
== RID_TRANSACTION_RELAXED
);
30867 token
= cp_parser_require_keyword (parser
, keyword
,
30868 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
30869 : RT_TRANSACTION_RELAXED
));
30870 gcc_assert (token
!= NULL
);
30872 if (keyword
== RID_TRANSACTION_RELAXED
)
30873 new_in
|= TM_STMT_ATTR_RELAXED
;
30876 attrs
= cp_parser_txn_attribute_opt (parser
);
30878 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
30881 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
30883 parser
->in_transaction
= new_in
;
30885 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
30886 ctor_initializer_p
= cp_parser_function_try_block (parser
);
30888 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
30889 (parser
, /*in_function_try_block=*/false);
30891 parser
->in_transaction
= old_in
;
30893 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
30895 return ctor_initializer_p
;
30898 /* Parse a __transaction_cancel statement.
30901 __transaction_cancel txn-attribute[opt] ;
30902 __transaction_cancel txn-attribute[opt] throw-expression ;
30904 ??? Cancel and throw is not yet implemented. */
30907 cp_parser_transaction_cancel (cp_parser
*parser
)
30910 bool is_outer
= false;
30913 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
30914 RT_TRANSACTION_CANCEL
);
30915 gcc_assert (token
!= NULL
);
30917 attrs
= cp_parser_txn_attribute_opt (parser
);
30919 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
30921 /* ??? Parse cancel-and-throw here. */
30923 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30927 error_at (token
->location
, "%<__transaction_cancel%> without "
30928 "transactional memory support enabled");
30929 return error_mark_node
;
30931 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
30933 error_at (token
->location
, "%<__transaction_cancel%> within a "
30934 "%<__transaction_relaxed%>");
30935 return error_mark_node
;
30939 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
30940 && !is_tm_may_cancel_outer (current_function_decl
))
30942 error_at (token
->location
, "outer %<__transaction_cancel%> not "
30943 "within outer %<__transaction_atomic%>");
30944 error_at (token
->location
,
30945 " or a %<transaction_may_cancel_outer%> function");
30946 return error_mark_node
;
30949 else if (parser
->in_transaction
== 0)
30951 error_at (token
->location
, "%<__transaction_cancel%> not within "
30952 "%<__transaction_atomic%>");
30953 return error_mark_node
;
30956 stmt
= build_tm_abort_call (token
->location
, is_outer
);
30964 static GTY (()) cp_parser
*the_parser
;
30967 /* Special handling for the first token or line in the file. The first
30968 thing in the file might be #pragma GCC pch_preprocess, which loads a
30969 PCH file, which is a GC collection point. So we need to handle this
30970 first pragma without benefit of an existing lexer structure.
30972 Always returns one token to the caller in *FIRST_TOKEN. This is
30973 either the true first token of the file, or the first token after
30974 the initial pragma. */
30977 cp_parser_initial_pragma (cp_token
*first_token
)
30981 cp_lexer_get_preprocessor_token (NULL
, first_token
);
30982 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
30985 cp_lexer_get_preprocessor_token (NULL
, first_token
);
30986 if (first_token
->type
== CPP_STRING
)
30988 name
= first_token
->u
.value
;
30990 cp_lexer_get_preprocessor_token (NULL
, first_token
);
30991 if (first_token
->type
!= CPP_PRAGMA_EOL
)
30992 error_at (first_token
->location
,
30993 "junk at end of %<#pragma GCC pch_preprocess%>");
30996 error_at (first_token
->location
, "expected string literal");
30998 /* Skip to the end of the pragma. */
30999 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
31000 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31002 /* Now actually load the PCH file. */
31004 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
31006 /* Read one more token to return to our caller. We have to do this
31007 after reading the PCH file in, since its pointers have to be
31009 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31012 /* Normal parsing of a pragma token. Here we can (and must) use the
31016 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
31018 cp_token
*pragma_tok
;
31021 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
31022 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
31023 parser
->lexer
->in_pragma
= true;
31025 id
= pragma_tok
->pragma_kind
;
31026 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
31027 cp_ensure_no_omp_declare_simd (parser
);
31030 case PRAGMA_GCC_PCH_PREPROCESS
:
31031 error_at (pragma_tok
->location
,
31032 "%<#pragma GCC pch_preprocess%> must be first");
31035 case PRAGMA_OMP_BARRIER
:
31038 case pragma_compound
:
31039 cp_parser_omp_barrier (parser
, pragma_tok
);
31042 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
31043 "used in compound statements");
31050 case PRAGMA_OMP_FLUSH
:
31053 case pragma_compound
:
31054 cp_parser_omp_flush (parser
, pragma_tok
);
31057 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
31058 "used in compound statements");
31065 case PRAGMA_OMP_TASKWAIT
:
31068 case pragma_compound
:
31069 cp_parser_omp_taskwait (parser
, pragma_tok
);
31072 error_at (pragma_tok
->location
,
31073 "%<#pragma omp taskwait%> may only be "
31074 "used in compound statements");
31081 case PRAGMA_OMP_TASKYIELD
:
31084 case pragma_compound
:
31085 cp_parser_omp_taskyield (parser
, pragma_tok
);
31088 error_at (pragma_tok
->location
,
31089 "%<#pragma omp taskyield%> may only be "
31090 "used in compound statements");
31097 case PRAGMA_OMP_CANCEL
:
31100 case pragma_compound
:
31101 cp_parser_omp_cancel (parser
, pragma_tok
);
31104 error_at (pragma_tok
->location
,
31105 "%<#pragma omp cancel%> may only be "
31106 "used in compound statements");
31113 case PRAGMA_OMP_CANCELLATION_POINT
:
31116 case pragma_compound
:
31117 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
31120 error_at (pragma_tok
->location
,
31121 "%<#pragma omp cancellation point%> may only be "
31122 "used in compound statements");
31129 case PRAGMA_OMP_THREADPRIVATE
:
31130 cp_parser_omp_threadprivate (parser
, pragma_tok
);
31133 case PRAGMA_OMP_DECLARE_REDUCTION
:
31134 cp_parser_omp_declare (parser
, pragma_tok
, context
);
31137 case PRAGMA_OMP_ATOMIC
:
31138 case PRAGMA_OMP_CRITICAL
:
31139 case PRAGMA_OMP_DISTRIBUTE
:
31140 case PRAGMA_OMP_FOR
:
31141 case PRAGMA_OMP_MASTER
:
31142 case PRAGMA_OMP_ORDERED
:
31143 case PRAGMA_OMP_PARALLEL
:
31144 case PRAGMA_OMP_SECTIONS
:
31145 case PRAGMA_OMP_SIMD
:
31146 case PRAGMA_OMP_SINGLE
:
31147 case PRAGMA_OMP_TASK
:
31148 case PRAGMA_OMP_TASKGROUP
:
31149 case PRAGMA_OMP_TEAMS
:
31150 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
31152 cp_parser_omp_construct (parser
, pragma_tok
);
31155 case PRAGMA_OMP_TARGET
:
31156 return cp_parser_omp_target (parser
, pragma_tok
, context
);
31158 case PRAGMA_OMP_END_DECLARE_TARGET
:
31159 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
31162 case PRAGMA_OMP_SECTION
:
31163 error_at (pragma_tok
->location
,
31164 "%<#pragma omp section%> may only be used in "
31165 "%<#pragma omp sections%> construct");
31170 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31172 tok
= cp_lexer_peek_token (the_parser
->lexer
);
31173 if (tok
->type
!= CPP_KEYWORD
31174 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
31175 && tok
->keyword
!= RID_DO
))
31177 cp_parser_error (parser
, "for, while or do statement expected");
31180 cp_parser_iteration_statement (parser
, true);
31184 case PRAGMA_CILK_SIMD
:
31185 if (context
== pragma_external
)
31187 error_at (pragma_tok
->location
,
31188 "%<#pragma simd%> must be inside a function");
31191 cp_parser_cilk_simd (parser
, pragma_tok
);
31195 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
31196 c_invoke_pragma_handler (id
);
31200 cp_parser_error (parser
, "expected declaration specifiers");
31204 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31208 /* The interface the pragma parsers have to the lexer. */
31211 pragma_lex (tree
*value
)
31214 enum cpp_ttype ret
;
31216 tok
= cp_lexer_peek_token (the_parser
->lexer
);
31219 *value
= tok
->u
.value
;
31221 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
31223 else if (ret
== CPP_STRING
)
31224 *value
= cp_parser_string_literal (the_parser
, false, false);
31227 cp_lexer_consume_token (the_parser
->lexer
);
31228 if (ret
== CPP_KEYWORD
)
31236 /* External interface. */
31238 /* Parse one entire translation unit. */
31241 c_parse_file (void)
31243 static bool already_called
= false;
31245 if (already_called
)
31247 sorry ("inter-module optimizations not implemented for C++");
31250 already_called
= true;
31252 the_parser
= cp_parser_new ();
31253 push_deferring_access_checks (flag_access_control
31254 ? dk_no_deferred
: dk_no_check
);
31255 cp_parser_translation_unit (the_parser
);
31259 /* Parses the Cilk Plus #pragma simd vectorlength clause:
31261 vectorlength ( constant-expression ) */
31264 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
)
31266 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31268 /* The vectorlength clause behaves exactly like OpenMP's safelen
31269 clause. Thus, vectorlength is represented as OMP 4.0
31271 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength", loc
);
31273 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31274 return error_mark_node
;
31276 expr
= cp_parser_constant_expression (parser
, false, NULL
);
31277 expr
= maybe_constant_value (expr
);
31279 if (TREE_CONSTANT (expr
)
31280 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
31281 error_at (loc
, "vectorlength must be a power of 2");
31282 else if (expr
!= error_mark_node
)
31284 tree c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
31285 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
31286 OMP_CLAUSE_CHAIN (c
) = clauses
;
31290 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31291 return error_mark_node
;
31295 /* Handles the Cilk Plus #pragma simd linear clause.
31297 linear ( simd-linear-variable-list )
31299 simd-linear-variable-list:
31300 simd-linear-variable
31301 simd-linear-variable-list , simd-linear-variable
31303 simd-linear-variable:
31305 id-expression : simd-linear-step
31308 conditional-expression */
31311 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
31313 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31315 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31317 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31319 cp_parser_error (parser
, "expected identifier");
31320 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31321 return error_mark_node
;
31324 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31325 parser
->colon_corrects_to_scope_p
= false;
31328 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31329 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31331 cp_parser_error (parser
, "expected variable-name");
31332 clauses
= error_mark_node
;
31336 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
31338 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
31340 if (decl
== error_mark_node
)
31342 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
31344 clauses
= error_mark_node
;
31348 tree e
= NULL_TREE
;
31349 tree step_size
= integer_one_node
;
31351 /* If present, parse the linear step. Otherwise, assume the default
31353 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
31355 cp_lexer_consume_token (parser
->lexer
);
31357 e
= cp_parser_assignment_expression (parser
, false, NULL
);
31358 e
= maybe_constant_value (e
);
31360 if (e
== error_mark_node
)
31362 /* If an error has occurred, then the whole pragma is
31363 considered ill-formed. Thus, no reason to keep
31365 clauses
= error_mark_node
;
31368 else if (type_dependent_expression_p (e
)
31369 || value_dependent_expression_p (e
)
31371 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
31372 && (TREE_CONSTANT (e
)
31376 cp_parser_error (parser
,
31377 "step size must be an integer constant "
31378 "expression or an integer variable");
31381 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31382 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
31383 OMP_CLAUSE_DECL (l
) = decl
;
31384 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
31385 OMP_CLAUSE_CHAIN (l
) = clauses
;
31388 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31389 cp_lexer_consume_token (parser
->lexer
);
31390 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31394 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31395 "expected %<,%> or %<)%> after %qE", decl
);
31396 clauses
= error_mark_node
;
31400 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31401 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31405 /* Returns the name of the next clause. If the clause is not
31406 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31407 token is not consumed. Otherwise, the appropriate enum from the
31408 pragma_simd_clause is returned and the token is consumed. */
31410 static pragma_cilk_clause
31411 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
31413 pragma_cilk_clause clause_type
;
31414 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31416 if (token
->keyword
== RID_PRIVATE
)
31417 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
31418 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
31419 return PRAGMA_CILK_CLAUSE_NONE
;
31420 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
31421 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
31422 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
31423 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
31424 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
31425 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
31426 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
31427 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
31428 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
31429 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
31431 return PRAGMA_CILK_CLAUSE_NONE
;
31433 cp_lexer_consume_token (parser
->lexer
);
31434 return clause_type
;
31437 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31440 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
31442 tree clauses
= NULL_TREE
;
31444 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31445 && clauses
!= error_mark_node
)
31447 pragma_cilk_clause c_kind
;
31448 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
31449 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
31450 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
);
31451 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
31452 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
31453 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
31454 /* Use the OpenMP 4.0 equivalent function. */
31455 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
31456 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
31457 /* Use the OpenMP 4.0 equivalent function. */
31458 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
31460 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
31461 /* Use the OMP 4.0 equivalent function. */
31462 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
31464 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
31465 /* Use the OMP 4.0 equivalent function. */
31466 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
31469 clauses
= error_mark_node
;
31470 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
31475 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
31477 if (clauses
== error_mark_node
)
31478 return error_mark_node
;
31480 return c_finish_cilk_clauses (clauses
);
31483 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31486 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
31488 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
31490 if (clauses
== error_mark_node
)
31493 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
31495 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31496 "for statement expected");
31500 tree sb
= begin_omp_structured_block ();
31501 int save
= cp_parser_begin_omp_structured_block (parser
);
31502 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
31504 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
31505 cp_parser_end_omp_structured_block (parser
, save
);
31506 add_stmt (finish_omp_structured_block (sb
));
31510 /* Create an identifier for a generic parameter type (a synthesized
31511 template parameter implied by `auto' or a concept identifier). */
31513 static GTY(()) int generic_parm_count
;
31515 make_generic_type_name ()
31518 sprintf (buf
, "auto:%d", ++generic_parm_count
);
31519 return get_identifier (buf
);
31522 /* Predicate that behaves as is_auto_or_concept but matches the parent
31523 node of the generic type rather than the generic type itself. This
31524 allows for type transformation in add_implicit_template_parms. */
31527 tree_type_is_auto_or_concept (const_tree t
)
31529 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
31532 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
31533 (creating a new template parameter list if necessary). Returns the newly
31534 created template type parm. */
31537 synthesize_implicit_template_parm (cp_parser
*parser
)
31539 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
31541 /* We are either continuing a function template that already contains implicit
31542 template parameters, creating a new fully-implicit function template, or
31543 extending an existing explicit function template with implicit template
31546 cp_binding_level
*const entry_scope
= current_binding_level
;
31548 bool become_template
= false;
31549 cp_binding_level
*parent_scope
= 0;
31551 if (parser
->implicit_template_scope
)
31553 gcc_assert (parser
->implicit_template_parms
);
31555 current_binding_level
= parser
->implicit_template_scope
;
31559 /* Roll back to the existing template parameter scope (in the case of
31560 extending an explicit function template) or introduce a new template
31561 parameter scope ahead of the function parameter scope (or class scope
31562 in the case of out-of-line member definitions). The function scope is
31563 added back after template parameter synthesis below. */
31565 cp_binding_level
*scope
= entry_scope
;
31567 while (scope
->kind
== sk_function_parms
)
31569 parent_scope
= scope
;
31570 scope
= scope
->level_chain
;
31572 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
)
31573 && parser
->num_classes_being_defined
== 0)
31574 while (scope
->kind
== sk_class
)
31576 parent_scope
= scope
;
31577 scope
= scope
->level_chain
;
31580 current_binding_level
= scope
;
31582 if (scope
->kind
!= sk_template_parms
)
31584 /* Introduce a new template parameter list for implicit template
31587 become_template
= true;
31589 parser
->implicit_template_scope
31590 = begin_scope (sk_template_parms
, NULL
);
31592 ++processing_template_decl
;
31594 parser
->fully_implicit_function_template_p
= true;
31595 ++parser
->num_template_parameter_lists
;
31599 /* Synthesize implicit template parameters at the end of the explicit
31600 template parameter list. */
31602 gcc_assert (current_template_parms
);
31604 parser
->implicit_template_scope
= scope
;
31606 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
31607 parser
->implicit_template_parms
31608 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
31612 /* Synthesize a new template parameter and track the current template
31613 parameter chain with implicit_template_parms. */
31615 tree synth_id
= make_generic_type_name ();
31616 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
31619 = process_template_parm (parser
->implicit_template_parms
,
31621 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
31622 /*non_type=*/false,
31623 /*param_pack=*/false);
31626 if (parser
->implicit_template_parms
)
31627 parser
->implicit_template_parms
31628 = TREE_CHAIN (parser
->implicit_template_parms
);
31630 parser
->implicit_template_parms
= new_parm
;
31632 tree new_type
= TREE_TYPE (getdecls ());
31634 /* If creating a fully implicit function template, start the new implicit
31635 template parameter list with this synthesized type, otherwise grow the
31636 current template parameter list. */
31638 if (become_template
)
31640 parent_scope
->level_chain
= current_binding_level
;
31642 tree new_parms
= make_tree_vec (1);
31643 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
31644 current_template_parms
= tree_cons (size_int (processing_template_decl
),
31645 new_parms
, current_template_parms
);
31649 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
31650 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
31651 new_parms
= grow_tree_vec_stat (new_parms
, new_parm_idx
+ 1);
31652 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
31655 current_binding_level
= entry_scope
;
31660 /* Finish the declaration of a fully implicit function template. Such a
31661 template has no explicit template parameter list so has not been through the
31662 normal template head and tail processing. synthesize_implicit_template_parm
31663 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
31664 provided if the declaration is a class member such that its template
31665 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
31666 form is returned. Otherwise NULL_TREE is returned. */
31669 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
31671 gcc_assert (parser
->fully_implicit_function_template_p
);
31673 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
31674 && DECL_VIRTUAL_P (member_decl_opt
))
31676 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
31677 "implicit templates may not be %<virtual%>");
31678 DECL_VIRTUAL_P (member_decl_opt
) = false;
31681 if (member_decl_opt
)
31682 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
31683 end_template_decl ();
31685 parser
->fully_implicit_function_template_p
= false;
31686 --parser
->num_template_parameter_lists
;
31688 return member_decl_opt
;
31691 #include "gt-cp-parser.h"