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 bool cp_parser_omp_declare_reduction_exprs
239 /* Manifest constants. */
240 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
241 #define CP_SAVED_TOKEN_STACK 5
245 /* The stream to which debugging output should be written. */
246 static FILE *cp_lexer_debug_stream
;
248 /* Nonzero if we are parsing an unevaluated operand: an operand to
249 sizeof, typeof, or alignof. */
250 int cp_unevaluated_operand
;
252 /* Dump up to NUM tokens in BUFFER to FILE starting with token
253 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
254 first token in BUFFER. If NUM is 0, dump all the tokens. If
255 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
256 highlighted by surrounding it in [[ ]]. */
259 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
260 cp_token
*start_token
, unsigned num
,
261 cp_token
*curr_token
)
263 unsigned i
, nprinted
;
267 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
273 num
= buffer
->length ();
275 if (start_token
== NULL
)
276 start_token
= buffer
->address ();
278 if (start_token
> buffer
->address ())
280 cp_lexer_print_token (file
, &(*buffer
)[0]);
281 fprintf (file
, " ... ");
286 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
288 if (token
== start_token
)
295 if (token
== curr_token
)
296 fprintf (file
, "[[");
298 cp_lexer_print_token (file
, token
);
300 if (token
== curr_token
)
301 fprintf (file
, "]]");
307 case CPP_CLOSE_BRACE
:
317 if (i
== num
&& i
< buffer
->length ())
319 fprintf (file
, " ... ");
320 cp_lexer_print_token (file
, &buffer
->last ());
323 fprintf (file
, "\n");
327 /* Dump all tokens in BUFFER to stderr. */
330 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
332 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
336 debug (vec
<cp_token
, va_gc
> &ref
)
338 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
342 debug (vec
<cp_token
, va_gc
> *ptr
)
347 fprintf (stderr
, "<nil>\n");
351 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
352 description for T. */
355 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
359 fprintf (file
, "%s: ", desc
);
360 print_node_brief (file
, "", t
, 0);
365 /* Dump parser context C to FILE. */
368 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
370 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
371 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
372 print_node_brief (file
, "", c
->object_type
, 0);
373 fprintf (file
, "}\n");
377 /* Print the stack of parsing contexts to FILE starting with FIRST. */
380 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
383 cp_parser_context
*c
;
385 fprintf (file
, "Parsing context stack:\n");
386 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
388 fprintf (file
, "\t#%u: ", i
);
389 cp_debug_print_context (file
, c
);
394 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
397 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
400 fprintf (file
, "%s: true\n", desc
);
404 /* Print an unparsed function entry UF to FILE. */
407 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
410 cp_default_arg_entry
*default_arg_fn
;
413 fprintf (file
, "\tFunctions with default args:\n");
415 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
418 fprintf (file
, "\t\tClass type: ");
419 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
420 fprintf (file
, "\t\tDeclaration: ");
421 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
422 fprintf (file
, "\n");
425 fprintf (file
, "\n\tFunctions with definitions that require "
426 "post-processing\n\t\t");
427 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
429 print_node_brief (file
, "", fn
, 0);
432 fprintf (file
, "\n");
434 fprintf (file
, "\n\tNon-static data members with initializers that require "
435 "post-processing\n\t\t");
436 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
438 print_node_brief (file
, "", fn
, 0);
441 fprintf (file
, "\n");
445 /* Print the stack of unparsed member functions S to FILE. */
448 cp_debug_print_unparsed_queues (FILE *file
,
449 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
452 cp_unparsed_functions_entry
*uf
;
454 fprintf (file
, "Unparsed functions\n");
455 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
457 fprintf (file
, "#%u:\n", i
);
458 cp_debug_print_unparsed_function (file
, uf
);
463 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
464 the given PARSER. If FILE is NULL, the output is printed on stderr. */
467 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
469 cp_token
*next_token
, *first_token
, *start_token
;
474 next_token
= parser
->lexer
->next_token
;
475 first_token
= parser
->lexer
->buffer
->address ();
476 start_token
= (next_token
> first_token
+ window_size
/ 2)
477 ? next_token
- window_size
/ 2
479 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
484 /* Dump debugging information for the given PARSER. If FILE is NULL,
485 the output is printed on stderr. */
488 cp_debug_parser (FILE *file
, cp_parser
*parser
)
490 const size_t window_size
= 20;
492 expanded_location eloc
;
497 fprintf (file
, "Parser state\n\n");
498 fprintf (file
, "Number of tokens: %u\n",
499 vec_safe_length (parser
->lexer
->buffer
));
500 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
501 cp_debug_print_tree_if_set (file
, "Object scope",
502 parser
->object_scope
);
503 cp_debug_print_tree_if_set (file
, "Qualifying scope",
504 parser
->qualifying_scope
);
505 cp_debug_print_context_stack (file
, parser
->context
);
506 cp_debug_print_flag (file
, "Allow GNU extensions",
507 parser
->allow_gnu_extensions_p
);
508 cp_debug_print_flag (file
, "'>' token is greater-than",
509 parser
->greater_than_is_operator_p
);
510 cp_debug_print_flag (file
, "Default args allowed in current "
511 "parameter list", parser
->default_arg_ok_p
);
512 cp_debug_print_flag (file
, "Parsing integral constant-expression",
513 parser
->integral_constant_expression_p
);
514 cp_debug_print_flag (file
, "Allow non-constant expression in current "
515 "constant-expression",
516 parser
->allow_non_integral_constant_expression_p
);
517 cp_debug_print_flag (file
, "Seen non-constant expression",
518 parser
->non_integral_constant_expression_p
);
519 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
521 parser
->local_variables_forbidden_p
);
522 cp_debug_print_flag (file
, "In unbraced linkage specification",
523 parser
->in_unbraced_linkage_specification_p
);
524 cp_debug_print_flag (file
, "Parsing a declarator",
525 parser
->in_declarator_p
);
526 cp_debug_print_flag (file
, "In template argument list",
527 parser
->in_template_argument_list_p
);
528 cp_debug_print_flag (file
, "Parsing an iteration statement",
529 parser
->in_statement
& IN_ITERATION_STMT
);
530 cp_debug_print_flag (file
, "Parsing a switch statement",
531 parser
->in_statement
& IN_SWITCH_STMT
);
532 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
533 parser
->in_statement
& IN_OMP_BLOCK
);
534 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
535 parser
->in_statement
& IN_OMP_FOR
);
536 cp_debug_print_flag (file
, "Parsing an if statement",
537 parser
->in_statement
& IN_IF_STMT
);
538 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
539 "context", parser
->in_type_id_in_expr_p
);
540 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
541 parser
->implicit_extern_c
);
542 cp_debug_print_flag (file
, "String expressions should be translated "
543 "to execution character set",
544 parser
->translate_strings_p
);
545 cp_debug_print_flag (file
, "Parsing function body outside of a "
546 "local class", parser
->in_function_body
);
547 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
548 parser
->colon_corrects_to_scope_p
);
549 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
550 parser
->colon_doesnt_start_class_def_p
);
551 if (parser
->type_definition_forbidden_message
)
552 fprintf (file
, "Error message for forbidden type definitions: %s\n",
553 parser
->type_definition_forbidden_message
);
554 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
555 fprintf (file
, "Number of class definitions in progress: %u\n",
556 parser
->num_classes_being_defined
);
557 fprintf (file
, "Number of template parameter lists for the current "
558 "declaration: %u\n", parser
->num_template_parameter_lists
);
559 cp_debug_parser_tokens (file
, parser
, window_size
);
560 token
= parser
->lexer
->next_token
;
561 fprintf (file
, "Next token to parse:\n");
562 fprintf (file
, "\tToken: ");
563 cp_lexer_print_token (file
, token
);
564 eloc
= expand_location (token
->location
);
565 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
566 fprintf (file
, "\tLine: %d\n", eloc
.line
);
567 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
571 debug (cp_parser
&ref
)
573 cp_debug_parser (stderr
, &ref
);
577 debug (cp_parser
*ptr
)
582 fprintf (stderr
, "<nil>\n");
585 /* Allocate memory for a new lexer object and return it. */
588 cp_lexer_alloc (void)
592 c_common_no_more_pch ();
594 /* Allocate the memory. */
595 lexer
= ggc_alloc_cleared_cp_lexer ();
597 /* Initially we are not debugging. */
598 lexer
->debugging_p
= false;
600 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
602 /* Create the buffer. */
603 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
609 /* Create a new main C++ lexer, the lexer that gets tokens from the
613 cp_lexer_new_main (void)
618 /* It's possible that parsing the first pragma will load a PCH file,
619 which is a GC collection point. So we have to do that before
620 allocating any memory. */
621 cp_parser_initial_pragma (&token
);
623 lexer
= cp_lexer_alloc ();
625 /* Put the first token in the buffer. */
626 lexer
->buffer
->quick_push (token
);
628 /* Get the remaining tokens from the preprocessor. */
629 while (token
.type
!= CPP_EOF
)
631 cp_lexer_get_preprocessor_token (lexer
, &token
);
632 vec_safe_push (lexer
->buffer
, token
);
635 lexer
->last_token
= lexer
->buffer
->address ()
636 + lexer
->buffer
->length ()
638 lexer
->next_token
= lexer
->buffer
->length ()
639 ? lexer
->buffer
->address ()
642 /* Subsequent preprocessor diagnostics should use compiler
643 diagnostic functions to get the compiler source location. */
646 gcc_assert (!lexer
->next_token
->purged_p
);
650 /* Create a new lexer whose token stream is primed with the tokens in
651 CACHE. When these tokens are exhausted, no new tokens will be read. */
654 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
656 cp_token
*first
= cache
->first
;
657 cp_token
*last
= cache
->last
;
658 cp_lexer
*lexer
= ggc_alloc_cleared_cp_lexer ();
660 /* We do not own the buffer. */
661 lexer
->buffer
= NULL
;
662 lexer
->next_token
= first
== last
? &eof_token
: first
;
663 lexer
->last_token
= last
;
665 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
667 /* Initially we are not debugging. */
668 lexer
->debugging_p
= false;
670 gcc_assert (!lexer
->next_token
->purged_p
);
674 /* Frees all resources associated with LEXER. */
677 cp_lexer_destroy (cp_lexer
*lexer
)
679 vec_free (lexer
->buffer
);
680 lexer
->saved_tokens
.release ();
684 /* Returns nonzero if debugging information should be output. */
687 cp_lexer_debugging_p (cp_lexer
*lexer
)
689 return lexer
->debugging_p
;
693 static inline cp_token_position
694 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
696 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
698 return lexer
->next_token
- previous_p
;
701 static inline cp_token
*
702 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
708 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
710 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
713 static inline cp_token_position
714 cp_lexer_previous_token_position (cp_lexer
*lexer
)
716 if (lexer
->next_token
== &eof_token
)
717 return lexer
->last_token
- 1;
719 return cp_lexer_token_position (lexer
, true);
722 static inline cp_token
*
723 cp_lexer_previous_token (cp_lexer
*lexer
)
725 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
727 return cp_lexer_token_at (lexer
, tp
);
730 /* nonzero if we are presently saving tokens. */
733 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
735 return lexer
->saved_tokens
.length () != 0;
738 /* Store the next token from the preprocessor in *TOKEN. Return true
739 if we reach EOF. If LEXER is NULL, assume we are handling an
740 initial #pragma pch_preprocess, and thus want the lexer to return
741 processed strings. */
744 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
746 static int is_extern_c
= 0;
748 /* Get a new token from the preprocessor. */
750 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
751 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
752 token
->keyword
= RID_MAX
;
753 token
->pragma_kind
= PRAGMA_NONE
;
754 token
->purged_p
= false;
756 /* On some systems, some header files are surrounded by an
757 implicit extern "C" block. Set a flag in the token if it
758 comes from such a header. */
759 is_extern_c
+= pending_lang_change
;
760 pending_lang_change
= 0;
761 token
->implicit_extern_c
= is_extern_c
> 0;
763 /* Check to see if this token is a keyword. */
764 if (token
->type
== CPP_NAME
)
766 if (C_IS_RESERVED_WORD (token
->u
.value
))
768 /* Mark this token as a keyword. */
769 token
->type
= CPP_KEYWORD
;
770 /* Record which keyword. */
771 token
->keyword
= C_RID_CODE (token
->u
.value
);
775 if (warn_cxx0x_compat
776 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
777 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
779 /* Warn about the C++0x keyword (but still treat it as
781 warning (OPT_Wc__0x_compat
,
782 "identifier %qE is a keyword in C++11",
785 /* Clear out the C_RID_CODE so we don't warn about this
786 particular identifier-turned-keyword again. */
787 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
790 token
->ambiguous_p
= false;
791 token
->keyword
= RID_MAX
;
794 else if (token
->type
== CPP_AT_NAME
)
796 /* This only happens in Objective-C++; it must be a keyword. */
797 token
->type
= CPP_KEYWORD
;
798 switch (C_RID_CODE (token
->u
.value
))
800 /* Replace 'class' with '@class', 'private' with '@private',
801 etc. This prevents confusion with the C++ keyword
802 'class', and makes the tokens consistent with other
803 Objective-C 'AT' keywords. For example '@class' is
804 reported as RID_AT_CLASS which is consistent with
805 '@synchronized', which is reported as
808 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
809 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
810 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
811 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
812 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
813 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
814 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
815 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
818 else if (token
->type
== CPP_PRAGMA
)
820 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
821 token
->pragma_kind
= ((enum pragma_kind
)
822 TREE_INT_CST_LOW (token
->u
.value
));
823 token
->u
.value
= NULL_TREE
;
827 /* Update the globals input_location and the input file stack from TOKEN. */
829 cp_lexer_set_source_position_from_token (cp_token
*token
)
831 if (token
->type
!= CPP_EOF
)
833 input_location
= token
->location
;
837 /* Return a pointer to the next token in the token stream, but do not
840 static inline cp_token
*
841 cp_lexer_peek_token (cp_lexer
*lexer
)
843 if (cp_lexer_debugging_p (lexer
))
845 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
846 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
847 putc ('\n', cp_lexer_debug_stream
);
849 return lexer
->next_token
;
852 /* Return true if the next token has the indicated TYPE. */
855 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
857 return cp_lexer_peek_token (lexer
)->type
== type
;
860 /* Return true if the next token does not have the indicated TYPE. */
863 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
865 return !cp_lexer_next_token_is (lexer
, type
);
868 /* Return true if the next token is the indicated KEYWORD. */
871 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
873 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
877 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
879 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
882 /* Return true if the next token is not the indicated KEYWORD. */
885 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
887 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
890 /* Return true if the next token is a keyword for a decl-specifier. */
893 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
897 token
= cp_lexer_peek_token (lexer
);
898 switch (token
->keyword
)
900 /* auto specifier: storage-class-specifier in C++,
901 simple-type-specifier in C++0x. */
903 /* Storage classes. */
909 /* Elaborated type specifiers. */
915 /* Simple type specifiers. */
930 /* GNU extensions. */
933 /* C++0x extensions. */
935 case RID_UNDERLYING_TYPE
:
943 /* Returns TRUE iff the token T begins a decltype type. */
946 token_is_decltype (cp_token
*t
)
948 return (t
->keyword
== RID_DECLTYPE
949 || t
->type
== CPP_DECLTYPE
);
952 /* Returns TRUE iff the next token begins a decltype type. */
955 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
957 cp_token
*t
= cp_lexer_peek_token (lexer
);
958 return token_is_decltype (t
);
961 /* Return a pointer to the Nth token in the token stream. If N is 1,
962 then this is precisely equivalent to cp_lexer_peek_token (except
963 that it is not inline). One would like to disallow that case, but
964 there is one case (cp_parser_nth_token_starts_template_id) where
965 the caller passes a variable for N and it might be 1. */
968 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
972 /* N is 1-based, not zero-based. */
975 if (cp_lexer_debugging_p (lexer
))
976 fprintf (cp_lexer_debug_stream
,
977 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
980 token
= lexer
->next_token
;
981 gcc_assert (!n
|| token
!= &eof_token
);
985 if (token
== lexer
->last_token
)
991 if (!token
->purged_p
)
995 if (cp_lexer_debugging_p (lexer
))
997 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
998 putc ('\n', cp_lexer_debug_stream
);
1004 /* Return the next token, and advance the lexer's next_token pointer
1005 to point to the next non-purged token. */
1008 cp_lexer_consume_token (cp_lexer
* lexer
)
1010 cp_token
*token
= lexer
->next_token
;
1012 gcc_assert (token
!= &eof_token
);
1013 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1017 lexer
->next_token
++;
1018 if (lexer
->next_token
== lexer
->last_token
)
1020 lexer
->next_token
= &eof_token
;
1025 while (lexer
->next_token
->purged_p
);
1027 cp_lexer_set_source_position_from_token (token
);
1029 /* Provide debugging output. */
1030 if (cp_lexer_debugging_p (lexer
))
1032 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1033 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1034 putc ('\n', cp_lexer_debug_stream
);
1040 /* Permanently remove the next token from the token stream, and
1041 advance the next_token pointer to refer to the next non-purged
1045 cp_lexer_purge_token (cp_lexer
*lexer
)
1047 cp_token
*tok
= lexer
->next_token
;
1049 gcc_assert (tok
!= &eof_token
);
1050 tok
->purged_p
= true;
1051 tok
->location
= UNKNOWN_LOCATION
;
1052 tok
->u
.value
= NULL_TREE
;
1053 tok
->keyword
= RID_MAX
;
1058 if (tok
== lexer
->last_token
)
1064 while (tok
->purged_p
);
1065 lexer
->next_token
= tok
;
1068 /* Permanently remove all tokens after TOK, up to, but not
1069 including, the token that will be returned next by
1070 cp_lexer_peek_token. */
1073 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1075 cp_token
*peek
= lexer
->next_token
;
1077 if (peek
== &eof_token
)
1078 peek
= lexer
->last_token
;
1080 gcc_assert (tok
< peek
);
1082 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1084 tok
->purged_p
= true;
1085 tok
->location
= UNKNOWN_LOCATION
;
1086 tok
->u
.value
= NULL_TREE
;
1087 tok
->keyword
= RID_MAX
;
1091 /* Begin saving tokens. All tokens consumed after this point will be
1095 cp_lexer_save_tokens (cp_lexer
* lexer
)
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer
))
1099 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1101 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1104 /* Commit to the portion of the token stream most recently saved. */
1107 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1109 /* Provide debugging output. */
1110 if (cp_lexer_debugging_p (lexer
))
1111 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1113 lexer
->saved_tokens
.pop ();
1116 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1117 to the token stream. Stop saving tokens. */
1120 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1122 /* Provide debugging output. */
1123 if (cp_lexer_debugging_p (lexer
))
1124 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1126 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1129 /* Print a representation of the TOKEN on the STREAM. */
1132 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1134 /* We don't use cpp_type2name here because the parser defines
1135 a few tokens of its own. */
1136 static const char *const token_names
[] = {
1137 /* cpplib-defined token types */
1138 #define OP(e, s) #e,
1139 #define TK(e, s) #e,
1143 /* C++ parser token types - see "Manifest constants", above. */
1146 "NESTED_NAME_SPECIFIER",
1149 /* For some tokens, print the associated data. */
1150 switch (token
->type
)
1153 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1154 For example, `struct' is mapped to an INTEGER_CST. */
1155 if (!identifier_p (token
->u
.value
))
1157 /* else fall through */
1159 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1166 case CPP_UTF8STRING
:
1167 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1171 print_generic_expr (stream
, token
->u
.value
, 0);
1175 /* If we have a name for the token, print it out. Otherwise, we
1176 simply give the numeric code. */
1177 if (token
->type
< ARRAY_SIZE(token_names
))
1178 fputs (token_names
[token
->type
], stream
);
1180 fprintf (stream
, "[%d]", token
->type
);
1186 debug (cp_token
&ref
)
1188 cp_lexer_print_token (stderr
, &ref
);
1189 fprintf (stderr
, "\n");
1193 debug (cp_token
*ptr
)
1198 fprintf (stderr
, "<nil>\n");
1202 /* Start emitting debugging information. */
1205 cp_lexer_start_debugging (cp_lexer
* lexer
)
1207 lexer
->debugging_p
= true;
1208 cp_lexer_debug_stream
= stderr
;
1211 /* Stop emitting debugging information. */
1214 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1216 lexer
->debugging_p
= false;
1217 cp_lexer_debug_stream
= NULL
;
1220 /* Create a new cp_token_cache, representing a range of tokens. */
1222 static cp_token_cache
*
1223 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1225 cp_token_cache
*cache
= ggc_alloc_cp_token_cache ();
1226 cache
->first
= first
;
1231 /* Diagnose if #pragma omp declare simd isn't followed immediately
1232 by function declaration or definition. */
1235 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1237 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1239 error ("%<#pragma omp declare simd%> not immediately followed by "
1240 "function declaration or definition");
1241 parser
->omp_declare_simd
= NULL
;
1245 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1246 and put that into "omp declare simd" attribute. */
1249 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1251 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1253 if (fndecl
== error_mark_node
)
1255 parser
->omp_declare_simd
= NULL
;
1258 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1260 cp_ensure_no_omp_declare_simd (parser
);
1266 /* Decl-specifiers. */
1268 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1271 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1273 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1278 /* Nothing other than the parser should be creating declarators;
1279 declarators are a semi-syntactic representation of C++ entities.
1280 Other parts of the front end that need to create entities (like
1281 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1283 static cp_declarator
*make_call_declarator
1284 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1285 static cp_declarator
*make_array_declarator
1286 (cp_declarator
*, tree
);
1287 static cp_declarator
*make_pointer_declarator
1288 (cp_cv_quals
, cp_declarator
*, tree
);
1289 static cp_declarator
*make_reference_declarator
1290 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1291 static cp_parameter_declarator
*make_parameter_declarator
1292 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1293 static cp_declarator
*make_ptrmem_declarator
1294 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1296 /* An erroneous declarator. */
1297 static cp_declarator
*cp_error_declarator
;
1299 /* The obstack on which declarators and related data structures are
1301 static struct obstack declarator_obstack
;
1303 /* Alloc BYTES from the declarator memory pool. */
1305 static inline void *
1306 alloc_declarator (size_t bytes
)
1308 return obstack_alloc (&declarator_obstack
, bytes
);
1311 /* Allocate a declarator of the indicated KIND. Clear fields that are
1312 common to all declarators. */
1314 static cp_declarator
*
1315 make_declarator (cp_declarator_kind kind
)
1317 cp_declarator
*declarator
;
1319 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1320 declarator
->kind
= kind
;
1321 declarator
->attributes
= NULL_TREE
;
1322 declarator
->std_attributes
= NULL_TREE
;
1323 declarator
->declarator
= NULL
;
1324 declarator
->parameter_pack_p
= false;
1325 declarator
->id_loc
= UNKNOWN_LOCATION
;
1330 /* Make a declarator for a generalized identifier. If
1331 QUALIFYING_SCOPE is non-NULL, the identifier is
1332 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1333 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1336 static cp_declarator
*
1337 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1338 special_function_kind sfk
)
1340 cp_declarator
*declarator
;
1342 /* It is valid to write:
1344 class C { void f(); };
1348 The standard is not clear about whether `typedef const C D' is
1349 legal; as of 2002-09-15 the committee is considering that
1350 question. EDG 3.0 allows that syntax. Therefore, we do as
1352 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1353 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1355 gcc_assert (identifier_p (unqualified_name
)
1356 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1357 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1359 declarator
= make_declarator (cdk_id
);
1360 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1361 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1362 declarator
->u
.id
.sfk
= sfk
;
1367 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1368 of modifiers such as const or volatile to apply to the pointer
1369 type, represented as identifiers. ATTRIBUTES represent the attributes that
1370 appertain to the pointer or reference. */
1373 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1376 cp_declarator
*declarator
;
1378 declarator
= make_declarator (cdk_pointer
);
1379 declarator
->declarator
= target
;
1380 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1381 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1384 declarator
->id_loc
= target
->id_loc
;
1385 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1386 target
->parameter_pack_p
= false;
1389 declarator
->parameter_pack_p
= false;
1391 declarator
->std_attributes
= attributes
;
1396 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1397 represent the attributes that appertain to the pointer or
1401 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1402 bool rvalue_ref
, tree attributes
)
1404 cp_declarator
*declarator
;
1406 declarator
= make_declarator (cdk_reference
);
1407 declarator
->declarator
= target
;
1408 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1409 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1412 declarator
->id_loc
= target
->id_loc
;
1413 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1414 target
->parameter_pack_p
= false;
1417 declarator
->parameter_pack_p
= false;
1419 declarator
->std_attributes
= attributes
;
1424 /* Like make_pointer_declarator -- but for a pointer to a non-static
1425 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1426 appertain to the pointer or reference. */
1429 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1430 cp_declarator
*pointee
,
1433 cp_declarator
*declarator
;
1435 declarator
= make_declarator (cdk_ptrmem
);
1436 declarator
->declarator
= pointee
;
1437 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1438 declarator
->u
.pointer
.class_type
= class_type
;
1442 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1443 pointee
->parameter_pack_p
= false;
1446 declarator
->parameter_pack_p
= false;
1448 declarator
->std_attributes
= attributes
;
1453 /* Make a declarator for the function given by TARGET, with the
1454 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1455 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1456 indicates what exceptions can be thrown. */
1459 make_call_declarator (cp_declarator
*target
,
1461 cp_cv_quals cv_qualifiers
,
1462 cp_virt_specifiers virt_specifiers
,
1463 cp_ref_qualifier ref_qualifier
,
1464 tree exception_specification
,
1465 tree late_return_type
)
1467 cp_declarator
*declarator
;
1469 declarator
= make_declarator (cdk_function
);
1470 declarator
->declarator
= target
;
1471 declarator
->u
.function
.parameters
= parms
;
1472 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1473 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1474 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1475 declarator
->u
.function
.exception_specification
= exception_specification
;
1476 declarator
->u
.function
.late_return_type
= late_return_type
;
1479 declarator
->id_loc
= target
->id_loc
;
1480 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1481 target
->parameter_pack_p
= false;
1484 declarator
->parameter_pack_p
= false;
1489 /* Make a declarator for an array of BOUNDS elements, each of which is
1490 defined by ELEMENT. */
1493 make_array_declarator (cp_declarator
*element
, tree bounds
)
1495 cp_declarator
*declarator
;
1497 declarator
= make_declarator (cdk_array
);
1498 declarator
->declarator
= element
;
1499 declarator
->u
.array
.bounds
= bounds
;
1502 declarator
->id_loc
= element
->id_loc
;
1503 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1504 element
->parameter_pack_p
= false;
1507 declarator
->parameter_pack_p
= false;
1512 /* Determine whether the declarator we've seen so far can be a
1513 parameter pack, when followed by an ellipsis. */
1515 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1517 /* Search for a declarator name, or any other declarator that goes
1518 after the point where the ellipsis could appear in a parameter
1519 pack. If we find any of these, then this declarator can not be
1520 made into a parameter pack. */
1522 while (declarator
&& !found
)
1524 switch ((int)declarator
->kind
)
1535 declarator
= declarator
->declarator
;
1543 cp_parameter_declarator
*no_parameters
;
1545 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1546 DECLARATOR and DEFAULT_ARGUMENT. */
1548 cp_parameter_declarator
*
1549 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1550 cp_declarator
*declarator
,
1551 tree default_argument
)
1553 cp_parameter_declarator
*parameter
;
1555 parameter
= ((cp_parameter_declarator
*)
1556 alloc_declarator (sizeof (cp_parameter_declarator
)));
1557 parameter
->next
= NULL
;
1558 if (decl_specifiers
)
1559 parameter
->decl_specifiers
= *decl_specifiers
;
1561 clear_decl_specs (¶meter
->decl_specifiers
);
1562 parameter
->declarator
= declarator
;
1563 parameter
->default_argument
= default_argument
;
1564 parameter
->ellipsis_p
= false;
1569 /* Returns true iff DECLARATOR is a declaration for a function. */
1572 function_declarator_p (const cp_declarator
*declarator
)
1576 if (declarator
->kind
== cdk_function
1577 && declarator
->declarator
->kind
== cdk_id
)
1579 if (declarator
->kind
== cdk_id
1580 || declarator
->kind
== cdk_error
)
1582 declarator
= declarator
->declarator
;
1592 A cp_parser parses the token stream as specified by the C++
1593 grammar. Its job is purely parsing, not semantic analysis. For
1594 example, the parser breaks the token stream into declarators,
1595 expressions, statements, and other similar syntactic constructs.
1596 It does not check that the types of the expressions on either side
1597 of an assignment-statement are compatible, or that a function is
1598 not declared with a parameter of type `void'.
1600 The parser invokes routines elsewhere in the compiler to perform
1601 semantic analysis and to build up the abstract syntax tree for the
1604 The parser (and the template instantiation code, which is, in a
1605 way, a close relative of parsing) are the only parts of the
1606 compiler that should be calling push_scope and pop_scope, or
1607 related functions. The parser (and template instantiation code)
1608 keeps track of what scope is presently active; everything else
1609 should simply honor that. (The code that generates static
1610 initializers may also need to set the scope, in order to check
1611 access control correctly when emitting the initializers.)
1616 The parser is of the standard recursive-descent variety. Upcoming
1617 tokens in the token stream are examined in order to determine which
1618 production to use when parsing a non-terminal. Some C++ constructs
1619 require arbitrary look ahead to disambiguate. For example, it is
1620 impossible, in the general case, to tell whether a statement is an
1621 expression or declaration without scanning the entire statement.
1622 Therefore, the parser is capable of "parsing tentatively." When the
1623 parser is not sure what construct comes next, it enters this mode.
1624 Then, while we attempt to parse the construct, the parser queues up
1625 error messages, rather than issuing them immediately, and saves the
1626 tokens it consumes. If the construct is parsed successfully, the
1627 parser "commits", i.e., it issues any queued error messages and
1628 the tokens that were being preserved are permanently discarded.
1629 If, however, the construct is not parsed successfully, the parser
1630 rolls back its state completely so that it can resume parsing using
1631 a different alternative.
1636 The performance of the parser could probably be improved substantially.
1637 We could often eliminate the need to parse tentatively by looking ahead
1638 a little bit. In some places, this approach might not entirely eliminate
1639 the need to parse tentatively, but it might still speed up the average
1642 /* Flags that are passed to some parsing functions. These values can
1643 be bitwise-ored together. */
1648 CP_PARSER_FLAGS_NONE
= 0x0,
1649 /* The construct is optional. If it is not present, then no error
1650 should be issued. */
1651 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1652 /* When parsing a type-specifier, treat user-defined type-names
1653 as non-type identifiers. */
1654 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1655 /* When parsing a type-specifier, do not try to parse a class-specifier
1656 or enum-specifier. */
1657 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1658 /* When parsing a decl-specifier-seq, only allow type-specifier or
1660 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1663 /* This type is used for parameters and variables which hold
1664 combinations of the above flags. */
1665 typedef int cp_parser_flags
;
1667 /* The different kinds of declarators we want to parse. */
1669 typedef enum cp_parser_declarator_kind
1671 /* We want an abstract declarator. */
1672 CP_PARSER_DECLARATOR_ABSTRACT
,
1673 /* We want a named declarator. */
1674 CP_PARSER_DECLARATOR_NAMED
,
1675 /* We don't mind, but the name must be an unqualified-id. */
1676 CP_PARSER_DECLARATOR_EITHER
1677 } cp_parser_declarator_kind
;
1679 /* The precedence values used to parse binary expressions. The minimum value
1680 of PREC must be 1, because zero is reserved to quickly discriminate
1681 binary operators from other tokens. */
1686 PREC_LOGICAL_OR_EXPRESSION
,
1687 PREC_LOGICAL_AND_EXPRESSION
,
1688 PREC_INCLUSIVE_OR_EXPRESSION
,
1689 PREC_EXCLUSIVE_OR_EXPRESSION
,
1690 PREC_AND_EXPRESSION
,
1691 PREC_EQUALITY_EXPRESSION
,
1692 PREC_RELATIONAL_EXPRESSION
,
1693 PREC_SHIFT_EXPRESSION
,
1694 PREC_ADDITIVE_EXPRESSION
,
1695 PREC_MULTIPLICATIVE_EXPRESSION
,
1697 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1700 /* A mapping from a token type to a corresponding tree node type, with a
1701 precedence value. */
1703 typedef struct cp_parser_binary_operations_map_node
1705 /* The token type. */
1706 enum cpp_ttype token_type
;
1707 /* The corresponding tree code. */
1708 enum tree_code tree_type
;
1709 /* The precedence of this operator. */
1710 enum cp_parser_prec prec
;
1711 } cp_parser_binary_operations_map_node
;
1713 typedef struct cp_parser_expression_stack_entry
1715 /* Left hand side of the binary operation we are currently
1718 /* Original tree code for left hand side, if it was a binary
1719 expression itself (used for -Wparentheses). */
1720 enum tree_code lhs_type
;
1721 /* Tree code for the binary operation we are parsing. */
1722 enum tree_code tree_type
;
1723 /* Precedence of the binary operation we are parsing. */
1724 enum cp_parser_prec prec
;
1725 /* Location of the binary operation we are parsing. */
1727 } cp_parser_expression_stack_entry
;
1729 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1730 entries because precedence levels on the stack are monotonically
1732 typedef struct cp_parser_expression_stack_entry
1733 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1737 /* Constructors and destructors. */
1739 static cp_parser_context
*cp_parser_context_new
1740 (cp_parser_context
*);
1742 /* Class variables. */
1744 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1746 /* The operator-precedence table used by cp_parser_binary_expression.
1747 Transformed into an associative array (binops_by_token) by
1750 static const cp_parser_binary_operations_map_node binops
[] = {
1751 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1752 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1754 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1755 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1756 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1758 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1759 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1761 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1762 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1764 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1765 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1766 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1767 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1769 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1770 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1772 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1774 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1776 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1778 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1780 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1783 /* The same as binops, but initialized by cp_parser_new so that
1784 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1786 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1788 /* Constructors and destructors. */
1790 /* Construct a new context. The context below this one on the stack
1791 is given by NEXT. */
1793 static cp_parser_context
*
1794 cp_parser_context_new (cp_parser_context
* next
)
1796 cp_parser_context
*context
;
1798 /* Allocate the storage. */
1799 if (cp_parser_context_free_list
!= NULL
)
1801 /* Pull the first entry from the free list. */
1802 context
= cp_parser_context_free_list
;
1803 cp_parser_context_free_list
= context
->next
;
1804 memset (context
, 0, sizeof (*context
));
1807 context
= ggc_alloc_cleared_cp_parser_context ();
1809 /* No errors have occurred yet in this context. */
1810 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1811 /* If this is not the bottommost context, copy information that we
1812 need from the previous context. */
1815 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1816 expression, then we are parsing one in this context, too. */
1817 context
->object_type
= next
->object_type
;
1818 /* Thread the stack. */
1819 context
->next
= next
;
1825 /* Managing the unparsed function queues. */
1827 #define unparsed_funs_with_default_args \
1828 parser->unparsed_queues->last ().funs_with_default_args
1829 #define unparsed_funs_with_definitions \
1830 parser->unparsed_queues->last ().funs_with_definitions
1831 #define unparsed_nsdmis \
1832 parser->unparsed_queues->last ().nsdmis
1835 push_unparsed_function_queues (cp_parser
*parser
)
1837 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
};
1838 vec_safe_push (parser
->unparsed_queues
, e
);
1842 pop_unparsed_function_queues (cp_parser
*parser
)
1844 release_tree_vector (unparsed_funs_with_definitions
);
1845 parser
->unparsed_queues
->pop ();
1850 /* Constructors and destructors. */
1852 static cp_parser
*cp_parser_new
1855 /* Routines to parse various constructs.
1857 Those that return `tree' will return the error_mark_node (rather
1858 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1859 Sometimes, they will return an ordinary node if error-recovery was
1860 attempted, even though a parse error occurred. So, to check
1861 whether or not a parse error occurred, you should always use
1862 cp_parser_error_occurred. If the construct is optional (indicated
1863 either by an `_opt' in the name of the function that does the
1864 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1865 the construct is not present. */
1867 /* Lexical conventions [gram.lex] */
1869 static tree cp_parser_identifier
1871 static tree cp_parser_string_literal
1872 (cp_parser
*, bool, bool);
1873 static tree cp_parser_userdef_char_literal
1875 static tree cp_parser_userdef_string_literal
1877 static tree cp_parser_userdef_numeric_literal
1880 /* Basic concepts [gram.basic] */
1882 static bool cp_parser_translation_unit
1885 /* Expressions [gram.expr] */
1887 static tree cp_parser_primary_expression
1888 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1889 static tree cp_parser_id_expression
1890 (cp_parser
*, bool, bool, bool *, bool, bool);
1891 static tree cp_parser_unqualified_id
1892 (cp_parser
*, bool, bool, bool, bool);
1893 static tree cp_parser_nested_name_specifier_opt
1894 (cp_parser
*, bool, bool, bool, bool);
1895 static tree cp_parser_nested_name_specifier
1896 (cp_parser
*, bool, bool, bool, bool);
1897 static tree cp_parser_qualifying_entity
1898 (cp_parser
*, bool, bool, bool, bool, bool);
1899 static tree cp_parser_postfix_expression
1900 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1901 static tree cp_parser_postfix_open_square_expression
1902 (cp_parser
*, tree
, bool, bool);
1903 static tree cp_parser_postfix_dot_deref_expression
1904 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1905 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1906 (cp_parser
*, int, bool, bool, bool *);
1907 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1908 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1909 static void cp_parser_pseudo_destructor_name
1910 (cp_parser
*, tree
, tree
*, tree
*);
1911 static tree cp_parser_unary_expression
1912 (cp_parser
*, bool, bool, cp_id_kind
*);
1913 static enum tree_code cp_parser_unary_operator
1915 static tree cp_parser_new_expression
1917 static vec
<tree
, va_gc
> *cp_parser_new_placement
1919 static tree cp_parser_new_type_id
1920 (cp_parser
*, tree
*);
1921 static cp_declarator
*cp_parser_new_declarator_opt
1923 static cp_declarator
*cp_parser_direct_new_declarator
1925 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1927 static tree cp_parser_delete_expression
1929 static tree cp_parser_cast_expression
1930 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1931 static tree cp_parser_binary_expression
1932 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1933 static tree cp_parser_question_colon_clause
1934 (cp_parser
*, tree
);
1935 static tree cp_parser_assignment_expression
1936 (cp_parser
*, bool, cp_id_kind
*);
1937 static enum tree_code cp_parser_assignment_operator_opt
1939 static tree cp_parser_expression
1940 (cp_parser
*, bool, cp_id_kind
*);
1941 static tree cp_parser_expression
1942 (cp_parser
*, bool, bool, cp_id_kind
*);
1943 static tree cp_parser_constant_expression
1944 (cp_parser
*, bool, bool *);
1945 static tree cp_parser_builtin_offsetof
1947 static tree cp_parser_lambda_expression
1949 static void cp_parser_lambda_introducer
1950 (cp_parser
*, tree
);
1951 static bool cp_parser_lambda_declarator_opt
1952 (cp_parser
*, tree
);
1953 static void cp_parser_lambda_body
1954 (cp_parser
*, tree
);
1956 /* Statements [gram.stmt.stmt] */
1958 static void cp_parser_statement
1959 (cp_parser
*, tree
, bool, bool *);
1960 static void cp_parser_label_for_labeled_statement
1961 (cp_parser
*, tree
);
1962 static tree cp_parser_expression_statement
1963 (cp_parser
*, tree
);
1964 static tree cp_parser_compound_statement
1965 (cp_parser
*, tree
, bool, bool);
1966 static void cp_parser_statement_seq_opt
1967 (cp_parser
*, tree
);
1968 static tree cp_parser_selection_statement
1969 (cp_parser
*, bool *);
1970 static tree cp_parser_condition
1972 static tree cp_parser_iteration_statement
1973 (cp_parser
*, bool);
1974 static bool cp_parser_for_init_statement
1975 (cp_parser
*, tree
*decl
);
1976 static tree cp_parser_for
1977 (cp_parser
*, bool);
1978 static tree cp_parser_c_for
1979 (cp_parser
*, tree
, tree
, bool);
1980 static tree cp_parser_range_for
1981 (cp_parser
*, tree
, tree
, tree
, bool);
1982 static void do_range_for_auto_deduction
1984 static tree cp_parser_perform_range_for_lookup
1985 (tree
, tree
*, tree
*);
1986 static tree cp_parser_range_for_member_function
1988 static tree cp_parser_jump_statement
1990 static void cp_parser_declaration_statement
1993 static tree cp_parser_implicitly_scoped_statement
1994 (cp_parser
*, bool *);
1995 static void cp_parser_already_scoped_statement
1998 /* Declarations [gram.dcl.dcl] */
2000 static void cp_parser_declaration_seq_opt
2002 static void cp_parser_declaration
2004 static void cp_parser_block_declaration
2005 (cp_parser
*, bool);
2006 static void cp_parser_simple_declaration
2007 (cp_parser
*, bool, tree
*);
2008 static void cp_parser_decl_specifier_seq
2009 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2010 static tree cp_parser_storage_class_specifier_opt
2012 static tree cp_parser_function_specifier_opt
2013 (cp_parser
*, cp_decl_specifier_seq
*);
2014 static tree cp_parser_type_specifier
2015 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2017 static tree cp_parser_simple_type_specifier
2018 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2019 static tree cp_parser_type_name
2021 static tree cp_parser_nonclass_name
2022 (cp_parser
* parser
);
2023 static tree cp_parser_elaborated_type_specifier
2024 (cp_parser
*, bool, bool);
2025 static tree cp_parser_enum_specifier
2027 static void cp_parser_enumerator_list
2028 (cp_parser
*, tree
);
2029 static void cp_parser_enumerator_definition
2030 (cp_parser
*, tree
);
2031 static tree cp_parser_namespace_name
2033 static void cp_parser_namespace_definition
2035 static void cp_parser_namespace_body
2037 static tree cp_parser_qualified_namespace_specifier
2039 static void cp_parser_namespace_alias_definition
2041 static bool cp_parser_using_declaration
2042 (cp_parser
*, bool);
2043 static void cp_parser_using_directive
2045 static tree cp_parser_alias_declaration
2047 static void cp_parser_asm_definition
2049 static void cp_parser_linkage_specification
2051 static void cp_parser_static_assert
2052 (cp_parser
*, bool);
2053 static tree cp_parser_decltype
2056 /* Declarators [gram.dcl.decl] */
2058 static tree cp_parser_init_declarator
2059 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*);
2060 static cp_declarator
*cp_parser_declarator
2061 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
2062 static cp_declarator
*cp_parser_direct_declarator
2063 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
2064 static enum tree_code cp_parser_ptr_operator
2065 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2066 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2068 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2070 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2072 static tree cp_parser_late_return_type_opt
2073 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2074 static tree cp_parser_declarator_id
2075 (cp_parser
*, bool);
2076 static tree cp_parser_type_id
2078 static tree cp_parser_template_type_arg
2080 static tree
cp_parser_trailing_type_id (cp_parser
*);
2081 static tree cp_parser_type_id_1
2082 (cp_parser
*, bool, bool);
2083 static void cp_parser_type_specifier_seq
2084 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2085 static tree cp_parser_parameter_declaration_clause
2087 static tree cp_parser_parameter_declaration_list
2088 (cp_parser
*, bool *);
2089 static cp_parameter_declarator
*cp_parser_parameter_declaration
2090 (cp_parser
*, bool, bool *);
2091 static tree cp_parser_default_argument
2092 (cp_parser
*, bool);
2093 static void cp_parser_function_body
2094 (cp_parser
*, bool);
2095 static tree cp_parser_initializer
2096 (cp_parser
*, bool *, bool *);
2097 static tree cp_parser_initializer_clause
2098 (cp_parser
*, bool *);
2099 static tree cp_parser_braced_list
2100 (cp_parser
*, bool*);
2101 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2102 (cp_parser
*, bool *);
2104 static bool cp_parser_ctor_initializer_opt_and_function_body
2105 (cp_parser
*, bool);
2107 static tree cp_parser_late_parsing_omp_declare_simd
2108 (cp_parser
*, tree
);
2110 static tree add_implicit_template_parms
2111 (cp_parser
*, size_t, tree
);
2112 static tree finish_fully_implicit_template
2113 (cp_parser
*, tree
);
2115 /* Classes [gram.class] */
2117 static tree cp_parser_class_name
2118 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2119 static tree cp_parser_class_specifier
2121 static tree cp_parser_class_head
2122 (cp_parser
*, bool *);
2123 static enum tag_types cp_parser_class_key
2125 static void cp_parser_member_specification_opt
2127 static void cp_parser_member_declaration
2129 static tree cp_parser_pure_specifier
2131 static tree cp_parser_constant_initializer
2134 /* Derived classes [gram.class.derived] */
2136 static tree cp_parser_base_clause
2138 static tree cp_parser_base_specifier
2141 /* Special member functions [gram.special] */
2143 static tree cp_parser_conversion_function_id
2145 static tree cp_parser_conversion_type_id
2147 static cp_declarator
*cp_parser_conversion_declarator_opt
2149 static bool cp_parser_ctor_initializer_opt
2151 static void cp_parser_mem_initializer_list
2153 static tree cp_parser_mem_initializer
2155 static tree cp_parser_mem_initializer_id
2158 /* Overloading [gram.over] */
2160 static tree cp_parser_operator_function_id
2162 static tree cp_parser_operator
2165 /* Templates [gram.temp] */
2167 static void cp_parser_template_declaration
2168 (cp_parser
*, bool);
2169 static tree cp_parser_template_parameter_list
2171 static tree cp_parser_template_parameter
2172 (cp_parser
*, bool *, bool *);
2173 static tree cp_parser_type_parameter
2174 (cp_parser
*, bool *);
2175 static tree cp_parser_template_id
2176 (cp_parser
*, bool, bool, enum tag_types
, bool);
2177 static tree cp_parser_template_name
2178 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2179 static tree cp_parser_template_argument_list
2181 static tree cp_parser_template_argument
2183 static void cp_parser_explicit_instantiation
2185 static void cp_parser_explicit_specialization
2188 /* Exception handling [gram.exception] */
2190 static tree cp_parser_try_block
2192 static bool cp_parser_function_try_block
2194 static void cp_parser_handler_seq
2196 static void cp_parser_handler
2198 static tree cp_parser_exception_declaration
2200 static tree cp_parser_throw_expression
2202 static tree cp_parser_exception_specification_opt
2204 static tree cp_parser_type_id_list
2207 /* GNU Extensions */
2209 static tree cp_parser_asm_specification_opt
2211 static tree cp_parser_asm_operand_list
2213 static tree cp_parser_asm_clobber_list
2215 static tree cp_parser_asm_label_list
2217 static bool cp_next_tokens_can_be_attribute_p
2219 static bool cp_next_tokens_can_be_gnu_attribute_p
2221 static bool cp_next_tokens_can_be_std_attribute_p
2223 static bool cp_nth_tokens_can_be_std_attribute_p
2224 (cp_parser
*, size_t);
2225 static bool cp_nth_tokens_can_be_gnu_attribute_p
2226 (cp_parser
*, size_t);
2227 static bool cp_nth_tokens_can_be_attribute_p
2228 (cp_parser
*, size_t);
2229 static tree cp_parser_attributes_opt
2231 static tree cp_parser_gnu_attributes_opt
2233 static tree cp_parser_gnu_attribute_list
2235 static tree cp_parser_std_attribute
2237 static tree cp_parser_std_attribute_spec
2239 static tree cp_parser_std_attribute_spec_seq
2241 static bool cp_parser_extension_opt
2242 (cp_parser
*, int *);
2243 static void cp_parser_label_declaration
2246 /* Transactional Memory Extensions */
2248 static tree cp_parser_transaction
2249 (cp_parser
*, enum rid
);
2250 static tree cp_parser_transaction_expression
2251 (cp_parser
*, enum rid
);
2252 static bool cp_parser_function_transaction
2253 (cp_parser
*, enum rid
);
2254 static tree cp_parser_transaction_cancel
2257 enum pragma_context
{
2264 static bool cp_parser_pragma
2265 (cp_parser
*, enum pragma_context
);
2267 /* Objective-C++ Productions */
2269 static tree cp_parser_objc_message_receiver
2271 static tree cp_parser_objc_message_args
2273 static tree cp_parser_objc_message_expression
2275 static tree cp_parser_objc_encode_expression
2277 static tree cp_parser_objc_defs_expression
2279 static tree cp_parser_objc_protocol_expression
2281 static tree cp_parser_objc_selector_expression
2283 static tree cp_parser_objc_expression
2285 static bool cp_parser_objc_selector_p
2287 static tree cp_parser_objc_selector
2289 static tree cp_parser_objc_protocol_refs_opt
2291 static void cp_parser_objc_declaration
2292 (cp_parser
*, tree
);
2293 static tree cp_parser_objc_statement
2295 static bool cp_parser_objc_valid_prefix_attributes
2296 (cp_parser
*, tree
*);
2297 static void cp_parser_objc_at_property_declaration
2299 static void cp_parser_objc_at_synthesize_declaration
2301 static void cp_parser_objc_at_dynamic_declaration
2303 static tree cp_parser_objc_struct_declaration
2306 /* Utility Routines */
2308 static tree cp_parser_lookup_name
2309 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2310 static tree cp_parser_lookup_name_simple
2311 (cp_parser
*, tree
, location_t
);
2312 static tree cp_parser_maybe_treat_template_as_class
2314 static bool cp_parser_check_declarator_template_parameters
2315 (cp_parser
*, cp_declarator
*, location_t
);
2316 static bool cp_parser_check_template_parameters
2317 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2318 static tree cp_parser_simple_cast_expression
2320 static tree cp_parser_global_scope_opt
2321 (cp_parser
*, bool);
2322 static bool cp_parser_constructor_declarator_p
2323 (cp_parser
*, bool);
2324 static tree cp_parser_function_definition_from_specifiers_and_declarator
2325 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2326 static tree cp_parser_function_definition_after_declarator
2327 (cp_parser
*, bool);
2328 static void cp_parser_template_declaration_after_export
2329 (cp_parser
*, bool);
2330 static void cp_parser_perform_template_parameter_access_checks
2331 (vec
<deferred_access_check
, va_gc
> *);
2332 static tree cp_parser_single_declaration
2333 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2334 static tree cp_parser_functional_cast
2335 (cp_parser
*, tree
);
2336 static tree cp_parser_save_member_function_body
2337 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2338 static tree cp_parser_save_nsdmi
2340 static tree cp_parser_enclosed_template_argument_list
2342 static void cp_parser_save_default_args
2343 (cp_parser
*, tree
);
2344 static void cp_parser_late_parsing_for_member
2345 (cp_parser
*, tree
);
2346 static tree cp_parser_late_parse_one_default_arg
2347 (cp_parser
*, tree
, tree
, tree
);
2348 static void cp_parser_late_parsing_nsdmi
2349 (cp_parser
*, tree
);
2350 static void cp_parser_late_parsing_default_args
2351 (cp_parser
*, tree
);
2352 static tree cp_parser_sizeof_operand
2353 (cp_parser
*, enum rid
);
2354 static tree cp_parser_trait_expr
2355 (cp_parser
*, enum rid
);
2356 static bool cp_parser_declares_only_class_p
2358 static void cp_parser_set_storage_class
2359 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2360 static void cp_parser_set_decl_spec_type
2361 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2362 static void set_and_check_decl_spec_loc
2363 (cp_decl_specifier_seq
*decl_specs
,
2364 cp_decl_spec ds
, cp_token
*);
2365 static bool cp_parser_friend_p
2366 (const cp_decl_specifier_seq
*);
2367 static void cp_parser_required_error
2368 (cp_parser
*, required_token
, bool);
2369 static cp_token
*cp_parser_require
2370 (cp_parser
*, enum cpp_ttype
, required_token
);
2371 static cp_token
*cp_parser_require_keyword
2372 (cp_parser
*, enum rid
, required_token
);
2373 static bool cp_parser_token_starts_function_definition_p
2375 static bool cp_parser_next_token_starts_class_definition_p
2377 static bool cp_parser_next_token_ends_template_argument_p
2379 static bool cp_parser_nth_token_starts_template_argument_list_p
2380 (cp_parser
*, size_t);
2381 static enum tag_types cp_parser_token_is_class_key
2383 static void cp_parser_check_class_key
2384 (enum tag_types
, tree type
);
2385 static void cp_parser_check_access_in_redeclaration
2386 (tree type
, location_t location
);
2387 static bool cp_parser_optional_template_keyword
2389 static void cp_parser_pre_parsed_nested_name_specifier
2391 static bool cp_parser_cache_group
2392 (cp_parser
*, enum cpp_ttype
, unsigned);
2393 static tree cp_parser_cache_defarg
2394 (cp_parser
*parser
, bool nsdmi
);
2395 static void cp_parser_parse_tentatively
2397 static void cp_parser_commit_to_tentative_parse
2399 static void cp_parser_commit_to_topmost_tentative_parse
2401 static void cp_parser_abort_tentative_parse
2403 static bool cp_parser_parse_definitely
2405 static inline bool cp_parser_parsing_tentatively
2407 static bool cp_parser_uncommitted_to_tentative_parse_p
2409 static void cp_parser_error
2410 (cp_parser
*, const char *);
2411 static void cp_parser_name_lookup_error
2412 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2413 static bool cp_parser_simulate_error
2415 static bool cp_parser_check_type_definition
2417 static void cp_parser_check_for_definition_in_return_type
2418 (cp_declarator
*, tree
, location_t type_location
);
2419 static void cp_parser_check_for_invalid_template_id
2420 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2421 static bool cp_parser_non_integral_constant_expression
2422 (cp_parser
*, non_integral_constant
);
2423 static void cp_parser_diagnose_invalid_type_name
2424 (cp_parser
*, tree
, tree
, location_t
);
2425 static bool cp_parser_parse_and_diagnose_invalid_type_name
2427 static int cp_parser_skip_to_closing_parenthesis
2428 (cp_parser
*, bool, bool, bool);
2429 static void cp_parser_skip_to_end_of_statement
2431 static void cp_parser_consume_semicolon_at_end_of_statement
2433 static void cp_parser_skip_to_end_of_block_or_statement
2435 static bool cp_parser_skip_to_closing_brace
2437 static void cp_parser_skip_to_end_of_template_parameter_list
2439 static void cp_parser_skip_to_pragma_eol
2440 (cp_parser
*, cp_token
*);
2441 static bool cp_parser_error_occurred
2443 static bool cp_parser_allow_gnu_extensions_p
2445 static bool cp_parser_is_pure_string_literal
2447 static bool cp_parser_is_string_literal
2449 static bool cp_parser_is_keyword
2450 (cp_token
*, enum rid
);
2451 static tree cp_parser_make_typename_type
2452 (cp_parser
*, tree
, tree
, location_t location
);
2453 static cp_declarator
* cp_parser_make_indirect_declarator
2454 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2456 /* Returns nonzero if we are parsing tentatively. */
2459 cp_parser_parsing_tentatively (cp_parser
* parser
)
2461 return parser
->context
->next
!= NULL
;
2464 /* Returns nonzero if TOKEN is a string literal. */
2467 cp_parser_is_pure_string_literal (cp_token
* token
)
2469 return (token
->type
== CPP_STRING
||
2470 token
->type
== CPP_STRING16
||
2471 token
->type
== CPP_STRING32
||
2472 token
->type
== CPP_WSTRING
||
2473 token
->type
== CPP_UTF8STRING
);
2476 /* Returns nonzero if TOKEN is a string literal
2477 of a user-defined string literal. */
2480 cp_parser_is_string_literal (cp_token
* token
)
2482 return (cp_parser_is_pure_string_literal (token
) ||
2483 token
->type
== CPP_STRING_USERDEF
||
2484 token
->type
== CPP_STRING16_USERDEF
||
2485 token
->type
== CPP_STRING32_USERDEF
||
2486 token
->type
== CPP_WSTRING_USERDEF
||
2487 token
->type
== CPP_UTF8STRING_USERDEF
);
2490 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2493 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2495 return token
->keyword
== keyword
;
2498 /* If not parsing tentatively, issue a diagnostic of the form
2499 FILE:LINE: MESSAGE before TOKEN
2500 where TOKEN is the next token in the input stream. MESSAGE
2501 (specified by the caller) is usually of the form "expected
2505 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2507 if (!cp_parser_simulate_error (parser
))
2509 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2510 /* This diagnostic makes more sense if it is tagged to the line
2511 of the token we just peeked at. */
2512 cp_lexer_set_source_position_from_token (token
);
2514 if (token
->type
== CPP_PRAGMA
)
2516 error_at (token
->location
,
2517 "%<#pragma%> is not allowed here");
2518 cp_parser_skip_to_pragma_eol (parser
, token
);
2522 c_parse_error (gmsgid
,
2523 /* Because c_parser_error does not understand
2524 CPP_KEYWORD, keywords are treated like
2526 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2527 token
->u
.value
, token
->flags
);
2531 /* Issue an error about name-lookup failing. NAME is the
2532 IDENTIFIER_NODE DECL is the result of
2533 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2534 the thing that we hoped to find. */
2537 cp_parser_name_lookup_error (cp_parser
* parser
,
2540 name_lookup_error desired
,
2541 location_t location
)
2543 /* If name lookup completely failed, tell the user that NAME was not
2545 if (decl
== error_mark_node
)
2547 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2548 error_at (location
, "%<%E::%E%> has not been declared",
2549 parser
->scope
, name
);
2550 else if (parser
->scope
== global_namespace
)
2551 error_at (location
, "%<::%E%> has not been declared", name
);
2552 else if (parser
->object_scope
2553 && !CLASS_TYPE_P (parser
->object_scope
))
2554 error_at (location
, "request for member %qE in non-class type %qT",
2555 name
, parser
->object_scope
);
2556 else if (parser
->object_scope
)
2557 error_at (location
, "%<%T::%E%> has not been declared",
2558 parser
->object_scope
, name
);
2560 error_at (location
, "%qE has not been declared", name
);
2562 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2567 error_at (location
, "%<%E::%E%> is not a type",
2568 parser
->scope
, name
);
2571 error_at (location
, "%<%E::%E%> is not a class or namespace",
2572 parser
->scope
, name
);
2576 "%<%E::%E%> is not a class, namespace, or enumeration",
2577 parser
->scope
, name
);
2584 else if (parser
->scope
== global_namespace
)
2589 error_at (location
, "%<::%E%> is not a type", name
);
2592 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2596 "%<::%E%> is not a class, namespace, or enumeration",
2608 error_at (location
, "%qE is not a type", name
);
2611 error_at (location
, "%qE is not a class or namespace", name
);
2615 "%qE is not a class, namespace, or enumeration", name
);
2623 /* If we are parsing tentatively, remember that an error has occurred
2624 during this tentative parse. Returns true if the error was
2625 simulated; false if a message should be issued by the caller. */
2628 cp_parser_simulate_error (cp_parser
* parser
)
2630 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2632 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2638 /* This function is called when a type is defined. If type
2639 definitions are forbidden at this point, an error message is
2643 cp_parser_check_type_definition (cp_parser
* parser
)
2645 /* If types are forbidden here, issue a message. */
2646 if (parser
->type_definition_forbidden_message
)
2648 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2649 in the message need to be interpreted. */
2650 error (parser
->type_definition_forbidden_message
);
2656 /* This function is called when the DECLARATOR is processed. The TYPE
2657 was a type defined in the decl-specifiers. If it is invalid to
2658 define a type in the decl-specifiers for DECLARATOR, an error is
2659 issued. TYPE_LOCATION is the location of TYPE and is used
2660 for error reporting. */
2663 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2664 tree type
, location_t type_location
)
2666 /* [dcl.fct] forbids type definitions in return types.
2667 Unfortunately, it's not easy to know whether or not we are
2668 processing a return type until after the fact. */
2670 && (declarator
->kind
== cdk_pointer
2671 || declarator
->kind
== cdk_reference
2672 || declarator
->kind
== cdk_ptrmem
))
2673 declarator
= declarator
->declarator
;
2675 && declarator
->kind
== cdk_function
)
2677 error_at (type_location
,
2678 "new types may not be defined in a return type");
2679 inform (type_location
,
2680 "(perhaps a semicolon is missing after the definition of %qT)",
2685 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2686 "<" in any valid C++ program. If the next token is indeed "<",
2687 issue a message warning the user about what appears to be an
2688 invalid attempt to form a template-id. LOCATION is the location
2689 of the type-specifier (TYPE) */
2692 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2694 enum tag_types tag_type
,
2695 location_t location
)
2697 cp_token_position start
= 0;
2699 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2702 error_at (location
, "%qT is not a template", type
);
2703 else if (identifier_p (type
))
2705 if (tag_type
!= none_type
)
2706 error_at (location
, "%qE is not a class template", type
);
2708 error_at (location
, "%qE is not a template", type
);
2711 error_at (location
, "invalid template-id");
2712 /* Remember the location of the invalid "<". */
2713 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2714 start
= cp_lexer_token_position (parser
->lexer
, true);
2715 /* Consume the "<". */
2716 cp_lexer_consume_token (parser
->lexer
);
2717 /* Parse the template arguments. */
2718 cp_parser_enclosed_template_argument_list (parser
);
2719 /* Permanently remove the invalid template arguments so that
2720 this error message is not issued again. */
2722 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2726 /* If parsing an integral constant-expression, issue an error message
2727 about the fact that THING appeared and return true. Otherwise,
2728 return false. In either case, set
2729 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2732 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2733 non_integral_constant thing
)
2735 parser
->non_integral_constant_expression_p
= true;
2736 if (parser
->integral_constant_expression_p
)
2738 if (!parser
->allow_non_integral_constant_expression_p
)
2740 const char *msg
= NULL
;
2744 error ("floating-point literal "
2745 "cannot appear in a constant-expression");
2748 error ("a cast to a type other than an integral or "
2749 "enumeration type cannot appear in a "
2750 "constant-expression");
2753 error ("%<typeid%> operator "
2754 "cannot appear in a constant-expression");
2757 error ("non-constant compound literals "
2758 "cannot appear in a constant-expression");
2761 error ("a function call "
2762 "cannot appear in a constant-expression");
2765 error ("an increment "
2766 "cannot appear in a constant-expression");
2769 error ("an decrement "
2770 "cannot appear in a constant-expression");
2773 error ("an array reference "
2774 "cannot appear in a constant-expression");
2776 case NIC_ADDR_LABEL
:
2777 error ("the address of a label "
2778 "cannot appear in a constant-expression");
2780 case NIC_OVERLOADED
:
2781 error ("calls to overloaded operators "
2782 "cannot appear in a constant-expression");
2784 case NIC_ASSIGNMENT
:
2785 error ("an assignment cannot appear in a constant-expression");
2788 error ("a comma operator "
2789 "cannot appear in a constant-expression");
2791 case NIC_CONSTRUCTOR
:
2792 error ("a call to a constructor "
2793 "cannot appear in a constant-expression");
2795 case NIC_TRANSACTION
:
2796 error ("a transaction expression "
2797 "cannot appear in a constant-expression");
2803 msg
= "__FUNCTION__";
2805 case NIC_PRETTY_FUNC
:
2806 msg
= "__PRETTY_FUNCTION__";
2826 case NIC_PREINCREMENT
:
2829 case NIC_PREDECREMENT
:
2842 error ("%qs cannot appear in a constant-expression", msg
);
2849 /* Emit a diagnostic for an invalid type name. SCOPE is the
2850 qualifying scope (or NULL, if none) for ID. This function commits
2851 to the current active tentative parse, if any. (Otherwise, the
2852 problematic construct might be encountered again later, resulting
2853 in duplicate error messages.) LOCATION is the location of ID. */
2856 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2857 tree scope
, tree id
,
2858 location_t location
)
2860 tree decl
, old_scope
;
2861 cp_parser_commit_to_tentative_parse (parser
);
2862 /* Try to lookup the identifier. */
2863 old_scope
= parser
->scope
;
2864 parser
->scope
= scope
;
2865 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2866 parser
->scope
= old_scope
;
2867 /* If the lookup found a template-name, it means that the user forgot
2868 to specify an argument list. Emit a useful error message. */
2869 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2871 "invalid use of template-name %qE without an argument list",
2873 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2874 error_at (location
, "invalid use of destructor %qD as a type", id
);
2875 else if (TREE_CODE (decl
) == TYPE_DECL
)
2876 /* Something like 'unsigned A a;' */
2877 error_at (location
, "invalid combination of multiple type-specifiers");
2878 else if (!parser
->scope
)
2880 /* Issue an error message. */
2881 error_at (location
, "%qE does not name a type", id
);
2882 /* If we're in a template class, it's possible that the user was
2883 referring to a type from a base class. For example:
2885 template <typename T> struct A { typedef T X; };
2886 template <typename T> struct B : public A<T> { X x; };
2888 The user should have said "typename A<T>::X". */
2889 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2890 inform (location
, "C++11 %<constexpr%> only available with "
2891 "-std=c++11 or -std=gnu++11");
2892 else if (processing_template_decl
&& current_class_type
2893 && TYPE_BINFO (current_class_type
))
2897 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2901 tree base_type
= BINFO_TYPE (b
);
2902 if (CLASS_TYPE_P (base_type
)
2903 && dependent_type_p (base_type
))
2906 /* Go from a particular instantiation of the
2907 template (which will have an empty TYPE_FIELDs),
2908 to the main version. */
2909 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2910 for (field
= TYPE_FIELDS (base_type
);
2912 field
= DECL_CHAIN (field
))
2913 if (TREE_CODE (field
) == TYPE_DECL
2914 && DECL_NAME (field
) == id
)
2917 "(perhaps %<typename %T::%E%> was intended)",
2918 BINFO_TYPE (b
), id
);
2927 /* Here we diagnose qualified-ids where the scope is actually correct,
2928 but the identifier does not resolve to a valid type name. */
2929 else if (parser
->scope
!= error_mark_node
)
2931 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2934 error_at (location_of (id
),
2935 "%qE in namespace %qE does not name a template type",
2938 error_at (location_of (id
),
2939 "%qE in namespace %qE does not name a type",
2942 else if (CLASS_TYPE_P (parser
->scope
)
2943 && constructor_name_p (id
, parser
->scope
))
2946 error_at (location
, "%<%T::%E%> names the constructor, not"
2947 " the type", parser
->scope
, id
);
2948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2949 error_at (location
, "and %qT has no template constructors",
2952 else if (TYPE_P (parser
->scope
)
2953 && dependent_scope_p (parser
->scope
))
2954 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2955 "%qT is a dependent scope",
2956 parser
->scope
, id
, parser
->scope
);
2957 else if (TYPE_P (parser
->scope
))
2959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2960 error_at (location_of (id
),
2961 "%qE in %q#T does not name a template type",
2964 error_at (location_of (id
),
2965 "%qE in %q#T does not name a type",
2973 /* Check for a common situation where a type-name should be present,
2974 but is not, and issue a sensible error message. Returns true if an
2975 invalid type-name was detected.
2977 The situation handled by this function are variable declarations of the
2978 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2979 Usually, `ID' should name a type, but if we got here it means that it
2980 does not. We try to emit the best possible error message depending on
2981 how exactly the id-expression looks like. */
2984 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2987 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2989 /* Avoid duplicate error about ambiguous lookup. */
2990 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2992 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2993 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2997 cp_parser_parse_tentatively (parser
);
2998 id
= cp_parser_id_expression (parser
,
2999 /*template_keyword_p=*/false,
3000 /*check_dependency_p=*/true,
3001 /*template_p=*/NULL
,
3002 /*declarator_p=*/true,
3003 /*optional_p=*/false);
3004 /* If the next token is a (, this is a function with no explicit return
3005 type, i.e. constructor, destructor or conversion op. */
3006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3007 || TREE_CODE (id
) == TYPE_DECL
)
3009 cp_parser_abort_tentative_parse (parser
);
3012 if (!cp_parser_parse_definitely (parser
))
3015 /* Emit a diagnostic for the invalid type. */
3016 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
3017 id
, token
->location
);
3019 /* If we aren't in the middle of a declarator (i.e. in a
3020 parameter-declaration-clause), skip to the end of the declaration;
3021 there's no point in trying to process it. */
3022 if (!parser
->in_declarator_p
)
3023 cp_parser_skip_to_end_of_block_or_statement (parser
);
3027 /* Consume tokens up to, and including, the next non-nested closing `)'.
3028 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3029 are doing error recovery. Returns -1 if OR_COMMA is true and we
3030 found an unnested comma. */
3033 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3038 unsigned paren_depth
= 0;
3039 unsigned brace_depth
= 0;
3040 unsigned square_depth
= 0;
3042 if (recovering
&& !or_comma
3043 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3048 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3050 switch (token
->type
)
3053 case CPP_PRAGMA_EOL
:
3054 /* If we've run out of tokens, then there is no closing `)'. */
3057 /* This is good for lambda expression capture-lists. */
3058 case CPP_OPEN_SQUARE
:
3061 case CPP_CLOSE_SQUARE
:
3062 if (!square_depth
--)
3067 /* This matches the processing in skip_to_end_of_statement. */
3072 case CPP_OPEN_BRACE
:
3075 case CPP_CLOSE_BRACE
:
3081 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3086 case CPP_OPEN_PAREN
:
3091 case CPP_CLOSE_PAREN
:
3092 if (!brace_depth
&& !paren_depth
--)
3095 cp_lexer_consume_token (parser
->lexer
);
3104 /* Consume the token. */
3105 cp_lexer_consume_token (parser
->lexer
);
3109 /* Consume tokens until we reach the end of the current statement.
3110 Normally, that will be just before consuming a `;'. However, if a
3111 non-nested `}' comes first, then we stop before consuming that. */
3114 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3116 unsigned nesting_depth
= 0;
3120 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3122 switch (token
->type
)
3125 case CPP_PRAGMA_EOL
:
3126 /* If we've run out of tokens, stop. */
3130 /* If the next token is a `;', we have reached the end of the
3136 case CPP_CLOSE_BRACE
:
3137 /* If this is a non-nested '}', stop before consuming it.
3138 That way, when confronted with something like:
3142 we stop before consuming the closing '}', even though we
3143 have not yet reached a `;'. */
3144 if (nesting_depth
== 0)
3147 /* If it is the closing '}' for a block that we have
3148 scanned, stop -- but only after consuming the token.
3154 we will stop after the body of the erroneously declared
3155 function, but before consuming the following `typedef'
3157 if (--nesting_depth
== 0)
3159 cp_lexer_consume_token (parser
->lexer
);
3163 case CPP_OPEN_BRACE
:
3171 /* Consume the token. */
3172 cp_lexer_consume_token (parser
->lexer
);
3176 /* This function is called at the end of a statement or declaration.
3177 If the next token is a semicolon, it is consumed; otherwise, error
3178 recovery is attempted. */
3181 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3183 /* Look for the trailing `;'. */
3184 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3186 /* If there is additional (erroneous) input, skip to the end of
3188 cp_parser_skip_to_end_of_statement (parser
);
3189 /* If the next token is now a `;', consume it. */
3190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3191 cp_lexer_consume_token (parser
->lexer
);
3195 /* Skip tokens until we have consumed an entire block, or until we
3196 have consumed a non-nested `;'. */
3199 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3201 int nesting_depth
= 0;
3203 while (nesting_depth
>= 0)
3205 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3207 switch (token
->type
)
3210 case CPP_PRAGMA_EOL
:
3211 /* If we've run out of tokens, stop. */
3215 /* Stop if this is an unnested ';'. */
3220 case CPP_CLOSE_BRACE
:
3221 /* Stop if this is an unnested '}', or closes the outermost
3224 if (nesting_depth
< 0)
3230 case CPP_OPEN_BRACE
:
3239 /* Consume the token. */
3240 cp_lexer_consume_token (parser
->lexer
);
3244 /* Skip tokens until a non-nested closing curly brace is the next
3245 token, or there are no more tokens. Return true in the first case,
3249 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3251 unsigned nesting_depth
= 0;
3255 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3257 switch (token
->type
)
3260 case CPP_PRAGMA_EOL
:
3261 /* If we've run out of tokens, stop. */
3264 case CPP_CLOSE_BRACE
:
3265 /* If the next token is a non-nested `}', then we have reached
3266 the end of the current block. */
3267 if (nesting_depth
-- == 0)
3271 case CPP_OPEN_BRACE
:
3272 /* If it the next token is a `{', then we are entering a new
3273 block. Consume the entire block. */
3281 /* Consume the token. */
3282 cp_lexer_consume_token (parser
->lexer
);
3286 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3287 parameter is the PRAGMA token, allowing us to purge the entire pragma
3291 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3295 parser
->lexer
->in_pragma
= false;
3298 token
= cp_lexer_consume_token (parser
->lexer
);
3299 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3301 /* Ensure that the pragma is not parsed again. */
3302 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3305 /* Require pragma end of line, resyncing with it as necessary. The
3306 arguments are as for cp_parser_skip_to_pragma_eol. */
3309 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3311 parser
->lexer
->in_pragma
= false;
3312 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3313 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3316 /* This is a simple wrapper around make_typename_type. When the id is
3317 an unresolved identifier node, we can provide a superior diagnostic
3318 using cp_parser_diagnose_invalid_type_name. */
3321 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3322 tree id
, location_t id_location
)
3325 if (identifier_p (id
))
3327 result
= make_typename_type (scope
, id
, typename_type
,
3328 /*complain=*/tf_none
);
3329 if (result
== error_mark_node
)
3330 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3333 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3336 /* This is a wrapper around the
3337 make_{pointer,ptrmem,reference}_declarator functions that decides
3338 which one to call based on the CODE and CLASS_TYPE arguments. The
3339 CODE argument should be one of the values returned by
3340 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3341 appertain to the pointer or reference. */
3343 static cp_declarator
*
3344 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3345 cp_cv_quals cv_qualifiers
,
3346 cp_declarator
*target
,
3349 if (code
== ERROR_MARK
)
3350 return cp_error_declarator
;
3352 if (code
== INDIRECT_REF
)
3353 if (class_type
== NULL_TREE
)
3354 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3356 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3357 target
, attributes
);
3358 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3359 return make_reference_declarator (cv_qualifiers
, target
,
3361 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3362 return make_reference_declarator (cv_qualifiers
, target
,
3367 /* Create a new C++ parser. */
3370 cp_parser_new (void)
3376 /* cp_lexer_new_main is called before doing GC allocation because
3377 cp_lexer_new_main might load a PCH file. */
3378 lexer
= cp_lexer_new_main ();
3380 /* Initialize the binops_by_token so that we can get the tree
3381 directly from the token. */
3382 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3383 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3385 parser
= ggc_alloc_cleared_cp_parser ();
3386 parser
->lexer
= lexer
;
3387 parser
->context
= cp_parser_context_new (NULL
);
3389 /* For now, we always accept GNU extensions. */
3390 parser
->allow_gnu_extensions_p
= 1;
3392 /* The `>' token is a greater-than operator, not the end of a
3394 parser
->greater_than_is_operator_p
= true;
3396 parser
->default_arg_ok_p
= true;
3398 /* We are not parsing a constant-expression. */
3399 parser
->integral_constant_expression_p
= false;
3400 parser
->allow_non_integral_constant_expression_p
= false;
3401 parser
->non_integral_constant_expression_p
= false;
3403 /* Local variable names are not forbidden. */
3404 parser
->local_variables_forbidden_p
= false;
3406 /* We are not processing an `extern "C"' declaration. */
3407 parser
->in_unbraced_linkage_specification_p
= false;
3409 /* We are not processing a declarator. */
3410 parser
->in_declarator_p
= false;
3412 /* We are not processing a template-argument-list. */
3413 parser
->in_template_argument_list_p
= false;
3415 /* We are not in an iteration statement. */
3416 parser
->in_statement
= 0;
3418 /* We are not in a switch statement. */
3419 parser
->in_switch_statement_p
= false;
3421 /* We are not parsing a type-id inside an expression. */
3422 parser
->in_type_id_in_expr_p
= false;
3424 /* Declarations aren't implicitly extern "C". */
3425 parser
->implicit_extern_c
= false;
3427 /* String literals should be translated to the execution character set. */
3428 parser
->translate_strings_p
= true;
3430 /* We are not parsing a function body. */
3431 parser
->in_function_body
= false;
3433 /* We can correct until told otherwise. */
3434 parser
->colon_corrects_to_scope_p
= true;
3436 /* The unparsed function queue is empty. */
3437 push_unparsed_function_queues (parser
);
3439 /* There are no classes being defined. */
3440 parser
->num_classes_being_defined
= 0;
3442 /* No template parameters apply. */
3443 parser
->num_template_parameter_lists
= 0;
3445 /* Not declaring an implicit function template. */
3446 parser
->fully_implicit_function_template_p
= false;
3451 /* Create a cp_lexer structure which will emit the tokens in CACHE
3452 and push it onto the parser's lexer stack. This is used for delayed
3453 parsing of in-class method bodies and default arguments, and should
3454 not be confused with tentative parsing. */
3456 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3458 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3459 lexer
->next
= parser
->lexer
;
3460 parser
->lexer
= lexer
;
3462 /* Move the current source position to that of the first token in the
3464 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3467 /* Pop the top lexer off the parser stack. This is never used for the
3468 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3470 cp_parser_pop_lexer (cp_parser
*parser
)
3472 cp_lexer
*lexer
= parser
->lexer
;
3473 parser
->lexer
= lexer
->next
;
3474 cp_lexer_destroy (lexer
);
3476 /* Put the current source position back where it was before this
3477 lexer was pushed. */
3478 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3481 /* Lexical conventions [gram.lex] */
3483 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3487 cp_parser_identifier (cp_parser
* parser
)
3491 /* Look for the identifier. */
3492 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3493 /* Return the value. */
3494 return token
? token
->u
.value
: error_mark_node
;
3497 /* Parse a sequence of adjacent string constants. Returns a
3498 TREE_STRING representing the combined, nul-terminated string
3499 constant. If TRANSLATE is true, translate the string to the
3500 execution character set. If WIDE_OK is true, a wide string is
3503 C++98 [lex.string] says that if a narrow string literal token is
3504 adjacent to a wide string literal token, the behavior is undefined.
3505 However, C99 6.4.5p4 says that this results in a wide string literal.
3506 We follow C99 here, for consistency with the C front end.
3508 This code is largely lifted from lex_string() in c-lex.c.
3510 FUTURE: ObjC++ will need to handle @-strings here. */
3512 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3516 struct obstack str_ob
;
3517 cpp_string str
, istr
, *strs
;
3519 enum cpp_ttype type
, curr_type
;
3520 int have_suffix_p
= 0;
3522 tree suffix_id
= NULL_TREE
;
3523 bool curr_tok_is_userdef_p
= false;
3525 tok
= cp_lexer_peek_token (parser
->lexer
);
3526 if (!cp_parser_is_string_literal (tok
))
3528 cp_parser_error (parser
, "expected string-literal");
3529 return error_mark_node
;
3532 if (cpp_userdef_string_p (tok
->type
))
3534 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3535 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3536 curr_tok_is_userdef_p
= true;
3540 string_tree
= tok
->u
.value
;
3541 curr_type
= tok
->type
;
3545 /* Try to avoid the overhead of creating and destroying an obstack
3546 for the common case of just one string. */
3547 if (!cp_parser_is_string_literal
3548 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3550 cp_lexer_consume_token (parser
->lexer
);
3552 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3553 str
.len
= TREE_STRING_LENGTH (string_tree
);
3556 if (curr_tok_is_userdef_p
)
3558 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3560 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3563 curr_type
= tok
->type
;
3569 gcc_obstack_init (&str_ob
);
3574 cp_lexer_consume_token (parser
->lexer
);
3576 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3577 str
.len
= TREE_STRING_LENGTH (string_tree
);
3579 if (curr_tok_is_userdef_p
)
3581 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3582 if (have_suffix_p
== 0)
3584 suffix_id
= curr_suffix_id
;
3587 else if (have_suffix_p
== 1
3588 && curr_suffix_id
!= suffix_id
)
3590 error ("inconsistent user-defined literal suffixes"
3591 " %qD and %qD in string literal",
3592 suffix_id
, curr_suffix_id
);
3595 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3598 curr_type
= tok
->type
;
3600 if (type
!= curr_type
)
3602 if (type
== CPP_STRING
)
3604 else if (curr_type
!= CPP_STRING
)
3605 error_at (tok
->location
,
3606 "unsupported non-standard concatenation "
3607 "of string literals");
3610 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3612 tok
= cp_lexer_peek_token (parser
->lexer
);
3613 if (cpp_userdef_string_p (tok
->type
))
3615 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3616 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3617 curr_tok_is_userdef_p
= true;
3621 string_tree
= tok
->u
.value
;
3622 curr_type
= tok
->type
;
3623 curr_tok_is_userdef_p
= false;
3626 while (cp_parser_is_string_literal (tok
));
3628 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3631 if (type
!= CPP_STRING
&& !wide_ok
)
3633 cp_parser_error (parser
, "a wide string is invalid in this context");
3637 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3638 (parse_in
, strs
, count
, &istr
, type
))
3640 value
= build_string (istr
.len
, (const char *)istr
.text
);
3641 free (CONST_CAST (unsigned char *, istr
.text
));
3647 case CPP_UTF8STRING
:
3648 TREE_TYPE (value
) = char_array_type_node
;
3651 TREE_TYPE (value
) = char16_array_type_node
;
3654 TREE_TYPE (value
) = char32_array_type_node
;
3657 TREE_TYPE (value
) = wchar_array_type_node
;
3661 value
= fix_string_type (value
);
3665 tree literal
= build_userdef_literal (suffix_id
, value
,
3666 OT_NONE
, NULL_TREE
);
3667 tok
->u
.value
= literal
;
3668 return cp_parser_userdef_string_literal (tok
);
3672 /* cpp_interpret_string has issued an error. */
3673 value
= error_mark_node
;
3676 obstack_free (&str_ob
, 0);
3681 /* Look up a literal operator with the name and the exact arguments. */
3684 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3687 decl
= lookup_name (name
);
3688 if (!decl
|| !is_overloaded_fn (decl
))
3689 return error_mark_node
;
3691 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3695 tree fn
= OVL_CURRENT (fns
);
3696 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3697 if (parmtypes
!= NULL_TREE
)
3699 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3700 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3702 tree tparm
= TREE_VALUE (parmtypes
);
3703 tree targ
= TREE_TYPE ((*args
)[ix
]);
3704 bool ptr
= TYPE_PTR_P (tparm
);
3705 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3706 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3708 || !same_type_p (TREE_TYPE (tparm
),
3713 && ix
== vec_safe_length (args
)
3714 /* May be this should be sufficient_parms_p instead,
3715 depending on how exactly should user-defined literals
3716 work in presence of default arguments on the literal
3717 operator parameters. */
3718 && parmtypes
== void_list_node
)
3723 return error_mark_node
;
3726 /* Parse a user-defined char constant. Returns a call to a user-defined
3727 literal operator taking the character as an argument. */
3730 cp_parser_userdef_char_literal (cp_parser
*parser
)
3732 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3733 tree literal
= token
->u
.value
;
3734 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3735 tree value
= USERDEF_LITERAL_VALUE (literal
);
3736 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3739 /* Build up a call to the user-defined operator */
3740 /* Lookup the name we got back from the id-expression. */
3741 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3742 vec_safe_push (args
, value
);
3743 decl
= lookup_literal_operator (name
, args
);
3744 if (!decl
|| decl
== error_mark_node
)
3746 error ("unable to find character literal operator %qD with %qT argument",
3747 name
, TREE_TYPE (value
));
3748 release_tree_vector (args
);
3749 return error_mark_node
;
3751 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3752 release_tree_vector (args
);
3753 if (result
!= error_mark_node
)
3756 error ("unable to find character literal operator %qD with %qT argument",
3757 name
, TREE_TYPE (value
));
3758 return error_mark_node
;
3761 /* A subroutine of cp_parser_userdef_numeric_literal to
3762 create a char... template parameter pack from a string node. */
3765 make_char_string_pack (tree value
)
3768 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3769 const char *str
= TREE_STRING_POINTER (value
);
3770 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3771 tree argvec
= make_tree_vec (1);
3773 /* Fill in CHARVEC with all of the parameters. */
3774 charvec
= make_tree_vec (len
);
3775 for (i
= 0; i
< len
; ++i
)
3776 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3778 /* Build the argument packs. */
3779 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3780 TREE_TYPE (argpack
) = char_type_node
;
3782 TREE_VEC_ELT (argvec
, 0) = argpack
;
3787 /* A subroutine of cp_parser_userdef_numeric_literal to
3788 create a char... template parameter pack from a string node. */
3791 make_string_pack (tree value
)
3794 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3795 const char *str
= TREE_STRING_POINTER (value
);
3796 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3797 tree argvec
= make_tree_vec (2);
3799 tree string_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3801 /* First template parm is character type. */
3802 TREE_VEC_ELT (argvec
, 0) = string_char_type_node
;
3804 /* Fill in CHARVEC with all of the parameters. */
3805 charvec
= make_tree_vec (len
);
3806 for (i
= 0; i
< len
; ++i
)
3807 TREE_VEC_ELT (charvec
, i
) = build_int_cst (string_char_type_node
, str
[i
]);
3809 /* Build the argument packs. */
3810 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3811 TREE_TYPE (argpack
) = string_char_type_node
;
3813 TREE_VEC_ELT (argvec
, 1) = argpack
;
3818 /* Parse a user-defined numeric constant. returns a call to a user-defined
3819 literal operator. */
3822 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3824 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3825 tree literal
= token
->u
.value
;
3826 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3827 tree value
= USERDEF_LITERAL_VALUE (literal
);
3828 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3829 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3830 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3832 vec
<tree
, va_gc
> *args
;
3834 /* Look for a literal operator taking the exact type of numeric argument
3835 as the literal value. */
3836 args
= make_tree_vector ();
3837 vec_safe_push (args
, value
);
3838 decl
= lookup_literal_operator (name
, args
);
3839 if (decl
&& decl
!= error_mark_node
)
3841 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3842 if (result
!= error_mark_node
)
3844 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3845 warning_at (token
->location
, OPT_Woverflow
,
3846 "integer literal exceeds range of %qT type",
3847 long_long_unsigned_type_node
);
3851 warning_at (token
->location
, OPT_Woverflow
,
3852 "floating literal exceeds range of %qT type",
3853 long_double_type_node
);
3854 else if (overflow
< 0)
3855 warning_at (token
->location
, OPT_Woverflow
,
3856 "floating literal truncated to zero");
3858 release_tree_vector (args
);
3862 release_tree_vector (args
);
3864 /* If the numeric argument didn't work, look for a raw literal
3865 operator taking a const char* argument consisting of the number
3866 in string format. */
3867 args
= make_tree_vector ();
3868 vec_safe_push (args
, num_string
);
3869 decl
= lookup_literal_operator (name
, args
);
3870 if (decl
&& decl
!= error_mark_node
)
3872 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3873 if (result
!= error_mark_node
)
3875 release_tree_vector (args
);
3879 release_tree_vector (args
);
3881 /* If the raw literal didn't work, look for a non-type template
3882 function with parameter pack char.... Call the function with
3883 template parameter characters representing the number. */
3884 args
= make_tree_vector ();
3885 decl
= lookup_literal_operator (name
, args
);
3886 if (decl
&& decl
!= error_mark_node
)
3888 tree tmpl_args
= make_char_string_pack (num_string
);
3889 decl
= lookup_template_function (decl
, tmpl_args
);
3890 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3891 if (result
!= error_mark_node
)
3893 release_tree_vector (args
);
3897 release_tree_vector (args
);
3899 error ("unable to find numeric literal operator %qD", name
);
3900 return error_mark_node
;
3903 /* Parse a user-defined string constant. Returns a call to a user-defined
3904 literal operator taking a character pointer and the length of the string
3908 cp_parser_userdef_string_literal (cp_token
*token
)
3910 tree literal
= token
->u
.value
;
3911 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3912 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3913 tree value
= USERDEF_LITERAL_VALUE (literal
);
3914 int len
= TREE_STRING_LENGTH (value
)
3915 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
3917 vec
<tree
, va_gc
> *args
;
3919 /* Look for a template function with typename parameter CharT
3920 and parameter pack CharT... Call the function with
3921 template parameter characters representing the string. */
3922 args
= make_tree_vector ();
3923 decl
= lookup_literal_operator (name
, args
);
3924 if (decl
&& decl
!= error_mark_node
)
3926 tree tmpl_args
= make_string_pack (value
);
3927 decl
= lookup_template_function (decl
, tmpl_args
);
3928 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3929 if (result
!= error_mark_node
)
3931 release_tree_vector (args
);
3935 release_tree_vector (args
);
3937 /* Build up a call to the user-defined operator */
3938 /* Lookup the name we got back from the id-expression. */
3939 args
= make_tree_vector ();
3940 vec_safe_push (args
, value
);
3941 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
3942 decl
= lookup_name (name
);
3943 if (!decl
|| decl
== error_mark_node
)
3945 error ("unable to find string literal operator %qD", name
);
3946 release_tree_vector (args
);
3947 return error_mark_node
;
3949 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3950 release_tree_vector (args
);
3951 if (result
!= error_mark_node
)
3954 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3955 name
, TREE_TYPE (value
), size_type_node
);
3956 return error_mark_node
;
3960 /* Basic concepts [gram.basic] */
3962 /* Parse a translation-unit.
3965 declaration-seq [opt]
3967 Returns TRUE if all went well. */
3970 cp_parser_translation_unit (cp_parser
* parser
)
3972 /* The address of the first non-permanent object on the declarator
3974 static void *declarator_obstack_base
;
3978 /* Create the declarator obstack, if necessary. */
3979 if (!cp_error_declarator
)
3981 gcc_obstack_init (&declarator_obstack
);
3982 /* Create the error declarator. */
3983 cp_error_declarator
= make_declarator (cdk_error
);
3984 /* Create the empty parameter list. */
3985 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3986 /* Remember where the base of the declarator obstack lies. */
3987 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3990 cp_parser_declaration_seq_opt (parser
);
3992 /* If there are no tokens left then all went well. */
3993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3995 /* Get rid of the token array; we don't need it any more. */
3996 cp_lexer_destroy (parser
->lexer
);
3997 parser
->lexer
= NULL
;
3999 /* This file might have been a context that's implicitly extern
4000 "C". If so, pop the lang context. (Only relevant for PCH.) */
4001 if (parser
->implicit_extern_c
)
4003 pop_lang_context ();
4004 parser
->implicit_extern_c
= false;
4008 finish_translation_unit ();
4014 cp_parser_error (parser
, "expected declaration");
4018 /* Make sure the declarator obstack was fully cleaned up. */
4019 gcc_assert (obstack_next_free (&declarator_obstack
)
4020 == declarator_obstack_base
);
4022 /* All went well. */
4026 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4027 decltype context. */
4029 static inline tsubst_flags_t
4030 complain_flags (bool decltype_p
)
4032 tsubst_flags_t complain
= tf_warning_or_error
;
4034 complain
|= tf_decltype
;
4038 /* Expressions [gram.expr] */
4040 /* Parse a primary-expression.
4051 ( compound-statement )
4052 __builtin_va_arg ( assignment-expression , type-id )
4053 __builtin_offsetof ( type-id , offsetof-expression )
4056 __has_nothrow_assign ( type-id )
4057 __has_nothrow_constructor ( type-id )
4058 __has_nothrow_copy ( type-id )
4059 __has_trivial_assign ( type-id )
4060 __has_trivial_constructor ( type-id )
4061 __has_trivial_copy ( type-id )
4062 __has_trivial_destructor ( type-id )
4063 __has_virtual_destructor ( type-id )
4064 __is_abstract ( type-id )
4065 __is_base_of ( type-id , type-id )
4066 __is_class ( type-id )
4067 __is_convertible_to ( type-id , type-id )
4068 __is_empty ( type-id )
4069 __is_enum ( type-id )
4070 __is_final ( type-id )
4071 __is_literal_type ( type-id )
4072 __is_pod ( type-id )
4073 __is_polymorphic ( type-id )
4074 __is_std_layout ( type-id )
4075 __is_trivial ( type-id )
4076 __is_union ( type-id )
4078 Objective-C++ Extension:
4086 ADDRESS_P is true iff this expression was immediately preceded by
4087 "&" and therefore might denote a pointer-to-member. CAST_P is true
4088 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4089 true iff this expression is a template argument.
4091 Returns a representation of the expression. Upon return, *IDK
4092 indicates what kind of id-expression (if any) was present. */
4095 cp_parser_primary_expression (cp_parser
*parser
,
4098 bool template_arg_p
,
4102 cp_token
*token
= NULL
;
4104 /* Assume the primary expression is not an id-expression. */
4105 *idk
= CP_ID_KIND_NONE
;
4107 /* Peek at the next token. */
4108 token
= cp_lexer_peek_token (parser
->lexer
);
4109 switch (token
->type
)
4118 user-defined-literal */
4124 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4125 return cp_parser_userdef_numeric_literal (parser
);
4126 token
= cp_lexer_consume_token (parser
->lexer
);
4127 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4129 error_at (token
->location
,
4130 "fixed-point types not supported in C++");
4131 return error_mark_node
;
4133 /* Floating-point literals are only allowed in an integral
4134 constant expression if they are cast to an integral or
4135 enumeration type. */
4136 if (TREE_CODE (token
->u
.value
) == REAL_CST
4137 && parser
->integral_constant_expression_p
4140 /* CAST_P will be set even in invalid code like "int(2.7 +
4141 ...)". Therefore, we have to check that the next token
4142 is sure to end the cast. */
4145 cp_token
*next_token
;
4147 next_token
= cp_lexer_peek_token (parser
->lexer
);
4148 if (/* The comma at the end of an
4149 enumerator-definition. */
4150 next_token
->type
!= CPP_COMMA
4151 /* The curly brace at the end of an enum-specifier. */
4152 && next_token
->type
!= CPP_CLOSE_BRACE
4153 /* The end of a statement. */
4154 && next_token
->type
!= CPP_SEMICOLON
4155 /* The end of the cast-expression. */
4156 && next_token
->type
!= CPP_CLOSE_PAREN
4157 /* The end of an array bound. */
4158 && next_token
->type
!= CPP_CLOSE_SQUARE
4159 /* The closing ">" in a template-argument-list. */
4160 && (next_token
->type
!= CPP_GREATER
4161 || parser
->greater_than_is_operator_p
)
4162 /* C++0x only: A ">>" treated like two ">" tokens,
4163 in a template-argument-list. */
4164 && (next_token
->type
!= CPP_RSHIFT
4165 || (cxx_dialect
== cxx98
)
4166 || parser
->greater_than_is_operator_p
))
4170 /* If we are within a cast, then the constraint that the
4171 cast is to an integral or enumeration type will be
4172 checked at that point. If we are not within a cast, then
4173 this code is invalid. */
4175 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4177 return token
->u
.value
;
4179 case CPP_CHAR_USERDEF
:
4180 case CPP_CHAR16_USERDEF
:
4181 case CPP_CHAR32_USERDEF
:
4182 case CPP_WCHAR_USERDEF
:
4183 return cp_parser_userdef_char_literal (parser
);
4189 case CPP_UTF8STRING
:
4190 case CPP_STRING_USERDEF
:
4191 case CPP_STRING16_USERDEF
:
4192 case CPP_STRING32_USERDEF
:
4193 case CPP_WSTRING_USERDEF
:
4194 case CPP_UTF8STRING_USERDEF
:
4195 /* ??? Should wide strings be allowed when parser->translate_strings_p
4196 is false (i.e. in attributes)? If not, we can kill the third
4197 argument to cp_parser_string_literal. */
4198 return cp_parser_string_literal (parser
,
4199 parser
->translate_strings_p
,
4202 case CPP_OPEN_PAREN
:
4205 bool saved_greater_than_is_operator_p
;
4207 /* Consume the `('. */
4208 cp_lexer_consume_token (parser
->lexer
);
4209 /* Within a parenthesized expression, a `>' token is always
4210 the greater-than operator. */
4211 saved_greater_than_is_operator_p
4212 = parser
->greater_than_is_operator_p
;
4213 parser
->greater_than_is_operator_p
= true;
4214 /* If we see `( { ' then we are looking at the beginning of
4215 a GNU statement-expression. */
4216 if (cp_parser_allow_gnu_extensions_p (parser
)
4217 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
4219 /* Statement-expressions are not allowed by the standard. */
4220 pedwarn (token
->location
, OPT_Wpedantic
,
4221 "ISO C++ forbids braced-groups within expressions");
4223 /* And they're not allowed outside of a function-body; you
4224 cannot, for example, write:
4226 int i = ({ int j = 3; j + 1; });
4228 at class or namespace scope. */
4229 if (!parser
->in_function_body
4230 || parser
->in_template_argument_list_p
)
4232 error_at (token
->location
,
4233 "statement-expressions are not allowed outside "
4234 "functions nor in template-argument lists");
4235 cp_parser_skip_to_end_of_block_or_statement (parser
);
4236 expr
= error_mark_node
;
4240 /* Start the statement-expression. */
4241 expr
= begin_stmt_expr ();
4242 /* Parse the compound-statement. */
4243 cp_parser_compound_statement (parser
, expr
, false, false);
4245 expr
= finish_stmt_expr (expr
, false);
4250 /* Parse the parenthesized expression. */
4251 expr
= cp_parser_expression (parser
, cast_p
, decltype_p
, idk
);
4252 /* Let the front end know that this expression was
4253 enclosed in parentheses. This matters in case, for
4254 example, the expression is of the form `A::B', since
4255 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4257 expr
= finish_parenthesized_expr (expr
);
4258 /* DR 705: Wrapping an unqualified name in parentheses
4259 suppresses arg-dependent lookup. We want to pass back
4260 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4261 (c++/37862), but none of the others. */
4262 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4263 *idk
= CP_ID_KIND_NONE
;
4265 /* The `>' token might be the end of a template-id or
4266 template-parameter-list now. */
4267 parser
->greater_than_is_operator_p
4268 = saved_greater_than_is_operator_p
;
4269 /* Consume the `)'. */
4270 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4271 cp_parser_skip_to_end_of_statement (parser
);
4276 case CPP_OPEN_SQUARE
:
4277 if (c_dialect_objc ())
4278 /* We have an Objective-C++ message. */
4279 return cp_parser_objc_expression (parser
);
4281 tree lam
= cp_parser_lambda_expression (parser
);
4282 /* Don't warn about a failed tentative parse. */
4283 if (cp_parser_error_occurred (parser
))
4284 return error_mark_node
;
4285 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4289 case CPP_OBJC_STRING
:
4290 if (c_dialect_objc ())
4291 /* We have an Objective-C++ string literal. */
4292 return cp_parser_objc_expression (parser
);
4293 cp_parser_error (parser
, "expected primary-expression");
4294 return error_mark_node
;
4297 switch (token
->keyword
)
4299 /* These two are the boolean literals. */
4301 cp_lexer_consume_token (parser
->lexer
);
4302 return boolean_true_node
;
4304 cp_lexer_consume_token (parser
->lexer
);
4305 return boolean_false_node
;
4307 /* The `__null' literal. */
4309 cp_lexer_consume_token (parser
->lexer
);
4312 /* The `nullptr' literal. */
4314 cp_lexer_consume_token (parser
->lexer
);
4315 return nullptr_node
;
4317 /* Recognize the `this' keyword. */
4319 cp_lexer_consume_token (parser
->lexer
);
4320 if (parser
->local_variables_forbidden_p
)
4322 error_at (token
->location
,
4323 "%<this%> may not be used in this context");
4324 return error_mark_node
;
4326 /* Pointers cannot appear in constant-expressions. */
4327 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4328 return error_mark_node
;
4329 return finish_this_expr ();
4331 /* The `operator' keyword can be the beginning of an
4336 case RID_FUNCTION_NAME
:
4337 case RID_PRETTY_FUNCTION_NAME
:
4338 case RID_C99_FUNCTION_NAME
:
4340 non_integral_constant name
;
4342 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4343 __func__ are the names of variables -- but they are
4344 treated specially. Therefore, they are handled here,
4345 rather than relying on the generic id-expression logic
4346 below. Grammatically, these names are id-expressions.
4348 Consume the token. */
4349 token
= cp_lexer_consume_token (parser
->lexer
);
4351 switch (token
->keyword
)
4353 case RID_FUNCTION_NAME
:
4354 name
= NIC_FUNC_NAME
;
4356 case RID_PRETTY_FUNCTION_NAME
:
4357 name
= NIC_PRETTY_FUNC
;
4359 case RID_C99_FUNCTION_NAME
:
4360 name
= NIC_C99_FUNC
;
4366 if (cp_parser_non_integral_constant_expression (parser
, name
))
4367 return error_mark_node
;
4369 /* Look up the name. */
4370 return finish_fname (token
->u
.value
);
4377 source_location type_location
;
4379 /* The `__builtin_va_arg' construct is used to handle
4380 `va_arg'. Consume the `__builtin_va_arg' token. */
4381 cp_lexer_consume_token (parser
->lexer
);
4382 /* Look for the opening `('. */
4383 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4384 /* Now, parse the assignment-expression. */
4385 expression
= cp_parser_assignment_expression (parser
,
4386 /*cast_p=*/false, NULL
);
4387 /* Look for the `,'. */
4388 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4389 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4390 /* Parse the type-id. */
4391 type
= cp_parser_type_id (parser
);
4392 /* Look for the closing `)'. */
4393 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4394 /* Using `va_arg' in a constant-expression is not
4396 if (cp_parser_non_integral_constant_expression (parser
,
4398 return error_mark_node
;
4399 return build_x_va_arg (type_location
, expression
, type
);
4403 return cp_parser_builtin_offsetof (parser
);
4405 case RID_HAS_NOTHROW_ASSIGN
:
4406 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4407 case RID_HAS_NOTHROW_COPY
:
4408 case RID_HAS_TRIVIAL_ASSIGN
:
4409 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4410 case RID_HAS_TRIVIAL_COPY
:
4411 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4412 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4413 case RID_IS_ABSTRACT
:
4414 case RID_IS_BASE_OF
:
4416 case RID_IS_CONVERTIBLE_TO
:
4420 case RID_IS_LITERAL_TYPE
:
4422 case RID_IS_POLYMORPHIC
:
4423 case RID_IS_STD_LAYOUT
:
4424 case RID_IS_TRIVIAL
:
4426 return cp_parser_trait_expr (parser
, token
->keyword
);
4428 /* Objective-C++ expressions. */
4430 case RID_AT_PROTOCOL
:
4431 case RID_AT_SELECTOR
:
4432 return cp_parser_objc_expression (parser
);
4435 if (parser
->in_function_body
4436 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4439 error_at (token
->location
,
4440 "a template declaration cannot appear at block scope");
4441 cp_parser_skip_to_end_of_block_or_statement (parser
);
4442 return error_mark_node
;
4445 cp_parser_error (parser
, "expected primary-expression");
4446 return error_mark_node
;
4449 /* An id-expression can start with either an identifier, a
4450 `::' as the beginning of a qualified-id, or the "operator"
4454 case CPP_TEMPLATE_ID
:
4455 case CPP_NESTED_NAME_SPECIFIER
:
4459 const char *error_msg
;
4462 cp_token
*id_expr_token
;
4465 /* Parse the id-expression. */
4467 = cp_parser_id_expression (parser
,
4468 /*template_keyword_p=*/false,
4469 /*check_dependency_p=*/true,
4471 /*declarator_p=*/false,
4472 /*optional_p=*/false);
4473 if (id_expression
== error_mark_node
)
4474 return error_mark_node
;
4475 id_expr_token
= token
;
4476 token
= cp_lexer_peek_token (parser
->lexer
);
4477 done
= (token
->type
!= CPP_OPEN_SQUARE
4478 && token
->type
!= CPP_OPEN_PAREN
4479 && token
->type
!= CPP_DOT
4480 && token
->type
!= CPP_DEREF
4481 && token
->type
!= CPP_PLUS_PLUS
4482 && token
->type
!= CPP_MINUS_MINUS
);
4483 /* If we have a template-id, then no further lookup is
4484 required. If the template-id was for a template-class, we
4485 will sometimes have a TYPE_DECL at this point. */
4486 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4487 || TREE_CODE (id_expression
) == TYPE_DECL
)
4488 decl
= id_expression
;
4489 /* Look up the name. */
4492 tree ambiguous_decls
;
4494 /* If we already know that this lookup is ambiguous, then
4495 we've already issued an error message; there's no reason
4497 if (id_expr_token
->type
== CPP_NAME
4498 && id_expr_token
->ambiguous_p
)
4500 cp_parser_simulate_error (parser
);
4501 return error_mark_node
;
4504 decl
= cp_parser_lookup_name (parser
, id_expression
,
4507 /*is_namespace=*/false,
4508 /*check_dependency=*/true,
4510 id_expr_token
->location
);
4511 /* If the lookup was ambiguous, an error will already have
4513 if (ambiguous_decls
)
4514 return error_mark_node
;
4516 /* In Objective-C++, we may have an Objective-C 2.0
4517 dot-syntax for classes here. */
4518 if (c_dialect_objc ()
4519 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4520 && TREE_CODE (decl
) == TYPE_DECL
4521 && objc_is_class_name (decl
))
4524 cp_lexer_consume_token (parser
->lexer
);
4525 component
= cp_parser_identifier (parser
);
4526 if (component
== error_mark_node
)
4527 return error_mark_node
;
4529 return objc_build_class_component_ref (id_expression
, component
);
4532 /* In Objective-C++, an instance variable (ivar) may be preferred
4533 to whatever cp_parser_lookup_name() found. */
4534 decl
= objc_lookup_ivar (decl
, id_expression
);
4536 /* If name lookup gives us a SCOPE_REF, then the
4537 qualifying scope was dependent. */
4538 if (TREE_CODE (decl
) == SCOPE_REF
)
4540 /* At this point, we do not know if DECL is a valid
4541 integral constant expression. We assume that it is
4542 in fact such an expression, so that code like:
4544 template <int N> struct A {
4548 is accepted. At template-instantiation time, we
4549 will check that B<N>::i is actually a constant. */
4552 /* Check to see if DECL is a local variable in a context
4553 where that is forbidden. */
4554 if (parser
->local_variables_forbidden_p
4555 && local_variable_p (decl
))
4557 /* It might be that we only found DECL because we are
4558 trying to be generous with pre-ISO scoping rules.
4559 For example, consider:
4563 for (int i = 0; i < 10; ++i) {}
4564 extern void f(int j = i);
4567 Here, name look up will originally find the out
4568 of scope `i'. We need to issue a warning message,
4569 but then use the global `i'. */
4570 decl
= check_for_out_of_scope_variable (decl
);
4571 if (local_variable_p (decl
))
4573 error_at (id_expr_token
->location
,
4574 "local variable %qD may not appear in this context",
4576 return error_mark_node
;
4581 decl
= (finish_id_expression
4582 (id_expression
, decl
, parser
->scope
,
4584 parser
->integral_constant_expression_p
,
4585 parser
->allow_non_integral_constant_expression_p
,
4586 &parser
->non_integral_constant_expression_p
,
4587 template_p
, done
, address_p
,
4590 id_expr_token
->location
));
4592 cp_parser_error (parser
, error_msg
);
4596 /* Anything else is an error. */
4598 cp_parser_error (parser
, "expected primary-expression");
4599 return error_mark_node
;
4604 cp_parser_primary_expression (cp_parser
*parser
,
4607 bool template_arg_p
,
4610 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4611 /*decltype*/false, idk
);
4614 /* Parse an id-expression.
4621 :: [opt] nested-name-specifier template [opt] unqualified-id
4623 :: operator-function-id
4626 Return a representation of the unqualified portion of the
4627 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4628 a `::' or nested-name-specifier.
4630 Often, if the id-expression was a qualified-id, the caller will
4631 want to make a SCOPE_REF to represent the qualified-id. This
4632 function does not do this in order to avoid wastefully creating
4633 SCOPE_REFs when they are not required.
4635 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4638 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4639 uninstantiated templates.
4641 If *TEMPLATE_P is non-NULL, it is set to true iff the
4642 `template' keyword is used to explicitly indicate that the entity
4643 named is a template.
4645 If DECLARATOR_P is true, the id-expression is appearing as part of
4646 a declarator, rather than as part of an expression. */
4649 cp_parser_id_expression (cp_parser
*parser
,
4650 bool template_keyword_p
,
4651 bool check_dependency_p
,
4656 bool global_scope_p
;
4657 bool nested_name_specifier_p
;
4659 /* Assume the `template' keyword was not used. */
4661 *template_p
= template_keyword_p
;
4663 /* Look for the optional `::' operator. */
4665 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4667 /* Look for the optional nested-name-specifier. */
4668 nested_name_specifier_p
4669 = (cp_parser_nested_name_specifier_opt (parser
,
4670 /*typename_keyword_p=*/false,
4675 /* If there is a nested-name-specifier, then we are looking at
4676 the first qualified-id production. */
4677 if (nested_name_specifier_p
)
4680 tree saved_object_scope
;
4681 tree saved_qualifying_scope
;
4682 tree unqualified_id
;
4685 /* See if the next token is the `template' keyword. */
4687 template_p
= &is_template
;
4688 *template_p
= cp_parser_optional_template_keyword (parser
);
4689 /* Name lookup we do during the processing of the
4690 unqualified-id might obliterate SCOPE. */
4691 saved_scope
= parser
->scope
;
4692 saved_object_scope
= parser
->object_scope
;
4693 saved_qualifying_scope
= parser
->qualifying_scope
;
4694 /* Process the final unqualified-id. */
4695 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4698 /*optional_p=*/false);
4699 /* Restore the SAVED_SCOPE for our caller. */
4700 parser
->scope
= saved_scope
;
4701 parser
->object_scope
= saved_object_scope
;
4702 parser
->qualifying_scope
= saved_qualifying_scope
;
4704 return unqualified_id
;
4706 /* Otherwise, if we are in global scope, then we are looking at one
4707 of the other qualified-id productions. */
4708 else if (global_scope_p
)
4713 /* Peek at the next token. */
4714 token
= cp_lexer_peek_token (parser
->lexer
);
4716 /* If it's an identifier, and the next token is not a "<", then
4717 we can avoid the template-id case. This is an optimization
4718 for this common case. */
4719 if (token
->type
== CPP_NAME
4720 && !cp_parser_nth_token_starts_template_argument_list_p
4722 return cp_parser_identifier (parser
);
4724 cp_parser_parse_tentatively (parser
);
4725 /* Try a template-id. */
4726 id
= cp_parser_template_id (parser
,
4727 /*template_keyword_p=*/false,
4728 /*check_dependency_p=*/true,
4731 /* If that worked, we're done. */
4732 if (cp_parser_parse_definitely (parser
))
4735 /* Peek at the next token. (Changes in the token buffer may
4736 have invalidated the pointer obtained above.) */
4737 token
= cp_lexer_peek_token (parser
->lexer
);
4739 switch (token
->type
)
4742 return cp_parser_identifier (parser
);
4745 if (token
->keyword
== RID_OPERATOR
)
4746 return cp_parser_operator_function_id (parser
);
4750 cp_parser_error (parser
, "expected id-expression");
4751 return error_mark_node
;
4755 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4756 /*check_dependency_p=*/true,
4761 /* Parse an unqualified-id.
4765 operator-function-id
4766 conversion-function-id
4770 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4771 keyword, in a construct like `A::template ...'.
4773 Returns a representation of unqualified-id. For the `identifier'
4774 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4775 production a BIT_NOT_EXPR is returned; the operand of the
4776 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4777 other productions, see the documentation accompanying the
4778 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4779 names are looked up in uninstantiated templates. If DECLARATOR_P
4780 is true, the unqualified-id is appearing as part of a declarator,
4781 rather than as part of an expression. */
4784 cp_parser_unqualified_id (cp_parser
* parser
,
4785 bool template_keyword_p
,
4786 bool check_dependency_p
,
4792 /* Peek at the next token. */
4793 token
= cp_lexer_peek_token (parser
->lexer
);
4795 switch (token
->type
)
4801 /* We don't know yet whether or not this will be a
4803 cp_parser_parse_tentatively (parser
);
4804 /* Try a template-id. */
4805 id
= cp_parser_template_id (parser
, template_keyword_p
,
4809 /* If it worked, we're done. */
4810 if (cp_parser_parse_definitely (parser
))
4812 /* Otherwise, it's an ordinary identifier. */
4813 return cp_parser_identifier (parser
);
4816 case CPP_TEMPLATE_ID
:
4817 return cp_parser_template_id (parser
, template_keyword_p
,
4825 tree qualifying_scope
;
4830 /* Consume the `~' token. */
4831 cp_lexer_consume_token (parser
->lexer
);
4832 /* Parse the class-name. The standard, as written, seems to
4835 template <typename T> struct S { ~S (); };
4836 template <typename T> S<T>::~S() {}
4838 is invalid, since `~' must be followed by a class-name, but
4839 `S<T>' is dependent, and so not known to be a class.
4840 That's not right; we need to look in uninstantiated
4841 templates. A further complication arises from:
4843 template <typename T> void f(T t) {
4847 Here, it is not possible to look up `T' in the scope of `T'
4848 itself. We must look in both the current scope, and the
4849 scope of the containing complete expression.
4851 Yet another issue is:
4860 The standard does not seem to say that the `S' in `~S'
4861 should refer to the type `S' and not the data member
4864 /* DR 244 says that we look up the name after the "~" in the
4865 same scope as we looked up the qualifying name. That idea
4866 isn't fully worked out; it's more complicated than that. */
4867 scope
= parser
->scope
;
4868 object_scope
= parser
->object_scope
;
4869 qualifying_scope
= parser
->qualifying_scope
;
4871 /* Check for invalid scopes. */
4872 if (scope
== error_mark_node
)
4874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4875 cp_lexer_consume_token (parser
->lexer
);
4876 return error_mark_node
;
4878 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4880 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4881 error_at (token
->location
,
4882 "scope %qT before %<~%> is not a class-name",
4884 cp_parser_simulate_error (parser
);
4885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4886 cp_lexer_consume_token (parser
->lexer
);
4887 return error_mark_node
;
4889 gcc_assert (!scope
|| TYPE_P (scope
));
4891 /* If the name is of the form "X::~X" it's OK even if X is a
4893 token
= cp_lexer_peek_token (parser
->lexer
);
4895 && token
->type
== CPP_NAME
4896 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4898 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4899 || (CLASS_TYPE_P (scope
)
4900 && constructor_name_p (token
->u
.value
, scope
))))
4902 cp_lexer_consume_token (parser
->lexer
);
4903 return build_nt (BIT_NOT_EXPR
, scope
);
4906 /* ~auto means the destructor of whatever the object is. */
4907 if (cp_parser_is_keyword (token
, RID_AUTO
))
4909 if (cxx_dialect
< cxx1y
)
4910 pedwarn (input_location
, 0,
4911 "%<~auto%> only available with "
4912 "-std=c++1y or -std=gnu++1y");
4913 cp_lexer_consume_token (parser
->lexer
);
4914 return build_nt (BIT_NOT_EXPR
, make_auto ());
4917 /* If there was an explicit qualification (S::~T), first look
4918 in the scope given by the qualification (i.e., S).
4920 Note: in the calls to cp_parser_class_name below we pass
4921 typename_type so that lookup finds the injected-class-name
4922 rather than the constructor. */
4924 type_decl
= NULL_TREE
;
4927 cp_parser_parse_tentatively (parser
);
4928 type_decl
= cp_parser_class_name (parser
,
4929 /*typename_keyword_p=*/false,
4930 /*template_keyword_p=*/false,
4932 /*check_dependency=*/false,
4933 /*class_head_p=*/false,
4935 if (cp_parser_parse_definitely (parser
))
4938 /* In "N::S::~S", look in "N" as well. */
4939 if (!done
&& scope
&& qualifying_scope
)
4941 cp_parser_parse_tentatively (parser
);
4942 parser
->scope
= qualifying_scope
;
4943 parser
->object_scope
= NULL_TREE
;
4944 parser
->qualifying_scope
= NULL_TREE
;
4946 = cp_parser_class_name (parser
,
4947 /*typename_keyword_p=*/false,
4948 /*template_keyword_p=*/false,
4950 /*check_dependency=*/false,
4951 /*class_head_p=*/false,
4953 if (cp_parser_parse_definitely (parser
))
4956 /* In "p->S::~T", look in the scope given by "*p" as well. */
4957 else if (!done
&& object_scope
)
4959 cp_parser_parse_tentatively (parser
);
4960 parser
->scope
= object_scope
;
4961 parser
->object_scope
= NULL_TREE
;
4962 parser
->qualifying_scope
= NULL_TREE
;
4964 = cp_parser_class_name (parser
,
4965 /*typename_keyword_p=*/false,
4966 /*template_keyword_p=*/false,
4968 /*check_dependency=*/false,
4969 /*class_head_p=*/false,
4971 if (cp_parser_parse_definitely (parser
))
4974 /* Look in the surrounding context. */
4977 parser
->scope
= NULL_TREE
;
4978 parser
->object_scope
= NULL_TREE
;
4979 parser
->qualifying_scope
= NULL_TREE
;
4980 if (processing_template_decl
)
4981 cp_parser_parse_tentatively (parser
);
4983 = cp_parser_class_name (parser
,
4984 /*typename_keyword_p=*/false,
4985 /*template_keyword_p=*/false,
4987 /*check_dependency=*/false,
4988 /*class_head_p=*/false,
4990 if (processing_template_decl
4991 && ! cp_parser_parse_definitely (parser
))
4993 /* We couldn't find a type with this name, so just accept
4994 it and check for a match at instantiation time. */
4995 type_decl
= cp_parser_identifier (parser
);
4996 if (type_decl
!= error_mark_node
)
4997 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5001 /* If an error occurred, assume that the name of the
5002 destructor is the same as the name of the qualifying
5003 class. That allows us to keep parsing after running
5004 into ill-formed destructor names. */
5005 if (type_decl
== error_mark_node
&& scope
)
5006 return build_nt (BIT_NOT_EXPR
, scope
);
5007 else if (type_decl
== error_mark_node
)
5008 return error_mark_node
;
5010 /* Check that destructor name and scope match. */
5011 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5013 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5014 error_at (token
->location
,
5015 "declaration of %<~%T%> as member of %qT",
5017 cp_parser_simulate_error (parser
);
5018 return error_mark_node
;
5023 A typedef-name that names a class shall not be used as the
5024 identifier in the declarator for a destructor declaration. */
5026 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5027 && !DECL_SELF_REFERENCE_P (type_decl
)
5028 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5029 error_at (token
->location
,
5030 "typedef-name %qD used as destructor declarator",
5033 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5037 if (token
->keyword
== RID_OPERATOR
)
5041 /* This could be a template-id, so we try that first. */
5042 cp_parser_parse_tentatively (parser
);
5043 /* Try a template-id. */
5044 id
= cp_parser_template_id (parser
, template_keyword_p
,
5045 /*check_dependency_p=*/true,
5048 /* If that worked, we're done. */
5049 if (cp_parser_parse_definitely (parser
))
5051 /* We still don't know whether we're looking at an
5052 operator-function-id or a conversion-function-id. */
5053 cp_parser_parse_tentatively (parser
);
5054 /* Try an operator-function-id. */
5055 id
= cp_parser_operator_function_id (parser
);
5056 /* If that didn't work, try a conversion-function-id. */
5057 if (!cp_parser_parse_definitely (parser
))
5058 id
= cp_parser_conversion_function_id (parser
);
5059 else if (UDLIT_OPER_P (id
))
5062 const char *name
= UDLIT_OP_SUFFIX (id
);
5063 if (name
[0] != '_' && !in_system_header
&& declarator_p
)
5064 warning (0, "literal operator suffixes not preceded by %<_%>"
5065 " are reserved for future standardization");
5075 cp_parser_error (parser
, "expected unqualified-id");
5076 return error_mark_node
;
5080 /* Parse an (optional) nested-name-specifier.
5082 nested-name-specifier: [C++98]
5083 class-or-namespace-name :: nested-name-specifier [opt]
5084 class-or-namespace-name :: template nested-name-specifier [opt]
5086 nested-name-specifier: [C++0x]
5089 nested-name-specifier identifier ::
5090 nested-name-specifier template [opt] simple-template-id ::
5092 PARSER->SCOPE should be set appropriately before this function is
5093 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5094 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5097 Sets PARSER->SCOPE to the class (TYPE) or namespace
5098 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5099 it unchanged if there is no nested-name-specifier. Returns the new
5100 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5102 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5103 part of a declaration and/or decl-specifier. */
5106 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5107 bool typename_keyword_p
,
5108 bool check_dependency_p
,
5110 bool is_declaration
)
5112 bool success
= false;
5113 cp_token_position start
= 0;
5116 /* Remember where the nested-name-specifier starts. */
5117 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5119 start
= cp_lexer_token_position (parser
->lexer
, false);
5120 push_deferring_access_checks (dk_deferred
);
5127 tree saved_qualifying_scope
;
5128 bool template_keyword_p
;
5130 /* Spot cases that cannot be the beginning of a
5131 nested-name-specifier. */
5132 token
= cp_lexer_peek_token (parser
->lexer
);
5134 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5135 the already parsed nested-name-specifier. */
5136 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5138 /* Grab the nested-name-specifier and continue the loop. */
5139 cp_parser_pre_parsed_nested_name_specifier (parser
);
5140 /* If we originally encountered this nested-name-specifier
5141 with IS_DECLARATION set to false, we will not have
5142 resolved TYPENAME_TYPEs, so we must do so here. */
5144 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5146 new_scope
= resolve_typename_type (parser
->scope
,
5147 /*only_current_p=*/false);
5148 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5149 parser
->scope
= new_scope
;
5155 /* Spot cases that cannot be the beginning of a
5156 nested-name-specifier. On the second and subsequent times
5157 through the loop, we look for the `template' keyword. */
5158 if (success
&& token
->keyword
== RID_TEMPLATE
)
5160 /* A template-id can start a nested-name-specifier. */
5161 else if (token
->type
== CPP_TEMPLATE_ID
)
5163 /* DR 743: decltype can be used in a nested-name-specifier. */
5164 else if (token_is_decltype (token
))
5168 /* If the next token is not an identifier, then it is
5169 definitely not a type-name or namespace-name. */
5170 if (token
->type
!= CPP_NAME
)
5172 /* If the following token is neither a `<' (to begin a
5173 template-id), nor a `::', then we are not looking at a
5174 nested-name-specifier. */
5175 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5177 if (token
->type
== CPP_COLON
5178 && parser
->colon_corrects_to_scope_p
5179 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5181 error_at (token
->location
,
5182 "found %<:%> in nested-name-specifier, expected %<::%>");
5183 token
->type
= CPP_SCOPE
;
5186 if (token
->type
!= CPP_SCOPE
5187 && !cp_parser_nth_token_starts_template_argument_list_p
5192 /* The nested-name-specifier is optional, so we parse
5194 cp_parser_parse_tentatively (parser
);
5196 /* Look for the optional `template' keyword, if this isn't the
5197 first time through the loop. */
5199 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5201 template_keyword_p
= false;
5203 /* Save the old scope since the name lookup we are about to do
5204 might destroy it. */
5205 old_scope
= parser
->scope
;
5206 saved_qualifying_scope
= parser
->qualifying_scope
;
5207 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5208 look up names in "X<T>::I" in order to determine that "Y" is
5209 a template. So, if we have a typename at this point, we make
5210 an effort to look through it. */
5212 && !typename_keyword_p
5214 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5215 parser
->scope
= resolve_typename_type (parser
->scope
,
5216 /*only_current_p=*/false);
5217 /* Parse the qualifying entity. */
5219 = cp_parser_qualifying_entity (parser
,
5225 /* Look for the `::' token. */
5226 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5228 /* If we found what we wanted, we keep going; otherwise, we're
5230 if (!cp_parser_parse_definitely (parser
))
5232 bool error_p
= false;
5234 /* Restore the OLD_SCOPE since it was valid before the
5235 failed attempt at finding the last
5236 class-or-namespace-name. */
5237 parser
->scope
= old_scope
;
5238 parser
->qualifying_scope
= saved_qualifying_scope
;
5240 /* If the next token is a decltype, and the one after that is a
5241 `::', then the decltype has failed to resolve to a class or
5242 enumeration type. Give this error even when parsing
5243 tentatively since it can't possibly be valid--and we're going
5244 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5245 won't get another chance.*/
5246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5247 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5250 token
= cp_lexer_consume_token (parser
->lexer
);
5251 error_at (token
->location
, "decltype evaluates to %qT, "
5252 "which is not a class or enumeration type",
5254 parser
->scope
= error_mark_node
;
5258 cp_lexer_consume_token (parser
->lexer
);
5261 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5263 /* If the next token is an identifier, and the one after
5264 that is a `::', then any valid interpretation would have
5265 found a class-or-namespace-name. */
5266 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5267 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5269 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5272 token
= cp_lexer_consume_token (parser
->lexer
);
5275 if (!token
->ambiguous_p
)
5278 tree ambiguous_decls
;
5280 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5282 /*is_template=*/false,
5283 /*is_namespace=*/false,
5284 /*check_dependency=*/true,
5287 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5288 error_at (token
->location
,
5289 "%qD used without template parameters",
5291 else if (ambiguous_decls
)
5293 // cp_parser_lookup_name has the same diagnostic,
5294 // thus make sure to emit it at most once.
5295 if (cp_parser_uncommitted_to_tentative_parse_p
5298 error_at (token
->location
,
5299 "reference to %qD is ambiguous",
5301 print_candidates (ambiguous_decls
);
5303 decl
= error_mark_node
;
5307 if (cxx_dialect
!= cxx98
)
5308 cp_parser_name_lookup_error
5309 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5312 cp_parser_name_lookup_error
5313 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5317 parser
->scope
= error_mark_node
;
5319 /* Treat this as a successful nested-name-specifier
5324 If the name found is not a class-name (clause
5325 _class_) or namespace-name (_namespace.def_), the
5326 program is ill-formed. */
5329 cp_lexer_consume_token (parser
->lexer
);
5333 /* We've found one valid nested-name-specifier. */
5335 /* Name lookup always gives us a DECL. */
5336 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5337 new_scope
= TREE_TYPE (new_scope
);
5338 /* Uses of "template" must be followed by actual templates. */
5339 if (template_keyword_p
5340 && !(CLASS_TYPE_P (new_scope
)
5341 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5342 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5343 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5344 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5345 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5346 == TEMPLATE_ID_EXPR
)))
5347 permerror (input_location
, TYPE_P (new_scope
)
5348 ? G_("%qT is not a template")
5349 : G_("%qD is not a template"),
5351 /* If it is a class scope, try to complete it; we are about to
5352 be looking up names inside the class. */
5353 if (TYPE_P (new_scope
)
5354 /* Since checking types for dependency can be expensive,
5355 avoid doing it if the type is already complete. */
5356 && !COMPLETE_TYPE_P (new_scope
)
5357 /* Do not try to complete dependent types. */
5358 && !dependent_type_p (new_scope
))
5360 new_scope
= complete_type (new_scope
);
5361 /* If it is a typedef to current class, use the current
5362 class instead, as the typedef won't have any names inside
5364 if (!COMPLETE_TYPE_P (new_scope
)
5365 && currently_open_class (new_scope
))
5366 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5368 /* Make sure we look in the right scope the next time through
5370 parser
->scope
= new_scope
;
5373 /* If parsing tentatively, replace the sequence of tokens that makes
5374 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5375 token. That way, should we re-parse the token stream, we will
5376 not have to repeat the effort required to do the parse, nor will
5377 we issue duplicate error messages. */
5378 if (success
&& start
)
5382 token
= cp_lexer_token_at (parser
->lexer
, start
);
5383 /* Reset the contents of the START token. */
5384 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5385 /* Retrieve any deferred checks. Do not pop this access checks yet
5386 so the memory will not be reclaimed during token replacing below. */
5387 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
5388 token
->u
.tree_check_value
->value
= parser
->scope
;
5389 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5390 token
->u
.tree_check_value
->qualifying_scope
=
5391 parser
->qualifying_scope
;
5392 token
->keyword
= RID_MAX
;
5394 /* Purge all subsequent tokens. */
5395 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5399 pop_to_parent_deferring_access_checks ();
5401 return success
? parser
->scope
: NULL_TREE
;
5404 /* Parse a nested-name-specifier. See
5405 cp_parser_nested_name_specifier_opt for details. This function
5406 behaves identically, except that it will an issue an error if no
5407 nested-name-specifier is present. */
5410 cp_parser_nested_name_specifier (cp_parser
*parser
,
5411 bool typename_keyword_p
,
5412 bool check_dependency_p
,
5414 bool is_declaration
)
5418 /* Look for the nested-name-specifier. */
5419 scope
= cp_parser_nested_name_specifier_opt (parser
,
5424 /* If it was not present, issue an error message. */
5427 cp_parser_error (parser
, "expected nested-name-specifier");
5428 parser
->scope
= NULL_TREE
;
5434 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5435 this is either a class-name or a namespace-name (which corresponds
5436 to the class-or-namespace-name production in the grammar). For
5437 C++0x, it can also be a type-name that refers to an enumeration
5438 type or a simple-template-id.
5440 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5441 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5442 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5443 TYPE_P is TRUE iff the next name should be taken as a class-name,
5444 even the same name is declared to be another entity in the same
5447 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5448 specified by the class-or-namespace-name. If neither is found the
5449 ERROR_MARK_NODE is returned. */
5452 cp_parser_qualifying_entity (cp_parser
*parser
,
5453 bool typename_keyword_p
,
5454 bool template_keyword_p
,
5455 bool check_dependency_p
,
5457 bool is_declaration
)
5460 tree saved_qualifying_scope
;
5461 tree saved_object_scope
;
5464 bool successful_parse_p
;
5466 /* DR 743: decltype can appear in a nested-name-specifier. */
5467 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5469 scope
= cp_parser_decltype (parser
);
5470 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5471 && !MAYBE_CLASS_TYPE_P (scope
))
5473 cp_parser_simulate_error (parser
);
5474 return error_mark_node
;
5476 if (TYPE_NAME (scope
))
5477 scope
= TYPE_NAME (scope
);
5481 /* Before we try to parse the class-name, we must save away the
5482 current PARSER->SCOPE since cp_parser_class_name will destroy
5484 saved_scope
= parser
->scope
;
5485 saved_qualifying_scope
= parser
->qualifying_scope
;
5486 saved_object_scope
= parser
->object_scope
;
5487 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5488 there is no need to look for a namespace-name. */
5489 only_class_p
= template_keyword_p
5490 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5492 cp_parser_parse_tentatively (parser
);
5493 scope
= cp_parser_class_name (parser
,
5496 type_p
? class_type
: none_type
,
5498 /*class_head_p=*/false,
5500 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5501 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5503 && cxx_dialect
!= cxx98
5504 && !successful_parse_p
)
5506 /* Restore the saved scope. */
5507 parser
->scope
= saved_scope
;
5508 parser
->qualifying_scope
= saved_qualifying_scope
;
5509 parser
->object_scope
= saved_object_scope
;
5511 /* Parse tentatively. */
5512 cp_parser_parse_tentatively (parser
);
5514 /* Parse a type-name */
5515 scope
= cp_parser_type_name (parser
);
5517 /* "If the name found does not designate a namespace or a class,
5518 enumeration, or dependent type, the program is ill-formed."
5520 We cover classes and dependent types above and namespaces below,
5521 so this code is only looking for enums. */
5522 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5523 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5524 cp_parser_simulate_error (parser
);
5526 successful_parse_p
= cp_parser_parse_definitely (parser
);
5528 /* If that didn't work, try for a namespace-name. */
5529 if (!only_class_p
&& !successful_parse_p
)
5531 /* Restore the saved scope. */
5532 parser
->scope
= saved_scope
;
5533 parser
->qualifying_scope
= saved_qualifying_scope
;
5534 parser
->object_scope
= saved_object_scope
;
5535 /* If we are not looking at an identifier followed by the scope
5536 resolution operator, then this is not part of a
5537 nested-name-specifier. (Note that this function is only used
5538 to parse the components of a nested-name-specifier.) */
5539 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5540 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5541 return error_mark_node
;
5542 scope
= cp_parser_namespace_name (parser
);
5548 /* Parse a postfix-expression.
5552 postfix-expression [ expression ]
5553 postfix-expression ( expression-list [opt] )
5554 simple-type-specifier ( expression-list [opt] )
5555 typename :: [opt] nested-name-specifier identifier
5556 ( expression-list [opt] )
5557 typename :: [opt] nested-name-specifier template [opt] template-id
5558 ( expression-list [opt] )
5559 postfix-expression . template [opt] id-expression
5560 postfix-expression -> template [opt] id-expression
5561 postfix-expression . pseudo-destructor-name
5562 postfix-expression -> pseudo-destructor-name
5563 postfix-expression ++
5564 postfix-expression --
5565 dynamic_cast < type-id > ( expression )
5566 static_cast < type-id > ( expression )
5567 reinterpret_cast < type-id > ( expression )
5568 const_cast < type-id > ( expression )
5569 typeid ( expression )
5575 ( type-id ) { initializer-list , [opt] }
5577 This extension is a GNU version of the C99 compound-literal
5578 construct. (The C99 grammar uses `type-name' instead of `type-id',
5579 but they are essentially the same concept.)
5581 If ADDRESS_P is true, the postfix expression is the operand of the
5582 `&' operator. CAST_P is true if this expression is the target of a
5585 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5586 class member access expressions [expr.ref].
5588 Returns a representation of the expression. */
5591 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5592 bool member_access_only_p
, bool decltype_p
,
5593 cp_id_kind
* pidk_return
)
5598 cp_id_kind idk
= CP_ID_KIND_NONE
;
5599 tree postfix_expression
= NULL_TREE
;
5600 bool is_member_access
= false;
5602 /* Peek at the next token. */
5603 token
= cp_lexer_peek_token (parser
->lexer
);
5604 loc
= token
->location
;
5605 /* Some of the productions are determined by keywords. */
5606 keyword
= token
->keyword
;
5616 const char *saved_message
;
5617 bool saved_in_type_id_in_expr_p
;
5619 /* All of these can be handled in the same way from the point
5620 of view of parsing. Begin by consuming the token
5621 identifying the cast. */
5622 cp_lexer_consume_token (parser
->lexer
);
5624 /* New types cannot be defined in the cast. */
5625 saved_message
= parser
->type_definition_forbidden_message
;
5626 parser
->type_definition_forbidden_message
5627 = G_("types may not be defined in casts");
5629 /* Look for the opening `<'. */
5630 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5631 /* Parse the type to which we are casting. */
5632 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5633 parser
->in_type_id_in_expr_p
= true;
5634 type
= cp_parser_type_id (parser
);
5635 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5636 /* Look for the closing `>'. */
5637 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5638 /* Restore the old message. */
5639 parser
->type_definition_forbidden_message
= saved_message
;
5641 bool saved_greater_than_is_operator_p
5642 = parser
->greater_than_is_operator_p
;
5643 parser
->greater_than_is_operator_p
= true;
5645 /* And the expression which is being cast. */
5646 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5647 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
5648 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5650 parser
->greater_than_is_operator_p
5651 = saved_greater_than_is_operator_p
;
5653 /* Only type conversions to integral or enumeration types
5654 can be used in constant-expressions. */
5655 if (!cast_valid_in_integral_constant_expression_p (type
)
5656 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5657 return error_mark_node
;
5663 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5667 = build_static_cast (type
, expression
, tf_warning_or_error
);
5671 = build_reinterpret_cast (type
, expression
,
5672 tf_warning_or_error
);
5676 = build_const_cast (type
, expression
, tf_warning_or_error
);
5687 const char *saved_message
;
5688 bool saved_in_type_id_in_expr_p
;
5690 /* Consume the `typeid' token. */
5691 cp_lexer_consume_token (parser
->lexer
);
5692 /* Look for the `(' token. */
5693 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5694 /* Types cannot be defined in a `typeid' expression. */
5695 saved_message
= parser
->type_definition_forbidden_message
;
5696 parser
->type_definition_forbidden_message
5697 = G_("types may not be defined in a %<typeid%> expression");
5698 /* We can't be sure yet whether we're looking at a type-id or an
5700 cp_parser_parse_tentatively (parser
);
5701 /* Try a type-id first. */
5702 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5703 parser
->in_type_id_in_expr_p
= true;
5704 type
= cp_parser_type_id (parser
);
5705 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5706 /* Look for the `)' token. Otherwise, we can't be sure that
5707 we're not looking at an expression: consider `typeid (int
5708 (3))', for example. */
5709 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5710 /* If all went well, simply lookup the type-id. */
5711 if (cp_parser_parse_definitely (parser
))
5712 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5713 /* Otherwise, fall back to the expression variant. */
5718 /* Look for an expression. */
5719 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
5720 /* Compute its typeid. */
5721 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5722 /* Look for the `)' token. */
5723 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5725 /* Restore the saved message. */
5726 parser
->type_definition_forbidden_message
= saved_message
;
5727 /* `typeid' may not appear in an integral constant expression. */
5728 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5729 return error_mark_node
;
5736 /* The syntax permitted here is the same permitted for an
5737 elaborated-type-specifier. */
5738 type
= cp_parser_elaborated_type_specifier (parser
,
5739 /*is_friend=*/false,
5740 /*is_declaration=*/false);
5741 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5745 case RID_BUILTIN_SHUFFLE
:
5747 vec
<tree
, va_gc
> *vec
;
5751 cp_lexer_consume_token (parser
->lexer
);
5752 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5753 /*cast_p=*/false, /*allow_expansion_p=*/true,
5754 /*non_constant_p=*/NULL
);
5756 return error_mark_node
;
5758 FOR_EACH_VEC_ELT (*vec
, i
, p
)
5761 if (vec
->length () == 2)
5762 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
5763 tf_warning_or_error
);
5764 else if (vec
->length () == 3)
5765 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
5766 tf_warning_or_error
);
5769 error_at (loc
, "wrong number of arguments to "
5770 "%<__builtin_shuffle%>");
5771 return error_mark_node
;
5780 /* If the next thing is a simple-type-specifier, we may be
5781 looking at a functional cast. We could also be looking at
5782 an id-expression. So, we try the functional cast, and if
5783 that doesn't work we fall back to the primary-expression. */
5784 cp_parser_parse_tentatively (parser
);
5785 /* Look for the simple-type-specifier. */
5786 type
= cp_parser_simple_type_specifier (parser
,
5787 /*decl_specs=*/NULL
,
5788 CP_PARSER_FLAGS_NONE
);
5789 /* Parse the cast itself. */
5790 if (!cp_parser_error_occurred (parser
))
5792 = cp_parser_functional_cast (parser
, type
);
5793 /* If that worked, we're done. */
5794 if (cp_parser_parse_definitely (parser
))
5797 /* If the functional-cast didn't work out, try a
5798 compound-literal. */
5799 if (cp_parser_allow_gnu_extensions_p (parser
)
5800 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5802 tree initializer
= NULL_TREE
;
5803 bool saved_in_type_id_in_expr_p
;
5805 cp_parser_parse_tentatively (parser
);
5806 /* Consume the `('. */
5807 cp_lexer_consume_token (parser
->lexer
);
5808 /* Parse the type. */
5809 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5810 parser
->in_type_id_in_expr_p
= true;
5811 type
= cp_parser_type_id (parser
);
5812 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5813 /* Look for the `)'. */
5814 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5815 /* If things aren't going well, there's no need to
5817 if (!cp_parser_error_occurred (parser
))
5819 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5821 bool non_constant_p
;
5822 /* Parse the brace-enclosed initializer list. */
5823 initializer
= cp_parser_braced_list (parser
,
5827 cp_parser_simulate_error (parser
);
5829 /* If that worked, we're definitely looking at a
5830 compound-literal expression. */
5831 if (cp_parser_parse_definitely (parser
))
5833 /* Warn the user that a compound literal is not
5834 allowed in standard C++. */
5835 pedwarn (input_location
, OPT_Wpedantic
,
5836 "ISO C++ forbids compound-literals");
5837 /* For simplicity, we disallow compound literals in
5838 constant-expressions. We could
5839 allow compound literals of integer type, whose
5840 initializer was a constant, in constant
5841 expressions. Permitting that usage, as a further
5842 extension, would not change the meaning of any
5843 currently accepted programs. (Of course, as
5844 compound literals are not part of ISO C++, the
5845 standard has nothing to say.) */
5846 if (cp_parser_non_integral_constant_expression (parser
,
5849 postfix_expression
= error_mark_node
;
5852 /* Form the representation of the compound-literal. */
5854 = finish_compound_literal (type
, initializer
,
5855 tf_warning_or_error
);
5860 /* It must be a primary-expression. */
5862 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5863 /*template_arg_p=*/false,
5870 /* Note that we don't need to worry about calling build_cplus_new on a
5871 class-valued CALL_EXPR in decltype when it isn't the end of the
5872 postfix-expression; unary_complex_lvalue will take care of that for
5875 /* Keep looping until the postfix-expression is complete. */
5878 if (idk
== CP_ID_KIND_UNQUALIFIED
5879 && identifier_p (postfix_expression
)
5880 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
5881 /* It is not a Koenig lookup function call. */
5883 = unqualified_name_lookup_error (postfix_expression
);
5885 /* Peek at the next token. */
5886 token
= cp_lexer_peek_token (parser
->lexer
);
5888 switch (token
->type
)
5890 case CPP_OPEN_SQUARE
:
5891 if (cp_next_tokens_can_be_std_attribute_p (parser
))
5893 cp_parser_error (parser
,
5894 "two consecutive %<[%> shall "
5895 "only introduce an attribute");
5896 return error_mark_node
;
5899 = cp_parser_postfix_open_square_expression (parser
,
5903 idk
= CP_ID_KIND_NONE
;
5904 is_member_access
= false;
5907 case CPP_OPEN_PAREN
:
5908 /* postfix-expression ( expression-list [opt] ) */
5911 bool is_builtin_constant_p
;
5912 bool saved_integral_constant_expression_p
= false;
5913 bool saved_non_integral_constant_expression_p
= false;
5914 tsubst_flags_t complain
= complain_flags (decltype_p
);
5915 vec
<tree
, va_gc
> *args
;
5917 is_member_access
= false;
5919 is_builtin_constant_p
5920 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
5921 if (is_builtin_constant_p
)
5923 /* The whole point of __builtin_constant_p is to allow
5924 non-constant expressions to appear as arguments. */
5925 saved_integral_constant_expression_p
5926 = parser
->integral_constant_expression_p
;
5927 saved_non_integral_constant_expression_p
5928 = parser
->non_integral_constant_expression_p
;
5929 parser
->integral_constant_expression_p
= false;
5931 args
= (cp_parser_parenthesized_expression_list
5933 /*cast_p=*/false, /*allow_expansion_p=*/true,
5934 /*non_constant_p=*/NULL
));
5935 if (is_builtin_constant_p
)
5937 parser
->integral_constant_expression_p
5938 = saved_integral_constant_expression_p
;
5939 parser
->non_integral_constant_expression_p
5940 = saved_non_integral_constant_expression_p
;
5945 postfix_expression
= error_mark_node
;
5949 /* Function calls are not permitted in
5950 constant-expressions. */
5951 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
5952 && cp_parser_non_integral_constant_expression (parser
,
5955 postfix_expression
= error_mark_node
;
5956 release_tree_vector (args
);
5961 if (idk
== CP_ID_KIND_UNQUALIFIED
5962 || idk
== CP_ID_KIND_TEMPLATE_ID
)
5964 if (identifier_p (postfix_expression
))
5966 if (!args
->is_empty ())
5969 if (!any_type_dependent_arguments_p (args
))
5971 = perform_koenig_lookup (postfix_expression
, args
,
5972 /*include_std=*/false,
5977 = unqualified_fn_lookup_error (postfix_expression
);
5979 /* We do not perform argument-dependent lookup if
5980 normal lookup finds a non-function, in accordance
5981 with the expected resolution of DR 218. */
5982 else if (!args
->is_empty ()
5983 && is_overloaded_fn (postfix_expression
))
5985 tree fn
= get_first_fn (postfix_expression
);
5986 fn
= STRIP_TEMPLATE (fn
);
5988 /* Do not do argument dependent lookup if regular
5989 lookup finds a member function or a block-scope
5990 function declaration. [basic.lookup.argdep]/3 */
5991 if (!DECL_FUNCTION_MEMBER_P (fn
)
5992 && !DECL_LOCAL_FUNCTION_P (fn
))
5995 if (!any_type_dependent_arguments_p (args
))
5997 = perform_koenig_lookup (postfix_expression
, args
,
5998 /*include_std=*/false,
6004 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6006 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6007 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6009 if (processing_template_decl
6010 && (type_dependent_expression_p (instance
)
6011 || (!BASELINK_P (fn
)
6012 && TREE_CODE (fn
) != FIELD_DECL
)
6013 || type_dependent_expression_p (fn
)
6014 || any_type_dependent_arguments_p (args
)))
6017 = build_nt_call_vec (postfix_expression
, args
);
6018 release_tree_vector (args
);
6022 if (BASELINK_P (fn
))
6025 = (build_new_method_call
6026 (instance
, fn
, &args
, NULL_TREE
,
6027 (idk
== CP_ID_KIND_QUALIFIED
6028 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6035 = finish_call_expr (postfix_expression
, &args
,
6036 /*disallow_virtual=*/false,
6040 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6041 || TREE_CODE (postfix_expression
) == MEMBER_REF
6042 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6043 postfix_expression
= (build_offset_ref_call_from_tree
6044 (postfix_expression
, &args
,
6046 else if (idk
== CP_ID_KIND_QUALIFIED
)
6047 /* A call to a static class member, or a namespace-scope
6050 = finish_call_expr (postfix_expression
, &args
,
6051 /*disallow_virtual=*/true,
6055 /* All other function calls. */
6057 = finish_call_expr (postfix_expression
, &args
,
6058 /*disallow_virtual=*/false,
6062 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6063 idk
= CP_ID_KIND_NONE
;
6065 release_tree_vector (args
);
6071 /* postfix-expression . template [opt] id-expression
6072 postfix-expression . pseudo-destructor-name
6073 postfix-expression -> template [opt] id-expression
6074 postfix-expression -> pseudo-destructor-name */
6076 /* Consume the `.' or `->' operator. */
6077 cp_lexer_consume_token (parser
->lexer
);
6080 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6084 is_member_access
= true;
6088 /* postfix-expression ++ */
6089 /* Consume the `++' token. */
6090 cp_lexer_consume_token (parser
->lexer
);
6091 /* Generate a representation for the complete expression. */
6093 = finish_increment_expr (postfix_expression
,
6094 POSTINCREMENT_EXPR
);
6095 /* Increments may not appear in constant-expressions. */
6096 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6097 postfix_expression
= error_mark_node
;
6098 idk
= CP_ID_KIND_NONE
;
6099 is_member_access
= false;
6102 case CPP_MINUS_MINUS
:
6103 /* postfix-expression -- */
6104 /* Consume the `--' token. */
6105 cp_lexer_consume_token (parser
->lexer
);
6106 /* Generate a representation for the complete expression. */
6108 = finish_increment_expr (postfix_expression
,
6109 POSTDECREMENT_EXPR
);
6110 /* Decrements may not appear in constant-expressions. */
6111 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6112 postfix_expression
= error_mark_node
;
6113 idk
= CP_ID_KIND_NONE
;
6114 is_member_access
= false;
6118 if (pidk_return
!= NULL
)
6119 * pidk_return
= idk
;
6120 if (member_access_only_p
)
6121 return is_member_access
? postfix_expression
: error_mark_node
;
6123 return postfix_expression
;
6127 /* We should never get here. */
6129 return error_mark_node
;
6132 /* This function parses Cilk Plus array notations. If a normal array expr. is
6133 parsed then the array index is passed back to the caller through *INIT_INDEX
6134 and the function returns a NULL_TREE. If array notation expr. is parsed,
6135 then *INIT_INDEX is ignored by the caller and the function returns
6136 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6140 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6143 cp_token
*token
= NULL
;
6144 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6145 if (!array_value
|| array_value
== error_mark_node
)
6147 cp_parser_skip_to_end_of_statement (parser
);
6148 return error_mark_node
;
6151 array_type
= TREE_TYPE (array_value
);
6153 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6154 parser
->colon_corrects_to_scope_p
= false;
6155 token
= cp_lexer_peek_token (parser
->lexer
);
6159 cp_parser_error (parser
, "expected %<:%> or numeral");
6160 return error_mark_node
;
6162 else if (token
->type
== CPP_COLON
)
6164 /* Consume the ':'. */
6165 cp_lexer_consume_token (parser
->lexer
);
6167 /* If we are here, then we have a case like this A[:]. */
6168 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6170 cp_parser_error (parser
, "expected %<]%>");
6171 cp_parser_skip_to_end_of_statement (parser
);
6172 return error_mark_node
;
6174 *init_index
= NULL_TREE
;
6176 length_index
= NULL_TREE
;
6180 /* If we are here, then there are three valid possibilities:
6182 2. ARRAY [ EXP : EXP ]
6183 3. ARRAY [ EXP : EXP : EXP ] */
6185 *init_index
= cp_parser_expression (parser
, false, NULL
);
6186 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6188 /* This indicates that we have a normal array expression. */
6189 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6193 /* Consume the ':'. */
6194 cp_lexer_consume_token (parser
->lexer
);
6195 length_index
= cp_parser_expression (parser
, false, NULL
);
6196 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6198 cp_lexer_consume_token (parser
->lexer
);
6199 stride
= cp_parser_expression (parser
, false, NULL
);
6202 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6204 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6205 || stride
== error_mark_node
)
6207 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6208 cp_lexer_consume_token (parser
->lexer
);
6209 return error_mark_node
;
6211 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6213 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6214 length_index
, stride
, array_type
);
6218 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6219 by cp_parser_builtin_offsetof. We're looking for
6221 postfix-expression [ expression ]
6222 postfix-expression [ braced-init-list ] (C++11)
6224 FOR_OFFSETOF is set if we're being called in that context, which
6225 changes how we deal with integer constant expressions. */
6228 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6229 tree postfix_expression
,
6233 tree index
= NULL_TREE
;
6234 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6235 bool saved_greater_than_is_operator_p
;
6237 /* Consume the `[' token. */
6238 cp_lexer_consume_token (parser
->lexer
);
6240 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6241 parser
->greater_than_is_operator_p
= true;
6243 /* Parse the index expression. */
6244 /* ??? For offsetof, there is a question of what to allow here. If
6245 offsetof is not being used in an integral constant expression context,
6246 then we *could* get the right answer by computing the value at runtime.
6247 If we are in an integral constant expression context, then we might
6248 could accept any constant expression; hard to say without analysis.
6249 Rather than open the barn door too wide right away, allow only integer
6250 constant expressions here. */
6252 index
= cp_parser_constant_expression (parser
, false, NULL
);
6255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6257 bool expr_nonconst_p
;
6258 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6259 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6260 if (flag_enable_cilkplus
6261 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6263 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6264 "braced list index is not allowed with array "
6266 cp_parser_skip_to_end_of_statement (parser
);
6267 return error_mark_node
;
6270 else if (flag_enable_cilkplus
)
6272 /* Here are have these two options:
6273 ARRAY[EXP : EXP] - Array notation expr with default
6275 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6277 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6278 postfix_expression
);
6283 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6286 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6288 /* Look for the closing `]'. */
6289 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6291 /* Build the ARRAY_REF. */
6292 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6295 /* When not doing offsetof, array references are not permitted in
6296 constant-expressions. */
6298 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6299 postfix_expression
= error_mark_node
;
6301 return postfix_expression
;
6304 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6305 by cp_parser_builtin_offsetof. We're looking for
6307 postfix-expression . template [opt] id-expression
6308 postfix-expression . pseudo-destructor-name
6309 postfix-expression -> template [opt] id-expression
6310 postfix-expression -> pseudo-destructor-name
6312 FOR_OFFSETOF is set if we're being called in that context. That sorta
6313 limits what of the above we'll actually accept, but nevermind.
6314 TOKEN_TYPE is the "." or "->" token, which will already have been
6315 removed from the stream. */
6318 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6319 enum cpp_ttype token_type
,
6320 tree postfix_expression
,
6321 bool for_offsetof
, cp_id_kind
*idk
,
6322 location_t location
)
6326 bool pseudo_destructor_p
;
6327 tree scope
= NULL_TREE
;
6329 /* If this is a `->' operator, dereference the pointer. */
6330 if (token_type
== CPP_DEREF
)
6331 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6332 tf_warning_or_error
);
6333 /* Check to see whether or not the expression is type-dependent. */
6334 dependent_p
= type_dependent_expression_p (postfix_expression
);
6335 /* The identifier following the `->' or `.' is not qualified. */
6336 parser
->scope
= NULL_TREE
;
6337 parser
->qualifying_scope
= NULL_TREE
;
6338 parser
->object_scope
= NULL_TREE
;
6339 *idk
= CP_ID_KIND_NONE
;
6341 /* Enter the scope corresponding to the type of the object
6342 given by the POSTFIX_EXPRESSION. */
6343 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6345 scope
= TREE_TYPE (postfix_expression
);
6346 /* According to the standard, no expression should ever have
6347 reference type. Unfortunately, we do not currently match
6348 the standard in this respect in that our internal representation
6349 of an expression may have reference type even when the standard
6350 says it does not. Therefore, we have to manually obtain the
6351 underlying type here. */
6352 scope
= non_reference (scope
);
6353 /* The type of the POSTFIX_EXPRESSION must be complete. */
6354 if (scope
== unknown_type_node
)
6356 error_at (location
, "%qE does not have class type",
6357 postfix_expression
);
6360 /* Unlike the object expression in other contexts, *this is not
6361 required to be of complete type for purposes of class member
6362 access (5.2.5) outside the member function body. */
6363 else if (postfix_expression
!= current_class_ref
6364 && !(processing_template_decl
&& scope
== current_class_type
))
6365 scope
= complete_type_or_else (scope
, NULL_TREE
);
6366 /* Let the name lookup machinery know that we are processing a
6367 class member access expression. */
6368 parser
->context
->object_type
= scope
;
6369 /* If something went wrong, we want to be able to discern that case,
6370 as opposed to the case where there was no SCOPE due to the type
6371 of expression being dependent. */
6373 scope
= error_mark_node
;
6374 /* If the SCOPE was erroneous, make the various semantic analysis
6375 functions exit quickly -- and without issuing additional error
6377 if (scope
== error_mark_node
)
6378 postfix_expression
= error_mark_node
;
6381 /* Assume this expression is not a pseudo-destructor access. */
6382 pseudo_destructor_p
= false;
6384 /* If the SCOPE is a scalar type, then, if this is a valid program,
6385 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6386 is type dependent, it can be pseudo-destructor-name or something else.
6387 Try to parse it as pseudo-destructor-name first. */
6388 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6393 cp_parser_parse_tentatively (parser
);
6394 /* Parse the pseudo-destructor-name. */
6396 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6399 && (cp_parser_error_occurred (parser
)
6400 || !SCALAR_TYPE_P (type
)))
6401 cp_parser_abort_tentative_parse (parser
);
6402 else if (cp_parser_parse_definitely (parser
))
6404 pseudo_destructor_p
= true;
6406 = finish_pseudo_destructor_expr (postfix_expression
,
6411 if (!pseudo_destructor_p
)
6413 /* If the SCOPE is not a scalar type, we are looking at an
6414 ordinary class member access expression, rather than a
6415 pseudo-destructor-name. */
6417 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6418 /* Parse the id-expression. */
6419 name
= (cp_parser_id_expression
6421 cp_parser_optional_template_keyword (parser
),
6422 /*check_dependency_p=*/true,
6424 /*declarator_p=*/false,
6425 /*optional_p=*/false));
6426 /* In general, build a SCOPE_REF if the member name is qualified.
6427 However, if the name was not dependent and has already been
6428 resolved; there is no need to build the SCOPE_REF. For example;
6430 struct X { void f(); };
6431 template <typename T> void f(T* t) { t->X::f(); }
6433 Even though "t" is dependent, "X::f" is not and has been resolved
6434 to a BASELINK; there is no need to include scope information. */
6436 /* But we do need to remember that there was an explicit scope for
6437 virtual function calls. */
6439 *idk
= CP_ID_KIND_QUALIFIED
;
6441 /* If the name is a template-id that names a type, we will get a
6442 TYPE_DECL here. That is invalid code. */
6443 if (TREE_CODE (name
) == TYPE_DECL
)
6445 error_at (token
->location
, "invalid use of %qD", name
);
6446 postfix_expression
= error_mark_node
;
6450 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6452 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6454 error_at (token
->location
, "%<%D::%D%> is not a class member",
6455 parser
->scope
, name
);
6456 postfix_expression
= error_mark_node
;
6459 name
= build_qualified_name (/*type=*/NULL_TREE
,
6463 parser
->scope
= NULL_TREE
;
6464 parser
->qualifying_scope
= NULL_TREE
;
6465 parser
->object_scope
= NULL_TREE
;
6467 if (parser
->scope
&& name
&& BASELINK_P (name
))
6468 adjust_result_of_qualified_name_lookup
6469 (name
, parser
->scope
, scope
);
6471 = finish_class_member_access_expr (postfix_expression
, name
,
6473 tf_warning_or_error
);
6477 /* We no longer need to look up names in the scope of the object on
6478 the left-hand side of the `.' or `->' operator. */
6479 parser
->context
->object_type
= NULL_TREE
;
6481 /* Outside of offsetof, these operators may not appear in
6482 constant-expressions. */
6484 && (cp_parser_non_integral_constant_expression
6485 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6486 postfix_expression
= error_mark_node
;
6488 return postfix_expression
;
6491 /* Parse a parenthesized expression-list.
6494 assignment-expression
6495 expression-list, assignment-expression
6500 identifier, expression-list
6502 CAST_P is true if this expression is the target of a cast.
6504 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6507 Returns a vector of trees. Each element is a representation of an
6508 assignment-expression. NULL is returned if the ( and or ) are
6509 missing. An empty, but allocated, vector is returned on no
6510 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6511 if we are parsing an attribute list for an attribute that wants a
6512 plain identifier argument, normal_attr for an attribute that wants
6513 an expression, or non_attr if we aren't parsing an attribute list. If
6514 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6515 not all of the expressions in the list were constant. */
6517 static vec
<tree
, va_gc
> *
6518 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6519 int is_attribute_list
,
6521 bool allow_expansion_p
,
6522 bool *non_constant_p
)
6524 vec
<tree
, va_gc
> *expression_list
;
6525 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6526 tree identifier
= NULL_TREE
;
6527 bool saved_greater_than_is_operator_p
;
6529 /* Assume all the expressions will be constant. */
6531 *non_constant_p
= false;
6533 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6536 expression_list
= make_tree_vector ();
6538 /* Within a parenthesized expression, a `>' token is always
6539 the greater-than operator. */
6540 saved_greater_than_is_operator_p
6541 = parser
->greater_than_is_operator_p
;
6542 parser
->greater_than_is_operator_p
= true;
6544 /* Consume expressions until there are no more. */
6545 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6550 /* At the beginning of attribute lists, check to see if the
6551 next token is an identifier. */
6552 if (is_attribute_list
== id_attr
6553 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6557 /* Consume the identifier. */
6558 token
= cp_lexer_consume_token (parser
->lexer
);
6559 /* Save the identifier. */
6560 identifier
= token
->u
.value
;
6564 bool expr_non_constant_p
;
6566 /* Parse the next assignment-expression. */
6567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6569 /* A braced-init-list. */
6570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6571 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6572 if (non_constant_p
&& expr_non_constant_p
)
6573 *non_constant_p
= true;
6575 else if (non_constant_p
)
6577 expr
= (cp_parser_constant_expression
6578 (parser
, /*allow_non_constant_p=*/true,
6579 &expr_non_constant_p
));
6580 if (expr_non_constant_p
)
6581 *non_constant_p
= true;
6584 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
6587 expr
= fold_non_dependent_expr (expr
);
6589 /* If we have an ellipsis, then this is an expression
6591 if (allow_expansion_p
6592 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6594 /* Consume the `...'. */
6595 cp_lexer_consume_token (parser
->lexer
);
6597 /* Build the argument pack. */
6598 expr
= make_pack_expansion (expr
);
6601 /* Add it to the list. We add error_mark_node
6602 expressions to the list, so that we can still tell if
6603 the correct form for a parenthesized expression-list
6604 is found. That gives better errors. */
6605 vec_safe_push (expression_list
, expr
);
6607 if (expr
== error_mark_node
)
6611 /* After the first item, attribute lists look the same as
6612 expression lists. */
6613 is_attribute_list
= non_attr
;
6616 /* If the next token isn't a `,', then we are done. */
6617 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6620 /* Otherwise, consume the `,' and keep going. */
6621 cp_lexer_consume_token (parser
->lexer
);
6624 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6629 /* We try and resync to an unnested comma, as that will give the
6630 user better diagnostics. */
6631 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6632 /*recovering=*/true,
6634 /*consume_paren=*/true);
6639 parser
->greater_than_is_operator_p
6640 = saved_greater_than_is_operator_p
;
6645 parser
->greater_than_is_operator_p
6646 = saved_greater_than_is_operator_p
;
6649 vec_safe_insert (expression_list
, 0, identifier
);
6651 return expression_list
;
6654 /* Parse a pseudo-destructor-name.
6656 pseudo-destructor-name:
6657 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6658 :: [opt] nested-name-specifier template template-id :: ~ type-name
6659 :: [opt] nested-name-specifier [opt] ~ type-name
6661 If either of the first two productions is used, sets *SCOPE to the
6662 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6663 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6664 or ERROR_MARK_NODE if the parse fails. */
6667 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6672 bool nested_name_specifier_p
;
6675 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
6676 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
6677 && !type_dependent_expression_p (object
))
6679 if (cxx_dialect
< cxx1y
)
6680 pedwarn (input_location
, 0,
6681 "%<~auto%> only available with "
6682 "-std=c++1y or -std=gnu++1y");
6683 cp_lexer_consume_token (parser
->lexer
);
6684 cp_lexer_consume_token (parser
->lexer
);
6686 *type
= TREE_TYPE (object
);
6690 /* Assume that things will not work out. */
6691 *type
= error_mark_node
;
6693 /* Look for the optional `::' operator. */
6694 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
6695 /* Look for the optional nested-name-specifier. */
6696 nested_name_specifier_p
6697 = (cp_parser_nested_name_specifier_opt (parser
,
6698 /*typename_keyword_p=*/false,
6699 /*check_dependency_p=*/true,
6701 /*is_declaration=*/false)
6703 /* Now, if we saw a nested-name-specifier, we might be doing the
6704 second production. */
6705 if (nested_name_specifier_p
6706 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
6708 /* Consume the `template' keyword. */
6709 cp_lexer_consume_token (parser
->lexer
);
6710 /* Parse the template-id. */
6711 cp_parser_template_id (parser
,
6712 /*template_keyword_p=*/true,
6713 /*check_dependency_p=*/false,
6715 /*is_declaration=*/true);
6716 /* Look for the `::' token. */
6717 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6719 /* If the next token is not a `~', then there might be some
6720 additional qualification. */
6721 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
6723 /* At this point, we're looking for "type-name :: ~". The type-name
6724 must not be a class-name, since this is a pseudo-destructor. So,
6725 it must be either an enum-name, or a typedef-name -- both of which
6726 are just identifiers. So, we peek ahead to check that the "::"
6727 and "~" tokens are present; if they are not, then we can avoid
6728 calling type_name. */
6729 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
6730 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
6731 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
6733 cp_parser_error (parser
, "non-scalar type");
6737 /* Look for the type-name. */
6738 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6739 if (*scope
== error_mark_node
)
6742 /* Look for the `::' token. */
6743 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6748 /* Look for the `~'. */
6749 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
6751 /* Once we see the ~, this has to be a pseudo-destructor. */
6752 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
6753 cp_parser_commit_to_topmost_tentative_parse (parser
);
6755 /* Look for the type-name again. We are not responsible for
6756 checking that it matches the first type-name. */
6757 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6760 /* Parse a unary-expression.
6766 unary-operator cast-expression
6767 sizeof unary-expression
6769 alignof ( type-id ) [C++0x]
6776 __extension__ cast-expression
6777 __alignof__ unary-expression
6778 __alignof__ ( type-id )
6779 alignof unary-expression [C++0x]
6780 __real__ cast-expression
6781 __imag__ cast-expression
6783 sizeof ( type-id ) { initializer-list , [opt] }
6784 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6785 __alignof__ ( type-id ) { initializer-list , [opt] }
6787 ADDRESS_P is true iff the unary-expression is appearing as the
6788 operand of the `&' operator. CAST_P is true if this expression is
6789 the target of a cast.
6791 Returns a representation of the expression. */
6794 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6795 bool decltype_p
, cp_id_kind
* pidk
)
6798 enum tree_code unary_operator
;
6800 /* Peek at the next token. */
6801 token
= cp_lexer_peek_token (parser
->lexer
);
6802 /* Some keywords give away the kind of expression. */
6803 if (token
->type
== CPP_KEYWORD
)
6805 enum rid keyword
= token
->keyword
;
6814 location_t first_loc
;
6816 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
6817 /* Consume the token. */
6818 cp_lexer_consume_token (parser
->lexer
);
6819 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6820 /* Parse the operand. */
6821 operand
= cp_parser_sizeof_operand (parser
, keyword
);
6823 if (TYPE_P (operand
))
6824 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
6827 /* ISO C++ defines alignof only with types, not with
6828 expressions. So pedwarn if alignof is used with a non-
6829 type expression. However, __alignof__ is ok. */
6830 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
6831 pedwarn (token
->location
, OPT_Wpedantic
,
6832 "ISO C++ does not allow %<alignof%> "
6835 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
6837 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6838 SIZEOF_EXPR with the original operand. */
6839 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
6841 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
6843 if (!processing_template_decl
&& TYPE_P (operand
))
6845 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
6846 build1 (NOP_EXPR
, operand
,
6848 SIZEOF_EXPR_TYPE_P (ret
) = 1;
6851 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
6852 TREE_SIDE_EFFECTS (ret
) = 0;
6853 TREE_READONLY (ret
) = 1;
6855 SET_EXPR_LOCATION (ret
, first_loc
);
6861 return cp_parser_new_expression (parser
);
6864 return cp_parser_delete_expression (parser
);
6868 /* The saved value of the PEDANTIC flag. */
6872 /* Save away the PEDANTIC flag. */
6873 cp_parser_extension_opt (parser
, &saved_pedantic
);
6874 /* Parse the cast-expression. */
6875 expr
= cp_parser_simple_cast_expression (parser
);
6876 /* Restore the PEDANTIC flag. */
6877 pedantic
= saved_pedantic
;
6887 /* Consume the `__real__' or `__imag__' token. */
6888 cp_lexer_consume_token (parser
->lexer
);
6889 /* Parse the cast-expression. */
6890 expression
= cp_parser_simple_cast_expression (parser
);
6891 /* Create the complete representation. */
6892 return build_x_unary_op (token
->location
,
6893 (keyword
== RID_REALPART
6894 ? REALPART_EXPR
: IMAGPART_EXPR
),
6896 tf_warning_or_error
);
6900 case RID_TRANSACTION_ATOMIC
:
6901 case RID_TRANSACTION_RELAXED
:
6902 return cp_parser_transaction_expression (parser
, keyword
);
6907 const char *saved_message
;
6908 bool saved_integral_constant_expression_p
;
6909 bool saved_non_integral_constant_expression_p
;
6910 bool saved_greater_than_is_operator_p
;
6912 cp_lexer_consume_token (parser
->lexer
);
6913 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6915 saved_message
= parser
->type_definition_forbidden_message
;
6916 parser
->type_definition_forbidden_message
6917 = G_("types may not be defined in %<noexcept%> expressions");
6919 saved_integral_constant_expression_p
6920 = parser
->integral_constant_expression_p
;
6921 saved_non_integral_constant_expression_p
6922 = parser
->non_integral_constant_expression_p
;
6923 parser
->integral_constant_expression_p
= false;
6925 saved_greater_than_is_operator_p
6926 = parser
->greater_than_is_operator_p
;
6927 parser
->greater_than_is_operator_p
= true;
6929 ++cp_unevaluated_operand
;
6930 ++c_inhibit_evaluation_warnings
;
6931 expr
= cp_parser_expression (parser
, false, NULL
);
6932 --c_inhibit_evaluation_warnings
;
6933 --cp_unevaluated_operand
;
6935 parser
->greater_than_is_operator_p
6936 = saved_greater_than_is_operator_p
;
6938 parser
->integral_constant_expression_p
6939 = saved_integral_constant_expression_p
;
6940 parser
->non_integral_constant_expression_p
6941 = saved_non_integral_constant_expression_p
;
6943 parser
->type_definition_forbidden_message
= saved_message
;
6945 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6946 return finish_noexcept_expr (expr
, tf_warning_or_error
);
6954 /* Look for the `:: new' and `:: delete', which also signal the
6955 beginning of a new-expression, or delete-expression,
6956 respectively. If the next token is `::', then it might be one of
6958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
6962 /* See if the token after the `::' is one of the keywords in
6963 which we're interested. */
6964 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
6965 /* If it's `new', we have a new-expression. */
6966 if (keyword
== RID_NEW
)
6967 return cp_parser_new_expression (parser
);
6968 /* Similarly, for `delete'. */
6969 else if (keyword
== RID_DELETE
)
6970 return cp_parser_delete_expression (parser
);
6973 /* Look for a unary operator. */
6974 unary_operator
= cp_parser_unary_operator (token
);
6975 /* The `++' and `--' operators can be handled similarly, even though
6976 they are not technically unary-operators in the grammar. */
6977 if (unary_operator
== ERROR_MARK
)
6979 if (token
->type
== CPP_PLUS_PLUS
)
6980 unary_operator
= PREINCREMENT_EXPR
;
6981 else if (token
->type
== CPP_MINUS_MINUS
)
6982 unary_operator
= PREDECREMENT_EXPR
;
6983 /* Handle the GNU address-of-label extension. */
6984 else if (cp_parser_allow_gnu_extensions_p (parser
)
6985 && token
->type
== CPP_AND_AND
)
6989 location_t loc
= token
->location
;
6991 /* Consume the '&&' token. */
6992 cp_lexer_consume_token (parser
->lexer
);
6993 /* Look for the identifier. */
6994 identifier
= cp_parser_identifier (parser
);
6995 /* Create an expression representing the address. */
6996 expression
= finish_label_address_expr (identifier
, loc
);
6997 if (cp_parser_non_integral_constant_expression (parser
,
6999 expression
= error_mark_node
;
7003 if (unary_operator
!= ERROR_MARK
)
7005 tree cast_expression
;
7006 tree expression
= error_mark_node
;
7007 non_integral_constant non_constant_p
= NIC_NONE
;
7008 location_t loc
= token
->location
;
7009 tsubst_flags_t complain
= complain_flags (decltype_p
);
7011 /* Consume the operator token. */
7012 token
= cp_lexer_consume_token (parser
->lexer
);
7013 /* Parse the cast-expression. */
7015 = cp_parser_cast_expression (parser
,
7016 unary_operator
== ADDR_EXPR
,
7020 /* Now, build an appropriate representation. */
7021 switch (unary_operator
)
7024 non_constant_p
= NIC_STAR
;
7025 expression
= build_x_indirect_ref (loc
, cast_expression
,
7031 non_constant_p
= NIC_ADDR
;
7034 expression
= build_x_unary_op (loc
, unary_operator
,
7039 case PREINCREMENT_EXPR
:
7040 case PREDECREMENT_EXPR
:
7041 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7042 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7044 case UNARY_PLUS_EXPR
:
7046 case TRUTH_NOT_EXPR
:
7047 expression
= finish_unary_op_expr (loc
, unary_operator
,
7048 cast_expression
, complain
);
7055 if (non_constant_p
!= NIC_NONE
7056 && cp_parser_non_integral_constant_expression (parser
,
7058 expression
= error_mark_node
;
7063 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7064 /*member_access_only_p=*/false,
7070 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7073 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7074 /*decltype*/false, pidk
);
7077 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7078 unary-operator, the corresponding tree code is returned. */
7080 static enum tree_code
7081 cp_parser_unary_operator (cp_token
* token
)
7083 switch (token
->type
)
7086 return INDIRECT_REF
;
7092 return UNARY_PLUS_EXPR
;
7098 return TRUTH_NOT_EXPR
;
7101 return BIT_NOT_EXPR
;
7108 /* Parse a new-expression.
7111 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7112 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7114 Returns a representation of the expression. */
7117 cp_parser_new_expression (cp_parser
* parser
)
7119 bool global_scope_p
;
7120 vec
<tree
, va_gc
> *placement
;
7122 vec
<tree
, va_gc
> *initializer
;
7123 tree nelts
= NULL_TREE
;
7126 /* Look for the optional `::' operator. */
7128 = (cp_parser_global_scope_opt (parser
,
7129 /*current_scope_valid_p=*/false)
7131 /* Look for the `new' operator. */
7132 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7133 /* There's no easy way to tell a new-placement from the
7134 `( type-id )' construct. */
7135 cp_parser_parse_tentatively (parser
);
7136 /* Look for a new-placement. */
7137 placement
= cp_parser_new_placement (parser
);
7138 /* If that didn't work out, there's no new-placement. */
7139 if (!cp_parser_parse_definitely (parser
))
7141 if (placement
!= NULL
)
7142 release_tree_vector (placement
);
7146 /* If the next token is a `(', then we have a parenthesized
7148 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7151 const char *saved_message
= parser
->type_definition_forbidden_message
;
7153 /* Consume the `('. */
7154 cp_lexer_consume_token (parser
->lexer
);
7156 /* Parse the type-id. */
7157 parser
->type_definition_forbidden_message
7158 = G_("types may not be defined in a new-expression");
7159 type
= cp_parser_type_id (parser
);
7160 parser
->type_definition_forbidden_message
= saved_message
;
7162 /* Look for the closing `)'. */
7163 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7164 token
= cp_lexer_peek_token (parser
->lexer
);
7165 /* There should not be a direct-new-declarator in this production,
7166 but GCC used to allowed this, so we check and emit a sensible error
7167 message for this case. */
7168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7170 error_at (token
->location
,
7171 "array bound forbidden after parenthesized type-id");
7172 inform (token
->location
,
7173 "try removing the parentheses around the type-id");
7174 cp_parser_direct_new_declarator (parser
);
7177 /* Otherwise, there must be a new-type-id. */
7179 type
= cp_parser_new_type_id (parser
, &nelts
);
7181 /* If the next token is a `(' or '{', then we have a new-initializer. */
7182 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7183 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7184 initializer
= cp_parser_new_initializer (parser
);
7188 /* A new-expression may not appear in an integral constant
7190 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7191 ret
= error_mark_node
;
7194 /* Create a representation of the new-expression. */
7195 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7196 tf_warning_or_error
);
7199 if (placement
!= NULL
)
7200 release_tree_vector (placement
);
7201 if (initializer
!= NULL
)
7202 release_tree_vector (initializer
);
7207 /* Parse a new-placement.
7212 Returns the same representation as for an expression-list. */
7214 static vec
<tree
, va_gc
> *
7215 cp_parser_new_placement (cp_parser
* parser
)
7217 vec
<tree
, va_gc
> *expression_list
;
7219 /* Parse the expression-list. */
7220 expression_list
= (cp_parser_parenthesized_expression_list
7221 (parser
, non_attr
, /*cast_p=*/false,
7222 /*allow_expansion_p=*/true,
7223 /*non_constant_p=*/NULL
));
7225 return expression_list
;
7228 /* Parse a new-type-id.
7231 type-specifier-seq new-declarator [opt]
7233 Returns the TYPE allocated. If the new-type-id indicates an array
7234 type, *NELTS is set to the number of elements in the last array
7235 bound; the TYPE will not include the last array bound. */
7238 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7240 cp_decl_specifier_seq type_specifier_seq
;
7241 cp_declarator
*new_declarator
;
7242 cp_declarator
*declarator
;
7243 cp_declarator
*outer_declarator
;
7244 const char *saved_message
;
7246 /* The type-specifier sequence must not contain type definitions.
7247 (It cannot contain declarations of new types either, but if they
7248 are not definitions we will catch that because they are not
7250 saved_message
= parser
->type_definition_forbidden_message
;
7251 parser
->type_definition_forbidden_message
7252 = G_("types may not be defined in a new-type-id");
7253 /* Parse the type-specifier-seq. */
7254 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7255 /*is_trailing_return=*/false,
7256 &type_specifier_seq
);
7257 /* Restore the old message. */
7258 parser
->type_definition_forbidden_message
= saved_message
;
7260 if (type_specifier_seq
.type
== error_mark_node
)
7261 return error_mark_node
;
7263 /* Parse the new-declarator. */
7264 new_declarator
= cp_parser_new_declarator_opt (parser
);
7266 /* Determine the number of elements in the last array dimension, if
7269 /* Skip down to the last array dimension. */
7270 declarator
= new_declarator
;
7271 outer_declarator
= NULL
;
7272 while (declarator
&& (declarator
->kind
== cdk_pointer
7273 || declarator
->kind
== cdk_ptrmem
))
7275 outer_declarator
= declarator
;
7276 declarator
= declarator
->declarator
;
7279 && declarator
->kind
== cdk_array
7280 && declarator
->declarator
7281 && declarator
->declarator
->kind
== cdk_array
)
7283 outer_declarator
= declarator
;
7284 declarator
= declarator
->declarator
;
7287 if (declarator
&& declarator
->kind
== cdk_array
)
7289 *nelts
= declarator
->u
.array
.bounds
;
7290 if (*nelts
== error_mark_node
)
7291 *nelts
= integer_one_node
;
7293 if (outer_declarator
)
7294 outer_declarator
->declarator
= declarator
->declarator
;
7296 new_declarator
= NULL
;
7299 return groktypename (&type_specifier_seq
, new_declarator
, false);
7302 /* Parse an (optional) new-declarator.
7305 ptr-operator new-declarator [opt]
7306 direct-new-declarator
7308 Returns the declarator. */
7310 static cp_declarator
*
7311 cp_parser_new_declarator_opt (cp_parser
* parser
)
7313 enum tree_code code
;
7314 tree type
, std_attributes
= NULL_TREE
;
7315 cp_cv_quals cv_quals
;
7317 /* We don't know if there's a ptr-operator next, or not. */
7318 cp_parser_parse_tentatively (parser
);
7319 /* Look for a ptr-operator. */
7320 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7321 /* If that worked, look for more new-declarators. */
7322 if (cp_parser_parse_definitely (parser
))
7324 cp_declarator
*declarator
;
7326 /* Parse another optional declarator. */
7327 declarator
= cp_parser_new_declarator_opt (parser
);
7329 declarator
= cp_parser_make_indirect_declarator
7330 (code
, type
, cv_quals
, declarator
, std_attributes
);
7335 /* If the next token is a `[', there is a direct-new-declarator. */
7336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7337 return cp_parser_direct_new_declarator (parser
);
7342 /* Parse a direct-new-declarator.
7344 direct-new-declarator:
7346 direct-new-declarator [constant-expression]
7350 static cp_declarator
*
7351 cp_parser_direct_new_declarator (cp_parser
* parser
)
7353 cp_declarator
*declarator
= NULL
;
7360 /* Look for the opening `['. */
7361 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7363 token
= cp_lexer_peek_token (parser
->lexer
);
7364 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7365 /* The standard requires that the expression have integral
7366 type. DR 74 adds enumeration types. We believe that the
7367 real intent is that these expressions be handled like the
7368 expression in a `switch' condition, which also allows
7369 classes with a single conversion to integral or
7370 enumeration type. */
7371 if (!processing_template_decl
)
7374 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7379 error_at (token
->location
,
7380 "expression in new-declarator must have integral "
7381 "or enumeration type");
7382 expression
= error_mark_node
;
7386 /* Look for the closing `]'. */
7387 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7389 /* Add this bound to the declarator. */
7390 declarator
= make_array_declarator (declarator
, expression
);
7392 /* If the next token is not a `[', then there are no more
7394 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7401 /* Parse a new-initializer.
7404 ( expression-list [opt] )
7407 Returns a representation of the expression-list. */
7409 static vec
<tree
, va_gc
> *
7410 cp_parser_new_initializer (cp_parser
* parser
)
7412 vec
<tree
, va_gc
> *expression_list
;
7414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7417 bool expr_non_constant_p
;
7418 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7419 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7420 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7421 expression_list
= make_tree_vector_single (t
);
7424 expression_list
= (cp_parser_parenthesized_expression_list
7425 (parser
, non_attr
, /*cast_p=*/false,
7426 /*allow_expansion_p=*/true,
7427 /*non_constant_p=*/NULL
));
7429 return expression_list
;
7432 /* Parse a delete-expression.
7435 :: [opt] delete cast-expression
7436 :: [opt] delete [ ] cast-expression
7438 Returns a representation of the expression. */
7441 cp_parser_delete_expression (cp_parser
* parser
)
7443 bool global_scope_p
;
7447 /* Look for the optional `::' operator. */
7449 = (cp_parser_global_scope_opt (parser
,
7450 /*current_scope_valid_p=*/false)
7452 /* Look for the `delete' keyword. */
7453 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7454 /* See if the array syntax is in use. */
7455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7457 /* Consume the `[' token. */
7458 cp_lexer_consume_token (parser
->lexer
);
7459 /* Look for the `]' token. */
7460 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7461 /* Remember that this is the `[]' construct. */
7467 /* Parse the cast-expression. */
7468 expression
= cp_parser_simple_cast_expression (parser
);
7470 /* A delete-expression may not appear in an integral constant
7472 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7473 return error_mark_node
;
7475 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7476 tf_warning_or_error
);
7479 /* Returns true if TOKEN may start a cast-expression and false
7483 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7485 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7486 switch (token
->type
)
7492 case CPP_CLOSE_SQUARE
:
7493 case CPP_CLOSE_PAREN
:
7494 case CPP_CLOSE_BRACE
:
7498 case CPP_DEREF_STAR
:
7506 case CPP_GREATER_EQ
:
7526 case CPP_OPEN_PAREN
:
7527 /* In ((type ()) () the last () isn't a valid cast-expression,
7528 so the whole must be parsed as postfix-expression. */
7529 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7532 /* '[' may start a primary-expression in obj-c++. */
7533 case CPP_OPEN_SQUARE
:
7534 return c_dialect_objc ();
7541 /* Parse a cast-expression.
7545 ( type-id ) cast-expression
7547 ADDRESS_P is true iff the unary-expression is appearing as the
7548 operand of the `&' operator. CAST_P is true if this expression is
7549 the target of a cast.
7551 Returns a representation of the expression. */
7554 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7555 bool decltype_p
, cp_id_kind
* pidk
)
7557 /* If it's a `(', then we might be looking at a cast. */
7558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7560 tree type
= NULL_TREE
;
7561 tree expr
= NULL_TREE
;
7562 bool compound_literal_p
;
7563 const char *saved_message
;
7565 /* There's no way to know yet whether or not this is a cast.
7566 For example, `(int (3))' is a unary-expression, while `(int)
7567 3' is a cast. So, we resort to parsing tentatively. */
7568 cp_parser_parse_tentatively (parser
);
7569 /* Types may not be defined in a cast. */
7570 saved_message
= parser
->type_definition_forbidden_message
;
7571 parser
->type_definition_forbidden_message
7572 = G_("types may not be defined in casts");
7573 /* Consume the `('. */
7574 cp_lexer_consume_token (parser
->lexer
);
7575 /* A very tricky bit is that `(struct S) { 3 }' is a
7576 compound-literal (which we permit in C++ as an extension).
7577 But, that construct is not a cast-expression -- it is a
7578 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7579 is legal; if the compound-literal were a cast-expression,
7580 you'd need an extra set of parentheses.) But, if we parse
7581 the type-id, and it happens to be a class-specifier, then we
7582 will commit to the parse at that point, because we cannot
7583 undo the action that is done when creating a new class. So,
7584 then we cannot back up and do a postfix-expression.
7586 Therefore, we scan ahead to the closing `)', and check to see
7587 if the token after the `)' is a `{'. If so, we are not
7588 looking at a cast-expression.
7590 Save tokens so that we can put them back. */
7591 cp_lexer_save_tokens (parser
->lexer
);
7592 /* Skip tokens until the next token is a closing parenthesis.
7593 If we find the closing `)', and the next token is a `{', then
7594 we are looking at a compound-literal. */
7596 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7597 /*consume_paren=*/true)
7598 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
7599 /* Roll back the tokens we skipped. */
7600 cp_lexer_rollback_tokens (parser
->lexer
);
7601 /* If we were looking at a compound-literal, simulate an error
7602 so that the call to cp_parser_parse_definitely below will
7604 if (compound_literal_p
)
7605 cp_parser_simulate_error (parser
);
7608 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7609 parser
->in_type_id_in_expr_p
= true;
7610 /* Look for the type-id. */
7611 type
= cp_parser_type_id (parser
);
7612 /* Look for the closing `)'. */
7613 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7614 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7617 /* Restore the saved message. */
7618 parser
->type_definition_forbidden_message
= saved_message
;
7620 /* At this point this can only be either a cast or a
7621 parenthesized ctor such as `(T ())' that looks like a cast to
7622 function returning T. */
7623 if (!cp_parser_error_occurred (parser
)
7624 && cp_parser_tokens_start_cast_expression (parser
))
7626 cp_parser_parse_definitely (parser
);
7627 expr
= cp_parser_cast_expression (parser
,
7628 /*address_p=*/false,
7630 /*decltype_p=*/false,
7633 /* Warn about old-style casts, if so requested. */
7634 if (warn_old_style_cast
7635 && !in_system_header
7636 && !VOID_TYPE_P (type
)
7637 && current_lang_name
!= lang_name_c
)
7638 warning (OPT_Wold_style_cast
, "use of old-style cast");
7640 /* Only type conversions to integral or enumeration types
7641 can be used in constant-expressions. */
7642 if (!cast_valid_in_integral_constant_expression_p (type
)
7643 && cp_parser_non_integral_constant_expression (parser
,
7645 return error_mark_node
;
7647 /* Perform the cast. */
7648 expr
= build_c_cast (input_location
, type
, expr
);
7652 cp_parser_abort_tentative_parse (parser
);
7655 /* If we get here, then it's not a cast, so it must be a
7656 unary-expression. */
7657 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7661 /* Parse a binary expression of the general form:
7665 pm-expression .* cast-expression
7666 pm-expression ->* cast-expression
7668 multiplicative-expression:
7670 multiplicative-expression * pm-expression
7671 multiplicative-expression / pm-expression
7672 multiplicative-expression % pm-expression
7674 additive-expression:
7675 multiplicative-expression
7676 additive-expression + multiplicative-expression
7677 additive-expression - multiplicative-expression
7681 shift-expression << additive-expression
7682 shift-expression >> additive-expression
7684 relational-expression:
7686 relational-expression < shift-expression
7687 relational-expression > shift-expression
7688 relational-expression <= shift-expression
7689 relational-expression >= shift-expression
7693 relational-expression:
7694 relational-expression <? shift-expression
7695 relational-expression >? shift-expression
7697 equality-expression:
7698 relational-expression
7699 equality-expression == relational-expression
7700 equality-expression != relational-expression
7704 and-expression & equality-expression
7706 exclusive-or-expression:
7708 exclusive-or-expression ^ and-expression
7710 inclusive-or-expression:
7711 exclusive-or-expression
7712 inclusive-or-expression | exclusive-or-expression
7714 logical-and-expression:
7715 inclusive-or-expression
7716 logical-and-expression && inclusive-or-expression
7718 logical-or-expression:
7719 logical-and-expression
7720 logical-or-expression || logical-and-expression
7722 All these are implemented with a single function like:
7725 simple-cast-expression
7726 binary-expression <token> binary-expression
7728 CAST_P is true if this expression is the target of a cast.
7730 The binops_by_token map is used to get the tree codes for each <token> type.
7731 binary-expressions are associated according to a precedence table. */
7733 #define TOKEN_PRECEDENCE(token) \
7734 (((token->type == CPP_GREATER \
7735 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7736 && !parser->greater_than_is_operator_p) \
7737 ? PREC_NOT_OPERATOR \
7738 : binops_by_token[token->type].prec)
7741 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7742 bool no_toplevel_fold_p
,
7744 enum cp_parser_prec prec
,
7747 cp_parser_expression_stack stack
;
7748 cp_parser_expression_stack_entry
*sp
= &stack
[0];
7749 cp_parser_expression_stack_entry current
;
7752 enum tree_code rhs_type
;
7753 enum cp_parser_prec new_prec
, lookahead_prec
;
7756 /* Parse the first expression. */
7757 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
7758 cast_p
, decltype_p
, pidk
);
7759 current
.lhs_type
= ERROR_MARK
;
7760 current
.prec
= prec
;
7762 if (cp_parser_error_occurred (parser
))
7763 return error_mark_node
;
7767 /* Get an operator token. */
7768 token
= cp_lexer_peek_token (parser
->lexer
);
7770 if (warn_cxx0x_compat
7771 && token
->type
== CPP_RSHIFT
7772 && !parser
->greater_than_is_operator_p
)
7774 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
7775 "%<>>%> operator is treated"
7776 " as two right angle brackets in C++11"))
7777 inform (token
->location
,
7778 "suggest parentheses around %<>>%> expression");
7781 new_prec
= TOKEN_PRECEDENCE (token
);
7783 /* Popping an entry off the stack means we completed a subexpression:
7784 - either we found a token which is not an operator (`>' where it is not
7785 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7786 will happen repeatedly;
7787 - or, we found an operator which has lower priority. This is the case
7788 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7790 if (new_prec
<= current
.prec
)
7799 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
7800 current
.loc
= token
->location
;
7802 /* We used the operator token. */
7803 cp_lexer_consume_token (parser
->lexer
);
7805 /* For "false && x" or "true || x", x will never be executed;
7806 disable warnings while evaluating it. */
7807 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7808 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
7809 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7810 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
7812 /* Extract another operand. It may be the RHS of this expression
7813 or the LHS of a new, higher priority expression. */
7814 rhs
= cp_parser_simple_cast_expression (parser
);
7815 rhs_type
= ERROR_MARK
;
7817 /* Get another operator token. Look up its precedence to avoid
7818 building a useless (immediately popped) stack entry for common
7819 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7820 token
= cp_lexer_peek_token (parser
->lexer
);
7821 lookahead_prec
= TOKEN_PRECEDENCE (token
);
7822 if (lookahead_prec
> new_prec
)
7824 /* ... and prepare to parse the RHS of the new, higher priority
7825 expression. Since precedence levels on the stack are
7826 monotonically increasing, we do not have to care about
7831 current
.lhs_type
= rhs_type
;
7832 current
.prec
= new_prec
;
7833 new_prec
= lookahead_prec
;
7837 lookahead_prec
= new_prec
;
7838 /* If the stack is not empty, we have parsed into LHS the right side
7839 (`4' in the example above) of an expression we had suspended.
7840 We can use the information on the stack to recover the LHS (`3')
7841 from the stack together with the tree code (`MULT_EXPR'), and
7842 the precedence of the higher level subexpression
7843 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7844 which will be used to actually build the additive expression. */
7846 rhs_type
= current
.lhs_type
;
7851 /* Undo the disabling of warnings done above. */
7852 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7853 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
7854 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7855 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
7858 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7859 ERROR_MARK for everything that is not a binary expression.
7860 This makes warn_about_parentheses miss some warnings that
7861 involve unary operators. For unary expressions we should
7862 pass the correct tree_code unless the unary expression was
7863 surrounded by parentheses.
7865 if (no_toplevel_fold_p
7866 && lookahead_prec
<= current
.prec
7868 current
.lhs
= build2 (current
.tree_type
,
7869 TREE_CODE_CLASS (current
.tree_type
)
7871 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
7874 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
7875 current
.lhs
, current
.lhs_type
,
7876 rhs
, rhs_type
, &overload
,
7877 complain_flags (decltype_p
));
7878 current
.lhs_type
= current
.tree_type
;
7879 if (EXPR_P (current
.lhs
))
7880 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
7882 /* If the binary operator required the use of an overloaded operator,
7883 then this expression cannot be an integral constant-expression.
7884 An overloaded operator can be used even if both operands are
7885 otherwise permissible in an integral constant-expression if at
7886 least one of the operands is of enumeration type. */
7889 && cp_parser_non_integral_constant_expression (parser
,
7891 return error_mark_node
;
7898 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7899 bool no_toplevel_fold_p
,
7900 enum cp_parser_prec prec
,
7903 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
7904 /*decltype*/false, prec
, pidk
);
7907 /* Parse the `? expression : assignment-expression' part of a
7908 conditional-expression. The LOGICAL_OR_EXPR is the
7909 logical-or-expression that started the conditional-expression.
7910 Returns a representation of the entire conditional-expression.
7912 This routine is used by cp_parser_assignment_expression.
7914 ? expression : assignment-expression
7918 ? : assignment-expression */
7921 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
7924 tree assignment_expr
;
7925 struct cp_token
*token
;
7926 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7928 /* Consume the `?' token. */
7929 cp_lexer_consume_token (parser
->lexer
);
7930 token
= cp_lexer_peek_token (parser
->lexer
);
7931 if (cp_parser_allow_gnu_extensions_p (parser
)
7932 && token
->type
== CPP_COLON
)
7934 pedwarn (token
->location
, OPT_Wpedantic
,
7935 "ISO C++ does not allow ?: with omitted middle operand");
7936 /* Implicit true clause. */
7938 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
7939 warn_for_omitted_condop (token
->location
, logical_or_expr
);
7943 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
7944 parser
->colon_corrects_to_scope_p
= false;
7945 /* Parse the expression. */
7946 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
7947 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7948 c_inhibit_evaluation_warnings
+=
7949 ((logical_or_expr
== truthvalue_true_node
)
7950 - (logical_or_expr
== truthvalue_false_node
));
7951 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
7954 /* The next token should be a `:'. */
7955 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
7956 /* Parse the assignment-expression. */
7957 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7958 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
7960 /* Build the conditional-expression. */
7961 return build_x_conditional_expr (loc
, logical_or_expr
,
7964 tf_warning_or_error
);
7967 /* Parse an assignment-expression.
7969 assignment-expression:
7970 conditional-expression
7971 logical-or-expression assignment-operator assignment_expression
7974 CAST_P is true if this expression is the target of a cast.
7975 DECLTYPE_P is true if this expression is the operand of decltype.
7977 Returns a representation for the expression. */
7980 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
7981 bool decltype_p
, cp_id_kind
* pidk
)
7985 /* If the next token is the `throw' keyword, then we're looking at
7986 a throw-expression. */
7987 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
7988 expr
= cp_parser_throw_expression (parser
);
7989 /* Otherwise, it must be that we are looking at a
7990 logical-or-expression. */
7993 /* Parse the binary expressions (logical-or-expression). */
7994 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
7996 PREC_NOT_OPERATOR
, pidk
);
7997 /* If the next token is a `?' then we're actually looking at a
7998 conditional-expression. */
7999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8000 return cp_parser_question_colon_clause (parser
, expr
);
8003 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8005 /* If it's an assignment-operator, we're using the second
8007 enum tree_code assignment_operator
8008 = cp_parser_assignment_operator_opt (parser
);
8009 if (assignment_operator
!= ERROR_MARK
)
8011 bool non_constant_p
;
8012 location_t saved_input_location
;
8014 /* Parse the right-hand side of the assignment. */
8015 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8017 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8018 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8020 /* An assignment may not appear in a
8021 constant-expression. */
8022 if (cp_parser_non_integral_constant_expression (parser
,
8024 return error_mark_node
;
8025 /* Build the assignment expression. Its default
8026 location is the location of the '=' token. */
8027 saved_input_location
= input_location
;
8028 input_location
= loc
;
8029 expr
= build_x_modify_expr (loc
, expr
,
8030 assignment_operator
,
8032 complain_flags (decltype_p
));
8033 input_location
= saved_input_location
;
8042 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
8045 return cp_parser_assignment_expression (parser
, cast_p
,
8046 /*decltype*/false, pidk
);
8049 /* Parse an (optional) assignment-operator.
8051 assignment-operator: one of
8052 = *= /= %= += -= >>= <<= &= ^= |=
8056 assignment-operator: one of
8059 If the next token is an assignment operator, the corresponding tree
8060 code is returned, and the token is consumed. For example, for
8061 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8062 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8063 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8064 operator, ERROR_MARK is returned. */
8066 static enum tree_code
8067 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8072 /* Peek at the next token. */
8073 token
= cp_lexer_peek_token (parser
->lexer
);
8075 switch (token
->type
)
8086 op
= TRUNC_DIV_EXPR
;
8090 op
= TRUNC_MOD_EXPR
;
8122 /* Nothing else is an assignment operator. */
8126 /* If it was an assignment operator, consume it. */
8127 if (op
!= ERROR_MARK
)
8128 cp_lexer_consume_token (parser
->lexer
);
8133 /* Parse an expression.
8136 assignment-expression
8137 expression , assignment-expression
8139 CAST_P is true if this expression is the target of a cast.
8140 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8141 except possibly parenthesized or on the RHS of a comma (N3276).
8143 Returns a representation of the expression. */
8146 cp_parser_expression (cp_parser
* parser
, bool cast_p
, bool decltype_p
,
8149 tree expression
= NULL_TREE
;
8150 location_t loc
= UNKNOWN_LOCATION
;
8154 tree assignment_expression
;
8156 /* Parse the next assignment-expression. */
8157 assignment_expression
8158 = cp_parser_assignment_expression (parser
, cast_p
, decltype_p
, pidk
);
8160 /* We don't create a temporary for a call that is the immediate operand
8161 of decltype or on the RHS of a comma. But when we see a comma, we
8162 need to create a temporary for a call on the LHS. */
8163 if (decltype_p
&& !processing_template_decl
8164 && TREE_CODE (assignment_expression
) == CALL_EXPR
8165 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8166 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8167 assignment_expression
8168 = build_cplus_new (TREE_TYPE (assignment_expression
),
8169 assignment_expression
, tf_warning_or_error
);
8171 /* If this is the first assignment-expression, we can just
8174 expression
= assignment_expression
;
8176 expression
= build_x_compound_expr (loc
, expression
,
8177 assignment_expression
,
8178 complain_flags (decltype_p
));
8179 /* If the next token is not a comma, then we are done with the
8181 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8183 /* Consume the `,'. */
8184 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8185 cp_lexer_consume_token (parser
->lexer
);
8186 /* A comma operator cannot appear in a constant-expression. */
8187 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8188 expression
= error_mark_node
;
8195 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
8197 return cp_parser_expression (parser
, cast_p
, /*decltype*/false, pidk
);
8200 /* Parse a constant-expression.
8202 constant-expression:
8203 conditional-expression
8205 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8206 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8207 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8208 is false, NON_CONSTANT_P should be NULL. */
8211 cp_parser_constant_expression (cp_parser
* parser
,
8212 bool allow_non_constant_p
,
8213 bool *non_constant_p
)
8215 bool saved_integral_constant_expression_p
;
8216 bool saved_allow_non_integral_constant_expression_p
;
8217 bool saved_non_integral_constant_expression_p
;
8220 /* It might seem that we could simply parse the
8221 conditional-expression, and then check to see if it were
8222 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8223 one that the compiler can figure out is constant, possibly after
8224 doing some simplifications or optimizations. The standard has a
8225 precise definition of constant-expression, and we must honor
8226 that, even though it is somewhat more restrictive.
8232 is not a legal declaration, because `(2, 3)' is not a
8233 constant-expression. The `,' operator is forbidden in a
8234 constant-expression. However, GCC's constant-folding machinery
8235 will fold this operation to an INTEGER_CST for `3'. */
8237 /* Save the old settings. */
8238 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8239 saved_allow_non_integral_constant_expression_p
8240 = parser
->allow_non_integral_constant_expression_p
;
8241 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8242 /* We are now parsing a constant-expression. */
8243 parser
->integral_constant_expression_p
= true;
8244 parser
->allow_non_integral_constant_expression_p
8245 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8246 parser
->non_integral_constant_expression_p
= false;
8247 /* Although the grammar says "conditional-expression", we parse an
8248 "assignment-expression", which also permits "throw-expression"
8249 and the use of assignment operators. In the case that
8250 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8251 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8252 actually essential that we look for an assignment-expression.
8253 For example, cp_parser_initializer_clauses uses this function to
8254 determine whether a particular assignment-expression is in fact
8256 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
8257 /* Restore the old settings. */
8258 parser
->integral_constant_expression_p
8259 = saved_integral_constant_expression_p
;
8260 parser
->allow_non_integral_constant_expression_p
8261 = saved_allow_non_integral_constant_expression_p
;
8262 if (cxx_dialect
>= cxx11
)
8264 /* Require an rvalue constant expression here; that's what our
8265 callers expect. Reference constant expressions are handled
8266 separately in e.g. cp_parser_template_argument. */
8267 bool is_const
= potential_rvalue_constant_expression (expression
);
8268 parser
->non_integral_constant_expression_p
= !is_const
;
8269 if (!is_const
&& !allow_non_constant_p
)
8270 require_potential_rvalue_constant_expression (expression
);
8272 if (allow_non_constant_p
)
8273 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8274 parser
->non_integral_constant_expression_p
8275 = saved_non_integral_constant_expression_p
;
8280 /* Parse __builtin_offsetof.
8282 offsetof-expression:
8283 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8285 offsetof-member-designator:
8287 | offsetof-member-designator "." id-expression
8288 | offsetof-member-designator "[" expression "]"
8289 | offsetof-member-designator "->" id-expression */
8292 cp_parser_builtin_offsetof (cp_parser
*parser
)
8294 int save_ice_p
, save_non_ice_p
;
8299 /* We're about to accept non-integral-constant things, but will
8300 definitely yield an integral constant expression. Save and
8301 restore these values around our local parsing. */
8302 save_ice_p
= parser
->integral_constant_expression_p
;
8303 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8305 /* Consume the "__builtin_offsetof" token. */
8306 cp_lexer_consume_token (parser
->lexer
);
8307 /* Consume the opening `('. */
8308 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8309 /* Parse the type-id. */
8310 type
= cp_parser_type_id (parser
);
8311 /* Look for the `,'. */
8312 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8313 token
= cp_lexer_peek_token (parser
->lexer
);
8315 /* Build the (type *)null that begins the traditional offsetof macro. */
8316 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8317 tf_warning_or_error
);
8319 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8320 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8321 true, &dummy
, token
->location
);
8324 token
= cp_lexer_peek_token (parser
->lexer
);
8325 switch (token
->type
)
8327 case CPP_OPEN_SQUARE
:
8328 /* offsetof-member-designator "[" expression "]" */
8329 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8334 /* offsetof-member-designator "->" identifier */
8335 expr
= grok_array_decl (token
->location
, expr
,
8336 integer_zero_node
, false);
8340 /* offsetof-member-designator "." identifier */
8341 cp_lexer_consume_token (parser
->lexer
);
8342 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8347 case CPP_CLOSE_PAREN
:
8348 /* Consume the ")" token. */
8349 cp_lexer_consume_token (parser
->lexer
);
8353 /* Error. We know the following require will fail, but
8354 that gives the proper error message. */
8355 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8356 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8357 expr
= error_mark_node
;
8363 /* If we're processing a template, we can't finish the semantics yet.
8364 Otherwise we can fold the entire expression now. */
8365 if (processing_template_decl
)
8366 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
8368 expr
= finish_offsetof (expr
);
8371 parser
->integral_constant_expression_p
= save_ice_p
;
8372 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8377 /* Parse a trait expression.
8379 Returns a representation of the expression, the underlying type
8380 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8383 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8386 tree type1
, type2
= NULL_TREE
;
8387 bool binary
= false;
8388 cp_decl_specifier_seq decl_specs
;
8392 case RID_HAS_NOTHROW_ASSIGN
:
8393 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8395 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8396 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8398 case RID_HAS_NOTHROW_COPY
:
8399 kind
= CPTK_HAS_NOTHROW_COPY
;
8401 case RID_HAS_TRIVIAL_ASSIGN
:
8402 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8404 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8405 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8407 case RID_HAS_TRIVIAL_COPY
:
8408 kind
= CPTK_HAS_TRIVIAL_COPY
;
8410 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8411 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8413 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8414 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8416 case RID_IS_ABSTRACT
:
8417 kind
= CPTK_IS_ABSTRACT
;
8419 case RID_IS_BASE_OF
:
8420 kind
= CPTK_IS_BASE_OF
;
8424 kind
= CPTK_IS_CLASS
;
8426 case RID_IS_CONVERTIBLE_TO
:
8427 kind
= CPTK_IS_CONVERTIBLE_TO
;
8431 kind
= CPTK_IS_EMPTY
;
8434 kind
= CPTK_IS_ENUM
;
8437 kind
= CPTK_IS_FINAL
;
8439 case RID_IS_LITERAL_TYPE
:
8440 kind
= CPTK_IS_LITERAL_TYPE
;
8445 case RID_IS_POLYMORPHIC
:
8446 kind
= CPTK_IS_POLYMORPHIC
;
8448 case RID_IS_STD_LAYOUT
:
8449 kind
= CPTK_IS_STD_LAYOUT
;
8451 case RID_IS_TRIVIAL
:
8452 kind
= CPTK_IS_TRIVIAL
;
8455 kind
= CPTK_IS_UNION
;
8457 case RID_UNDERLYING_TYPE
:
8458 kind
= CPTK_UNDERLYING_TYPE
;
8463 case RID_DIRECT_BASES
:
8464 kind
= CPTK_DIRECT_BASES
;
8470 /* Consume the token. */
8471 cp_lexer_consume_token (parser
->lexer
);
8473 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8475 type1
= cp_parser_type_id (parser
);
8477 if (type1
== error_mark_node
)
8478 return error_mark_node
;
8480 /* Build a trivial decl-specifier-seq. */
8481 clear_decl_specs (&decl_specs
);
8482 decl_specs
.type
= type1
;
8484 /* Call grokdeclarator to figure out what type this is. */
8485 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8486 /*initialized=*/0, /*attrlist=*/NULL
);
8490 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8492 type2
= cp_parser_type_id (parser
);
8494 if (type2
== error_mark_node
)
8495 return error_mark_node
;
8497 /* Build a trivial decl-specifier-seq. */
8498 clear_decl_specs (&decl_specs
);
8499 decl_specs
.type
= type2
;
8501 /* Call grokdeclarator to figure out what type this is. */
8502 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8503 /*initialized=*/0, /*attrlist=*/NULL
);
8506 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8508 /* Complete the trait expression, which may mean either processing
8509 the trait expr now or saving it for template instantiation. */
8512 case CPTK_UNDERLYING_TYPE
:
8513 return finish_underlying_type (type1
);
8515 return finish_bases (type1
, false);
8516 case CPTK_DIRECT_BASES
:
8517 return finish_bases (type1
, true);
8519 return finish_trait_expr (kind
, type1
, type2
);
8523 /* Lambdas that appear in variable initializer or default argument scope
8524 get that in their mangling, so we need to record it. We might as well
8525 use the count for function and namespace scopes as well. */
8526 static GTY(()) tree lambda_scope
;
8527 static GTY(()) int lambda_count
;
8528 typedef struct GTY(()) tree_int
8533 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8536 start_lambda_scope (tree decl
)
8540 /* Once we're inside a function, we ignore other scopes and just push
8541 the function again so that popping works properly. */
8542 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8543 decl
= current_function_decl
;
8544 ti
.t
= lambda_scope
;
8545 ti
.i
= lambda_count
;
8546 vec_safe_push (lambda_scope_stack
, ti
);
8547 if (lambda_scope
!= decl
)
8549 /* Don't reset the count if we're still in the same function. */
8550 lambda_scope
= decl
;
8556 record_lambda_scope (tree lambda
)
8558 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8559 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8563 finish_lambda_scope (void)
8565 tree_int
*p
= &lambda_scope_stack
->last ();
8566 if (lambda_scope
!= p
->t
)
8568 lambda_scope
= p
->t
;
8569 lambda_count
= p
->i
;
8571 lambda_scope_stack
->pop ();
8574 /* Parse a lambda expression.
8577 lambda-introducer lambda-declarator [opt] compound-statement
8579 Returns a representation of the expression. */
8582 cp_parser_lambda_expression (cp_parser
* parser
)
8584 tree lambda_expr
= build_lambda_expr ();
8588 LAMBDA_EXPR_LOCATION (lambda_expr
)
8589 = cp_lexer_peek_token (parser
->lexer
)->location
;
8591 if (cp_unevaluated_operand
)
8592 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8593 "lambda-expression in unevaluated context");
8595 /* We may be in the middle of deferred access check. Disable
8597 push_deferring_access_checks (dk_no_deferred
);
8599 cp_parser_lambda_introducer (parser
, lambda_expr
);
8601 type
= begin_lambda_type (lambda_expr
);
8602 if (type
== error_mark_node
)
8603 return error_mark_node
;
8605 record_lambda_scope (lambda_expr
);
8607 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8608 determine_visibility (TYPE_NAME (type
));
8610 /* Now that we've started the type, add the capture fields for any
8611 explicit captures. */
8612 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8615 /* Inside the class, surrounding template-parameter-lists do not apply. */
8616 unsigned int saved_num_template_parameter_lists
8617 = parser
->num_template_parameter_lists
;
8618 unsigned char in_statement
= parser
->in_statement
;
8619 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
8620 bool fully_implicit_function_template_p
= parser
->fully_implicit_function_template_p
;
8622 parser
->num_template_parameter_lists
= 0;
8623 parser
->in_statement
= 0;
8624 parser
->in_switch_statement_p
= false;
8625 parser
->fully_implicit_function_template_p
= false;
8627 /* By virtue of defining a local class, a lambda expression has access to
8628 the private variables of enclosing classes. */
8630 ok
= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
8633 cp_parser_lambda_body (parser
, lambda_expr
);
8634 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8635 cp_parser_skip_to_end_of_block_or_statement (parser
);
8637 /* The capture list was built up in reverse order; fix that now. */
8638 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
8639 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8642 maybe_add_lambda_conv_op (type
);
8644 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
8646 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
8647 parser
->in_statement
= in_statement
;
8648 parser
->in_switch_statement_p
= in_switch_statement_p
;
8649 parser
->fully_implicit_function_template_p
= fully_implicit_function_template_p
;
8652 pop_deferring_access_checks ();
8654 /* This field is only used during parsing of the lambda. */
8655 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
8657 /* This lambda shouldn't have any proxies left at this point. */
8658 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
8659 /* And now that we're done, push proxies for an enclosing lambda. */
8660 insert_pending_capture_proxies ();
8663 return build_lambda_object (lambda_expr
);
8665 return error_mark_node
;
8668 /* Parse the beginning of a lambda expression.
8671 [ lambda-capture [opt] ]
8673 LAMBDA_EXPR is the current representation of the lambda expression. */
8676 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
8678 /* Need commas after the first capture. */
8681 /* Eat the leading `['. */
8682 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8684 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
8686 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
8687 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
8688 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8689 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
8691 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
8693 cp_lexer_consume_token (parser
->lexer
);
8697 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
8699 cp_token
* capture_token
;
8701 tree capture_init_expr
;
8702 cp_id_kind idk
= CP_ID_KIND_NONE
;
8703 bool explicit_init_p
= false;
8705 enum capture_kind_type
8710 enum capture_kind_type capture_kind
= BY_COPY
;
8712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
8714 error ("expected end of capture-list");
8721 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8723 /* Possibly capture `this'. */
8724 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
8726 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8727 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
8728 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
8729 "with by-copy capture default");
8730 cp_lexer_consume_token (parser
->lexer
);
8731 add_capture (lambda_expr
,
8732 /*id=*/this_identifier
,
8733 /*initializer=*/finish_this_expr(),
8734 /*by_reference_p=*/false,
8739 /* Remember whether we want to capture as a reference or not. */
8740 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
8742 capture_kind
= BY_REFERENCE
;
8743 cp_lexer_consume_token (parser
->lexer
);
8746 /* Get the identifier. */
8747 capture_token
= cp_lexer_peek_token (parser
->lexer
);
8748 capture_id
= cp_parser_identifier (parser
);
8750 if (capture_id
== error_mark_node
)
8751 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8752 delimiters, but I modified this to stop on unnested ']' as well. It
8753 was already changed to stop on unnested '}', so the
8754 "closing_parenthesis" name is no more misleading with my change. */
8756 cp_parser_skip_to_closing_parenthesis (parser
,
8757 /*recovering=*/true,
8759 /*consume_paren=*/true);
8763 /* Find the initializer for this capture. */
8764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
8765 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
8766 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8768 bool direct
, non_constant
;
8769 /* An explicit initializer exists. */
8770 if (cxx_dialect
< cxx1y
)
8771 pedwarn (input_location
, 0,
8772 "lambda capture initializers "
8773 "only available with -std=c++1y or -std=gnu++1y");
8774 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
8776 explicit_init_p
= true;
8780 const char* error_msg
;
8782 /* Turn the identifier into an id-expression. */
8784 = cp_parser_lookup_name_simple (parser
, capture_id
,
8785 capture_token
->location
);
8787 if (capture_init_expr
== error_mark_node
)
8789 unqualified_name_lookup_error (capture_id
);
8792 else if (DECL_P (capture_init_expr
)
8793 && (!VAR_P (capture_init_expr
)
8794 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
8796 error_at (capture_token
->location
,
8797 "capture of non-variable %qD ",
8799 inform (0, "%q+#D declared here", capture_init_expr
);
8802 if (VAR_P (capture_init_expr
)
8803 && decl_storage_duration (capture_init_expr
) != dk_auto
)
8805 pedwarn (capture_token
->location
, 0, "capture of variable "
8806 "%qD with non-automatic storage duration",
8808 inform (0, "%q+#D declared here", capture_init_expr
);
8813 = finish_id_expression
8818 /*integral_constant_expression_p=*/false,
8819 /*allow_non_integral_constant_expression_p=*/false,
8820 /*non_integral_constant_expression_p=*/NULL
,
8821 /*template_p=*/false,
8823 /*address_p=*/false,
8824 /*template_arg_p=*/false,
8826 capture_token
->location
);
8828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8830 cp_lexer_consume_token (parser
->lexer
);
8831 capture_init_expr
= make_pack_expansion (capture_init_expr
);
8834 check_for_bare_parameter_packs (capture_init_expr
);
8837 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
8838 && !explicit_init_p
)
8840 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
8841 && capture_kind
== BY_COPY
)
8842 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
8843 "of %qD redundant with by-copy capture default",
8845 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
8846 && capture_kind
== BY_REFERENCE
)
8847 pedwarn (capture_token
->location
, 0, "explicit by-reference "
8848 "capture of %qD redundant with by-reference capture "
8849 "default", capture_id
);
8852 add_capture (lambda_expr
,
8855 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
8859 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8862 /* Parse the (optional) middle of a lambda expression.
8865 < template-parameter-list [opt] >
8866 ( parameter-declaration-clause [opt] )
8867 attribute-specifier [opt]
8869 exception-specification [opt]
8870 lambda-return-type-clause [opt]
8872 LAMBDA_EXPR is the current representation of the lambda expression. */
8875 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
8877 /* 5.1.1.4 of the standard says:
8878 If a lambda-expression does not include a lambda-declarator, it is as if
8879 the lambda-declarator were ().
8880 This means an empty parameter list, no attributes, and no exception
8882 tree param_list
= void_list_node
;
8883 tree attributes
= NULL_TREE
;
8884 tree exception_spec
= NULL_TREE
;
8885 tree template_param_list
= NULL_TREE
;
8887 /* The template-parameter-list is optional, but must begin with
8888 an opening angle if present. */
8889 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
8891 if (cxx_dialect
< cxx1y
)
8892 pedwarn (parser
->lexer
->next_token
->location
, 0,
8893 "lambda templates are only available with "
8894 "-std=c++1y or -std=gnu++1y");
8896 cp_lexer_consume_token (parser
->lexer
);
8898 template_param_list
= cp_parser_template_parameter_list (parser
);
8900 cp_parser_skip_to_end_of_template_parameter_list (parser
);
8902 /* We just processed one more parameter list. */
8903 ++parser
->num_template_parameter_lists
;
8906 /* The parameter-declaration-clause is optional (unless
8907 template-parameter-list was given), but must begin with an
8908 opening parenthesis if present. */
8909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8911 cp_lexer_consume_token (parser
->lexer
);
8913 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
8915 /* Parse parameters. */
8916 param_list
= cp_parser_parameter_declaration_clause (parser
);
8918 /* Default arguments shall not be specified in the
8919 parameter-declaration-clause of a lambda-declarator. */
8920 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
8921 if (TREE_PURPOSE (t
))
8922 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
8923 "default argument specified for lambda parameter");
8925 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8927 attributes
= cp_parser_attributes_opt (parser
);
8929 /* Parse optional `mutable' keyword. */
8930 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
8932 cp_lexer_consume_token (parser
->lexer
);
8933 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
8936 /* Parse optional exception specification. */
8937 exception_spec
= cp_parser_exception_specification_opt (parser
);
8939 /* Parse optional trailing return type. */
8940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
8942 cp_lexer_consume_token (parser
->lexer
);
8943 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
8944 = cp_parser_trailing_type_id (parser
);
8947 /* The function parameters must be in scope all the way until after the
8948 trailing-return-type in case of decltype. */
8949 pop_bindings_and_leave_scope ();
8951 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
8952 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8954 /* Create the function call operator.
8956 Messing with declarators like this is no uglier than building up the
8957 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8960 cp_decl_specifier_seq return_type_specs
;
8961 cp_declarator
* declarator
;
8966 clear_decl_specs (&return_type_specs
);
8967 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
8968 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
8970 /* Maybe we will deduce the return type later. */
8971 return_type_specs
.type
= make_auto ();
8973 p
= obstack_alloc (&declarator_obstack
, 0);
8975 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
8978 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
8979 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
8980 declarator
= make_call_declarator (declarator
, param_list
, quals
,
8981 VIRT_SPEC_UNSPECIFIED
,
8984 /*late_return_type=*/NULL_TREE
);
8985 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
8987 fco
= grokmethod (&return_type_specs
,
8990 if (fco
!= error_mark_node
)
8992 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
8993 DECL_ARTIFICIAL (fco
) = 1;
8994 /* Give the object parameter a different name. */
8995 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
8996 if (template_param_list
)
8998 fco
= finish_member_template_decl (fco
);
8999 finish_template_decl (template_param_list
);
9000 --parser
->num_template_parameter_lists
;
9002 else if (parser
->fully_implicit_function_template_p
)
9003 fco
= finish_fully_implicit_template (parser
, fco
);
9006 finish_member_declaration (fco
);
9008 obstack_free (&declarator_obstack
, p
);
9010 return (fco
!= error_mark_node
);
9014 /* Parse the body of a lambda expression, which is simply
9018 but which requires special handling.
9019 LAMBDA_EXPR is the current representation of the lambda expression. */
9022 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9024 bool nested
= (current_function_decl
!= NULL_TREE
);
9025 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9027 push_function_context ();
9029 /* Still increment function_depth so that we don't GC in the
9030 middle of an expression. */
9032 /* Clear this in case we're in the middle of a default argument. */
9033 parser
->local_variables_forbidden_p
= false;
9035 /* Finish the function call operator
9037 + late_parsing_for_member
9038 + function_definition_after_declarator
9039 + ctor_initializer_opt_and_function_body */
9041 tree fco
= lambda_function (lambda_expr
);
9047 /* Let the front end know that we are going to be defining this
9049 start_preparsed_function (fco
,
9051 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9053 start_lambda_scope (fco
);
9054 body
= begin_function_body ();
9056 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9059 /* Push the proxies for any explicit captures. */
9060 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9061 cap
= TREE_CHAIN (cap
))
9062 build_capture_proxy (TREE_PURPOSE (cap
));
9064 compound_stmt
= begin_compound_stmt (0);
9066 /* 5.1.1.4 of the standard says:
9067 If a lambda-expression does not include a trailing-return-type, it
9068 is as if the trailing-return-type denotes the following type:
9069 * if the compound-statement is of the form
9070 { return attribute-specifier [opt] expression ; }
9071 the type of the returned expression after lvalue-to-rvalue
9072 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9073 (_conv.array_ 4.2), and function-to-pointer conversion
9075 * otherwise, void. */
9077 /* In a lambda that has neither a lambda-return-type-clause
9078 nor a deducible form, errors should be reported for return statements
9079 in the body. Since we used void as the placeholder return type, parsing
9080 the body as usual will give such desired behavior. */
9081 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9082 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9083 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9085 tree expr
= NULL_TREE
;
9086 cp_id_kind idk
= CP_ID_KIND_NONE
;
9088 /* Parse tentatively in case there's more after the initial return
9090 cp_parser_parse_tentatively (parser
);
9092 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9094 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
9096 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9097 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9099 if (cp_parser_parse_definitely (parser
))
9101 if (!processing_template_decl
)
9102 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9104 /* Will get error here if type not deduced yet. */
9105 finish_return_stmt (expr
);
9113 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9114 cp_parser_label_declaration (parser
);
9115 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9116 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9119 finish_compound_stmt (compound_stmt
);
9122 finish_function_body (body
);
9123 finish_lambda_scope ();
9125 /* Finish the function and generate code for it if necessary. */
9126 tree fn
= finish_function (/*inline*/2);
9128 /* Only expand if the call op is not a template. */
9129 if (!DECL_TEMPLATE_INFO (fco
))
9130 expand_or_defer_fn (fn
);
9133 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9135 pop_function_context();
9140 /* Statements [gram.stmt.stmt] */
9142 /* Parse a statement.
9146 expression-statement
9151 declaration-statement
9158 attribute-specifier-seq (opt) expression-statement
9159 attribute-specifier-seq (opt) compound-statement
9160 attribute-specifier-seq (opt) selection-statement
9161 attribute-specifier-seq (opt) iteration-statement
9162 attribute-specifier-seq (opt) jump-statement
9163 declaration-statement
9164 attribute-specifier-seq (opt) try-block
9171 IN_COMPOUND is true when the statement is nested inside a
9172 cp_parser_compound_statement; this matters for certain pragmas.
9174 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9175 is a (possibly labeled) if statement which is not enclosed in braces
9176 and has an else clause. This is used to implement -Wparentheses. */
9179 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9180 bool in_compound
, bool *if_p
)
9182 tree statement
, std_attrs
= NULL_TREE
;
9184 location_t statement_location
, attrs_location
;
9189 /* There is no statement yet. */
9190 statement
= NULL_TREE
;
9192 cp_lexer_save_tokens (parser
->lexer
);
9193 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9194 if (c_dialect_objc ())
9195 /* In obj-c++, seeing '[[' might be the either the beginning of
9196 c++11 attributes, or a nested objc-message-expression. So
9197 let's parse the c++11 attributes tentatively. */
9198 cp_parser_parse_tentatively (parser
);
9199 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9200 if (c_dialect_objc ())
9202 if (!cp_parser_parse_definitely (parser
))
9203 std_attrs
= NULL_TREE
;
9206 /* Peek at the next token. */
9207 token
= cp_lexer_peek_token (parser
->lexer
);
9208 /* Remember the location of the first token in the statement. */
9209 statement_location
= token
->location
;
9210 /* If this is a keyword, then that will often determine what kind of
9211 statement we have. */
9212 if (token
->type
== CPP_KEYWORD
)
9214 enum rid keyword
= token
->keyword
;
9220 /* Looks like a labeled-statement with a case label.
9221 Parse the label, and then use tail recursion to parse
9223 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9228 statement
= cp_parser_selection_statement (parser
, if_p
);
9234 statement
= cp_parser_iteration_statement (parser
, false);
9241 statement
= cp_parser_jump_statement (parser
);
9244 /* Objective-C++ exception-handling constructs. */
9247 case RID_AT_FINALLY
:
9248 case RID_AT_SYNCHRONIZED
:
9250 statement
= cp_parser_objc_statement (parser
);
9254 statement
= cp_parser_try_block (parser
);
9258 /* This must be a namespace alias definition. */
9259 cp_parser_declaration_statement (parser
);
9262 case RID_TRANSACTION_ATOMIC
:
9263 case RID_TRANSACTION_RELAXED
:
9264 statement
= cp_parser_transaction (parser
, keyword
);
9266 case RID_TRANSACTION_CANCEL
:
9267 statement
= cp_parser_transaction_cancel (parser
);
9271 /* It might be a keyword like `int' that can start a
9272 declaration-statement. */
9276 else if (token
->type
== CPP_NAME
)
9278 /* If the next token is a `:', then we are looking at a
9279 labeled-statement. */
9280 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9281 if (token
->type
== CPP_COLON
)
9283 /* Looks like a labeled-statement with an ordinary label.
9284 Parse the label, and then use tail recursion to parse
9287 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9291 /* Anything that starts with a `{' must be a compound-statement. */
9292 else if (token
->type
== CPP_OPEN_BRACE
)
9293 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9294 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9295 a statement all its own. */
9296 else if (token
->type
== CPP_PRAGMA
)
9298 /* Only certain OpenMP pragmas are attached to statements, and thus
9299 are considered statements themselves. All others are not. In
9300 the context of a compound, accept the pragma as a "statement" and
9301 return so that we can check for a close brace. Otherwise we
9302 require a real statement and must go back and read one. */
9304 cp_parser_pragma (parser
, pragma_compound
);
9305 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9309 else if (token
->type
== CPP_EOF
)
9311 cp_parser_error (parser
, "expected statement");
9315 /* Everything else must be a declaration-statement or an
9316 expression-statement. Try for the declaration-statement
9317 first, unless we are looking at a `;', in which case we know that
9318 we have an expression-statement. */
9321 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9323 if (std_attrs
!= NULL_TREE
)
9325 /* Attributes should be parsed as part of the the
9326 declaration, so let's un-parse them. */
9327 cp_lexer_rollback_tokens (parser
->lexer
);
9328 std_attrs
= NULL_TREE
;
9331 cp_parser_parse_tentatively (parser
);
9332 /* Try to parse the declaration-statement. */
9333 cp_parser_declaration_statement (parser
);
9334 /* If that worked, we're done. */
9335 if (cp_parser_parse_definitely (parser
))
9338 /* Look for an expression-statement instead. */
9339 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9342 /* Set the line number for the statement. */
9343 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9344 SET_EXPR_LOCATION (statement
, statement_location
);
9346 /* Note that for now, we don't do anything with c++11 statements
9347 parsed at this level. */
9348 if (std_attrs
!= NULL_TREE
)
9349 warning_at (attrs_location
,
9351 "attributes at the beginning of statement are ignored");
9354 /* Parse the label for a labeled-statement, i.e.
9357 case constant-expression :
9361 case constant-expression ... constant-expression : statement
9363 When a label is parsed without errors, the label is added to the
9364 parse tree by the finish_* functions, so this function doesn't
9365 have to return the label. */
9368 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9371 tree label
= NULL_TREE
;
9372 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9374 /* The next token should be an identifier. */
9375 token
= cp_lexer_peek_token (parser
->lexer
);
9376 if (token
->type
!= CPP_NAME
9377 && token
->type
!= CPP_KEYWORD
)
9379 cp_parser_error (parser
, "expected labeled-statement");
9383 parser
->colon_corrects_to_scope_p
= false;
9384 switch (token
->keyword
)
9391 /* Consume the `case' token. */
9392 cp_lexer_consume_token (parser
->lexer
);
9393 /* Parse the constant-expression. */
9394 expr
= cp_parser_constant_expression (parser
,
9395 /*allow_non_constant_p=*/false,
9398 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9399 if (ellipsis
->type
== CPP_ELLIPSIS
)
9401 /* Consume the `...' token. */
9402 cp_lexer_consume_token (parser
->lexer
);
9404 cp_parser_constant_expression (parser
,
9405 /*allow_non_constant_p=*/false,
9407 /* We don't need to emit warnings here, as the common code
9408 will do this for us. */
9411 expr_hi
= NULL_TREE
;
9413 if (parser
->in_switch_statement_p
)
9414 finish_case_label (token
->location
, expr
, expr_hi
);
9416 error_at (token
->location
,
9417 "case label %qE not within a switch statement",
9423 /* Consume the `default' token. */
9424 cp_lexer_consume_token (parser
->lexer
);
9426 if (parser
->in_switch_statement_p
)
9427 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9429 error_at (token
->location
, "case label not within a switch statement");
9433 /* Anything else must be an ordinary label. */
9434 label
= finish_label_stmt (cp_parser_identifier (parser
));
9438 /* Require the `:' token. */
9439 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9441 /* An ordinary label may optionally be followed by attributes.
9442 However, this is only permitted if the attributes are then
9443 followed by a semicolon. This is because, for backward
9444 compatibility, when parsing
9445 lab: __attribute__ ((unused)) int i;
9446 we want the attribute to attach to "i", not "lab". */
9447 if (label
!= NULL_TREE
9448 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9451 cp_parser_parse_tentatively (parser
);
9452 attrs
= cp_parser_gnu_attributes_opt (parser
);
9453 if (attrs
== NULL_TREE
9454 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9455 cp_parser_abort_tentative_parse (parser
);
9456 else if (!cp_parser_parse_definitely (parser
))
9459 attributes
= chainon (attributes
, attrs
);
9462 if (attributes
!= NULL_TREE
)
9463 cplus_decl_attributes (&label
, attributes
, 0);
9465 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9468 /* Parse an expression-statement.
9470 expression-statement:
9473 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9474 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9475 indicates whether this expression-statement is part of an
9476 expression statement. */
9479 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9481 tree statement
= NULL_TREE
;
9482 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9484 /* If the next token is a ';', then there is no expression
9486 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9488 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9489 if (statement
== error_mark_node
9490 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9492 cp_parser_skip_to_end_of_block_or_statement (parser
);
9493 return error_mark_node
;
9497 /* Give a helpful message for "A<T>::type t;" and the like. */
9498 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9499 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9501 if (TREE_CODE (statement
) == SCOPE_REF
)
9502 error_at (token
->location
, "need %<typename%> before %qE because "
9503 "%qT is a dependent scope",
9504 statement
, TREE_OPERAND (statement
, 0));
9505 else if (is_overloaded_fn (statement
)
9506 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9509 tree fn
= get_first_fn (statement
);
9510 error_at (token
->location
,
9511 "%<%T::%D%> names the constructor, not the type",
9512 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9516 /* Consume the final `;'. */
9517 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9519 if (in_statement_expr
9520 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9521 /* This is the final expression statement of a statement
9523 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9525 statement
= finish_expr_stmt (statement
);
9530 /* Parse a compound-statement.
9533 { statement-seq [opt] }
9538 { label-declaration-seq [opt] statement-seq [opt] }
9540 label-declaration-seq:
9542 label-declaration-seq label-declaration
9544 Returns a tree representing the statement. */
9547 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
9548 bool in_try
, bool function_body
)
9552 /* Consume the `{'. */
9553 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9554 return error_mark_node
;
9555 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
9557 pedwarn (input_location
, OPT_Wpedantic
,
9558 "compound-statement in constexpr function");
9559 /* Begin the compound-statement. */
9560 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
9561 /* If the next keyword is `__label__' we have a label declaration. */
9562 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9563 cp_parser_label_declaration (parser
);
9564 /* Parse an (optional) statement-seq. */
9565 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
9566 /* Finish the compound-statement. */
9567 finish_compound_stmt (compound_stmt
);
9568 /* Consume the `}'. */
9569 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9571 return compound_stmt
;
9574 /* Parse an (optional) statement-seq.
9578 statement-seq [opt] statement */
9581 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
9583 /* Scan statements until there aren't any more. */
9586 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9588 /* If we are looking at a `}', then we have run out of
9589 statements; the same is true if we have reached the end
9590 of file, or have stumbled upon a stray '@end'. */
9591 if (token
->type
== CPP_CLOSE_BRACE
9592 || token
->type
== CPP_EOF
9593 || token
->type
== CPP_PRAGMA_EOL
9594 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
9597 /* If we are in a compound statement and find 'else' then
9598 something went wrong. */
9599 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
9601 if (parser
->in_statement
& IN_IF_STMT
)
9605 token
= cp_lexer_consume_token (parser
->lexer
);
9606 error_at (token
->location
, "%<else%> without a previous %<if%>");
9610 /* Parse the statement. */
9611 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
9615 /* Parse a selection-statement.
9617 selection-statement:
9618 if ( condition ) statement
9619 if ( condition ) statement else statement
9620 switch ( condition ) statement
9622 Returns the new IF_STMT or SWITCH_STMT.
9624 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9625 is a (possibly labeled) if statement which is not enclosed in
9626 braces and has an else clause. This is used to implement
9630 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
9638 /* Peek at the next token. */
9639 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
9641 /* See what kind of keyword it is. */
9642 keyword
= token
->keyword
;
9651 /* Look for the `('. */
9652 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
9654 cp_parser_skip_to_end_of_statement (parser
);
9655 return error_mark_node
;
9658 /* Begin the selection-statement. */
9659 if (keyword
== RID_IF
)
9660 statement
= begin_if_stmt ();
9662 statement
= begin_switch_stmt ();
9664 /* Parse the condition. */
9665 condition
= cp_parser_condition (parser
);
9666 /* Look for the `)'. */
9667 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
9668 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9669 /*consume_paren=*/true);
9671 if (keyword
== RID_IF
)
9674 unsigned char in_statement
;
9676 /* Add the condition. */
9677 finish_if_stmt_cond (condition
, statement
);
9679 /* Parse the then-clause. */
9680 in_statement
= parser
->in_statement
;
9681 parser
->in_statement
|= IN_IF_STMT
;
9682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9684 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9685 add_stmt (build_empty_stmt (loc
));
9686 cp_lexer_consume_token (parser
->lexer
);
9687 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
9688 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
9689 "empty body in an %<if%> statement");
9693 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
9694 parser
->in_statement
= in_statement
;
9696 finish_then_clause (statement
);
9698 /* If the next token is `else', parse the else-clause. */
9699 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9702 /* Consume the `else' keyword. */
9703 cp_lexer_consume_token (parser
->lexer
);
9704 begin_else_clause (statement
);
9705 /* Parse the else-clause. */
9706 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9709 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9711 OPT_Wempty_body
, "suggest braces around "
9712 "empty body in an %<else%> statement");
9713 add_stmt (build_empty_stmt (loc
));
9714 cp_lexer_consume_token (parser
->lexer
);
9717 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9719 finish_else_clause (statement
);
9721 /* If we are currently parsing a then-clause, then
9722 IF_P will not be NULL. We set it to true to
9723 indicate that this if statement has an else clause.
9724 This may trigger the Wparentheses warning below
9725 when we get back up to the parent if statement. */
9731 /* This if statement does not have an else clause. If
9732 NESTED_IF is true, then the then-clause is an if
9733 statement which does have an else clause. We warn
9734 about the potential ambiguity. */
9736 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
9737 "suggest explicit braces to avoid ambiguous"
9741 /* Now we're all done with the if-statement. */
9742 finish_if_stmt (statement
);
9746 bool in_switch_statement_p
;
9747 unsigned char in_statement
;
9749 /* Add the condition. */
9750 finish_switch_cond (condition
, statement
);
9752 /* Parse the body of the switch-statement. */
9753 in_switch_statement_p
= parser
->in_switch_statement_p
;
9754 in_statement
= parser
->in_statement
;
9755 parser
->in_switch_statement_p
= true;
9756 parser
->in_statement
|= IN_SWITCH_STMT
;
9757 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9758 parser
->in_switch_statement_p
= in_switch_statement_p
;
9759 parser
->in_statement
= in_statement
;
9761 /* Now we're all done with the switch-statement. */
9762 finish_switch_stmt (statement
);
9770 cp_parser_error (parser
, "expected selection-statement");
9771 return error_mark_node
;
9775 /* Parse a condition.
9779 type-specifier-seq declarator = initializer-clause
9780 type-specifier-seq declarator braced-init-list
9785 type-specifier-seq declarator asm-specification [opt]
9786 attributes [opt] = assignment-expression
9788 Returns the expression that should be tested. */
9791 cp_parser_condition (cp_parser
* parser
)
9793 cp_decl_specifier_seq type_specifiers
;
9794 const char *saved_message
;
9795 int declares_class_or_enum
;
9797 /* Try the declaration first. */
9798 cp_parser_parse_tentatively (parser
);
9799 /* New types are not allowed in the type-specifier-seq for a
9801 saved_message
= parser
->type_definition_forbidden_message
;
9802 parser
->type_definition_forbidden_message
9803 = G_("types may not be defined in conditions");
9804 /* Parse the type-specifier-seq. */
9805 cp_parser_decl_specifier_seq (parser
,
9806 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
9808 &declares_class_or_enum
);
9809 /* Restore the saved message. */
9810 parser
->type_definition_forbidden_message
= saved_message
;
9811 /* If all is well, we might be looking at a declaration. */
9812 if (!cp_parser_error_occurred (parser
))
9815 tree asm_specification
;
9817 cp_declarator
*declarator
;
9818 tree initializer
= NULL_TREE
;
9820 /* Parse the declarator. */
9821 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9822 /*ctor_dtor_or_conv_p=*/NULL
,
9823 /*parenthesized_p=*/NULL
,
9824 /*member_p=*/false);
9825 /* Parse the attributes. */
9826 attributes
= cp_parser_attributes_opt (parser
);
9827 /* Parse the asm-specification. */
9828 asm_specification
= cp_parser_asm_specification_opt (parser
);
9829 /* If the next token is not an `=' or '{', then we might still be
9830 looking at an expression. For example:
9834 looks like a decl-specifier-seq and a declarator -- but then
9835 there is no `=', so this is an expression. */
9836 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9837 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9838 cp_parser_simulate_error (parser
);
9840 /* If we did see an `=' or '{', then we are looking at a declaration
9842 if (cp_parser_parse_definitely (parser
))
9845 bool non_constant_p
;
9846 bool flags
= LOOKUP_ONLYCONVERTING
;
9848 /* Create the declaration. */
9849 decl
= start_decl (declarator
, &type_specifiers
,
9850 /*initialized_p=*/true,
9851 attributes
, /*prefix_attributes=*/NULL_TREE
,
9854 /* Parse the initializer. */
9855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9857 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
9858 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
9863 /* Consume the `='. */
9864 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
9865 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
9867 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
9868 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9870 /* Process the initializer. */
9871 cp_finish_decl (decl
,
9872 initializer
, !non_constant_p
,
9877 pop_scope (pushed_scope
);
9879 return convert_from_reference (decl
);
9882 /* If we didn't even get past the declarator successfully, we are
9883 definitely not looking at a declaration. */
9885 cp_parser_abort_tentative_parse (parser
);
9887 /* Otherwise, we are looking at an expression. */
9888 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9891 /* Parses a for-statement or range-for-statement until the closing ')',
9895 cp_parser_for (cp_parser
*parser
, bool ivdep
)
9897 tree init
, scope
, decl
;
9900 /* Begin the for-statement. */
9901 scope
= begin_for_scope (&init
);
9903 /* Parse the initialization. */
9904 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
9907 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
9909 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
9913 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
9915 /* Normal for loop */
9916 tree condition
= NULL_TREE
;
9917 tree expression
= NULL_TREE
;
9920 stmt
= begin_for_stmt (scope
, init
);
9921 /* The for-init-statement has already been parsed in
9922 cp_parser_for_init_statement, so no work is needed here. */
9923 finish_for_init_stmt (stmt
);
9925 /* If there's a condition, process it. */
9926 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9927 condition
= cp_parser_condition (parser
);
9930 cp_parser_error (parser
, "missing loop condition in loop with "
9931 "%<GCC ivdep%> pragma");
9932 condition
= error_mark_node
;
9934 finish_for_cond (condition
, stmt
, ivdep
);
9935 /* Look for the `;'. */
9936 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9938 /* If there's an expression, process it. */
9939 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
9940 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9941 finish_for_expr (expression
, stmt
);
9946 /* Tries to parse a range-based for-statement:
9949 decl-specifier-seq declarator : expression
9951 The decl-specifier-seq declarator and the `:' are already parsed by
9952 cp_parser_for_init_statement. If processing_template_decl it returns a
9953 newly created RANGE_FOR_STMT; if not, it is converted to a
9954 regular FOR_STMT. */
9957 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
9960 tree stmt
, range_expr
;
9962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9964 bool expr_non_constant_p
;
9965 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9968 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9970 /* If in template, STMT is converted to a normal for-statement
9971 at instantiation. If not, it is done just ahead. */
9972 if (processing_template_decl
)
9974 if (check_for_bare_parameter_packs (range_expr
))
9975 range_expr
= error_mark_node
;
9976 stmt
= begin_range_for_stmt (scope
, init
);
9978 RANGE_FOR_IVDEP (stmt
) = 1;
9979 finish_range_for_decl (stmt
, range_decl
, range_expr
);
9980 if (!type_dependent_expression_p (range_expr
)
9981 /* do_auto_deduction doesn't mess with template init-lists. */
9982 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
9983 do_range_for_auto_deduction (range_decl
, range_expr
);
9987 stmt
= begin_for_stmt (scope
, init
);
9988 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
9993 /* Subroutine of cp_convert_range_for: given the initializer expression,
9994 builds up the range temporary. */
9997 build_range_temp (tree range_expr
)
9999 tree range_type
, range_temp
;
10001 /* Find out the type deduced by the declaration
10002 `auto &&__range = range_expr'. */
10003 range_type
= cp_build_reference_type (make_auto (), true);
10004 range_type
= do_auto_deduction (range_type
, range_expr
,
10005 type_uses_auto (range_type
));
10007 /* Create the __range variable. */
10008 range_temp
= build_decl (input_location
, VAR_DECL
,
10009 get_identifier ("__for_range"), range_type
);
10010 TREE_USED (range_temp
) = 1;
10011 DECL_ARTIFICIAL (range_temp
) = 1;
10016 /* Used by cp_parser_range_for in template context: we aren't going to
10017 do a full conversion yet, but we still need to resolve auto in the
10018 type of the for-range-declaration if present. This is basically
10019 a shortcut version of cp_convert_range_for. */
10022 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10024 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10027 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10028 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10029 iter_type
= (cp_parser_perform_range_for_lookup
10030 (range_temp
, &begin_dummy
, &end_dummy
));
10033 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10035 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10036 tf_warning_or_error
);
10037 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10038 iter_decl
, auto_node
);
10043 /* Converts a range-based for-statement into a normal
10044 for-statement, as per the definition.
10046 for (RANGE_DECL : RANGE_EXPR)
10049 should be equivalent to:
10052 auto &&__range = RANGE_EXPR;
10053 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10057 RANGE_DECL = *__begin;
10062 If RANGE_EXPR is an array:
10063 BEGIN_EXPR = __range
10064 END_EXPR = __range + ARRAY_SIZE(__range)
10065 Else if RANGE_EXPR has a member 'begin' or 'end':
10066 BEGIN_EXPR = __range.begin()
10067 END_EXPR = __range.end()
10069 BEGIN_EXPR = begin(__range)
10070 END_EXPR = end(__range);
10072 If __range has a member 'begin' but not 'end', or vice versa, we must
10073 still use the second alternative (it will surely fail, however).
10074 When calling begin()/end() in the third alternative we must use
10075 argument dependent lookup, but always considering 'std' as an associated
10079 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10083 tree iter_type
, begin_expr
, end_expr
;
10084 tree condition
, expression
;
10086 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10087 /* If an error happened previously do nothing or else a lot of
10088 unhelpful errors would be issued. */
10089 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10094 if (TREE_CODE (range_expr
) == VAR_DECL
10095 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10096 /* Can't bind a reference to an array of runtime bound. */
10097 range_temp
= range_expr
;
10100 range_temp
= build_range_temp (range_expr
);
10101 pushdecl (range_temp
);
10102 cp_finish_decl (range_temp
, range_expr
,
10103 /*is_constant_init*/false, NULL_TREE
,
10104 LOOKUP_ONLYCONVERTING
);
10105 range_temp
= convert_from_reference (range_temp
);
10107 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10108 &begin_expr
, &end_expr
);
10111 /* The new for initialization statement. */
10112 begin
= build_decl (input_location
, VAR_DECL
,
10113 get_identifier ("__for_begin"), iter_type
);
10114 TREE_USED (begin
) = 1;
10115 DECL_ARTIFICIAL (begin
) = 1;
10117 cp_finish_decl (begin
, begin_expr
,
10118 /*is_constant_init*/false, NULL_TREE
,
10119 LOOKUP_ONLYCONVERTING
);
10121 end
= build_decl (input_location
, VAR_DECL
,
10122 get_identifier ("__for_end"), iter_type
);
10123 TREE_USED (end
) = 1;
10124 DECL_ARTIFICIAL (end
) = 1;
10126 cp_finish_decl (end
, end_expr
,
10127 /*is_constant_init*/false, NULL_TREE
,
10128 LOOKUP_ONLYCONVERTING
);
10130 finish_for_init_stmt (statement
);
10132 /* The new for condition. */
10133 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10136 NULL
, tf_warning_or_error
);
10137 finish_for_cond (condition
, statement
, ivdep
);
10139 /* The new increment expression. */
10140 expression
= finish_unary_op_expr (input_location
,
10141 PREINCREMENT_EXPR
, begin
,
10142 tf_warning_or_error
);
10143 finish_for_expr (expression
, statement
);
10145 /* The declaration is initialized with *__begin inside the loop body. */
10146 cp_finish_decl (range_decl
,
10147 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10148 tf_warning_or_error
),
10149 /*is_constant_init*/false, NULL_TREE
,
10150 LOOKUP_ONLYCONVERTING
);
10155 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10156 We need to solve both at the same time because the method used
10157 depends on the existence of members begin or end.
10158 Returns the type deduced for the iterator expression. */
10161 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10163 if (error_operand_p (range
))
10165 *begin
= *end
= error_mark_node
;
10166 return error_mark_node
;
10169 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10171 error ("range-based %<for%> expression of type %qT "
10172 "has incomplete type", TREE_TYPE (range
));
10173 *begin
= *end
= error_mark_node
;
10174 return error_mark_node
;
10176 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10178 /* If RANGE is an array, we will use pointer arithmetic. */
10180 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10182 array_type_nelts_top (TREE_TYPE (range
)),
10184 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10188 /* If it is not an array, we must do a bit of magic. */
10189 tree id_begin
, id_end
;
10190 tree member_begin
, member_end
;
10192 *begin
= *end
= error_mark_node
;
10194 id_begin
= get_identifier ("begin");
10195 id_end
= get_identifier ("end");
10196 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10197 /*protect=*/2, /*want_type=*/false,
10198 tf_warning_or_error
);
10199 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10200 /*protect=*/2, /*want_type=*/false,
10201 tf_warning_or_error
);
10203 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10205 /* Use the member functions. */
10206 if (member_begin
!= NULL_TREE
)
10207 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10209 error ("range-based %<for%> expression of type %qT has an "
10210 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10212 if (member_end
!= NULL_TREE
)
10213 *end
= cp_parser_range_for_member_function (range
, id_end
);
10215 error ("range-based %<for%> expression of type %qT has a "
10216 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10220 /* Use global functions with ADL. */
10221 vec
<tree
, va_gc
> *vec
;
10222 vec
= make_tree_vector ();
10224 vec_safe_push (vec
, range
);
10226 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10227 /*include_std=*/true,
10228 tf_warning_or_error
);
10229 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10230 tf_warning_or_error
);
10231 member_end
= perform_koenig_lookup (id_end
, vec
,
10232 /*include_std=*/true,
10233 tf_warning_or_error
);
10234 *end
= finish_call_expr (member_end
, &vec
, false, true,
10235 tf_warning_or_error
);
10237 release_tree_vector (vec
);
10240 /* Last common checks. */
10241 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10243 /* If one of the expressions is an error do no more checks. */
10244 *begin
= *end
= error_mark_node
;
10245 return error_mark_node
;
10247 else if (type_dependent_expression_p (*begin
)
10248 || type_dependent_expression_p (*end
))
10249 /* Can happen, when, eg, in a template context, Koenig lookup
10250 can't resolve begin/end (c++/58503). */
10254 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10255 /* The unqualified type of the __begin and __end temporaries should
10256 be the same, as required by the multiple auto declaration. */
10257 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10258 error ("inconsistent begin/end types in range-based %<for%> "
10259 "statement: %qT and %qT",
10260 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10266 /* Helper function for cp_parser_perform_range_for_lookup.
10267 Builds a tree for RANGE.IDENTIFIER(). */
10270 cp_parser_range_for_member_function (tree range
, tree identifier
)
10273 vec
<tree
, va_gc
> *vec
;
10275 member
= finish_class_member_access_expr (range
, identifier
,
10276 false, tf_warning_or_error
);
10277 if (member
== error_mark_node
)
10278 return error_mark_node
;
10280 vec
= make_tree_vector ();
10281 res
= finish_call_expr (member
, &vec
,
10282 /*disallow_virtual=*/false,
10283 /*koenig_p=*/false,
10284 tf_warning_or_error
);
10285 release_tree_vector (vec
);
10289 /* Parse an iteration-statement.
10291 iteration-statement:
10292 while ( condition ) statement
10293 do statement while ( expression ) ;
10294 for ( for-init-statement condition [opt] ; expression [opt] )
10297 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10300 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10305 unsigned char in_statement
;
10307 /* Peek at the next token. */
10308 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10310 return error_mark_node
;
10312 /* Remember whether or not we are already within an iteration
10314 in_statement
= parser
->in_statement
;
10316 /* See what kind of keyword it is. */
10317 keyword
= token
->keyword
;
10324 /* Begin the while-statement. */
10325 statement
= begin_while_stmt ();
10326 /* Look for the `('. */
10327 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10328 /* Parse the condition. */
10329 condition
= cp_parser_condition (parser
);
10330 finish_while_stmt_cond (condition
, statement
, ivdep
);
10331 /* Look for the `)'. */
10332 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10333 /* Parse the dependent statement. */
10334 parser
->in_statement
= IN_ITERATION_STMT
;
10335 cp_parser_already_scoped_statement (parser
);
10336 parser
->in_statement
= in_statement
;
10337 /* We're done with the while-statement. */
10338 finish_while_stmt (statement
);
10346 /* Begin the do-statement. */
10347 statement
= begin_do_stmt ();
10348 /* Parse the body of the do-statement. */
10349 parser
->in_statement
= IN_ITERATION_STMT
;
10350 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10351 parser
->in_statement
= in_statement
;
10352 finish_do_body (statement
);
10353 /* Look for the `while' keyword. */
10354 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10355 /* Look for the `('. */
10356 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10357 /* Parse the expression. */
10358 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10359 /* We're done with the do-statement. */
10360 finish_do_stmt (expression
, statement
, ivdep
);
10361 /* Look for the `)'. */
10362 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10363 /* Look for the `;'. */
10364 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10370 /* Look for the `('. */
10371 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10373 statement
= cp_parser_for (parser
, ivdep
);
10375 /* Look for the `)'. */
10376 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10378 /* Parse the body of the for-statement. */
10379 parser
->in_statement
= IN_ITERATION_STMT
;
10380 cp_parser_already_scoped_statement (parser
);
10381 parser
->in_statement
= in_statement
;
10383 /* We're done with the for-statement. */
10384 finish_for_stmt (statement
);
10389 cp_parser_error (parser
, "expected iteration-statement");
10390 statement
= error_mark_node
;
10397 /* Parse a for-init-statement or the declarator of a range-based-for.
10398 Returns true if a range-based-for declaration is seen.
10400 for-init-statement:
10401 expression-statement
10402 simple-declaration */
10405 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10407 /* If the next token is a `;', then we have an empty
10408 expression-statement. Grammatically, this is also a
10409 simple-declaration, but an invalid one, because it does not
10410 declare anything. Therefore, if we did not handle this case
10411 specially, we would issue an error message about an invalid
10413 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10415 bool is_range_for
= false;
10416 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10418 parser
->colon_corrects_to_scope_p
= false;
10420 /* We're going to speculatively look for a declaration, falling back
10421 to an expression, if necessary. */
10422 cp_parser_parse_tentatively (parser
);
10423 /* Parse the declaration. */
10424 cp_parser_simple_declaration (parser
,
10425 /*function_definition_allowed_p=*/false,
10427 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10430 /* It is a range-for, consume the ':' */
10431 cp_lexer_consume_token (parser
->lexer
);
10432 is_range_for
= true;
10433 if (cxx_dialect
< cxx11
)
10435 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10436 "range-based %<for%> loops are not allowed "
10438 *decl
= error_mark_node
;
10442 /* The ';' is not consumed yet because we told
10443 cp_parser_simple_declaration not to. */
10444 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10446 if (cp_parser_parse_definitely (parser
))
10447 return is_range_for
;
10448 /* If the tentative parse failed, then we shall need to look for an
10449 expression-statement. */
10451 /* If we are here, it is an expression-statement. */
10452 cp_parser_expression_statement (parser
, NULL_TREE
);
10456 /* Parse a jump-statement.
10461 return expression [opt] ;
10462 return braced-init-list ;
10468 goto * expression ;
10470 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10473 cp_parser_jump_statement (cp_parser
* parser
)
10475 tree statement
= error_mark_node
;
10478 unsigned char in_statement
;
10480 /* Peek at the next token. */
10481 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10483 return error_mark_node
;
10485 /* See what kind of keyword it is. */
10486 keyword
= token
->keyword
;
10490 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
10491 switch (in_statement
)
10494 error_at (token
->location
, "break statement not within loop or switch");
10497 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
10498 || in_statement
== IN_ITERATION_STMT
);
10499 statement
= finish_break_stmt ();
10502 error_at (token
->location
, "invalid exit from OpenMP structured block");
10505 error_at (token
->location
, "break statement used with OpenMP for loop");
10508 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10512 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10515 error_at (token
->location
, "continue statement not within a loop");
10517 case IN_ITERATION_STMT
:
10519 statement
= finish_continue_stmt ();
10522 error_at (token
->location
, "invalid exit from OpenMP structured block");
10525 gcc_unreachable ();
10527 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10533 bool expr_non_constant_p
;
10535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10537 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10538 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10540 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10541 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10543 /* If the next token is a `;', then there is no
10546 /* Build the return-statement. */
10547 statement
= finish_return_stmt (expr
);
10548 /* Look for the final `;'. */
10549 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10554 /* Create the goto-statement. */
10555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
10557 /* Issue a warning about this use of a GNU extension. */
10558 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
10559 /* Consume the '*' token. */
10560 cp_lexer_consume_token (parser
->lexer
);
10561 /* Parse the dependent expression. */
10562 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
10565 finish_goto_stmt (cp_parser_identifier (parser
));
10566 /* Look for the final `;'. */
10567 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10571 cp_parser_error (parser
, "expected jump-statement");
10578 /* Parse a declaration-statement.
10580 declaration-statement:
10581 block-declaration */
10584 cp_parser_declaration_statement (cp_parser
* parser
)
10588 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10589 p
= obstack_alloc (&declarator_obstack
, 0);
10591 /* Parse the block-declaration. */
10592 cp_parser_block_declaration (parser
, /*statement_p=*/true);
10594 /* Free any declarators allocated. */
10595 obstack_free (&declarator_obstack
, p
);
10598 /* Some dependent statements (like `if (cond) statement'), are
10599 implicitly in their own scope. In other words, if the statement is
10600 a single statement (as opposed to a compound-statement), it is
10601 none-the-less treated as if it were enclosed in braces. Any
10602 declarations appearing in the dependent statement are out of scope
10603 after control passes that point. This function parses a statement,
10604 but ensures that is in its own scope, even if it is not a
10605 compound-statement.
10607 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10608 is a (possibly labeled) if statement which is not enclosed in
10609 braces and has an else clause. This is used to implement
10612 Returns the new statement. */
10615 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
10622 /* Mark if () ; with a special NOP_EXPR. */
10623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10625 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10626 cp_lexer_consume_token (parser
->lexer
);
10627 statement
= add_stmt (build_empty_stmt (loc
));
10629 /* if a compound is opened, we simply parse the statement directly. */
10630 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10631 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
10632 /* If the token is not a `{', then we must take special action. */
10635 /* Create a compound-statement. */
10636 statement
= begin_compound_stmt (0);
10637 /* Parse the dependent-statement. */
10638 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
10639 /* Finish the dummy compound-statement. */
10640 finish_compound_stmt (statement
);
10643 /* Return the statement. */
10647 /* For some dependent statements (like `while (cond) statement'), we
10648 have already created a scope. Therefore, even if the dependent
10649 statement is a compound-statement, we do not want to create another
10653 cp_parser_already_scoped_statement (cp_parser
* parser
)
10655 /* If the token is a `{', then we must take special action. */
10656 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10657 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
10660 /* Avoid calling cp_parser_compound_statement, so that we
10661 don't create a new scope. Do everything else by hand. */
10662 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
10663 /* If the next keyword is `__label__' we have a label declaration. */
10664 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10665 cp_parser_label_declaration (parser
);
10666 /* Parse an (optional) statement-seq. */
10667 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10668 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10672 /* Declarations [gram.dcl.dcl] */
10674 /* Parse an optional declaration-sequence.
10678 declaration-seq declaration */
10681 cp_parser_declaration_seq_opt (cp_parser
* parser
)
10687 token
= cp_lexer_peek_token (parser
->lexer
);
10689 if (token
->type
== CPP_CLOSE_BRACE
10690 || token
->type
== CPP_EOF
10691 || token
->type
== CPP_PRAGMA_EOL
)
10694 if (token
->type
== CPP_SEMICOLON
)
10696 /* A declaration consisting of a single semicolon is
10697 invalid. Allow it unless we're being pedantic. */
10698 cp_lexer_consume_token (parser
->lexer
);
10699 if (!in_system_header
)
10700 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
10704 /* If we're entering or exiting a region that's implicitly
10705 extern "C", modify the lang context appropriately. */
10706 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
10708 push_lang_context (lang_name_c
);
10709 parser
->implicit_extern_c
= true;
10711 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
10713 pop_lang_context ();
10714 parser
->implicit_extern_c
= false;
10717 if (token
->type
== CPP_PRAGMA
)
10719 /* A top-level declaration can consist solely of a #pragma.
10720 A nested declaration cannot, so this is done here and not
10721 in cp_parser_declaration. (A #pragma at block scope is
10722 handled in cp_parser_statement.) */
10723 cp_parser_pragma (parser
, pragma_external
);
10727 /* Parse the declaration itself. */
10728 cp_parser_declaration (parser
);
10732 /* Parse a declaration.
10736 function-definition
10737 template-declaration
10738 explicit-instantiation
10739 explicit-specialization
10740 linkage-specification
10741 namespace-definition
10746 __extension__ declaration */
10749 cp_parser_declaration (cp_parser
* parser
)
10753 int saved_pedantic
;
10755 tree attributes
= NULL_TREE
;
10757 /* Check for the `__extension__' keyword. */
10758 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10760 /* Parse the qualified declaration. */
10761 cp_parser_declaration (parser
);
10762 /* Restore the PEDANTIC flag. */
10763 pedantic
= saved_pedantic
;
10768 /* Try to figure out what kind of declaration is present. */
10769 token1
= *cp_lexer_peek_token (parser
->lexer
);
10771 if (token1
.type
!= CPP_EOF
)
10772 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
10775 token2
.type
= CPP_EOF
;
10776 token2
.keyword
= RID_MAX
;
10779 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10780 p
= obstack_alloc (&declarator_obstack
, 0);
10782 /* If the next token is `extern' and the following token is a string
10783 literal, then we have a linkage specification. */
10784 if (token1
.keyword
== RID_EXTERN
10785 && cp_parser_is_pure_string_literal (&token2
))
10786 cp_parser_linkage_specification (parser
);
10787 /* If the next token is `template', then we have either a template
10788 declaration, an explicit instantiation, or an explicit
10790 else if (token1
.keyword
== RID_TEMPLATE
)
10792 /* `template <>' indicates a template specialization. */
10793 if (token2
.type
== CPP_LESS
10794 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
10795 cp_parser_explicit_specialization (parser
);
10796 /* `template <' indicates a template declaration. */
10797 else if (token2
.type
== CPP_LESS
)
10798 cp_parser_template_declaration (parser
, /*member_p=*/false);
10799 /* Anything else must be an explicit instantiation. */
10801 cp_parser_explicit_instantiation (parser
);
10803 /* If the next token is `export', then we have a template
10805 else if (token1
.keyword
== RID_EXPORT
)
10806 cp_parser_template_declaration (parser
, /*member_p=*/false);
10807 /* If the next token is `extern', 'static' or 'inline' and the one
10808 after that is `template', we have a GNU extended explicit
10809 instantiation directive. */
10810 else if (cp_parser_allow_gnu_extensions_p (parser
)
10811 && (token1
.keyword
== RID_EXTERN
10812 || token1
.keyword
== RID_STATIC
10813 || token1
.keyword
== RID_INLINE
)
10814 && token2
.keyword
== RID_TEMPLATE
)
10815 cp_parser_explicit_instantiation (parser
);
10816 /* If the next token is `namespace', check for a named or unnamed
10817 namespace definition. */
10818 else if (token1
.keyword
== RID_NAMESPACE
10819 && (/* A named namespace definition. */
10820 (token2
.type
== CPP_NAME
10821 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
10823 /* An unnamed namespace definition. */
10824 || token2
.type
== CPP_OPEN_BRACE
10825 || token2
.keyword
== RID_ATTRIBUTE
))
10826 cp_parser_namespace_definition (parser
);
10827 /* An inline (associated) namespace definition. */
10828 else if (token1
.keyword
== RID_INLINE
10829 && token2
.keyword
== RID_NAMESPACE
)
10830 cp_parser_namespace_definition (parser
);
10831 /* Objective-C++ declaration/definition. */
10832 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
10833 cp_parser_objc_declaration (parser
, NULL_TREE
);
10834 else if (c_dialect_objc ()
10835 && token1
.keyword
== RID_ATTRIBUTE
10836 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
10837 cp_parser_objc_declaration (parser
, attributes
);
10838 /* We must have either a block declaration or a function
10841 /* Try to parse a block-declaration, or a function-definition. */
10842 cp_parser_block_declaration (parser
, /*statement_p=*/false);
10844 /* Free any declarators allocated. */
10845 obstack_free (&declarator_obstack
, p
);
10848 /* Parse a block-declaration.
10853 namespace-alias-definition
10860 __extension__ block-declaration
10865 static_assert-declaration
10867 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10868 part of a declaration-statement. */
10871 cp_parser_block_declaration (cp_parser
*parser
,
10875 int saved_pedantic
;
10877 /* Check for the `__extension__' keyword. */
10878 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10880 /* Parse the qualified declaration. */
10881 cp_parser_block_declaration (parser
, statement_p
);
10882 /* Restore the PEDANTIC flag. */
10883 pedantic
= saved_pedantic
;
10888 /* Peek at the next token to figure out which kind of declaration is
10890 token1
= cp_lexer_peek_token (parser
->lexer
);
10892 /* If the next keyword is `asm', we have an asm-definition. */
10893 if (token1
->keyword
== RID_ASM
)
10896 cp_parser_commit_to_tentative_parse (parser
);
10897 cp_parser_asm_definition (parser
);
10899 /* If the next keyword is `namespace', we have a
10900 namespace-alias-definition. */
10901 else if (token1
->keyword
== RID_NAMESPACE
)
10902 cp_parser_namespace_alias_definition (parser
);
10903 /* If the next keyword is `using', we have a
10904 using-declaration, a using-directive, or an alias-declaration. */
10905 else if (token1
->keyword
== RID_USING
)
10910 cp_parser_commit_to_tentative_parse (parser
);
10911 /* If the token after `using' is `namespace', then we have a
10912 using-directive. */
10913 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10914 if (token2
->keyword
== RID_NAMESPACE
)
10915 cp_parser_using_directive (parser
);
10916 /* If the second token after 'using' is '=', then we have an
10917 alias-declaration. */
10918 else if (cxx_dialect
>= cxx11
10919 && token2
->type
== CPP_NAME
10920 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
10921 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
10922 cp_parser_alias_declaration (parser
);
10923 /* Otherwise, it's a using-declaration. */
10925 cp_parser_using_declaration (parser
,
10926 /*access_declaration_p=*/false);
10928 /* If the next keyword is `__label__' we have a misplaced label
10930 else if (token1
->keyword
== RID_LABEL
)
10932 cp_lexer_consume_token (parser
->lexer
);
10933 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
10934 cp_parser_skip_to_end_of_statement (parser
);
10935 /* If the next token is now a `;', consume it. */
10936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10937 cp_lexer_consume_token (parser
->lexer
);
10939 /* If the next token is `static_assert' we have a static assertion. */
10940 else if (token1
->keyword
== RID_STATIC_ASSERT
)
10941 cp_parser_static_assert (parser
, /*member_p=*/false);
10942 /* Anything else must be a simple-declaration. */
10944 cp_parser_simple_declaration (parser
, !statement_p
,
10945 /*maybe_range_for_decl*/NULL
);
10948 /* Parse a simple-declaration.
10950 simple-declaration:
10951 decl-specifier-seq [opt] init-declarator-list [opt] ;
10953 init-declarator-list:
10955 init-declarator-list , init-declarator
10957 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10958 function-definition as a simple-declaration.
10960 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10961 parsed declaration if it is an uninitialized single declarator not followed
10962 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10963 if present, will not be consumed. */
10966 cp_parser_simple_declaration (cp_parser
* parser
,
10967 bool function_definition_allowed_p
,
10968 tree
*maybe_range_for_decl
)
10970 cp_decl_specifier_seq decl_specifiers
;
10971 int declares_class_or_enum
;
10972 bool saw_declarator
;
10974 if (maybe_range_for_decl
)
10975 *maybe_range_for_decl
= NULL_TREE
;
10977 /* Defer access checks until we know what is being declared; the
10978 checks for names appearing in the decl-specifier-seq should be
10979 done as if we were in the scope of the thing being declared. */
10980 push_deferring_access_checks (dk_deferred
);
10982 /* Parse the decl-specifier-seq. We have to keep track of whether
10983 or not the decl-specifier-seq declares a named class or
10984 enumeration type, since that is the only case in which the
10985 init-declarator-list is allowed to be empty.
10989 In a simple-declaration, the optional init-declarator-list can be
10990 omitted only when declaring a class or enumeration, that is when
10991 the decl-specifier-seq contains either a class-specifier, an
10992 elaborated-type-specifier, or an enum-specifier. */
10993 cp_parser_decl_specifier_seq (parser
,
10994 CP_PARSER_FLAGS_OPTIONAL
,
10996 &declares_class_or_enum
);
10997 /* We no longer need to defer access checks. */
10998 stop_deferring_access_checks ();
11000 /* In a block scope, a valid declaration must always have a
11001 decl-specifier-seq. By not trying to parse declarators, we can
11002 resolve the declaration/expression ambiguity more quickly. */
11003 if (!function_definition_allowed_p
11004 && !decl_specifiers
.any_specifiers_p
)
11006 cp_parser_error (parser
, "expected declaration");
11010 /* If the next two tokens are both identifiers, the code is
11011 erroneous. The usual cause of this situation is code like:
11015 where "T" should name a type -- but does not. */
11016 if (!decl_specifiers
.any_type_specifiers_p
11017 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11019 /* If parsing tentatively, we should commit; we really are
11020 looking at a declaration. */
11021 cp_parser_commit_to_tentative_parse (parser
);
11026 /* If we have seen at least one decl-specifier, and the next token
11027 is not a parenthesis, then we must be looking at a declaration.
11028 (After "int (" we might be looking at a functional cast.) */
11029 if (decl_specifiers
.any_specifiers_p
11030 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11031 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11032 && !cp_parser_error_occurred (parser
))
11033 cp_parser_commit_to_tentative_parse (parser
);
11035 /* Keep going until we hit the `;' at the end of the simple
11037 saw_declarator
= false;
11038 while (cp_lexer_next_token_is_not (parser
->lexer
,
11042 bool function_definition_p
;
11045 if (saw_declarator
)
11047 /* If we are processing next declarator, coma is expected */
11048 token
= cp_lexer_peek_token (parser
->lexer
);
11049 gcc_assert (token
->type
== CPP_COMMA
);
11050 cp_lexer_consume_token (parser
->lexer
);
11051 if (maybe_range_for_decl
)
11052 *maybe_range_for_decl
= error_mark_node
;
11055 saw_declarator
= true;
11057 /* Parse the init-declarator. */
11058 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11060 function_definition_allowed_p
,
11061 /*member_p=*/false,
11062 declares_class_or_enum
,
11063 &function_definition_p
,
11064 maybe_range_for_decl
);
11065 /* If an error occurred while parsing tentatively, exit quickly.
11066 (That usually happens when in the body of a function; each
11067 statement is treated as a declaration-statement until proven
11069 if (cp_parser_error_occurred (parser
))
11071 /* Handle function definitions specially. */
11072 if (function_definition_p
)
11074 /* If the next token is a `,', then we are probably
11075 processing something like:
11079 which is erroneous. */
11080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11082 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11083 error_at (token
->location
,
11085 " declarations and function-definitions is forbidden");
11087 /* Otherwise, we're done with the list of declarators. */
11090 pop_deferring_access_checks ();
11094 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11095 *maybe_range_for_decl
= decl
;
11096 /* The next token should be either a `,' or a `;'. */
11097 token
= cp_lexer_peek_token (parser
->lexer
);
11098 /* If it's a `,', there are more declarators to come. */
11099 if (token
->type
== CPP_COMMA
)
11100 /* will be consumed next time around */;
11101 /* If it's a `;', we are done. */
11102 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11104 /* Anything else is an error. */
11107 /* If we have already issued an error message we don't need
11108 to issue another one. */
11109 if (decl
!= error_mark_node
11110 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11111 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11112 /* Skip tokens until we reach the end of the statement. */
11113 cp_parser_skip_to_end_of_statement (parser
);
11114 /* If the next token is now a `;', consume it. */
11115 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11116 cp_lexer_consume_token (parser
->lexer
);
11119 /* After the first time around, a function-definition is not
11120 allowed -- even if it was OK at first. For example:
11125 function_definition_allowed_p
= false;
11128 /* Issue an error message if no declarators are present, and the
11129 decl-specifier-seq does not itself declare a class or
11130 enumeration: [dcl.dcl]/3. */
11131 if (!saw_declarator
)
11133 if (cp_parser_declares_only_class_p (parser
))
11135 if (!declares_class_or_enum
11136 && decl_specifiers
.type
11137 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11138 /* Ensure an error is issued anyway when finish_decltype_type,
11139 called via cp_parser_decl_specifier_seq, returns a class or
11140 an enumeration (c++/51786). */
11141 decl_specifiers
.type
= NULL_TREE
;
11142 shadow_tag (&decl_specifiers
);
11144 /* Perform any deferred access checks. */
11145 perform_deferred_access_checks (tf_warning_or_error
);
11148 /* Consume the `;'. */
11149 if (!maybe_range_for_decl
)
11150 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11153 pop_deferring_access_checks ();
11156 /* Parse a decl-specifier-seq.
11158 decl-specifier-seq:
11159 decl-specifier-seq [opt] decl-specifier
11160 decl-specifier attribute-specifier-seq [opt] (C++11)
11163 storage-class-specifier
11174 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11176 The parser flags FLAGS is used to control type-specifier parsing.
11178 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11181 1: one of the decl-specifiers is an elaborated-type-specifier
11182 (i.e., a type declaration)
11183 2: one of the decl-specifiers is an enum-specifier or a
11184 class-specifier (i.e., a type definition)
11189 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11190 cp_parser_flags flags
,
11191 cp_decl_specifier_seq
*decl_specs
,
11192 int* declares_class_or_enum
)
11194 bool constructor_possible_p
= !parser
->in_declarator_p
;
11195 bool found_decl_spec
= false;
11196 cp_token
*start_token
= NULL
;
11199 /* Clear DECL_SPECS. */
11200 clear_decl_specs (decl_specs
);
11202 /* Assume no class or enumeration type is declared. */
11203 *declares_class_or_enum
= 0;
11205 /* Keep reading specifiers until there are no more to read. */
11208 bool constructor_p
;
11212 /* Peek at the next token. */
11213 token
= cp_lexer_peek_token (parser
->lexer
);
11215 /* Save the first token of the decl spec list for error
11218 start_token
= token
;
11219 /* Handle attributes. */
11220 if (cp_next_tokens_can_be_attribute_p (parser
))
11222 /* Parse the attributes. */
11223 tree attrs
= cp_parser_attributes_opt (parser
);
11225 /* In a sequence of declaration specifiers, c++11 attributes
11226 appertain to the type that precede them. In that case
11229 The attribute-specifier-seq affects the type only for
11230 the declaration it appears in, not other declarations
11231 involving the same type.
11233 But for now let's force the user to position the
11234 attribute either at the beginning of the declaration or
11235 after the declarator-id, which would clearly mean that it
11236 applies to the declarator. */
11237 if (cxx11_attribute_p (attrs
))
11239 if (!found_decl_spec
)
11240 /* The c++11 attribute is at the beginning of the
11241 declaration. It appertains to the entity being
11245 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11247 /* This is an attribute following a
11248 class-specifier. */
11249 if (decl_specs
->type_definition_p
)
11250 warn_misplaced_attr_for_class_type (token
->location
,
11256 decl_specs
->std_attributes
11257 = chainon (decl_specs
->std_attributes
,
11259 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11260 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11266 decl_specs
->attributes
11267 = chainon (decl_specs
->attributes
,
11269 if (decl_specs
->locations
[ds_attribute
] == 0)
11270 decl_specs
->locations
[ds_attribute
] = token
->location
;
11273 /* Assume we will find a decl-specifier keyword. */
11274 found_decl_spec
= true;
11275 /* If the next token is an appropriate keyword, we can simply
11276 add it to the list. */
11277 switch (token
->keyword
)
11283 if (!at_class_scope_p ())
11285 error_at (token
->location
, "%<friend%> used outside of class");
11286 cp_lexer_purge_token (parser
->lexer
);
11291 /* Consume the token. */
11292 cp_lexer_consume_token (parser
->lexer
);
11296 case RID_CONSTEXPR
:
11298 cp_lexer_consume_token (parser
->lexer
);
11301 /* function-specifier:
11308 cp_parser_function_specifier_opt (parser
, decl_specs
);
11315 /* Consume the token. */
11316 cp_lexer_consume_token (parser
->lexer
);
11317 /* A constructor declarator cannot appear in a typedef. */
11318 constructor_possible_p
= false;
11319 /* The "typedef" keyword can only occur in a declaration; we
11320 may as well commit at this point. */
11321 cp_parser_commit_to_tentative_parse (parser
);
11323 if (decl_specs
->storage_class
!= sc_none
)
11324 decl_specs
->conflicting_specifiers_p
= true;
11327 /* storage-class-specifier:
11337 if (cxx_dialect
== cxx98
)
11339 /* Consume the token. */
11340 cp_lexer_consume_token (parser
->lexer
);
11342 /* Complain about `auto' as a storage specifier, if
11343 we're complaining about C++0x compatibility. */
11344 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11345 " changes meaning in C++11; please remove it");
11347 /* Set the storage class anyway. */
11348 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11352 /* C++0x auto type-specifier. */
11353 found_decl_spec
= false;
11360 /* Consume the token. */
11361 cp_lexer_consume_token (parser
->lexer
);
11362 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11366 /* Consume the token. */
11368 cp_lexer_consume_token (parser
->lexer
);
11372 /* We did not yet find a decl-specifier yet. */
11373 found_decl_spec
= false;
11377 if (found_decl_spec
11378 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11379 && token
->keyword
!= RID_CONSTEXPR
)
11380 error ("decl-specifier invalid in condition");
11383 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11385 /* Constructors are a special case. The `S' in `S()' is not a
11386 decl-specifier; it is the beginning of the declarator. */
11388 = (!found_decl_spec
11389 && constructor_possible_p
11390 && (cp_parser_constructor_declarator_p
11391 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11393 /* If we don't have a DECL_SPEC yet, then we must be looking at
11394 a type-specifier. */
11395 if (!found_decl_spec
&& !constructor_p
)
11397 int decl_spec_declares_class_or_enum
;
11398 bool is_cv_qualifier
;
11402 = cp_parser_type_specifier (parser
, flags
,
11404 /*is_declaration=*/true,
11405 &decl_spec_declares_class_or_enum
,
11407 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11409 /* If this type-specifier referenced a user-defined type
11410 (a typedef, class-name, etc.), then we can't allow any
11411 more such type-specifiers henceforth.
11415 The longest sequence of decl-specifiers that could
11416 possibly be a type name is taken as the
11417 decl-specifier-seq of a declaration. The sequence shall
11418 be self-consistent as described below.
11422 As a general rule, at most one type-specifier is allowed
11423 in the complete decl-specifier-seq of a declaration. The
11424 only exceptions are the following:
11426 -- const or volatile can be combined with any other
11429 -- signed or unsigned can be combined with char, long,
11437 void g (const int Pc);
11439 Here, Pc is *not* part of the decl-specifier seq; it's
11440 the declarator. Therefore, once we see a type-specifier
11441 (other than a cv-qualifier), we forbid any additional
11442 user-defined types. We *do* still allow things like `int
11443 int' to be considered a decl-specifier-seq, and issue the
11444 error message later. */
11445 if (type_spec
&& !is_cv_qualifier
)
11446 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11447 /* A constructor declarator cannot follow a type-specifier. */
11450 constructor_possible_p
= false;
11451 found_decl_spec
= true;
11452 if (!is_cv_qualifier
)
11453 decl_specs
->any_type_specifiers_p
= true;
11457 /* If we still do not have a DECL_SPEC, then there are no more
11458 decl-specifiers. */
11459 if (!found_decl_spec
)
11462 decl_specs
->any_specifiers_p
= true;
11463 /* After we see one decl-specifier, further decl-specifiers are
11464 always optional. */
11465 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
11468 /* Don't allow a friend specifier with a class definition. */
11469 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
11470 && (*declares_class_or_enum
& 2))
11471 error_at (decl_specs
->locations
[ds_friend
],
11472 "class definition may not be declared a friend");
11475 /* Parse an (optional) storage-class-specifier.
11477 storage-class-specifier:
11486 storage-class-specifier:
11489 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11492 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
11494 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
11497 if (cxx_dialect
!= cxx98
)
11499 /* Fall through for C++98. */
11506 /* Consume the token. */
11507 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11514 /* Parse an (optional) function-specifier.
11516 function-specifier:
11521 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11522 Updates DECL_SPECS, if it is non-NULL. */
11525 cp_parser_function_specifier_opt (cp_parser
* parser
,
11526 cp_decl_specifier_seq
*decl_specs
)
11528 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11529 switch (token
->keyword
)
11532 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
11536 /* 14.5.2.3 [temp.mem]
11538 A member function template shall not be virtual. */
11539 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11540 error_at (token
->location
, "templates may not be %<virtual%>");
11542 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
11546 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
11553 /* Consume the token. */
11554 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11557 /* Parse a linkage-specification.
11559 linkage-specification:
11560 extern string-literal { declaration-seq [opt] }
11561 extern string-literal declaration */
11564 cp_parser_linkage_specification (cp_parser
* parser
)
11568 /* Look for the `extern' keyword. */
11569 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
11571 /* Look for the string-literal. */
11572 linkage
= cp_parser_string_literal (parser
, false, false);
11574 /* Transform the literal into an identifier. If the literal is a
11575 wide-character string, or contains embedded NULs, then we can't
11576 handle it as the user wants. */
11577 if (strlen (TREE_STRING_POINTER (linkage
))
11578 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
11580 cp_parser_error (parser
, "invalid linkage-specification");
11581 /* Assume C++ linkage. */
11582 linkage
= lang_name_cplusplus
;
11585 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
11587 /* We're now using the new linkage. */
11588 push_lang_context (linkage
);
11590 /* If the next token is a `{', then we're using the first
11592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11594 cp_ensure_no_omp_declare_simd (parser
);
11596 /* Consume the `{' token. */
11597 cp_lexer_consume_token (parser
->lexer
);
11598 /* Parse the declarations. */
11599 cp_parser_declaration_seq_opt (parser
);
11600 /* Look for the closing `}'. */
11601 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11603 /* Otherwise, there's just one declaration. */
11606 bool saved_in_unbraced_linkage_specification_p
;
11608 saved_in_unbraced_linkage_specification_p
11609 = parser
->in_unbraced_linkage_specification_p
;
11610 parser
->in_unbraced_linkage_specification_p
= true;
11611 cp_parser_declaration (parser
);
11612 parser
->in_unbraced_linkage_specification_p
11613 = saved_in_unbraced_linkage_specification_p
;
11616 /* We're done with the linkage-specification. */
11617 pop_lang_context ();
11620 /* Parse a static_assert-declaration.
11622 static_assert-declaration:
11623 static_assert ( constant-expression , string-literal ) ;
11625 If MEMBER_P, this static_assert is a class member. */
11628 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
11633 location_t saved_loc
;
11636 /* Peek at the `static_assert' token so we can keep track of exactly
11637 where the static assertion started. */
11638 token
= cp_lexer_peek_token (parser
->lexer
);
11639 saved_loc
= token
->location
;
11641 /* Look for the `static_assert' keyword. */
11642 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
11646 /* We know we are in a static assertion; commit to any tentative
11648 if (cp_parser_parsing_tentatively (parser
))
11649 cp_parser_commit_to_tentative_parse (parser
);
11651 /* Parse the `(' starting the static assertion condition. */
11652 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11654 /* Parse the constant-expression. Allow a non-constant expression
11655 here in order to give better diagnostics in finish_static_assert. */
11657 cp_parser_constant_expression (parser
,
11658 /*allow_non_constant_p=*/true,
11659 /*non_constant_p=*/&dummy
);
11661 /* Parse the separating `,'. */
11662 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11664 /* Parse the string-literal message. */
11665 message
= cp_parser_string_literal (parser
,
11666 /*translate=*/false,
11669 /* A `)' completes the static assertion. */
11670 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11671 cp_parser_skip_to_closing_parenthesis (parser
,
11672 /*recovering=*/true,
11673 /*or_comma=*/false,
11674 /*consume_paren=*/true);
11676 /* A semicolon terminates the declaration. */
11677 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11679 /* Complete the static assertion, which may mean either processing
11680 the static assert now or saving it for template instantiation. */
11681 finish_static_assert (condition
, message
, saved_loc
, member_p
);
11684 /* Parse the expression in decltype ( expression ). */
11687 cp_parser_decltype_expr (cp_parser
*parser
,
11688 bool &id_expression_or_member_access_p
)
11690 cp_token
*id_expr_start_token
;
11693 /* First, try parsing an id-expression. */
11694 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
11695 cp_parser_parse_tentatively (parser
);
11696 expr
= cp_parser_id_expression (parser
,
11697 /*template_keyword_p=*/false,
11698 /*check_dependency_p=*/true,
11699 /*template_p=*/NULL
,
11700 /*declarator_p=*/false,
11701 /*optional_p=*/false);
11703 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
11705 bool non_integral_constant_expression_p
= false;
11706 tree id_expression
= expr
;
11708 const char *error_msg
;
11710 if (identifier_p (expr
))
11711 /* Lookup the name we got back from the id-expression. */
11712 expr
= cp_parser_lookup_name_simple (parser
, expr
,
11713 id_expr_start_token
->location
);
11716 && expr
!= error_mark_node
11717 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
11718 && TREE_CODE (expr
) != TYPE_DECL
11719 && (TREE_CODE (expr
) != BIT_NOT_EXPR
11720 || !TYPE_P (TREE_OPERAND (expr
, 0)))
11721 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11723 /* Complete lookup of the id-expression. */
11724 expr
= (finish_id_expression
11725 (id_expression
, expr
, parser
->scope
, &idk
,
11726 /*integral_constant_expression_p=*/false,
11727 /*allow_non_integral_constant_expression_p=*/true,
11728 &non_integral_constant_expression_p
,
11729 /*template_p=*/false,
11731 /*address_p=*/false,
11732 /*template_arg_p=*/false,
11734 id_expr_start_token
->location
));
11736 if (expr
== error_mark_node
)
11737 /* We found an id-expression, but it was something that we
11738 should not have found. This is an error, not something
11739 we can recover from, so note that we found an
11740 id-expression and we'll recover as gracefully as
11742 id_expression_or_member_access_p
= true;
11746 && expr
!= error_mark_node
11747 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11748 /* We have an id-expression. */
11749 id_expression_or_member_access_p
= true;
11752 if (!id_expression_or_member_access_p
)
11754 /* Abort the id-expression parse. */
11755 cp_parser_abort_tentative_parse (parser
);
11757 /* Parsing tentatively, again. */
11758 cp_parser_parse_tentatively (parser
);
11760 /* Parse a class member access. */
11761 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
11762 /*cast_p=*/false, /*decltype*/true,
11763 /*member_access_only_p=*/true, NULL
);
11766 && expr
!= error_mark_node
11767 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11768 /* We have an id-expression. */
11769 id_expression_or_member_access_p
= true;
11772 if (id_expression_or_member_access_p
)
11773 /* We have parsed the complete id-expression or member access. */
11774 cp_parser_parse_definitely (parser
);
11777 /* Abort our attempt to parse an id-expression or member access
11779 cp_parser_abort_tentative_parse (parser
);
11781 /* Parse a full expression. */
11782 expr
= cp_parser_expression (parser
, /*cast_p=*/false,
11783 /*decltype*/true, NULL
);
11789 /* Parse a `decltype' type. Returns the type.
11791 simple-type-specifier:
11792 decltype ( expression )
11794 decltype ( auto ) */
11797 cp_parser_decltype (cp_parser
*parser
)
11800 bool id_expression_or_member_access_p
= false;
11801 const char *saved_message
;
11802 bool saved_integral_constant_expression_p
;
11803 bool saved_non_integral_constant_expression_p
;
11804 bool saved_greater_than_is_operator_p
;
11805 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11807 if (start_token
->type
== CPP_DECLTYPE
)
11809 /* Already parsed. */
11810 cp_lexer_consume_token (parser
->lexer
);
11811 return start_token
->u
.value
;
11814 /* Look for the `decltype' token. */
11815 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
11816 return error_mark_node
;
11818 /* Parse the opening `('. */
11819 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11820 return error_mark_node
;
11822 /* decltype (auto) */
11823 if (cxx_dialect
>= cxx1y
11824 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
11826 cp_lexer_consume_token (parser
->lexer
);
11827 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11828 return error_mark_node
;
11829 expr
= make_decltype_auto ();
11830 AUTO_IS_DECLTYPE (expr
) = true;
11834 /* Types cannot be defined in a `decltype' expression. Save away the
11836 saved_message
= parser
->type_definition_forbidden_message
;
11838 /* And create the new one. */
11839 parser
->type_definition_forbidden_message
11840 = G_("types may not be defined in %<decltype%> expressions");
11842 /* The restrictions on constant-expressions do not apply inside
11843 decltype expressions. */
11844 saved_integral_constant_expression_p
11845 = parser
->integral_constant_expression_p
;
11846 saved_non_integral_constant_expression_p
11847 = parser
->non_integral_constant_expression_p
;
11848 parser
->integral_constant_expression_p
= false;
11850 /* Within a parenthesized expression, a `>' token is always
11851 the greater-than operator. */
11852 saved_greater_than_is_operator_p
11853 = parser
->greater_than_is_operator_p
;
11854 parser
->greater_than_is_operator_p
= true;
11856 /* Do not actually evaluate the expression. */
11857 ++cp_unevaluated_operand
;
11859 /* Do not warn about problems with the expression. */
11860 ++c_inhibit_evaluation_warnings
;
11862 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
11864 /* Go back to evaluating expressions. */
11865 --cp_unevaluated_operand
;
11866 --c_inhibit_evaluation_warnings
;
11868 /* The `>' token might be the end of a template-id or
11869 template-parameter-list now. */
11870 parser
->greater_than_is_operator_p
11871 = saved_greater_than_is_operator_p
;
11873 /* Restore the old message and the integral constant expression
11875 parser
->type_definition_forbidden_message
= saved_message
;
11876 parser
->integral_constant_expression_p
11877 = saved_integral_constant_expression_p
;
11878 parser
->non_integral_constant_expression_p
11879 = saved_non_integral_constant_expression_p
;
11881 /* Parse to the closing `)'. */
11882 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11884 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11885 /*consume_paren=*/true);
11886 return error_mark_node
;
11889 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
11890 tf_warning_or_error
);
11893 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11895 start_token
->type
= CPP_DECLTYPE
;
11896 start_token
->u
.value
= expr
;
11897 start_token
->keyword
= RID_MAX
;
11898 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
11903 /* Special member functions [gram.special] */
11905 /* Parse a conversion-function-id.
11907 conversion-function-id:
11908 operator conversion-type-id
11910 Returns an IDENTIFIER_NODE representing the operator. */
11913 cp_parser_conversion_function_id (cp_parser
* parser
)
11917 tree saved_qualifying_scope
;
11918 tree saved_object_scope
;
11919 tree pushed_scope
= NULL_TREE
;
11921 /* Look for the `operator' token. */
11922 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
11923 return error_mark_node
;
11924 /* When we parse the conversion-type-id, the current scope will be
11925 reset. However, we need that information in able to look up the
11926 conversion function later, so we save it here. */
11927 saved_scope
= parser
->scope
;
11928 saved_qualifying_scope
= parser
->qualifying_scope
;
11929 saved_object_scope
= parser
->object_scope
;
11930 /* We must enter the scope of the class so that the names of
11931 entities declared within the class are available in the
11932 conversion-type-id. For example, consider:
11939 S::operator I() { ... }
11941 In order to see that `I' is a type-name in the definition, we
11942 must be in the scope of `S'. */
11944 pushed_scope
= push_scope (saved_scope
);
11945 /* Parse the conversion-type-id. */
11946 type
= cp_parser_conversion_type_id (parser
);
11947 /* Leave the scope of the class, if any. */
11949 pop_scope (pushed_scope
);
11950 /* Restore the saved scope. */
11951 parser
->scope
= saved_scope
;
11952 parser
->qualifying_scope
= saved_qualifying_scope
;
11953 parser
->object_scope
= saved_object_scope
;
11954 /* If the TYPE is invalid, indicate failure. */
11955 if (type
== error_mark_node
)
11956 return error_mark_node
;
11957 return mangle_conv_op_name_for_type (type
);
11960 /* Parse a conversion-type-id:
11962 conversion-type-id:
11963 type-specifier-seq conversion-declarator [opt]
11965 Returns the TYPE specified. */
11968 cp_parser_conversion_type_id (cp_parser
* parser
)
11971 cp_decl_specifier_seq type_specifiers
;
11972 cp_declarator
*declarator
;
11973 tree type_specified
;
11974 const char *saved_message
;
11976 /* Parse the attributes. */
11977 attributes
= cp_parser_attributes_opt (parser
);
11979 saved_message
= parser
->type_definition_forbidden_message
;
11980 parser
->type_definition_forbidden_message
11981 = G_("types may not be defined in a conversion-type-id");
11983 /* Parse the type-specifiers. */
11984 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
11985 /*is_trailing_return=*/false,
11988 parser
->type_definition_forbidden_message
= saved_message
;
11990 /* If that didn't work, stop. */
11991 if (type_specifiers
.type
== error_mark_node
)
11992 return error_mark_node
;
11993 /* Parse the conversion-declarator. */
11994 declarator
= cp_parser_conversion_declarator_opt (parser
);
11996 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
11997 /*initialized=*/0, &attributes
);
11999 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12001 /* Don't give this error when parsing tentatively. This happens to
12002 work because we always parse this definitively once. */
12003 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12004 && type_uses_auto (type_specified
))
12006 if (cxx_dialect
< cxx1y
)
12008 error ("invalid use of %<auto%> in conversion operator");
12009 return error_mark_node
;
12011 else if (template_parm_scope_p ())
12012 warning (0, "use of %<auto%> in member template "
12013 "conversion operator can never be deduced");
12016 return type_specified
;
12019 /* Parse an (optional) conversion-declarator.
12021 conversion-declarator:
12022 ptr-operator conversion-declarator [opt]
12026 static cp_declarator
*
12027 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12029 enum tree_code code
;
12030 tree class_type
, std_attributes
= NULL_TREE
;
12031 cp_cv_quals cv_quals
;
12033 /* We don't know if there's a ptr-operator next, or not. */
12034 cp_parser_parse_tentatively (parser
);
12035 /* Try the ptr-operator. */
12036 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12038 /* If it worked, look for more conversion-declarators. */
12039 if (cp_parser_parse_definitely (parser
))
12041 cp_declarator
*declarator
;
12043 /* Parse another optional declarator. */
12044 declarator
= cp_parser_conversion_declarator_opt (parser
);
12046 declarator
= cp_parser_make_indirect_declarator
12047 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12055 /* Parse an (optional) ctor-initializer.
12058 : mem-initializer-list
12060 Returns TRUE iff the ctor-initializer was actually present. */
12063 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12065 /* If the next token is not a `:', then there is no
12066 ctor-initializer. */
12067 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12069 /* Do default initialization of any bases and members. */
12070 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12071 finish_mem_initializers (NULL_TREE
);
12076 /* Consume the `:' token. */
12077 cp_lexer_consume_token (parser
->lexer
);
12078 /* And the mem-initializer-list. */
12079 cp_parser_mem_initializer_list (parser
);
12084 /* Parse a mem-initializer-list.
12086 mem-initializer-list:
12087 mem-initializer ... [opt]
12088 mem-initializer ... [opt] , mem-initializer-list */
12091 cp_parser_mem_initializer_list (cp_parser
* parser
)
12093 tree mem_initializer_list
= NULL_TREE
;
12094 tree target_ctor
= error_mark_node
;
12095 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12097 /* Let the semantic analysis code know that we are starting the
12098 mem-initializer-list. */
12099 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12100 error_at (token
->location
,
12101 "only constructors take member initializers");
12103 /* Loop through the list. */
12106 tree mem_initializer
;
12108 token
= cp_lexer_peek_token (parser
->lexer
);
12109 /* Parse the mem-initializer. */
12110 mem_initializer
= cp_parser_mem_initializer (parser
);
12111 /* If the next token is a `...', we're expanding member initializers. */
12112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12114 /* Consume the `...'. */
12115 cp_lexer_consume_token (parser
->lexer
);
12117 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12118 can be expanded but members cannot. */
12119 if (mem_initializer
!= error_mark_node
12120 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12122 error_at (token
->location
,
12123 "cannot expand initializer for member %<%D%>",
12124 TREE_PURPOSE (mem_initializer
));
12125 mem_initializer
= error_mark_node
;
12128 /* Construct the pack expansion type. */
12129 if (mem_initializer
!= error_mark_node
)
12130 mem_initializer
= make_pack_expansion (mem_initializer
);
12132 if (target_ctor
!= error_mark_node
12133 && mem_initializer
!= error_mark_node
)
12135 error ("mem-initializer for %qD follows constructor delegation",
12136 TREE_PURPOSE (mem_initializer
));
12137 mem_initializer
= error_mark_node
;
12139 /* Look for a target constructor. */
12140 if (mem_initializer
!= error_mark_node
12141 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12142 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12144 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12145 if (mem_initializer_list
)
12147 error ("constructor delegation follows mem-initializer for %qD",
12148 TREE_PURPOSE (mem_initializer_list
));
12149 mem_initializer
= error_mark_node
;
12151 target_ctor
= mem_initializer
;
12153 /* Add it to the list, unless it was erroneous. */
12154 if (mem_initializer
!= error_mark_node
)
12156 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12157 mem_initializer_list
= mem_initializer
;
12159 /* If the next token is not a `,', we're done. */
12160 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12162 /* Consume the `,' token. */
12163 cp_lexer_consume_token (parser
->lexer
);
12166 /* Perform semantic analysis. */
12167 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12168 finish_mem_initializers (mem_initializer_list
);
12171 /* Parse a mem-initializer.
12174 mem-initializer-id ( expression-list [opt] )
12175 mem-initializer-id braced-init-list
12180 ( expression-list [opt] )
12182 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12183 class) or FIELD_DECL (for a non-static data member) to initialize;
12184 the TREE_VALUE is the expression-list. An empty initialization
12185 list is represented by void_list_node. */
12188 cp_parser_mem_initializer (cp_parser
* parser
)
12190 tree mem_initializer_id
;
12191 tree expression_list
;
12193 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12195 /* Find out what is being initialized. */
12196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12198 permerror (token
->location
,
12199 "anachronistic old-style base class initializer");
12200 mem_initializer_id
= NULL_TREE
;
12204 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12205 if (mem_initializer_id
== error_mark_node
)
12206 return mem_initializer_id
;
12208 member
= expand_member_init (mem_initializer_id
);
12209 if (member
&& !DECL_P (member
))
12210 in_base_initializer
= 1;
12212 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12214 bool expr_non_constant_p
;
12215 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12216 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12217 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12218 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12222 vec
<tree
, va_gc
> *vec
;
12223 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12225 /*allow_expansion_p=*/true,
12226 /*non_constant_p=*/NULL
);
12228 return error_mark_node
;
12229 expression_list
= build_tree_list_vec (vec
);
12230 release_tree_vector (vec
);
12233 if (expression_list
== error_mark_node
)
12234 return error_mark_node
;
12235 if (!expression_list
)
12236 expression_list
= void_type_node
;
12238 in_base_initializer
= 0;
12240 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12243 /* Parse a mem-initializer-id.
12245 mem-initializer-id:
12246 :: [opt] nested-name-specifier [opt] class-name
12249 Returns a TYPE indicating the class to be initializer for the first
12250 production. Returns an IDENTIFIER_NODE indicating the data member
12251 to be initialized for the second production. */
12254 cp_parser_mem_initializer_id (cp_parser
* parser
)
12256 bool global_scope_p
;
12257 bool nested_name_specifier_p
;
12258 bool template_p
= false;
12261 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12263 /* `typename' is not allowed in this context ([temp.res]). */
12264 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12266 error_at (token
->location
,
12267 "keyword %<typename%> not allowed in this context (a qualified "
12268 "member initializer is implicitly a type)");
12269 cp_lexer_consume_token (parser
->lexer
);
12271 /* Look for the optional `::' operator. */
12273 = (cp_parser_global_scope_opt (parser
,
12274 /*current_scope_valid_p=*/false)
12276 /* Look for the optional nested-name-specifier. The simplest way to
12281 The keyword `typename' is not permitted in a base-specifier or
12282 mem-initializer; in these contexts a qualified name that
12283 depends on a template-parameter is implicitly assumed to be a
12286 is to assume that we have seen the `typename' keyword at this
12288 nested_name_specifier_p
12289 = (cp_parser_nested_name_specifier_opt (parser
,
12290 /*typename_keyword_p=*/true,
12291 /*check_dependency_p=*/true,
12293 /*is_declaration=*/true)
12295 if (nested_name_specifier_p
)
12296 template_p
= cp_parser_optional_template_keyword (parser
);
12297 /* If there is a `::' operator or a nested-name-specifier, then we
12298 are definitely looking for a class-name. */
12299 if (global_scope_p
|| nested_name_specifier_p
)
12300 return cp_parser_class_name (parser
,
12301 /*typename_keyword_p=*/true,
12302 /*template_keyword_p=*/template_p
,
12304 /*check_dependency_p=*/true,
12305 /*class_head_p=*/false,
12306 /*is_declaration=*/true);
12307 /* Otherwise, we could also be looking for an ordinary identifier. */
12308 cp_parser_parse_tentatively (parser
);
12309 /* Try a class-name. */
12310 id
= cp_parser_class_name (parser
,
12311 /*typename_keyword_p=*/true,
12312 /*template_keyword_p=*/false,
12314 /*check_dependency_p=*/true,
12315 /*class_head_p=*/false,
12316 /*is_declaration=*/true);
12317 /* If we found one, we're done. */
12318 if (cp_parser_parse_definitely (parser
))
12320 /* Otherwise, look for an ordinary identifier. */
12321 return cp_parser_identifier (parser
);
12324 /* Overloading [gram.over] */
12326 /* Parse an operator-function-id.
12328 operator-function-id:
12331 Returns an IDENTIFIER_NODE for the operator which is a
12332 human-readable spelling of the identifier, e.g., `operator +'. */
12335 cp_parser_operator_function_id (cp_parser
* parser
)
12337 /* Look for the `operator' keyword. */
12338 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12339 return error_mark_node
;
12340 /* And then the name of the operator itself. */
12341 return cp_parser_operator (parser
);
12344 /* Return an identifier node for a user-defined literal operator.
12345 The suffix identifier is chained to the operator name identifier. */
12348 cp_literal_operator_id (const char* name
)
12351 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12352 + strlen (name
) + 10);
12353 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12354 identifier
= get_identifier (buffer
);
12359 /* Parse an operator.
12362 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12363 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12364 || ++ -- , ->* -> () []
12371 Returns an IDENTIFIER_NODE for the operator which is a
12372 human-readable spelling of the identifier, e.g., `operator +'. */
12375 cp_parser_operator (cp_parser
* parser
)
12377 tree id
= NULL_TREE
;
12379 bool bad_encoding_prefix
= false;
12381 /* Peek at the next token. */
12382 token
= cp_lexer_peek_token (parser
->lexer
);
12383 /* Figure out which operator we have. */
12384 switch (token
->type
)
12390 /* The keyword should be either `new' or `delete'. */
12391 if (token
->keyword
== RID_NEW
)
12393 else if (token
->keyword
== RID_DELETE
)
12398 /* Consume the `new' or `delete' token. */
12399 cp_lexer_consume_token (parser
->lexer
);
12401 /* Peek at the next token. */
12402 token
= cp_lexer_peek_token (parser
->lexer
);
12403 /* If it's a `[' token then this is the array variant of the
12405 if (token
->type
== CPP_OPEN_SQUARE
)
12407 /* Consume the `[' token. */
12408 cp_lexer_consume_token (parser
->lexer
);
12409 /* Look for the `]' token. */
12410 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12411 id
= ansi_opname (op
== NEW_EXPR
12412 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12414 /* Otherwise, we have the non-array variant. */
12416 id
= ansi_opname (op
);
12422 id
= ansi_opname (PLUS_EXPR
);
12426 id
= ansi_opname (MINUS_EXPR
);
12430 id
= ansi_opname (MULT_EXPR
);
12434 id
= ansi_opname (TRUNC_DIV_EXPR
);
12438 id
= ansi_opname (TRUNC_MOD_EXPR
);
12442 id
= ansi_opname (BIT_XOR_EXPR
);
12446 id
= ansi_opname (BIT_AND_EXPR
);
12450 id
= ansi_opname (BIT_IOR_EXPR
);
12454 id
= ansi_opname (BIT_NOT_EXPR
);
12458 id
= ansi_opname (TRUTH_NOT_EXPR
);
12462 id
= ansi_assopname (NOP_EXPR
);
12466 id
= ansi_opname (LT_EXPR
);
12470 id
= ansi_opname (GT_EXPR
);
12474 id
= ansi_assopname (PLUS_EXPR
);
12478 id
= ansi_assopname (MINUS_EXPR
);
12482 id
= ansi_assopname (MULT_EXPR
);
12486 id
= ansi_assopname (TRUNC_DIV_EXPR
);
12490 id
= ansi_assopname (TRUNC_MOD_EXPR
);
12494 id
= ansi_assopname (BIT_XOR_EXPR
);
12498 id
= ansi_assopname (BIT_AND_EXPR
);
12502 id
= ansi_assopname (BIT_IOR_EXPR
);
12506 id
= ansi_opname (LSHIFT_EXPR
);
12510 id
= ansi_opname (RSHIFT_EXPR
);
12513 case CPP_LSHIFT_EQ
:
12514 id
= ansi_assopname (LSHIFT_EXPR
);
12517 case CPP_RSHIFT_EQ
:
12518 id
= ansi_assopname (RSHIFT_EXPR
);
12522 id
= ansi_opname (EQ_EXPR
);
12526 id
= ansi_opname (NE_EXPR
);
12530 id
= ansi_opname (LE_EXPR
);
12533 case CPP_GREATER_EQ
:
12534 id
= ansi_opname (GE_EXPR
);
12538 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
12542 id
= ansi_opname (TRUTH_ORIF_EXPR
);
12545 case CPP_PLUS_PLUS
:
12546 id
= ansi_opname (POSTINCREMENT_EXPR
);
12549 case CPP_MINUS_MINUS
:
12550 id
= ansi_opname (PREDECREMENT_EXPR
);
12554 id
= ansi_opname (COMPOUND_EXPR
);
12557 case CPP_DEREF_STAR
:
12558 id
= ansi_opname (MEMBER_REF
);
12562 id
= ansi_opname (COMPONENT_REF
);
12565 case CPP_OPEN_PAREN
:
12566 /* Consume the `('. */
12567 cp_lexer_consume_token (parser
->lexer
);
12568 /* Look for the matching `)'. */
12569 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
12570 return ansi_opname (CALL_EXPR
);
12572 case CPP_OPEN_SQUARE
:
12573 /* Consume the `['. */
12574 cp_lexer_consume_token (parser
->lexer
);
12575 /* Look for the matching `]'. */
12576 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12577 return ansi_opname (ARRAY_REF
);
12582 case CPP_UTF8STRING
:
12583 bad_encoding_prefix
= true;
12584 /* Fall through. */
12587 if (cxx_dialect
== cxx98
)
12588 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12589 if (bad_encoding_prefix
)
12591 error ("invalid encoding prefix in literal operator");
12592 return error_mark_node
;
12594 if (TREE_STRING_LENGTH (token
->u
.value
) > 2)
12596 error ("expected empty string after %<operator%> keyword");
12597 return error_mark_node
;
12599 /* Consume the string. */
12600 cp_lexer_consume_token (parser
->lexer
);
12601 /* Look for the suffix identifier. */
12602 token
= cp_lexer_peek_token (parser
->lexer
);
12603 if (token
->type
== CPP_NAME
)
12605 id
= cp_parser_identifier (parser
);
12606 if (id
!= error_mark_node
)
12608 const char *name
= IDENTIFIER_POINTER (id
);
12609 return cp_literal_operator_id (name
);
12612 else if (token
->type
== CPP_KEYWORD
)
12614 error ("unexpected keyword;"
12615 " remove space between quotes and suffix identifier");
12616 return error_mark_node
;
12620 error ("expected suffix identifier");
12621 return error_mark_node
;
12624 case CPP_WSTRING_USERDEF
:
12625 case CPP_STRING16_USERDEF
:
12626 case CPP_STRING32_USERDEF
:
12627 case CPP_UTF8STRING_USERDEF
:
12628 bad_encoding_prefix
= true;
12629 /* Fall through. */
12631 case CPP_STRING_USERDEF
:
12632 if (cxx_dialect
== cxx98
)
12633 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12634 if (bad_encoding_prefix
)
12636 error ("invalid encoding prefix in literal operator");
12637 return error_mark_node
;
12640 tree string_tree
= USERDEF_LITERAL_VALUE (token
->u
.value
);
12641 if (TREE_STRING_LENGTH (string_tree
) > 2)
12643 error ("expected empty string after %<operator%> keyword");
12644 return error_mark_node
;
12646 id
= USERDEF_LITERAL_SUFFIX_ID (token
->u
.value
);
12647 /* Consume the user-defined string literal. */
12648 cp_lexer_consume_token (parser
->lexer
);
12649 if (id
!= error_mark_node
)
12651 const char *name
= IDENTIFIER_POINTER (id
);
12652 return cp_literal_operator_id (name
);
12655 return error_mark_node
;
12659 /* Anything else is an error. */
12663 /* If we have selected an identifier, we need to consume the
12666 cp_lexer_consume_token (parser
->lexer
);
12667 /* Otherwise, no valid operator name was present. */
12670 cp_parser_error (parser
, "expected operator");
12671 id
= error_mark_node
;
12677 /* Parse a template-declaration.
12679 template-declaration:
12680 export [opt] template < template-parameter-list > declaration
12682 If MEMBER_P is TRUE, this template-declaration occurs within a
12685 The grammar rule given by the standard isn't correct. What
12686 is really meant is:
12688 template-declaration:
12689 export [opt] template-parameter-list-seq
12690 decl-specifier-seq [opt] init-declarator [opt] ;
12691 export [opt] template-parameter-list-seq
12692 function-definition
12694 template-parameter-list-seq:
12695 template-parameter-list-seq [opt]
12696 template < template-parameter-list > */
12699 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
12701 /* Check for `export'. */
12702 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
12704 /* Consume the `export' token. */
12705 cp_lexer_consume_token (parser
->lexer
);
12706 /* Warn that we do not support `export'. */
12707 warning (0, "keyword %<export%> not implemented, and will be ignored");
12710 cp_parser_template_declaration_after_export (parser
, member_p
);
12713 /* Parse a template-parameter-list.
12715 template-parameter-list:
12717 template-parameter-list , template-parameter
12719 Returns a TREE_LIST. Each node represents a template parameter.
12720 The nodes are connected via their TREE_CHAINs. */
12723 cp_parser_template_parameter_list (cp_parser
* parser
)
12725 tree parameter_list
= NULL_TREE
;
12727 begin_template_parm_list ();
12729 /* The loop below parses the template parms. We first need to know
12730 the total number of template parms to be able to compute proper
12731 canonical types of each dependent type. So after the loop, when
12732 we know the total number of template parms,
12733 end_template_parm_list computes the proper canonical types and
12734 fixes up the dependent types accordingly. */
12739 bool is_parameter_pack
;
12740 location_t parm_loc
;
12742 /* Parse the template-parameter. */
12743 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12744 parameter
= cp_parser_template_parameter (parser
,
12746 &is_parameter_pack
);
12747 /* Add it to the list. */
12748 if (parameter
!= error_mark_node
)
12749 parameter_list
= process_template_parm (parameter_list
,
12753 is_parameter_pack
);
12756 tree err_parm
= build_tree_list (parameter
, parameter
);
12757 parameter_list
= chainon (parameter_list
, err_parm
);
12760 /* If the next token is not a `,', we're done. */
12761 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12763 /* Otherwise, consume the `,' token. */
12764 cp_lexer_consume_token (parser
->lexer
);
12767 return end_template_parm_list (parameter_list
);
12770 /* Parse a template-parameter.
12772 template-parameter:
12774 parameter-declaration
12776 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12777 the parameter. The TREE_PURPOSE is the default value, if any.
12778 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12779 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12780 set to true iff this parameter is a parameter pack. */
12783 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
12784 bool *is_parameter_pack
)
12787 cp_parameter_declarator
*parameter_declarator
;
12788 cp_declarator
*id_declarator
;
12791 /* Assume it is a type parameter or a template parameter. */
12792 *is_non_type
= false;
12793 /* Assume it not a parameter pack. */
12794 *is_parameter_pack
= false;
12795 /* Peek at the next token. */
12796 token
= cp_lexer_peek_token (parser
->lexer
);
12797 /* If it is `class' or `template', we have a type-parameter. */
12798 if (token
->keyword
== RID_TEMPLATE
)
12799 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12800 /* If it is `class' or `typename' we do not know yet whether it is a
12801 type parameter or a non-type parameter. Consider:
12803 template <typename T, typename T::X X> ...
12807 template <class C, class D*> ...
12809 Here, the first parameter is a type parameter, and the second is
12810 a non-type parameter. We can tell by looking at the token after
12811 the identifier -- if it is a `,', `=', or `>' then we have a type
12813 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
12815 /* Peek at the token after `class' or `typename'. */
12816 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12817 /* If it's an ellipsis, we have a template type parameter
12819 if (token
->type
== CPP_ELLIPSIS
)
12820 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12821 /* If it's an identifier, skip it. */
12822 if (token
->type
== CPP_NAME
)
12823 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
12824 /* Now, see if the token looks like the end of a template
12826 if (token
->type
== CPP_COMMA
12827 || token
->type
== CPP_EQ
12828 || token
->type
== CPP_GREATER
)
12829 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12832 /* Otherwise, it is a non-type parameter.
12836 When parsing a default template-argument for a non-type
12837 template-parameter, the first non-nested `>' is taken as the end
12838 of the template parameter-list rather than a greater-than
12840 *is_non_type
= true;
12841 parameter_declarator
12842 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
12843 /*parenthesized_p=*/NULL
);
12845 /* If the parameter declaration is marked as a parameter pack, set
12846 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12847 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12849 if (parameter_declarator
12850 && parameter_declarator
->declarator
12851 && parameter_declarator
->declarator
->parameter_pack_p
)
12853 *is_parameter_pack
= true;
12854 parameter_declarator
->declarator
->parameter_pack_p
= false;
12857 if (parameter_declarator
12858 && parameter_declarator
->default_argument
)
12860 /* Can happen in some cases of erroneous input (c++/34892). */
12861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12862 /* Consume the `...' for better error recovery. */
12863 cp_lexer_consume_token (parser
->lexer
);
12865 /* If the next token is an ellipsis, and we don't already have it
12866 marked as a parameter pack, then we have a parameter pack (that
12867 has no declarator). */
12868 else if (!*is_parameter_pack
12869 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12870 && (declarator_can_be_parameter_pack
12871 (parameter_declarator
->declarator
)))
12873 /* Consume the `...'. */
12874 cp_lexer_consume_token (parser
->lexer
);
12875 maybe_warn_variadic_templates ();
12877 *is_parameter_pack
= true;
12879 /* We might end up with a pack expansion as the type of the non-type
12880 template parameter, in which case this is a non-type template
12882 else if (parameter_declarator
12883 && parameter_declarator
->decl_specifiers
.type
12884 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
12886 *is_parameter_pack
= true;
12887 parameter_declarator
->decl_specifiers
.type
=
12888 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
12891 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12893 /* Parameter packs cannot have default arguments. However, a
12894 user may try to do so, so we'll parse them and give an
12895 appropriate diagnostic here. */
12897 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12899 /* Find the name of the parameter pack. */
12900 id_declarator
= parameter_declarator
->declarator
;
12901 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
12902 id_declarator
= id_declarator
->declarator
;
12904 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
12905 error_at (start_token
->location
,
12906 "template parameter pack %qD cannot have a default argument",
12907 id_declarator
->u
.id
.unqualified_name
);
12909 error_at (start_token
->location
,
12910 "template parameter pack cannot have a default argument");
12912 /* Parse the default argument, but throw away the result. */
12913 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
12916 parm
= grokdeclarator (parameter_declarator
->declarator
,
12917 ¶meter_declarator
->decl_specifiers
,
12918 TPARM
, /*initialized=*/0,
12919 /*attrlist=*/NULL
);
12920 if (parm
== error_mark_node
)
12921 return error_mark_node
;
12923 return build_tree_list (parameter_declarator
->default_argument
, parm
);
12926 /* Parse a type-parameter.
12929 class identifier [opt]
12930 class identifier [opt] = type-id
12931 typename identifier [opt]
12932 typename identifier [opt] = type-id
12933 template < template-parameter-list > class identifier [opt]
12934 template < template-parameter-list > class identifier [opt]
12937 GNU Extension (variadic templates):
12940 class ... identifier [opt]
12941 typename ... identifier [opt]
12943 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12944 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12945 the declaration of the parameter.
12947 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12950 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
12955 /* Look for a keyword to tell us what kind of parameter this is. */
12956 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
12958 return error_mark_node
;
12960 switch (token
->keyword
)
12966 tree default_argument
;
12968 /* If the next token is an ellipsis, we have a template
12970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12972 /* Consume the `...' token. */
12973 cp_lexer_consume_token (parser
->lexer
);
12974 maybe_warn_variadic_templates ();
12976 *is_parameter_pack
= true;
12979 /* If the next token is an identifier, then it names the
12981 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12982 identifier
= cp_parser_identifier (parser
);
12984 identifier
= NULL_TREE
;
12986 /* Create the parameter. */
12987 parameter
= finish_template_type_parm (class_type_node
, identifier
);
12989 /* If the next token is an `=', we have a default argument. */
12990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12992 /* Consume the `=' token. */
12993 cp_lexer_consume_token (parser
->lexer
);
12994 /* Parse the default-argument. */
12995 push_deferring_access_checks (dk_no_deferred
);
12996 default_argument
= cp_parser_type_id (parser
);
12998 /* Template parameter packs cannot have default
13000 if (*is_parameter_pack
)
13003 error_at (token
->location
,
13004 "template parameter pack %qD cannot have a "
13005 "default argument", identifier
);
13007 error_at (token
->location
,
13008 "template parameter packs cannot have "
13009 "default arguments");
13010 default_argument
= NULL_TREE
;
13012 pop_deferring_access_checks ();
13015 default_argument
= NULL_TREE
;
13017 /* Create the combined representation of the parameter and the
13018 default argument. */
13019 parameter
= build_tree_list (default_argument
, parameter
);
13026 tree default_argument
;
13028 /* Look for the `<'. */
13029 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13030 /* Parse the template-parameter-list. */
13031 cp_parser_template_parameter_list (parser
);
13032 /* Look for the `>'. */
13033 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13034 /* Look for the `class' keyword. */
13035 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
13036 /* If the next token is an ellipsis, we have a template
13038 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13040 /* Consume the `...' token. */
13041 cp_lexer_consume_token (parser
->lexer
);
13042 maybe_warn_variadic_templates ();
13044 *is_parameter_pack
= true;
13046 /* If the next token is an `=', then there is a
13047 default-argument. If the next token is a `>', we are at
13048 the end of the parameter-list. If the next token is a `,',
13049 then we are at the end of this parameter. */
13050 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13051 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13052 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13054 identifier
= cp_parser_identifier (parser
);
13055 /* Treat invalid names as if the parameter were nameless. */
13056 if (identifier
== error_mark_node
)
13057 identifier
= NULL_TREE
;
13060 identifier
= NULL_TREE
;
13062 /* Create the template parameter. */
13063 parameter
= finish_template_template_parm (class_type_node
,
13066 /* If the next token is an `=', then there is a
13067 default-argument. */
13068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13072 /* Consume the `='. */
13073 cp_lexer_consume_token (parser
->lexer
);
13074 /* Parse the id-expression. */
13075 push_deferring_access_checks (dk_no_deferred
);
13076 /* save token before parsing the id-expression, for error
13078 token
= cp_lexer_peek_token (parser
->lexer
);
13080 = cp_parser_id_expression (parser
,
13081 /*template_keyword_p=*/false,
13082 /*check_dependency_p=*/true,
13083 /*template_p=*/&is_template
,
13084 /*declarator_p=*/false,
13085 /*optional_p=*/false);
13086 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13087 /* If the id-expression was a template-id that refers to
13088 a template-class, we already have the declaration here,
13089 so no further lookup is needed. */
13092 /* Look up the name. */
13094 = cp_parser_lookup_name (parser
, default_argument
,
13096 /*is_template=*/is_template
,
13097 /*is_namespace=*/false,
13098 /*check_dependency=*/true,
13099 /*ambiguous_decls=*/NULL
,
13101 /* See if the default argument is valid. */
13103 = check_template_template_default_arg (default_argument
);
13105 /* Template parameter packs cannot have default
13107 if (*is_parameter_pack
)
13110 error_at (token
->location
,
13111 "template parameter pack %qD cannot "
13112 "have a default argument",
13115 error_at (token
->location
, "template parameter packs cannot "
13116 "have default arguments");
13117 default_argument
= NULL_TREE
;
13119 pop_deferring_access_checks ();
13122 default_argument
= NULL_TREE
;
13124 /* Create the combined representation of the parameter and the
13125 default argument. */
13126 parameter
= build_tree_list (default_argument
, parameter
);
13131 gcc_unreachable ();
13138 /* Parse a template-id.
13141 template-name < template-argument-list [opt] >
13143 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13144 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13145 returned. Otherwise, if the template-name names a function, or set
13146 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13147 names a class, returns a TYPE_DECL for the specialization.
13149 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13150 uninstantiated templates. */
13153 cp_parser_template_id (cp_parser
*parser
,
13154 bool template_keyword_p
,
13155 bool check_dependency_p
,
13156 enum tag_types tag_type
,
13157 bool is_declaration
)
13163 cp_token_position start_of_id
= 0;
13164 deferred_access_check
*chk
;
13165 vec
<deferred_access_check
, va_gc
> *access_check
;
13166 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13167 bool is_identifier
;
13169 /* If the next token corresponds to a template-id, there is no need
13171 next_token
= cp_lexer_peek_token (parser
->lexer
);
13172 if (next_token
->type
== CPP_TEMPLATE_ID
)
13174 struct tree_check
*check_value
;
13176 /* Get the stored value. */
13177 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13178 /* Perform any access checks that were deferred. */
13179 access_check
= check_value
->checks
;
13182 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13183 perform_or_defer_access_check (chk
->binfo
,
13186 tf_warning_or_error
);
13188 /* Return the stored value. */
13189 return check_value
->value
;
13192 /* Avoid performing name lookup if there is no possibility of
13193 finding a template-id. */
13194 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13195 || (next_token
->type
== CPP_NAME
13196 && !cp_parser_nth_token_starts_template_argument_list_p
13199 cp_parser_error (parser
, "expected template-id");
13200 return error_mark_node
;
13203 /* Remember where the template-id starts. */
13204 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13205 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13207 push_deferring_access_checks (dk_deferred
);
13209 /* Parse the template-name. */
13210 is_identifier
= false;
13211 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13212 check_dependency_p
,
13216 if (templ
== error_mark_node
|| is_identifier
)
13218 pop_deferring_access_checks ();
13222 /* If we find the sequence `[:' after a template-name, it's probably
13223 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13224 parse correctly the argument list. */
13225 next_token
= cp_lexer_peek_token (parser
->lexer
);
13226 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13227 if (next_token
->type
== CPP_OPEN_SQUARE
13228 && next_token
->flags
& DIGRAPH
13229 && next_token_2
->type
== CPP_COLON
13230 && !(next_token_2
->flags
& PREV_WHITE
))
13232 cp_parser_parse_tentatively (parser
);
13233 /* Change `:' into `::'. */
13234 next_token_2
->type
= CPP_SCOPE
;
13235 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13237 cp_lexer_consume_token (parser
->lexer
);
13239 /* Parse the arguments. */
13240 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13241 if (!cp_parser_parse_definitely (parser
))
13243 /* If we couldn't parse an argument list, then we revert our changes
13244 and return simply an error. Maybe this is not a template-id
13246 next_token_2
->type
= CPP_COLON
;
13247 cp_parser_error (parser
, "expected %<<%>");
13248 pop_deferring_access_checks ();
13249 return error_mark_node
;
13251 /* Otherwise, emit an error about the invalid digraph, but continue
13252 parsing because we got our argument list. */
13253 if (permerror (next_token
->location
,
13254 "%<<::%> cannot begin a template-argument list"))
13256 static bool hint
= false;
13257 inform (next_token
->location
,
13258 "%<<:%> is an alternate spelling for %<[%>."
13259 " Insert whitespace between %<<%> and %<::%>");
13260 if (!hint
&& !flag_permissive
)
13262 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13263 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13264 "accept your code)");
13271 /* Look for the `<' that starts the template-argument-list. */
13272 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13274 pop_deferring_access_checks ();
13275 return error_mark_node
;
13277 /* Parse the arguments. */
13278 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13281 /* Build a representation of the specialization. */
13282 if (identifier_p (templ
))
13283 template_id
= build_min_nt_loc (next_token
->location
,
13286 else if (DECL_TYPE_TEMPLATE_P (templ
)
13287 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13289 bool entering_scope
;
13290 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13291 template (rather than some instantiation thereof) only if
13292 is not nested within some other construct. For example, in
13293 "template <typename T> void f(T) { A<T>::", A<T> is just an
13294 instantiation of A. */
13295 entering_scope
= (template_parm_scope_p ()
13296 && cp_lexer_next_token_is (parser
->lexer
,
13299 = finish_template_type (templ
, arguments
, entering_scope
);
13303 /* If it's not a class-template or a template-template, it should be
13304 a function-template. */
13305 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13306 || TREE_CODE (templ
) == OVERLOAD
13307 || BASELINK_P (templ
)));
13309 template_id
= lookup_template_function (templ
, arguments
);
13312 /* If parsing tentatively, replace the sequence of tokens that makes
13313 up the template-id with a CPP_TEMPLATE_ID token. That way,
13314 should we re-parse the token stream, we will not have to repeat
13315 the effort required to do the parse, nor will we issue duplicate
13316 error messages about problems during instantiation of the
13320 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13322 /* Reset the contents of the START_OF_ID token. */
13323 token
->type
= CPP_TEMPLATE_ID
;
13324 /* Retrieve any deferred checks. Do not pop this access checks yet
13325 so the memory will not be reclaimed during token replacing below. */
13326 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
13327 token
->u
.tree_check_value
->value
= template_id
;
13328 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13329 token
->keyword
= RID_MAX
;
13331 /* Purge all subsequent tokens. */
13332 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13334 /* ??? Can we actually assume that, if template_id ==
13335 error_mark_node, we will have issued a diagnostic to the
13336 user, as opposed to simply marking the tentative parse as
13338 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13339 error_at (token
->location
, "parse error in template argument list");
13342 pop_to_parent_deferring_access_checks ();
13343 return template_id
;
13346 /* Parse a template-name.
13351 The standard should actually say:
13355 operator-function-id
13357 A defect report has been filed about this issue.
13359 A conversion-function-id cannot be a template name because they cannot
13360 be part of a template-id. In fact, looking at this code:
13362 a.operator K<int>()
13364 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13365 It is impossible to call a templated conversion-function-id with an
13366 explicit argument list, since the only allowed template parameter is
13367 the type to which it is converting.
13369 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13370 `template' keyword, in a construction like:
13374 In that case `f' is taken to be a template-name, even though there
13375 is no way of knowing for sure.
13377 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13378 name refers to a set of overloaded functions, at least one of which
13379 is a template, or an IDENTIFIER_NODE with the name of the template,
13380 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13381 names are looked up inside uninstantiated templates. */
13384 cp_parser_template_name (cp_parser
* parser
,
13385 bool template_keyword_p
,
13386 bool check_dependency_p
,
13387 bool is_declaration
,
13388 enum tag_types tag_type
,
13389 bool *is_identifier
)
13394 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13396 /* If the next token is `operator', then we have either an
13397 operator-function-id or a conversion-function-id. */
13398 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13400 /* We don't know whether we're looking at an
13401 operator-function-id or a conversion-function-id. */
13402 cp_parser_parse_tentatively (parser
);
13403 /* Try an operator-function-id. */
13404 identifier
= cp_parser_operator_function_id (parser
);
13405 /* If that didn't work, try a conversion-function-id. */
13406 if (!cp_parser_parse_definitely (parser
))
13408 cp_parser_error (parser
, "expected template-name");
13409 return error_mark_node
;
13412 /* Look for the identifier. */
13414 identifier
= cp_parser_identifier (parser
);
13416 /* If we didn't find an identifier, we don't have a template-id. */
13417 if (identifier
== error_mark_node
)
13418 return error_mark_node
;
13420 /* If the name immediately followed the `template' keyword, then it
13421 is a template-name. However, if the next token is not `<', then
13422 we do not treat it as a template-name, since it is not being used
13423 as part of a template-id. This enables us to handle constructs
13426 template <typename T> struct S { S(); };
13427 template <typename T> S<T>::S();
13429 correctly. We would treat `S' as a template -- if it were `S<T>'
13430 -- but we do not if there is no `<'. */
13432 if (processing_template_decl
13433 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13435 /* In a declaration, in a dependent context, we pretend that the
13436 "template" keyword was present in order to improve error
13437 recovery. For example, given:
13439 template <typename T> void f(T::X<int>);
13441 we want to treat "X<int>" as a template-id. */
13443 && !template_keyword_p
13444 && parser
->scope
&& TYPE_P (parser
->scope
)
13445 && check_dependency_p
13446 && dependent_scope_p (parser
->scope
)
13447 /* Do not do this for dtors (or ctors), since they never
13448 need the template keyword before their name. */
13449 && !constructor_name_p (identifier
, parser
->scope
))
13451 cp_token_position start
= 0;
13453 /* Explain what went wrong. */
13454 error_at (token
->location
, "non-template %qD used as template",
13456 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
13457 parser
->scope
, identifier
);
13458 /* If parsing tentatively, find the location of the "<" token. */
13459 if (cp_parser_simulate_error (parser
))
13460 start
= cp_lexer_token_position (parser
->lexer
, true);
13461 /* Parse the template arguments so that we can issue error
13462 messages about them. */
13463 cp_lexer_consume_token (parser
->lexer
);
13464 cp_parser_enclosed_template_argument_list (parser
);
13465 /* Skip tokens until we find a good place from which to
13466 continue parsing. */
13467 cp_parser_skip_to_closing_parenthesis (parser
,
13468 /*recovering=*/true,
13470 /*consume_paren=*/false);
13471 /* If parsing tentatively, permanently remove the
13472 template argument list. That will prevent duplicate
13473 error messages from being issued about the missing
13474 "template" keyword. */
13476 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
13478 *is_identifier
= true;
13482 /* If the "template" keyword is present, then there is generally
13483 no point in doing name-lookup, so we just return IDENTIFIER.
13484 But, if the qualifying scope is non-dependent then we can
13485 (and must) do name-lookup normally. */
13486 if (template_keyword_p
13488 || (TYPE_P (parser
->scope
)
13489 && dependent_type_p (parser
->scope
))))
13493 /* Look up the name. */
13494 decl
= cp_parser_lookup_name (parser
, identifier
,
13496 /*is_template=*/true,
13497 /*is_namespace=*/false,
13498 check_dependency_p
,
13499 /*ambiguous_decls=*/NULL
,
13502 /* If DECL is a template, then the name was a template-name. */
13503 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
13507 tree fn
= NULL_TREE
;
13509 /* The standard does not explicitly indicate whether a name that
13510 names a set of overloaded declarations, some of which are
13511 templates, is a template-name. However, such a name should
13512 be a template-name; otherwise, there is no way to form a
13513 template-id for the overloaded templates. */
13514 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
13515 if (TREE_CODE (fns
) == OVERLOAD
)
13516 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
13517 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
13522 /* The name does not name a template. */
13523 cp_parser_error (parser
, "expected template-name");
13524 return error_mark_node
;
13528 /* If DECL is dependent, and refers to a function, then just return
13529 its name; we will look it up again during template instantiation. */
13530 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
13532 tree scope
= ovl_scope (decl
);
13533 if (TYPE_P (scope
) && dependent_type_p (scope
))
13540 /* Parse a template-argument-list.
13542 template-argument-list:
13543 template-argument ... [opt]
13544 template-argument-list , template-argument ... [opt]
13546 Returns a TREE_VEC containing the arguments. */
13549 cp_parser_template_argument_list (cp_parser
* parser
)
13551 tree fixed_args
[10];
13552 unsigned n_args
= 0;
13553 unsigned alloced
= 10;
13554 tree
*arg_ary
= fixed_args
;
13556 bool saved_in_template_argument_list_p
;
13558 bool saved_non_ice_p
;
13560 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
13561 parser
->in_template_argument_list_p
= true;
13562 /* Even if the template-id appears in an integral
13563 constant-expression, the contents of the argument list do
13565 saved_ice_p
= parser
->integral_constant_expression_p
;
13566 parser
->integral_constant_expression_p
= false;
13567 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
13568 parser
->non_integral_constant_expression_p
= false;
13570 /* Parse the arguments. */
13576 /* Consume the comma. */
13577 cp_lexer_consume_token (parser
->lexer
);
13579 /* Parse the template-argument. */
13580 argument
= cp_parser_template_argument (parser
);
13582 /* If the next token is an ellipsis, we're expanding a template
13584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13586 if (argument
== error_mark_node
)
13588 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13589 error_at (token
->location
,
13590 "expected parameter pack before %<...%>");
13592 /* Consume the `...' token. */
13593 cp_lexer_consume_token (parser
->lexer
);
13595 /* Make the argument into a TYPE_PACK_EXPANSION or
13596 EXPR_PACK_EXPANSION. */
13597 argument
= make_pack_expansion (argument
);
13600 if (n_args
== alloced
)
13604 if (arg_ary
== fixed_args
)
13606 arg_ary
= XNEWVEC (tree
, alloced
);
13607 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
13610 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
13612 arg_ary
[n_args
++] = argument
;
13614 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
13616 vec
= make_tree_vec (n_args
);
13619 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
13621 if (arg_ary
!= fixed_args
)
13623 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
13624 parser
->integral_constant_expression_p
= saved_ice_p
;
13625 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
13626 #ifdef ENABLE_CHECKING
13627 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
13632 /* Parse a template-argument.
13635 assignment-expression
13639 The representation is that of an assignment-expression, type-id, or
13640 id-expression -- except that the qualified id-expression is
13641 evaluated, so that the value returned is either a DECL or an
13644 Although the standard says "assignment-expression", it forbids
13645 throw-expressions or assignments in the template argument.
13646 Therefore, we use "conditional-expression" instead. */
13649 cp_parser_template_argument (cp_parser
* parser
)
13654 bool maybe_type_id
= false;
13655 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
13656 location_t loc
= 0;
13659 /* There's really no way to know what we're looking at, so we just
13660 try each alternative in order.
13664 In a template-argument, an ambiguity between a type-id and an
13665 expression is resolved to a type-id, regardless of the form of
13666 the corresponding template-parameter.
13668 Therefore, we try a type-id first. */
13669 cp_parser_parse_tentatively (parser
);
13670 argument
= cp_parser_template_type_arg (parser
);
13671 /* If there was no error parsing the type-id but the next token is a
13672 '>>', our behavior depends on which dialect of C++ we're
13673 parsing. In C++98, we probably found a typo for '> >'. But there
13674 are type-id which are also valid expressions. For instance:
13676 struct X { int operator >> (int); };
13677 template <int V> struct Foo {};
13680 Here 'X()' is a valid type-id of a function type, but the user just
13681 wanted to write the expression "X() >> 5". Thus, we remember that we
13682 found a valid type-id, but we still try to parse the argument as an
13683 expression to see what happens.
13685 In C++0x, the '>>' will be considered two separate '>'
13687 if (!cp_parser_error_occurred (parser
)
13688 && cxx_dialect
== cxx98
13689 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
13691 maybe_type_id
= true;
13692 cp_parser_abort_tentative_parse (parser
);
13696 /* If the next token isn't a `,' or a `>', then this argument wasn't
13697 really finished. This means that the argument is not a valid
13699 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13700 cp_parser_error (parser
, "expected template-argument");
13701 /* If that worked, we're done. */
13702 if (cp_parser_parse_definitely (parser
))
13705 /* We're still not sure what the argument will be. */
13706 cp_parser_parse_tentatively (parser
);
13707 /* Try a template. */
13708 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
13709 argument
= cp_parser_id_expression (parser
,
13710 /*template_keyword_p=*/false,
13711 /*check_dependency_p=*/true,
13713 /*declarator_p=*/false,
13714 /*optional_p=*/false);
13715 /* If the next token isn't a `,' or a `>', then this argument wasn't
13716 really finished. */
13717 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13718 cp_parser_error (parser
, "expected template-argument");
13719 if (!cp_parser_error_occurred (parser
))
13721 /* Figure out what is being referred to. If the id-expression
13722 was for a class template specialization, then we will have a
13723 TYPE_DECL at this point. There is no need to do name lookup
13724 at this point in that case. */
13725 if (TREE_CODE (argument
) != TYPE_DECL
)
13726 argument
= cp_parser_lookup_name (parser
, argument
,
13728 /*is_template=*/template_p
,
13729 /*is_namespace=*/false,
13730 /*check_dependency=*/true,
13731 /*ambiguous_decls=*/NULL
,
13732 argument_start_token
->location
);
13733 if (TREE_CODE (argument
) != TEMPLATE_DECL
13734 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
13735 cp_parser_error (parser
, "expected template-name");
13737 if (cp_parser_parse_definitely (parser
))
13739 /* It must be a non-type argument. There permitted cases are given
13740 in [temp.arg.nontype]:
13742 -- an integral constant-expression of integral or enumeration
13745 -- the name of a non-type template-parameter; or
13747 -- the name of an object or function with external linkage...
13749 -- the address of an object or function with external linkage...
13751 -- a pointer to member... */
13752 /* Look for a non-type template parameter. */
13753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13755 cp_parser_parse_tentatively (parser
);
13756 argument
= cp_parser_primary_expression (parser
,
13757 /*address_p=*/false,
13759 /*template_arg_p=*/true,
13761 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
13762 || !cp_parser_next_token_ends_template_argument_p (parser
))
13763 cp_parser_simulate_error (parser
);
13764 if (cp_parser_parse_definitely (parser
))
13768 /* If the next token is "&", the argument must be the address of an
13769 object or function with external linkage. */
13770 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
13773 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13774 cp_lexer_consume_token (parser
->lexer
);
13776 /* See if we might have an id-expression. */
13777 token
= cp_lexer_peek_token (parser
->lexer
);
13778 if (token
->type
== CPP_NAME
13779 || token
->keyword
== RID_OPERATOR
13780 || token
->type
== CPP_SCOPE
13781 || token
->type
== CPP_TEMPLATE_ID
13782 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
13784 cp_parser_parse_tentatively (parser
);
13785 argument
= cp_parser_primary_expression (parser
,
13788 /*template_arg_p=*/true,
13790 if (cp_parser_error_occurred (parser
)
13791 || !cp_parser_next_token_ends_template_argument_p (parser
))
13792 cp_parser_abort_tentative_parse (parser
);
13797 if (INDIRECT_REF_P (argument
))
13799 gcc_assert (REFERENCE_REF_P (argument
));
13800 argument
= TREE_OPERAND (argument
, 0);
13803 /* If we're in a template, we represent a qualified-id referring
13804 to a static data member as a SCOPE_REF even if the scope isn't
13805 dependent so that we can check access control later. */
13807 if (TREE_CODE (probe
) == SCOPE_REF
)
13808 probe
= TREE_OPERAND (probe
, 1);
13811 /* A variable without external linkage might still be a
13812 valid constant-expression, so no error is issued here
13813 if the external-linkage check fails. */
13814 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
13815 cp_parser_simulate_error (parser
);
13817 else if (is_overloaded_fn (argument
))
13818 /* All overloaded functions are allowed; if the external
13819 linkage test does not pass, an error will be issued
13823 && (TREE_CODE (argument
) == OFFSET_REF
13824 || TREE_CODE (argument
) == SCOPE_REF
))
13825 /* A pointer-to-member. */
13827 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
13830 cp_parser_simulate_error (parser
);
13832 if (cp_parser_parse_definitely (parser
))
13835 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
13836 tf_warning_or_error
);
13841 /* If the argument started with "&", there are no other valid
13842 alternatives at this point. */
13845 cp_parser_error (parser
, "invalid non-type template argument");
13846 return error_mark_node
;
13849 /* If the argument wasn't successfully parsed as a type-id followed
13850 by '>>', the argument can only be a constant expression now.
13851 Otherwise, we try parsing the constant-expression tentatively,
13852 because the argument could really be a type-id. */
13854 cp_parser_parse_tentatively (parser
);
13855 argument
= cp_parser_constant_expression (parser
,
13856 /*allow_non_constant_p=*/false,
13857 /*non_constant_p=*/NULL
);
13858 if (!maybe_type_id
)
13860 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13861 cp_parser_error (parser
, "expected template-argument");
13862 if (cp_parser_parse_definitely (parser
))
13864 /* We did our best to parse the argument as a non type-id, but that
13865 was the only alternative that matched (albeit with a '>' after
13866 it). We can assume it's just a typo from the user, and a
13867 diagnostic will then be issued. */
13868 return cp_parser_template_type_arg (parser
);
13871 /* Parse an explicit-instantiation.
13873 explicit-instantiation:
13874 template declaration
13876 Although the standard says `declaration', what it really means is:
13878 explicit-instantiation:
13879 template decl-specifier-seq [opt] declarator [opt] ;
13881 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13882 supposed to be allowed. A defect report has been filed about this
13887 explicit-instantiation:
13888 storage-class-specifier template
13889 decl-specifier-seq [opt] declarator [opt] ;
13890 function-specifier template
13891 decl-specifier-seq [opt] declarator [opt] ; */
13894 cp_parser_explicit_instantiation (cp_parser
* parser
)
13896 int declares_class_or_enum
;
13897 cp_decl_specifier_seq decl_specifiers
;
13898 tree extension_specifier
= NULL_TREE
;
13900 timevar_push (TV_TEMPLATE_INST
);
13902 /* Look for an (optional) storage-class-specifier or
13903 function-specifier. */
13904 if (cp_parser_allow_gnu_extensions_p (parser
))
13906 extension_specifier
13907 = cp_parser_storage_class_specifier_opt (parser
);
13908 if (!extension_specifier
)
13909 extension_specifier
13910 = cp_parser_function_specifier_opt (parser
,
13911 /*decl_specs=*/NULL
);
13914 /* Look for the `template' keyword. */
13915 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
13916 /* Let the front end know that we are processing an explicit
13918 begin_explicit_instantiation ();
13919 /* [temp.explicit] says that we are supposed to ignore access
13920 control while processing explicit instantiation directives. */
13921 push_deferring_access_checks (dk_no_check
);
13922 /* Parse a decl-specifier-seq. */
13923 cp_parser_decl_specifier_seq (parser
,
13924 CP_PARSER_FLAGS_OPTIONAL
,
13926 &declares_class_or_enum
);
13927 /* If there was exactly one decl-specifier, and it declared a class,
13928 and there's no declarator, then we have an explicit type
13930 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
13934 type
= check_tag_decl (&decl_specifiers
,
13935 /*explicit_type_instantiation_p=*/true);
13936 /* Turn access control back on for names used during
13937 template instantiation. */
13938 pop_deferring_access_checks ();
13940 do_type_instantiation (type
, extension_specifier
,
13941 /*complain=*/tf_error
);
13945 cp_declarator
*declarator
;
13948 /* Parse the declarator. */
13950 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13951 /*ctor_dtor_or_conv_p=*/NULL
,
13952 /*parenthesized_p=*/NULL
,
13953 /*member_p=*/false);
13954 if (declares_class_or_enum
& 2)
13955 cp_parser_check_for_definition_in_return_type (declarator
,
13956 decl_specifiers
.type
,
13957 decl_specifiers
.locations
[ds_type_spec
]);
13958 if (declarator
!= cp_error_declarator
)
13960 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
13961 permerror (decl_specifiers
.locations
[ds_inline
],
13962 "explicit instantiation shall not use"
13963 " %<inline%> specifier");
13964 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
13965 permerror (decl_specifiers
.locations
[ds_constexpr
],
13966 "explicit instantiation shall not use"
13967 " %<constexpr%> specifier");
13969 decl
= grokdeclarator (declarator
, &decl_specifiers
,
13970 NORMAL
, 0, &decl_specifiers
.attributes
);
13971 /* Turn access control back on for names used during
13972 template instantiation. */
13973 pop_deferring_access_checks ();
13974 /* Do the explicit instantiation. */
13975 do_decl_instantiation (decl
, extension_specifier
);
13979 pop_deferring_access_checks ();
13980 /* Skip the body of the explicit instantiation. */
13981 cp_parser_skip_to_end_of_statement (parser
);
13984 /* We're done with the instantiation. */
13985 end_explicit_instantiation ();
13987 cp_parser_consume_semicolon_at_end_of_statement (parser
);
13989 timevar_pop (TV_TEMPLATE_INST
);
13992 /* Parse an explicit-specialization.
13994 explicit-specialization:
13995 template < > declaration
13997 Although the standard says `declaration', what it really means is:
13999 explicit-specialization:
14000 template <> decl-specifier [opt] init-declarator [opt] ;
14001 template <> function-definition
14002 template <> explicit-specialization
14003 template <> template-declaration */
14006 cp_parser_explicit_specialization (cp_parser
* parser
)
14008 bool need_lang_pop
;
14009 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14011 /* Look for the `template' keyword. */
14012 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14013 /* Look for the `<'. */
14014 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14015 /* Look for the `>'. */
14016 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14017 /* We have processed another parameter list. */
14018 ++parser
->num_template_parameter_lists
;
14021 A template ... explicit specialization ... shall not have C
14023 if (current_lang_name
== lang_name_c
)
14025 error_at (token
->location
, "template specialization with C linkage");
14026 /* Give it C++ linkage to avoid confusing other parts of the
14028 push_lang_context (lang_name_cplusplus
);
14029 need_lang_pop
= true;
14032 need_lang_pop
= false;
14033 /* Let the front end know that we are beginning a specialization. */
14034 if (!begin_specialization ())
14036 end_specialization ();
14040 /* If the next keyword is `template', we need to figure out whether
14041 or not we're looking a template-declaration. */
14042 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14044 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14045 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14046 cp_parser_template_declaration_after_export (parser
,
14047 /*member_p=*/false);
14049 cp_parser_explicit_specialization (parser
);
14052 /* Parse the dependent declaration. */
14053 cp_parser_single_declaration (parser
,
14055 /*member_p=*/false,
14056 /*explicit_specialization_p=*/true,
14057 /*friend_p=*/NULL
);
14058 /* We're done with the specialization. */
14059 end_specialization ();
14060 /* For the erroneous case of a template with C linkage, we pushed an
14061 implicit C++ linkage scope; exit that scope now. */
14063 pop_lang_context ();
14064 /* We're done with this parameter list. */
14065 --parser
->num_template_parameter_lists
;
14068 /* Parse a type-specifier.
14071 simple-type-specifier
14074 elaborated-type-specifier
14082 Returns a representation of the type-specifier. For a
14083 class-specifier, enum-specifier, or elaborated-type-specifier, a
14084 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14086 The parser flags FLAGS is used to control type-specifier parsing.
14088 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14089 in a decl-specifier-seq.
14091 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14092 class-specifier, enum-specifier, or elaborated-type-specifier, then
14093 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14094 if a type is declared; 2 if it is defined. Otherwise, it is set to
14097 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14098 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14099 is set to FALSE. */
14102 cp_parser_type_specifier (cp_parser
* parser
,
14103 cp_parser_flags flags
,
14104 cp_decl_specifier_seq
*decl_specs
,
14105 bool is_declaration
,
14106 int* declares_class_or_enum
,
14107 bool* is_cv_qualifier
)
14109 tree type_spec
= NULL_TREE
;
14112 cp_decl_spec ds
= ds_last
;
14114 /* Assume this type-specifier does not declare a new type. */
14115 if (declares_class_or_enum
)
14116 *declares_class_or_enum
= 0;
14117 /* And that it does not specify a cv-qualifier. */
14118 if (is_cv_qualifier
)
14119 *is_cv_qualifier
= false;
14120 /* Peek at the next token. */
14121 token
= cp_lexer_peek_token (parser
->lexer
);
14123 /* If we're looking at a keyword, we can use that to guide the
14124 production we choose. */
14125 keyword
= token
->keyword
;
14129 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14130 goto elaborated_type_specifier
;
14132 /* Look for the enum-specifier. */
14133 type_spec
= cp_parser_enum_specifier (parser
);
14134 /* If that worked, we're done. */
14137 if (declares_class_or_enum
)
14138 *declares_class_or_enum
= 2;
14140 cp_parser_set_decl_spec_type (decl_specs
,
14143 /*type_definition_p=*/true);
14147 goto elaborated_type_specifier
;
14149 /* Any of these indicate either a class-specifier, or an
14150 elaborated-type-specifier. */
14154 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14155 goto elaborated_type_specifier
;
14157 /* Parse tentatively so that we can back up if we don't find a
14158 class-specifier. */
14159 cp_parser_parse_tentatively (parser
);
14160 /* Look for the class-specifier. */
14161 type_spec
= cp_parser_class_specifier (parser
);
14162 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14163 /* If that worked, we're done. */
14164 if (cp_parser_parse_definitely (parser
))
14166 if (declares_class_or_enum
)
14167 *declares_class_or_enum
= 2;
14169 cp_parser_set_decl_spec_type (decl_specs
,
14172 /*type_definition_p=*/true);
14176 /* Fall through. */
14177 elaborated_type_specifier
:
14178 /* We're declaring (not defining) a class or enum. */
14179 if (declares_class_or_enum
)
14180 *declares_class_or_enum
= 1;
14182 /* Fall through. */
14184 /* Look for an elaborated-type-specifier. */
14186 = (cp_parser_elaborated_type_specifier
14188 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14191 cp_parser_set_decl_spec_type (decl_specs
,
14194 /*type_definition_p=*/false);
14199 if (is_cv_qualifier
)
14200 *is_cv_qualifier
= true;
14205 if (is_cv_qualifier
)
14206 *is_cv_qualifier
= true;
14211 if (is_cv_qualifier
)
14212 *is_cv_qualifier
= true;
14216 /* The `__complex__' keyword is a GNU extension. */
14224 /* Handle simple keywords. */
14229 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14230 decl_specs
->any_specifiers_p
= true;
14232 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14235 /* If we do not already have a type-specifier, assume we are looking
14236 at a simple-type-specifier. */
14237 type_spec
= cp_parser_simple_type_specifier (parser
,
14241 /* If we didn't find a type-specifier, and a type-specifier was not
14242 optional in this context, issue an error message. */
14243 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14245 cp_parser_error (parser
, "expected type specifier");
14246 return error_mark_node
;
14252 /* Parse a simple-type-specifier.
14254 simple-type-specifier:
14255 :: [opt] nested-name-specifier [opt] type-name
14256 :: [opt] nested-name-specifier template template-id
14271 simple-type-specifier:
14273 decltype ( expression )
14276 __underlying_type ( type-id )
14280 simple-type-specifier:
14282 __typeof__ unary-expression
14283 __typeof__ ( type-id )
14284 __typeof__ ( type-id ) { initializer-list , [opt] }
14286 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14287 appropriately updated. */
14290 cp_parser_simple_type_specifier (cp_parser
* parser
,
14291 cp_decl_specifier_seq
*decl_specs
,
14292 cp_parser_flags flags
)
14294 tree type
= NULL_TREE
;
14297 /* Peek at the next token. */
14298 token
= cp_lexer_peek_token (parser
->lexer
);
14300 /* If we're looking at a keyword, things are easy. */
14301 switch (token
->keyword
)
14305 decl_specs
->explicit_char_p
= true;
14306 type
= char_type_node
;
14309 type
= char16_type_node
;
14312 type
= char32_type_node
;
14315 type
= wchar_type_node
;
14318 type
= boolean_type_node
;
14321 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14322 type
= short_integer_type_node
;
14326 decl_specs
->explicit_int_p
= true;
14327 type
= integer_type_node
;
14330 if (!int128_integer_type_node
)
14333 decl_specs
->explicit_int128_p
= true;
14334 type
= int128_integer_type_node
;
14338 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14339 type
= long_integer_type_node
;
14342 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14343 type
= integer_type_node
;
14346 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14347 type
= unsigned_type_node
;
14350 type
= float_type_node
;
14353 type
= double_type_node
;
14356 type
= void_type_node
;
14360 maybe_warn_cpp0x (CPP0X_AUTO
);
14361 type
= make_auto ();
14365 /* Since DR 743, decltype can either be a simple-type-specifier by
14366 itself or begin a nested-name-specifier. Parsing it will replace
14367 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14368 handling below decide what to do. */
14369 cp_parser_decltype (parser
);
14370 cp_lexer_set_token_position (parser
->lexer
, token
);
14374 /* Consume the `typeof' token. */
14375 cp_lexer_consume_token (parser
->lexer
);
14376 /* Parse the operand to `typeof'. */
14377 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14378 /* If it is not already a TYPE, take its type. */
14379 if (!TYPE_P (type
))
14380 type
= finish_typeof (type
);
14383 cp_parser_set_decl_spec_type (decl_specs
, type
,
14385 /*type_definition_p=*/false);
14389 case RID_UNDERLYING_TYPE
:
14390 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14392 cp_parser_set_decl_spec_type (decl_specs
, type
,
14394 /*type_definition_p=*/false);
14399 case RID_DIRECT_BASES
:
14400 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14402 cp_parser_set_decl_spec_type (decl_specs
, type
,
14404 /*type_definition_p=*/false);
14410 /* If token is an already-parsed decltype not followed by ::,
14411 it's a simple-type-specifier. */
14412 if (token
->type
== CPP_DECLTYPE
14413 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
14415 type
= token
->u
.value
;
14417 cp_parser_set_decl_spec_type (decl_specs
, type
,
14419 /*type_definition_p=*/false);
14420 cp_lexer_consume_token (parser
->lexer
);
14424 /* If the type-specifier was for a built-in type, we're done. */
14427 /* Record the type. */
14429 && (token
->keyword
!= RID_SIGNED
14430 && token
->keyword
!= RID_UNSIGNED
14431 && token
->keyword
!= RID_SHORT
14432 && token
->keyword
!= RID_LONG
))
14433 cp_parser_set_decl_spec_type (decl_specs
,
14436 /*type_definition_p=*/false);
14438 decl_specs
->any_specifiers_p
= true;
14440 /* Consume the token. */
14441 cp_lexer_consume_token (parser
->lexer
);
14443 /* There is no valid C++ program where a non-template type is
14444 followed by a "<". That usually indicates that the user thought
14445 that the type was a template. */
14446 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
14449 return TYPE_NAME (type
);
14452 /* The type-specifier must be a user-defined type. */
14453 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
14458 /* Don't gobble tokens or issue error messages if this is an
14459 optional type-specifier. */
14460 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
14461 cp_parser_parse_tentatively (parser
);
14463 /* Look for the optional `::' operator. */
14465 = (cp_parser_global_scope_opt (parser
,
14466 /*current_scope_valid_p=*/false)
14468 /* Look for the nested-name specifier. */
14470 = (cp_parser_nested_name_specifier_opt (parser
,
14471 /*typename_keyword_p=*/false,
14472 /*check_dependency_p=*/true,
14474 /*is_declaration=*/false)
14476 token
= cp_lexer_peek_token (parser
->lexer
);
14477 /* If we have seen a nested-name-specifier, and the next token
14478 is `template', then we are using the template-id production. */
14480 && cp_parser_optional_template_keyword (parser
))
14482 /* Look for the template-id. */
14483 type
= cp_parser_template_id (parser
,
14484 /*template_keyword_p=*/true,
14485 /*check_dependency_p=*/true,
14487 /*is_declaration=*/false);
14488 /* If the template-id did not name a type, we are out of
14490 if (TREE_CODE (type
) != TYPE_DECL
)
14492 cp_parser_error (parser
, "expected template-id for type");
14496 /* Otherwise, look for a type-name. */
14498 type
= cp_parser_type_name (parser
);
14499 /* Keep track of all name-lookups performed in class scopes. */
14503 && TREE_CODE (type
) == TYPE_DECL
14504 && identifier_p (DECL_NAME (type
)))
14505 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
14506 /* If it didn't work out, we don't have a TYPE. */
14507 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
14508 && !cp_parser_parse_definitely (parser
))
14510 if (type
&& decl_specs
)
14511 cp_parser_set_decl_spec_type (decl_specs
, type
,
14513 /*type_definition_p=*/false);
14516 /* If we didn't get a type-name, issue an error message. */
14517 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14519 cp_parser_error (parser
, "expected type-name");
14520 return error_mark_node
;
14523 if (type
&& type
!= error_mark_node
)
14525 /* See if TYPE is an Objective-C type, and if so, parse and
14526 accept any protocol references following it. Do this before
14527 the cp_parser_check_for_invalid_template_id() call, because
14528 Objective-C types can be followed by '<...>' which would
14529 enclose protocol names rather than template arguments, and so
14530 everything is fine. */
14531 if (c_dialect_objc () && !parser
->scope
14532 && (objc_is_id (type
) || objc_is_class_name (type
)))
14534 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14535 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
14537 /* Clobber the "unqualified" type previously entered into
14538 DECL_SPECS with the new, improved protocol-qualified version. */
14540 decl_specs
->type
= qual_type
;
14545 /* There is no valid C++ program where a non-template type is
14546 followed by a "<". That usually indicates that the user
14547 thought that the type was a template. */
14548 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
14556 /* Parse a type-name.
14562 simple-template-id [in c++0x]
14570 Returns a TYPE_DECL for the type. */
14573 cp_parser_type_name (cp_parser
* parser
)
14577 /* We can't know yet whether it is a class-name or not. */
14578 cp_parser_parse_tentatively (parser
);
14579 /* Try a class-name. */
14580 type_decl
= cp_parser_class_name (parser
,
14581 /*typename_keyword_p=*/false,
14582 /*template_keyword_p=*/false,
14584 /*check_dependency_p=*/true,
14585 /*class_head_p=*/false,
14586 /*is_declaration=*/false);
14587 /* If it's not a class-name, keep looking. */
14588 if (!cp_parser_parse_definitely (parser
))
14590 if (cxx_dialect
< cxx11
)
14591 /* It must be a typedef-name or an enum-name. */
14592 return cp_parser_nonclass_name (parser
);
14594 cp_parser_parse_tentatively (parser
);
14595 /* It is either a simple-template-id representing an
14596 instantiation of an alias template... */
14597 type_decl
= cp_parser_template_id (parser
,
14598 /*template_keyword_p=*/false,
14599 /*check_dependency_p=*/false,
14601 /*is_declaration=*/false);
14602 /* Note that this must be an instantiation of an alias template
14603 because [temp.names]/6 says:
14605 A template-id that names an alias template specialization
14608 Whereas [temp.names]/7 says:
14610 A simple-template-id that names a class template
14611 specialization is a class-name. */
14612 if (type_decl
!= NULL_TREE
14613 && TREE_CODE (type_decl
) == TYPE_DECL
14614 && TYPE_DECL_ALIAS_P (type_decl
))
14615 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
14617 cp_parser_simulate_error (parser
);
14619 if (!cp_parser_parse_definitely (parser
))
14620 /* ... Or a typedef-name or an enum-name. */
14621 return cp_parser_nonclass_name (parser
);
14627 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14635 Returns a TYPE_DECL for the type. */
14638 cp_parser_nonclass_name (cp_parser
* parser
)
14643 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14644 identifier
= cp_parser_identifier (parser
);
14645 if (identifier
== error_mark_node
)
14646 return error_mark_node
;
14648 /* Look up the type-name. */
14649 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
14651 if (TREE_CODE (type_decl
) == USING_DECL
)
14653 if (!DECL_DEPENDENT_P (type_decl
))
14654 type_decl
= strip_using_decl (type_decl
);
14655 else if (USING_DECL_TYPENAME_P (type_decl
))
14657 /* We have found a type introduced by a using
14658 declaration at class scope that refers to a dependent
14661 using typename :: [opt] nested-name-specifier unqualified-id ;
14663 type_decl
= make_typename_type (TREE_TYPE (type_decl
),
14664 DECL_NAME (type_decl
),
14665 typename_type
, tf_error
);
14666 if (type_decl
!= error_mark_node
)
14667 type_decl
= TYPE_NAME (type_decl
);
14671 if (TREE_CODE (type_decl
) != TYPE_DECL
14672 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
14674 /* See if this is an Objective-C type. */
14675 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14676 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
14678 type_decl
= TYPE_NAME (type
);
14681 /* Issue an error if we did not find a type-name. */
14682 if (TREE_CODE (type_decl
) != TYPE_DECL
14683 /* In Objective-C, we have the complication that class names are
14684 normally type names and start declarations (eg, the
14685 "NSObject" in "NSObject *object;"), but can be used in an
14686 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14687 is an expression. So, a classname followed by a dot is not a
14688 valid type-name. */
14689 || (objc_is_class_name (TREE_TYPE (type_decl
))
14690 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
14692 if (!cp_parser_simulate_error (parser
))
14693 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
14694 NLE_TYPE
, token
->location
);
14695 return error_mark_node
;
14697 /* Remember that the name was used in the definition of the
14698 current class so that we can check later to see if the
14699 meaning would have been different after the class was
14700 entirely defined. */
14701 else if (type_decl
!= error_mark_node
14703 maybe_note_name_used_in_class (identifier
, type_decl
);
14708 /* Parse an elaborated-type-specifier. Note that the grammar given
14709 here incorporates the resolution to DR68.
14711 elaborated-type-specifier:
14712 class-key :: [opt] nested-name-specifier [opt] identifier
14713 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14714 enum-key :: [opt] nested-name-specifier [opt] identifier
14715 typename :: [opt] nested-name-specifier identifier
14716 typename :: [opt] nested-name-specifier template [opt]
14721 elaborated-type-specifier:
14722 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14723 class-key attributes :: [opt] nested-name-specifier [opt]
14724 template [opt] template-id
14725 enum attributes :: [opt] nested-name-specifier [opt] identifier
14727 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14728 declared `friend'. If IS_DECLARATION is TRUE, then this
14729 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14730 something is being declared.
14732 Returns the TYPE specified. */
14735 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
14737 bool is_declaration
)
14739 enum tag_types tag_type
;
14741 tree type
= NULL_TREE
;
14742 tree attributes
= NULL_TREE
;
14744 cp_token
*token
= NULL
;
14746 /* See if we're looking at the `enum' keyword. */
14747 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
14749 /* Consume the `enum' token. */
14750 cp_lexer_consume_token (parser
->lexer
);
14751 /* Remember that it's an enumeration type. */
14752 tag_type
= enum_type
;
14753 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14754 enums) is used here. */
14755 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14756 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14758 pedwarn (input_location
, 0, "elaborated-type-specifier "
14759 "for a scoped enum must not use the %<%D%> keyword",
14760 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
14761 /* Consume the `struct' or `class' and parse it anyway. */
14762 cp_lexer_consume_token (parser
->lexer
);
14764 /* Parse the attributes. */
14765 attributes
= cp_parser_attributes_opt (parser
);
14767 /* Or, it might be `typename'. */
14768 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14771 /* Consume the `typename' token. */
14772 cp_lexer_consume_token (parser
->lexer
);
14773 /* Remember that it's a `typename' type. */
14774 tag_type
= typename_type
;
14776 /* Otherwise it must be a class-key. */
14779 tag_type
= cp_parser_class_key (parser
);
14780 if (tag_type
== none_type
)
14781 return error_mark_node
;
14782 /* Parse the attributes. */
14783 attributes
= cp_parser_attributes_opt (parser
);
14786 /* Look for the `::' operator. */
14787 globalscope
= cp_parser_global_scope_opt (parser
,
14788 /*current_scope_valid_p=*/false);
14789 /* Look for the nested-name-specifier. */
14790 if (tag_type
== typename_type
&& !globalscope
)
14792 if (!cp_parser_nested_name_specifier (parser
,
14793 /*typename_keyword_p=*/true,
14794 /*check_dependency_p=*/true,
14797 return error_mark_node
;
14800 /* Even though `typename' is not present, the proposed resolution
14801 to Core Issue 180 says that in `class A<T>::B', `B' should be
14802 considered a type-name, even if `A<T>' is dependent. */
14803 cp_parser_nested_name_specifier_opt (parser
,
14804 /*typename_keyword_p=*/true,
14805 /*check_dependency_p=*/true,
14808 /* For everything but enumeration types, consider a template-id.
14809 For an enumeration type, consider only a plain identifier. */
14810 if (tag_type
!= enum_type
)
14812 bool template_p
= false;
14815 /* Allow the `template' keyword. */
14816 template_p
= cp_parser_optional_template_keyword (parser
);
14817 /* If we didn't see `template', we don't know if there's a
14818 template-id or not. */
14820 cp_parser_parse_tentatively (parser
);
14821 /* Parse the template-id. */
14822 token
= cp_lexer_peek_token (parser
->lexer
);
14823 decl
= cp_parser_template_id (parser
, template_p
,
14824 /*check_dependency_p=*/true,
14827 /* If we didn't find a template-id, look for an ordinary
14829 if (!template_p
&& !cp_parser_parse_definitely (parser
))
14831 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14832 in effect, then we must assume that, upon instantiation, the
14833 template will correspond to a class. */
14834 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
14835 && tag_type
== typename_type
)
14836 type
= make_typename_type (parser
->scope
, decl
,
14838 /*complain=*/tf_error
);
14839 /* If the `typename' keyword is in effect and DECL is not a type
14840 decl, then type is non existent. */
14841 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
14843 else if (TREE_CODE (decl
) == TYPE_DECL
)
14844 type
= check_elaborated_type_specifier (tag_type
, decl
,
14845 /*allow_template_p=*/true);
14846 else if (decl
== error_mark_node
)
14847 type
= error_mark_node
;
14852 token
= cp_lexer_peek_token (parser
->lexer
);
14853 identifier
= cp_parser_identifier (parser
);
14855 if (identifier
== error_mark_node
)
14857 parser
->scope
= NULL_TREE
;
14858 return error_mark_node
;
14861 /* For a `typename', we needn't call xref_tag. */
14862 if (tag_type
== typename_type
14863 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
14864 return cp_parser_make_typename_type (parser
, parser
->scope
,
14867 /* Look up a qualified name in the usual way. */
14871 tree ambiguous_decls
;
14873 decl
= cp_parser_lookup_name (parser
, identifier
,
14875 /*is_template=*/false,
14876 /*is_namespace=*/false,
14877 /*check_dependency=*/true,
14881 /* If the lookup was ambiguous, an error will already have been
14883 if (ambiguous_decls
)
14884 return error_mark_node
;
14886 /* If we are parsing friend declaration, DECL may be a
14887 TEMPLATE_DECL tree node here. However, we need to check
14888 whether this TEMPLATE_DECL results in valid code. Consider
14889 the following example:
14892 template <class T> class C {};
14895 template <class T> friend class N::C; // #1, valid code
14897 template <class T> class Y {
14898 friend class N::C; // #2, invalid code
14901 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14902 name lookup of `N::C'. We see that friend declaration must
14903 be template for the code to be valid. Note that
14904 processing_template_decl does not work here since it is
14905 always 1 for the above two cases. */
14907 decl
= (cp_parser_maybe_treat_template_as_class
14908 (decl
, /*tag_name_p=*/is_friend
14909 && parser
->num_template_parameter_lists
));
14911 if (TREE_CODE (decl
) != TYPE_DECL
)
14913 cp_parser_diagnose_invalid_type_name (parser
,
14917 return error_mark_node
;
14920 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
14922 bool allow_template
= (parser
->num_template_parameter_lists
14923 || DECL_SELF_REFERENCE_P (decl
));
14924 type
= check_elaborated_type_specifier (tag_type
, decl
,
14927 if (type
== error_mark_node
)
14928 return error_mark_node
;
14931 /* Forward declarations of nested types, such as
14936 are invalid unless all components preceding the final '::'
14937 are complete. If all enclosing types are complete, these
14938 declarations become merely pointless.
14940 Invalid forward declarations of nested types are errors
14941 caught elsewhere in parsing. Those that are pointless arrive
14944 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14945 && !is_friend
&& !processing_explicit_instantiation
)
14946 warning (0, "declaration %qD does not declare anything", decl
);
14948 type
= TREE_TYPE (decl
);
14952 /* An elaborated-type-specifier sometimes introduces a new type and
14953 sometimes names an existing type. Normally, the rule is that it
14954 introduces a new type only if there is not an existing type of
14955 the same name already in scope. For example, given:
14958 void f() { struct S s; }
14960 the `struct S' in the body of `f' is the same `struct S' as in
14961 the global scope; the existing definition is used. However, if
14962 there were no global declaration, this would introduce a new
14963 local class named `S'.
14965 An exception to this rule applies to the following code:
14967 namespace N { struct S; }
14969 Here, the elaborated-type-specifier names a new type
14970 unconditionally; even if there is already an `S' in the
14971 containing scope this declaration names a new type.
14972 This exception only applies if the elaborated-type-specifier
14973 forms the complete declaration:
14977 A declaration consisting solely of `class-key identifier ;' is
14978 either a redeclaration of the name in the current scope or a
14979 forward declaration of the identifier as a class name. It
14980 introduces the name into the current scope.
14982 We are in this situation precisely when the next token is a `;'.
14984 An exception to the exception is that a `friend' declaration does
14985 *not* name a new type; i.e., given:
14987 struct S { friend struct T; };
14989 `T' is not a new type in the scope of `S'.
14991 Also, `new struct S' or `sizeof (struct S)' never results in the
14992 definition of a new type; a new type can only be declared in a
14993 declaration context. */
14999 /* Friends have special name lookup rules. */
15000 ts
= ts_within_enclosing_non_class
;
15001 else if (is_declaration
15002 && cp_lexer_next_token_is (parser
->lexer
,
15004 /* This is a `class-key identifier ;' */
15010 (parser
->num_template_parameter_lists
15011 && (cp_parser_next_token_starts_class_definition_p (parser
)
15012 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15013 /* An unqualified name was used to reference this type, so
15014 there were no qualifying templates. */
15015 if (!cp_parser_check_template_parameters (parser
,
15016 /*num_templates=*/0,
15018 /*declarator=*/NULL
))
15019 return error_mark_node
;
15020 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15024 if (type
== error_mark_node
)
15025 return error_mark_node
;
15027 /* Allow attributes on forward declarations of classes. */
15030 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15031 warning (OPT_Wattributes
,
15032 "attributes ignored on uninstantiated type");
15033 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15034 && ! processing_explicit_instantiation
)
15035 warning (OPT_Wattributes
,
15036 "attributes ignored on template instantiation");
15037 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15038 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15040 warning (OPT_Wattributes
,
15041 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15044 if (tag_type
!= enum_type
)
15046 /* Indicate whether this class was declared as a `class' or as a
15048 if (TREE_CODE (type
) == RECORD_TYPE
)
15049 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15050 cp_parser_check_class_key (tag_type
, type
);
15053 /* A "<" cannot follow an elaborated type specifier. If that
15054 happens, the user was probably trying to form a template-id. */
15055 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15061 /* Parse an enum-specifier.
15064 enum-head { enumerator-list [opt] }
15065 enum-head { enumerator-list , } [C++0x]
15068 enum-key identifier [opt] enum-base [opt]
15069 enum-key nested-name-specifier identifier enum-base [opt]
15074 enum struct [C++0x]
15077 : type-specifier-seq
15079 opaque-enum-specifier:
15080 enum-key identifier enum-base [opt] ;
15083 enum-key attributes[opt] identifier [opt] enum-base [opt]
15084 { enumerator-list [opt] }attributes[opt]
15085 enum-key attributes[opt] identifier [opt] enum-base [opt]
15086 { enumerator-list, }attributes[opt] [C++0x]
15088 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15089 if the token stream isn't an enum-specifier after all. */
15092 cp_parser_enum_specifier (cp_parser
* parser
)
15095 tree type
= NULL_TREE
;
15097 tree nested_name_specifier
= NULL_TREE
;
15099 bool scoped_enum_p
= false;
15100 bool has_underlying_type
= false;
15101 bool nested_being_defined
= false;
15102 bool new_value_list
= false;
15103 bool is_new_type
= false;
15104 bool is_anonymous
= false;
15105 tree underlying_type
= NULL_TREE
;
15106 cp_token
*type_start_token
= NULL
;
15107 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15109 parser
->colon_corrects_to_scope_p
= false;
15111 /* Parse tentatively so that we can back up if we don't find a
15113 cp_parser_parse_tentatively (parser
);
15115 /* Caller guarantees that the current token is 'enum', an identifier
15116 possibly follows, and the token after that is an opening brace.
15117 If we don't have an identifier, fabricate an anonymous name for
15118 the enumeration being defined. */
15119 cp_lexer_consume_token (parser
->lexer
);
15121 /* Parse the "class" or "struct", which indicates a scoped
15122 enumeration type in C++0x. */
15123 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15124 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15126 if (cxx_dialect
< cxx11
)
15127 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15129 /* Consume the `struct' or `class' token. */
15130 cp_lexer_consume_token (parser
->lexer
);
15132 scoped_enum_p
= true;
15135 attributes
= cp_parser_attributes_opt (parser
);
15137 /* Clear the qualification. */
15138 parser
->scope
= NULL_TREE
;
15139 parser
->qualifying_scope
= NULL_TREE
;
15140 parser
->object_scope
= NULL_TREE
;
15142 /* Figure out in what scope the declaration is being placed. */
15143 prev_scope
= current_scope ();
15145 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15147 push_deferring_access_checks (dk_no_check
);
15148 nested_name_specifier
15149 = cp_parser_nested_name_specifier_opt (parser
,
15150 /*typename_keyword_p=*/true,
15151 /*check_dependency_p=*/false,
15153 /*is_declaration=*/false);
15155 if (nested_name_specifier
)
15159 identifier
= cp_parser_identifier (parser
);
15160 name
= cp_parser_lookup_name (parser
, identifier
,
15162 /*is_template=*/false,
15163 /*is_namespace=*/false,
15164 /*check_dependency=*/true,
15165 /*ambiguous_decls=*/NULL
,
15167 if (name
&& name
!= error_mark_node
)
15169 type
= TREE_TYPE (name
);
15170 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15172 /* Are template enums allowed in ISO? */
15173 if (template_parm_scope_p ())
15174 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15175 "%qD is an enumeration template", name
);
15176 /* ignore a typename reference, for it will be solved by name
15181 else if (nested_name_specifier
== error_mark_node
)
15182 /* We already issued an error. */;
15184 error_at (type_start_token
->location
,
15185 "%qD is not an enumerator-name", identifier
);
15189 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15190 identifier
= cp_parser_identifier (parser
);
15193 identifier
= make_anon_name ();
15194 is_anonymous
= true;
15196 error_at (type_start_token
->location
,
15197 "anonymous scoped enum is not allowed");
15200 pop_deferring_access_checks ();
15202 /* Check for the `:' that denotes a specified underlying type in C++0x.
15203 Note that a ':' could also indicate a bitfield width, however. */
15204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15206 cp_decl_specifier_seq type_specifiers
;
15208 /* Consume the `:'. */
15209 cp_lexer_consume_token (parser
->lexer
);
15211 /* Parse the type-specifier-seq. */
15212 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15213 /*is_trailing_return=*/false,
15216 /* At this point this is surely not elaborated type specifier. */
15217 if (!cp_parser_parse_definitely (parser
))
15220 if (cxx_dialect
< cxx11
)
15221 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15223 has_underlying_type
= true;
15225 /* If that didn't work, stop. */
15226 if (type_specifiers
.type
!= error_mark_node
)
15228 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15229 /*initialized=*/0, NULL
);
15230 if (underlying_type
== error_mark_node
)
15231 underlying_type
= NULL_TREE
;
15235 /* Look for the `{' but don't consume it yet. */
15236 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15238 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15240 cp_parser_error (parser
, "expected %<{%>");
15241 if (has_underlying_type
)
15247 /* An opaque-enum-specifier must have a ';' here. */
15248 if ((scoped_enum_p
|| underlying_type
)
15249 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15251 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15252 if (has_underlying_type
)
15260 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15263 if (nested_name_specifier
)
15265 if (CLASS_TYPE_P (nested_name_specifier
))
15267 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15268 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15269 push_scope (nested_name_specifier
);
15271 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15273 push_nested_namespace (nested_name_specifier
);
15277 /* Issue an error message if type-definitions are forbidden here. */
15278 if (!cp_parser_check_type_definition (parser
))
15279 type
= error_mark_node
;
15281 /* Create the new type. We do this before consuming the opening
15282 brace so the enum will be recorded as being on the line of its
15283 tag (or the 'enum' keyword, if there is no tag). */
15284 type
= start_enum (identifier
, type
, underlying_type
,
15285 scoped_enum_p
, &is_new_type
);
15287 /* If the next token is not '{' it is an opaque-enum-specifier or an
15288 elaborated-type-specifier. */
15289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15291 timevar_push (TV_PARSE_ENUM
);
15292 if (nested_name_specifier
15293 && nested_name_specifier
!= error_mark_node
)
15295 /* The following catches invalid code such as:
15296 enum class S<int>::E { A, B, C }; */
15297 if (!processing_specialization
15298 && CLASS_TYPE_P (nested_name_specifier
)
15299 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15300 error_at (type_start_token
->location
, "cannot add an enumerator "
15301 "list to a template instantiation");
15303 /* If that scope does not contain the scope in which the
15304 class was originally declared, the program is invalid. */
15305 if (prev_scope
&& !is_ancestor (prev_scope
, nested_name_specifier
))
15307 if (at_namespace_scope_p ())
15308 error_at (type_start_token
->location
,
15309 "declaration of %qD in namespace %qD which does not "
15311 type
, prev_scope
, nested_name_specifier
);
15313 error_at (type_start_token
->location
,
15314 "declaration of %qD in %qD which does not enclose %qD",
15315 type
, prev_scope
, nested_name_specifier
);
15316 type
= error_mark_node
;
15321 begin_scope (sk_scoped_enum
, type
);
15323 /* Consume the opening brace. */
15324 cp_lexer_consume_token (parser
->lexer
);
15326 if (type
== error_mark_node
)
15327 ; /* Nothing to add */
15328 else if (OPAQUE_ENUM_P (type
)
15329 || (cxx_dialect
> cxx98
&& processing_specialization
))
15331 new_value_list
= true;
15332 SET_OPAQUE_ENUM_P (type
, false);
15333 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15337 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
15338 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15339 "previous definition here");
15340 type
= error_mark_node
;
15343 if (type
== error_mark_node
)
15344 cp_parser_skip_to_end_of_block_or_statement (parser
);
15345 /* If the next token is not '}', then there are some enumerators. */
15346 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15348 if (is_anonymous
&& !scoped_enum_p
)
15349 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15350 "ISO C++ forbids empty anonymous enum");
15353 cp_parser_enumerator_list (parser
, type
);
15355 /* Consume the final '}'. */
15356 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15360 timevar_pop (TV_PARSE_ENUM
);
15364 /* If a ';' follows, then it is an opaque-enum-specifier
15365 and additional restrictions apply. */
15366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15369 error_at (type_start_token
->location
,
15370 "opaque-enum-specifier without name");
15371 else if (nested_name_specifier
)
15372 error_at (type_start_token
->location
,
15373 "opaque-enum-specifier must use a simple identifier");
15377 /* Look for trailing attributes to apply to this enumeration, and
15378 apply them if appropriate. */
15379 if (cp_parser_allow_gnu_extensions_p (parser
))
15381 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15382 trailing_attr
= chainon (trailing_attr
, attributes
);
15383 cplus_decl_attributes (&type
,
15385 (int) ATTR_FLAG_TYPE_IN_PLACE
);
15388 /* Finish up the enumeration. */
15389 if (type
!= error_mark_node
)
15391 if (new_value_list
)
15392 finish_enum_value_list (type
);
15394 finish_enum (type
);
15397 if (nested_name_specifier
)
15399 if (CLASS_TYPE_P (nested_name_specifier
))
15401 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
15402 pop_scope (nested_name_specifier
);
15404 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15406 pop_nested_namespace (nested_name_specifier
);
15410 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
15414 /* Parse an enumerator-list. The enumerators all have the indicated
15418 enumerator-definition
15419 enumerator-list , enumerator-definition */
15422 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
15426 /* Parse an enumerator-definition. */
15427 cp_parser_enumerator_definition (parser
, type
);
15429 /* If the next token is not a ',', we've reached the end of
15431 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15433 /* Otherwise, consume the `,' and keep going. */
15434 cp_lexer_consume_token (parser
->lexer
);
15435 /* If the next token is a `}', there is a trailing comma. */
15436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15438 if (cxx_dialect
< cxx11
&& !in_system_header
)
15439 pedwarn (input_location
, OPT_Wpedantic
,
15440 "comma at end of enumerator list");
15446 /* Parse an enumerator-definition. The enumerator has the indicated
15449 enumerator-definition:
15451 enumerator = constant-expression
15457 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
15463 /* Save the input location because we are interested in the location
15464 of the identifier and not the location of the explicit value. */
15465 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15467 /* Look for the identifier. */
15468 identifier
= cp_parser_identifier (parser
);
15469 if (identifier
== error_mark_node
)
15472 /* If the next token is an '=', then there is an explicit value. */
15473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15475 /* Consume the `=' token. */
15476 cp_lexer_consume_token (parser
->lexer
);
15477 /* Parse the value. */
15478 value
= cp_parser_constant_expression (parser
,
15479 /*allow_non_constant_p=*/false,
15485 /* If we are processing a template, make sure the initializer of the
15486 enumerator doesn't contain any bare template parameter pack. */
15487 if (check_for_bare_parameter_packs (value
))
15488 value
= error_mark_node
;
15490 /* integral_constant_value will pull out this expression, so make sure
15491 it's folded as appropriate. */
15492 value
= fold_non_dependent_expr (value
);
15494 /* Create the enumerator. */
15495 build_enumerator (identifier
, value
, type
, loc
);
15498 /* Parse a namespace-name.
15501 original-namespace-name
15504 Returns the NAMESPACE_DECL for the namespace. */
15507 cp_parser_namespace_name (cp_parser
* parser
)
15510 tree namespace_decl
;
15512 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15514 /* Get the name of the namespace. */
15515 identifier
= cp_parser_identifier (parser
);
15516 if (identifier
== error_mark_node
)
15517 return error_mark_node
;
15519 /* Look up the identifier in the currently active scope. Look only
15520 for namespaces, due to:
15522 [basic.lookup.udir]
15524 When looking up a namespace-name in a using-directive or alias
15525 definition, only namespace names are considered.
15529 [basic.lookup.qual]
15531 During the lookup of a name preceding the :: scope resolution
15532 operator, object, function, and enumerator names are ignored.
15534 (Note that cp_parser_qualifying_entity only calls this
15535 function if the token after the name is the scope resolution
15537 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
15539 /*is_template=*/false,
15540 /*is_namespace=*/true,
15541 /*check_dependency=*/true,
15542 /*ambiguous_decls=*/NULL
,
15544 /* If it's not a namespace, issue an error. */
15545 if (namespace_decl
== error_mark_node
15546 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
15548 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15549 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
15550 cp_parser_error (parser
, "expected namespace-name");
15551 namespace_decl
= error_mark_node
;
15554 return namespace_decl
;
15557 /* Parse a namespace-definition.
15559 namespace-definition:
15560 named-namespace-definition
15561 unnamed-namespace-definition
15563 named-namespace-definition:
15564 original-namespace-definition
15565 extension-namespace-definition
15567 original-namespace-definition:
15568 namespace identifier { namespace-body }
15570 extension-namespace-definition:
15571 namespace original-namespace-name { namespace-body }
15573 unnamed-namespace-definition:
15574 namespace { namespace-body } */
15577 cp_parser_namespace_definition (cp_parser
* parser
)
15579 tree identifier
, attribs
;
15580 bool has_visibility
;
15583 cp_ensure_no_omp_declare_simd (parser
);
15584 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
15586 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
15588 cp_lexer_consume_token (parser
->lexer
);
15593 /* Look for the `namespace' keyword. */
15594 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15596 /* Get the name of the namespace. We do not attempt to distinguish
15597 between an original-namespace-definition and an
15598 extension-namespace-definition at this point. The semantic
15599 analysis routines are responsible for that. */
15600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15601 identifier
= cp_parser_identifier (parser
);
15603 identifier
= NULL_TREE
;
15605 /* Parse any specified attributes. */
15606 attribs
= cp_parser_attributes_opt (parser
);
15608 /* Look for the `{' to start the namespace. */
15609 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
15610 /* Start the namespace. */
15611 push_namespace (identifier
);
15613 /* "inline namespace" is equivalent to a stub namespace definition
15614 followed by a strong using directive. */
15617 tree name_space
= current_namespace
;
15618 /* Set up namespace association. */
15619 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
15620 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
15621 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
15622 /* Import the contents of the inline namespace. */
15624 do_using_directive (name_space
);
15625 push_namespace (identifier
);
15628 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
15630 /* Parse the body of the namespace. */
15631 cp_parser_namespace_body (parser
);
15633 if (has_visibility
)
15634 pop_visibility (1);
15636 /* Finish the namespace. */
15638 /* Look for the final `}'. */
15639 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15642 /* Parse a namespace-body.
15645 declaration-seq [opt] */
15648 cp_parser_namespace_body (cp_parser
* parser
)
15650 cp_parser_declaration_seq_opt (parser
);
15653 /* Parse a namespace-alias-definition.
15655 namespace-alias-definition:
15656 namespace identifier = qualified-namespace-specifier ; */
15659 cp_parser_namespace_alias_definition (cp_parser
* parser
)
15662 tree namespace_specifier
;
15664 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15666 /* Look for the `namespace' keyword. */
15667 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15668 /* Look for the identifier. */
15669 identifier
= cp_parser_identifier (parser
);
15670 if (identifier
== error_mark_node
)
15672 /* Look for the `=' token. */
15673 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
15674 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15676 error_at (token
->location
, "%<namespace%> definition is not allowed here");
15677 /* Skip the definition. */
15678 cp_lexer_consume_token (parser
->lexer
);
15679 if (cp_parser_skip_to_closing_brace (parser
))
15680 cp_lexer_consume_token (parser
->lexer
);
15683 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15684 /* Look for the qualified-namespace-specifier. */
15685 namespace_specifier
15686 = cp_parser_qualified_namespace_specifier (parser
);
15687 /* Look for the `;' token. */
15688 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15690 /* Register the alias in the symbol table. */
15691 do_namespace_alias (identifier
, namespace_specifier
);
15694 /* Parse a qualified-namespace-specifier.
15696 qualified-namespace-specifier:
15697 :: [opt] nested-name-specifier [opt] namespace-name
15699 Returns a NAMESPACE_DECL corresponding to the specified
15703 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
15705 /* Look for the optional `::'. */
15706 cp_parser_global_scope_opt (parser
,
15707 /*current_scope_valid_p=*/false);
15709 /* Look for the optional nested-name-specifier. */
15710 cp_parser_nested_name_specifier_opt (parser
,
15711 /*typename_keyword_p=*/false,
15712 /*check_dependency_p=*/true,
15714 /*is_declaration=*/true);
15716 return cp_parser_namespace_name (parser
);
15719 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15720 access declaration.
15723 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15724 using :: unqualified-id ;
15726 access-declaration:
15732 cp_parser_using_declaration (cp_parser
* parser
,
15733 bool access_declaration_p
)
15736 bool typename_p
= false;
15737 bool global_scope_p
;
15741 int oldcount
= errorcount
;
15742 cp_token
*diag_token
= NULL
;
15744 if (access_declaration_p
)
15746 diag_token
= cp_lexer_peek_token (parser
->lexer
);
15747 cp_parser_parse_tentatively (parser
);
15751 /* Look for the `using' keyword. */
15752 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15754 /* Peek at the next token. */
15755 token
= cp_lexer_peek_token (parser
->lexer
);
15756 /* See if it's `typename'. */
15757 if (token
->keyword
== RID_TYPENAME
)
15759 /* Remember that we've seen it. */
15761 /* Consume the `typename' token. */
15762 cp_lexer_consume_token (parser
->lexer
);
15766 /* Look for the optional global scope qualification. */
15768 = (cp_parser_global_scope_opt (parser
,
15769 /*current_scope_valid_p=*/false)
15772 /* If we saw `typename', or didn't see `::', then there must be a
15773 nested-name-specifier present. */
15774 if (typename_p
|| !global_scope_p
)
15775 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
15776 /*check_dependency_p=*/true,
15778 /*is_declaration=*/true);
15779 /* Otherwise, we could be in either of the two productions. In that
15780 case, treat the nested-name-specifier as optional. */
15782 qscope
= cp_parser_nested_name_specifier_opt (parser
,
15783 /*typename_keyword_p=*/false,
15784 /*check_dependency_p=*/true,
15786 /*is_declaration=*/true);
15788 qscope
= global_namespace
;
15790 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
15791 /* Something has already gone wrong; there's no need to parse
15792 further. Since an error has occurred, the return value of
15793 cp_parser_parse_definitely will be false, as required. */
15794 return cp_parser_parse_definitely (parser
);
15796 token
= cp_lexer_peek_token (parser
->lexer
);
15797 /* Parse the unqualified-id. */
15798 identifier
= cp_parser_unqualified_id (parser
,
15799 /*template_keyword_p=*/false,
15800 /*check_dependency_p=*/true,
15801 /*declarator_p=*/true,
15802 /*optional_p=*/false);
15804 if (access_declaration_p
)
15806 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15807 cp_parser_simulate_error (parser
);
15808 if (!cp_parser_parse_definitely (parser
))
15812 /* The function we call to handle a using-declaration is different
15813 depending on what scope we are in. */
15814 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
15816 else if (!identifier_p (identifier
)
15817 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
15818 /* [namespace.udecl]
15820 A using declaration shall not name a template-id. */
15821 error_at (token
->location
,
15822 "a template-id may not appear in a using-declaration");
15825 if (at_class_scope_p ())
15827 /* Create the USING_DECL. */
15828 decl
= do_class_using_decl (parser
->scope
, identifier
);
15830 if (decl
&& typename_p
)
15831 USING_DECL_TYPENAME_P (decl
) = 1;
15833 if (check_for_bare_parameter_packs (decl
))
15836 /* Add it to the list of members in this class. */
15837 finish_member_declaration (decl
);
15841 decl
= cp_parser_lookup_name_simple (parser
,
15844 if (decl
== error_mark_node
)
15845 cp_parser_name_lookup_error (parser
, identifier
,
15848 else if (check_for_bare_parameter_packs (decl
))
15850 else if (!at_namespace_scope_p ())
15851 do_local_using_decl (decl
, qscope
, identifier
);
15853 do_toplevel_using_decl (decl
, qscope
, identifier
);
15857 /* Look for the final `;'. */
15858 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15860 if (access_declaration_p
&& errorcount
== oldcount
)
15861 warning_at (diag_token
->location
, OPT_Wdeprecated
,
15862 "access declarations are deprecated "
15863 "in favour of using-declarations; "
15864 "suggestion: add the %<using%> keyword");
15869 /* Parse an alias-declaration.
15872 using identifier attribute-specifier-seq [opt] = type-id */
15875 cp_parser_alias_declaration (cp_parser
* parser
)
15877 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
15878 location_t id_location
;
15879 cp_declarator
*declarator
;
15880 cp_decl_specifier_seq decl_specs
;
15882 const char *saved_message
= NULL
;
15884 /* Look for the `using' keyword. */
15885 cp_token
*using_token
15886 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15887 if (using_token
== NULL
)
15888 return error_mark_node
;
15890 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
15891 id
= cp_parser_identifier (parser
);
15892 if (id
== error_mark_node
)
15893 return error_mark_node
;
15895 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
15896 attributes
= cp_parser_attributes_opt (parser
);
15897 if (attributes
== error_mark_node
)
15898 return error_mark_node
;
15900 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15902 if (cp_parser_error_occurred (parser
))
15903 return error_mark_node
;
15905 cp_parser_commit_to_tentative_parse (parser
);
15907 /* Now we are going to parse the type-id of the declaration. */
15912 "A type-specifier-seq shall not define a class or enumeration
15913 unless it appears in the type-id of an alias-declaration (7.1.3) that
15914 is not the declaration of a template-declaration."
15916 In other words, if we currently are in an alias template, the
15917 type-id should not define a type.
15919 So let's set parser->type_definition_forbidden_message in that
15920 case; cp_parser_check_type_definition (called by
15921 cp_parser_class_specifier) will then emit an error if a type is
15922 defined in the type-id. */
15923 if (parser
->num_template_parameter_lists
)
15925 saved_message
= parser
->type_definition_forbidden_message
;
15926 parser
->type_definition_forbidden_message
=
15927 G_("types may not be defined in alias template declarations");
15930 type
= cp_parser_type_id (parser
);
15932 /* Restore the error message if need be. */
15933 if (parser
->num_template_parameter_lists
)
15934 parser
->type_definition_forbidden_message
= saved_message
;
15936 if (type
== error_mark_node
)
15938 cp_parser_skip_to_end_of_block_or_statement (parser
);
15939 return error_mark_node
;
15942 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15944 if (cp_parser_error_occurred (parser
))
15946 cp_parser_skip_to_end_of_block_or_statement (parser
);
15947 return error_mark_node
;
15950 /* A typedef-name can also be introduced by an alias-declaration. The
15951 identifier following the using keyword becomes a typedef-name. It has
15952 the same semantics as if it were introduced by the typedef
15953 specifier. In particular, it does not define a new type and it shall
15954 not appear in the type-id. */
15956 clear_decl_specs (&decl_specs
);
15957 decl_specs
.type
= type
;
15958 if (attributes
!= NULL_TREE
)
15960 decl_specs
.attributes
= attributes
;
15961 set_and_check_decl_spec_loc (&decl_specs
,
15965 set_and_check_decl_spec_loc (&decl_specs
,
15968 set_and_check_decl_spec_loc (&decl_specs
,
15972 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
15973 declarator
->id_loc
= id_location
;
15975 member_p
= at_class_scope_p ();
15977 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
15978 NULL_TREE
, attributes
);
15980 decl
= start_decl (declarator
, &decl_specs
, 0,
15981 attributes
, NULL_TREE
, &pushed_scope
);
15982 if (decl
== error_mark_node
)
15985 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
15988 pop_scope (pushed_scope
);
15990 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15991 added into the symbol table; otherwise, return the TYPE_DECL. */
15992 if (DECL_LANG_SPECIFIC (decl
)
15993 && DECL_TEMPLATE_INFO (decl
)
15994 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
15996 decl
= DECL_TI_TEMPLATE (decl
);
15998 check_member_template (decl
);
16004 /* Parse a using-directive.
16007 using namespace :: [opt] nested-name-specifier [opt]
16008 namespace-name ; */
16011 cp_parser_using_directive (cp_parser
* parser
)
16013 tree namespace_decl
;
16016 /* Look for the `using' keyword. */
16017 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16018 /* And the `namespace' keyword. */
16019 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16020 /* Look for the optional `::' operator. */
16021 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16022 /* And the optional nested-name-specifier. */
16023 cp_parser_nested_name_specifier_opt (parser
,
16024 /*typename_keyword_p=*/false,
16025 /*check_dependency_p=*/true,
16027 /*is_declaration=*/true);
16028 /* Get the namespace being used. */
16029 namespace_decl
= cp_parser_namespace_name (parser
);
16030 /* And any specified attributes. */
16031 attribs
= cp_parser_attributes_opt (parser
);
16032 /* Update the symbol table. */
16033 parse_using_directive (namespace_decl
, attribs
);
16034 /* Look for the final `;'. */
16035 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16038 /* Parse an asm-definition.
16041 asm ( string-literal ) ;
16046 asm volatile [opt] ( string-literal ) ;
16047 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16048 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16049 : asm-operand-list [opt] ) ;
16050 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16051 : asm-operand-list [opt]
16052 : asm-clobber-list [opt] ) ;
16053 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16054 : asm-clobber-list [opt]
16055 : asm-goto-list ) ; */
16058 cp_parser_asm_definition (cp_parser
* parser
)
16061 tree outputs
= NULL_TREE
;
16062 tree inputs
= NULL_TREE
;
16063 tree clobbers
= NULL_TREE
;
16064 tree labels
= NULL_TREE
;
16066 bool volatile_p
= false;
16067 bool extended_p
= false;
16068 bool invalid_inputs_p
= false;
16069 bool invalid_outputs_p
= false;
16070 bool goto_p
= false;
16071 required_token missing
= RT_NONE
;
16073 /* Look for the `asm' keyword. */
16074 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16075 /* See if the next token is `volatile'. */
16076 if (cp_parser_allow_gnu_extensions_p (parser
)
16077 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16079 /* Remember that we saw the `volatile' keyword. */
16081 /* Consume the token. */
16082 cp_lexer_consume_token (parser
->lexer
);
16084 if (cp_parser_allow_gnu_extensions_p (parser
)
16085 && parser
->in_function_body
16086 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16088 /* Remember that we saw the `goto' keyword. */
16090 /* Consume the token. */
16091 cp_lexer_consume_token (parser
->lexer
);
16093 /* Look for the opening `('. */
16094 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16096 /* Look for the string. */
16097 string
= cp_parser_string_literal (parser
, false, false);
16098 if (string
== error_mark_node
)
16100 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16101 /*consume_paren=*/true);
16105 /* If we're allowing GNU extensions, check for the extended assembly
16106 syntax. Unfortunately, the `:' tokens need not be separated by
16107 a space in C, and so, for compatibility, we tolerate that here
16108 too. Doing that means that we have to treat the `::' operator as
16110 if (cp_parser_allow_gnu_extensions_p (parser
)
16111 && parser
->in_function_body
16112 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16113 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16115 bool inputs_p
= false;
16116 bool clobbers_p
= false;
16117 bool labels_p
= false;
16119 /* The extended syntax was used. */
16122 /* Look for outputs. */
16123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16125 /* Consume the `:'. */
16126 cp_lexer_consume_token (parser
->lexer
);
16127 /* Parse the output-operands. */
16128 if (cp_lexer_next_token_is_not (parser
->lexer
,
16130 && cp_lexer_next_token_is_not (parser
->lexer
,
16132 && cp_lexer_next_token_is_not (parser
->lexer
,
16135 outputs
= cp_parser_asm_operand_list (parser
);
16137 if (outputs
== error_mark_node
)
16138 invalid_outputs_p
= true;
16140 /* If the next token is `::', there are no outputs, and the
16141 next token is the beginning of the inputs. */
16142 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16143 /* The inputs are coming next. */
16146 /* Look for inputs. */
16148 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16150 /* Consume the `:' or `::'. */
16151 cp_lexer_consume_token (parser
->lexer
);
16152 /* Parse the output-operands. */
16153 if (cp_lexer_next_token_is_not (parser
->lexer
,
16155 && cp_lexer_next_token_is_not (parser
->lexer
,
16157 && cp_lexer_next_token_is_not (parser
->lexer
,
16159 inputs
= cp_parser_asm_operand_list (parser
);
16161 if (inputs
== error_mark_node
)
16162 invalid_inputs_p
= true;
16164 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16165 /* The clobbers are coming next. */
16168 /* Look for clobbers. */
16170 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16173 /* Consume the `:' or `::'. */
16174 cp_lexer_consume_token (parser
->lexer
);
16175 /* Parse the clobbers. */
16176 if (cp_lexer_next_token_is_not (parser
->lexer
,
16178 && cp_lexer_next_token_is_not (parser
->lexer
,
16180 clobbers
= cp_parser_asm_clobber_list (parser
);
16183 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16184 /* The labels are coming next. */
16187 /* Look for labels. */
16189 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16192 /* Consume the `:' or `::'. */
16193 cp_lexer_consume_token (parser
->lexer
);
16194 /* Parse the labels. */
16195 labels
= cp_parser_asm_label_list (parser
);
16198 if (goto_p
&& !labels_p
)
16199 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16202 missing
= RT_COLON_SCOPE
;
16204 /* Look for the closing `)'. */
16205 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16206 missing
? missing
: RT_CLOSE_PAREN
))
16207 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16208 /*consume_paren=*/true);
16209 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16211 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16213 /* Create the ASM_EXPR. */
16214 if (parser
->in_function_body
)
16216 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16217 inputs
, clobbers
, labels
);
16218 /* If the extended syntax was not used, mark the ASM_EXPR. */
16221 tree temp
= asm_stmt
;
16222 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16223 temp
= TREE_OPERAND (temp
, 0);
16225 ASM_INPUT_P (temp
) = 1;
16229 add_asm_node (string
);
16233 /* Declarators [gram.dcl.decl] */
16235 /* Parse an init-declarator.
16238 declarator initializer [opt]
16243 declarator asm-specification [opt] attributes [opt] initializer [opt]
16245 function-definition:
16246 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16248 decl-specifier-seq [opt] declarator function-try-block
16252 function-definition:
16253 __extension__ function-definition
16257 function-definition:
16258 decl-specifier-seq [opt] declarator function-transaction-block
16260 The DECL_SPECIFIERS apply to this declarator. Returns a
16261 representation of the entity declared. If MEMBER_P is TRUE, then
16262 this declarator appears in a class scope. The new DECL created by
16263 this declarator is returned.
16265 The CHECKS are access checks that should be performed once we know
16266 what entity is being declared (and, therefore, what classes have
16269 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16270 for a function-definition here as well. If the declarator is a
16271 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16272 be TRUE upon return. By that point, the function-definition will
16273 have been completely parsed.
16275 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16278 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16279 parsed declaration if it is an uninitialized single declarator not followed
16280 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16281 if present, will not be consumed. If returned, this declarator will be
16282 created with SD_INITIALIZED but will not call cp_finish_decl. */
16285 cp_parser_init_declarator (cp_parser
* parser
,
16286 cp_decl_specifier_seq
*decl_specifiers
,
16287 vec
<deferred_access_check
, va_gc
> *checks
,
16288 bool function_definition_allowed_p
,
16290 int declares_class_or_enum
,
16291 bool* function_definition_p
,
16292 tree
* maybe_range_for_decl
)
16294 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16295 *attributes_start_token
= NULL
;
16296 cp_declarator
*declarator
;
16297 tree prefix_attributes
;
16298 tree attributes
= NULL
;
16299 tree asm_specification
;
16301 tree decl
= NULL_TREE
;
16303 int is_initialized
;
16304 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16305 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16307 enum cpp_ttype initialization_kind
;
16308 bool is_direct_init
= false;
16309 bool is_non_constant_init
;
16310 int ctor_dtor_or_conv_p
;
16312 tree pushed_scope
= NULL_TREE
;
16313 bool range_for_decl_p
= false;
16314 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16316 /* Gather the attributes that were provided with the
16317 decl-specifiers. */
16318 prefix_attributes
= decl_specifiers
->attributes
;
16320 /* Assume that this is not the declarator for a function
16322 if (function_definition_p
)
16323 *function_definition_p
= false;
16325 /* Default arguments are only permitted for function parameters. */
16326 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16327 parser
->default_arg_ok_p
= false;
16329 /* Defer access checks while parsing the declarator; we cannot know
16330 what names are accessible until we know what is being
16332 resume_deferring_access_checks ();
16334 /* Parse the declarator. */
16335 token
= cp_lexer_peek_token (parser
->lexer
);
16337 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16338 &ctor_dtor_or_conv_p
,
16339 /*parenthesized_p=*/NULL
,
16341 /* Gather up the deferred checks. */
16342 stop_deferring_access_checks ();
16344 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16346 /* If the DECLARATOR was erroneous, there's no need to go
16348 if (declarator
== cp_error_declarator
)
16349 return error_mark_node
;
16351 /* Check that the number of template-parameter-lists is OK. */
16352 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16354 return error_mark_node
;
16356 if (declares_class_or_enum
& 2)
16357 cp_parser_check_for_definition_in_return_type (declarator
,
16358 decl_specifiers
->type
,
16359 decl_specifiers
->locations
[ds_type_spec
]);
16361 /* Figure out what scope the entity declared by the DECLARATOR is
16362 located in. `grokdeclarator' sometimes changes the scope, so
16363 we compute it now. */
16364 scope
= get_scope_of_declarator (declarator
);
16366 /* Perform any lookups in the declared type which were thought to be
16367 dependent, but are not in the scope of the declarator. */
16368 decl_specifiers
->type
16369 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
16371 /* If we're allowing GNU extensions, look for an
16372 asm-specification. */
16373 if (cp_parser_allow_gnu_extensions_p (parser
))
16375 /* Look for an asm-specification. */
16376 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
16377 asm_specification
= cp_parser_asm_specification_opt (parser
);
16380 asm_specification
= NULL_TREE
;
16382 /* Look for attributes. */
16383 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
16384 attributes
= cp_parser_attributes_opt (parser
);
16386 /* Peek at the next token. */
16387 token
= cp_lexer_peek_token (parser
->lexer
);
16389 if (function_declarator_p (declarator
))
16391 /* Check to see if the token indicates the start of a
16392 function-definition. */
16393 if (cp_parser_token_starts_function_definition_p (token
))
16395 if (!function_definition_allowed_p
)
16397 /* If a function-definition should not appear here, issue an
16399 cp_parser_error (parser
,
16400 "a function-definition is not allowed here");
16401 return error_mark_node
;
16404 location_t func_brace_location
16405 = cp_lexer_peek_token (parser
->lexer
)->location
;
16407 /* Neither attributes nor an asm-specification are allowed
16408 on a function-definition. */
16409 if (asm_specification
)
16410 error_at (asm_spec_start_token
->location
,
16411 "an asm-specification is not allowed "
16412 "on a function-definition");
16414 error_at (attributes_start_token
->location
,
16415 "attributes are not allowed "
16416 "on a function-definition");
16417 /* This is a function-definition. */
16418 *function_definition_p
= true;
16420 /* Parse the function definition. */
16422 decl
= cp_parser_save_member_function_body (parser
,
16425 prefix_attributes
);
16428 (cp_parser_function_definition_from_specifiers_and_declarator
16429 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
16431 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
16433 /* This is where the prologue starts... */
16434 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
16435 = func_brace_location
;
16444 Only in function declarations for constructors, destructors, and
16445 type conversions can the decl-specifier-seq be omitted.
16447 We explicitly postpone this check past the point where we handle
16448 function-definitions because we tolerate function-definitions
16449 that are missing their return types in some modes. */
16450 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
16452 cp_parser_error (parser
,
16453 "expected constructor, destructor, or type conversion");
16454 return error_mark_node
;
16457 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16458 if (token
->type
== CPP_EQ
16459 || token
->type
== CPP_OPEN_PAREN
16460 || token
->type
== CPP_OPEN_BRACE
)
16462 is_initialized
= SD_INITIALIZED
;
16463 initialization_kind
= token
->type
;
16464 if (maybe_range_for_decl
)
16465 *maybe_range_for_decl
= error_mark_node
;
16467 if (token
->type
== CPP_EQ
16468 && function_declarator_p (declarator
))
16470 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16471 if (t2
->keyword
== RID_DEFAULT
)
16472 is_initialized
= SD_DEFAULTED
;
16473 else if (t2
->keyword
== RID_DELETE
)
16474 is_initialized
= SD_DELETED
;
16479 /* If the init-declarator isn't initialized and isn't followed by a
16480 `,' or `;', it's not a valid init-declarator. */
16481 if (token
->type
!= CPP_COMMA
16482 && token
->type
!= CPP_SEMICOLON
)
16484 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
16485 range_for_decl_p
= true;
16488 cp_parser_error (parser
, "expected initializer");
16489 return error_mark_node
;
16492 is_initialized
= SD_UNINITIALIZED
;
16493 initialization_kind
= CPP_EOF
;
16496 /* Because start_decl has side-effects, we should only call it if we
16497 know we're going ahead. By this point, we know that we cannot
16498 possibly be looking at any other construct. */
16499 cp_parser_commit_to_tentative_parse (parser
);
16501 /* If the decl specifiers were bad, issue an error now that we're
16502 sure this was intended to be a declarator. Then continue
16503 declaring the variable(s), as int, to try to cut down on further
16505 if (decl_specifiers
->any_specifiers_p
16506 && decl_specifiers
->type
== error_mark_node
)
16508 cp_parser_error (parser
, "invalid type in declaration");
16509 decl_specifiers
->type
= integer_type_node
;
16512 /* Check to see whether or not this declaration is a friend. */
16513 friend_p
= cp_parser_friend_p (decl_specifiers
);
16515 /* Enter the newly declared entry in the symbol table. If we're
16516 processing a declaration in a class-specifier, we wait until
16517 after processing the initializer. */
16520 if (parser
->in_unbraced_linkage_specification_p
)
16521 decl_specifiers
->storage_class
= sc_extern
;
16522 decl
= start_decl (declarator
, decl_specifiers
,
16523 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
16524 attributes
, prefix_attributes
, &pushed_scope
);
16525 cp_finalize_omp_declare_simd (parser
, decl
);
16526 /* Adjust location of decl if declarator->id_loc is more appropriate:
16527 set, and decl wasn't merged with another decl, in which case its
16528 location would be different from input_location, and more accurate. */
16530 && declarator
->id_loc
!= UNKNOWN_LOCATION
16531 && DECL_SOURCE_LOCATION (decl
) == input_location
)
16532 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
16535 /* Enter the SCOPE. That way unqualified names appearing in the
16536 initializer will be looked up in SCOPE. */
16537 pushed_scope
= push_scope (scope
);
16539 /* Perform deferred access control checks, now that we know in which
16540 SCOPE the declared entity resides. */
16541 if (!member_p
&& decl
)
16543 tree saved_current_function_decl
= NULL_TREE
;
16545 /* If the entity being declared is a function, pretend that we
16546 are in its scope. If it is a `friend', it may have access to
16547 things that would not otherwise be accessible. */
16548 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16550 saved_current_function_decl
= current_function_decl
;
16551 current_function_decl
= decl
;
16554 /* Perform access checks for template parameters. */
16555 cp_parser_perform_template_parameter_access_checks (checks
);
16557 /* Perform the access control checks for the declarator and the
16558 decl-specifiers. */
16559 perform_deferred_access_checks (tf_warning_or_error
);
16561 /* Restore the saved value. */
16562 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16563 current_function_decl
= saved_current_function_decl
;
16566 /* Parse the initializer. */
16567 initializer
= NULL_TREE
;
16568 is_direct_init
= false;
16569 is_non_constant_init
= true;
16570 if (is_initialized
)
16572 if (function_declarator_p (declarator
))
16574 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
16575 if (initialization_kind
== CPP_EQ
)
16576 initializer
= cp_parser_pure_specifier (parser
);
16579 /* If the declaration was erroneous, we don't really
16580 know what the user intended, so just silently
16581 consume the initializer. */
16582 if (decl
!= error_mark_node
)
16583 error_at (initializer_start_token
->location
,
16584 "initializer provided for function");
16585 cp_parser_skip_to_closing_parenthesis (parser
,
16586 /*recovering=*/true,
16587 /*or_comma=*/false,
16588 /*consume_paren=*/true);
16593 /* We want to record the extra mangling scope for in-class
16594 initializers of class members and initializers of static data
16595 member templates. The former involves deferring
16596 parsing of the initializer until end of class as with default
16597 arguments. So right here we only handle the latter. */
16598 if (!member_p
&& processing_template_decl
)
16599 start_lambda_scope (decl
);
16600 initializer
= cp_parser_initializer (parser
,
16602 &is_non_constant_init
);
16603 if (!member_p
&& processing_template_decl
)
16604 finish_lambda_scope ();
16605 if (initializer
== error_mark_node
)
16606 cp_parser_skip_to_end_of_statement (parser
);
16610 /* The old parser allows attributes to appear after a parenthesized
16611 initializer. Mark Mitchell proposed removing this functionality
16612 on the GCC mailing lists on 2002-08-13. This parser accepts the
16613 attributes -- but ignores them. */
16614 if (cp_parser_allow_gnu_extensions_p (parser
)
16615 && initialization_kind
== CPP_OPEN_PAREN
)
16616 if (cp_parser_attributes_opt (parser
))
16617 warning (OPT_Wattributes
,
16618 "attributes after parenthesized initializer ignored");
16620 /* For an in-class declaration, use `grokfield' to create the
16626 pop_scope (pushed_scope
);
16627 pushed_scope
= NULL_TREE
;
16629 decl
= grokfield (declarator
, decl_specifiers
,
16630 initializer
, !is_non_constant_init
,
16631 /*asmspec=*/NULL_TREE
,
16632 chainon (attributes
, prefix_attributes
));
16633 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16634 cp_parser_save_default_args (parser
, decl
);
16635 cp_finalize_omp_declare_simd (parser
, decl
);
16638 /* Finish processing the declaration. But, skip member
16640 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
16642 cp_finish_decl (decl
,
16643 initializer
, !is_non_constant_init
,
16645 /* If the initializer is in parentheses, then this is
16646 a direct-initialization, which means that an
16647 `explicit' constructor is OK. Otherwise, an
16648 `explicit' constructor cannot be used. */
16649 ((is_direct_init
|| !is_initialized
)
16650 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
16652 else if ((cxx_dialect
!= cxx98
) && friend_p
16653 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16654 /* Core issue #226 (C++0x only): A default template-argument
16655 shall not be specified in a friend class template
16657 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
16658 /*is_partial=*/false, /*is_friend_decl=*/1);
16660 if (!friend_p
&& pushed_scope
)
16661 pop_scope (pushed_scope
);
16663 if (function_declarator_p (declarator
)
16664 && parser
->fully_implicit_function_template_p
)
16667 decl
= finish_fully_implicit_template (parser
, decl
);
16669 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
16675 /* Parse a declarator.
16679 ptr-operator declarator
16681 abstract-declarator:
16682 ptr-operator abstract-declarator [opt]
16683 direct-abstract-declarator
16688 attributes [opt] direct-declarator
16689 attributes [opt] ptr-operator declarator
16691 abstract-declarator:
16692 attributes [opt] ptr-operator abstract-declarator [opt]
16693 attributes [opt] direct-abstract-declarator
16695 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16696 detect constructor, destructor or conversion operators. It is set
16697 to -1 if the declarator is a name, and +1 if it is a
16698 function. Otherwise it is set to zero. Usually you just want to
16699 test for >0, but internally the negative value is used.
16701 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16702 a decl-specifier-seq unless it declares a constructor, destructor,
16703 or conversion. It might seem that we could check this condition in
16704 semantic analysis, rather than parsing, but that makes it difficult
16705 to handle something like `f()'. We want to notice that there are
16706 no decl-specifiers, and therefore realize that this is an
16707 expression, not a declaration.)
16709 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16710 the declarator is a direct-declarator of the form "(...)".
16712 MEMBER_P is true iff this declarator is a member-declarator. */
16714 static cp_declarator
*
16715 cp_parser_declarator (cp_parser
* parser
,
16716 cp_parser_declarator_kind dcl_kind
,
16717 int* ctor_dtor_or_conv_p
,
16718 bool* parenthesized_p
,
16721 cp_declarator
*declarator
;
16722 enum tree_code code
;
16723 cp_cv_quals cv_quals
;
16725 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
16727 /* Assume this is not a constructor, destructor, or type-conversion
16729 if (ctor_dtor_or_conv_p
)
16730 *ctor_dtor_or_conv_p
= 0;
16732 if (cp_parser_allow_gnu_extensions_p (parser
))
16733 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
16735 /* Check for the ptr-operator production. */
16736 cp_parser_parse_tentatively (parser
);
16737 /* Parse the ptr-operator. */
16738 code
= cp_parser_ptr_operator (parser
,
16743 /* If that worked, then we have a ptr-operator. */
16744 if (cp_parser_parse_definitely (parser
))
16746 /* If a ptr-operator was found, then this declarator was not
16748 if (parenthesized_p
)
16749 *parenthesized_p
= true;
16750 /* The dependent declarator is optional if we are parsing an
16751 abstract-declarator. */
16752 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16753 cp_parser_parse_tentatively (parser
);
16755 /* Parse the dependent declarator. */
16756 declarator
= cp_parser_declarator (parser
, dcl_kind
,
16757 /*ctor_dtor_or_conv_p=*/NULL
,
16758 /*parenthesized_p=*/NULL
,
16759 /*member_p=*/false);
16761 /* If we are parsing an abstract-declarator, we must handle the
16762 case where the dependent declarator is absent. */
16763 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
16764 && !cp_parser_parse_definitely (parser
))
16767 declarator
= cp_parser_make_indirect_declarator
16768 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
16770 /* Everything else is a direct-declarator. */
16773 if (parenthesized_p
)
16774 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
16776 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
16777 ctor_dtor_or_conv_p
,
16781 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
16782 declarator
->attributes
= gnu_attributes
;
16786 /* Parse a direct-declarator or direct-abstract-declarator.
16790 direct-declarator ( parameter-declaration-clause )
16791 cv-qualifier-seq [opt]
16792 ref-qualifier [opt]
16793 exception-specification [opt]
16794 direct-declarator [ constant-expression [opt] ]
16797 direct-abstract-declarator:
16798 direct-abstract-declarator [opt]
16799 ( parameter-declaration-clause )
16800 cv-qualifier-seq [opt]
16801 ref-qualifier [opt]
16802 exception-specification [opt]
16803 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16804 ( abstract-declarator )
16806 Returns a representation of the declarator. DCL_KIND is
16807 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16808 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16809 we are parsing a direct-declarator. It is
16810 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16811 of ambiguity we prefer an abstract declarator, as per
16812 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16813 cp_parser_declarator. */
16815 static cp_declarator
*
16816 cp_parser_direct_declarator (cp_parser
* parser
,
16817 cp_parser_declarator_kind dcl_kind
,
16818 int* ctor_dtor_or_conv_p
,
16822 cp_declarator
*declarator
= NULL
;
16823 tree scope
= NULL_TREE
;
16824 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16825 bool saved_in_declarator_p
= parser
->in_declarator_p
;
16827 tree pushed_scope
= NULL_TREE
;
16831 /* Peek at the next token. */
16832 token
= cp_lexer_peek_token (parser
->lexer
);
16833 if (token
->type
== CPP_OPEN_PAREN
)
16835 /* This is either a parameter-declaration-clause, or a
16836 parenthesized declarator. When we know we are parsing a
16837 named declarator, it must be a parenthesized declarator
16838 if FIRST is true. For instance, `(int)' is a
16839 parameter-declaration-clause, with an omitted
16840 direct-abstract-declarator. But `((*))', is a
16841 parenthesized abstract declarator. Finally, when T is a
16842 template parameter `(T)' is a
16843 parameter-declaration-clause, and not a parenthesized
16846 We first try and parse a parameter-declaration-clause,
16847 and then try a nested declarator (if FIRST is true).
16849 It is not an error for it not to be a
16850 parameter-declaration-clause, even when FIRST is
16856 The first is the declaration of a function while the
16857 second is the definition of a variable, including its
16860 Having seen only the parenthesis, we cannot know which of
16861 these two alternatives should be selected. Even more
16862 complex are examples like:
16867 The former is a function-declaration; the latter is a
16868 variable initialization.
16870 Thus again, we try a parameter-declaration-clause, and if
16871 that fails, we back out and return. */
16873 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16876 unsigned saved_num_template_parameter_lists
;
16877 bool is_declarator
= false;
16879 /* In a member-declarator, the only valid interpretation
16880 of a parenthesis is the start of a
16881 parameter-declaration-clause. (It is invalid to
16882 initialize a static data member with a parenthesized
16883 initializer; only the "=" form of initialization is
16886 cp_parser_parse_tentatively (parser
);
16888 /* Consume the `('. */
16889 cp_lexer_consume_token (parser
->lexer
);
16892 /* If this is going to be an abstract declarator, we're
16893 in a declarator and we can't have default args. */
16894 parser
->default_arg_ok_p
= false;
16895 parser
->in_declarator_p
= true;
16898 /* Inside the function parameter list, surrounding
16899 template-parameter-lists do not apply. */
16900 saved_num_template_parameter_lists
16901 = parser
->num_template_parameter_lists
;
16902 parser
->num_template_parameter_lists
= 0;
16904 begin_scope (sk_function_parms
, NULL_TREE
);
16906 /* Parse the parameter-declaration-clause. */
16907 params
= cp_parser_parameter_declaration_clause (parser
);
16909 /* Restore saved template parameter lists accounting for implicit
16910 template parameters. */
16911 parser
->num_template_parameter_lists
16912 += saved_num_template_parameter_lists
;
16914 /* Consume the `)'. */
16915 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
16917 /* If all went well, parse the cv-qualifier-seq,
16918 ref-qualifier and the exception-specification. */
16919 if (member_p
|| cp_parser_parse_definitely (parser
))
16921 cp_cv_quals cv_quals
;
16922 cp_virt_specifiers virt_specifiers
;
16923 cp_ref_qualifier ref_qual
;
16924 tree exception_specification
;
16927 bool memfn
= (member_p
|| (pushed_scope
16928 && CLASS_TYPE_P (pushed_scope
)));
16930 is_declarator
= true;
16932 if (ctor_dtor_or_conv_p
)
16933 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
16936 /* Parse the cv-qualifier-seq. */
16937 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
16938 /* Parse the ref-qualifier. */
16939 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
16940 /* And the exception-specification. */
16941 exception_specification
16942 = cp_parser_exception_specification_opt (parser
);
16944 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16946 late_return
= (cp_parser_late_return_type_opt
16947 (parser
, declarator
,
16948 memfn
? cv_quals
: -1));
16951 /* Parse the virt-specifier-seq. */
16952 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
16954 /* Create the function-declarator. */
16955 declarator
= make_call_declarator (declarator
,
16960 exception_specification
,
16962 declarator
->std_attributes
= attrs
;
16963 /* Any subsequent parameter lists are to do with
16964 return type, so are not those of the declared
16966 parser
->default_arg_ok_p
= false;
16969 /* Remove the function parms from scope. */
16970 pop_bindings_and_leave_scope ();
16973 /* Repeat the main loop. */
16977 /* If this is the first, we can try a parenthesized
16981 bool saved_in_type_id_in_expr_p
;
16983 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16984 parser
->in_declarator_p
= saved_in_declarator_p
;
16986 /* Consume the `('. */
16987 cp_lexer_consume_token (parser
->lexer
);
16988 /* Parse the nested declarator. */
16989 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16990 parser
->in_type_id_in_expr_p
= true;
16992 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
16993 /*parenthesized_p=*/NULL
,
16995 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16997 /* Expect a `)'. */
16998 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
16999 declarator
= cp_error_declarator
;
17000 if (declarator
== cp_error_declarator
)
17003 goto handle_declarator
;
17005 /* Otherwise, we must be done. */
17009 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17010 && token
->type
== CPP_OPEN_SQUARE
17011 && !cp_next_tokens_can_be_attribute_p (parser
))
17013 /* Parse an array-declarator. */
17014 tree bounds
, attrs
;
17016 if (ctor_dtor_or_conv_p
)
17017 *ctor_dtor_or_conv_p
= 0;
17020 parser
->default_arg_ok_p
= false;
17021 parser
->in_declarator_p
= true;
17022 /* Consume the `['. */
17023 cp_lexer_consume_token (parser
->lexer
);
17024 /* Peek at the next token. */
17025 token
= cp_lexer_peek_token (parser
->lexer
);
17026 /* If the next token is `]', then there is no
17027 constant-expression. */
17028 if (token
->type
!= CPP_CLOSE_SQUARE
)
17030 bool non_constant_p
;
17032 = cp_parser_constant_expression (parser
,
17033 /*allow_non_constant=*/true,
17035 if (!non_constant_p
)
17037 else if (error_operand_p (bounds
))
17038 /* Already gave an error. */;
17039 else if (!parser
->in_function_body
17040 || current_binding_level
->kind
== sk_function_parms
)
17042 /* Normally, the array bound must be an integral constant
17043 expression. However, as an extension, we allow VLAs
17044 in function scopes as long as they aren't part of a
17045 parameter declaration. */
17046 cp_parser_error (parser
,
17047 "array bound is not an integer constant");
17048 bounds
= error_mark_node
;
17050 else if (processing_template_decl
)
17052 /* Remember this wasn't a constant-expression. */
17053 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17054 TREE_SIDE_EFFECTS (bounds
) = 1;
17058 bounds
= NULL_TREE
;
17059 /* Look for the closing `]'. */
17060 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17062 declarator
= cp_error_declarator
;
17066 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17067 declarator
= make_array_declarator (declarator
, bounds
);
17068 declarator
->std_attributes
= attrs
;
17070 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17073 tree qualifying_scope
;
17074 tree unqualified_name
;
17076 special_function_kind sfk
;
17078 bool pack_expansion_p
= false;
17079 cp_token
*declarator_id_start_token
;
17081 /* Parse a declarator-id */
17082 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17085 cp_parser_parse_tentatively (parser
);
17087 /* If we see an ellipsis, we should be looking at a
17089 if (token
->type
== CPP_ELLIPSIS
)
17091 /* Consume the `...' */
17092 cp_lexer_consume_token (parser
->lexer
);
17094 pack_expansion_p
= true;
17098 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17100 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17101 qualifying_scope
= parser
->scope
;
17106 if (!unqualified_name
&& pack_expansion_p
)
17108 /* Check whether an error occurred. */
17109 okay
= !cp_parser_error_occurred (parser
);
17111 /* We already consumed the ellipsis to mark a
17112 parameter pack, but we have no way to report it,
17113 so abort the tentative parse. We will be exiting
17114 immediately anyway. */
17115 cp_parser_abort_tentative_parse (parser
);
17118 okay
= cp_parser_parse_definitely (parser
);
17121 unqualified_name
= error_mark_node
;
17122 else if (unqualified_name
17123 && (qualifying_scope
17124 || (!identifier_p (unqualified_name
))))
17126 cp_parser_error (parser
, "expected unqualified-id");
17127 unqualified_name
= error_mark_node
;
17131 if (!unqualified_name
)
17133 if (unqualified_name
== error_mark_node
)
17135 declarator
= cp_error_declarator
;
17136 pack_expansion_p
= false;
17137 declarator
->parameter_pack_p
= false;
17141 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17143 if (qualifying_scope
&& at_namespace_scope_p ()
17144 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17146 /* In the declaration of a member of a template class
17147 outside of the class itself, the SCOPE will sometimes
17148 be a TYPENAME_TYPE. For example, given:
17150 template <typename T>
17151 int S<T>::R::i = 3;
17153 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17154 this context, we must resolve S<T>::R to an ordinary
17155 type, rather than a typename type.
17157 The reason we normally avoid resolving TYPENAME_TYPEs
17158 is that a specialization of `S' might render
17159 `S<T>::R' not a type. However, if `S' is
17160 specialized, then this `i' will not be used, so there
17161 is no harm in resolving the types here. */
17164 /* Resolve the TYPENAME_TYPE. */
17165 type
= resolve_typename_type (qualifying_scope
,
17166 /*only_current_p=*/false);
17167 /* If that failed, the declarator is invalid. */
17168 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17170 if (typedef_variant_p (type
))
17171 error_at (declarator_id_start_token
->location
,
17172 "cannot define member of dependent typedef "
17175 error_at (declarator_id_start_token
->location
,
17176 "%<%T::%E%> is not a type",
17177 TYPE_CONTEXT (qualifying_scope
),
17178 TYPE_IDENTIFIER (qualifying_scope
));
17180 qualifying_scope
= type
;
17185 if (unqualified_name
)
17189 if (qualifying_scope
17190 && CLASS_TYPE_P (qualifying_scope
))
17191 class_type
= qualifying_scope
;
17193 class_type
= current_class_type
;
17195 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17197 tree name_type
= TREE_TYPE (unqualified_name
);
17198 if (class_type
&& same_type_p (name_type
, class_type
))
17200 if (qualifying_scope
17201 && CLASSTYPE_USE_TEMPLATE (name_type
))
17203 error_at (declarator_id_start_token
->location
,
17204 "invalid use of constructor as a template");
17205 inform (declarator_id_start_token
->location
,
17206 "use %<%T::%D%> instead of %<%T::%D%> to "
17207 "name the constructor in a qualified name",
17209 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17210 class_type
, name_type
);
17211 declarator
= cp_error_declarator
;
17215 unqualified_name
= constructor_name (class_type
);
17219 /* We do not attempt to print the declarator
17220 here because we do not have enough
17221 information about its original syntactic
17223 cp_parser_error (parser
, "invalid declarator");
17224 declarator
= cp_error_declarator
;
17231 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17232 sfk
= sfk_destructor
;
17233 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17234 sfk
= sfk_conversion
;
17235 else if (/* There's no way to declare a constructor
17236 for an anonymous type, even if the type
17237 got a name for linkage purposes. */
17238 !TYPE_WAS_ANONYMOUS (class_type
)
17239 && constructor_name_p (unqualified_name
,
17242 unqualified_name
= constructor_name (class_type
);
17243 sfk
= sfk_constructor
;
17245 else if (is_overloaded_fn (unqualified_name
)
17246 && DECL_CONSTRUCTOR_P (get_first_fn
17247 (unqualified_name
)))
17248 sfk
= sfk_constructor
;
17250 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17251 *ctor_dtor_or_conv_p
= -1;
17254 declarator
= make_id_declarator (qualifying_scope
,
17257 declarator
->std_attributes
= attrs
;
17258 declarator
->id_loc
= token
->location
;
17259 declarator
->parameter_pack_p
= pack_expansion_p
;
17261 if (pack_expansion_p
)
17262 maybe_warn_variadic_templates ();
17265 handle_declarator
:;
17266 scope
= get_scope_of_declarator (declarator
);
17269 /* Any names that appear after the declarator-id for a
17270 member are looked up in the containing scope. */
17271 if (at_function_scope_p ())
17273 /* But declarations with qualified-ids can't appear in a
17275 cp_parser_error (parser
, "qualified-id in declaration");
17278 pushed_scope
= push_scope (scope
);
17280 parser
->in_declarator_p
= true;
17281 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17282 || (declarator
&& declarator
->kind
== cdk_id
))
17283 /* Default args are only allowed on function
17285 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17287 parser
->default_arg_ok_p
= false;
17296 /* For an abstract declarator, we might wind up with nothing at this
17297 point. That's an error; the declarator is not optional. */
17299 cp_parser_error (parser
, "expected declarator");
17301 /* If we entered a scope, we must exit it now. */
17303 pop_scope (pushed_scope
);
17305 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17306 parser
->in_declarator_p
= saved_in_declarator_p
;
17311 /* Parse a ptr-operator.
17314 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17315 * cv-qualifier-seq [opt]
17317 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17318 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17323 & cv-qualifier-seq [opt]
17325 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17326 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17327 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17328 filled in with the TYPE containing the member. *CV_QUALS is
17329 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17330 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17331 Note that the tree codes returned by this function have nothing
17332 to do with the types of trees that will be eventually be created
17333 to represent the pointer or reference type being parsed. They are
17334 just constants with suggestive names. */
17335 static enum tree_code
17336 cp_parser_ptr_operator (cp_parser
* parser
,
17338 cp_cv_quals
*cv_quals
,
17341 enum tree_code code
= ERROR_MARK
;
17343 tree attrs
= NULL_TREE
;
17345 /* Assume that it's not a pointer-to-member. */
17347 /* And that there are no cv-qualifiers. */
17348 *cv_quals
= TYPE_UNQUALIFIED
;
17350 /* Peek at the next token. */
17351 token
= cp_lexer_peek_token (parser
->lexer
);
17353 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17354 if (token
->type
== CPP_MULT
)
17355 code
= INDIRECT_REF
;
17356 else if (token
->type
== CPP_AND
)
17358 else if ((cxx_dialect
!= cxx98
) &&
17359 token
->type
== CPP_AND_AND
) /* C++0x only */
17360 code
= NON_LVALUE_EXPR
;
17362 if (code
!= ERROR_MARK
)
17364 /* Consume the `*', `&' or `&&'. */
17365 cp_lexer_consume_token (parser
->lexer
);
17367 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17368 `&', if we are allowing GNU extensions. (The only qualifier
17369 that can legally appear after `&' is `restrict', but that is
17370 enforced during semantic analysis. */
17371 if (code
== INDIRECT_REF
17372 || cp_parser_allow_gnu_extensions_p (parser
))
17373 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17375 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17376 if (attributes
!= NULL
)
17377 *attributes
= attrs
;
17381 /* Try the pointer-to-member case. */
17382 cp_parser_parse_tentatively (parser
);
17383 /* Look for the optional `::' operator. */
17384 cp_parser_global_scope_opt (parser
,
17385 /*current_scope_valid_p=*/false);
17386 /* Look for the nested-name specifier. */
17387 token
= cp_lexer_peek_token (parser
->lexer
);
17388 cp_parser_nested_name_specifier (parser
,
17389 /*typename_keyword_p=*/false,
17390 /*check_dependency_p=*/true,
17392 /*is_declaration=*/false);
17393 /* If we found it, and the next token is a `*', then we are
17394 indeed looking at a pointer-to-member operator. */
17395 if (!cp_parser_error_occurred (parser
)
17396 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
17398 /* Indicate that the `*' operator was used. */
17399 code
= INDIRECT_REF
;
17401 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
17402 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
17403 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
17404 error_at (token
->location
, "cannot form pointer to member of "
17405 "non-class %q#T", parser
->scope
);
17408 /* The type of which the member is a member is given by the
17410 *type
= parser
->scope
;
17411 /* The next name will not be qualified. */
17412 parser
->scope
= NULL_TREE
;
17413 parser
->qualifying_scope
= NULL_TREE
;
17414 parser
->object_scope
= NULL_TREE
;
17415 /* Look for optional c++11 attributes. */
17416 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17417 if (attributes
!= NULL
)
17418 *attributes
= attrs
;
17419 /* Look for the optional cv-qualifier-seq. */
17420 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17423 /* If that didn't work we don't have a ptr-operator. */
17424 if (!cp_parser_parse_definitely (parser
))
17425 cp_parser_error (parser
, "expected ptr-operator");
17431 /* Parse an (optional) cv-qualifier-seq.
17434 cv-qualifier cv-qualifier-seq [opt]
17445 Returns a bitmask representing the cv-qualifiers. */
17448 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
17450 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
17455 cp_cv_quals cv_qualifier
;
17457 /* Peek at the next token. */
17458 token
= cp_lexer_peek_token (parser
->lexer
);
17459 /* See if it's a cv-qualifier. */
17460 switch (token
->keyword
)
17463 cv_qualifier
= TYPE_QUAL_CONST
;
17467 cv_qualifier
= TYPE_QUAL_VOLATILE
;
17471 cv_qualifier
= TYPE_QUAL_RESTRICT
;
17475 cv_qualifier
= TYPE_UNQUALIFIED
;
17482 if (cv_quals
& cv_qualifier
)
17484 error_at (token
->location
, "duplicate cv-qualifier");
17485 cp_lexer_purge_token (parser
->lexer
);
17489 cp_lexer_consume_token (parser
->lexer
);
17490 cv_quals
|= cv_qualifier
;
17497 /* Parse an (optional) ref-qualifier
17503 Returns cp_ref_qualifier representing ref-qualifier. */
17505 static cp_ref_qualifier
17506 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
17508 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
17510 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17511 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
17516 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
17517 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17519 switch (token
->type
)
17522 curr_ref_qual
= REF_QUAL_LVALUE
;
17526 curr_ref_qual
= REF_QUAL_RVALUE
;
17530 curr_ref_qual
= REF_QUAL_NONE
;
17534 if (!curr_ref_qual
)
17538 error_at (token
->location
, "multiple ref-qualifiers");
17539 cp_lexer_purge_token (parser
->lexer
);
17543 ref_qual
= curr_ref_qual
;
17544 cp_lexer_consume_token (parser
->lexer
);
17551 /* Parse an (optional) virt-specifier-seq.
17553 virt-specifier-seq:
17554 virt-specifier virt-specifier-seq [opt]
17560 Returns a bitmask representing the virt-specifiers. */
17562 static cp_virt_specifiers
17563 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
17565 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
17570 cp_virt_specifiers virt_specifier
;
17572 /* Peek at the next token. */
17573 token
= cp_lexer_peek_token (parser
->lexer
);
17574 /* See if it's a virt-specifier-qualifier. */
17575 if (token
->type
!= CPP_NAME
)
17577 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
17579 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17580 virt_specifier
= VIRT_SPEC_OVERRIDE
;
17582 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
17584 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17585 virt_specifier
= VIRT_SPEC_FINAL
;
17587 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
17589 virt_specifier
= VIRT_SPEC_FINAL
;
17594 if (virt_specifiers
& virt_specifier
)
17596 error_at (token
->location
, "duplicate virt-specifier");
17597 cp_lexer_purge_token (parser
->lexer
);
17601 cp_lexer_consume_token (parser
->lexer
);
17602 virt_specifiers
|= virt_specifier
;
17605 return virt_specifiers
;
17608 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17609 is in scope even though it isn't real. */
17612 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
17616 if (current_class_ptr
)
17618 /* We don't clear this between NSDMIs. Is it already what we want? */
17619 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
17620 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
17621 && cp_type_quals (type
) == quals
)
17625 this_parm
= build_this_parm (ctype
, quals
);
17626 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17627 current_class_ptr
= NULL_TREE
;
17629 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
17630 current_class_ptr
= this_parm
;
17633 /* Return true iff our current scope is a non-static data member
17637 parsing_nsdmi (void)
17639 /* We recognize NSDMI context by the context-less 'this' pointer set up
17640 by the function above. */
17641 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
17646 /* Parse a late-specified return type, if any. This is not a separate
17647 non-terminal, but part of a function declarator, which looks like
17649 -> trailing-type-specifier-seq abstract-declarator(opt)
17651 Returns the type indicated by the type-id.
17653 In addition to this this parses any queued up omp declare simd
17656 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17660 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
17664 tree type
= NULL_TREE
;
17665 bool declare_simd_p
= (parser
->omp_declare_simd
17667 && declarator
->kind
== cdk_id
);
17669 /* Peek at the next token. */
17670 token
= cp_lexer_peek_token (parser
->lexer
);
17671 /* A late-specified return type is indicated by an initial '->'. */
17672 if (token
->type
!= CPP_DEREF
&& !declare_simd_p
)
17675 tree save_ccp
= current_class_ptr
;
17676 tree save_ccr
= current_class_ref
;
17679 /* DR 1207: 'this' is in scope in the trailing return type. */
17680 inject_this_parameter (current_class_type
, quals
);
17683 if (token
->type
== CPP_DEREF
)
17685 /* Consume the ->. */
17686 cp_lexer_consume_token (parser
->lexer
);
17688 type
= cp_parser_trailing_type_id (parser
);
17691 if (declare_simd_p
)
17692 declarator
->std_attributes
17693 = cp_parser_late_parsing_omp_declare_simd (parser
,
17694 declarator
->std_attributes
);
17698 current_class_ptr
= save_ccp
;
17699 current_class_ref
= save_ccr
;
17705 /* Parse a declarator-id.
17709 :: [opt] nested-name-specifier [opt] type-name
17711 In the `id-expression' case, the value returned is as for
17712 cp_parser_id_expression if the id-expression was an unqualified-id.
17713 If the id-expression was a qualified-id, then a SCOPE_REF is
17714 returned. The first operand is the scope (either a NAMESPACE_DECL
17715 or TREE_TYPE), but the second is still just a representation of an
17719 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
17722 /* The expression must be an id-expression. Assume that qualified
17723 names are the names of types so that:
17726 int S<T>::R::i = 3;
17728 will work; we must treat `S<T>::R' as the name of a type.
17729 Similarly, assume that qualified names are templates, where
17733 int S<T>::R<T>::i = 3;
17736 id
= cp_parser_id_expression (parser
,
17737 /*template_keyword_p=*/false,
17738 /*check_dependency_p=*/false,
17739 /*template_p=*/NULL
,
17740 /*declarator_p=*/true,
17742 if (id
&& BASELINK_P (id
))
17743 id
= BASELINK_FUNCTIONS (id
);
17747 /* Parse a type-id.
17750 type-specifier-seq abstract-declarator [opt]
17752 Returns the TYPE specified. */
17755 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
17756 bool is_trailing_return
)
17758 cp_decl_specifier_seq type_specifier_seq
;
17759 cp_declarator
*abstract_declarator
;
17761 /* Parse the type-specifier-seq. */
17762 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17763 is_trailing_return
,
17764 &type_specifier_seq
);
17765 if (type_specifier_seq
.type
== error_mark_node
)
17766 return error_mark_node
;
17768 /* There might or might not be an abstract declarator. */
17769 cp_parser_parse_tentatively (parser
);
17770 /* Look for the declarator. */
17771 abstract_declarator
17772 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
17773 /*parenthesized_p=*/NULL
,
17774 /*member_p=*/false);
17775 /* Check to see if there really was a declarator. */
17776 if (!cp_parser_parse_definitely (parser
))
17777 abstract_declarator
= NULL
;
17779 if (type_specifier_seq
.type
17780 && cxx_dialect
< cxx1y
17781 && type_uses_auto (type_specifier_seq
.type
))
17783 /* A type-id with type 'auto' is only ok if the abstract declarator
17784 is a function declarator with a late-specified return type. */
17785 if (abstract_declarator
17786 && abstract_declarator
->kind
== cdk_function
17787 && abstract_declarator
->u
.function
.late_return_type
)
17791 error ("invalid use of %<auto%>");
17792 return error_mark_node
;
17796 return groktypename (&type_specifier_seq
, abstract_declarator
,
17800 static tree
cp_parser_type_id (cp_parser
*parser
)
17802 return cp_parser_type_id_1 (parser
, false, false);
17805 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
17808 const char *saved_message
= parser
->type_definition_forbidden_message
;
17809 parser
->type_definition_forbidden_message
17810 = G_("types may not be defined in template arguments");
17811 r
= cp_parser_type_id_1 (parser
, true, false);
17812 parser
->type_definition_forbidden_message
= saved_message
;
17816 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
17818 return cp_parser_type_id_1 (parser
, false, true);
17821 /* Parse a type-specifier-seq.
17823 type-specifier-seq:
17824 type-specifier type-specifier-seq [opt]
17828 type-specifier-seq:
17829 attributes type-specifier-seq [opt]
17831 If IS_DECLARATION is true, we are at the start of a "condition" or
17832 exception-declaration, so we might be followed by a declarator-id.
17834 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17835 i.e. we've just seen "->".
17837 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17840 cp_parser_type_specifier_seq (cp_parser
* parser
,
17841 bool is_declaration
,
17842 bool is_trailing_return
,
17843 cp_decl_specifier_seq
*type_specifier_seq
)
17845 bool seen_type_specifier
= false;
17846 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
17847 cp_token
*start_token
= NULL
;
17849 /* Clear the TYPE_SPECIFIER_SEQ. */
17850 clear_decl_specs (type_specifier_seq
);
17852 /* In the context of a trailing return type, enum E { } is an
17853 elaborated-type-specifier followed by a function-body, not an
17855 if (is_trailing_return
)
17856 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
17858 /* Parse the type-specifiers and attributes. */
17861 tree type_specifier
;
17862 bool is_cv_qualifier
;
17864 /* Check for attributes first. */
17865 if (cp_next_tokens_can_be_attribute_p (parser
))
17867 type_specifier_seq
->attributes
=
17868 chainon (type_specifier_seq
->attributes
,
17869 cp_parser_attributes_opt (parser
));
17873 /* record the token of the beginning of the type specifier seq,
17874 for error reporting purposes*/
17876 start_token
= cp_lexer_peek_token (parser
->lexer
);
17878 /* Look for the type-specifier. */
17879 type_specifier
= cp_parser_type_specifier (parser
,
17881 type_specifier_seq
,
17882 /*is_declaration=*/false,
17885 if (!type_specifier
)
17887 /* If the first type-specifier could not be found, this is not a
17888 type-specifier-seq at all. */
17889 if (!seen_type_specifier
)
17891 cp_parser_error (parser
, "expected type-specifier");
17892 type_specifier_seq
->type
= error_mark_node
;
17895 /* If subsequent type-specifiers could not be found, the
17896 type-specifier-seq is complete. */
17900 seen_type_specifier
= true;
17901 /* The standard says that a condition can be:
17903 type-specifier-seq declarator = assignment-expression
17910 we should treat the "S" as a declarator, not as a
17911 type-specifier. The standard doesn't say that explicitly for
17912 type-specifier-seq, but it does say that for
17913 decl-specifier-seq in an ordinary declaration. Perhaps it
17914 would be clearer just to allow a decl-specifier-seq here, and
17915 then add a semantic restriction that if any decl-specifiers
17916 that are not type-specifiers appear, the program is invalid. */
17917 if (is_declaration
&& !is_cv_qualifier
)
17918 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
17922 /* Parse a parameter-declaration-clause.
17924 parameter-declaration-clause:
17925 parameter-declaration-list [opt] ... [opt]
17926 parameter-declaration-list , ...
17928 Returns a representation for the parameter declarations. A return
17929 value of NULL indicates a parameter-declaration-clause consisting
17930 only of an ellipsis. */
17933 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
17940 /* Peek at the next token. */
17941 token
= cp_lexer_peek_token (parser
->lexer
);
17942 /* Check for trivial parameter-declaration-clauses. */
17943 if (token
->type
== CPP_ELLIPSIS
)
17945 /* Consume the `...' token. */
17946 cp_lexer_consume_token (parser
->lexer
);
17949 else if (token
->type
== CPP_CLOSE_PAREN
)
17950 /* There are no parameters. */
17952 #ifndef NO_IMPLICIT_EXTERN_C
17953 if (in_system_header
&& current_class_type
== NULL
17954 && current_lang_name
== lang_name_c
)
17958 return void_list_node
;
17960 /* Check for `(void)', too, which is a special case. */
17961 else if (token
->keyword
== RID_VOID
17962 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17963 == CPP_CLOSE_PAREN
))
17965 /* Consume the `void' token. */
17966 cp_lexer_consume_token (parser
->lexer
);
17967 /* There are no parameters. */
17968 return void_list_node
;
17971 /* Parse the parameter-declaration-list. */
17972 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
17973 /* If a parse error occurred while parsing the
17974 parameter-declaration-list, then the entire
17975 parameter-declaration-clause is erroneous. */
17979 /* Peek at the next token. */
17980 token
= cp_lexer_peek_token (parser
->lexer
);
17981 /* If it's a `,', the clause should terminate with an ellipsis. */
17982 if (token
->type
== CPP_COMMA
)
17984 /* Consume the `,'. */
17985 cp_lexer_consume_token (parser
->lexer
);
17986 /* Expect an ellipsis. */
17988 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
17990 /* It might also be `...' if the optional trailing `,' was
17992 else if (token
->type
== CPP_ELLIPSIS
)
17994 /* Consume the `...' token. */
17995 cp_lexer_consume_token (parser
->lexer
);
17996 /* And remember that we saw it. */
18000 ellipsis_p
= false;
18002 /* Finish the parameter list. */
18004 parameters
= chainon (parameters
, void_list_node
);
18009 /* Parse a parameter-declaration-list.
18011 parameter-declaration-list:
18012 parameter-declaration
18013 parameter-declaration-list , parameter-declaration
18015 Returns a representation of the parameter-declaration-list, as for
18016 cp_parser_parameter_declaration_clause. However, the
18017 `void_list_node' is never appended to the list. Upon return,
18018 *IS_ERROR will be true iff an error occurred. */
18021 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18023 tree parameters
= NULL_TREE
;
18024 tree
*tail
= ¶meters
;
18025 bool saved_in_unbraced_linkage_specification_p
;
18027 int implicit_template_parms
= 0;
18029 /* Assume all will go well. */
18031 /* The special considerations that apply to a function within an
18032 unbraced linkage specifications do not apply to the parameters
18033 to the function. */
18034 saved_in_unbraced_linkage_specification_p
18035 = parser
->in_unbraced_linkage_specification_p
;
18036 parser
->in_unbraced_linkage_specification_p
= false;
18038 /* Look for more parameters. */
18041 cp_parameter_declarator
*parameter
;
18042 tree decl
= error_mark_node
;
18043 bool parenthesized_p
= false;
18044 /* Parse the parameter. */
18046 = cp_parser_parameter_declaration (parser
,
18047 /*template_parm_p=*/false,
18050 /* We don't know yet if the enclosing context is deprecated, so wait
18051 and warn in grokparms if appropriate. */
18052 deprecated_state
= DEPRECATED_SUPPRESS
;
18056 decl
= grokdeclarator (parameter
->declarator
,
18057 ¶meter
->decl_specifiers
,
18059 parameter
->default_argument
!= NULL_TREE
,
18060 ¶meter
->decl_specifiers
.attributes
);
18062 if (TREE_TYPE (decl
) != error_mark_node
18063 && parameter
->decl_specifiers
.type
18064 && is_auto_or_concept (parameter
->decl_specifiers
.type
))
18065 ++implicit_template_parms
;
18068 deprecated_state
= DEPRECATED_NORMAL
;
18070 /* If a parse error occurred parsing the parameter declaration,
18071 then the entire parameter-declaration-list is erroneous. */
18072 if (decl
== error_mark_node
)
18075 parameters
= error_mark_node
;
18079 if (parameter
->decl_specifiers
.attributes
)
18080 cplus_decl_attributes (&decl
,
18081 parameter
->decl_specifiers
.attributes
,
18083 if (DECL_NAME (decl
))
18084 decl
= pushdecl (decl
);
18086 if (decl
!= error_mark_node
)
18088 retrofit_lang_decl (decl
);
18089 DECL_PARM_INDEX (decl
) = ++index
;
18090 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18093 /* Add the new parameter to the list. */
18094 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18095 tail
= &TREE_CHAIN (*tail
);
18097 /* Peek at the next token. */
18098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18099 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18100 /* These are for Objective-C++ */
18101 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18102 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18103 /* The parameter-declaration-list is complete. */
18105 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18109 /* Peek at the next token. */
18110 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18111 /* If it's an ellipsis, then the list is complete. */
18112 if (token
->type
== CPP_ELLIPSIS
)
18114 /* Otherwise, there must be more parameters. Consume the
18116 cp_lexer_consume_token (parser
->lexer
);
18117 /* When parsing something like:
18119 int i(float f, double d)
18121 we can tell after seeing the declaration for "f" that we
18122 are not looking at an initialization of a variable "i",
18123 but rather at the declaration of a function "i".
18125 Due to the fact that the parsing of template arguments
18126 (as specified to a template-id) requires backtracking we
18127 cannot use this technique when inside a template argument
18129 if (!parser
->in_template_argument_list_p
18130 && !parser
->in_type_id_in_expr_p
18131 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18132 /* However, a parameter-declaration of the form
18133 "float(f)" (which is a valid declaration of a
18134 parameter "f") can also be interpreted as an
18135 expression (the conversion of "f" to "float"). */
18136 && !parenthesized_p
)
18137 cp_parser_commit_to_tentative_parse (parser
);
18141 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18142 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18143 cp_parser_skip_to_closing_parenthesis (parser
,
18144 /*recovering=*/true,
18145 /*or_comma=*/false,
18146 /*consume_paren=*/false);
18151 parser
->in_unbraced_linkage_specification_p
18152 = saved_in_unbraced_linkage_specification_p
;
18154 if (parameters
!= error_mark_node
&& implicit_template_parms
)
18155 parameters
= add_implicit_template_parms (parser
,
18156 implicit_template_parms
,
18162 /* Parse a parameter declaration.
18164 parameter-declaration:
18165 decl-specifier-seq ... [opt] declarator
18166 decl-specifier-seq declarator = assignment-expression
18167 decl-specifier-seq ... [opt] abstract-declarator [opt]
18168 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18170 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18171 declares a template parameter. (In that case, a non-nested `>'
18172 token encountered during the parsing of the assignment-expression
18173 is not interpreted as a greater-than operator.)
18175 Returns a representation of the parameter, or NULL if an error
18176 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18177 true iff the declarator is of the form "(p)". */
18179 static cp_parameter_declarator
*
18180 cp_parser_parameter_declaration (cp_parser
*parser
,
18181 bool template_parm_p
,
18182 bool *parenthesized_p
)
18184 int declares_class_or_enum
;
18185 cp_decl_specifier_seq decl_specifiers
;
18186 cp_declarator
*declarator
;
18187 tree default_argument
;
18188 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18189 const char *saved_message
;
18191 /* In a template parameter, `>' is not an operator.
18195 When parsing a default template-argument for a non-type
18196 template-parameter, the first non-nested `>' is taken as the end
18197 of the template parameter-list rather than a greater-than
18200 /* Type definitions may not appear in parameter types. */
18201 saved_message
= parser
->type_definition_forbidden_message
;
18202 parser
->type_definition_forbidden_message
18203 = G_("types may not be defined in parameter types");
18205 /* Parse the declaration-specifiers. */
18206 cp_parser_decl_specifier_seq (parser
,
18207 CP_PARSER_FLAGS_NONE
,
18209 &declares_class_or_enum
);
18211 /* Complain about missing 'typename' or other invalid type names. */
18212 if (!decl_specifiers
.any_type_specifiers_p
18213 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18214 decl_specifiers
.type
= error_mark_node
;
18216 /* If an error occurred, there's no reason to attempt to parse the
18217 rest of the declaration. */
18218 if (cp_parser_error_occurred (parser
))
18220 parser
->type_definition_forbidden_message
= saved_message
;
18224 /* Peek at the next token. */
18225 token
= cp_lexer_peek_token (parser
->lexer
);
18227 /* If the next token is a `)', `,', `=', `>', or `...', then there
18228 is no declarator. However, when variadic templates are enabled,
18229 there may be a declarator following `...'. */
18230 if (token
->type
== CPP_CLOSE_PAREN
18231 || token
->type
== CPP_COMMA
18232 || token
->type
== CPP_EQ
18233 || token
->type
== CPP_GREATER
)
18236 if (parenthesized_p
)
18237 *parenthesized_p
= false;
18239 /* Otherwise, there should be a declarator. */
18242 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18243 parser
->default_arg_ok_p
= false;
18245 /* After seeing a decl-specifier-seq, if the next token is not a
18246 "(", there is no possibility that the code is a valid
18247 expression. Therefore, if parsing tentatively, we commit at
18249 if (!parser
->in_template_argument_list_p
18250 /* In an expression context, having seen:
18254 we cannot be sure whether we are looking at a
18255 function-type (taking a "char" as a parameter) or a cast
18256 of some object of type "char" to "int". */
18257 && !parser
->in_type_id_in_expr_p
18258 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18259 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
18260 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
18261 cp_parser_commit_to_tentative_parse (parser
);
18262 /* Parse the declarator. */
18263 declarator_token_start
= token
;
18264 declarator
= cp_parser_declarator (parser
,
18265 CP_PARSER_DECLARATOR_EITHER
,
18266 /*ctor_dtor_or_conv_p=*/NULL
,
18268 /*member_p=*/false);
18269 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18270 /* After the declarator, allow more attributes. */
18271 decl_specifiers
.attributes
18272 = chainon (decl_specifiers
.attributes
,
18273 cp_parser_attributes_opt (parser
));
18276 /* If the next token is an ellipsis, and we have not seen a
18277 declarator name, and the type of the declarator contains parameter
18278 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18279 a parameter pack expansion expression. Otherwise, leave the
18280 ellipsis for a C-style variadic function. */
18281 token
= cp_lexer_peek_token (parser
->lexer
);
18282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18284 tree type
= decl_specifiers
.type
;
18286 if (type
&& DECL_P (type
))
18287 type
= TREE_TYPE (type
);
18290 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
18291 && declarator_can_be_parameter_pack (declarator
)
18292 && (!declarator
|| !declarator
->parameter_pack_p
)
18293 && uses_parameter_packs (type
))
18295 /* Consume the `...'. */
18296 cp_lexer_consume_token (parser
->lexer
);
18297 maybe_warn_variadic_templates ();
18299 /* Build a pack expansion type */
18301 declarator
->parameter_pack_p
= true;
18303 decl_specifiers
.type
= make_pack_expansion (type
);
18307 /* The restriction on defining new types applies only to the type
18308 of the parameter, not to the default argument. */
18309 parser
->type_definition_forbidden_message
= saved_message
;
18311 /* If the next token is `=', then process a default argument. */
18312 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18314 token
= cp_lexer_peek_token (parser
->lexer
);
18315 /* If we are defining a class, then the tokens that make up the
18316 default argument must be saved and processed later. */
18317 if (!template_parm_p
&& at_class_scope_p ()
18318 && TYPE_BEING_DEFINED (current_class_type
)
18319 && !LAMBDA_TYPE_P (current_class_type
))
18320 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
18321 /* Outside of a class definition, we can just parse the
18322 assignment-expression. */
18325 = cp_parser_default_argument (parser
, template_parm_p
);
18327 if (!parser
->default_arg_ok_p
)
18329 if (flag_permissive
)
18330 warning (0, "deprecated use of default argument for parameter of non-function");
18333 error_at (token
->location
,
18334 "default arguments are only "
18335 "permitted for function parameters");
18336 default_argument
= NULL_TREE
;
18339 else if ((declarator
&& declarator
->parameter_pack_p
)
18340 || (decl_specifiers
.type
18341 && PACK_EXPANSION_P (decl_specifiers
.type
)))
18343 /* Find the name of the parameter pack. */
18344 cp_declarator
*id_declarator
= declarator
;
18345 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
18346 id_declarator
= id_declarator
->declarator
;
18348 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
18349 error_at (declarator_token_start
->location
,
18351 ? G_("template parameter pack %qD "
18352 "cannot have a default argument")
18353 : G_("parameter pack %qD cannot have "
18354 "a default argument"),
18355 id_declarator
->u
.id
.unqualified_name
);
18357 error_at (declarator_token_start
->location
,
18359 ? G_("template parameter pack cannot have "
18360 "a default argument")
18361 : G_("parameter pack cannot have a "
18362 "default argument"));
18364 default_argument
= NULL_TREE
;
18368 default_argument
= NULL_TREE
;
18370 return make_parameter_declarator (&decl_specifiers
,
18375 /* Parse a default argument and return it.
18377 TEMPLATE_PARM_P is true if this is a default argument for a
18378 non-type template parameter. */
18380 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
18382 tree default_argument
= NULL_TREE
;
18383 bool saved_greater_than_is_operator_p
;
18384 bool saved_local_variables_forbidden_p
;
18385 bool non_constant_p
, is_direct_init
;
18387 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18389 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
18390 parser
->greater_than_is_operator_p
= !template_parm_p
;
18391 /* Local variable names (and the `this' keyword) may not
18392 appear in a default argument. */
18393 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
18394 parser
->local_variables_forbidden_p
= true;
18395 /* Parse the assignment-expression. */
18396 if (template_parm_p
)
18397 push_deferring_access_checks (dk_no_deferred
);
18399 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
18400 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
18401 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18402 if (template_parm_p
)
18403 pop_deferring_access_checks ();
18404 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
18405 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
18407 return default_argument
;
18410 /* Parse a function-body.
18413 compound_statement */
18416 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
18418 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
18421 /* Parse a ctor-initializer-opt followed by a function-body. Return
18422 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18423 is true we are parsing a function-try-block. */
18426 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
18427 bool in_function_try_block
)
18430 bool ctor_initializer_p
;
18431 const bool check_body_p
=
18432 DECL_CONSTRUCTOR_P (current_function_decl
)
18433 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
18436 /* Begin the function body. */
18437 body
= begin_function_body ();
18438 /* Parse the optional ctor-initializer. */
18439 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
18441 /* If we're parsing a constexpr constructor definition, we need
18442 to check that the constructor body is indeed empty. However,
18443 before we get to cp_parser_function_body lot of junk has been
18444 generated, so we can't just check that we have an empty block.
18445 Rather we take a snapshot of the outermost block, and check whether
18446 cp_parser_function_body changed its state. */
18449 list
= cur_stmt_list
;
18450 if (STATEMENT_LIST_TAIL (list
))
18451 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
18453 /* Parse the function-body. */
18454 cp_parser_function_body (parser
, in_function_try_block
);
18456 check_constexpr_ctor_body (last
, list
);
18457 /* Finish the function body. */
18458 finish_function_body (body
);
18460 return ctor_initializer_p
;
18463 /* Parse an initializer.
18466 = initializer-clause
18467 ( expression-list )
18469 Returns an expression representing the initializer. If no
18470 initializer is present, NULL_TREE is returned.
18472 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18473 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18474 set to TRUE if there is no initializer present. If there is an
18475 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18476 is set to true; otherwise it is set to false. */
18479 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
18480 bool* non_constant_p
)
18485 /* Peek at the next token. */
18486 token
= cp_lexer_peek_token (parser
->lexer
);
18488 /* Let our caller know whether or not this initializer was
18490 *is_direct_init
= (token
->type
!= CPP_EQ
);
18491 /* Assume that the initializer is constant. */
18492 *non_constant_p
= false;
18494 if (token
->type
== CPP_EQ
)
18496 /* Consume the `='. */
18497 cp_lexer_consume_token (parser
->lexer
);
18498 /* Parse the initializer-clause. */
18499 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
18501 else if (token
->type
== CPP_OPEN_PAREN
)
18503 vec
<tree
, va_gc
> *vec
;
18504 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
18506 /*allow_expansion_p=*/true,
18509 return error_mark_node
;
18510 init
= build_tree_list_vec (vec
);
18511 release_tree_vector (vec
);
18513 else if (token
->type
== CPP_OPEN_BRACE
)
18515 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18516 init
= cp_parser_braced_list (parser
, non_constant_p
);
18517 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
18521 /* Anything else is an error. */
18522 cp_parser_error (parser
, "expected initializer");
18523 init
= error_mark_node
;
18529 /* Parse an initializer-clause.
18531 initializer-clause:
18532 assignment-expression
18535 Returns an expression representing the initializer.
18537 If the `assignment-expression' production is used the value
18538 returned is simply a representation for the expression.
18540 Otherwise, calls cp_parser_braced_list. */
18543 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
18547 /* Assume the expression is constant. */
18548 *non_constant_p
= false;
18550 /* If it is not a `{', then we are looking at an
18551 assignment-expression. */
18552 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
18555 = cp_parser_constant_expression (parser
,
18556 /*allow_non_constant_p=*/true,
18560 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
18562 return initializer
;
18565 /* Parse a brace-enclosed initializer list.
18568 { initializer-list , [opt] }
18571 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18572 the elements of the initializer-list (or NULL, if the last
18573 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18574 NULL_TREE. There is no way to detect whether or not the optional
18575 trailing `,' was provided. NON_CONSTANT_P is as for
18576 cp_parser_initializer. */
18579 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
18583 /* Consume the `{' token. */
18584 cp_lexer_consume_token (parser
->lexer
);
18585 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18586 initializer
= make_node (CONSTRUCTOR
);
18587 /* If it's not a `}', then there is a non-trivial initializer. */
18588 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
18590 /* Parse the initializer list. */
18591 CONSTRUCTOR_ELTS (initializer
)
18592 = cp_parser_initializer_list (parser
, non_constant_p
);
18593 /* A trailing `,' token is allowed. */
18594 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18595 cp_lexer_consume_token (parser
->lexer
);
18598 *non_constant_p
= false;
18599 /* Now, there should be a trailing `}'. */
18600 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18601 TREE_TYPE (initializer
) = init_list_type_node
;
18602 return initializer
;
18605 /* Parse an initializer-list.
18608 initializer-clause ... [opt]
18609 initializer-list , initializer-clause ... [opt]
18614 designation initializer-clause ...[opt]
18615 initializer-list , designation initializer-clause ...[opt]
18620 [ constant-expression ] =
18622 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18623 for the initializer. If the INDEX of the elt is non-NULL, it is the
18624 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18625 as for cp_parser_initializer. */
18627 static vec
<constructor_elt
, va_gc
> *
18628 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
18630 vec
<constructor_elt
, va_gc
> *v
= NULL
;
18632 /* Assume all of the expressions are constant. */
18633 *non_constant_p
= false;
18635 /* Parse the rest of the list. */
18641 bool clause_non_constant_p
;
18643 /* If the next token is an identifier and the following one is a
18644 colon, we are looking at the GNU designated-initializer
18646 if (cp_parser_allow_gnu_extensions_p (parser
)
18647 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
18648 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
18650 /* Warn the user that they are using an extension. */
18651 pedwarn (input_location
, OPT_Wpedantic
,
18652 "ISO C++ does not allow designated initializers");
18653 /* Consume the identifier. */
18654 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18655 /* Consume the `:'. */
18656 cp_lexer_consume_token (parser
->lexer
);
18658 /* Also handle the C99 syntax, '. id ='. */
18659 else if (cp_parser_allow_gnu_extensions_p (parser
)
18660 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
18661 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
18662 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
18664 /* Warn the user that they are using an extension. */
18665 pedwarn (input_location
, OPT_Wpedantic
,
18666 "ISO C++ does not allow C99 designated initializers");
18667 /* Consume the `.'. */
18668 cp_lexer_consume_token (parser
->lexer
);
18669 /* Consume the identifier. */
18670 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18671 /* Consume the `='. */
18672 cp_lexer_consume_token (parser
->lexer
);
18674 /* Also handle C99 array designators, '[ const ] ='. */
18675 else if (cp_parser_allow_gnu_extensions_p (parser
)
18676 && !c_dialect_objc ()
18677 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
18679 /* In C++11, [ could start a lambda-introducer. */
18680 bool non_const
= false;
18682 cp_parser_parse_tentatively (parser
);
18683 cp_lexer_consume_token (parser
->lexer
);
18684 designator
= cp_parser_constant_expression (parser
, true, &non_const
);
18685 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
18686 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18687 if (!cp_parser_parse_definitely (parser
))
18688 designator
= NULL_TREE
;
18689 else if (non_const
)
18690 require_potential_rvalue_constant_expression (designator
);
18693 designator
= NULL_TREE
;
18695 /* Parse the initializer. */
18696 initializer
= cp_parser_initializer_clause (parser
,
18697 &clause_non_constant_p
);
18698 /* If any clause is non-constant, so is the entire initializer. */
18699 if (clause_non_constant_p
)
18700 *non_constant_p
= true;
18702 /* If we have an ellipsis, this is an initializer pack
18704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18706 /* Consume the `...'. */
18707 cp_lexer_consume_token (parser
->lexer
);
18709 /* Turn the initializer into an initializer expansion. */
18710 initializer
= make_pack_expansion (initializer
);
18713 /* Add it to the vector. */
18714 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
18716 /* If the next token is not a comma, we have reached the end of
18718 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18721 /* Peek at the next token. */
18722 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18723 /* If the next token is a `}', then we're still done. An
18724 initializer-clause can have a trailing `,' after the
18725 initializer-list and before the closing `}'. */
18726 if (token
->type
== CPP_CLOSE_BRACE
)
18729 /* Consume the `,' token. */
18730 cp_lexer_consume_token (parser
->lexer
);
18736 /* Classes [gram.class] */
18738 /* Parse a class-name.
18744 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18745 to indicate that names looked up in dependent types should be
18746 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18747 keyword has been used to indicate that the name that appears next
18748 is a template. TAG_TYPE indicates the explicit tag given before
18749 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18750 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18751 is the class being defined in a class-head.
18753 Returns the TYPE_DECL representing the class. */
18756 cp_parser_class_name (cp_parser
*parser
,
18757 bool typename_keyword_p
,
18758 bool template_keyword_p
,
18759 enum tag_types tag_type
,
18760 bool check_dependency_p
,
18762 bool is_declaration
)
18768 tree identifier
= NULL_TREE
;
18770 /* All class-names start with an identifier. */
18771 token
= cp_lexer_peek_token (parser
->lexer
);
18772 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
18774 cp_parser_error (parser
, "expected class-name");
18775 return error_mark_node
;
18778 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18779 to a template-id, so we save it here. */
18780 scope
= parser
->scope
;
18781 if (scope
== error_mark_node
)
18782 return error_mark_node
;
18784 /* Any name names a type if we're following the `typename' keyword
18785 in a qualified name where the enclosing scope is type-dependent. */
18786 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
18787 && dependent_type_p (scope
));
18788 /* Handle the common case (an identifier, but not a template-id)
18790 if (token
->type
== CPP_NAME
18791 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
18793 cp_token
*identifier_token
;
18796 /* Look for the identifier. */
18797 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
18798 ambiguous_p
= identifier_token
->ambiguous_p
;
18799 identifier
= cp_parser_identifier (parser
);
18800 /* If the next token isn't an identifier, we are certainly not
18801 looking at a class-name. */
18802 if (identifier
== error_mark_node
)
18803 decl
= error_mark_node
;
18804 /* If we know this is a type-name, there's no need to look it
18806 else if (typename_p
)
18810 tree ambiguous_decls
;
18811 /* If we already know that this lookup is ambiguous, then
18812 we've already issued an error message; there's no reason
18816 cp_parser_simulate_error (parser
);
18817 return error_mark_node
;
18819 /* If the next token is a `::', then the name must be a type
18822 [basic.lookup.qual]
18824 During the lookup for a name preceding the :: scope
18825 resolution operator, object, function, and enumerator
18826 names are ignored. */
18827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18828 tag_type
= typename_type
;
18829 /* Look up the name. */
18830 decl
= cp_parser_lookup_name (parser
, identifier
,
18832 /*is_template=*/false,
18833 /*is_namespace=*/false,
18834 check_dependency_p
,
18836 identifier_token
->location
);
18837 if (ambiguous_decls
)
18839 if (cp_parser_parsing_tentatively (parser
))
18840 cp_parser_simulate_error (parser
);
18841 return error_mark_node
;
18847 /* Try a template-id. */
18848 decl
= cp_parser_template_id (parser
, template_keyword_p
,
18849 check_dependency_p
,
18852 if (decl
== error_mark_node
)
18853 return error_mark_node
;
18856 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
18858 /* If this is a typename, create a TYPENAME_TYPE. */
18859 if (typename_p
&& decl
!= error_mark_node
)
18861 decl
= make_typename_type (scope
, decl
, typename_type
,
18862 /*complain=*/tf_error
);
18863 if (decl
!= error_mark_node
)
18864 decl
= TYPE_NAME (decl
);
18867 decl
= strip_using_decl (decl
);
18869 /* Check to see that it is really the name of a class. */
18870 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18871 && identifier_p (TREE_OPERAND (decl
, 0))
18872 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18873 /* Situations like this:
18875 template <typename T> struct A {
18876 typename T::template X<int>::I i;
18879 are problematic. Is `T::template X<int>' a class-name? The
18880 standard does not seem to be definitive, but there is no other
18881 valid interpretation of the following `::'. Therefore, those
18882 names are considered class-names. */
18884 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
18885 if (decl
!= error_mark_node
)
18886 decl
= TYPE_NAME (decl
);
18888 else if (TREE_CODE (decl
) != TYPE_DECL
18889 || TREE_TYPE (decl
) == error_mark_node
18890 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
18891 /* In Objective-C 2.0, a classname followed by '.' starts a
18892 dot-syntax expression, and it's not a type-name. */
18893 || (c_dialect_objc ()
18894 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
18895 && objc_is_class_name (decl
)))
18896 decl
= error_mark_node
;
18898 if (decl
== error_mark_node
)
18899 cp_parser_error (parser
, "expected class-name");
18900 else if (identifier
&& !parser
->scope
)
18901 maybe_note_name_used_in_class (identifier
, decl
);
18906 /* Parse a class-specifier.
18909 class-head { member-specification [opt] }
18911 Returns the TREE_TYPE representing the class. */
18914 cp_parser_class_specifier_1 (cp_parser
* parser
)
18917 tree attributes
= NULL_TREE
;
18918 bool nested_name_specifier_p
;
18919 unsigned saved_num_template_parameter_lists
;
18920 bool saved_in_function_body
;
18921 unsigned char in_statement
;
18922 bool in_switch_statement_p
;
18923 bool saved_in_unbraced_linkage_specification_p
;
18924 tree old_scope
= NULL_TREE
;
18925 tree scope
= NULL_TREE
;
18926 cp_token
*closing_brace
;
18928 push_deferring_access_checks (dk_no_deferred
);
18930 /* Parse the class-head. */
18931 type
= cp_parser_class_head (parser
,
18932 &nested_name_specifier_p
);
18933 /* If the class-head was a semantic disaster, skip the entire body
18937 cp_parser_skip_to_end_of_block_or_statement (parser
);
18938 pop_deferring_access_checks ();
18939 return error_mark_node
;
18942 /* Look for the `{'. */
18943 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
18945 pop_deferring_access_checks ();
18946 return error_mark_node
;
18949 cp_ensure_no_omp_declare_simd (parser
);
18951 /* Issue an error message if type-definitions are forbidden here. */
18952 cp_parser_check_type_definition (parser
);
18953 /* Remember that we are defining one more class. */
18954 ++parser
->num_classes_being_defined
;
18955 /* Inside the class, surrounding template-parameter-lists do not
18957 saved_num_template_parameter_lists
18958 = parser
->num_template_parameter_lists
;
18959 parser
->num_template_parameter_lists
= 0;
18960 /* We are not in a function body. */
18961 saved_in_function_body
= parser
->in_function_body
;
18962 parser
->in_function_body
= false;
18963 /* Or in a loop. */
18964 in_statement
= parser
->in_statement
;
18965 parser
->in_statement
= 0;
18966 /* Or in a switch. */
18967 in_switch_statement_p
= parser
->in_switch_statement_p
;
18968 parser
->in_switch_statement_p
= false;
18969 /* We are not immediately inside an extern "lang" block. */
18970 saved_in_unbraced_linkage_specification_p
18971 = parser
->in_unbraced_linkage_specification_p
;
18972 parser
->in_unbraced_linkage_specification_p
= false;
18974 /* Start the class. */
18975 if (nested_name_specifier_p
)
18977 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
18978 old_scope
= push_inner_scope (scope
);
18980 type
= begin_class_definition (type
);
18982 if (type
== error_mark_node
)
18983 /* If the type is erroneous, skip the entire body of the class. */
18984 cp_parser_skip_to_closing_brace (parser
);
18986 /* Parse the member-specification. */
18987 cp_parser_member_specification_opt (parser
);
18989 /* Look for the trailing `}'. */
18990 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18991 /* Look for trailing attributes to apply to this class. */
18992 if (cp_parser_allow_gnu_extensions_p (parser
))
18993 attributes
= cp_parser_gnu_attributes_opt (parser
);
18994 if (type
!= error_mark_node
)
18995 type
= finish_struct (type
, attributes
);
18996 if (nested_name_specifier_p
)
18997 pop_inner_scope (old_scope
, scope
);
18999 /* We've finished a type definition. Check for the common syntax
19000 error of forgetting a semicolon after the definition. We need to
19001 be careful, as we can't just check for not-a-semicolon and be done
19002 with it; the user might have typed:
19004 class X { } c = ...;
19005 class X { } *p = ...;
19007 and so forth. Instead, enumerate all the possible tokens that
19008 might follow this production; if we don't see one of them, then
19009 complain and silently insert the semicolon. */
19011 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19012 bool want_semicolon
= true;
19014 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19015 /* Don't try to parse c++11 attributes here. As per the
19016 grammar, that should be a task for
19017 cp_parser_decl_specifier_seq. */
19018 want_semicolon
= false;
19020 switch (token
->type
)
19023 case CPP_SEMICOLON
:
19026 case CPP_OPEN_PAREN
:
19027 case CPP_CLOSE_PAREN
:
19029 want_semicolon
= false;
19032 /* While it's legal for type qualifiers and storage class
19033 specifiers to follow type definitions in the grammar, only
19034 compiler testsuites contain code like that. Assume that if
19035 we see such code, then what we're really seeing is a case
19039 const <type> var = ...;
19044 static <type> func (...) ...
19046 i.e. the qualifier or specifier applies to the next
19047 declaration. To do so, however, we need to look ahead one
19048 more token to see if *that* token is a type specifier.
19050 This code could be improved to handle:
19053 static const <type> var = ...; */
19055 if (keyword_is_decl_specifier (token
->keyword
))
19057 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19059 /* Handling user-defined types here would be nice, but very
19062 = (lookahead
->type
== CPP_KEYWORD
19063 && keyword_begins_type_specifier (lookahead
->keyword
));
19070 /* If we don't have a type, then something is very wrong and we
19071 shouldn't try to do anything clever. Likewise for not seeing the
19073 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19075 cp_token_position prev
19076 = cp_lexer_previous_token_position (parser
->lexer
);
19077 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19078 location_t loc
= prev_token
->location
;
19080 if (CLASSTYPE_DECLARED_CLASS (type
))
19081 error_at (loc
, "expected %<;%> after class definition");
19082 else if (TREE_CODE (type
) == RECORD_TYPE
)
19083 error_at (loc
, "expected %<;%> after struct definition");
19084 else if (TREE_CODE (type
) == UNION_TYPE
)
19085 error_at (loc
, "expected %<;%> after union definition");
19087 gcc_unreachable ();
19089 /* Unget one token and smash it to look as though we encountered
19090 a semicolon in the input stream. */
19091 cp_lexer_set_token_position (parser
->lexer
, prev
);
19092 token
= cp_lexer_peek_token (parser
->lexer
);
19093 token
->type
= CPP_SEMICOLON
;
19094 token
->keyword
= RID_MAX
;
19098 /* If this class is not itself within the scope of another class,
19099 then we need to parse the bodies of all of the queued function
19100 definitions. Note that the queued functions defined in a class
19101 are not always processed immediately following the
19102 class-specifier for that class. Consider:
19105 struct B { void f() { sizeof (A); } };
19108 If `f' were processed before the processing of `A' were
19109 completed, there would be no way to compute the size of `A'.
19110 Note that the nesting we are interested in here is lexical --
19111 not the semantic nesting given by TYPE_CONTEXT. In particular,
19114 struct A { struct B; };
19115 struct A::B { void f() { } };
19117 there is no need to delay the parsing of `A::B::f'. */
19118 if (--parser
->num_classes_being_defined
== 0)
19121 tree class_type
= NULL_TREE
;
19122 tree pushed_scope
= NULL_TREE
;
19124 cp_default_arg_entry
*e
;
19125 tree save_ccp
, save_ccr
;
19127 /* In a first pass, parse default arguments to the functions.
19128 Then, in a second pass, parse the bodies of the functions.
19129 This two-phased approach handles cases like:
19137 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
19140 /* If there are default arguments that have not yet been processed,
19141 take care of them now. */
19142 if (class_type
!= e
->class_type
)
19145 pop_scope (pushed_scope
);
19146 class_type
= e
->class_type
;
19147 pushed_scope
= push_scope (class_type
);
19149 /* Make sure that any template parameters are in scope. */
19150 maybe_begin_member_template_processing (decl
);
19151 /* Parse the default argument expressions. */
19152 cp_parser_late_parsing_default_args (parser
, decl
);
19153 /* Remove any template parameters from the symbol table. */
19154 maybe_end_member_template_processing ();
19156 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
19157 /* Now parse any NSDMIs. */
19158 save_ccp
= current_class_ptr
;
19159 save_ccr
= current_class_ref
;
19160 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
19162 if (class_type
!= DECL_CONTEXT (decl
))
19165 pop_scope (pushed_scope
);
19166 class_type
= DECL_CONTEXT (decl
);
19167 pushed_scope
= push_scope (class_type
);
19169 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
19170 cp_parser_late_parsing_nsdmi (parser
, decl
);
19172 vec_safe_truncate (unparsed_nsdmis
, 0);
19173 current_class_ptr
= save_ccp
;
19174 current_class_ref
= save_ccr
;
19176 pop_scope (pushed_scope
);
19177 /* Now parse the body of the functions. */
19180 /* OpenMP UDRs need to be parsed before all other functions. */
19181 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19182 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
19183 cp_parser_late_parsing_for_member (parser
, decl
);
19184 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19185 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
19186 cp_parser_late_parsing_for_member (parser
, decl
);
19189 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19190 cp_parser_late_parsing_for_member (parser
, decl
);
19191 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
19194 /* Put back any saved access checks. */
19195 pop_deferring_access_checks ();
19197 /* Restore saved state. */
19198 parser
->in_switch_statement_p
= in_switch_statement_p
;
19199 parser
->in_statement
= in_statement
;
19200 parser
->in_function_body
= saved_in_function_body
;
19201 parser
->num_template_parameter_lists
19202 = saved_num_template_parameter_lists
;
19203 parser
->in_unbraced_linkage_specification_p
19204 = saved_in_unbraced_linkage_specification_p
;
19210 cp_parser_class_specifier (cp_parser
* parser
)
19213 timevar_push (TV_PARSE_STRUCT
);
19214 ret
= cp_parser_class_specifier_1 (parser
);
19215 timevar_pop (TV_PARSE_STRUCT
);
19219 /* Parse a class-head.
19222 class-key identifier [opt] base-clause [opt]
19223 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19224 class-key nested-name-specifier [opt] template-id
19227 class-virt-specifier:
19231 class-key attributes identifier [opt] base-clause [opt]
19232 class-key attributes nested-name-specifier identifier base-clause [opt]
19233 class-key attributes nested-name-specifier [opt] template-id
19236 Upon return BASES is initialized to the list of base classes (or
19237 NULL, if there are none) in the same form returned by
19238 cp_parser_base_clause.
19240 Returns the TYPE of the indicated class. Sets
19241 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19242 involving a nested-name-specifier was used, and FALSE otherwise.
19244 Returns error_mark_node if this is not a class-head.
19246 Returns NULL_TREE if the class-head is syntactically valid, but
19247 semantically invalid in a way that means we should skip the entire
19248 body of the class. */
19251 cp_parser_class_head (cp_parser
* parser
,
19252 bool* nested_name_specifier_p
)
19254 tree nested_name_specifier
;
19255 enum tag_types class_key
;
19256 tree id
= NULL_TREE
;
19257 tree type
= NULL_TREE
;
19260 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
19261 bool template_id_p
= false;
19262 bool qualified_p
= false;
19263 bool invalid_nested_name_p
= false;
19264 bool invalid_explicit_specialization_p
= false;
19265 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
19266 tree pushed_scope
= NULL_TREE
;
19267 unsigned num_templates
;
19268 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
19269 /* Assume no nested-name-specifier will be present. */
19270 *nested_name_specifier_p
= false;
19271 /* Assume no template parameter lists will be used in defining the
19274 parser
->colon_corrects_to_scope_p
= false;
19276 /* Look for the class-key. */
19277 class_key
= cp_parser_class_key (parser
);
19278 if (class_key
== none_type
)
19279 return error_mark_node
;
19281 /* Parse the attributes. */
19282 attributes
= cp_parser_attributes_opt (parser
);
19284 /* If the next token is `::', that is invalid -- but sometimes
19285 people do try to write:
19289 Handle this gracefully by accepting the extra qualifier, and then
19290 issuing an error about it later if this really is a
19291 class-head. If it turns out just to be an elaborated type
19292 specifier, remain silent. */
19293 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
19294 qualified_p
= true;
19296 push_deferring_access_checks (dk_no_check
);
19298 /* Determine the name of the class. Begin by looking for an
19299 optional nested-name-specifier. */
19300 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
19301 nested_name_specifier
19302 = cp_parser_nested_name_specifier_opt (parser
,
19303 /*typename_keyword_p=*/false,
19304 /*check_dependency_p=*/false,
19306 /*is_declaration=*/false);
19307 /* If there was a nested-name-specifier, then there *must* be an
19309 if (nested_name_specifier
)
19311 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19312 /* Although the grammar says `identifier', it really means
19313 `class-name' or `template-name'. You are only allowed to
19314 define a class that has already been declared with this
19317 The proposed resolution for Core Issue 180 says that wherever
19318 you see `class T::X' you should treat `X' as a type-name.
19320 It is OK to define an inaccessible class; for example:
19322 class A { class B; };
19325 We do not know if we will see a class-name, or a
19326 template-name. We look for a class-name first, in case the
19327 class-name is a template-id; if we looked for the
19328 template-name first we would stop after the template-name. */
19329 cp_parser_parse_tentatively (parser
);
19330 type
= cp_parser_class_name (parser
,
19331 /*typename_keyword_p=*/false,
19332 /*template_keyword_p=*/false,
19334 /*check_dependency_p=*/false,
19335 /*class_head_p=*/true,
19336 /*is_declaration=*/false);
19337 /* If that didn't work, ignore the nested-name-specifier. */
19338 if (!cp_parser_parse_definitely (parser
))
19340 invalid_nested_name_p
= true;
19341 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19342 id
= cp_parser_identifier (parser
);
19343 if (id
== error_mark_node
)
19346 /* If we could not find a corresponding TYPE, treat this
19347 declaration like an unqualified declaration. */
19348 if (type
== error_mark_node
)
19349 nested_name_specifier
= NULL_TREE
;
19350 /* Otherwise, count the number of templates used in TYPE and its
19351 containing scopes. */
19356 for (scope
= TREE_TYPE (type
);
19357 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
19358 scope
= get_containing_scope (scope
))
19360 && CLASS_TYPE_P (scope
)
19361 && CLASSTYPE_TEMPLATE_INFO (scope
)
19362 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
19363 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
19364 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
19368 /* Otherwise, the identifier is optional. */
19371 /* We don't know whether what comes next is a template-id,
19372 an identifier, or nothing at all. */
19373 cp_parser_parse_tentatively (parser
);
19374 /* Check for a template-id. */
19375 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19376 id
= cp_parser_template_id (parser
,
19377 /*template_keyword_p=*/false,
19378 /*check_dependency_p=*/true,
19380 /*is_declaration=*/true);
19381 /* If that didn't work, it could still be an identifier. */
19382 if (!cp_parser_parse_definitely (parser
))
19384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19386 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19387 id
= cp_parser_identifier (parser
);
19394 template_id_p
= true;
19399 pop_deferring_access_checks ();
19403 cp_parser_check_for_invalid_template_id (parser
, id
,
19405 type_start_token
->location
);
19407 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19409 /* If it's not a `:' or a `{' then we can't really be looking at a
19410 class-head, since a class-head only appears as part of a
19411 class-specifier. We have to detect this situation before calling
19412 xref_tag, since that has irreversible side-effects. */
19413 if (!cp_parser_next_token_starts_class_definition_p (parser
))
19415 cp_parser_error (parser
, "expected %<{%> or %<:%>");
19416 type
= error_mark_node
;
19420 /* At this point, we're going ahead with the class-specifier, even
19421 if some other problem occurs. */
19422 cp_parser_commit_to_tentative_parse (parser
);
19423 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
19425 cp_parser_error (parser
,
19426 "cannot specify %<override%> for a class");
19427 type
= error_mark_node
;
19430 /* Issue the error about the overly-qualified name now. */
19433 cp_parser_error (parser
,
19434 "global qualification of class name is invalid");
19435 type
= error_mark_node
;
19438 else if (invalid_nested_name_p
)
19440 cp_parser_error (parser
,
19441 "qualified name does not name a class");
19442 type
= error_mark_node
;
19445 else if (nested_name_specifier
)
19449 /* Reject typedef-names in class heads. */
19450 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
19452 error_at (type_start_token
->location
,
19453 "invalid class name in declaration of %qD",
19459 /* Figure out in what scope the declaration is being placed. */
19460 scope
= current_scope ();
19461 /* If that scope does not contain the scope in which the
19462 class was originally declared, the program is invalid. */
19463 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
19465 if (at_namespace_scope_p ())
19466 error_at (type_start_token
->location
,
19467 "declaration of %qD in namespace %qD which does not "
19469 type
, scope
, nested_name_specifier
);
19471 error_at (type_start_token
->location
,
19472 "declaration of %qD in %qD which does not enclose %qD",
19473 type
, scope
, nested_name_specifier
);
19479 A declarator-id shall not be qualified except for the
19480 definition of a ... nested class outside of its class
19481 ... [or] the definition or explicit instantiation of a
19482 class member of a namespace outside of its namespace. */
19483 if (scope
== nested_name_specifier
)
19485 permerror (nested_name_specifier_token_start
->location
,
19486 "extra qualification not allowed");
19487 nested_name_specifier
= NULL_TREE
;
19491 /* An explicit-specialization must be preceded by "template <>". If
19492 it is not, try to recover gracefully. */
19493 if (at_namespace_scope_p ()
19494 && parser
->num_template_parameter_lists
== 0
19497 error_at (type_start_token
->location
,
19498 "an explicit specialization must be preceded by %<template <>%>");
19499 invalid_explicit_specialization_p
= true;
19500 /* Take the same action that would have been taken by
19501 cp_parser_explicit_specialization. */
19502 ++parser
->num_template_parameter_lists
;
19503 begin_specialization ();
19505 /* There must be no "return" statements between this point and the
19506 end of this function; set "type "to the correct return value and
19507 use "goto done;" to return. */
19508 /* Make sure that the right number of template parameters were
19510 if (!cp_parser_check_template_parameters (parser
, num_templates
,
19511 type_start_token
->location
,
19512 /*declarator=*/NULL
))
19514 /* If something went wrong, there is no point in even trying to
19515 process the class-definition. */
19520 /* Look up the type. */
19523 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
19524 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
19525 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
19527 error_at (type_start_token
->location
,
19528 "function template %qD redeclared as a class template", id
);
19529 type
= error_mark_node
;
19533 type
= TREE_TYPE (id
);
19534 type
= maybe_process_partial_specialization (type
);
19536 if (nested_name_specifier
)
19537 pushed_scope
= push_scope (nested_name_specifier
);
19539 else if (nested_name_specifier
)
19545 template <typename T> struct S { struct T };
19546 template <typename T> struct S<T>::T { };
19548 we will get a TYPENAME_TYPE when processing the definition of
19549 `S::T'. We need to resolve it to the actual type before we
19550 try to define it. */
19551 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
19553 class_type
= resolve_typename_type (TREE_TYPE (type
),
19554 /*only_current_p=*/false);
19555 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
19556 type
= TYPE_NAME (class_type
);
19559 cp_parser_error (parser
, "could not resolve typename type");
19560 type
= error_mark_node
;
19564 if (maybe_process_partial_specialization (TREE_TYPE (type
))
19565 == error_mark_node
)
19571 class_type
= current_class_type
;
19572 /* Enter the scope indicated by the nested-name-specifier. */
19573 pushed_scope
= push_scope (nested_name_specifier
);
19574 /* Get the canonical version of this type. */
19575 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
19576 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19577 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
19579 type
= push_template_decl (type
);
19580 if (type
== error_mark_node
)
19587 type
= TREE_TYPE (type
);
19588 *nested_name_specifier_p
= true;
19590 else /* The name is not a nested name. */
19592 /* If the class was unnamed, create a dummy name. */
19594 id
= make_anon_name ();
19595 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
19596 parser
->num_template_parameter_lists
);
19599 /* Indicate whether this class was declared as a `class' or as a
19601 if (TREE_CODE (type
) == RECORD_TYPE
)
19602 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
19603 cp_parser_check_class_key (class_key
, type
);
19605 /* If this type was already complete, and we see another definition,
19606 that's an error. */
19607 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
19609 error_at (type_start_token
->location
, "redefinition of %q#T",
19611 error_at (type_start_token
->location
, "previous definition of %q+#T",
19616 else if (type
== error_mark_node
)
19621 /* Apply attributes now, before any use of the class as a template
19622 argument in its base list. */
19623 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
19624 fixup_attribute_variants (type
);
19627 /* We will have entered the scope containing the class; the names of
19628 base classes should be looked up in that context. For example:
19630 struct A { struct B {}; struct C; };
19631 struct A::C : B {};
19635 /* Get the list of base-classes, if there is one. */
19636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19637 bases
= cp_parser_base_clause (parser
);
19641 /* If we're really defining a class, process the base classes.
19642 If they're invalid, fail. */
19643 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
19644 && !xref_basetypes (type
, bases
))
19648 /* Leave the scope given by the nested-name-specifier. We will
19649 enter the class scope itself while processing the members. */
19651 pop_scope (pushed_scope
);
19653 if (invalid_explicit_specialization_p
)
19655 end_specialization ();
19656 --parser
->num_template_parameter_lists
;
19660 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
19661 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
19662 CLASSTYPE_FINAL (type
) = 1;
19664 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19668 /* Parse a class-key.
19675 Returns the kind of class-key specified, or none_type to indicate
19678 static enum tag_types
19679 cp_parser_class_key (cp_parser
* parser
)
19682 enum tag_types tag_type
;
19684 /* Look for the class-key. */
19685 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
19689 /* Check to see if the TOKEN is a class-key. */
19690 tag_type
= cp_parser_token_is_class_key (token
);
19692 cp_parser_error (parser
, "expected class-key");
19696 /* Parse an (optional) member-specification.
19698 member-specification:
19699 member-declaration member-specification [opt]
19700 access-specifier : member-specification [opt] */
19703 cp_parser_member_specification_opt (cp_parser
* parser
)
19710 /* Peek at the next token. */
19711 token
= cp_lexer_peek_token (parser
->lexer
);
19712 /* If it's a `}', or EOF then we've seen all the members. */
19713 if (token
->type
== CPP_CLOSE_BRACE
19714 || token
->type
== CPP_EOF
19715 || token
->type
== CPP_PRAGMA_EOL
)
19718 /* See if this token is a keyword. */
19719 keyword
= token
->keyword
;
19723 case RID_PROTECTED
:
19725 /* Consume the access-specifier. */
19726 cp_lexer_consume_token (parser
->lexer
);
19727 /* Remember which access-specifier is active. */
19728 current_access_specifier
= token
->u
.value
;
19729 /* Look for the `:'. */
19730 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
19734 /* Accept #pragmas at class scope. */
19735 if (token
->type
== CPP_PRAGMA
)
19737 cp_parser_pragma (parser
, pragma_member
);
19741 /* Otherwise, the next construction must be a
19742 member-declaration. */
19743 cp_parser_member_declaration (parser
);
19748 /* Parse a member-declaration.
19750 member-declaration:
19751 decl-specifier-seq [opt] member-declarator-list [opt] ;
19752 function-definition ; [opt]
19753 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19755 template-declaration
19758 member-declarator-list:
19760 member-declarator-list , member-declarator
19763 declarator pure-specifier [opt]
19764 declarator constant-initializer [opt]
19765 identifier [opt] : constant-expression
19769 member-declaration:
19770 __extension__ member-declaration
19773 declarator attributes [opt] pure-specifier [opt]
19774 declarator attributes [opt] constant-initializer [opt]
19775 identifier [opt] attributes [opt] : constant-expression
19779 member-declaration:
19780 static_assert-declaration */
19783 cp_parser_member_declaration (cp_parser
* parser
)
19785 cp_decl_specifier_seq decl_specifiers
;
19786 tree prefix_attributes
;
19788 int declares_class_or_enum
;
19790 cp_token
*token
= NULL
;
19791 cp_token
*decl_spec_token_start
= NULL
;
19792 cp_token
*initializer_token_start
= NULL
;
19793 int saved_pedantic
;
19794 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
19796 /* Check for the `__extension__' keyword. */
19797 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
19800 cp_parser_member_declaration (parser
);
19801 /* Restore the old value of the PEDANTIC flag. */
19802 pedantic
= saved_pedantic
;
19807 /* Check for a template-declaration. */
19808 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
19810 /* An explicit specialization here is an error condition, and we
19811 expect the specialization handler to detect and report this. */
19812 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
19813 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
19814 cp_parser_explicit_specialization (parser
);
19816 cp_parser_template_declaration (parser
, /*member_p=*/true);
19821 /* Check for a using-declaration. */
19822 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
19824 if (cxx_dialect
< cxx11
)
19826 /* Parse the using-declaration. */
19827 cp_parser_using_declaration (parser
,
19828 /*access_declaration_p=*/false);
19834 bool alias_decl_expected
;
19835 cp_parser_parse_tentatively (parser
);
19836 decl
= cp_parser_alias_declaration (parser
);
19837 /* Note that if we actually see the '=' token after the
19838 identifier, cp_parser_alias_declaration commits the
19839 tentative parse. In that case, we really expects an
19840 alias-declaration. Otherwise, we expect a using
19842 alias_decl_expected
=
19843 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
19844 cp_parser_parse_definitely (parser
);
19846 if (alias_decl_expected
)
19847 finish_member_declaration (decl
);
19849 cp_parser_using_declaration (parser
,
19850 /*access_declaration_p=*/false);
19855 /* Check for @defs. */
19856 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
19859 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
19860 ivar
= ivar_chains
;
19864 ivar
= TREE_CHAIN (member
);
19865 TREE_CHAIN (member
) = NULL_TREE
;
19866 finish_member_declaration (member
);
19871 /* If the next token is `static_assert' we have a static assertion. */
19872 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
19874 cp_parser_static_assert (parser
, /*member_p=*/true);
19878 parser
->colon_corrects_to_scope_p
= false;
19880 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
19883 /* Parse the decl-specifier-seq. */
19884 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
19885 cp_parser_decl_specifier_seq (parser
,
19886 CP_PARSER_FLAGS_OPTIONAL
,
19888 &declares_class_or_enum
);
19889 /* Check for an invalid type-name. */
19890 if (!decl_specifiers
.any_type_specifiers_p
19891 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19893 /* If there is no declarator, then the decl-specifier-seq should
19895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19897 /* If there was no decl-specifier-seq, and the next token is a
19898 `;', then we have something like:
19904 Each member-declaration shall declare at least one member
19905 name of the class. */
19906 if (!decl_specifiers
.any_specifiers_p
)
19908 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19909 if (!in_system_header_at (token
->location
))
19910 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
19916 /* See if this declaration is a friend. */
19917 friend_p
= cp_parser_friend_p (&decl_specifiers
);
19918 /* If there were decl-specifiers, check to see if there was
19919 a class-declaration. */
19920 type
= check_tag_decl (&decl_specifiers
,
19921 /*explicit_type_instantiation_p=*/false);
19922 /* Nested classes have already been added to the class, but
19923 a `friend' needs to be explicitly registered. */
19926 /* If the `friend' keyword was present, the friend must
19927 be introduced with a class-key. */
19928 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
19929 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
19930 "in C++03 a class-key must be used "
19931 "when declaring a friend");
19934 template <typename T> struct A {
19935 friend struct A<T>::B;
19938 A<T>::B will be represented by a TYPENAME_TYPE, and
19939 therefore not recognized by check_tag_decl. */
19942 type
= decl_specifiers
.type
;
19943 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
19944 type
= TREE_TYPE (type
);
19946 if (!type
|| !TYPE_P (type
))
19947 error_at (decl_spec_token_start
->location
,
19948 "friend declaration does not name a class or "
19951 make_friend_class (current_class_type
, type
,
19952 /*complain=*/true);
19954 /* If there is no TYPE, an error message will already have
19956 else if (!type
|| type
== error_mark_node
)
19958 /* An anonymous aggregate has to be handled specially; such
19959 a declaration really declares a data member (with a
19960 particular type), as opposed to a nested class. */
19961 else if (ANON_AGGR_TYPE_P (type
))
19964 if (decl_specifiers
.storage_class
!= sc_none
)
19965 error_at (decl_spec_token_start
->location
,
19966 "a storage class on an anonymous aggregate "
19967 "in class scope is not allowed");
19969 /* Remove constructors and such from TYPE, now that we
19970 know it is an anonymous aggregate. */
19971 fixup_anonymous_aggr (type
);
19972 /* And make the corresponding data member. */
19973 decl
= build_decl (decl_spec_token_start
->location
,
19974 FIELD_DECL
, NULL_TREE
, type
);
19975 /* Add it to the class. */
19976 finish_member_declaration (decl
);
19979 cp_parser_check_access_in_redeclaration
19981 decl_spec_token_start
->location
);
19986 bool assume_semicolon
= false;
19988 /* Clear attributes from the decl_specifiers but keep them
19989 around as prefix attributes that apply them to the entity
19991 prefix_attributes
= decl_specifiers
.attributes
;
19992 decl_specifiers
.attributes
= NULL_TREE
;
19994 /* See if these declarations will be friends. */
19995 friend_p
= cp_parser_friend_p (&decl_specifiers
);
19997 /* Keep going until we hit the `;' at the end of the
19999 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20001 tree attributes
= NULL_TREE
;
20002 tree first_attribute
;
20004 /* Peek at the next token. */
20005 token
= cp_lexer_peek_token (parser
->lexer
);
20007 /* Check for a bitfield declaration. */
20008 if (token
->type
== CPP_COLON
20009 || (token
->type
== CPP_NAME
20010 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20016 /* Get the name of the bitfield. Note that we cannot just
20017 check TOKEN here because it may have been invalidated by
20018 the call to cp_lexer_peek_nth_token above. */
20019 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20020 identifier
= cp_parser_identifier (parser
);
20022 identifier
= NULL_TREE
;
20024 /* Consume the `:' token. */
20025 cp_lexer_consume_token (parser
->lexer
);
20026 /* Get the width of the bitfield. */
20028 = cp_parser_constant_expression (parser
,
20029 /*allow_non_constant=*/false,
20032 /* Look for attributes that apply to the bitfield. */
20033 attributes
= cp_parser_attributes_opt (parser
);
20034 /* Remember which attributes are prefix attributes and
20036 first_attribute
= attributes
;
20037 /* Combine the attributes. */
20038 attributes
= chainon (prefix_attributes
, attributes
);
20040 /* Create the bitfield declaration. */
20041 decl
= grokbitfield (identifier
20042 ? make_id_declarator (NULL_TREE
,
20052 cp_declarator
*declarator
;
20054 tree asm_specification
;
20055 int ctor_dtor_or_conv_p
;
20057 /* Parse the declarator. */
20059 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20060 &ctor_dtor_or_conv_p
,
20061 /*parenthesized_p=*/NULL
,
20062 /*member_p=*/true);
20064 /* If something went wrong parsing the declarator, make sure
20065 that we at least consume some tokens. */
20066 if (declarator
== cp_error_declarator
)
20068 /* Skip to the end of the statement. */
20069 cp_parser_skip_to_end_of_statement (parser
);
20070 /* If the next token is not a semicolon, that is
20071 probably because we just skipped over the body of
20072 a function. So, we consume a semicolon if
20073 present, but do not issue an error message if it
20075 if (cp_lexer_next_token_is (parser
->lexer
,
20077 cp_lexer_consume_token (parser
->lexer
);
20081 if (declares_class_or_enum
& 2)
20082 cp_parser_check_for_definition_in_return_type
20083 (declarator
, decl_specifiers
.type
,
20084 decl_specifiers
.locations
[ds_type_spec
]);
20086 /* Look for an asm-specification. */
20087 asm_specification
= cp_parser_asm_specification_opt (parser
);
20088 /* Look for attributes that apply to the declaration. */
20089 attributes
= cp_parser_attributes_opt (parser
);
20090 /* Remember which attributes are prefix attributes and
20092 first_attribute
= attributes
;
20093 /* Combine the attributes. */
20094 attributes
= chainon (prefix_attributes
, attributes
);
20096 /* If it's an `=', then we have a constant-initializer or a
20097 pure-specifier. It is not correct to parse the
20098 initializer before registering the member declaration
20099 since the member declaration should be in scope while
20100 its initializer is processed. However, the rest of the
20101 front end does not yet provide an interface that allows
20102 us to handle this correctly. */
20103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20107 A pure-specifier shall be used only in the declaration of
20108 a virtual function.
20110 A member-declarator can contain a constant-initializer
20111 only if it declares a static member of integral or
20114 Therefore, if the DECLARATOR is for a function, we look
20115 for a pure-specifier; otherwise, we look for a
20116 constant-initializer. When we call `grokfield', it will
20117 perform more stringent semantics checks. */
20118 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
20119 if (function_declarator_p (declarator
)
20120 || (decl_specifiers
.type
20121 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
20122 && declarator
->kind
== cdk_id
20123 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
20124 == FUNCTION_TYPE
)))
20125 initializer
= cp_parser_pure_specifier (parser
);
20126 else if (decl_specifiers
.storage_class
!= sc_static
)
20127 initializer
= cp_parser_save_nsdmi (parser
);
20128 else if (cxx_dialect
>= cxx11
)
20131 /* Don't require a constant rvalue in C++11, since we
20132 might want a reference constant. We'll enforce
20133 constancy later. */
20134 cp_lexer_consume_token (parser
->lexer
);
20135 /* Parse the initializer. */
20136 initializer
= cp_parser_initializer_clause (parser
,
20140 /* Parse the initializer. */
20141 initializer
= cp_parser_constant_initializer (parser
);
20143 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20144 && !function_declarator_p (declarator
))
20147 if (decl_specifiers
.storage_class
!= sc_static
)
20148 initializer
= cp_parser_save_nsdmi (parser
);
20150 initializer
= cp_parser_initializer (parser
, &x
, &x
);
20152 /* Otherwise, there is no initializer. */
20154 initializer
= NULL_TREE
;
20156 /* See if we are probably looking at a function
20157 definition. We are certainly not looking at a
20158 member-declarator. Calling `grokfield' has
20159 side-effects, so we must not do it unless we are sure
20160 that we are looking at a member-declarator. */
20161 if (cp_parser_token_starts_function_definition_p
20162 (cp_lexer_peek_token (parser
->lexer
)))
20164 /* The grammar does not allow a pure-specifier to be
20165 used when a member function is defined. (It is
20166 possible that this fact is an oversight in the
20167 standard, since a pure function may be defined
20168 outside of the class-specifier. */
20169 if (initializer
&& initializer_token_start
)
20170 error_at (initializer_token_start
->location
,
20171 "pure-specifier on function-definition");
20172 decl
= cp_parser_save_member_function_body (parser
,
20176 /* If the member was not a friend, declare it here. */
20179 if (parser
->fully_implicit_function_template_p
)
20180 decl
= finish_fully_implicit_template (parser
, decl
);
20181 finish_member_declaration (decl
);
20183 /* Peek at the next token. */
20184 token
= cp_lexer_peek_token (parser
->lexer
);
20185 /* If the next token is a semicolon, consume it. */
20186 if (token
->type
== CPP_SEMICOLON
)
20187 cp_lexer_consume_token (parser
->lexer
);
20191 if (declarator
->kind
== cdk_function
)
20192 declarator
->id_loc
= token
->location
;
20193 /* Create the declaration. */
20194 decl
= grokfield (declarator
, &decl_specifiers
,
20195 initializer
, /*init_const_expr_p=*/true,
20196 asm_specification
, attributes
);
20197 if (parser
->fully_implicit_function_template_p
)
20198 decl
= finish_fully_implicit_template (parser
, decl
);
20201 cp_finalize_omp_declare_simd (parser
, decl
);
20203 /* Reset PREFIX_ATTRIBUTES. */
20204 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
20205 attributes
= TREE_CHAIN (attributes
);
20207 TREE_CHAIN (attributes
) = NULL_TREE
;
20209 /* If there is any qualification still in effect, clear it
20210 now; we will be starting fresh with the next declarator. */
20211 parser
->scope
= NULL_TREE
;
20212 parser
->qualifying_scope
= NULL_TREE
;
20213 parser
->object_scope
= NULL_TREE
;
20214 /* If it's a `,', then there are more declarators. */
20215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20217 cp_lexer_consume_token (parser
->lexer
);
20218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20220 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
20221 error_at (token
->location
,
20222 "stray %<,%> at end of member declaration");
20225 /* If the next token isn't a `;', then we have a parse error. */
20226 else if (cp_lexer_next_token_is_not (parser
->lexer
,
20229 /* The next token might be a ways away from where the
20230 actual semicolon is missing. Find the previous token
20231 and use that for our error position. */
20232 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
20233 error_at (token
->location
,
20234 "expected %<;%> at end of member declaration");
20236 /* Assume that the user meant to provide a semicolon. If
20237 we were to cp_parser_skip_to_end_of_statement, we might
20238 skip to a semicolon inside a member function definition
20239 and issue nonsensical error messages. */
20240 assume_semicolon
= true;
20245 /* Add DECL to the list of members. */
20247 finish_member_declaration (decl
);
20249 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20250 cp_parser_save_default_args (parser
, decl
);
20251 else if (TREE_CODE (decl
) == FIELD_DECL
20252 && !DECL_C_BIT_FIELD (decl
)
20253 && DECL_INITIAL (decl
))
20254 /* Add DECL to the queue of NSDMI to be parsed later. */
20255 vec_safe_push (unparsed_nsdmis
, decl
);
20258 if (assume_semicolon
)
20263 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20265 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20268 /* Parse a pure-specifier.
20273 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20274 Otherwise, ERROR_MARK_NODE is returned. */
20277 cp_parser_pure_specifier (cp_parser
* parser
)
20281 /* Look for the `=' token. */
20282 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
20283 return error_mark_node
;
20284 /* Look for the `0' token. */
20285 token
= cp_lexer_peek_token (parser
->lexer
);
20287 if (token
->type
== CPP_EOF
20288 || token
->type
== CPP_PRAGMA_EOL
)
20289 return error_mark_node
;
20291 cp_lexer_consume_token (parser
->lexer
);
20293 /* Accept = default or = delete in c++0x mode. */
20294 if (token
->keyword
== RID_DEFAULT
20295 || token
->keyword
== RID_DELETE
)
20297 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
20298 return token
->u
.value
;
20301 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20302 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
20304 cp_parser_error (parser
,
20305 "invalid pure specifier (only %<= 0%> is allowed)");
20306 cp_parser_skip_to_end_of_statement (parser
);
20307 return error_mark_node
;
20309 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20311 error_at (token
->location
, "templates may not be %<virtual%>");
20312 return error_mark_node
;
20315 return integer_zero_node
;
20318 /* Parse a constant-initializer.
20320 constant-initializer:
20321 = constant-expression
20323 Returns a representation of the constant-expression. */
20326 cp_parser_constant_initializer (cp_parser
* parser
)
20328 /* Look for the `=' token. */
20329 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
20330 return error_mark_node
;
20332 /* It is invalid to write:
20334 struct S { static const int i = { 7 }; };
20337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20339 cp_parser_error (parser
,
20340 "a brace-enclosed initializer is not allowed here");
20341 /* Consume the opening brace. */
20342 cp_lexer_consume_token (parser
->lexer
);
20343 /* Skip the initializer. */
20344 cp_parser_skip_to_closing_brace (parser
);
20345 /* Look for the trailing `}'. */
20346 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20348 return error_mark_node
;
20351 return cp_parser_constant_expression (parser
,
20352 /*allow_non_constant=*/false,
20356 /* Derived classes [gram.class.derived] */
20358 /* Parse a base-clause.
20361 : base-specifier-list
20363 base-specifier-list:
20364 base-specifier ... [opt]
20365 base-specifier-list , base-specifier ... [opt]
20367 Returns a TREE_LIST representing the base-classes, in the order in
20368 which they were declared. The representation of each node is as
20369 described by cp_parser_base_specifier.
20371 In the case that no bases are specified, this function will return
20372 NULL_TREE, not ERROR_MARK_NODE. */
20375 cp_parser_base_clause (cp_parser
* parser
)
20377 tree bases
= NULL_TREE
;
20379 /* Look for the `:' that begins the list. */
20380 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20382 /* Scan the base-specifier-list. */
20387 bool pack_expansion_p
= false;
20389 /* Look for the base-specifier. */
20390 base
= cp_parser_base_specifier (parser
);
20391 /* Look for the (optional) ellipsis. */
20392 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20394 /* Consume the `...'. */
20395 cp_lexer_consume_token (parser
->lexer
);
20397 pack_expansion_p
= true;
20400 /* Add BASE to the front of the list. */
20401 if (base
&& base
!= error_mark_node
)
20403 if (pack_expansion_p
)
20404 /* Make this a pack expansion type. */
20405 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
20407 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
20409 TREE_CHAIN (base
) = bases
;
20413 /* Peek at the next token. */
20414 token
= cp_lexer_peek_token (parser
->lexer
);
20415 /* If it's not a comma, then the list is complete. */
20416 if (token
->type
!= CPP_COMMA
)
20418 /* Consume the `,'. */
20419 cp_lexer_consume_token (parser
->lexer
);
20422 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20423 base class had a qualified name. However, the next name that
20424 appears is certainly not qualified. */
20425 parser
->scope
= NULL_TREE
;
20426 parser
->qualifying_scope
= NULL_TREE
;
20427 parser
->object_scope
= NULL_TREE
;
20429 return nreverse (bases
);
20432 /* Parse a base-specifier.
20435 :: [opt] nested-name-specifier [opt] class-name
20436 virtual access-specifier [opt] :: [opt] nested-name-specifier
20438 access-specifier virtual [opt] :: [opt] nested-name-specifier
20441 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20442 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20443 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20444 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20447 cp_parser_base_specifier (cp_parser
* parser
)
20451 bool virtual_p
= false;
20452 bool duplicate_virtual_error_issued_p
= false;
20453 bool duplicate_access_error_issued_p
= false;
20454 bool class_scope_p
, template_p
;
20455 tree access
= access_default_node
;
20458 /* Process the optional `virtual' and `access-specifier'. */
20461 /* Peek at the next token. */
20462 token
= cp_lexer_peek_token (parser
->lexer
);
20463 /* Process `virtual'. */
20464 switch (token
->keyword
)
20467 /* If `virtual' appears more than once, issue an error. */
20468 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
20470 cp_parser_error (parser
,
20471 "%<virtual%> specified more than once in base-specified");
20472 duplicate_virtual_error_issued_p
= true;
20477 /* Consume the `virtual' token. */
20478 cp_lexer_consume_token (parser
->lexer
);
20483 case RID_PROTECTED
:
20485 /* If more than one access specifier appears, issue an
20487 if (access
!= access_default_node
20488 && !duplicate_access_error_issued_p
)
20490 cp_parser_error (parser
,
20491 "more than one access specifier in base-specified");
20492 duplicate_access_error_issued_p
= true;
20495 access
= ridpointers
[(int) token
->keyword
];
20497 /* Consume the access-specifier. */
20498 cp_lexer_consume_token (parser
->lexer
);
20507 /* It is not uncommon to see programs mechanically, erroneously, use
20508 the 'typename' keyword to denote (dependent) qualified types
20509 as base classes. */
20510 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
20512 token
= cp_lexer_peek_token (parser
->lexer
);
20513 if (!processing_template_decl
)
20514 error_at (token
->location
,
20515 "keyword %<typename%> not allowed outside of templates");
20517 error_at (token
->location
,
20518 "keyword %<typename%> not allowed in this context "
20519 "(the base class is implicitly a type)");
20520 cp_lexer_consume_token (parser
->lexer
);
20523 /* Look for the optional `::' operator. */
20524 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
20525 /* Look for the nested-name-specifier. The simplest way to
20530 The keyword `typename' is not permitted in a base-specifier or
20531 mem-initializer; in these contexts a qualified name that
20532 depends on a template-parameter is implicitly assumed to be a
20535 is to pretend that we have seen the `typename' keyword at this
20537 cp_parser_nested_name_specifier_opt (parser
,
20538 /*typename_keyword_p=*/true,
20539 /*check_dependency_p=*/true,
20541 /*is_declaration=*/true);
20542 /* If the base class is given by a qualified name, assume that names
20543 we see are type names or templates, as appropriate. */
20544 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
20545 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
20548 && cp_lexer_next_token_is_decltype (parser
->lexer
))
20549 /* DR 950 allows decltype as a base-specifier. */
20550 type
= cp_parser_decltype (parser
);
20553 /* Otherwise, look for the class-name. */
20554 type
= cp_parser_class_name (parser
,
20558 /*check_dependency_p=*/true,
20559 /*class_head_p=*/false,
20560 /*is_declaration=*/true);
20561 type
= TREE_TYPE (type
);
20564 if (type
== error_mark_node
)
20565 return error_mark_node
;
20567 return finish_base_specifier (type
, access
, virtual_p
);
20570 /* Exception handling [gram.exception] */
20572 /* Parse an (optional) noexcept-specification.
20574 noexcept-specification:
20575 noexcept ( constant-expression ) [opt]
20577 If no noexcept-specification is present, returns NULL_TREE.
20578 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20579 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20580 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20581 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20582 in which case a boolean condition is returned instead. */
20585 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
20586 bool require_constexpr
,
20587 bool* consumed_expr
,
20591 const char *saved_message
;
20593 /* Peek at the next token. */
20594 token
= cp_lexer_peek_token (parser
->lexer
);
20596 /* Is it a noexcept-specification? */
20597 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
20600 cp_lexer_consume_token (parser
->lexer
);
20602 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
20604 cp_lexer_consume_token (parser
->lexer
);
20606 if (require_constexpr
)
20608 /* Types may not be defined in an exception-specification. */
20609 saved_message
= parser
->type_definition_forbidden_message
;
20610 parser
->type_definition_forbidden_message
20611 = G_("types may not be defined in an exception-specification");
20613 expr
= cp_parser_constant_expression (parser
, false, NULL
);
20615 /* Restore the saved message. */
20616 parser
->type_definition_forbidden_message
= saved_message
;
20620 expr
= cp_parser_expression (parser
, false, NULL
);
20621 *consumed_expr
= true;
20624 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20628 expr
= boolean_true_node
;
20629 if (!require_constexpr
)
20630 *consumed_expr
= false;
20633 /* We cannot build a noexcept-spec right away because this will check
20634 that expr is a constexpr. */
20636 return build_noexcept_spec (expr
, tf_warning_or_error
);
20644 /* Parse an (optional) exception-specification.
20646 exception-specification:
20647 throw ( type-id-list [opt] )
20649 Returns a TREE_LIST representing the exception-specification. The
20650 TREE_VALUE of each node is a type. */
20653 cp_parser_exception_specification_opt (cp_parser
* parser
)
20657 const char *saved_message
;
20659 /* Peek at the next token. */
20660 token
= cp_lexer_peek_token (parser
->lexer
);
20662 /* Is it a noexcept-specification? */
20663 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
20665 if (type_id_list
!= NULL_TREE
)
20666 return type_id_list
;
20668 /* If it's not `throw', then there's no exception-specification. */
20669 if (!cp_parser_is_keyword (token
, RID_THROW
))
20673 /* Enable this once a lot of code has transitioned to noexcept? */
20674 if (cxx_dialect
>= cxx11
&& !in_system_header
)
20675 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
20676 "deprecated in C++0x; use %<noexcept%> instead");
20679 /* Consume the `throw'. */
20680 cp_lexer_consume_token (parser
->lexer
);
20682 /* Look for the `('. */
20683 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20685 /* Peek at the next token. */
20686 token
= cp_lexer_peek_token (parser
->lexer
);
20687 /* If it's not a `)', then there is a type-id-list. */
20688 if (token
->type
!= CPP_CLOSE_PAREN
)
20690 /* Types may not be defined in an exception-specification. */
20691 saved_message
= parser
->type_definition_forbidden_message
;
20692 parser
->type_definition_forbidden_message
20693 = G_("types may not be defined in an exception-specification");
20694 /* Parse the type-id-list. */
20695 type_id_list
= cp_parser_type_id_list (parser
);
20696 /* Restore the saved message. */
20697 parser
->type_definition_forbidden_message
= saved_message
;
20700 type_id_list
= empty_except_spec
;
20702 /* Look for the `)'. */
20703 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20705 return type_id_list
;
20708 /* Parse an (optional) type-id-list.
20712 type-id-list , type-id ... [opt]
20714 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20715 in the order that the types were presented. */
20718 cp_parser_type_id_list (cp_parser
* parser
)
20720 tree types
= NULL_TREE
;
20727 /* Get the next type-id. */
20728 type
= cp_parser_type_id (parser
);
20729 /* Parse the optional ellipsis. */
20730 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20732 /* Consume the `...'. */
20733 cp_lexer_consume_token (parser
->lexer
);
20735 /* Turn the type into a pack expansion expression. */
20736 type
= make_pack_expansion (type
);
20738 /* Add it to the list. */
20739 types
= add_exception_specifier (types
, type
, /*complain=*/1);
20740 /* Peek at the next token. */
20741 token
= cp_lexer_peek_token (parser
->lexer
);
20742 /* If it is not a `,', we are done. */
20743 if (token
->type
!= CPP_COMMA
)
20745 /* Consume the `,'. */
20746 cp_lexer_consume_token (parser
->lexer
);
20749 return nreverse (types
);
20752 /* Parse a try-block.
20755 try compound-statement handler-seq */
20758 cp_parser_try_block (cp_parser
* parser
)
20762 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
20763 try_block
= begin_try_block ();
20764 cp_parser_compound_statement (parser
, NULL
, true, false);
20765 finish_try_block (try_block
);
20766 cp_parser_handler_seq (parser
);
20767 finish_handler_sequence (try_block
);
20772 /* Parse a function-try-block.
20774 function-try-block:
20775 try ctor-initializer [opt] function-body handler-seq */
20778 cp_parser_function_try_block (cp_parser
* parser
)
20780 tree compound_stmt
;
20782 bool ctor_initializer_p
;
20784 /* Look for the `try' keyword. */
20785 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
20787 /* Let the rest of the front end know where we are. */
20788 try_block
= begin_function_try_block (&compound_stmt
);
20789 /* Parse the function-body. */
20790 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
20791 (parser
, /*in_function_try_block=*/true);
20792 /* We're done with the `try' part. */
20793 finish_function_try_block (try_block
);
20794 /* Parse the handlers. */
20795 cp_parser_handler_seq (parser
);
20796 /* We're done with the handlers. */
20797 finish_function_handler_sequence (try_block
, compound_stmt
);
20799 return ctor_initializer_p
;
20802 /* Parse a handler-seq.
20805 handler handler-seq [opt] */
20808 cp_parser_handler_seq (cp_parser
* parser
)
20814 /* Parse the handler. */
20815 cp_parser_handler (parser
);
20816 /* Peek at the next token. */
20817 token
= cp_lexer_peek_token (parser
->lexer
);
20818 /* If it's not `catch' then there are no more handlers. */
20819 if (!cp_parser_is_keyword (token
, RID_CATCH
))
20824 /* Parse a handler.
20827 catch ( exception-declaration ) compound-statement */
20830 cp_parser_handler (cp_parser
* parser
)
20835 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
20836 handler
= begin_handler ();
20837 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20838 declaration
= cp_parser_exception_declaration (parser
);
20839 finish_handler_parms (declaration
, handler
);
20840 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20841 cp_parser_compound_statement (parser
, NULL
, false, false);
20842 finish_handler (handler
);
20845 /* Parse an exception-declaration.
20847 exception-declaration:
20848 type-specifier-seq declarator
20849 type-specifier-seq abstract-declarator
20853 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20854 ellipsis variant is used. */
20857 cp_parser_exception_declaration (cp_parser
* parser
)
20859 cp_decl_specifier_seq type_specifiers
;
20860 cp_declarator
*declarator
;
20861 const char *saved_message
;
20863 /* If it's an ellipsis, it's easy to handle. */
20864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20866 /* Consume the `...' token. */
20867 cp_lexer_consume_token (parser
->lexer
);
20871 /* Types may not be defined in exception-declarations. */
20872 saved_message
= parser
->type_definition_forbidden_message
;
20873 parser
->type_definition_forbidden_message
20874 = G_("types may not be defined in exception-declarations");
20876 /* Parse the type-specifier-seq. */
20877 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
20878 /*is_trailing_return=*/false,
20880 /* If it's a `)', then there is no declarator. */
20881 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
20884 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
20885 /*ctor_dtor_or_conv_p=*/NULL
,
20886 /*parenthesized_p=*/NULL
,
20887 /*member_p=*/false);
20889 /* Restore the saved message. */
20890 parser
->type_definition_forbidden_message
= saved_message
;
20892 if (!type_specifiers
.any_specifiers_p
)
20893 return error_mark_node
;
20895 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
20898 /* Parse a throw-expression.
20901 throw assignment-expression [opt]
20903 Returns a THROW_EXPR representing the throw-expression. */
20906 cp_parser_throw_expression (cp_parser
* parser
)
20911 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
20912 token
= cp_lexer_peek_token (parser
->lexer
);
20913 /* Figure out whether or not there is an assignment-expression
20914 following the "throw" keyword. */
20915 if (token
->type
== CPP_COMMA
20916 || token
->type
== CPP_SEMICOLON
20917 || token
->type
== CPP_CLOSE_PAREN
20918 || token
->type
== CPP_CLOSE_SQUARE
20919 || token
->type
== CPP_CLOSE_BRACE
20920 || token
->type
== CPP_COLON
)
20921 expression
= NULL_TREE
;
20923 expression
= cp_parser_assignment_expression (parser
,
20924 /*cast_p=*/false, NULL
);
20926 return build_throw (expression
);
20929 /* GNU Extensions */
20931 /* Parse an (optional) asm-specification.
20934 asm ( string-literal )
20936 If the asm-specification is present, returns a STRING_CST
20937 corresponding to the string-literal. Otherwise, returns
20941 cp_parser_asm_specification_opt (cp_parser
* parser
)
20944 tree asm_specification
;
20946 /* Peek at the next token. */
20947 token
= cp_lexer_peek_token (parser
->lexer
);
20948 /* If the next token isn't the `asm' keyword, then there's no
20949 asm-specification. */
20950 if (!cp_parser_is_keyword (token
, RID_ASM
))
20953 /* Consume the `asm' token. */
20954 cp_lexer_consume_token (parser
->lexer
);
20955 /* Look for the `('. */
20956 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20958 /* Look for the string-literal. */
20959 asm_specification
= cp_parser_string_literal (parser
, false, false);
20961 /* Look for the `)'. */
20962 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20964 return asm_specification
;
20967 /* Parse an asm-operand-list.
20971 asm-operand-list , asm-operand
20974 string-literal ( expression )
20975 [ string-literal ] string-literal ( expression )
20977 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20978 each node is the expression. The TREE_PURPOSE is itself a
20979 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20980 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20981 is a STRING_CST for the string literal before the parenthesis. Returns
20982 ERROR_MARK_NODE if any of the operands are invalid. */
20985 cp_parser_asm_operand_list (cp_parser
* parser
)
20987 tree asm_operands
= NULL_TREE
;
20988 bool invalid_operands
= false;
20992 tree string_literal
;
20996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
20998 /* Consume the `[' token. */
20999 cp_lexer_consume_token (parser
->lexer
);
21000 /* Read the operand name. */
21001 name
= cp_parser_identifier (parser
);
21002 if (name
!= error_mark_node
)
21003 name
= build_string (IDENTIFIER_LENGTH (name
),
21004 IDENTIFIER_POINTER (name
));
21005 /* Look for the closing `]'. */
21006 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21010 /* Look for the string-literal. */
21011 string_literal
= cp_parser_string_literal (parser
, false, false);
21013 /* Look for the `('. */
21014 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21015 /* Parse the expression. */
21016 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
21017 /* Look for the `)'. */
21018 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21020 if (name
== error_mark_node
21021 || string_literal
== error_mark_node
21022 || expression
== error_mark_node
)
21023 invalid_operands
= true;
21025 /* Add this operand to the list. */
21026 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21029 /* If the next token is not a `,', there are no more
21031 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21033 /* Consume the `,'. */
21034 cp_lexer_consume_token (parser
->lexer
);
21037 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21040 /* Parse an asm-clobber-list.
21044 asm-clobber-list , string-literal
21046 Returns a TREE_LIST, indicating the clobbers in the order that they
21047 appeared. The TREE_VALUE of each node is a STRING_CST. */
21050 cp_parser_asm_clobber_list (cp_parser
* parser
)
21052 tree clobbers
= NULL_TREE
;
21056 tree string_literal
;
21058 /* Look for the string literal. */
21059 string_literal
= cp_parser_string_literal (parser
, false, false);
21060 /* Add it to the list. */
21061 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21062 /* If the next token is not a `,', then the list is
21064 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21066 /* Consume the `,' token. */
21067 cp_lexer_consume_token (parser
->lexer
);
21073 /* Parse an asm-label-list.
21077 asm-label-list , identifier
21079 Returns a TREE_LIST, indicating the labels in the order that they
21080 appeared. The TREE_VALUE of each node is a label. */
21083 cp_parser_asm_label_list (cp_parser
* parser
)
21085 tree labels
= NULL_TREE
;
21089 tree identifier
, label
, name
;
21091 /* Look for the identifier. */
21092 identifier
= cp_parser_identifier (parser
);
21093 if (!error_operand_p (identifier
))
21095 label
= lookup_label (identifier
);
21096 if (TREE_CODE (label
) == LABEL_DECL
)
21098 TREE_USED (label
) = 1;
21099 check_goto (label
);
21100 name
= build_string (IDENTIFIER_LENGTH (identifier
),
21101 IDENTIFIER_POINTER (identifier
));
21102 labels
= tree_cons (name
, label
, labels
);
21105 /* If the next token is not a `,', then the list is
21107 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21109 /* Consume the `,' token. */
21110 cp_lexer_consume_token (parser
->lexer
);
21113 return nreverse (labels
);
21116 /* Return TRUE iff the next tokens in the stream are possibly the
21117 beginning of a GNU extension attribute. */
21120 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
21122 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
21125 /* Return TRUE iff the next tokens in the stream are possibly the
21126 beginning of a standard C++-11 attribute specifier. */
21129 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
21131 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
21134 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21135 beginning of a standard C++-11 attribute specifier. */
21138 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
21140 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21142 return (cxx_dialect
>= cxx11
21143 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
21144 || (token
->type
== CPP_OPEN_SQUARE
21145 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
21146 && token
->type
== CPP_OPEN_SQUARE
)));
21149 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21150 beginning of a GNU extension attribute. */
21153 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
21155 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21157 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
21160 /* Return true iff the next tokens can be the beginning of either a
21161 GNU attribute list, or a standard C++11 attribute sequence. */
21164 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
21166 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
21167 || cp_next_tokens_can_be_std_attribute_p (parser
));
21170 /* Return true iff the next Nth tokens can be the beginning of either
21171 a GNU attribute list, or a standard C++11 attribute sequence. */
21174 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
21176 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
21177 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
21180 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21181 of GNU attributes, or return NULL. */
21184 cp_parser_attributes_opt (cp_parser
*parser
)
21186 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
21187 return cp_parser_gnu_attributes_opt (parser
);
21188 return cp_parser_std_attribute_spec_seq (parser
);
21191 /* Parse an (optional) series of attributes.
21194 attributes attribute
21197 __attribute__ (( attribute-list [opt] ))
21199 The return value is as for cp_parser_gnu_attribute_list. */
21202 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
21204 tree attributes
= NULL_TREE
;
21209 tree attribute_list
;
21212 /* Peek at the next token. */
21213 token
= cp_lexer_peek_token (parser
->lexer
);
21214 /* If it's not `__attribute__', then we're done. */
21215 if (token
->keyword
!= RID_ATTRIBUTE
)
21218 /* Consume the `__attribute__' keyword. */
21219 cp_lexer_consume_token (parser
->lexer
);
21220 /* Look for the two `(' tokens. */
21221 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21222 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21224 /* Peek at the next token. */
21225 token
= cp_lexer_peek_token (parser
->lexer
);
21226 if (token
->type
!= CPP_CLOSE_PAREN
)
21227 /* Parse the attribute-list. */
21228 attribute_list
= cp_parser_gnu_attribute_list (parser
);
21230 /* If the next token is a `)', then there is no attribute
21232 attribute_list
= NULL
;
21234 /* Look for the two `)' tokens. */
21235 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
21237 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
21240 cp_parser_skip_to_end_of_statement (parser
);
21242 /* Add these new attributes to the list. */
21243 attributes
= chainon (attributes
, attribute_list
);
21249 /* Parse a GNU attribute-list.
21253 attribute-list , attribute
21257 identifier ( identifier )
21258 identifier ( identifier , expression-list )
21259 identifier ( expression-list )
21261 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21262 to an attribute. The TREE_PURPOSE of each node is the identifier
21263 indicating which attribute is in use. The TREE_VALUE represents
21264 the arguments, if any. */
21267 cp_parser_gnu_attribute_list (cp_parser
* parser
)
21269 tree attribute_list
= NULL_TREE
;
21270 bool save_translate_strings_p
= parser
->translate_strings_p
;
21272 parser
->translate_strings_p
= false;
21279 /* Look for the identifier. We also allow keywords here; for
21280 example `__attribute__ ((const))' is legal. */
21281 token
= cp_lexer_peek_token (parser
->lexer
);
21282 if (token
->type
== CPP_NAME
21283 || token
->type
== CPP_KEYWORD
)
21285 tree arguments
= NULL_TREE
;
21287 /* Consume the token. */
21288 token
= cp_lexer_consume_token (parser
->lexer
);
21290 /* Save away the identifier that indicates which attribute
21292 identifier
= (token
->type
== CPP_KEYWORD
)
21293 /* For keywords, use the canonical spelling, not the
21294 parsed identifier. */
21295 ? ridpointers
[(int) token
->keyword
]
21298 attribute
= build_tree_list (identifier
, NULL_TREE
);
21300 /* Peek at the next token. */
21301 token
= cp_lexer_peek_token (parser
->lexer
);
21302 /* If it's an `(', then parse the attribute arguments. */
21303 if (token
->type
== CPP_OPEN_PAREN
)
21305 vec
<tree
, va_gc
> *vec
;
21306 int attr_flag
= (attribute_takes_identifier_p (identifier
)
21307 ? id_attr
: normal_attr
);
21308 vec
= cp_parser_parenthesized_expression_list
21309 (parser
, attr_flag
, /*cast_p=*/false,
21310 /*allow_expansion_p=*/false,
21311 /*non_constant_p=*/NULL
);
21313 arguments
= error_mark_node
;
21316 arguments
= build_tree_list_vec (vec
);
21317 release_tree_vector (vec
);
21319 /* Save the arguments away. */
21320 TREE_VALUE (attribute
) = arguments
;
21323 if (arguments
!= error_mark_node
)
21325 /* Add this attribute to the list. */
21326 TREE_CHAIN (attribute
) = attribute_list
;
21327 attribute_list
= attribute
;
21330 token
= cp_lexer_peek_token (parser
->lexer
);
21332 /* Now, look for more attributes. If the next token isn't a
21333 `,', we're done. */
21334 if (token
->type
!= CPP_COMMA
)
21337 /* Consume the comma and keep going. */
21338 cp_lexer_consume_token (parser
->lexer
);
21340 parser
->translate_strings_p
= save_translate_strings_p
;
21342 /* We built up the list in reverse order. */
21343 return nreverse (attribute_list
);
21346 /* Parse a standard C++11 attribute.
21348 The returned representation is a TREE_LIST which TREE_PURPOSE is
21349 the scoped name of the attribute, and the TREE_VALUE is its
21352 Note that the scoped name of the attribute is itself a TREE_LIST
21353 which TREE_PURPOSE is the namespace of the attribute, and
21354 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21355 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21356 and which TREE_PURPOSE is directly the attribute name.
21358 Clients of the attribute code should use get_attribute_namespace
21359 and get_attribute_name to get the actual namespace and name of
21360 attributes, regardless of their being GNU or C++11 attributes.
21363 attribute-token attribute-argument-clause [opt]
21367 attribute-scoped-token
21369 attribute-scoped-token:
21370 attribute-namespace :: identifier
21372 attribute-namespace:
21375 attribute-argument-clause:
21376 ( balanced-token-seq )
21378 balanced-token-seq:
21379 balanced-token [opt]
21380 balanced-token-seq balanced-token
21383 ( balanced-token-seq )
21384 [ balanced-token-seq ]
21385 { balanced-token-seq }. */
21388 cp_parser_std_attribute (cp_parser
*parser
)
21390 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
21393 /* First, parse name of the the attribute, a.k.a
21394 attribute-token. */
21396 token
= cp_lexer_peek_token (parser
->lexer
);
21397 if (token
->type
== CPP_NAME
)
21398 attr_id
= token
->u
.value
;
21399 else if (token
->type
== CPP_KEYWORD
)
21400 attr_id
= ridpointers
[(int) token
->keyword
];
21401 else if (token
->flags
& NAMED_OP
)
21402 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
21404 if (attr_id
== NULL_TREE
)
21407 cp_lexer_consume_token (parser
->lexer
);
21409 token
= cp_lexer_peek_token (parser
->lexer
);
21410 if (token
->type
== CPP_SCOPE
)
21412 /* We are seeing a scoped attribute token. */
21414 cp_lexer_consume_token (parser
->lexer
);
21417 token
= cp_lexer_consume_token (parser
->lexer
);
21418 if (token
->type
== CPP_NAME
)
21419 attr_id
= token
->u
.value
;
21420 else if (token
->type
== CPP_KEYWORD
)
21421 attr_id
= ridpointers
[(int) token
->keyword
];
21424 error_at (token
->location
,
21425 "expected an identifier for the attribute name");
21426 return error_mark_node
;
21428 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
21430 token
= cp_lexer_peek_token (parser
->lexer
);
21434 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
21436 /* C++11 noreturn attribute is equivalent to GNU's. */
21437 if (is_attribute_p ("noreturn", attr_id
))
21438 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21439 /* C++14 deprecated attribute is equivalent to GNU's. */
21440 else if (cxx_dialect
>= cxx1y
&& is_attribute_p ("deprecated", attr_id
))
21441 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21444 /* Now parse the optional argument clause of the attribute. */
21446 if (token
->type
!= CPP_OPEN_PAREN
)
21450 vec
<tree
, va_gc
> *vec
;
21451 int attr_flag
= normal_attr
;
21453 if (attr_ns
== get_identifier ("gnu")
21454 && attribute_takes_identifier_p (attr_id
))
21455 /* A GNU attribute that takes an identifier in parameter. */
21456 attr_flag
= id_attr
;
21458 vec
= cp_parser_parenthesized_expression_list
21459 (parser
, attr_flag
, /*cast_p=*/false,
21460 /*allow_expansion_p=*/true,
21461 /*non_constant_p=*/NULL
);
21463 arguments
= error_mark_node
;
21466 arguments
= build_tree_list_vec (vec
);
21467 release_tree_vector (vec
);
21470 if (arguments
== error_mark_node
)
21471 attribute
= error_mark_node
;
21473 TREE_VALUE (attribute
) = arguments
;
21479 /* Parse a list of standard C++-11 attributes.
21483 attribute-list , attribute[opt]
21485 attribute-list , attribute ...
21489 cp_parser_std_attribute_list (cp_parser
*parser
)
21491 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
21492 cp_token
*token
= NULL
;
21496 attribute
= cp_parser_std_attribute (parser
);
21497 if (attribute
== error_mark_node
)
21499 if (attribute
!= NULL_TREE
)
21501 TREE_CHAIN (attribute
) = attributes
;
21502 attributes
= attribute
;
21504 token
= cp_lexer_peek_token (parser
->lexer
);
21505 if (token
->type
!= CPP_COMMA
)
21507 cp_lexer_consume_token (parser
->lexer
);
21509 attributes
= nreverse (attributes
);
21513 /* Parse a standard C++-11 attribute specifier.
21515 attribute-specifier:
21516 [ [ attribute-list ] ]
21517 alignment-specifier
21519 alignment-specifier:
21520 alignas ( type-id ... [opt] )
21521 alignas ( alignment-expression ... [opt] ). */
21524 cp_parser_std_attribute_spec (cp_parser
*parser
)
21526 tree attributes
= NULL_TREE
;
21527 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21529 if (token
->type
== CPP_OPEN_SQUARE
21530 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
21532 cp_lexer_consume_token (parser
->lexer
);
21533 cp_lexer_consume_token (parser
->lexer
);
21535 attributes
= cp_parser_std_attribute_list (parser
);
21537 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
21538 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
21539 cp_parser_skip_to_end_of_statement (parser
);
21541 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21542 when we are sure that we have actually parsed them. */
21543 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21549 /* Look for an alignment-specifier. */
21551 token
= cp_lexer_peek_token (parser
->lexer
);
21553 if (token
->type
!= CPP_KEYWORD
21554 || token
->keyword
!= RID_ALIGNAS
)
21557 cp_lexer_consume_token (parser
->lexer
);
21558 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21560 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
21562 cp_parser_error (parser
, "expected %<(%>");
21563 return error_mark_node
;
21566 cp_parser_parse_tentatively (parser
);
21567 alignas_expr
= cp_parser_type_id (parser
);
21569 if (!cp_parser_parse_definitely (parser
))
21571 gcc_assert (alignas_expr
== error_mark_node
21572 || alignas_expr
== NULL_TREE
);
21575 cp_parser_assignment_expression (parser
, /*cast_p=*/false,
21576 /**cp_id_kind=*/NULL
);
21577 if (alignas_expr
== error_mark_node
)
21578 cp_parser_skip_to_end_of_statement (parser
);
21579 if (alignas_expr
== NULL_TREE
21580 || alignas_expr
== error_mark_node
)
21581 return alignas_expr
;
21584 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
21586 cp_parser_error (parser
, "expected %<)%>");
21587 return error_mark_node
;
21590 alignas_expr
= cxx_alignas_expr (alignas_expr
);
21592 /* Build the C++-11 representation of an 'aligned'
21595 build_tree_list (build_tree_list (get_identifier ("gnu"),
21596 get_identifier ("aligned")),
21597 build_tree_list (NULL_TREE
, alignas_expr
));
21603 /* Parse a standard C++-11 attribute-specifier-seq.
21605 attribute-specifier-seq:
21606 attribute-specifier-seq [opt] attribute-specifier
21610 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
21612 tree attr_specs
= NULL
;
21616 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
21617 if (attr_spec
== NULL_TREE
)
21619 if (attr_spec
== error_mark_node
)
21620 return error_mark_node
;
21622 TREE_CHAIN (attr_spec
) = attr_specs
;
21623 attr_specs
= attr_spec
;
21626 attr_specs
= nreverse (attr_specs
);
21630 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21631 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21632 current value of the PEDANTIC flag, regardless of whether or not
21633 the `__extension__' keyword is present. The caller is responsible
21634 for restoring the value of the PEDANTIC flag. */
21637 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
21639 /* Save the old value of the PEDANTIC flag. */
21640 *saved_pedantic
= pedantic
;
21642 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
21644 /* Consume the `__extension__' token. */
21645 cp_lexer_consume_token (parser
->lexer
);
21646 /* We're not being pedantic while the `__extension__' keyword is
21656 /* Parse a label declaration.
21659 __label__ label-declarator-seq ;
21661 label-declarator-seq:
21662 identifier , label-declarator-seq
21666 cp_parser_label_declaration (cp_parser
* parser
)
21668 /* Look for the `__label__' keyword. */
21669 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
21675 /* Look for an identifier. */
21676 identifier
= cp_parser_identifier (parser
);
21677 /* If we failed, stop. */
21678 if (identifier
== error_mark_node
)
21680 /* Declare it as a label. */
21681 finish_label_decl (identifier
);
21682 /* If the next token is a `;', stop. */
21683 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21685 /* Look for the `,' separating the label declarations. */
21686 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
21689 /* Look for the final `;'. */
21690 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21693 /* Support Functions */
21695 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21696 NAME should have one of the representations used for an
21697 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21698 is returned. If PARSER->SCOPE is a dependent type, then a
21699 SCOPE_REF is returned.
21701 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21702 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21703 was formed. Abstractly, such entities should not be passed to this
21704 function, because they do not need to be looked up, but it is
21705 simpler to check for this special case here, rather than at the
21708 In cases not explicitly covered above, this function returns a
21709 DECL, OVERLOAD, or baselink representing the result of the lookup.
21710 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21713 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21714 (e.g., "struct") that was used. In that case bindings that do not
21715 refer to types are ignored.
21717 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21720 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21723 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21726 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21727 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21728 NULL_TREE otherwise. */
21731 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
21732 enum tag_types tag_type
,
21735 bool check_dependency
,
21736 tree
*ambiguous_decls
,
21737 location_t name_location
)
21740 tree object_type
= parser
->context
->object_type
;
21742 /* Assume that the lookup will be unambiguous. */
21743 if (ambiguous_decls
)
21744 *ambiguous_decls
= NULL_TREE
;
21746 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21747 no longer valid. Note that if we are parsing tentatively, and
21748 the parse fails, OBJECT_TYPE will be automatically restored. */
21749 parser
->context
->object_type
= NULL_TREE
;
21751 if (name
== error_mark_node
)
21752 return error_mark_node
;
21754 /* A template-id has already been resolved; there is no lookup to
21756 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
21758 if (BASELINK_P (name
))
21760 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
21761 == TEMPLATE_ID_EXPR
);
21765 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21766 it should already have been checked to make sure that the name
21767 used matches the type being destroyed. */
21768 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
21772 /* Figure out to which type this destructor applies. */
21774 type
= parser
->scope
;
21775 else if (object_type
)
21776 type
= object_type
;
21778 type
= current_class_type
;
21779 /* If that's not a class type, there is no destructor. */
21780 if (!type
|| !CLASS_TYPE_P (type
))
21781 return error_mark_node
;
21782 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
21783 lazily_declare_fn (sfk_destructor
, type
);
21784 if (!CLASSTYPE_DESTRUCTORS (type
))
21785 return error_mark_node
;
21786 /* If it was a class type, return the destructor. */
21787 return CLASSTYPE_DESTRUCTORS (type
);
21790 /* By this point, the NAME should be an ordinary identifier. If
21791 the id-expression was a qualified name, the qualifying scope is
21792 stored in PARSER->SCOPE at this point. */
21793 gcc_assert (identifier_p (name
));
21795 /* Perform the lookup. */
21800 if (parser
->scope
== error_mark_node
)
21801 return error_mark_node
;
21803 /* If the SCOPE is dependent, the lookup must be deferred until
21804 the template is instantiated -- unless we are explicitly
21805 looking up names in uninstantiated templates. Even then, we
21806 cannot look up the name if the scope is not a class type; it
21807 might, for example, be a template type parameter. */
21808 dependent_p
= (TYPE_P (parser
->scope
)
21809 && dependent_scope_p (parser
->scope
));
21810 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
21812 /* Defer lookup. */
21813 decl
= error_mark_node
;
21816 tree pushed_scope
= NULL_TREE
;
21818 /* If PARSER->SCOPE is a dependent type, then it must be a
21819 class type, and we must not be checking dependencies;
21820 otherwise, we would have processed this lookup above. So
21821 that PARSER->SCOPE is not considered a dependent base by
21822 lookup_member, we must enter the scope here. */
21824 pushed_scope
= push_scope (parser
->scope
);
21826 /* If the PARSER->SCOPE is a template specialization, it
21827 may be instantiated during name lookup. In that case,
21828 errors may be issued. Even if we rollback the current
21829 tentative parse, those errors are valid. */
21830 decl
= lookup_qualified_name (parser
->scope
, name
,
21831 tag_type
!= none_type
,
21832 /*complain=*/true);
21834 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21835 lookup result and the nested-name-specifier nominates a class C:
21836 * if the name specified after the nested-name-specifier, when
21837 looked up in C, is the injected-class-name of C (Clause 9), or
21838 * if the name specified after the nested-name-specifier is the
21839 same as the identifier or the simple-template-id's template-
21840 name in the last component of the nested-name-specifier,
21841 the name is instead considered to name the constructor of
21842 class C. [ Note: for example, the constructor is not an
21843 acceptable lookup result in an elaborated-type-specifier so
21844 the constructor would not be used in place of the
21845 injected-class-name. --end note ] Such a constructor name
21846 shall be used only in the declarator-id of a declaration that
21847 names a constructor or in a using-declaration. */
21848 if (tag_type
== none_type
21849 && DECL_SELF_REFERENCE_P (decl
)
21850 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
21851 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
21852 tag_type
!= none_type
,
21853 /*complain=*/true);
21855 /* If we have a single function from a using decl, pull it out. */
21856 if (TREE_CODE (decl
) == OVERLOAD
21857 && !really_overloaded_fn (decl
))
21858 decl
= OVL_FUNCTION (decl
);
21861 pop_scope (pushed_scope
);
21864 /* If the scope is a dependent type and either we deferred lookup or
21865 we did lookup but didn't find the name, rememeber the name. */
21866 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
21867 && dependent_type_p (parser
->scope
))
21873 /* The resolution to Core Issue 180 says that `struct
21874 A::B' should be considered a type-name, even if `A'
21876 type
= make_typename_type (parser
->scope
, name
, tag_type
,
21877 /*complain=*/tf_error
);
21878 if (type
!= error_mark_node
)
21879 decl
= TYPE_NAME (type
);
21881 else if (is_template
21882 && (cp_parser_next_token_ends_template_argument_p (parser
)
21883 || cp_lexer_next_token_is (parser
->lexer
,
21885 decl
= make_unbound_class_template (parser
->scope
,
21887 /*complain=*/tf_error
);
21889 decl
= build_qualified_name (/*type=*/NULL_TREE
,
21890 parser
->scope
, name
,
21893 parser
->qualifying_scope
= parser
->scope
;
21894 parser
->object_scope
= NULL_TREE
;
21896 else if (object_type
)
21898 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21899 OBJECT_TYPE is not a class. */
21900 if (CLASS_TYPE_P (object_type
))
21901 /* If the OBJECT_TYPE is a template specialization, it may
21902 be instantiated during name lookup. In that case, errors
21903 may be issued. Even if we rollback the current tentative
21904 parse, those errors are valid. */
21905 decl
= lookup_member (object_type
,
21908 tag_type
!= none_type
,
21909 tf_warning_or_error
);
21914 /* Look it up in the enclosing context. */
21915 decl
= lookup_name_real (name
, tag_type
!= none_type
,
21917 /*block_p=*/true, is_namespace
, 0);
21918 parser
->object_scope
= object_type
;
21919 parser
->qualifying_scope
= NULL_TREE
;
21923 decl
= lookup_name_real (name
, tag_type
!= none_type
,
21925 /*block_p=*/true, is_namespace
, 0);
21926 parser
->qualifying_scope
= NULL_TREE
;
21927 parser
->object_scope
= NULL_TREE
;
21930 /* If the lookup failed, let our caller know. */
21931 if (!decl
|| decl
== error_mark_node
)
21932 return error_mark_node
;
21934 /* Pull out the template from an injected-class-name (or multiple). */
21936 decl
= maybe_get_template_decl_from_type_decl (decl
);
21938 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21939 if (TREE_CODE (decl
) == TREE_LIST
)
21941 if (ambiguous_decls
)
21942 *ambiguous_decls
= decl
;
21943 /* The error message we have to print is too complicated for
21944 cp_parser_error, so we incorporate its actions directly. */
21945 if (!cp_parser_simulate_error (parser
))
21947 error_at (name_location
, "reference to %qD is ambiguous",
21949 print_candidates (decl
);
21951 return error_mark_node
;
21954 gcc_assert (DECL_P (decl
)
21955 || TREE_CODE (decl
) == OVERLOAD
21956 || TREE_CODE (decl
) == SCOPE_REF
21957 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
21958 || BASELINK_P (decl
));
21960 /* If we have resolved the name of a member declaration, check to
21961 see if the declaration is accessible. When the name resolves to
21962 set of overloaded functions, accessibility is checked when
21963 overload resolution is done.
21965 During an explicit instantiation, access is not checked at all,
21966 as per [temp.explicit]. */
21968 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
21970 maybe_record_typedef_use (decl
);
21975 /* Like cp_parser_lookup_name, but for use in the typical case where
21976 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21977 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21980 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
21982 return cp_parser_lookup_name (parser
, name
,
21984 /*is_template=*/false,
21985 /*is_namespace=*/false,
21986 /*check_dependency=*/true,
21987 /*ambiguous_decls=*/NULL
,
21991 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21992 the current context, return the TYPE_DECL. If TAG_NAME_P is
21993 true, the DECL indicates the class being defined in a class-head,
21994 or declared in an elaborated-type-specifier.
21996 Otherwise, return DECL. */
21999 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22001 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22002 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22005 template <typename T> struct B;
22008 template <typename T> struct A::B {};
22010 Similarly, in an elaborated-type-specifier:
22012 namespace N { struct X{}; }
22015 template <typename T> friend struct N::X;
22018 However, if the DECL refers to a class type, and we are in
22019 the scope of the class, then the name lookup automatically
22020 finds the TYPE_DECL created by build_self_reference rather
22021 than a TEMPLATE_DECL. For example, in:
22023 template <class T> struct S {
22027 there is no need to handle such case. */
22029 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
22030 return DECL_TEMPLATE_RESULT (decl
);
22035 /* If too many, or too few, template-parameter lists apply to the
22036 declarator, issue an error message. Returns TRUE if all went well,
22037 and FALSE otherwise. */
22040 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
22041 cp_declarator
*declarator
,
22042 location_t declarator_location
)
22044 switch (declarator
->kind
)
22048 unsigned num_templates
= 0;
22049 tree scope
= declarator
->u
.id
.qualifying_scope
;
22052 num_templates
= num_template_headers_for_class (scope
);
22053 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
22054 == TEMPLATE_ID_EXPR
)
22055 /* If the DECLARATOR has the form `X<y>' then it uses one
22056 additional level of template parameters. */
22059 return cp_parser_check_template_parameters
22060 (parser
, num_templates
, declarator_location
, declarator
);
22066 case cdk_reference
:
22068 return (cp_parser_check_declarator_template_parameters
22069 (parser
, declarator
->declarator
, declarator_location
));
22075 gcc_unreachable ();
22080 /* NUM_TEMPLATES were used in the current declaration. If that is
22081 invalid, return FALSE and issue an error messages. Otherwise,
22082 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22083 declarator and we can print more accurate diagnostics. */
22086 cp_parser_check_template_parameters (cp_parser
* parser
,
22087 unsigned num_templates
,
22088 location_t location
,
22089 cp_declarator
*declarator
)
22091 /* If there are the same number of template classes and parameter
22092 lists, that's OK. */
22093 if (parser
->num_template_parameter_lists
== num_templates
)
22095 /* If there are more, but only one more, then we are referring to a
22096 member template. That's OK too. */
22097 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
22099 /* If there are more template classes than parameter lists, we have
22102 template <class T> void S<T>::R<T>::f (); */
22103 if (parser
->num_template_parameter_lists
< num_templates
)
22105 if (declarator
&& !current_function_decl
)
22106 error_at (location
, "specializing member %<%T::%E%> "
22107 "requires %<template<>%> syntax",
22108 declarator
->u
.id
.qualifying_scope
,
22109 declarator
->u
.id
.unqualified_name
);
22110 else if (declarator
)
22111 error_at (location
, "invalid declaration of %<%T::%E%>",
22112 declarator
->u
.id
.qualifying_scope
,
22113 declarator
->u
.id
.unqualified_name
);
22115 error_at (location
, "too few template-parameter-lists");
22118 /* Otherwise, there are too many template parameter lists. We have
22121 template <class T> template <class U> void S::f(); */
22122 error_at (location
, "too many template-parameter-lists");
22126 /* Parse an optional `::' token indicating that the following name is
22127 from the global namespace. If so, PARSER->SCOPE is set to the
22128 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22129 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22130 Returns the new value of PARSER->SCOPE, if the `::' token is
22131 present, and NULL_TREE otherwise. */
22134 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
22138 /* Peek at the next token. */
22139 token
= cp_lexer_peek_token (parser
->lexer
);
22140 /* If we're looking at a `::' token then we're starting from the
22141 global namespace, not our current location. */
22142 if (token
->type
== CPP_SCOPE
)
22144 /* Consume the `::' token. */
22145 cp_lexer_consume_token (parser
->lexer
);
22146 /* Set the SCOPE so that we know where to start the lookup. */
22147 parser
->scope
= global_namespace
;
22148 parser
->qualifying_scope
= global_namespace
;
22149 parser
->object_scope
= NULL_TREE
;
22151 return parser
->scope
;
22153 else if (!current_scope_valid_p
)
22155 parser
->scope
= NULL_TREE
;
22156 parser
->qualifying_scope
= NULL_TREE
;
22157 parser
->object_scope
= NULL_TREE
;
22163 /* Returns TRUE if the upcoming token sequence is the start of a
22164 constructor declarator. If FRIEND_P is true, the declarator is
22165 preceded by the `friend' specifier. */
22168 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
22170 bool constructor_p
;
22171 tree nested_name_specifier
;
22172 cp_token
*next_token
;
22174 /* The common case is that this is not a constructor declarator, so
22175 try to avoid doing lots of work if at all possible. It's not
22176 valid declare a constructor at function scope. */
22177 if (parser
->in_function_body
)
22179 /* And only certain tokens can begin a constructor declarator. */
22180 next_token
= cp_lexer_peek_token (parser
->lexer
);
22181 if (next_token
->type
!= CPP_NAME
22182 && next_token
->type
!= CPP_SCOPE
22183 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
22184 && next_token
->type
!= CPP_TEMPLATE_ID
)
22187 /* Parse tentatively; we are going to roll back all of the tokens
22189 cp_parser_parse_tentatively (parser
);
22190 /* Assume that we are looking at a constructor declarator. */
22191 constructor_p
= true;
22193 /* Look for the optional `::' operator. */
22194 cp_parser_global_scope_opt (parser
,
22195 /*current_scope_valid_p=*/false);
22196 /* Look for the nested-name-specifier. */
22197 nested_name_specifier
22198 = (cp_parser_nested_name_specifier_opt (parser
,
22199 /*typename_keyword_p=*/false,
22200 /*check_dependency_p=*/false,
22202 /*is_declaration=*/false));
22203 /* Outside of a class-specifier, there must be a
22204 nested-name-specifier. */
22205 if (!nested_name_specifier
&&
22206 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
22208 constructor_p
= false;
22209 else if (nested_name_specifier
== error_mark_node
)
22210 constructor_p
= false;
22212 /* If we have a class scope, this is easy; DR 147 says that S::S always
22213 names the constructor, and no other qualified name could. */
22214 if (constructor_p
&& nested_name_specifier
22215 && CLASS_TYPE_P (nested_name_specifier
))
22217 tree id
= cp_parser_unqualified_id (parser
,
22218 /*template_keyword_p=*/false,
22219 /*check_dependency_p=*/false,
22220 /*declarator_p=*/true,
22221 /*optional_p=*/false);
22222 if (is_overloaded_fn (id
))
22223 id
= DECL_NAME (get_first_fn (id
));
22224 if (!constructor_name_p (id
, nested_name_specifier
))
22225 constructor_p
= false;
22227 /* If we still think that this might be a constructor-declarator,
22228 look for a class-name. */
22229 else if (constructor_p
)
22233 template <typename T> struct S {
22237 we must recognize that the nested `S' names a class. */
22239 type_decl
= cp_parser_class_name (parser
,
22240 /*typename_keyword_p=*/false,
22241 /*template_keyword_p=*/false,
22243 /*check_dependency_p=*/false,
22244 /*class_head_p=*/false,
22245 /*is_declaration=*/false);
22246 /* If there was no class-name, then this is not a constructor. */
22247 constructor_p
= !cp_parser_error_occurred (parser
);
22249 /* If we're still considering a constructor, we have to see a `(',
22250 to begin the parameter-declaration-clause, followed by either a
22251 `)', an `...', or a decl-specifier. We need to check for a
22252 type-specifier to avoid being fooled into thinking that:
22256 is a constructor. (It is actually a function named `f' that
22257 takes one parameter (of type `int') and returns a value of type
22260 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22261 constructor_p
= false;
22264 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
22265 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
22266 /* A parameter declaration begins with a decl-specifier,
22267 which is either the "attribute" keyword, a storage class
22268 specifier, or (usually) a type-specifier. */
22269 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
22272 tree pushed_scope
= NULL_TREE
;
22273 unsigned saved_num_template_parameter_lists
;
22275 /* Names appearing in the type-specifier should be looked up
22276 in the scope of the class. */
22277 if (current_class_type
)
22281 type
= TREE_TYPE (type_decl
);
22282 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22284 type
= resolve_typename_type (type
,
22285 /*only_current_p=*/false);
22286 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22288 cp_parser_abort_tentative_parse (parser
);
22292 pushed_scope
= push_scope (type
);
22295 /* Inside the constructor parameter list, surrounding
22296 template-parameter-lists do not apply. */
22297 saved_num_template_parameter_lists
22298 = parser
->num_template_parameter_lists
;
22299 parser
->num_template_parameter_lists
= 0;
22301 /* Look for the type-specifier. */
22302 cp_parser_type_specifier (parser
,
22303 CP_PARSER_FLAGS_NONE
,
22304 /*decl_specs=*/NULL
,
22305 /*is_declarator=*/true,
22306 /*declares_class_or_enum=*/NULL
,
22307 /*is_cv_qualifier=*/NULL
);
22309 parser
->num_template_parameter_lists
22310 = saved_num_template_parameter_lists
;
22312 /* Leave the scope of the class. */
22314 pop_scope (pushed_scope
);
22316 constructor_p
= !cp_parser_error_occurred (parser
);
22320 /* We did not really want to consume any tokens. */
22321 cp_parser_abort_tentative_parse (parser
);
22323 return constructor_p
;
22326 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22327 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22328 they must be performed once we are in the scope of the function.
22330 Returns the function defined. */
22333 cp_parser_function_definition_from_specifiers_and_declarator
22334 (cp_parser
* parser
,
22335 cp_decl_specifier_seq
*decl_specifiers
,
22337 const cp_declarator
*declarator
)
22342 /* Begin the function-definition. */
22343 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
22345 /* The things we're about to see are not directly qualified by any
22346 template headers we've seen thus far. */
22347 reset_specialization ();
22349 /* If there were names looked up in the decl-specifier-seq that we
22350 did not check, check them now. We must wait until we are in the
22351 scope of the function to perform the checks, since the function
22352 might be a friend. */
22353 perform_deferred_access_checks (tf_warning_or_error
);
22357 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
22358 parser
->omp_declare_simd
= NULL
;
22363 /* Skip the entire function. */
22364 cp_parser_skip_to_end_of_block_or_statement (parser
);
22365 fn
= error_mark_node
;
22367 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
22369 /* Seen already, skip it. An error message has already been output. */
22370 cp_parser_skip_to_end_of_block_or_statement (parser
);
22371 fn
= current_function_decl
;
22372 current_function_decl
= NULL_TREE
;
22373 /* If this is a function from a class, pop the nested class. */
22374 if (current_class_name
)
22375 pop_nested_class ();
22380 if (DECL_DECLARED_INLINE_P (current_function_decl
))
22381 tv
= TV_PARSE_INLINE
;
22383 tv
= TV_PARSE_FUNC
;
22385 fn
= cp_parser_function_definition_after_declarator (parser
,
22386 /*inline_p=*/false);
22393 /* Parse the part of a function-definition that follows the
22394 declarator. INLINE_P is TRUE iff this function is an inline
22395 function defined within a class-specifier.
22397 Returns the function defined. */
22400 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
22404 bool ctor_initializer_p
= false;
22405 bool saved_in_unbraced_linkage_specification_p
;
22406 bool saved_in_function_body
;
22407 unsigned saved_num_template_parameter_lists
;
22410 saved_in_function_body
= parser
->in_function_body
;
22411 parser
->in_function_body
= true;
22412 /* If the next token is `return', then the code may be trying to
22413 make use of the "named return value" extension that G++ used to
22415 token
= cp_lexer_peek_token (parser
->lexer
);
22416 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
22418 /* Consume the `return' keyword. */
22419 cp_lexer_consume_token (parser
->lexer
);
22420 /* Look for the identifier that indicates what value is to be
22422 cp_parser_identifier (parser
);
22423 /* Issue an error message. */
22424 error_at (token
->location
,
22425 "named return values are no longer supported");
22426 /* Skip tokens until we reach the start of the function body. */
22429 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22430 if (token
->type
== CPP_OPEN_BRACE
22431 || token
->type
== CPP_EOF
22432 || token
->type
== CPP_PRAGMA_EOL
)
22434 cp_lexer_consume_token (parser
->lexer
);
22437 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22438 anything declared inside `f'. */
22439 saved_in_unbraced_linkage_specification_p
22440 = parser
->in_unbraced_linkage_specification_p
;
22441 parser
->in_unbraced_linkage_specification_p
= false;
22442 /* Inside the function, surrounding template-parameter-lists do not
22444 saved_num_template_parameter_lists
22445 = parser
->num_template_parameter_lists
;
22446 parser
->num_template_parameter_lists
= 0;
22448 start_lambda_scope (current_function_decl
);
22450 /* If the next token is `try', `__transaction_atomic', or
22451 `__transaction_relaxed`, then we are looking at either function-try-block
22452 or function-transaction-block. Note that all of these include the
22454 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
22455 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22456 RID_TRANSACTION_ATOMIC
);
22457 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
22458 RID_TRANSACTION_RELAXED
))
22459 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22460 RID_TRANSACTION_RELAXED
);
22461 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
22462 ctor_initializer_p
= cp_parser_function_try_block (parser
);
22464 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
22465 (parser
, /*in_function_try_block=*/false);
22467 finish_lambda_scope ();
22469 /* Finish the function. */
22470 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
22471 (inline_p
? 2 : 0));
22472 /* Generate code for it, if necessary. */
22473 expand_or_defer_fn (fn
);
22474 /* Restore the saved values. */
22475 parser
->in_unbraced_linkage_specification_p
22476 = saved_in_unbraced_linkage_specification_p
;
22477 parser
->num_template_parameter_lists
22478 = saved_num_template_parameter_lists
;
22479 parser
->in_function_body
= saved_in_function_body
;
22481 if (parser
->fully_implicit_function_template_p
)
22482 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
22487 /* Parse a template-declaration, assuming that the `export' (and
22488 `extern') keywords, if present, has already been scanned. MEMBER_P
22489 is as for cp_parser_template_declaration. */
22492 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
22494 tree decl
= NULL_TREE
;
22495 vec
<deferred_access_check
, va_gc
> *checks
;
22496 tree parameter_list
;
22497 bool friend_p
= false;
22498 bool need_lang_pop
;
22501 /* Look for the `template' keyword. */
22502 token
= cp_lexer_peek_token (parser
->lexer
);
22503 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
22507 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
22509 if (at_class_scope_p () && current_function_decl
)
22511 /* 14.5.2.2 [temp.mem]
22513 A local class shall not have member templates. */
22514 error_at (token
->location
,
22515 "invalid declaration of member template in local class");
22516 cp_parser_skip_to_end_of_block_or_statement (parser
);
22521 A template ... shall not have C linkage. */
22522 if (current_lang_name
== lang_name_c
)
22524 error_at (token
->location
, "template with C linkage");
22525 /* Give it C++ linkage to avoid confusing other parts of the
22527 push_lang_context (lang_name_cplusplus
);
22528 need_lang_pop
= true;
22531 need_lang_pop
= false;
22533 /* We cannot perform access checks on the template parameter
22534 declarations until we know what is being declared, just as we
22535 cannot check the decl-specifier list. */
22536 push_deferring_access_checks (dk_deferred
);
22538 /* If the next token is `>', then we have an invalid
22539 specialization. Rather than complain about an invalid template
22540 parameter, issue an error message here. */
22541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
22543 cp_parser_error (parser
, "invalid explicit specialization");
22544 begin_specialization ();
22545 parameter_list
= NULL_TREE
;
22549 /* Parse the template parameters. */
22550 parameter_list
= cp_parser_template_parameter_list (parser
);
22553 /* Get the deferred access checks from the parameter list. These
22554 will be checked once we know what is being declared, as for a
22555 member template the checks must be performed in the scope of the
22556 class containing the member. */
22557 checks
= get_deferred_access_checks ();
22559 /* Look for the `>'. */
22560 cp_parser_skip_to_end_of_template_parameter_list (parser
);
22561 /* We just processed one more parameter list. */
22562 ++parser
->num_template_parameter_lists
;
22563 /* If the next token is `template', there are more template
22565 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
22567 cp_parser_template_declaration_after_export (parser
, member_p
);
22568 else if (cxx_dialect
>= cxx11
22569 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
22570 decl
= cp_parser_alias_declaration (parser
);
22573 /* There are no access checks when parsing a template, as we do not
22574 know if a specialization will be a friend. */
22575 push_deferring_access_checks (dk_no_check
);
22576 token
= cp_lexer_peek_token (parser
->lexer
);
22577 decl
= cp_parser_single_declaration (parser
,
22580 /*explicit_specialization_p=*/false,
22582 pop_deferring_access_checks ();
22584 /* If this is a member template declaration, let the front
22586 if (member_p
&& !friend_p
&& decl
)
22588 if (TREE_CODE (decl
) == TYPE_DECL
)
22589 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
22591 decl
= finish_member_template_decl (decl
);
22593 else if (friend_p
&& decl
22594 && DECL_DECLARES_TYPE_P (decl
))
22595 make_friend_class (current_class_type
, TREE_TYPE (decl
),
22596 /*complain=*/true);
22598 /* We are done with the current parameter list. */
22599 --parser
->num_template_parameter_lists
;
22601 pop_deferring_access_checks ();
22604 finish_template_decl (parameter_list
);
22606 /* Check the template arguments for a literal operator template. */
22608 && DECL_DECLARES_FUNCTION_P (decl
)
22609 && UDLIT_OPER_P (DECL_NAME (decl
)))
22612 if (parameter_list
== NULL_TREE
)
22616 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
22617 if (num_parms
== 1)
22619 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
22620 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
22621 if (TREE_TYPE (parm
) != char_type_node
22622 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
22625 else if (num_parms
== 2 && cxx_dialect
>= cxx1y
)
22627 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
22628 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
22629 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
22630 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
22631 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
22632 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
22639 error ("literal operator template %qD has invalid parameter list."
22640 " Expected non-type template argument pack <char...>"
22641 " or <typename CharT, CharT...>",
22644 /* Register member declarations. */
22645 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
22646 finish_member_declaration (decl
);
22647 /* For the erroneous case of a template with C linkage, we pushed an
22648 implicit C++ linkage scope; exit that scope now. */
22650 pop_lang_context ();
22651 /* If DECL is a function template, we must return to parse it later.
22652 (Even though there is no definition, there might be default
22653 arguments that need handling.) */
22654 if (member_p
&& decl
22655 && DECL_DECLARES_FUNCTION_P (decl
))
22656 vec_safe_push (unparsed_funs_with_definitions
, decl
);
22659 /* Perform the deferred access checks from a template-parameter-list.
22660 CHECKS is a TREE_LIST of access checks, as returned by
22661 get_deferred_access_checks. */
22664 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
22666 ++processing_template_parmlist
;
22667 perform_access_checks (checks
, tf_warning_or_error
);
22668 --processing_template_parmlist
;
22671 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22672 `function-definition' sequence that follows a template header.
22673 If MEMBER_P is true, this declaration appears in a class scope.
22675 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22676 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22679 cp_parser_single_declaration (cp_parser
* parser
,
22680 vec
<deferred_access_check
, va_gc
> *checks
,
22682 bool explicit_specialization_p
,
22685 int declares_class_or_enum
;
22686 tree decl
= NULL_TREE
;
22687 cp_decl_specifier_seq decl_specifiers
;
22688 bool function_definition_p
= false;
22689 cp_token
*decl_spec_token_start
;
22691 /* This function is only used when processing a template
22693 gcc_assert (innermost_scope_kind () == sk_template_parms
22694 || innermost_scope_kind () == sk_template_spec
);
22696 /* Defer access checks until we know what is being declared. */
22697 push_deferring_access_checks (dk_deferred
);
22699 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22701 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22702 cp_parser_decl_specifier_seq (parser
,
22703 CP_PARSER_FLAGS_OPTIONAL
,
22705 &declares_class_or_enum
);
22707 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
22709 /* There are no template typedefs. */
22710 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
22712 error_at (decl_spec_token_start
->location
,
22713 "template declaration of %<typedef%>");
22714 decl
= error_mark_node
;
22717 /* Gather up the access checks that occurred the
22718 decl-specifier-seq. */
22719 stop_deferring_access_checks ();
22721 /* Check for the declaration of a template class. */
22722 if (declares_class_or_enum
)
22724 if (cp_parser_declares_only_class_p (parser
))
22726 decl
= shadow_tag (&decl_specifiers
);
22731 friend template <typename T> struct A<T>::B;
22734 A<T>::B will be represented by a TYPENAME_TYPE, and
22735 therefore not recognized by shadow_tag. */
22736 if (friend_p
&& *friend_p
22738 && decl_specifiers
.type
22739 && TYPE_P (decl_specifiers
.type
))
22740 decl
= decl_specifiers
.type
;
22742 if (decl
&& decl
!= error_mark_node
)
22743 decl
= TYPE_NAME (decl
);
22745 decl
= error_mark_node
;
22747 /* Perform access checks for template parameters. */
22748 cp_parser_perform_template_parameter_access_checks (checks
);
22752 /* Complain about missing 'typename' or other invalid type names. */
22753 if (!decl_specifiers
.any_type_specifiers_p
22754 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22756 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22757 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22758 the rest of this declaration. */
22759 decl
= error_mark_node
;
22763 /* If it's not a template class, try for a template function. If
22764 the next token is a `;', then this declaration does not declare
22765 anything. But, if there were errors in the decl-specifiers, then
22766 the error might well have come from an attempted class-specifier.
22767 In that case, there's no need to warn about a missing declarator. */
22769 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
22770 || decl_specifiers
.type
!= error_mark_node
))
22772 decl
= cp_parser_init_declarator (parser
,
22775 /*function_definition_allowed_p=*/true,
22777 declares_class_or_enum
,
22778 &function_definition_p
,
22781 /* 7.1.1-1 [dcl.stc]
22783 A storage-class-specifier shall not be specified in an explicit
22784 specialization... */
22786 && explicit_specialization_p
22787 && decl_specifiers
.storage_class
!= sc_none
)
22789 error_at (decl_spec_token_start
->location
,
22790 "explicit template specialization cannot have a storage class");
22791 decl
= error_mark_node
;
22794 if (decl
&& VAR_P (decl
))
22795 check_template_variable (decl
);
22798 /* Look for a trailing `;' after the declaration. */
22799 if (!function_definition_p
22800 && (decl
== error_mark_node
22801 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
22802 cp_parser_skip_to_end_of_block_or_statement (parser
);
22805 pop_deferring_access_checks ();
22807 /* Clear any current qualification; whatever comes next is the start
22808 of something new. */
22809 parser
->scope
= NULL_TREE
;
22810 parser
->qualifying_scope
= NULL_TREE
;
22811 parser
->object_scope
= NULL_TREE
;
22816 /* Parse a cast-expression that is not the operand of a unary "&". */
22819 cp_parser_simple_cast_expression (cp_parser
*parser
)
22821 return cp_parser_cast_expression (parser
, /*address_p=*/false,
22822 /*cast_p=*/false, /*decltype*/false, NULL
);
22825 /* Parse a functional cast to TYPE. Returns an expression
22826 representing the cast. */
22829 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
22831 vec
<tree
, va_gc
> *vec
;
22832 tree expression_list
;
22836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22838 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22839 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
22840 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
22841 if (TREE_CODE (type
) == TYPE_DECL
)
22842 type
= TREE_TYPE (type
);
22843 return finish_compound_literal (type
, expression_list
,
22844 tf_warning_or_error
);
22848 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22850 /*allow_expansion_p=*/true,
22851 /*non_constant_p=*/NULL
);
22853 expression_list
= error_mark_node
;
22856 expression_list
= build_tree_list_vec (vec
);
22857 release_tree_vector (vec
);
22860 cast
= build_functional_cast (type
, expression_list
,
22861 tf_warning_or_error
);
22862 /* [expr.const]/1: In an integral constant expression "only type
22863 conversions to integral or enumeration type can be used". */
22864 if (TREE_CODE (type
) == TYPE_DECL
)
22865 type
= TREE_TYPE (type
);
22866 if (cast
!= error_mark_node
22867 && !cast_valid_in_integral_constant_expression_p (type
)
22868 && cp_parser_non_integral_constant_expression (parser
,
22870 return error_mark_node
;
22874 /* Save the tokens that make up the body of a member function defined
22875 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22876 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22877 specifiers applied to the declaration. Returns the FUNCTION_DECL
22878 for the member function. */
22881 cp_parser_save_member_function_body (cp_parser
* parser
,
22882 cp_decl_specifier_seq
*decl_specifiers
,
22883 cp_declarator
*declarator
,
22890 /* Create the FUNCTION_DECL. */
22891 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
22892 cp_finalize_omp_declare_simd (parser
, fn
);
22893 /* If something went badly wrong, bail out now. */
22894 if (fn
== error_mark_node
)
22896 /* If there's a function-body, skip it. */
22897 if (cp_parser_token_starts_function_definition_p
22898 (cp_lexer_peek_token (parser
->lexer
)))
22899 cp_parser_skip_to_end_of_block_or_statement (parser
);
22900 return error_mark_node
;
22903 /* Remember it, if there default args to post process. */
22904 cp_parser_save_default_args (parser
, fn
);
22906 /* Save away the tokens that make up the body of the
22908 first
= parser
->lexer
->next_token
;
22909 /* Handle function try blocks. */
22910 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
22911 cp_lexer_consume_token (parser
->lexer
);
22912 /* We can have braced-init-list mem-initializers before the fn body. */
22913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22915 cp_lexer_consume_token (parser
->lexer
);
22916 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22918 /* cache_group will stop after an un-nested { } pair, too. */
22919 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
22922 /* variadic mem-inits have ... after the ')'. */
22923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22924 cp_lexer_consume_token (parser
->lexer
);
22927 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
22928 /* Handle function try blocks. */
22929 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
22930 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
22931 last
= parser
->lexer
->next_token
;
22933 /* Save away the inline definition; we will process it when the
22934 class is complete. */
22935 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
22936 DECL_PENDING_INLINE_P (fn
) = 1;
22938 /* We need to know that this was defined in the class, so that
22939 friend templates are handled correctly. */
22940 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
22942 /* Add FN to the queue of functions to be parsed later. */
22943 vec_safe_push (unparsed_funs_with_definitions
, fn
);
22948 /* Save the tokens that make up the in-class initializer for a non-static
22949 data member. Returns a DEFAULT_ARG. */
22952 cp_parser_save_nsdmi (cp_parser
* parser
)
22954 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
22957 /* Parse a template-argument-list, as well as the trailing ">" (but
22958 not the opening "<"). See cp_parser_template_argument_list for the
22962 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
22966 tree saved_qualifying_scope
;
22967 tree saved_object_scope
;
22968 bool saved_greater_than_is_operator_p
;
22969 int saved_unevaluated_operand
;
22970 int saved_inhibit_evaluation_warnings
;
22974 When parsing a template-id, the first non-nested `>' is taken as
22975 the end of the template-argument-list rather than a greater-than
22977 saved_greater_than_is_operator_p
22978 = parser
->greater_than_is_operator_p
;
22979 parser
->greater_than_is_operator_p
= false;
22980 /* Parsing the argument list may modify SCOPE, so we save it
22982 saved_scope
= parser
->scope
;
22983 saved_qualifying_scope
= parser
->qualifying_scope
;
22984 saved_object_scope
= parser
->object_scope
;
22985 /* We need to evaluate the template arguments, even though this
22986 template-id may be nested within a "sizeof". */
22987 saved_unevaluated_operand
= cp_unevaluated_operand
;
22988 cp_unevaluated_operand
= 0;
22989 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22990 c_inhibit_evaluation_warnings
= 0;
22991 /* Parse the template-argument-list itself. */
22992 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
22993 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
22994 arguments
= NULL_TREE
;
22996 arguments
= cp_parser_template_argument_list (parser
);
22997 /* Look for the `>' that ends the template-argument-list. If we find
22998 a '>>' instead, it's probably just a typo. */
22999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23001 if (cxx_dialect
!= cxx98
)
23003 /* In C++0x, a `>>' in a template argument list or cast
23004 expression is considered to be two separate `>'
23005 tokens. So, change the current token to a `>', but don't
23006 consume it: it will be consumed later when the outer
23007 template argument list (or cast expression) is parsed.
23008 Note that this replacement of `>' for `>>' is necessary
23009 even if we are parsing tentatively: in the tentative
23010 case, after calling
23011 cp_parser_enclosed_template_argument_list we will always
23012 throw away all of the template arguments and the first
23013 closing `>', either because the template argument list
23014 was erroneous or because we are replacing those tokens
23015 with a CPP_TEMPLATE_ID token. The second `>' (which will
23016 not have been thrown away) is needed either to close an
23017 outer template argument list or to complete a new-style
23019 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23020 token
->type
= CPP_GREATER
;
23022 else if (!saved_greater_than_is_operator_p
)
23024 /* If we're in a nested template argument list, the '>>' has
23025 to be a typo for '> >'. We emit the error message, but we
23026 continue parsing and we push a '>' as next token, so that
23027 the argument list will be parsed correctly. Note that the
23028 global source location is still on the token before the
23029 '>>', so we need to say explicitly where we want it. */
23030 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23031 error_at (token
->location
, "%<>>%> should be %<> >%> "
23032 "within a nested template argument list");
23034 token
->type
= CPP_GREATER
;
23038 /* If this is not a nested template argument list, the '>>'
23039 is a typo for '>'. Emit an error message and continue.
23040 Same deal about the token location, but here we can get it
23041 right by consuming the '>>' before issuing the diagnostic. */
23042 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
23043 error_at (token
->location
,
23044 "spurious %<>>%>, use %<>%> to terminate "
23045 "a template argument list");
23049 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23050 /* The `>' token might be a greater-than operator again now. */
23051 parser
->greater_than_is_operator_p
23052 = saved_greater_than_is_operator_p
;
23053 /* Restore the SAVED_SCOPE. */
23054 parser
->scope
= saved_scope
;
23055 parser
->qualifying_scope
= saved_qualifying_scope
;
23056 parser
->object_scope
= saved_object_scope
;
23057 cp_unevaluated_operand
= saved_unevaluated_operand
;
23058 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23063 /* MEMBER_FUNCTION is a member function, or a friend. If default
23064 arguments, or the body of the function have not yet been parsed,
23068 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
23070 timevar_push (TV_PARSE_INMETH
);
23071 /* If this member is a template, get the underlying
23073 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
23074 member_function
= DECL_TEMPLATE_RESULT (member_function
);
23076 /* There should not be any class definitions in progress at this
23077 point; the bodies of members are only parsed outside of all class
23079 gcc_assert (parser
->num_classes_being_defined
== 0);
23080 /* While we're parsing the member functions we might encounter more
23081 classes. We want to handle them right away, but we don't want
23082 them getting mixed up with functions that are currently in the
23084 push_unparsed_function_queues (parser
);
23086 /* Make sure that any template parameters are in scope. */
23087 maybe_begin_member_template_processing (member_function
);
23089 /* If the body of the function has not yet been parsed, parse it
23091 if (DECL_PENDING_INLINE_P (member_function
))
23093 tree function_scope
;
23094 cp_token_cache
*tokens
;
23096 /* The function is no longer pending; we are processing it. */
23097 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
23098 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
23099 DECL_PENDING_INLINE_P (member_function
) = 0;
23101 /* If this is a local class, enter the scope of the containing
23103 function_scope
= current_function_decl
;
23104 if (function_scope
)
23105 push_function_context ();
23107 /* Push the body of the function onto the lexer stack. */
23108 cp_parser_push_lexer_for_tokens (parser
, tokens
);
23110 /* Let the front end know that we going to be defining this
23112 start_preparsed_function (member_function
, NULL_TREE
,
23113 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
23115 /* Don't do access checking if it is a templated function. */
23116 if (processing_template_decl
)
23117 push_deferring_access_checks (dk_no_check
);
23119 /* #pragma omp declare reduction needs special parsing. */
23120 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
23122 parser
->lexer
->in_pragma
= true;
23123 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
23124 finish_function (0);
23125 cp_check_omp_declare_reduction (member_function
);
23128 /* Now, parse the body of the function. */
23129 cp_parser_function_definition_after_declarator (parser
,
23130 /*inline_p=*/true);
23132 if (processing_template_decl
)
23133 pop_deferring_access_checks ();
23135 /* Leave the scope of the containing function. */
23136 if (function_scope
)
23137 pop_function_context ();
23138 cp_parser_pop_lexer (parser
);
23141 /* Remove any template parameters from the symbol table. */
23142 maybe_end_member_template_processing ();
23144 /* Restore the queue. */
23145 pop_unparsed_function_queues (parser
);
23146 timevar_pop (TV_PARSE_INMETH
);
23149 /* If DECL contains any default args, remember it on the unparsed
23150 functions queue. */
23153 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
23157 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
23159 probe
= TREE_CHAIN (probe
))
23160 if (TREE_PURPOSE (probe
))
23162 cp_default_arg_entry entry
= {current_class_type
, decl
};
23163 vec_safe_push (unparsed_funs_with_default_args
, entry
);
23168 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23169 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23170 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23171 from the parameter-type-list. */
23174 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
23175 tree default_arg
, tree parmtype
)
23177 cp_token_cache
*tokens
;
23181 if (default_arg
== error_mark_node
)
23182 return error_mark_node
;
23184 /* Push the saved tokens for the default argument onto the parser's
23186 tokens
= DEFARG_TOKENS (default_arg
);
23187 cp_parser_push_lexer_for_tokens (parser
, tokens
);
23189 start_lambda_scope (decl
);
23191 /* Parse the default argument. */
23192 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
23193 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
23194 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23196 finish_lambda_scope ();
23198 if (parsed_arg
== error_mark_node
)
23199 cp_parser_skip_to_end_of_statement (parser
);
23201 if (!processing_template_decl
)
23203 /* In a non-template class, check conversions now. In a template,
23204 we'll wait and instantiate these as needed. */
23205 if (TREE_CODE (decl
) == PARM_DECL
)
23206 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
23207 tf_warning_or_error
);
23210 int flags
= LOOKUP_IMPLICIT
;
23211 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
)
23212 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg
))
23213 flags
= LOOKUP_NORMAL
;
23214 parsed_arg
= digest_init_flags (TREE_TYPE (decl
), parsed_arg
, flags
);
23218 /* If the token stream has not been completely used up, then
23219 there was extra junk after the end of the default
23221 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
23223 if (TREE_CODE (decl
) == PARM_DECL
)
23224 cp_parser_error (parser
, "expected %<,%>");
23226 cp_parser_error (parser
, "expected %<;%>");
23229 /* Revert to the main lexer. */
23230 cp_parser_pop_lexer (parser
);
23235 /* FIELD is a non-static data member with an initializer which we saved for
23236 later; parse it now. */
23239 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
23243 push_unparsed_function_queues (parser
);
23244 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
23245 DECL_INITIAL (field
),
23247 pop_unparsed_function_queues (parser
);
23249 DECL_INITIAL (field
) = def
;
23252 /* FN is a FUNCTION_DECL which may contains a parameter with an
23253 unparsed DEFAULT_ARG. Parse the default args now. This function
23254 assumes that the current scope is the scope in which the default
23255 argument should be processed. */
23258 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
23260 bool saved_local_variables_forbidden_p
;
23261 tree parm
, parmdecl
;
23263 /* While we're parsing the default args, we might (due to the
23264 statement expression extension) encounter more classes. We want
23265 to handle them right away, but we don't want them getting mixed
23266 up with default args that are currently in the queue. */
23267 push_unparsed_function_queues (parser
);
23269 /* Local variable names (and the `this' keyword) may not appear
23270 in a default argument. */
23271 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
23272 parser
->local_variables_forbidden_p
= true;
23274 push_defarg_context (fn
);
23276 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
23277 parmdecl
= DECL_ARGUMENTS (fn
);
23278 parm
&& parm
!= void_list_node
;
23279 parm
= TREE_CHAIN (parm
),
23280 parmdecl
= DECL_CHAIN (parmdecl
))
23282 tree default_arg
= TREE_PURPOSE (parm
);
23284 vec
<tree
, va_gc
> *insts
;
23291 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
23292 /* This can happen for a friend declaration for a function
23293 already declared with default arguments. */
23297 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
23299 TREE_VALUE (parm
));
23300 if (parsed_arg
== error_mark_node
)
23305 TREE_PURPOSE (parm
) = parsed_arg
;
23307 /* Update any instantiations we've already created. */
23308 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
23309 vec_safe_iterate (insts
, ix
, ©
); ix
++)
23310 TREE_PURPOSE (copy
) = parsed_arg
;
23313 pop_defarg_context ();
23315 /* Make sure no default arg is missing. */
23316 check_default_args (fn
);
23318 /* Restore the state of local_variables_forbidden_p. */
23319 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
23321 /* Restore the queue. */
23322 pop_unparsed_function_queues (parser
);
23325 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23327 sizeof ... ( identifier )
23329 where the 'sizeof' token has already been consumed. */
23332 cp_parser_sizeof_pack (cp_parser
*parser
)
23334 /* Consume the `...'. */
23335 cp_lexer_consume_token (parser
->lexer
);
23336 maybe_warn_variadic_templates ();
23338 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
23340 cp_lexer_consume_token (parser
->lexer
);
23342 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
23343 "%<sizeof...%> argument must be surrounded by parentheses");
23345 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23346 tree name
= cp_parser_identifier (parser
);
23347 if (name
== error_mark_node
)
23348 return error_mark_node
;
23349 /* The name is not qualified. */
23350 parser
->scope
= NULL_TREE
;
23351 parser
->qualifying_scope
= NULL_TREE
;
23352 parser
->object_scope
= NULL_TREE
;
23353 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
23354 if (expr
== error_mark_node
)
23355 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
23357 if (TREE_CODE (expr
) == TYPE_DECL
)
23358 expr
= TREE_TYPE (expr
);
23359 else if (TREE_CODE (expr
) == CONST_DECL
)
23360 expr
= DECL_INITIAL (expr
);
23361 expr
= make_pack_expansion (expr
);
23364 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23369 /* Parse the operand of `sizeof' (or a similar operator). Returns
23370 either a TYPE or an expression, depending on the form of the
23371 input. The KEYWORD indicates which kind of expression we have
23375 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
23377 tree expr
= NULL_TREE
;
23378 const char *saved_message
;
23380 bool saved_integral_constant_expression_p
;
23381 bool saved_non_integral_constant_expression_p
;
23383 /* If it's a `...', then we are computing the length of a parameter
23385 if (keyword
== RID_SIZEOF
23386 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23387 return cp_parser_sizeof_pack (parser
);
23389 /* Types cannot be defined in a `sizeof' expression. Save away the
23391 saved_message
= parser
->type_definition_forbidden_message
;
23392 /* And create the new one. */
23393 tmp
= concat ("types may not be defined in %<",
23394 IDENTIFIER_POINTER (ridpointers
[keyword
]),
23395 "%> expressions", NULL
);
23396 parser
->type_definition_forbidden_message
= tmp
;
23398 /* The restrictions on constant-expressions do not apply inside
23399 sizeof expressions. */
23400 saved_integral_constant_expression_p
23401 = parser
->integral_constant_expression_p
;
23402 saved_non_integral_constant_expression_p
23403 = parser
->non_integral_constant_expression_p
;
23404 parser
->integral_constant_expression_p
= false;
23406 /* Do not actually evaluate the expression. */
23407 ++cp_unevaluated_operand
;
23408 ++c_inhibit_evaluation_warnings
;
23409 /* If it's a `(', then we might be looking at the type-id
23411 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23413 tree type
= NULL_TREE
;
23414 bool compound_literal_p
;
23416 /* We can't be sure yet whether we're looking at a type-id or an
23418 cp_parser_parse_tentatively (parser
);
23419 /* Consume the `('. */
23420 cp_lexer_consume_token (parser
->lexer
);
23421 /* Note: as a GNU Extension, compound literals are considered
23422 postfix-expressions as they are in C99, so they are valid
23423 arguments to sizeof. See comment in cp_parser_cast_expression
23425 cp_lexer_save_tokens (parser
->lexer
);
23426 /* Skip tokens until the next token is a closing parenthesis.
23427 If we find the closing `)', and the next token is a `{', then
23428 we are looking at a compound-literal. */
23430 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
23431 /*consume_paren=*/true)
23432 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
23433 /* Roll back the tokens we skipped. */
23434 cp_lexer_rollback_tokens (parser
->lexer
);
23435 /* If we were looking at a compound-literal, simulate an error
23436 so that the call to cp_parser_parse_definitely below will
23438 if (compound_literal_p
)
23439 cp_parser_simulate_error (parser
);
23442 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
23443 parser
->in_type_id_in_expr_p
= true;
23444 /* Look for the type-id. */
23445 type
= cp_parser_type_id (parser
);
23446 /* Look for the closing `)'. */
23447 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23448 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
23451 /* If all went well, then we're done. */
23452 if (cp_parser_parse_definitely (parser
))
23454 cp_decl_specifier_seq decl_specs
;
23456 /* Build a trivial decl-specifier-seq. */
23457 clear_decl_specs (&decl_specs
);
23458 decl_specs
.type
= type
;
23460 /* Call grokdeclarator to figure out what type this is. */
23461 expr
= grokdeclarator (NULL
,
23465 /*attrlist=*/NULL
);
23469 /* If the type-id production did not work out, then we must be
23470 looking at the unary-expression production. */
23472 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
23473 /*cast_p=*/false, NULL
);
23475 /* Go back to evaluating expressions. */
23476 --cp_unevaluated_operand
;
23477 --c_inhibit_evaluation_warnings
;
23479 /* Free the message we created. */
23481 /* And restore the old one. */
23482 parser
->type_definition_forbidden_message
= saved_message
;
23483 parser
->integral_constant_expression_p
23484 = saved_integral_constant_expression_p
;
23485 parser
->non_integral_constant_expression_p
23486 = saved_non_integral_constant_expression_p
;
23491 /* If the current declaration has no declarator, return true. */
23494 cp_parser_declares_only_class_p (cp_parser
*parser
)
23496 /* If the next token is a `;' or a `,' then there is no
23498 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
23499 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
23502 /* Update the DECL_SPECS to reflect the storage class indicated by
23506 cp_parser_set_storage_class (cp_parser
*parser
,
23507 cp_decl_specifier_seq
*decl_specs
,
23511 cp_storage_class storage_class
;
23513 if (parser
->in_unbraced_linkage_specification_p
)
23515 error_at (token
->location
, "invalid use of %qD in linkage specification",
23516 ridpointers
[keyword
]);
23519 else if (decl_specs
->storage_class
!= sc_none
)
23521 decl_specs
->conflicting_specifiers_p
= true;
23525 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
23526 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
23527 && decl_specs
->gnu_thread_keyword_p
)
23529 pedwarn (decl_specs
->locations
[ds_thread
], 0,
23530 "%<__thread%> before %qD", ridpointers
[keyword
]);
23536 storage_class
= sc_auto
;
23539 storage_class
= sc_register
;
23542 storage_class
= sc_static
;
23545 storage_class
= sc_extern
;
23548 storage_class
= sc_mutable
;
23551 gcc_unreachable ();
23553 decl_specs
->storage_class
= storage_class
;
23554 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
23556 /* A storage class specifier cannot be applied alongside a typedef
23557 specifier. If there is a typedef specifier present then set
23558 conflicting_specifiers_p which will trigger an error later
23559 on in grokdeclarator. */
23560 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
23561 decl_specs
->conflicting_specifiers_p
= true;
23564 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23565 is true, the type is a class or enum definition. */
23568 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
23571 bool type_definition_p
)
23573 decl_specs
->any_specifiers_p
= true;
23575 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23576 (with, for example, in "typedef int wchar_t;") we remember that
23577 this is what happened. In system headers, we ignore these
23578 declarations so that G++ can work with system headers that are not
23580 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
23581 && !type_definition_p
23582 && (type_spec
== boolean_type_node
23583 || type_spec
== char16_type_node
23584 || type_spec
== char32_type_node
23585 || type_spec
== wchar_type_node
)
23586 && (decl_specs
->type
23587 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
23588 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
23589 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
23590 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
23592 decl_specs
->redefined_builtin_type
= type_spec
;
23593 set_and_check_decl_spec_loc (decl_specs
,
23594 ds_redefined_builtin_type_spec
,
23596 if (!decl_specs
->type
)
23598 decl_specs
->type
= type_spec
;
23599 decl_specs
->type_definition_p
= false;
23600 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
23603 else if (decl_specs
->type
)
23604 decl_specs
->multiple_types_p
= true;
23607 decl_specs
->type
= type_spec
;
23608 decl_specs
->type_definition_p
= type_definition_p
;
23609 decl_specs
->redefined_builtin_type
= NULL_TREE
;
23610 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
23614 /* True iff TOKEN is the GNU keyword __thread. */
23617 token_is__thread (cp_token
*token
)
23619 gcc_assert (token
->keyword
== RID_THREAD
);
23620 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
23623 /* Set the location for a declarator specifier and check if it is
23626 DECL_SPECS is the sequence of declarator specifiers onto which to
23629 DS is the single declarator specifier to set which location is to
23630 be set onto the existing sequence of declarators.
23632 LOCATION is the location for the declarator specifier to
23636 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
23637 cp_decl_spec ds
, cp_token
*token
)
23639 gcc_assert (ds
< ds_last
);
23641 if (decl_specs
== NULL
)
23644 source_location location
= token
->location
;
23646 if (decl_specs
->locations
[ds
] == 0)
23648 decl_specs
->locations
[ds
] = location
;
23649 if (ds
== ds_thread
)
23650 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
23656 if (decl_specs
->locations
[ds_long_long
] != 0)
23657 error_at (location
,
23658 "%<long long long%> is too long for GCC");
23661 decl_specs
->locations
[ds_long_long
] = location
;
23662 pedwarn_cxx98 (location
,
23664 "ISO C++ 1998 does not support %<long long%>");
23667 else if (ds
== ds_thread
)
23669 bool gnu
= token_is__thread (token
);
23670 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
23671 error_at (location
,
23672 "both %<__thread%> and %<thread_local%> specified");
23674 error_at (location
, "duplicate %qD", token
->u
.value
);
23678 static const char *const decl_spec_names
[] = {
23695 error_at (location
,
23696 "duplicate %qs", decl_spec_names
[ds
]);
23701 /* Return true iff the declarator specifier DS is present in the
23702 sequence of declarator specifiers DECL_SPECS. */
23705 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
23708 gcc_assert (ds
< ds_last
);
23710 if (decl_specs
== NULL
)
23713 return decl_specs
->locations
[ds
] != 0;
23716 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23717 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23720 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
23722 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
23725 /* Issue an error message indicating that TOKEN_DESC was expected.
23726 If KEYWORD is true, it indicated this function is called by
23727 cp_parser_require_keword and the required token can only be
23728 a indicated keyword. */
23731 cp_parser_required_error (cp_parser
*parser
,
23732 required_token token_desc
,
23735 switch (token_desc
)
23738 cp_parser_error (parser
, "expected %<new%>");
23741 cp_parser_error (parser
, "expected %<delete%>");
23744 cp_parser_error (parser
, "expected %<return%>");
23747 cp_parser_error (parser
, "expected %<while%>");
23750 cp_parser_error (parser
, "expected %<extern%>");
23752 case RT_STATIC_ASSERT
:
23753 cp_parser_error (parser
, "expected %<static_assert%>");
23756 cp_parser_error (parser
, "expected %<decltype%>");
23759 cp_parser_error (parser
, "expected %<operator%>");
23762 cp_parser_error (parser
, "expected %<class%>");
23765 cp_parser_error (parser
, "expected %<template%>");
23768 cp_parser_error (parser
, "expected %<namespace%>");
23771 cp_parser_error (parser
, "expected %<using%>");
23774 cp_parser_error (parser
, "expected %<asm%>");
23777 cp_parser_error (parser
, "expected %<try%>");
23780 cp_parser_error (parser
, "expected %<catch%>");
23783 cp_parser_error (parser
, "expected %<throw%>");
23786 cp_parser_error (parser
, "expected %<__label__%>");
23789 cp_parser_error (parser
, "expected %<@try%>");
23791 case RT_AT_SYNCHRONIZED
:
23792 cp_parser_error (parser
, "expected %<@synchronized%>");
23795 cp_parser_error (parser
, "expected %<@throw%>");
23797 case RT_TRANSACTION_ATOMIC
:
23798 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
23800 case RT_TRANSACTION_RELAXED
:
23801 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
23808 switch (token_desc
)
23811 cp_parser_error (parser
, "expected %<;%>");
23813 case RT_OPEN_PAREN
:
23814 cp_parser_error (parser
, "expected %<(%>");
23816 case RT_CLOSE_BRACE
:
23817 cp_parser_error (parser
, "expected %<}%>");
23819 case RT_OPEN_BRACE
:
23820 cp_parser_error (parser
, "expected %<{%>");
23822 case RT_CLOSE_SQUARE
:
23823 cp_parser_error (parser
, "expected %<]%>");
23825 case RT_OPEN_SQUARE
:
23826 cp_parser_error (parser
, "expected %<[%>");
23829 cp_parser_error (parser
, "expected %<,%>");
23832 cp_parser_error (parser
, "expected %<::%>");
23835 cp_parser_error (parser
, "expected %<<%>");
23838 cp_parser_error (parser
, "expected %<>%>");
23841 cp_parser_error (parser
, "expected %<=%>");
23844 cp_parser_error (parser
, "expected %<...%>");
23847 cp_parser_error (parser
, "expected %<*%>");
23850 cp_parser_error (parser
, "expected %<~%>");
23853 cp_parser_error (parser
, "expected %<:%>");
23855 case RT_COLON_SCOPE
:
23856 cp_parser_error (parser
, "expected %<:%> or %<::%>");
23858 case RT_CLOSE_PAREN
:
23859 cp_parser_error (parser
, "expected %<)%>");
23861 case RT_COMMA_CLOSE_PAREN
:
23862 cp_parser_error (parser
, "expected %<,%> or %<)%>");
23864 case RT_PRAGMA_EOL
:
23865 cp_parser_error (parser
, "expected end of line");
23868 cp_parser_error (parser
, "expected identifier");
23871 cp_parser_error (parser
, "expected selection-statement");
23873 case RT_INTERATION
:
23874 cp_parser_error (parser
, "expected iteration-statement");
23877 cp_parser_error (parser
, "expected jump-statement");
23880 cp_parser_error (parser
, "expected class-key");
23882 case RT_CLASS_TYPENAME_TEMPLATE
:
23883 cp_parser_error (parser
,
23884 "expected %<class%>, %<typename%>, or %<template%>");
23887 gcc_unreachable ();
23891 gcc_unreachable ();
23896 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23897 issue an error message indicating that TOKEN_DESC was expected.
23899 Returns the token consumed, if the token had the appropriate type.
23900 Otherwise, returns NULL. */
23903 cp_parser_require (cp_parser
* parser
,
23904 enum cpp_ttype type
,
23905 required_token token_desc
)
23907 if (cp_lexer_next_token_is (parser
->lexer
, type
))
23908 return cp_lexer_consume_token (parser
->lexer
);
23911 /* Output the MESSAGE -- unless we're parsing tentatively. */
23912 if (!cp_parser_simulate_error (parser
))
23913 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
23918 /* An error message is produced if the next token is not '>'.
23919 All further tokens are skipped until the desired token is
23920 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23923 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
23925 /* Current level of '< ... >'. */
23926 unsigned level
= 0;
23927 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23928 unsigned nesting_depth
= 0;
23930 /* Are we ready, yet? If not, issue error message. */
23931 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
23934 /* Skip tokens until the desired token is found. */
23937 /* Peek at the next token. */
23938 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
23941 if (!nesting_depth
)
23946 if (cxx_dialect
== cxx98
)
23947 /* C++0x views the `>>' operator as two `>' tokens, but
23950 else if (!nesting_depth
&& level
-- == 0)
23952 /* We've hit a `>>' where the first `>' closes the
23953 template argument list, and the second `>' is
23954 spurious. Just consume the `>>' and stop; we've
23955 already produced at least one error. */
23956 cp_lexer_consume_token (parser
->lexer
);
23959 /* Fall through for C++0x, so we handle the second `>' in
23963 if (!nesting_depth
&& level
-- == 0)
23965 /* We've reached the token we want, consume it and stop. */
23966 cp_lexer_consume_token (parser
->lexer
);
23971 case CPP_OPEN_PAREN
:
23972 case CPP_OPEN_SQUARE
:
23976 case CPP_CLOSE_PAREN
:
23977 case CPP_CLOSE_SQUARE
:
23978 if (nesting_depth
-- == 0)
23983 case CPP_PRAGMA_EOL
:
23984 case CPP_SEMICOLON
:
23985 case CPP_OPEN_BRACE
:
23986 case CPP_CLOSE_BRACE
:
23987 /* The '>' was probably forgotten, don't look further. */
23994 /* Consume this token. */
23995 cp_lexer_consume_token (parser
->lexer
);
23999 /* If the next token is the indicated keyword, consume it. Otherwise,
24000 issue an error message indicating that TOKEN_DESC was expected.
24002 Returns the token consumed, if the token had the appropriate type.
24003 Otherwise, returns NULL. */
24006 cp_parser_require_keyword (cp_parser
* parser
,
24008 required_token token_desc
)
24010 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
24012 if (token
&& token
->keyword
!= keyword
)
24014 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
24021 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24022 function-definition. */
24025 cp_parser_token_starts_function_definition_p (cp_token
* token
)
24027 return (/* An ordinary function-body begins with an `{'. */
24028 token
->type
== CPP_OPEN_BRACE
24029 /* A ctor-initializer begins with a `:'. */
24030 || token
->type
== CPP_COLON
24031 /* A function-try-block begins with `try'. */
24032 || token
->keyword
== RID_TRY
24033 /* A function-transaction-block begins with `__transaction_atomic'
24034 or `__transaction_relaxed'. */
24035 || token
->keyword
== RID_TRANSACTION_ATOMIC
24036 || token
->keyword
== RID_TRANSACTION_RELAXED
24037 /* The named return value extension begins with `return'. */
24038 || token
->keyword
== RID_RETURN
);
24041 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24045 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
24049 token
= cp_lexer_peek_token (parser
->lexer
);
24050 return (token
->type
== CPP_OPEN_BRACE
24051 || (token
->type
== CPP_COLON
24052 && !parser
->colon_doesnt_start_class_def_p
));
24055 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24056 C++0x) ending a template-argument. */
24059 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
24063 token
= cp_lexer_peek_token (parser
->lexer
);
24064 return (token
->type
== CPP_COMMA
24065 || token
->type
== CPP_GREATER
24066 || token
->type
== CPP_ELLIPSIS
24067 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
24070 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24071 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24074 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
24079 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24080 if (token
->type
== CPP_LESS
)
24082 /* Check for the sequence `<::' in the original code. It would be lexed as
24083 `[:', where `[' is a digraph, and there is no whitespace before
24085 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
24088 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
24089 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
24095 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24096 or none_type otherwise. */
24098 static enum tag_types
24099 cp_parser_token_is_class_key (cp_token
* token
)
24101 switch (token
->keyword
)
24106 return record_type
;
24115 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24118 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
24120 if (type
== error_mark_node
)
24122 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
24124 if (permerror (input_location
, "%qs tag used in naming %q#T",
24125 class_key
== union_type
? "union"
24126 : class_key
== record_type
? "struct" : "class",
24128 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
24129 "%q#T was previously declared here", type
);
24133 /* Issue an error message if DECL is redeclared with different
24134 access than its original declaration [class.access.spec/3].
24135 This applies to nested classes and nested class templates.
24139 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
24141 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
24144 if ((TREE_PRIVATE (decl
)
24145 != (current_access_specifier
== access_private_node
))
24146 || (TREE_PROTECTED (decl
)
24147 != (current_access_specifier
== access_protected_node
)))
24148 error_at (location
, "%qD redeclared with different access", decl
);
24151 /* Look for the `template' keyword, as a syntactic disambiguator.
24152 Return TRUE iff it is present, in which case it will be
24156 cp_parser_optional_template_keyword (cp_parser
*parser
)
24158 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24160 /* In C++98 the `template' keyword can only be used within templates;
24161 outside templates the parser can always figure out what is a
24162 template and what is not. In C++11, per the resolution of DR 468,
24163 `template' is allowed in cases where it is not strictly necessary. */
24164 if (!processing_template_decl
24165 && pedantic
&& cxx_dialect
== cxx98
)
24167 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24168 pedwarn (token
->location
, OPT_Wpedantic
,
24169 "in C++98 %<template%> (as a disambiguator) is only "
24170 "allowed within templates");
24171 /* If this part of the token stream is rescanned, the same
24172 error message would be generated. So, we purge the token
24173 from the stream. */
24174 cp_lexer_purge_token (parser
->lexer
);
24179 /* Consume the `template' keyword. */
24180 cp_lexer_consume_token (parser
->lexer
);
24187 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24188 set PARSER->SCOPE, and perform other related actions. */
24191 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
24194 struct tree_check
*check_value
;
24195 deferred_access_check
*chk
;
24196 vec
<deferred_access_check
, va_gc
> *checks
;
24198 /* Get the stored value. */
24199 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
24200 /* Perform any access checks that were deferred. */
24201 checks
= check_value
->checks
;
24204 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
24205 perform_or_defer_access_check (chk
->binfo
,
24207 chk
->diag_decl
, tf_warning_or_error
);
24209 /* Set the scope from the stored value. */
24210 parser
->scope
= check_value
->value
;
24211 parser
->qualifying_scope
= check_value
->qualifying_scope
;
24212 parser
->object_scope
= NULL_TREE
;
24215 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24216 encounter the end of a block before what we were looking for. */
24219 cp_parser_cache_group (cp_parser
*parser
,
24220 enum cpp_ttype end
,
24225 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24227 /* Abort a parenthesized expression if we encounter a semicolon. */
24228 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
24229 && token
->type
== CPP_SEMICOLON
)
24231 /* If we've reached the end of the file, stop. */
24232 if (token
->type
== CPP_EOF
24233 || (end
!= CPP_PRAGMA_EOL
24234 && token
->type
== CPP_PRAGMA_EOL
))
24236 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
24237 /* We've hit the end of an enclosing block, so there's been some
24238 kind of syntax error. */
24241 /* Consume the token. */
24242 cp_lexer_consume_token (parser
->lexer
);
24243 /* See if it starts a new group. */
24244 if (token
->type
== CPP_OPEN_BRACE
)
24246 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
24247 /* In theory this should probably check end == '}', but
24248 cp_parser_save_member_function_body needs it to exit
24249 after either '}' or ')' when called with ')'. */
24253 else if (token
->type
== CPP_OPEN_PAREN
)
24255 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
24256 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
24259 else if (token
->type
== CPP_PRAGMA
)
24260 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
24261 else if (token
->type
== end
)
24266 /* Like above, for caching a default argument or NSDMI. Both of these are
24267 terminated by a non-nested comma, but it can be unclear whether or not a
24268 comma is nested in a template argument list unless we do more parsing.
24269 In order to handle this ambiguity, when we encounter a ',' after a '<'
24270 we try to parse what follows as a parameter-declaration-list (in the
24271 case of a default argument) or a member-declarator (in the case of an
24272 NSDMI). If that succeeds, then we stop caching. */
24275 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
24277 unsigned depth
= 0;
24278 int maybe_template_id
= 0;
24279 cp_token
*first_token
;
24281 tree default_argument
;
24283 /* Add tokens until we have processed the entire default
24284 argument. We add the range [first_token, token). */
24285 first_token
= cp_lexer_peek_token (parser
->lexer
);
24286 if (first_token
->type
== CPP_OPEN_BRACE
)
24288 /* For list-initialization, this is straightforward. */
24289 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
24290 token
= cp_lexer_peek_token (parser
->lexer
);
24296 /* Peek at the next token. */
24297 token
= cp_lexer_peek_token (parser
->lexer
);
24298 /* What we do depends on what token we have. */
24299 switch (token
->type
)
24301 /* In valid code, a default argument must be
24302 immediately followed by a `,' `)', or `...'. */
24304 if (depth
== 0 && maybe_template_id
)
24306 /* If we've seen a '<', we might be in a
24307 template-argument-list. Until Core issue 325 is
24308 resolved, we don't know how this situation ought
24309 to be handled, so try to DTRT. We check whether
24310 what comes after the comma is a valid parameter
24311 declaration list. If it is, then the comma ends
24312 the default argument; otherwise the default
24313 argument continues. */
24314 bool error
= false;
24316 /* Set ITALP so cp_parser_parameter_declaration_list
24317 doesn't decide to commit to this parse. */
24318 bool saved_italp
= parser
->in_template_argument_list_p
;
24319 parser
->in_template_argument_list_p
= true;
24321 cp_parser_parse_tentatively (parser
);
24322 cp_lexer_consume_token (parser
->lexer
);
24326 int ctor_dtor_or_conv_p
;
24327 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24328 &ctor_dtor_or_conv_p
,
24329 /*parenthesized_p=*/NULL
,
24330 /*member_p=*/true);
24334 begin_scope (sk_function_parms
, NULL_TREE
);
24335 cp_parser_parameter_declaration_list (parser
, &error
);
24336 pop_bindings_and_leave_scope ();
24338 if (!cp_parser_error_occurred (parser
) && !error
)
24340 cp_parser_abort_tentative_parse (parser
);
24342 parser
->in_template_argument_list_p
= saved_italp
;
24345 case CPP_CLOSE_PAREN
:
24347 /* If we run into a non-nested `;', `}', or `]',
24348 then the code is invalid -- but the default
24349 argument is certainly over. */
24350 case CPP_SEMICOLON
:
24351 case CPP_CLOSE_BRACE
:
24352 case CPP_CLOSE_SQUARE
:
24354 /* Handle correctly int n = sizeof ... ( p ); */
24355 && !(nsdmi
&& token
->type
== CPP_ELLIPSIS
))
24357 /* Update DEPTH, if necessary. */
24358 else if (token
->type
== CPP_CLOSE_PAREN
24359 || token
->type
== CPP_CLOSE_BRACE
24360 || token
->type
== CPP_CLOSE_SQUARE
)
24364 case CPP_OPEN_PAREN
:
24365 case CPP_OPEN_SQUARE
:
24366 case CPP_OPEN_BRACE
:
24372 /* This might be the comparison operator, or it might
24373 start a template argument list. */
24374 ++maybe_template_id
;
24378 if (cxx_dialect
== cxx98
)
24380 /* Fall through for C++0x, which treats the `>>'
24381 operator like two `>' tokens in certain
24387 /* This might be an operator, or it might close a
24388 template argument list. But if a previous '<'
24389 started a template argument list, this will have
24390 closed it, so we can't be in one anymore. */
24391 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
24392 if (maybe_template_id
< 0)
24393 maybe_template_id
= 0;
24397 /* If we run out of tokens, issue an error message. */
24399 case CPP_PRAGMA_EOL
:
24400 error_at (token
->location
, "file ends in default argument");
24406 /* In these cases, we should look for template-ids.
24407 For example, if the default argument is
24408 `X<int, double>()', we need to do name lookup to
24409 figure out whether or not `X' is a template; if
24410 so, the `,' does not end the default argument.
24412 That is not yet done. */
24419 /* If we've reached the end, stop. */
24423 /* Add the token to the token block. */
24424 token
= cp_lexer_consume_token (parser
->lexer
);
24427 /* Create a DEFAULT_ARG to represent the unparsed default
24429 default_argument
= make_node (DEFAULT_ARG
);
24430 DEFARG_TOKENS (default_argument
)
24431 = cp_token_cache_new (first_token
, token
);
24432 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
24434 return default_argument
;
24437 /* Begin parsing tentatively. We always save tokens while parsing
24438 tentatively so that if the tentative parsing fails we can restore the
24442 cp_parser_parse_tentatively (cp_parser
* parser
)
24444 /* Enter a new parsing context. */
24445 parser
->context
= cp_parser_context_new (parser
->context
);
24446 /* Begin saving tokens. */
24447 cp_lexer_save_tokens (parser
->lexer
);
24448 /* In order to avoid repetitive access control error messages,
24449 access checks are queued up until we are no longer parsing
24451 push_deferring_access_checks (dk_deferred
);
24454 /* Commit to the currently active tentative parse. */
24457 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
24459 cp_parser_context
*context
;
24462 /* Mark all of the levels as committed. */
24463 lexer
= parser
->lexer
;
24464 for (context
= parser
->context
; context
->next
; context
= context
->next
)
24466 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24468 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24469 while (!cp_lexer_saving_tokens (lexer
))
24470 lexer
= lexer
->next
;
24471 cp_lexer_commit_tokens (lexer
);
24475 /* Commit to the topmost currently active tentative parse.
24477 Note that this function shouldn't be called when there are
24478 irreversible side-effects while in a tentative state. For
24479 example, we shouldn't create a permanent entry in the symbol
24480 table, or issue an error message that might not apply if the
24481 tentative parse is aborted. */
24484 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
24486 cp_parser_context
*context
= parser
->context
;
24487 cp_lexer
*lexer
= parser
->lexer
;
24491 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24493 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24495 while (!cp_lexer_saving_tokens (lexer
))
24496 lexer
= lexer
->next
;
24497 cp_lexer_commit_tokens (lexer
);
24501 /* Abort the currently active tentative parse. All consumed tokens
24502 will be rolled back, and no diagnostics will be issued. */
24505 cp_parser_abort_tentative_parse (cp_parser
* parser
)
24507 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
24508 || errorcount
> 0);
24509 cp_parser_simulate_error (parser
);
24510 /* Now, pretend that we want to see if the construct was
24511 successfully parsed. */
24512 cp_parser_parse_definitely (parser
);
24515 /* Stop parsing tentatively. If a parse error has occurred, restore the
24516 token stream. Otherwise, commit to the tokens we have consumed.
24517 Returns true if no error occurred; false otherwise. */
24520 cp_parser_parse_definitely (cp_parser
* parser
)
24522 bool error_occurred
;
24523 cp_parser_context
*context
;
24525 /* Remember whether or not an error occurred, since we are about to
24526 destroy that information. */
24527 error_occurred
= cp_parser_error_occurred (parser
);
24528 /* Remove the topmost context from the stack. */
24529 context
= parser
->context
;
24530 parser
->context
= context
->next
;
24531 /* If no parse errors occurred, commit to the tentative parse. */
24532 if (!error_occurred
)
24534 /* Commit to the tokens read tentatively, unless that was
24536 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
24537 cp_lexer_commit_tokens (parser
->lexer
);
24539 pop_to_parent_deferring_access_checks ();
24541 /* Otherwise, if errors occurred, roll back our state so that things
24542 are just as they were before we began the tentative parse. */
24545 cp_lexer_rollback_tokens (parser
->lexer
);
24546 pop_deferring_access_checks ();
24548 /* Add the context to the front of the free list. */
24549 context
->next
= cp_parser_context_free_list
;
24550 cp_parser_context_free_list
= context
;
24552 return !error_occurred
;
24555 /* Returns true if we are parsing tentatively and are not committed to
24556 this tentative parse. */
24559 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
24561 return (cp_parser_parsing_tentatively (parser
)
24562 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
24565 /* Returns nonzero iff an error has occurred during the most recent
24566 tentative parse. */
24569 cp_parser_error_occurred (cp_parser
* parser
)
24571 return (cp_parser_parsing_tentatively (parser
)
24572 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
24575 /* Returns nonzero if GNU extensions are allowed. */
24578 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
24580 return parser
->allow_gnu_extensions_p
;
24583 /* Objective-C++ Productions */
24586 /* Parse an Objective-C expression, which feeds into a primary-expression
24590 objc-message-expression
24591 objc-string-literal
24592 objc-encode-expression
24593 objc-protocol-expression
24594 objc-selector-expression
24596 Returns a tree representation of the expression. */
24599 cp_parser_objc_expression (cp_parser
* parser
)
24601 /* Try to figure out what kind of declaration is present. */
24602 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
24606 case CPP_OPEN_SQUARE
:
24607 return cp_parser_objc_message_expression (parser
);
24609 case CPP_OBJC_STRING
:
24610 kwd
= cp_lexer_consume_token (parser
->lexer
);
24611 return objc_build_string_object (kwd
->u
.value
);
24614 switch (kwd
->keyword
)
24616 case RID_AT_ENCODE
:
24617 return cp_parser_objc_encode_expression (parser
);
24619 case RID_AT_PROTOCOL
:
24620 return cp_parser_objc_protocol_expression (parser
);
24622 case RID_AT_SELECTOR
:
24623 return cp_parser_objc_selector_expression (parser
);
24629 error_at (kwd
->location
,
24630 "misplaced %<@%D%> Objective-C++ construct",
24632 cp_parser_skip_to_end_of_block_or_statement (parser
);
24635 return error_mark_node
;
24638 /* Parse an Objective-C message expression.
24640 objc-message-expression:
24641 [ objc-message-receiver objc-message-args ]
24643 Returns a representation of an Objective-C message. */
24646 cp_parser_objc_message_expression (cp_parser
* parser
)
24648 tree receiver
, messageargs
;
24650 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
24651 receiver
= cp_parser_objc_message_receiver (parser
);
24652 messageargs
= cp_parser_objc_message_args (parser
);
24653 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24655 return objc_build_message_expr (receiver
, messageargs
);
24658 /* Parse an objc-message-receiver.
24660 objc-message-receiver:
24662 simple-type-specifier
24664 Returns a representation of the type or expression. */
24667 cp_parser_objc_message_receiver (cp_parser
* parser
)
24671 /* An Objective-C message receiver may be either (1) a type
24672 or (2) an expression. */
24673 cp_parser_parse_tentatively (parser
);
24674 rcv
= cp_parser_expression (parser
, false, NULL
);
24676 if (cp_parser_parse_definitely (parser
))
24679 rcv
= cp_parser_simple_type_specifier (parser
,
24680 /*decl_specs=*/NULL
,
24681 CP_PARSER_FLAGS_NONE
);
24683 return objc_get_class_reference (rcv
);
24686 /* Parse the arguments and selectors comprising an Objective-C message.
24691 objc-selector-args , objc-comma-args
24693 objc-selector-args:
24694 objc-selector [opt] : assignment-expression
24695 objc-selector-args objc-selector [opt] : assignment-expression
24698 assignment-expression
24699 objc-comma-args , assignment-expression
24701 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24702 selector arguments and TREE_VALUE containing a list of comma
24706 cp_parser_objc_message_args (cp_parser
* parser
)
24708 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
24709 bool maybe_unary_selector_p
= true;
24710 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24712 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
24714 tree selector
= NULL_TREE
, arg
;
24716 if (token
->type
!= CPP_COLON
)
24717 selector
= cp_parser_objc_selector (parser
);
24719 /* Detect if we have a unary selector. */
24720 if (maybe_unary_selector_p
24721 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
24722 return build_tree_list (selector
, NULL_TREE
);
24724 maybe_unary_selector_p
= false;
24725 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24726 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
24729 = chainon (sel_args
,
24730 build_tree_list (selector
, arg
));
24732 token
= cp_lexer_peek_token (parser
->lexer
);
24735 /* Handle non-selector arguments, if any. */
24736 while (token
->type
== CPP_COMMA
)
24740 cp_lexer_consume_token (parser
->lexer
);
24741 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
24744 = chainon (addl_args
,
24745 build_tree_list (NULL_TREE
, arg
));
24747 token
= cp_lexer_peek_token (parser
->lexer
);
24750 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
24752 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
24753 return build_tree_list (error_mark_node
, error_mark_node
);
24756 return build_tree_list (sel_args
, addl_args
);
24759 /* Parse an Objective-C encode expression.
24761 objc-encode-expression:
24762 @encode objc-typename
24764 Returns an encoded representation of the type argument. */
24767 cp_parser_objc_encode_expression (cp_parser
* parser
)
24772 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
24773 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24774 token
= cp_lexer_peek_token (parser
->lexer
);
24775 type
= complete_type (cp_parser_type_id (parser
));
24776 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24780 error_at (token
->location
,
24781 "%<@encode%> must specify a type as an argument");
24782 return error_mark_node
;
24785 /* This happens if we find @encode(T) (where T is a template
24786 typename or something dependent on a template typename) when
24787 parsing a template. In that case, we can't compile it
24788 immediately, but we rather create an AT_ENCODE_EXPR which will
24789 need to be instantiated when the template is used.
24791 if (dependent_type_p (type
))
24793 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
24794 TREE_READONLY (value
) = 1;
24798 return objc_build_encode_expr (type
);
24801 /* Parse an Objective-C @defs expression. */
24804 cp_parser_objc_defs_expression (cp_parser
*parser
)
24808 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
24809 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24810 name
= cp_parser_identifier (parser
);
24811 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24813 return objc_get_class_ivars (name
);
24816 /* Parse an Objective-C protocol expression.
24818 objc-protocol-expression:
24819 @protocol ( identifier )
24821 Returns a representation of the protocol expression. */
24824 cp_parser_objc_protocol_expression (cp_parser
* parser
)
24828 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
24829 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24830 proto
= cp_parser_identifier (parser
);
24831 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24833 return objc_build_protocol_expr (proto
);
24836 /* Parse an Objective-C selector expression.
24838 objc-selector-expression:
24839 @selector ( objc-method-signature )
24841 objc-method-signature:
24847 objc-selector-seq objc-selector :
24849 Returns a representation of the method selector. */
24852 cp_parser_objc_selector_expression (cp_parser
* parser
)
24854 tree sel_seq
= NULL_TREE
;
24855 bool maybe_unary_selector_p
= true;
24857 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24859 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
24860 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24861 token
= cp_lexer_peek_token (parser
->lexer
);
24863 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
24864 || token
->type
== CPP_SCOPE
)
24866 tree selector
= NULL_TREE
;
24868 if (token
->type
!= CPP_COLON
24869 || token
->type
== CPP_SCOPE
)
24870 selector
= cp_parser_objc_selector (parser
);
24872 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
24873 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
24875 /* Detect if we have a unary selector. */
24876 if (maybe_unary_selector_p
)
24878 sel_seq
= selector
;
24879 goto finish_selector
;
24883 cp_parser_error (parser
, "expected %<:%>");
24886 maybe_unary_selector_p
= false;
24887 token
= cp_lexer_consume_token (parser
->lexer
);
24889 if (token
->type
== CPP_SCOPE
)
24892 = chainon (sel_seq
,
24893 build_tree_list (selector
, NULL_TREE
));
24895 = chainon (sel_seq
,
24896 build_tree_list (NULL_TREE
, NULL_TREE
));
24900 = chainon (sel_seq
,
24901 build_tree_list (selector
, NULL_TREE
));
24903 token
= cp_lexer_peek_token (parser
->lexer
);
24907 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24909 return objc_build_selector_expr (loc
, sel_seq
);
24912 /* Parse a list of identifiers.
24914 objc-identifier-list:
24916 objc-identifier-list , identifier
24918 Returns a TREE_LIST of identifier nodes. */
24921 cp_parser_objc_identifier_list (cp_parser
* parser
)
24927 identifier
= cp_parser_identifier (parser
);
24928 if (identifier
== error_mark_node
)
24929 return error_mark_node
;
24931 list
= build_tree_list (NULL_TREE
, identifier
);
24932 sep
= cp_lexer_peek_token (parser
->lexer
);
24934 while (sep
->type
== CPP_COMMA
)
24936 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
24937 identifier
= cp_parser_identifier (parser
);
24938 if (identifier
== error_mark_node
)
24941 list
= chainon (list
, build_tree_list (NULL_TREE
,
24943 sep
= cp_lexer_peek_token (parser
->lexer
);
24949 /* Parse an Objective-C alias declaration.
24951 objc-alias-declaration:
24952 @compatibility_alias identifier identifier ;
24954 This function registers the alias mapping with the Objective-C front end.
24955 It returns nothing. */
24958 cp_parser_objc_alias_declaration (cp_parser
* parser
)
24962 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
24963 alias
= cp_parser_identifier (parser
);
24964 orig
= cp_parser_identifier (parser
);
24965 objc_declare_alias (alias
, orig
);
24966 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24969 /* Parse an Objective-C class forward-declaration.
24971 objc-class-declaration:
24972 @class objc-identifier-list ;
24974 The function registers the forward declarations with the Objective-C
24975 front end. It returns nothing. */
24978 cp_parser_objc_class_declaration (cp_parser
* parser
)
24980 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
24985 id
= cp_parser_identifier (parser
);
24986 if (id
== error_mark_node
)
24989 objc_declare_class (id
);
24991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24992 cp_lexer_consume_token (parser
->lexer
);
24996 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24999 /* Parse a list of Objective-C protocol references.
25001 objc-protocol-refs-opt:
25002 objc-protocol-refs [opt]
25004 objc-protocol-refs:
25005 < objc-identifier-list >
25007 Returns a TREE_LIST of identifiers, if any. */
25010 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
25012 tree protorefs
= NULL_TREE
;
25014 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
25016 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
25017 protorefs
= cp_parser_objc_identifier_list (parser
);
25018 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
25024 /* Parse a Objective-C visibility specification. */
25027 cp_parser_objc_visibility_spec (cp_parser
* parser
)
25029 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
25031 switch (vis
->keyword
)
25033 case RID_AT_PRIVATE
:
25034 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
25036 case RID_AT_PROTECTED
:
25037 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
25039 case RID_AT_PUBLIC
:
25040 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
25042 case RID_AT_PACKAGE
:
25043 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
25049 /* Eat '@private'/'@protected'/'@public'. */
25050 cp_lexer_consume_token (parser
->lexer
);
25053 /* Parse an Objective-C method type. Return 'true' if it is a class
25054 (+) method, and 'false' if it is an instance (-) method. */
25057 cp_parser_objc_method_type (cp_parser
* parser
)
25059 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
25065 /* Parse an Objective-C protocol qualifier. */
25068 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
25070 tree quals
= NULL_TREE
, node
;
25071 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25073 node
= token
->u
.value
;
25075 while (node
&& identifier_p (node
)
25076 && (node
== ridpointers
[(int) RID_IN
]
25077 || node
== ridpointers
[(int) RID_OUT
]
25078 || node
== ridpointers
[(int) RID_INOUT
]
25079 || node
== ridpointers
[(int) RID_BYCOPY
]
25080 || node
== ridpointers
[(int) RID_BYREF
]
25081 || node
== ridpointers
[(int) RID_ONEWAY
]))
25083 quals
= tree_cons (NULL_TREE
, node
, quals
);
25084 cp_lexer_consume_token (parser
->lexer
);
25085 token
= cp_lexer_peek_token (parser
->lexer
);
25086 node
= token
->u
.value
;
25092 /* Parse an Objective-C typename. */
25095 cp_parser_objc_typename (cp_parser
* parser
)
25097 tree type_name
= NULL_TREE
;
25099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25101 tree proto_quals
, cp_type
= NULL_TREE
;
25103 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
25104 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
25106 /* An ObjC type name may consist of just protocol qualifiers, in which
25107 case the type shall default to 'id'. */
25108 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
25110 cp_type
= cp_parser_type_id (parser
);
25112 /* If the type could not be parsed, an error has already
25113 been produced. For error recovery, behave as if it had
25114 not been specified, which will use the default type
25116 if (cp_type
== error_mark_node
)
25118 cp_type
= NULL_TREE
;
25119 /* We need to skip to the closing parenthesis as
25120 cp_parser_type_id() does not seem to do it for
25122 cp_parser_skip_to_closing_parenthesis (parser
,
25123 /*recovering=*/true,
25124 /*or_comma=*/false,
25125 /*consume_paren=*/false);
25129 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25130 type_name
= build_tree_list (proto_quals
, cp_type
);
25136 /* Check to see if TYPE refers to an Objective-C selector name. */
25139 cp_parser_objc_selector_p (enum cpp_ttype type
)
25141 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
25142 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
25143 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
25144 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
25145 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
25148 /* Parse an Objective-C selector. */
25151 cp_parser_objc_selector (cp_parser
* parser
)
25153 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
25155 if (!cp_parser_objc_selector_p (token
->type
))
25157 error_at (token
->location
, "invalid Objective-C++ selector name");
25158 return error_mark_node
;
25161 /* C++ operator names are allowed to appear in ObjC selectors. */
25162 switch (token
->type
)
25164 case CPP_AND_AND
: return get_identifier ("and");
25165 case CPP_AND_EQ
: return get_identifier ("and_eq");
25166 case CPP_AND
: return get_identifier ("bitand");
25167 case CPP_OR
: return get_identifier ("bitor");
25168 case CPP_COMPL
: return get_identifier ("compl");
25169 case CPP_NOT
: return get_identifier ("not");
25170 case CPP_NOT_EQ
: return get_identifier ("not_eq");
25171 case CPP_OR_OR
: return get_identifier ("or");
25172 case CPP_OR_EQ
: return get_identifier ("or_eq");
25173 case CPP_XOR
: return get_identifier ("xor");
25174 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
25175 default: return token
->u
.value
;
25179 /* Parse an Objective-C params list. */
25182 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
25184 tree params
= NULL_TREE
;
25185 bool maybe_unary_selector_p
= true;
25186 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25188 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25190 tree selector
= NULL_TREE
, type_name
, identifier
;
25191 tree parm_attr
= NULL_TREE
;
25193 if (token
->keyword
== RID_ATTRIBUTE
)
25196 if (token
->type
!= CPP_COLON
)
25197 selector
= cp_parser_objc_selector (parser
);
25199 /* Detect if we have a unary selector. */
25200 if (maybe_unary_selector_p
25201 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25203 params
= selector
; /* Might be followed by attributes. */
25207 maybe_unary_selector_p
= false;
25208 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
25210 /* Something went quite wrong. There should be a colon
25211 here, but there is not. Stop parsing parameters. */
25214 type_name
= cp_parser_objc_typename (parser
);
25215 /* New ObjC allows attributes on parameters too. */
25216 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
25217 parm_attr
= cp_parser_attributes_opt (parser
);
25218 identifier
= cp_parser_identifier (parser
);
25222 objc_build_keyword_decl (selector
,
25227 token
= cp_lexer_peek_token (parser
->lexer
);
25230 if (params
== NULL_TREE
)
25232 cp_parser_error (parser
, "objective-c++ method declaration is expected");
25233 return error_mark_node
;
25236 /* We allow tail attributes for the method. */
25237 if (token
->keyword
== RID_ATTRIBUTE
)
25239 *attributes
= cp_parser_attributes_opt (parser
);
25240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25241 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25243 cp_parser_error (parser
,
25244 "method attributes must be specified at the end");
25245 return error_mark_node
;
25248 if (params
== NULL_TREE
)
25250 cp_parser_error (parser
, "objective-c++ method declaration is expected");
25251 return error_mark_node
;
25256 /* Parse the non-keyword Objective-C params. */
25259 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
25262 tree params
= make_node (TREE_LIST
);
25263 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25264 *ellipsisp
= false; /* Initially, assume no ellipsis. */
25266 while (token
->type
== CPP_COMMA
)
25268 cp_parameter_declarator
*parmdecl
;
25271 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25272 token
= cp_lexer_peek_token (parser
->lexer
);
25274 if (token
->type
== CPP_ELLIPSIS
)
25276 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
25278 token
= cp_lexer_peek_token (parser
->lexer
);
25282 /* TODO: parse attributes for tail parameters. */
25283 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
25284 parm
= grokdeclarator (parmdecl
->declarator
,
25285 &parmdecl
->decl_specifiers
,
25286 PARM
, /*initialized=*/0,
25287 /*attrlist=*/NULL
);
25289 chainon (params
, build_tree_list (NULL_TREE
, parm
));
25290 token
= cp_lexer_peek_token (parser
->lexer
);
25293 /* We allow tail attributes for the method. */
25294 if (token
->keyword
== RID_ATTRIBUTE
)
25296 if (*attributes
== NULL_TREE
)
25298 *attributes
= cp_parser_attributes_opt (parser
);
25299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25300 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25304 /* We have an error, but parse the attributes, so that we can
25306 *attributes
= cp_parser_attributes_opt (parser
);
25308 cp_parser_error (parser
,
25309 "method attributes must be specified at the end");
25310 return error_mark_node
;
25316 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25319 cp_parser_objc_interstitial_code (cp_parser
* parser
)
25321 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25323 /* If the next token is `extern' and the following token is a string
25324 literal, then we have a linkage specification. */
25325 if (token
->keyword
== RID_EXTERN
25326 && cp_parser_is_pure_string_literal
25327 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
25328 cp_parser_linkage_specification (parser
);
25329 /* Handle #pragma, if any. */
25330 else if (token
->type
== CPP_PRAGMA
)
25331 cp_parser_pragma (parser
, pragma_objc_icode
);
25332 /* Allow stray semicolons. */
25333 else if (token
->type
== CPP_SEMICOLON
)
25334 cp_lexer_consume_token (parser
->lexer
);
25335 /* Mark methods as optional or required, when building protocols. */
25336 else if (token
->keyword
== RID_AT_OPTIONAL
)
25338 cp_lexer_consume_token (parser
->lexer
);
25339 objc_set_method_opt (true);
25341 else if (token
->keyword
== RID_AT_REQUIRED
)
25343 cp_lexer_consume_token (parser
->lexer
);
25344 objc_set_method_opt (false);
25346 else if (token
->keyword
== RID_NAMESPACE
)
25347 cp_parser_namespace_definition (parser
);
25348 /* Other stray characters must generate errors. */
25349 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
25351 cp_lexer_consume_token (parser
->lexer
);
25352 error ("stray %qs between Objective-C++ methods",
25353 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
25355 /* Finally, try to parse a block-declaration, or a function-definition. */
25357 cp_parser_block_declaration (parser
, /*statement_p=*/false);
25360 /* Parse a method signature. */
25363 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
25365 tree rettype
, kwdparms
, optparms
;
25366 bool ellipsis
= false;
25367 bool is_class_method
;
25369 is_class_method
= cp_parser_objc_method_type (parser
);
25370 rettype
= cp_parser_objc_typename (parser
);
25371 *attributes
= NULL_TREE
;
25372 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
25373 if (kwdparms
== error_mark_node
)
25374 return error_mark_node
;
25375 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
25376 if (optparms
== error_mark_node
)
25377 return error_mark_node
;
25379 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
25383 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
25386 cp_lexer_save_tokens (parser
->lexer
);
25387 tattr
= cp_parser_attributes_opt (parser
);
25388 gcc_assert (tattr
) ;
25390 /* If the attributes are followed by a method introducer, this is not allowed.
25391 Dump the attributes and flag the situation. */
25392 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
25393 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
25396 /* Otherwise, the attributes introduce some interstitial code, possibly so
25397 rewind to allow that check. */
25398 cp_lexer_rollback_tokens (parser
->lexer
);
25402 /* Parse an Objective-C method prototype list. */
25405 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
25407 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25409 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25411 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
25413 tree attributes
, sig
;
25414 bool is_class_method
;
25415 if (token
->type
== CPP_PLUS
)
25416 is_class_method
= true;
25418 is_class_method
= false;
25419 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
25420 if (sig
== error_mark_node
)
25422 cp_parser_skip_to_end_of_block_or_statement (parser
);
25423 token
= cp_lexer_peek_token (parser
->lexer
);
25426 objc_add_method_declaration (is_class_method
, sig
, attributes
);
25427 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25429 else if (token
->keyword
== RID_AT_PROPERTY
)
25430 cp_parser_objc_at_property_declaration (parser
);
25431 else if (token
->keyword
== RID_ATTRIBUTE
25432 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25433 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25435 "prefix attributes are ignored for methods");
25437 /* Allow for interspersed non-ObjC++ code. */
25438 cp_parser_objc_interstitial_code (parser
);
25440 token
= cp_lexer_peek_token (parser
->lexer
);
25443 if (token
->type
!= CPP_EOF
)
25444 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25446 cp_parser_error (parser
, "expected %<@end%>");
25448 objc_finish_interface ();
25451 /* Parse an Objective-C method definition list. */
25454 cp_parser_objc_method_definition_list (cp_parser
* parser
)
25456 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25458 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25462 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
25465 tree sig
, attribute
;
25466 bool is_class_method
;
25467 if (token
->type
== CPP_PLUS
)
25468 is_class_method
= true;
25470 is_class_method
= false;
25471 push_deferring_access_checks (dk_deferred
);
25472 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
25473 if (sig
== error_mark_node
)
25475 cp_parser_skip_to_end_of_block_or_statement (parser
);
25476 token
= cp_lexer_peek_token (parser
->lexer
);
25479 objc_start_method_definition (is_class_method
, sig
, attribute
,
25482 /* For historical reasons, we accept an optional semicolon. */
25483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25484 cp_lexer_consume_token (parser
->lexer
);
25486 ptk
= cp_lexer_peek_token (parser
->lexer
);
25487 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
25488 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
25490 perform_deferred_access_checks (tf_warning_or_error
);
25491 stop_deferring_access_checks ();
25492 meth
= cp_parser_function_definition_after_declarator (parser
,
25494 pop_deferring_access_checks ();
25495 objc_finish_method_definition (meth
);
25498 /* The following case will be removed once @synthesize is
25499 completely implemented. */
25500 else if (token
->keyword
== RID_AT_PROPERTY
)
25501 cp_parser_objc_at_property_declaration (parser
);
25502 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
25503 cp_parser_objc_at_synthesize_declaration (parser
);
25504 else if (token
->keyword
== RID_AT_DYNAMIC
)
25505 cp_parser_objc_at_dynamic_declaration (parser
);
25506 else if (token
->keyword
== RID_ATTRIBUTE
25507 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25508 warning_at (token
->location
, OPT_Wattributes
,
25509 "prefix attributes are ignored for methods");
25511 /* Allow for interspersed non-ObjC++ code. */
25512 cp_parser_objc_interstitial_code (parser
);
25514 token
= cp_lexer_peek_token (parser
->lexer
);
25517 if (token
->type
!= CPP_EOF
)
25518 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25520 cp_parser_error (parser
, "expected %<@end%>");
25522 objc_finish_implementation ();
25525 /* Parse Objective-C ivars. */
25528 cp_parser_objc_class_ivars (cp_parser
* parser
)
25530 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25532 if (token
->type
!= CPP_OPEN_BRACE
)
25533 return; /* No ivars specified. */
25535 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
25536 token
= cp_lexer_peek_token (parser
->lexer
);
25538 while (token
->type
!= CPP_CLOSE_BRACE
25539 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25541 cp_decl_specifier_seq declspecs
;
25542 int decl_class_or_enum_p
;
25543 tree prefix_attributes
;
25545 cp_parser_objc_visibility_spec (parser
);
25547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25550 cp_parser_decl_specifier_seq (parser
,
25551 CP_PARSER_FLAGS_OPTIONAL
,
25553 &decl_class_or_enum_p
);
25555 /* auto, register, static, extern, mutable. */
25556 if (declspecs
.storage_class
!= sc_none
)
25558 cp_parser_error (parser
, "invalid type for instance variable");
25559 declspecs
.storage_class
= sc_none
;
25562 /* thread_local. */
25563 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
25565 cp_parser_error (parser
, "invalid type for instance variable");
25566 declspecs
.locations
[ds_thread
] = 0;
25570 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
25572 cp_parser_error (parser
, "invalid type for instance variable");
25573 declspecs
.locations
[ds_typedef
] = 0;
25576 prefix_attributes
= declspecs
.attributes
;
25577 declspecs
.attributes
= NULL_TREE
;
25579 /* Keep going until we hit the `;' at the end of the
25581 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25583 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
25584 cp_declarator
*declarator
= NULL
;
25585 int ctor_dtor_or_conv_p
;
25587 /* Check for a (possibly unnamed) bitfield declaration. */
25588 token
= cp_lexer_peek_token (parser
->lexer
);
25589 if (token
->type
== CPP_COLON
)
25592 if (token
->type
== CPP_NAME
25593 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
25596 /* Get the name of the bitfield. */
25597 declarator
= make_id_declarator (NULL_TREE
,
25598 cp_parser_identifier (parser
),
25602 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
25603 /* Get the width of the bitfield. */
25605 = cp_parser_constant_expression (parser
,
25606 /*allow_non_constant=*/false,
25611 /* Parse the declarator. */
25613 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25614 &ctor_dtor_or_conv_p
,
25615 /*parenthesized_p=*/NULL
,
25616 /*member_p=*/false);
25619 /* Look for attributes that apply to the ivar. */
25620 attributes
= cp_parser_attributes_opt (parser
);
25621 /* Remember which attributes are prefix attributes and
25623 first_attribute
= attributes
;
25624 /* Combine the attributes. */
25625 attributes
= chainon (prefix_attributes
, attributes
);
25628 /* Create the bitfield declaration. */
25629 decl
= grokbitfield (declarator
, &declspecs
,
25633 decl
= grokfield (declarator
, &declspecs
,
25634 NULL_TREE
, /*init_const_expr_p=*/false,
25635 NULL_TREE
, attributes
);
25637 /* Add the instance variable. */
25638 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
25639 objc_add_instance_variable (decl
);
25641 /* Reset PREFIX_ATTRIBUTES. */
25642 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
25643 attributes
= TREE_CHAIN (attributes
);
25645 TREE_CHAIN (attributes
) = NULL_TREE
;
25647 token
= cp_lexer_peek_token (parser
->lexer
);
25649 if (token
->type
== CPP_COMMA
)
25651 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25657 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25658 token
= cp_lexer_peek_token (parser
->lexer
);
25661 if (token
->keyword
== RID_AT_END
)
25662 cp_parser_error (parser
, "expected %<}%>");
25664 /* Do not consume the RID_AT_END, so it will be read again as terminating
25665 the @interface of @implementation. */
25666 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25667 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
25669 /* For historical reasons, we accept an optional semicolon. */
25670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25671 cp_lexer_consume_token (parser
->lexer
);
25674 /* Parse an Objective-C protocol declaration. */
25677 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
25679 tree proto
, protorefs
;
25682 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25683 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
25685 tok
= cp_lexer_peek_token (parser
->lexer
);
25686 error_at (tok
->location
, "identifier expected after %<@protocol%>");
25687 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25691 /* See if we have a forward declaration or a definition. */
25692 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25694 /* Try a forward declaration first. */
25695 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
25701 id
= cp_parser_identifier (parser
);
25702 if (id
== error_mark_node
)
25705 objc_declare_protocol (id
, attributes
);
25707 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25708 cp_lexer_consume_token (parser
->lexer
);
25712 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25715 /* Ok, we got a full-fledged definition (or at least should). */
25718 proto
= cp_parser_identifier (parser
);
25719 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
25720 objc_start_protocol (proto
, protorefs
, attributes
);
25721 cp_parser_objc_method_prototype_list (parser
);
25725 /* Parse an Objective-C superclass or category. */
25728 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
25731 tree
*categ
, bool *is_class_extension
)
25733 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
25735 *super
= *categ
= NULL_TREE
;
25736 *is_class_extension
= false;
25737 if (next
->type
== CPP_COLON
)
25739 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
25740 *super
= cp_parser_identifier (parser
);
25742 else if (next
->type
== CPP_OPEN_PAREN
)
25744 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
25746 /* If there is no category name, and this is an @interface, we
25747 have a class extension. */
25748 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25750 *categ
= NULL_TREE
;
25751 *is_class_extension
= true;
25754 *categ
= cp_parser_identifier (parser
);
25756 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25760 /* Parse an Objective-C class interface. */
25763 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
25765 tree name
, super
, categ
, protos
;
25766 bool is_class_extension
;
25768 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
25769 name
= cp_parser_identifier (parser
);
25770 if (name
== error_mark_node
)
25772 /* It's hard to recover because even if valid @interface stuff
25773 is to follow, we can't compile it (or validate it) if we
25774 don't even know which class it refers to. Let's assume this
25775 was a stray '@interface' token in the stream and skip it.
25779 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
25780 &is_class_extension
);
25781 protos
= cp_parser_objc_protocol_refs_opt (parser
);
25783 /* We have either a class or a category on our hands. */
25784 if (categ
|| is_class_extension
)
25785 objc_start_category_interface (name
, categ
, protos
, attributes
);
25788 objc_start_class_interface (name
, super
, protos
, attributes
);
25789 /* Handle instance variable declarations, if any. */
25790 cp_parser_objc_class_ivars (parser
);
25791 objc_continue_interface ();
25794 cp_parser_objc_method_prototype_list (parser
);
25797 /* Parse an Objective-C class implementation. */
25800 cp_parser_objc_class_implementation (cp_parser
* parser
)
25802 tree name
, super
, categ
;
25803 bool is_class_extension
;
25805 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
25806 name
= cp_parser_identifier (parser
);
25807 if (name
== error_mark_node
)
25809 /* It's hard to recover because even if valid @implementation
25810 stuff is to follow, we can't compile it (or validate it) if
25811 we don't even know which class it refers to. Let's assume
25812 this was a stray '@implementation' token in the stream and
25817 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
25818 &is_class_extension
);
25820 /* We have either a class or a category on our hands. */
25822 objc_start_category_implementation (name
, categ
);
25825 objc_start_class_implementation (name
, super
);
25826 /* Handle instance variable declarations, if any. */
25827 cp_parser_objc_class_ivars (parser
);
25828 objc_continue_implementation ();
25831 cp_parser_objc_method_definition_list (parser
);
25834 /* Consume the @end token and finish off the implementation. */
25837 cp_parser_objc_end_implementation (cp_parser
* parser
)
25839 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25840 objc_finish_implementation ();
25843 /* Parse an Objective-C declaration. */
25846 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
25848 /* Try to figure out what kind of declaration is present. */
25849 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25852 switch (kwd
->keyword
)
25857 error_at (kwd
->location
, "attributes may not be specified before"
25858 " the %<@%D%> Objective-C++ keyword",
25862 case RID_AT_IMPLEMENTATION
:
25863 warning_at (kwd
->location
, OPT_Wattributes
,
25864 "prefix attributes are ignored before %<@%D%>",
25871 switch (kwd
->keyword
)
25874 cp_parser_objc_alias_declaration (parser
);
25877 cp_parser_objc_class_declaration (parser
);
25879 case RID_AT_PROTOCOL
:
25880 cp_parser_objc_protocol_declaration (parser
, attributes
);
25882 case RID_AT_INTERFACE
:
25883 cp_parser_objc_class_interface (parser
, attributes
);
25885 case RID_AT_IMPLEMENTATION
:
25886 cp_parser_objc_class_implementation (parser
);
25889 cp_parser_objc_end_implementation (parser
);
25892 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
25894 cp_parser_skip_to_end_of_block_or_statement (parser
);
25898 /* Parse an Objective-C try-catch-finally statement.
25900 objc-try-catch-finally-stmt:
25901 @try compound-statement objc-catch-clause-seq [opt]
25902 objc-finally-clause [opt]
25904 objc-catch-clause-seq:
25905 objc-catch-clause objc-catch-clause-seq [opt]
25908 @catch ( objc-exception-declaration ) compound-statement
25910 objc-finally-clause:
25911 @finally compound-statement
25913 objc-exception-declaration:
25914 parameter-declaration
25917 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25921 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25922 for C. Keep them in sync. */
25925 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
25927 location_t location
;
25930 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
25931 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
25932 objc_maybe_warn_exceptions (location
);
25933 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25934 node, lest it get absorbed into the surrounding block. */
25935 stmt
= push_stmt_list ();
25936 cp_parser_compound_statement (parser
, NULL
, false, false);
25937 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
25939 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
25941 cp_parameter_declarator
*parm
;
25942 tree parameter_declaration
= error_mark_node
;
25943 bool seen_open_paren
= false;
25945 cp_lexer_consume_token (parser
->lexer
);
25946 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25947 seen_open_paren
= true;
25948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25950 /* We have "@catch (...)" (where the '...' are literally
25951 what is in the code). Skip the '...'.
25952 parameter_declaration is set to NULL_TREE, and
25953 objc_being_catch_clauses() knows that that means
25955 cp_lexer_consume_token (parser
->lexer
);
25956 parameter_declaration
= NULL_TREE
;
25960 /* We have "@catch (NSException *exception)" or something
25961 like that. Parse the parameter declaration. */
25962 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
25964 parameter_declaration
= error_mark_node
;
25966 parameter_declaration
= grokdeclarator (parm
->declarator
,
25967 &parm
->decl_specifiers
,
25968 PARM
, /*initialized=*/0,
25969 /*attrlist=*/NULL
);
25971 if (seen_open_paren
)
25972 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25975 /* If there was no open parenthesis, we are recovering from
25976 an error, and we are trying to figure out what mistake
25977 the user has made. */
25979 /* If there is an immediate closing parenthesis, the user
25980 probably forgot the opening one (ie, they typed "@catch
25981 NSException *e)". Parse the closing parenthesis and keep
25983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25984 cp_lexer_consume_token (parser
->lexer
);
25986 /* If these is no immediate closing parenthesis, the user
25987 probably doesn't know that parenthesis are required at
25988 all (ie, they typed "@catch NSException *e"). So, just
25989 forget about the closing parenthesis and keep going. */
25991 objc_begin_catch_clause (parameter_declaration
);
25992 cp_parser_compound_statement (parser
, NULL
, false, false);
25993 objc_finish_catch_clause ();
25995 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
25997 cp_lexer_consume_token (parser
->lexer
);
25998 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
25999 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26000 node, lest it get absorbed into the surrounding block. */
26001 stmt
= push_stmt_list ();
26002 cp_parser_compound_statement (parser
, NULL
, false, false);
26003 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
26006 return objc_finish_try_stmt ();
26009 /* Parse an Objective-C synchronized statement.
26011 objc-synchronized-stmt:
26012 @synchronized ( expression ) compound-statement
26014 Returns NULL_TREE. */
26017 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
26019 location_t location
;
26022 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
26024 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26025 objc_maybe_warn_exceptions (location
);
26026 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
26027 lock
= cp_parser_expression (parser
, false, NULL
);
26028 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26030 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26031 node, lest it get absorbed into the surrounding block. */
26032 stmt
= push_stmt_list ();
26033 cp_parser_compound_statement (parser
, NULL
, false, false);
26035 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
26038 /* Parse an Objective-C throw statement.
26041 @throw assignment-expression [opt] ;
26043 Returns a constructed '@throw' statement. */
26046 cp_parser_objc_throw_statement (cp_parser
*parser
)
26048 tree expr
= NULL_TREE
;
26049 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26051 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
26053 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26054 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
26056 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26058 return objc_build_throw_stmt (loc
, expr
);
26061 /* Parse an Objective-C statement. */
26064 cp_parser_objc_statement (cp_parser
* parser
)
26066 /* Try to figure out what kind of declaration is present. */
26067 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26069 switch (kwd
->keyword
)
26072 return cp_parser_objc_try_catch_finally_statement (parser
);
26073 case RID_AT_SYNCHRONIZED
:
26074 return cp_parser_objc_synchronized_statement (parser
);
26076 return cp_parser_objc_throw_statement (parser
);
26078 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26080 cp_parser_skip_to_end_of_block_or_statement (parser
);
26083 return error_mark_node
;
26086 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26087 look ahead to see if an objc keyword follows the attributes. This
26088 is to detect the use of prefix attributes on ObjC @interface and
26092 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
26094 cp_lexer_save_tokens (parser
->lexer
);
26095 *attrib
= cp_parser_attributes_opt (parser
);
26096 gcc_assert (*attrib
);
26097 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
26099 cp_lexer_commit_tokens (parser
->lexer
);
26102 cp_lexer_rollback_tokens (parser
->lexer
);
26106 /* This routine is a minimal replacement for
26107 c_parser_struct_declaration () used when parsing the list of
26108 types/names or ObjC++ properties. For example, when parsing the
26111 @property (readonly) int a, b, c;
26113 this function is responsible for parsing "int a, int b, int c" and
26114 returning the declarations as CHAIN of DECLs.
26116 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26117 similar parsing. */
26119 cp_parser_objc_struct_declaration (cp_parser
*parser
)
26121 tree decls
= NULL_TREE
;
26122 cp_decl_specifier_seq declspecs
;
26123 int decl_class_or_enum_p
;
26124 tree prefix_attributes
;
26126 cp_parser_decl_specifier_seq (parser
,
26127 CP_PARSER_FLAGS_NONE
,
26129 &decl_class_or_enum_p
);
26131 if (declspecs
.type
== error_mark_node
)
26132 return error_mark_node
;
26134 /* auto, register, static, extern, mutable. */
26135 if (declspecs
.storage_class
!= sc_none
)
26137 cp_parser_error (parser
, "invalid type for property");
26138 declspecs
.storage_class
= sc_none
;
26141 /* thread_local. */
26142 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26144 cp_parser_error (parser
, "invalid type for property");
26145 declspecs
.locations
[ds_thread
] = 0;
26149 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26151 cp_parser_error (parser
, "invalid type for property");
26152 declspecs
.locations
[ds_typedef
] = 0;
26155 prefix_attributes
= declspecs
.attributes
;
26156 declspecs
.attributes
= NULL_TREE
;
26158 /* Keep going until we hit the `;' at the end of the declaration. */
26159 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26161 tree attributes
, first_attribute
, decl
;
26162 cp_declarator
*declarator
;
26165 /* Parse the declarator. */
26166 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26167 NULL
, NULL
, false);
26169 /* Look for attributes that apply to the ivar. */
26170 attributes
= cp_parser_attributes_opt (parser
);
26171 /* Remember which attributes are prefix attributes and
26173 first_attribute
= attributes
;
26174 /* Combine the attributes. */
26175 attributes
= chainon (prefix_attributes
, attributes
);
26177 decl
= grokfield (declarator
, &declspecs
,
26178 NULL_TREE
, /*init_const_expr_p=*/false,
26179 NULL_TREE
, attributes
);
26181 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
26182 return error_mark_node
;
26184 /* Reset PREFIX_ATTRIBUTES. */
26185 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26186 attributes
= TREE_CHAIN (attributes
);
26188 TREE_CHAIN (attributes
) = NULL_TREE
;
26190 DECL_CHAIN (decl
) = decls
;
26193 token
= cp_lexer_peek_token (parser
->lexer
);
26194 if (token
->type
== CPP_COMMA
)
26196 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26205 /* Parse an Objective-C @property declaration. The syntax is:
26207 objc-property-declaration:
26208 '@property' objc-property-attributes[opt] struct-declaration ;
26210 objc-property-attributes:
26211 '(' objc-property-attribute-list ')'
26213 objc-property-attribute-list:
26214 objc-property-attribute
26215 objc-property-attribute-list, objc-property-attribute
26217 objc-property-attribute
26218 'getter' = identifier
26219 'setter' = identifier
26228 @property NSString *name;
26229 @property (readonly) id object;
26230 @property (retain, nonatomic, getter=getTheName) id name;
26231 @property int a, b, c;
26233 PS: This function is identical to
26234 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26236 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
26238 /* The following variables hold the attributes of the properties as
26239 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26240 seen. When we see an attribute, we set them to 'true' (if they
26241 are boolean properties) or to the identifier (if they have an
26242 argument, ie, for getter and setter). Note that here we only
26243 parse the list of attributes, check the syntax and accumulate the
26244 attributes that we find. objc_add_property_declaration() will
26245 then process the information. */
26246 bool property_assign
= false;
26247 bool property_copy
= false;
26248 tree property_getter_ident
= NULL_TREE
;
26249 bool property_nonatomic
= false;
26250 bool property_readonly
= false;
26251 bool property_readwrite
= false;
26252 bool property_retain
= false;
26253 tree property_setter_ident
= NULL_TREE
;
26255 /* 'properties' is the list of properties that we read. Usually a
26256 single one, but maybe more (eg, in "@property int a, b, c;" there
26261 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26263 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
26265 /* Parse the optional attribute list... */
26266 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26269 cp_lexer_consume_token (parser
->lexer
);
26273 bool syntax_error
= false;
26274 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26277 if (token
->type
!= CPP_NAME
)
26279 cp_parser_error (parser
, "expected identifier");
26282 keyword
= C_RID_CODE (token
->u
.value
);
26283 cp_lexer_consume_token (parser
->lexer
);
26286 case RID_ASSIGN
: property_assign
= true; break;
26287 case RID_COPY
: property_copy
= true; break;
26288 case RID_NONATOMIC
: property_nonatomic
= true; break;
26289 case RID_READONLY
: property_readonly
= true; break;
26290 case RID_READWRITE
: property_readwrite
= true; break;
26291 case RID_RETAIN
: property_retain
= true; break;
26295 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
26297 if (keyword
== RID_GETTER
)
26298 cp_parser_error (parser
,
26299 "missing %<=%> (after %<getter%> attribute)");
26301 cp_parser_error (parser
,
26302 "missing %<=%> (after %<setter%> attribute)");
26303 syntax_error
= true;
26306 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
26307 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
26309 cp_parser_error (parser
, "expected identifier");
26310 syntax_error
= true;
26313 if (keyword
== RID_SETTER
)
26315 if (property_setter_ident
!= NULL_TREE
)
26317 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
26318 cp_lexer_consume_token (parser
->lexer
);
26321 property_setter_ident
= cp_parser_objc_selector (parser
);
26322 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26323 cp_parser_error (parser
, "setter name must terminate with %<:%>");
26325 cp_lexer_consume_token (parser
->lexer
);
26329 if (property_getter_ident
!= NULL_TREE
)
26331 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
26332 cp_lexer_consume_token (parser
->lexer
);
26335 property_getter_ident
= cp_parser_objc_selector (parser
);
26339 cp_parser_error (parser
, "unknown property attribute");
26340 syntax_error
= true;
26347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26348 cp_lexer_consume_token (parser
->lexer
);
26353 /* FIXME: "@property (setter, assign);" will generate a spurious
26354 "error: expected ‘)’ before ‘,’ token". This is because
26355 cp_parser_require, unlike the C counterpart, will produce an
26356 error even if we are in error recovery. */
26357 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26359 cp_parser_skip_to_closing_parenthesis (parser
,
26360 /*recovering=*/true,
26361 /*or_comma=*/false,
26362 /*consume_paren=*/true);
26366 /* ... and the property declaration(s). */
26367 properties
= cp_parser_objc_struct_declaration (parser
);
26369 if (properties
== error_mark_node
)
26371 cp_parser_skip_to_end_of_statement (parser
);
26372 /* If the next token is now a `;', consume it. */
26373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26374 cp_lexer_consume_token (parser
->lexer
);
26378 if (properties
== NULL_TREE
)
26379 cp_parser_error (parser
, "expected identifier");
26382 /* Comma-separated properties are chained together in
26383 reverse order; add them one by one. */
26384 properties
= nreverse (properties
);
26386 for (; properties
; properties
= TREE_CHAIN (properties
))
26387 objc_add_property_declaration (loc
, copy_node (properties
),
26388 property_readonly
, property_readwrite
,
26389 property_assign
, property_retain
,
26390 property_copy
, property_nonatomic
,
26391 property_getter_ident
, property_setter_ident
);
26394 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26397 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26399 objc-synthesize-declaration:
26400 @synthesize objc-synthesize-identifier-list ;
26402 objc-synthesize-identifier-list:
26403 objc-synthesize-identifier
26404 objc-synthesize-identifier-list, objc-synthesize-identifier
26406 objc-synthesize-identifier
26408 identifier = identifier
26411 @synthesize MyProperty;
26412 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26414 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26415 for C. Keep them in sync.
26418 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
26420 tree list
= NULL_TREE
;
26422 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26424 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
26427 tree property
, ivar
;
26428 property
= cp_parser_identifier (parser
);
26429 if (property
== error_mark_node
)
26431 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
26436 cp_lexer_consume_token (parser
->lexer
);
26437 ivar
= cp_parser_identifier (parser
);
26438 if (ivar
== error_mark_node
)
26440 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26446 list
= chainon (list
, build_tree_list (ivar
, property
));
26447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26448 cp_lexer_consume_token (parser
->lexer
);
26452 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26453 objc_add_synthesize_declaration (loc
, list
);
26456 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26458 objc-dynamic-declaration:
26459 @dynamic identifier-list ;
26462 @dynamic MyProperty;
26463 @dynamic MyProperty, AnotherProperty;
26465 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26466 for C. Keep them in sync.
26469 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
26471 tree list
= NULL_TREE
;
26473 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26475 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
26479 property
= cp_parser_identifier (parser
);
26480 if (property
== error_mark_node
)
26482 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26485 list
= chainon (list
, build_tree_list (NULL
, property
));
26486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26487 cp_lexer_consume_token (parser
->lexer
);
26491 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26492 objc_add_dynamic_declaration (loc
, list
);
26496 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26498 /* Returns name of the next clause.
26499 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26500 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26501 returned and the token is consumed. */
26503 static pragma_omp_clause
26504 cp_parser_omp_clause_name (cp_parser
*parser
)
26506 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
26508 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
26509 result
= PRAGMA_OMP_CLAUSE_IF
;
26510 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
26511 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
26512 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
26513 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
26514 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
26515 result
= PRAGMA_OMP_CLAUSE_FOR
;
26516 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26518 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26519 const char *p
= IDENTIFIER_POINTER (id
);
26524 if (!strcmp ("aligned", p
))
26525 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
26528 if (!strcmp ("collapse", p
))
26529 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
26530 else if (!strcmp ("copyin", p
))
26531 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
26532 else if (!strcmp ("copyprivate", p
))
26533 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
26536 if (!strcmp ("depend", p
))
26537 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
26538 else if (!strcmp ("device", p
))
26539 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
26540 else if (!strcmp ("dist_schedule", p
))
26541 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
26544 if (!strcmp ("final", p
))
26545 result
= PRAGMA_OMP_CLAUSE_FINAL
;
26546 else if (!strcmp ("firstprivate", p
))
26547 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
26548 else if (!strcmp ("from", p
))
26549 result
= PRAGMA_OMP_CLAUSE_FROM
;
26552 if (!strcmp ("inbranch", p
))
26553 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
26556 if (!strcmp ("lastprivate", p
))
26557 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
26558 else if (!strcmp ("linear", p
))
26559 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
26562 if (!strcmp ("map", p
))
26563 result
= PRAGMA_OMP_CLAUSE_MAP
;
26564 else if (!strcmp ("mergeable", p
))
26565 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
26568 if (!strcmp ("notinbranch", p
))
26569 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
26570 else if (!strcmp ("nowait", p
))
26571 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
26572 else if (!strcmp ("num_teams", p
))
26573 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
26574 else if (!strcmp ("num_threads", p
))
26575 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
26578 if (!strcmp ("ordered", p
))
26579 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
26582 if (!strcmp ("parallel", p
))
26583 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
26584 else if (!strcmp ("proc_bind", p
))
26585 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
26588 if (!strcmp ("reduction", p
))
26589 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
26592 if (!strcmp ("safelen", p
))
26593 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
26594 else if (!strcmp ("schedule", p
))
26595 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
26596 else if (!strcmp ("sections", p
))
26597 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
26598 else if (!strcmp ("shared", p
))
26599 result
= PRAGMA_OMP_CLAUSE_SHARED
;
26600 else if (!strcmp ("simdlen", p
))
26601 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
26604 if (!strcmp ("taskgroup", p
))
26605 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
26606 else if (!strcmp ("thread_limit", p
))
26607 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
26608 else if (!strcmp ("to", p
))
26609 result
= PRAGMA_OMP_CLAUSE_TO
;
26612 if (!strcmp ("uniform", p
))
26613 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
26614 else if (!strcmp ("untied", p
))
26615 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
26620 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
26621 cp_lexer_consume_token (parser
->lexer
);
26626 /* Validate that a clause of the given type does not already exist. */
26629 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
26630 const char *name
, location_t location
)
26634 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
26635 if (OMP_CLAUSE_CODE (c
) == code
)
26637 error_at (location
, "too many %qs clauses", name
);
26645 variable-list , identifier
26647 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
26648 colon). An opening parenthesis will have been consumed by the caller.
26650 If KIND is nonzero, create the appropriate node and install the decl
26651 in OMP_CLAUSE_DECL and add the node to the head of the list.
26653 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26654 return the list created.
26656 COLON can be NULL if only closing parenthesis should end the list,
26657 or pointer to bool which will receive false if the list is terminated
26658 by closing parenthesis or true if the list is terminated by colon. */
26661 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
26662 tree list
, bool *colon
)
26665 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
26668 parser
->colon_corrects_to_scope_p
= false;
26675 token
= cp_lexer_peek_token (parser
->lexer
);
26676 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
26677 /*check_dependency_p=*/true,
26678 /*template_p=*/NULL
,
26679 /*declarator_p=*/false,
26680 /*optional_p=*/false);
26681 if (name
== error_mark_node
)
26684 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
26685 if (decl
== error_mark_node
)
26686 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
26688 else if (kind
!= 0)
26692 case OMP_CLAUSE_MAP
:
26693 case OMP_CLAUSE_FROM
:
26694 case OMP_CLAUSE_TO
:
26695 case OMP_CLAUSE_DEPEND
:
26696 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
26698 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
26700 parser
->colon_corrects_to_scope_p
= false;
26701 cp_lexer_consume_token (parser
->lexer
);
26702 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26703 low_bound
= cp_parser_expression (parser
, /*cast_p=*/false,
26706 parser
->colon_corrects_to_scope_p
26707 = saved_colon_corrects_to_scope_p
;
26708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
26709 length
= integer_one_node
;
26712 /* Look for `:'. */
26713 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26715 if (!cp_lexer_next_token_is (parser
->lexer
,
26717 length
= cp_parser_expression (parser
,
26721 /* Look for the closing `]'. */
26722 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
26725 decl
= tree_cons (low_bound
, length
, decl
);
26732 tree u
= build_omp_clause (token
->location
, kind
);
26733 OMP_CLAUSE_DECL (u
) = decl
;
26734 OMP_CLAUSE_CHAIN (u
) = list
;
26738 list
= tree_cons (decl
, NULL_TREE
, list
);
26741 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26743 cp_lexer_consume_token (parser
->lexer
);
26747 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
26749 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26752 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
26756 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26760 /* Try to resync to an unnested comma. Copied from
26761 cp_parser_parenthesized_expression_list. */
26764 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
26765 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
26766 /*recovering=*/true,
26768 /*consume_paren=*/true);
26776 /* Similarly, but expect leading and trailing parenthesis. This is a very
26777 common case for omp clauses. */
26780 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
26782 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26783 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
26788 collapse ( constant-expression ) */
26791 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
26797 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26798 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26801 num
= cp_parser_constant_expression (parser
, false, NULL
);
26803 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26804 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26805 /*or_comma=*/false,
26806 /*consume_paren=*/true);
26808 if (num
== error_mark_node
)
26810 num
= fold_non_dependent_expr (num
);
26811 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
26812 || !host_integerp (num
, 0)
26813 || (n
= tree_low_cst (num
, 0)) <= 0
26816 error_at (loc
, "collapse argument needs positive constant integer expression");
26820 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
26821 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
26822 OMP_CLAUSE_CHAIN (c
) = list
;
26823 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
26829 default ( shared | none ) */
26832 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
26834 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
26837 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26841 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26842 const char *p
= IDENTIFIER_POINTER (id
);
26847 if (strcmp ("none", p
) != 0)
26849 kind
= OMP_CLAUSE_DEFAULT_NONE
;
26853 if (strcmp ("shared", p
) != 0)
26855 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
26862 cp_lexer_consume_token (parser
->lexer
);
26867 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
26870 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26871 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26872 /*or_comma=*/false,
26873 /*consume_paren=*/true);
26875 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
26878 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
26879 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
26880 OMP_CLAUSE_CHAIN (c
) = list
;
26881 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
26887 final ( expression ) */
26890 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
26894 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26897 t
= cp_parser_condition (parser
);
26899 if (t
== error_mark_node
26900 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26901 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26902 /*or_comma=*/false,
26903 /*consume_paren=*/true);
26905 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
26907 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
26908 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
26909 OMP_CLAUSE_CHAIN (c
) = list
;
26915 if ( expression ) */
26918 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
26922 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26925 t
= cp_parser_condition (parser
);
26927 if (t
== error_mark_node
26928 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26929 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26930 /*or_comma=*/false,
26931 /*consume_paren=*/true);
26933 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
26935 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
26936 OMP_CLAUSE_IF_EXPR (c
) = t
;
26937 OMP_CLAUSE_CHAIN (c
) = list
;
26946 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
26947 tree list
, location_t location
)
26951 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
26954 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
26955 OMP_CLAUSE_CHAIN (c
) = list
;
26963 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
26964 tree list
, location_t location
)
26968 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
26970 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
26971 OMP_CLAUSE_CHAIN (c
) = list
;
26976 num_threads ( expression ) */
26979 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
26980 location_t location
)
26984 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26987 t
= cp_parser_expression (parser
, false, NULL
);
26989 if (t
== error_mark_node
26990 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26991 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26992 /*or_comma=*/false,
26993 /*consume_paren=*/true);
26995 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
26996 "num_threads", location
);
26998 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
26999 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
27000 OMP_CLAUSE_CHAIN (c
) = list
;
27009 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
27010 tree list
, location_t location
)
27014 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
27015 "ordered", location
);
27017 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
27018 OMP_CLAUSE_CHAIN (c
) = list
;
27023 reduction ( reduction-operator : variable-list )
27025 reduction-operator:
27026 One of: + * - & ^ | && ||
27030 reduction-operator:
27031 One of: + * - & ^ | && || min max
27035 reduction-operator:
27036 One of: + * - & ^ | && ||
27040 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
27042 enum tree_code code
= ERROR_MARK
;
27043 tree nlist
, c
, id
= NULL_TREE
;
27045 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27048 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27050 case CPP_PLUS
: code
= PLUS_EXPR
; break;
27051 case CPP_MULT
: code
= MULT_EXPR
; break;
27052 case CPP_MINUS
: code
= MINUS_EXPR
; break;
27053 case CPP_AND
: code
= BIT_AND_EXPR
; break;
27054 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
27055 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
27056 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
27057 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
27061 if (code
!= ERROR_MARK
)
27062 cp_lexer_consume_token (parser
->lexer
);
27065 bool saved_colon_corrects_to_scope_p
;
27066 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27067 parser
->colon_corrects_to_scope_p
= false;
27068 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
27069 /*check_dependency_p=*/true,
27070 /*template_p=*/NULL
,
27071 /*declarator_p=*/false,
27072 /*optional_p=*/false);
27073 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27074 if (identifier_p (id
))
27076 const char *p
= IDENTIFIER_POINTER (id
);
27078 if (strcmp (p
, "min") == 0)
27080 else if (strcmp (p
, "max") == 0)
27082 else if (id
== ansi_opname (PLUS_EXPR
))
27084 else if (id
== ansi_opname (MULT_EXPR
))
27086 else if (id
== ansi_opname (MINUS_EXPR
))
27088 else if (id
== ansi_opname (BIT_AND_EXPR
))
27089 code
= BIT_AND_EXPR
;
27090 else if (id
== ansi_opname (BIT_IOR_EXPR
))
27091 code
= BIT_IOR_EXPR
;
27092 else if (id
== ansi_opname (BIT_XOR_EXPR
))
27093 code
= BIT_XOR_EXPR
;
27094 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
27095 code
= TRUTH_ANDIF_EXPR
;
27096 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
27097 code
= TRUTH_ORIF_EXPR
;
27098 id
= omp_reduction_id (code
, id
, NULL_TREE
);
27099 tree scope
= parser
->scope
;
27101 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
27102 parser
->scope
= NULL_TREE
;
27103 parser
->qualifying_scope
= NULL_TREE
;
27104 parser
->object_scope
= NULL_TREE
;
27108 error ("invalid reduction-identifier");
27110 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27111 /*or_comma=*/false,
27112 /*consume_paren=*/true);
27117 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27120 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
27122 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27124 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
27125 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
27132 schedule ( schedule-kind )
27133 schedule ( schedule-kind , expression )
27136 static | dynamic | guided | runtime | auto */
27139 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
27143 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27146 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
27148 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27150 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27151 const char *p
= IDENTIFIER_POINTER (id
);
27156 if (strcmp ("dynamic", p
) != 0)
27158 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
27162 if (strcmp ("guided", p
) != 0)
27164 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
27168 if (strcmp ("runtime", p
) != 0)
27170 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
27177 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
27178 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
27179 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
27180 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
27183 cp_lexer_consume_token (parser
->lexer
);
27185 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27188 cp_lexer_consume_token (parser
->lexer
);
27190 token
= cp_lexer_peek_token (parser
->lexer
);
27191 t
= cp_parser_assignment_expression (parser
, false, NULL
);
27193 if (t
== error_mark_node
)
27195 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
27196 error_at (token
->location
, "schedule %<runtime%> does not take "
27197 "a %<chunk_size%> parameter");
27198 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
27199 error_at (token
->location
, "schedule %<auto%> does not take "
27200 "a %<chunk_size%> parameter");
27202 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
27204 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27207 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
27210 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
27211 OMP_CLAUSE_CHAIN (c
) = list
;
27215 cp_parser_error (parser
, "invalid schedule kind");
27217 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27218 /*or_comma=*/false,
27219 /*consume_paren=*/true);
27227 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
27228 tree list
, location_t location
)
27232 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
27234 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
27235 OMP_CLAUSE_CHAIN (c
) = list
;
27244 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
27245 tree list
, location_t location
)
27247 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
27248 tree c
= build_omp_clause (location
, code
);
27249 OMP_CLAUSE_CHAIN (c
) = list
;
27260 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
27261 enum omp_clause_code code
,
27262 tree list
, location_t location
)
27264 tree c
= build_omp_clause (location
, code
);
27265 OMP_CLAUSE_CHAIN (c
) = list
;
27270 num_teams ( expression ) */
27273 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
27274 location_t location
)
27278 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27281 t
= cp_parser_expression (parser
, false, NULL
);
27283 if (t
== error_mark_node
27284 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27285 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27286 /*or_comma=*/false,
27287 /*consume_paren=*/true);
27289 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
27290 "num_teams", location
);
27292 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
27293 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
27294 OMP_CLAUSE_CHAIN (c
) = list
;
27300 thread_limit ( expression ) */
27303 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
27304 location_t location
)
27308 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27311 t
= cp_parser_expression (parser
, false, NULL
);
27313 if (t
== error_mark_node
27314 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27315 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27316 /*or_comma=*/false,
27317 /*consume_paren=*/true);
27319 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
27320 "thread_limit", location
);
27322 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
27323 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
27324 OMP_CLAUSE_CHAIN (c
) = list
;
27330 aligned ( variable-list )
27331 aligned ( variable-list : constant-expression ) */
27334 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
27336 tree nlist
, c
, alignment
= NULL_TREE
;
27339 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27342 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
27347 alignment
= cp_parser_constant_expression (parser
, false, NULL
);
27349 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27350 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27351 /*or_comma=*/false,
27352 /*consume_paren=*/true);
27354 if (alignment
== error_mark_node
)
27355 alignment
= NULL_TREE
;
27358 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27359 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
27365 linear ( variable-list )
27366 linear ( variable-list : expression ) */
27369 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
)
27371 tree nlist
, c
, step
= integer_one_node
;
27374 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27377 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
27382 step
= cp_parser_expression (parser
, false, NULL
);
27384 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27385 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27386 /*or_comma=*/false,
27387 /*consume_paren=*/true);
27389 if (step
== error_mark_node
)
27393 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27394 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
27400 safelen ( constant-expression ) */
27403 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
27404 location_t location
)
27408 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27411 t
= cp_parser_constant_expression (parser
, false, NULL
);
27413 if (t
== error_mark_node
27414 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27415 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27416 /*or_comma=*/false,
27417 /*consume_paren=*/true);
27419 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
27421 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
27422 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
27423 OMP_CLAUSE_CHAIN (c
) = list
;
27429 simdlen ( constant-expression ) */
27432 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
27433 location_t location
)
27437 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27440 t
= cp_parser_constant_expression (parser
, false, NULL
);
27442 if (t
== error_mark_node
27443 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27444 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27445 /*or_comma=*/false,
27446 /*consume_paren=*/true);
27448 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
27450 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
27451 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
27452 OMP_CLAUSE_CHAIN (c
) = list
;
27458 depend ( depend-kind : variable-list )
27461 in | out | inout */
27464 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
27467 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
27469 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27474 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27475 const char *p
= IDENTIFIER_POINTER (id
);
27477 if (strcmp ("in", p
) == 0)
27478 kind
= OMP_CLAUSE_DEPEND_IN
;
27479 else if (strcmp ("inout", p
) == 0)
27480 kind
= OMP_CLAUSE_DEPEND_INOUT
;
27481 else if (strcmp ("out", p
) == 0)
27482 kind
= OMP_CLAUSE_DEPEND_OUT
;
27489 cp_lexer_consume_token (parser
->lexer
);
27490 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27493 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
27496 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27497 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
27502 cp_parser_error (parser
, "invalid depend kind");
27504 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27505 /*or_comma=*/false,
27506 /*consume_paren=*/true);
27511 map ( map-kind : variable-list )
27512 map ( variable-list )
27515 alloc | to | from | tofrom */
27518 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
27521 enum omp_clause_map_kind kind
= OMP_CLAUSE_MAP_TOFROM
;
27523 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
27527 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
27529 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27530 const char *p
= IDENTIFIER_POINTER (id
);
27532 if (strcmp ("alloc", p
) == 0)
27533 kind
= OMP_CLAUSE_MAP_ALLOC
;
27534 else if (strcmp ("to", p
) == 0)
27535 kind
= OMP_CLAUSE_MAP_TO
;
27536 else if (strcmp ("from", p
) == 0)
27537 kind
= OMP_CLAUSE_MAP_FROM
;
27538 else if (strcmp ("tofrom", p
) == 0)
27539 kind
= OMP_CLAUSE_MAP_TOFROM
;
27542 cp_parser_error (parser
, "invalid map kind");
27543 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27544 /*or_comma=*/false,
27545 /*consume_paren=*/true);
27548 cp_lexer_consume_token (parser
->lexer
);
27549 cp_lexer_consume_token (parser
->lexer
);
27552 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
27555 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27556 OMP_CLAUSE_MAP_KIND (c
) = kind
;
27562 device ( expression ) */
27565 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
27566 location_t location
)
27570 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27573 t
= cp_parser_expression (parser
, false, NULL
);
27575 if (t
== error_mark_node
27576 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27577 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27578 /*or_comma=*/false,
27579 /*consume_paren=*/true);
27581 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
27582 "device", location
);
27584 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
27585 OMP_CLAUSE_DEVICE_ID (c
) = t
;
27586 OMP_CLAUSE_CHAIN (c
) = list
;
27592 dist_schedule ( static )
27593 dist_schedule ( static , expression ) */
27596 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
27597 location_t location
)
27601 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27604 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
27606 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
27608 cp_lexer_consume_token (parser
->lexer
);
27610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27612 cp_lexer_consume_token (parser
->lexer
);
27614 t
= cp_parser_assignment_expression (parser
, false, NULL
);
27616 if (t
== error_mark_node
)
27618 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
27620 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27623 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
27626 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
27628 OMP_CLAUSE_CHAIN (c
) = list
;
27632 cp_parser_error (parser
, "invalid dist_schedule kind");
27634 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27635 /*or_comma=*/false,
27636 /*consume_paren=*/true);
27641 proc_bind ( proc-bind-kind )
27644 master | close | spread */
27647 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
27648 location_t location
)
27651 enum omp_clause_proc_bind_kind kind
;
27653 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27658 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27659 const char *p
= IDENTIFIER_POINTER (id
);
27661 if (strcmp ("master", p
) == 0)
27662 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
27663 else if (strcmp ("close", p
) == 0)
27664 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
27665 else if (strcmp ("spread", p
) == 0)
27666 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
27673 cp_lexer_consume_token (parser
->lexer
);
27674 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
27677 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
27678 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
27680 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
27681 OMP_CLAUSE_CHAIN (c
) = list
;
27685 cp_parser_error (parser
, "invalid depend kind");
27687 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27688 /*or_comma=*/false,
27689 /*consume_paren=*/true);
27693 /* Parse all OpenMP clauses. The set clauses allowed by the directive
27694 is a bitmask in MASK. Return the list of clauses found; the result
27695 of clause default goes in *pdefault. */
27698 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
27699 const char *where
, cp_token
*pragma_tok
,
27700 bool finish_p
= true)
27702 tree clauses
= NULL
;
27704 cp_token
*token
= NULL
;
27706 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
27708 pragma_omp_clause c_kind
;
27709 const char *c_name
;
27710 tree prev
= clauses
;
27712 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27713 cp_lexer_consume_token (parser
->lexer
);
27715 token
= cp_lexer_peek_token (parser
->lexer
);
27716 c_kind
= cp_parser_omp_clause_name (parser
);
27720 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
27721 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
27723 c_name
= "collapse";
27725 case PRAGMA_OMP_CLAUSE_COPYIN
:
27726 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
27729 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
27730 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
27732 c_name
= "copyprivate";
27734 case PRAGMA_OMP_CLAUSE_DEFAULT
:
27735 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
27737 c_name
= "default";
27739 case PRAGMA_OMP_CLAUSE_FINAL
:
27740 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
27743 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
27744 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
27746 c_name
= "firstprivate";
27748 case PRAGMA_OMP_CLAUSE_IF
:
27749 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
27752 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
27753 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
27755 c_name
= "lastprivate";
27757 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
27758 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
27760 c_name
= "mergeable";
27762 case PRAGMA_OMP_CLAUSE_NOWAIT
:
27763 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
27766 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
27767 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
27769 c_name
= "num_threads";
27771 case PRAGMA_OMP_CLAUSE_ORDERED
:
27772 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
27774 c_name
= "ordered";
27776 case PRAGMA_OMP_CLAUSE_PRIVATE
:
27777 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
27779 c_name
= "private";
27781 case PRAGMA_OMP_CLAUSE_REDUCTION
:
27782 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
27783 c_name
= "reduction";
27785 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
27786 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
27788 c_name
= "schedule";
27790 case PRAGMA_OMP_CLAUSE_SHARED
:
27791 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
27795 case PRAGMA_OMP_CLAUSE_UNTIED
:
27796 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
27800 case PRAGMA_OMP_CLAUSE_INBRANCH
:
27801 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
27802 clauses
, token
->location
);
27803 c_name
= "inbranch";
27805 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
27806 clauses
= cp_parser_omp_clause_branch (parser
,
27807 OMP_CLAUSE_NOTINBRANCH
,
27808 clauses
, token
->location
);
27809 c_name
= "notinbranch";
27811 case PRAGMA_OMP_CLAUSE_PARALLEL
:
27812 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
27813 clauses
, token
->location
);
27814 c_name
= "parallel";
27818 error_at (token
->location
, "%qs must be the first clause of %qs",
27823 case PRAGMA_OMP_CLAUSE_FOR
:
27824 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
27825 clauses
, token
->location
);
27828 goto clause_not_first
;
27830 case PRAGMA_OMP_CLAUSE_SECTIONS
:
27831 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
27832 clauses
, token
->location
);
27833 c_name
= "sections";
27835 goto clause_not_first
;
27837 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
27838 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
27839 clauses
, token
->location
);
27840 c_name
= "taskgroup";
27842 goto clause_not_first
;
27844 case PRAGMA_OMP_CLAUSE_TO
:
27845 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
27849 case PRAGMA_OMP_CLAUSE_FROM
:
27850 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
27854 case PRAGMA_OMP_CLAUSE_UNIFORM
:
27855 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
27857 c_name
= "uniform";
27859 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
27860 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
27862 c_name
= "num_teams";
27864 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
27865 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
27867 c_name
= "thread_limit";
27869 case PRAGMA_OMP_CLAUSE_ALIGNED
:
27870 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
27871 c_name
= "aligned";
27873 case PRAGMA_OMP_CLAUSE_LINEAR
:
27874 clauses
= cp_parser_omp_clause_linear (parser
, clauses
);
27877 case PRAGMA_OMP_CLAUSE_DEPEND
:
27878 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
27881 case PRAGMA_OMP_CLAUSE_MAP
:
27882 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
27885 case PRAGMA_OMP_CLAUSE_DEVICE
:
27886 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
27890 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
27891 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
27893 c_name
= "dist_schedule";
27895 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
27896 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
27898 c_name
= "proc_bind";
27900 case PRAGMA_OMP_CLAUSE_SAFELEN
:
27901 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
27903 c_name
= "safelen";
27905 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
27906 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
27908 c_name
= "simdlen";
27911 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
27917 if (((mask
>> c_kind
) & 1) == 0)
27919 /* Remove the invalid clause(s) from the list to avoid
27920 confusing the rest of the compiler. */
27922 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
27926 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
27928 return finish_omp_clauses (clauses
);
27936 In practice, we're also interested in adding the statement to an
27937 outer node. So it is convenient if we work around the fact that
27938 cp_parser_statement calls add_stmt. */
27941 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
27943 unsigned save
= parser
->in_statement
;
27945 /* Only move the values to IN_OMP_BLOCK if they weren't false.
27946 This preserves the "not within loop or switch" style error messages
27947 for nonsense cases like
27953 if (parser
->in_statement
)
27954 parser
->in_statement
= IN_OMP_BLOCK
;
27960 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
27962 parser
->in_statement
= save
;
27966 cp_parser_omp_structured_block (cp_parser
*parser
)
27968 tree stmt
= begin_omp_structured_block ();
27969 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
27971 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
27973 cp_parser_end_omp_structured_block (parser
, save
);
27974 return finish_omp_structured_block (stmt
);
27978 # pragma omp atomic new-line
27982 x binop= expr | x++ | ++x | x-- | --x
27984 +, *, -, /, &, ^, |, <<, >>
27986 where x is an lvalue expression with scalar type.
27989 # pragma omp atomic new-line
27992 # pragma omp atomic read new-line
27995 # pragma omp atomic write new-line
27998 # pragma omp atomic update new-line
28001 # pragma omp atomic capture new-line
28004 # pragma omp atomic capture new-line
28012 expression-stmt | x = x binop expr
28014 v = expression-stmt
28016 { v = x; update-stmt; } | { update-stmt; v = x; }
28020 expression-stmt | x = x binop expr | x = expr binop x
28024 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28026 where x and v are lvalue expressions with scalar type. */
28029 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
28031 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
28032 tree rhs1
= NULL_TREE
, orig_lhs
;
28033 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
28034 bool structured_block
= false;
28035 bool seq_cst
= false;
28037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28039 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28040 const char *p
= IDENTIFIER_POINTER (id
);
28042 if (!strcmp (p
, "read"))
28043 code
= OMP_ATOMIC_READ
;
28044 else if (!strcmp (p
, "write"))
28046 else if (!strcmp (p
, "update"))
28048 else if (!strcmp (p
, "capture"))
28049 code
= OMP_ATOMIC_CAPTURE_NEW
;
28053 cp_lexer_consume_token (parser
->lexer
);
28056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28058 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28059 const char *p
= IDENTIFIER_POINTER (id
);
28061 if (!strcmp (p
, "seq_cst"))
28064 cp_lexer_consume_token (parser
->lexer
);
28067 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28071 case OMP_ATOMIC_READ
:
28072 case NOP_EXPR
: /* atomic write */
28073 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28074 /*cast_p=*/false, NULL
);
28075 if (v
== error_mark_node
)
28077 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28079 if (code
== NOP_EXPR
)
28080 lhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28082 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28083 /*cast_p=*/false, NULL
);
28084 if (lhs
== error_mark_node
)
28086 if (code
== NOP_EXPR
)
28088 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28096 case OMP_ATOMIC_CAPTURE_NEW
:
28097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28099 cp_lexer_consume_token (parser
->lexer
);
28100 structured_block
= true;
28104 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28105 /*cast_p=*/false, NULL
);
28106 if (v
== error_mark_node
)
28108 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28116 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28117 /*cast_p=*/false, NULL
);
28119 switch (TREE_CODE (lhs
))
28124 case POSTINCREMENT_EXPR
:
28125 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
28126 code
= OMP_ATOMIC_CAPTURE_OLD
;
28128 case PREINCREMENT_EXPR
:
28129 lhs
= TREE_OPERAND (lhs
, 0);
28130 opcode
= PLUS_EXPR
;
28131 rhs
= integer_one_node
;
28134 case POSTDECREMENT_EXPR
:
28135 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
28136 code
= OMP_ATOMIC_CAPTURE_OLD
;
28138 case PREDECREMENT_EXPR
:
28139 lhs
= TREE_OPERAND (lhs
, 0);
28140 opcode
= MINUS_EXPR
;
28141 rhs
= integer_one_node
;
28144 case COMPOUND_EXPR
:
28145 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
28146 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
28147 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
28148 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
28149 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28150 (TREE_OPERAND (lhs
, 1), 0), 0)))
28152 /* Undo effects of boolean_increment for post {in,de}crement. */
28153 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
28156 if (TREE_CODE (lhs
) == MODIFY_EXPR
28157 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
28159 /* Undo effects of boolean_increment. */
28160 if (integer_onep (TREE_OPERAND (lhs
, 1)))
28162 /* This is pre or post increment. */
28163 rhs
= TREE_OPERAND (lhs
, 1);
28164 lhs
= TREE_OPERAND (lhs
, 0);
28166 if (code
== OMP_ATOMIC_CAPTURE_NEW
28167 && !structured_block
28168 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
28169 code
= OMP_ATOMIC_CAPTURE_OLD
;
28175 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28178 opcode
= MULT_EXPR
;
28181 opcode
= TRUNC_DIV_EXPR
;
28184 opcode
= PLUS_EXPR
;
28187 opcode
= MINUS_EXPR
;
28189 case CPP_LSHIFT_EQ
:
28190 opcode
= LSHIFT_EXPR
;
28192 case CPP_RSHIFT_EQ
:
28193 opcode
= RSHIFT_EXPR
;
28196 opcode
= BIT_AND_EXPR
;
28199 opcode
= BIT_IOR_EXPR
;
28202 opcode
= BIT_XOR_EXPR
;
28205 enum cp_parser_prec oprec
;
28207 cp_lexer_consume_token (parser
->lexer
);
28208 cp_parser_parse_tentatively (parser
);
28209 rhs1
= cp_parser_simple_cast_expression (parser
);
28210 if (rhs1
== error_mark_node
)
28212 cp_parser_abort_tentative_parse (parser
);
28213 cp_parser_simple_cast_expression (parser
);
28216 token
= cp_lexer_peek_token (parser
->lexer
);
28217 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
28219 cp_parser_abort_tentative_parse (parser
);
28220 cp_parser_parse_tentatively (parser
);
28221 rhs
= cp_parser_binary_expression (parser
, false, true,
28222 PREC_NOT_OPERATOR
, NULL
);
28223 if (rhs
== error_mark_node
)
28225 cp_parser_abort_tentative_parse (parser
);
28226 cp_parser_binary_expression (parser
, false, true,
28227 PREC_NOT_OPERATOR
, NULL
);
28230 switch (TREE_CODE (rhs
))
28233 case TRUNC_DIV_EXPR
:
28241 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
28243 if (cp_parser_parse_definitely (parser
))
28245 opcode
= TREE_CODE (rhs
);
28246 rhs1
= TREE_OPERAND (rhs
, 0);
28247 rhs
= TREE_OPERAND (rhs
, 1);
28257 cp_parser_abort_tentative_parse (parser
);
28258 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
28260 rhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28261 if (rhs
== error_mark_node
)
28267 cp_parser_error (parser
,
28268 "invalid form of %<#pragma omp atomic%>");
28271 if (!cp_parser_parse_definitely (parser
))
28273 switch (token
->type
)
28275 case CPP_SEMICOLON
:
28276 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
28278 code
= OMP_ATOMIC_CAPTURE_OLD
;
28283 cp_lexer_consume_token (parser
->lexer
);
28286 else if (structured_block
)
28293 cp_parser_error (parser
,
28294 "invalid form of %<#pragma omp atomic%>");
28297 opcode
= MULT_EXPR
;
28300 opcode
= TRUNC_DIV_EXPR
;
28303 opcode
= PLUS_EXPR
;
28306 opcode
= MINUS_EXPR
;
28309 opcode
= LSHIFT_EXPR
;
28312 opcode
= RSHIFT_EXPR
;
28315 opcode
= BIT_AND_EXPR
;
28318 opcode
= BIT_IOR_EXPR
;
28321 opcode
= BIT_XOR_EXPR
;
28324 cp_parser_error (parser
,
28325 "invalid operator for %<#pragma omp atomic%>");
28328 oprec
= TOKEN_PRECEDENCE (token
);
28329 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
28330 if (commutative_tree_code (opcode
))
28331 oprec
= (enum cp_parser_prec
) (oprec
- 1);
28332 cp_lexer_consume_token (parser
->lexer
);
28333 rhs
= cp_parser_binary_expression (parser
, false, false,
28335 if (rhs
== error_mark_node
)
28340 cp_parser_error (parser
,
28341 "invalid operator for %<#pragma omp atomic%>");
28344 cp_lexer_consume_token (parser
->lexer
);
28346 rhs
= cp_parser_expression (parser
, false, NULL
);
28347 if (rhs
== error_mark_node
)
28352 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
28354 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
28356 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28357 /*cast_p=*/false, NULL
);
28358 if (v
== error_mark_node
)
28360 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28362 lhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28363 /*cast_p=*/false, NULL
);
28364 if (lhs1
== error_mark_node
)
28367 if (structured_block
)
28369 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28370 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
28373 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
28374 if (!structured_block
)
28375 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28379 cp_parser_skip_to_end_of_block_or_statement (parser
);
28380 if (structured_block
)
28382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28383 cp_lexer_consume_token (parser
->lexer
);
28384 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
28386 cp_parser_skip_to_end_of_block_or_statement (parser
);
28387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28388 cp_lexer_consume_token (parser
->lexer
);
28395 # pragma omp barrier new-line */
28398 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
28400 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28401 finish_omp_barrier ();
28405 # pragma omp critical [(name)] new-line
28406 structured-block */
28409 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
28411 tree stmt
, name
= NULL
;
28413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28415 cp_lexer_consume_token (parser
->lexer
);
28417 name
= cp_parser_identifier (parser
);
28419 if (name
== error_mark_node
28420 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28421 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28422 /*or_comma=*/false,
28423 /*consume_paren=*/true);
28424 if (name
== error_mark_node
)
28427 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28429 stmt
= cp_parser_omp_structured_block (parser
);
28430 return c_finish_omp_critical (input_location
, stmt
, name
);
28434 # pragma omp flush flush-vars[opt] new-line
28437 ( variable-list ) */
28440 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
28442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28443 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
28444 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28446 finish_omp_flush ();
28449 /* Helper function, to parse omp for increment expression. */
28452 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
28454 tree cond
= cp_parser_binary_expression (parser
, false, true,
28455 PREC_NOT_OPERATOR
, NULL
);
28456 if (cond
== error_mark_node
28457 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28459 cp_parser_skip_to_end_of_statement (parser
);
28460 return error_mark_node
;
28463 switch (TREE_CODE (cond
))
28471 return error_mark_node
;
28474 /* If decl is an iterator, preserve LHS and RHS of the relational
28475 expr until finish_omp_for. */
28477 && (type_dependent_expression_p (decl
)
28478 || CLASS_TYPE_P (TREE_TYPE (decl
))))
28481 return build_x_binary_op (input_location
, TREE_CODE (cond
),
28482 TREE_OPERAND (cond
, 0), ERROR_MARK
,
28483 TREE_OPERAND (cond
, 1), ERROR_MARK
,
28484 /*overload=*/NULL
, tf_warning_or_error
);
28487 /* Helper function, to parse omp for increment expression. */
28490 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
28492 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28498 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
28500 op
= (token
->type
== CPP_PLUS_PLUS
28501 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
28502 cp_lexer_consume_token (parser
->lexer
);
28503 lhs
= cp_parser_simple_cast_expression (parser
);
28505 return error_mark_node
;
28506 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
28509 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
28511 return error_mark_node
;
28513 token
= cp_lexer_peek_token (parser
->lexer
);
28514 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
28516 op
= (token
->type
== CPP_PLUS_PLUS
28517 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
28518 cp_lexer_consume_token (parser
->lexer
);
28519 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
28522 op
= cp_parser_assignment_operator_opt (parser
);
28523 if (op
== ERROR_MARK
)
28524 return error_mark_node
;
28526 if (op
!= NOP_EXPR
)
28528 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
28529 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
28530 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
28533 lhs
= cp_parser_binary_expression (parser
, false, false,
28534 PREC_ADDITIVE_EXPRESSION
, NULL
);
28535 token
= cp_lexer_peek_token (parser
->lexer
);
28536 decl_first
= lhs
== decl
;
28539 if (token
->type
!= CPP_PLUS
28540 && token
->type
!= CPP_MINUS
)
28541 return error_mark_node
;
28545 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
28546 cp_lexer_consume_token (parser
->lexer
);
28547 rhs
= cp_parser_binary_expression (parser
, false, false,
28548 PREC_ADDITIVE_EXPRESSION
, NULL
);
28549 token
= cp_lexer_peek_token (parser
->lexer
);
28550 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
28552 if (lhs
== NULL_TREE
)
28554 if (op
== PLUS_EXPR
)
28557 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
28558 tf_warning_or_error
);
28561 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
28562 ERROR_MARK
, NULL
, tf_warning_or_error
);
28565 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
28569 if (rhs
!= decl
|| op
== MINUS_EXPR
)
28570 return error_mark_node
;
28571 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
28574 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
28576 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
28579 /* Parse the restricted form of the for statement allowed by OpenMP. */
28582 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
28585 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
28586 tree real_decl
, initv
, condv
, incrv
, declv
;
28587 tree this_pre_body
, cl
;
28588 location_t loc_first
;
28589 bool collapse_err
= false;
28590 int i
, collapse
= 1, nbraces
= 0;
28591 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
28593 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
28594 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
28595 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
28597 gcc_assert (collapse
>= 1);
28599 declv
= make_tree_vec (collapse
);
28600 initv
= make_tree_vec (collapse
);
28601 condv
= make_tree_vec (collapse
);
28602 incrv
= make_tree_vec (collapse
);
28604 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
28606 for (i
= 0; i
< collapse
; i
++)
28608 int bracecount
= 0;
28609 bool add_private_clause
= false;
28612 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
28614 cp_parser_error (parser
, "for statement expected");
28617 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
28619 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28622 init
= decl
= real_decl
= NULL
;
28623 this_pre_body
= push_stmt_list ();
28624 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28626 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
28630 integer-type var = lb
28631 random-access-iterator-type var = lb
28632 pointer-type var = lb
28634 cp_decl_specifier_seq type_specifiers
;
28636 /* First, try to parse as an initialized declaration. See
28637 cp_parser_condition, from whence the bulk of this is copied. */
28639 cp_parser_parse_tentatively (parser
);
28640 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
28641 /*is_trailing_return=*/false,
28643 if (cp_parser_parse_definitely (parser
))
28645 /* If parsing a type specifier seq succeeded, then this
28646 MUST be a initialized declaration. */
28647 tree asm_specification
, attributes
;
28648 cp_declarator
*declarator
;
28650 declarator
= cp_parser_declarator (parser
,
28651 CP_PARSER_DECLARATOR_NAMED
,
28652 /*ctor_dtor_or_conv_p=*/NULL
,
28653 /*parenthesized_p=*/NULL
,
28654 /*member_p=*/false);
28655 attributes
= cp_parser_attributes_opt (parser
);
28656 asm_specification
= cp_parser_asm_specification_opt (parser
);
28658 if (declarator
== cp_error_declarator
)
28659 cp_parser_skip_to_end_of_statement (parser
);
28663 tree pushed_scope
, auto_node
;
28665 decl
= start_decl (declarator
, &type_specifiers
,
28666 SD_INITIALIZED
, attributes
,
28667 /*prefix_attributes=*/NULL_TREE
,
28670 auto_node
= type_uses_auto (TREE_TYPE (decl
));
28671 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
28673 if (cp_lexer_next_token_is (parser
->lexer
,
28675 error ("parenthesized initialization is not allowed in "
28676 "OpenMP %<for%> loop");
28678 /* Trigger an error. */
28679 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
28681 init
= error_mark_node
;
28682 cp_parser_skip_to_end_of_statement (parser
);
28684 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
28685 || type_dependent_expression_p (decl
)
28688 bool is_direct_init
, is_non_constant_init
;
28690 init
= cp_parser_initializer (parser
,
28692 &is_non_constant_init
);
28697 = do_auto_deduction (TREE_TYPE (decl
), init
,
28700 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
28701 && !type_dependent_expression_p (decl
))
28705 cp_finish_decl (decl
, init
, !is_non_constant_init
,
28707 LOOKUP_ONLYCONVERTING
);
28708 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
28710 vec_safe_push (for_block
, this_pre_body
);
28714 init
= pop_stmt_list (this_pre_body
);
28715 this_pre_body
= NULL_TREE
;
28720 cp_lexer_consume_token (parser
->lexer
);
28721 init
= cp_parser_assignment_expression (parser
, false, NULL
);
28724 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
28725 init
= error_mark_node
;
28727 cp_finish_decl (decl
, NULL_TREE
,
28728 /*init_const_expr_p=*/false,
28730 LOOKUP_ONLYCONVERTING
);
28734 pop_scope (pushed_scope
);
28740 /* If parsing a type specifier sequence failed, then
28741 this MUST be a simple expression. */
28742 cp_parser_parse_tentatively (parser
);
28743 decl
= cp_parser_primary_expression (parser
, false, false,
28745 if (!cp_parser_error_occurred (parser
)
28748 && CLASS_TYPE_P (TREE_TYPE (decl
)))
28752 cp_parser_parse_definitely (parser
);
28753 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
28754 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
28755 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
28758 tf_warning_or_error
));
28759 add_private_clause
= true;
28764 cp_parser_abort_tentative_parse (parser
);
28765 init
= cp_parser_expression (parser
, false, NULL
);
28768 if (TREE_CODE (init
) == MODIFY_EXPR
28769 || TREE_CODE (init
) == MODOP_EXPR
)
28770 real_decl
= TREE_OPERAND (init
, 0);
28775 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
28778 this_pre_body
= pop_stmt_list (this_pre_body
);
28782 pre_body
= push_stmt_list ();
28784 add_stmt (this_pre_body
);
28785 pre_body
= pop_stmt_list (pre_body
);
28788 pre_body
= this_pre_body
;
28793 if (cclauses
!= NULL
28794 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
28795 && real_decl
!= NULL_TREE
)
28798 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
28799 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
28800 && OMP_CLAUSE_DECL (*c
) == real_decl
)
28802 error_at (loc
, "iteration variable %qD"
28803 " should not be firstprivate", real_decl
);
28804 *c
= OMP_CLAUSE_CHAIN (*c
);
28806 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
28807 && OMP_CLAUSE_DECL (*c
) == real_decl
)
28809 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
28810 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
28811 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
28812 OMP_CLAUSE_DECL (l
) = real_decl
;
28813 OMP_CLAUSE_CHAIN (l
) = clauses
;
28814 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
28816 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
28817 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
28818 add_private_clause
= false;
28822 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
28823 && OMP_CLAUSE_DECL (*c
) == real_decl
)
28824 add_private_clause
= false;
28825 c
= &OMP_CLAUSE_CHAIN (*c
);
28829 if (add_private_clause
)
28832 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
28834 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
28835 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
28836 && OMP_CLAUSE_DECL (c
) == decl
)
28838 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
28839 && OMP_CLAUSE_DECL (c
) == decl
)
28840 error_at (loc
, "iteration variable %qD "
28841 "should not be firstprivate",
28843 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
28844 && OMP_CLAUSE_DECL (c
) == decl
)
28845 error_at (loc
, "iteration variable %qD should not be reduction",
28850 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
28851 OMP_CLAUSE_DECL (c
) = decl
;
28852 c
= finish_omp_clauses (c
);
28855 OMP_CLAUSE_CHAIN (c
) = clauses
;
28862 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28863 cond
= cp_parser_omp_for_cond (parser
, decl
);
28864 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
28867 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
28869 /* If decl is an iterator, preserve the operator on decl
28870 until finish_omp_for. */
28872 && ((processing_template_decl
28873 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
28874 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
28875 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
28877 incr
= cp_parser_expression (parser
, false, NULL
);
28878 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
28879 SET_EXPR_LOCATION (incr
, input_location
);
28882 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28883 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28884 /*or_comma=*/false,
28885 /*consume_paren=*/true);
28887 TREE_VEC_ELT (declv
, i
) = decl
;
28888 TREE_VEC_ELT (initv
, i
) = init
;
28889 TREE_VEC_ELT (condv
, i
) = cond
;
28890 TREE_VEC_ELT (incrv
, i
) = incr
;
28892 if (i
== collapse
- 1)
28895 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
28896 in between the collapsed for loops to be still considered perfectly
28897 nested. Hopefully the final version clarifies this.
28898 For now handle (multiple) {'s and empty statements. */
28899 cp_parser_parse_tentatively (parser
);
28902 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
28904 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28906 cp_lexer_consume_token (parser
->lexer
);
28909 else if (bracecount
28910 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28911 cp_lexer_consume_token (parser
->lexer
);
28914 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28915 error_at (loc
, "not enough collapsed for loops");
28916 collapse_err
= true;
28917 cp_parser_abort_tentative_parse (parser
);
28926 cp_parser_parse_definitely (parser
);
28927 nbraces
+= bracecount
;
28931 /* Note that we saved the original contents of this flag when we entered
28932 the structured block, and so we don't need to re-save it here. */
28933 parser
->in_statement
= IN_OMP_FOR
;
28935 /* Note that the grammar doesn't call for a structured block here,
28936 though the loop as a whole is a structured block. */
28937 body
= push_stmt_list ();
28938 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
28939 body
= pop_stmt_list (body
);
28941 if (declv
== NULL_TREE
)
28944 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
28945 pre_body
, clauses
);
28949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28951 cp_lexer_consume_token (parser
->lexer
);
28954 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28955 cp_lexer_consume_token (parser
->lexer
);
28960 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28961 "collapsed loops not perfectly nested");
28963 collapse_err
= true;
28964 cp_parser_statement_seq_opt (parser
, NULL
);
28965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28970 while (!for_block
->is_empty ())
28971 add_stmt (pop_stmt_list (for_block
->pop ()));
28972 release_tree_vector (for_block
);
28977 /* Helper function for OpenMP parsing, split clauses and call
28978 finish_omp_clauses on each of the set of clauses afterwards. */
28981 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
28982 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
28985 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
28986 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
28988 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
28992 #pragma omp simd simd-clause[optseq] new-line
28995 #define OMP_SIMD_CLAUSE_MASK \
28996 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
28997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
28998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
28999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29005 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
29006 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29008 tree clauses
, sb
, ret
;
29010 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29012 strcat (p_name
, " simd");
29013 mask
|= OMP_SIMD_CLAUSE_MASK
;
29014 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
29016 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29020 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
29021 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
29024 sb
= begin_omp_structured_block ();
29025 save
= cp_parser_begin_omp_structured_block (parser
);
29027 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
29029 cp_parser_end_omp_structured_block (parser
, save
);
29030 add_stmt (finish_omp_structured_block (sb
));
29036 #pragma omp for for-clause[optseq] new-line
29040 #pragma omp for simd for-simd-clause[optseq] new-line
29043 #define OMP_FOR_CLAUSE_MASK \
29044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29054 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
29055 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29057 tree clauses
, sb
, ret
;
29059 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29061 strcat (p_name
, " for");
29062 mask
|= OMP_FOR_CLAUSE_MASK
;
29064 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
29066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29068 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29069 const char *p
= IDENTIFIER_POINTER (id
);
29071 if (strcmp (p
, "simd") == 0)
29073 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29074 if (cclauses
== NULL
)
29075 cclauses
= cclauses_buf
;
29077 cp_lexer_consume_token (parser
->lexer
);
29078 sb
= begin_omp_structured_block ();
29079 save
= cp_parser_begin_omp_structured_block (parser
);
29080 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29082 cp_parser_end_omp_structured_block (parser
, save
);
29083 tree body
= finish_omp_structured_block (sb
);
29086 ret
= make_node (OMP_FOR
);
29087 TREE_TYPE (ret
) = void_type_node
;
29088 OMP_FOR_BODY (ret
) = body
;
29089 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29090 SET_EXPR_LOCATION (ret
, loc
);
29096 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29100 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
29101 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29104 sb
= begin_omp_structured_block ();
29105 save
= cp_parser_begin_omp_structured_block (parser
);
29107 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
29109 cp_parser_end_omp_structured_block (parser
, save
);
29110 add_stmt (finish_omp_structured_block (sb
));
29116 # pragma omp master new-line
29117 structured-block */
29120 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
29122 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29123 return c_finish_omp_master (input_location
,
29124 cp_parser_omp_structured_block (parser
));
29128 # pragma omp ordered new-line
29129 structured-block */
29132 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
29134 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29135 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29136 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
29142 { section-sequence }
29145 section-directive[opt] structured-block
29146 section-sequence section-directive structured-block */
29149 cp_parser_omp_sections_scope (cp_parser
*parser
)
29151 tree stmt
, substmt
;
29152 bool error_suppress
= false;
29155 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
29158 stmt
= push_stmt_list ();
29160 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
29162 substmt
= cp_parser_omp_structured_block (parser
);
29163 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
29164 add_stmt (substmt
);
29169 tok
= cp_lexer_peek_token (parser
->lexer
);
29170 if (tok
->type
== CPP_CLOSE_BRACE
)
29172 if (tok
->type
== CPP_EOF
)
29175 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
29177 cp_lexer_consume_token (parser
->lexer
);
29178 cp_parser_require_pragma_eol (parser
, tok
);
29179 error_suppress
= false;
29181 else if (!error_suppress
)
29183 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
29184 error_suppress
= true;
29187 substmt
= cp_parser_omp_structured_block (parser
);
29188 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
29189 add_stmt (substmt
);
29191 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29193 substmt
= pop_stmt_list (stmt
);
29195 stmt
= make_node (OMP_SECTIONS
);
29196 TREE_TYPE (stmt
) = void_type_node
;
29197 OMP_SECTIONS_BODY (stmt
) = substmt
;
29204 # pragma omp sections sections-clause[optseq] newline
29207 #define OMP_SECTIONS_CLAUSE_MASK \
29208 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29215 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
29216 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29219 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29221 strcat (p_name
, " sections");
29222 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
29224 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
29226 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29230 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
29231 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
29234 ret
= cp_parser_omp_sections_scope (parser
);
29236 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
29242 # pragma parallel parallel-clause new-line
29243 # pragma parallel for parallel-for-clause new-line
29244 # pragma parallel sections parallel-sections-clause new-line
29247 # pragma parallel for simd parallel-for-simd-clause new-line */
29249 #define OMP_PARALLEL_CLAUSE_MASK \
29250 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29261 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
29262 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29264 tree stmt
, clauses
, block
;
29266 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29268 strcat (p_name
, " parallel");
29269 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
29271 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29273 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29274 if (cclauses
== NULL
)
29275 cclauses
= cclauses_buf
;
29277 cp_lexer_consume_token (parser
->lexer
);
29278 block
= begin_omp_parallel ();
29279 save
= cp_parser_begin_omp_structured_block (parser
);
29280 cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29281 cp_parser_end_omp_structured_block (parser
, save
);
29282 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
29284 OMP_PARALLEL_COMBINED (stmt
) = 1;
29289 error_at (loc
, "expected %<for%> after %qs", p_name
);
29290 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29293 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29295 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29296 const char *p
= IDENTIFIER_POINTER (id
);
29297 if (strcmp (p
, "sections") == 0)
29299 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29300 cclauses
= cclauses_buf
;
29302 cp_lexer_consume_token (parser
->lexer
);
29303 block
= begin_omp_parallel ();
29304 save
= cp_parser_begin_omp_structured_block (parser
);
29305 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29306 cp_parser_end_omp_structured_block (parser
, save
);
29307 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
29309 OMP_PARALLEL_COMBINED (stmt
) = 1;
29314 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
29316 block
= begin_omp_parallel ();
29317 save
= cp_parser_begin_omp_structured_block (parser
);
29318 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29319 cp_parser_end_omp_structured_block (parser
, save
);
29320 stmt
= finish_omp_parallel (clauses
, block
);
29325 # pragma omp single single-clause[optseq] new-line
29326 structured-block */
29328 #define OMP_SINGLE_CLAUSE_MASK \
29329 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29335 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
29337 tree stmt
= make_node (OMP_SINGLE
);
29338 TREE_TYPE (stmt
) = void_type_node
;
29340 OMP_SINGLE_CLAUSES (stmt
)
29341 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
29342 "#pragma omp single", pragma_tok
);
29343 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29345 return add_stmt (stmt
);
29349 # pragma omp task task-clause[optseq] new-line
29350 structured-block */
29352 #define OMP_TASK_CLAUSE_MASK \
29353 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29364 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
29366 tree clauses
, block
;
29369 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
29370 "#pragma omp task", pragma_tok
);
29371 block
= begin_omp_task ();
29372 save
= cp_parser_begin_omp_structured_block (parser
);
29373 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29374 cp_parser_end_omp_structured_block (parser
, save
);
29375 return finish_omp_task (clauses
, block
);
29379 # pragma omp taskwait new-line */
29382 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
29384 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29385 finish_omp_taskwait ();
29389 # pragma omp taskyield new-line */
29392 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
29394 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29395 finish_omp_taskyield ();
29399 # pragma omp taskgroup new-line
29400 structured-block */
29403 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
29405 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29406 return c_finish_omp_taskgroup (input_location
,
29407 cp_parser_omp_structured_block (parser
));
29412 # pragma omp threadprivate (variable-list) */
29415 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
29419 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29420 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29422 finish_omp_threadprivate (vars
);
29426 # pragma omp cancel cancel-clause[optseq] new-line */
29428 #define OMP_CANCEL_CLAUSE_MASK \
29429 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29436 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
29438 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
29439 "#pragma omp cancel", pragma_tok
);
29440 finish_omp_cancel (clauses
);
29444 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
29446 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
29447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
29453 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
29456 bool point_seen
= false;
29458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29460 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29461 const char *p
= IDENTIFIER_POINTER (id
);
29463 if (strcmp (p
, "point") == 0)
29465 cp_lexer_consume_token (parser
->lexer
);
29471 cp_parser_error (parser
, "expected %<point%>");
29472 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29476 clauses
= cp_parser_omp_all_clauses (parser
,
29477 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
29478 "#pragma omp cancellation point",
29480 finish_omp_cancellation_point (clauses
);
29484 #pragma omp distribute distribute-clause[optseq] new-line
29487 #define OMP_DISTRIBUTE_CLAUSE_MASK \
29488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
29491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29494 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
29495 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29497 tree clauses
, sb
, ret
;
29499 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29501 strcat (p_name
, " distribute");
29502 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
29504 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
);
29509 bool parallel
= false;
29511 if (strcmp (p
, "simd") == 0)
29514 parallel
= strcmp (p
, "parallel") == 0;
29515 if (parallel
|| simd
)
29517 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29518 if (cclauses
== NULL
)
29519 cclauses
= cclauses_buf
;
29520 cp_lexer_consume_token (parser
->lexer
);
29521 sb
= begin_omp_structured_block ();
29522 save
= cp_parser_begin_omp_structured_block (parser
);
29524 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29527 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
29529 cp_parser_end_omp_structured_block (parser
, save
);
29530 tree body
= finish_omp_structured_block (sb
);
29533 ret
= make_node (OMP_DISTRIBUTE
);
29534 TREE_TYPE (ret
) = void_type_node
;
29535 OMP_FOR_BODY (ret
) = body
;
29536 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
29537 SET_EXPR_LOCATION (ret
, loc
);
29543 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29547 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
29548 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
29551 sb
= begin_omp_structured_block ();
29552 save
= cp_parser_begin_omp_structured_block (parser
);
29554 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
29556 cp_parser_end_omp_structured_block (parser
, save
);
29557 add_stmt (finish_omp_structured_block (sb
));
29563 # pragma omp teams teams-clause[optseq] new-line
29564 structured-block */
29566 #define OMP_TEAMS_CLAUSE_MASK \
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_REDUCTION) \
29571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
29572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
29573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
29576 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
29577 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29579 tree clauses
, sb
, ret
;
29581 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29583 strcat (p_name
, " teams");
29584 mask
|= OMP_TEAMS_CLAUSE_MASK
;
29586 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29588 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29589 const char *p
= IDENTIFIER_POINTER (id
);
29590 if (strcmp (p
, "distribute") == 0)
29592 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29593 if (cclauses
== NULL
)
29594 cclauses
= cclauses_buf
;
29596 cp_lexer_consume_token (parser
->lexer
);
29597 sb
= begin_omp_structured_block ();
29598 save
= cp_parser_begin_omp_structured_block (parser
);
29599 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
29601 cp_parser_end_omp_structured_block (parser
, save
);
29602 tree body
= finish_omp_structured_block (sb
);
29605 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
29606 ret
= make_node (OMP_TEAMS
);
29607 TREE_TYPE (ret
) = void_type_node
;
29608 OMP_TEAMS_CLAUSES (ret
) = clauses
;
29609 OMP_TEAMS_BODY (ret
) = body
;
29610 return add_stmt (ret
);
29614 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29618 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
29619 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
29622 tree stmt
= make_node (OMP_TEAMS
);
29623 TREE_TYPE (stmt
) = void_type_node
;
29624 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
29625 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29627 return add_stmt (stmt
);
29631 # pragma omp target data target-data-clause[optseq] new-line
29632 structured-block */
29634 #define OMP_TARGET_DATA_CLAUSE_MASK \
29635 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29640 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
29642 tree stmt
= make_node (OMP_TARGET_DATA
);
29643 TREE_TYPE (stmt
) = void_type_node
;
29645 OMP_TARGET_DATA_CLAUSES (stmt
)
29646 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
29647 "#pragma omp target data", pragma_tok
);
29648 keep_next_level (true);
29649 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29651 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
29652 return add_stmt (stmt
);
29656 # pragma omp target update target-update-clause[optseq] new-line */
29658 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
29659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
29660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
29661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29665 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
29666 enum pragma_context context
)
29668 if (context
== pragma_stmt
)
29670 error_at (pragma_tok
->location
,
29671 "%<#pragma omp target update%> may only be "
29672 "used in compound statements");
29673 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29678 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
29679 "#pragma omp target update", pragma_tok
);
29680 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
29681 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
29683 error_at (pragma_tok
->location
,
29684 "%<#pragma omp target update must contain at least one "
29685 "%<from%> or %<to%> clauses");
29689 tree stmt
= make_node (OMP_TARGET_UPDATE
);
29690 TREE_TYPE (stmt
) = void_type_node
;
29691 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
29692 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
29698 # pragma omp target target-clause[optseq] new-line
29699 structured-block */
29701 #define OMP_TARGET_CLAUSE_MASK \
29702 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29707 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
29708 enum pragma_context context
)
29710 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
29712 cp_parser_error (parser
, "expected declaration specifiers");
29713 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29719 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29720 const char *p
= IDENTIFIER_POINTER (id
);
29722 if (strcmp (p
, "data") == 0)
29724 cp_lexer_consume_token (parser
->lexer
);
29725 cp_parser_omp_target_data (parser
, pragma_tok
);
29728 else if (strcmp (p
, "update") == 0)
29730 cp_lexer_consume_token (parser
->lexer
);
29731 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
29733 else if (strcmp (p
, "teams") == 0)
29735 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
29736 char p_name
[sizeof ("#pragma omp target teams distribute "
29737 "parallel for simd")];
29739 cp_lexer_consume_token (parser
->lexer
);
29740 strcpy (p_name
, "#pragma omp target");
29741 keep_next_level (true);
29742 tree sb
= begin_omp_structured_block ();
29743 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
29744 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
29745 OMP_TARGET_CLAUSE_MASK
, cclauses
);
29746 cp_parser_end_omp_structured_block (parser
, save
);
29747 tree body
= finish_omp_structured_block (sb
);
29750 tree stmt
= make_node (OMP_TARGET
);
29751 TREE_TYPE (stmt
) = void_type_node
;
29752 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
29753 OMP_TARGET_BODY (stmt
) = body
;
29759 tree stmt
= make_node (OMP_TARGET
);
29760 TREE_TYPE (stmt
) = void_type_node
;
29762 OMP_TARGET_CLAUSES (stmt
)
29763 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
29764 "#pragma omp target", pragma_tok
);
29765 keep_next_level (true);
29766 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29768 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
29774 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
29776 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
29777 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
29778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
29781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
29782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
29785 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
29786 enum pragma_context context
)
29788 bool first_p
= parser
->omp_declare_simd
== NULL
;
29789 cp_omp_declare_simd_data data
;
29792 data
.error_seen
= false;
29793 data
.fndecl_seen
= false;
29794 data
.tokens
= vNULL
;
29795 parser
->omp_declare_simd
= &data
;
29797 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
29798 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
29799 cp_lexer_consume_token (parser
->lexer
);
29800 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29801 parser
->omp_declare_simd
->error_seen
= true;
29802 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29803 struct cp_token_cache
*cp
29804 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
29805 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
29808 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
29809 cp_parser_pragma (parser
, context
);
29812 case pragma_external
:
29813 cp_parser_declaration (parser
);
29815 case pragma_member
:
29816 cp_parser_member_declaration (parser
);
29818 case pragma_objc_icode
:
29819 cp_parser_block_declaration (parser
, /*statement_p=*/false);
29822 cp_parser_declaration_statement (parser
);
29825 if (parser
->omp_declare_simd
29826 && !parser
->omp_declare_simd
->error_seen
29827 && !parser
->omp_declare_simd
->fndecl_seen
)
29828 error_at (pragma_tok
->location
,
29829 "%<#pragma omp declare simd%> not immediately followed by "
29830 "function declaration or definition");
29831 data
.tokens
.release ();
29832 parser
->omp_declare_simd
= NULL
;
29836 /* Finalize #pragma omp declare simd clauses after direct declarator has
29837 been parsed, and put that into "omp declare simd" attribute. */
29840 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
29842 struct cp_token_cache
*ce
;
29843 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
29846 if (!data
->error_seen
&& data
->fndecl_seen
)
29848 error ("%<#pragma omp declare simd%> not immediately followed by "
29849 "a single function declaration or definition");
29850 data
->error_seen
= true;
29853 if (data
->error_seen
)
29856 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
29860 cp_parser_push_lexer_for_tokens (parser
, ce
);
29861 parser
->lexer
->in_pragma
= true;
29862 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
29863 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
29864 cp_lexer_consume_token (parser
->lexer
);
29865 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
29866 "#pragma omp declare simd", pragma_tok
);
29867 cp_parser_pop_lexer (parser
);
29869 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
29870 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
29871 TREE_CHAIN (c
) = attrs
;
29872 if (processing_template_decl
)
29873 ATTR_IS_DEPENDENT (c
) = 1;
29877 data
->fndecl_seen
= true;
29883 # pragma omp declare target new-line
29884 declarations and definitions
29885 # pragma omp end declare target new-line */
29888 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
29890 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29891 scope_chain
->omp_declare_target_attribute
++;
29895 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
29897 const char *p
= "";
29898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29900 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29901 p
= IDENTIFIER_POINTER (id
);
29903 if (strcmp (p
, "declare") == 0)
29905 cp_lexer_consume_token (parser
->lexer
);
29907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29909 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29910 p
= IDENTIFIER_POINTER (id
);
29912 if (strcmp (p
, "target") == 0)
29913 cp_lexer_consume_token (parser
->lexer
);
29916 cp_parser_error (parser
, "expected %<target%>");
29917 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29923 cp_parser_error (parser
, "expected %<declare%>");
29924 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29927 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29928 if (!scope_chain
->omp_declare_target_attribute
)
29929 error_at (pragma_tok
->location
,
29930 "%<#pragma omp end declare target%> without corresponding "
29931 "%<#pragma omp declare target%>");
29933 scope_chain
->omp_declare_target_attribute
--;
29936 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
29937 expression and optional initializer clause of
29938 #pragma omp declare reduction. We store the expression(s) as
29939 either 3, 6 or 7 special statements inside of the artificial function's
29940 body. The first two statements are DECL_EXPRs for the artificial
29941 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
29942 expression that uses those variables.
29943 If there was any INITIALIZER clause, this is followed by further statements,
29944 the fourth and fifth statements are DECL_EXPRs for the artificial
29945 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
29946 constructor variant (first token after open paren is not omp_priv),
29947 then the sixth statement is a statement with the function call expression
29948 that uses the OMP_PRIV and optionally OMP_ORIG variable.
29949 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
29950 to initialize the OMP_PRIV artificial variable and there is seventh
29951 statement, a DECL_EXPR of the OMP_PRIV statement again. */
29954 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
29956 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
29957 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
29958 type
= TREE_TYPE (type
);
29959 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
29960 DECL_ARTIFICIAL (omp_out
) = 1;
29961 pushdecl (omp_out
);
29962 add_decl_expr (omp_out
);
29963 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
29964 DECL_ARTIFICIAL (omp_in
) = 1;
29966 add_decl_expr (omp_in
);
29968 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
29970 keep_next_level (true);
29971 tree block
= begin_omp_structured_block ();
29972 combiner
= cp_parser_expression (parser
, false, NULL
);
29973 finish_expr_stmt (combiner
);
29974 block
= finish_omp_structured_block (block
);
29977 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29980 const char *p
= "";
29981 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29983 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29984 p
= IDENTIFIER_POINTER (id
);
29987 if (strcmp (p
, "initializer") == 0)
29989 cp_lexer_consume_token (parser
->lexer
);
29990 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29994 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29996 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29997 p
= IDENTIFIER_POINTER (id
);
30000 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
30001 DECL_ARTIFICIAL (omp_priv
) = 1;
30002 pushdecl (omp_priv
);
30003 add_decl_expr (omp_priv
);
30004 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
30005 DECL_ARTIFICIAL (omp_orig
) = 1;
30006 pushdecl (omp_orig
);
30007 add_decl_expr (omp_orig
);
30009 keep_next_level (true);
30010 block
= begin_omp_structured_block ();
30013 if (strcmp (p
, "omp_priv") == 0)
30015 bool is_direct_init
, is_non_constant_init
;
30017 cp_lexer_consume_token (parser
->lexer
);
30018 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30019 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
30020 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
30021 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30023 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
30024 == CPP_CLOSE_PAREN
))
30026 finish_omp_structured_block (block
);
30027 error ("invalid initializer clause");
30030 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
30031 &is_non_constant_init
);
30032 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
30033 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
30037 cp_parser_parse_tentatively (parser
);
30038 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
30039 /*check_dependency_p=*/true,
30040 /*template_p=*/NULL
,
30041 /*declarator_p=*/false,
30042 /*optional_p=*/false);
30043 vec
<tree
, va_gc
> *args
;
30044 if (fn_name
== error_mark_node
30045 || cp_parser_error_occurred (parser
)
30046 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
30047 || ((args
= cp_parser_parenthesized_expression_list
30048 (parser
, non_attr
, /*cast_p=*/false,
30049 /*allow_expansion_p=*/true,
30050 /*non_constant_p=*/NULL
)),
30051 cp_parser_error_occurred (parser
)))
30053 finish_omp_structured_block (block
);
30054 cp_parser_abort_tentative_parse (parser
);
30055 cp_parser_error (parser
, "expected id-expression (arguments)");
30060 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
30061 if (arg
== omp_priv
30062 || (TREE_CODE (arg
) == ADDR_EXPR
30063 && TREE_OPERAND (arg
, 0) == omp_priv
))
30065 cp_parser_abort_tentative_parse (parser
);
30066 if (arg
== NULL_TREE
)
30067 error ("one of the initializer call arguments should be %<omp_priv%>"
30068 " or %<&omp_priv%>");
30069 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
30071 finish_expr_stmt (initializer
);
30074 block
= finish_omp_structured_block (block
);
30075 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
30076 finish_expr_stmt (block
);
30079 add_decl_expr (omp_orig
);
30081 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30085 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
30086 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
30092 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30093 initializer-clause[opt] new-line
30095 initializer-clause:
30096 initializer (omp_priv initializer)
30097 initializer (function-name (argument-list)) */
30100 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
30101 enum pragma_context
)
30103 vec
<tree
> types
= vNULL
;
30104 enum tree_code reduc_code
= ERROR_MARK
;
30105 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
30107 cp_token
*first_token
;
30108 cp_token_cache
*cp
;
30111 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30114 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
30117 reduc_code
= PLUS_EXPR
;
30120 reduc_code
= MULT_EXPR
;
30123 reduc_code
= MINUS_EXPR
;
30126 reduc_code
= BIT_AND_EXPR
;
30129 reduc_code
= BIT_XOR_EXPR
;
30132 reduc_code
= BIT_IOR_EXPR
;
30135 reduc_code
= TRUTH_ANDIF_EXPR
;
30138 reduc_code
= TRUTH_ORIF_EXPR
;
30141 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
30144 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30145 "%<|%>, %<&&%>, %<||%> or identifier");
30149 if (reduc_code
!= ERROR_MARK
)
30150 cp_lexer_consume_token (parser
->lexer
);
30152 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
30153 if (reduc_id
== error_mark_node
)
30156 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30159 /* Types may not be defined in declare reduction type list. */
30160 const char *saved_message
;
30161 saved_message
= parser
->type_definition_forbidden_message
;
30162 parser
->type_definition_forbidden_message
30163 = G_("types may not be defined in declare reduction type list");
30164 bool saved_colon_corrects_to_scope_p
;
30165 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30166 parser
->colon_corrects_to_scope_p
= false;
30167 bool saved_colon_doesnt_start_class_def_p
;
30168 saved_colon_doesnt_start_class_def_p
30169 = parser
->colon_doesnt_start_class_def_p
;
30170 parser
->colon_doesnt_start_class_def_p
= true;
30174 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30175 type
= cp_parser_type_id (parser
);
30176 if (type
== error_mark_node
)
30178 else if (ARITHMETIC_TYPE_P (type
)
30179 && (orig_reduc_id
== NULL_TREE
30180 || (TREE_CODE (type
) != COMPLEX_TYPE
30181 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
30183 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
30185 error_at (loc
, "predeclared arithmetic type %qT in "
30186 "%<#pragma omp declare reduction%>", type
);
30187 else if (TREE_CODE (type
) == FUNCTION_TYPE
30188 || TREE_CODE (type
) == METHOD_TYPE
30189 || TREE_CODE (type
) == ARRAY_TYPE
)
30190 error_at (loc
, "function or array type %qT in "
30191 "%<#pragma omp declare reduction%>", type
);
30192 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
30193 error_at (loc
, "reference type %qT in "
30194 "%<#pragma omp declare reduction%>", type
);
30195 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
30196 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
30197 "%<#pragma omp declare reduction%>", type
);
30199 types
.safe_push (type
);
30201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30202 cp_lexer_consume_token (parser
->lexer
);
30207 /* Restore the saved message. */
30208 parser
->type_definition_forbidden_message
= saved_message
;
30209 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30210 parser
->colon_doesnt_start_class_def_p
30211 = saved_colon_doesnt_start_class_def_p
;
30213 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
30214 || types
.is_empty ())
30217 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30222 first_token
= cp_lexer_peek_token (parser
->lexer
);
30225 FOR_EACH_VEC_ELT (types
, i
, type
)
30228 = build_function_type_list (void_type_node
,
30229 cp_build_reference_type (type
, false),
30231 tree this_reduc_id
= reduc_id
;
30232 if (!dependent_type_p (type
))
30233 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
30234 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
30235 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
30236 DECL_ARTIFICIAL (fndecl
) = 1;
30237 DECL_EXTERNAL (fndecl
) = 1;
30238 DECL_DECLARED_INLINE_P (fndecl
) = 1;
30239 DECL_IGNORED_P (fndecl
) = 1;
30240 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
30241 DECL_ATTRIBUTES (fndecl
)
30242 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
30243 DECL_ATTRIBUTES (fndecl
));
30244 if (processing_template_decl
)
30245 fndecl
= push_template_decl (fndecl
);
30246 bool block_scope
= false;
30247 tree block
= NULL_TREE
;
30248 if (current_function_decl
)
30250 block_scope
= true;
30251 DECL_CONTEXT (fndecl
) = global_namespace
;
30252 if (!processing_template_decl
)
30255 else if (current_class_type
)
30259 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30260 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30261 cp_lexer_consume_token (parser
->lexer
);
30262 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30264 cp
= cp_token_cache_new (first_token
,
30265 cp_lexer_peek_nth_token (parser
->lexer
,
30268 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
30269 finish_member_declaration (fndecl
);
30270 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
30271 DECL_PENDING_INLINE_P (fndecl
) = 1;
30272 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
30277 DECL_CONTEXT (fndecl
) = current_namespace
;
30281 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
30283 block
= begin_omp_structured_block ();
30286 cp_parser_push_lexer_for_tokens (parser
, cp
);
30287 parser
->lexer
->in_pragma
= true;
30289 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
30292 finish_function (0);
30294 DECL_CONTEXT (fndecl
) = current_function_decl
;
30296 cp_parser_pop_lexer (parser
);
30300 cp_parser_pop_lexer (parser
);
30302 finish_function (0);
30305 DECL_CONTEXT (fndecl
) = current_function_decl
;
30306 block
= finish_omp_structured_block (block
);
30307 if (TREE_CODE (block
) == BIND_EXPR
)
30308 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
30309 else if (TREE_CODE (block
) == STATEMENT_LIST
)
30310 DECL_SAVED_TREE (fndecl
) = block
;
30311 if (processing_template_decl
)
30312 add_decl_expr (fndecl
);
30314 cp_check_omp_declare_reduction (fndecl
);
30315 if (cp
== NULL
&& types
.length () > 1)
30316 cp
= cp_token_cache_new (first_token
,
30317 cp_lexer_peek_nth_token (parser
->lexer
, 2));
30318 if (errs
!= errorcount
)
30322 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30327 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30328 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30329 initializer-clause[opt] new-line
30330 #pragma omp declare target new-line */
30333 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
30334 enum pragma_context context
)
30336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30338 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30339 const char *p
= IDENTIFIER_POINTER (id
);
30341 if (strcmp (p
, "simd") == 0)
30343 cp_lexer_consume_token (parser
->lexer
);
30344 cp_parser_omp_declare_simd (parser
, pragma_tok
,
30348 cp_ensure_no_omp_declare_simd (parser
);
30349 if (strcmp (p
, "reduction") == 0)
30351 cp_lexer_consume_token (parser
->lexer
);
30352 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
30356 if (strcmp (p
, "target") == 0)
30358 cp_lexer_consume_token (parser
->lexer
);
30359 cp_parser_omp_declare_target (parser
, pragma_tok
);
30363 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
30365 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30368 /* Main entry point to OpenMP statement pragmas. */
30371 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
30374 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
30375 omp_clause_mask
mask (0);
30377 switch (pragma_tok
->pragma_kind
)
30379 case PRAGMA_OMP_ATOMIC
:
30380 cp_parser_omp_atomic (parser
, pragma_tok
);
30382 case PRAGMA_OMP_CRITICAL
:
30383 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
30385 case PRAGMA_OMP_DISTRIBUTE
:
30386 strcpy (p_name
, "#pragma omp");
30387 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
30389 case PRAGMA_OMP_FOR
:
30390 strcpy (p_name
, "#pragma omp");
30391 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
30393 case PRAGMA_OMP_MASTER
:
30394 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
30396 case PRAGMA_OMP_ORDERED
:
30397 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
30399 case PRAGMA_OMP_PARALLEL
:
30400 strcpy (p_name
, "#pragma omp");
30401 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
30403 case PRAGMA_OMP_SECTIONS
:
30404 strcpy (p_name
, "#pragma omp");
30405 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
30407 case PRAGMA_OMP_SIMD
:
30408 strcpy (p_name
, "#pragma omp");
30409 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
30411 case PRAGMA_OMP_SINGLE
:
30412 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
30414 case PRAGMA_OMP_TASK
:
30415 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
30417 case PRAGMA_OMP_TASKGROUP
:
30418 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
30420 case PRAGMA_OMP_TEAMS
:
30421 strcpy (p_name
, "#pragma omp");
30422 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
30425 gcc_unreachable ();
30429 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30432 /* Transactional Memory parsing routines. */
30434 /* Parse a transaction attribute.
30440 ??? Simplify this when C++0x bracket attributes are
30441 implemented properly. */
30444 cp_parser_txn_attribute_opt (cp_parser
*parser
)
30447 tree attr_name
, attr
= NULL
;
30449 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30450 return cp_parser_attributes_opt (parser
);
30452 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
30454 cp_lexer_consume_token (parser
->lexer
);
30455 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
30458 token
= cp_lexer_peek_token (parser
->lexer
);
30459 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
30461 token
= cp_lexer_consume_token (parser
->lexer
);
30463 attr_name
= (token
->type
== CPP_KEYWORD
30464 /* For keywords, use the canonical spelling,
30465 not the parsed identifier. */
30466 ? ridpointers
[(int) token
->keyword
]
30468 attr
= build_tree_list (attr_name
, NULL_TREE
);
30471 cp_parser_error (parser
, "expected identifier");
30473 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30475 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30479 /* Parse a __transaction_atomic or __transaction_relaxed statement.
30481 transaction-statement:
30482 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
30484 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
30488 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
30490 unsigned char old_in
= parser
->in_transaction
;
30491 unsigned char this_in
= 1, new_in
;
30493 tree stmt
, attrs
, noex
;
30495 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
30496 || keyword
== RID_TRANSACTION_RELAXED
);
30497 token
= cp_parser_require_keyword (parser
, keyword
,
30498 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
30499 : RT_TRANSACTION_RELAXED
));
30500 gcc_assert (token
!= NULL
);
30502 if (keyword
== RID_TRANSACTION_RELAXED
)
30503 this_in
|= TM_STMT_ATTR_RELAXED
;
30506 attrs
= cp_parser_txn_attribute_opt (parser
);
30508 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
30511 /* Parse a noexcept specification. */
30512 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
30514 /* Keep track if we're in the lexical scope of an outer transaction. */
30515 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
30517 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
30519 parser
->in_transaction
= new_in
;
30520 cp_parser_compound_statement (parser
, NULL
, false, false);
30521 parser
->in_transaction
= old_in
;
30523 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
30528 /* Parse a __transaction_atomic or __transaction_relaxed expression.
30530 transaction-expression:
30531 __transaction_atomic txn-noexcept-spec[opt] ( expression )
30532 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
30536 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
30538 unsigned char old_in
= parser
->in_transaction
;
30539 unsigned char this_in
= 1;
30544 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
30545 || keyword
== RID_TRANSACTION_RELAXED
);
30548 error (keyword
== RID_TRANSACTION_RELAXED
30549 ? G_("%<__transaction_relaxed%> without transactional memory "
30551 : G_("%<__transaction_atomic%> without transactional memory "
30552 "support enabled"));
30554 token
= cp_parser_require_keyword (parser
, keyword
,
30555 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
30556 : RT_TRANSACTION_RELAXED
));
30557 gcc_assert (token
!= NULL
);
30559 if (keyword
== RID_TRANSACTION_RELAXED
)
30560 this_in
|= TM_STMT_ATTR_RELAXED
;
30562 /* Set this early. This might mean that we allow transaction_cancel in
30563 an expression that we find out later actually has to be a constexpr.
30564 However, we expect that cxx_constant_value will be able to deal with
30565 this; also, if the noexcept has no constexpr, then what we parse next
30566 really is a transaction's body. */
30567 parser
->in_transaction
= this_in
;
30569 /* Parse a noexcept specification. */
30570 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
30573 if (!noex
|| !noex_expr
30574 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
30576 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
30578 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
30579 expr
= finish_parenthesized_expr (expr
);
30581 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
30585 /* The only expression that is available got parsed for the noexcept
30586 already. noexcept is true then. */
30588 noex
= boolean_true_node
;
30591 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
30592 parser
->in_transaction
= old_in
;
30594 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
30595 return error_mark_node
;
30597 return (flag_tm
? expr
: error_mark_node
);
30600 /* Parse a function-transaction-block.
30602 function-transaction-block:
30603 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
30605 __transaction_atomic txn-attribute[opt] function-try-block
30606 __transaction_relaxed ctor-initializer[opt] function-body
30607 __transaction_relaxed function-try-block
30611 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
30613 unsigned char old_in
= parser
->in_transaction
;
30614 unsigned char new_in
= 1;
30615 tree compound_stmt
, stmt
, attrs
;
30616 bool ctor_initializer_p
;
30619 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
30620 || keyword
== RID_TRANSACTION_RELAXED
);
30621 token
= cp_parser_require_keyword (parser
, keyword
,
30622 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
30623 : RT_TRANSACTION_RELAXED
));
30624 gcc_assert (token
!= NULL
);
30626 if (keyword
== RID_TRANSACTION_RELAXED
)
30627 new_in
|= TM_STMT_ATTR_RELAXED
;
30630 attrs
= cp_parser_txn_attribute_opt (parser
);
30632 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
30635 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
30637 parser
->in_transaction
= new_in
;
30639 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
30640 ctor_initializer_p
= cp_parser_function_try_block (parser
);
30642 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
30643 (parser
, /*in_function_try_block=*/false);
30645 parser
->in_transaction
= old_in
;
30647 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
30649 return ctor_initializer_p
;
30652 /* Parse a __transaction_cancel statement.
30655 __transaction_cancel txn-attribute[opt] ;
30656 __transaction_cancel txn-attribute[opt] throw-expression ;
30658 ??? Cancel and throw is not yet implemented. */
30661 cp_parser_transaction_cancel (cp_parser
*parser
)
30664 bool is_outer
= false;
30667 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
30668 RT_TRANSACTION_CANCEL
);
30669 gcc_assert (token
!= NULL
);
30671 attrs
= cp_parser_txn_attribute_opt (parser
);
30673 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
30675 /* ??? Parse cancel-and-throw here. */
30677 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30681 error_at (token
->location
, "%<__transaction_cancel%> without "
30682 "transactional memory support enabled");
30683 return error_mark_node
;
30685 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
30687 error_at (token
->location
, "%<__transaction_cancel%> within a "
30688 "%<__transaction_relaxed%>");
30689 return error_mark_node
;
30693 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
30694 && !is_tm_may_cancel_outer (current_function_decl
))
30696 error_at (token
->location
, "outer %<__transaction_cancel%> not "
30697 "within outer %<__transaction_atomic%>");
30698 error_at (token
->location
,
30699 " or a %<transaction_may_cancel_outer%> function");
30700 return error_mark_node
;
30703 else if (parser
->in_transaction
== 0)
30705 error_at (token
->location
, "%<__transaction_cancel%> not within "
30706 "%<__transaction_atomic%>");
30707 return error_mark_node
;
30710 stmt
= build_tm_abort_call (token
->location
, is_outer
);
30718 static GTY (()) cp_parser
*the_parser
;
30721 /* Special handling for the first token or line in the file. The first
30722 thing in the file might be #pragma GCC pch_preprocess, which loads a
30723 PCH file, which is a GC collection point. So we need to handle this
30724 first pragma without benefit of an existing lexer structure.
30726 Always returns one token to the caller in *FIRST_TOKEN. This is
30727 either the true first token of the file, or the first token after
30728 the initial pragma. */
30731 cp_parser_initial_pragma (cp_token
*first_token
)
30735 cp_lexer_get_preprocessor_token (NULL
, first_token
);
30736 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
30739 cp_lexer_get_preprocessor_token (NULL
, first_token
);
30740 if (first_token
->type
== CPP_STRING
)
30742 name
= first_token
->u
.value
;
30744 cp_lexer_get_preprocessor_token (NULL
, first_token
);
30745 if (first_token
->type
!= CPP_PRAGMA_EOL
)
30746 error_at (first_token
->location
,
30747 "junk at end of %<#pragma GCC pch_preprocess%>");
30750 error_at (first_token
->location
, "expected string literal");
30752 /* Skip to the end of the pragma. */
30753 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
30754 cp_lexer_get_preprocessor_token (NULL
, first_token
);
30756 /* Now actually load the PCH file. */
30758 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
30760 /* Read one more token to return to our caller. We have to do this
30761 after reading the PCH file in, since its pointers have to be
30763 cp_lexer_get_preprocessor_token (NULL
, first_token
);
30766 /* Normal parsing of a pragma token. Here we can (and must) use the
30770 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
30772 cp_token
*pragma_tok
;
30775 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
30776 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
30777 parser
->lexer
->in_pragma
= true;
30779 id
= pragma_tok
->pragma_kind
;
30780 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
30781 cp_ensure_no_omp_declare_simd (parser
);
30784 case PRAGMA_GCC_PCH_PREPROCESS
:
30785 error_at (pragma_tok
->location
,
30786 "%<#pragma GCC pch_preprocess%> must be first");
30789 case PRAGMA_OMP_BARRIER
:
30792 case pragma_compound
:
30793 cp_parser_omp_barrier (parser
, pragma_tok
);
30796 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
30797 "used in compound statements");
30804 case PRAGMA_OMP_FLUSH
:
30807 case pragma_compound
:
30808 cp_parser_omp_flush (parser
, pragma_tok
);
30811 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
30812 "used in compound statements");
30819 case PRAGMA_OMP_TASKWAIT
:
30822 case pragma_compound
:
30823 cp_parser_omp_taskwait (parser
, pragma_tok
);
30826 error_at (pragma_tok
->location
,
30827 "%<#pragma omp taskwait%> may only be "
30828 "used in compound statements");
30835 case PRAGMA_OMP_TASKYIELD
:
30838 case pragma_compound
:
30839 cp_parser_omp_taskyield (parser
, pragma_tok
);
30842 error_at (pragma_tok
->location
,
30843 "%<#pragma omp taskyield%> may only be "
30844 "used in compound statements");
30851 case PRAGMA_OMP_CANCEL
:
30854 case pragma_compound
:
30855 cp_parser_omp_cancel (parser
, pragma_tok
);
30858 error_at (pragma_tok
->location
,
30859 "%<#pragma omp cancel%> may only be "
30860 "used in compound statements");
30867 case PRAGMA_OMP_CANCELLATION_POINT
:
30870 case pragma_compound
:
30871 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
30874 error_at (pragma_tok
->location
,
30875 "%<#pragma omp cancellation point%> may only be "
30876 "used in compound statements");
30883 case PRAGMA_OMP_THREADPRIVATE
:
30884 cp_parser_omp_threadprivate (parser
, pragma_tok
);
30887 case PRAGMA_OMP_DECLARE_REDUCTION
:
30888 cp_parser_omp_declare (parser
, pragma_tok
, context
);
30891 case PRAGMA_OMP_ATOMIC
:
30892 case PRAGMA_OMP_CRITICAL
:
30893 case PRAGMA_OMP_DISTRIBUTE
:
30894 case PRAGMA_OMP_FOR
:
30895 case PRAGMA_OMP_MASTER
:
30896 case PRAGMA_OMP_ORDERED
:
30897 case PRAGMA_OMP_PARALLEL
:
30898 case PRAGMA_OMP_SECTIONS
:
30899 case PRAGMA_OMP_SIMD
:
30900 case PRAGMA_OMP_SINGLE
:
30901 case PRAGMA_OMP_TASK
:
30902 case PRAGMA_OMP_TASKGROUP
:
30903 case PRAGMA_OMP_TEAMS
:
30904 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
30906 cp_parser_omp_construct (parser
, pragma_tok
);
30909 case PRAGMA_OMP_TARGET
:
30910 return cp_parser_omp_target (parser
, pragma_tok
, context
);
30912 case PRAGMA_OMP_END_DECLARE_TARGET
:
30913 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
30916 case PRAGMA_OMP_SECTION
:
30917 error_at (pragma_tok
->location
,
30918 "%<#pragma omp section%> may only be used in "
30919 "%<#pragma omp sections%> construct");
30924 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30926 tok
= cp_lexer_peek_token (the_parser
->lexer
);
30927 if (tok
->type
!= CPP_KEYWORD
30928 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
30929 && tok
->keyword
!= RID_DO
))
30931 cp_parser_error (parser
, "for, while or do statement expected");
30934 cp_parser_iteration_statement (parser
, true);
30939 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
30940 c_invoke_pragma_handler (id
);
30944 cp_parser_error (parser
, "expected declaration specifiers");
30948 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30952 /* The interface the pragma parsers have to the lexer. */
30955 pragma_lex (tree
*value
)
30958 enum cpp_ttype ret
;
30960 tok
= cp_lexer_peek_token (the_parser
->lexer
);
30963 *value
= tok
->u
.value
;
30965 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
30967 else if (ret
== CPP_STRING
)
30968 *value
= cp_parser_string_literal (the_parser
, false, false);
30971 cp_lexer_consume_token (the_parser
->lexer
);
30972 if (ret
== CPP_KEYWORD
)
30980 /* External interface. */
30982 /* Parse one entire translation unit. */
30985 c_parse_file (void)
30987 static bool already_called
= false;
30989 if (already_called
)
30991 sorry ("inter-module optimizations not implemented for C++");
30994 already_called
= true;
30996 the_parser
= cp_parser_new ();
30997 push_deferring_access_checks (flag_access_control
30998 ? dk_no_deferred
: dk_no_check
);
30999 cp_parser_translation_unit (the_parser
);
31003 /* Create an identifier for a generic parameter type (a synthesized
31004 template parameter implied by `auto' or a concept identifier). */
31006 static GTY(()) int generic_parm_count
;
31008 make_generic_type_name ()
31011 sprintf (buf
, "<auto%d>", ++generic_parm_count
);
31012 return get_identifier (buf
);
31015 /* Predicate that behaves as is_auto_or_concept but matches the parent
31016 node of the generic type rather than the generic type itself. This
31017 allows for type transformation in add_implicit_template_parms. */
31020 tree_type_is_auto_or_concept (const_tree t
)
31022 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
31025 /* Add EXPECT_COUNT implicit template parameters gleaned from the generic
31026 type parameters in PARAMETERS to the CURRENT_TEMPLATE_PARMS (creating a new
31027 template parameter list if necessary). Returns PARAMETERS suitably rewritten
31028 to reference the newly created types or ERROR_MARK_NODE on failure. */
31031 add_implicit_template_parms (cp_parser
*parser
, size_t expect_count
,
31034 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
31036 cp_binding_level
*fn_parms_scope
= current_binding_level
;
31038 bool become_template
=
31039 fn_parms_scope
->level_chain
->kind
!= sk_template_parms
;
31041 size_t synth_count
= 0;
31043 /* Roll back a scope level and either introduce a new template parameter list
31044 or update an existing one. The function scope is added back after template
31045 parameter synthesis below. */
31046 current_binding_level
= fn_parms_scope
->level_chain
;
31048 /* TPARMS tracks the function's template parameter list. This is either a new
31049 chain in the case of a fully implicit function template or an extension of
31050 the function's explicitly specified template parameter list. */
31051 tree tparms
= NULL_TREE
;
31053 if (become_template
)
31055 push_deferring_access_checks (dk_deferred
);
31056 begin_template_parm_list ();
31058 parser
->fully_implicit_function_template_p
= true;
31059 ++parser
->num_template_parameter_lists
;
31063 /* Roll back the innermost template parameter list such that it may be
31064 extended in the loop below as if it were being explicitly declared. */
31066 gcc_assert (current_template_parms
);
31068 /* Pop the innermost template parms into TPARMS. */
31069 tree inner_vec
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
31070 current_template_parms
= TREE_CHAIN (current_template_parms
);
31072 size_t inner_vec_len
= TREE_VEC_LENGTH (inner_vec
);
31073 if (inner_vec_len
!= 0)
31075 tree t
= tparms
= TREE_VEC_ELT (inner_vec
, 0);
31076 for (size_t n
= 1; n
< inner_vec_len
; ++n
)
31077 t
= TREE_CHAIN (t
) = TREE_VEC_ELT (inner_vec
, n
);
31080 ++processing_template_parmlist
;
31083 for (tree p
= parameters
; p
&& synth_count
< expect_count
; p
= TREE_CHAIN (p
))
31085 tree generic_type_ptr
31086 = find_type_usage (TREE_VALUE (p
), tree_type_is_auto_or_concept
);
31088 if (!generic_type_ptr
)
31093 tree synth_id
= make_generic_type_name ();
31094 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
31096 tparms
= process_template_parm (tparms
, DECL_SOURCE_LOCATION (TREE_VALUE
31098 build_tree_list (NULL_TREE
,
31100 /*non_type=*/false,
31101 /*param_pack=*/false);
31103 /* Rewrite the type of P to be the template_parm added above (getdecls is
31104 used to retrieve it since it is the most recent declaration in this
31105 scope). Qualifiers need to be preserved also. */
31107 tree
& cur_type
= TREE_TYPE (generic_type_ptr
);
31108 tree new_type
= TREE_TYPE (getdecls ());
31110 if (TYPE_QUALS (cur_type
))
31111 cur_type
= cp_build_qualified_type (new_type
, TYPE_QUALS (cur_type
));
31113 cur_type
= new_type
;
31116 gcc_assert (synth_count
== expect_count
);
31118 push_binding_level (fn_parms_scope
);
31120 end_template_parm_list (tparms
);
31125 /* Finish the declaration of a fully implicit function template. Such a
31126 template has no explicit template parameter list so has not been through the
31127 normal template head and tail processing. add_implicit_template_parms tries
31128 to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
31129 provided if the declaration is a class member such that its template
31130 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
31131 form is returned. Otherwise NULL_TREE is returned. */
31134 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
31136 gcc_assert (parser
->fully_implicit_function_template_p
);
31138 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
31139 && DECL_VIRTUAL_P (member_decl_opt
))
31141 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
31142 "implicit templates may not be %<virtual%>");
31143 DECL_VIRTUAL_P (member_decl_opt
) = false;
31146 pop_deferring_access_checks ();
31147 if (member_decl_opt
)
31148 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
31149 end_template_decl ();
31151 parser
->fully_implicit_function_template_p
= false;
31152 --parser
->num_template_parameter_lists
;
31154 return member_decl_opt
;
31157 #include "gt-cp-parser.h"