2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
31 #include "trans-mem.h"
34 #include "c-family/c-pragma.h"
37 #include "diagnostic-core.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
43 #include "tree-pretty-print.h"
45 #include "type-utils.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
116 } non_integral_constant
;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 typedef enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_INTERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
188 static cp_lexer
*cp_lexer_new_main
190 static cp_lexer
*cp_lexer_new_from_tokens
191 (cp_token_cache
*tokens
);
192 static void cp_lexer_destroy
194 static int cp_lexer_saving_tokens
196 static cp_token
*cp_lexer_token_at
197 (cp_lexer
*, cp_token_position
);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer
*, cp_token
*);
200 static inline cp_token
*cp_lexer_peek_token
202 static cp_token
*cp_lexer_peek_nth_token
203 (cp_lexer
*, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer
*, enum cpp_ttype
);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer
*, enum cpp_ttype
);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer
*, enum rid
);
210 static cp_token
*cp_lexer_consume_token
212 static void cp_lexer_purge_token
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer
*, cp_token_position
);
216 static void cp_lexer_save_tokens
218 static void cp_lexer_commit_tokens
220 static void cp_lexer_rollback_tokens
222 static void cp_lexer_print_token
223 (FILE *, cp_token
*);
224 static inline bool cp_lexer_debugging_p
226 static void cp_lexer_start_debugging
227 (cp_lexer
*) ATTRIBUTE_UNUSED
;
228 static void cp_lexer_stop_debugging
229 (cp_lexer
*) ATTRIBUTE_UNUSED
;
231 static cp_token_cache
*cp_token_cache_new
232 (cp_token
*, cp_token
*);
234 static void cp_parser_initial_pragma
237 static tree cp_literal_operator_id
240 static void cp_parser_cilk_simd
241 (cp_parser
*, cp_token
*);
242 static bool cp_parser_omp_declare_reduction_exprs
244 static tree cp_parser_cilk_simd_vectorlength
245 (cp_parser
*, tree
, bool);
247 /* Manifest constants. */
248 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
249 #define CP_SAVED_TOKEN_STACK 5
253 /* The stream to which debugging output should be written. */
254 static FILE *cp_lexer_debug_stream
;
256 /* Nonzero if we are parsing an unevaluated operand: an operand to
257 sizeof, typeof, or alignof. */
258 int cp_unevaluated_operand
;
260 /* Dump up to NUM tokens in BUFFER to FILE starting with token
261 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
262 first token in BUFFER. If NUM is 0, dump all the tokens. If
263 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
264 highlighted by surrounding it in [[ ]]. */
267 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
268 cp_token
*start_token
, unsigned num
,
269 cp_token
*curr_token
)
271 unsigned i
, nprinted
;
275 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
281 num
= buffer
->length ();
283 if (start_token
== NULL
)
284 start_token
= buffer
->address ();
286 if (start_token
> buffer
->address ())
288 cp_lexer_print_token (file
, &(*buffer
)[0]);
289 fprintf (file
, " ... ");
294 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
296 if (token
== start_token
)
303 if (token
== curr_token
)
304 fprintf (file
, "[[");
306 cp_lexer_print_token (file
, token
);
308 if (token
== curr_token
)
309 fprintf (file
, "]]");
315 case CPP_CLOSE_BRACE
:
325 if (i
== num
&& i
< buffer
->length ())
327 fprintf (file
, " ... ");
328 cp_lexer_print_token (file
, &buffer
->last ());
331 fprintf (file
, "\n");
335 /* Dump all tokens in BUFFER to stderr. */
338 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
340 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
344 debug (vec
<cp_token
, va_gc
> &ref
)
346 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
350 debug (vec
<cp_token
, va_gc
> *ptr
)
355 fprintf (stderr
, "<nil>\n");
359 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
360 description for T. */
363 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
367 fprintf (file
, "%s: ", desc
);
368 print_node_brief (file
, "", t
, 0);
373 /* Dump parser context C to FILE. */
376 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
378 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
379 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
380 print_node_brief (file
, "", c
->object_type
, 0);
381 fprintf (file
, "}\n");
385 /* Print the stack of parsing contexts to FILE starting with FIRST. */
388 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
391 cp_parser_context
*c
;
393 fprintf (file
, "Parsing context stack:\n");
394 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
396 fprintf (file
, "\t#%u: ", i
);
397 cp_debug_print_context (file
, c
);
402 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
405 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
408 fprintf (file
, "%s: true\n", desc
);
412 /* Print an unparsed function entry UF to FILE. */
415 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
418 cp_default_arg_entry
*default_arg_fn
;
421 fprintf (file
, "\tFunctions with default args:\n");
423 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
426 fprintf (file
, "\t\tClass type: ");
427 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
428 fprintf (file
, "\t\tDeclaration: ");
429 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
430 fprintf (file
, "\n");
433 fprintf (file
, "\n\tFunctions with definitions that require "
434 "post-processing\n\t\t");
435 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
437 print_node_brief (file
, "", fn
, 0);
440 fprintf (file
, "\n");
442 fprintf (file
, "\n\tNon-static data members with initializers that require "
443 "post-processing\n\t\t");
444 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
446 print_node_brief (file
, "", fn
, 0);
449 fprintf (file
, "\n");
453 /* Print the stack of unparsed member functions S to FILE. */
456 cp_debug_print_unparsed_queues (FILE *file
,
457 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
460 cp_unparsed_functions_entry
*uf
;
462 fprintf (file
, "Unparsed functions\n");
463 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
465 fprintf (file
, "#%u:\n", i
);
466 cp_debug_print_unparsed_function (file
, uf
);
471 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
472 the given PARSER. If FILE is NULL, the output is printed on stderr. */
475 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
477 cp_token
*next_token
, *first_token
, *start_token
;
482 next_token
= parser
->lexer
->next_token
;
483 first_token
= parser
->lexer
->buffer
->address ();
484 start_token
= (next_token
> first_token
+ window_size
/ 2)
485 ? next_token
- window_size
/ 2
487 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
492 /* Dump debugging information for the given PARSER. If FILE is NULL,
493 the output is printed on stderr. */
496 cp_debug_parser (FILE *file
, cp_parser
*parser
)
498 const size_t window_size
= 20;
500 expanded_location eloc
;
505 fprintf (file
, "Parser state\n\n");
506 fprintf (file
, "Number of tokens: %u\n",
507 vec_safe_length (parser
->lexer
->buffer
));
508 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
509 cp_debug_print_tree_if_set (file
, "Object scope",
510 parser
->object_scope
);
511 cp_debug_print_tree_if_set (file
, "Qualifying scope",
512 parser
->qualifying_scope
);
513 cp_debug_print_context_stack (file
, parser
->context
);
514 cp_debug_print_flag (file
, "Allow GNU extensions",
515 parser
->allow_gnu_extensions_p
);
516 cp_debug_print_flag (file
, "'>' token is greater-than",
517 parser
->greater_than_is_operator_p
);
518 cp_debug_print_flag (file
, "Default args allowed in current "
519 "parameter list", parser
->default_arg_ok_p
);
520 cp_debug_print_flag (file
, "Parsing integral constant-expression",
521 parser
->integral_constant_expression_p
);
522 cp_debug_print_flag (file
, "Allow non-constant expression in current "
523 "constant-expression",
524 parser
->allow_non_integral_constant_expression_p
);
525 cp_debug_print_flag (file
, "Seen non-constant expression",
526 parser
->non_integral_constant_expression_p
);
527 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
529 parser
->local_variables_forbidden_p
);
530 cp_debug_print_flag (file
, "In unbraced linkage specification",
531 parser
->in_unbraced_linkage_specification_p
);
532 cp_debug_print_flag (file
, "Parsing a declarator",
533 parser
->in_declarator_p
);
534 cp_debug_print_flag (file
, "In template argument list",
535 parser
->in_template_argument_list_p
);
536 cp_debug_print_flag (file
, "Parsing an iteration statement",
537 parser
->in_statement
& IN_ITERATION_STMT
);
538 cp_debug_print_flag (file
, "Parsing a switch statement",
539 parser
->in_statement
& IN_SWITCH_STMT
);
540 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
541 parser
->in_statement
& IN_OMP_BLOCK
);
542 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
543 parser
->in_statement
& IN_CILK_SIMD_FOR
);
544 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
545 parser
->in_statement
& IN_OMP_FOR
);
546 cp_debug_print_flag (file
, "Parsing an if statement",
547 parser
->in_statement
& IN_IF_STMT
);
548 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
549 "context", parser
->in_type_id_in_expr_p
);
550 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
551 parser
->implicit_extern_c
);
552 cp_debug_print_flag (file
, "String expressions should be translated "
553 "to execution character set",
554 parser
->translate_strings_p
);
555 cp_debug_print_flag (file
, "Parsing function body outside of a "
556 "local class", parser
->in_function_body
);
557 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
558 parser
->colon_corrects_to_scope_p
);
559 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
560 parser
->colon_doesnt_start_class_def_p
);
561 if (parser
->type_definition_forbidden_message
)
562 fprintf (file
, "Error message for forbidden type definitions: %s\n",
563 parser
->type_definition_forbidden_message
);
564 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
565 fprintf (file
, "Number of class definitions in progress: %u\n",
566 parser
->num_classes_being_defined
);
567 fprintf (file
, "Number of template parameter lists for the current "
568 "declaration: %u\n", parser
->num_template_parameter_lists
);
569 cp_debug_parser_tokens (file
, parser
, window_size
);
570 token
= parser
->lexer
->next_token
;
571 fprintf (file
, "Next token to parse:\n");
572 fprintf (file
, "\tToken: ");
573 cp_lexer_print_token (file
, token
);
574 eloc
= expand_location (token
->location
);
575 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
576 fprintf (file
, "\tLine: %d\n", eloc
.line
);
577 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
581 debug (cp_parser
&ref
)
583 cp_debug_parser (stderr
, &ref
);
587 debug (cp_parser
*ptr
)
592 fprintf (stderr
, "<nil>\n");
595 /* Allocate memory for a new lexer object and return it. */
598 cp_lexer_alloc (void)
602 c_common_no_more_pch ();
604 /* Allocate the memory. */
605 lexer
= ggc_alloc_cleared_cp_lexer ();
607 /* Initially we are not debugging. */
608 lexer
->debugging_p
= false;
610 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
612 /* Create the buffer. */
613 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
619 /* Create a new main C++ lexer, the lexer that gets tokens from the
623 cp_lexer_new_main (void)
628 /* It's possible that parsing the first pragma will load a PCH file,
629 which is a GC collection point. So we have to do that before
630 allocating any memory. */
631 cp_parser_initial_pragma (&token
);
633 lexer
= cp_lexer_alloc ();
635 /* Put the first token in the buffer. */
636 lexer
->buffer
->quick_push (token
);
638 /* Get the remaining tokens from the preprocessor. */
639 while (token
.type
!= CPP_EOF
)
641 cp_lexer_get_preprocessor_token (lexer
, &token
);
642 vec_safe_push (lexer
->buffer
, token
);
645 lexer
->last_token
= lexer
->buffer
->address ()
646 + lexer
->buffer
->length ()
648 lexer
->next_token
= lexer
->buffer
->length ()
649 ? lexer
->buffer
->address ()
652 /* Subsequent preprocessor diagnostics should use compiler
653 diagnostic functions to get the compiler source location. */
656 gcc_assert (!lexer
->next_token
->purged_p
);
660 /* Create a new lexer whose token stream is primed with the tokens in
661 CACHE. When these tokens are exhausted, no new tokens will be read. */
664 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
666 cp_token
*first
= cache
->first
;
667 cp_token
*last
= cache
->last
;
668 cp_lexer
*lexer
= ggc_alloc_cleared_cp_lexer ();
670 /* We do not own the buffer. */
671 lexer
->buffer
= NULL
;
672 lexer
->next_token
= first
== last
? &eof_token
: first
;
673 lexer
->last_token
= last
;
675 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
677 /* Initially we are not debugging. */
678 lexer
->debugging_p
= false;
680 gcc_assert (!lexer
->next_token
->purged_p
);
684 /* Frees all resources associated with LEXER. */
687 cp_lexer_destroy (cp_lexer
*lexer
)
689 vec_free (lexer
->buffer
);
690 lexer
->saved_tokens
.release ();
694 /* Returns nonzero if debugging information should be output. */
697 cp_lexer_debugging_p (cp_lexer
*lexer
)
699 return lexer
->debugging_p
;
703 static inline cp_token_position
704 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
706 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
708 return lexer
->next_token
- previous_p
;
711 static inline cp_token
*
712 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
718 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
720 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
723 static inline cp_token_position
724 cp_lexer_previous_token_position (cp_lexer
*lexer
)
726 if (lexer
->next_token
== &eof_token
)
727 return lexer
->last_token
- 1;
729 return cp_lexer_token_position (lexer
, true);
732 static inline cp_token
*
733 cp_lexer_previous_token (cp_lexer
*lexer
)
735 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
737 return cp_lexer_token_at (lexer
, tp
);
740 /* nonzero if we are presently saving tokens. */
743 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
745 return lexer
->saved_tokens
.length () != 0;
748 /* Store the next token from the preprocessor in *TOKEN. Return true
749 if we reach EOF. If LEXER is NULL, assume we are handling an
750 initial #pragma pch_preprocess, and thus want the lexer to return
751 processed strings. */
754 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
756 static int is_extern_c
= 0;
758 /* Get a new token from the preprocessor. */
760 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
761 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
762 token
->keyword
= RID_MAX
;
763 token
->pragma_kind
= PRAGMA_NONE
;
764 token
->purged_p
= false;
765 token
->error_reported
= false;
767 /* On some systems, some header files are surrounded by an
768 implicit extern "C" block. Set a flag in the token if it
769 comes from such a header. */
770 is_extern_c
+= pending_lang_change
;
771 pending_lang_change
= 0;
772 token
->implicit_extern_c
= is_extern_c
> 0;
774 /* Check to see if this token is a keyword. */
775 if (token
->type
== CPP_NAME
)
777 if (C_IS_RESERVED_WORD (token
->u
.value
))
779 /* Mark this token as a keyword. */
780 token
->type
= CPP_KEYWORD
;
781 /* Record which keyword. */
782 token
->keyword
= C_RID_CODE (token
->u
.value
);
786 if (warn_cxx0x_compat
787 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
788 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
790 /* Warn about the C++0x keyword (but still treat it as
792 warning (OPT_Wc__0x_compat
,
793 "identifier %qE is a keyword in C++11",
796 /* Clear out the C_RID_CODE so we don't warn about this
797 particular identifier-turned-keyword again. */
798 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
801 token
->keyword
= RID_MAX
;
804 else if (token
->type
== CPP_AT_NAME
)
806 /* This only happens in Objective-C++; it must be a keyword. */
807 token
->type
= CPP_KEYWORD
;
808 switch (C_RID_CODE (token
->u
.value
))
810 /* Replace 'class' with '@class', 'private' with '@private',
811 etc. This prevents confusion with the C++ keyword
812 'class', and makes the tokens consistent with other
813 Objective-C 'AT' keywords. For example '@class' is
814 reported as RID_AT_CLASS which is consistent with
815 '@synchronized', which is reported as
818 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
819 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
820 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
821 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
822 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
823 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
824 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
825 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
828 else if (token
->type
== CPP_PRAGMA
)
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token
->pragma_kind
= ((enum pragma_kind
)
832 TREE_INT_CST_LOW (token
->u
.value
));
833 token
->u
.value
= NULL_TREE
;
837 /* Update the globals input_location and the input file stack from TOKEN. */
839 cp_lexer_set_source_position_from_token (cp_token
*token
)
841 if (token
->type
!= CPP_EOF
)
843 input_location
= token
->location
;
847 /* Update the globals input_location and the input file stack from LEXER. */
849 cp_lexer_set_source_position (cp_lexer
*lexer
)
851 cp_token
*token
= cp_lexer_peek_token (lexer
);
852 cp_lexer_set_source_position_from_token (token
);
855 /* Return a pointer to the next token in the token stream, but do not
858 static inline cp_token
*
859 cp_lexer_peek_token (cp_lexer
*lexer
)
861 if (cp_lexer_debugging_p (lexer
))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
864 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
865 putc ('\n', cp_lexer_debug_stream
);
867 return lexer
->next_token
;
870 /* Return true if the next token has the indicated TYPE. */
873 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
875 return cp_lexer_peek_token (lexer
)->type
== type
;
878 /* Return true if the next token does not have the indicated TYPE. */
881 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
883 return !cp_lexer_next_token_is (lexer
, type
);
886 /* Return true if the next token is the indicated KEYWORD. */
889 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
891 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
895 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
897 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
900 /* Return true if the next token is not the indicated KEYWORD. */
903 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
905 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
908 /* Return true if the next token is a keyword for a decl-specifier. */
911 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
915 token
= cp_lexer_peek_token (lexer
);
916 switch (token
->keyword
)
918 /* auto specifier: storage-class-specifier in C++,
919 simple-type-specifier in C++0x. */
921 /* Storage classes. */
927 /* Elaborated type specifiers. */
933 /* Simple type specifiers. */
948 /* GNU extensions. */
951 /* C++0x extensions. */
953 case RID_UNDERLYING_TYPE
:
961 /* Returns TRUE iff the token T begins a decltype type. */
964 token_is_decltype (cp_token
*t
)
966 return (t
->keyword
== RID_DECLTYPE
967 || t
->type
== CPP_DECLTYPE
);
970 /* Returns TRUE iff the next token begins a decltype type. */
973 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
975 cp_token
*t
= cp_lexer_peek_token (lexer
);
976 return token_is_decltype (t
);
979 /* Return a pointer to the Nth token in the token stream. If N is 1,
980 then this is precisely equivalent to cp_lexer_peek_token (except
981 that it is not inline). One would like to disallow that case, but
982 there is one case (cp_parser_nth_token_starts_template_id) where
983 the caller passes a variable for N and it might be 1. */
986 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
990 /* N is 1-based, not zero-based. */
993 if (cp_lexer_debugging_p (lexer
))
994 fprintf (cp_lexer_debug_stream
,
995 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
998 token
= lexer
->next_token
;
999 gcc_assert (!n
|| token
!= &eof_token
);
1003 if (token
== lexer
->last_token
)
1009 if (!token
->purged_p
)
1013 if (cp_lexer_debugging_p (lexer
))
1015 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1016 putc ('\n', cp_lexer_debug_stream
);
1022 /* Return the next token, and advance the lexer's next_token pointer
1023 to point to the next non-purged token. */
1026 cp_lexer_consume_token (cp_lexer
* lexer
)
1028 cp_token
*token
= lexer
->next_token
;
1030 gcc_assert (token
!= &eof_token
);
1031 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1035 lexer
->next_token
++;
1036 if (lexer
->next_token
== lexer
->last_token
)
1038 lexer
->next_token
= &eof_token
;
1043 while (lexer
->next_token
->purged_p
);
1045 cp_lexer_set_source_position_from_token (token
);
1047 /* Provide debugging output. */
1048 if (cp_lexer_debugging_p (lexer
))
1050 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1051 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1052 putc ('\n', cp_lexer_debug_stream
);
1058 /* Permanently remove the next token from the token stream, and
1059 advance the next_token pointer to refer to the next non-purged
1063 cp_lexer_purge_token (cp_lexer
*lexer
)
1065 cp_token
*tok
= lexer
->next_token
;
1067 gcc_assert (tok
!= &eof_token
);
1068 tok
->purged_p
= true;
1069 tok
->location
= UNKNOWN_LOCATION
;
1070 tok
->u
.value
= NULL_TREE
;
1071 tok
->keyword
= RID_MAX
;
1076 if (tok
== lexer
->last_token
)
1082 while (tok
->purged_p
);
1083 lexer
->next_token
= tok
;
1086 /* Permanently remove all tokens after TOK, up to, but not
1087 including, the token that will be returned next by
1088 cp_lexer_peek_token. */
1091 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1093 cp_token
*peek
= lexer
->next_token
;
1095 if (peek
== &eof_token
)
1096 peek
= lexer
->last_token
;
1098 gcc_assert (tok
< peek
);
1100 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1102 tok
->purged_p
= true;
1103 tok
->location
= UNKNOWN_LOCATION
;
1104 tok
->u
.value
= NULL_TREE
;
1105 tok
->keyword
= RID_MAX
;
1109 /* Begin saving tokens. All tokens consumed after this point will be
1113 cp_lexer_save_tokens (cp_lexer
* lexer
)
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer
))
1117 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1119 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1122 /* Commit to the portion of the token stream most recently saved. */
1125 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1127 /* Provide debugging output. */
1128 if (cp_lexer_debugging_p (lexer
))
1129 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1131 lexer
->saved_tokens
.pop ();
1134 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1135 to the token stream. Stop saving tokens. */
1138 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1140 /* Provide debugging output. */
1141 if (cp_lexer_debugging_p (lexer
))
1142 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1144 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1147 /* Print a representation of the TOKEN on the STREAM. */
1150 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1152 /* We don't use cpp_type2name here because the parser defines
1153 a few tokens of its own. */
1154 static const char *const token_names
[] = {
1155 /* cpplib-defined token types */
1156 #define OP(e, s) #e,
1157 #define TK(e, s) #e,
1161 /* C++ parser token types - see "Manifest constants", above. */
1164 "NESTED_NAME_SPECIFIER",
1167 /* For some tokens, print the associated data. */
1168 switch (token
->type
)
1171 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1172 For example, `struct' is mapped to an INTEGER_CST. */
1173 if (!identifier_p (token
->u
.value
))
1175 /* else fall through */
1177 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1184 case CPP_UTF8STRING
:
1185 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1189 print_generic_expr (stream
, token
->u
.value
, 0);
1193 /* If we have a name for the token, print it out. Otherwise, we
1194 simply give the numeric code. */
1195 if (token
->type
< ARRAY_SIZE(token_names
))
1196 fputs (token_names
[token
->type
], stream
);
1198 fprintf (stream
, "[%d]", token
->type
);
1204 debug (cp_token
&ref
)
1206 cp_lexer_print_token (stderr
, &ref
);
1207 fprintf (stderr
, "\n");
1211 debug (cp_token
*ptr
)
1216 fprintf (stderr
, "<nil>\n");
1220 /* Start emitting debugging information. */
1223 cp_lexer_start_debugging (cp_lexer
* lexer
)
1225 lexer
->debugging_p
= true;
1226 cp_lexer_debug_stream
= stderr
;
1229 /* Stop emitting debugging information. */
1232 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1234 lexer
->debugging_p
= false;
1235 cp_lexer_debug_stream
= NULL
;
1238 /* Create a new cp_token_cache, representing a range of tokens. */
1240 static cp_token_cache
*
1241 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1243 cp_token_cache
*cache
= ggc_alloc_cp_token_cache ();
1244 cache
->first
= first
;
1249 /* Diagnose if #pragma omp declare simd isn't followed immediately
1250 by function declaration or definition. */
1253 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1255 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1257 error ("%<#pragma omp declare simd%> not immediately followed by "
1258 "function declaration or definition");
1259 parser
->omp_declare_simd
= NULL
;
1263 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1264 and put that into "omp declare simd" attribute. */
1267 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1269 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1271 if (fndecl
== error_mark_node
)
1273 parser
->omp_declare_simd
= NULL
;
1276 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1278 cp_ensure_no_omp_declare_simd (parser
);
1284 /* Decl-specifiers. */
1286 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1289 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1291 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1296 /* Nothing other than the parser should be creating declarators;
1297 declarators are a semi-syntactic representation of C++ entities.
1298 Other parts of the front end that need to create entities (like
1299 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1301 static cp_declarator
*make_call_declarator
1302 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1303 static cp_declarator
*make_array_declarator
1304 (cp_declarator
*, tree
);
1305 static cp_declarator
*make_pointer_declarator
1306 (cp_cv_quals
, cp_declarator
*, tree
);
1307 static cp_declarator
*make_reference_declarator
1308 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1309 static cp_parameter_declarator
*make_parameter_declarator
1310 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1311 static cp_declarator
*make_ptrmem_declarator
1312 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1314 /* An erroneous declarator. */
1315 static cp_declarator
*cp_error_declarator
;
1317 /* The obstack on which declarators and related data structures are
1319 static struct obstack declarator_obstack
;
1321 /* Alloc BYTES from the declarator memory pool. */
1323 static inline void *
1324 alloc_declarator (size_t bytes
)
1326 return obstack_alloc (&declarator_obstack
, bytes
);
1329 /* Allocate a declarator of the indicated KIND. Clear fields that are
1330 common to all declarators. */
1332 static cp_declarator
*
1333 make_declarator (cp_declarator_kind kind
)
1335 cp_declarator
*declarator
;
1337 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1338 declarator
->kind
= kind
;
1339 declarator
->attributes
= NULL_TREE
;
1340 declarator
->std_attributes
= NULL_TREE
;
1341 declarator
->declarator
= NULL
;
1342 declarator
->parameter_pack_p
= false;
1343 declarator
->id_loc
= UNKNOWN_LOCATION
;
1348 /* Make a declarator for a generalized identifier. If
1349 QUALIFYING_SCOPE is non-NULL, the identifier is
1350 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1351 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1354 static cp_declarator
*
1355 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1356 special_function_kind sfk
)
1358 cp_declarator
*declarator
;
1360 /* It is valid to write:
1362 class C { void f(); };
1366 The standard is not clear about whether `typedef const C D' is
1367 legal; as of 2002-09-15 the committee is considering that
1368 question. EDG 3.0 allows that syntax. Therefore, we do as
1370 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1371 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1373 gcc_assert (identifier_p (unqualified_name
)
1374 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1375 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1377 declarator
= make_declarator (cdk_id
);
1378 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1379 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1380 declarator
->u
.id
.sfk
= sfk
;
1385 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1386 of modifiers such as const or volatile to apply to the pointer
1387 type, represented as identifiers. ATTRIBUTES represent the attributes that
1388 appertain to the pointer or reference. */
1391 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1394 cp_declarator
*declarator
;
1396 declarator
= make_declarator (cdk_pointer
);
1397 declarator
->declarator
= target
;
1398 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1399 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1402 declarator
->id_loc
= target
->id_loc
;
1403 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1404 target
->parameter_pack_p
= false;
1407 declarator
->parameter_pack_p
= false;
1409 declarator
->std_attributes
= attributes
;
1414 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1415 represent the attributes that appertain to the pointer or
1419 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1420 bool rvalue_ref
, tree attributes
)
1422 cp_declarator
*declarator
;
1424 declarator
= make_declarator (cdk_reference
);
1425 declarator
->declarator
= target
;
1426 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1427 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1430 declarator
->id_loc
= target
->id_loc
;
1431 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1432 target
->parameter_pack_p
= false;
1435 declarator
->parameter_pack_p
= false;
1437 declarator
->std_attributes
= attributes
;
1442 /* Like make_pointer_declarator -- but for a pointer to a non-static
1443 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1444 appertain to the pointer or reference. */
1447 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1448 cp_declarator
*pointee
,
1451 cp_declarator
*declarator
;
1453 declarator
= make_declarator (cdk_ptrmem
);
1454 declarator
->declarator
= pointee
;
1455 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1456 declarator
->u
.pointer
.class_type
= class_type
;
1460 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1461 pointee
->parameter_pack_p
= false;
1464 declarator
->parameter_pack_p
= false;
1466 declarator
->std_attributes
= attributes
;
1471 /* Make a declarator for the function given by TARGET, with the
1472 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1473 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1474 indicates what exceptions can be thrown. */
1477 make_call_declarator (cp_declarator
*target
,
1479 cp_cv_quals cv_qualifiers
,
1480 cp_virt_specifiers virt_specifiers
,
1481 cp_ref_qualifier ref_qualifier
,
1482 tree exception_specification
,
1483 tree late_return_type
)
1485 cp_declarator
*declarator
;
1487 declarator
= make_declarator (cdk_function
);
1488 declarator
->declarator
= target
;
1489 declarator
->u
.function
.parameters
= parms
;
1490 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1491 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1492 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1493 declarator
->u
.function
.exception_specification
= exception_specification
;
1494 declarator
->u
.function
.late_return_type
= late_return_type
;
1497 declarator
->id_loc
= target
->id_loc
;
1498 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1499 target
->parameter_pack_p
= false;
1502 declarator
->parameter_pack_p
= false;
1507 /* Make a declarator for an array of BOUNDS elements, each of which is
1508 defined by ELEMENT. */
1511 make_array_declarator (cp_declarator
*element
, tree bounds
)
1513 cp_declarator
*declarator
;
1515 declarator
= make_declarator (cdk_array
);
1516 declarator
->declarator
= element
;
1517 declarator
->u
.array
.bounds
= bounds
;
1520 declarator
->id_loc
= element
->id_loc
;
1521 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1522 element
->parameter_pack_p
= false;
1525 declarator
->parameter_pack_p
= false;
1530 /* Determine whether the declarator we've seen so far can be a
1531 parameter pack, when followed by an ellipsis. */
1533 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1535 /* Search for a declarator name, or any other declarator that goes
1536 after the point where the ellipsis could appear in a parameter
1537 pack. If we find any of these, then this declarator can not be
1538 made into a parameter pack. */
1540 while (declarator
&& !found
)
1542 switch ((int)declarator
->kind
)
1553 declarator
= declarator
->declarator
;
1561 cp_parameter_declarator
*no_parameters
;
1563 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1564 DECLARATOR and DEFAULT_ARGUMENT. */
1566 cp_parameter_declarator
*
1567 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1568 cp_declarator
*declarator
,
1569 tree default_argument
)
1571 cp_parameter_declarator
*parameter
;
1573 parameter
= ((cp_parameter_declarator
*)
1574 alloc_declarator (sizeof (cp_parameter_declarator
)));
1575 parameter
->next
= NULL
;
1576 if (decl_specifiers
)
1577 parameter
->decl_specifiers
= *decl_specifiers
;
1579 clear_decl_specs (¶meter
->decl_specifiers
);
1580 parameter
->declarator
= declarator
;
1581 parameter
->default_argument
= default_argument
;
1582 parameter
->ellipsis_p
= false;
1587 /* Returns true iff DECLARATOR is a declaration for a function. */
1590 function_declarator_p (const cp_declarator
*declarator
)
1594 if (declarator
->kind
== cdk_function
1595 && declarator
->declarator
->kind
== cdk_id
)
1597 if (declarator
->kind
== cdk_id
1598 || declarator
->kind
== cdk_error
)
1600 declarator
= declarator
->declarator
;
1610 A cp_parser parses the token stream as specified by the C++
1611 grammar. Its job is purely parsing, not semantic analysis. For
1612 example, the parser breaks the token stream into declarators,
1613 expressions, statements, and other similar syntactic constructs.
1614 It does not check that the types of the expressions on either side
1615 of an assignment-statement are compatible, or that a function is
1616 not declared with a parameter of type `void'.
1618 The parser invokes routines elsewhere in the compiler to perform
1619 semantic analysis and to build up the abstract syntax tree for the
1622 The parser (and the template instantiation code, which is, in a
1623 way, a close relative of parsing) are the only parts of the
1624 compiler that should be calling push_scope and pop_scope, or
1625 related functions. The parser (and template instantiation code)
1626 keeps track of what scope is presently active; everything else
1627 should simply honor that. (The code that generates static
1628 initializers may also need to set the scope, in order to check
1629 access control correctly when emitting the initializers.)
1634 The parser is of the standard recursive-descent variety. Upcoming
1635 tokens in the token stream are examined in order to determine which
1636 production to use when parsing a non-terminal. Some C++ constructs
1637 require arbitrary look ahead to disambiguate. For example, it is
1638 impossible, in the general case, to tell whether a statement is an
1639 expression or declaration without scanning the entire statement.
1640 Therefore, the parser is capable of "parsing tentatively." When the
1641 parser is not sure what construct comes next, it enters this mode.
1642 Then, while we attempt to parse the construct, the parser queues up
1643 error messages, rather than issuing them immediately, and saves the
1644 tokens it consumes. If the construct is parsed successfully, the
1645 parser "commits", i.e., it issues any queued error messages and
1646 the tokens that were being preserved are permanently discarded.
1647 If, however, the construct is not parsed successfully, the parser
1648 rolls back its state completely so that it can resume parsing using
1649 a different alternative.
1654 The performance of the parser could probably be improved substantially.
1655 We could often eliminate the need to parse tentatively by looking ahead
1656 a little bit. In some places, this approach might not entirely eliminate
1657 the need to parse tentatively, but it might still speed up the average
1660 /* Flags that are passed to some parsing functions. These values can
1661 be bitwise-ored together. */
1666 CP_PARSER_FLAGS_NONE
= 0x0,
1667 /* The construct is optional. If it is not present, then no error
1668 should be issued. */
1669 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1670 /* When parsing a type-specifier, treat user-defined type-names
1671 as non-type identifiers. */
1672 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1673 /* When parsing a type-specifier, do not try to parse a class-specifier
1674 or enum-specifier. */
1675 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1676 /* When parsing a decl-specifier-seq, only allow type-specifier or
1678 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1681 /* This type is used for parameters and variables which hold
1682 combinations of the above flags. */
1683 typedef int cp_parser_flags
;
1685 /* The different kinds of declarators we want to parse. */
1687 typedef enum cp_parser_declarator_kind
1689 /* We want an abstract declarator. */
1690 CP_PARSER_DECLARATOR_ABSTRACT
,
1691 /* We want a named declarator. */
1692 CP_PARSER_DECLARATOR_NAMED
,
1693 /* We don't mind, but the name must be an unqualified-id. */
1694 CP_PARSER_DECLARATOR_EITHER
1695 } cp_parser_declarator_kind
;
1697 /* The precedence values used to parse binary expressions. The minimum value
1698 of PREC must be 1, because zero is reserved to quickly discriminate
1699 binary operators from other tokens. */
1704 PREC_LOGICAL_OR_EXPRESSION
,
1705 PREC_LOGICAL_AND_EXPRESSION
,
1706 PREC_INCLUSIVE_OR_EXPRESSION
,
1707 PREC_EXCLUSIVE_OR_EXPRESSION
,
1708 PREC_AND_EXPRESSION
,
1709 PREC_EQUALITY_EXPRESSION
,
1710 PREC_RELATIONAL_EXPRESSION
,
1711 PREC_SHIFT_EXPRESSION
,
1712 PREC_ADDITIVE_EXPRESSION
,
1713 PREC_MULTIPLICATIVE_EXPRESSION
,
1715 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1718 /* A mapping from a token type to a corresponding tree node type, with a
1719 precedence value. */
1721 typedef struct cp_parser_binary_operations_map_node
1723 /* The token type. */
1724 enum cpp_ttype token_type
;
1725 /* The corresponding tree code. */
1726 enum tree_code tree_type
;
1727 /* The precedence of this operator. */
1728 enum cp_parser_prec prec
;
1729 } cp_parser_binary_operations_map_node
;
1731 typedef struct cp_parser_expression_stack_entry
1733 /* Left hand side of the binary operation we are currently
1736 /* Original tree code for left hand side, if it was a binary
1737 expression itself (used for -Wparentheses). */
1738 enum tree_code lhs_type
;
1739 /* Tree code for the binary operation we are parsing. */
1740 enum tree_code tree_type
;
1741 /* Precedence of the binary operation we are parsing. */
1742 enum cp_parser_prec prec
;
1743 /* Location of the binary operation we are parsing. */
1745 } cp_parser_expression_stack_entry
;
1747 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1748 entries because precedence levels on the stack are monotonically
1750 typedef struct cp_parser_expression_stack_entry
1751 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1755 /* Constructors and destructors. */
1757 static cp_parser_context
*cp_parser_context_new
1758 (cp_parser_context
*);
1760 /* Class variables. */
1762 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1764 /* The operator-precedence table used by cp_parser_binary_expression.
1765 Transformed into an associative array (binops_by_token) by
1768 static const cp_parser_binary_operations_map_node binops
[] = {
1769 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1770 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1772 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1773 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1774 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1776 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1777 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1779 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1780 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1782 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1783 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1784 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1785 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1787 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1788 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1790 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1792 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1794 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1796 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1798 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1801 /* The same as binops, but initialized by cp_parser_new so that
1802 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1804 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1806 /* Constructors and destructors. */
1808 /* Construct a new context. The context below this one on the stack
1809 is given by NEXT. */
1811 static cp_parser_context
*
1812 cp_parser_context_new (cp_parser_context
* next
)
1814 cp_parser_context
*context
;
1816 /* Allocate the storage. */
1817 if (cp_parser_context_free_list
!= NULL
)
1819 /* Pull the first entry from the free list. */
1820 context
= cp_parser_context_free_list
;
1821 cp_parser_context_free_list
= context
->next
;
1822 memset (context
, 0, sizeof (*context
));
1825 context
= ggc_alloc_cleared_cp_parser_context ();
1827 /* No errors have occurred yet in this context. */
1828 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1829 /* If this is not the bottommost context, copy information that we
1830 need from the previous context. */
1833 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1834 expression, then we are parsing one in this context, too. */
1835 context
->object_type
= next
->object_type
;
1836 /* Thread the stack. */
1837 context
->next
= next
;
1843 /* Managing the unparsed function queues. */
1845 #define unparsed_funs_with_default_args \
1846 parser->unparsed_queues->last ().funs_with_default_args
1847 #define unparsed_funs_with_definitions \
1848 parser->unparsed_queues->last ().funs_with_definitions
1849 #define unparsed_nsdmis \
1850 parser->unparsed_queues->last ().nsdmis
1853 push_unparsed_function_queues (cp_parser
*parser
)
1855 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
};
1856 vec_safe_push (parser
->unparsed_queues
, e
);
1860 pop_unparsed_function_queues (cp_parser
*parser
)
1862 release_tree_vector (unparsed_funs_with_definitions
);
1863 parser
->unparsed_queues
->pop ();
1868 /* Constructors and destructors. */
1870 static cp_parser
*cp_parser_new
1873 /* Routines to parse various constructs.
1875 Those that return `tree' will return the error_mark_node (rather
1876 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1877 Sometimes, they will return an ordinary node if error-recovery was
1878 attempted, even though a parse error occurred. So, to check
1879 whether or not a parse error occurred, you should always use
1880 cp_parser_error_occurred. If the construct is optional (indicated
1881 either by an `_opt' in the name of the function that does the
1882 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1883 the construct is not present. */
1885 /* Lexical conventions [gram.lex] */
1887 static tree cp_parser_identifier
1889 static tree cp_parser_string_literal
1890 (cp_parser
*, bool, bool);
1891 static tree cp_parser_userdef_char_literal
1893 static tree cp_parser_userdef_string_literal
1895 static tree cp_parser_userdef_numeric_literal
1898 /* Basic concepts [gram.basic] */
1900 static bool cp_parser_translation_unit
1903 /* Expressions [gram.expr] */
1905 static tree cp_parser_primary_expression
1906 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1907 static tree cp_parser_id_expression
1908 (cp_parser
*, bool, bool, bool *, bool, bool);
1909 static tree cp_parser_unqualified_id
1910 (cp_parser
*, bool, bool, bool, bool);
1911 static tree cp_parser_nested_name_specifier_opt
1912 (cp_parser
*, bool, bool, bool, bool);
1913 static tree cp_parser_nested_name_specifier
1914 (cp_parser
*, bool, bool, bool, bool);
1915 static tree cp_parser_qualifying_entity
1916 (cp_parser
*, bool, bool, bool, bool, bool);
1917 static tree cp_parser_postfix_expression
1918 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1919 static tree cp_parser_postfix_open_square_expression
1920 (cp_parser
*, tree
, bool, bool);
1921 static tree cp_parser_postfix_dot_deref_expression
1922 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1923 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1924 (cp_parser
*, int, bool, bool, bool *);
1925 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1926 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1927 static void cp_parser_pseudo_destructor_name
1928 (cp_parser
*, tree
, tree
*, tree
*);
1929 static tree cp_parser_unary_expression
1930 (cp_parser
*, bool, bool, cp_id_kind
*);
1931 static enum tree_code cp_parser_unary_operator
1933 static tree cp_parser_new_expression
1935 static vec
<tree
, va_gc
> *cp_parser_new_placement
1937 static tree cp_parser_new_type_id
1938 (cp_parser
*, tree
*);
1939 static cp_declarator
*cp_parser_new_declarator_opt
1941 static cp_declarator
*cp_parser_direct_new_declarator
1943 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1945 static tree cp_parser_delete_expression
1947 static tree cp_parser_cast_expression
1948 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1949 static tree cp_parser_binary_expression
1950 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1951 static tree cp_parser_question_colon_clause
1952 (cp_parser
*, tree
);
1953 static tree cp_parser_assignment_expression
1954 (cp_parser
*, bool, cp_id_kind
*);
1955 static enum tree_code cp_parser_assignment_operator_opt
1957 static tree cp_parser_expression
1958 (cp_parser
*, bool, cp_id_kind
*);
1959 static tree cp_parser_expression
1960 (cp_parser
*, bool, bool, cp_id_kind
*);
1961 static tree cp_parser_constant_expression
1962 (cp_parser
*, bool, bool *);
1963 static tree cp_parser_builtin_offsetof
1965 static tree cp_parser_lambda_expression
1967 static void cp_parser_lambda_introducer
1968 (cp_parser
*, tree
);
1969 static bool cp_parser_lambda_declarator_opt
1970 (cp_parser
*, tree
);
1971 static void cp_parser_lambda_body
1972 (cp_parser
*, tree
);
1974 /* Statements [gram.stmt.stmt] */
1976 static void cp_parser_statement
1977 (cp_parser
*, tree
, bool, bool *);
1978 static void cp_parser_label_for_labeled_statement
1979 (cp_parser
*, tree
);
1980 static tree cp_parser_expression_statement
1981 (cp_parser
*, tree
);
1982 static tree cp_parser_compound_statement
1983 (cp_parser
*, tree
, bool, bool);
1984 static void cp_parser_statement_seq_opt
1985 (cp_parser
*, tree
);
1986 static tree cp_parser_selection_statement
1987 (cp_parser
*, bool *);
1988 static tree cp_parser_condition
1990 static tree cp_parser_iteration_statement
1991 (cp_parser
*, bool);
1992 static bool cp_parser_for_init_statement
1993 (cp_parser
*, tree
*decl
);
1994 static tree cp_parser_for
1995 (cp_parser
*, bool);
1996 static tree cp_parser_c_for
1997 (cp_parser
*, tree
, tree
, bool);
1998 static tree cp_parser_range_for
1999 (cp_parser
*, tree
, tree
, tree
, bool);
2000 static void do_range_for_auto_deduction
2002 static tree cp_parser_perform_range_for_lookup
2003 (tree
, tree
*, tree
*);
2004 static tree cp_parser_range_for_member_function
2006 static tree cp_parser_jump_statement
2008 static void cp_parser_declaration_statement
2011 static tree cp_parser_implicitly_scoped_statement
2012 (cp_parser
*, bool *);
2013 static void cp_parser_already_scoped_statement
2016 /* Declarations [gram.dcl.dcl] */
2018 static void cp_parser_declaration_seq_opt
2020 static void cp_parser_declaration
2022 static void cp_parser_block_declaration
2023 (cp_parser
*, bool);
2024 static void cp_parser_simple_declaration
2025 (cp_parser
*, bool, tree
*);
2026 static void cp_parser_decl_specifier_seq
2027 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2028 static tree cp_parser_storage_class_specifier_opt
2030 static tree cp_parser_function_specifier_opt
2031 (cp_parser
*, cp_decl_specifier_seq
*);
2032 static tree cp_parser_type_specifier
2033 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2035 static tree cp_parser_simple_type_specifier
2036 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2037 static tree cp_parser_type_name
2039 static tree cp_parser_nonclass_name
2040 (cp_parser
* parser
);
2041 static tree cp_parser_elaborated_type_specifier
2042 (cp_parser
*, bool, bool);
2043 static tree cp_parser_enum_specifier
2045 static void cp_parser_enumerator_list
2046 (cp_parser
*, tree
);
2047 static void cp_parser_enumerator_definition
2048 (cp_parser
*, tree
);
2049 static tree cp_parser_namespace_name
2051 static void cp_parser_namespace_definition
2053 static void cp_parser_namespace_body
2055 static tree cp_parser_qualified_namespace_specifier
2057 static void cp_parser_namespace_alias_definition
2059 static bool cp_parser_using_declaration
2060 (cp_parser
*, bool);
2061 static void cp_parser_using_directive
2063 static tree cp_parser_alias_declaration
2065 static void cp_parser_asm_definition
2067 static void cp_parser_linkage_specification
2069 static void cp_parser_static_assert
2070 (cp_parser
*, bool);
2071 static tree cp_parser_decltype
2074 /* Declarators [gram.dcl.decl] */
2076 static tree cp_parser_init_declarator
2077 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*);
2078 static cp_declarator
*cp_parser_declarator
2079 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
2080 static cp_declarator
*cp_parser_direct_declarator
2081 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
2082 static enum tree_code cp_parser_ptr_operator
2083 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2084 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2086 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2088 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2090 static tree cp_parser_late_return_type_opt
2091 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2092 static tree cp_parser_declarator_id
2093 (cp_parser
*, bool);
2094 static tree cp_parser_type_id
2096 static tree cp_parser_template_type_arg
2098 static tree
cp_parser_trailing_type_id (cp_parser
*);
2099 static tree cp_parser_type_id_1
2100 (cp_parser
*, bool, bool);
2101 static void cp_parser_type_specifier_seq
2102 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2103 static tree cp_parser_parameter_declaration_clause
2105 static tree cp_parser_parameter_declaration_list
2106 (cp_parser
*, bool *);
2107 static cp_parameter_declarator
*cp_parser_parameter_declaration
2108 (cp_parser
*, bool, bool *);
2109 static tree cp_parser_default_argument
2110 (cp_parser
*, bool);
2111 static void cp_parser_function_body
2112 (cp_parser
*, bool);
2113 static tree cp_parser_initializer
2114 (cp_parser
*, bool *, bool *);
2115 static tree cp_parser_initializer_clause
2116 (cp_parser
*, bool *);
2117 static tree cp_parser_braced_list
2118 (cp_parser
*, bool*);
2119 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2120 (cp_parser
*, bool *);
2122 static bool cp_parser_ctor_initializer_opt_and_function_body
2123 (cp_parser
*, bool);
2125 static tree cp_parser_late_parsing_omp_declare_simd
2126 (cp_parser
*, tree
);
2128 static tree cp_parser_late_parsing_cilk_simd_fn_info
2129 (cp_parser
*, tree
);
2131 static tree synthesize_implicit_template_parm
2133 static tree finish_fully_implicit_template
2134 (cp_parser
*, tree
);
2136 /* Classes [gram.class] */
2138 static tree cp_parser_class_name
2139 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2140 static tree cp_parser_class_specifier
2142 static tree cp_parser_class_head
2143 (cp_parser
*, bool *);
2144 static enum tag_types cp_parser_class_key
2146 static void cp_parser_member_specification_opt
2148 static void cp_parser_member_declaration
2150 static tree cp_parser_pure_specifier
2152 static tree cp_parser_constant_initializer
2155 /* Derived classes [gram.class.derived] */
2157 static tree cp_parser_base_clause
2159 static tree cp_parser_base_specifier
2162 /* Special member functions [gram.special] */
2164 static tree cp_parser_conversion_function_id
2166 static tree cp_parser_conversion_type_id
2168 static cp_declarator
*cp_parser_conversion_declarator_opt
2170 static bool cp_parser_ctor_initializer_opt
2172 static void cp_parser_mem_initializer_list
2174 static tree cp_parser_mem_initializer
2176 static tree cp_parser_mem_initializer_id
2179 /* Overloading [gram.over] */
2181 static tree cp_parser_operator_function_id
2183 static tree cp_parser_operator
2186 /* Templates [gram.temp] */
2188 static void cp_parser_template_declaration
2189 (cp_parser
*, bool);
2190 static tree cp_parser_template_parameter_list
2192 static tree cp_parser_template_parameter
2193 (cp_parser
*, bool *, bool *);
2194 static tree cp_parser_type_parameter
2195 (cp_parser
*, bool *);
2196 static tree cp_parser_template_id
2197 (cp_parser
*, bool, bool, enum tag_types
, bool);
2198 static tree cp_parser_template_name
2199 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2200 static tree cp_parser_template_argument_list
2202 static tree cp_parser_template_argument
2204 static void cp_parser_explicit_instantiation
2206 static void cp_parser_explicit_specialization
2209 /* Exception handling [gram.exception] */
2211 static tree cp_parser_try_block
2213 static bool cp_parser_function_try_block
2215 static void cp_parser_handler_seq
2217 static void cp_parser_handler
2219 static tree cp_parser_exception_declaration
2221 static tree cp_parser_throw_expression
2223 static tree cp_parser_exception_specification_opt
2225 static tree cp_parser_type_id_list
2228 /* GNU Extensions */
2230 static tree cp_parser_asm_specification_opt
2232 static tree cp_parser_asm_operand_list
2234 static tree cp_parser_asm_clobber_list
2236 static tree cp_parser_asm_label_list
2238 static bool cp_next_tokens_can_be_attribute_p
2240 static bool cp_next_tokens_can_be_gnu_attribute_p
2242 static bool cp_next_tokens_can_be_std_attribute_p
2244 static bool cp_nth_tokens_can_be_std_attribute_p
2245 (cp_parser
*, size_t);
2246 static bool cp_nth_tokens_can_be_gnu_attribute_p
2247 (cp_parser
*, size_t);
2248 static bool cp_nth_tokens_can_be_attribute_p
2249 (cp_parser
*, size_t);
2250 static tree cp_parser_attributes_opt
2252 static tree cp_parser_gnu_attributes_opt
2254 static tree cp_parser_gnu_attribute_list
2256 static tree cp_parser_std_attribute
2258 static tree cp_parser_std_attribute_spec
2260 static tree cp_parser_std_attribute_spec_seq
2262 static bool cp_parser_extension_opt
2263 (cp_parser
*, int *);
2264 static void cp_parser_label_declaration
2267 /* Transactional Memory Extensions */
2269 static tree cp_parser_transaction
2270 (cp_parser
*, enum rid
);
2271 static tree cp_parser_transaction_expression
2272 (cp_parser
*, enum rid
);
2273 static bool cp_parser_function_transaction
2274 (cp_parser
*, enum rid
);
2275 static tree cp_parser_transaction_cancel
2278 enum pragma_context
{
2285 static bool cp_parser_pragma
2286 (cp_parser
*, enum pragma_context
);
2288 /* Objective-C++ Productions */
2290 static tree cp_parser_objc_message_receiver
2292 static tree cp_parser_objc_message_args
2294 static tree cp_parser_objc_message_expression
2296 static tree cp_parser_objc_encode_expression
2298 static tree cp_parser_objc_defs_expression
2300 static tree cp_parser_objc_protocol_expression
2302 static tree cp_parser_objc_selector_expression
2304 static tree cp_parser_objc_expression
2306 static bool cp_parser_objc_selector_p
2308 static tree cp_parser_objc_selector
2310 static tree cp_parser_objc_protocol_refs_opt
2312 static void cp_parser_objc_declaration
2313 (cp_parser
*, tree
);
2314 static tree cp_parser_objc_statement
2316 static bool cp_parser_objc_valid_prefix_attributes
2317 (cp_parser
*, tree
*);
2318 static void cp_parser_objc_at_property_declaration
2320 static void cp_parser_objc_at_synthesize_declaration
2322 static void cp_parser_objc_at_dynamic_declaration
2324 static tree cp_parser_objc_struct_declaration
2327 /* Utility Routines */
2329 static tree cp_parser_lookup_name
2330 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2331 static tree cp_parser_lookup_name_simple
2332 (cp_parser
*, tree
, location_t
);
2333 static tree cp_parser_maybe_treat_template_as_class
2335 static bool cp_parser_check_declarator_template_parameters
2336 (cp_parser
*, cp_declarator
*, location_t
);
2337 static bool cp_parser_check_template_parameters
2338 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2339 static tree cp_parser_simple_cast_expression
2341 static tree cp_parser_global_scope_opt
2342 (cp_parser
*, bool);
2343 static bool cp_parser_constructor_declarator_p
2344 (cp_parser
*, bool);
2345 static tree cp_parser_function_definition_from_specifiers_and_declarator
2346 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2347 static tree cp_parser_function_definition_after_declarator
2348 (cp_parser
*, bool);
2349 static void cp_parser_template_declaration_after_export
2350 (cp_parser
*, bool);
2351 static void cp_parser_perform_template_parameter_access_checks
2352 (vec
<deferred_access_check
, va_gc
> *);
2353 static tree cp_parser_single_declaration
2354 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2355 static tree cp_parser_functional_cast
2356 (cp_parser
*, tree
);
2357 static tree cp_parser_save_member_function_body
2358 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2359 static tree cp_parser_save_nsdmi
2361 static tree cp_parser_enclosed_template_argument_list
2363 static void cp_parser_save_default_args
2364 (cp_parser
*, tree
);
2365 static void cp_parser_late_parsing_for_member
2366 (cp_parser
*, tree
);
2367 static tree cp_parser_late_parse_one_default_arg
2368 (cp_parser
*, tree
, tree
, tree
);
2369 static void cp_parser_late_parsing_nsdmi
2370 (cp_parser
*, tree
);
2371 static void cp_parser_late_parsing_default_args
2372 (cp_parser
*, tree
);
2373 static tree cp_parser_sizeof_operand
2374 (cp_parser
*, enum rid
);
2375 static tree cp_parser_trait_expr
2376 (cp_parser
*, enum rid
);
2377 static bool cp_parser_declares_only_class_p
2379 static void cp_parser_set_storage_class
2380 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2381 static void cp_parser_set_decl_spec_type
2382 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2383 static void set_and_check_decl_spec_loc
2384 (cp_decl_specifier_seq
*decl_specs
,
2385 cp_decl_spec ds
, cp_token
*);
2386 static bool cp_parser_friend_p
2387 (const cp_decl_specifier_seq
*);
2388 static void cp_parser_required_error
2389 (cp_parser
*, required_token
, bool);
2390 static cp_token
*cp_parser_require
2391 (cp_parser
*, enum cpp_ttype
, required_token
);
2392 static cp_token
*cp_parser_require_keyword
2393 (cp_parser
*, enum rid
, required_token
);
2394 static bool cp_parser_token_starts_function_definition_p
2396 static bool cp_parser_next_token_starts_class_definition_p
2398 static bool cp_parser_next_token_ends_template_argument_p
2400 static bool cp_parser_nth_token_starts_template_argument_list_p
2401 (cp_parser
*, size_t);
2402 static enum tag_types cp_parser_token_is_class_key
2404 static void cp_parser_check_class_key
2405 (enum tag_types
, tree type
);
2406 static void cp_parser_check_access_in_redeclaration
2407 (tree type
, location_t location
);
2408 static bool cp_parser_optional_template_keyword
2410 static void cp_parser_pre_parsed_nested_name_specifier
2412 static bool cp_parser_cache_group
2413 (cp_parser
*, enum cpp_ttype
, unsigned);
2414 static tree cp_parser_cache_defarg
2415 (cp_parser
*parser
, bool nsdmi
);
2416 static void cp_parser_parse_tentatively
2418 static void cp_parser_commit_to_tentative_parse
2420 static void cp_parser_commit_to_topmost_tentative_parse
2422 static void cp_parser_abort_tentative_parse
2424 static bool cp_parser_parse_definitely
2426 static inline bool cp_parser_parsing_tentatively
2428 static bool cp_parser_uncommitted_to_tentative_parse_p
2430 static void cp_parser_error
2431 (cp_parser
*, const char *);
2432 static void cp_parser_name_lookup_error
2433 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2434 static bool cp_parser_simulate_error
2436 static bool cp_parser_check_type_definition
2438 static void cp_parser_check_for_definition_in_return_type
2439 (cp_declarator
*, tree
, location_t type_location
);
2440 static void cp_parser_check_for_invalid_template_id
2441 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2442 static bool cp_parser_non_integral_constant_expression
2443 (cp_parser
*, non_integral_constant
);
2444 static void cp_parser_diagnose_invalid_type_name
2445 (cp_parser
*, tree
, tree
, location_t
);
2446 static bool cp_parser_parse_and_diagnose_invalid_type_name
2448 static int cp_parser_skip_to_closing_parenthesis
2449 (cp_parser
*, bool, bool, bool);
2450 static void cp_parser_skip_to_end_of_statement
2452 static void cp_parser_consume_semicolon_at_end_of_statement
2454 static void cp_parser_skip_to_end_of_block_or_statement
2456 static bool cp_parser_skip_to_closing_brace
2458 static void cp_parser_skip_to_end_of_template_parameter_list
2460 static void cp_parser_skip_to_pragma_eol
2461 (cp_parser
*, cp_token
*);
2462 static bool cp_parser_error_occurred
2464 static bool cp_parser_allow_gnu_extensions_p
2466 static bool cp_parser_is_pure_string_literal
2468 static bool cp_parser_is_string_literal
2470 static bool cp_parser_is_keyword
2471 (cp_token
*, enum rid
);
2472 static tree cp_parser_make_typename_type
2473 (cp_parser
*, tree
, tree
, location_t location
);
2474 static cp_declarator
* cp_parser_make_indirect_declarator
2475 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2477 /* Returns nonzero if we are parsing tentatively. */
2480 cp_parser_parsing_tentatively (cp_parser
* parser
)
2482 return parser
->context
->next
!= NULL
;
2485 /* Returns nonzero if TOKEN is a string literal. */
2488 cp_parser_is_pure_string_literal (cp_token
* token
)
2490 return (token
->type
== CPP_STRING
||
2491 token
->type
== CPP_STRING16
||
2492 token
->type
== CPP_STRING32
||
2493 token
->type
== CPP_WSTRING
||
2494 token
->type
== CPP_UTF8STRING
);
2497 /* Returns nonzero if TOKEN is a string literal
2498 of a user-defined string literal. */
2501 cp_parser_is_string_literal (cp_token
* token
)
2503 return (cp_parser_is_pure_string_literal (token
) ||
2504 token
->type
== CPP_STRING_USERDEF
||
2505 token
->type
== CPP_STRING16_USERDEF
||
2506 token
->type
== CPP_STRING32_USERDEF
||
2507 token
->type
== CPP_WSTRING_USERDEF
||
2508 token
->type
== CPP_UTF8STRING_USERDEF
);
2511 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2514 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2516 return token
->keyword
== keyword
;
2519 /* If not parsing tentatively, issue a diagnostic of the form
2520 FILE:LINE: MESSAGE before TOKEN
2521 where TOKEN is the next token in the input stream. MESSAGE
2522 (specified by the caller) is usually of the form "expected
2526 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2528 if (!cp_parser_simulate_error (parser
))
2530 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2531 /* This diagnostic makes more sense if it is tagged to the line
2532 of the token we just peeked at. */
2533 cp_lexer_set_source_position_from_token (token
);
2535 if (token
->type
== CPP_PRAGMA
)
2537 error_at (token
->location
,
2538 "%<#pragma%> is not allowed here");
2539 cp_parser_skip_to_pragma_eol (parser
, token
);
2543 c_parse_error (gmsgid
,
2544 /* Because c_parser_error does not understand
2545 CPP_KEYWORD, keywords are treated like
2547 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2548 token
->u
.value
, token
->flags
);
2552 /* Issue an error about name-lookup failing. NAME is the
2553 IDENTIFIER_NODE DECL is the result of
2554 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2555 the thing that we hoped to find. */
2558 cp_parser_name_lookup_error (cp_parser
* parser
,
2561 name_lookup_error desired
,
2562 location_t location
)
2564 /* If name lookup completely failed, tell the user that NAME was not
2566 if (decl
== error_mark_node
)
2568 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2569 error_at (location
, "%<%E::%E%> has not been declared",
2570 parser
->scope
, name
);
2571 else if (parser
->scope
== global_namespace
)
2572 error_at (location
, "%<::%E%> has not been declared", name
);
2573 else if (parser
->object_scope
2574 && !CLASS_TYPE_P (parser
->object_scope
))
2575 error_at (location
, "request for member %qE in non-class type %qT",
2576 name
, parser
->object_scope
);
2577 else if (parser
->object_scope
)
2578 error_at (location
, "%<%T::%E%> has not been declared",
2579 parser
->object_scope
, name
);
2581 error_at (location
, "%qE has not been declared", name
);
2583 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2588 error_at (location
, "%<%E::%E%> is not a type",
2589 parser
->scope
, name
);
2592 error_at (location
, "%<%E::%E%> is not a class or namespace",
2593 parser
->scope
, name
);
2597 "%<%E::%E%> is not a class, namespace, or enumeration",
2598 parser
->scope
, name
);
2605 else if (parser
->scope
== global_namespace
)
2610 error_at (location
, "%<::%E%> is not a type", name
);
2613 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2617 "%<::%E%> is not a class, namespace, or enumeration",
2629 error_at (location
, "%qE is not a type", name
);
2632 error_at (location
, "%qE is not a class or namespace", name
);
2636 "%qE is not a class, namespace, or enumeration", name
);
2644 /* If we are parsing tentatively, remember that an error has occurred
2645 during this tentative parse. Returns true if the error was
2646 simulated; false if a message should be issued by the caller. */
2649 cp_parser_simulate_error (cp_parser
* parser
)
2651 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2653 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2659 /* This function is called when a type is defined. If type
2660 definitions are forbidden at this point, an error message is
2664 cp_parser_check_type_definition (cp_parser
* parser
)
2666 /* If types are forbidden here, issue a message. */
2667 if (parser
->type_definition_forbidden_message
)
2669 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2670 in the message need to be interpreted. */
2671 error (parser
->type_definition_forbidden_message
);
2677 /* This function is called when the DECLARATOR is processed. The TYPE
2678 was a type defined in the decl-specifiers. If it is invalid to
2679 define a type in the decl-specifiers for DECLARATOR, an error is
2680 issued. TYPE_LOCATION is the location of TYPE and is used
2681 for error reporting. */
2684 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2685 tree type
, location_t type_location
)
2687 /* [dcl.fct] forbids type definitions in return types.
2688 Unfortunately, it's not easy to know whether or not we are
2689 processing a return type until after the fact. */
2691 && (declarator
->kind
== cdk_pointer
2692 || declarator
->kind
== cdk_reference
2693 || declarator
->kind
== cdk_ptrmem
))
2694 declarator
= declarator
->declarator
;
2696 && declarator
->kind
== cdk_function
)
2698 error_at (type_location
,
2699 "new types may not be defined in a return type");
2700 inform (type_location
,
2701 "(perhaps a semicolon is missing after the definition of %qT)",
2706 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2707 "<" in any valid C++ program. If the next token is indeed "<",
2708 issue a message warning the user about what appears to be an
2709 invalid attempt to form a template-id. LOCATION is the location
2710 of the type-specifier (TYPE) */
2713 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2715 enum tag_types tag_type
,
2716 location_t location
)
2718 cp_token_position start
= 0;
2720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2723 error_at (location
, "%qT is not a template", type
);
2724 else if (identifier_p (type
))
2726 if (tag_type
!= none_type
)
2727 error_at (location
, "%qE is not a class template", type
);
2729 error_at (location
, "%qE is not a template", type
);
2732 error_at (location
, "invalid template-id");
2733 /* Remember the location of the invalid "<". */
2734 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2735 start
= cp_lexer_token_position (parser
->lexer
, true);
2736 /* Consume the "<". */
2737 cp_lexer_consume_token (parser
->lexer
);
2738 /* Parse the template arguments. */
2739 cp_parser_enclosed_template_argument_list (parser
);
2740 /* Permanently remove the invalid template arguments so that
2741 this error message is not issued again. */
2743 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2747 /* If parsing an integral constant-expression, issue an error message
2748 about the fact that THING appeared and return true. Otherwise,
2749 return false. In either case, set
2750 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2753 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2754 non_integral_constant thing
)
2756 parser
->non_integral_constant_expression_p
= true;
2757 if (parser
->integral_constant_expression_p
)
2759 if (!parser
->allow_non_integral_constant_expression_p
)
2761 const char *msg
= NULL
;
2765 error ("floating-point literal "
2766 "cannot appear in a constant-expression");
2769 error ("a cast to a type other than an integral or "
2770 "enumeration type cannot appear in a "
2771 "constant-expression");
2774 error ("%<typeid%> operator "
2775 "cannot appear in a constant-expression");
2778 error ("non-constant compound literals "
2779 "cannot appear in a constant-expression");
2782 error ("a function call "
2783 "cannot appear in a constant-expression");
2786 error ("an increment "
2787 "cannot appear in a constant-expression");
2790 error ("an decrement "
2791 "cannot appear in a constant-expression");
2794 error ("an array reference "
2795 "cannot appear in a constant-expression");
2797 case NIC_ADDR_LABEL
:
2798 error ("the address of a label "
2799 "cannot appear in a constant-expression");
2801 case NIC_OVERLOADED
:
2802 error ("calls to overloaded operators "
2803 "cannot appear in a constant-expression");
2805 case NIC_ASSIGNMENT
:
2806 error ("an assignment cannot appear in a constant-expression");
2809 error ("a comma operator "
2810 "cannot appear in a constant-expression");
2812 case NIC_CONSTRUCTOR
:
2813 error ("a call to a constructor "
2814 "cannot appear in a constant-expression");
2816 case NIC_TRANSACTION
:
2817 error ("a transaction expression "
2818 "cannot appear in a constant-expression");
2824 msg
= "__FUNCTION__";
2826 case NIC_PRETTY_FUNC
:
2827 msg
= "__PRETTY_FUNCTION__";
2847 case NIC_PREINCREMENT
:
2850 case NIC_PREDECREMENT
:
2863 error ("%qs cannot appear in a constant-expression", msg
);
2870 /* Emit a diagnostic for an invalid type name. SCOPE is the
2871 qualifying scope (or NULL, if none) for ID. This function commits
2872 to the current active tentative parse, if any. (Otherwise, the
2873 problematic construct might be encountered again later, resulting
2874 in duplicate error messages.) LOCATION is the location of ID. */
2877 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2878 tree scope
, tree id
,
2879 location_t location
)
2881 tree decl
, old_scope
;
2882 cp_parser_commit_to_tentative_parse (parser
);
2883 /* Try to lookup the identifier. */
2884 old_scope
= parser
->scope
;
2885 parser
->scope
= scope
;
2886 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2887 parser
->scope
= old_scope
;
2888 /* If the lookup found a template-name, it means that the user forgot
2889 to specify an argument list. Emit a useful error message. */
2890 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2892 "invalid use of template-name %qE without an argument list",
2894 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2895 error_at (location
, "invalid use of destructor %qD as a type", id
);
2896 else if (TREE_CODE (decl
) == TYPE_DECL
)
2897 /* Something like 'unsigned A a;' */
2898 error_at (location
, "invalid combination of multiple type-specifiers");
2899 else if (!parser
->scope
)
2901 /* Issue an error message. */
2902 error_at (location
, "%qE does not name a type", id
);
2903 /* If we're in a template class, it's possible that the user was
2904 referring to a type from a base class. For example:
2906 template <typename T> struct A { typedef T X; };
2907 template <typename T> struct B : public A<T> { X x; };
2909 The user should have said "typename A<T>::X". */
2910 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2911 inform (location
, "C++11 %<constexpr%> only available with "
2912 "-std=c++11 or -std=gnu++11");
2913 else if (processing_template_decl
&& current_class_type
2914 && TYPE_BINFO (current_class_type
))
2918 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2922 tree base_type
= BINFO_TYPE (b
);
2923 if (CLASS_TYPE_P (base_type
)
2924 && dependent_type_p (base_type
))
2927 /* Go from a particular instantiation of the
2928 template (which will have an empty TYPE_FIELDs),
2929 to the main version. */
2930 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2931 for (field
= TYPE_FIELDS (base_type
);
2933 field
= DECL_CHAIN (field
))
2934 if (TREE_CODE (field
) == TYPE_DECL
2935 && DECL_NAME (field
) == id
)
2938 "(perhaps %<typename %T::%E%> was intended)",
2939 BINFO_TYPE (b
), id
);
2948 /* Here we diagnose qualified-ids where the scope is actually correct,
2949 but the identifier does not resolve to a valid type name. */
2950 else if (parser
->scope
!= error_mark_node
)
2952 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2954 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2955 error_at (location_of (id
),
2956 "%qE in namespace %qE does not name a template type",
2959 error_at (location_of (id
),
2960 "%qE in namespace %qE does not name a type",
2963 else if (CLASS_TYPE_P (parser
->scope
)
2964 && constructor_name_p (id
, parser
->scope
))
2967 error_at (location
, "%<%T::%E%> names the constructor, not"
2968 " the type", parser
->scope
, id
);
2969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2970 error_at (location
, "and %qT has no template constructors",
2973 else if (TYPE_P (parser
->scope
)
2974 && dependent_scope_p (parser
->scope
))
2975 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2976 "%qT is a dependent scope",
2977 parser
->scope
, id
, parser
->scope
);
2978 else if (TYPE_P (parser
->scope
))
2980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2981 error_at (location_of (id
),
2982 "%qE in %q#T does not name a template type",
2985 error_at (location_of (id
),
2986 "%qE in %q#T does not name a type",
2994 /* Check for a common situation where a type-name should be present,
2995 but is not, and issue a sensible error message. Returns true if an
2996 invalid type-name was detected.
2998 The situation handled by this function are variable declarations of the
2999 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3000 Usually, `ID' should name a type, but if we got here it means that it
3001 does not. We try to emit the best possible error message depending on
3002 how exactly the id-expression looks like. */
3005 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3008 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3010 /* Avoid duplicate error about ambiguous lookup. */
3011 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3013 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3014 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3018 cp_parser_parse_tentatively (parser
);
3019 id
= cp_parser_id_expression (parser
,
3020 /*template_keyword_p=*/false,
3021 /*check_dependency_p=*/true,
3022 /*template_p=*/NULL
,
3023 /*declarator_p=*/true,
3024 /*optional_p=*/false);
3025 /* If the next token is a (, this is a function with no explicit return
3026 type, i.e. constructor, destructor or conversion op. */
3027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3028 || TREE_CODE (id
) == TYPE_DECL
)
3030 cp_parser_abort_tentative_parse (parser
);
3033 if (!cp_parser_parse_definitely (parser
))
3036 /* Emit a diagnostic for the invalid type. */
3037 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
3038 id
, token
->location
);
3040 /* If we aren't in the middle of a declarator (i.e. in a
3041 parameter-declaration-clause), skip to the end of the declaration;
3042 there's no point in trying to process it. */
3043 if (!parser
->in_declarator_p
)
3044 cp_parser_skip_to_end_of_block_or_statement (parser
);
3048 /* Consume tokens up to, and including, the next non-nested closing `)'.
3049 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3050 are doing error recovery. Returns -1 if OR_COMMA is true and we
3051 found an unnested comma. */
3054 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3059 unsigned paren_depth
= 0;
3060 unsigned brace_depth
= 0;
3061 unsigned square_depth
= 0;
3063 if (recovering
&& !or_comma
3064 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3069 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3071 switch (token
->type
)
3074 case CPP_PRAGMA_EOL
:
3075 /* If we've run out of tokens, then there is no closing `)'. */
3078 /* This is good for lambda expression capture-lists. */
3079 case CPP_OPEN_SQUARE
:
3082 case CPP_CLOSE_SQUARE
:
3083 if (!square_depth
--)
3088 /* This matches the processing in skip_to_end_of_statement. */
3093 case CPP_OPEN_BRACE
:
3096 case CPP_CLOSE_BRACE
:
3102 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3107 case CPP_OPEN_PAREN
:
3112 case CPP_CLOSE_PAREN
:
3113 if (!brace_depth
&& !paren_depth
--)
3116 cp_lexer_consume_token (parser
->lexer
);
3125 /* Consume the token. */
3126 cp_lexer_consume_token (parser
->lexer
);
3130 /* Consume tokens until we reach the end of the current statement.
3131 Normally, that will be just before consuming a `;'. However, if a
3132 non-nested `}' comes first, then we stop before consuming that. */
3135 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3137 unsigned nesting_depth
= 0;
3139 /* Unwind generic function template scope if necessary. */
3140 if (parser
->fully_implicit_function_template_p
)
3141 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3145 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3147 switch (token
->type
)
3150 case CPP_PRAGMA_EOL
:
3151 /* If we've run out of tokens, stop. */
3155 /* If the next token is a `;', we have reached the end of the
3161 case CPP_CLOSE_BRACE
:
3162 /* If this is a non-nested '}', stop before consuming it.
3163 That way, when confronted with something like:
3167 we stop before consuming the closing '}', even though we
3168 have not yet reached a `;'. */
3169 if (nesting_depth
== 0)
3172 /* If it is the closing '}' for a block that we have
3173 scanned, stop -- but only after consuming the token.
3179 we will stop after the body of the erroneously declared
3180 function, but before consuming the following `typedef'
3182 if (--nesting_depth
== 0)
3184 cp_lexer_consume_token (parser
->lexer
);
3188 case CPP_OPEN_BRACE
:
3196 /* Consume the token. */
3197 cp_lexer_consume_token (parser
->lexer
);
3201 /* This function is called at the end of a statement or declaration.
3202 If the next token is a semicolon, it is consumed; otherwise, error
3203 recovery is attempted. */
3206 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3208 /* Look for the trailing `;'. */
3209 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3211 /* If there is additional (erroneous) input, skip to the end of
3213 cp_parser_skip_to_end_of_statement (parser
);
3214 /* If the next token is now a `;', consume it. */
3215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3216 cp_lexer_consume_token (parser
->lexer
);
3220 /* Skip tokens until we have consumed an entire block, or until we
3221 have consumed a non-nested `;'. */
3224 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3226 int nesting_depth
= 0;
3228 /* Unwind generic function template scope if necessary. */
3229 if (parser
->fully_implicit_function_template_p
)
3230 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3232 while (nesting_depth
>= 0)
3234 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3236 switch (token
->type
)
3239 case CPP_PRAGMA_EOL
:
3240 /* If we've run out of tokens, stop. */
3244 /* Stop if this is an unnested ';'. */
3249 case CPP_CLOSE_BRACE
:
3250 /* Stop if this is an unnested '}', or closes the outermost
3253 if (nesting_depth
< 0)
3259 case CPP_OPEN_BRACE
:
3268 /* Consume the token. */
3269 cp_lexer_consume_token (parser
->lexer
);
3273 /* Skip tokens until a non-nested closing curly brace is the next
3274 token, or there are no more tokens. Return true in the first case,
3278 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3280 unsigned nesting_depth
= 0;
3284 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3286 switch (token
->type
)
3289 case CPP_PRAGMA_EOL
:
3290 /* If we've run out of tokens, stop. */
3293 case CPP_CLOSE_BRACE
:
3294 /* If the next token is a non-nested `}', then we have reached
3295 the end of the current block. */
3296 if (nesting_depth
-- == 0)
3300 case CPP_OPEN_BRACE
:
3301 /* If it the next token is a `{', then we are entering a new
3302 block. Consume the entire block. */
3310 /* Consume the token. */
3311 cp_lexer_consume_token (parser
->lexer
);
3315 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3316 parameter is the PRAGMA token, allowing us to purge the entire pragma
3320 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3324 parser
->lexer
->in_pragma
= false;
3327 token
= cp_lexer_consume_token (parser
->lexer
);
3328 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3330 /* Ensure that the pragma is not parsed again. */
3331 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3334 /* Require pragma end of line, resyncing with it as necessary. The
3335 arguments are as for cp_parser_skip_to_pragma_eol. */
3338 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3340 parser
->lexer
->in_pragma
= false;
3341 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3342 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3345 /* This is a simple wrapper around make_typename_type. When the id is
3346 an unresolved identifier node, we can provide a superior diagnostic
3347 using cp_parser_diagnose_invalid_type_name. */
3350 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3351 tree id
, location_t id_location
)
3354 if (identifier_p (id
))
3356 result
= make_typename_type (scope
, id
, typename_type
,
3357 /*complain=*/tf_none
);
3358 if (result
== error_mark_node
)
3359 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3362 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3365 /* This is a wrapper around the
3366 make_{pointer,ptrmem,reference}_declarator functions that decides
3367 which one to call based on the CODE and CLASS_TYPE arguments. The
3368 CODE argument should be one of the values returned by
3369 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3370 appertain to the pointer or reference. */
3372 static cp_declarator
*
3373 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3374 cp_cv_quals cv_qualifiers
,
3375 cp_declarator
*target
,
3378 if (code
== ERROR_MARK
)
3379 return cp_error_declarator
;
3381 if (code
== INDIRECT_REF
)
3382 if (class_type
== NULL_TREE
)
3383 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3385 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3386 target
, attributes
);
3387 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3388 return make_reference_declarator (cv_qualifiers
, target
,
3390 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3391 return make_reference_declarator (cv_qualifiers
, target
,
3396 /* Create a new C++ parser. */
3399 cp_parser_new (void)
3405 /* cp_lexer_new_main is called before doing GC allocation because
3406 cp_lexer_new_main might load a PCH file. */
3407 lexer
= cp_lexer_new_main ();
3409 /* Initialize the binops_by_token so that we can get the tree
3410 directly from the token. */
3411 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3412 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3414 parser
= ggc_alloc_cleared_cp_parser ();
3415 parser
->lexer
= lexer
;
3416 parser
->context
= cp_parser_context_new (NULL
);
3418 /* For now, we always accept GNU extensions. */
3419 parser
->allow_gnu_extensions_p
= 1;
3421 /* The `>' token is a greater-than operator, not the end of a
3423 parser
->greater_than_is_operator_p
= true;
3425 parser
->default_arg_ok_p
= true;
3427 /* We are not parsing a constant-expression. */
3428 parser
->integral_constant_expression_p
= false;
3429 parser
->allow_non_integral_constant_expression_p
= false;
3430 parser
->non_integral_constant_expression_p
= false;
3432 /* Local variable names are not forbidden. */
3433 parser
->local_variables_forbidden_p
= false;
3435 /* We are not processing an `extern "C"' declaration. */
3436 parser
->in_unbraced_linkage_specification_p
= false;
3438 /* We are not processing a declarator. */
3439 parser
->in_declarator_p
= false;
3441 /* We are not processing a template-argument-list. */
3442 parser
->in_template_argument_list_p
= false;
3444 /* We are not in an iteration statement. */
3445 parser
->in_statement
= 0;
3447 /* We are not in a switch statement. */
3448 parser
->in_switch_statement_p
= false;
3450 /* We are not parsing a type-id inside an expression. */
3451 parser
->in_type_id_in_expr_p
= false;
3453 /* Declarations aren't implicitly extern "C". */
3454 parser
->implicit_extern_c
= false;
3456 /* String literals should be translated to the execution character set. */
3457 parser
->translate_strings_p
= true;
3459 /* We are not parsing a function body. */
3460 parser
->in_function_body
= false;
3462 /* We can correct until told otherwise. */
3463 parser
->colon_corrects_to_scope_p
= true;
3465 /* The unparsed function queue is empty. */
3466 push_unparsed_function_queues (parser
);
3468 /* There are no classes being defined. */
3469 parser
->num_classes_being_defined
= 0;
3471 /* No template parameters apply. */
3472 parser
->num_template_parameter_lists
= 0;
3474 /* Not declaring an implicit function template. */
3475 parser
->auto_is_implicit_function_template_parm_p
= false;
3476 parser
->fully_implicit_function_template_p
= false;
3477 parser
->implicit_template_parms
= 0;
3478 parser
->implicit_template_scope
= 0;
3483 /* Create a cp_lexer structure which will emit the tokens in CACHE
3484 and push it onto the parser's lexer stack. This is used for delayed
3485 parsing of in-class method bodies and default arguments, and should
3486 not be confused with tentative parsing. */
3488 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3490 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3491 lexer
->next
= parser
->lexer
;
3492 parser
->lexer
= lexer
;
3494 /* Move the current source position to that of the first token in the
3496 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3499 /* Pop the top lexer off the parser stack. This is never used for the
3500 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3502 cp_parser_pop_lexer (cp_parser
*parser
)
3504 cp_lexer
*lexer
= parser
->lexer
;
3505 parser
->lexer
= lexer
->next
;
3506 cp_lexer_destroy (lexer
);
3508 /* Put the current source position back where it was before this
3509 lexer was pushed. */
3510 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3513 /* Lexical conventions [gram.lex] */
3515 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3519 cp_parser_identifier (cp_parser
* parser
)
3523 /* Look for the identifier. */
3524 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3525 /* Return the value. */
3526 return token
? token
->u
.value
: error_mark_node
;
3529 /* Parse a sequence of adjacent string constants. Returns a
3530 TREE_STRING representing the combined, nul-terminated string
3531 constant. If TRANSLATE is true, translate the string to the
3532 execution character set. If WIDE_OK is true, a wide string is
3535 C++98 [lex.string] says that if a narrow string literal token is
3536 adjacent to a wide string literal token, the behavior is undefined.
3537 However, C99 6.4.5p4 says that this results in a wide string literal.
3538 We follow C99 here, for consistency with the C front end.
3540 This code is largely lifted from lex_string() in c-lex.c.
3542 FUTURE: ObjC++ will need to handle @-strings here. */
3544 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3548 struct obstack str_ob
;
3549 cpp_string str
, istr
, *strs
;
3551 enum cpp_ttype type
, curr_type
;
3552 int have_suffix_p
= 0;
3554 tree suffix_id
= NULL_TREE
;
3555 bool curr_tok_is_userdef_p
= false;
3557 tok
= cp_lexer_peek_token (parser
->lexer
);
3558 if (!cp_parser_is_string_literal (tok
))
3560 cp_parser_error (parser
, "expected string-literal");
3561 return error_mark_node
;
3564 if (cpp_userdef_string_p (tok
->type
))
3566 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3567 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3568 curr_tok_is_userdef_p
= true;
3572 string_tree
= tok
->u
.value
;
3573 curr_type
= tok
->type
;
3577 /* Try to avoid the overhead of creating and destroying an obstack
3578 for the common case of just one string. */
3579 if (!cp_parser_is_string_literal
3580 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3582 cp_lexer_consume_token (parser
->lexer
);
3584 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3585 str
.len
= TREE_STRING_LENGTH (string_tree
);
3588 if (curr_tok_is_userdef_p
)
3590 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3592 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3595 curr_type
= tok
->type
;
3601 gcc_obstack_init (&str_ob
);
3606 cp_lexer_consume_token (parser
->lexer
);
3608 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3609 str
.len
= TREE_STRING_LENGTH (string_tree
);
3611 if (curr_tok_is_userdef_p
)
3613 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3614 if (have_suffix_p
== 0)
3616 suffix_id
= curr_suffix_id
;
3619 else if (have_suffix_p
== 1
3620 && curr_suffix_id
!= suffix_id
)
3622 error ("inconsistent user-defined literal suffixes"
3623 " %qD and %qD in string literal",
3624 suffix_id
, curr_suffix_id
);
3627 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3630 curr_type
= tok
->type
;
3632 if (type
!= curr_type
)
3634 if (type
== CPP_STRING
)
3636 else if (curr_type
!= CPP_STRING
)
3637 error_at (tok
->location
,
3638 "unsupported non-standard concatenation "
3639 "of string literals");
3642 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3644 tok
= cp_lexer_peek_token (parser
->lexer
);
3645 if (cpp_userdef_string_p (tok
->type
))
3647 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3648 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3649 curr_tok_is_userdef_p
= true;
3653 string_tree
= tok
->u
.value
;
3654 curr_type
= tok
->type
;
3655 curr_tok_is_userdef_p
= false;
3658 while (cp_parser_is_string_literal (tok
));
3660 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3663 if (type
!= CPP_STRING
&& !wide_ok
)
3665 cp_parser_error (parser
, "a wide string is invalid in this context");
3669 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3670 (parse_in
, strs
, count
, &istr
, type
))
3672 value
= build_string (istr
.len
, (const char *)istr
.text
);
3673 free (CONST_CAST (unsigned char *, istr
.text
));
3679 case CPP_UTF8STRING
:
3680 TREE_TYPE (value
) = char_array_type_node
;
3683 TREE_TYPE (value
) = char16_array_type_node
;
3686 TREE_TYPE (value
) = char32_array_type_node
;
3689 TREE_TYPE (value
) = wchar_array_type_node
;
3693 value
= fix_string_type (value
);
3697 tree literal
= build_userdef_literal (suffix_id
, value
,
3698 OT_NONE
, NULL_TREE
);
3699 tok
->u
.value
= literal
;
3700 return cp_parser_userdef_string_literal (tok
);
3704 /* cpp_interpret_string has issued an error. */
3705 value
= error_mark_node
;
3708 obstack_free (&str_ob
, 0);
3713 /* Look up a literal operator with the name and the exact arguments. */
3716 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3719 decl
= lookup_name (name
);
3720 if (!decl
|| !is_overloaded_fn (decl
))
3721 return error_mark_node
;
3723 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3727 tree fn
= OVL_CURRENT (fns
);
3728 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3729 if (parmtypes
!= NULL_TREE
)
3731 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3732 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3734 tree tparm
= TREE_VALUE (parmtypes
);
3735 tree targ
= TREE_TYPE ((*args
)[ix
]);
3736 bool ptr
= TYPE_PTR_P (tparm
);
3737 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3738 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3740 || !same_type_p (TREE_TYPE (tparm
),
3745 && ix
== vec_safe_length (args
)
3746 /* May be this should be sufficient_parms_p instead,
3747 depending on how exactly should user-defined literals
3748 work in presence of default arguments on the literal
3749 operator parameters. */
3750 && parmtypes
== void_list_node
)
3755 return error_mark_node
;
3758 /* Parse a user-defined char constant. Returns a call to a user-defined
3759 literal operator taking the character as an argument. */
3762 cp_parser_userdef_char_literal (cp_parser
*parser
)
3764 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3765 tree literal
= token
->u
.value
;
3766 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3767 tree value
= USERDEF_LITERAL_VALUE (literal
);
3768 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3771 /* Build up a call to the user-defined operator */
3772 /* Lookup the name we got back from the id-expression. */
3773 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3774 vec_safe_push (args
, value
);
3775 decl
= lookup_literal_operator (name
, args
);
3776 if (!decl
|| decl
== error_mark_node
)
3778 error ("unable to find character literal operator %qD with %qT argument",
3779 name
, TREE_TYPE (value
));
3780 release_tree_vector (args
);
3781 return error_mark_node
;
3783 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3784 release_tree_vector (args
);
3785 if (result
!= error_mark_node
)
3788 error ("unable to find character literal operator %qD with %qT argument",
3789 name
, TREE_TYPE (value
));
3790 return error_mark_node
;
3793 /* A subroutine of cp_parser_userdef_numeric_literal to
3794 create a char... template parameter pack from a string node. */
3797 make_char_string_pack (tree value
)
3800 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3801 const char *str
= TREE_STRING_POINTER (value
);
3802 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3803 tree argvec
= make_tree_vec (1);
3805 /* Fill in CHARVEC with all of the parameters. */
3806 charvec
= make_tree_vec (len
);
3807 for (i
= 0; i
< len
; ++i
)
3808 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3810 /* Build the argument packs. */
3811 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3812 TREE_TYPE (argpack
) = char_type_node
;
3814 TREE_VEC_ELT (argvec
, 0) = argpack
;
3819 /* A subroutine of cp_parser_userdef_numeric_literal to
3820 create a char... template parameter pack from a string node. */
3823 make_string_pack (tree value
)
3826 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3827 const unsigned char *str
3828 = (const unsigned char *) TREE_STRING_POINTER (value
);
3829 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3830 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3831 tree argvec
= make_tree_vec (2);
3833 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3834 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3836 /* First template parm is character type. */
3837 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3839 /* Fill in CHARVEC with all of the parameters. */
3840 charvec
= make_tree_vec (len
);
3841 for (int i
= 0; i
< len
; ++i
)
3842 TREE_VEC_ELT (charvec
, i
)
3843 = double_int_to_tree (str_char_type_node
,
3844 double_int::from_buffer (str
+ i
* sz
, sz
));
3846 /* Build the argument packs. */
3847 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3848 TREE_TYPE (argpack
) = str_char_type_node
;
3850 TREE_VEC_ELT (argvec
, 1) = argpack
;
3855 /* Parse a user-defined numeric constant. returns a call to a user-defined
3856 literal operator. */
3859 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3861 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3862 tree literal
= token
->u
.value
;
3863 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3864 tree value
= USERDEF_LITERAL_VALUE (literal
);
3865 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3866 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3867 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3869 vec
<tree
, va_gc
> *args
;
3871 /* Look for a literal operator taking the exact type of numeric argument
3872 as the literal value. */
3873 args
= make_tree_vector ();
3874 vec_safe_push (args
, value
);
3875 decl
= lookup_literal_operator (name
, args
);
3876 if (decl
&& decl
!= error_mark_node
)
3878 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3879 if (result
!= error_mark_node
)
3881 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3882 warning_at (token
->location
, OPT_Woverflow
,
3883 "integer literal exceeds range of %qT type",
3884 long_long_unsigned_type_node
);
3888 warning_at (token
->location
, OPT_Woverflow
,
3889 "floating literal exceeds range of %qT type",
3890 long_double_type_node
);
3891 else if (overflow
< 0)
3892 warning_at (token
->location
, OPT_Woverflow
,
3893 "floating literal truncated to zero");
3895 release_tree_vector (args
);
3899 release_tree_vector (args
);
3901 /* If the numeric argument didn't work, look for a raw literal
3902 operator taking a const char* argument consisting of the number
3903 in string format. */
3904 args
= make_tree_vector ();
3905 vec_safe_push (args
, num_string
);
3906 decl
= lookup_literal_operator (name
, args
);
3907 if (decl
&& decl
!= error_mark_node
)
3909 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3910 if (result
!= error_mark_node
)
3912 release_tree_vector (args
);
3916 release_tree_vector (args
);
3918 /* If the raw literal didn't work, look for a non-type template
3919 function with parameter pack char.... Call the function with
3920 template parameter characters representing the number. */
3921 args
= make_tree_vector ();
3922 decl
= lookup_literal_operator (name
, args
);
3923 if (decl
&& decl
!= error_mark_node
)
3925 tree tmpl_args
= make_char_string_pack (num_string
);
3926 decl
= lookup_template_function (decl
, tmpl_args
);
3927 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3928 if (result
!= error_mark_node
)
3930 release_tree_vector (args
);
3934 release_tree_vector (args
);
3936 error ("unable to find numeric literal operator %qD", name
);
3937 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
3938 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
3939 "to enable more built-in suffixes");
3940 return error_mark_node
;
3943 /* Parse a user-defined string constant. Returns a call to a user-defined
3944 literal operator taking a character pointer and the length of the string
3948 cp_parser_userdef_string_literal (cp_token
*token
)
3950 tree literal
= token
->u
.value
;
3951 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3952 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3953 tree value
= USERDEF_LITERAL_VALUE (literal
);
3954 int len
= TREE_STRING_LENGTH (value
)
3955 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
3957 vec
<tree
, va_gc
> *args
;
3959 /* Look for a template function with typename parameter CharT
3960 and parameter pack CharT... Call the function with
3961 template parameter characters representing the string. */
3962 args
= make_tree_vector ();
3963 decl
= lookup_literal_operator (name
, args
);
3964 if (decl
&& decl
!= error_mark_node
)
3966 tree tmpl_args
= make_string_pack (value
);
3967 decl
= lookup_template_function (decl
, tmpl_args
);
3968 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3969 if (result
!= error_mark_node
)
3971 release_tree_vector (args
);
3975 release_tree_vector (args
);
3977 /* Build up a call to the user-defined operator */
3978 /* Lookup the name we got back from the id-expression. */
3979 args
= make_tree_vector ();
3980 vec_safe_push (args
, value
);
3981 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
3982 decl
= lookup_name (name
);
3983 if (!decl
|| decl
== error_mark_node
)
3985 error ("unable to find string literal operator %qD", name
);
3986 release_tree_vector (args
);
3987 return error_mark_node
;
3989 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3990 release_tree_vector (args
);
3991 if (result
!= error_mark_node
)
3994 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3995 name
, TREE_TYPE (value
), size_type_node
);
3996 return error_mark_node
;
4000 /* Basic concepts [gram.basic] */
4002 /* Parse a translation-unit.
4005 declaration-seq [opt]
4007 Returns TRUE if all went well. */
4010 cp_parser_translation_unit (cp_parser
* parser
)
4012 /* The address of the first non-permanent object on the declarator
4014 static void *declarator_obstack_base
;
4018 /* Create the declarator obstack, if necessary. */
4019 if (!cp_error_declarator
)
4021 gcc_obstack_init (&declarator_obstack
);
4022 /* Create the error declarator. */
4023 cp_error_declarator
= make_declarator (cdk_error
);
4024 /* Create the empty parameter list. */
4025 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4026 /* Remember where the base of the declarator obstack lies. */
4027 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4030 cp_parser_declaration_seq_opt (parser
);
4032 /* If there are no tokens left then all went well. */
4033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4035 /* Get rid of the token array; we don't need it any more. */
4036 cp_lexer_destroy (parser
->lexer
);
4037 parser
->lexer
= NULL
;
4039 /* This file might have been a context that's implicitly extern
4040 "C". If so, pop the lang context. (Only relevant for PCH.) */
4041 if (parser
->implicit_extern_c
)
4043 pop_lang_context ();
4044 parser
->implicit_extern_c
= false;
4048 finish_translation_unit ();
4054 cp_parser_error (parser
, "expected declaration");
4058 /* Make sure the declarator obstack was fully cleaned up. */
4059 gcc_assert (obstack_next_free (&declarator_obstack
)
4060 == declarator_obstack_base
);
4062 /* All went well. */
4066 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4067 decltype context. */
4069 static inline tsubst_flags_t
4070 complain_flags (bool decltype_p
)
4072 tsubst_flags_t complain
= tf_warning_or_error
;
4074 complain
|= tf_decltype
;
4078 /* Expressions [gram.expr] */
4080 /* Parse a primary-expression.
4091 ( compound-statement )
4092 __builtin_va_arg ( assignment-expression , type-id )
4093 __builtin_offsetof ( type-id , offsetof-expression )
4096 __has_nothrow_assign ( type-id )
4097 __has_nothrow_constructor ( type-id )
4098 __has_nothrow_copy ( type-id )
4099 __has_trivial_assign ( type-id )
4100 __has_trivial_constructor ( type-id )
4101 __has_trivial_copy ( type-id )
4102 __has_trivial_destructor ( type-id )
4103 __has_virtual_destructor ( type-id )
4104 __is_abstract ( type-id )
4105 __is_base_of ( type-id , type-id )
4106 __is_class ( type-id )
4107 __is_convertible_to ( type-id , type-id )
4108 __is_empty ( type-id )
4109 __is_enum ( type-id )
4110 __is_final ( type-id )
4111 __is_literal_type ( type-id )
4112 __is_pod ( type-id )
4113 __is_polymorphic ( type-id )
4114 __is_std_layout ( type-id )
4115 __is_trivial ( type-id )
4116 __is_union ( type-id )
4118 Objective-C++ Extension:
4126 ADDRESS_P is true iff this expression was immediately preceded by
4127 "&" and therefore might denote a pointer-to-member. CAST_P is true
4128 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4129 true iff this expression is a template argument.
4131 Returns a representation of the expression. Upon return, *IDK
4132 indicates what kind of id-expression (if any) was present. */
4135 cp_parser_primary_expression (cp_parser
*parser
,
4138 bool template_arg_p
,
4142 cp_token
*token
= NULL
;
4144 /* Assume the primary expression is not an id-expression. */
4145 *idk
= CP_ID_KIND_NONE
;
4147 /* Peek at the next token. */
4148 token
= cp_lexer_peek_token (parser
->lexer
);
4149 switch (token
->type
)
4158 user-defined-literal */
4164 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4165 return cp_parser_userdef_numeric_literal (parser
);
4166 token
= cp_lexer_consume_token (parser
->lexer
);
4167 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4169 error_at (token
->location
,
4170 "fixed-point types not supported in C++");
4171 return error_mark_node
;
4173 /* Floating-point literals are only allowed in an integral
4174 constant expression if they are cast to an integral or
4175 enumeration type. */
4176 if (TREE_CODE (token
->u
.value
) == REAL_CST
4177 && parser
->integral_constant_expression_p
4180 /* CAST_P will be set even in invalid code like "int(2.7 +
4181 ...)". Therefore, we have to check that the next token
4182 is sure to end the cast. */
4185 cp_token
*next_token
;
4187 next_token
= cp_lexer_peek_token (parser
->lexer
);
4188 if (/* The comma at the end of an
4189 enumerator-definition. */
4190 next_token
->type
!= CPP_COMMA
4191 /* The curly brace at the end of an enum-specifier. */
4192 && next_token
->type
!= CPP_CLOSE_BRACE
4193 /* The end of a statement. */
4194 && next_token
->type
!= CPP_SEMICOLON
4195 /* The end of the cast-expression. */
4196 && next_token
->type
!= CPP_CLOSE_PAREN
4197 /* The end of an array bound. */
4198 && next_token
->type
!= CPP_CLOSE_SQUARE
4199 /* The closing ">" in a template-argument-list. */
4200 && (next_token
->type
!= CPP_GREATER
4201 || parser
->greater_than_is_operator_p
)
4202 /* C++0x only: A ">>" treated like two ">" tokens,
4203 in a template-argument-list. */
4204 && (next_token
->type
!= CPP_RSHIFT
4205 || (cxx_dialect
== cxx98
)
4206 || parser
->greater_than_is_operator_p
))
4210 /* If we are within a cast, then the constraint that the
4211 cast is to an integral or enumeration type will be
4212 checked at that point. If we are not within a cast, then
4213 this code is invalid. */
4215 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4217 return token
->u
.value
;
4219 case CPP_CHAR_USERDEF
:
4220 case CPP_CHAR16_USERDEF
:
4221 case CPP_CHAR32_USERDEF
:
4222 case CPP_WCHAR_USERDEF
:
4223 return cp_parser_userdef_char_literal (parser
);
4229 case CPP_UTF8STRING
:
4230 case CPP_STRING_USERDEF
:
4231 case CPP_STRING16_USERDEF
:
4232 case CPP_STRING32_USERDEF
:
4233 case CPP_WSTRING_USERDEF
:
4234 case CPP_UTF8STRING_USERDEF
:
4235 /* ??? Should wide strings be allowed when parser->translate_strings_p
4236 is false (i.e. in attributes)? If not, we can kill the third
4237 argument to cp_parser_string_literal. */
4238 return cp_parser_string_literal (parser
,
4239 parser
->translate_strings_p
,
4242 case CPP_OPEN_PAREN
:
4245 bool saved_greater_than_is_operator_p
;
4247 /* Consume the `('. */
4248 cp_lexer_consume_token (parser
->lexer
);
4249 /* Within a parenthesized expression, a `>' token is always
4250 the greater-than operator. */
4251 saved_greater_than_is_operator_p
4252 = parser
->greater_than_is_operator_p
;
4253 parser
->greater_than_is_operator_p
= true;
4254 /* If we see `( { ' then we are looking at the beginning of
4255 a GNU statement-expression. */
4256 if (cp_parser_allow_gnu_extensions_p (parser
)
4257 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
4259 /* Statement-expressions are not allowed by the standard. */
4260 pedwarn (token
->location
, OPT_Wpedantic
,
4261 "ISO C++ forbids braced-groups within expressions");
4263 /* And they're not allowed outside of a function-body; you
4264 cannot, for example, write:
4266 int i = ({ int j = 3; j + 1; });
4268 at class or namespace scope. */
4269 if (!parser
->in_function_body
4270 || parser
->in_template_argument_list_p
)
4272 error_at (token
->location
,
4273 "statement-expressions are not allowed outside "
4274 "functions nor in template-argument lists");
4275 cp_parser_skip_to_end_of_block_or_statement (parser
);
4276 expr
= error_mark_node
;
4280 /* Start the statement-expression. */
4281 expr
= begin_stmt_expr ();
4282 /* Parse the compound-statement. */
4283 cp_parser_compound_statement (parser
, expr
, false, false);
4285 expr
= finish_stmt_expr (expr
, false);
4290 /* Parse the parenthesized expression. */
4291 expr
= cp_parser_expression (parser
, cast_p
, decltype_p
, idk
);
4292 /* Let the front end know that this expression was
4293 enclosed in parentheses. This matters in case, for
4294 example, the expression is of the form `A::B', since
4295 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4297 expr
= finish_parenthesized_expr (expr
);
4298 /* DR 705: Wrapping an unqualified name in parentheses
4299 suppresses arg-dependent lookup. We want to pass back
4300 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4301 (c++/37862), but none of the others. */
4302 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4303 *idk
= CP_ID_KIND_NONE
;
4305 /* The `>' token might be the end of a template-id or
4306 template-parameter-list now. */
4307 parser
->greater_than_is_operator_p
4308 = saved_greater_than_is_operator_p
;
4309 /* Consume the `)'. */
4310 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4311 cp_parser_skip_to_end_of_statement (parser
);
4316 case CPP_OPEN_SQUARE
:
4317 if (c_dialect_objc ())
4318 /* We have an Objective-C++ message. */
4319 return cp_parser_objc_expression (parser
);
4321 tree lam
= cp_parser_lambda_expression (parser
);
4322 /* Don't warn about a failed tentative parse. */
4323 if (cp_parser_error_occurred (parser
))
4324 return error_mark_node
;
4325 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4329 case CPP_OBJC_STRING
:
4330 if (c_dialect_objc ())
4331 /* We have an Objective-C++ string literal. */
4332 return cp_parser_objc_expression (parser
);
4333 cp_parser_error (parser
, "expected primary-expression");
4334 return error_mark_node
;
4337 switch (token
->keyword
)
4339 /* These two are the boolean literals. */
4341 cp_lexer_consume_token (parser
->lexer
);
4342 return boolean_true_node
;
4344 cp_lexer_consume_token (parser
->lexer
);
4345 return boolean_false_node
;
4347 /* The `__null' literal. */
4349 cp_lexer_consume_token (parser
->lexer
);
4352 /* The `nullptr' literal. */
4354 cp_lexer_consume_token (parser
->lexer
);
4355 return nullptr_node
;
4357 /* Recognize the `this' keyword. */
4359 cp_lexer_consume_token (parser
->lexer
);
4360 if (parser
->local_variables_forbidden_p
)
4362 error_at (token
->location
,
4363 "%<this%> may not be used in this context");
4364 return error_mark_node
;
4366 /* Pointers cannot appear in constant-expressions. */
4367 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4368 return error_mark_node
;
4369 return finish_this_expr ();
4371 /* The `operator' keyword can be the beginning of an
4376 case RID_FUNCTION_NAME
:
4377 case RID_PRETTY_FUNCTION_NAME
:
4378 case RID_C99_FUNCTION_NAME
:
4380 non_integral_constant name
;
4382 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4383 __func__ are the names of variables -- but they are
4384 treated specially. Therefore, they are handled here,
4385 rather than relying on the generic id-expression logic
4386 below. Grammatically, these names are id-expressions.
4388 Consume the token. */
4389 token
= cp_lexer_consume_token (parser
->lexer
);
4391 switch (token
->keyword
)
4393 case RID_FUNCTION_NAME
:
4394 name
= NIC_FUNC_NAME
;
4396 case RID_PRETTY_FUNCTION_NAME
:
4397 name
= NIC_PRETTY_FUNC
;
4399 case RID_C99_FUNCTION_NAME
:
4400 name
= NIC_C99_FUNC
;
4406 if (cp_parser_non_integral_constant_expression (parser
, name
))
4407 return error_mark_node
;
4409 /* Look up the name. */
4410 return finish_fname (token
->u
.value
);
4417 source_location type_location
;
4419 /* The `__builtin_va_arg' construct is used to handle
4420 `va_arg'. Consume the `__builtin_va_arg' token. */
4421 cp_lexer_consume_token (parser
->lexer
);
4422 /* Look for the opening `('. */
4423 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4424 /* Now, parse the assignment-expression. */
4425 expression
= cp_parser_assignment_expression (parser
,
4426 /*cast_p=*/false, NULL
);
4427 /* Look for the `,'. */
4428 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4429 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4430 /* Parse the type-id. */
4431 type
= cp_parser_type_id (parser
);
4432 /* Look for the closing `)'. */
4433 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4434 /* Using `va_arg' in a constant-expression is not
4436 if (cp_parser_non_integral_constant_expression (parser
,
4438 return error_mark_node
;
4439 return build_x_va_arg (type_location
, expression
, type
);
4443 return cp_parser_builtin_offsetof (parser
);
4445 case RID_HAS_NOTHROW_ASSIGN
:
4446 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4447 case RID_HAS_NOTHROW_COPY
:
4448 case RID_HAS_TRIVIAL_ASSIGN
:
4449 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4450 case RID_HAS_TRIVIAL_COPY
:
4451 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4452 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4453 case RID_IS_ABSTRACT
:
4454 case RID_IS_BASE_OF
:
4456 case RID_IS_CONVERTIBLE_TO
:
4460 case RID_IS_LITERAL_TYPE
:
4462 case RID_IS_POLYMORPHIC
:
4463 case RID_IS_STD_LAYOUT
:
4464 case RID_IS_TRIVIAL
:
4466 return cp_parser_trait_expr (parser
, token
->keyword
);
4468 /* Objective-C++ expressions. */
4470 case RID_AT_PROTOCOL
:
4471 case RID_AT_SELECTOR
:
4472 return cp_parser_objc_expression (parser
);
4475 if (parser
->in_function_body
4476 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4479 error_at (token
->location
,
4480 "a template declaration cannot appear at block scope");
4481 cp_parser_skip_to_end_of_block_or_statement (parser
);
4482 return error_mark_node
;
4485 cp_parser_error (parser
, "expected primary-expression");
4486 return error_mark_node
;
4489 /* An id-expression can start with either an identifier, a
4490 `::' as the beginning of a qualified-id, or the "operator"
4494 case CPP_TEMPLATE_ID
:
4495 case CPP_NESTED_NAME_SPECIFIER
:
4499 const char *error_msg
;
4502 cp_token
*id_expr_token
;
4505 /* Parse the id-expression. */
4507 = cp_parser_id_expression (parser
,
4508 /*template_keyword_p=*/false,
4509 /*check_dependency_p=*/true,
4511 /*declarator_p=*/false,
4512 /*optional_p=*/false);
4513 if (id_expression
== error_mark_node
)
4514 return error_mark_node
;
4515 id_expr_token
= token
;
4516 token
= cp_lexer_peek_token (parser
->lexer
);
4517 done
= (token
->type
!= CPP_OPEN_SQUARE
4518 && token
->type
!= CPP_OPEN_PAREN
4519 && token
->type
!= CPP_DOT
4520 && token
->type
!= CPP_DEREF
4521 && token
->type
!= CPP_PLUS_PLUS
4522 && token
->type
!= CPP_MINUS_MINUS
);
4523 /* If we have a template-id, then no further lookup is
4524 required. If the template-id was for a template-class, we
4525 will sometimes have a TYPE_DECL at this point. */
4526 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4527 || TREE_CODE (id_expression
) == TYPE_DECL
)
4528 decl
= id_expression
;
4529 /* Look up the name. */
4532 tree ambiguous_decls
;
4534 /* If we already know that this lookup is ambiguous, then
4535 we've already issued an error message; there's no reason
4537 if (id_expr_token
->type
== CPP_NAME
4538 && id_expr_token
->error_reported
)
4540 cp_parser_simulate_error (parser
);
4541 return error_mark_node
;
4544 decl
= cp_parser_lookup_name (parser
, id_expression
,
4547 /*is_namespace=*/false,
4548 /*check_dependency=*/true,
4550 id_expr_token
->location
);
4551 /* If the lookup was ambiguous, an error will already have
4553 if (ambiguous_decls
)
4554 return error_mark_node
;
4556 /* In Objective-C++, we may have an Objective-C 2.0
4557 dot-syntax for classes here. */
4558 if (c_dialect_objc ()
4559 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4560 && TREE_CODE (decl
) == TYPE_DECL
4561 && objc_is_class_name (decl
))
4564 cp_lexer_consume_token (parser
->lexer
);
4565 component
= cp_parser_identifier (parser
);
4566 if (component
== error_mark_node
)
4567 return error_mark_node
;
4569 return objc_build_class_component_ref (id_expression
, component
);
4572 /* In Objective-C++, an instance variable (ivar) may be preferred
4573 to whatever cp_parser_lookup_name() found. */
4574 decl
= objc_lookup_ivar (decl
, id_expression
);
4576 /* If name lookup gives us a SCOPE_REF, then the
4577 qualifying scope was dependent. */
4578 if (TREE_CODE (decl
) == SCOPE_REF
)
4580 /* At this point, we do not know if DECL is a valid
4581 integral constant expression. We assume that it is
4582 in fact such an expression, so that code like:
4584 template <int N> struct A {
4588 is accepted. At template-instantiation time, we
4589 will check that B<N>::i is actually a constant. */
4592 /* Check to see if DECL is a local variable in a context
4593 where that is forbidden. */
4594 if (parser
->local_variables_forbidden_p
4595 && local_variable_p (decl
))
4597 /* It might be that we only found DECL because we are
4598 trying to be generous with pre-ISO scoping rules.
4599 For example, consider:
4603 for (int i = 0; i < 10; ++i) {}
4604 extern void f(int j = i);
4607 Here, name look up will originally find the out
4608 of scope `i'. We need to issue a warning message,
4609 but then use the global `i'. */
4610 decl
= check_for_out_of_scope_variable (decl
);
4611 if (local_variable_p (decl
))
4613 error_at (id_expr_token
->location
,
4614 "local variable %qD may not appear in this context",
4616 return error_mark_node
;
4621 decl
= (finish_id_expression
4622 (id_expression
, decl
, parser
->scope
,
4624 parser
->integral_constant_expression_p
,
4625 parser
->allow_non_integral_constant_expression_p
,
4626 &parser
->non_integral_constant_expression_p
,
4627 template_p
, done
, address_p
,
4630 id_expr_token
->location
));
4632 cp_parser_error (parser
, error_msg
);
4636 /* Anything else is an error. */
4638 cp_parser_error (parser
, "expected primary-expression");
4639 return error_mark_node
;
4644 cp_parser_primary_expression (cp_parser
*parser
,
4647 bool template_arg_p
,
4650 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4651 /*decltype*/false, idk
);
4654 /* Parse an id-expression.
4661 :: [opt] nested-name-specifier template [opt] unqualified-id
4663 :: operator-function-id
4666 Return a representation of the unqualified portion of the
4667 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4668 a `::' or nested-name-specifier.
4670 Often, if the id-expression was a qualified-id, the caller will
4671 want to make a SCOPE_REF to represent the qualified-id. This
4672 function does not do this in order to avoid wastefully creating
4673 SCOPE_REFs when they are not required.
4675 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4678 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4679 uninstantiated templates.
4681 If *TEMPLATE_P is non-NULL, it is set to true iff the
4682 `template' keyword is used to explicitly indicate that the entity
4683 named is a template.
4685 If DECLARATOR_P is true, the id-expression is appearing as part of
4686 a declarator, rather than as part of an expression. */
4689 cp_parser_id_expression (cp_parser
*parser
,
4690 bool template_keyword_p
,
4691 bool check_dependency_p
,
4696 bool global_scope_p
;
4697 bool nested_name_specifier_p
;
4699 /* Assume the `template' keyword was not used. */
4701 *template_p
= template_keyword_p
;
4703 /* Look for the optional `::' operator. */
4705 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4707 /* Look for the optional nested-name-specifier. */
4708 nested_name_specifier_p
4709 = (cp_parser_nested_name_specifier_opt (parser
,
4710 /*typename_keyword_p=*/false,
4715 /* If there is a nested-name-specifier, then we are looking at
4716 the first qualified-id production. */
4717 if (nested_name_specifier_p
)
4720 tree saved_object_scope
;
4721 tree saved_qualifying_scope
;
4722 tree unqualified_id
;
4725 /* See if the next token is the `template' keyword. */
4727 template_p
= &is_template
;
4728 *template_p
= cp_parser_optional_template_keyword (parser
);
4729 /* Name lookup we do during the processing of the
4730 unqualified-id might obliterate SCOPE. */
4731 saved_scope
= parser
->scope
;
4732 saved_object_scope
= parser
->object_scope
;
4733 saved_qualifying_scope
= parser
->qualifying_scope
;
4734 /* Process the final unqualified-id. */
4735 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4738 /*optional_p=*/false);
4739 /* Restore the SAVED_SCOPE for our caller. */
4740 parser
->scope
= saved_scope
;
4741 parser
->object_scope
= saved_object_scope
;
4742 parser
->qualifying_scope
= saved_qualifying_scope
;
4744 return unqualified_id
;
4746 /* Otherwise, if we are in global scope, then we are looking at one
4747 of the other qualified-id productions. */
4748 else if (global_scope_p
)
4753 /* Peek at the next token. */
4754 token
= cp_lexer_peek_token (parser
->lexer
);
4756 /* If it's an identifier, and the next token is not a "<", then
4757 we can avoid the template-id case. This is an optimization
4758 for this common case. */
4759 if (token
->type
== CPP_NAME
4760 && !cp_parser_nth_token_starts_template_argument_list_p
4762 return cp_parser_identifier (parser
);
4764 cp_parser_parse_tentatively (parser
);
4765 /* Try a template-id. */
4766 id
= cp_parser_template_id (parser
,
4767 /*template_keyword_p=*/false,
4768 /*check_dependency_p=*/true,
4771 /* If that worked, we're done. */
4772 if (cp_parser_parse_definitely (parser
))
4775 /* Peek at the next token. (Changes in the token buffer may
4776 have invalidated the pointer obtained above.) */
4777 token
= cp_lexer_peek_token (parser
->lexer
);
4779 switch (token
->type
)
4782 return cp_parser_identifier (parser
);
4785 if (token
->keyword
== RID_OPERATOR
)
4786 return cp_parser_operator_function_id (parser
);
4790 cp_parser_error (parser
, "expected id-expression");
4791 return error_mark_node
;
4795 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4796 /*check_dependency_p=*/true,
4801 /* Parse an unqualified-id.
4805 operator-function-id
4806 conversion-function-id
4810 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4811 keyword, in a construct like `A::template ...'.
4813 Returns a representation of unqualified-id. For the `identifier'
4814 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4815 production a BIT_NOT_EXPR is returned; the operand of the
4816 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4817 other productions, see the documentation accompanying the
4818 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4819 names are looked up in uninstantiated templates. If DECLARATOR_P
4820 is true, the unqualified-id is appearing as part of a declarator,
4821 rather than as part of an expression. */
4824 cp_parser_unqualified_id (cp_parser
* parser
,
4825 bool template_keyword_p
,
4826 bool check_dependency_p
,
4832 /* Peek at the next token. */
4833 token
= cp_lexer_peek_token (parser
->lexer
);
4835 switch (token
->type
)
4841 /* We don't know yet whether or not this will be a
4843 cp_parser_parse_tentatively (parser
);
4844 /* Try a template-id. */
4845 id
= cp_parser_template_id (parser
, template_keyword_p
,
4849 /* If it worked, we're done. */
4850 if (cp_parser_parse_definitely (parser
))
4852 /* Otherwise, it's an ordinary identifier. */
4853 return cp_parser_identifier (parser
);
4856 case CPP_TEMPLATE_ID
:
4857 return cp_parser_template_id (parser
, template_keyword_p
,
4865 tree qualifying_scope
;
4870 /* Consume the `~' token. */
4871 cp_lexer_consume_token (parser
->lexer
);
4872 /* Parse the class-name. The standard, as written, seems to
4875 template <typename T> struct S { ~S (); };
4876 template <typename T> S<T>::~S() {}
4878 is invalid, since `~' must be followed by a class-name, but
4879 `S<T>' is dependent, and so not known to be a class.
4880 That's not right; we need to look in uninstantiated
4881 templates. A further complication arises from:
4883 template <typename T> void f(T t) {
4887 Here, it is not possible to look up `T' in the scope of `T'
4888 itself. We must look in both the current scope, and the
4889 scope of the containing complete expression.
4891 Yet another issue is:
4900 The standard does not seem to say that the `S' in `~S'
4901 should refer to the type `S' and not the data member
4904 /* DR 244 says that we look up the name after the "~" in the
4905 same scope as we looked up the qualifying name. That idea
4906 isn't fully worked out; it's more complicated than that. */
4907 scope
= parser
->scope
;
4908 object_scope
= parser
->object_scope
;
4909 qualifying_scope
= parser
->qualifying_scope
;
4911 /* Check for invalid scopes. */
4912 if (scope
== error_mark_node
)
4914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4915 cp_lexer_consume_token (parser
->lexer
);
4916 return error_mark_node
;
4918 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4921 error_at (token
->location
,
4922 "scope %qT before %<~%> is not a class-name",
4924 cp_parser_simulate_error (parser
);
4925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4926 cp_lexer_consume_token (parser
->lexer
);
4927 return error_mark_node
;
4929 gcc_assert (!scope
|| TYPE_P (scope
));
4931 /* If the name is of the form "X::~X" it's OK even if X is a
4933 token
= cp_lexer_peek_token (parser
->lexer
);
4935 && token
->type
== CPP_NAME
4936 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4938 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4939 || (CLASS_TYPE_P (scope
)
4940 && constructor_name_p (token
->u
.value
, scope
))))
4942 cp_lexer_consume_token (parser
->lexer
);
4943 return build_nt (BIT_NOT_EXPR
, scope
);
4946 /* ~auto means the destructor of whatever the object is. */
4947 if (cp_parser_is_keyword (token
, RID_AUTO
))
4949 if (cxx_dialect
< cxx1y
)
4950 pedwarn (input_location
, 0,
4951 "%<~auto%> only available with "
4952 "-std=c++1y or -std=gnu++1y");
4953 cp_lexer_consume_token (parser
->lexer
);
4954 return build_nt (BIT_NOT_EXPR
, make_auto ());
4957 /* If there was an explicit qualification (S::~T), first look
4958 in the scope given by the qualification (i.e., S).
4960 Note: in the calls to cp_parser_class_name below we pass
4961 typename_type so that lookup finds the injected-class-name
4962 rather than the constructor. */
4964 type_decl
= NULL_TREE
;
4967 cp_parser_parse_tentatively (parser
);
4968 type_decl
= cp_parser_class_name (parser
,
4969 /*typename_keyword_p=*/false,
4970 /*template_keyword_p=*/false,
4972 /*check_dependency=*/false,
4973 /*class_head_p=*/false,
4975 if (cp_parser_parse_definitely (parser
))
4978 /* In "N::S::~S", look in "N" as well. */
4979 if (!done
&& scope
&& qualifying_scope
)
4981 cp_parser_parse_tentatively (parser
);
4982 parser
->scope
= qualifying_scope
;
4983 parser
->object_scope
= NULL_TREE
;
4984 parser
->qualifying_scope
= NULL_TREE
;
4986 = cp_parser_class_name (parser
,
4987 /*typename_keyword_p=*/false,
4988 /*template_keyword_p=*/false,
4990 /*check_dependency=*/false,
4991 /*class_head_p=*/false,
4993 if (cp_parser_parse_definitely (parser
))
4996 /* In "p->S::~T", look in the scope given by "*p" as well. */
4997 else if (!done
&& object_scope
)
4999 cp_parser_parse_tentatively (parser
);
5000 parser
->scope
= object_scope
;
5001 parser
->object_scope
= NULL_TREE
;
5002 parser
->qualifying_scope
= NULL_TREE
;
5004 = cp_parser_class_name (parser
,
5005 /*typename_keyword_p=*/false,
5006 /*template_keyword_p=*/false,
5008 /*check_dependency=*/false,
5009 /*class_head_p=*/false,
5011 if (cp_parser_parse_definitely (parser
))
5014 /* Look in the surrounding context. */
5017 parser
->scope
= NULL_TREE
;
5018 parser
->object_scope
= NULL_TREE
;
5019 parser
->qualifying_scope
= NULL_TREE
;
5020 if (processing_template_decl
)
5021 cp_parser_parse_tentatively (parser
);
5023 = cp_parser_class_name (parser
,
5024 /*typename_keyword_p=*/false,
5025 /*template_keyword_p=*/false,
5027 /*check_dependency=*/false,
5028 /*class_head_p=*/false,
5030 if (processing_template_decl
5031 && ! cp_parser_parse_definitely (parser
))
5033 /* We couldn't find a type with this name, so just accept
5034 it and check for a match at instantiation time. */
5035 type_decl
= cp_parser_identifier (parser
);
5036 if (type_decl
!= error_mark_node
)
5037 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5041 /* If an error occurred, assume that the name of the
5042 destructor is the same as the name of the qualifying
5043 class. That allows us to keep parsing after running
5044 into ill-formed destructor names. */
5045 if (type_decl
== error_mark_node
&& scope
)
5046 return build_nt (BIT_NOT_EXPR
, scope
);
5047 else if (type_decl
== error_mark_node
)
5048 return error_mark_node
;
5050 /* Check that destructor name and scope match. */
5051 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5054 error_at (token
->location
,
5055 "declaration of %<~%T%> as member of %qT",
5057 cp_parser_simulate_error (parser
);
5058 return error_mark_node
;
5063 A typedef-name that names a class shall not be used as the
5064 identifier in the declarator for a destructor declaration. */
5066 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5067 && !DECL_SELF_REFERENCE_P (type_decl
)
5068 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5069 error_at (token
->location
,
5070 "typedef-name %qD used as destructor declarator",
5073 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5077 if (token
->keyword
== RID_OPERATOR
)
5081 /* This could be a template-id, so we try that first. */
5082 cp_parser_parse_tentatively (parser
);
5083 /* Try a template-id. */
5084 id
= cp_parser_template_id (parser
, template_keyword_p
,
5085 /*check_dependency_p=*/true,
5088 /* If that worked, we're done. */
5089 if (cp_parser_parse_definitely (parser
))
5091 /* We still don't know whether we're looking at an
5092 operator-function-id or a conversion-function-id. */
5093 cp_parser_parse_tentatively (parser
);
5094 /* Try an operator-function-id. */
5095 id
= cp_parser_operator_function_id (parser
);
5096 /* If that didn't work, try a conversion-function-id. */
5097 if (!cp_parser_parse_definitely (parser
))
5098 id
= cp_parser_conversion_function_id (parser
);
5099 else if (UDLIT_OPER_P (id
))
5102 const char *name
= UDLIT_OP_SUFFIX (id
);
5103 if (name
[0] != '_' && !in_system_header_at (input_location
)
5105 warning (0, "literal operator suffixes not preceded by %<_%>"
5106 " are reserved for future standardization");
5116 cp_parser_error (parser
, "expected unqualified-id");
5117 return error_mark_node
;
5121 /* Parse an (optional) nested-name-specifier.
5123 nested-name-specifier: [C++98]
5124 class-or-namespace-name :: nested-name-specifier [opt]
5125 class-or-namespace-name :: template nested-name-specifier [opt]
5127 nested-name-specifier: [C++0x]
5130 nested-name-specifier identifier ::
5131 nested-name-specifier template [opt] simple-template-id ::
5133 PARSER->SCOPE should be set appropriately before this function is
5134 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5135 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5138 Sets PARSER->SCOPE to the class (TYPE) or namespace
5139 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5140 it unchanged if there is no nested-name-specifier. Returns the new
5141 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5143 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5144 part of a declaration and/or decl-specifier. */
5147 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5148 bool typename_keyword_p
,
5149 bool check_dependency_p
,
5151 bool is_declaration
)
5153 bool success
= false;
5154 cp_token_position start
= 0;
5157 /* Remember where the nested-name-specifier starts. */
5158 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5160 start
= cp_lexer_token_position (parser
->lexer
, false);
5161 push_deferring_access_checks (dk_deferred
);
5168 tree saved_qualifying_scope
;
5169 bool template_keyword_p
;
5171 /* Spot cases that cannot be the beginning of a
5172 nested-name-specifier. */
5173 token
= cp_lexer_peek_token (parser
->lexer
);
5175 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5176 the already parsed nested-name-specifier. */
5177 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5179 /* Grab the nested-name-specifier and continue the loop. */
5180 cp_parser_pre_parsed_nested_name_specifier (parser
);
5181 /* If we originally encountered this nested-name-specifier
5182 with IS_DECLARATION set to false, we will not have
5183 resolved TYPENAME_TYPEs, so we must do so here. */
5185 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5187 new_scope
= resolve_typename_type (parser
->scope
,
5188 /*only_current_p=*/false);
5189 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5190 parser
->scope
= new_scope
;
5196 /* Spot cases that cannot be the beginning of a
5197 nested-name-specifier. On the second and subsequent times
5198 through the loop, we look for the `template' keyword. */
5199 if (success
&& token
->keyword
== RID_TEMPLATE
)
5201 /* A template-id can start a nested-name-specifier. */
5202 else if (token
->type
== CPP_TEMPLATE_ID
)
5204 /* DR 743: decltype can be used in a nested-name-specifier. */
5205 else if (token_is_decltype (token
))
5209 /* If the next token is not an identifier, then it is
5210 definitely not a type-name or namespace-name. */
5211 if (token
->type
!= CPP_NAME
)
5213 /* If the following token is neither a `<' (to begin a
5214 template-id), nor a `::', then we are not looking at a
5215 nested-name-specifier. */
5216 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5218 if (token
->type
== CPP_COLON
5219 && parser
->colon_corrects_to_scope_p
5220 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5222 error_at (token
->location
,
5223 "found %<:%> in nested-name-specifier, expected %<::%>");
5224 token
->type
= CPP_SCOPE
;
5227 if (token
->type
!= CPP_SCOPE
5228 && !cp_parser_nth_token_starts_template_argument_list_p
5233 /* The nested-name-specifier is optional, so we parse
5235 cp_parser_parse_tentatively (parser
);
5237 /* Look for the optional `template' keyword, if this isn't the
5238 first time through the loop. */
5240 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5242 template_keyword_p
= false;
5244 /* Save the old scope since the name lookup we are about to do
5245 might destroy it. */
5246 old_scope
= parser
->scope
;
5247 saved_qualifying_scope
= parser
->qualifying_scope
;
5248 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5249 look up names in "X<T>::I" in order to determine that "Y" is
5250 a template. So, if we have a typename at this point, we make
5251 an effort to look through it. */
5253 && !typename_keyword_p
5255 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5256 parser
->scope
= resolve_typename_type (parser
->scope
,
5257 /*only_current_p=*/false);
5258 /* Parse the qualifying entity. */
5260 = cp_parser_qualifying_entity (parser
,
5266 /* Look for the `::' token. */
5267 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5269 /* If we found what we wanted, we keep going; otherwise, we're
5271 if (!cp_parser_parse_definitely (parser
))
5273 bool error_p
= false;
5275 /* Restore the OLD_SCOPE since it was valid before the
5276 failed attempt at finding the last
5277 class-or-namespace-name. */
5278 parser
->scope
= old_scope
;
5279 parser
->qualifying_scope
= saved_qualifying_scope
;
5281 /* If the next token is a decltype, and the one after that is a
5282 `::', then the decltype has failed to resolve to a class or
5283 enumeration type. Give this error even when parsing
5284 tentatively since it can't possibly be valid--and we're going
5285 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5286 won't get another chance.*/
5287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5288 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5291 token
= cp_lexer_consume_token (parser
->lexer
);
5292 error_at (token
->location
, "decltype evaluates to %qT, "
5293 "which is not a class or enumeration type",
5295 parser
->scope
= error_mark_node
;
5299 cp_lexer_consume_token (parser
->lexer
);
5302 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5304 /* If the next token is an identifier, and the one after
5305 that is a `::', then any valid interpretation would have
5306 found a class-or-namespace-name. */
5307 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5308 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5310 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5313 token
= cp_lexer_consume_token (parser
->lexer
);
5316 if (!token
->error_reported
)
5319 tree ambiguous_decls
;
5321 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5323 /*is_template=*/false,
5324 /*is_namespace=*/false,
5325 /*check_dependency=*/true,
5328 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5329 error_at (token
->location
,
5330 "%qD used without template parameters",
5332 else if (ambiguous_decls
)
5334 // cp_parser_lookup_name has the same diagnostic,
5335 // thus make sure to emit it at most once.
5336 if (cp_parser_uncommitted_to_tentative_parse_p
5339 error_at (token
->location
,
5340 "reference to %qD is ambiguous",
5342 print_candidates (ambiguous_decls
);
5344 decl
= error_mark_node
;
5348 if (cxx_dialect
!= cxx98
)
5349 cp_parser_name_lookup_error
5350 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5353 cp_parser_name_lookup_error
5354 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5358 parser
->scope
= error_mark_node
;
5360 /* Treat this as a successful nested-name-specifier
5365 If the name found is not a class-name (clause
5366 _class_) or namespace-name (_namespace.def_), the
5367 program is ill-formed. */
5370 cp_lexer_consume_token (parser
->lexer
);
5374 /* We've found one valid nested-name-specifier. */
5376 /* Name lookup always gives us a DECL. */
5377 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5378 new_scope
= TREE_TYPE (new_scope
);
5379 /* Uses of "template" must be followed by actual templates. */
5380 if (template_keyword_p
5381 && !(CLASS_TYPE_P (new_scope
)
5382 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5383 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5384 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5385 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5386 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5387 == TEMPLATE_ID_EXPR
)))
5388 permerror (input_location
, TYPE_P (new_scope
)
5389 ? G_("%qT is not a template")
5390 : G_("%qD is not a template"),
5392 /* If it is a class scope, try to complete it; we are about to
5393 be looking up names inside the class. */
5394 if (TYPE_P (new_scope
)
5395 /* Since checking types for dependency can be expensive,
5396 avoid doing it if the type is already complete. */
5397 && !COMPLETE_TYPE_P (new_scope
)
5398 /* Do not try to complete dependent types. */
5399 && !dependent_type_p (new_scope
))
5401 new_scope
= complete_type (new_scope
);
5402 /* If it is a typedef to current class, use the current
5403 class instead, as the typedef won't have any names inside
5405 if (!COMPLETE_TYPE_P (new_scope
)
5406 && currently_open_class (new_scope
))
5407 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5409 /* Make sure we look in the right scope the next time through
5411 parser
->scope
= new_scope
;
5414 /* If parsing tentatively, replace the sequence of tokens that makes
5415 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5416 token. That way, should we re-parse the token stream, we will
5417 not have to repeat the effort required to do the parse, nor will
5418 we issue duplicate error messages. */
5419 if (success
&& start
)
5423 token
= cp_lexer_token_at (parser
->lexer
, start
);
5424 /* Reset the contents of the START token. */
5425 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5426 /* Retrieve any deferred checks. Do not pop this access checks yet
5427 so the memory will not be reclaimed during token replacing below. */
5428 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
5429 token
->u
.tree_check_value
->value
= parser
->scope
;
5430 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5431 token
->u
.tree_check_value
->qualifying_scope
=
5432 parser
->qualifying_scope
;
5433 token
->keyword
= RID_MAX
;
5435 /* Purge all subsequent tokens. */
5436 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5440 pop_to_parent_deferring_access_checks ();
5442 return success
? parser
->scope
: NULL_TREE
;
5445 /* Parse a nested-name-specifier. See
5446 cp_parser_nested_name_specifier_opt for details. This function
5447 behaves identically, except that it will an issue an error if no
5448 nested-name-specifier is present. */
5451 cp_parser_nested_name_specifier (cp_parser
*parser
,
5452 bool typename_keyword_p
,
5453 bool check_dependency_p
,
5455 bool is_declaration
)
5459 /* Look for the nested-name-specifier. */
5460 scope
= cp_parser_nested_name_specifier_opt (parser
,
5465 /* If it was not present, issue an error message. */
5468 cp_parser_error (parser
, "expected nested-name-specifier");
5469 parser
->scope
= NULL_TREE
;
5475 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5476 this is either a class-name or a namespace-name (which corresponds
5477 to the class-or-namespace-name production in the grammar). For
5478 C++0x, it can also be a type-name that refers to an enumeration
5479 type or a simple-template-id.
5481 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5482 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5483 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5484 TYPE_P is TRUE iff the next name should be taken as a class-name,
5485 even the same name is declared to be another entity in the same
5488 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5489 specified by the class-or-namespace-name. If neither is found the
5490 ERROR_MARK_NODE is returned. */
5493 cp_parser_qualifying_entity (cp_parser
*parser
,
5494 bool typename_keyword_p
,
5495 bool template_keyword_p
,
5496 bool check_dependency_p
,
5498 bool is_declaration
)
5501 tree saved_qualifying_scope
;
5502 tree saved_object_scope
;
5505 bool successful_parse_p
;
5507 /* DR 743: decltype can appear in a nested-name-specifier. */
5508 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5510 scope
= cp_parser_decltype (parser
);
5511 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5512 && !MAYBE_CLASS_TYPE_P (scope
))
5514 cp_parser_simulate_error (parser
);
5515 return error_mark_node
;
5517 if (TYPE_NAME (scope
))
5518 scope
= TYPE_NAME (scope
);
5522 /* Before we try to parse the class-name, we must save away the
5523 current PARSER->SCOPE since cp_parser_class_name will destroy
5525 saved_scope
= parser
->scope
;
5526 saved_qualifying_scope
= parser
->qualifying_scope
;
5527 saved_object_scope
= parser
->object_scope
;
5528 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5529 there is no need to look for a namespace-name. */
5530 only_class_p
= template_keyword_p
5531 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5533 cp_parser_parse_tentatively (parser
);
5534 scope
= cp_parser_class_name (parser
,
5537 type_p
? class_type
: none_type
,
5539 /*class_head_p=*/false,
5541 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5542 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5544 && cxx_dialect
!= cxx98
5545 && !successful_parse_p
)
5547 /* Restore the saved scope. */
5548 parser
->scope
= saved_scope
;
5549 parser
->qualifying_scope
= saved_qualifying_scope
;
5550 parser
->object_scope
= saved_object_scope
;
5552 /* Parse tentatively. */
5553 cp_parser_parse_tentatively (parser
);
5555 /* Parse a type-name */
5556 scope
= cp_parser_type_name (parser
);
5558 /* "If the name found does not designate a namespace or a class,
5559 enumeration, or dependent type, the program is ill-formed."
5561 We cover classes and dependent types above and namespaces below,
5562 so this code is only looking for enums. */
5563 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5564 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5565 cp_parser_simulate_error (parser
);
5567 successful_parse_p
= cp_parser_parse_definitely (parser
);
5569 /* If that didn't work, try for a namespace-name. */
5570 if (!only_class_p
&& !successful_parse_p
)
5572 /* Restore the saved scope. */
5573 parser
->scope
= saved_scope
;
5574 parser
->qualifying_scope
= saved_qualifying_scope
;
5575 parser
->object_scope
= saved_object_scope
;
5576 /* If we are not looking at an identifier followed by the scope
5577 resolution operator, then this is not part of a
5578 nested-name-specifier. (Note that this function is only used
5579 to parse the components of a nested-name-specifier.) */
5580 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5581 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5582 return error_mark_node
;
5583 scope
= cp_parser_namespace_name (parser
);
5589 /* Parse a postfix-expression.
5593 postfix-expression [ expression ]
5594 postfix-expression ( expression-list [opt] )
5595 simple-type-specifier ( expression-list [opt] )
5596 typename :: [opt] nested-name-specifier identifier
5597 ( expression-list [opt] )
5598 typename :: [opt] nested-name-specifier template [opt] template-id
5599 ( expression-list [opt] )
5600 postfix-expression . template [opt] id-expression
5601 postfix-expression -> template [opt] id-expression
5602 postfix-expression . pseudo-destructor-name
5603 postfix-expression -> pseudo-destructor-name
5604 postfix-expression ++
5605 postfix-expression --
5606 dynamic_cast < type-id > ( expression )
5607 static_cast < type-id > ( expression )
5608 reinterpret_cast < type-id > ( expression )
5609 const_cast < type-id > ( expression )
5610 typeid ( expression )
5616 ( type-id ) { initializer-list , [opt] }
5618 This extension is a GNU version of the C99 compound-literal
5619 construct. (The C99 grammar uses `type-name' instead of `type-id',
5620 but they are essentially the same concept.)
5622 If ADDRESS_P is true, the postfix expression is the operand of the
5623 `&' operator. CAST_P is true if this expression is the target of a
5626 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5627 class member access expressions [expr.ref].
5629 Returns a representation of the expression. */
5632 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5633 bool member_access_only_p
, bool decltype_p
,
5634 cp_id_kind
* pidk_return
)
5639 cp_id_kind idk
= CP_ID_KIND_NONE
;
5640 tree postfix_expression
= NULL_TREE
;
5641 bool is_member_access
= false;
5642 int saved_in_statement
= -1;
5644 /* Peek at the next token. */
5645 token
= cp_lexer_peek_token (parser
->lexer
);
5646 loc
= token
->location
;
5647 /* Some of the productions are determined by keywords. */
5648 keyword
= token
->keyword
;
5658 const char *saved_message
;
5659 bool saved_in_type_id_in_expr_p
;
5661 /* All of these can be handled in the same way from the point
5662 of view of parsing. Begin by consuming the token
5663 identifying the cast. */
5664 cp_lexer_consume_token (parser
->lexer
);
5666 /* New types cannot be defined in the cast. */
5667 saved_message
= parser
->type_definition_forbidden_message
;
5668 parser
->type_definition_forbidden_message
5669 = G_("types may not be defined in casts");
5671 /* Look for the opening `<'. */
5672 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5673 /* Parse the type to which we are casting. */
5674 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5675 parser
->in_type_id_in_expr_p
= true;
5676 type
= cp_parser_type_id (parser
);
5677 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5678 /* Look for the closing `>'. */
5679 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5680 /* Restore the old message. */
5681 parser
->type_definition_forbidden_message
= saved_message
;
5683 bool saved_greater_than_is_operator_p
5684 = parser
->greater_than_is_operator_p
;
5685 parser
->greater_than_is_operator_p
= true;
5687 /* And the expression which is being cast. */
5688 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5689 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
5690 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5692 parser
->greater_than_is_operator_p
5693 = saved_greater_than_is_operator_p
;
5695 /* Only type conversions to integral or enumeration types
5696 can be used in constant-expressions. */
5697 if (!cast_valid_in_integral_constant_expression_p (type
)
5698 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5699 return error_mark_node
;
5705 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5709 = build_static_cast (type
, expression
, tf_warning_or_error
);
5713 = build_reinterpret_cast (type
, expression
,
5714 tf_warning_or_error
);
5718 = build_const_cast (type
, expression
, tf_warning_or_error
);
5729 const char *saved_message
;
5730 bool saved_in_type_id_in_expr_p
;
5732 /* Consume the `typeid' token. */
5733 cp_lexer_consume_token (parser
->lexer
);
5734 /* Look for the `(' token. */
5735 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5736 /* Types cannot be defined in a `typeid' expression. */
5737 saved_message
= parser
->type_definition_forbidden_message
;
5738 parser
->type_definition_forbidden_message
5739 = G_("types may not be defined in a %<typeid%> expression");
5740 /* We can't be sure yet whether we're looking at a type-id or an
5742 cp_parser_parse_tentatively (parser
);
5743 /* Try a type-id first. */
5744 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5745 parser
->in_type_id_in_expr_p
= true;
5746 type
= cp_parser_type_id (parser
);
5747 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5748 /* Look for the `)' token. Otherwise, we can't be sure that
5749 we're not looking at an expression: consider `typeid (int
5750 (3))', for example. */
5751 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5752 /* If all went well, simply lookup the type-id. */
5753 if (cp_parser_parse_definitely (parser
))
5754 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5755 /* Otherwise, fall back to the expression variant. */
5760 /* Look for an expression. */
5761 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
5762 /* Compute its typeid. */
5763 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5764 /* Look for the `)' token. */
5765 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5767 /* Restore the saved message. */
5768 parser
->type_definition_forbidden_message
= saved_message
;
5769 /* `typeid' may not appear in an integral constant expression. */
5770 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5771 return error_mark_node
;
5778 /* The syntax permitted here is the same permitted for an
5779 elaborated-type-specifier. */
5780 type
= cp_parser_elaborated_type_specifier (parser
,
5781 /*is_friend=*/false,
5782 /*is_declaration=*/false);
5783 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5787 case RID_CILK_SPAWN
:
5789 cp_lexer_consume_token (parser
->lexer
);
5790 token
= cp_lexer_peek_token (parser
->lexer
);
5791 if (token
->type
== CPP_SEMICOLON
)
5793 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
5795 postfix_expression
= error_mark_node
;
5798 else if (!current_function_decl
)
5800 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
5801 "inside a function");
5802 postfix_expression
= error_mark_node
;
5807 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5808 saved_in_statement
= parser
->in_statement
;
5809 parser
->in_statement
|= IN_CILK_SPAWN
;
5811 cfun
->calls_cilk_spawn
= 1;
5812 postfix_expression
=
5813 cp_parser_postfix_expression (parser
, false, false,
5814 false, false, &idk
);
5817 error_at (token
->location
, "-fcilkplus must be enabled to use"
5818 " %<_Cilk_spawn%>");
5819 cfun
->calls_cilk_spawn
= 0;
5821 else if (saved_in_statement
& IN_CILK_SPAWN
)
5823 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
5824 "are not permitted");
5825 postfix_expression
= error_mark_node
;
5826 cfun
->calls_cilk_spawn
= 0;
5830 postfix_expression
= build_cilk_spawn (token
->location
,
5831 postfix_expression
);
5832 if (postfix_expression
!= error_mark_node
)
5833 SET_EXPR_LOCATION (postfix_expression
, input_location
);
5834 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
5842 tree sync_expr
= build_cilk_sync ();
5843 SET_EXPR_LOCATION (sync_expr
,
5844 cp_lexer_peek_token (parser
->lexer
)->location
);
5845 finish_expr_stmt (sync_expr
);
5848 error_at (token
->location
, "-fcilkplus must be enabled to use"
5850 cp_lexer_consume_token (parser
->lexer
);
5853 case RID_BUILTIN_SHUFFLE
:
5855 vec
<tree
, va_gc
> *vec
;
5859 cp_lexer_consume_token (parser
->lexer
);
5860 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5861 /*cast_p=*/false, /*allow_expansion_p=*/true,
5862 /*non_constant_p=*/NULL
);
5864 return error_mark_node
;
5866 FOR_EACH_VEC_ELT (*vec
, i
, p
)
5869 if (vec
->length () == 2)
5870 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
5871 tf_warning_or_error
);
5872 else if (vec
->length () == 3)
5873 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
5874 tf_warning_or_error
);
5877 error_at (loc
, "wrong number of arguments to "
5878 "%<__builtin_shuffle%>");
5879 return error_mark_node
;
5888 /* If the next thing is a simple-type-specifier, we may be
5889 looking at a functional cast. We could also be looking at
5890 an id-expression. So, we try the functional cast, and if
5891 that doesn't work we fall back to the primary-expression. */
5892 cp_parser_parse_tentatively (parser
);
5893 /* Look for the simple-type-specifier. */
5894 type
= cp_parser_simple_type_specifier (parser
,
5895 /*decl_specs=*/NULL
,
5896 CP_PARSER_FLAGS_NONE
);
5897 /* Parse the cast itself. */
5898 if (!cp_parser_error_occurred (parser
))
5900 = cp_parser_functional_cast (parser
, type
);
5901 /* If that worked, we're done. */
5902 if (cp_parser_parse_definitely (parser
))
5905 /* If the functional-cast didn't work out, try a
5906 compound-literal. */
5907 if (cp_parser_allow_gnu_extensions_p (parser
)
5908 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5910 tree initializer
= NULL_TREE
;
5911 bool compound_literal_p
;
5913 cp_parser_parse_tentatively (parser
);
5914 /* Consume the `('. */
5915 cp_lexer_consume_token (parser
->lexer
);
5917 /* Avoid calling cp_parser_type_id pointlessly, see comment
5918 in cp_parser_cast_expression about c++/29234. */
5919 cp_lexer_save_tokens (parser
->lexer
);
5922 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5923 /*consume_paren=*/true)
5924 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5926 /* Roll back the tokens we skipped. */
5927 cp_lexer_rollback_tokens (parser
->lexer
);
5929 if (!compound_literal_p
)
5930 cp_parser_simulate_error (parser
);
5933 /* Parse the type. */
5934 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5935 parser
->in_type_id_in_expr_p
= true;
5936 type
= cp_parser_type_id (parser
);
5937 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5938 /* Look for the `)'. */
5939 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5942 /* If things aren't going well, there's no need to
5944 if (!cp_parser_error_occurred (parser
))
5946 bool non_constant_p
;
5947 /* Parse the brace-enclosed initializer list. */
5948 initializer
= cp_parser_braced_list (parser
,
5951 /* If that worked, we're definitely looking at a
5952 compound-literal expression. */
5953 if (cp_parser_parse_definitely (parser
))
5955 /* Warn the user that a compound literal is not
5956 allowed in standard C++. */
5957 pedwarn (input_location
, OPT_Wpedantic
,
5958 "ISO C++ forbids compound-literals");
5959 /* For simplicity, we disallow compound literals in
5960 constant-expressions. We could
5961 allow compound literals of integer type, whose
5962 initializer was a constant, in constant
5963 expressions. Permitting that usage, as a further
5964 extension, would not change the meaning of any
5965 currently accepted programs. (Of course, as
5966 compound literals are not part of ISO C++, the
5967 standard has nothing to say.) */
5968 if (cp_parser_non_integral_constant_expression (parser
,
5971 postfix_expression
= error_mark_node
;
5974 /* Form the representation of the compound-literal. */
5976 = finish_compound_literal (type
, initializer
,
5977 tf_warning_or_error
);
5982 /* It must be a primary-expression. */
5984 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5985 /*template_arg_p=*/false,
5992 /* Note that we don't need to worry about calling build_cplus_new on a
5993 class-valued CALL_EXPR in decltype when it isn't the end of the
5994 postfix-expression; unary_complex_lvalue will take care of that for
5997 /* Keep looping until the postfix-expression is complete. */
6000 if (idk
== CP_ID_KIND_UNQUALIFIED
6001 && identifier_p (postfix_expression
)
6002 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6003 /* It is not a Koenig lookup function call. */
6005 = unqualified_name_lookup_error (postfix_expression
);
6007 /* Peek at the next token. */
6008 token
= cp_lexer_peek_token (parser
->lexer
);
6010 switch (token
->type
)
6012 case CPP_OPEN_SQUARE
:
6013 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6015 cp_parser_error (parser
,
6016 "two consecutive %<[%> shall "
6017 "only introduce an attribute");
6018 return error_mark_node
;
6021 = cp_parser_postfix_open_square_expression (parser
,
6025 idk
= CP_ID_KIND_NONE
;
6026 is_member_access
= false;
6029 case CPP_OPEN_PAREN
:
6030 /* postfix-expression ( expression-list [opt] ) */
6033 bool is_builtin_constant_p
;
6034 bool saved_integral_constant_expression_p
= false;
6035 bool saved_non_integral_constant_expression_p
= false;
6036 tsubst_flags_t complain
= complain_flags (decltype_p
);
6037 vec
<tree
, va_gc
> *args
;
6039 is_member_access
= false;
6041 is_builtin_constant_p
6042 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6043 if (is_builtin_constant_p
)
6045 /* The whole point of __builtin_constant_p is to allow
6046 non-constant expressions to appear as arguments. */
6047 saved_integral_constant_expression_p
6048 = parser
->integral_constant_expression_p
;
6049 saved_non_integral_constant_expression_p
6050 = parser
->non_integral_constant_expression_p
;
6051 parser
->integral_constant_expression_p
= false;
6053 args
= (cp_parser_parenthesized_expression_list
6055 /*cast_p=*/false, /*allow_expansion_p=*/true,
6056 /*non_constant_p=*/NULL
));
6057 if (is_builtin_constant_p
)
6059 parser
->integral_constant_expression_p
6060 = saved_integral_constant_expression_p
;
6061 parser
->non_integral_constant_expression_p
6062 = saved_non_integral_constant_expression_p
;
6067 postfix_expression
= error_mark_node
;
6071 /* Function calls are not permitted in
6072 constant-expressions. */
6073 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6074 && cp_parser_non_integral_constant_expression (parser
,
6077 postfix_expression
= error_mark_node
;
6078 release_tree_vector (args
);
6083 if (idk
== CP_ID_KIND_UNQUALIFIED
6084 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6086 if (identifier_p (postfix_expression
))
6088 if (!args
->is_empty ())
6091 if (!any_type_dependent_arguments_p (args
))
6093 = perform_koenig_lookup (postfix_expression
, args
,
6098 = unqualified_fn_lookup_error (postfix_expression
);
6100 /* We do not perform argument-dependent lookup if
6101 normal lookup finds a non-function, in accordance
6102 with the expected resolution of DR 218. */
6103 else if (!args
->is_empty ()
6104 && is_overloaded_fn (postfix_expression
))
6106 tree fn
= get_first_fn (postfix_expression
);
6107 fn
= STRIP_TEMPLATE (fn
);
6109 /* Do not do argument dependent lookup if regular
6110 lookup finds a member function or a block-scope
6111 function declaration. [basic.lookup.argdep]/3 */
6112 if (!DECL_FUNCTION_MEMBER_P (fn
)
6113 && !DECL_LOCAL_FUNCTION_P (fn
))
6116 if (!any_type_dependent_arguments_p (args
))
6118 = perform_koenig_lookup (postfix_expression
, args
,
6124 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6126 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6127 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6129 if (processing_template_decl
6130 && (type_dependent_expression_p (instance
)
6131 || (!BASELINK_P (fn
)
6132 && TREE_CODE (fn
) != FIELD_DECL
)
6133 || type_dependent_expression_p (fn
)
6134 || any_type_dependent_arguments_p (args
)))
6137 = build_nt_call_vec (postfix_expression
, args
);
6138 release_tree_vector (args
);
6142 if (BASELINK_P (fn
))
6145 = (build_new_method_call
6146 (instance
, fn
, &args
, NULL_TREE
,
6147 (idk
== CP_ID_KIND_QUALIFIED
6148 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6155 = finish_call_expr (postfix_expression
, &args
,
6156 /*disallow_virtual=*/false,
6160 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6161 || TREE_CODE (postfix_expression
) == MEMBER_REF
6162 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6163 postfix_expression
= (build_offset_ref_call_from_tree
6164 (postfix_expression
, &args
,
6166 else if (idk
== CP_ID_KIND_QUALIFIED
)
6167 /* A call to a static class member, or a namespace-scope
6170 = finish_call_expr (postfix_expression
, &args
,
6171 /*disallow_virtual=*/true,
6175 /* All other function calls. */
6177 = finish_call_expr (postfix_expression
, &args
,
6178 /*disallow_virtual=*/false,
6182 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6183 idk
= CP_ID_KIND_NONE
;
6185 release_tree_vector (args
);
6191 /* postfix-expression . template [opt] id-expression
6192 postfix-expression . pseudo-destructor-name
6193 postfix-expression -> template [opt] id-expression
6194 postfix-expression -> pseudo-destructor-name */
6196 /* Consume the `.' or `->' operator. */
6197 cp_lexer_consume_token (parser
->lexer
);
6200 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6204 is_member_access
= true;
6208 /* postfix-expression ++ */
6209 /* Consume the `++' token. */
6210 cp_lexer_consume_token (parser
->lexer
);
6211 /* Generate a representation for the complete expression. */
6213 = finish_increment_expr (postfix_expression
,
6214 POSTINCREMENT_EXPR
);
6215 /* Increments may not appear in constant-expressions. */
6216 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6217 postfix_expression
= error_mark_node
;
6218 idk
= CP_ID_KIND_NONE
;
6219 is_member_access
= false;
6222 case CPP_MINUS_MINUS
:
6223 /* postfix-expression -- */
6224 /* Consume the `--' token. */
6225 cp_lexer_consume_token (parser
->lexer
);
6226 /* Generate a representation for the complete expression. */
6228 = finish_increment_expr (postfix_expression
,
6229 POSTDECREMENT_EXPR
);
6230 /* Decrements may not appear in constant-expressions. */
6231 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6232 postfix_expression
= error_mark_node
;
6233 idk
= CP_ID_KIND_NONE
;
6234 is_member_access
= false;
6238 if (pidk_return
!= NULL
)
6239 * pidk_return
= idk
;
6240 if (member_access_only_p
)
6241 return is_member_access
? postfix_expression
: error_mark_node
;
6243 return postfix_expression
;
6247 /* We should never get here. */
6249 return error_mark_node
;
6252 /* This function parses Cilk Plus array notations. If a normal array expr. is
6253 parsed then the array index is passed back to the caller through *INIT_INDEX
6254 and the function returns a NULL_TREE. If array notation expr. is parsed,
6255 then *INIT_INDEX is ignored by the caller and the function returns
6256 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6260 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6263 cp_token
*token
= NULL
;
6264 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6265 if (!array_value
|| array_value
== error_mark_node
)
6267 cp_parser_skip_to_end_of_statement (parser
);
6268 return error_mark_node
;
6271 array_type
= TREE_TYPE (array_value
);
6273 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6274 parser
->colon_corrects_to_scope_p
= false;
6275 token
= cp_lexer_peek_token (parser
->lexer
);
6279 cp_parser_error (parser
, "expected %<:%> or numeral");
6280 return error_mark_node
;
6282 else if (token
->type
== CPP_COLON
)
6284 /* Consume the ':'. */
6285 cp_lexer_consume_token (parser
->lexer
);
6287 /* If we are here, then we have a case like this A[:]. */
6288 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6290 cp_parser_error (parser
, "expected %<]%>");
6291 cp_parser_skip_to_end_of_statement (parser
);
6292 return error_mark_node
;
6294 *init_index
= NULL_TREE
;
6296 length_index
= NULL_TREE
;
6300 /* If we are here, then there are three valid possibilities:
6302 2. ARRAY [ EXP : EXP ]
6303 3. ARRAY [ EXP : EXP : EXP ] */
6305 *init_index
= cp_parser_expression (parser
, false, NULL
);
6306 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6308 /* This indicates that we have a normal array expression. */
6309 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6313 /* Consume the ':'. */
6314 cp_lexer_consume_token (parser
->lexer
);
6315 length_index
= cp_parser_expression (parser
, false, NULL
);
6316 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6318 cp_lexer_consume_token (parser
->lexer
);
6319 stride
= cp_parser_expression (parser
, false, NULL
);
6322 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6324 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6325 || stride
== error_mark_node
)
6327 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6328 cp_lexer_consume_token (parser
->lexer
);
6329 return error_mark_node
;
6331 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6333 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6334 length_index
, stride
, array_type
);
6338 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6339 by cp_parser_builtin_offsetof. We're looking for
6341 postfix-expression [ expression ]
6342 postfix-expression [ braced-init-list ] (C++11)
6344 FOR_OFFSETOF is set if we're being called in that context, which
6345 changes how we deal with integer constant expressions. */
6348 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6349 tree postfix_expression
,
6353 tree index
= NULL_TREE
;
6354 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6355 bool saved_greater_than_is_operator_p
;
6357 /* Consume the `[' token. */
6358 cp_lexer_consume_token (parser
->lexer
);
6360 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6361 parser
->greater_than_is_operator_p
= true;
6363 /* Parse the index expression. */
6364 /* ??? For offsetof, there is a question of what to allow here. If
6365 offsetof is not being used in an integral constant expression context,
6366 then we *could* get the right answer by computing the value at runtime.
6367 If we are in an integral constant expression context, then we might
6368 could accept any constant expression; hard to say without analysis.
6369 Rather than open the barn door too wide right away, allow only integer
6370 constant expressions here. */
6372 index
= cp_parser_constant_expression (parser
, false, NULL
);
6375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6377 bool expr_nonconst_p
;
6378 cp_lexer_set_source_position (parser
->lexer
);
6379 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6380 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6382 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6384 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6385 "braced list index is not allowed with array "
6387 cp_parser_skip_to_end_of_statement (parser
);
6388 return error_mark_node
;
6391 else if (flag_cilkplus
)
6393 /* Here are have these two options:
6394 ARRAY[EXP : EXP] - Array notation expr with default
6396 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6398 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6399 postfix_expression
);
6404 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6407 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6409 /* Look for the closing `]'. */
6410 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6412 /* Build the ARRAY_REF. */
6413 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6416 /* When not doing offsetof, array references are not permitted in
6417 constant-expressions. */
6419 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6420 postfix_expression
= error_mark_node
;
6422 return postfix_expression
;
6425 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6426 by cp_parser_builtin_offsetof. We're looking for
6428 postfix-expression . template [opt] id-expression
6429 postfix-expression . pseudo-destructor-name
6430 postfix-expression -> template [opt] id-expression
6431 postfix-expression -> pseudo-destructor-name
6433 FOR_OFFSETOF is set if we're being called in that context. That sorta
6434 limits what of the above we'll actually accept, but nevermind.
6435 TOKEN_TYPE is the "." or "->" token, which will already have been
6436 removed from the stream. */
6439 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6440 enum cpp_ttype token_type
,
6441 tree postfix_expression
,
6442 bool for_offsetof
, cp_id_kind
*idk
,
6443 location_t location
)
6447 bool pseudo_destructor_p
;
6448 tree scope
= NULL_TREE
;
6450 /* If this is a `->' operator, dereference the pointer. */
6451 if (token_type
== CPP_DEREF
)
6452 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6453 tf_warning_or_error
);
6454 /* Check to see whether or not the expression is type-dependent. */
6455 dependent_p
= type_dependent_expression_p (postfix_expression
);
6456 /* The identifier following the `->' or `.' is not qualified. */
6457 parser
->scope
= NULL_TREE
;
6458 parser
->qualifying_scope
= NULL_TREE
;
6459 parser
->object_scope
= NULL_TREE
;
6460 *idk
= CP_ID_KIND_NONE
;
6462 /* Enter the scope corresponding to the type of the object
6463 given by the POSTFIX_EXPRESSION. */
6464 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6466 scope
= TREE_TYPE (postfix_expression
);
6467 /* According to the standard, no expression should ever have
6468 reference type. Unfortunately, we do not currently match
6469 the standard in this respect in that our internal representation
6470 of an expression may have reference type even when the standard
6471 says it does not. Therefore, we have to manually obtain the
6472 underlying type here. */
6473 scope
= non_reference (scope
);
6474 /* The type of the POSTFIX_EXPRESSION must be complete. */
6475 if (scope
== unknown_type_node
)
6477 error_at (location
, "%qE does not have class type",
6478 postfix_expression
);
6481 /* Unlike the object expression in other contexts, *this is not
6482 required to be of complete type for purposes of class member
6483 access (5.2.5) outside the member function body. */
6484 else if (postfix_expression
!= current_class_ref
6485 && !(processing_template_decl
&& scope
== current_class_type
))
6486 scope
= complete_type_or_else (scope
, NULL_TREE
);
6487 /* Let the name lookup machinery know that we are processing a
6488 class member access expression. */
6489 parser
->context
->object_type
= scope
;
6490 /* If something went wrong, we want to be able to discern that case,
6491 as opposed to the case where there was no SCOPE due to the type
6492 of expression being dependent. */
6494 scope
= error_mark_node
;
6495 /* If the SCOPE was erroneous, make the various semantic analysis
6496 functions exit quickly -- and without issuing additional error
6498 if (scope
== error_mark_node
)
6499 postfix_expression
= error_mark_node
;
6502 /* Assume this expression is not a pseudo-destructor access. */
6503 pseudo_destructor_p
= false;
6505 /* If the SCOPE is a scalar type, then, if this is a valid program,
6506 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6507 is type dependent, it can be pseudo-destructor-name or something else.
6508 Try to parse it as pseudo-destructor-name first. */
6509 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6514 cp_parser_parse_tentatively (parser
);
6515 /* Parse the pseudo-destructor-name. */
6517 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6520 && (cp_parser_error_occurred (parser
)
6521 || !SCALAR_TYPE_P (type
)))
6522 cp_parser_abort_tentative_parse (parser
);
6523 else if (cp_parser_parse_definitely (parser
))
6525 pseudo_destructor_p
= true;
6527 = finish_pseudo_destructor_expr (postfix_expression
,
6532 if (!pseudo_destructor_p
)
6534 /* If the SCOPE is not a scalar type, we are looking at an
6535 ordinary class member access expression, rather than a
6536 pseudo-destructor-name. */
6538 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6539 /* Parse the id-expression. */
6540 name
= (cp_parser_id_expression
6542 cp_parser_optional_template_keyword (parser
),
6543 /*check_dependency_p=*/true,
6545 /*declarator_p=*/false,
6546 /*optional_p=*/false));
6547 /* In general, build a SCOPE_REF if the member name is qualified.
6548 However, if the name was not dependent and has already been
6549 resolved; there is no need to build the SCOPE_REF. For example;
6551 struct X { void f(); };
6552 template <typename T> void f(T* t) { t->X::f(); }
6554 Even though "t" is dependent, "X::f" is not and has been resolved
6555 to a BASELINK; there is no need to include scope information. */
6557 /* But we do need to remember that there was an explicit scope for
6558 virtual function calls. */
6560 *idk
= CP_ID_KIND_QUALIFIED
;
6562 /* If the name is a template-id that names a type, we will get a
6563 TYPE_DECL here. That is invalid code. */
6564 if (TREE_CODE (name
) == TYPE_DECL
)
6566 error_at (token
->location
, "invalid use of %qD", name
);
6567 postfix_expression
= error_mark_node
;
6571 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6573 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6575 error_at (token
->location
, "%<%D::%D%> is not a class member",
6576 parser
->scope
, name
);
6577 postfix_expression
= error_mark_node
;
6580 name
= build_qualified_name (/*type=*/NULL_TREE
,
6584 parser
->scope
= NULL_TREE
;
6585 parser
->qualifying_scope
= NULL_TREE
;
6586 parser
->object_scope
= NULL_TREE
;
6588 if (parser
->scope
&& name
&& BASELINK_P (name
))
6589 adjust_result_of_qualified_name_lookup
6590 (name
, parser
->scope
, scope
);
6592 = finish_class_member_access_expr (postfix_expression
, name
,
6594 tf_warning_or_error
);
6598 /* We no longer need to look up names in the scope of the object on
6599 the left-hand side of the `.' or `->' operator. */
6600 parser
->context
->object_type
= NULL_TREE
;
6602 /* Outside of offsetof, these operators may not appear in
6603 constant-expressions. */
6605 && (cp_parser_non_integral_constant_expression
6606 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6607 postfix_expression
= error_mark_node
;
6609 return postfix_expression
;
6612 /* Parse a parenthesized expression-list.
6615 assignment-expression
6616 expression-list, assignment-expression
6621 identifier, expression-list
6623 CAST_P is true if this expression is the target of a cast.
6625 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6628 Returns a vector of trees. Each element is a representation of an
6629 assignment-expression. NULL is returned if the ( and or ) are
6630 missing. An empty, but allocated, vector is returned on no
6631 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6632 if we are parsing an attribute list for an attribute that wants a
6633 plain identifier argument, normal_attr for an attribute that wants
6634 an expression, or non_attr if we aren't parsing an attribute list. If
6635 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6636 not all of the expressions in the list were constant. */
6638 static vec
<tree
, va_gc
> *
6639 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6640 int is_attribute_list
,
6642 bool allow_expansion_p
,
6643 bool *non_constant_p
)
6645 vec
<tree
, va_gc
> *expression_list
;
6646 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6647 tree identifier
= NULL_TREE
;
6648 bool saved_greater_than_is_operator_p
;
6650 /* Assume all the expressions will be constant. */
6652 *non_constant_p
= false;
6654 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6657 expression_list
= make_tree_vector ();
6659 /* Within a parenthesized expression, a `>' token is always
6660 the greater-than operator. */
6661 saved_greater_than_is_operator_p
6662 = parser
->greater_than_is_operator_p
;
6663 parser
->greater_than_is_operator_p
= true;
6665 /* Consume expressions until there are no more. */
6666 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6671 /* At the beginning of attribute lists, check to see if the
6672 next token is an identifier. */
6673 if (is_attribute_list
== id_attr
6674 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6678 /* Consume the identifier. */
6679 token
= cp_lexer_consume_token (parser
->lexer
);
6680 /* Save the identifier. */
6681 identifier
= token
->u
.value
;
6685 bool expr_non_constant_p
;
6687 /* Parse the next assignment-expression. */
6688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6690 /* A braced-init-list. */
6691 cp_lexer_set_source_position (parser
->lexer
);
6692 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6693 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6694 if (non_constant_p
&& expr_non_constant_p
)
6695 *non_constant_p
= true;
6697 else if (non_constant_p
)
6699 expr
= (cp_parser_constant_expression
6700 (parser
, /*allow_non_constant_p=*/true,
6701 &expr_non_constant_p
));
6702 if (expr_non_constant_p
)
6703 *non_constant_p
= true;
6706 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
6709 expr
= fold_non_dependent_expr (expr
);
6711 /* If we have an ellipsis, then this is an expression
6713 if (allow_expansion_p
6714 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6716 /* Consume the `...'. */
6717 cp_lexer_consume_token (parser
->lexer
);
6719 /* Build the argument pack. */
6720 expr
= make_pack_expansion (expr
);
6723 /* Add it to the list. We add error_mark_node
6724 expressions to the list, so that we can still tell if
6725 the correct form for a parenthesized expression-list
6726 is found. That gives better errors. */
6727 vec_safe_push (expression_list
, expr
);
6729 if (expr
== error_mark_node
)
6733 /* After the first item, attribute lists look the same as
6734 expression lists. */
6735 is_attribute_list
= non_attr
;
6738 /* If the next token isn't a `,', then we are done. */
6739 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6742 /* Otherwise, consume the `,' and keep going. */
6743 cp_lexer_consume_token (parser
->lexer
);
6746 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6751 /* We try and resync to an unnested comma, as that will give the
6752 user better diagnostics. */
6753 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6754 /*recovering=*/true,
6756 /*consume_paren=*/true);
6761 parser
->greater_than_is_operator_p
6762 = saved_greater_than_is_operator_p
;
6767 parser
->greater_than_is_operator_p
6768 = saved_greater_than_is_operator_p
;
6771 vec_safe_insert (expression_list
, 0, identifier
);
6773 return expression_list
;
6776 /* Parse a pseudo-destructor-name.
6778 pseudo-destructor-name:
6779 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6780 :: [opt] nested-name-specifier template template-id :: ~ type-name
6781 :: [opt] nested-name-specifier [opt] ~ type-name
6783 If either of the first two productions is used, sets *SCOPE to the
6784 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6785 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6786 or ERROR_MARK_NODE if the parse fails. */
6789 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6794 bool nested_name_specifier_p
;
6797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
6798 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
6799 && !type_dependent_expression_p (object
))
6801 if (cxx_dialect
< cxx1y
)
6802 pedwarn (input_location
, 0,
6803 "%<~auto%> only available with "
6804 "-std=c++1y or -std=gnu++1y");
6805 cp_lexer_consume_token (parser
->lexer
);
6806 cp_lexer_consume_token (parser
->lexer
);
6808 *type
= TREE_TYPE (object
);
6812 /* Assume that things will not work out. */
6813 *type
= error_mark_node
;
6815 /* Look for the optional `::' operator. */
6816 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
6817 /* Look for the optional nested-name-specifier. */
6818 nested_name_specifier_p
6819 = (cp_parser_nested_name_specifier_opt (parser
,
6820 /*typename_keyword_p=*/false,
6821 /*check_dependency_p=*/true,
6823 /*is_declaration=*/false)
6825 /* Now, if we saw a nested-name-specifier, we might be doing the
6826 second production. */
6827 if (nested_name_specifier_p
6828 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
6830 /* Consume the `template' keyword. */
6831 cp_lexer_consume_token (parser
->lexer
);
6832 /* Parse the template-id. */
6833 cp_parser_template_id (parser
,
6834 /*template_keyword_p=*/true,
6835 /*check_dependency_p=*/false,
6837 /*is_declaration=*/true);
6838 /* Look for the `::' token. */
6839 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6841 /* If the next token is not a `~', then there might be some
6842 additional qualification. */
6843 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
6845 /* At this point, we're looking for "type-name :: ~". The type-name
6846 must not be a class-name, since this is a pseudo-destructor. So,
6847 it must be either an enum-name, or a typedef-name -- both of which
6848 are just identifiers. So, we peek ahead to check that the "::"
6849 and "~" tokens are present; if they are not, then we can avoid
6850 calling type_name. */
6851 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
6852 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
6853 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
6855 cp_parser_error (parser
, "non-scalar type");
6859 /* Look for the type-name. */
6860 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6861 if (*scope
== error_mark_node
)
6864 /* Look for the `::' token. */
6865 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6870 /* Look for the `~'. */
6871 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
6873 /* Once we see the ~, this has to be a pseudo-destructor. */
6874 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
6875 cp_parser_commit_to_topmost_tentative_parse (parser
);
6877 /* Look for the type-name again. We are not responsible for
6878 checking that it matches the first type-name. */
6879 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6882 /* Parse a unary-expression.
6888 unary-operator cast-expression
6889 sizeof unary-expression
6891 alignof ( type-id ) [C++0x]
6898 __extension__ cast-expression
6899 __alignof__ unary-expression
6900 __alignof__ ( type-id )
6901 alignof unary-expression [C++0x]
6902 __real__ cast-expression
6903 __imag__ cast-expression
6905 sizeof ( type-id ) { initializer-list , [opt] }
6906 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6907 __alignof__ ( type-id ) { initializer-list , [opt] }
6909 ADDRESS_P is true iff the unary-expression is appearing as the
6910 operand of the `&' operator. CAST_P is true if this expression is
6911 the target of a cast.
6913 Returns a representation of the expression. */
6916 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6917 bool decltype_p
, cp_id_kind
* pidk
)
6920 enum tree_code unary_operator
;
6922 /* Peek at the next token. */
6923 token
= cp_lexer_peek_token (parser
->lexer
);
6924 /* Some keywords give away the kind of expression. */
6925 if (token
->type
== CPP_KEYWORD
)
6927 enum rid keyword
= token
->keyword
;
6936 location_t first_loc
;
6938 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
6939 /* Consume the token. */
6940 cp_lexer_consume_token (parser
->lexer
);
6941 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6942 /* Parse the operand. */
6943 operand
= cp_parser_sizeof_operand (parser
, keyword
);
6945 if (TYPE_P (operand
))
6946 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
6949 /* ISO C++ defines alignof only with types, not with
6950 expressions. So pedwarn if alignof is used with a non-
6951 type expression. However, __alignof__ is ok. */
6952 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
6953 pedwarn (token
->location
, OPT_Wpedantic
,
6954 "ISO C++ does not allow %<alignof%> "
6957 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
6959 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6960 SIZEOF_EXPR with the original operand. */
6961 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
6963 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
6965 if (!processing_template_decl
&& TYPE_P (operand
))
6967 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
6968 build1 (NOP_EXPR
, operand
,
6970 SIZEOF_EXPR_TYPE_P (ret
) = 1;
6973 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
6974 TREE_SIDE_EFFECTS (ret
) = 0;
6975 TREE_READONLY (ret
) = 1;
6977 SET_EXPR_LOCATION (ret
, first_loc
);
6983 return cp_parser_new_expression (parser
);
6986 return cp_parser_delete_expression (parser
);
6990 /* The saved value of the PEDANTIC flag. */
6994 /* Save away the PEDANTIC flag. */
6995 cp_parser_extension_opt (parser
, &saved_pedantic
);
6996 /* Parse the cast-expression. */
6997 expr
= cp_parser_simple_cast_expression (parser
);
6998 /* Restore the PEDANTIC flag. */
6999 pedantic
= saved_pedantic
;
7009 /* Consume the `__real__' or `__imag__' token. */
7010 cp_lexer_consume_token (parser
->lexer
);
7011 /* Parse the cast-expression. */
7012 expression
= cp_parser_simple_cast_expression (parser
);
7013 /* Create the complete representation. */
7014 return build_x_unary_op (token
->location
,
7015 (keyword
== RID_REALPART
7016 ? REALPART_EXPR
: IMAGPART_EXPR
),
7018 tf_warning_or_error
);
7022 case RID_TRANSACTION_ATOMIC
:
7023 case RID_TRANSACTION_RELAXED
:
7024 return cp_parser_transaction_expression (parser
, keyword
);
7029 const char *saved_message
;
7030 bool saved_integral_constant_expression_p
;
7031 bool saved_non_integral_constant_expression_p
;
7032 bool saved_greater_than_is_operator_p
;
7034 cp_lexer_consume_token (parser
->lexer
);
7035 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7037 saved_message
= parser
->type_definition_forbidden_message
;
7038 parser
->type_definition_forbidden_message
7039 = G_("types may not be defined in %<noexcept%> expressions");
7041 saved_integral_constant_expression_p
7042 = parser
->integral_constant_expression_p
;
7043 saved_non_integral_constant_expression_p
7044 = parser
->non_integral_constant_expression_p
;
7045 parser
->integral_constant_expression_p
= false;
7047 saved_greater_than_is_operator_p
7048 = parser
->greater_than_is_operator_p
;
7049 parser
->greater_than_is_operator_p
= true;
7051 ++cp_unevaluated_operand
;
7052 ++c_inhibit_evaluation_warnings
;
7053 expr
= cp_parser_expression (parser
, false, NULL
);
7054 --c_inhibit_evaluation_warnings
;
7055 --cp_unevaluated_operand
;
7057 parser
->greater_than_is_operator_p
7058 = saved_greater_than_is_operator_p
;
7060 parser
->integral_constant_expression_p
7061 = saved_integral_constant_expression_p
;
7062 parser
->non_integral_constant_expression_p
7063 = saved_non_integral_constant_expression_p
;
7065 parser
->type_definition_forbidden_message
= saved_message
;
7067 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7068 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7076 /* Look for the `:: new' and `:: delete', which also signal the
7077 beginning of a new-expression, or delete-expression,
7078 respectively. If the next token is `::', then it might be one of
7080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7084 /* See if the token after the `::' is one of the keywords in
7085 which we're interested. */
7086 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7087 /* If it's `new', we have a new-expression. */
7088 if (keyword
== RID_NEW
)
7089 return cp_parser_new_expression (parser
);
7090 /* Similarly, for `delete'. */
7091 else if (keyword
== RID_DELETE
)
7092 return cp_parser_delete_expression (parser
);
7095 /* Look for a unary operator. */
7096 unary_operator
= cp_parser_unary_operator (token
);
7097 /* The `++' and `--' operators can be handled similarly, even though
7098 they are not technically unary-operators in the grammar. */
7099 if (unary_operator
== ERROR_MARK
)
7101 if (token
->type
== CPP_PLUS_PLUS
)
7102 unary_operator
= PREINCREMENT_EXPR
;
7103 else if (token
->type
== CPP_MINUS_MINUS
)
7104 unary_operator
= PREDECREMENT_EXPR
;
7105 /* Handle the GNU address-of-label extension. */
7106 else if (cp_parser_allow_gnu_extensions_p (parser
)
7107 && token
->type
== CPP_AND_AND
)
7111 location_t loc
= token
->location
;
7113 /* Consume the '&&' token. */
7114 cp_lexer_consume_token (parser
->lexer
);
7115 /* Look for the identifier. */
7116 identifier
= cp_parser_identifier (parser
);
7117 /* Create an expression representing the address. */
7118 expression
= finish_label_address_expr (identifier
, loc
);
7119 if (cp_parser_non_integral_constant_expression (parser
,
7121 expression
= error_mark_node
;
7125 if (unary_operator
!= ERROR_MARK
)
7127 tree cast_expression
;
7128 tree expression
= error_mark_node
;
7129 non_integral_constant non_constant_p
= NIC_NONE
;
7130 location_t loc
= token
->location
;
7131 tsubst_flags_t complain
= complain_flags (decltype_p
);
7133 /* Consume the operator token. */
7134 token
= cp_lexer_consume_token (parser
->lexer
);
7135 /* Parse the cast-expression. */
7137 = cp_parser_cast_expression (parser
,
7138 unary_operator
== ADDR_EXPR
,
7142 /* Now, build an appropriate representation. */
7143 switch (unary_operator
)
7146 non_constant_p
= NIC_STAR
;
7147 expression
= build_x_indirect_ref (loc
, cast_expression
,
7153 non_constant_p
= NIC_ADDR
;
7156 expression
= build_x_unary_op (loc
, unary_operator
,
7161 case PREINCREMENT_EXPR
:
7162 case PREDECREMENT_EXPR
:
7163 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7164 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7166 case UNARY_PLUS_EXPR
:
7168 case TRUTH_NOT_EXPR
:
7169 expression
= finish_unary_op_expr (loc
, unary_operator
,
7170 cast_expression
, complain
);
7177 if (non_constant_p
!= NIC_NONE
7178 && cp_parser_non_integral_constant_expression (parser
,
7180 expression
= error_mark_node
;
7185 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7186 /*member_access_only_p=*/false,
7192 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7195 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7196 /*decltype*/false, pidk
);
7199 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7200 unary-operator, the corresponding tree code is returned. */
7202 static enum tree_code
7203 cp_parser_unary_operator (cp_token
* token
)
7205 switch (token
->type
)
7208 return INDIRECT_REF
;
7214 return UNARY_PLUS_EXPR
;
7220 return TRUTH_NOT_EXPR
;
7223 return BIT_NOT_EXPR
;
7230 /* Parse a new-expression.
7233 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7234 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7236 Returns a representation of the expression. */
7239 cp_parser_new_expression (cp_parser
* parser
)
7241 bool global_scope_p
;
7242 vec
<tree
, va_gc
> *placement
;
7244 vec
<tree
, va_gc
> *initializer
;
7245 tree nelts
= NULL_TREE
;
7248 /* Look for the optional `::' operator. */
7250 = (cp_parser_global_scope_opt (parser
,
7251 /*current_scope_valid_p=*/false)
7253 /* Look for the `new' operator. */
7254 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7255 /* There's no easy way to tell a new-placement from the
7256 `( type-id )' construct. */
7257 cp_parser_parse_tentatively (parser
);
7258 /* Look for a new-placement. */
7259 placement
= cp_parser_new_placement (parser
);
7260 /* If that didn't work out, there's no new-placement. */
7261 if (!cp_parser_parse_definitely (parser
))
7263 if (placement
!= NULL
)
7264 release_tree_vector (placement
);
7268 /* If the next token is a `(', then we have a parenthesized
7270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7273 const char *saved_message
= parser
->type_definition_forbidden_message
;
7275 /* Consume the `('. */
7276 cp_lexer_consume_token (parser
->lexer
);
7278 /* Parse the type-id. */
7279 parser
->type_definition_forbidden_message
7280 = G_("types may not be defined in a new-expression");
7281 type
= cp_parser_type_id (parser
);
7282 parser
->type_definition_forbidden_message
= saved_message
;
7284 /* Look for the closing `)'. */
7285 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7286 token
= cp_lexer_peek_token (parser
->lexer
);
7287 /* There should not be a direct-new-declarator in this production,
7288 but GCC used to allowed this, so we check and emit a sensible error
7289 message for this case. */
7290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7292 error_at (token
->location
,
7293 "array bound forbidden after parenthesized type-id");
7294 inform (token
->location
,
7295 "try removing the parentheses around the type-id");
7296 cp_parser_direct_new_declarator (parser
);
7299 /* Otherwise, there must be a new-type-id. */
7301 type
= cp_parser_new_type_id (parser
, &nelts
);
7303 /* If the next token is a `(' or '{', then we have a new-initializer. */
7304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7305 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7306 initializer
= cp_parser_new_initializer (parser
);
7310 /* A new-expression may not appear in an integral constant
7312 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7313 ret
= error_mark_node
;
7316 /* Create a representation of the new-expression. */
7317 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7318 tf_warning_or_error
);
7321 if (placement
!= NULL
)
7322 release_tree_vector (placement
);
7323 if (initializer
!= NULL
)
7324 release_tree_vector (initializer
);
7329 /* Parse a new-placement.
7334 Returns the same representation as for an expression-list. */
7336 static vec
<tree
, va_gc
> *
7337 cp_parser_new_placement (cp_parser
* parser
)
7339 vec
<tree
, va_gc
> *expression_list
;
7341 /* Parse the expression-list. */
7342 expression_list
= (cp_parser_parenthesized_expression_list
7343 (parser
, non_attr
, /*cast_p=*/false,
7344 /*allow_expansion_p=*/true,
7345 /*non_constant_p=*/NULL
));
7347 return expression_list
;
7350 /* Parse a new-type-id.
7353 type-specifier-seq new-declarator [opt]
7355 Returns the TYPE allocated. If the new-type-id indicates an array
7356 type, *NELTS is set to the number of elements in the last array
7357 bound; the TYPE will not include the last array bound. */
7360 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7362 cp_decl_specifier_seq type_specifier_seq
;
7363 cp_declarator
*new_declarator
;
7364 cp_declarator
*declarator
;
7365 cp_declarator
*outer_declarator
;
7366 const char *saved_message
;
7368 /* The type-specifier sequence must not contain type definitions.
7369 (It cannot contain declarations of new types either, but if they
7370 are not definitions we will catch that because they are not
7372 saved_message
= parser
->type_definition_forbidden_message
;
7373 parser
->type_definition_forbidden_message
7374 = G_("types may not be defined in a new-type-id");
7375 /* Parse the type-specifier-seq. */
7376 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7377 /*is_trailing_return=*/false,
7378 &type_specifier_seq
);
7379 /* Restore the old message. */
7380 parser
->type_definition_forbidden_message
= saved_message
;
7382 if (type_specifier_seq
.type
== error_mark_node
)
7383 return error_mark_node
;
7385 /* Parse the new-declarator. */
7386 new_declarator
= cp_parser_new_declarator_opt (parser
);
7388 /* Determine the number of elements in the last array dimension, if
7391 /* Skip down to the last array dimension. */
7392 declarator
= new_declarator
;
7393 outer_declarator
= NULL
;
7394 while (declarator
&& (declarator
->kind
== cdk_pointer
7395 || declarator
->kind
== cdk_ptrmem
))
7397 outer_declarator
= declarator
;
7398 declarator
= declarator
->declarator
;
7401 && declarator
->kind
== cdk_array
7402 && declarator
->declarator
7403 && declarator
->declarator
->kind
== cdk_array
)
7405 outer_declarator
= declarator
;
7406 declarator
= declarator
->declarator
;
7409 if (declarator
&& declarator
->kind
== cdk_array
)
7411 *nelts
= declarator
->u
.array
.bounds
;
7412 if (*nelts
== error_mark_node
)
7413 *nelts
= integer_one_node
;
7415 if (outer_declarator
)
7416 outer_declarator
->declarator
= declarator
->declarator
;
7418 new_declarator
= NULL
;
7421 return groktypename (&type_specifier_seq
, new_declarator
, false);
7424 /* Parse an (optional) new-declarator.
7427 ptr-operator new-declarator [opt]
7428 direct-new-declarator
7430 Returns the declarator. */
7432 static cp_declarator
*
7433 cp_parser_new_declarator_opt (cp_parser
* parser
)
7435 enum tree_code code
;
7436 tree type
, std_attributes
= NULL_TREE
;
7437 cp_cv_quals cv_quals
;
7439 /* We don't know if there's a ptr-operator next, or not. */
7440 cp_parser_parse_tentatively (parser
);
7441 /* Look for a ptr-operator. */
7442 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7443 /* If that worked, look for more new-declarators. */
7444 if (cp_parser_parse_definitely (parser
))
7446 cp_declarator
*declarator
;
7448 /* Parse another optional declarator. */
7449 declarator
= cp_parser_new_declarator_opt (parser
);
7451 declarator
= cp_parser_make_indirect_declarator
7452 (code
, type
, cv_quals
, declarator
, std_attributes
);
7457 /* If the next token is a `[', there is a direct-new-declarator. */
7458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7459 return cp_parser_direct_new_declarator (parser
);
7464 /* Parse a direct-new-declarator.
7466 direct-new-declarator:
7468 direct-new-declarator [constant-expression]
7472 static cp_declarator
*
7473 cp_parser_direct_new_declarator (cp_parser
* parser
)
7475 cp_declarator
*declarator
= NULL
;
7482 /* Look for the opening `['. */
7483 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7485 token
= cp_lexer_peek_token (parser
->lexer
);
7486 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7487 /* The standard requires that the expression have integral
7488 type. DR 74 adds enumeration types. We believe that the
7489 real intent is that these expressions be handled like the
7490 expression in a `switch' condition, which also allows
7491 classes with a single conversion to integral or
7492 enumeration type. */
7493 if (!processing_template_decl
)
7496 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7501 error_at (token
->location
,
7502 "expression in new-declarator must have integral "
7503 "or enumeration type");
7504 expression
= error_mark_node
;
7508 /* Look for the closing `]'. */
7509 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7511 /* Add this bound to the declarator. */
7512 declarator
= make_array_declarator (declarator
, expression
);
7514 /* If the next token is not a `[', then there are no more
7516 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7523 /* Parse a new-initializer.
7526 ( expression-list [opt] )
7529 Returns a representation of the expression-list. */
7531 static vec
<tree
, va_gc
> *
7532 cp_parser_new_initializer (cp_parser
* parser
)
7534 vec
<tree
, va_gc
> *expression_list
;
7536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7539 bool expr_non_constant_p
;
7540 cp_lexer_set_source_position (parser
->lexer
);
7541 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7542 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7543 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7544 expression_list
= make_tree_vector_single (t
);
7547 expression_list
= (cp_parser_parenthesized_expression_list
7548 (parser
, non_attr
, /*cast_p=*/false,
7549 /*allow_expansion_p=*/true,
7550 /*non_constant_p=*/NULL
));
7552 return expression_list
;
7555 /* Parse a delete-expression.
7558 :: [opt] delete cast-expression
7559 :: [opt] delete [ ] cast-expression
7561 Returns a representation of the expression. */
7564 cp_parser_delete_expression (cp_parser
* parser
)
7566 bool global_scope_p
;
7570 /* Look for the optional `::' operator. */
7572 = (cp_parser_global_scope_opt (parser
,
7573 /*current_scope_valid_p=*/false)
7575 /* Look for the `delete' keyword. */
7576 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7577 /* See if the array syntax is in use. */
7578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7580 /* Consume the `[' token. */
7581 cp_lexer_consume_token (parser
->lexer
);
7582 /* Look for the `]' token. */
7583 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7584 /* Remember that this is the `[]' construct. */
7590 /* Parse the cast-expression. */
7591 expression
= cp_parser_simple_cast_expression (parser
);
7593 /* A delete-expression may not appear in an integral constant
7595 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7596 return error_mark_node
;
7598 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7599 tf_warning_or_error
);
7602 /* Returns true if TOKEN may start a cast-expression and false
7606 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7608 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7609 switch (token
->type
)
7615 case CPP_CLOSE_SQUARE
:
7616 case CPP_CLOSE_PAREN
:
7617 case CPP_CLOSE_BRACE
:
7618 case CPP_OPEN_BRACE
:
7622 case CPP_DEREF_STAR
:
7630 case CPP_GREATER_EQ
:
7650 case CPP_OPEN_PAREN
:
7651 /* In ((type ()) () the last () isn't a valid cast-expression,
7652 so the whole must be parsed as postfix-expression. */
7653 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7656 /* '[' may start a primary-expression in obj-c++. */
7657 case CPP_OPEN_SQUARE
:
7658 return c_dialect_objc ();
7665 /* Parse a cast-expression.
7669 ( type-id ) cast-expression
7671 ADDRESS_P is true iff the unary-expression is appearing as the
7672 operand of the `&' operator. CAST_P is true if this expression is
7673 the target of a cast.
7675 Returns a representation of the expression. */
7678 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7679 bool decltype_p
, cp_id_kind
* pidk
)
7681 /* If it's a `(', then we might be looking at a cast. */
7682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7684 tree type
= NULL_TREE
;
7685 tree expr
= NULL_TREE
;
7686 bool cast_expression_p
;
7687 const char *saved_message
;
7689 /* There's no way to know yet whether or not this is a cast.
7690 For example, `(int (3))' is a unary-expression, while `(int)
7691 3' is a cast. So, we resort to parsing tentatively. */
7692 cp_parser_parse_tentatively (parser
);
7693 /* Types may not be defined in a cast. */
7694 saved_message
= parser
->type_definition_forbidden_message
;
7695 parser
->type_definition_forbidden_message
7696 = G_("types may not be defined in casts");
7697 /* Consume the `('. */
7698 cp_lexer_consume_token (parser
->lexer
);
7699 /* A very tricky bit is that `(struct S) { 3 }' is a
7700 compound-literal (which we permit in C++ as an extension).
7701 But, that construct is not a cast-expression -- it is a
7702 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7703 is legal; if the compound-literal were a cast-expression,
7704 you'd need an extra set of parentheses.) But, if we parse
7705 the type-id, and it happens to be a class-specifier, then we
7706 will commit to the parse at that point, because we cannot
7707 undo the action that is done when creating a new class. So,
7708 then we cannot back up and do a postfix-expression.
7709 Another tricky case is the following (c++/29234):
7711 struct S { void operator () (); };
7718 As a type-id we parse the parenthesized S()() as a function
7719 returning a function, groktypename complains and we cannot
7720 back up in this case either.
7722 Therefore, we scan ahead to the closing `)', and check to see
7723 if the tokens after the `)' can start a cast-expression. Otherwise
7724 we are dealing with an unary-expression, a postfix-expression
7727 Save tokens so that we can put them back. */
7728 cp_lexer_save_tokens (parser
->lexer
);
7730 /* We may be looking at a cast-expression. */
7732 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7733 /*consume_paren=*/true)
7734 && cp_parser_tokens_start_cast_expression (parser
));
7736 /* Roll back the tokens we skipped. */
7737 cp_lexer_rollback_tokens (parser
->lexer
);
7738 /* If we aren't looking at a cast-expression, simulate an error so
7739 that the call to cp_parser_parse_definitely below will fail. */
7740 if (!cast_expression_p
)
7741 cp_parser_simulate_error (parser
);
7744 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7745 parser
->in_type_id_in_expr_p
= true;
7746 /* Look for the type-id. */
7747 type
= cp_parser_type_id (parser
);
7748 /* Look for the closing `)'. */
7749 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7750 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7753 /* Restore the saved message. */
7754 parser
->type_definition_forbidden_message
= saved_message
;
7756 /* At this point this can only be either a cast or a
7757 parenthesized ctor such as `(T ())' that looks like a cast to
7758 function returning T. */
7759 if (!cp_parser_error_occurred (parser
))
7761 cp_parser_parse_definitely (parser
);
7762 expr
= cp_parser_cast_expression (parser
,
7763 /*address_p=*/false,
7765 /*decltype_p=*/false,
7768 /* Warn about old-style casts, if so requested. */
7769 if (warn_old_style_cast
7770 && !in_system_header_at (input_location
)
7771 && !VOID_TYPE_P (type
)
7772 && current_lang_name
!= lang_name_c
)
7773 warning (OPT_Wold_style_cast
, "use of old-style cast");
7775 /* Only type conversions to integral or enumeration types
7776 can be used in constant-expressions. */
7777 if (!cast_valid_in_integral_constant_expression_p (type
)
7778 && cp_parser_non_integral_constant_expression (parser
,
7780 return error_mark_node
;
7782 /* Perform the cast. */
7783 expr
= build_c_cast (input_location
, type
, expr
);
7787 cp_parser_abort_tentative_parse (parser
);
7790 /* If we get here, then it's not a cast, so it must be a
7791 unary-expression. */
7792 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7796 /* Parse a binary expression of the general form:
7800 pm-expression .* cast-expression
7801 pm-expression ->* cast-expression
7803 multiplicative-expression:
7805 multiplicative-expression * pm-expression
7806 multiplicative-expression / pm-expression
7807 multiplicative-expression % pm-expression
7809 additive-expression:
7810 multiplicative-expression
7811 additive-expression + multiplicative-expression
7812 additive-expression - multiplicative-expression
7816 shift-expression << additive-expression
7817 shift-expression >> additive-expression
7819 relational-expression:
7821 relational-expression < shift-expression
7822 relational-expression > shift-expression
7823 relational-expression <= shift-expression
7824 relational-expression >= shift-expression
7828 relational-expression:
7829 relational-expression <? shift-expression
7830 relational-expression >? shift-expression
7832 equality-expression:
7833 relational-expression
7834 equality-expression == relational-expression
7835 equality-expression != relational-expression
7839 and-expression & equality-expression
7841 exclusive-or-expression:
7843 exclusive-or-expression ^ and-expression
7845 inclusive-or-expression:
7846 exclusive-or-expression
7847 inclusive-or-expression | exclusive-or-expression
7849 logical-and-expression:
7850 inclusive-or-expression
7851 logical-and-expression && inclusive-or-expression
7853 logical-or-expression:
7854 logical-and-expression
7855 logical-or-expression || logical-and-expression
7857 All these are implemented with a single function like:
7860 simple-cast-expression
7861 binary-expression <token> binary-expression
7863 CAST_P is true if this expression is the target of a cast.
7865 The binops_by_token map is used to get the tree codes for each <token> type.
7866 binary-expressions are associated according to a precedence table. */
7868 #define TOKEN_PRECEDENCE(token) \
7869 (((token->type == CPP_GREATER \
7870 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7871 && !parser->greater_than_is_operator_p) \
7872 ? PREC_NOT_OPERATOR \
7873 : binops_by_token[token->type].prec)
7876 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7877 bool no_toplevel_fold_p
,
7879 enum cp_parser_prec prec
,
7882 cp_parser_expression_stack stack
;
7883 cp_parser_expression_stack_entry
*sp
= &stack
[0];
7884 cp_parser_expression_stack_entry current
;
7887 enum tree_code rhs_type
;
7888 enum cp_parser_prec new_prec
, lookahead_prec
;
7891 /* Parse the first expression. */
7892 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
7893 cast_p
, decltype_p
, pidk
);
7894 current
.lhs_type
= ERROR_MARK
;
7895 current
.prec
= prec
;
7897 if (cp_parser_error_occurred (parser
))
7898 return error_mark_node
;
7902 /* Get an operator token. */
7903 token
= cp_lexer_peek_token (parser
->lexer
);
7905 if (warn_cxx0x_compat
7906 && token
->type
== CPP_RSHIFT
7907 && !parser
->greater_than_is_operator_p
)
7909 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
7910 "%<>>%> operator is treated"
7911 " as two right angle brackets in C++11"))
7912 inform (token
->location
,
7913 "suggest parentheses around %<>>%> expression");
7916 new_prec
= TOKEN_PRECEDENCE (token
);
7918 /* Popping an entry off the stack means we completed a subexpression:
7919 - either we found a token which is not an operator (`>' where it is not
7920 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7921 will happen repeatedly;
7922 - or, we found an operator which has lower priority. This is the case
7923 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7925 if (new_prec
<= current
.prec
)
7934 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
7935 current
.loc
= token
->location
;
7937 /* We used the operator token. */
7938 cp_lexer_consume_token (parser
->lexer
);
7940 /* For "false && x" or "true || x", x will never be executed;
7941 disable warnings while evaluating it. */
7942 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7943 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
7944 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7945 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
7947 /* Extract another operand. It may be the RHS of this expression
7948 or the LHS of a new, higher priority expression. */
7949 rhs
= cp_parser_simple_cast_expression (parser
);
7950 rhs_type
= ERROR_MARK
;
7952 /* Get another operator token. Look up its precedence to avoid
7953 building a useless (immediately popped) stack entry for common
7954 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7955 token
= cp_lexer_peek_token (parser
->lexer
);
7956 lookahead_prec
= TOKEN_PRECEDENCE (token
);
7957 if (lookahead_prec
> new_prec
)
7959 /* ... and prepare to parse the RHS of the new, higher priority
7960 expression. Since precedence levels on the stack are
7961 monotonically increasing, we do not have to care about
7966 current
.lhs_type
= rhs_type
;
7967 current
.prec
= new_prec
;
7968 new_prec
= lookahead_prec
;
7972 lookahead_prec
= new_prec
;
7973 /* If the stack is not empty, we have parsed into LHS the right side
7974 (`4' in the example above) of an expression we had suspended.
7975 We can use the information on the stack to recover the LHS (`3')
7976 from the stack together with the tree code (`MULT_EXPR'), and
7977 the precedence of the higher level subexpression
7978 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7979 which will be used to actually build the additive expression. */
7981 rhs_type
= current
.lhs_type
;
7986 /* Undo the disabling of warnings done above. */
7987 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7988 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
7989 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7990 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
7993 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7994 ERROR_MARK for everything that is not a binary expression.
7995 This makes warn_about_parentheses miss some warnings that
7996 involve unary operators. For unary expressions we should
7997 pass the correct tree_code unless the unary expression was
7998 surrounded by parentheses.
8000 if (no_toplevel_fold_p
8001 && lookahead_prec
<= current
.prec
8003 current
.lhs
= build2 (current
.tree_type
,
8004 TREE_CODE_CLASS (current
.tree_type
)
8006 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8009 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8010 current
.lhs
, current
.lhs_type
,
8011 rhs
, rhs_type
, &overload
,
8012 complain_flags (decltype_p
));
8013 current
.lhs_type
= current
.tree_type
;
8014 if (EXPR_P (current
.lhs
))
8015 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
8017 /* If the binary operator required the use of an overloaded operator,
8018 then this expression cannot be an integral constant-expression.
8019 An overloaded operator can be used even if both operands are
8020 otherwise permissible in an integral constant-expression if at
8021 least one of the operands is of enumeration type. */
8024 && cp_parser_non_integral_constant_expression (parser
,
8026 return error_mark_node
;
8033 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8034 bool no_toplevel_fold_p
,
8035 enum cp_parser_prec prec
,
8038 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8039 /*decltype*/false, prec
, pidk
);
8042 /* Parse the `? expression : assignment-expression' part of a
8043 conditional-expression. The LOGICAL_OR_EXPR is the
8044 logical-or-expression that started the conditional-expression.
8045 Returns a representation of the entire conditional-expression.
8047 This routine is used by cp_parser_assignment_expression.
8049 ? expression : assignment-expression
8053 ? : assignment-expression */
8056 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8059 tree assignment_expr
;
8060 struct cp_token
*token
;
8061 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8063 /* Consume the `?' token. */
8064 cp_lexer_consume_token (parser
->lexer
);
8065 token
= cp_lexer_peek_token (parser
->lexer
);
8066 if (cp_parser_allow_gnu_extensions_p (parser
)
8067 && token
->type
== CPP_COLON
)
8069 pedwarn (token
->location
, OPT_Wpedantic
,
8070 "ISO C++ does not allow ?: with omitted middle operand");
8071 /* Implicit true clause. */
8073 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8074 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8078 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8079 parser
->colon_corrects_to_scope_p
= false;
8080 /* Parse the expression. */
8081 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8082 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8083 c_inhibit_evaluation_warnings
+=
8084 ((logical_or_expr
== truthvalue_true_node
)
8085 - (logical_or_expr
== truthvalue_false_node
));
8086 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8089 /* The next token should be a `:'. */
8090 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8091 /* Parse the assignment-expression. */
8092 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
8093 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8095 /* Build the conditional-expression. */
8096 return build_x_conditional_expr (loc
, logical_or_expr
,
8099 tf_warning_or_error
);
8102 /* Parse an assignment-expression.
8104 assignment-expression:
8105 conditional-expression
8106 logical-or-expression assignment-operator assignment_expression
8109 CAST_P is true if this expression is the target of a cast.
8110 DECLTYPE_P is true if this expression is the operand of decltype.
8112 Returns a representation for the expression. */
8115 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
8116 bool decltype_p
, cp_id_kind
* pidk
)
8120 /* If the next token is the `throw' keyword, then we're looking at
8121 a throw-expression. */
8122 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8123 expr
= cp_parser_throw_expression (parser
);
8124 /* Otherwise, it must be that we are looking at a
8125 logical-or-expression. */
8128 /* Parse the binary expressions (logical-or-expression). */
8129 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8131 PREC_NOT_OPERATOR
, pidk
);
8132 /* If the next token is a `?' then we're actually looking at a
8133 conditional-expression. */
8134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8135 return cp_parser_question_colon_clause (parser
, expr
);
8138 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8140 /* If it's an assignment-operator, we're using the second
8142 enum tree_code assignment_operator
8143 = cp_parser_assignment_operator_opt (parser
);
8144 if (assignment_operator
!= ERROR_MARK
)
8146 bool non_constant_p
;
8147 location_t saved_input_location
;
8149 /* Parse the right-hand side of the assignment. */
8150 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8152 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8153 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8155 /* An assignment may not appear in a
8156 constant-expression. */
8157 if (cp_parser_non_integral_constant_expression (parser
,
8159 return error_mark_node
;
8160 /* Build the assignment expression. Its default
8161 location is the location of the '=' token. */
8162 saved_input_location
= input_location
;
8163 input_location
= loc
;
8164 expr
= build_x_modify_expr (loc
, expr
,
8165 assignment_operator
,
8167 complain_flags (decltype_p
));
8168 input_location
= saved_input_location
;
8177 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
8180 return cp_parser_assignment_expression (parser
, cast_p
,
8181 /*decltype*/false, pidk
);
8184 /* Parse an (optional) assignment-operator.
8186 assignment-operator: one of
8187 = *= /= %= += -= >>= <<= &= ^= |=
8191 assignment-operator: one of
8194 If the next token is an assignment operator, the corresponding tree
8195 code is returned, and the token is consumed. For example, for
8196 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8197 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8198 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8199 operator, ERROR_MARK is returned. */
8201 static enum tree_code
8202 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8207 /* Peek at the next token. */
8208 token
= cp_lexer_peek_token (parser
->lexer
);
8210 switch (token
->type
)
8221 op
= TRUNC_DIV_EXPR
;
8225 op
= TRUNC_MOD_EXPR
;
8257 /* Nothing else is an assignment operator. */
8261 /* If it was an assignment operator, consume it. */
8262 if (op
!= ERROR_MARK
)
8263 cp_lexer_consume_token (parser
->lexer
);
8268 /* Parse an expression.
8271 assignment-expression
8272 expression , assignment-expression
8274 CAST_P is true if this expression is the target of a cast.
8275 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8276 except possibly parenthesized or on the RHS of a comma (N3276).
8278 Returns a representation of the expression. */
8281 cp_parser_expression (cp_parser
* parser
, bool cast_p
, bool decltype_p
,
8284 tree expression
= NULL_TREE
;
8285 location_t loc
= UNKNOWN_LOCATION
;
8289 tree assignment_expression
;
8291 /* Parse the next assignment-expression. */
8292 assignment_expression
8293 = cp_parser_assignment_expression (parser
, cast_p
, decltype_p
, pidk
);
8295 /* We don't create a temporary for a call that is the immediate operand
8296 of decltype or on the RHS of a comma. But when we see a comma, we
8297 need to create a temporary for a call on the LHS. */
8298 if (decltype_p
&& !processing_template_decl
8299 && TREE_CODE (assignment_expression
) == CALL_EXPR
8300 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8301 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8302 assignment_expression
8303 = build_cplus_new (TREE_TYPE (assignment_expression
),
8304 assignment_expression
, tf_warning_or_error
);
8306 /* If this is the first assignment-expression, we can just
8309 expression
= assignment_expression
;
8311 expression
= build_x_compound_expr (loc
, expression
,
8312 assignment_expression
,
8313 complain_flags (decltype_p
));
8314 /* If the next token is not a comma, then we are done with the
8316 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8318 /* Consume the `,'. */
8319 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8320 cp_lexer_consume_token (parser
->lexer
);
8321 /* A comma operator cannot appear in a constant-expression. */
8322 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8323 expression
= error_mark_node
;
8330 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
8332 return cp_parser_expression (parser
, cast_p
, /*decltype*/false, pidk
);
8335 /* Parse a constant-expression.
8337 constant-expression:
8338 conditional-expression
8340 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8341 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8342 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8343 is false, NON_CONSTANT_P should be NULL. */
8346 cp_parser_constant_expression (cp_parser
* parser
,
8347 bool allow_non_constant_p
,
8348 bool *non_constant_p
)
8350 bool saved_integral_constant_expression_p
;
8351 bool saved_allow_non_integral_constant_expression_p
;
8352 bool saved_non_integral_constant_expression_p
;
8355 /* It might seem that we could simply parse the
8356 conditional-expression, and then check to see if it were
8357 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8358 one that the compiler can figure out is constant, possibly after
8359 doing some simplifications or optimizations. The standard has a
8360 precise definition of constant-expression, and we must honor
8361 that, even though it is somewhat more restrictive.
8367 is not a legal declaration, because `(2, 3)' is not a
8368 constant-expression. The `,' operator is forbidden in a
8369 constant-expression. However, GCC's constant-folding machinery
8370 will fold this operation to an INTEGER_CST for `3'. */
8372 /* Save the old settings. */
8373 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8374 saved_allow_non_integral_constant_expression_p
8375 = parser
->allow_non_integral_constant_expression_p
;
8376 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8377 /* We are now parsing a constant-expression. */
8378 parser
->integral_constant_expression_p
= true;
8379 parser
->allow_non_integral_constant_expression_p
8380 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8381 parser
->non_integral_constant_expression_p
= false;
8382 /* Although the grammar says "conditional-expression", we parse an
8383 "assignment-expression", which also permits "throw-expression"
8384 and the use of assignment operators. In the case that
8385 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8386 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8387 actually essential that we look for an assignment-expression.
8388 For example, cp_parser_initializer_clauses uses this function to
8389 determine whether a particular assignment-expression is in fact
8391 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
8392 /* Restore the old settings. */
8393 parser
->integral_constant_expression_p
8394 = saved_integral_constant_expression_p
;
8395 parser
->allow_non_integral_constant_expression_p
8396 = saved_allow_non_integral_constant_expression_p
;
8397 if (cxx_dialect
>= cxx11
)
8399 /* Require an rvalue constant expression here; that's what our
8400 callers expect. Reference constant expressions are handled
8401 separately in e.g. cp_parser_template_argument. */
8402 bool is_const
= potential_rvalue_constant_expression (expression
);
8403 parser
->non_integral_constant_expression_p
= !is_const
;
8404 if (!is_const
&& !allow_non_constant_p
)
8405 require_potential_rvalue_constant_expression (expression
);
8407 if (allow_non_constant_p
)
8408 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8409 parser
->non_integral_constant_expression_p
8410 = saved_non_integral_constant_expression_p
;
8415 /* Parse __builtin_offsetof.
8417 offsetof-expression:
8418 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8420 offsetof-member-designator:
8422 | offsetof-member-designator "." id-expression
8423 | offsetof-member-designator "[" expression "]"
8424 | offsetof-member-designator "->" id-expression */
8427 cp_parser_builtin_offsetof (cp_parser
*parser
)
8429 int save_ice_p
, save_non_ice_p
;
8434 /* We're about to accept non-integral-constant things, but will
8435 definitely yield an integral constant expression. Save and
8436 restore these values around our local parsing. */
8437 save_ice_p
= parser
->integral_constant_expression_p
;
8438 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8440 /* Consume the "__builtin_offsetof" token. */
8441 cp_lexer_consume_token (parser
->lexer
);
8442 /* Consume the opening `('. */
8443 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8444 /* Parse the type-id. */
8445 type
= cp_parser_type_id (parser
);
8446 /* Look for the `,'. */
8447 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8448 token
= cp_lexer_peek_token (parser
->lexer
);
8450 /* Build the (type *)null that begins the traditional offsetof macro. */
8451 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8452 tf_warning_or_error
);
8454 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8455 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8456 true, &dummy
, token
->location
);
8459 token
= cp_lexer_peek_token (parser
->lexer
);
8460 switch (token
->type
)
8462 case CPP_OPEN_SQUARE
:
8463 /* offsetof-member-designator "[" expression "]" */
8464 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8469 /* offsetof-member-designator "->" identifier */
8470 expr
= grok_array_decl (token
->location
, expr
,
8471 integer_zero_node
, false);
8475 /* offsetof-member-designator "." identifier */
8476 cp_lexer_consume_token (parser
->lexer
);
8477 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8482 case CPP_CLOSE_PAREN
:
8483 /* Consume the ")" token. */
8484 cp_lexer_consume_token (parser
->lexer
);
8488 /* Error. We know the following require will fail, but
8489 that gives the proper error message. */
8490 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8491 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8492 expr
= error_mark_node
;
8498 /* If we're processing a template, we can't finish the semantics yet.
8499 Otherwise we can fold the entire expression now. */
8500 if (processing_template_decl
)
8501 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
8503 expr
= finish_offsetof (expr
);
8506 parser
->integral_constant_expression_p
= save_ice_p
;
8507 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8512 /* Parse a trait expression.
8514 Returns a representation of the expression, the underlying type
8515 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8518 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8521 tree type1
, type2
= NULL_TREE
;
8522 bool binary
= false;
8523 cp_decl_specifier_seq decl_specs
;
8527 case RID_HAS_NOTHROW_ASSIGN
:
8528 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8530 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8531 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8533 case RID_HAS_NOTHROW_COPY
:
8534 kind
= CPTK_HAS_NOTHROW_COPY
;
8536 case RID_HAS_TRIVIAL_ASSIGN
:
8537 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8539 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8540 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8542 case RID_HAS_TRIVIAL_COPY
:
8543 kind
= CPTK_HAS_TRIVIAL_COPY
;
8545 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8546 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8548 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8549 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8551 case RID_IS_ABSTRACT
:
8552 kind
= CPTK_IS_ABSTRACT
;
8554 case RID_IS_BASE_OF
:
8555 kind
= CPTK_IS_BASE_OF
;
8559 kind
= CPTK_IS_CLASS
;
8561 case RID_IS_CONVERTIBLE_TO
:
8562 kind
= CPTK_IS_CONVERTIBLE_TO
;
8566 kind
= CPTK_IS_EMPTY
;
8569 kind
= CPTK_IS_ENUM
;
8572 kind
= CPTK_IS_FINAL
;
8574 case RID_IS_LITERAL_TYPE
:
8575 kind
= CPTK_IS_LITERAL_TYPE
;
8580 case RID_IS_POLYMORPHIC
:
8581 kind
= CPTK_IS_POLYMORPHIC
;
8583 case RID_IS_STD_LAYOUT
:
8584 kind
= CPTK_IS_STD_LAYOUT
;
8586 case RID_IS_TRIVIAL
:
8587 kind
= CPTK_IS_TRIVIAL
;
8590 kind
= CPTK_IS_UNION
;
8592 case RID_UNDERLYING_TYPE
:
8593 kind
= CPTK_UNDERLYING_TYPE
;
8598 case RID_DIRECT_BASES
:
8599 kind
= CPTK_DIRECT_BASES
;
8605 /* Consume the token. */
8606 cp_lexer_consume_token (parser
->lexer
);
8608 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8610 type1
= cp_parser_type_id (parser
);
8612 if (type1
== error_mark_node
)
8613 return error_mark_node
;
8615 /* Build a trivial decl-specifier-seq. */
8616 clear_decl_specs (&decl_specs
);
8617 decl_specs
.type
= type1
;
8619 /* Call grokdeclarator to figure out what type this is. */
8620 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8621 /*initialized=*/0, /*attrlist=*/NULL
);
8625 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8627 type2
= cp_parser_type_id (parser
);
8629 if (type2
== error_mark_node
)
8630 return error_mark_node
;
8632 /* Build a trivial decl-specifier-seq. */
8633 clear_decl_specs (&decl_specs
);
8634 decl_specs
.type
= type2
;
8636 /* Call grokdeclarator to figure out what type this is. */
8637 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8638 /*initialized=*/0, /*attrlist=*/NULL
);
8641 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8643 /* Complete the trait expression, which may mean either processing
8644 the trait expr now or saving it for template instantiation. */
8647 case CPTK_UNDERLYING_TYPE
:
8648 return finish_underlying_type (type1
);
8650 return finish_bases (type1
, false);
8651 case CPTK_DIRECT_BASES
:
8652 return finish_bases (type1
, true);
8654 return finish_trait_expr (kind
, type1
, type2
);
8658 /* Lambdas that appear in variable initializer or default argument scope
8659 get that in their mangling, so we need to record it. We might as well
8660 use the count for function and namespace scopes as well. */
8661 static GTY(()) tree lambda_scope
;
8662 static GTY(()) int lambda_count
;
8663 typedef struct GTY(()) tree_int
8668 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8671 start_lambda_scope (tree decl
)
8675 /* Once we're inside a function, we ignore other scopes and just push
8676 the function again so that popping works properly. */
8677 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8678 decl
= current_function_decl
;
8679 ti
.t
= lambda_scope
;
8680 ti
.i
= lambda_count
;
8681 vec_safe_push (lambda_scope_stack
, ti
);
8682 if (lambda_scope
!= decl
)
8684 /* Don't reset the count if we're still in the same function. */
8685 lambda_scope
= decl
;
8691 record_lambda_scope (tree lambda
)
8693 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8694 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8698 finish_lambda_scope (void)
8700 tree_int
*p
= &lambda_scope_stack
->last ();
8701 if (lambda_scope
!= p
->t
)
8703 lambda_scope
= p
->t
;
8704 lambda_count
= p
->i
;
8706 lambda_scope_stack
->pop ();
8709 /* Parse a lambda expression.
8712 lambda-introducer lambda-declarator [opt] compound-statement
8714 Returns a representation of the expression. */
8717 cp_parser_lambda_expression (cp_parser
* parser
)
8719 tree lambda_expr
= build_lambda_expr ();
8722 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8724 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
8726 if (cp_unevaluated_operand
)
8728 if (!token
->error_reported
)
8730 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8731 "lambda-expression in unevaluated context");
8732 token
->error_reported
= true;
8737 /* We may be in the middle of deferred access check. Disable
8739 push_deferring_access_checks (dk_no_deferred
);
8741 cp_parser_lambda_introducer (parser
, lambda_expr
);
8743 type
= begin_lambda_type (lambda_expr
);
8744 if (type
== error_mark_node
)
8745 return error_mark_node
;
8747 record_lambda_scope (lambda_expr
);
8749 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8750 determine_visibility (TYPE_NAME (type
));
8752 /* Now that we've started the type, add the capture fields for any
8753 explicit captures. */
8754 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8757 /* Inside the class, surrounding template-parameter-lists do not apply. */
8758 unsigned int saved_num_template_parameter_lists
8759 = parser
->num_template_parameter_lists
;
8760 unsigned char in_statement
= parser
->in_statement
;
8761 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
8762 bool fully_implicit_function_template_p
8763 = parser
->fully_implicit_function_template_p
;
8764 tree implicit_template_parms
= parser
->implicit_template_parms
;
8765 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
8766 bool auto_is_implicit_function_template_parm_p
8767 = parser
->auto_is_implicit_function_template_parm_p
;
8769 parser
->num_template_parameter_lists
= 0;
8770 parser
->in_statement
= 0;
8771 parser
->in_switch_statement_p
= false;
8772 parser
->fully_implicit_function_template_p
= false;
8773 parser
->implicit_template_parms
= 0;
8774 parser
->implicit_template_scope
= 0;
8775 parser
->auto_is_implicit_function_template_parm_p
= false;
8777 /* By virtue of defining a local class, a lambda expression has access to
8778 the private variables of enclosing classes. */
8780 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
8783 cp_parser_lambda_body (parser
, lambda_expr
);
8784 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8786 if (cp_parser_skip_to_closing_brace (parser
))
8787 cp_lexer_consume_token (parser
->lexer
);
8790 /* The capture list was built up in reverse order; fix that now. */
8791 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
8792 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8795 maybe_add_lambda_conv_op (type
);
8797 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
8799 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
8800 parser
->in_statement
= in_statement
;
8801 parser
->in_switch_statement_p
= in_switch_statement_p
;
8802 parser
->fully_implicit_function_template_p
8803 = fully_implicit_function_template_p
;
8804 parser
->implicit_template_parms
= implicit_template_parms
;
8805 parser
->implicit_template_scope
= implicit_template_scope
;
8806 parser
->auto_is_implicit_function_template_parm_p
8807 = auto_is_implicit_function_template_parm_p
;
8810 pop_deferring_access_checks ();
8812 /* This field is only used during parsing of the lambda. */
8813 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
8815 /* This lambda shouldn't have any proxies left at this point. */
8816 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
8817 /* And now that we're done, push proxies for an enclosing lambda. */
8818 insert_pending_capture_proxies ();
8821 return build_lambda_object (lambda_expr
);
8823 return error_mark_node
;
8826 /* Parse the beginning of a lambda expression.
8829 [ lambda-capture [opt] ]
8831 LAMBDA_EXPR is the current representation of the lambda expression. */
8834 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
8836 /* Need commas after the first capture. */
8839 /* Eat the leading `['. */
8840 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8842 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
8844 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
8845 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
8846 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8847 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
8849 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
8851 cp_lexer_consume_token (parser
->lexer
);
8855 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
8857 cp_token
* capture_token
;
8859 tree capture_init_expr
;
8860 cp_id_kind idk
= CP_ID_KIND_NONE
;
8861 bool explicit_init_p
= false;
8863 enum capture_kind_type
8868 enum capture_kind_type capture_kind
= BY_COPY
;
8870 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
8872 error ("expected end of capture-list");
8879 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8881 /* Possibly capture `this'. */
8882 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
8884 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8885 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
8886 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
8887 "with by-copy capture default");
8888 cp_lexer_consume_token (parser
->lexer
);
8889 add_capture (lambda_expr
,
8890 /*id=*/this_identifier
,
8891 /*initializer=*/finish_this_expr(),
8892 /*by_reference_p=*/false,
8897 /* Remember whether we want to capture as a reference or not. */
8898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
8900 capture_kind
= BY_REFERENCE
;
8901 cp_lexer_consume_token (parser
->lexer
);
8904 /* Get the identifier. */
8905 capture_token
= cp_lexer_peek_token (parser
->lexer
);
8906 capture_id
= cp_parser_identifier (parser
);
8908 if (capture_id
== error_mark_node
)
8909 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8910 delimiters, but I modified this to stop on unnested ']' as well. It
8911 was already changed to stop on unnested '}', so the
8912 "closing_parenthesis" name is no more misleading with my change. */
8914 cp_parser_skip_to_closing_parenthesis (parser
,
8915 /*recovering=*/true,
8917 /*consume_paren=*/true);
8921 /* Find the initializer for this capture. */
8922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
8923 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
8924 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8926 bool direct
, non_constant
;
8927 /* An explicit initializer exists. */
8928 if (cxx_dialect
< cxx1y
)
8929 pedwarn (input_location
, 0,
8930 "lambda capture initializers "
8931 "only available with -std=c++1y or -std=gnu++1y");
8932 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
8934 explicit_init_p
= true;
8935 if (capture_init_expr
== NULL_TREE
)
8937 error ("empty initializer for lambda init-capture");
8938 capture_init_expr
= error_mark_node
;
8943 const char* error_msg
;
8945 /* Turn the identifier into an id-expression. */
8947 = cp_parser_lookup_name_simple (parser
, capture_id
,
8948 capture_token
->location
);
8950 if (capture_init_expr
== error_mark_node
)
8952 unqualified_name_lookup_error (capture_id
);
8955 else if (DECL_P (capture_init_expr
)
8956 && (!VAR_P (capture_init_expr
)
8957 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
8959 error_at (capture_token
->location
,
8960 "capture of non-variable %qD ",
8962 inform (0, "%q+#D declared here", capture_init_expr
);
8965 if (VAR_P (capture_init_expr
)
8966 && decl_storage_duration (capture_init_expr
) != dk_auto
)
8968 if (pedwarn (capture_token
->location
, 0, "capture of variable "
8969 "%qD with non-automatic storage duration",
8971 inform (0, "%q+#D declared here", capture_init_expr
);
8976 = finish_id_expression
8981 /*integral_constant_expression_p=*/false,
8982 /*allow_non_integral_constant_expression_p=*/false,
8983 /*non_integral_constant_expression_p=*/NULL
,
8984 /*template_p=*/false,
8986 /*address_p=*/false,
8987 /*template_arg_p=*/false,
8989 capture_token
->location
);
8991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8993 cp_lexer_consume_token (parser
->lexer
);
8994 capture_init_expr
= make_pack_expansion (capture_init_expr
);
8997 check_for_bare_parameter_packs (capture_init_expr
);
9000 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9001 && !explicit_init_p
)
9003 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9004 && capture_kind
== BY_COPY
)
9005 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9006 "of %qD redundant with by-copy capture default",
9008 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9009 && capture_kind
== BY_REFERENCE
)
9010 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9011 "capture of %qD redundant with by-reference capture "
9012 "default", capture_id
);
9015 add_capture (lambda_expr
,
9018 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9022 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9025 /* Parse the (optional) middle of a lambda expression.
9028 < template-parameter-list [opt] >
9029 ( parameter-declaration-clause [opt] )
9030 attribute-specifier [opt]
9032 exception-specification [opt]
9033 lambda-return-type-clause [opt]
9035 LAMBDA_EXPR is the current representation of the lambda expression. */
9038 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9040 /* 5.1.1.4 of the standard says:
9041 If a lambda-expression does not include a lambda-declarator, it is as if
9042 the lambda-declarator were ().
9043 This means an empty parameter list, no attributes, and no exception
9045 tree param_list
= void_list_node
;
9046 tree attributes
= NULL_TREE
;
9047 tree exception_spec
= NULL_TREE
;
9048 tree template_param_list
= NULL_TREE
;
9050 /* The template-parameter-list is optional, but must begin with
9051 an opening angle if present. */
9052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9054 if (cxx_dialect
< cxx1y
)
9055 pedwarn (parser
->lexer
->next_token
->location
, 0,
9056 "lambda templates are only available with "
9057 "-std=c++1y or -std=gnu++1y");
9059 cp_lexer_consume_token (parser
->lexer
);
9061 template_param_list
= cp_parser_template_parameter_list (parser
);
9063 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9065 /* We just processed one more parameter list. */
9066 ++parser
->num_template_parameter_lists
;
9069 /* The parameter-declaration-clause is optional (unless
9070 template-parameter-list was given), but must begin with an
9071 opening parenthesis if present. */
9072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9074 cp_lexer_consume_token (parser
->lexer
);
9076 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9078 /* Parse parameters. */
9079 param_list
= cp_parser_parameter_declaration_clause (parser
);
9081 /* Default arguments shall not be specified in the
9082 parameter-declaration-clause of a lambda-declarator. */
9083 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9084 if (TREE_PURPOSE (t
))
9085 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9086 "default argument specified for lambda parameter");
9088 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9090 attributes
= cp_parser_attributes_opt (parser
);
9092 /* Parse optional `mutable' keyword. */
9093 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9095 cp_lexer_consume_token (parser
->lexer
);
9096 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9099 /* Parse optional exception specification. */
9100 exception_spec
= cp_parser_exception_specification_opt (parser
);
9102 /* Parse optional trailing return type. */
9103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9105 cp_lexer_consume_token (parser
->lexer
);
9106 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9107 = cp_parser_trailing_type_id (parser
);
9110 /* The function parameters must be in scope all the way until after the
9111 trailing-return-type in case of decltype. */
9112 pop_bindings_and_leave_scope ();
9114 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9115 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9117 /* Create the function call operator.
9119 Messing with declarators like this is no uglier than building up the
9120 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9123 cp_decl_specifier_seq return_type_specs
;
9124 cp_declarator
* declarator
;
9129 clear_decl_specs (&return_type_specs
);
9130 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9131 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9133 /* Maybe we will deduce the return type later. */
9134 return_type_specs
.type
= make_auto ();
9136 p
= obstack_alloc (&declarator_obstack
, 0);
9138 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9141 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9142 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9143 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9144 VIRT_SPEC_UNSPECIFIED
,
9147 /*late_return_type=*/NULL_TREE
);
9148 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9150 fco
= grokmethod (&return_type_specs
,
9153 if (fco
!= error_mark_node
)
9155 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9156 DECL_ARTIFICIAL (fco
) = 1;
9157 /* Give the object parameter a different name. */
9158 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9160 if (template_param_list
)
9162 fco
= finish_member_template_decl (fco
);
9163 finish_template_decl (template_param_list
);
9164 --parser
->num_template_parameter_lists
;
9166 else if (parser
->fully_implicit_function_template_p
)
9167 fco
= finish_fully_implicit_template (parser
, fco
);
9169 finish_member_declaration (fco
);
9171 obstack_free (&declarator_obstack
, p
);
9173 return (fco
!= error_mark_node
);
9177 /* Parse the body of a lambda expression, which is simply
9181 but which requires special handling.
9182 LAMBDA_EXPR is the current representation of the lambda expression. */
9185 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9187 bool nested
= (current_function_decl
!= NULL_TREE
);
9188 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9190 push_function_context ();
9192 /* Still increment function_depth so that we don't GC in the
9193 middle of an expression. */
9195 /* Clear this in case we're in the middle of a default argument. */
9196 parser
->local_variables_forbidden_p
= false;
9198 /* Finish the function call operator
9200 + late_parsing_for_member
9201 + function_definition_after_declarator
9202 + ctor_initializer_opt_and_function_body */
9204 tree fco
= lambda_function (lambda_expr
);
9210 /* Let the front end know that we are going to be defining this
9212 start_preparsed_function (fco
,
9214 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9216 start_lambda_scope (fco
);
9217 body
= begin_function_body ();
9219 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9222 /* Push the proxies for any explicit captures. */
9223 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9224 cap
= TREE_CHAIN (cap
))
9225 build_capture_proxy (TREE_PURPOSE (cap
));
9227 compound_stmt
= begin_compound_stmt (0);
9229 /* 5.1.1.4 of the standard says:
9230 If a lambda-expression does not include a trailing-return-type, it
9231 is as if the trailing-return-type denotes the following type:
9232 * if the compound-statement is of the form
9233 { return attribute-specifier [opt] expression ; }
9234 the type of the returned expression after lvalue-to-rvalue
9235 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9236 (_conv.array_ 4.2), and function-to-pointer conversion
9238 * otherwise, void. */
9240 /* In a lambda that has neither a lambda-return-type-clause
9241 nor a deducible form, errors should be reported for return statements
9242 in the body. Since we used void as the placeholder return type, parsing
9243 the body as usual will give such desired behavior. */
9244 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9245 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9246 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9248 tree expr
= NULL_TREE
;
9249 cp_id_kind idk
= CP_ID_KIND_NONE
;
9251 /* Parse tentatively in case there's more after the initial return
9253 cp_parser_parse_tentatively (parser
);
9255 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9257 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
9259 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9260 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9262 if (cp_parser_parse_definitely (parser
))
9264 if (!processing_template_decl
)
9265 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9267 /* Will get error here if type not deduced yet. */
9268 finish_return_stmt (expr
);
9276 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9277 cp_parser_label_declaration (parser
);
9278 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9279 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9282 finish_compound_stmt (compound_stmt
);
9285 finish_function_body (body
);
9286 finish_lambda_scope ();
9288 /* Finish the function and generate code for it if necessary. */
9289 tree fn
= finish_function (/*inline*/2);
9291 /* Only expand if the call op is not a template. */
9292 if (!DECL_TEMPLATE_INFO (fco
))
9293 expand_or_defer_fn (fn
);
9296 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9298 pop_function_context();
9303 /* Statements [gram.stmt.stmt] */
9305 /* Parse a statement.
9309 expression-statement
9314 declaration-statement
9321 attribute-specifier-seq (opt) expression-statement
9322 attribute-specifier-seq (opt) compound-statement
9323 attribute-specifier-seq (opt) selection-statement
9324 attribute-specifier-seq (opt) iteration-statement
9325 attribute-specifier-seq (opt) jump-statement
9326 declaration-statement
9327 attribute-specifier-seq (opt) try-block
9334 IN_COMPOUND is true when the statement is nested inside a
9335 cp_parser_compound_statement; this matters for certain pragmas.
9337 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9338 is a (possibly labeled) if statement which is not enclosed in braces
9339 and has an else clause. This is used to implement -Wparentheses. */
9342 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9343 bool in_compound
, bool *if_p
)
9345 tree statement
, std_attrs
= NULL_TREE
;
9347 location_t statement_location
, attrs_location
;
9352 /* There is no statement yet. */
9353 statement
= NULL_TREE
;
9355 cp_lexer_save_tokens (parser
->lexer
);
9356 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9357 if (c_dialect_objc ())
9358 /* In obj-c++, seeing '[[' might be the either the beginning of
9359 c++11 attributes, or a nested objc-message-expression. So
9360 let's parse the c++11 attributes tentatively. */
9361 cp_parser_parse_tentatively (parser
);
9362 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9363 if (c_dialect_objc ())
9365 if (!cp_parser_parse_definitely (parser
))
9366 std_attrs
= NULL_TREE
;
9369 /* Peek at the next token. */
9370 token
= cp_lexer_peek_token (parser
->lexer
);
9371 /* Remember the location of the first token in the statement. */
9372 statement_location
= token
->location
;
9373 /* If this is a keyword, then that will often determine what kind of
9374 statement we have. */
9375 if (token
->type
== CPP_KEYWORD
)
9377 enum rid keyword
= token
->keyword
;
9383 /* Looks like a labeled-statement with a case label.
9384 Parse the label, and then use tail recursion to parse
9386 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9391 statement
= cp_parser_selection_statement (parser
, if_p
);
9397 statement
= cp_parser_iteration_statement (parser
, false);
9404 statement
= cp_parser_jump_statement (parser
);
9407 /* Objective-C++ exception-handling constructs. */
9410 case RID_AT_FINALLY
:
9411 case RID_AT_SYNCHRONIZED
:
9413 statement
= cp_parser_objc_statement (parser
);
9417 statement
= cp_parser_try_block (parser
);
9421 /* This must be a namespace alias definition. */
9422 cp_parser_declaration_statement (parser
);
9425 case RID_TRANSACTION_ATOMIC
:
9426 case RID_TRANSACTION_RELAXED
:
9427 statement
= cp_parser_transaction (parser
, keyword
);
9429 case RID_TRANSACTION_CANCEL
:
9430 statement
= cp_parser_transaction_cancel (parser
);
9434 /* It might be a keyword like `int' that can start a
9435 declaration-statement. */
9439 else if (token
->type
== CPP_NAME
)
9441 /* If the next token is a `:', then we are looking at a
9442 labeled-statement. */
9443 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9444 if (token
->type
== CPP_COLON
)
9446 /* Looks like a labeled-statement with an ordinary label.
9447 Parse the label, and then use tail recursion to parse
9450 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9454 /* Anything that starts with a `{' must be a compound-statement. */
9455 else if (token
->type
== CPP_OPEN_BRACE
)
9456 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9457 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9458 a statement all its own. */
9459 else if (token
->type
== CPP_PRAGMA
)
9461 /* Only certain OpenMP pragmas are attached to statements, and thus
9462 are considered statements themselves. All others are not. In
9463 the context of a compound, accept the pragma as a "statement" and
9464 return so that we can check for a close brace. Otherwise we
9465 require a real statement and must go back and read one. */
9467 cp_parser_pragma (parser
, pragma_compound
);
9468 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9472 else if (token
->type
== CPP_EOF
)
9474 cp_parser_error (parser
, "expected statement");
9478 /* Everything else must be a declaration-statement or an
9479 expression-statement. Try for the declaration-statement
9480 first, unless we are looking at a `;', in which case we know that
9481 we have an expression-statement. */
9484 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9486 if (std_attrs
!= NULL_TREE
)
9488 /* Attributes should be parsed as part of the the
9489 declaration, so let's un-parse them. */
9490 cp_lexer_rollback_tokens (parser
->lexer
);
9491 std_attrs
= NULL_TREE
;
9494 cp_parser_parse_tentatively (parser
);
9495 /* Try to parse the declaration-statement. */
9496 cp_parser_declaration_statement (parser
);
9497 /* If that worked, we're done. */
9498 if (cp_parser_parse_definitely (parser
))
9501 /* Look for an expression-statement instead. */
9502 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9505 /* Set the line number for the statement. */
9506 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9507 SET_EXPR_LOCATION (statement
, statement_location
);
9509 /* Note that for now, we don't do anything with c++11 statements
9510 parsed at this level. */
9511 if (std_attrs
!= NULL_TREE
)
9512 warning_at (attrs_location
,
9514 "attributes at the beginning of statement are ignored");
9517 /* Parse the label for a labeled-statement, i.e.
9520 case constant-expression :
9524 case constant-expression ... constant-expression : statement
9526 When a label is parsed without errors, the label is added to the
9527 parse tree by the finish_* functions, so this function doesn't
9528 have to return the label. */
9531 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9534 tree label
= NULL_TREE
;
9535 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9537 /* The next token should be an identifier. */
9538 token
= cp_lexer_peek_token (parser
->lexer
);
9539 if (token
->type
!= CPP_NAME
9540 && token
->type
!= CPP_KEYWORD
)
9542 cp_parser_error (parser
, "expected labeled-statement");
9546 parser
->colon_corrects_to_scope_p
= false;
9547 switch (token
->keyword
)
9554 /* Consume the `case' token. */
9555 cp_lexer_consume_token (parser
->lexer
);
9556 /* Parse the constant-expression. */
9557 expr
= cp_parser_constant_expression (parser
,
9558 /*allow_non_constant_p=*/false,
9561 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9562 if (ellipsis
->type
== CPP_ELLIPSIS
)
9564 /* Consume the `...' token. */
9565 cp_lexer_consume_token (parser
->lexer
);
9567 cp_parser_constant_expression (parser
,
9568 /*allow_non_constant_p=*/false,
9570 /* We don't need to emit warnings here, as the common code
9571 will do this for us. */
9574 expr_hi
= NULL_TREE
;
9576 if (parser
->in_switch_statement_p
)
9577 finish_case_label (token
->location
, expr
, expr_hi
);
9579 error_at (token
->location
,
9580 "case label %qE not within a switch statement",
9586 /* Consume the `default' token. */
9587 cp_lexer_consume_token (parser
->lexer
);
9589 if (parser
->in_switch_statement_p
)
9590 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9592 error_at (token
->location
, "case label not within a switch statement");
9596 /* Anything else must be an ordinary label. */
9597 label
= finish_label_stmt (cp_parser_identifier (parser
));
9601 /* Require the `:' token. */
9602 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9604 /* An ordinary label may optionally be followed by attributes.
9605 However, this is only permitted if the attributes are then
9606 followed by a semicolon. This is because, for backward
9607 compatibility, when parsing
9608 lab: __attribute__ ((unused)) int i;
9609 we want the attribute to attach to "i", not "lab". */
9610 if (label
!= NULL_TREE
9611 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9614 cp_parser_parse_tentatively (parser
);
9615 attrs
= cp_parser_gnu_attributes_opt (parser
);
9616 if (attrs
== NULL_TREE
9617 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9618 cp_parser_abort_tentative_parse (parser
);
9619 else if (!cp_parser_parse_definitely (parser
))
9622 attributes
= chainon (attributes
, attrs
);
9625 if (attributes
!= NULL_TREE
)
9626 cplus_decl_attributes (&label
, attributes
, 0);
9628 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9631 /* Parse an expression-statement.
9633 expression-statement:
9636 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9637 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9638 indicates whether this expression-statement is part of an
9639 expression statement. */
9642 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9644 tree statement
= NULL_TREE
;
9645 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9647 /* If the next token is a ';', then there is no expression
9649 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9651 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9652 if (statement
== error_mark_node
9653 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9655 cp_parser_skip_to_end_of_block_or_statement (parser
);
9656 return error_mark_node
;
9660 /* Give a helpful message for "A<T>::type t;" and the like. */
9661 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9662 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9664 if (TREE_CODE (statement
) == SCOPE_REF
)
9665 error_at (token
->location
, "need %<typename%> before %qE because "
9666 "%qT is a dependent scope",
9667 statement
, TREE_OPERAND (statement
, 0));
9668 else if (is_overloaded_fn (statement
)
9669 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9672 tree fn
= get_first_fn (statement
);
9673 error_at (token
->location
,
9674 "%<%T::%D%> names the constructor, not the type",
9675 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9679 /* Consume the final `;'. */
9680 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9682 if (in_statement_expr
9683 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9684 /* This is the final expression statement of a statement
9686 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9688 statement
= finish_expr_stmt (statement
);
9693 /* Parse a compound-statement.
9696 { statement-seq [opt] }
9701 { label-declaration-seq [opt] statement-seq [opt] }
9703 label-declaration-seq:
9705 label-declaration-seq label-declaration
9707 Returns a tree representing the statement. */
9710 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
9711 bool in_try
, bool function_body
)
9715 /* Consume the `{'. */
9716 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9717 return error_mark_node
;
9718 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
9720 pedwarn (input_location
, OPT_Wpedantic
,
9721 "compound-statement in constexpr function");
9722 /* Begin the compound-statement. */
9723 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
9724 /* If the next keyword is `__label__' we have a label declaration. */
9725 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9726 cp_parser_label_declaration (parser
);
9727 /* Parse an (optional) statement-seq. */
9728 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
9729 /* Finish the compound-statement. */
9730 finish_compound_stmt (compound_stmt
);
9731 /* Consume the `}'. */
9732 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9734 return compound_stmt
;
9737 /* Parse an (optional) statement-seq.
9741 statement-seq [opt] statement */
9744 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
9746 /* Scan statements until there aren't any more. */
9749 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9751 /* If we are looking at a `}', then we have run out of
9752 statements; the same is true if we have reached the end
9753 of file, or have stumbled upon a stray '@end'. */
9754 if (token
->type
== CPP_CLOSE_BRACE
9755 || token
->type
== CPP_EOF
9756 || token
->type
== CPP_PRAGMA_EOL
9757 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
9760 /* If we are in a compound statement and find 'else' then
9761 something went wrong. */
9762 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
9764 if (parser
->in_statement
& IN_IF_STMT
)
9768 token
= cp_lexer_consume_token (parser
->lexer
);
9769 error_at (token
->location
, "%<else%> without a previous %<if%>");
9773 /* Parse the statement. */
9774 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
9778 /* Parse a selection-statement.
9780 selection-statement:
9781 if ( condition ) statement
9782 if ( condition ) statement else statement
9783 switch ( condition ) statement
9785 Returns the new IF_STMT or SWITCH_STMT.
9787 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9788 is a (possibly labeled) if statement which is not enclosed in
9789 braces and has an else clause. This is used to implement
9793 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
9801 /* Peek at the next token. */
9802 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
9804 /* See what kind of keyword it is. */
9805 keyword
= token
->keyword
;
9814 /* Look for the `('. */
9815 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
9817 cp_parser_skip_to_end_of_statement (parser
);
9818 return error_mark_node
;
9821 /* Begin the selection-statement. */
9822 if (keyword
== RID_IF
)
9823 statement
= begin_if_stmt ();
9825 statement
= begin_switch_stmt ();
9827 /* Parse the condition. */
9828 condition
= cp_parser_condition (parser
);
9829 /* Look for the `)'. */
9830 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
9831 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9832 /*consume_paren=*/true);
9834 if (keyword
== RID_IF
)
9837 unsigned char in_statement
;
9839 /* Add the condition. */
9840 finish_if_stmt_cond (condition
, statement
);
9842 /* Parse the then-clause. */
9843 in_statement
= parser
->in_statement
;
9844 parser
->in_statement
|= IN_IF_STMT
;
9845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9847 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9848 add_stmt (build_empty_stmt (loc
));
9849 cp_lexer_consume_token (parser
->lexer
);
9850 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
9851 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
9852 "empty body in an %<if%> statement");
9856 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
9857 parser
->in_statement
= in_statement
;
9859 finish_then_clause (statement
);
9861 /* If the next token is `else', parse the else-clause. */
9862 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9865 /* Consume the `else' keyword. */
9866 cp_lexer_consume_token (parser
->lexer
);
9867 begin_else_clause (statement
);
9868 /* Parse the else-clause. */
9869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9872 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9874 OPT_Wempty_body
, "suggest braces around "
9875 "empty body in an %<else%> statement");
9876 add_stmt (build_empty_stmt (loc
));
9877 cp_lexer_consume_token (parser
->lexer
);
9880 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9882 finish_else_clause (statement
);
9884 /* If we are currently parsing a then-clause, then
9885 IF_P will not be NULL. We set it to true to
9886 indicate that this if statement has an else clause.
9887 This may trigger the Wparentheses warning below
9888 when we get back up to the parent if statement. */
9894 /* This if statement does not have an else clause. If
9895 NESTED_IF is true, then the then-clause is an if
9896 statement which does have an else clause. We warn
9897 about the potential ambiguity. */
9899 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
9900 "suggest explicit braces to avoid ambiguous"
9904 /* Now we're all done with the if-statement. */
9905 finish_if_stmt (statement
);
9909 bool in_switch_statement_p
;
9910 unsigned char in_statement
;
9912 /* Add the condition. */
9913 finish_switch_cond (condition
, statement
);
9915 /* Parse the body of the switch-statement. */
9916 in_switch_statement_p
= parser
->in_switch_statement_p
;
9917 in_statement
= parser
->in_statement
;
9918 parser
->in_switch_statement_p
= true;
9919 parser
->in_statement
|= IN_SWITCH_STMT
;
9920 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9921 parser
->in_switch_statement_p
= in_switch_statement_p
;
9922 parser
->in_statement
= in_statement
;
9924 /* Now we're all done with the switch-statement. */
9925 finish_switch_stmt (statement
);
9933 cp_parser_error (parser
, "expected selection-statement");
9934 return error_mark_node
;
9938 /* Parse a condition.
9942 type-specifier-seq declarator = initializer-clause
9943 type-specifier-seq declarator braced-init-list
9948 type-specifier-seq declarator asm-specification [opt]
9949 attributes [opt] = assignment-expression
9951 Returns the expression that should be tested. */
9954 cp_parser_condition (cp_parser
* parser
)
9956 cp_decl_specifier_seq type_specifiers
;
9957 const char *saved_message
;
9958 int declares_class_or_enum
;
9960 /* Try the declaration first. */
9961 cp_parser_parse_tentatively (parser
);
9962 /* New types are not allowed in the type-specifier-seq for a
9964 saved_message
= parser
->type_definition_forbidden_message
;
9965 parser
->type_definition_forbidden_message
9966 = G_("types may not be defined in conditions");
9967 /* Parse the type-specifier-seq. */
9968 cp_parser_decl_specifier_seq (parser
,
9969 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
9971 &declares_class_or_enum
);
9972 /* Restore the saved message. */
9973 parser
->type_definition_forbidden_message
= saved_message
;
9974 /* If all is well, we might be looking at a declaration. */
9975 if (!cp_parser_error_occurred (parser
))
9978 tree asm_specification
;
9980 cp_declarator
*declarator
;
9981 tree initializer
= NULL_TREE
;
9983 /* Parse the declarator. */
9984 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9985 /*ctor_dtor_or_conv_p=*/NULL
,
9986 /*parenthesized_p=*/NULL
,
9987 /*member_p=*/false);
9988 /* Parse the attributes. */
9989 attributes
= cp_parser_attributes_opt (parser
);
9990 /* Parse the asm-specification. */
9991 asm_specification
= cp_parser_asm_specification_opt (parser
);
9992 /* If the next token is not an `=' or '{', then we might still be
9993 looking at an expression. For example:
9997 looks like a decl-specifier-seq and a declarator -- but then
9998 there is no `=', so this is an expression. */
9999 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10000 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10001 cp_parser_simulate_error (parser
);
10003 /* If we did see an `=' or '{', then we are looking at a declaration
10005 if (cp_parser_parse_definitely (parser
))
10008 bool non_constant_p
;
10009 bool flags
= LOOKUP_ONLYCONVERTING
;
10011 /* Create the declaration. */
10012 decl
= start_decl (declarator
, &type_specifiers
,
10013 /*initialized_p=*/true,
10014 attributes
, /*prefix_attributes=*/NULL_TREE
,
10017 /* Parse the initializer. */
10018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10020 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10021 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10026 /* Consume the `='. */
10027 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10028 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10030 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10031 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10033 /* Process the initializer. */
10034 cp_finish_decl (decl
,
10035 initializer
, !non_constant_p
,
10040 pop_scope (pushed_scope
);
10042 return convert_from_reference (decl
);
10045 /* If we didn't even get past the declarator successfully, we are
10046 definitely not looking at a declaration. */
10048 cp_parser_abort_tentative_parse (parser
);
10050 /* Otherwise, we are looking at an expression. */
10051 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10054 /* Parses a for-statement or range-for-statement until the closing ')',
10058 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10060 tree init
, scope
, decl
;
10063 /* Begin the for-statement. */
10064 scope
= begin_for_scope (&init
);
10066 /* Parse the initialization. */
10067 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10070 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10072 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10076 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10078 /* Normal for loop */
10079 tree condition
= NULL_TREE
;
10080 tree expression
= NULL_TREE
;
10083 stmt
= begin_for_stmt (scope
, init
);
10084 /* The for-init-statement has already been parsed in
10085 cp_parser_for_init_statement, so no work is needed here. */
10086 finish_for_init_stmt (stmt
);
10088 /* If there's a condition, process it. */
10089 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10090 condition
= cp_parser_condition (parser
);
10093 cp_parser_error (parser
, "missing loop condition in loop with "
10094 "%<GCC ivdep%> pragma");
10095 condition
= error_mark_node
;
10097 finish_for_cond (condition
, stmt
, ivdep
);
10098 /* Look for the `;'. */
10099 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10101 /* If there's an expression, process it. */
10102 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10103 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10104 finish_for_expr (expression
, stmt
);
10109 /* Tries to parse a range-based for-statement:
10112 decl-specifier-seq declarator : expression
10114 The decl-specifier-seq declarator and the `:' are already parsed by
10115 cp_parser_for_init_statement. If processing_template_decl it returns a
10116 newly created RANGE_FOR_STMT; if not, it is converted to a
10117 regular FOR_STMT. */
10120 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10123 tree stmt
, range_expr
;
10125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10127 bool expr_non_constant_p
;
10128 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10131 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10133 /* If in template, STMT is converted to a normal for-statement
10134 at instantiation. If not, it is done just ahead. */
10135 if (processing_template_decl
)
10137 if (check_for_bare_parameter_packs (range_expr
))
10138 range_expr
= error_mark_node
;
10139 stmt
= begin_range_for_stmt (scope
, init
);
10141 RANGE_FOR_IVDEP (stmt
) = 1;
10142 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10143 if (!type_dependent_expression_p (range_expr
)
10144 /* do_auto_deduction doesn't mess with template init-lists. */
10145 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10146 do_range_for_auto_deduction (range_decl
, range_expr
);
10150 stmt
= begin_for_stmt (scope
, init
);
10151 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10156 /* Subroutine of cp_convert_range_for: given the initializer expression,
10157 builds up the range temporary. */
10160 build_range_temp (tree range_expr
)
10162 tree range_type
, range_temp
;
10164 /* Find out the type deduced by the declaration
10165 `auto &&__range = range_expr'. */
10166 range_type
= cp_build_reference_type (make_auto (), true);
10167 range_type
= do_auto_deduction (range_type
, range_expr
,
10168 type_uses_auto (range_type
));
10170 /* Create the __range variable. */
10171 range_temp
= build_decl (input_location
, VAR_DECL
,
10172 get_identifier ("__for_range"), range_type
);
10173 TREE_USED (range_temp
) = 1;
10174 DECL_ARTIFICIAL (range_temp
) = 1;
10179 /* Used by cp_parser_range_for in template context: we aren't going to
10180 do a full conversion yet, but we still need to resolve auto in the
10181 type of the for-range-declaration if present. This is basically
10182 a shortcut version of cp_convert_range_for. */
10185 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10187 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10190 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10191 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10192 iter_type
= (cp_parser_perform_range_for_lookup
10193 (range_temp
, &begin_dummy
, &end_dummy
));
10196 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10198 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10199 tf_warning_or_error
);
10200 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10201 iter_decl
, auto_node
);
10206 /* Converts a range-based for-statement into a normal
10207 for-statement, as per the definition.
10209 for (RANGE_DECL : RANGE_EXPR)
10212 should be equivalent to:
10215 auto &&__range = RANGE_EXPR;
10216 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10220 RANGE_DECL = *__begin;
10225 If RANGE_EXPR is an array:
10226 BEGIN_EXPR = __range
10227 END_EXPR = __range + ARRAY_SIZE(__range)
10228 Else if RANGE_EXPR has a member 'begin' or 'end':
10229 BEGIN_EXPR = __range.begin()
10230 END_EXPR = __range.end()
10232 BEGIN_EXPR = begin(__range)
10233 END_EXPR = end(__range);
10235 If __range has a member 'begin' but not 'end', or vice versa, we must
10236 still use the second alternative (it will surely fail, however).
10237 When calling begin()/end() in the third alternative we must use
10238 argument dependent lookup, but always considering 'std' as an associated
10242 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10246 tree iter_type
, begin_expr
, end_expr
;
10247 tree condition
, expression
;
10249 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10250 /* If an error happened previously do nothing or else a lot of
10251 unhelpful errors would be issued. */
10252 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10257 if (TREE_CODE (range_expr
) == VAR_DECL
10258 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10259 /* Can't bind a reference to an array of runtime bound. */
10260 range_temp
= range_expr
;
10263 range_temp
= build_range_temp (range_expr
);
10264 pushdecl (range_temp
);
10265 cp_finish_decl (range_temp
, range_expr
,
10266 /*is_constant_init*/false, NULL_TREE
,
10267 LOOKUP_ONLYCONVERTING
);
10268 range_temp
= convert_from_reference (range_temp
);
10270 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10271 &begin_expr
, &end_expr
);
10274 /* The new for initialization statement. */
10275 begin
= build_decl (input_location
, VAR_DECL
,
10276 get_identifier ("__for_begin"), iter_type
);
10277 TREE_USED (begin
) = 1;
10278 DECL_ARTIFICIAL (begin
) = 1;
10280 cp_finish_decl (begin
, begin_expr
,
10281 /*is_constant_init*/false, NULL_TREE
,
10282 LOOKUP_ONLYCONVERTING
);
10284 end
= build_decl (input_location
, VAR_DECL
,
10285 get_identifier ("__for_end"), iter_type
);
10286 TREE_USED (end
) = 1;
10287 DECL_ARTIFICIAL (end
) = 1;
10289 cp_finish_decl (end
, end_expr
,
10290 /*is_constant_init*/false, NULL_TREE
,
10291 LOOKUP_ONLYCONVERTING
);
10293 finish_for_init_stmt (statement
);
10295 /* The new for condition. */
10296 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10299 NULL
, tf_warning_or_error
);
10300 finish_for_cond (condition
, statement
, ivdep
);
10302 /* The new increment expression. */
10303 expression
= finish_unary_op_expr (input_location
,
10304 PREINCREMENT_EXPR
, begin
,
10305 tf_warning_or_error
);
10306 finish_for_expr (expression
, statement
);
10308 /* The declaration is initialized with *__begin inside the loop body. */
10309 cp_finish_decl (range_decl
,
10310 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10311 tf_warning_or_error
),
10312 /*is_constant_init*/false, NULL_TREE
,
10313 LOOKUP_ONLYCONVERTING
);
10318 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10319 We need to solve both at the same time because the method used
10320 depends on the existence of members begin or end.
10321 Returns the type deduced for the iterator expression. */
10324 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10326 if (error_operand_p (range
))
10328 *begin
= *end
= error_mark_node
;
10329 return error_mark_node
;
10332 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10334 error ("range-based %<for%> expression of type %qT "
10335 "has incomplete type", TREE_TYPE (range
));
10336 *begin
= *end
= error_mark_node
;
10337 return error_mark_node
;
10339 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10341 /* If RANGE is an array, we will use pointer arithmetic. */
10343 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10345 array_type_nelts_top (TREE_TYPE (range
)),
10347 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10351 /* If it is not an array, we must do a bit of magic. */
10352 tree id_begin
, id_end
;
10353 tree member_begin
, member_end
;
10355 *begin
= *end
= error_mark_node
;
10357 id_begin
= get_identifier ("begin");
10358 id_end
= get_identifier ("end");
10359 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10360 /*protect=*/2, /*want_type=*/false,
10361 tf_warning_or_error
);
10362 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10363 /*protect=*/2, /*want_type=*/false,
10364 tf_warning_or_error
);
10366 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10368 /* Use the member functions. */
10369 if (member_begin
!= NULL_TREE
)
10370 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10372 error ("range-based %<for%> expression of type %qT has an "
10373 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10375 if (member_end
!= NULL_TREE
)
10376 *end
= cp_parser_range_for_member_function (range
, id_end
);
10378 error ("range-based %<for%> expression of type %qT has a "
10379 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10383 /* Use global functions with ADL. */
10384 vec
<tree
, va_gc
> *vec
;
10385 vec
= make_tree_vector ();
10387 vec_safe_push (vec
, range
);
10389 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10390 tf_warning_or_error
);
10391 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10392 tf_warning_or_error
);
10393 member_end
= perform_koenig_lookup (id_end
, vec
,
10394 tf_warning_or_error
);
10395 *end
= finish_call_expr (member_end
, &vec
, false, true,
10396 tf_warning_or_error
);
10398 release_tree_vector (vec
);
10401 /* Last common checks. */
10402 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10404 /* If one of the expressions is an error do no more checks. */
10405 *begin
= *end
= error_mark_node
;
10406 return error_mark_node
;
10408 else if (type_dependent_expression_p (*begin
)
10409 || type_dependent_expression_p (*end
))
10410 /* Can happen, when, eg, in a template context, Koenig lookup
10411 can't resolve begin/end (c++/58503). */
10415 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10416 /* The unqualified type of the __begin and __end temporaries should
10417 be the same, as required by the multiple auto declaration. */
10418 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10419 error ("inconsistent begin/end types in range-based %<for%> "
10420 "statement: %qT and %qT",
10421 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10427 /* Helper function for cp_parser_perform_range_for_lookup.
10428 Builds a tree for RANGE.IDENTIFIER(). */
10431 cp_parser_range_for_member_function (tree range
, tree identifier
)
10434 vec
<tree
, va_gc
> *vec
;
10436 member
= finish_class_member_access_expr (range
, identifier
,
10437 false, tf_warning_or_error
);
10438 if (member
== error_mark_node
)
10439 return error_mark_node
;
10441 vec
= make_tree_vector ();
10442 res
= finish_call_expr (member
, &vec
,
10443 /*disallow_virtual=*/false,
10444 /*koenig_p=*/false,
10445 tf_warning_or_error
);
10446 release_tree_vector (vec
);
10450 /* Parse an iteration-statement.
10452 iteration-statement:
10453 while ( condition ) statement
10454 do statement while ( expression ) ;
10455 for ( for-init-statement condition [opt] ; expression [opt] )
10458 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10461 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10466 unsigned char in_statement
;
10468 /* Peek at the next token. */
10469 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10471 return error_mark_node
;
10473 /* Remember whether or not we are already within an iteration
10475 in_statement
= parser
->in_statement
;
10477 /* See what kind of keyword it is. */
10478 keyword
= token
->keyword
;
10485 /* Begin the while-statement. */
10486 statement
= begin_while_stmt ();
10487 /* Look for the `('. */
10488 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10489 /* Parse the condition. */
10490 condition
= cp_parser_condition (parser
);
10491 finish_while_stmt_cond (condition
, statement
, ivdep
);
10492 /* Look for the `)'. */
10493 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10494 /* Parse the dependent statement. */
10495 parser
->in_statement
= IN_ITERATION_STMT
;
10496 cp_parser_already_scoped_statement (parser
);
10497 parser
->in_statement
= in_statement
;
10498 /* We're done with the while-statement. */
10499 finish_while_stmt (statement
);
10507 /* Begin the do-statement. */
10508 statement
= begin_do_stmt ();
10509 /* Parse the body of the do-statement. */
10510 parser
->in_statement
= IN_ITERATION_STMT
;
10511 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10512 parser
->in_statement
= in_statement
;
10513 finish_do_body (statement
);
10514 /* Look for the `while' keyword. */
10515 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10516 /* Look for the `('. */
10517 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10518 /* Parse the expression. */
10519 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10520 /* We're done with the do-statement. */
10521 finish_do_stmt (expression
, statement
, ivdep
);
10522 /* Look for the `)'. */
10523 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10524 /* Look for the `;'. */
10525 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10531 /* Look for the `('. */
10532 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10534 statement
= cp_parser_for (parser
, ivdep
);
10536 /* Look for the `)'. */
10537 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10539 /* Parse the body of the for-statement. */
10540 parser
->in_statement
= IN_ITERATION_STMT
;
10541 cp_parser_already_scoped_statement (parser
);
10542 parser
->in_statement
= in_statement
;
10544 /* We're done with the for-statement. */
10545 finish_for_stmt (statement
);
10550 cp_parser_error (parser
, "expected iteration-statement");
10551 statement
= error_mark_node
;
10558 /* Parse a for-init-statement or the declarator of a range-based-for.
10559 Returns true if a range-based-for declaration is seen.
10561 for-init-statement:
10562 expression-statement
10563 simple-declaration */
10566 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10568 /* If the next token is a `;', then we have an empty
10569 expression-statement. Grammatically, this is also a
10570 simple-declaration, but an invalid one, because it does not
10571 declare anything. Therefore, if we did not handle this case
10572 specially, we would issue an error message about an invalid
10574 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10576 bool is_range_for
= false;
10577 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10579 parser
->colon_corrects_to_scope_p
= false;
10581 /* We're going to speculatively look for a declaration, falling back
10582 to an expression, if necessary. */
10583 cp_parser_parse_tentatively (parser
);
10584 /* Parse the declaration. */
10585 cp_parser_simple_declaration (parser
,
10586 /*function_definition_allowed_p=*/false,
10588 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10591 /* It is a range-for, consume the ':' */
10592 cp_lexer_consume_token (parser
->lexer
);
10593 is_range_for
= true;
10594 if (cxx_dialect
< cxx11
)
10596 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10597 "range-based %<for%> loops are not allowed "
10599 *decl
= error_mark_node
;
10603 /* The ';' is not consumed yet because we told
10604 cp_parser_simple_declaration not to. */
10605 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10607 if (cp_parser_parse_definitely (parser
))
10608 return is_range_for
;
10609 /* If the tentative parse failed, then we shall need to look for an
10610 expression-statement. */
10612 /* If we are here, it is an expression-statement. */
10613 cp_parser_expression_statement (parser
, NULL_TREE
);
10617 /* Parse a jump-statement.
10622 return expression [opt] ;
10623 return braced-init-list ;
10629 goto * expression ;
10631 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10634 cp_parser_jump_statement (cp_parser
* parser
)
10636 tree statement
= error_mark_node
;
10639 unsigned char in_statement
;
10641 /* Peek at the next token. */
10642 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10644 return error_mark_node
;
10646 /* See what kind of keyword it is. */
10647 keyword
= token
->keyword
;
10651 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
10652 switch (in_statement
)
10655 error_at (token
->location
, "break statement not within loop or switch");
10658 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
10659 || in_statement
== IN_ITERATION_STMT
);
10660 statement
= finish_break_stmt ();
10661 if (in_statement
== IN_ITERATION_STMT
)
10662 break_maybe_infinite_loop ();
10665 error_at (token
->location
, "invalid exit from OpenMP structured block");
10668 error_at (token
->location
, "break statement used with OpenMP for loop");
10670 case IN_CILK_SIMD_FOR
:
10671 error_at (token
->location
, "break statement used with Cilk Plus for loop");
10674 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10678 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10681 error_at (token
->location
, "continue statement not within a loop");
10683 case IN_CILK_SIMD_FOR
:
10684 error_at (token
->location
,
10685 "continue statement within %<#pragma simd%> loop body");
10686 /* Fall through. */
10687 case IN_ITERATION_STMT
:
10689 statement
= finish_continue_stmt ();
10692 error_at (token
->location
, "invalid exit from OpenMP structured block");
10695 gcc_unreachable ();
10697 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10703 bool expr_non_constant_p
;
10705 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10707 cp_lexer_set_source_position (parser
->lexer
);
10708 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10709 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10711 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10712 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10714 /* If the next token is a `;', then there is no
10717 /* Build the return-statement. */
10718 statement
= finish_return_stmt (expr
);
10719 /* Look for the final `;'. */
10720 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10725 /* Create the goto-statement. */
10726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
10728 /* Issue a warning about this use of a GNU extension. */
10729 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
10730 /* Consume the '*' token. */
10731 cp_lexer_consume_token (parser
->lexer
);
10732 /* Parse the dependent expression. */
10733 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
10736 finish_goto_stmt (cp_parser_identifier (parser
));
10737 /* Look for the final `;'. */
10738 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10742 cp_parser_error (parser
, "expected jump-statement");
10749 /* Parse a declaration-statement.
10751 declaration-statement:
10752 block-declaration */
10755 cp_parser_declaration_statement (cp_parser
* parser
)
10759 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10760 p
= obstack_alloc (&declarator_obstack
, 0);
10762 /* Parse the block-declaration. */
10763 cp_parser_block_declaration (parser
, /*statement_p=*/true);
10765 /* Free any declarators allocated. */
10766 obstack_free (&declarator_obstack
, p
);
10769 /* Some dependent statements (like `if (cond) statement'), are
10770 implicitly in their own scope. In other words, if the statement is
10771 a single statement (as opposed to a compound-statement), it is
10772 none-the-less treated as if it were enclosed in braces. Any
10773 declarations appearing in the dependent statement are out of scope
10774 after control passes that point. This function parses a statement,
10775 but ensures that is in its own scope, even if it is not a
10776 compound-statement.
10778 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10779 is a (possibly labeled) if statement which is not enclosed in
10780 braces and has an else clause. This is used to implement
10783 Returns the new statement. */
10786 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
10793 /* Mark if () ; with a special NOP_EXPR. */
10794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10796 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10797 cp_lexer_consume_token (parser
->lexer
);
10798 statement
= add_stmt (build_empty_stmt (loc
));
10800 /* if a compound is opened, we simply parse the statement directly. */
10801 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10802 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
10803 /* If the token is not a `{', then we must take special action. */
10806 /* Create a compound-statement. */
10807 statement
= begin_compound_stmt (0);
10808 /* Parse the dependent-statement. */
10809 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
10810 /* Finish the dummy compound-statement. */
10811 finish_compound_stmt (statement
);
10814 /* Return the statement. */
10818 /* For some dependent statements (like `while (cond) statement'), we
10819 have already created a scope. Therefore, even if the dependent
10820 statement is a compound-statement, we do not want to create another
10824 cp_parser_already_scoped_statement (cp_parser
* parser
)
10826 /* If the token is a `{', then we must take special action. */
10827 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10828 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
10831 /* Avoid calling cp_parser_compound_statement, so that we
10832 don't create a new scope. Do everything else by hand. */
10833 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
10834 /* If the next keyword is `__label__' we have a label declaration. */
10835 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10836 cp_parser_label_declaration (parser
);
10837 /* Parse an (optional) statement-seq. */
10838 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10839 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10843 /* Declarations [gram.dcl.dcl] */
10845 /* Parse an optional declaration-sequence.
10849 declaration-seq declaration */
10852 cp_parser_declaration_seq_opt (cp_parser
* parser
)
10858 token
= cp_lexer_peek_token (parser
->lexer
);
10860 if (token
->type
== CPP_CLOSE_BRACE
10861 || token
->type
== CPP_EOF
10862 || token
->type
== CPP_PRAGMA_EOL
)
10865 if (token
->type
== CPP_SEMICOLON
)
10867 /* A declaration consisting of a single semicolon is
10868 invalid. Allow it unless we're being pedantic. */
10869 cp_lexer_consume_token (parser
->lexer
);
10870 if (!in_system_header_at (input_location
))
10871 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
10875 /* If we're entering or exiting a region that's implicitly
10876 extern "C", modify the lang context appropriately. */
10877 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
10879 push_lang_context (lang_name_c
);
10880 parser
->implicit_extern_c
= true;
10882 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
10884 pop_lang_context ();
10885 parser
->implicit_extern_c
= false;
10888 if (token
->type
== CPP_PRAGMA
)
10890 /* A top-level declaration can consist solely of a #pragma.
10891 A nested declaration cannot, so this is done here and not
10892 in cp_parser_declaration. (A #pragma at block scope is
10893 handled in cp_parser_statement.) */
10894 cp_parser_pragma (parser
, pragma_external
);
10898 /* Parse the declaration itself. */
10899 cp_parser_declaration (parser
);
10903 /* Parse a declaration.
10907 function-definition
10908 template-declaration
10909 explicit-instantiation
10910 explicit-specialization
10911 linkage-specification
10912 namespace-definition
10917 __extension__ declaration */
10920 cp_parser_declaration (cp_parser
* parser
)
10924 int saved_pedantic
;
10926 tree attributes
= NULL_TREE
;
10928 /* Check for the `__extension__' keyword. */
10929 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10931 /* Parse the qualified declaration. */
10932 cp_parser_declaration (parser
);
10933 /* Restore the PEDANTIC flag. */
10934 pedantic
= saved_pedantic
;
10939 /* Try to figure out what kind of declaration is present. */
10940 token1
= *cp_lexer_peek_token (parser
->lexer
);
10942 if (token1
.type
!= CPP_EOF
)
10943 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
10946 token2
.type
= CPP_EOF
;
10947 token2
.keyword
= RID_MAX
;
10950 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10951 p
= obstack_alloc (&declarator_obstack
, 0);
10953 /* If the next token is `extern' and the following token is a string
10954 literal, then we have a linkage specification. */
10955 if (token1
.keyword
== RID_EXTERN
10956 && cp_parser_is_pure_string_literal (&token2
))
10957 cp_parser_linkage_specification (parser
);
10958 /* If the next token is `template', then we have either a template
10959 declaration, an explicit instantiation, or an explicit
10961 else if (token1
.keyword
== RID_TEMPLATE
)
10963 /* `template <>' indicates a template specialization. */
10964 if (token2
.type
== CPP_LESS
10965 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
10966 cp_parser_explicit_specialization (parser
);
10967 /* `template <' indicates a template declaration. */
10968 else if (token2
.type
== CPP_LESS
)
10969 cp_parser_template_declaration (parser
, /*member_p=*/false);
10970 /* Anything else must be an explicit instantiation. */
10972 cp_parser_explicit_instantiation (parser
);
10974 /* If the next token is `export', then we have a template
10976 else if (token1
.keyword
== RID_EXPORT
)
10977 cp_parser_template_declaration (parser
, /*member_p=*/false);
10978 /* If the next token is `extern', 'static' or 'inline' and the one
10979 after that is `template', we have a GNU extended explicit
10980 instantiation directive. */
10981 else if (cp_parser_allow_gnu_extensions_p (parser
)
10982 && (token1
.keyword
== RID_EXTERN
10983 || token1
.keyword
== RID_STATIC
10984 || token1
.keyword
== RID_INLINE
)
10985 && token2
.keyword
== RID_TEMPLATE
)
10986 cp_parser_explicit_instantiation (parser
);
10987 /* If the next token is `namespace', check for a named or unnamed
10988 namespace definition. */
10989 else if (token1
.keyword
== RID_NAMESPACE
10990 && (/* A named namespace definition. */
10991 (token2
.type
== CPP_NAME
10992 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
10994 /* An unnamed namespace definition. */
10995 || token2
.type
== CPP_OPEN_BRACE
10996 || token2
.keyword
== RID_ATTRIBUTE
))
10997 cp_parser_namespace_definition (parser
);
10998 /* An inline (associated) namespace definition. */
10999 else if (token1
.keyword
== RID_INLINE
11000 && token2
.keyword
== RID_NAMESPACE
)
11001 cp_parser_namespace_definition (parser
);
11002 /* Objective-C++ declaration/definition. */
11003 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11004 cp_parser_objc_declaration (parser
, NULL_TREE
);
11005 else if (c_dialect_objc ()
11006 && token1
.keyword
== RID_ATTRIBUTE
11007 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11008 cp_parser_objc_declaration (parser
, attributes
);
11009 /* We must have either a block declaration or a function
11012 /* Try to parse a block-declaration, or a function-definition. */
11013 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11015 /* Free any declarators allocated. */
11016 obstack_free (&declarator_obstack
, p
);
11019 /* Parse a block-declaration.
11024 namespace-alias-definition
11031 __extension__ block-declaration
11036 static_assert-declaration
11038 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11039 part of a declaration-statement. */
11042 cp_parser_block_declaration (cp_parser
*parser
,
11046 int saved_pedantic
;
11048 /* Check for the `__extension__' keyword. */
11049 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11051 /* Parse the qualified declaration. */
11052 cp_parser_block_declaration (parser
, statement_p
);
11053 /* Restore the PEDANTIC flag. */
11054 pedantic
= saved_pedantic
;
11059 /* Peek at the next token to figure out which kind of declaration is
11061 token1
= cp_lexer_peek_token (parser
->lexer
);
11063 /* If the next keyword is `asm', we have an asm-definition. */
11064 if (token1
->keyword
== RID_ASM
)
11067 cp_parser_commit_to_tentative_parse (parser
);
11068 cp_parser_asm_definition (parser
);
11070 /* If the next keyword is `namespace', we have a
11071 namespace-alias-definition. */
11072 else if (token1
->keyword
== RID_NAMESPACE
)
11073 cp_parser_namespace_alias_definition (parser
);
11074 /* If the next keyword is `using', we have a
11075 using-declaration, a using-directive, or an alias-declaration. */
11076 else if (token1
->keyword
== RID_USING
)
11081 cp_parser_commit_to_tentative_parse (parser
);
11082 /* If the token after `using' is `namespace', then we have a
11083 using-directive. */
11084 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11085 if (token2
->keyword
== RID_NAMESPACE
)
11086 cp_parser_using_directive (parser
);
11087 /* If the second token after 'using' is '=', then we have an
11088 alias-declaration. */
11089 else if (cxx_dialect
>= cxx11
11090 && token2
->type
== CPP_NAME
11091 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11092 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11093 cp_parser_alias_declaration (parser
);
11094 /* Otherwise, it's a using-declaration. */
11096 cp_parser_using_declaration (parser
,
11097 /*access_declaration_p=*/false);
11099 /* If the next keyword is `__label__' we have a misplaced label
11101 else if (token1
->keyword
== RID_LABEL
)
11103 cp_lexer_consume_token (parser
->lexer
);
11104 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11105 cp_parser_skip_to_end_of_statement (parser
);
11106 /* If the next token is now a `;', consume it. */
11107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11108 cp_lexer_consume_token (parser
->lexer
);
11110 /* If the next token is `static_assert' we have a static assertion. */
11111 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11112 cp_parser_static_assert (parser
, /*member_p=*/false);
11113 /* Anything else must be a simple-declaration. */
11115 cp_parser_simple_declaration (parser
, !statement_p
,
11116 /*maybe_range_for_decl*/NULL
);
11119 /* Parse a simple-declaration.
11121 simple-declaration:
11122 decl-specifier-seq [opt] init-declarator-list [opt] ;
11124 init-declarator-list:
11126 init-declarator-list , init-declarator
11128 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11129 function-definition as a simple-declaration.
11131 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11132 parsed declaration if it is an uninitialized single declarator not followed
11133 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11134 if present, will not be consumed. */
11137 cp_parser_simple_declaration (cp_parser
* parser
,
11138 bool function_definition_allowed_p
,
11139 tree
*maybe_range_for_decl
)
11141 cp_decl_specifier_seq decl_specifiers
;
11142 int declares_class_or_enum
;
11143 bool saw_declarator
;
11145 if (maybe_range_for_decl
)
11146 *maybe_range_for_decl
= NULL_TREE
;
11148 /* Defer access checks until we know what is being declared; the
11149 checks for names appearing in the decl-specifier-seq should be
11150 done as if we were in the scope of the thing being declared. */
11151 push_deferring_access_checks (dk_deferred
);
11153 /* Parse the decl-specifier-seq. We have to keep track of whether
11154 or not the decl-specifier-seq declares a named class or
11155 enumeration type, since that is the only case in which the
11156 init-declarator-list is allowed to be empty.
11160 In a simple-declaration, the optional init-declarator-list can be
11161 omitted only when declaring a class or enumeration, that is when
11162 the decl-specifier-seq contains either a class-specifier, an
11163 elaborated-type-specifier, or an enum-specifier. */
11164 cp_parser_decl_specifier_seq (parser
,
11165 CP_PARSER_FLAGS_OPTIONAL
,
11167 &declares_class_or_enum
);
11168 /* We no longer need to defer access checks. */
11169 stop_deferring_access_checks ();
11171 /* In a block scope, a valid declaration must always have a
11172 decl-specifier-seq. By not trying to parse declarators, we can
11173 resolve the declaration/expression ambiguity more quickly. */
11174 if (!function_definition_allowed_p
11175 && !decl_specifiers
.any_specifiers_p
)
11177 cp_parser_error (parser
, "expected declaration");
11181 /* If the next two tokens are both identifiers, the code is
11182 erroneous. The usual cause of this situation is code like:
11186 where "T" should name a type -- but does not. */
11187 if (!decl_specifiers
.any_type_specifiers_p
11188 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11190 /* If parsing tentatively, we should commit; we really are
11191 looking at a declaration. */
11192 cp_parser_commit_to_tentative_parse (parser
);
11197 /* If we have seen at least one decl-specifier, and the next token
11198 is not a parenthesis, then we must be looking at a declaration.
11199 (After "int (" we might be looking at a functional cast.) */
11200 if (decl_specifiers
.any_specifiers_p
11201 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11202 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11203 && !cp_parser_error_occurred (parser
))
11204 cp_parser_commit_to_tentative_parse (parser
);
11206 /* Keep going until we hit the `;' at the end of the simple
11208 saw_declarator
= false;
11209 while (cp_lexer_next_token_is_not (parser
->lexer
,
11213 bool function_definition_p
;
11216 if (saw_declarator
)
11218 /* If we are processing next declarator, coma is expected */
11219 token
= cp_lexer_peek_token (parser
->lexer
);
11220 gcc_assert (token
->type
== CPP_COMMA
);
11221 cp_lexer_consume_token (parser
->lexer
);
11222 if (maybe_range_for_decl
)
11223 *maybe_range_for_decl
= error_mark_node
;
11226 saw_declarator
= true;
11228 /* Parse the init-declarator. */
11229 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11231 function_definition_allowed_p
,
11232 /*member_p=*/false,
11233 declares_class_or_enum
,
11234 &function_definition_p
,
11235 maybe_range_for_decl
);
11236 /* If an error occurred while parsing tentatively, exit quickly.
11237 (That usually happens when in the body of a function; each
11238 statement is treated as a declaration-statement until proven
11240 if (cp_parser_error_occurred (parser
))
11242 /* Handle function definitions specially. */
11243 if (function_definition_p
)
11245 /* If the next token is a `,', then we are probably
11246 processing something like:
11250 which is erroneous. */
11251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11253 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11254 error_at (token
->location
,
11256 " declarations and function-definitions is forbidden");
11258 /* Otherwise, we're done with the list of declarators. */
11261 pop_deferring_access_checks ();
11265 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11266 *maybe_range_for_decl
= decl
;
11267 /* The next token should be either a `,' or a `;'. */
11268 token
= cp_lexer_peek_token (parser
->lexer
);
11269 /* If it's a `,', there are more declarators to come. */
11270 if (token
->type
== CPP_COMMA
)
11271 /* will be consumed next time around */;
11272 /* If it's a `;', we are done. */
11273 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11275 /* Anything else is an error. */
11278 /* If we have already issued an error message we don't need
11279 to issue another one. */
11280 if (decl
!= error_mark_node
11281 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11282 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11283 /* Skip tokens until we reach the end of the statement. */
11284 cp_parser_skip_to_end_of_statement (parser
);
11285 /* If the next token is now a `;', consume it. */
11286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11287 cp_lexer_consume_token (parser
->lexer
);
11290 /* After the first time around, a function-definition is not
11291 allowed -- even if it was OK at first. For example:
11296 function_definition_allowed_p
= false;
11299 /* Issue an error message if no declarators are present, and the
11300 decl-specifier-seq does not itself declare a class or
11301 enumeration: [dcl.dcl]/3. */
11302 if (!saw_declarator
)
11304 if (cp_parser_declares_only_class_p (parser
))
11306 if (!declares_class_or_enum
11307 && decl_specifiers
.type
11308 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11309 /* Ensure an error is issued anyway when finish_decltype_type,
11310 called via cp_parser_decl_specifier_seq, returns a class or
11311 an enumeration (c++/51786). */
11312 decl_specifiers
.type
= NULL_TREE
;
11313 shadow_tag (&decl_specifiers
);
11315 /* Perform any deferred access checks. */
11316 perform_deferred_access_checks (tf_warning_or_error
);
11319 /* Consume the `;'. */
11320 if (!maybe_range_for_decl
)
11321 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11324 pop_deferring_access_checks ();
11327 /* Parse a decl-specifier-seq.
11329 decl-specifier-seq:
11330 decl-specifier-seq [opt] decl-specifier
11331 decl-specifier attribute-specifier-seq [opt] (C++11)
11334 storage-class-specifier
11345 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11347 The parser flags FLAGS is used to control type-specifier parsing.
11349 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11352 1: one of the decl-specifiers is an elaborated-type-specifier
11353 (i.e., a type declaration)
11354 2: one of the decl-specifiers is an enum-specifier or a
11355 class-specifier (i.e., a type definition)
11360 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11361 cp_parser_flags flags
,
11362 cp_decl_specifier_seq
*decl_specs
,
11363 int* declares_class_or_enum
)
11365 bool constructor_possible_p
= !parser
->in_declarator_p
;
11366 bool found_decl_spec
= false;
11367 cp_token
*start_token
= NULL
;
11370 /* Clear DECL_SPECS. */
11371 clear_decl_specs (decl_specs
);
11373 /* Assume no class or enumeration type is declared. */
11374 *declares_class_or_enum
= 0;
11376 /* Keep reading specifiers until there are no more to read. */
11379 bool constructor_p
;
11383 /* Peek at the next token. */
11384 token
= cp_lexer_peek_token (parser
->lexer
);
11386 /* Save the first token of the decl spec list for error
11389 start_token
= token
;
11390 /* Handle attributes. */
11391 if (cp_next_tokens_can_be_attribute_p (parser
))
11393 /* Parse the attributes. */
11394 tree attrs
= cp_parser_attributes_opt (parser
);
11396 /* In a sequence of declaration specifiers, c++11 attributes
11397 appertain to the type that precede them. In that case
11400 The attribute-specifier-seq affects the type only for
11401 the declaration it appears in, not other declarations
11402 involving the same type.
11404 But for now let's force the user to position the
11405 attribute either at the beginning of the declaration or
11406 after the declarator-id, which would clearly mean that it
11407 applies to the declarator. */
11408 if (cxx11_attribute_p (attrs
))
11410 if (!found_decl_spec
)
11411 /* The c++11 attribute is at the beginning of the
11412 declaration. It appertains to the entity being
11416 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11418 /* This is an attribute following a
11419 class-specifier. */
11420 if (decl_specs
->type_definition_p
)
11421 warn_misplaced_attr_for_class_type (token
->location
,
11427 decl_specs
->std_attributes
11428 = chainon (decl_specs
->std_attributes
,
11430 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11431 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11437 decl_specs
->attributes
11438 = chainon (decl_specs
->attributes
,
11440 if (decl_specs
->locations
[ds_attribute
] == 0)
11441 decl_specs
->locations
[ds_attribute
] = token
->location
;
11444 /* Assume we will find a decl-specifier keyword. */
11445 found_decl_spec
= true;
11446 /* If the next token is an appropriate keyword, we can simply
11447 add it to the list. */
11448 switch (token
->keyword
)
11454 if (!at_class_scope_p ())
11456 error_at (token
->location
, "%<friend%> used outside of class");
11457 cp_lexer_purge_token (parser
->lexer
);
11462 /* Consume the token. */
11463 cp_lexer_consume_token (parser
->lexer
);
11467 case RID_CONSTEXPR
:
11469 cp_lexer_consume_token (parser
->lexer
);
11472 /* function-specifier:
11479 cp_parser_function_specifier_opt (parser
, decl_specs
);
11486 /* Consume the token. */
11487 cp_lexer_consume_token (parser
->lexer
);
11488 /* A constructor declarator cannot appear in a typedef. */
11489 constructor_possible_p
= false;
11490 /* The "typedef" keyword can only occur in a declaration; we
11491 may as well commit at this point. */
11492 cp_parser_commit_to_tentative_parse (parser
);
11494 if (decl_specs
->storage_class
!= sc_none
)
11495 decl_specs
->conflicting_specifiers_p
= true;
11498 /* storage-class-specifier:
11508 if (cxx_dialect
== cxx98
)
11510 /* Consume the token. */
11511 cp_lexer_consume_token (parser
->lexer
);
11513 /* Complain about `auto' as a storage specifier, if
11514 we're complaining about C++0x compatibility. */
11515 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11516 " changes meaning in C++11; please remove it");
11518 /* Set the storage class anyway. */
11519 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11523 /* C++0x auto type-specifier. */
11524 found_decl_spec
= false;
11531 /* Consume the token. */
11532 cp_lexer_consume_token (parser
->lexer
);
11533 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11537 /* Consume the token. */
11539 cp_lexer_consume_token (parser
->lexer
);
11543 /* We did not yet find a decl-specifier yet. */
11544 found_decl_spec
= false;
11548 if (found_decl_spec
11549 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11550 && token
->keyword
!= RID_CONSTEXPR
)
11551 error ("decl-specifier invalid in condition");
11554 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11556 /* Constructors are a special case. The `S' in `S()' is not a
11557 decl-specifier; it is the beginning of the declarator. */
11559 = (!found_decl_spec
11560 && constructor_possible_p
11561 && (cp_parser_constructor_declarator_p
11562 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11564 /* If we don't have a DECL_SPEC yet, then we must be looking at
11565 a type-specifier. */
11566 if (!found_decl_spec
&& !constructor_p
)
11568 int decl_spec_declares_class_or_enum
;
11569 bool is_cv_qualifier
;
11573 = cp_parser_type_specifier (parser
, flags
,
11575 /*is_declaration=*/true,
11576 &decl_spec_declares_class_or_enum
,
11578 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11580 /* If this type-specifier referenced a user-defined type
11581 (a typedef, class-name, etc.), then we can't allow any
11582 more such type-specifiers henceforth.
11586 The longest sequence of decl-specifiers that could
11587 possibly be a type name is taken as the
11588 decl-specifier-seq of a declaration. The sequence shall
11589 be self-consistent as described below.
11593 As a general rule, at most one type-specifier is allowed
11594 in the complete decl-specifier-seq of a declaration. The
11595 only exceptions are the following:
11597 -- const or volatile can be combined with any other
11600 -- signed or unsigned can be combined with char, long,
11608 void g (const int Pc);
11610 Here, Pc is *not* part of the decl-specifier seq; it's
11611 the declarator. Therefore, once we see a type-specifier
11612 (other than a cv-qualifier), we forbid any additional
11613 user-defined types. We *do* still allow things like `int
11614 int' to be considered a decl-specifier-seq, and issue the
11615 error message later. */
11616 if (type_spec
&& !is_cv_qualifier
)
11617 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11618 /* A constructor declarator cannot follow a type-specifier. */
11621 constructor_possible_p
= false;
11622 found_decl_spec
= true;
11623 if (!is_cv_qualifier
)
11624 decl_specs
->any_type_specifiers_p
= true;
11628 /* If we still do not have a DECL_SPEC, then there are no more
11629 decl-specifiers. */
11630 if (!found_decl_spec
)
11633 decl_specs
->any_specifiers_p
= true;
11634 /* After we see one decl-specifier, further decl-specifiers are
11635 always optional. */
11636 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
11639 /* Don't allow a friend specifier with a class definition. */
11640 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
11641 && (*declares_class_or_enum
& 2))
11642 error_at (decl_specs
->locations
[ds_friend
],
11643 "class definition may not be declared a friend");
11646 /* Parse an (optional) storage-class-specifier.
11648 storage-class-specifier:
11657 storage-class-specifier:
11660 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11663 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
11665 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
11668 if (cxx_dialect
!= cxx98
)
11670 /* Fall through for C++98. */
11677 /* Consume the token. */
11678 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11685 /* Parse an (optional) function-specifier.
11687 function-specifier:
11692 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11693 Updates DECL_SPECS, if it is non-NULL. */
11696 cp_parser_function_specifier_opt (cp_parser
* parser
,
11697 cp_decl_specifier_seq
*decl_specs
)
11699 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11700 switch (token
->keyword
)
11703 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
11707 /* 14.5.2.3 [temp.mem]
11709 A member function template shall not be virtual. */
11710 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11711 error_at (token
->location
, "templates may not be %<virtual%>");
11713 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
11717 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
11724 /* Consume the token. */
11725 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11728 /* Parse a linkage-specification.
11730 linkage-specification:
11731 extern string-literal { declaration-seq [opt] }
11732 extern string-literal declaration */
11735 cp_parser_linkage_specification (cp_parser
* parser
)
11739 /* Look for the `extern' keyword. */
11740 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
11742 /* Look for the string-literal. */
11743 linkage
= cp_parser_string_literal (parser
, false, false);
11745 /* Transform the literal into an identifier. If the literal is a
11746 wide-character string, or contains embedded NULs, then we can't
11747 handle it as the user wants. */
11748 if (strlen (TREE_STRING_POINTER (linkage
))
11749 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
11751 cp_parser_error (parser
, "invalid linkage-specification");
11752 /* Assume C++ linkage. */
11753 linkage
= lang_name_cplusplus
;
11756 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
11758 /* We're now using the new linkage. */
11759 push_lang_context (linkage
);
11761 /* If the next token is a `{', then we're using the first
11763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11765 cp_ensure_no_omp_declare_simd (parser
);
11767 /* Consume the `{' token. */
11768 cp_lexer_consume_token (parser
->lexer
);
11769 /* Parse the declarations. */
11770 cp_parser_declaration_seq_opt (parser
);
11771 /* Look for the closing `}'. */
11772 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11774 /* Otherwise, there's just one declaration. */
11777 bool saved_in_unbraced_linkage_specification_p
;
11779 saved_in_unbraced_linkage_specification_p
11780 = parser
->in_unbraced_linkage_specification_p
;
11781 parser
->in_unbraced_linkage_specification_p
= true;
11782 cp_parser_declaration (parser
);
11783 parser
->in_unbraced_linkage_specification_p
11784 = saved_in_unbraced_linkage_specification_p
;
11787 /* We're done with the linkage-specification. */
11788 pop_lang_context ();
11791 /* Parse a static_assert-declaration.
11793 static_assert-declaration:
11794 static_assert ( constant-expression , string-literal ) ;
11796 If MEMBER_P, this static_assert is a class member. */
11799 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
11804 location_t saved_loc
;
11807 /* Peek at the `static_assert' token so we can keep track of exactly
11808 where the static assertion started. */
11809 token
= cp_lexer_peek_token (parser
->lexer
);
11810 saved_loc
= token
->location
;
11812 /* Look for the `static_assert' keyword. */
11813 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
11817 /* We know we are in a static assertion; commit to any tentative
11819 if (cp_parser_parsing_tentatively (parser
))
11820 cp_parser_commit_to_tentative_parse (parser
);
11822 /* Parse the `(' starting the static assertion condition. */
11823 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11825 /* Parse the constant-expression. Allow a non-constant expression
11826 here in order to give better diagnostics in finish_static_assert. */
11828 cp_parser_constant_expression (parser
,
11829 /*allow_non_constant_p=*/true,
11830 /*non_constant_p=*/&dummy
);
11832 /* Parse the separating `,'. */
11833 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11835 /* Parse the string-literal message. */
11836 message
= cp_parser_string_literal (parser
,
11837 /*translate=*/false,
11840 /* A `)' completes the static assertion. */
11841 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11842 cp_parser_skip_to_closing_parenthesis (parser
,
11843 /*recovering=*/true,
11844 /*or_comma=*/false,
11845 /*consume_paren=*/true);
11847 /* A semicolon terminates the declaration. */
11848 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11850 /* Complete the static assertion, which may mean either processing
11851 the static assert now or saving it for template instantiation. */
11852 finish_static_assert (condition
, message
, saved_loc
, member_p
);
11855 /* Parse the expression in decltype ( expression ). */
11858 cp_parser_decltype_expr (cp_parser
*parser
,
11859 bool &id_expression_or_member_access_p
)
11861 cp_token
*id_expr_start_token
;
11864 /* First, try parsing an id-expression. */
11865 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
11866 cp_parser_parse_tentatively (parser
);
11867 expr
= cp_parser_id_expression (parser
,
11868 /*template_keyword_p=*/false,
11869 /*check_dependency_p=*/true,
11870 /*template_p=*/NULL
,
11871 /*declarator_p=*/false,
11872 /*optional_p=*/false);
11874 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
11876 bool non_integral_constant_expression_p
= false;
11877 tree id_expression
= expr
;
11879 const char *error_msg
;
11881 if (identifier_p (expr
))
11882 /* Lookup the name we got back from the id-expression. */
11883 expr
= cp_parser_lookup_name_simple (parser
, expr
,
11884 id_expr_start_token
->location
);
11887 && expr
!= error_mark_node
11888 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
11889 && TREE_CODE (expr
) != TYPE_DECL
11890 && (TREE_CODE (expr
) != BIT_NOT_EXPR
11891 || !TYPE_P (TREE_OPERAND (expr
, 0)))
11892 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11894 /* Complete lookup of the id-expression. */
11895 expr
= (finish_id_expression
11896 (id_expression
, expr
, parser
->scope
, &idk
,
11897 /*integral_constant_expression_p=*/false,
11898 /*allow_non_integral_constant_expression_p=*/true,
11899 &non_integral_constant_expression_p
,
11900 /*template_p=*/false,
11902 /*address_p=*/false,
11903 /*template_arg_p=*/false,
11905 id_expr_start_token
->location
));
11907 if (expr
== error_mark_node
)
11908 /* We found an id-expression, but it was something that we
11909 should not have found. This is an error, not something
11910 we can recover from, so note that we found an
11911 id-expression and we'll recover as gracefully as
11913 id_expression_or_member_access_p
= true;
11917 && expr
!= error_mark_node
11918 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11919 /* We have an id-expression. */
11920 id_expression_or_member_access_p
= true;
11923 if (!id_expression_or_member_access_p
)
11925 /* Abort the id-expression parse. */
11926 cp_parser_abort_tentative_parse (parser
);
11928 /* Parsing tentatively, again. */
11929 cp_parser_parse_tentatively (parser
);
11931 /* Parse a class member access. */
11932 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
11933 /*cast_p=*/false, /*decltype*/true,
11934 /*member_access_only_p=*/true, NULL
);
11937 && expr
!= error_mark_node
11938 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11939 /* We have an id-expression. */
11940 id_expression_or_member_access_p
= true;
11943 if (id_expression_or_member_access_p
)
11944 /* We have parsed the complete id-expression or member access. */
11945 cp_parser_parse_definitely (parser
);
11948 /* Abort our attempt to parse an id-expression or member access
11950 cp_parser_abort_tentative_parse (parser
);
11952 /* Parse a full expression. */
11953 expr
= cp_parser_expression (parser
, /*cast_p=*/false,
11954 /*decltype*/true, NULL
);
11960 /* Parse a `decltype' type. Returns the type.
11962 simple-type-specifier:
11963 decltype ( expression )
11965 decltype ( auto ) */
11968 cp_parser_decltype (cp_parser
*parser
)
11971 bool id_expression_or_member_access_p
= false;
11972 const char *saved_message
;
11973 bool saved_integral_constant_expression_p
;
11974 bool saved_non_integral_constant_expression_p
;
11975 bool saved_greater_than_is_operator_p
;
11976 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11978 if (start_token
->type
== CPP_DECLTYPE
)
11980 /* Already parsed. */
11981 cp_lexer_consume_token (parser
->lexer
);
11982 return start_token
->u
.value
;
11985 /* Look for the `decltype' token. */
11986 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
11987 return error_mark_node
;
11989 /* Parse the opening `('. */
11990 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11991 return error_mark_node
;
11993 /* decltype (auto) */
11994 if (cxx_dialect
>= cxx1y
11995 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
11997 cp_lexer_consume_token (parser
->lexer
);
11998 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11999 return error_mark_node
;
12000 expr
= make_decltype_auto ();
12001 AUTO_IS_DECLTYPE (expr
) = true;
12005 /* Types cannot be defined in a `decltype' expression. Save away the
12007 saved_message
= parser
->type_definition_forbidden_message
;
12009 /* And create the new one. */
12010 parser
->type_definition_forbidden_message
12011 = G_("types may not be defined in %<decltype%> expressions");
12013 /* The restrictions on constant-expressions do not apply inside
12014 decltype expressions. */
12015 saved_integral_constant_expression_p
12016 = parser
->integral_constant_expression_p
;
12017 saved_non_integral_constant_expression_p
12018 = parser
->non_integral_constant_expression_p
;
12019 parser
->integral_constant_expression_p
= false;
12021 /* Within a parenthesized expression, a `>' token is always
12022 the greater-than operator. */
12023 saved_greater_than_is_operator_p
12024 = parser
->greater_than_is_operator_p
;
12025 parser
->greater_than_is_operator_p
= true;
12027 /* Do not actually evaluate the expression. */
12028 ++cp_unevaluated_operand
;
12030 /* Do not warn about problems with the expression. */
12031 ++c_inhibit_evaluation_warnings
;
12033 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12035 /* Go back to evaluating expressions. */
12036 --cp_unevaluated_operand
;
12037 --c_inhibit_evaluation_warnings
;
12039 /* The `>' token might be the end of a template-id or
12040 template-parameter-list now. */
12041 parser
->greater_than_is_operator_p
12042 = saved_greater_than_is_operator_p
;
12044 /* Restore the old message and the integral constant expression
12046 parser
->type_definition_forbidden_message
= saved_message
;
12047 parser
->integral_constant_expression_p
12048 = saved_integral_constant_expression_p
;
12049 parser
->non_integral_constant_expression_p
12050 = saved_non_integral_constant_expression_p
;
12052 /* Parse to the closing `)'. */
12053 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12055 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12056 /*consume_paren=*/true);
12057 return error_mark_node
;
12060 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12061 tf_warning_or_error
);
12064 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12066 start_token
->type
= CPP_DECLTYPE
;
12067 start_token
->u
.value
= expr
;
12068 start_token
->keyword
= RID_MAX
;
12069 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12074 /* Special member functions [gram.special] */
12076 /* Parse a conversion-function-id.
12078 conversion-function-id:
12079 operator conversion-type-id
12081 Returns an IDENTIFIER_NODE representing the operator. */
12084 cp_parser_conversion_function_id (cp_parser
* parser
)
12088 tree saved_qualifying_scope
;
12089 tree saved_object_scope
;
12090 tree pushed_scope
= NULL_TREE
;
12092 /* Look for the `operator' token. */
12093 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12094 return error_mark_node
;
12095 /* When we parse the conversion-type-id, the current scope will be
12096 reset. However, we need that information in able to look up the
12097 conversion function later, so we save it here. */
12098 saved_scope
= parser
->scope
;
12099 saved_qualifying_scope
= parser
->qualifying_scope
;
12100 saved_object_scope
= parser
->object_scope
;
12101 /* We must enter the scope of the class so that the names of
12102 entities declared within the class are available in the
12103 conversion-type-id. For example, consider:
12110 S::operator I() { ... }
12112 In order to see that `I' is a type-name in the definition, we
12113 must be in the scope of `S'. */
12115 pushed_scope
= push_scope (saved_scope
);
12116 /* Parse the conversion-type-id. */
12117 type
= cp_parser_conversion_type_id (parser
);
12118 /* Leave the scope of the class, if any. */
12120 pop_scope (pushed_scope
);
12121 /* Restore the saved scope. */
12122 parser
->scope
= saved_scope
;
12123 parser
->qualifying_scope
= saved_qualifying_scope
;
12124 parser
->object_scope
= saved_object_scope
;
12125 /* If the TYPE is invalid, indicate failure. */
12126 if (type
== error_mark_node
)
12127 return error_mark_node
;
12128 return mangle_conv_op_name_for_type (type
);
12131 /* Parse a conversion-type-id:
12133 conversion-type-id:
12134 type-specifier-seq conversion-declarator [opt]
12136 Returns the TYPE specified. */
12139 cp_parser_conversion_type_id (cp_parser
* parser
)
12142 cp_decl_specifier_seq type_specifiers
;
12143 cp_declarator
*declarator
;
12144 tree type_specified
;
12145 const char *saved_message
;
12147 /* Parse the attributes. */
12148 attributes
= cp_parser_attributes_opt (parser
);
12150 saved_message
= parser
->type_definition_forbidden_message
;
12151 parser
->type_definition_forbidden_message
12152 = G_("types may not be defined in a conversion-type-id");
12154 /* Parse the type-specifiers. */
12155 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12156 /*is_trailing_return=*/false,
12159 parser
->type_definition_forbidden_message
= saved_message
;
12161 /* If that didn't work, stop. */
12162 if (type_specifiers
.type
== error_mark_node
)
12163 return error_mark_node
;
12164 /* Parse the conversion-declarator. */
12165 declarator
= cp_parser_conversion_declarator_opt (parser
);
12167 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12168 /*initialized=*/0, &attributes
);
12170 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12172 /* Don't give this error when parsing tentatively. This happens to
12173 work because we always parse this definitively once. */
12174 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12175 && type_uses_auto (type_specified
))
12177 if (cxx_dialect
< cxx1y
)
12179 error ("invalid use of %<auto%> in conversion operator");
12180 return error_mark_node
;
12182 else if (template_parm_scope_p ())
12183 warning (0, "use of %<auto%> in member template "
12184 "conversion operator can never be deduced");
12187 return type_specified
;
12190 /* Parse an (optional) conversion-declarator.
12192 conversion-declarator:
12193 ptr-operator conversion-declarator [opt]
12197 static cp_declarator
*
12198 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12200 enum tree_code code
;
12201 tree class_type
, std_attributes
= NULL_TREE
;
12202 cp_cv_quals cv_quals
;
12204 /* We don't know if there's a ptr-operator next, or not. */
12205 cp_parser_parse_tentatively (parser
);
12206 /* Try the ptr-operator. */
12207 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12209 /* If it worked, look for more conversion-declarators. */
12210 if (cp_parser_parse_definitely (parser
))
12212 cp_declarator
*declarator
;
12214 /* Parse another optional declarator. */
12215 declarator
= cp_parser_conversion_declarator_opt (parser
);
12217 declarator
= cp_parser_make_indirect_declarator
12218 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12226 /* Parse an (optional) ctor-initializer.
12229 : mem-initializer-list
12231 Returns TRUE iff the ctor-initializer was actually present. */
12234 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12236 /* If the next token is not a `:', then there is no
12237 ctor-initializer. */
12238 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12240 /* Do default initialization of any bases and members. */
12241 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12242 finish_mem_initializers (NULL_TREE
);
12247 /* Consume the `:' token. */
12248 cp_lexer_consume_token (parser
->lexer
);
12249 /* And the mem-initializer-list. */
12250 cp_parser_mem_initializer_list (parser
);
12255 /* Parse a mem-initializer-list.
12257 mem-initializer-list:
12258 mem-initializer ... [opt]
12259 mem-initializer ... [opt] , mem-initializer-list */
12262 cp_parser_mem_initializer_list (cp_parser
* parser
)
12264 tree mem_initializer_list
= NULL_TREE
;
12265 tree target_ctor
= error_mark_node
;
12266 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12268 /* Let the semantic analysis code know that we are starting the
12269 mem-initializer-list. */
12270 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12271 error_at (token
->location
,
12272 "only constructors take member initializers");
12274 /* Loop through the list. */
12277 tree mem_initializer
;
12279 token
= cp_lexer_peek_token (parser
->lexer
);
12280 /* Parse the mem-initializer. */
12281 mem_initializer
= cp_parser_mem_initializer (parser
);
12282 /* If the next token is a `...', we're expanding member initializers. */
12283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12285 /* Consume the `...'. */
12286 cp_lexer_consume_token (parser
->lexer
);
12288 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12289 can be expanded but members cannot. */
12290 if (mem_initializer
!= error_mark_node
12291 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12293 error_at (token
->location
,
12294 "cannot expand initializer for member %<%D%>",
12295 TREE_PURPOSE (mem_initializer
));
12296 mem_initializer
= error_mark_node
;
12299 /* Construct the pack expansion type. */
12300 if (mem_initializer
!= error_mark_node
)
12301 mem_initializer
= make_pack_expansion (mem_initializer
);
12303 if (target_ctor
!= error_mark_node
12304 && mem_initializer
!= error_mark_node
)
12306 error ("mem-initializer for %qD follows constructor delegation",
12307 TREE_PURPOSE (mem_initializer
));
12308 mem_initializer
= error_mark_node
;
12310 /* Look for a target constructor. */
12311 if (mem_initializer
!= error_mark_node
12312 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12313 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12315 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12316 if (mem_initializer_list
)
12318 error ("constructor delegation follows mem-initializer for %qD",
12319 TREE_PURPOSE (mem_initializer_list
));
12320 mem_initializer
= error_mark_node
;
12322 target_ctor
= mem_initializer
;
12324 /* Add it to the list, unless it was erroneous. */
12325 if (mem_initializer
!= error_mark_node
)
12327 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12328 mem_initializer_list
= mem_initializer
;
12330 /* If the next token is not a `,', we're done. */
12331 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12333 /* Consume the `,' token. */
12334 cp_lexer_consume_token (parser
->lexer
);
12337 /* Perform semantic analysis. */
12338 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12339 finish_mem_initializers (mem_initializer_list
);
12342 /* Parse a mem-initializer.
12345 mem-initializer-id ( expression-list [opt] )
12346 mem-initializer-id braced-init-list
12351 ( expression-list [opt] )
12353 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12354 class) or FIELD_DECL (for a non-static data member) to initialize;
12355 the TREE_VALUE is the expression-list. An empty initialization
12356 list is represented by void_list_node. */
12359 cp_parser_mem_initializer (cp_parser
* parser
)
12361 tree mem_initializer_id
;
12362 tree expression_list
;
12364 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12366 /* Find out what is being initialized. */
12367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12369 permerror (token
->location
,
12370 "anachronistic old-style base class initializer");
12371 mem_initializer_id
= NULL_TREE
;
12375 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12376 if (mem_initializer_id
== error_mark_node
)
12377 return mem_initializer_id
;
12379 member
= expand_member_init (mem_initializer_id
);
12380 if (member
&& !DECL_P (member
))
12381 in_base_initializer
= 1;
12383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12385 bool expr_non_constant_p
;
12386 cp_lexer_set_source_position (parser
->lexer
);
12387 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12388 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12389 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12390 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12394 vec
<tree
, va_gc
> *vec
;
12395 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12397 /*allow_expansion_p=*/true,
12398 /*non_constant_p=*/NULL
);
12400 return error_mark_node
;
12401 expression_list
= build_tree_list_vec (vec
);
12402 release_tree_vector (vec
);
12405 if (expression_list
== error_mark_node
)
12406 return error_mark_node
;
12407 if (!expression_list
)
12408 expression_list
= void_type_node
;
12410 in_base_initializer
= 0;
12412 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12415 /* Parse a mem-initializer-id.
12417 mem-initializer-id:
12418 :: [opt] nested-name-specifier [opt] class-name
12421 Returns a TYPE indicating the class to be initializer for the first
12422 production. Returns an IDENTIFIER_NODE indicating the data member
12423 to be initialized for the second production. */
12426 cp_parser_mem_initializer_id (cp_parser
* parser
)
12428 bool global_scope_p
;
12429 bool nested_name_specifier_p
;
12430 bool template_p
= false;
12433 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12435 /* `typename' is not allowed in this context ([temp.res]). */
12436 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12438 error_at (token
->location
,
12439 "keyword %<typename%> not allowed in this context (a qualified "
12440 "member initializer is implicitly a type)");
12441 cp_lexer_consume_token (parser
->lexer
);
12443 /* Look for the optional `::' operator. */
12445 = (cp_parser_global_scope_opt (parser
,
12446 /*current_scope_valid_p=*/false)
12448 /* Look for the optional nested-name-specifier. The simplest way to
12453 The keyword `typename' is not permitted in a base-specifier or
12454 mem-initializer; in these contexts a qualified name that
12455 depends on a template-parameter is implicitly assumed to be a
12458 is to assume that we have seen the `typename' keyword at this
12460 nested_name_specifier_p
12461 = (cp_parser_nested_name_specifier_opt (parser
,
12462 /*typename_keyword_p=*/true,
12463 /*check_dependency_p=*/true,
12465 /*is_declaration=*/true)
12467 if (nested_name_specifier_p
)
12468 template_p
= cp_parser_optional_template_keyword (parser
);
12469 /* If there is a `::' operator or a nested-name-specifier, then we
12470 are definitely looking for a class-name. */
12471 if (global_scope_p
|| nested_name_specifier_p
)
12472 return cp_parser_class_name (parser
,
12473 /*typename_keyword_p=*/true,
12474 /*template_keyword_p=*/template_p
,
12476 /*check_dependency_p=*/true,
12477 /*class_head_p=*/false,
12478 /*is_declaration=*/true);
12479 /* Otherwise, we could also be looking for an ordinary identifier. */
12480 cp_parser_parse_tentatively (parser
);
12481 /* Try a class-name. */
12482 id
= cp_parser_class_name (parser
,
12483 /*typename_keyword_p=*/true,
12484 /*template_keyword_p=*/false,
12486 /*check_dependency_p=*/true,
12487 /*class_head_p=*/false,
12488 /*is_declaration=*/true);
12489 /* If we found one, we're done. */
12490 if (cp_parser_parse_definitely (parser
))
12492 /* Otherwise, look for an ordinary identifier. */
12493 return cp_parser_identifier (parser
);
12496 /* Overloading [gram.over] */
12498 /* Parse an operator-function-id.
12500 operator-function-id:
12503 Returns an IDENTIFIER_NODE for the operator which is a
12504 human-readable spelling of the identifier, e.g., `operator +'. */
12507 cp_parser_operator_function_id (cp_parser
* parser
)
12509 /* Look for the `operator' keyword. */
12510 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12511 return error_mark_node
;
12512 /* And then the name of the operator itself. */
12513 return cp_parser_operator (parser
);
12516 /* Return an identifier node for a user-defined literal operator.
12517 The suffix identifier is chained to the operator name identifier. */
12520 cp_literal_operator_id (const char* name
)
12523 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12524 + strlen (name
) + 10);
12525 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12526 identifier
= get_identifier (buffer
);
12531 /* Parse an operator.
12534 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12535 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12536 || ++ -- , ->* -> () []
12543 Returns an IDENTIFIER_NODE for the operator which is a
12544 human-readable spelling of the identifier, e.g., `operator +'. */
12547 cp_parser_operator (cp_parser
* parser
)
12549 tree id
= NULL_TREE
;
12551 bool bad_encoding_prefix
= false;
12553 /* Peek at the next token. */
12554 token
= cp_lexer_peek_token (parser
->lexer
);
12555 /* Figure out which operator we have. */
12556 switch (token
->type
)
12562 /* The keyword should be either `new' or `delete'. */
12563 if (token
->keyword
== RID_NEW
)
12565 else if (token
->keyword
== RID_DELETE
)
12570 /* Consume the `new' or `delete' token. */
12571 cp_lexer_consume_token (parser
->lexer
);
12573 /* Peek at the next token. */
12574 token
= cp_lexer_peek_token (parser
->lexer
);
12575 /* If it's a `[' token then this is the array variant of the
12577 if (token
->type
== CPP_OPEN_SQUARE
)
12579 /* Consume the `[' token. */
12580 cp_lexer_consume_token (parser
->lexer
);
12581 /* Look for the `]' token. */
12582 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12583 id
= ansi_opname (op
== NEW_EXPR
12584 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12586 /* Otherwise, we have the non-array variant. */
12588 id
= ansi_opname (op
);
12594 id
= ansi_opname (PLUS_EXPR
);
12598 id
= ansi_opname (MINUS_EXPR
);
12602 id
= ansi_opname (MULT_EXPR
);
12606 id
= ansi_opname (TRUNC_DIV_EXPR
);
12610 id
= ansi_opname (TRUNC_MOD_EXPR
);
12614 id
= ansi_opname (BIT_XOR_EXPR
);
12618 id
= ansi_opname (BIT_AND_EXPR
);
12622 id
= ansi_opname (BIT_IOR_EXPR
);
12626 id
= ansi_opname (BIT_NOT_EXPR
);
12630 id
= ansi_opname (TRUTH_NOT_EXPR
);
12634 id
= ansi_assopname (NOP_EXPR
);
12638 id
= ansi_opname (LT_EXPR
);
12642 id
= ansi_opname (GT_EXPR
);
12646 id
= ansi_assopname (PLUS_EXPR
);
12650 id
= ansi_assopname (MINUS_EXPR
);
12654 id
= ansi_assopname (MULT_EXPR
);
12658 id
= ansi_assopname (TRUNC_DIV_EXPR
);
12662 id
= ansi_assopname (TRUNC_MOD_EXPR
);
12666 id
= ansi_assopname (BIT_XOR_EXPR
);
12670 id
= ansi_assopname (BIT_AND_EXPR
);
12674 id
= ansi_assopname (BIT_IOR_EXPR
);
12678 id
= ansi_opname (LSHIFT_EXPR
);
12682 id
= ansi_opname (RSHIFT_EXPR
);
12685 case CPP_LSHIFT_EQ
:
12686 id
= ansi_assopname (LSHIFT_EXPR
);
12689 case CPP_RSHIFT_EQ
:
12690 id
= ansi_assopname (RSHIFT_EXPR
);
12694 id
= ansi_opname (EQ_EXPR
);
12698 id
= ansi_opname (NE_EXPR
);
12702 id
= ansi_opname (LE_EXPR
);
12705 case CPP_GREATER_EQ
:
12706 id
= ansi_opname (GE_EXPR
);
12710 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
12714 id
= ansi_opname (TRUTH_ORIF_EXPR
);
12717 case CPP_PLUS_PLUS
:
12718 id
= ansi_opname (POSTINCREMENT_EXPR
);
12721 case CPP_MINUS_MINUS
:
12722 id
= ansi_opname (PREDECREMENT_EXPR
);
12726 id
= ansi_opname (COMPOUND_EXPR
);
12729 case CPP_DEREF_STAR
:
12730 id
= ansi_opname (MEMBER_REF
);
12734 id
= ansi_opname (COMPONENT_REF
);
12737 case CPP_OPEN_PAREN
:
12738 /* Consume the `('. */
12739 cp_lexer_consume_token (parser
->lexer
);
12740 /* Look for the matching `)'. */
12741 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
12742 return ansi_opname (CALL_EXPR
);
12744 case CPP_OPEN_SQUARE
:
12745 /* Consume the `['. */
12746 cp_lexer_consume_token (parser
->lexer
);
12747 /* Look for the matching `]'. */
12748 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12749 return ansi_opname (ARRAY_REF
);
12754 case CPP_UTF8STRING
:
12755 bad_encoding_prefix
= true;
12756 /* Fall through. */
12759 if (cxx_dialect
== cxx98
)
12760 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12761 if (bad_encoding_prefix
)
12763 error ("invalid encoding prefix in literal operator");
12764 return error_mark_node
;
12766 if (TREE_STRING_LENGTH (token
->u
.value
) > 2)
12768 error ("expected empty string after %<operator%> keyword");
12769 return error_mark_node
;
12771 /* Consume the string. */
12772 cp_lexer_consume_token (parser
->lexer
);
12773 /* Look for the suffix identifier. */
12774 token
= cp_lexer_peek_token (parser
->lexer
);
12775 if (token
->type
== CPP_NAME
)
12777 id
= cp_parser_identifier (parser
);
12778 if (id
!= error_mark_node
)
12780 const char *name
= IDENTIFIER_POINTER (id
);
12781 return cp_literal_operator_id (name
);
12784 else if (token
->type
== CPP_KEYWORD
)
12786 error ("unexpected keyword;"
12787 " remove space between quotes and suffix identifier");
12788 return error_mark_node
;
12792 error ("expected suffix identifier");
12793 return error_mark_node
;
12796 case CPP_WSTRING_USERDEF
:
12797 case CPP_STRING16_USERDEF
:
12798 case CPP_STRING32_USERDEF
:
12799 case CPP_UTF8STRING_USERDEF
:
12800 bad_encoding_prefix
= true;
12801 /* Fall through. */
12803 case CPP_STRING_USERDEF
:
12804 if (cxx_dialect
== cxx98
)
12805 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12806 if (bad_encoding_prefix
)
12808 error ("invalid encoding prefix in literal operator");
12809 return error_mark_node
;
12812 tree string_tree
= USERDEF_LITERAL_VALUE (token
->u
.value
);
12813 if (TREE_STRING_LENGTH (string_tree
) > 2)
12815 error ("expected empty string after %<operator%> keyword");
12816 return error_mark_node
;
12818 id
= USERDEF_LITERAL_SUFFIX_ID (token
->u
.value
);
12819 /* Consume the user-defined string literal. */
12820 cp_lexer_consume_token (parser
->lexer
);
12821 if (id
!= error_mark_node
)
12823 const char *name
= IDENTIFIER_POINTER (id
);
12824 return cp_literal_operator_id (name
);
12827 return error_mark_node
;
12831 /* Anything else is an error. */
12835 /* If we have selected an identifier, we need to consume the
12838 cp_lexer_consume_token (parser
->lexer
);
12839 /* Otherwise, no valid operator name was present. */
12842 cp_parser_error (parser
, "expected operator");
12843 id
= error_mark_node
;
12849 /* Parse a template-declaration.
12851 template-declaration:
12852 export [opt] template < template-parameter-list > declaration
12854 If MEMBER_P is TRUE, this template-declaration occurs within a
12857 The grammar rule given by the standard isn't correct. What
12858 is really meant is:
12860 template-declaration:
12861 export [opt] template-parameter-list-seq
12862 decl-specifier-seq [opt] init-declarator [opt] ;
12863 export [opt] template-parameter-list-seq
12864 function-definition
12866 template-parameter-list-seq:
12867 template-parameter-list-seq [opt]
12868 template < template-parameter-list > */
12871 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
12873 /* Check for `export'. */
12874 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
12876 /* Consume the `export' token. */
12877 cp_lexer_consume_token (parser
->lexer
);
12878 /* Warn that we do not support `export'. */
12879 warning (0, "keyword %<export%> not implemented, and will be ignored");
12882 cp_parser_template_declaration_after_export (parser
, member_p
);
12885 /* Parse a template-parameter-list.
12887 template-parameter-list:
12889 template-parameter-list , template-parameter
12891 Returns a TREE_LIST. Each node represents a template parameter.
12892 The nodes are connected via their TREE_CHAINs. */
12895 cp_parser_template_parameter_list (cp_parser
* parser
)
12897 tree parameter_list
= NULL_TREE
;
12899 begin_template_parm_list ();
12901 /* The loop below parses the template parms. We first need to know
12902 the total number of template parms to be able to compute proper
12903 canonical types of each dependent type. So after the loop, when
12904 we know the total number of template parms,
12905 end_template_parm_list computes the proper canonical types and
12906 fixes up the dependent types accordingly. */
12911 bool is_parameter_pack
;
12912 location_t parm_loc
;
12914 /* Parse the template-parameter. */
12915 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12916 parameter
= cp_parser_template_parameter (parser
,
12918 &is_parameter_pack
);
12919 /* Add it to the list. */
12920 if (parameter
!= error_mark_node
)
12921 parameter_list
= process_template_parm (parameter_list
,
12925 is_parameter_pack
);
12928 tree err_parm
= build_tree_list (parameter
, parameter
);
12929 parameter_list
= chainon (parameter_list
, err_parm
);
12932 /* If the next token is not a `,', we're done. */
12933 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12935 /* Otherwise, consume the `,' token. */
12936 cp_lexer_consume_token (parser
->lexer
);
12939 return end_template_parm_list (parameter_list
);
12942 /* Parse a template-parameter.
12944 template-parameter:
12946 parameter-declaration
12948 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12949 the parameter. The TREE_PURPOSE is the default value, if any.
12950 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12951 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12952 set to true iff this parameter is a parameter pack. */
12955 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
12956 bool *is_parameter_pack
)
12959 cp_parameter_declarator
*parameter_declarator
;
12960 cp_declarator
*id_declarator
;
12963 /* Assume it is a type parameter or a template parameter. */
12964 *is_non_type
= false;
12965 /* Assume it not a parameter pack. */
12966 *is_parameter_pack
= false;
12967 /* Peek at the next token. */
12968 token
= cp_lexer_peek_token (parser
->lexer
);
12969 /* If it is `class' or `template', we have a type-parameter. */
12970 if (token
->keyword
== RID_TEMPLATE
)
12971 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12972 /* If it is `class' or `typename' we do not know yet whether it is a
12973 type parameter or a non-type parameter. Consider:
12975 template <typename T, typename T::X X> ...
12979 template <class C, class D*> ...
12981 Here, the first parameter is a type parameter, and the second is
12982 a non-type parameter. We can tell by looking at the token after
12983 the identifier -- if it is a `,', `=', or `>' then we have a type
12985 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
12987 /* Peek at the token after `class' or `typename'. */
12988 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12989 /* If it's an ellipsis, we have a template type parameter
12991 if (token
->type
== CPP_ELLIPSIS
)
12992 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12993 /* If it's an identifier, skip it. */
12994 if (token
->type
== CPP_NAME
)
12995 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
12996 /* Now, see if the token looks like the end of a template
12998 if (token
->type
== CPP_COMMA
12999 || token
->type
== CPP_EQ
13000 || token
->type
== CPP_GREATER
)
13001 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13004 /* Otherwise, it is a non-type parameter.
13008 When parsing a default template-argument for a non-type
13009 template-parameter, the first non-nested `>' is taken as the end
13010 of the template parameter-list rather than a greater-than
13012 *is_non_type
= true;
13013 parameter_declarator
13014 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
13015 /*parenthesized_p=*/NULL
);
13017 if (!parameter_declarator
)
13018 return error_mark_node
;
13020 /* If the parameter declaration is marked as a parameter pack, set
13021 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13022 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13024 if (parameter_declarator
->declarator
13025 && parameter_declarator
->declarator
->parameter_pack_p
)
13027 *is_parameter_pack
= true;
13028 parameter_declarator
->declarator
->parameter_pack_p
= false;
13031 if (parameter_declarator
->default_argument
)
13033 /* Can happen in some cases of erroneous input (c++/34892). */
13034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13035 /* Consume the `...' for better error recovery. */
13036 cp_lexer_consume_token (parser
->lexer
);
13038 /* If the next token is an ellipsis, and we don't already have it
13039 marked as a parameter pack, then we have a parameter pack (that
13040 has no declarator). */
13041 else if (!*is_parameter_pack
13042 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13043 && (declarator_can_be_parameter_pack
13044 (parameter_declarator
->declarator
)))
13046 /* Consume the `...'. */
13047 cp_lexer_consume_token (parser
->lexer
);
13048 maybe_warn_variadic_templates ();
13050 *is_parameter_pack
= true;
13052 /* We might end up with a pack expansion as the type of the non-type
13053 template parameter, in which case this is a non-type template
13055 else if (parameter_declarator
->decl_specifiers
.type
13056 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
13058 *is_parameter_pack
= true;
13059 parameter_declarator
->decl_specifiers
.type
=
13060 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
13063 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13065 /* Parameter packs cannot have default arguments. However, a
13066 user may try to do so, so we'll parse them and give an
13067 appropriate diagnostic here. */
13069 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13071 /* Find the name of the parameter pack. */
13072 id_declarator
= parameter_declarator
->declarator
;
13073 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
13074 id_declarator
= id_declarator
->declarator
;
13076 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
13077 error_at (start_token
->location
,
13078 "template parameter pack %qD cannot have a default argument",
13079 id_declarator
->u
.id
.unqualified_name
);
13081 error_at (start_token
->location
,
13082 "template parameter pack cannot have a default argument");
13084 /* Parse the default argument, but throw away the result. */
13085 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
13088 parm
= grokdeclarator (parameter_declarator
->declarator
,
13089 ¶meter_declarator
->decl_specifiers
,
13090 TPARM
, /*initialized=*/0,
13091 /*attrlist=*/NULL
);
13092 if (parm
== error_mark_node
)
13093 return error_mark_node
;
13095 return build_tree_list (parameter_declarator
->default_argument
, parm
);
13098 /* Parse a type-parameter.
13101 class identifier [opt]
13102 class identifier [opt] = type-id
13103 typename identifier [opt]
13104 typename identifier [opt] = type-id
13105 template < template-parameter-list > class identifier [opt]
13106 template < template-parameter-list > class identifier [opt]
13109 GNU Extension (variadic templates):
13112 class ... identifier [opt]
13113 typename ... identifier [opt]
13115 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13116 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13117 the declaration of the parameter.
13119 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13122 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13127 /* Look for a keyword to tell us what kind of parameter this is. */
13128 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13130 return error_mark_node
;
13132 switch (token
->keyword
)
13138 tree default_argument
;
13140 /* If the next token is an ellipsis, we have a template
13142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13144 /* Consume the `...' token. */
13145 cp_lexer_consume_token (parser
->lexer
);
13146 maybe_warn_variadic_templates ();
13148 *is_parameter_pack
= true;
13151 /* If the next token is an identifier, then it names the
13153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13154 identifier
= cp_parser_identifier (parser
);
13156 identifier
= NULL_TREE
;
13158 /* Create the parameter. */
13159 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13161 /* If the next token is an `=', we have a default argument. */
13162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13164 /* Consume the `=' token. */
13165 cp_lexer_consume_token (parser
->lexer
);
13166 /* Parse the default-argument. */
13167 push_deferring_access_checks (dk_no_deferred
);
13168 default_argument
= cp_parser_type_id (parser
);
13170 /* Template parameter packs cannot have default
13172 if (*is_parameter_pack
)
13175 error_at (token
->location
,
13176 "template parameter pack %qD cannot have a "
13177 "default argument", identifier
);
13179 error_at (token
->location
,
13180 "template parameter packs cannot have "
13181 "default arguments");
13182 default_argument
= NULL_TREE
;
13184 pop_deferring_access_checks ();
13187 default_argument
= NULL_TREE
;
13189 /* Create the combined representation of the parameter and the
13190 default argument. */
13191 parameter
= build_tree_list (default_argument
, parameter
);
13198 tree default_argument
;
13200 /* Look for the `<'. */
13201 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13202 /* Parse the template-parameter-list. */
13203 cp_parser_template_parameter_list (parser
);
13204 /* Look for the `>'. */
13205 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13206 /* Look for the `class' keyword. */
13207 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
13208 /* If the next token is an ellipsis, we have a template
13210 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13212 /* Consume the `...' token. */
13213 cp_lexer_consume_token (parser
->lexer
);
13214 maybe_warn_variadic_templates ();
13216 *is_parameter_pack
= true;
13218 /* If the next token is an `=', then there is a
13219 default-argument. If the next token is a `>', we are at
13220 the end of the parameter-list. If the next token is a `,',
13221 then we are at the end of this parameter. */
13222 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13223 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13224 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13226 identifier
= cp_parser_identifier (parser
);
13227 /* Treat invalid names as if the parameter were nameless. */
13228 if (identifier
== error_mark_node
)
13229 identifier
= NULL_TREE
;
13232 identifier
= NULL_TREE
;
13234 /* Create the template parameter. */
13235 parameter
= finish_template_template_parm (class_type_node
,
13238 /* If the next token is an `=', then there is a
13239 default-argument. */
13240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13244 /* Consume the `='. */
13245 cp_lexer_consume_token (parser
->lexer
);
13246 /* Parse the id-expression. */
13247 push_deferring_access_checks (dk_no_deferred
);
13248 /* save token before parsing the id-expression, for error
13250 token
= cp_lexer_peek_token (parser
->lexer
);
13252 = cp_parser_id_expression (parser
,
13253 /*template_keyword_p=*/false,
13254 /*check_dependency_p=*/true,
13255 /*template_p=*/&is_template
,
13256 /*declarator_p=*/false,
13257 /*optional_p=*/false);
13258 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13259 /* If the id-expression was a template-id that refers to
13260 a template-class, we already have the declaration here,
13261 so no further lookup is needed. */
13264 /* Look up the name. */
13266 = cp_parser_lookup_name (parser
, default_argument
,
13268 /*is_template=*/is_template
,
13269 /*is_namespace=*/false,
13270 /*check_dependency=*/true,
13271 /*ambiguous_decls=*/NULL
,
13273 /* See if the default argument is valid. */
13275 = check_template_template_default_arg (default_argument
);
13277 /* Template parameter packs cannot have default
13279 if (*is_parameter_pack
)
13282 error_at (token
->location
,
13283 "template parameter pack %qD cannot "
13284 "have a default argument",
13287 error_at (token
->location
, "template parameter packs cannot "
13288 "have default arguments");
13289 default_argument
= NULL_TREE
;
13291 pop_deferring_access_checks ();
13294 default_argument
= NULL_TREE
;
13296 /* Create the combined representation of the parameter and the
13297 default argument. */
13298 parameter
= build_tree_list (default_argument
, parameter
);
13303 gcc_unreachable ();
13310 /* Parse a template-id.
13313 template-name < template-argument-list [opt] >
13315 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13316 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13317 returned. Otherwise, if the template-name names a function, or set
13318 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13319 names a class, returns a TYPE_DECL for the specialization.
13321 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13322 uninstantiated templates. */
13325 cp_parser_template_id (cp_parser
*parser
,
13326 bool template_keyword_p
,
13327 bool check_dependency_p
,
13328 enum tag_types tag_type
,
13329 bool is_declaration
)
13335 cp_token_position start_of_id
= 0;
13336 deferred_access_check
*chk
;
13337 vec
<deferred_access_check
, va_gc
> *access_check
;
13338 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13339 bool is_identifier
;
13341 /* If the next token corresponds to a template-id, there is no need
13343 next_token
= cp_lexer_peek_token (parser
->lexer
);
13344 if (next_token
->type
== CPP_TEMPLATE_ID
)
13346 struct tree_check
*check_value
;
13348 /* Get the stored value. */
13349 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13350 /* Perform any access checks that were deferred. */
13351 access_check
= check_value
->checks
;
13354 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13355 perform_or_defer_access_check (chk
->binfo
,
13358 tf_warning_or_error
);
13360 /* Return the stored value. */
13361 return check_value
->value
;
13364 /* Avoid performing name lookup if there is no possibility of
13365 finding a template-id. */
13366 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13367 || (next_token
->type
== CPP_NAME
13368 && !cp_parser_nth_token_starts_template_argument_list_p
13371 cp_parser_error (parser
, "expected template-id");
13372 return error_mark_node
;
13375 /* Remember where the template-id starts. */
13376 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13377 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13379 push_deferring_access_checks (dk_deferred
);
13381 /* Parse the template-name. */
13382 is_identifier
= false;
13383 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13384 check_dependency_p
,
13388 if (templ
== error_mark_node
|| is_identifier
)
13390 pop_deferring_access_checks ();
13394 /* If we find the sequence `[:' after a template-name, it's probably
13395 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13396 parse correctly the argument list. */
13397 next_token
= cp_lexer_peek_token (parser
->lexer
);
13398 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13399 if (next_token
->type
== CPP_OPEN_SQUARE
13400 && next_token
->flags
& DIGRAPH
13401 && next_token_2
->type
== CPP_COLON
13402 && !(next_token_2
->flags
& PREV_WHITE
))
13404 cp_parser_parse_tentatively (parser
);
13405 /* Change `:' into `::'. */
13406 next_token_2
->type
= CPP_SCOPE
;
13407 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13409 cp_lexer_consume_token (parser
->lexer
);
13411 /* Parse the arguments. */
13412 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13413 if (!cp_parser_parse_definitely (parser
))
13415 /* If we couldn't parse an argument list, then we revert our changes
13416 and return simply an error. Maybe this is not a template-id
13418 next_token_2
->type
= CPP_COLON
;
13419 cp_parser_error (parser
, "expected %<<%>");
13420 pop_deferring_access_checks ();
13421 return error_mark_node
;
13423 /* Otherwise, emit an error about the invalid digraph, but continue
13424 parsing because we got our argument list. */
13425 if (permerror (next_token
->location
,
13426 "%<<::%> cannot begin a template-argument list"))
13428 static bool hint
= false;
13429 inform (next_token
->location
,
13430 "%<<:%> is an alternate spelling for %<[%>."
13431 " Insert whitespace between %<<%> and %<::%>");
13432 if (!hint
&& !flag_permissive
)
13434 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13435 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13436 "accept your code)");
13443 /* Look for the `<' that starts the template-argument-list. */
13444 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13446 pop_deferring_access_checks ();
13447 return error_mark_node
;
13449 /* Parse the arguments. */
13450 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13453 /* Build a representation of the specialization. */
13454 if (identifier_p (templ
))
13455 template_id
= build_min_nt_loc (next_token
->location
,
13458 else if (DECL_TYPE_TEMPLATE_P (templ
)
13459 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13461 bool entering_scope
;
13462 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13463 template (rather than some instantiation thereof) only if
13464 is not nested within some other construct. For example, in
13465 "template <typename T> void f(T) { A<T>::", A<T> is just an
13466 instantiation of A. */
13467 entering_scope
= (template_parm_scope_p ()
13468 && cp_lexer_next_token_is (parser
->lexer
,
13471 = finish_template_type (templ
, arguments
, entering_scope
);
13475 /* If it's not a class-template or a template-template, it should be
13476 a function-template. */
13477 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13478 || TREE_CODE (templ
) == OVERLOAD
13479 || BASELINK_P (templ
)));
13481 template_id
= lookup_template_function (templ
, arguments
);
13484 /* If parsing tentatively, replace the sequence of tokens that makes
13485 up the template-id with a CPP_TEMPLATE_ID token. That way,
13486 should we re-parse the token stream, we will not have to repeat
13487 the effort required to do the parse, nor will we issue duplicate
13488 error messages about problems during instantiation of the
13491 /* Don't do this if we had a parse error in a declarator; re-parsing
13492 might succeed if a name changes meaning (60361). */
13493 && !(cp_parser_error_occurred (parser
)
13494 && cp_parser_parsing_tentatively (parser
)
13495 && parser
->in_declarator_p
))
13497 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13499 /* Reset the contents of the START_OF_ID token. */
13500 token
->type
= CPP_TEMPLATE_ID
;
13501 /* Retrieve any deferred checks. Do not pop this access checks yet
13502 so the memory will not be reclaimed during token replacing below. */
13503 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
13504 token
->u
.tree_check_value
->value
= template_id
;
13505 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13506 token
->keyword
= RID_MAX
;
13508 /* Purge all subsequent tokens. */
13509 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13511 /* ??? Can we actually assume that, if template_id ==
13512 error_mark_node, we will have issued a diagnostic to the
13513 user, as opposed to simply marking the tentative parse as
13515 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13516 error_at (token
->location
, "parse error in template argument list");
13519 pop_to_parent_deferring_access_checks ();
13520 return template_id
;
13523 /* Parse a template-name.
13528 The standard should actually say:
13532 operator-function-id
13534 A defect report has been filed about this issue.
13536 A conversion-function-id cannot be a template name because they cannot
13537 be part of a template-id. In fact, looking at this code:
13539 a.operator K<int>()
13541 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13542 It is impossible to call a templated conversion-function-id with an
13543 explicit argument list, since the only allowed template parameter is
13544 the type to which it is converting.
13546 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13547 `template' keyword, in a construction like:
13551 In that case `f' is taken to be a template-name, even though there
13552 is no way of knowing for sure.
13554 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13555 name refers to a set of overloaded functions, at least one of which
13556 is a template, or an IDENTIFIER_NODE with the name of the template,
13557 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13558 names are looked up inside uninstantiated templates. */
13561 cp_parser_template_name (cp_parser
* parser
,
13562 bool template_keyword_p
,
13563 bool check_dependency_p
,
13564 bool is_declaration
,
13565 enum tag_types tag_type
,
13566 bool *is_identifier
)
13571 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13573 /* If the next token is `operator', then we have either an
13574 operator-function-id or a conversion-function-id. */
13575 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13577 /* We don't know whether we're looking at an
13578 operator-function-id or a conversion-function-id. */
13579 cp_parser_parse_tentatively (parser
);
13580 /* Try an operator-function-id. */
13581 identifier
= cp_parser_operator_function_id (parser
);
13582 /* If that didn't work, try a conversion-function-id. */
13583 if (!cp_parser_parse_definitely (parser
))
13585 cp_parser_error (parser
, "expected template-name");
13586 return error_mark_node
;
13589 /* Look for the identifier. */
13591 identifier
= cp_parser_identifier (parser
);
13593 /* If we didn't find an identifier, we don't have a template-id. */
13594 if (identifier
== error_mark_node
)
13595 return error_mark_node
;
13597 /* If the name immediately followed the `template' keyword, then it
13598 is a template-name. However, if the next token is not `<', then
13599 we do not treat it as a template-name, since it is not being used
13600 as part of a template-id. This enables us to handle constructs
13603 template <typename T> struct S { S(); };
13604 template <typename T> S<T>::S();
13606 correctly. We would treat `S' as a template -- if it were `S<T>'
13607 -- but we do not if there is no `<'. */
13609 if (processing_template_decl
13610 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13612 /* In a declaration, in a dependent context, we pretend that the
13613 "template" keyword was present in order to improve error
13614 recovery. For example, given:
13616 template <typename T> void f(T::X<int>);
13618 we want to treat "X<int>" as a template-id. */
13620 && !template_keyword_p
13621 && parser
->scope
&& TYPE_P (parser
->scope
)
13622 && check_dependency_p
13623 && dependent_scope_p (parser
->scope
)
13624 /* Do not do this for dtors (or ctors), since they never
13625 need the template keyword before their name. */
13626 && !constructor_name_p (identifier
, parser
->scope
))
13628 cp_token_position start
= 0;
13630 /* Explain what went wrong. */
13631 error_at (token
->location
, "non-template %qD used as template",
13633 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
13634 parser
->scope
, identifier
);
13635 /* If parsing tentatively, find the location of the "<" token. */
13636 if (cp_parser_simulate_error (parser
))
13637 start
= cp_lexer_token_position (parser
->lexer
, true);
13638 /* Parse the template arguments so that we can issue error
13639 messages about them. */
13640 cp_lexer_consume_token (parser
->lexer
);
13641 cp_parser_enclosed_template_argument_list (parser
);
13642 /* Skip tokens until we find a good place from which to
13643 continue parsing. */
13644 cp_parser_skip_to_closing_parenthesis (parser
,
13645 /*recovering=*/true,
13647 /*consume_paren=*/false);
13648 /* If parsing tentatively, permanently remove the
13649 template argument list. That will prevent duplicate
13650 error messages from being issued about the missing
13651 "template" keyword. */
13653 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
13655 *is_identifier
= true;
13659 /* If the "template" keyword is present, then there is generally
13660 no point in doing name-lookup, so we just return IDENTIFIER.
13661 But, if the qualifying scope is non-dependent then we can
13662 (and must) do name-lookup normally. */
13663 if (template_keyword_p
13665 || (TYPE_P (parser
->scope
)
13666 && dependent_type_p (parser
->scope
))))
13670 /* Look up the name. */
13671 decl
= cp_parser_lookup_name (parser
, identifier
,
13673 /*is_template=*/true,
13674 /*is_namespace=*/false,
13675 check_dependency_p
,
13676 /*ambiguous_decls=*/NULL
,
13679 /* If DECL is a template, then the name was a template-name. */
13680 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
13684 tree fn
= NULL_TREE
;
13686 /* The standard does not explicitly indicate whether a name that
13687 names a set of overloaded declarations, some of which are
13688 templates, is a template-name. However, such a name should
13689 be a template-name; otherwise, there is no way to form a
13690 template-id for the overloaded templates. */
13691 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
13692 if (TREE_CODE (fns
) == OVERLOAD
)
13693 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
13694 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
13699 /* The name does not name a template. */
13700 cp_parser_error (parser
, "expected template-name");
13701 return error_mark_node
;
13705 /* If DECL is dependent, and refers to a function, then just return
13706 its name; we will look it up again during template instantiation. */
13707 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
13709 tree scope
= ovl_scope (decl
);
13710 if (TYPE_P (scope
) && dependent_type_p (scope
))
13717 /* Parse a template-argument-list.
13719 template-argument-list:
13720 template-argument ... [opt]
13721 template-argument-list , template-argument ... [opt]
13723 Returns a TREE_VEC containing the arguments. */
13726 cp_parser_template_argument_list (cp_parser
* parser
)
13728 tree fixed_args
[10];
13729 unsigned n_args
= 0;
13730 unsigned alloced
= 10;
13731 tree
*arg_ary
= fixed_args
;
13733 bool saved_in_template_argument_list_p
;
13735 bool saved_non_ice_p
;
13737 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
13738 parser
->in_template_argument_list_p
= true;
13739 /* Even if the template-id appears in an integral
13740 constant-expression, the contents of the argument list do
13742 saved_ice_p
= parser
->integral_constant_expression_p
;
13743 parser
->integral_constant_expression_p
= false;
13744 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
13745 parser
->non_integral_constant_expression_p
= false;
13747 /* Parse the arguments. */
13753 /* Consume the comma. */
13754 cp_lexer_consume_token (parser
->lexer
);
13756 /* Parse the template-argument. */
13757 argument
= cp_parser_template_argument (parser
);
13759 /* If the next token is an ellipsis, we're expanding a template
13761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13763 if (argument
== error_mark_node
)
13765 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13766 error_at (token
->location
,
13767 "expected parameter pack before %<...%>");
13769 /* Consume the `...' token. */
13770 cp_lexer_consume_token (parser
->lexer
);
13772 /* Make the argument into a TYPE_PACK_EXPANSION or
13773 EXPR_PACK_EXPANSION. */
13774 argument
= make_pack_expansion (argument
);
13777 if (n_args
== alloced
)
13781 if (arg_ary
== fixed_args
)
13783 arg_ary
= XNEWVEC (tree
, alloced
);
13784 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
13787 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
13789 arg_ary
[n_args
++] = argument
;
13791 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
13793 vec
= make_tree_vec (n_args
);
13796 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
13798 if (arg_ary
!= fixed_args
)
13800 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
13801 parser
->integral_constant_expression_p
= saved_ice_p
;
13802 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
13803 #ifdef ENABLE_CHECKING
13804 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
13809 /* Parse a template-argument.
13812 assignment-expression
13816 The representation is that of an assignment-expression, type-id, or
13817 id-expression -- except that the qualified id-expression is
13818 evaluated, so that the value returned is either a DECL or an
13821 Although the standard says "assignment-expression", it forbids
13822 throw-expressions or assignments in the template argument.
13823 Therefore, we use "conditional-expression" instead. */
13826 cp_parser_template_argument (cp_parser
* parser
)
13831 bool maybe_type_id
= false;
13832 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
13833 location_t loc
= 0;
13836 /* There's really no way to know what we're looking at, so we just
13837 try each alternative in order.
13841 In a template-argument, an ambiguity between a type-id and an
13842 expression is resolved to a type-id, regardless of the form of
13843 the corresponding template-parameter.
13845 Therefore, we try a type-id first. */
13846 cp_parser_parse_tentatively (parser
);
13847 argument
= cp_parser_template_type_arg (parser
);
13848 /* If there was no error parsing the type-id but the next token is a
13849 '>>', our behavior depends on which dialect of C++ we're
13850 parsing. In C++98, we probably found a typo for '> >'. But there
13851 are type-id which are also valid expressions. For instance:
13853 struct X { int operator >> (int); };
13854 template <int V> struct Foo {};
13857 Here 'X()' is a valid type-id of a function type, but the user just
13858 wanted to write the expression "X() >> 5". Thus, we remember that we
13859 found a valid type-id, but we still try to parse the argument as an
13860 expression to see what happens.
13862 In C++0x, the '>>' will be considered two separate '>'
13864 if (!cp_parser_error_occurred (parser
)
13865 && cxx_dialect
== cxx98
13866 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
13868 maybe_type_id
= true;
13869 cp_parser_abort_tentative_parse (parser
);
13873 /* If the next token isn't a `,' or a `>', then this argument wasn't
13874 really finished. This means that the argument is not a valid
13876 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13877 cp_parser_error (parser
, "expected template-argument");
13878 /* If that worked, we're done. */
13879 if (cp_parser_parse_definitely (parser
))
13882 /* We're still not sure what the argument will be. */
13883 cp_parser_parse_tentatively (parser
);
13884 /* Try a template. */
13885 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
13886 argument
= cp_parser_id_expression (parser
,
13887 /*template_keyword_p=*/false,
13888 /*check_dependency_p=*/true,
13890 /*declarator_p=*/false,
13891 /*optional_p=*/false);
13892 /* If the next token isn't a `,' or a `>', then this argument wasn't
13893 really finished. */
13894 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13895 cp_parser_error (parser
, "expected template-argument");
13896 if (!cp_parser_error_occurred (parser
))
13898 /* Figure out what is being referred to. If the id-expression
13899 was for a class template specialization, then we will have a
13900 TYPE_DECL at this point. There is no need to do name lookup
13901 at this point in that case. */
13902 if (TREE_CODE (argument
) != TYPE_DECL
)
13903 argument
= cp_parser_lookup_name (parser
, argument
,
13905 /*is_template=*/template_p
,
13906 /*is_namespace=*/false,
13907 /*check_dependency=*/true,
13908 /*ambiguous_decls=*/NULL
,
13909 argument_start_token
->location
);
13910 if (TREE_CODE (argument
) != TEMPLATE_DECL
13911 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
13912 cp_parser_error (parser
, "expected template-name");
13914 if (cp_parser_parse_definitely (parser
))
13916 /* It must be a non-type argument. There permitted cases are given
13917 in [temp.arg.nontype]:
13919 -- an integral constant-expression of integral or enumeration
13922 -- the name of a non-type template-parameter; or
13924 -- the name of an object or function with external linkage...
13926 -- the address of an object or function with external linkage...
13928 -- a pointer to member... */
13929 /* Look for a non-type template parameter. */
13930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13932 cp_parser_parse_tentatively (parser
);
13933 argument
= cp_parser_primary_expression (parser
,
13934 /*address_p=*/false,
13936 /*template_arg_p=*/true,
13938 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
13939 || !cp_parser_next_token_ends_template_argument_p (parser
))
13940 cp_parser_simulate_error (parser
);
13941 if (cp_parser_parse_definitely (parser
))
13945 /* If the next token is "&", the argument must be the address of an
13946 object or function with external linkage. */
13947 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
13950 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13951 cp_lexer_consume_token (parser
->lexer
);
13953 /* See if we might have an id-expression. */
13954 token
= cp_lexer_peek_token (parser
->lexer
);
13955 if (token
->type
== CPP_NAME
13956 || token
->keyword
== RID_OPERATOR
13957 || token
->type
== CPP_SCOPE
13958 || token
->type
== CPP_TEMPLATE_ID
13959 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
13961 cp_parser_parse_tentatively (parser
);
13962 argument
= cp_parser_primary_expression (parser
,
13965 /*template_arg_p=*/true,
13967 if (cp_parser_error_occurred (parser
)
13968 || !cp_parser_next_token_ends_template_argument_p (parser
))
13969 cp_parser_abort_tentative_parse (parser
);
13974 if (INDIRECT_REF_P (argument
))
13976 /* Strip the dereference temporarily. */
13977 gcc_assert (REFERENCE_REF_P (argument
));
13978 argument
= TREE_OPERAND (argument
, 0);
13981 /* If we're in a template, we represent a qualified-id referring
13982 to a static data member as a SCOPE_REF even if the scope isn't
13983 dependent so that we can check access control later. */
13985 if (TREE_CODE (probe
) == SCOPE_REF
)
13986 probe
= TREE_OPERAND (probe
, 1);
13989 /* A variable without external linkage might still be a
13990 valid constant-expression, so no error is issued here
13991 if the external-linkage check fails. */
13992 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
13993 cp_parser_simulate_error (parser
);
13995 else if (is_overloaded_fn (argument
))
13996 /* All overloaded functions are allowed; if the external
13997 linkage test does not pass, an error will be issued
14001 && (TREE_CODE (argument
) == OFFSET_REF
14002 || TREE_CODE (argument
) == SCOPE_REF
))
14003 /* A pointer-to-member. */
14005 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
14008 cp_parser_simulate_error (parser
);
14010 if (cp_parser_parse_definitely (parser
))
14013 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
14014 tf_warning_or_error
);
14016 argument
= convert_from_reference (argument
);
14021 /* If the argument started with "&", there are no other valid
14022 alternatives at this point. */
14025 cp_parser_error (parser
, "invalid non-type template argument");
14026 return error_mark_node
;
14029 /* If the argument wasn't successfully parsed as a type-id followed
14030 by '>>', the argument can only be a constant expression now.
14031 Otherwise, we try parsing the constant-expression tentatively,
14032 because the argument could really be a type-id. */
14034 cp_parser_parse_tentatively (parser
);
14035 argument
= cp_parser_constant_expression (parser
,
14036 /*allow_non_constant_p=*/false,
14037 /*non_constant_p=*/NULL
);
14038 if (!maybe_type_id
)
14040 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14041 cp_parser_error (parser
, "expected template-argument");
14042 if (cp_parser_parse_definitely (parser
))
14044 /* We did our best to parse the argument as a non type-id, but that
14045 was the only alternative that matched (albeit with a '>' after
14046 it). We can assume it's just a typo from the user, and a
14047 diagnostic will then be issued. */
14048 return cp_parser_template_type_arg (parser
);
14051 /* Parse an explicit-instantiation.
14053 explicit-instantiation:
14054 template declaration
14056 Although the standard says `declaration', what it really means is:
14058 explicit-instantiation:
14059 template decl-specifier-seq [opt] declarator [opt] ;
14061 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14062 supposed to be allowed. A defect report has been filed about this
14067 explicit-instantiation:
14068 storage-class-specifier template
14069 decl-specifier-seq [opt] declarator [opt] ;
14070 function-specifier template
14071 decl-specifier-seq [opt] declarator [opt] ; */
14074 cp_parser_explicit_instantiation (cp_parser
* parser
)
14076 int declares_class_or_enum
;
14077 cp_decl_specifier_seq decl_specifiers
;
14078 tree extension_specifier
= NULL_TREE
;
14080 timevar_push (TV_TEMPLATE_INST
);
14082 /* Look for an (optional) storage-class-specifier or
14083 function-specifier. */
14084 if (cp_parser_allow_gnu_extensions_p (parser
))
14086 extension_specifier
14087 = cp_parser_storage_class_specifier_opt (parser
);
14088 if (!extension_specifier
)
14089 extension_specifier
14090 = cp_parser_function_specifier_opt (parser
,
14091 /*decl_specs=*/NULL
);
14094 /* Look for the `template' keyword. */
14095 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14096 /* Let the front end know that we are processing an explicit
14098 begin_explicit_instantiation ();
14099 /* [temp.explicit] says that we are supposed to ignore access
14100 control while processing explicit instantiation directives. */
14101 push_deferring_access_checks (dk_no_check
);
14102 /* Parse a decl-specifier-seq. */
14103 cp_parser_decl_specifier_seq (parser
,
14104 CP_PARSER_FLAGS_OPTIONAL
,
14106 &declares_class_or_enum
);
14107 /* If there was exactly one decl-specifier, and it declared a class,
14108 and there's no declarator, then we have an explicit type
14110 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
14114 type
= check_tag_decl (&decl_specifiers
,
14115 /*explicit_type_instantiation_p=*/true);
14116 /* Turn access control back on for names used during
14117 template instantiation. */
14118 pop_deferring_access_checks ();
14120 do_type_instantiation (type
, extension_specifier
,
14121 /*complain=*/tf_error
);
14125 cp_declarator
*declarator
;
14128 /* Parse the declarator. */
14130 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14131 /*ctor_dtor_or_conv_p=*/NULL
,
14132 /*parenthesized_p=*/NULL
,
14133 /*member_p=*/false);
14134 if (declares_class_or_enum
& 2)
14135 cp_parser_check_for_definition_in_return_type (declarator
,
14136 decl_specifiers
.type
,
14137 decl_specifiers
.locations
[ds_type_spec
]);
14138 if (declarator
!= cp_error_declarator
)
14140 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14141 permerror (decl_specifiers
.locations
[ds_inline
],
14142 "explicit instantiation shall not use"
14143 " %<inline%> specifier");
14144 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14145 permerror (decl_specifiers
.locations
[ds_constexpr
],
14146 "explicit instantiation shall not use"
14147 " %<constexpr%> specifier");
14149 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14150 NORMAL
, 0, &decl_specifiers
.attributes
);
14151 /* Turn access control back on for names used during
14152 template instantiation. */
14153 pop_deferring_access_checks ();
14154 /* Do the explicit instantiation. */
14155 do_decl_instantiation (decl
, extension_specifier
);
14159 pop_deferring_access_checks ();
14160 /* Skip the body of the explicit instantiation. */
14161 cp_parser_skip_to_end_of_statement (parser
);
14164 /* We're done with the instantiation. */
14165 end_explicit_instantiation ();
14167 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14169 timevar_pop (TV_TEMPLATE_INST
);
14172 /* Parse an explicit-specialization.
14174 explicit-specialization:
14175 template < > declaration
14177 Although the standard says `declaration', what it really means is:
14179 explicit-specialization:
14180 template <> decl-specifier [opt] init-declarator [opt] ;
14181 template <> function-definition
14182 template <> explicit-specialization
14183 template <> template-declaration */
14186 cp_parser_explicit_specialization (cp_parser
* parser
)
14188 bool need_lang_pop
;
14189 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14191 /* Look for the `template' keyword. */
14192 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14193 /* Look for the `<'. */
14194 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14195 /* Look for the `>'. */
14196 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14197 /* We have processed another parameter list. */
14198 ++parser
->num_template_parameter_lists
;
14201 A template ... explicit specialization ... shall not have C
14203 if (current_lang_name
== lang_name_c
)
14205 error_at (token
->location
, "template specialization with C linkage");
14206 /* Give it C++ linkage to avoid confusing other parts of the
14208 push_lang_context (lang_name_cplusplus
);
14209 need_lang_pop
= true;
14212 need_lang_pop
= false;
14213 /* Let the front end know that we are beginning a specialization. */
14214 if (!begin_specialization ())
14216 end_specialization ();
14220 /* If the next keyword is `template', we need to figure out whether
14221 or not we're looking a template-declaration. */
14222 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14224 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14225 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14226 cp_parser_template_declaration_after_export (parser
,
14227 /*member_p=*/false);
14229 cp_parser_explicit_specialization (parser
);
14232 /* Parse the dependent declaration. */
14233 cp_parser_single_declaration (parser
,
14235 /*member_p=*/false,
14236 /*explicit_specialization_p=*/true,
14237 /*friend_p=*/NULL
);
14238 /* We're done with the specialization. */
14239 end_specialization ();
14240 /* For the erroneous case of a template with C linkage, we pushed an
14241 implicit C++ linkage scope; exit that scope now. */
14243 pop_lang_context ();
14244 /* We're done with this parameter list. */
14245 --parser
->num_template_parameter_lists
;
14248 /* Parse a type-specifier.
14251 simple-type-specifier
14254 elaborated-type-specifier
14262 Returns a representation of the type-specifier. For a
14263 class-specifier, enum-specifier, or elaborated-type-specifier, a
14264 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14266 The parser flags FLAGS is used to control type-specifier parsing.
14268 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14269 in a decl-specifier-seq.
14271 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14272 class-specifier, enum-specifier, or elaborated-type-specifier, then
14273 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14274 if a type is declared; 2 if it is defined. Otherwise, it is set to
14277 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14278 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14279 is set to FALSE. */
14282 cp_parser_type_specifier (cp_parser
* parser
,
14283 cp_parser_flags flags
,
14284 cp_decl_specifier_seq
*decl_specs
,
14285 bool is_declaration
,
14286 int* declares_class_or_enum
,
14287 bool* is_cv_qualifier
)
14289 tree type_spec
= NULL_TREE
;
14292 cp_decl_spec ds
= ds_last
;
14294 /* Assume this type-specifier does not declare a new type. */
14295 if (declares_class_or_enum
)
14296 *declares_class_or_enum
= 0;
14297 /* And that it does not specify a cv-qualifier. */
14298 if (is_cv_qualifier
)
14299 *is_cv_qualifier
= false;
14300 /* Peek at the next token. */
14301 token
= cp_lexer_peek_token (parser
->lexer
);
14303 /* If we're looking at a keyword, we can use that to guide the
14304 production we choose. */
14305 keyword
= token
->keyword
;
14309 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14310 goto elaborated_type_specifier
;
14312 /* Look for the enum-specifier. */
14313 type_spec
= cp_parser_enum_specifier (parser
);
14314 /* If that worked, we're done. */
14317 if (declares_class_or_enum
)
14318 *declares_class_or_enum
= 2;
14320 cp_parser_set_decl_spec_type (decl_specs
,
14323 /*type_definition_p=*/true);
14327 goto elaborated_type_specifier
;
14329 /* Any of these indicate either a class-specifier, or an
14330 elaborated-type-specifier. */
14334 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14335 goto elaborated_type_specifier
;
14337 /* Parse tentatively so that we can back up if we don't find a
14338 class-specifier. */
14339 cp_parser_parse_tentatively (parser
);
14340 /* Look for the class-specifier. */
14341 type_spec
= cp_parser_class_specifier (parser
);
14342 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14343 /* If that worked, we're done. */
14344 if (cp_parser_parse_definitely (parser
))
14346 if (declares_class_or_enum
)
14347 *declares_class_or_enum
= 2;
14349 cp_parser_set_decl_spec_type (decl_specs
,
14352 /*type_definition_p=*/true);
14356 /* Fall through. */
14357 elaborated_type_specifier
:
14358 /* We're declaring (not defining) a class or enum. */
14359 if (declares_class_or_enum
)
14360 *declares_class_or_enum
= 1;
14362 /* Fall through. */
14364 /* Look for an elaborated-type-specifier. */
14366 = (cp_parser_elaborated_type_specifier
14368 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14371 cp_parser_set_decl_spec_type (decl_specs
,
14374 /*type_definition_p=*/false);
14379 if (is_cv_qualifier
)
14380 *is_cv_qualifier
= true;
14385 if (is_cv_qualifier
)
14386 *is_cv_qualifier
= true;
14391 if (is_cv_qualifier
)
14392 *is_cv_qualifier
= true;
14396 /* The `__complex__' keyword is a GNU extension. */
14404 /* Handle simple keywords. */
14409 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14410 decl_specs
->any_specifiers_p
= true;
14412 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14415 /* If we do not already have a type-specifier, assume we are looking
14416 at a simple-type-specifier. */
14417 type_spec
= cp_parser_simple_type_specifier (parser
,
14421 /* If we didn't find a type-specifier, and a type-specifier was not
14422 optional in this context, issue an error message. */
14423 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14425 cp_parser_error (parser
, "expected type specifier");
14426 return error_mark_node
;
14432 /* Parse a simple-type-specifier.
14434 simple-type-specifier:
14435 :: [opt] nested-name-specifier [opt] type-name
14436 :: [opt] nested-name-specifier template template-id
14451 simple-type-specifier:
14453 decltype ( expression )
14456 __underlying_type ( type-id )
14460 simple-type-specifier:
14462 __typeof__ unary-expression
14463 __typeof__ ( type-id )
14464 __typeof__ ( type-id ) { initializer-list , [opt] }
14466 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14467 appropriately updated. */
14470 cp_parser_simple_type_specifier (cp_parser
* parser
,
14471 cp_decl_specifier_seq
*decl_specs
,
14472 cp_parser_flags flags
)
14474 tree type
= NULL_TREE
;
14477 /* Peek at the next token. */
14478 token
= cp_lexer_peek_token (parser
->lexer
);
14480 /* If we're looking at a keyword, things are easy. */
14481 switch (token
->keyword
)
14485 decl_specs
->explicit_char_p
= true;
14486 type
= char_type_node
;
14489 type
= char16_type_node
;
14492 type
= char32_type_node
;
14495 type
= wchar_type_node
;
14498 type
= boolean_type_node
;
14501 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14502 type
= short_integer_type_node
;
14506 decl_specs
->explicit_int_p
= true;
14507 type
= integer_type_node
;
14510 if (!int128_integer_type_node
)
14513 decl_specs
->explicit_int128_p
= true;
14514 type
= int128_integer_type_node
;
14518 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14519 type
= long_integer_type_node
;
14522 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14523 type
= integer_type_node
;
14526 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14527 type
= unsigned_type_node
;
14530 type
= float_type_node
;
14533 type
= double_type_node
;
14536 type
= void_type_node
;
14540 maybe_warn_cpp0x (CPP0X_AUTO
);
14541 if (parser
->auto_is_implicit_function_template_parm_p
)
14543 type
= synthesize_implicit_template_parm (parser
);
14545 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14547 if (cxx_dialect
< cxx1y
)
14548 pedwarn (location_of (type
), 0,
14549 "use of %<auto%> in lambda parameter declaration "
14550 "only available with "
14551 "-std=c++1y or -std=gnu++1y");
14553 else if (cxx_dialect
< cxx1y
)
14554 pedwarn (location_of (type
), 0,
14555 "use of %<auto%> in parameter declaration "
14556 "only available with "
14557 "-std=c++1y or -std=gnu++1y");
14559 pedwarn (location_of (type
), OPT_Wpedantic
,
14560 "ISO C++ forbids use of %<auto%> in parameter "
14564 type
= make_auto ();
14568 /* Since DR 743, decltype can either be a simple-type-specifier by
14569 itself or begin a nested-name-specifier. Parsing it will replace
14570 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14571 handling below decide what to do. */
14572 cp_parser_decltype (parser
);
14573 cp_lexer_set_token_position (parser
->lexer
, token
);
14577 /* Consume the `typeof' token. */
14578 cp_lexer_consume_token (parser
->lexer
);
14579 /* Parse the operand to `typeof'. */
14580 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14581 /* If it is not already a TYPE, take its type. */
14582 if (!TYPE_P (type
))
14583 type
= finish_typeof (type
);
14586 cp_parser_set_decl_spec_type (decl_specs
, type
,
14588 /*type_definition_p=*/false);
14592 case RID_UNDERLYING_TYPE
:
14593 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14595 cp_parser_set_decl_spec_type (decl_specs
, type
,
14597 /*type_definition_p=*/false);
14602 case RID_DIRECT_BASES
:
14603 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14605 cp_parser_set_decl_spec_type (decl_specs
, type
,
14607 /*type_definition_p=*/false);
14613 /* If token is an already-parsed decltype not followed by ::,
14614 it's a simple-type-specifier. */
14615 if (token
->type
== CPP_DECLTYPE
14616 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
14618 type
= token
->u
.value
;
14620 cp_parser_set_decl_spec_type (decl_specs
, type
,
14622 /*type_definition_p=*/false);
14623 cp_lexer_consume_token (parser
->lexer
);
14627 /* If the type-specifier was for a built-in type, we're done. */
14630 /* Record the type. */
14632 && (token
->keyword
!= RID_SIGNED
14633 && token
->keyword
!= RID_UNSIGNED
14634 && token
->keyword
!= RID_SHORT
14635 && token
->keyword
!= RID_LONG
))
14636 cp_parser_set_decl_spec_type (decl_specs
,
14639 /*type_definition_p=*/false);
14641 decl_specs
->any_specifiers_p
= true;
14643 /* Consume the token. */
14644 cp_lexer_consume_token (parser
->lexer
);
14646 /* There is no valid C++ program where a non-template type is
14647 followed by a "<". That usually indicates that the user thought
14648 that the type was a template. */
14649 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
14652 return TYPE_NAME (type
);
14655 /* The type-specifier must be a user-defined type. */
14656 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
14661 /* Don't gobble tokens or issue error messages if this is an
14662 optional type-specifier. */
14663 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
14664 cp_parser_parse_tentatively (parser
);
14666 /* Look for the optional `::' operator. */
14668 = (cp_parser_global_scope_opt (parser
,
14669 /*current_scope_valid_p=*/false)
14671 /* Look for the nested-name specifier. */
14673 = (cp_parser_nested_name_specifier_opt (parser
,
14674 /*typename_keyword_p=*/false,
14675 /*check_dependency_p=*/true,
14677 /*is_declaration=*/false)
14679 token
= cp_lexer_peek_token (parser
->lexer
);
14680 /* If we have seen a nested-name-specifier, and the next token
14681 is `template', then we are using the template-id production. */
14683 && cp_parser_optional_template_keyword (parser
))
14685 /* Look for the template-id. */
14686 type
= cp_parser_template_id (parser
,
14687 /*template_keyword_p=*/true,
14688 /*check_dependency_p=*/true,
14690 /*is_declaration=*/false);
14691 /* If the template-id did not name a type, we are out of
14693 if (TREE_CODE (type
) != TYPE_DECL
)
14695 cp_parser_error (parser
, "expected template-id for type");
14699 /* Otherwise, look for a type-name. */
14701 type
= cp_parser_type_name (parser
);
14702 /* Keep track of all name-lookups performed in class scopes. */
14706 && TREE_CODE (type
) == TYPE_DECL
14707 && identifier_p (DECL_NAME (type
)))
14708 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
14709 /* If it didn't work out, we don't have a TYPE. */
14710 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
14711 && !cp_parser_parse_definitely (parser
))
14713 if (type
&& decl_specs
)
14714 cp_parser_set_decl_spec_type (decl_specs
, type
,
14716 /*type_definition_p=*/false);
14719 /* If we didn't get a type-name, issue an error message. */
14720 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14722 cp_parser_error (parser
, "expected type-name");
14723 return error_mark_node
;
14726 if (type
&& type
!= error_mark_node
)
14728 /* See if TYPE is an Objective-C type, and if so, parse and
14729 accept any protocol references following it. Do this before
14730 the cp_parser_check_for_invalid_template_id() call, because
14731 Objective-C types can be followed by '<...>' which would
14732 enclose protocol names rather than template arguments, and so
14733 everything is fine. */
14734 if (c_dialect_objc () && !parser
->scope
14735 && (objc_is_id (type
) || objc_is_class_name (type
)))
14737 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14738 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
14740 /* Clobber the "unqualified" type previously entered into
14741 DECL_SPECS with the new, improved protocol-qualified version. */
14743 decl_specs
->type
= qual_type
;
14748 /* There is no valid C++ program where a non-template type is
14749 followed by a "<". That usually indicates that the user
14750 thought that the type was a template. */
14751 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
14759 /* Parse a type-name.
14765 simple-template-id [in c++0x]
14773 Returns a TYPE_DECL for the type. */
14776 cp_parser_type_name (cp_parser
* parser
)
14780 /* We can't know yet whether it is a class-name or not. */
14781 cp_parser_parse_tentatively (parser
);
14782 /* Try a class-name. */
14783 type_decl
= cp_parser_class_name (parser
,
14784 /*typename_keyword_p=*/false,
14785 /*template_keyword_p=*/false,
14787 /*check_dependency_p=*/true,
14788 /*class_head_p=*/false,
14789 /*is_declaration=*/false);
14790 /* If it's not a class-name, keep looking. */
14791 if (!cp_parser_parse_definitely (parser
))
14793 if (cxx_dialect
< cxx11
)
14794 /* It must be a typedef-name or an enum-name. */
14795 return cp_parser_nonclass_name (parser
);
14797 cp_parser_parse_tentatively (parser
);
14798 /* It is either a simple-template-id representing an
14799 instantiation of an alias template... */
14800 type_decl
= cp_parser_template_id (parser
,
14801 /*template_keyword_p=*/false,
14802 /*check_dependency_p=*/true,
14804 /*is_declaration=*/false);
14805 /* Note that this must be an instantiation of an alias template
14806 because [temp.names]/6 says:
14808 A template-id that names an alias template specialization
14811 Whereas [temp.names]/7 says:
14813 A simple-template-id that names a class template
14814 specialization is a class-name. */
14815 if (type_decl
!= NULL_TREE
14816 && TREE_CODE (type_decl
) == TYPE_DECL
14817 && TYPE_DECL_ALIAS_P (type_decl
))
14818 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
14820 cp_parser_simulate_error (parser
);
14822 if (!cp_parser_parse_definitely (parser
))
14823 /* ... Or a typedef-name or an enum-name. */
14824 return cp_parser_nonclass_name (parser
);
14830 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14838 Returns a TYPE_DECL for the type. */
14841 cp_parser_nonclass_name (cp_parser
* parser
)
14846 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14847 identifier
= cp_parser_identifier (parser
);
14848 if (identifier
== error_mark_node
)
14849 return error_mark_node
;
14851 /* Look up the type-name. */
14852 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
14854 type_decl
= strip_using_decl (type_decl
);
14856 if (TREE_CODE (type_decl
) != TYPE_DECL
14857 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
14859 /* See if this is an Objective-C type. */
14860 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14861 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
14863 type_decl
= TYPE_NAME (type
);
14866 /* Issue an error if we did not find a type-name. */
14867 if (TREE_CODE (type_decl
) != TYPE_DECL
14868 /* In Objective-C, we have the complication that class names are
14869 normally type names and start declarations (eg, the
14870 "NSObject" in "NSObject *object;"), but can be used in an
14871 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14872 is an expression. So, a classname followed by a dot is not a
14873 valid type-name. */
14874 || (objc_is_class_name (TREE_TYPE (type_decl
))
14875 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
14877 if (!cp_parser_simulate_error (parser
))
14878 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
14879 NLE_TYPE
, token
->location
);
14880 return error_mark_node
;
14882 /* Remember that the name was used in the definition of the
14883 current class so that we can check later to see if the
14884 meaning would have been different after the class was
14885 entirely defined. */
14886 else if (type_decl
!= error_mark_node
14888 maybe_note_name_used_in_class (identifier
, type_decl
);
14893 /* Parse an elaborated-type-specifier. Note that the grammar given
14894 here incorporates the resolution to DR68.
14896 elaborated-type-specifier:
14897 class-key :: [opt] nested-name-specifier [opt] identifier
14898 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14899 enum-key :: [opt] nested-name-specifier [opt] identifier
14900 typename :: [opt] nested-name-specifier identifier
14901 typename :: [opt] nested-name-specifier template [opt]
14906 elaborated-type-specifier:
14907 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14908 class-key attributes :: [opt] nested-name-specifier [opt]
14909 template [opt] template-id
14910 enum attributes :: [opt] nested-name-specifier [opt] identifier
14912 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14913 declared `friend'. If IS_DECLARATION is TRUE, then this
14914 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14915 something is being declared.
14917 Returns the TYPE specified. */
14920 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
14922 bool is_declaration
)
14924 enum tag_types tag_type
;
14926 tree type
= NULL_TREE
;
14927 tree attributes
= NULL_TREE
;
14929 cp_token
*token
= NULL
;
14931 /* See if we're looking at the `enum' keyword. */
14932 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
14934 /* Consume the `enum' token. */
14935 cp_lexer_consume_token (parser
->lexer
);
14936 /* Remember that it's an enumeration type. */
14937 tag_type
= enum_type
;
14938 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14939 enums) is used here. */
14940 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14941 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14943 pedwarn (input_location
, 0, "elaborated-type-specifier "
14944 "for a scoped enum must not use the %<%D%> keyword",
14945 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
14946 /* Consume the `struct' or `class' and parse it anyway. */
14947 cp_lexer_consume_token (parser
->lexer
);
14949 /* Parse the attributes. */
14950 attributes
= cp_parser_attributes_opt (parser
);
14952 /* Or, it might be `typename'. */
14953 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14956 /* Consume the `typename' token. */
14957 cp_lexer_consume_token (parser
->lexer
);
14958 /* Remember that it's a `typename' type. */
14959 tag_type
= typename_type
;
14961 /* Otherwise it must be a class-key. */
14964 tag_type
= cp_parser_class_key (parser
);
14965 if (tag_type
== none_type
)
14966 return error_mark_node
;
14967 /* Parse the attributes. */
14968 attributes
= cp_parser_attributes_opt (parser
);
14971 /* Look for the `::' operator. */
14972 globalscope
= cp_parser_global_scope_opt (parser
,
14973 /*current_scope_valid_p=*/false);
14974 /* Look for the nested-name-specifier. */
14975 if (tag_type
== typename_type
&& !globalscope
)
14977 if (!cp_parser_nested_name_specifier (parser
,
14978 /*typename_keyword_p=*/true,
14979 /*check_dependency_p=*/true,
14982 return error_mark_node
;
14985 /* Even though `typename' is not present, the proposed resolution
14986 to Core Issue 180 says that in `class A<T>::B', `B' should be
14987 considered a type-name, even if `A<T>' is dependent. */
14988 cp_parser_nested_name_specifier_opt (parser
,
14989 /*typename_keyword_p=*/true,
14990 /*check_dependency_p=*/true,
14993 /* For everything but enumeration types, consider a template-id.
14994 For an enumeration type, consider only a plain identifier. */
14995 if (tag_type
!= enum_type
)
14997 bool template_p
= false;
15000 /* Allow the `template' keyword. */
15001 template_p
= cp_parser_optional_template_keyword (parser
);
15002 /* If we didn't see `template', we don't know if there's a
15003 template-id or not. */
15005 cp_parser_parse_tentatively (parser
);
15006 /* Parse the template-id. */
15007 token
= cp_lexer_peek_token (parser
->lexer
);
15008 decl
= cp_parser_template_id (parser
, template_p
,
15009 /*check_dependency_p=*/true,
15012 /* If we didn't find a template-id, look for an ordinary
15014 if (!template_p
&& !cp_parser_parse_definitely (parser
))
15016 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15017 in effect, then we must assume that, upon instantiation, the
15018 template will correspond to a class. */
15019 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15020 && tag_type
== typename_type
)
15021 type
= make_typename_type (parser
->scope
, decl
,
15023 /*complain=*/tf_error
);
15024 /* If the `typename' keyword is in effect and DECL is not a type
15025 decl, then type is non existent. */
15026 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
15028 else if (TREE_CODE (decl
) == TYPE_DECL
)
15029 type
= check_elaborated_type_specifier (tag_type
, decl
,
15030 /*allow_template_p=*/true);
15031 else if (decl
== error_mark_node
)
15032 type
= error_mark_node
;
15037 token
= cp_lexer_peek_token (parser
->lexer
);
15038 identifier
= cp_parser_identifier (parser
);
15040 if (identifier
== error_mark_node
)
15042 parser
->scope
= NULL_TREE
;
15043 return error_mark_node
;
15046 /* For a `typename', we needn't call xref_tag. */
15047 if (tag_type
== typename_type
15048 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
15049 return cp_parser_make_typename_type (parser
, parser
->scope
,
15052 /* Look up a qualified name in the usual way. */
15056 tree ambiguous_decls
;
15058 decl
= cp_parser_lookup_name (parser
, identifier
,
15060 /*is_template=*/false,
15061 /*is_namespace=*/false,
15062 /*check_dependency=*/true,
15066 /* If the lookup was ambiguous, an error will already have been
15068 if (ambiguous_decls
)
15069 return error_mark_node
;
15071 /* If we are parsing friend declaration, DECL may be a
15072 TEMPLATE_DECL tree node here. However, we need to check
15073 whether this TEMPLATE_DECL results in valid code. Consider
15074 the following example:
15077 template <class T> class C {};
15080 template <class T> friend class N::C; // #1, valid code
15082 template <class T> class Y {
15083 friend class N::C; // #2, invalid code
15086 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15087 name lookup of `N::C'. We see that friend declaration must
15088 be template for the code to be valid. Note that
15089 processing_template_decl does not work here since it is
15090 always 1 for the above two cases. */
15092 decl
= (cp_parser_maybe_treat_template_as_class
15093 (decl
, /*tag_name_p=*/is_friend
15094 && parser
->num_template_parameter_lists
));
15096 if (TREE_CODE (decl
) != TYPE_DECL
)
15098 cp_parser_diagnose_invalid_type_name (parser
,
15102 return error_mark_node
;
15105 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15107 bool allow_template
= (parser
->num_template_parameter_lists
15108 || DECL_SELF_REFERENCE_P (decl
));
15109 type
= check_elaborated_type_specifier (tag_type
, decl
,
15112 if (type
== error_mark_node
)
15113 return error_mark_node
;
15116 /* Forward declarations of nested types, such as
15121 are invalid unless all components preceding the final '::'
15122 are complete. If all enclosing types are complete, these
15123 declarations become merely pointless.
15125 Invalid forward declarations of nested types are errors
15126 caught elsewhere in parsing. Those that are pointless arrive
15129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15130 && !is_friend
&& !processing_explicit_instantiation
)
15131 warning (0, "declaration %qD does not declare anything", decl
);
15133 type
= TREE_TYPE (decl
);
15137 /* An elaborated-type-specifier sometimes introduces a new type and
15138 sometimes names an existing type. Normally, the rule is that it
15139 introduces a new type only if there is not an existing type of
15140 the same name already in scope. For example, given:
15143 void f() { struct S s; }
15145 the `struct S' in the body of `f' is the same `struct S' as in
15146 the global scope; the existing definition is used. However, if
15147 there were no global declaration, this would introduce a new
15148 local class named `S'.
15150 An exception to this rule applies to the following code:
15152 namespace N { struct S; }
15154 Here, the elaborated-type-specifier names a new type
15155 unconditionally; even if there is already an `S' in the
15156 containing scope this declaration names a new type.
15157 This exception only applies if the elaborated-type-specifier
15158 forms the complete declaration:
15162 A declaration consisting solely of `class-key identifier ;' is
15163 either a redeclaration of the name in the current scope or a
15164 forward declaration of the identifier as a class name. It
15165 introduces the name into the current scope.
15167 We are in this situation precisely when the next token is a `;'.
15169 An exception to the exception is that a `friend' declaration does
15170 *not* name a new type; i.e., given:
15172 struct S { friend struct T; };
15174 `T' is not a new type in the scope of `S'.
15176 Also, `new struct S' or `sizeof (struct S)' never results in the
15177 definition of a new type; a new type can only be declared in a
15178 declaration context. */
15184 /* Friends have special name lookup rules. */
15185 ts
= ts_within_enclosing_non_class
;
15186 else if (is_declaration
15187 && cp_lexer_next_token_is (parser
->lexer
,
15189 /* This is a `class-key identifier ;' */
15195 (parser
->num_template_parameter_lists
15196 && (cp_parser_next_token_starts_class_definition_p (parser
)
15197 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15198 /* An unqualified name was used to reference this type, so
15199 there were no qualifying templates. */
15200 if (!cp_parser_check_template_parameters (parser
,
15201 /*num_templates=*/0,
15203 /*declarator=*/NULL
))
15204 return error_mark_node
;
15205 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15209 if (type
== error_mark_node
)
15210 return error_mark_node
;
15212 /* Allow attributes on forward declarations of classes. */
15215 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15216 warning (OPT_Wattributes
,
15217 "attributes ignored on uninstantiated type");
15218 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15219 && ! processing_explicit_instantiation
)
15220 warning (OPT_Wattributes
,
15221 "attributes ignored on template instantiation");
15222 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15223 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15225 warning (OPT_Wattributes
,
15226 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15229 if (tag_type
!= enum_type
)
15231 /* Indicate whether this class was declared as a `class' or as a
15233 if (TREE_CODE (type
) == RECORD_TYPE
)
15234 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15235 cp_parser_check_class_key (tag_type
, type
);
15238 /* A "<" cannot follow an elaborated type specifier. If that
15239 happens, the user was probably trying to form a template-id. */
15240 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15246 /* Parse an enum-specifier.
15249 enum-head { enumerator-list [opt] }
15250 enum-head { enumerator-list , } [C++0x]
15253 enum-key identifier [opt] enum-base [opt]
15254 enum-key nested-name-specifier identifier enum-base [opt]
15259 enum struct [C++0x]
15262 : type-specifier-seq
15264 opaque-enum-specifier:
15265 enum-key identifier enum-base [opt] ;
15268 enum-key attributes[opt] identifier [opt] enum-base [opt]
15269 { enumerator-list [opt] }attributes[opt]
15270 enum-key attributes[opt] identifier [opt] enum-base [opt]
15271 { enumerator-list, }attributes[opt] [C++0x]
15273 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15274 if the token stream isn't an enum-specifier after all. */
15277 cp_parser_enum_specifier (cp_parser
* parser
)
15280 tree type
= NULL_TREE
;
15282 tree nested_name_specifier
= NULL_TREE
;
15284 bool scoped_enum_p
= false;
15285 bool has_underlying_type
= false;
15286 bool nested_being_defined
= false;
15287 bool new_value_list
= false;
15288 bool is_new_type
= false;
15289 bool is_anonymous
= false;
15290 tree underlying_type
= NULL_TREE
;
15291 cp_token
*type_start_token
= NULL
;
15292 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15294 parser
->colon_corrects_to_scope_p
= false;
15296 /* Parse tentatively so that we can back up if we don't find a
15298 cp_parser_parse_tentatively (parser
);
15300 /* Caller guarantees that the current token is 'enum', an identifier
15301 possibly follows, and the token after that is an opening brace.
15302 If we don't have an identifier, fabricate an anonymous name for
15303 the enumeration being defined. */
15304 cp_lexer_consume_token (parser
->lexer
);
15306 /* Parse the "class" or "struct", which indicates a scoped
15307 enumeration type in C++0x. */
15308 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15309 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15311 if (cxx_dialect
< cxx11
)
15312 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15314 /* Consume the `struct' or `class' token. */
15315 cp_lexer_consume_token (parser
->lexer
);
15317 scoped_enum_p
= true;
15320 attributes
= cp_parser_attributes_opt (parser
);
15322 /* Clear the qualification. */
15323 parser
->scope
= NULL_TREE
;
15324 parser
->qualifying_scope
= NULL_TREE
;
15325 parser
->object_scope
= NULL_TREE
;
15327 /* Figure out in what scope the declaration is being placed. */
15328 prev_scope
= current_scope ();
15330 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15332 push_deferring_access_checks (dk_no_check
);
15333 nested_name_specifier
15334 = cp_parser_nested_name_specifier_opt (parser
,
15335 /*typename_keyword_p=*/true,
15336 /*check_dependency_p=*/false,
15338 /*is_declaration=*/false);
15340 if (nested_name_specifier
)
15344 identifier
= cp_parser_identifier (parser
);
15345 name
= cp_parser_lookup_name (parser
, identifier
,
15347 /*is_template=*/false,
15348 /*is_namespace=*/false,
15349 /*check_dependency=*/true,
15350 /*ambiguous_decls=*/NULL
,
15352 if (name
&& name
!= error_mark_node
)
15354 type
= TREE_TYPE (name
);
15355 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15357 /* Are template enums allowed in ISO? */
15358 if (template_parm_scope_p ())
15359 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15360 "%qD is an enumeration template", name
);
15361 /* ignore a typename reference, for it will be solved by name
15366 else if (nested_name_specifier
== error_mark_node
)
15367 /* We already issued an error. */;
15369 error_at (type_start_token
->location
,
15370 "%qD is not an enumerator-name", identifier
);
15374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15375 identifier
= cp_parser_identifier (parser
);
15378 identifier
= make_anon_name ();
15379 is_anonymous
= true;
15381 error_at (type_start_token
->location
,
15382 "anonymous scoped enum is not allowed");
15385 pop_deferring_access_checks ();
15387 /* Check for the `:' that denotes a specified underlying type in C++0x.
15388 Note that a ':' could also indicate a bitfield width, however. */
15389 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15391 cp_decl_specifier_seq type_specifiers
;
15393 /* Consume the `:'. */
15394 cp_lexer_consume_token (parser
->lexer
);
15396 /* Parse the type-specifier-seq. */
15397 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15398 /*is_trailing_return=*/false,
15401 /* At this point this is surely not elaborated type specifier. */
15402 if (!cp_parser_parse_definitely (parser
))
15405 if (cxx_dialect
< cxx11
)
15406 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15408 has_underlying_type
= true;
15410 /* If that didn't work, stop. */
15411 if (type_specifiers
.type
!= error_mark_node
)
15413 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15414 /*initialized=*/0, NULL
);
15415 if (underlying_type
== error_mark_node
15416 || check_for_bare_parameter_packs (underlying_type
))
15417 underlying_type
= NULL_TREE
;
15421 /* Look for the `{' but don't consume it yet. */
15422 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15424 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15426 cp_parser_error (parser
, "expected %<{%>");
15427 if (has_underlying_type
)
15433 /* An opaque-enum-specifier must have a ';' here. */
15434 if ((scoped_enum_p
|| underlying_type
)
15435 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15437 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15438 if (has_underlying_type
)
15446 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15449 if (nested_name_specifier
)
15451 if (CLASS_TYPE_P (nested_name_specifier
))
15453 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15454 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15455 push_scope (nested_name_specifier
);
15457 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15459 push_nested_namespace (nested_name_specifier
);
15463 /* Issue an error message if type-definitions are forbidden here. */
15464 if (!cp_parser_check_type_definition (parser
))
15465 type
= error_mark_node
;
15467 /* Create the new type. We do this before consuming the opening
15468 brace so the enum will be recorded as being on the line of its
15469 tag (or the 'enum' keyword, if there is no tag). */
15470 type
= start_enum (identifier
, type
, underlying_type
,
15471 scoped_enum_p
, &is_new_type
);
15473 /* If the next token is not '{' it is an opaque-enum-specifier or an
15474 elaborated-type-specifier. */
15475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15477 timevar_push (TV_PARSE_ENUM
);
15478 if (nested_name_specifier
15479 && nested_name_specifier
!= error_mark_node
)
15481 /* The following catches invalid code such as:
15482 enum class S<int>::E { A, B, C }; */
15483 if (!processing_specialization
15484 && CLASS_TYPE_P (nested_name_specifier
)
15485 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15486 error_at (type_start_token
->location
, "cannot add an enumerator "
15487 "list to a template instantiation");
15489 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
15491 error_at (type_start_token
->location
,
15492 "%<%T::%E%> has not been declared",
15493 TYPE_CONTEXT (nested_name_specifier
),
15494 nested_name_specifier
);
15495 type
= error_mark_node
;
15497 /* If that scope does not contain the scope in which the
15498 class was originally declared, the program is invalid. */
15499 else if (prev_scope
&& !is_ancestor (prev_scope
,
15500 nested_name_specifier
))
15502 if (at_namespace_scope_p ())
15503 error_at (type_start_token
->location
,
15504 "declaration of %qD in namespace %qD which does not "
15506 type
, prev_scope
, nested_name_specifier
);
15508 error_at (type_start_token
->location
,
15509 "declaration of %qD in %qD which does not "
15511 type
, prev_scope
, nested_name_specifier
);
15512 type
= error_mark_node
;
15517 begin_scope (sk_scoped_enum
, type
);
15519 /* Consume the opening brace. */
15520 cp_lexer_consume_token (parser
->lexer
);
15522 if (type
== error_mark_node
)
15523 ; /* Nothing to add */
15524 else if (OPAQUE_ENUM_P (type
)
15525 || (cxx_dialect
> cxx98
&& processing_specialization
))
15527 new_value_list
= true;
15528 SET_OPAQUE_ENUM_P (type
, false);
15529 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15533 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
15534 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15535 "previous definition here");
15536 type
= error_mark_node
;
15539 if (type
== error_mark_node
)
15540 cp_parser_skip_to_end_of_block_or_statement (parser
);
15541 /* If the next token is not '}', then there are some enumerators. */
15542 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15544 if (is_anonymous
&& !scoped_enum_p
)
15545 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15546 "ISO C++ forbids empty anonymous enum");
15549 cp_parser_enumerator_list (parser
, type
);
15551 /* Consume the final '}'. */
15552 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15556 timevar_pop (TV_PARSE_ENUM
);
15560 /* If a ';' follows, then it is an opaque-enum-specifier
15561 and additional restrictions apply. */
15562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15565 error_at (type_start_token
->location
,
15566 "opaque-enum-specifier without name");
15567 else if (nested_name_specifier
)
15568 error_at (type_start_token
->location
,
15569 "opaque-enum-specifier must use a simple identifier");
15573 /* Look for trailing attributes to apply to this enumeration, and
15574 apply them if appropriate. */
15575 if (cp_parser_allow_gnu_extensions_p (parser
))
15577 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15578 trailing_attr
= chainon (trailing_attr
, attributes
);
15579 cplus_decl_attributes (&type
,
15581 (int) ATTR_FLAG_TYPE_IN_PLACE
);
15584 /* Finish up the enumeration. */
15585 if (type
!= error_mark_node
)
15587 if (new_value_list
)
15588 finish_enum_value_list (type
);
15590 finish_enum (type
);
15593 if (nested_name_specifier
)
15595 if (CLASS_TYPE_P (nested_name_specifier
))
15597 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
15598 pop_scope (nested_name_specifier
);
15600 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15602 pop_nested_namespace (nested_name_specifier
);
15606 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
15610 /* Parse an enumerator-list. The enumerators all have the indicated
15614 enumerator-definition
15615 enumerator-list , enumerator-definition */
15618 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
15622 /* Parse an enumerator-definition. */
15623 cp_parser_enumerator_definition (parser
, type
);
15625 /* If the next token is not a ',', we've reached the end of
15627 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15629 /* Otherwise, consume the `,' and keep going. */
15630 cp_lexer_consume_token (parser
->lexer
);
15631 /* If the next token is a `}', there is a trailing comma. */
15632 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15634 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
15635 pedwarn (input_location
, OPT_Wpedantic
,
15636 "comma at end of enumerator list");
15642 /* Parse an enumerator-definition. The enumerator has the indicated
15645 enumerator-definition:
15647 enumerator = constant-expression
15653 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
15659 /* Save the input location because we are interested in the location
15660 of the identifier and not the location of the explicit value. */
15661 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15663 /* Look for the identifier. */
15664 identifier
= cp_parser_identifier (parser
);
15665 if (identifier
== error_mark_node
)
15668 /* If the next token is an '=', then there is an explicit value. */
15669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15671 /* Consume the `=' token. */
15672 cp_lexer_consume_token (parser
->lexer
);
15673 /* Parse the value. */
15674 value
= cp_parser_constant_expression (parser
,
15675 /*allow_non_constant_p=*/false,
15681 /* If we are processing a template, make sure the initializer of the
15682 enumerator doesn't contain any bare template parameter pack. */
15683 if (check_for_bare_parameter_packs (value
))
15684 value
= error_mark_node
;
15686 /* integral_constant_value will pull out this expression, so make sure
15687 it's folded as appropriate. */
15688 value
= fold_non_dependent_expr (value
);
15690 /* Create the enumerator. */
15691 build_enumerator (identifier
, value
, type
, loc
);
15694 /* Parse a namespace-name.
15697 original-namespace-name
15700 Returns the NAMESPACE_DECL for the namespace. */
15703 cp_parser_namespace_name (cp_parser
* parser
)
15706 tree namespace_decl
;
15708 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15710 /* Get the name of the namespace. */
15711 identifier
= cp_parser_identifier (parser
);
15712 if (identifier
== error_mark_node
)
15713 return error_mark_node
;
15715 /* Look up the identifier in the currently active scope. Look only
15716 for namespaces, due to:
15718 [basic.lookup.udir]
15720 When looking up a namespace-name in a using-directive or alias
15721 definition, only namespace names are considered.
15725 [basic.lookup.qual]
15727 During the lookup of a name preceding the :: scope resolution
15728 operator, object, function, and enumerator names are ignored.
15730 (Note that cp_parser_qualifying_entity only calls this
15731 function if the token after the name is the scope resolution
15733 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
15735 /*is_template=*/false,
15736 /*is_namespace=*/true,
15737 /*check_dependency=*/true,
15738 /*ambiguous_decls=*/NULL
,
15740 /* If it's not a namespace, issue an error. */
15741 if (namespace_decl
== error_mark_node
15742 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
15744 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15745 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
15746 cp_parser_error (parser
, "expected namespace-name");
15747 namespace_decl
= error_mark_node
;
15750 return namespace_decl
;
15753 /* Parse a namespace-definition.
15755 namespace-definition:
15756 named-namespace-definition
15757 unnamed-namespace-definition
15759 named-namespace-definition:
15760 original-namespace-definition
15761 extension-namespace-definition
15763 original-namespace-definition:
15764 namespace identifier { namespace-body }
15766 extension-namespace-definition:
15767 namespace original-namespace-name { namespace-body }
15769 unnamed-namespace-definition:
15770 namespace { namespace-body } */
15773 cp_parser_namespace_definition (cp_parser
* parser
)
15775 tree identifier
, attribs
;
15776 bool has_visibility
;
15779 cp_ensure_no_omp_declare_simd (parser
);
15780 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
15782 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
15784 cp_lexer_consume_token (parser
->lexer
);
15789 /* Look for the `namespace' keyword. */
15790 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15792 /* Get the name of the namespace. We do not attempt to distinguish
15793 between an original-namespace-definition and an
15794 extension-namespace-definition at this point. The semantic
15795 analysis routines are responsible for that. */
15796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15797 identifier
= cp_parser_identifier (parser
);
15799 identifier
= NULL_TREE
;
15801 /* Parse any specified attributes. */
15802 attribs
= cp_parser_attributes_opt (parser
);
15804 /* Look for the `{' to start the namespace. */
15805 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
15806 /* Start the namespace. */
15807 push_namespace (identifier
);
15809 /* "inline namespace" is equivalent to a stub namespace definition
15810 followed by a strong using directive. */
15813 tree name_space
= current_namespace
;
15814 /* Set up namespace association. */
15815 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
15816 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
15817 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
15818 /* Import the contents of the inline namespace. */
15820 do_using_directive (name_space
);
15821 push_namespace (identifier
);
15824 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
15826 /* Parse the body of the namespace. */
15827 cp_parser_namespace_body (parser
);
15829 if (has_visibility
)
15830 pop_visibility (1);
15832 /* Finish the namespace. */
15834 /* Look for the final `}'. */
15835 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15838 /* Parse a namespace-body.
15841 declaration-seq [opt] */
15844 cp_parser_namespace_body (cp_parser
* parser
)
15846 cp_parser_declaration_seq_opt (parser
);
15849 /* Parse a namespace-alias-definition.
15851 namespace-alias-definition:
15852 namespace identifier = qualified-namespace-specifier ; */
15855 cp_parser_namespace_alias_definition (cp_parser
* parser
)
15858 tree namespace_specifier
;
15860 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15862 /* Look for the `namespace' keyword. */
15863 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15864 /* Look for the identifier. */
15865 identifier
= cp_parser_identifier (parser
);
15866 if (identifier
== error_mark_node
)
15868 /* Look for the `=' token. */
15869 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
15870 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15872 error_at (token
->location
, "%<namespace%> definition is not allowed here");
15873 /* Skip the definition. */
15874 cp_lexer_consume_token (parser
->lexer
);
15875 if (cp_parser_skip_to_closing_brace (parser
))
15876 cp_lexer_consume_token (parser
->lexer
);
15879 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15880 /* Look for the qualified-namespace-specifier. */
15881 namespace_specifier
15882 = cp_parser_qualified_namespace_specifier (parser
);
15883 /* Look for the `;' token. */
15884 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15886 /* Register the alias in the symbol table. */
15887 do_namespace_alias (identifier
, namespace_specifier
);
15890 /* Parse a qualified-namespace-specifier.
15892 qualified-namespace-specifier:
15893 :: [opt] nested-name-specifier [opt] namespace-name
15895 Returns a NAMESPACE_DECL corresponding to the specified
15899 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
15901 /* Look for the optional `::'. */
15902 cp_parser_global_scope_opt (parser
,
15903 /*current_scope_valid_p=*/false);
15905 /* Look for the optional nested-name-specifier. */
15906 cp_parser_nested_name_specifier_opt (parser
,
15907 /*typename_keyword_p=*/false,
15908 /*check_dependency_p=*/true,
15910 /*is_declaration=*/true);
15912 return cp_parser_namespace_name (parser
);
15915 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15916 access declaration.
15919 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15920 using :: unqualified-id ;
15922 access-declaration:
15928 cp_parser_using_declaration (cp_parser
* parser
,
15929 bool access_declaration_p
)
15932 bool typename_p
= false;
15933 bool global_scope_p
;
15937 int oldcount
= errorcount
;
15938 cp_token
*diag_token
= NULL
;
15940 if (access_declaration_p
)
15942 diag_token
= cp_lexer_peek_token (parser
->lexer
);
15943 cp_parser_parse_tentatively (parser
);
15947 /* Look for the `using' keyword. */
15948 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15950 /* Peek at the next token. */
15951 token
= cp_lexer_peek_token (parser
->lexer
);
15952 /* See if it's `typename'. */
15953 if (token
->keyword
== RID_TYPENAME
)
15955 /* Remember that we've seen it. */
15957 /* Consume the `typename' token. */
15958 cp_lexer_consume_token (parser
->lexer
);
15962 /* Look for the optional global scope qualification. */
15964 = (cp_parser_global_scope_opt (parser
,
15965 /*current_scope_valid_p=*/false)
15968 /* If we saw `typename', or didn't see `::', then there must be a
15969 nested-name-specifier present. */
15970 if (typename_p
|| !global_scope_p
)
15972 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
15973 /*check_dependency_p=*/true,
15975 /*is_declaration=*/true);
15976 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
15978 cp_parser_skip_to_end_of_block_or_statement (parser
);
15982 /* Otherwise, we could be in either of the two productions. In that
15983 case, treat the nested-name-specifier as optional. */
15985 qscope
= cp_parser_nested_name_specifier_opt (parser
,
15986 /*typename_keyword_p=*/false,
15987 /*check_dependency_p=*/true,
15989 /*is_declaration=*/true);
15991 qscope
= global_namespace
;
15993 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
15994 /* Something has already gone wrong; there's no need to parse
15995 further. Since an error has occurred, the return value of
15996 cp_parser_parse_definitely will be false, as required. */
15997 return cp_parser_parse_definitely (parser
);
15999 token
= cp_lexer_peek_token (parser
->lexer
);
16000 /* Parse the unqualified-id. */
16001 identifier
= cp_parser_unqualified_id (parser
,
16002 /*template_keyword_p=*/false,
16003 /*check_dependency_p=*/true,
16004 /*declarator_p=*/true,
16005 /*optional_p=*/false);
16007 if (access_declaration_p
)
16009 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16010 cp_parser_simulate_error (parser
);
16011 if (!cp_parser_parse_definitely (parser
))
16015 /* The function we call to handle a using-declaration is different
16016 depending on what scope we are in. */
16017 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
16019 else if (!identifier_p (identifier
)
16020 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
16021 /* [namespace.udecl]
16023 A using declaration shall not name a template-id. */
16024 error_at (token
->location
,
16025 "a template-id may not appear in a using-declaration");
16028 if (at_class_scope_p ())
16030 /* Create the USING_DECL. */
16031 decl
= do_class_using_decl (parser
->scope
, identifier
);
16033 if (decl
&& typename_p
)
16034 USING_DECL_TYPENAME_P (decl
) = 1;
16036 if (check_for_bare_parameter_packs (decl
))
16038 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16042 /* Add it to the list of members in this class. */
16043 finish_member_declaration (decl
);
16047 decl
= cp_parser_lookup_name_simple (parser
,
16050 if (decl
== error_mark_node
)
16051 cp_parser_name_lookup_error (parser
, identifier
,
16054 else if (check_for_bare_parameter_packs (decl
))
16056 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16059 else if (!at_namespace_scope_p ())
16060 do_local_using_decl (decl
, qscope
, identifier
);
16062 do_toplevel_using_decl (decl
, qscope
, identifier
);
16066 /* Look for the final `;'. */
16067 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16069 if (access_declaration_p
&& errorcount
== oldcount
)
16070 warning_at (diag_token
->location
, OPT_Wdeprecated
,
16071 "access declarations are deprecated "
16072 "in favour of using-declarations; "
16073 "suggestion: add the %<using%> keyword");
16078 /* Parse an alias-declaration.
16081 using identifier attribute-specifier-seq [opt] = type-id */
16084 cp_parser_alias_declaration (cp_parser
* parser
)
16086 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
16087 location_t id_location
;
16088 cp_declarator
*declarator
;
16089 cp_decl_specifier_seq decl_specs
;
16091 const char *saved_message
= NULL
;
16093 /* Look for the `using' keyword. */
16094 cp_token
*using_token
16095 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16096 if (using_token
== NULL
)
16097 return error_mark_node
;
16099 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
16100 id
= cp_parser_identifier (parser
);
16101 if (id
== error_mark_node
)
16102 return error_mark_node
;
16104 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
16105 attributes
= cp_parser_attributes_opt (parser
);
16106 if (attributes
== error_mark_node
)
16107 return error_mark_node
;
16109 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16111 if (cp_parser_error_occurred (parser
))
16112 return error_mark_node
;
16114 cp_parser_commit_to_tentative_parse (parser
);
16116 /* Now we are going to parse the type-id of the declaration. */
16121 "A type-specifier-seq shall not define a class or enumeration
16122 unless it appears in the type-id of an alias-declaration (7.1.3) that
16123 is not the declaration of a template-declaration."
16125 In other words, if we currently are in an alias template, the
16126 type-id should not define a type.
16128 So let's set parser->type_definition_forbidden_message in that
16129 case; cp_parser_check_type_definition (called by
16130 cp_parser_class_specifier) will then emit an error if a type is
16131 defined in the type-id. */
16132 if (parser
->num_template_parameter_lists
)
16134 saved_message
= parser
->type_definition_forbidden_message
;
16135 parser
->type_definition_forbidden_message
=
16136 G_("types may not be defined in alias template declarations");
16139 type
= cp_parser_type_id (parser
);
16141 /* Restore the error message if need be. */
16142 if (parser
->num_template_parameter_lists
)
16143 parser
->type_definition_forbidden_message
= saved_message
;
16145 if (type
== error_mark_node
)
16147 cp_parser_skip_to_end_of_block_or_statement (parser
);
16148 return error_mark_node
;
16151 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16153 if (cp_parser_error_occurred (parser
))
16155 cp_parser_skip_to_end_of_block_or_statement (parser
);
16156 return error_mark_node
;
16159 /* A typedef-name can also be introduced by an alias-declaration. The
16160 identifier following the using keyword becomes a typedef-name. It has
16161 the same semantics as if it were introduced by the typedef
16162 specifier. In particular, it does not define a new type and it shall
16163 not appear in the type-id. */
16165 clear_decl_specs (&decl_specs
);
16166 decl_specs
.type
= type
;
16167 if (attributes
!= NULL_TREE
)
16169 decl_specs
.attributes
= attributes
;
16170 set_and_check_decl_spec_loc (&decl_specs
,
16174 set_and_check_decl_spec_loc (&decl_specs
,
16177 set_and_check_decl_spec_loc (&decl_specs
,
16181 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16182 declarator
->id_loc
= id_location
;
16184 member_p
= at_class_scope_p ();
16186 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16187 NULL_TREE
, attributes
);
16189 decl
= start_decl (declarator
, &decl_specs
, 0,
16190 attributes
, NULL_TREE
, &pushed_scope
);
16191 if (decl
== error_mark_node
)
16194 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16197 pop_scope (pushed_scope
);
16199 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16200 added into the symbol table; otherwise, return the TYPE_DECL. */
16201 if (DECL_LANG_SPECIFIC (decl
)
16202 && DECL_TEMPLATE_INFO (decl
)
16203 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16205 decl
= DECL_TI_TEMPLATE (decl
);
16207 check_member_template (decl
);
16213 /* Parse a using-directive.
16216 using namespace :: [opt] nested-name-specifier [opt]
16217 namespace-name ; */
16220 cp_parser_using_directive (cp_parser
* parser
)
16222 tree namespace_decl
;
16225 /* Look for the `using' keyword. */
16226 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16227 /* And the `namespace' keyword. */
16228 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16229 /* Look for the optional `::' operator. */
16230 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16231 /* And the optional nested-name-specifier. */
16232 cp_parser_nested_name_specifier_opt (parser
,
16233 /*typename_keyword_p=*/false,
16234 /*check_dependency_p=*/true,
16236 /*is_declaration=*/true);
16237 /* Get the namespace being used. */
16238 namespace_decl
= cp_parser_namespace_name (parser
);
16239 /* And any specified attributes. */
16240 attribs
= cp_parser_attributes_opt (parser
);
16241 /* Update the symbol table. */
16242 parse_using_directive (namespace_decl
, attribs
);
16243 /* Look for the final `;'. */
16244 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16247 /* Parse an asm-definition.
16250 asm ( string-literal ) ;
16255 asm volatile [opt] ( string-literal ) ;
16256 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16257 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16258 : asm-operand-list [opt] ) ;
16259 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16260 : asm-operand-list [opt]
16261 : asm-clobber-list [opt] ) ;
16262 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16263 : asm-clobber-list [opt]
16264 : asm-goto-list ) ; */
16267 cp_parser_asm_definition (cp_parser
* parser
)
16270 tree outputs
= NULL_TREE
;
16271 tree inputs
= NULL_TREE
;
16272 tree clobbers
= NULL_TREE
;
16273 tree labels
= NULL_TREE
;
16275 bool volatile_p
= false;
16276 bool extended_p
= false;
16277 bool invalid_inputs_p
= false;
16278 bool invalid_outputs_p
= false;
16279 bool goto_p
= false;
16280 required_token missing
= RT_NONE
;
16282 /* Look for the `asm' keyword. */
16283 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16284 /* See if the next token is `volatile'. */
16285 if (cp_parser_allow_gnu_extensions_p (parser
)
16286 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16288 /* Remember that we saw the `volatile' keyword. */
16290 /* Consume the token. */
16291 cp_lexer_consume_token (parser
->lexer
);
16293 if (cp_parser_allow_gnu_extensions_p (parser
)
16294 && parser
->in_function_body
16295 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16297 /* Remember that we saw the `goto' keyword. */
16299 /* Consume the token. */
16300 cp_lexer_consume_token (parser
->lexer
);
16302 /* Look for the opening `('. */
16303 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16305 /* Look for the string. */
16306 string
= cp_parser_string_literal (parser
, false, false);
16307 if (string
== error_mark_node
)
16309 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16310 /*consume_paren=*/true);
16314 /* If we're allowing GNU extensions, check for the extended assembly
16315 syntax. Unfortunately, the `:' tokens need not be separated by
16316 a space in C, and so, for compatibility, we tolerate that here
16317 too. Doing that means that we have to treat the `::' operator as
16319 if (cp_parser_allow_gnu_extensions_p (parser
)
16320 && parser
->in_function_body
16321 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16322 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16324 bool inputs_p
= false;
16325 bool clobbers_p
= false;
16326 bool labels_p
= false;
16328 /* The extended syntax was used. */
16331 /* Look for outputs. */
16332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16334 /* Consume the `:'. */
16335 cp_lexer_consume_token (parser
->lexer
);
16336 /* Parse the output-operands. */
16337 if (cp_lexer_next_token_is_not (parser
->lexer
,
16339 && cp_lexer_next_token_is_not (parser
->lexer
,
16341 && cp_lexer_next_token_is_not (parser
->lexer
,
16344 outputs
= cp_parser_asm_operand_list (parser
);
16346 if (outputs
== error_mark_node
)
16347 invalid_outputs_p
= true;
16349 /* If the next token is `::', there are no outputs, and the
16350 next token is the beginning of the inputs. */
16351 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16352 /* The inputs are coming next. */
16355 /* Look for inputs. */
16357 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16359 /* Consume the `:' or `::'. */
16360 cp_lexer_consume_token (parser
->lexer
);
16361 /* Parse the output-operands. */
16362 if (cp_lexer_next_token_is_not (parser
->lexer
,
16364 && cp_lexer_next_token_is_not (parser
->lexer
,
16366 && cp_lexer_next_token_is_not (parser
->lexer
,
16368 inputs
= cp_parser_asm_operand_list (parser
);
16370 if (inputs
== error_mark_node
)
16371 invalid_inputs_p
= true;
16373 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16374 /* The clobbers are coming next. */
16377 /* Look for clobbers. */
16379 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16382 /* Consume the `:' or `::'. */
16383 cp_lexer_consume_token (parser
->lexer
);
16384 /* Parse the clobbers. */
16385 if (cp_lexer_next_token_is_not (parser
->lexer
,
16387 && cp_lexer_next_token_is_not (parser
->lexer
,
16389 clobbers
= cp_parser_asm_clobber_list (parser
);
16392 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16393 /* The labels are coming next. */
16396 /* Look for labels. */
16398 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16401 /* Consume the `:' or `::'. */
16402 cp_lexer_consume_token (parser
->lexer
);
16403 /* Parse the labels. */
16404 labels
= cp_parser_asm_label_list (parser
);
16407 if (goto_p
&& !labels_p
)
16408 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16411 missing
= RT_COLON_SCOPE
;
16413 /* Look for the closing `)'. */
16414 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16415 missing
? missing
: RT_CLOSE_PAREN
))
16416 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16417 /*consume_paren=*/true);
16418 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16420 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16422 /* Create the ASM_EXPR. */
16423 if (parser
->in_function_body
)
16425 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16426 inputs
, clobbers
, labels
);
16427 /* If the extended syntax was not used, mark the ASM_EXPR. */
16430 tree temp
= asm_stmt
;
16431 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16432 temp
= TREE_OPERAND (temp
, 0);
16434 ASM_INPUT_P (temp
) = 1;
16438 add_asm_node (string
);
16442 /* Declarators [gram.dcl.decl] */
16444 /* Parse an init-declarator.
16447 declarator initializer [opt]
16452 declarator asm-specification [opt] attributes [opt] initializer [opt]
16454 function-definition:
16455 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16457 decl-specifier-seq [opt] declarator function-try-block
16461 function-definition:
16462 __extension__ function-definition
16466 function-definition:
16467 decl-specifier-seq [opt] declarator function-transaction-block
16469 The DECL_SPECIFIERS apply to this declarator. Returns a
16470 representation of the entity declared. If MEMBER_P is TRUE, then
16471 this declarator appears in a class scope. The new DECL created by
16472 this declarator is returned.
16474 The CHECKS are access checks that should be performed once we know
16475 what entity is being declared (and, therefore, what classes have
16478 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16479 for a function-definition here as well. If the declarator is a
16480 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16481 be TRUE upon return. By that point, the function-definition will
16482 have been completely parsed.
16484 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16487 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16488 parsed declaration if it is an uninitialized single declarator not followed
16489 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16490 if present, will not be consumed. If returned, this declarator will be
16491 created with SD_INITIALIZED but will not call cp_finish_decl. */
16494 cp_parser_init_declarator (cp_parser
* parser
,
16495 cp_decl_specifier_seq
*decl_specifiers
,
16496 vec
<deferred_access_check
, va_gc
> *checks
,
16497 bool function_definition_allowed_p
,
16499 int declares_class_or_enum
,
16500 bool* function_definition_p
,
16501 tree
* maybe_range_for_decl
)
16503 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16504 *attributes_start_token
= NULL
;
16505 cp_declarator
*declarator
;
16506 tree prefix_attributes
;
16507 tree attributes
= NULL
;
16508 tree asm_specification
;
16510 tree decl
= NULL_TREE
;
16512 int is_initialized
;
16513 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16514 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16516 enum cpp_ttype initialization_kind
;
16517 bool is_direct_init
= false;
16518 bool is_non_constant_init
;
16519 int ctor_dtor_or_conv_p
;
16521 tree pushed_scope
= NULL_TREE
;
16522 bool range_for_decl_p
= false;
16523 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16525 /* Gather the attributes that were provided with the
16526 decl-specifiers. */
16527 prefix_attributes
= decl_specifiers
->attributes
;
16529 /* Assume that this is not the declarator for a function
16531 if (function_definition_p
)
16532 *function_definition_p
= false;
16534 /* Default arguments are only permitted for function parameters. */
16535 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16536 parser
->default_arg_ok_p
= false;
16538 /* Defer access checks while parsing the declarator; we cannot know
16539 what names are accessible until we know what is being
16541 resume_deferring_access_checks ();
16543 /* Parse the declarator. */
16544 token
= cp_lexer_peek_token (parser
->lexer
);
16546 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16547 &ctor_dtor_or_conv_p
,
16548 /*parenthesized_p=*/NULL
,
16550 /* Gather up the deferred checks. */
16551 stop_deferring_access_checks ();
16553 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16555 /* If the DECLARATOR was erroneous, there's no need to go
16557 if (declarator
== cp_error_declarator
)
16558 return error_mark_node
;
16560 /* Check that the number of template-parameter-lists is OK. */
16561 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16563 return error_mark_node
;
16565 if (declares_class_or_enum
& 2)
16566 cp_parser_check_for_definition_in_return_type (declarator
,
16567 decl_specifiers
->type
,
16568 decl_specifiers
->locations
[ds_type_spec
]);
16570 /* Figure out what scope the entity declared by the DECLARATOR is
16571 located in. `grokdeclarator' sometimes changes the scope, so
16572 we compute it now. */
16573 scope
= get_scope_of_declarator (declarator
);
16575 /* Perform any lookups in the declared type which were thought to be
16576 dependent, but are not in the scope of the declarator. */
16577 decl_specifiers
->type
16578 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
16580 /* If we're allowing GNU extensions, look for an
16581 asm-specification. */
16582 if (cp_parser_allow_gnu_extensions_p (parser
))
16584 /* Look for an asm-specification. */
16585 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
16586 asm_specification
= cp_parser_asm_specification_opt (parser
);
16589 asm_specification
= NULL_TREE
;
16591 /* Look for attributes. */
16592 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
16593 attributes
= cp_parser_attributes_opt (parser
);
16595 /* Peek at the next token. */
16596 token
= cp_lexer_peek_token (parser
->lexer
);
16598 if (function_declarator_p (declarator
))
16600 /* Check to see if the token indicates the start of a
16601 function-definition. */
16602 if (cp_parser_token_starts_function_definition_p (token
))
16604 if (!function_definition_allowed_p
)
16606 /* If a function-definition should not appear here, issue an
16608 cp_parser_error (parser
,
16609 "a function-definition is not allowed here");
16610 return error_mark_node
;
16613 location_t func_brace_location
16614 = cp_lexer_peek_token (parser
->lexer
)->location
;
16616 /* Neither attributes nor an asm-specification are allowed
16617 on a function-definition. */
16618 if (asm_specification
)
16619 error_at (asm_spec_start_token
->location
,
16620 "an asm-specification is not allowed "
16621 "on a function-definition");
16623 error_at (attributes_start_token
->location
,
16624 "attributes are not allowed "
16625 "on a function-definition");
16626 /* This is a function-definition. */
16627 *function_definition_p
= true;
16629 /* Parse the function definition. */
16631 decl
= cp_parser_save_member_function_body (parser
,
16634 prefix_attributes
);
16637 (cp_parser_function_definition_from_specifiers_and_declarator
16638 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
16640 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
16642 /* This is where the prologue starts... */
16643 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
16644 = func_brace_location
;
16653 Only in function declarations for constructors, destructors, and
16654 type conversions can the decl-specifier-seq be omitted.
16656 We explicitly postpone this check past the point where we handle
16657 function-definitions because we tolerate function-definitions
16658 that are missing their return types in some modes. */
16659 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
16661 cp_parser_error (parser
,
16662 "expected constructor, destructor, or type conversion");
16663 return error_mark_node
;
16666 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16667 if (token
->type
== CPP_EQ
16668 || token
->type
== CPP_OPEN_PAREN
16669 || token
->type
== CPP_OPEN_BRACE
)
16671 is_initialized
= SD_INITIALIZED
;
16672 initialization_kind
= token
->type
;
16673 if (maybe_range_for_decl
)
16674 *maybe_range_for_decl
= error_mark_node
;
16676 if (token
->type
== CPP_EQ
16677 && function_declarator_p (declarator
))
16679 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16680 if (t2
->keyword
== RID_DEFAULT
)
16681 is_initialized
= SD_DEFAULTED
;
16682 else if (t2
->keyword
== RID_DELETE
)
16683 is_initialized
= SD_DELETED
;
16688 /* If the init-declarator isn't initialized and isn't followed by a
16689 `,' or `;', it's not a valid init-declarator. */
16690 if (token
->type
!= CPP_COMMA
16691 && token
->type
!= CPP_SEMICOLON
)
16693 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
16694 range_for_decl_p
= true;
16697 cp_parser_error (parser
, "expected initializer");
16698 return error_mark_node
;
16701 is_initialized
= SD_UNINITIALIZED
;
16702 initialization_kind
= CPP_EOF
;
16705 /* Because start_decl has side-effects, we should only call it if we
16706 know we're going ahead. By this point, we know that we cannot
16707 possibly be looking at any other construct. */
16708 cp_parser_commit_to_tentative_parse (parser
);
16710 /* If the decl specifiers were bad, issue an error now that we're
16711 sure this was intended to be a declarator. Then continue
16712 declaring the variable(s), as int, to try to cut down on further
16714 if (decl_specifiers
->any_specifiers_p
16715 && decl_specifiers
->type
== error_mark_node
)
16717 cp_parser_error (parser
, "invalid type in declaration");
16718 decl_specifiers
->type
= integer_type_node
;
16721 /* Check to see whether or not this declaration is a friend. */
16722 friend_p
= cp_parser_friend_p (decl_specifiers
);
16724 /* Enter the newly declared entry in the symbol table. If we're
16725 processing a declaration in a class-specifier, we wait until
16726 after processing the initializer. */
16729 if (parser
->in_unbraced_linkage_specification_p
)
16730 decl_specifiers
->storage_class
= sc_extern
;
16731 decl
= start_decl (declarator
, decl_specifiers
,
16732 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
16733 attributes
, prefix_attributes
, &pushed_scope
);
16734 cp_finalize_omp_declare_simd (parser
, decl
);
16735 /* Adjust location of decl if declarator->id_loc is more appropriate:
16736 set, and decl wasn't merged with another decl, in which case its
16737 location would be different from input_location, and more accurate. */
16739 && declarator
->id_loc
!= UNKNOWN_LOCATION
16740 && DECL_SOURCE_LOCATION (decl
) == input_location
)
16741 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
16744 /* Enter the SCOPE. That way unqualified names appearing in the
16745 initializer will be looked up in SCOPE. */
16746 pushed_scope
= push_scope (scope
);
16748 /* Perform deferred access control checks, now that we know in which
16749 SCOPE the declared entity resides. */
16750 if (!member_p
&& decl
)
16752 tree saved_current_function_decl
= NULL_TREE
;
16754 /* If the entity being declared is a function, pretend that we
16755 are in its scope. If it is a `friend', it may have access to
16756 things that would not otherwise be accessible. */
16757 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16759 saved_current_function_decl
= current_function_decl
;
16760 current_function_decl
= decl
;
16763 /* Perform access checks for template parameters. */
16764 cp_parser_perform_template_parameter_access_checks (checks
);
16766 /* Perform the access control checks for the declarator and the
16767 decl-specifiers. */
16768 perform_deferred_access_checks (tf_warning_or_error
);
16770 /* Restore the saved value. */
16771 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16772 current_function_decl
= saved_current_function_decl
;
16775 /* Parse the initializer. */
16776 initializer
= NULL_TREE
;
16777 is_direct_init
= false;
16778 is_non_constant_init
= true;
16779 if (is_initialized
)
16781 if (function_declarator_p (declarator
))
16783 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
16784 if (initialization_kind
== CPP_EQ
)
16785 initializer
= cp_parser_pure_specifier (parser
);
16788 /* If the declaration was erroneous, we don't really
16789 know what the user intended, so just silently
16790 consume the initializer. */
16791 if (decl
!= error_mark_node
)
16792 error_at (initializer_start_token
->location
,
16793 "initializer provided for function");
16794 cp_parser_skip_to_closing_parenthesis (parser
,
16795 /*recovering=*/true,
16796 /*or_comma=*/false,
16797 /*consume_paren=*/true);
16802 /* We want to record the extra mangling scope for in-class
16803 initializers of class members and initializers of static data
16804 member templates. The former involves deferring
16805 parsing of the initializer until end of class as with default
16806 arguments. So right here we only handle the latter. */
16807 if (!member_p
&& processing_template_decl
)
16808 start_lambda_scope (decl
);
16809 initializer
= cp_parser_initializer (parser
,
16811 &is_non_constant_init
);
16812 if (!member_p
&& processing_template_decl
)
16813 finish_lambda_scope ();
16814 if (initializer
== error_mark_node
)
16815 cp_parser_skip_to_end_of_statement (parser
);
16819 /* The old parser allows attributes to appear after a parenthesized
16820 initializer. Mark Mitchell proposed removing this functionality
16821 on the GCC mailing lists on 2002-08-13. This parser accepts the
16822 attributes -- but ignores them. */
16823 if (cp_parser_allow_gnu_extensions_p (parser
)
16824 && initialization_kind
== CPP_OPEN_PAREN
)
16825 if (cp_parser_attributes_opt (parser
))
16826 warning (OPT_Wattributes
,
16827 "attributes after parenthesized initializer ignored");
16829 /* A non-template declaration involving a function parameter list containing
16830 an implicit template parameter will have been made into a template. If it
16831 turns out that the resulting declaration is not an actual function then
16832 finish the template declaration here. An error message will already have
16834 if (parser
->fully_implicit_function_template_p
)
16835 if (!function_declarator_p (declarator
))
16839 pop_scope (pushed_scope
);
16842 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
16845 /* For an in-class declaration, use `grokfield' to create the
16851 pop_scope (pushed_scope
);
16852 pushed_scope
= NULL_TREE
;
16854 decl
= grokfield (declarator
, decl_specifiers
,
16855 initializer
, !is_non_constant_init
,
16856 /*asmspec=*/NULL_TREE
,
16857 chainon (attributes
, prefix_attributes
));
16858 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16859 cp_parser_save_default_args (parser
, decl
);
16860 cp_finalize_omp_declare_simd (parser
, decl
);
16863 /* Finish processing the declaration. But, skip member
16865 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
16867 cp_finish_decl (decl
,
16868 initializer
, !is_non_constant_init
,
16870 /* If the initializer is in parentheses, then this is
16871 a direct-initialization, which means that an
16872 `explicit' constructor is OK. Otherwise, an
16873 `explicit' constructor cannot be used. */
16874 ((is_direct_init
|| !is_initialized
)
16875 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
16877 else if ((cxx_dialect
!= cxx98
) && friend_p
16878 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16879 /* Core issue #226 (C++0x only): A default template-argument
16880 shall not be specified in a friend class template
16882 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
16883 /*is_partial=*/false, /*is_friend_decl=*/1);
16885 if (!friend_p
&& pushed_scope
)
16886 pop_scope (pushed_scope
);
16888 if (function_declarator_p (declarator
)
16889 && parser
->fully_implicit_function_template_p
)
16892 decl
= finish_fully_implicit_template (parser
, decl
);
16894 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
16900 /* Parse a declarator.
16904 ptr-operator declarator
16906 abstract-declarator:
16907 ptr-operator abstract-declarator [opt]
16908 direct-abstract-declarator
16913 attributes [opt] direct-declarator
16914 attributes [opt] ptr-operator declarator
16916 abstract-declarator:
16917 attributes [opt] ptr-operator abstract-declarator [opt]
16918 attributes [opt] direct-abstract-declarator
16920 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16921 detect constructor, destructor or conversion operators. It is set
16922 to -1 if the declarator is a name, and +1 if it is a
16923 function. Otherwise it is set to zero. Usually you just want to
16924 test for >0, but internally the negative value is used.
16926 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16927 a decl-specifier-seq unless it declares a constructor, destructor,
16928 or conversion. It might seem that we could check this condition in
16929 semantic analysis, rather than parsing, but that makes it difficult
16930 to handle something like `f()'. We want to notice that there are
16931 no decl-specifiers, and therefore realize that this is an
16932 expression, not a declaration.)
16934 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16935 the declarator is a direct-declarator of the form "(...)".
16937 MEMBER_P is true iff this declarator is a member-declarator. */
16939 static cp_declarator
*
16940 cp_parser_declarator (cp_parser
* parser
,
16941 cp_parser_declarator_kind dcl_kind
,
16942 int* ctor_dtor_or_conv_p
,
16943 bool* parenthesized_p
,
16946 cp_declarator
*declarator
;
16947 enum tree_code code
;
16948 cp_cv_quals cv_quals
;
16950 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
16952 /* Assume this is not a constructor, destructor, or type-conversion
16954 if (ctor_dtor_or_conv_p
)
16955 *ctor_dtor_or_conv_p
= 0;
16957 if (cp_parser_allow_gnu_extensions_p (parser
))
16958 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
16960 /* Check for the ptr-operator production. */
16961 cp_parser_parse_tentatively (parser
);
16962 /* Parse the ptr-operator. */
16963 code
= cp_parser_ptr_operator (parser
,
16968 /* If that worked, then we have a ptr-operator. */
16969 if (cp_parser_parse_definitely (parser
))
16971 /* If a ptr-operator was found, then this declarator was not
16973 if (parenthesized_p
)
16974 *parenthesized_p
= true;
16975 /* The dependent declarator is optional if we are parsing an
16976 abstract-declarator. */
16977 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16978 cp_parser_parse_tentatively (parser
);
16980 /* Parse the dependent declarator. */
16981 declarator
= cp_parser_declarator (parser
, dcl_kind
,
16982 /*ctor_dtor_or_conv_p=*/NULL
,
16983 /*parenthesized_p=*/NULL
,
16984 /*member_p=*/false);
16986 /* If we are parsing an abstract-declarator, we must handle the
16987 case where the dependent declarator is absent. */
16988 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
16989 && !cp_parser_parse_definitely (parser
))
16992 declarator
= cp_parser_make_indirect_declarator
16993 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
16995 /* Everything else is a direct-declarator. */
16998 if (parenthesized_p
)
16999 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
17001 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
17002 ctor_dtor_or_conv_p
,
17006 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
17007 declarator
->attributes
= gnu_attributes
;
17011 /* Parse a direct-declarator or direct-abstract-declarator.
17015 direct-declarator ( parameter-declaration-clause )
17016 cv-qualifier-seq [opt]
17017 ref-qualifier [opt]
17018 exception-specification [opt]
17019 direct-declarator [ constant-expression [opt] ]
17022 direct-abstract-declarator:
17023 direct-abstract-declarator [opt]
17024 ( parameter-declaration-clause )
17025 cv-qualifier-seq [opt]
17026 ref-qualifier [opt]
17027 exception-specification [opt]
17028 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17029 ( abstract-declarator )
17031 Returns a representation of the declarator. DCL_KIND is
17032 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17033 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17034 we are parsing a direct-declarator. It is
17035 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17036 of ambiguity we prefer an abstract declarator, as per
17037 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17038 cp_parser_declarator. */
17040 static cp_declarator
*
17041 cp_parser_direct_declarator (cp_parser
* parser
,
17042 cp_parser_declarator_kind dcl_kind
,
17043 int* ctor_dtor_or_conv_p
,
17047 cp_declarator
*declarator
= NULL
;
17048 tree scope
= NULL_TREE
;
17049 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17050 bool saved_in_declarator_p
= parser
->in_declarator_p
;
17052 tree pushed_scope
= NULL_TREE
;
17056 /* Peek at the next token. */
17057 token
= cp_lexer_peek_token (parser
->lexer
);
17058 if (token
->type
== CPP_OPEN_PAREN
)
17060 /* This is either a parameter-declaration-clause, or a
17061 parenthesized declarator. When we know we are parsing a
17062 named declarator, it must be a parenthesized declarator
17063 if FIRST is true. For instance, `(int)' is a
17064 parameter-declaration-clause, with an omitted
17065 direct-abstract-declarator. But `((*))', is a
17066 parenthesized abstract declarator. Finally, when T is a
17067 template parameter `(T)' is a
17068 parameter-declaration-clause, and not a parenthesized
17071 We first try and parse a parameter-declaration-clause,
17072 and then try a nested declarator (if FIRST is true).
17074 It is not an error for it not to be a
17075 parameter-declaration-clause, even when FIRST is
17081 The first is the declaration of a function while the
17082 second is the definition of a variable, including its
17085 Having seen only the parenthesis, we cannot know which of
17086 these two alternatives should be selected. Even more
17087 complex are examples like:
17092 The former is a function-declaration; the latter is a
17093 variable initialization.
17095 Thus again, we try a parameter-declaration-clause, and if
17096 that fails, we back out and return. */
17098 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17101 bool is_declarator
= false;
17103 /* In a member-declarator, the only valid interpretation
17104 of a parenthesis is the start of a
17105 parameter-declaration-clause. (It is invalid to
17106 initialize a static data member with a parenthesized
17107 initializer; only the "=" form of initialization is
17110 cp_parser_parse_tentatively (parser
);
17112 /* Consume the `('. */
17113 cp_lexer_consume_token (parser
->lexer
);
17116 /* If this is going to be an abstract declarator, we're
17117 in a declarator and we can't have default args. */
17118 parser
->default_arg_ok_p
= false;
17119 parser
->in_declarator_p
= true;
17122 begin_scope (sk_function_parms
, NULL_TREE
);
17124 /* Parse the parameter-declaration-clause. */
17125 params
= cp_parser_parameter_declaration_clause (parser
);
17127 /* Consume the `)'. */
17128 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17130 /* If all went well, parse the cv-qualifier-seq,
17131 ref-qualifier and the exception-specification. */
17132 if (member_p
|| cp_parser_parse_definitely (parser
))
17134 cp_cv_quals cv_quals
;
17135 cp_virt_specifiers virt_specifiers
;
17136 cp_ref_qualifier ref_qual
;
17137 tree exception_specification
;
17140 bool memfn
= (member_p
|| (pushed_scope
17141 && CLASS_TYPE_P (pushed_scope
)));
17143 is_declarator
= true;
17145 if (ctor_dtor_or_conv_p
)
17146 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17149 /* Parse the cv-qualifier-seq. */
17150 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17151 /* Parse the ref-qualifier. */
17152 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17153 /* And the exception-specification. */
17154 exception_specification
17155 = cp_parser_exception_specification_opt (parser
);
17157 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17159 /* In here, we handle cases where attribute is used after
17160 the function declaration. For example:
17161 void func (int x) __attribute__((vector(..))); */
17163 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
17165 cp_parser_parse_tentatively (parser
);
17166 tree attr
= cp_parser_gnu_attributes_opt (parser
);
17167 if (cp_lexer_next_token_is_not (parser
->lexer
,
17169 && cp_lexer_next_token_is_not (parser
->lexer
,
17171 cp_parser_abort_tentative_parse (parser
);
17172 else if (!cp_parser_parse_definitely (parser
))
17175 attrs
= chainon (attr
, attrs
);
17177 late_return
= (cp_parser_late_return_type_opt
17178 (parser
, declarator
,
17179 memfn
? cv_quals
: -1));
17182 /* Parse the virt-specifier-seq. */
17183 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17185 /* Create the function-declarator. */
17186 declarator
= make_call_declarator (declarator
,
17191 exception_specification
,
17193 declarator
->std_attributes
= attrs
;
17194 /* Any subsequent parameter lists are to do with
17195 return type, so are not those of the declared
17197 parser
->default_arg_ok_p
= false;
17200 /* Remove the function parms from scope. */
17201 pop_bindings_and_leave_scope ();
17204 /* Repeat the main loop. */
17208 /* If this is the first, we can try a parenthesized
17212 bool saved_in_type_id_in_expr_p
;
17214 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17215 parser
->in_declarator_p
= saved_in_declarator_p
;
17217 /* Consume the `('. */
17218 cp_lexer_consume_token (parser
->lexer
);
17219 /* Parse the nested declarator. */
17220 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17221 parser
->in_type_id_in_expr_p
= true;
17223 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17224 /*parenthesized_p=*/NULL
,
17226 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17228 /* Expect a `)'. */
17229 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17230 declarator
= cp_error_declarator
;
17231 if (declarator
== cp_error_declarator
)
17234 goto handle_declarator
;
17236 /* Otherwise, we must be done. */
17240 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17241 && token
->type
== CPP_OPEN_SQUARE
17242 && !cp_next_tokens_can_be_attribute_p (parser
))
17244 /* Parse an array-declarator. */
17245 tree bounds
, attrs
;
17247 if (ctor_dtor_or_conv_p
)
17248 *ctor_dtor_or_conv_p
= 0;
17251 parser
->default_arg_ok_p
= false;
17252 parser
->in_declarator_p
= true;
17253 /* Consume the `['. */
17254 cp_lexer_consume_token (parser
->lexer
);
17255 /* Peek at the next token. */
17256 token
= cp_lexer_peek_token (parser
->lexer
);
17257 /* If the next token is `]', then there is no
17258 constant-expression. */
17259 if (token
->type
!= CPP_CLOSE_SQUARE
)
17261 bool non_constant_p
;
17263 = cp_parser_constant_expression (parser
,
17264 /*allow_non_constant=*/true,
17266 if (!non_constant_p
)
17268 else if (error_operand_p (bounds
))
17269 /* Already gave an error. */;
17270 else if (!parser
->in_function_body
17271 || current_binding_level
->kind
== sk_function_parms
)
17273 /* Normally, the array bound must be an integral constant
17274 expression. However, as an extension, we allow VLAs
17275 in function scopes as long as they aren't part of a
17276 parameter declaration. */
17277 cp_parser_error (parser
,
17278 "array bound is not an integer constant");
17279 bounds
= error_mark_node
;
17281 else if (processing_template_decl
17282 && !type_dependent_expression_p (bounds
))
17284 /* Remember this wasn't a constant-expression. */
17285 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17286 TREE_SIDE_EFFECTS (bounds
) = 1;
17290 bounds
= NULL_TREE
;
17291 /* Look for the closing `]'. */
17292 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17294 declarator
= cp_error_declarator
;
17298 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17299 declarator
= make_array_declarator (declarator
, bounds
);
17300 declarator
->std_attributes
= attrs
;
17302 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17305 tree qualifying_scope
;
17306 tree unqualified_name
;
17308 special_function_kind sfk
;
17310 bool pack_expansion_p
= false;
17311 cp_token
*declarator_id_start_token
;
17313 /* Parse a declarator-id */
17314 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17317 cp_parser_parse_tentatively (parser
);
17319 /* If we see an ellipsis, we should be looking at a
17321 if (token
->type
== CPP_ELLIPSIS
)
17323 /* Consume the `...' */
17324 cp_lexer_consume_token (parser
->lexer
);
17326 pack_expansion_p
= true;
17330 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17332 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17333 qualifying_scope
= parser
->scope
;
17338 if (!unqualified_name
&& pack_expansion_p
)
17340 /* Check whether an error occurred. */
17341 okay
= !cp_parser_error_occurred (parser
);
17343 /* We already consumed the ellipsis to mark a
17344 parameter pack, but we have no way to report it,
17345 so abort the tentative parse. We will be exiting
17346 immediately anyway. */
17347 cp_parser_abort_tentative_parse (parser
);
17350 okay
= cp_parser_parse_definitely (parser
);
17353 unqualified_name
= error_mark_node
;
17354 else if (unqualified_name
17355 && (qualifying_scope
17356 || (!identifier_p (unqualified_name
))))
17358 cp_parser_error (parser
, "expected unqualified-id");
17359 unqualified_name
= error_mark_node
;
17363 if (!unqualified_name
)
17365 if (unqualified_name
== error_mark_node
)
17367 declarator
= cp_error_declarator
;
17368 pack_expansion_p
= false;
17369 declarator
->parameter_pack_p
= false;
17373 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17375 if (qualifying_scope
&& at_namespace_scope_p ()
17376 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17378 /* In the declaration of a member of a template class
17379 outside of the class itself, the SCOPE will sometimes
17380 be a TYPENAME_TYPE. For example, given:
17382 template <typename T>
17383 int S<T>::R::i = 3;
17385 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17386 this context, we must resolve S<T>::R to an ordinary
17387 type, rather than a typename type.
17389 The reason we normally avoid resolving TYPENAME_TYPEs
17390 is that a specialization of `S' might render
17391 `S<T>::R' not a type. However, if `S' is
17392 specialized, then this `i' will not be used, so there
17393 is no harm in resolving the types here. */
17396 /* Resolve the TYPENAME_TYPE. */
17397 type
= resolve_typename_type (qualifying_scope
,
17398 /*only_current_p=*/false);
17399 /* If that failed, the declarator is invalid. */
17400 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17402 if (typedef_variant_p (type
))
17403 error_at (declarator_id_start_token
->location
,
17404 "cannot define member of dependent typedef "
17407 error_at (declarator_id_start_token
->location
,
17408 "%<%T::%E%> is not a type",
17409 TYPE_CONTEXT (qualifying_scope
),
17410 TYPE_IDENTIFIER (qualifying_scope
));
17412 qualifying_scope
= type
;
17417 if (unqualified_name
)
17421 if (qualifying_scope
17422 && CLASS_TYPE_P (qualifying_scope
))
17423 class_type
= qualifying_scope
;
17425 class_type
= current_class_type
;
17427 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17429 tree name_type
= TREE_TYPE (unqualified_name
);
17430 if (class_type
&& same_type_p (name_type
, class_type
))
17432 if (qualifying_scope
17433 && CLASSTYPE_USE_TEMPLATE (name_type
))
17435 error_at (declarator_id_start_token
->location
,
17436 "invalid use of constructor as a template");
17437 inform (declarator_id_start_token
->location
,
17438 "use %<%T::%D%> instead of %<%T::%D%> to "
17439 "name the constructor in a qualified name",
17441 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17442 class_type
, name_type
);
17443 declarator
= cp_error_declarator
;
17447 unqualified_name
= constructor_name (class_type
);
17451 /* We do not attempt to print the declarator
17452 here because we do not have enough
17453 information about its original syntactic
17455 cp_parser_error (parser
, "invalid declarator");
17456 declarator
= cp_error_declarator
;
17463 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17464 sfk
= sfk_destructor
;
17465 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17466 sfk
= sfk_conversion
;
17467 else if (/* There's no way to declare a constructor
17468 for an anonymous type, even if the type
17469 got a name for linkage purposes. */
17470 !TYPE_WAS_ANONYMOUS (class_type
)
17471 && constructor_name_p (unqualified_name
,
17474 unqualified_name
= constructor_name (class_type
);
17475 sfk
= sfk_constructor
;
17477 else if (is_overloaded_fn (unqualified_name
)
17478 && DECL_CONSTRUCTOR_P (get_first_fn
17479 (unqualified_name
)))
17480 sfk
= sfk_constructor
;
17482 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17483 *ctor_dtor_or_conv_p
= -1;
17486 declarator
= make_id_declarator (qualifying_scope
,
17489 declarator
->std_attributes
= attrs
;
17490 declarator
->id_loc
= token
->location
;
17491 declarator
->parameter_pack_p
= pack_expansion_p
;
17493 if (pack_expansion_p
)
17494 maybe_warn_variadic_templates ();
17497 handle_declarator
:;
17498 scope
= get_scope_of_declarator (declarator
);
17501 /* Any names that appear after the declarator-id for a
17502 member are looked up in the containing scope. */
17503 if (at_function_scope_p ())
17505 /* But declarations with qualified-ids can't appear in a
17507 cp_parser_error (parser
, "qualified-id in declaration");
17508 declarator
= cp_error_declarator
;
17511 pushed_scope
= push_scope (scope
);
17513 parser
->in_declarator_p
= true;
17514 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17515 || (declarator
&& declarator
->kind
== cdk_id
))
17516 /* Default args are only allowed on function
17518 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17520 parser
->default_arg_ok_p
= false;
17529 /* For an abstract declarator, we might wind up with nothing at this
17530 point. That's an error; the declarator is not optional. */
17532 cp_parser_error (parser
, "expected declarator");
17534 /* If we entered a scope, we must exit it now. */
17536 pop_scope (pushed_scope
);
17538 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17539 parser
->in_declarator_p
= saved_in_declarator_p
;
17544 /* Parse a ptr-operator.
17547 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17548 * cv-qualifier-seq [opt]
17550 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17551 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17556 & cv-qualifier-seq [opt]
17558 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17559 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17560 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17561 filled in with the TYPE containing the member. *CV_QUALS is
17562 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17563 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17564 Note that the tree codes returned by this function have nothing
17565 to do with the types of trees that will be eventually be created
17566 to represent the pointer or reference type being parsed. They are
17567 just constants with suggestive names. */
17568 static enum tree_code
17569 cp_parser_ptr_operator (cp_parser
* parser
,
17571 cp_cv_quals
*cv_quals
,
17574 enum tree_code code
= ERROR_MARK
;
17576 tree attrs
= NULL_TREE
;
17578 /* Assume that it's not a pointer-to-member. */
17580 /* And that there are no cv-qualifiers. */
17581 *cv_quals
= TYPE_UNQUALIFIED
;
17583 /* Peek at the next token. */
17584 token
= cp_lexer_peek_token (parser
->lexer
);
17586 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17587 if (token
->type
== CPP_MULT
)
17588 code
= INDIRECT_REF
;
17589 else if (token
->type
== CPP_AND
)
17591 else if ((cxx_dialect
!= cxx98
) &&
17592 token
->type
== CPP_AND_AND
) /* C++0x only */
17593 code
= NON_LVALUE_EXPR
;
17595 if (code
!= ERROR_MARK
)
17597 /* Consume the `*', `&' or `&&'. */
17598 cp_lexer_consume_token (parser
->lexer
);
17600 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17601 `&', if we are allowing GNU extensions. (The only qualifier
17602 that can legally appear after `&' is `restrict', but that is
17603 enforced during semantic analysis. */
17604 if (code
== INDIRECT_REF
17605 || cp_parser_allow_gnu_extensions_p (parser
))
17606 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17608 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17609 if (attributes
!= NULL
)
17610 *attributes
= attrs
;
17614 /* Try the pointer-to-member case. */
17615 cp_parser_parse_tentatively (parser
);
17616 /* Look for the optional `::' operator. */
17617 cp_parser_global_scope_opt (parser
,
17618 /*current_scope_valid_p=*/false);
17619 /* Look for the nested-name specifier. */
17620 token
= cp_lexer_peek_token (parser
->lexer
);
17621 cp_parser_nested_name_specifier (parser
,
17622 /*typename_keyword_p=*/false,
17623 /*check_dependency_p=*/true,
17625 /*is_declaration=*/false);
17626 /* If we found it, and the next token is a `*', then we are
17627 indeed looking at a pointer-to-member operator. */
17628 if (!cp_parser_error_occurred (parser
)
17629 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
17631 /* Indicate that the `*' operator was used. */
17632 code
= INDIRECT_REF
;
17634 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
17635 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
17636 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
17637 error_at (token
->location
, "cannot form pointer to member of "
17638 "non-class %q#T", parser
->scope
);
17641 /* The type of which the member is a member is given by the
17643 *type
= parser
->scope
;
17644 /* The next name will not be qualified. */
17645 parser
->scope
= NULL_TREE
;
17646 parser
->qualifying_scope
= NULL_TREE
;
17647 parser
->object_scope
= NULL_TREE
;
17648 /* Look for optional c++11 attributes. */
17649 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17650 if (attributes
!= NULL
)
17651 *attributes
= attrs
;
17652 /* Look for the optional cv-qualifier-seq. */
17653 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17656 /* If that didn't work we don't have a ptr-operator. */
17657 if (!cp_parser_parse_definitely (parser
))
17658 cp_parser_error (parser
, "expected ptr-operator");
17664 /* Parse an (optional) cv-qualifier-seq.
17667 cv-qualifier cv-qualifier-seq [opt]
17678 Returns a bitmask representing the cv-qualifiers. */
17681 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
17683 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
17688 cp_cv_quals cv_qualifier
;
17690 /* Peek at the next token. */
17691 token
= cp_lexer_peek_token (parser
->lexer
);
17692 /* See if it's a cv-qualifier. */
17693 switch (token
->keyword
)
17696 cv_qualifier
= TYPE_QUAL_CONST
;
17700 cv_qualifier
= TYPE_QUAL_VOLATILE
;
17704 cv_qualifier
= TYPE_QUAL_RESTRICT
;
17708 cv_qualifier
= TYPE_UNQUALIFIED
;
17715 if (cv_quals
& cv_qualifier
)
17717 error_at (token
->location
, "duplicate cv-qualifier");
17718 cp_lexer_purge_token (parser
->lexer
);
17722 cp_lexer_consume_token (parser
->lexer
);
17723 cv_quals
|= cv_qualifier
;
17730 /* Parse an (optional) ref-qualifier
17736 Returns cp_ref_qualifier representing ref-qualifier. */
17738 static cp_ref_qualifier
17739 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
17741 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
17743 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17744 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
17749 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
17750 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17752 switch (token
->type
)
17755 curr_ref_qual
= REF_QUAL_LVALUE
;
17759 curr_ref_qual
= REF_QUAL_RVALUE
;
17763 curr_ref_qual
= REF_QUAL_NONE
;
17767 if (!curr_ref_qual
)
17771 error_at (token
->location
, "multiple ref-qualifiers");
17772 cp_lexer_purge_token (parser
->lexer
);
17776 ref_qual
= curr_ref_qual
;
17777 cp_lexer_consume_token (parser
->lexer
);
17784 /* Parse an (optional) virt-specifier-seq.
17786 virt-specifier-seq:
17787 virt-specifier virt-specifier-seq [opt]
17793 Returns a bitmask representing the virt-specifiers. */
17795 static cp_virt_specifiers
17796 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
17798 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
17803 cp_virt_specifiers virt_specifier
;
17805 /* Peek at the next token. */
17806 token
= cp_lexer_peek_token (parser
->lexer
);
17807 /* See if it's a virt-specifier-qualifier. */
17808 if (token
->type
!= CPP_NAME
)
17810 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
17812 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17813 virt_specifier
= VIRT_SPEC_OVERRIDE
;
17815 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
17817 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17818 virt_specifier
= VIRT_SPEC_FINAL
;
17820 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
17822 virt_specifier
= VIRT_SPEC_FINAL
;
17827 if (virt_specifiers
& virt_specifier
)
17829 error_at (token
->location
, "duplicate virt-specifier");
17830 cp_lexer_purge_token (parser
->lexer
);
17834 cp_lexer_consume_token (parser
->lexer
);
17835 virt_specifiers
|= virt_specifier
;
17838 return virt_specifiers
;
17841 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17842 is in scope even though it isn't real. */
17845 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
17849 if (current_class_ptr
)
17851 /* We don't clear this between NSDMIs. Is it already what we want? */
17852 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
17853 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
17854 && cp_type_quals (type
) == quals
)
17858 this_parm
= build_this_parm (ctype
, quals
);
17859 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17860 current_class_ptr
= NULL_TREE
;
17862 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
17863 current_class_ptr
= this_parm
;
17866 /* Return true iff our current scope is a non-static data member
17870 parsing_nsdmi (void)
17872 /* We recognize NSDMI context by the context-less 'this' pointer set up
17873 by the function above. */
17874 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
17879 /* Parse a late-specified return type, if any. This is not a separate
17880 non-terminal, but part of a function declarator, which looks like
17882 -> trailing-type-specifier-seq abstract-declarator(opt)
17884 Returns the type indicated by the type-id.
17886 In addition to this this parses any queued up omp declare simd
17887 clauses and Cilk Plus SIMD-enabled function's vector attributes.
17889 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17893 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
17897 tree type
= NULL_TREE
;
17898 bool declare_simd_p
= (parser
->omp_declare_simd
17900 && declarator
->kind
== cdk_id
);
17902 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
17903 && declarator
&& declarator
->kind
== cdk_id
);
17905 /* Peek at the next token. */
17906 token
= cp_lexer_peek_token (parser
->lexer
);
17907 /* A late-specified return type is indicated by an initial '->'. */
17908 if (token
->type
!= CPP_DEREF
&& !(declare_simd_p
|| cilk_simd_fn_vector_p
))
17911 tree save_ccp
= current_class_ptr
;
17912 tree save_ccr
= current_class_ref
;
17915 /* DR 1207: 'this' is in scope in the trailing return type. */
17916 inject_this_parameter (current_class_type
, quals
);
17919 if (token
->type
== CPP_DEREF
)
17921 /* Consume the ->. */
17922 cp_lexer_consume_token (parser
->lexer
);
17924 type
= cp_parser_trailing_type_id (parser
);
17927 if (cilk_simd_fn_vector_p
)
17928 declarator
->std_attributes
17929 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
17930 declarator
->std_attributes
);
17931 if (declare_simd_p
)
17932 declarator
->std_attributes
17933 = cp_parser_late_parsing_omp_declare_simd (parser
,
17934 declarator
->std_attributes
);
17938 current_class_ptr
= save_ccp
;
17939 current_class_ref
= save_ccr
;
17945 /* Parse a declarator-id.
17949 :: [opt] nested-name-specifier [opt] type-name
17951 In the `id-expression' case, the value returned is as for
17952 cp_parser_id_expression if the id-expression was an unqualified-id.
17953 If the id-expression was a qualified-id, then a SCOPE_REF is
17954 returned. The first operand is the scope (either a NAMESPACE_DECL
17955 or TREE_TYPE), but the second is still just a representation of an
17959 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
17962 /* The expression must be an id-expression. Assume that qualified
17963 names are the names of types so that:
17966 int S<T>::R::i = 3;
17968 will work; we must treat `S<T>::R' as the name of a type.
17969 Similarly, assume that qualified names are templates, where
17973 int S<T>::R<T>::i = 3;
17976 id
= cp_parser_id_expression (parser
,
17977 /*template_keyword_p=*/false,
17978 /*check_dependency_p=*/false,
17979 /*template_p=*/NULL
,
17980 /*declarator_p=*/true,
17982 if (id
&& BASELINK_P (id
))
17983 id
= BASELINK_FUNCTIONS (id
);
17987 /* Parse a type-id.
17990 type-specifier-seq abstract-declarator [opt]
17992 Returns the TYPE specified. */
17995 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
17996 bool is_trailing_return
)
17998 cp_decl_specifier_seq type_specifier_seq
;
17999 cp_declarator
*abstract_declarator
;
18001 /* Parse the type-specifier-seq. */
18002 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18003 is_trailing_return
,
18004 &type_specifier_seq
);
18005 if (type_specifier_seq
.type
== error_mark_node
)
18006 return error_mark_node
;
18008 /* There might or might not be an abstract declarator. */
18009 cp_parser_parse_tentatively (parser
);
18010 /* Look for the declarator. */
18011 abstract_declarator
18012 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
18013 /*parenthesized_p=*/NULL
,
18014 /*member_p=*/false);
18015 /* Check to see if there really was a declarator. */
18016 if (!cp_parser_parse_definitely (parser
))
18017 abstract_declarator
= NULL
;
18019 if (type_specifier_seq
.type
18020 /* None of the valid uses of 'auto' in C++14 involve the type-id
18021 nonterminal, but it is valid in a trailing-return-type. */
18022 && !(cxx_dialect
>= cxx1y
&& is_trailing_return
)
18023 && type_uses_auto (type_specifier_seq
.type
))
18025 /* A type-id with type 'auto' is only ok if the abstract declarator
18026 is a function declarator with a late-specified return type. */
18027 if (abstract_declarator
18028 && abstract_declarator
->kind
== cdk_function
18029 && abstract_declarator
->u
.function
.late_return_type
)
18033 error ("invalid use of %<auto%>");
18034 return error_mark_node
;
18038 return groktypename (&type_specifier_seq
, abstract_declarator
,
18042 static tree
cp_parser_type_id (cp_parser
*parser
)
18044 return cp_parser_type_id_1 (parser
, false, false);
18047 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
18050 const char *saved_message
= parser
->type_definition_forbidden_message
;
18051 parser
->type_definition_forbidden_message
18052 = G_("types may not be defined in template arguments");
18053 r
= cp_parser_type_id_1 (parser
, true, false);
18054 parser
->type_definition_forbidden_message
= saved_message
;
18055 if (cxx_dialect
>= cxx1y
&& type_uses_auto (r
))
18057 error ("invalid use of %<auto%> in template argument");
18058 r
= error_mark_node
;
18063 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
18065 return cp_parser_type_id_1 (parser
, false, true);
18068 /* Parse a type-specifier-seq.
18070 type-specifier-seq:
18071 type-specifier type-specifier-seq [opt]
18075 type-specifier-seq:
18076 attributes type-specifier-seq [opt]
18078 If IS_DECLARATION is true, we are at the start of a "condition" or
18079 exception-declaration, so we might be followed by a declarator-id.
18081 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18082 i.e. we've just seen "->".
18084 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18087 cp_parser_type_specifier_seq (cp_parser
* parser
,
18088 bool is_declaration
,
18089 bool is_trailing_return
,
18090 cp_decl_specifier_seq
*type_specifier_seq
)
18092 bool seen_type_specifier
= false;
18093 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
18094 cp_token
*start_token
= NULL
;
18096 /* Clear the TYPE_SPECIFIER_SEQ. */
18097 clear_decl_specs (type_specifier_seq
);
18099 /* In the context of a trailing return type, enum E { } is an
18100 elaborated-type-specifier followed by a function-body, not an
18102 if (is_trailing_return
)
18103 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
18105 /* Parse the type-specifiers and attributes. */
18108 tree type_specifier
;
18109 bool is_cv_qualifier
;
18111 /* Check for attributes first. */
18112 if (cp_next_tokens_can_be_attribute_p (parser
))
18114 type_specifier_seq
->attributes
=
18115 chainon (type_specifier_seq
->attributes
,
18116 cp_parser_attributes_opt (parser
));
18120 /* record the token of the beginning of the type specifier seq,
18121 for error reporting purposes*/
18123 start_token
= cp_lexer_peek_token (parser
->lexer
);
18125 /* Look for the type-specifier. */
18126 type_specifier
= cp_parser_type_specifier (parser
,
18128 type_specifier_seq
,
18129 /*is_declaration=*/false,
18132 if (!type_specifier
)
18134 /* If the first type-specifier could not be found, this is not a
18135 type-specifier-seq at all. */
18136 if (!seen_type_specifier
)
18138 /* Set in_declarator_p to avoid skipping to the semicolon. */
18139 int in_decl
= parser
->in_declarator_p
;
18140 parser
->in_declarator_p
= true;
18142 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
18143 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18144 cp_parser_error (parser
, "expected type-specifier");
18146 parser
->in_declarator_p
= in_decl
;
18148 type_specifier_seq
->type
= error_mark_node
;
18151 /* If subsequent type-specifiers could not be found, the
18152 type-specifier-seq is complete. */
18156 seen_type_specifier
= true;
18157 /* The standard says that a condition can be:
18159 type-specifier-seq declarator = assignment-expression
18166 we should treat the "S" as a declarator, not as a
18167 type-specifier. The standard doesn't say that explicitly for
18168 type-specifier-seq, but it does say that for
18169 decl-specifier-seq in an ordinary declaration. Perhaps it
18170 would be clearer just to allow a decl-specifier-seq here, and
18171 then add a semantic restriction that if any decl-specifiers
18172 that are not type-specifiers appear, the program is invalid. */
18173 if (is_declaration
&& !is_cv_qualifier
)
18174 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18178 /* Return whether the function currently being declared has an associated
18179 template parameter list. */
18182 function_being_declared_is_template_p (cp_parser
* parser
)
18184 if (!current_template_parms
|| processing_template_parmlist
)
18187 if (parser
->implicit_template_scope
)
18190 if (at_class_scope_p ()
18191 && TYPE_BEING_DEFINED (current_class_type
))
18192 return parser
->num_template_parameter_lists
!= 0;
18194 return ((int) parser
->num_template_parameter_lists
> template_class_depth
18195 (current_class_type
));
18198 /* Parse a parameter-declaration-clause.
18200 parameter-declaration-clause:
18201 parameter-declaration-list [opt] ... [opt]
18202 parameter-declaration-list , ...
18204 Returns a representation for the parameter declarations. A return
18205 value of NULL indicates a parameter-declaration-clause consisting
18206 only of an ellipsis. */
18209 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18218 int auto_is_implicit_function_template_parm_p
;
18220 parser
->auto_is_implicit_function_template_parm_p
18221 = auto_is_implicit_function_template_parm_p
;
18223 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18227 if (!processing_specialization
18228 && !processing_template_parmlist
18229 && !processing_explicit_instantiation
)
18230 if (!current_function_decl
18231 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
18232 parser
->auto_is_implicit_function_template_parm_p
= true;
18234 /* Peek at the next token. */
18235 token
= cp_lexer_peek_token (parser
->lexer
);
18236 /* Check for trivial parameter-declaration-clauses. */
18237 if (token
->type
== CPP_ELLIPSIS
)
18239 /* Consume the `...' token. */
18240 cp_lexer_consume_token (parser
->lexer
);
18243 else if (token
->type
== CPP_CLOSE_PAREN
)
18244 /* There are no parameters. */
18246 #ifndef NO_IMPLICIT_EXTERN_C
18247 if (in_system_header_at (input_location
)
18248 && current_class_type
== NULL
18249 && current_lang_name
== lang_name_c
)
18253 return void_list_node
;
18255 /* Check for `(void)', too, which is a special case. */
18256 else if (token
->keyword
== RID_VOID
18257 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18258 == CPP_CLOSE_PAREN
))
18260 /* Consume the `void' token. */
18261 cp_lexer_consume_token (parser
->lexer
);
18262 /* There are no parameters. */
18263 return void_list_node
;
18266 /* Parse the parameter-declaration-list. */
18267 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18268 /* If a parse error occurred while parsing the
18269 parameter-declaration-list, then the entire
18270 parameter-declaration-clause is erroneous. */
18274 /* Peek at the next token. */
18275 token
= cp_lexer_peek_token (parser
->lexer
);
18276 /* If it's a `,', the clause should terminate with an ellipsis. */
18277 if (token
->type
== CPP_COMMA
)
18279 /* Consume the `,'. */
18280 cp_lexer_consume_token (parser
->lexer
);
18281 /* Expect an ellipsis. */
18283 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18285 /* It might also be `...' if the optional trailing `,' was
18287 else if (token
->type
== CPP_ELLIPSIS
)
18289 /* Consume the `...' token. */
18290 cp_lexer_consume_token (parser
->lexer
);
18291 /* And remember that we saw it. */
18295 ellipsis_p
= false;
18297 /* Finish the parameter list. */
18299 parameters
= chainon (parameters
, void_list_node
);
18304 /* Parse a parameter-declaration-list.
18306 parameter-declaration-list:
18307 parameter-declaration
18308 parameter-declaration-list , parameter-declaration
18310 Returns a representation of the parameter-declaration-list, as for
18311 cp_parser_parameter_declaration_clause. However, the
18312 `void_list_node' is never appended to the list. Upon return,
18313 *IS_ERROR will be true iff an error occurred. */
18316 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18318 tree parameters
= NULL_TREE
;
18319 tree
*tail
= ¶meters
;
18320 bool saved_in_unbraced_linkage_specification_p
;
18323 /* Assume all will go well. */
18325 /* The special considerations that apply to a function within an
18326 unbraced linkage specifications do not apply to the parameters
18327 to the function. */
18328 saved_in_unbraced_linkage_specification_p
18329 = parser
->in_unbraced_linkage_specification_p
;
18330 parser
->in_unbraced_linkage_specification_p
= false;
18332 /* Look for more parameters. */
18335 cp_parameter_declarator
*parameter
;
18336 tree decl
= error_mark_node
;
18337 bool parenthesized_p
= false;
18338 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
18339 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18340 (current_template_parms
)) : 0);
18342 /* Parse the parameter. */
18344 = cp_parser_parameter_declaration (parser
,
18345 /*template_parm_p=*/false,
18348 /* We don't know yet if the enclosing context is deprecated, so wait
18349 and warn in grokparms if appropriate. */
18350 deprecated_state
= DEPRECATED_SUPPRESS
;
18354 /* If a function parameter pack was specified and an implicit template
18355 parameter was introduced during cp_parser_parameter_declaration,
18356 change any implicit parameters introduced into packs. */
18357 if (parser
->implicit_template_parms
18358 && parameter
->declarator
18359 && parameter
->declarator
->parameter_pack_p
)
18361 int latest_template_parm_idx
= TREE_VEC_LENGTH
18362 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18364 if (latest_template_parm_idx
!= template_parm_idx
)
18365 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18366 (parameter
->decl_specifiers
.type
,
18367 template_parm_idx
, latest_template_parm_idx
);
18370 decl
= grokdeclarator (parameter
->declarator
,
18371 ¶meter
->decl_specifiers
,
18373 parameter
->default_argument
!= NULL_TREE
,
18374 ¶meter
->decl_specifiers
.attributes
);
18377 deprecated_state
= DEPRECATED_NORMAL
;
18379 /* If a parse error occurred parsing the parameter declaration,
18380 then the entire parameter-declaration-list is erroneous. */
18381 if (decl
== error_mark_node
)
18384 parameters
= error_mark_node
;
18388 if (parameter
->decl_specifiers
.attributes
)
18389 cplus_decl_attributes (&decl
,
18390 parameter
->decl_specifiers
.attributes
,
18392 if (DECL_NAME (decl
))
18393 decl
= pushdecl (decl
);
18395 if (decl
!= error_mark_node
)
18397 retrofit_lang_decl (decl
);
18398 DECL_PARM_INDEX (decl
) = ++index
;
18399 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18402 /* Add the new parameter to the list. */
18403 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18404 tail
= &TREE_CHAIN (*tail
);
18406 /* Peek at the next token. */
18407 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18408 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18409 /* These are for Objective-C++ */
18410 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18411 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18412 /* The parameter-declaration-list is complete. */
18414 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18418 /* Peek at the next token. */
18419 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18420 /* If it's an ellipsis, then the list is complete. */
18421 if (token
->type
== CPP_ELLIPSIS
)
18423 /* Otherwise, there must be more parameters. Consume the
18425 cp_lexer_consume_token (parser
->lexer
);
18426 /* When parsing something like:
18428 int i(float f, double d)
18430 we can tell after seeing the declaration for "f" that we
18431 are not looking at an initialization of a variable "i",
18432 but rather at the declaration of a function "i".
18434 Due to the fact that the parsing of template arguments
18435 (as specified to a template-id) requires backtracking we
18436 cannot use this technique when inside a template argument
18438 if (!parser
->in_template_argument_list_p
18439 && !parser
->in_type_id_in_expr_p
18440 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18441 /* However, a parameter-declaration of the form
18442 "float(f)" (which is a valid declaration of a
18443 parameter "f") can also be interpreted as an
18444 expression (the conversion of "f" to "float"). */
18445 && !parenthesized_p
)
18446 cp_parser_commit_to_tentative_parse (parser
);
18450 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18451 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18452 cp_parser_skip_to_closing_parenthesis (parser
,
18453 /*recovering=*/true,
18454 /*or_comma=*/false,
18455 /*consume_paren=*/false);
18460 parser
->in_unbraced_linkage_specification_p
18461 = saved_in_unbraced_linkage_specification_p
;
18463 /* Reset implicit_template_scope if we are about to leave the function
18464 parameter list that introduced it. Note that for out-of-line member
18465 definitions, there will be one or more class scopes before we get to
18466 the template parameter scope. */
18468 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18469 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
18471 while (maybe_its
->kind
== sk_class
)
18472 maybe_its
= maybe_its
->level_chain
;
18473 if (maybe_its
== its
)
18475 parser
->implicit_template_parms
= 0;
18476 parser
->implicit_template_scope
= 0;
18483 /* Parse a parameter declaration.
18485 parameter-declaration:
18486 decl-specifier-seq ... [opt] declarator
18487 decl-specifier-seq declarator = assignment-expression
18488 decl-specifier-seq ... [opt] abstract-declarator [opt]
18489 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18491 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18492 declares a template parameter. (In that case, a non-nested `>'
18493 token encountered during the parsing of the assignment-expression
18494 is not interpreted as a greater-than operator.)
18496 Returns a representation of the parameter, or NULL if an error
18497 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18498 true iff the declarator is of the form "(p)". */
18500 static cp_parameter_declarator
*
18501 cp_parser_parameter_declaration (cp_parser
*parser
,
18502 bool template_parm_p
,
18503 bool *parenthesized_p
)
18505 int declares_class_or_enum
;
18506 cp_decl_specifier_seq decl_specifiers
;
18507 cp_declarator
*declarator
;
18508 tree default_argument
;
18509 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18510 const char *saved_message
;
18512 /* In a template parameter, `>' is not an operator.
18516 When parsing a default template-argument for a non-type
18517 template-parameter, the first non-nested `>' is taken as the end
18518 of the template parameter-list rather than a greater-than
18521 /* Type definitions may not appear in parameter types. */
18522 saved_message
= parser
->type_definition_forbidden_message
;
18523 parser
->type_definition_forbidden_message
18524 = G_("types may not be defined in parameter types");
18526 /* Parse the declaration-specifiers. */
18527 cp_parser_decl_specifier_seq (parser
,
18528 CP_PARSER_FLAGS_NONE
,
18530 &declares_class_or_enum
);
18532 /* Complain about missing 'typename' or other invalid type names. */
18533 if (!decl_specifiers
.any_type_specifiers_p
18534 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18535 decl_specifiers
.type
= error_mark_node
;
18537 /* If an error occurred, there's no reason to attempt to parse the
18538 rest of the declaration. */
18539 if (cp_parser_error_occurred (parser
))
18541 parser
->type_definition_forbidden_message
= saved_message
;
18545 /* Peek at the next token. */
18546 token
= cp_lexer_peek_token (parser
->lexer
);
18548 /* If the next token is a `)', `,', `=', `>', or `...', then there
18549 is no declarator. However, when variadic templates are enabled,
18550 there may be a declarator following `...'. */
18551 if (token
->type
== CPP_CLOSE_PAREN
18552 || token
->type
== CPP_COMMA
18553 || token
->type
== CPP_EQ
18554 || token
->type
== CPP_GREATER
)
18557 if (parenthesized_p
)
18558 *parenthesized_p
= false;
18560 /* Otherwise, there should be a declarator. */
18563 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18564 parser
->default_arg_ok_p
= false;
18566 /* After seeing a decl-specifier-seq, if the next token is not a
18567 "(", there is no possibility that the code is a valid
18568 expression. Therefore, if parsing tentatively, we commit at
18570 if (!parser
->in_template_argument_list_p
18571 /* In an expression context, having seen:
18575 we cannot be sure whether we are looking at a
18576 function-type (taking a "char" as a parameter) or a cast
18577 of some object of type "char" to "int". */
18578 && !parser
->in_type_id_in_expr_p
18579 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18580 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
18581 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
18582 cp_parser_commit_to_tentative_parse (parser
);
18583 /* Parse the declarator. */
18584 declarator_token_start
= token
;
18585 declarator
= cp_parser_declarator (parser
,
18586 CP_PARSER_DECLARATOR_EITHER
,
18587 /*ctor_dtor_or_conv_p=*/NULL
,
18589 /*member_p=*/false);
18590 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18591 /* After the declarator, allow more attributes. */
18592 decl_specifiers
.attributes
18593 = chainon (decl_specifiers
.attributes
,
18594 cp_parser_attributes_opt (parser
));
18597 /* If the next token is an ellipsis, and we have not seen a
18598 declarator name, and the type of the declarator contains parameter
18599 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18600 a parameter pack expansion expression. Otherwise, leave the
18601 ellipsis for a C-style variadic function. */
18602 token
= cp_lexer_peek_token (parser
->lexer
);
18603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18605 tree type
= decl_specifiers
.type
;
18607 if (type
&& DECL_P (type
))
18608 type
= TREE_TYPE (type
);
18611 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
18612 && declarator_can_be_parameter_pack (declarator
)
18613 && (!declarator
|| !declarator
->parameter_pack_p
)
18614 && uses_parameter_packs (type
))
18616 /* Consume the `...'. */
18617 cp_lexer_consume_token (parser
->lexer
);
18618 maybe_warn_variadic_templates ();
18620 /* Build a pack expansion type */
18622 declarator
->parameter_pack_p
= true;
18624 decl_specifiers
.type
= make_pack_expansion (type
);
18628 /* The restriction on defining new types applies only to the type
18629 of the parameter, not to the default argument. */
18630 parser
->type_definition_forbidden_message
= saved_message
;
18632 /* If the next token is `=', then process a default argument. */
18633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18635 token
= cp_lexer_peek_token (parser
->lexer
);
18636 /* If we are defining a class, then the tokens that make up the
18637 default argument must be saved and processed later. */
18638 if (!template_parm_p
&& at_class_scope_p ()
18639 && TYPE_BEING_DEFINED (current_class_type
)
18640 && !LAMBDA_TYPE_P (current_class_type
))
18641 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
18642 /* Outside of a class definition, we can just parse the
18643 assignment-expression. */
18646 = cp_parser_default_argument (parser
, template_parm_p
);
18648 if (!parser
->default_arg_ok_p
)
18650 if (flag_permissive
)
18651 warning (0, "deprecated use of default argument for parameter of non-function");
18654 error_at (token
->location
,
18655 "default arguments are only "
18656 "permitted for function parameters");
18657 default_argument
= NULL_TREE
;
18660 else if ((declarator
&& declarator
->parameter_pack_p
)
18661 || (decl_specifiers
.type
18662 && PACK_EXPANSION_P (decl_specifiers
.type
)))
18664 /* Find the name of the parameter pack. */
18665 cp_declarator
*id_declarator
= declarator
;
18666 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
18667 id_declarator
= id_declarator
->declarator
;
18669 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
18670 error_at (declarator_token_start
->location
,
18672 ? G_("template parameter pack %qD "
18673 "cannot have a default argument")
18674 : G_("parameter pack %qD cannot have "
18675 "a default argument"),
18676 id_declarator
->u
.id
.unqualified_name
);
18678 error_at (declarator_token_start
->location
,
18680 ? G_("template parameter pack cannot have "
18681 "a default argument")
18682 : G_("parameter pack cannot have a "
18683 "default argument"));
18685 default_argument
= NULL_TREE
;
18689 default_argument
= NULL_TREE
;
18691 return make_parameter_declarator (&decl_specifiers
,
18696 /* Parse a default argument and return it.
18698 TEMPLATE_PARM_P is true if this is a default argument for a
18699 non-type template parameter. */
18701 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
18703 tree default_argument
= NULL_TREE
;
18704 bool saved_greater_than_is_operator_p
;
18705 bool saved_local_variables_forbidden_p
;
18706 bool non_constant_p
, is_direct_init
;
18708 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18710 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
18711 parser
->greater_than_is_operator_p
= !template_parm_p
;
18712 /* Local variable names (and the `this' keyword) may not
18713 appear in a default argument. */
18714 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
18715 parser
->local_variables_forbidden_p
= true;
18716 /* Parse the assignment-expression. */
18717 if (template_parm_p
)
18718 push_deferring_access_checks (dk_no_deferred
);
18719 tree saved_class_ptr
= NULL_TREE
;
18720 tree saved_class_ref
= NULL_TREE
;
18721 /* The "this" pointer is not valid in a default argument. */
18724 saved_class_ptr
= current_class_ptr
;
18725 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
18726 saved_class_ref
= current_class_ref
;
18727 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
18730 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
18731 /* Restore the "this" pointer. */
18734 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
18735 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
18737 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
18738 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18739 if (template_parm_p
)
18740 pop_deferring_access_checks ();
18741 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
18742 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
18744 return default_argument
;
18747 /* Parse a function-body.
18750 compound_statement */
18753 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
18755 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
18758 /* Parse a ctor-initializer-opt followed by a function-body. Return
18759 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18760 is true we are parsing a function-try-block. */
18763 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
18764 bool in_function_try_block
)
18767 bool ctor_initializer_p
;
18768 const bool check_body_p
=
18769 DECL_CONSTRUCTOR_P (current_function_decl
)
18770 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
18773 /* Begin the function body. */
18774 body
= begin_function_body ();
18775 /* Parse the optional ctor-initializer. */
18776 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
18778 /* If we're parsing a constexpr constructor definition, we need
18779 to check that the constructor body is indeed empty. However,
18780 before we get to cp_parser_function_body lot of junk has been
18781 generated, so we can't just check that we have an empty block.
18782 Rather we take a snapshot of the outermost block, and check whether
18783 cp_parser_function_body changed its state. */
18786 list
= cur_stmt_list
;
18787 if (STATEMENT_LIST_TAIL (list
))
18788 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
18790 /* Parse the function-body. */
18791 cp_parser_function_body (parser
, in_function_try_block
);
18793 check_constexpr_ctor_body (last
, list
);
18794 /* Finish the function body. */
18795 finish_function_body (body
);
18797 return ctor_initializer_p
;
18800 /* Parse an initializer.
18803 = initializer-clause
18804 ( expression-list )
18806 Returns an expression representing the initializer. If no
18807 initializer is present, NULL_TREE is returned.
18809 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18810 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18811 set to TRUE if there is no initializer present. If there is an
18812 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18813 is set to true; otherwise it is set to false. */
18816 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
18817 bool* non_constant_p
)
18822 /* Peek at the next token. */
18823 token
= cp_lexer_peek_token (parser
->lexer
);
18825 /* Let our caller know whether or not this initializer was
18827 *is_direct_init
= (token
->type
!= CPP_EQ
);
18828 /* Assume that the initializer is constant. */
18829 *non_constant_p
= false;
18831 if (token
->type
== CPP_EQ
)
18833 /* Consume the `='. */
18834 cp_lexer_consume_token (parser
->lexer
);
18835 /* Parse the initializer-clause. */
18836 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
18838 else if (token
->type
== CPP_OPEN_PAREN
)
18840 vec
<tree
, va_gc
> *vec
;
18841 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
18843 /*allow_expansion_p=*/true,
18846 return error_mark_node
;
18847 init
= build_tree_list_vec (vec
);
18848 release_tree_vector (vec
);
18850 else if (token
->type
== CPP_OPEN_BRACE
)
18852 cp_lexer_set_source_position (parser
->lexer
);
18853 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18854 init
= cp_parser_braced_list (parser
, non_constant_p
);
18855 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
18859 /* Anything else is an error. */
18860 cp_parser_error (parser
, "expected initializer");
18861 init
= error_mark_node
;
18867 /* Parse an initializer-clause.
18869 initializer-clause:
18870 assignment-expression
18873 Returns an expression representing the initializer.
18875 If the `assignment-expression' production is used the value
18876 returned is simply a representation for the expression.
18878 Otherwise, calls cp_parser_braced_list. */
18881 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
18885 /* Assume the expression is constant. */
18886 *non_constant_p
= false;
18888 /* If it is not a `{', then we are looking at an
18889 assignment-expression. */
18890 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
18893 = cp_parser_constant_expression (parser
,
18894 /*allow_non_constant_p=*/true,
18898 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
18900 return initializer
;
18903 /* Parse a brace-enclosed initializer list.
18906 { initializer-list , [opt] }
18909 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18910 the elements of the initializer-list (or NULL, if the last
18911 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18912 NULL_TREE. There is no way to detect whether or not the optional
18913 trailing `,' was provided. NON_CONSTANT_P is as for
18914 cp_parser_initializer. */
18917 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
18921 /* Consume the `{' token. */
18922 cp_lexer_consume_token (parser
->lexer
);
18923 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18924 initializer
= make_node (CONSTRUCTOR
);
18925 /* If it's not a `}', then there is a non-trivial initializer. */
18926 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
18928 /* Parse the initializer list. */
18929 CONSTRUCTOR_ELTS (initializer
)
18930 = cp_parser_initializer_list (parser
, non_constant_p
);
18931 /* A trailing `,' token is allowed. */
18932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18933 cp_lexer_consume_token (parser
->lexer
);
18936 *non_constant_p
= false;
18937 /* Now, there should be a trailing `}'. */
18938 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18939 TREE_TYPE (initializer
) = init_list_type_node
;
18940 return initializer
;
18943 /* Parse an initializer-list.
18946 initializer-clause ... [opt]
18947 initializer-list , initializer-clause ... [opt]
18952 designation initializer-clause ...[opt]
18953 initializer-list , designation initializer-clause ...[opt]
18958 [ constant-expression ] =
18960 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18961 for the initializer. If the INDEX of the elt is non-NULL, it is the
18962 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18963 as for cp_parser_initializer. */
18965 static vec
<constructor_elt
, va_gc
> *
18966 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
18968 vec
<constructor_elt
, va_gc
> *v
= NULL
;
18970 /* Assume all of the expressions are constant. */
18971 *non_constant_p
= false;
18973 /* Parse the rest of the list. */
18979 bool clause_non_constant_p
;
18981 /* If the next token is an identifier and the following one is a
18982 colon, we are looking at the GNU designated-initializer
18984 if (cp_parser_allow_gnu_extensions_p (parser
)
18985 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
18986 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
18988 /* Warn the user that they are using an extension. */
18989 pedwarn (input_location
, OPT_Wpedantic
,
18990 "ISO C++ does not allow designated initializers");
18991 /* Consume the identifier. */
18992 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18993 /* Consume the `:'. */
18994 cp_lexer_consume_token (parser
->lexer
);
18996 /* Also handle the C99 syntax, '. id ='. */
18997 else if (cp_parser_allow_gnu_extensions_p (parser
)
18998 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
18999 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
19000 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
19002 /* Warn the user that they are using an extension. */
19003 pedwarn (input_location
, OPT_Wpedantic
,
19004 "ISO C++ does not allow C99 designated initializers");
19005 /* Consume the `.'. */
19006 cp_lexer_consume_token (parser
->lexer
);
19007 /* Consume the identifier. */
19008 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19009 /* Consume the `='. */
19010 cp_lexer_consume_token (parser
->lexer
);
19012 /* Also handle C99 array designators, '[ const ] ='. */
19013 else if (cp_parser_allow_gnu_extensions_p (parser
)
19014 && !c_dialect_objc ()
19015 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19017 /* In C++11, [ could start a lambda-introducer. */
19018 bool non_const
= false;
19020 cp_parser_parse_tentatively (parser
);
19021 cp_lexer_consume_token (parser
->lexer
);
19022 designator
= cp_parser_constant_expression (parser
, true, &non_const
);
19023 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19024 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19025 if (!cp_parser_parse_definitely (parser
))
19026 designator
= NULL_TREE
;
19027 else if (non_const
)
19028 require_potential_rvalue_constant_expression (designator
);
19031 designator
= NULL_TREE
;
19033 /* Parse the initializer. */
19034 initializer
= cp_parser_initializer_clause (parser
,
19035 &clause_non_constant_p
);
19036 /* If any clause is non-constant, so is the entire initializer. */
19037 if (clause_non_constant_p
)
19038 *non_constant_p
= true;
19040 /* If we have an ellipsis, this is an initializer pack
19042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19044 /* Consume the `...'. */
19045 cp_lexer_consume_token (parser
->lexer
);
19047 /* Turn the initializer into an initializer expansion. */
19048 initializer
= make_pack_expansion (initializer
);
19051 /* Add it to the vector. */
19052 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
19054 /* If the next token is not a comma, we have reached the end of
19056 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19059 /* Peek at the next token. */
19060 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19061 /* If the next token is a `}', then we're still done. An
19062 initializer-clause can have a trailing `,' after the
19063 initializer-list and before the closing `}'. */
19064 if (token
->type
== CPP_CLOSE_BRACE
)
19067 /* Consume the `,' token. */
19068 cp_lexer_consume_token (parser
->lexer
);
19074 /* Classes [gram.class] */
19076 /* Parse a class-name.
19082 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19083 to indicate that names looked up in dependent types should be
19084 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19085 keyword has been used to indicate that the name that appears next
19086 is a template. TAG_TYPE indicates the explicit tag given before
19087 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19088 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19089 is the class being defined in a class-head.
19091 Returns the TYPE_DECL representing the class. */
19094 cp_parser_class_name (cp_parser
*parser
,
19095 bool typename_keyword_p
,
19096 bool template_keyword_p
,
19097 enum tag_types tag_type
,
19098 bool check_dependency_p
,
19100 bool is_declaration
)
19106 tree identifier
= NULL_TREE
;
19108 /* All class-names start with an identifier. */
19109 token
= cp_lexer_peek_token (parser
->lexer
);
19110 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
19112 cp_parser_error (parser
, "expected class-name");
19113 return error_mark_node
;
19116 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19117 to a template-id, so we save it here. */
19118 scope
= parser
->scope
;
19119 if (scope
== error_mark_node
)
19120 return error_mark_node
;
19122 /* Any name names a type if we're following the `typename' keyword
19123 in a qualified name where the enclosing scope is type-dependent. */
19124 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
19125 && dependent_type_p (scope
));
19126 /* Handle the common case (an identifier, but not a template-id)
19128 if (token
->type
== CPP_NAME
19129 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
19131 cp_token
*identifier_token
;
19134 /* Look for the identifier. */
19135 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
19136 ambiguous_p
= identifier_token
->error_reported
;
19137 identifier
= cp_parser_identifier (parser
);
19138 /* If the next token isn't an identifier, we are certainly not
19139 looking at a class-name. */
19140 if (identifier
== error_mark_node
)
19141 decl
= error_mark_node
;
19142 /* If we know this is a type-name, there's no need to look it
19144 else if (typename_p
)
19148 tree ambiguous_decls
;
19149 /* If we already know that this lookup is ambiguous, then
19150 we've already issued an error message; there's no reason
19154 cp_parser_simulate_error (parser
);
19155 return error_mark_node
;
19157 /* If the next token is a `::', then the name must be a type
19160 [basic.lookup.qual]
19162 During the lookup for a name preceding the :: scope
19163 resolution operator, object, function, and enumerator
19164 names are ignored. */
19165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19166 tag_type
= typename_type
;
19167 /* Look up the name. */
19168 decl
= cp_parser_lookup_name (parser
, identifier
,
19170 /*is_template=*/false,
19171 /*is_namespace=*/false,
19172 check_dependency_p
,
19174 identifier_token
->location
);
19175 if (ambiguous_decls
)
19177 if (cp_parser_parsing_tentatively (parser
))
19178 cp_parser_simulate_error (parser
);
19179 return error_mark_node
;
19185 /* Try a template-id. */
19186 decl
= cp_parser_template_id (parser
, template_keyword_p
,
19187 check_dependency_p
,
19190 if (decl
== error_mark_node
)
19191 return error_mark_node
;
19194 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
19196 /* If this is a typename, create a TYPENAME_TYPE. */
19197 if (typename_p
&& decl
!= error_mark_node
)
19199 decl
= make_typename_type (scope
, decl
, typename_type
,
19200 /*complain=*/tf_error
);
19201 if (decl
!= error_mark_node
)
19202 decl
= TYPE_NAME (decl
);
19205 decl
= strip_using_decl (decl
);
19207 /* Check to see that it is really the name of a class. */
19208 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19209 && identifier_p (TREE_OPERAND (decl
, 0))
19210 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19211 /* Situations like this:
19213 template <typename T> struct A {
19214 typename T::template X<int>::I i;
19217 are problematic. Is `T::template X<int>' a class-name? The
19218 standard does not seem to be definitive, but there is no other
19219 valid interpretation of the following `::'. Therefore, those
19220 names are considered class-names. */
19222 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19223 if (decl
!= error_mark_node
)
19224 decl
= TYPE_NAME (decl
);
19226 else if (TREE_CODE (decl
) != TYPE_DECL
19227 || TREE_TYPE (decl
) == error_mark_node
19228 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19229 /* In Objective-C 2.0, a classname followed by '.' starts a
19230 dot-syntax expression, and it's not a type-name. */
19231 || (c_dialect_objc ()
19232 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19233 && objc_is_class_name (decl
)))
19234 decl
= error_mark_node
;
19236 if (decl
== error_mark_node
)
19237 cp_parser_error (parser
, "expected class-name");
19238 else if (identifier
&& !parser
->scope
)
19239 maybe_note_name_used_in_class (identifier
, decl
);
19244 /* Parse a class-specifier.
19247 class-head { member-specification [opt] }
19249 Returns the TREE_TYPE representing the class. */
19252 cp_parser_class_specifier_1 (cp_parser
* parser
)
19255 tree attributes
= NULL_TREE
;
19256 bool nested_name_specifier_p
;
19257 unsigned saved_num_template_parameter_lists
;
19258 bool saved_in_function_body
;
19259 unsigned char in_statement
;
19260 bool in_switch_statement_p
;
19261 bool saved_in_unbraced_linkage_specification_p
;
19262 tree old_scope
= NULL_TREE
;
19263 tree scope
= NULL_TREE
;
19264 cp_token
*closing_brace
;
19266 push_deferring_access_checks (dk_no_deferred
);
19268 /* Parse the class-head. */
19269 type
= cp_parser_class_head (parser
,
19270 &nested_name_specifier_p
);
19271 /* If the class-head was a semantic disaster, skip the entire body
19275 cp_parser_skip_to_end_of_block_or_statement (parser
);
19276 pop_deferring_access_checks ();
19277 return error_mark_node
;
19280 /* Look for the `{'. */
19281 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19283 pop_deferring_access_checks ();
19284 return error_mark_node
;
19287 cp_ensure_no_omp_declare_simd (parser
);
19289 /* Issue an error message if type-definitions are forbidden here. */
19290 cp_parser_check_type_definition (parser
);
19291 /* Remember that we are defining one more class. */
19292 ++parser
->num_classes_being_defined
;
19293 /* Inside the class, surrounding template-parameter-lists do not
19295 saved_num_template_parameter_lists
19296 = parser
->num_template_parameter_lists
;
19297 parser
->num_template_parameter_lists
= 0;
19298 /* We are not in a function body. */
19299 saved_in_function_body
= parser
->in_function_body
;
19300 parser
->in_function_body
= false;
19301 /* Or in a loop. */
19302 in_statement
= parser
->in_statement
;
19303 parser
->in_statement
= 0;
19304 /* Or in a switch. */
19305 in_switch_statement_p
= parser
->in_switch_statement_p
;
19306 parser
->in_switch_statement_p
= false;
19307 /* We are not immediately inside an extern "lang" block. */
19308 saved_in_unbraced_linkage_specification_p
19309 = parser
->in_unbraced_linkage_specification_p
;
19310 parser
->in_unbraced_linkage_specification_p
= false;
19312 /* Start the class. */
19313 if (nested_name_specifier_p
)
19315 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19316 old_scope
= push_inner_scope (scope
);
19318 type
= begin_class_definition (type
);
19320 if (type
== error_mark_node
)
19321 /* If the type is erroneous, skip the entire body of the class. */
19322 cp_parser_skip_to_closing_brace (parser
);
19324 /* Parse the member-specification. */
19325 cp_parser_member_specification_opt (parser
);
19327 /* Look for the trailing `}'. */
19328 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19329 /* Look for trailing attributes to apply to this class. */
19330 if (cp_parser_allow_gnu_extensions_p (parser
))
19331 attributes
= cp_parser_gnu_attributes_opt (parser
);
19332 if (type
!= error_mark_node
)
19333 type
= finish_struct (type
, attributes
);
19334 if (nested_name_specifier_p
)
19335 pop_inner_scope (old_scope
, scope
);
19337 /* We've finished a type definition. Check for the common syntax
19338 error of forgetting a semicolon after the definition. We need to
19339 be careful, as we can't just check for not-a-semicolon and be done
19340 with it; the user might have typed:
19342 class X { } c = ...;
19343 class X { } *p = ...;
19345 and so forth. Instead, enumerate all the possible tokens that
19346 might follow this production; if we don't see one of them, then
19347 complain and silently insert the semicolon. */
19349 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19350 bool want_semicolon
= true;
19352 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19353 /* Don't try to parse c++11 attributes here. As per the
19354 grammar, that should be a task for
19355 cp_parser_decl_specifier_seq. */
19356 want_semicolon
= false;
19358 switch (token
->type
)
19361 case CPP_SEMICOLON
:
19364 case CPP_OPEN_PAREN
:
19365 case CPP_CLOSE_PAREN
:
19367 want_semicolon
= false;
19370 /* While it's legal for type qualifiers and storage class
19371 specifiers to follow type definitions in the grammar, only
19372 compiler testsuites contain code like that. Assume that if
19373 we see such code, then what we're really seeing is a case
19377 const <type> var = ...;
19382 static <type> func (...) ...
19384 i.e. the qualifier or specifier applies to the next
19385 declaration. To do so, however, we need to look ahead one
19386 more token to see if *that* token is a type specifier.
19388 This code could be improved to handle:
19391 static const <type> var = ...; */
19393 if (keyword_is_decl_specifier (token
->keyword
))
19395 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19397 /* Handling user-defined types here would be nice, but very
19400 = (lookahead
->type
== CPP_KEYWORD
19401 && keyword_begins_type_specifier (lookahead
->keyword
));
19408 /* If we don't have a type, then something is very wrong and we
19409 shouldn't try to do anything clever. Likewise for not seeing the
19411 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19413 cp_token_position prev
19414 = cp_lexer_previous_token_position (parser
->lexer
);
19415 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19416 location_t loc
= prev_token
->location
;
19418 if (CLASSTYPE_DECLARED_CLASS (type
))
19419 error_at (loc
, "expected %<;%> after class definition");
19420 else if (TREE_CODE (type
) == RECORD_TYPE
)
19421 error_at (loc
, "expected %<;%> after struct definition");
19422 else if (TREE_CODE (type
) == UNION_TYPE
)
19423 error_at (loc
, "expected %<;%> after union definition");
19425 gcc_unreachable ();
19427 /* Unget one token and smash it to look as though we encountered
19428 a semicolon in the input stream. */
19429 cp_lexer_set_token_position (parser
->lexer
, prev
);
19430 token
= cp_lexer_peek_token (parser
->lexer
);
19431 token
->type
= CPP_SEMICOLON
;
19432 token
->keyword
= RID_MAX
;
19436 /* If this class is not itself within the scope of another class,
19437 then we need to parse the bodies of all of the queued function
19438 definitions. Note that the queued functions defined in a class
19439 are not always processed immediately following the
19440 class-specifier for that class. Consider:
19443 struct B { void f() { sizeof (A); } };
19446 If `f' were processed before the processing of `A' were
19447 completed, there would be no way to compute the size of `A'.
19448 Note that the nesting we are interested in here is lexical --
19449 not the semantic nesting given by TYPE_CONTEXT. In particular,
19452 struct A { struct B; };
19453 struct A::B { void f() { } };
19455 there is no need to delay the parsing of `A::B::f'. */
19456 if (--parser
->num_classes_being_defined
== 0)
19459 tree class_type
= NULL_TREE
;
19460 tree pushed_scope
= NULL_TREE
;
19462 cp_default_arg_entry
*e
;
19463 tree save_ccp
, save_ccr
;
19465 /* In a first pass, parse default arguments to the functions.
19466 Then, in a second pass, parse the bodies of the functions.
19467 This two-phased approach handles cases like:
19475 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
19478 /* If there are default arguments that have not yet been processed,
19479 take care of them now. */
19480 if (class_type
!= e
->class_type
)
19483 pop_scope (pushed_scope
);
19484 class_type
= e
->class_type
;
19485 pushed_scope
= push_scope (class_type
);
19487 /* Make sure that any template parameters are in scope. */
19488 maybe_begin_member_template_processing (decl
);
19489 /* Parse the default argument expressions. */
19490 cp_parser_late_parsing_default_args (parser
, decl
);
19491 /* Remove any template parameters from the symbol table. */
19492 maybe_end_member_template_processing ();
19494 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
19495 /* Now parse any NSDMIs. */
19496 save_ccp
= current_class_ptr
;
19497 save_ccr
= current_class_ref
;
19498 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
19500 if (class_type
!= DECL_CONTEXT (decl
))
19503 pop_scope (pushed_scope
);
19504 class_type
= DECL_CONTEXT (decl
);
19505 pushed_scope
= push_scope (class_type
);
19507 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
19508 cp_parser_late_parsing_nsdmi (parser
, decl
);
19510 vec_safe_truncate (unparsed_nsdmis
, 0);
19511 current_class_ptr
= save_ccp
;
19512 current_class_ref
= save_ccr
;
19514 pop_scope (pushed_scope
);
19515 /* Now parse the body of the functions. */
19518 /* OpenMP UDRs need to be parsed before all other functions. */
19519 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19520 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
19521 cp_parser_late_parsing_for_member (parser
, decl
);
19522 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19523 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
19524 cp_parser_late_parsing_for_member (parser
, decl
);
19527 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19528 cp_parser_late_parsing_for_member (parser
, decl
);
19529 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
19532 /* Put back any saved access checks. */
19533 pop_deferring_access_checks ();
19535 /* Restore saved state. */
19536 parser
->in_switch_statement_p
= in_switch_statement_p
;
19537 parser
->in_statement
= in_statement
;
19538 parser
->in_function_body
= saved_in_function_body
;
19539 parser
->num_template_parameter_lists
19540 = saved_num_template_parameter_lists
;
19541 parser
->in_unbraced_linkage_specification_p
19542 = saved_in_unbraced_linkage_specification_p
;
19548 cp_parser_class_specifier (cp_parser
* parser
)
19551 timevar_push (TV_PARSE_STRUCT
);
19552 ret
= cp_parser_class_specifier_1 (parser
);
19553 timevar_pop (TV_PARSE_STRUCT
);
19557 /* Parse a class-head.
19560 class-key identifier [opt] base-clause [opt]
19561 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19562 class-key nested-name-specifier [opt] template-id
19565 class-virt-specifier:
19569 class-key attributes identifier [opt] base-clause [opt]
19570 class-key attributes nested-name-specifier identifier base-clause [opt]
19571 class-key attributes nested-name-specifier [opt] template-id
19574 Upon return BASES is initialized to the list of base classes (or
19575 NULL, if there are none) in the same form returned by
19576 cp_parser_base_clause.
19578 Returns the TYPE of the indicated class. Sets
19579 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19580 involving a nested-name-specifier was used, and FALSE otherwise.
19582 Returns error_mark_node if this is not a class-head.
19584 Returns NULL_TREE if the class-head is syntactically valid, but
19585 semantically invalid in a way that means we should skip the entire
19586 body of the class. */
19589 cp_parser_class_head (cp_parser
* parser
,
19590 bool* nested_name_specifier_p
)
19592 tree nested_name_specifier
;
19593 enum tag_types class_key
;
19594 tree id
= NULL_TREE
;
19595 tree type
= NULL_TREE
;
19598 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
19599 bool template_id_p
= false;
19600 bool qualified_p
= false;
19601 bool invalid_nested_name_p
= false;
19602 bool invalid_explicit_specialization_p
= false;
19603 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
19604 tree pushed_scope
= NULL_TREE
;
19605 unsigned num_templates
;
19606 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
19607 /* Assume no nested-name-specifier will be present. */
19608 *nested_name_specifier_p
= false;
19609 /* Assume no template parameter lists will be used in defining the
19612 parser
->colon_corrects_to_scope_p
= false;
19614 /* Look for the class-key. */
19615 class_key
= cp_parser_class_key (parser
);
19616 if (class_key
== none_type
)
19617 return error_mark_node
;
19619 /* Parse the attributes. */
19620 attributes
= cp_parser_attributes_opt (parser
);
19622 /* If the next token is `::', that is invalid -- but sometimes
19623 people do try to write:
19627 Handle this gracefully by accepting the extra qualifier, and then
19628 issuing an error about it later if this really is a
19629 class-head. If it turns out just to be an elaborated type
19630 specifier, remain silent. */
19631 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
19632 qualified_p
= true;
19634 push_deferring_access_checks (dk_no_check
);
19636 /* Determine the name of the class. Begin by looking for an
19637 optional nested-name-specifier. */
19638 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
19639 nested_name_specifier
19640 = cp_parser_nested_name_specifier_opt (parser
,
19641 /*typename_keyword_p=*/false,
19642 /*check_dependency_p=*/false,
19644 /*is_declaration=*/false);
19645 /* If there was a nested-name-specifier, then there *must* be an
19647 if (nested_name_specifier
)
19649 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19650 /* Although the grammar says `identifier', it really means
19651 `class-name' or `template-name'. You are only allowed to
19652 define a class that has already been declared with this
19655 The proposed resolution for Core Issue 180 says that wherever
19656 you see `class T::X' you should treat `X' as a type-name.
19658 It is OK to define an inaccessible class; for example:
19660 class A { class B; };
19663 We do not know if we will see a class-name, or a
19664 template-name. We look for a class-name first, in case the
19665 class-name is a template-id; if we looked for the
19666 template-name first we would stop after the template-name. */
19667 cp_parser_parse_tentatively (parser
);
19668 type
= cp_parser_class_name (parser
,
19669 /*typename_keyword_p=*/false,
19670 /*template_keyword_p=*/false,
19672 /*check_dependency_p=*/false,
19673 /*class_head_p=*/true,
19674 /*is_declaration=*/false);
19675 /* If that didn't work, ignore the nested-name-specifier. */
19676 if (!cp_parser_parse_definitely (parser
))
19678 invalid_nested_name_p
= true;
19679 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19680 id
= cp_parser_identifier (parser
);
19681 if (id
== error_mark_node
)
19684 /* If we could not find a corresponding TYPE, treat this
19685 declaration like an unqualified declaration. */
19686 if (type
== error_mark_node
)
19687 nested_name_specifier
= NULL_TREE
;
19688 /* Otherwise, count the number of templates used in TYPE and its
19689 containing scopes. */
19694 for (scope
= TREE_TYPE (type
);
19695 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
19696 scope
= get_containing_scope (scope
))
19698 && CLASS_TYPE_P (scope
)
19699 && CLASSTYPE_TEMPLATE_INFO (scope
)
19700 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
19701 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
19702 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
19706 /* Otherwise, the identifier is optional. */
19709 /* We don't know whether what comes next is a template-id,
19710 an identifier, or nothing at all. */
19711 cp_parser_parse_tentatively (parser
);
19712 /* Check for a template-id. */
19713 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19714 id
= cp_parser_template_id (parser
,
19715 /*template_keyword_p=*/false,
19716 /*check_dependency_p=*/true,
19718 /*is_declaration=*/true);
19719 /* If that didn't work, it could still be an identifier. */
19720 if (!cp_parser_parse_definitely (parser
))
19722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19724 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19725 id
= cp_parser_identifier (parser
);
19732 template_id_p
= true;
19737 pop_deferring_access_checks ();
19741 cp_parser_check_for_invalid_template_id (parser
, id
,
19743 type_start_token
->location
);
19745 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19747 /* If it's not a `:' or a `{' then we can't really be looking at a
19748 class-head, since a class-head only appears as part of a
19749 class-specifier. We have to detect this situation before calling
19750 xref_tag, since that has irreversible side-effects. */
19751 if (!cp_parser_next_token_starts_class_definition_p (parser
))
19753 cp_parser_error (parser
, "expected %<{%> or %<:%>");
19754 type
= error_mark_node
;
19758 /* At this point, we're going ahead with the class-specifier, even
19759 if some other problem occurs. */
19760 cp_parser_commit_to_tentative_parse (parser
);
19761 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
19763 cp_parser_error (parser
,
19764 "cannot specify %<override%> for a class");
19765 type
= error_mark_node
;
19768 /* Issue the error about the overly-qualified name now. */
19771 cp_parser_error (parser
,
19772 "global qualification of class name is invalid");
19773 type
= error_mark_node
;
19776 else if (invalid_nested_name_p
)
19778 cp_parser_error (parser
,
19779 "qualified name does not name a class");
19780 type
= error_mark_node
;
19783 else if (nested_name_specifier
)
19787 /* Reject typedef-names in class heads. */
19788 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
19790 error_at (type_start_token
->location
,
19791 "invalid class name in declaration of %qD",
19797 /* Figure out in what scope the declaration is being placed. */
19798 scope
= current_scope ();
19799 /* If that scope does not contain the scope in which the
19800 class was originally declared, the program is invalid. */
19801 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
19803 if (at_namespace_scope_p ())
19804 error_at (type_start_token
->location
,
19805 "declaration of %qD in namespace %qD which does not "
19807 type
, scope
, nested_name_specifier
);
19809 error_at (type_start_token
->location
,
19810 "declaration of %qD in %qD which does not enclose %qD",
19811 type
, scope
, nested_name_specifier
);
19817 A declarator-id shall not be qualified except for the
19818 definition of a ... nested class outside of its class
19819 ... [or] the definition or explicit instantiation of a
19820 class member of a namespace outside of its namespace. */
19821 if (scope
== nested_name_specifier
)
19823 permerror (nested_name_specifier_token_start
->location
,
19824 "extra qualification not allowed");
19825 nested_name_specifier
= NULL_TREE
;
19829 /* An explicit-specialization must be preceded by "template <>". If
19830 it is not, try to recover gracefully. */
19831 if (at_namespace_scope_p ()
19832 && parser
->num_template_parameter_lists
== 0
19835 error_at (type_start_token
->location
,
19836 "an explicit specialization must be preceded by %<template <>%>");
19837 invalid_explicit_specialization_p
= true;
19838 /* Take the same action that would have been taken by
19839 cp_parser_explicit_specialization. */
19840 ++parser
->num_template_parameter_lists
;
19841 begin_specialization ();
19843 /* There must be no "return" statements between this point and the
19844 end of this function; set "type "to the correct return value and
19845 use "goto done;" to return. */
19846 /* Make sure that the right number of template parameters were
19848 if (!cp_parser_check_template_parameters (parser
, num_templates
,
19849 type_start_token
->location
,
19850 /*declarator=*/NULL
))
19852 /* If something went wrong, there is no point in even trying to
19853 process the class-definition. */
19858 /* Look up the type. */
19861 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
19862 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
19863 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
19865 error_at (type_start_token
->location
,
19866 "function template %qD redeclared as a class template", id
);
19867 type
= error_mark_node
;
19871 type
= TREE_TYPE (id
);
19872 type
= maybe_process_partial_specialization (type
);
19874 if (nested_name_specifier
)
19875 pushed_scope
= push_scope (nested_name_specifier
);
19877 else if (nested_name_specifier
)
19883 template <typename T> struct S { struct T };
19884 template <typename T> struct S<T>::T { };
19886 we will get a TYPENAME_TYPE when processing the definition of
19887 `S::T'. We need to resolve it to the actual type before we
19888 try to define it. */
19889 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
19891 class_type
= resolve_typename_type (TREE_TYPE (type
),
19892 /*only_current_p=*/false);
19893 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
19894 type
= TYPE_NAME (class_type
);
19897 cp_parser_error (parser
, "could not resolve typename type");
19898 type
= error_mark_node
;
19902 if (maybe_process_partial_specialization (TREE_TYPE (type
))
19903 == error_mark_node
)
19909 class_type
= current_class_type
;
19910 /* Enter the scope indicated by the nested-name-specifier. */
19911 pushed_scope
= push_scope (nested_name_specifier
);
19912 /* Get the canonical version of this type. */
19913 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
19914 /* Call push_template_decl if it seems like we should be defining a
19915 template either from the template headers or the type we're
19916 defining, so that we diagnose both extra and missing headers. */
19917 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
19918 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
))
19919 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
19920 (TREE_TYPE (type
)))))
19921 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
19923 type
= push_template_decl (type
);
19924 if (type
== error_mark_node
)
19931 type
= TREE_TYPE (type
);
19932 *nested_name_specifier_p
= true;
19934 else /* The name is not a nested name. */
19936 /* If the class was unnamed, create a dummy name. */
19938 id
= make_anon_name ();
19939 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
19940 parser
->num_template_parameter_lists
);
19943 /* Indicate whether this class was declared as a `class' or as a
19945 if (TREE_CODE (type
) == RECORD_TYPE
)
19946 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
19947 cp_parser_check_class_key (class_key
, type
);
19949 /* If this type was already complete, and we see another definition,
19950 that's an error. */
19951 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
19953 error_at (type_start_token
->location
, "redefinition of %q#T",
19955 error_at (type_start_token
->location
, "previous definition of %q+#T",
19960 else if (type
== error_mark_node
)
19965 /* Apply attributes now, before any use of the class as a template
19966 argument in its base list. */
19967 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
19968 fixup_attribute_variants (type
);
19971 /* We will have entered the scope containing the class; the names of
19972 base classes should be looked up in that context. For example:
19974 struct A { struct B {}; struct C; };
19975 struct A::C : B {};
19979 /* Get the list of base-classes, if there is one. */
19980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19982 /* PR59482: enter the class scope so that base-specifiers are looked
19986 bases
= cp_parser_base_clause (parser
);
19987 /* PR59482: get out of the previously pushed class scope so that the
19988 subsequent pops pop the right thing. */
19995 /* If we're really defining a class, process the base classes.
19996 If they're invalid, fail. */
19997 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
19998 && !xref_basetypes (type
, bases
))
20002 /* Leave the scope given by the nested-name-specifier. We will
20003 enter the class scope itself while processing the members. */
20005 pop_scope (pushed_scope
);
20007 if (invalid_explicit_specialization_p
)
20009 end_specialization ();
20010 --parser
->num_template_parameter_lists
;
20014 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20015 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
20016 CLASSTYPE_FINAL (type
) = 1;
20018 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20022 /* Parse a class-key.
20029 Returns the kind of class-key specified, or none_type to indicate
20032 static enum tag_types
20033 cp_parser_class_key (cp_parser
* parser
)
20036 enum tag_types tag_type
;
20038 /* Look for the class-key. */
20039 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
20043 /* Check to see if the TOKEN is a class-key. */
20044 tag_type
= cp_parser_token_is_class_key (token
);
20046 cp_parser_error (parser
, "expected class-key");
20050 /* Parse an (optional) member-specification.
20052 member-specification:
20053 member-declaration member-specification [opt]
20054 access-specifier : member-specification [opt] */
20057 cp_parser_member_specification_opt (cp_parser
* parser
)
20064 /* Peek at the next token. */
20065 token
= cp_lexer_peek_token (parser
->lexer
);
20066 /* If it's a `}', or EOF then we've seen all the members. */
20067 if (token
->type
== CPP_CLOSE_BRACE
20068 || token
->type
== CPP_EOF
20069 || token
->type
== CPP_PRAGMA_EOL
)
20072 /* See if this token is a keyword. */
20073 keyword
= token
->keyword
;
20077 case RID_PROTECTED
:
20079 /* Consume the access-specifier. */
20080 cp_lexer_consume_token (parser
->lexer
);
20081 /* Remember which access-specifier is active. */
20082 current_access_specifier
= token
->u
.value
;
20083 /* Look for the `:'. */
20084 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20088 /* Accept #pragmas at class scope. */
20089 if (token
->type
== CPP_PRAGMA
)
20091 cp_parser_pragma (parser
, pragma_member
);
20095 /* Otherwise, the next construction must be a
20096 member-declaration. */
20097 cp_parser_member_declaration (parser
);
20102 /* Parse a member-declaration.
20104 member-declaration:
20105 decl-specifier-seq [opt] member-declarator-list [opt] ;
20106 function-definition ; [opt]
20107 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20109 template-declaration
20112 member-declarator-list:
20114 member-declarator-list , member-declarator
20117 declarator pure-specifier [opt]
20118 declarator constant-initializer [opt]
20119 identifier [opt] : constant-expression
20123 member-declaration:
20124 __extension__ member-declaration
20127 declarator attributes [opt] pure-specifier [opt]
20128 declarator attributes [opt] constant-initializer [opt]
20129 identifier [opt] attributes [opt] : constant-expression
20133 member-declaration:
20134 static_assert-declaration */
20137 cp_parser_member_declaration (cp_parser
* parser
)
20139 cp_decl_specifier_seq decl_specifiers
;
20140 tree prefix_attributes
;
20142 int declares_class_or_enum
;
20144 cp_token
*token
= NULL
;
20145 cp_token
*decl_spec_token_start
= NULL
;
20146 cp_token
*initializer_token_start
= NULL
;
20147 int saved_pedantic
;
20148 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20150 /* Check for the `__extension__' keyword. */
20151 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
20154 cp_parser_member_declaration (parser
);
20155 /* Restore the old value of the PEDANTIC flag. */
20156 pedantic
= saved_pedantic
;
20161 /* Check for a template-declaration. */
20162 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20164 /* An explicit specialization here is an error condition, and we
20165 expect the specialization handler to detect and report this. */
20166 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
20167 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
20168 cp_parser_explicit_specialization (parser
);
20170 cp_parser_template_declaration (parser
, /*member_p=*/true);
20175 /* Check for a using-declaration. */
20176 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
20178 if (cxx_dialect
< cxx11
)
20180 /* Parse the using-declaration. */
20181 cp_parser_using_declaration (parser
,
20182 /*access_declaration_p=*/false);
20188 bool alias_decl_expected
;
20189 cp_parser_parse_tentatively (parser
);
20190 decl
= cp_parser_alias_declaration (parser
);
20191 /* Note that if we actually see the '=' token after the
20192 identifier, cp_parser_alias_declaration commits the
20193 tentative parse. In that case, we really expects an
20194 alias-declaration. Otherwise, we expect a using
20196 alias_decl_expected
=
20197 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
20198 cp_parser_parse_definitely (parser
);
20200 if (alias_decl_expected
)
20201 finish_member_declaration (decl
);
20203 cp_parser_using_declaration (parser
,
20204 /*access_declaration_p=*/false);
20209 /* Check for @defs. */
20210 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
20213 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
20214 ivar
= ivar_chains
;
20218 ivar
= TREE_CHAIN (member
);
20219 TREE_CHAIN (member
) = NULL_TREE
;
20220 finish_member_declaration (member
);
20225 /* If the next token is `static_assert' we have a static assertion. */
20226 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
20228 cp_parser_static_assert (parser
, /*member_p=*/true);
20232 parser
->colon_corrects_to_scope_p
= false;
20234 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20237 /* Parse the decl-specifier-seq. */
20238 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20239 cp_parser_decl_specifier_seq (parser
,
20240 CP_PARSER_FLAGS_OPTIONAL
,
20242 &declares_class_or_enum
);
20243 /* Check for an invalid type-name. */
20244 if (!decl_specifiers
.any_type_specifiers_p
20245 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20247 /* If there is no declarator, then the decl-specifier-seq should
20249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20251 /* If there was no decl-specifier-seq, and the next token is a
20252 `;', then we have something like:
20258 Each member-declaration shall declare at least one member
20259 name of the class. */
20260 if (!decl_specifiers
.any_specifiers_p
)
20262 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20263 if (!in_system_header_at (token
->location
))
20264 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20270 /* See if this declaration is a friend. */
20271 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20272 /* If there were decl-specifiers, check to see if there was
20273 a class-declaration. */
20274 type
= check_tag_decl (&decl_specifiers
,
20275 /*explicit_type_instantiation_p=*/false);
20276 /* Nested classes have already been added to the class, but
20277 a `friend' needs to be explicitly registered. */
20280 /* If the `friend' keyword was present, the friend must
20281 be introduced with a class-key. */
20282 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20283 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20284 "in C++03 a class-key must be used "
20285 "when declaring a friend");
20288 template <typename T> struct A {
20289 friend struct A<T>::B;
20292 A<T>::B will be represented by a TYPENAME_TYPE, and
20293 therefore not recognized by check_tag_decl. */
20296 type
= decl_specifiers
.type
;
20297 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20298 type
= TREE_TYPE (type
);
20300 if (!type
|| !TYPE_P (type
))
20301 error_at (decl_spec_token_start
->location
,
20302 "friend declaration does not name a class or "
20305 make_friend_class (current_class_type
, type
,
20306 /*complain=*/true);
20308 /* If there is no TYPE, an error message will already have
20310 else if (!type
|| type
== error_mark_node
)
20312 /* An anonymous aggregate has to be handled specially; such
20313 a declaration really declares a data member (with a
20314 particular type), as opposed to a nested class. */
20315 else if (ANON_AGGR_TYPE_P (type
))
20318 if (decl_specifiers
.storage_class
!= sc_none
)
20319 error_at (decl_spec_token_start
->location
,
20320 "a storage class on an anonymous aggregate "
20321 "in class scope is not allowed");
20323 /* Remove constructors and such from TYPE, now that we
20324 know it is an anonymous aggregate. */
20325 fixup_anonymous_aggr (type
);
20326 /* And make the corresponding data member. */
20327 decl
= build_decl (decl_spec_token_start
->location
,
20328 FIELD_DECL
, NULL_TREE
, type
);
20329 /* Add it to the class. */
20330 finish_member_declaration (decl
);
20333 cp_parser_check_access_in_redeclaration
20335 decl_spec_token_start
->location
);
20340 bool assume_semicolon
= false;
20342 /* Clear attributes from the decl_specifiers but keep them
20343 around as prefix attributes that apply them to the entity
20345 prefix_attributes
= decl_specifiers
.attributes
;
20346 decl_specifiers
.attributes
= NULL_TREE
;
20348 /* See if these declarations will be friends. */
20349 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20351 /* Keep going until we hit the `;' at the end of the
20353 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20355 tree attributes
= NULL_TREE
;
20356 tree first_attribute
;
20358 /* Peek at the next token. */
20359 token
= cp_lexer_peek_token (parser
->lexer
);
20361 /* Check for a bitfield declaration. */
20362 if (token
->type
== CPP_COLON
20363 || (token
->type
== CPP_NAME
20364 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20370 /* Get the name of the bitfield. Note that we cannot just
20371 check TOKEN here because it may have been invalidated by
20372 the call to cp_lexer_peek_nth_token above. */
20373 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20374 identifier
= cp_parser_identifier (parser
);
20376 identifier
= NULL_TREE
;
20378 /* Consume the `:' token. */
20379 cp_lexer_consume_token (parser
->lexer
);
20380 /* Get the width of the bitfield. */
20382 = cp_parser_constant_expression (parser
,
20383 /*allow_non_constant=*/false,
20386 /* Look for attributes that apply to the bitfield. */
20387 attributes
= cp_parser_attributes_opt (parser
);
20388 /* Remember which attributes are prefix attributes and
20390 first_attribute
= attributes
;
20391 /* Combine the attributes. */
20392 attributes
= chainon (prefix_attributes
, attributes
);
20394 /* Create the bitfield declaration. */
20395 decl
= grokbitfield (identifier
20396 ? make_id_declarator (NULL_TREE
,
20406 cp_declarator
*declarator
;
20408 tree asm_specification
;
20409 int ctor_dtor_or_conv_p
;
20411 /* Parse the declarator. */
20413 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20414 &ctor_dtor_or_conv_p
,
20415 /*parenthesized_p=*/NULL
,
20416 /*member_p=*/true);
20418 /* If something went wrong parsing the declarator, make sure
20419 that we at least consume some tokens. */
20420 if (declarator
== cp_error_declarator
)
20422 /* Skip to the end of the statement. */
20423 cp_parser_skip_to_end_of_statement (parser
);
20424 /* If the next token is not a semicolon, that is
20425 probably because we just skipped over the body of
20426 a function. So, we consume a semicolon if
20427 present, but do not issue an error message if it
20429 if (cp_lexer_next_token_is (parser
->lexer
,
20431 cp_lexer_consume_token (parser
->lexer
);
20435 if (declares_class_or_enum
& 2)
20436 cp_parser_check_for_definition_in_return_type
20437 (declarator
, decl_specifiers
.type
,
20438 decl_specifiers
.locations
[ds_type_spec
]);
20440 /* Look for an asm-specification. */
20441 asm_specification
= cp_parser_asm_specification_opt (parser
);
20442 /* Look for attributes that apply to the declaration. */
20443 attributes
= cp_parser_attributes_opt (parser
);
20444 /* Remember which attributes are prefix attributes and
20446 first_attribute
= attributes
;
20447 /* Combine the attributes. */
20448 attributes
= chainon (prefix_attributes
, attributes
);
20450 /* If it's an `=', then we have a constant-initializer or a
20451 pure-specifier. It is not correct to parse the
20452 initializer before registering the member declaration
20453 since the member declaration should be in scope while
20454 its initializer is processed. However, the rest of the
20455 front end does not yet provide an interface that allows
20456 us to handle this correctly. */
20457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20461 A pure-specifier shall be used only in the declaration of
20462 a virtual function.
20464 A member-declarator can contain a constant-initializer
20465 only if it declares a static member of integral or
20468 Therefore, if the DECLARATOR is for a function, we look
20469 for a pure-specifier; otherwise, we look for a
20470 constant-initializer. When we call `grokfield', it will
20471 perform more stringent semantics checks. */
20472 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
20473 if (function_declarator_p (declarator
)
20474 || (decl_specifiers
.type
20475 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
20476 && declarator
->kind
== cdk_id
20477 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
20478 == FUNCTION_TYPE
)))
20479 initializer
= cp_parser_pure_specifier (parser
);
20480 else if (decl_specifiers
.storage_class
!= sc_static
)
20481 initializer
= cp_parser_save_nsdmi (parser
);
20482 else if (cxx_dialect
>= cxx11
)
20485 /* Don't require a constant rvalue in C++11, since we
20486 might want a reference constant. We'll enforce
20487 constancy later. */
20488 cp_lexer_consume_token (parser
->lexer
);
20489 /* Parse the initializer. */
20490 initializer
= cp_parser_initializer_clause (parser
,
20494 /* Parse the initializer. */
20495 initializer
= cp_parser_constant_initializer (parser
);
20497 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20498 && !function_declarator_p (declarator
))
20501 if (decl_specifiers
.storage_class
!= sc_static
)
20502 initializer
= cp_parser_save_nsdmi (parser
);
20504 initializer
= cp_parser_initializer (parser
, &x
, &x
);
20506 /* Otherwise, there is no initializer. */
20508 initializer
= NULL_TREE
;
20510 /* See if we are probably looking at a function
20511 definition. We are certainly not looking at a
20512 member-declarator. Calling `grokfield' has
20513 side-effects, so we must not do it unless we are sure
20514 that we are looking at a member-declarator. */
20515 if (cp_parser_token_starts_function_definition_p
20516 (cp_lexer_peek_token (parser
->lexer
)))
20518 /* The grammar does not allow a pure-specifier to be
20519 used when a member function is defined. (It is
20520 possible that this fact is an oversight in the
20521 standard, since a pure function may be defined
20522 outside of the class-specifier. */
20523 if (initializer
&& initializer_token_start
)
20524 error_at (initializer_token_start
->location
,
20525 "pure-specifier on function-definition");
20526 decl
= cp_parser_save_member_function_body (parser
,
20530 if (parser
->fully_implicit_function_template_p
)
20531 decl
= finish_fully_implicit_template (parser
, decl
);
20532 /* If the member was not a friend, declare it here. */
20534 finish_member_declaration (decl
);
20535 /* Peek at the next token. */
20536 token
= cp_lexer_peek_token (parser
->lexer
);
20537 /* If the next token is a semicolon, consume it. */
20538 if (token
->type
== CPP_SEMICOLON
)
20539 cp_lexer_consume_token (parser
->lexer
);
20543 if (declarator
->kind
== cdk_function
)
20544 declarator
->id_loc
= token
->location
;
20545 /* Create the declaration. */
20546 decl
= grokfield (declarator
, &decl_specifiers
,
20547 initializer
, /*init_const_expr_p=*/true,
20548 asm_specification
, attributes
);
20549 if (parser
->fully_implicit_function_template_p
)
20552 finish_fully_implicit_template (parser
, 0);
20554 decl
= finish_fully_implicit_template (parser
, decl
);
20558 cp_finalize_omp_declare_simd (parser
, decl
);
20560 /* Reset PREFIX_ATTRIBUTES. */
20561 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
20562 attributes
= TREE_CHAIN (attributes
);
20564 TREE_CHAIN (attributes
) = NULL_TREE
;
20566 /* If there is any qualification still in effect, clear it
20567 now; we will be starting fresh with the next declarator. */
20568 parser
->scope
= NULL_TREE
;
20569 parser
->qualifying_scope
= NULL_TREE
;
20570 parser
->object_scope
= NULL_TREE
;
20571 /* If it's a `,', then there are more declarators. */
20572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20574 cp_lexer_consume_token (parser
->lexer
);
20575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20577 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
20578 error_at (token
->location
,
20579 "stray %<,%> at end of member declaration");
20582 /* If the next token isn't a `;', then we have a parse error. */
20583 else if (cp_lexer_next_token_is_not (parser
->lexer
,
20586 /* The next token might be a ways away from where the
20587 actual semicolon is missing. Find the previous token
20588 and use that for our error position. */
20589 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
20590 error_at (token
->location
,
20591 "expected %<;%> at end of member declaration");
20593 /* Assume that the user meant to provide a semicolon. If
20594 we were to cp_parser_skip_to_end_of_statement, we might
20595 skip to a semicolon inside a member function definition
20596 and issue nonsensical error messages. */
20597 assume_semicolon
= true;
20602 /* Add DECL to the list of members. */
20604 finish_member_declaration (decl
);
20606 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20607 cp_parser_save_default_args (parser
, decl
);
20608 else if (TREE_CODE (decl
) == FIELD_DECL
20609 && !DECL_C_BIT_FIELD (decl
)
20610 && DECL_INITIAL (decl
))
20611 /* Add DECL to the queue of NSDMI to be parsed later. */
20612 vec_safe_push (unparsed_nsdmis
, decl
);
20615 if (assume_semicolon
)
20620 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20622 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20625 /* Parse a pure-specifier.
20630 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20631 Otherwise, ERROR_MARK_NODE is returned. */
20634 cp_parser_pure_specifier (cp_parser
* parser
)
20638 /* Look for the `=' token. */
20639 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
20640 return error_mark_node
;
20641 /* Look for the `0' token. */
20642 token
= cp_lexer_peek_token (parser
->lexer
);
20644 if (token
->type
== CPP_EOF
20645 || token
->type
== CPP_PRAGMA_EOL
)
20646 return error_mark_node
;
20648 cp_lexer_consume_token (parser
->lexer
);
20650 /* Accept = default or = delete in c++0x mode. */
20651 if (token
->keyword
== RID_DEFAULT
20652 || token
->keyword
== RID_DELETE
)
20654 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
20655 return token
->u
.value
;
20658 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20659 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
20661 cp_parser_error (parser
,
20662 "invalid pure specifier (only %<= 0%> is allowed)");
20663 cp_parser_skip_to_end_of_statement (parser
);
20664 return error_mark_node
;
20666 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20668 error_at (token
->location
, "templates may not be %<virtual%>");
20669 return error_mark_node
;
20672 return integer_zero_node
;
20675 /* Parse a constant-initializer.
20677 constant-initializer:
20678 = constant-expression
20680 Returns a representation of the constant-expression. */
20683 cp_parser_constant_initializer (cp_parser
* parser
)
20685 /* Look for the `=' token. */
20686 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
20687 return error_mark_node
;
20689 /* It is invalid to write:
20691 struct S { static const int i = { 7 }; };
20694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20696 cp_parser_error (parser
,
20697 "a brace-enclosed initializer is not allowed here");
20698 /* Consume the opening brace. */
20699 cp_lexer_consume_token (parser
->lexer
);
20700 /* Skip the initializer. */
20701 cp_parser_skip_to_closing_brace (parser
);
20702 /* Look for the trailing `}'. */
20703 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20705 return error_mark_node
;
20708 return cp_parser_constant_expression (parser
,
20709 /*allow_non_constant=*/false,
20713 /* Derived classes [gram.class.derived] */
20715 /* Parse a base-clause.
20718 : base-specifier-list
20720 base-specifier-list:
20721 base-specifier ... [opt]
20722 base-specifier-list , base-specifier ... [opt]
20724 Returns a TREE_LIST representing the base-classes, in the order in
20725 which they were declared. The representation of each node is as
20726 described by cp_parser_base_specifier.
20728 In the case that no bases are specified, this function will return
20729 NULL_TREE, not ERROR_MARK_NODE. */
20732 cp_parser_base_clause (cp_parser
* parser
)
20734 tree bases
= NULL_TREE
;
20736 /* Look for the `:' that begins the list. */
20737 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20739 /* Scan the base-specifier-list. */
20744 bool pack_expansion_p
= false;
20746 /* Look for the base-specifier. */
20747 base
= cp_parser_base_specifier (parser
);
20748 /* Look for the (optional) ellipsis. */
20749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20751 /* Consume the `...'. */
20752 cp_lexer_consume_token (parser
->lexer
);
20754 pack_expansion_p
= true;
20757 /* Add BASE to the front of the list. */
20758 if (base
&& base
!= error_mark_node
)
20760 if (pack_expansion_p
)
20761 /* Make this a pack expansion type. */
20762 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
20764 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
20766 TREE_CHAIN (base
) = bases
;
20770 /* Peek at the next token. */
20771 token
= cp_lexer_peek_token (parser
->lexer
);
20772 /* If it's not a comma, then the list is complete. */
20773 if (token
->type
!= CPP_COMMA
)
20775 /* Consume the `,'. */
20776 cp_lexer_consume_token (parser
->lexer
);
20779 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20780 base class had a qualified name. However, the next name that
20781 appears is certainly not qualified. */
20782 parser
->scope
= NULL_TREE
;
20783 parser
->qualifying_scope
= NULL_TREE
;
20784 parser
->object_scope
= NULL_TREE
;
20786 return nreverse (bases
);
20789 /* Parse a base-specifier.
20792 :: [opt] nested-name-specifier [opt] class-name
20793 virtual access-specifier [opt] :: [opt] nested-name-specifier
20795 access-specifier virtual [opt] :: [opt] nested-name-specifier
20798 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20799 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20800 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20801 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20804 cp_parser_base_specifier (cp_parser
* parser
)
20808 bool virtual_p
= false;
20809 bool duplicate_virtual_error_issued_p
= false;
20810 bool duplicate_access_error_issued_p
= false;
20811 bool class_scope_p
, template_p
;
20812 tree access
= access_default_node
;
20815 /* Process the optional `virtual' and `access-specifier'. */
20818 /* Peek at the next token. */
20819 token
= cp_lexer_peek_token (parser
->lexer
);
20820 /* Process `virtual'. */
20821 switch (token
->keyword
)
20824 /* If `virtual' appears more than once, issue an error. */
20825 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
20827 cp_parser_error (parser
,
20828 "%<virtual%> specified more than once in base-specified");
20829 duplicate_virtual_error_issued_p
= true;
20834 /* Consume the `virtual' token. */
20835 cp_lexer_consume_token (parser
->lexer
);
20840 case RID_PROTECTED
:
20842 /* If more than one access specifier appears, issue an
20844 if (access
!= access_default_node
20845 && !duplicate_access_error_issued_p
)
20847 cp_parser_error (parser
,
20848 "more than one access specifier in base-specified");
20849 duplicate_access_error_issued_p
= true;
20852 access
= ridpointers
[(int) token
->keyword
];
20854 /* Consume the access-specifier. */
20855 cp_lexer_consume_token (parser
->lexer
);
20864 /* It is not uncommon to see programs mechanically, erroneously, use
20865 the 'typename' keyword to denote (dependent) qualified types
20866 as base classes. */
20867 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
20869 token
= cp_lexer_peek_token (parser
->lexer
);
20870 if (!processing_template_decl
)
20871 error_at (token
->location
,
20872 "keyword %<typename%> not allowed outside of templates");
20874 error_at (token
->location
,
20875 "keyword %<typename%> not allowed in this context "
20876 "(the base class is implicitly a type)");
20877 cp_lexer_consume_token (parser
->lexer
);
20880 /* Look for the optional `::' operator. */
20881 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
20882 /* Look for the nested-name-specifier. The simplest way to
20887 The keyword `typename' is not permitted in a base-specifier or
20888 mem-initializer; in these contexts a qualified name that
20889 depends on a template-parameter is implicitly assumed to be a
20892 is to pretend that we have seen the `typename' keyword at this
20894 cp_parser_nested_name_specifier_opt (parser
,
20895 /*typename_keyword_p=*/true,
20896 /*check_dependency_p=*/true,
20898 /*is_declaration=*/true);
20899 /* If the base class is given by a qualified name, assume that names
20900 we see are type names or templates, as appropriate. */
20901 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
20902 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
20905 && cp_lexer_next_token_is_decltype (parser
->lexer
))
20906 /* DR 950 allows decltype as a base-specifier. */
20907 type
= cp_parser_decltype (parser
);
20910 /* Otherwise, look for the class-name. */
20911 type
= cp_parser_class_name (parser
,
20915 /*check_dependency_p=*/true,
20916 /*class_head_p=*/false,
20917 /*is_declaration=*/true);
20918 type
= TREE_TYPE (type
);
20921 if (type
== error_mark_node
)
20922 return error_mark_node
;
20924 return finish_base_specifier (type
, access
, virtual_p
);
20927 /* Exception handling [gram.exception] */
20929 /* Parse an (optional) noexcept-specification.
20931 noexcept-specification:
20932 noexcept ( constant-expression ) [opt]
20934 If no noexcept-specification is present, returns NULL_TREE.
20935 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20936 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20937 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20938 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20939 in which case a boolean condition is returned instead. */
20942 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
20943 bool require_constexpr
,
20944 bool* consumed_expr
,
20948 const char *saved_message
;
20950 /* Peek at the next token. */
20951 token
= cp_lexer_peek_token (parser
->lexer
);
20953 /* Is it a noexcept-specification? */
20954 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
20957 cp_lexer_consume_token (parser
->lexer
);
20959 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
20961 cp_lexer_consume_token (parser
->lexer
);
20963 if (require_constexpr
)
20965 /* Types may not be defined in an exception-specification. */
20966 saved_message
= parser
->type_definition_forbidden_message
;
20967 parser
->type_definition_forbidden_message
20968 = G_("types may not be defined in an exception-specification");
20970 expr
= cp_parser_constant_expression (parser
, false, NULL
);
20972 /* Restore the saved message. */
20973 parser
->type_definition_forbidden_message
= saved_message
;
20977 expr
= cp_parser_expression (parser
, false, NULL
);
20978 *consumed_expr
= true;
20981 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20985 expr
= boolean_true_node
;
20986 if (!require_constexpr
)
20987 *consumed_expr
= false;
20990 /* We cannot build a noexcept-spec right away because this will check
20991 that expr is a constexpr. */
20993 return build_noexcept_spec (expr
, tf_warning_or_error
);
21001 /* Parse an (optional) exception-specification.
21003 exception-specification:
21004 throw ( type-id-list [opt] )
21006 Returns a TREE_LIST representing the exception-specification. The
21007 TREE_VALUE of each node is a type. */
21010 cp_parser_exception_specification_opt (cp_parser
* parser
)
21014 const char *saved_message
;
21016 /* Peek at the next token. */
21017 token
= cp_lexer_peek_token (parser
->lexer
);
21019 /* Is it a noexcept-specification? */
21020 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
21022 if (type_id_list
!= NULL_TREE
)
21023 return type_id_list
;
21025 /* If it's not `throw', then there's no exception-specification. */
21026 if (!cp_parser_is_keyword (token
, RID_THROW
))
21030 /* Enable this once a lot of code has transitioned to noexcept? */
21031 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
21032 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
21033 "deprecated in C++0x; use %<noexcept%> instead");
21036 /* Consume the `throw'. */
21037 cp_lexer_consume_token (parser
->lexer
);
21039 /* Look for the `('. */
21040 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21042 /* Peek at the next token. */
21043 token
= cp_lexer_peek_token (parser
->lexer
);
21044 /* If it's not a `)', then there is a type-id-list. */
21045 if (token
->type
!= CPP_CLOSE_PAREN
)
21047 /* Types may not be defined in an exception-specification. */
21048 saved_message
= parser
->type_definition_forbidden_message
;
21049 parser
->type_definition_forbidden_message
21050 = G_("types may not be defined in an exception-specification");
21051 /* Parse the type-id-list. */
21052 type_id_list
= cp_parser_type_id_list (parser
);
21053 /* Restore the saved message. */
21054 parser
->type_definition_forbidden_message
= saved_message
;
21057 type_id_list
= empty_except_spec
;
21059 /* Look for the `)'. */
21060 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21062 return type_id_list
;
21065 /* Parse an (optional) type-id-list.
21069 type-id-list , type-id ... [opt]
21071 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21072 in the order that the types were presented. */
21075 cp_parser_type_id_list (cp_parser
* parser
)
21077 tree types
= NULL_TREE
;
21084 /* Get the next type-id. */
21085 type
= cp_parser_type_id (parser
);
21086 /* Parse the optional ellipsis. */
21087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21089 /* Consume the `...'. */
21090 cp_lexer_consume_token (parser
->lexer
);
21092 /* Turn the type into a pack expansion expression. */
21093 type
= make_pack_expansion (type
);
21095 /* Add it to the list. */
21096 types
= add_exception_specifier (types
, type
, /*complain=*/1);
21097 /* Peek at the next token. */
21098 token
= cp_lexer_peek_token (parser
->lexer
);
21099 /* If it is not a `,', we are done. */
21100 if (token
->type
!= CPP_COMMA
)
21102 /* Consume the `,'. */
21103 cp_lexer_consume_token (parser
->lexer
);
21106 return nreverse (types
);
21109 /* Parse a try-block.
21112 try compound-statement handler-seq */
21115 cp_parser_try_block (cp_parser
* parser
)
21119 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
21120 try_block
= begin_try_block ();
21121 cp_parser_compound_statement (parser
, NULL
, true, false);
21122 finish_try_block (try_block
);
21123 cp_parser_handler_seq (parser
);
21124 finish_handler_sequence (try_block
);
21129 /* Parse a function-try-block.
21131 function-try-block:
21132 try ctor-initializer [opt] function-body handler-seq */
21135 cp_parser_function_try_block (cp_parser
* parser
)
21137 tree compound_stmt
;
21139 bool ctor_initializer_p
;
21141 /* Look for the `try' keyword. */
21142 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
21144 /* Let the rest of the front end know where we are. */
21145 try_block
= begin_function_try_block (&compound_stmt
);
21146 /* Parse the function-body. */
21147 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21148 (parser
, /*in_function_try_block=*/true);
21149 /* We're done with the `try' part. */
21150 finish_function_try_block (try_block
);
21151 /* Parse the handlers. */
21152 cp_parser_handler_seq (parser
);
21153 /* We're done with the handlers. */
21154 finish_function_handler_sequence (try_block
, compound_stmt
);
21156 return ctor_initializer_p
;
21159 /* Parse a handler-seq.
21162 handler handler-seq [opt] */
21165 cp_parser_handler_seq (cp_parser
* parser
)
21171 /* Parse the handler. */
21172 cp_parser_handler (parser
);
21173 /* Peek at the next token. */
21174 token
= cp_lexer_peek_token (parser
->lexer
);
21175 /* If it's not `catch' then there are no more handlers. */
21176 if (!cp_parser_is_keyword (token
, RID_CATCH
))
21181 /* Parse a handler.
21184 catch ( exception-declaration ) compound-statement */
21187 cp_parser_handler (cp_parser
* parser
)
21192 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
21193 handler
= begin_handler ();
21194 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21195 declaration
= cp_parser_exception_declaration (parser
);
21196 finish_handler_parms (declaration
, handler
);
21197 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21198 cp_parser_compound_statement (parser
, NULL
, false, false);
21199 finish_handler (handler
);
21202 /* Parse an exception-declaration.
21204 exception-declaration:
21205 type-specifier-seq declarator
21206 type-specifier-seq abstract-declarator
21210 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21211 ellipsis variant is used. */
21214 cp_parser_exception_declaration (cp_parser
* parser
)
21216 cp_decl_specifier_seq type_specifiers
;
21217 cp_declarator
*declarator
;
21218 const char *saved_message
;
21220 /* If it's an ellipsis, it's easy to handle. */
21221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21223 /* Consume the `...' token. */
21224 cp_lexer_consume_token (parser
->lexer
);
21228 /* Types may not be defined in exception-declarations. */
21229 saved_message
= parser
->type_definition_forbidden_message
;
21230 parser
->type_definition_forbidden_message
21231 = G_("types may not be defined in exception-declarations");
21233 /* Parse the type-specifier-seq. */
21234 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
21235 /*is_trailing_return=*/false,
21237 /* If it's a `)', then there is no declarator. */
21238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21241 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21242 /*ctor_dtor_or_conv_p=*/NULL
,
21243 /*parenthesized_p=*/NULL
,
21244 /*member_p=*/false);
21246 /* Restore the saved message. */
21247 parser
->type_definition_forbidden_message
= saved_message
;
21249 if (!type_specifiers
.any_specifiers_p
)
21250 return error_mark_node
;
21252 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21255 /* Parse a throw-expression.
21258 throw assignment-expression [opt]
21260 Returns a THROW_EXPR representing the throw-expression. */
21263 cp_parser_throw_expression (cp_parser
* parser
)
21268 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21269 token
= cp_lexer_peek_token (parser
->lexer
);
21270 /* Figure out whether or not there is an assignment-expression
21271 following the "throw" keyword. */
21272 if (token
->type
== CPP_COMMA
21273 || token
->type
== CPP_SEMICOLON
21274 || token
->type
== CPP_CLOSE_PAREN
21275 || token
->type
== CPP_CLOSE_SQUARE
21276 || token
->type
== CPP_CLOSE_BRACE
21277 || token
->type
== CPP_COLON
)
21278 expression
= NULL_TREE
;
21280 expression
= cp_parser_assignment_expression (parser
,
21281 /*cast_p=*/false, NULL
);
21283 return build_throw (expression
);
21286 /* GNU Extensions */
21288 /* Parse an (optional) asm-specification.
21291 asm ( string-literal )
21293 If the asm-specification is present, returns a STRING_CST
21294 corresponding to the string-literal. Otherwise, returns
21298 cp_parser_asm_specification_opt (cp_parser
* parser
)
21301 tree asm_specification
;
21303 /* Peek at the next token. */
21304 token
= cp_lexer_peek_token (parser
->lexer
);
21305 /* If the next token isn't the `asm' keyword, then there's no
21306 asm-specification. */
21307 if (!cp_parser_is_keyword (token
, RID_ASM
))
21310 /* Consume the `asm' token. */
21311 cp_lexer_consume_token (parser
->lexer
);
21312 /* Look for the `('. */
21313 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21315 /* Look for the string-literal. */
21316 asm_specification
= cp_parser_string_literal (parser
, false, false);
21318 /* Look for the `)'. */
21319 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21321 return asm_specification
;
21324 /* Parse an asm-operand-list.
21328 asm-operand-list , asm-operand
21331 string-literal ( expression )
21332 [ string-literal ] string-literal ( expression )
21334 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21335 each node is the expression. The TREE_PURPOSE is itself a
21336 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21337 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21338 is a STRING_CST for the string literal before the parenthesis. Returns
21339 ERROR_MARK_NODE if any of the operands are invalid. */
21342 cp_parser_asm_operand_list (cp_parser
* parser
)
21344 tree asm_operands
= NULL_TREE
;
21345 bool invalid_operands
= false;
21349 tree string_literal
;
21353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21355 /* Consume the `[' token. */
21356 cp_lexer_consume_token (parser
->lexer
);
21357 /* Read the operand name. */
21358 name
= cp_parser_identifier (parser
);
21359 if (name
!= error_mark_node
)
21360 name
= build_string (IDENTIFIER_LENGTH (name
),
21361 IDENTIFIER_POINTER (name
));
21362 /* Look for the closing `]'. */
21363 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21367 /* Look for the string-literal. */
21368 string_literal
= cp_parser_string_literal (parser
, false, false);
21370 /* Look for the `('. */
21371 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21372 /* Parse the expression. */
21373 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
21374 /* Look for the `)'. */
21375 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21377 if (name
== error_mark_node
21378 || string_literal
== error_mark_node
21379 || expression
== error_mark_node
)
21380 invalid_operands
= true;
21382 /* Add this operand to the list. */
21383 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21386 /* If the next token is not a `,', there are no more
21388 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21390 /* Consume the `,'. */
21391 cp_lexer_consume_token (parser
->lexer
);
21394 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21397 /* Parse an asm-clobber-list.
21401 asm-clobber-list , string-literal
21403 Returns a TREE_LIST, indicating the clobbers in the order that they
21404 appeared. The TREE_VALUE of each node is a STRING_CST. */
21407 cp_parser_asm_clobber_list (cp_parser
* parser
)
21409 tree clobbers
= NULL_TREE
;
21413 tree string_literal
;
21415 /* Look for the string literal. */
21416 string_literal
= cp_parser_string_literal (parser
, false, false);
21417 /* Add it to the list. */
21418 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21419 /* If the next token is not a `,', then the list is
21421 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21423 /* Consume the `,' token. */
21424 cp_lexer_consume_token (parser
->lexer
);
21430 /* Parse an asm-label-list.
21434 asm-label-list , identifier
21436 Returns a TREE_LIST, indicating the labels in the order that they
21437 appeared. The TREE_VALUE of each node is a label. */
21440 cp_parser_asm_label_list (cp_parser
* parser
)
21442 tree labels
= NULL_TREE
;
21446 tree identifier
, label
, name
;
21448 /* Look for the identifier. */
21449 identifier
= cp_parser_identifier (parser
);
21450 if (!error_operand_p (identifier
))
21452 label
= lookup_label (identifier
);
21453 if (TREE_CODE (label
) == LABEL_DECL
)
21455 TREE_USED (label
) = 1;
21456 check_goto (label
);
21457 name
= build_string (IDENTIFIER_LENGTH (identifier
),
21458 IDENTIFIER_POINTER (identifier
));
21459 labels
= tree_cons (name
, label
, labels
);
21462 /* If the next token is not a `,', then the list is
21464 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21466 /* Consume the `,' token. */
21467 cp_lexer_consume_token (parser
->lexer
);
21470 return nreverse (labels
);
21473 /* Return TRUE iff the next tokens in the stream are possibly the
21474 beginning of a GNU extension attribute. */
21477 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
21479 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
21482 /* Return TRUE iff the next tokens in the stream are possibly the
21483 beginning of a standard C++-11 attribute specifier. */
21486 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
21488 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
21491 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21492 beginning of a standard C++-11 attribute specifier. */
21495 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
21497 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21499 return (cxx_dialect
>= cxx11
21500 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
21501 || (token
->type
== CPP_OPEN_SQUARE
21502 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
21503 && token
->type
== CPP_OPEN_SQUARE
)));
21506 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21507 beginning of a GNU extension attribute. */
21510 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
21512 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21514 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
21517 /* Return true iff the next tokens can be the beginning of either a
21518 GNU attribute list, or a standard C++11 attribute sequence. */
21521 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
21523 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
21524 || cp_next_tokens_can_be_std_attribute_p (parser
));
21527 /* Return true iff the next Nth tokens can be the beginning of either
21528 a GNU attribute list, or a standard C++11 attribute sequence. */
21531 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
21533 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
21534 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
21537 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21538 of GNU attributes, or return NULL. */
21541 cp_parser_attributes_opt (cp_parser
*parser
)
21543 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
21544 return cp_parser_gnu_attributes_opt (parser
);
21545 return cp_parser_std_attribute_spec_seq (parser
);
21548 #define CILK_SIMD_FN_CLAUSE_MASK \
21549 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21550 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21551 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21552 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21553 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21555 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21556 vector [(<clauses>)] */
21559 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
21561 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
21562 cp_token
*token
= v_token
;
21565 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
21566 parser
->cilk_simd_fn_info
->error_seen
= false;
21567 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
21568 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
21570 int paren_scope
= 0;
21571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21573 cp_lexer_consume_token (parser
->lexer
);
21574 v_token
= cp_lexer_peek_token (parser
->lexer
);
21577 while (paren_scope
> 0)
21579 token
= cp_lexer_peek_token (parser
->lexer
);
21580 if (token
->type
== CPP_OPEN_PAREN
)
21582 else if (token
->type
== CPP_CLOSE_PAREN
)
21584 /* Do not push the last ')' */
21585 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
21586 cp_lexer_consume_token (parser
->lexer
);
21589 token
->type
= CPP_PRAGMA_EOL
;
21590 parser
->lexer
->next_token
= token
;
21591 cp_lexer_consume_token (parser
->lexer
);
21593 struct cp_token_cache
*cp
21594 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
21595 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
21598 /* Parse an (optional) series of attributes.
21601 attributes attribute
21604 __attribute__ (( attribute-list [opt] ))
21606 The return value is as for cp_parser_gnu_attribute_list. */
21609 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
21611 tree attributes
= NULL_TREE
;
21616 tree attribute_list
;
21619 /* Peek at the next token. */
21620 token
= cp_lexer_peek_token (parser
->lexer
);
21621 /* If it's not `__attribute__', then we're done. */
21622 if (token
->keyword
!= RID_ATTRIBUTE
)
21625 /* Consume the `__attribute__' keyword. */
21626 cp_lexer_consume_token (parser
->lexer
);
21627 /* Look for the two `(' tokens. */
21628 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21629 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21631 /* Peek at the next token. */
21632 token
= cp_lexer_peek_token (parser
->lexer
);
21633 if (token
->type
!= CPP_CLOSE_PAREN
)
21634 /* Parse the attribute-list. */
21635 attribute_list
= cp_parser_gnu_attribute_list (parser
);
21637 /* If the next token is a `)', then there is no attribute
21639 attribute_list
= NULL
;
21641 /* Look for the two `)' tokens. */
21642 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
21644 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
21647 cp_parser_skip_to_end_of_statement (parser
);
21649 /* Add these new attributes to the list. */
21650 attributes
= chainon (attributes
, attribute_list
);
21656 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21657 "__vector" or "__vector__." */
21660 is_cilkplus_vector_p (tree name
)
21662 if (flag_cilkplus
&& is_attribute_p ("vector", name
))
21667 /* Parse a GNU attribute-list.
21671 attribute-list , attribute
21675 identifier ( identifier )
21676 identifier ( identifier , expression-list )
21677 identifier ( expression-list )
21679 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21680 to an attribute. The TREE_PURPOSE of each node is the identifier
21681 indicating which attribute is in use. The TREE_VALUE represents
21682 the arguments, if any. */
21685 cp_parser_gnu_attribute_list (cp_parser
* parser
)
21687 tree attribute_list
= NULL_TREE
;
21688 bool save_translate_strings_p
= parser
->translate_strings_p
;
21690 parser
->translate_strings_p
= false;
21697 /* Look for the identifier. We also allow keywords here; for
21698 example `__attribute__ ((const))' is legal. */
21699 token
= cp_lexer_peek_token (parser
->lexer
);
21700 if (token
->type
== CPP_NAME
21701 || token
->type
== CPP_KEYWORD
)
21703 tree arguments
= NULL_TREE
;
21705 /* Consume the token, but save it since we need it for the
21706 SIMD enabled function parsing. */
21707 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
21709 /* Save away the identifier that indicates which attribute
21711 identifier
= (token
->type
== CPP_KEYWORD
)
21712 /* For keywords, use the canonical spelling, not the
21713 parsed identifier. */
21714 ? ridpointers
[(int) token
->keyword
]
21715 : id_token
->u
.value
;
21717 attribute
= build_tree_list (identifier
, NULL_TREE
);
21719 /* Peek at the next token. */
21720 token
= cp_lexer_peek_token (parser
->lexer
);
21721 /* If it's an `(', then parse the attribute arguments. */
21722 if (token
->type
== CPP_OPEN_PAREN
)
21724 vec
<tree
, va_gc
> *vec
;
21725 int attr_flag
= (attribute_takes_identifier_p (identifier
)
21726 ? id_attr
: normal_attr
);
21727 if (is_cilkplus_vector_p (identifier
))
21729 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
21733 vec
= cp_parser_parenthesized_expression_list
21734 (parser
, attr_flag
, /*cast_p=*/false,
21735 /*allow_expansion_p=*/false,
21736 /*non_constant_p=*/NULL
);
21738 arguments
= error_mark_node
;
21741 arguments
= build_tree_list_vec (vec
);
21742 release_tree_vector (vec
);
21744 /* Save the arguments away. */
21745 TREE_VALUE (attribute
) = arguments
;
21747 else if (is_cilkplus_vector_p (identifier
))
21749 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
21753 if (arguments
!= error_mark_node
)
21755 /* Add this attribute to the list. */
21756 TREE_CHAIN (attribute
) = attribute_list
;
21757 attribute_list
= attribute
;
21760 token
= cp_lexer_peek_token (parser
->lexer
);
21762 /* Now, look for more attributes. If the next token isn't a
21763 `,', we're done. */
21764 if (token
->type
!= CPP_COMMA
)
21767 /* Consume the comma and keep going. */
21768 cp_lexer_consume_token (parser
->lexer
);
21770 parser
->translate_strings_p
= save_translate_strings_p
;
21772 /* We built up the list in reverse order. */
21773 return nreverse (attribute_list
);
21776 /* Parse a standard C++11 attribute.
21778 The returned representation is a TREE_LIST which TREE_PURPOSE is
21779 the scoped name of the attribute, and the TREE_VALUE is its
21782 Note that the scoped name of the attribute is itself a TREE_LIST
21783 which TREE_PURPOSE is the namespace of the attribute, and
21784 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21785 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21786 and which TREE_PURPOSE is directly the attribute name.
21788 Clients of the attribute code should use get_attribute_namespace
21789 and get_attribute_name to get the actual namespace and name of
21790 attributes, regardless of their being GNU or C++11 attributes.
21793 attribute-token attribute-argument-clause [opt]
21797 attribute-scoped-token
21799 attribute-scoped-token:
21800 attribute-namespace :: identifier
21802 attribute-namespace:
21805 attribute-argument-clause:
21806 ( balanced-token-seq )
21808 balanced-token-seq:
21809 balanced-token [opt]
21810 balanced-token-seq balanced-token
21813 ( balanced-token-seq )
21814 [ balanced-token-seq ]
21815 { balanced-token-seq }. */
21818 cp_parser_std_attribute (cp_parser
*parser
)
21820 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
21823 /* First, parse name of the the attribute, a.k.a
21824 attribute-token. */
21826 token
= cp_lexer_peek_token (parser
->lexer
);
21827 if (token
->type
== CPP_NAME
)
21828 attr_id
= token
->u
.value
;
21829 else if (token
->type
== CPP_KEYWORD
)
21830 attr_id
= ridpointers
[(int) token
->keyword
];
21831 else if (token
->flags
& NAMED_OP
)
21832 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
21834 if (attr_id
== NULL_TREE
)
21837 cp_lexer_consume_token (parser
->lexer
);
21839 token
= cp_lexer_peek_token (parser
->lexer
);
21840 if (token
->type
== CPP_SCOPE
)
21842 /* We are seeing a scoped attribute token. */
21844 cp_lexer_consume_token (parser
->lexer
);
21847 token
= cp_lexer_consume_token (parser
->lexer
);
21848 if (token
->type
== CPP_NAME
)
21849 attr_id
= token
->u
.value
;
21850 else if (token
->type
== CPP_KEYWORD
)
21851 attr_id
= ridpointers
[(int) token
->keyword
];
21854 error_at (token
->location
,
21855 "expected an identifier for the attribute name");
21856 return error_mark_node
;
21858 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
21860 token
= cp_lexer_peek_token (parser
->lexer
);
21864 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
21866 /* C++11 noreturn attribute is equivalent to GNU's. */
21867 if (is_attribute_p ("noreturn", attr_id
))
21868 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21869 /* C++14 deprecated attribute is equivalent to GNU's. */
21870 else if (cxx_dialect
>= cxx1y
&& is_attribute_p ("deprecated", attr_id
))
21871 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21874 /* Now parse the optional argument clause of the attribute. */
21876 if (token
->type
!= CPP_OPEN_PAREN
)
21880 vec
<tree
, va_gc
> *vec
;
21881 int attr_flag
= normal_attr
;
21883 if (attr_ns
== get_identifier ("gnu")
21884 && attribute_takes_identifier_p (attr_id
))
21885 /* A GNU attribute that takes an identifier in parameter. */
21886 attr_flag
= id_attr
;
21888 vec
= cp_parser_parenthesized_expression_list
21889 (parser
, attr_flag
, /*cast_p=*/false,
21890 /*allow_expansion_p=*/true,
21891 /*non_constant_p=*/NULL
);
21893 arguments
= error_mark_node
;
21896 arguments
= build_tree_list_vec (vec
);
21897 release_tree_vector (vec
);
21900 if (arguments
== error_mark_node
)
21901 attribute
= error_mark_node
;
21903 TREE_VALUE (attribute
) = arguments
;
21909 /* Parse a list of standard C++-11 attributes.
21913 attribute-list , attribute[opt]
21915 attribute-list , attribute ...
21919 cp_parser_std_attribute_list (cp_parser
*parser
)
21921 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
21922 cp_token
*token
= NULL
;
21926 attribute
= cp_parser_std_attribute (parser
);
21927 if (attribute
== error_mark_node
)
21929 if (attribute
!= NULL_TREE
)
21931 TREE_CHAIN (attribute
) = attributes
;
21932 attributes
= attribute
;
21934 token
= cp_lexer_peek_token (parser
->lexer
);
21935 if (token
->type
!= CPP_COMMA
)
21937 cp_lexer_consume_token (parser
->lexer
);
21939 attributes
= nreverse (attributes
);
21943 /* Parse a standard C++-11 attribute specifier.
21945 attribute-specifier:
21946 [ [ attribute-list ] ]
21947 alignment-specifier
21949 alignment-specifier:
21950 alignas ( type-id ... [opt] )
21951 alignas ( alignment-expression ... [opt] ). */
21954 cp_parser_std_attribute_spec (cp_parser
*parser
)
21956 tree attributes
= NULL_TREE
;
21957 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21959 if (token
->type
== CPP_OPEN_SQUARE
21960 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
21962 cp_lexer_consume_token (parser
->lexer
);
21963 cp_lexer_consume_token (parser
->lexer
);
21965 attributes
= cp_parser_std_attribute_list (parser
);
21967 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
21968 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
21969 cp_parser_skip_to_end_of_statement (parser
);
21971 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21972 when we are sure that we have actually parsed them. */
21973 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21979 /* Look for an alignment-specifier. */
21981 token
= cp_lexer_peek_token (parser
->lexer
);
21983 if (token
->type
!= CPP_KEYWORD
21984 || token
->keyword
!= RID_ALIGNAS
)
21987 cp_lexer_consume_token (parser
->lexer
);
21988 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21990 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
21992 cp_parser_error (parser
, "expected %<(%>");
21993 return error_mark_node
;
21996 cp_parser_parse_tentatively (parser
);
21997 alignas_expr
= cp_parser_type_id (parser
);
21999 if (!cp_parser_parse_definitely (parser
))
22001 gcc_assert (alignas_expr
== error_mark_node
22002 || alignas_expr
== NULL_TREE
);
22005 cp_parser_assignment_expression (parser
, /*cast_p=*/false,
22006 /**cp_id_kind=*/NULL
);
22007 if (alignas_expr
== error_mark_node
)
22008 cp_parser_skip_to_end_of_statement (parser
);
22009 if (alignas_expr
== NULL_TREE
22010 || alignas_expr
== error_mark_node
)
22011 return alignas_expr
;
22014 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
22016 cp_parser_error (parser
, "expected %<)%>");
22017 return error_mark_node
;
22020 alignas_expr
= cxx_alignas_expr (alignas_expr
);
22022 /* Build the C++-11 representation of an 'aligned'
22025 build_tree_list (build_tree_list (get_identifier ("gnu"),
22026 get_identifier ("aligned")),
22027 build_tree_list (NULL_TREE
, alignas_expr
));
22033 /* Parse a standard C++-11 attribute-specifier-seq.
22035 attribute-specifier-seq:
22036 attribute-specifier-seq [opt] attribute-specifier
22040 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
22042 tree attr_specs
= NULL
;
22046 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
22047 if (attr_spec
== NULL_TREE
)
22049 if (attr_spec
== error_mark_node
)
22050 return error_mark_node
;
22052 TREE_CHAIN (attr_spec
) = attr_specs
;
22053 attr_specs
= attr_spec
;
22056 attr_specs
= nreverse (attr_specs
);
22060 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22061 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22062 current value of the PEDANTIC flag, regardless of whether or not
22063 the `__extension__' keyword is present. The caller is responsible
22064 for restoring the value of the PEDANTIC flag. */
22067 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
22069 /* Save the old value of the PEDANTIC flag. */
22070 *saved_pedantic
= pedantic
;
22072 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
22074 /* Consume the `__extension__' token. */
22075 cp_lexer_consume_token (parser
->lexer
);
22076 /* We're not being pedantic while the `__extension__' keyword is
22086 /* Parse a label declaration.
22089 __label__ label-declarator-seq ;
22091 label-declarator-seq:
22092 identifier , label-declarator-seq
22096 cp_parser_label_declaration (cp_parser
* parser
)
22098 /* Look for the `__label__' keyword. */
22099 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
22105 /* Look for an identifier. */
22106 identifier
= cp_parser_identifier (parser
);
22107 /* If we failed, stop. */
22108 if (identifier
== error_mark_node
)
22110 /* Declare it as a label. */
22111 finish_label_decl (identifier
);
22112 /* If the next token is a `;', stop. */
22113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22115 /* Look for the `,' separating the label declarations. */
22116 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
22119 /* Look for the final `;'. */
22120 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22123 /* Support Functions */
22125 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22126 NAME should have one of the representations used for an
22127 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22128 is returned. If PARSER->SCOPE is a dependent type, then a
22129 SCOPE_REF is returned.
22131 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22132 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22133 was formed. Abstractly, such entities should not be passed to this
22134 function, because they do not need to be looked up, but it is
22135 simpler to check for this special case here, rather than at the
22138 In cases not explicitly covered above, this function returns a
22139 DECL, OVERLOAD, or baselink representing the result of the lookup.
22140 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22143 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22144 (e.g., "struct") that was used. In that case bindings that do not
22145 refer to types are ignored.
22147 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22150 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22153 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22156 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22157 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22158 NULL_TREE otherwise. */
22161 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
22162 enum tag_types tag_type
,
22165 bool check_dependency
,
22166 tree
*ambiguous_decls
,
22167 location_t name_location
)
22170 tree object_type
= parser
->context
->object_type
;
22172 /* Assume that the lookup will be unambiguous. */
22173 if (ambiguous_decls
)
22174 *ambiguous_decls
= NULL_TREE
;
22176 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22177 no longer valid. Note that if we are parsing tentatively, and
22178 the parse fails, OBJECT_TYPE will be automatically restored. */
22179 parser
->context
->object_type
= NULL_TREE
;
22181 if (name
== error_mark_node
)
22182 return error_mark_node
;
22184 /* A template-id has already been resolved; there is no lookup to
22186 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
22188 if (BASELINK_P (name
))
22190 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
22191 == TEMPLATE_ID_EXPR
);
22195 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22196 it should already have been checked to make sure that the name
22197 used matches the type being destroyed. */
22198 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
22202 /* Figure out to which type this destructor applies. */
22204 type
= parser
->scope
;
22205 else if (object_type
)
22206 type
= object_type
;
22208 type
= current_class_type
;
22209 /* If that's not a class type, there is no destructor. */
22210 if (!type
|| !CLASS_TYPE_P (type
))
22211 return error_mark_node
;
22212 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
22213 lazily_declare_fn (sfk_destructor
, type
);
22214 if (!CLASSTYPE_DESTRUCTORS (type
))
22215 return error_mark_node
;
22216 /* If it was a class type, return the destructor. */
22217 return CLASSTYPE_DESTRUCTORS (type
);
22220 /* By this point, the NAME should be an ordinary identifier. If
22221 the id-expression was a qualified name, the qualifying scope is
22222 stored in PARSER->SCOPE at this point. */
22223 gcc_assert (identifier_p (name
));
22225 /* Perform the lookup. */
22230 if (parser
->scope
== error_mark_node
)
22231 return error_mark_node
;
22233 /* If the SCOPE is dependent, the lookup must be deferred until
22234 the template is instantiated -- unless we are explicitly
22235 looking up names in uninstantiated templates. Even then, we
22236 cannot look up the name if the scope is not a class type; it
22237 might, for example, be a template type parameter. */
22238 dependent_p
= (TYPE_P (parser
->scope
)
22239 && dependent_scope_p (parser
->scope
));
22240 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
22242 /* Defer lookup. */
22243 decl
= error_mark_node
;
22246 tree pushed_scope
= NULL_TREE
;
22248 /* If PARSER->SCOPE is a dependent type, then it must be a
22249 class type, and we must not be checking dependencies;
22250 otherwise, we would have processed this lookup above. So
22251 that PARSER->SCOPE is not considered a dependent base by
22252 lookup_member, we must enter the scope here. */
22254 pushed_scope
= push_scope (parser
->scope
);
22256 /* If the PARSER->SCOPE is a template specialization, it
22257 may be instantiated during name lookup. In that case,
22258 errors may be issued. Even if we rollback the current
22259 tentative parse, those errors are valid. */
22260 decl
= lookup_qualified_name (parser
->scope
, name
,
22261 tag_type
!= none_type
,
22262 /*complain=*/true);
22264 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22265 lookup result and the nested-name-specifier nominates a class C:
22266 * if the name specified after the nested-name-specifier, when
22267 looked up in C, is the injected-class-name of C (Clause 9), or
22268 * if the name specified after the nested-name-specifier is the
22269 same as the identifier or the simple-template-id's template-
22270 name in the last component of the nested-name-specifier,
22271 the name is instead considered to name the constructor of
22272 class C. [ Note: for example, the constructor is not an
22273 acceptable lookup result in an elaborated-type-specifier so
22274 the constructor would not be used in place of the
22275 injected-class-name. --end note ] Such a constructor name
22276 shall be used only in the declarator-id of a declaration that
22277 names a constructor or in a using-declaration. */
22278 if (tag_type
== none_type
22279 && DECL_SELF_REFERENCE_P (decl
)
22280 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
22281 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
22282 tag_type
!= none_type
,
22283 /*complain=*/true);
22285 /* If we have a single function from a using decl, pull it out. */
22286 if (TREE_CODE (decl
) == OVERLOAD
22287 && !really_overloaded_fn (decl
))
22288 decl
= OVL_FUNCTION (decl
);
22291 pop_scope (pushed_scope
);
22294 /* If the scope is a dependent type and either we deferred lookup or
22295 we did lookup but didn't find the name, rememeber the name. */
22296 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
22297 && dependent_type_p (parser
->scope
))
22303 /* The resolution to Core Issue 180 says that `struct
22304 A::B' should be considered a type-name, even if `A'
22306 type
= make_typename_type (parser
->scope
, name
, tag_type
,
22307 /*complain=*/tf_error
);
22308 if (type
!= error_mark_node
)
22309 decl
= TYPE_NAME (type
);
22311 else if (is_template
22312 && (cp_parser_next_token_ends_template_argument_p (parser
)
22313 || cp_lexer_next_token_is (parser
->lexer
,
22315 decl
= make_unbound_class_template (parser
->scope
,
22317 /*complain=*/tf_error
);
22319 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22320 parser
->scope
, name
,
22323 parser
->qualifying_scope
= parser
->scope
;
22324 parser
->object_scope
= NULL_TREE
;
22326 else if (object_type
)
22328 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22329 OBJECT_TYPE is not a class. */
22330 if (CLASS_TYPE_P (object_type
))
22331 /* If the OBJECT_TYPE is a template specialization, it may
22332 be instantiated during name lookup. In that case, errors
22333 may be issued. Even if we rollback the current tentative
22334 parse, those errors are valid. */
22335 decl
= lookup_member (object_type
,
22338 tag_type
!= none_type
,
22339 tf_warning_or_error
);
22344 /* Look it up in the enclosing context. */
22345 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22347 /*block_p=*/true, is_namespace
, 0);
22348 parser
->object_scope
= object_type
;
22349 parser
->qualifying_scope
= NULL_TREE
;
22353 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22355 /*block_p=*/true, is_namespace
, 0);
22356 parser
->qualifying_scope
= NULL_TREE
;
22357 parser
->object_scope
= NULL_TREE
;
22360 /* If the lookup failed, let our caller know. */
22361 if (!decl
|| decl
== error_mark_node
)
22362 return error_mark_node
;
22364 /* Pull out the template from an injected-class-name (or multiple). */
22366 decl
= maybe_get_template_decl_from_type_decl (decl
);
22368 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22369 if (TREE_CODE (decl
) == TREE_LIST
)
22371 if (ambiguous_decls
)
22372 *ambiguous_decls
= decl
;
22373 /* The error message we have to print is too complicated for
22374 cp_parser_error, so we incorporate its actions directly. */
22375 if (!cp_parser_simulate_error (parser
))
22377 error_at (name_location
, "reference to %qD is ambiguous",
22379 print_candidates (decl
);
22381 return error_mark_node
;
22384 gcc_assert (DECL_P (decl
)
22385 || TREE_CODE (decl
) == OVERLOAD
22386 || TREE_CODE (decl
) == SCOPE_REF
22387 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
22388 || BASELINK_P (decl
));
22390 /* If we have resolved the name of a member declaration, check to
22391 see if the declaration is accessible. When the name resolves to
22392 set of overloaded functions, accessibility is checked when
22393 overload resolution is done.
22395 During an explicit instantiation, access is not checked at all,
22396 as per [temp.explicit]. */
22398 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
22400 maybe_record_typedef_use (decl
);
22405 /* Like cp_parser_lookup_name, but for use in the typical case where
22406 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22407 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22410 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
22412 return cp_parser_lookup_name (parser
, name
,
22414 /*is_template=*/false,
22415 /*is_namespace=*/false,
22416 /*check_dependency=*/true,
22417 /*ambiguous_decls=*/NULL
,
22421 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22422 the current context, return the TYPE_DECL. If TAG_NAME_P is
22423 true, the DECL indicates the class being defined in a class-head,
22424 or declared in an elaborated-type-specifier.
22426 Otherwise, return DECL. */
22429 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22431 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22432 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22435 template <typename T> struct B;
22438 template <typename T> struct A::B {};
22440 Similarly, in an elaborated-type-specifier:
22442 namespace N { struct X{}; }
22445 template <typename T> friend struct N::X;
22448 However, if the DECL refers to a class type, and we are in
22449 the scope of the class, then the name lookup automatically
22450 finds the TYPE_DECL created by build_self_reference rather
22451 than a TEMPLATE_DECL. For example, in:
22453 template <class T> struct S {
22457 there is no need to handle such case. */
22459 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
22460 return DECL_TEMPLATE_RESULT (decl
);
22465 /* If too many, or too few, template-parameter lists apply to the
22466 declarator, issue an error message. Returns TRUE if all went well,
22467 and FALSE otherwise. */
22470 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
22471 cp_declarator
*declarator
,
22472 location_t declarator_location
)
22474 switch (declarator
->kind
)
22478 unsigned num_templates
= 0;
22479 tree scope
= declarator
->u
.id
.qualifying_scope
;
22482 num_templates
= num_template_headers_for_class (scope
);
22483 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
22484 == TEMPLATE_ID_EXPR
)
22485 /* If the DECLARATOR has the form `X<y>' then it uses one
22486 additional level of template parameters. */
22489 return cp_parser_check_template_parameters
22490 (parser
, num_templates
, declarator_location
, declarator
);
22496 case cdk_reference
:
22498 return (cp_parser_check_declarator_template_parameters
22499 (parser
, declarator
->declarator
, declarator_location
));
22505 gcc_unreachable ();
22510 /* NUM_TEMPLATES were used in the current declaration. If that is
22511 invalid, return FALSE and issue an error messages. Otherwise,
22512 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22513 declarator and we can print more accurate diagnostics. */
22516 cp_parser_check_template_parameters (cp_parser
* parser
,
22517 unsigned num_templates
,
22518 location_t location
,
22519 cp_declarator
*declarator
)
22521 /* If there are the same number of template classes and parameter
22522 lists, that's OK. */
22523 if (parser
->num_template_parameter_lists
== num_templates
)
22525 /* If there are more, but only one more, then we are referring to a
22526 member template. That's OK too. */
22527 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
22529 /* If there are more template classes than parameter lists, we have
22532 template <class T> void S<T>::R<T>::f (); */
22533 if (parser
->num_template_parameter_lists
< num_templates
)
22535 if (declarator
&& !current_function_decl
)
22536 error_at (location
, "specializing member %<%T::%E%> "
22537 "requires %<template<>%> syntax",
22538 declarator
->u
.id
.qualifying_scope
,
22539 declarator
->u
.id
.unqualified_name
);
22540 else if (declarator
)
22541 error_at (location
, "invalid declaration of %<%T::%E%>",
22542 declarator
->u
.id
.qualifying_scope
,
22543 declarator
->u
.id
.unqualified_name
);
22545 error_at (location
, "too few template-parameter-lists");
22548 /* Otherwise, there are too many template parameter lists. We have
22551 template <class T> template <class U> void S::f(); */
22552 error_at (location
, "too many template-parameter-lists");
22556 /* Parse an optional `::' token indicating that the following name is
22557 from the global namespace. If so, PARSER->SCOPE is set to the
22558 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22559 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22560 Returns the new value of PARSER->SCOPE, if the `::' token is
22561 present, and NULL_TREE otherwise. */
22564 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
22568 /* Peek at the next token. */
22569 token
= cp_lexer_peek_token (parser
->lexer
);
22570 /* If we're looking at a `::' token then we're starting from the
22571 global namespace, not our current location. */
22572 if (token
->type
== CPP_SCOPE
)
22574 /* Consume the `::' token. */
22575 cp_lexer_consume_token (parser
->lexer
);
22576 /* Set the SCOPE so that we know where to start the lookup. */
22577 parser
->scope
= global_namespace
;
22578 parser
->qualifying_scope
= global_namespace
;
22579 parser
->object_scope
= NULL_TREE
;
22581 return parser
->scope
;
22583 else if (!current_scope_valid_p
)
22585 parser
->scope
= NULL_TREE
;
22586 parser
->qualifying_scope
= NULL_TREE
;
22587 parser
->object_scope
= NULL_TREE
;
22593 /* Returns TRUE if the upcoming token sequence is the start of a
22594 constructor declarator. If FRIEND_P is true, the declarator is
22595 preceded by the `friend' specifier. */
22598 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
22600 bool constructor_p
;
22601 bool outside_class_specifier_p
;
22602 tree nested_name_specifier
;
22603 cp_token
*next_token
;
22605 /* The common case is that this is not a constructor declarator, so
22606 try to avoid doing lots of work if at all possible. It's not
22607 valid declare a constructor at function scope. */
22608 if (parser
->in_function_body
)
22610 /* And only certain tokens can begin a constructor declarator. */
22611 next_token
= cp_lexer_peek_token (parser
->lexer
);
22612 if (next_token
->type
!= CPP_NAME
22613 && next_token
->type
!= CPP_SCOPE
22614 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
22615 && next_token
->type
!= CPP_TEMPLATE_ID
)
22618 /* Parse tentatively; we are going to roll back all of the tokens
22620 cp_parser_parse_tentatively (parser
);
22621 /* Assume that we are looking at a constructor declarator. */
22622 constructor_p
= true;
22624 /* Look for the optional `::' operator. */
22625 cp_parser_global_scope_opt (parser
,
22626 /*current_scope_valid_p=*/false);
22627 /* Look for the nested-name-specifier. */
22628 nested_name_specifier
22629 = (cp_parser_nested_name_specifier_opt (parser
,
22630 /*typename_keyword_p=*/false,
22631 /*check_dependency_p=*/false,
22633 /*is_declaration=*/false));
22635 outside_class_specifier_p
= (!at_class_scope_p ()
22636 || !TYPE_BEING_DEFINED (current_class_type
)
22639 /* Outside of a class-specifier, there must be a
22640 nested-name-specifier. */
22641 if (!nested_name_specifier
&& outside_class_specifier_p
)
22642 constructor_p
= false;
22643 else if (nested_name_specifier
== error_mark_node
)
22644 constructor_p
= false;
22646 /* If we have a class scope, this is easy; DR 147 says that S::S always
22647 names the constructor, and no other qualified name could. */
22648 if (constructor_p
&& nested_name_specifier
22649 && CLASS_TYPE_P (nested_name_specifier
))
22651 tree id
= cp_parser_unqualified_id (parser
,
22652 /*template_keyword_p=*/false,
22653 /*check_dependency_p=*/false,
22654 /*declarator_p=*/true,
22655 /*optional_p=*/false);
22656 if (is_overloaded_fn (id
))
22657 id
= DECL_NAME (get_first_fn (id
));
22658 if (!constructor_name_p (id
, nested_name_specifier
))
22659 constructor_p
= false;
22661 /* If we still think that this might be a constructor-declarator,
22662 look for a class-name. */
22663 else if (constructor_p
)
22667 template <typename T> struct S {
22671 we must recognize that the nested `S' names a class. */
22673 type_decl
= cp_parser_class_name (parser
,
22674 /*typename_keyword_p=*/false,
22675 /*template_keyword_p=*/false,
22677 /*check_dependency_p=*/false,
22678 /*class_head_p=*/false,
22679 /*is_declaration=*/false);
22680 /* If there was no class-name, then this is not a constructor.
22681 Otherwise, if we are in a class-specifier and we aren't
22682 handling a friend declaration, check that its type matches
22683 current_class_type (c++/38313). Note: error_mark_node
22684 is left alone for error recovery purposes. */
22685 constructor_p
= (!cp_parser_error_occurred (parser
)
22686 && (outside_class_specifier_p
22687 || type_decl
== error_mark_node
22688 || same_type_p (current_class_type
,
22689 TREE_TYPE (type_decl
))));
22691 /* If we're still considering a constructor, we have to see a `(',
22692 to begin the parameter-declaration-clause, followed by either a
22693 `)', an `...', or a decl-specifier. We need to check for a
22694 type-specifier to avoid being fooled into thinking that:
22698 is a constructor. (It is actually a function named `f' that
22699 takes one parameter (of type `int') and returns a value of type
22702 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22703 constructor_p
= false;
22706 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
22707 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
22708 /* A parameter declaration begins with a decl-specifier,
22709 which is either the "attribute" keyword, a storage class
22710 specifier, or (usually) a type-specifier. */
22711 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
22714 tree pushed_scope
= NULL_TREE
;
22715 unsigned saved_num_template_parameter_lists
;
22717 /* Names appearing in the type-specifier should be looked up
22718 in the scope of the class. */
22719 if (current_class_type
)
22723 type
= TREE_TYPE (type_decl
);
22724 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22726 type
= resolve_typename_type (type
,
22727 /*only_current_p=*/false);
22728 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22730 cp_parser_abort_tentative_parse (parser
);
22734 pushed_scope
= push_scope (type
);
22737 /* Inside the constructor parameter list, surrounding
22738 template-parameter-lists do not apply. */
22739 saved_num_template_parameter_lists
22740 = parser
->num_template_parameter_lists
;
22741 parser
->num_template_parameter_lists
= 0;
22743 /* Look for the type-specifier. */
22744 cp_parser_type_specifier (parser
,
22745 CP_PARSER_FLAGS_NONE
,
22746 /*decl_specs=*/NULL
,
22747 /*is_declarator=*/true,
22748 /*declares_class_or_enum=*/NULL
,
22749 /*is_cv_qualifier=*/NULL
);
22751 parser
->num_template_parameter_lists
22752 = saved_num_template_parameter_lists
;
22754 /* Leave the scope of the class. */
22756 pop_scope (pushed_scope
);
22758 constructor_p
= !cp_parser_error_occurred (parser
);
22762 /* We did not really want to consume any tokens. */
22763 cp_parser_abort_tentative_parse (parser
);
22765 return constructor_p
;
22768 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22769 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22770 they must be performed once we are in the scope of the function.
22772 Returns the function defined. */
22775 cp_parser_function_definition_from_specifiers_and_declarator
22776 (cp_parser
* parser
,
22777 cp_decl_specifier_seq
*decl_specifiers
,
22779 const cp_declarator
*declarator
)
22784 /* Begin the function-definition. */
22785 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
22787 /* The things we're about to see are not directly qualified by any
22788 template headers we've seen thus far. */
22789 reset_specialization ();
22791 /* If there were names looked up in the decl-specifier-seq that we
22792 did not check, check them now. We must wait until we are in the
22793 scope of the function to perform the checks, since the function
22794 might be a friend. */
22795 perform_deferred_access_checks (tf_warning_or_error
);
22799 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
22800 parser
->omp_declare_simd
= NULL
;
22805 /* Skip the entire function. */
22806 cp_parser_skip_to_end_of_block_or_statement (parser
);
22807 fn
= error_mark_node
;
22809 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
22811 /* Seen already, skip it. An error message has already been output. */
22812 cp_parser_skip_to_end_of_block_or_statement (parser
);
22813 fn
= current_function_decl
;
22814 current_function_decl
= NULL_TREE
;
22815 /* If this is a function from a class, pop the nested class. */
22816 if (current_class_name
)
22817 pop_nested_class ();
22822 if (DECL_DECLARED_INLINE_P (current_function_decl
))
22823 tv
= TV_PARSE_INLINE
;
22825 tv
= TV_PARSE_FUNC
;
22827 fn
= cp_parser_function_definition_after_declarator (parser
,
22828 /*inline_p=*/false);
22835 /* Parse the part of a function-definition that follows the
22836 declarator. INLINE_P is TRUE iff this function is an inline
22837 function defined within a class-specifier.
22839 Returns the function defined. */
22842 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
22846 bool ctor_initializer_p
= false;
22847 bool saved_in_unbraced_linkage_specification_p
;
22848 bool saved_in_function_body
;
22849 unsigned saved_num_template_parameter_lists
;
22851 bool fully_implicit_function_template_p
22852 = parser
->fully_implicit_function_template_p
;
22853 parser
->fully_implicit_function_template_p
= false;
22854 tree implicit_template_parms
22855 = parser
->implicit_template_parms
;
22856 parser
->implicit_template_parms
= 0;
22857 cp_binding_level
* implicit_template_scope
22858 = parser
->implicit_template_scope
;
22859 parser
->implicit_template_scope
= 0;
22861 saved_in_function_body
= parser
->in_function_body
;
22862 parser
->in_function_body
= true;
22863 /* If the next token is `return', then the code may be trying to
22864 make use of the "named return value" extension that G++ used to
22866 token
= cp_lexer_peek_token (parser
->lexer
);
22867 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
22869 /* Consume the `return' keyword. */
22870 cp_lexer_consume_token (parser
->lexer
);
22871 /* Look for the identifier that indicates what value is to be
22873 cp_parser_identifier (parser
);
22874 /* Issue an error message. */
22875 error_at (token
->location
,
22876 "named return values are no longer supported");
22877 /* Skip tokens until we reach the start of the function body. */
22880 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22881 if (token
->type
== CPP_OPEN_BRACE
22882 || token
->type
== CPP_EOF
22883 || token
->type
== CPP_PRAGMA_EOL
)
22885 cp_lexer_consume_token (parser
->lexer
);
22888 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22889 anything declared inside `f'. */
22890 saved_in_unbraced_linkage_specification_p
22891 = parser
->in_unbraced_linkage_specification_p
;
22892 parser
->in_unbraced_linkage_specification_p
= false;
22893 /* Inside the function, surrounding template-parameter-lists do not
22895 saved_num_template_parameter_lists
22896 = parser
->num_template_parameter_lists
;
22897 parser
->num_template_parameter_lists
= 0;
22899 start_lambda_scope (current_function_decl
);
22901 /* If the next token is `try', `__transaction_atomic', or
22902 `__transaction_relaxed`, then we are looking at either function-try-block
22903 or function-transaction-block. Note that all of these include the
22905 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
22906 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22907 RID_TRANSACTION_ATOMIC
);
22908 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
22909 RID_TRANSACTION_RELAXED
))
22910 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22911 RID_TRANSACTION_RELAXED
);
22912 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
22913 ctor_initializer_p
= cp_parser_function_try_block (parser
);
22915 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
22916 (parser
, /*in_function_try_block=*/false);
22918 finish_lambda_scope ();
22920 /* Finish the function. */
22921 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
22922 (inline_p
? 2 : 0));
22923 /* Generate code for it, if necessary. */
22924 expand_or_defer_fn (fn
);
22925 /* Restore the saved values. */
22926 parser
->in_unbraced_linkage_specification_p
22927 = saved_in_unbraced_linkage_specification_p
;
22928 parser
->num_template_parameter_lists
22929 = saved_num_template_parameter_lists
;
22930 parser
->in_function_body
= saved_in_function_body
;
22932 parser
->fully_implicit_function_template_p
22933 = fully_implicit_function_template_p
;
22934 parser
->implicit_template_parms
22935 = implicit_template_parms
;
22936 parser
->implicit_template_scope
22937 = implicit_template_scope
;
22939 if (parser
->fully_implicit_function_template_p
)
22940 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
22945 /* Parse a template-declaration, assuming that the `export' (and
22946 `extern') keywords, if present, has already been scanned. MEMBER_P
22947 is as for cp_parser_template_declaration. */
22950 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
22952 tree decl
= NULL_TREE
;
22953 vec
<deferred_access_check
, va_gc
> *checks
;
22954 tree parameter_list
;
22955 bool friend_p
= false;
22956 bool need_lang_pop
;
22959 /* Look for the `template' keyword. */
22960 token
= cp_lexer_peek_token (parser
->lexer
);
22961 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
22965 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
22967 if (at_class_scope_p () && current_function_decl
)
22969 /* 14.5.2.2 [temp.mem]
22971 A local class shall not have member templates. */
22972 error_at (token
->location
,
22973 "invalid declaration of member template in local class");
22974 cp_parser_skip_to_end_of_block_or_statement (parser
);
22979 A template ... shall not have C linkage. */
22980 if (current_lang_name
== lang_name_c
)
22982 error_at (token
->location
, "template with C linkage");
22983 /* Give it C++ linkage to avoid confusing other parts of the
22985 push_lang_context (lang_name_cplusplus
);
22986 need_lang_pop
= true;
22989 need_lang_pop
= false;
22991 /* We cannot perform access checks on the template parameter
22992 declarations until we know what is being declared, just as we
22993 cannot check the decl-specifier list. */
22994 push_deferring_access_checks (dk_deferred
);
22996 /* If the next token is `>', then we have an invalid
22997 specialization. Rather than complain about an invalid template
22998 parameter, issue an error message here. */
22999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
23001 cp_parser_error (parser
, "invalid explicit specialization");
23002 begin_specialization ();
23003 parameter_list
= NULL_TREE
;
23007 /* Parse the template parameters. */
23008 parameter_list
= cp_parser_template_parameter_list (parser
);
23011 /* Get the deferred access checks from the parameter list. These
23012 will be checked once we know what is being declared, as for a
23013 member template the checks must be performed in the scope of the
23014 class containing the member. */
23015 checks
= get_deferred_access_checks ();
23017 /* Look for the `>'. */
23018 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23019 /* We just processed one more parameter list. */
23020 ++parser
->num_template_parameter_lists
;
23021 /* If the next token is `template', there are more template
23023 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23025 cp_parser_template_declaration_after_export (parser
, member_p
);
23026 else if (cxx_dialect
>= cxx11
23027 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23028 decl
= cp_parser_alias_declaration (parser
);
23031 /* There are no access checks when parsing a template, as we do not
23032 know if a specialization will be a friend. */
23033 push_deferring_access_checks (dk_no_check
);
23034 token
= cp_lexer_peek_token (parser
->lexer
);
23035 decl
= cp_parser_single_declaration (parser
,
23038 /*explicit_specialization_p=*/false,
23040 pop_deferring_access_checks ();
23042 /* If this is a member template declaration, let the front
23044 if (member_p
&& !friend_p
&& decl
)
23046 if (TREE_CODE (decl
) == TYPE_DECL
)
23047 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
23049 decl
= finish_member_template_decl (decl
);
23051 else if (friend_p
&& decl
23052 && DECL_DECLARES_TYPE_P (decl
))
23053 make_friend_class (current_class_type
, TREE_TYPE (decl
),
23054 /*complain=*/true);
23056 /* We are done with the current parameter list. */
23057 --parser
->num_template_parameter_lists
;
23059 pop_deferring_access_checks ();
23062 finish_template_decl (parameter_list
);
23064 /* Check the template arguments for a literal operator template. */
23066 && DECL_DECLARES_FUNCTION_P (decl
)
23067 && UDLIT_OPER_P (DECL_NAME (decl
)))
23070 if (parameter_list
== NULL_TREE
)
23074 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
23075 if (num_parms
== 1)
23077 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
23078 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23079 if (TREE_TYPE (parm
) != char_type_node
23080 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23083 else if (num_parms
== 2 && cxx_dialect
>= cxx1y
)
23085 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
23086 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
23087 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
23088 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23089 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
23090 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23097 error ("literal operator template %qD has invalid parameter list."
23098 " Expected non-type template argument pack <char...>"
23099 " or <typename CharT, CharT...>",
23102 /* Register member declarations. */
23103 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
23104 finish_member_declaration (decl
);
23105 /* For the erroneous case of a template with C linkage, we pushed an
23106 implicit C++ linkage scope; exit that scope now. */
23108 pop_lang_context ();
23109 /* If DECL is a function template, we must return to parse it later.
23110 (Even though there is no definition, there might be default
23111 arguments that need handling.) */
23112 if (member_p
&& decl
23113 && DECL_DECLARES_FUNCTION_P (decl
))
23114 vec_safe_push (unparsed_funs_with_definitions
, decl
);
23117 /* Perform the deferred access checks from a template-parameter-list.
23118 CHECKS is a TREE_LIST of access checks, as returned by
23119 get_deferred_access_checks. */
23122 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
23124 ++processing_template_parmlist
;
23125 perform_access_checks (checks
, tf_warning_or_error
);
23126 --processing_template_parmlist
;
23129 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23130 `function-definition' sequence that follows a template header.
23131 If MEMBER_P is true, this declaration appears in a class scope.
23133 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23134 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23137 cp_parser_single_declaration (cp_parser
* parser
,
23138 vec
<deferred_access_check
, va_gc
> *checks
,
23140 bool explicit_specialization_p
,
23143 int declares_class_or_enum
;
23144 tree decl
= NULL_TREE
;
23145 cp_decl_specifier_seq decl_specifiers
;
23146 bool function_definition_p
= false;
23147 cp_token
*decl_spec_token_start
;
23149 /* This function is only used when processing a template
23151 gcc_assert (innermost_scope_kind () == sk_template_parms
23152 || innermost_scope_kind () == sk_template_spec
);
23154 /* Defer access checks until we know what is being declared. */
23155 push_deferring_access_checks (dk_deferred
);
23157 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23159 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23160 cp_parser_decl_specifier_seq (parser
,
23161 CP_PARSER_FLAGS_OPTIONAL
,
23163 &declares_class_or_enum
);
23165 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
23167 /* There are no template typedefs. */
23168 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
23170 error_at (decl_spec_token_start
->location
,
23171 "template declaration of %<typedef%>");
23172 decl
= error_mark_node
;
23175 /* Gather up the access checks that occurred the
23176 decl-specifier-seq. */
23177 stop_deferring_access_checks ();
23179 /* Check for the declaration of a template class. */
23180 if (declares_class_or_enum
)
23182 if (cp_parser_declares_only_class_p (parser
))
23184 decl
= shadow_tag (&decl_specifiers
);
23189 friend template <typename T> struct A<T>::B;
23192 A<T>::B will be represented by a TYPENAME_TYPE, and
23193 therefore not recognized by shadow_tag. */
23194 if (friend_p
&& *friend_p
23196 && decl_specifiers
.type
23197 && TYPE_P (decl_specifiers
.type
))
23198 decl
= decl_specifiers
.type
;
23200 if (decl
&& decl
!= error_mark_node
)
23201 decl
= TYPE_NAME (decl
);
23203 decl
= error_mark_node
;
23205 /* Perform access checks for template parameters. */
23206 cp_parser_perform_template_parameter_access_checks (checks
);
23210 /* Complain about missing 'typename' or other invalid type names. */
23211 if (!decl_specifiers
.any_type_specifiers_p
23212 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23214 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23215 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23216 the rest of this declaration. */
23217 decl
= error_mark_node
;
23221 /* If it's not a template class, try for a template function. If
23222 the next token is a `;', then this declaration does not declare
23223 anything. But, if there were errors in the decl-specifiers, then
23224 the error might well have come from an attempted class-specifier.
23225 In that case, there's no need to warn about a missing declarator. */
23227 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
23228 || decl_specifiers
.type
!= error_mark_node
))
23230 decl
= cp_parser_init_declarator (parser
,
23233 /*function_definition_allowed_p=*/true,
23235 declares_class_or_enum
,
23236 &function_definition_p
,
23239 /* 7.1.1-1 [dcl.stc]
23241 A storage-class-specifier shall not be specified in an explicit
23242 specialization... */
23244 && explicit_specialization_p
23245 && decl_specifiers
.storage_class
!= sc_none
)
23247 error_at (decl_spec_token_start
->location
,
23248 "explicit template specialization cannot have a storage class");
23249 decl
= error_mark_node
;
23252 if (decl
&& VAR_P (decl
))
23253 check_template_variable (decl
);
23256 /* Look for a trailing `;' after the declaration. */
23257 if (!function_definition_p
23258 && (decl
== error_mark_node
23259 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
23260 cp_parser_skip_to_end_of_block_or_statement (parser
);
23263 pop_deferring_access_checks ();
23265 /* Clear any current qualification; whatever comes next is the start
23266 of something new. */
23267 parser
->scope
= NULL_TREE
;
23268 parser
->qualifying_scope
= NULL_TREE
;
23269 parser
->object_scope
= NULL_TREE
;
23274 /* Parse a cast-expression that is not the operand of a unary "&". */
23277 cp_parser_simple_cast_expression (cp_parser
*parser
)
23279 return cp_parser_cast_expression (parser
, /*address_p=*/false,
23280 /*cast_p=*/false, /*decltype*/false, NULL
);
23283 /* Parse a functional cast to TYPE. Returns an expression
23284 representing the cast. */
23287 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
23289 vec
<tree
, va_gc
> *vec
;
23290 tree expression_list
;
23295 type
= error_mark_node
;
23297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23299 cp_lexer_set_source_position (parser
->lexer
);
23300 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23301 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
23302 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
23303 if (TREE_CODE (type
) == TYPE_DECL
)
23304 type
= TREE_TYPE (type
);
23305 return finish_compound_literal (type
, expression_list
,
23306 tf_warning_or_error
);
23310 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23312 /*allow_expansion_p=*/true,
23313 /*non_constant_p=*/NULL
);
23315 expression_list
= error_mark_node
;
23318 expression_list
= build_tree_list_vec (vec
);
23319 release_tree_vector (vec
);
23322 cast
= build_functional_cast (type
, expression_list
,
23323 tf_warning_or_error
);
23324 /* [expr.const]/1: In an integral constant expression "only type
23325 conversions to integral or enumeration type can be used". */
23326 if (TREE_CODE (type
) == TYPE_DECL
)
23327 type
= TREE_TYPE (type
);
23328 if (cast
!= error_mark_node
23329 && !cast_valid_in_integral_constant_expression_p (type
)
23330 && cp_parser_non_integral_constant_expression (parser
,
23332 return error_mark_node
;
23336 /* Save the tokens that make up the body of a member function defined
23337 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23338 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23339 specifiers applied to the declaration. Returns the FUNCTION_DECL
23340 for the member function. */
23343 cp_parser_save_member_function_body (cp_parser
* parser
,
23344 cp_decl_specifier_seq
*decl_specifiers
,
23345 cp_declarator
*declarator
,
23352 /* Create the FUNCTION_DECL. */
23353 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
23354 cp_finalize_omp_declare_simd (parser
, fn
);
23355 /* If something went badly wrong, bail out now. */
23356 if (fn
== error_mark_node
)
23358 /* If there's a function-body, skip it. */
23359 if (cp_parser_token_starts_function_definition_p
23360 (cp_lexer_peek_token (parser
->lexer
)))
23361 cp_parser_skip_to_end_of_block_or_statement (parser
);
23362 return error_mark_node
;
23365 /* Remember it, if there default args to post process. */
23366 cp_parser_save_default_args (parser
, fn
);
23368 /* Save away the tokens that make up the body of the
23370 first
= parser
->lexer
->next_token
;
23371 /* Handle function try blocks. */
23372 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23373 cp_lexer_consume_token (parser
->lexer
);
23374 /* We can have braced-init-list mem-initializers before the fn body. */
23375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23377 cp_lexer_consume_token (parser
->lexer
);
23378 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23380 /* cache_group will stop after an un-nested { } pair, too. */
23381 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
23384 /* variadic mem-inits have ... after the ')'. */
23385 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23386 cp_lexer_consume_token (parser
->lexer
);
23389 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23390 /* Handle function try blocks. */
23391 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
23392 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23393 last
= parser
->lexer
->next_token
;
23395 /* Save away the inline definition; we will process it when the
23396 class is complete. */
23397 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
23398 DECL_PENDING_INLINE_P (fn
) = 1;
23400 /* We need to know that this was defined in the class, so that
23401 friend templates are handled correctly. */
23402 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
23404 /* Add FN to the queue of functions to be parsed later. */
23405 vec_safe_push (unparsed_funs_with_definitions
, fn
);
23410 /* Save the tokens that make up the in-class initializer for a non-static
23411 data member. Returns a DEFAULT_ARG. */
23414 cp_parser_save_nsdmi (cp_parser
* parser
)
23416 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
23419 /* Parse a template-argument-list, as well as the trailing ">" (but
23420 not the opening "<"). See cp_parser_template_argument_list for the
23424 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
23428 tree saved_qualifying_scope
;
23429 tree saved_object_scope
;
23430 bool saved_greater_than_is_operator_p
;
23431 int saved_unevaluated_operand
;
23432 int saved_inhibit_evaluation_warnings
;
23436 When parsing a template-id, the first non-nested `>' is taken as
23437 the end of the template-argument-list rather than a greater-than
23439 saved_greater_than_is_operator_p
23440 = parser
->greater_than_is_operator_p
;
23441 parser
->greater_than_is_operator_p
= false;
23442 /* Parsing the argument list may modify SCOPE, so we save it
23444 saved_scope
= parser
->scope
;
23445 saved_qualifying_scope
= parser
->qualifying_scope
;
23446 saved_object_scope
= parser
->object_scope
;
23447 /* We need to evaluate the template arguments, even though this
23448 template-id may be nested within a "sizeof". */
23449 saved_unevaluated_operand
= cp_unevaluated_operand
;
23450 cp_unevaluated_operand
= 0;
23451 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23452 c_inhibit_evaluation_warnings
= 0;
23453 /* Parse the template-argument-list itself. */
23454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
23455 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23456 arguments
= NULL_TREE
;
23458 arguments
= cp_parser_template_argument_list (parser
);
23459 /* Look for the `>' that ends the template-argument-list. If we find
23460 a '>>' instead, it's probably just a typo. */
23461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23463 if (cxx_dialect
!= cxx98
)
23465 /* In C++0x, a `>>' in a template argument list or cast
23466 expression is considered to be two separate `>'
23467 tokens. So, change the current token to a `>', but don't
23468 consume it: it will be consumed later when the outer
23469 template argument list (or cast expression) is parsed.
23470 Note that this replacement of `>' for `>>' is necessary
23471 even if we are parsing tentatively: in the tentative
23472 case, after calling
23473 cp_parser_enclosed_template_argument_list we will always
23474 throw away all of the template arguments and the first
23475 closing `>', either because the template argument list
23476 was erroneous or because we are replacing those tokens
23477 with a CPP_TEMPLATE_ID token. The second `>' (which will
23478 not have been thrown away) is needed either to close an
23479 outer template argument list or to complete a new-style
23481 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23482 token
->type
= CPP_GREATER
;
23484 else if (!saved_greater_than_is_operator_p
)
23486 /* If we're in a nested template argument list, the '>>' has
23487 to be a typo for '> >'. We emit the error message, but we
23488 continue parsing and we push a '>' as next token, so that
23489 the argument list will be parsed correctly. Note that the
23490 global source location is still on the token before the
23491 '>>', so we need to say explicitly where we want it. */
23492 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23493 error_at (token
->location
, "%<>>%> should be %<> >%> "
23494 "within a nested template argument list");
23496 token
->type
= CPP_GREATER
;
23500 /* If this is not a nested template argument list, the '>>'
23501 is a typo for '>'. Emit an error message and continue.
23502 Same deal about the token location, but here we can get it
23503 right by consuming the '>>' before issuing the diagnostic. */
23504 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
23505 error_at (token
->location
,
23506 "spurious %<>>%>, use %<>%> to terminate "
23507 "a template argument list");
23511 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23512 /* The `>' token might be a greater-than operator again now. */
23513 parser
->greater_than_is_operator_p
23514 = saved_greater_than_is_operator_p
;
23515 /* Restore the SAVED_SCOPE. */
23516 parser
->scope
= saved_scope
;
23517 parser
->qualifying_scope
= saved_qualifying_scope
;
23518 parser
->object_scope
= saved_object_scope
;
23519 cp_unevaluated_operand
= saved_unevaluated_operand
;
23520 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23525 /* MEMBER_FUNCTION is a member function, or a friend. If default
23526 arguments, or the body of the function have not yet been parsed,
23530 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
23532 timevar_push (TV_PARSE_INMETH
);
23533 /* If this member is a template, get the underlying
23535 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
23536 member_function
= DECL_TEMPLATE_RESULT (member_function
);
23538 /* There should not be any class definitions in progress at this
23539 point; the bodies of members are only parsed outside of all class
23541 gcc_assert (parser
->num_classes_being_defined
== 0);
23542 /* While we're parsing the member functions we might encounter more
23543 classes. We want to handle them right away, but we don't want
23544 them getting mixed up with functions that are currently in the
23546 push_unparsed_function_queues (parser
);
23548 /* Make sure that any template parameters are in scope. */
23549 maybe_begin_member_template_processing (member_function
);
23551 /* If the body of the function has not yet been parsed, parse it
23553 if (DECL_PENDING_INLINE_P (member_function
))
23555 tree function_scope
;
23556 cp_token_cache
*tokens
;
23558 /* The function is no longer pending; we are processing it. */
23559 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
23560 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
23561 DECL_PENDING_INLINE_P (member_function
) = 0;
23563 /* If this is a local class, enter the scope of the containing
23565 function_scope
= current_function_decl
;
23566 if (function_scope
)
23567 push_function_context ();
23569 /* Push the body of the function onto the lexer stack. */
23570 cp_parser_push_lexer_for_tokens (parser
, tokens
);
23572 /* Let the front end know that we going to be defining this
23574 start_preparsed_function (member_function
, NULL_TREE
,
23575 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
23577 /* Don't do access checking if it is a templated function. */
23578 if (processing_template_decl
)
23579 push_deferring_access_checks (dk_no_check
);
23581 /* #pragma omp declare reduction needs special parsing. */
23582 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
23584 parser
->lexer
->in_pragma
= true;
23585 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
23586 finish_function (/*inline*/2);
23587 cp_check_omp_declare_reduction (member_function
);
23590 /* Now, parse the body of the function. */
23591 cp_parser_function_definition_after_declarator (parser
,
23592 /*inline_p=*/true);
23594 if (processing_template_decl
)
23595 pop_deferring_access_checks ();
23597 /* Leave the scope of the containing function. */
23598 if (function_scope
)
23599 pop_function_context ();
23600 cp_parser_pop_lexer (parser
);
23603 /* Remove any template parameters from the symbol table. */
23604 maybe_end_member_template_processing ();
23606 /* Restore the queue. */
23607 pop_unparsed_function_queues (parser
);
23608 timevar_pop (TV_PARSE_INMETH
);
23611 /* If DECL contains any default args, remember it on the unparsed
23612 functions queue. */
23615 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
23619 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
23621 probe
= TREE_CHAIN (probe
))
23622 if (TREE_PURPOSE (probe
))
23624 cp_default_arg_entry entry
= {current_class_type
, decl
};
23625 vec_safe_push (unparsed_funs_with_default_args
, entry
);
23630 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23631 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23632 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23633 from the parameter-type-list. */
23636 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
23637 tree default_arg
, tree parmtype
)
23639 cp_token_cache
*tokens
;
23643 if (default_arg
== error_mark_node
)
23644 return error_mark_node
;
23646 /* Push the saved tokens for the default argument onto the parser's
23648 tokens
= DEFARG_TOKENS (default_arg
);
23649 cp_parser_push_lexer_for_tokens (parser
, tokens
);
23651 start_lambda_scope (decl
);
23653 /* Parse the default argument. */
23654 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
23655 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
23656 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23658 finish_lambda_scope ();
23660 if (parsed_arg
== error_mark_node
)
23661 cp_parser_skip_to_end_of_statement (parser
);
23663 if (!processing_template_decl
)
23665 /* In a non-template class, check conversions now. In a template,
23666 we'll wait and instantiate these as needed. */
23667 if (TREE_CODE (decl
) == PARM_DECL
)
23668 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
23669 tf_warning_or_error
);
23672 int flags
= LOOKUP_IMPLICIT
;
23673 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
)
23674 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg
))
23675 flags
= LOOKUP_NORMAL
;
23676 parsed_arg
= digest_init_flags (TREE_TYPE (decl
), parsed_arg
, flags
);
23677 if (TREE_CODE (parsed_arg
) == TARGET_EXPR
)
23678 /* This represents the whole initialization. */
23679 TARGET_EXPR_DIRECT_INIT_P (parsed_arg
) = true;
23683 /* If the token stream has not been completely used up, then
23684 there was extra junk after the end of the default
23686 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
23688 if (TREE_CODE (decl
) == PARM_DECL
)
23689 cp_parser_error (parser
, "expected %<,%>");
23691 cp_parser_error (parser
, "expected %<;%>");
23694 /* Revert to the main lexer. */
23695 cp_parser_pop_lexer (parser
);
23700 /* FIELD is a non-static data member with an initializer which we saved for
23701 later; parse it now. */
23704 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
23708 maybe_begin_member_template_processing (field
);
23710 push_unparsed_function_queues (parser
);
23711 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
23712 DECL_INITIAL (field
),
23714 pop_unparsed_function_queues (parser
);
23716 maybe_end_member_template_processing ();
23718 DECL_INITIAL (field
) = def
;
23721 /* FN is a FUNCTION_DECL which may contains a parameter with an
23722 unparsed DEFAULT_ARG. Parse the default args now. This function
23723 assumes that the current scope is the scope in which the default
23724 argument should be processed. */
23727 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
23729 bool saved_local_variables_forbidden_p
;
23730 tree parm
, parmdecl
;
23732 /* While we're parsing the default args, we might (due to the
23733 statement expression extension) encounter more classes. We want
23734 to handle them right away, but we don't want them getting mixed
23735 up with default args that are currently in the queue. */
23736 push_unparsed_function_queues (parser
);
23738 /* Local variable names (and the `this' keyword) may not appear
23739 in a default argument. */
23740 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
23741 parser
->local_variables_forbidden_p
= true;
23743 push_defarg_context (fn
);
23745 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
23746 parmdecl
= DECL_ARGUMENTS (fn
);
23747 parm
&& parm
!= void_list_node
;
23748 parm
= TREE_CHAIN (parm
),
23749 parmdecl
= DECL_CHAIN (parmdecl
))
23751 tree default_arg
= TREE_PURPOSE (parm
);
23753 vec
<tree
, va_gc
> *insts
;
23760 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
23761 /* This can happen for a friend declaration for a function
23762 already declared with default arguments. */
23766 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
23768 TREE_VALUE (parm
));
23769 if (parsed_arg
== error_mark_node
)
23774 TREE_PURPOSE (parm
) = parsed_arg
;
23776 /* Update any instantiations we've already created. */
23777 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
23778 vec_safe_iterate (insts
, ix
, ©
); ix
++)
23779 TREE_PURPOSE (copy
) = parsed_arg
;
23782 pop_defarg_context ();
23784 /* Make sure no default arg is missing. */
23785 check_default_args (fn
);
23787 /* Restore the state of local_variables_forbidden_p. */
23788 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
23790 /* Restore the queue. */
23791 pop_unparsed_function_queues (parser
);
23794 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23796 sizeof ... ( identifier )
23798 where the 'sizeof' token has already been consumed. */
23801 cp_parser_sizeof_pack (cp_parser
*parser
)
23803 /* Consume the `...'. */
23804 cp_lexer_consume_token (parser
->lexer
);
23805 maybe_warn_variadic_templates ();
23807 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
23809 cp_lexer_consume_token (parser
->lexer
);
23811 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
23812 "%<sizeof...%> argument must be surrounded by parentheses");
23814 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23815 tree name
= cp_parser_identifier (parser
);
23816 if (name
== error_mark_node
)
23817 return error_mark_node
;
23818 /* The name is not qualified. */
23819 parser
->scope
= NULL_TREE
;
23820 parser
->qualifying_scope
= NULL_TREE
;
23821 parser
->object_scope
= NULL_TREE
;
23822 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
23823 if (expr
== error_mark_node
)
23824 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
23826 if (TREE_CODE (expr
) == TYPE_DECL
)
23827 expr
= TREE_TYPE (expr
);
23828 else if (TREE_CODE (expr
) == CONST_DECL
)
23829 expr
= DECL_INITIAL (expr
);
23830 expr
= make_pack_expansion (expr
);
23833 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23838 /* Parse the operand of `sizeof' (or a similar operator). Returns
23839 either a TYPE or an expression, depending on the form of the
23840 input. The KEYWORD indicates which kind of expression we have
23844 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
23846 tree expr
= NULL_TREE
;
23847 const char *saved_message
;
23849 bool saved_integral_constant_expression_p
;
23850 bool saved_non_integral_constant_expression_p
;
23852 /* If it's a `...', then we are computing the length of a parameter
23854 if (keyword
== RID_SIZEOF
23855 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23856 return cp_parser_sizeof_pack (parser
);
23858 /* Types cannot be defined in a `sizeof' expression. Save away the
23860 saved_message
= parser
->type_definition_forbidden_message
;
23861 /* And create the new one. */
23862 tmp
= concat ("types may not be defined in %<",
23863 IDENTIFIER_POINTER (ridpointers
[keyword
]),
23864 "%> expressions", NULL
);
23865 parser
->type_definition_forbidden_message
= tmp
;
23867 /* The restrictions on constant-expressions do not apply inside
23868 sizeof expressions. */
23869 saved_integral_constant_expression_p
23870 = parser
->integral_constant_expression_p
;
23871 saved_non_integral_constant_expression_p
23872 = parser
->non_integral_constant_expression_p
;
23873 parser
->integral_constant_expression_p
= false;
23875 /* Do not actually evaluate the expression. */
23876 ++cp_unevaluated_operand
;
23877 ++c_inhibit_evaluation_warnings
;
23878 /* If it's a `(', then we might be looking at the type-id
23880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23882 tree type
= NULL_TREE
;
23883 bool compound_literal_p
;
23885 /* We can't be sure yet whether we're looking at a type-id or an
23887 cp_parser_parse_tentatively (parser
);
23888 /* Consume the `('. */
23889 cp_lexer_consume_token (parser
->lexer
);
23890 /* Note: as a GNU Extension, compound literals are considered
23891 postfix-expressions as they are in C99, so they are valid
23892 arguments to sizeof. See comment in cp_parser_cast_expression
23894 cp_lexer_save_tokens (parser
->lexer
);
23895 /* Skip tokens until the next token is a closing parenthesis.
23896 If we find the closing `)', and the next token is a `{', then
23897 we are looking at a compound-literal. */
23899 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
23900 /*consume_paren=*/true)
23901 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
23902 /* Roll back the tokens we skipped. */
23903 cp_lexer_rollback_tokens (parser
->lexer
);
23904 /* If we were looking at a compound-literal, simulate an error
23905 so that the call to cp_parser_parse_definitely below will
23907 if (compound_literal_p
)
23908 cp_parser_simulate_error (parser
);
23911 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
23912 parser
->in_type_id_in_expr_p
= true;
23913 /* Look for the type-id. */
23914 type
= cp_parser_type_id (parser
);
23915 /* Look for the closing `)'. */
23916 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23917 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
23920 /* If all went well, then we're done. */
23921 if (cp_parser_parse_definitely (parser
))
23923 cp_decl_specifier_seq decl_specs
;
23925 /* Build a trivial decl-specifier-seq. */
23926 clear_decl_specs (&decl_specs
);
23927 decl_specs
.type
= type
;
23929 /* Call grokdeclarator to figure out what type this is. */
23930 expr
= grokdeclarator (NULL
,
23934 /*attrlist=*/NULL
);
23938 /* If the type-id production did not work out, then we must be
23939 looking at the unary-expression production. */
23941 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
23942 /*cast_p=*/false, NULL
);
23944 /* Go back to evaluating expressions. */
23945 --cp_unevaluated_operand
;
23946 --c_inhibit_evaluation_warnings
;
23948 /* Free the message we created. */
23950 /* And restore the old one. */
23951 parser
->type_definition_forbidden_message
= saved_message
;
23952 parser
->integral_constant_expression_p
23953 = saved_integral_constant_expression_p
;
23954 parser
->non_integral_constant_expression_p
23955 = saved_non_integral_constant_expression_p
;
23960 /* If the current declaration has no declarator, return true. */
23963 cp_parser_declares_only_class_p (cp_parser
*parser
)
23965 /* If the next token is a `;' or a `,' then there is no
23967 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
23968 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
23971 /* Update the DECL_SPECS to reflect the storage class indicated by
23975 cp_parser_set_storage_class (cp_parser
*parser
,
23976 cp_decl_specifier_seq
*decl_specs
,
23980 cp_storage_class storage_class
;
23982 if (parser
->in_unbraced_linkage_specification_p
)
23984 error_at (token
->location
, "invalid use of %qD in linkage specification",
23985 ridpointers
[keyword
]);
23988 else if (decl_specs
->storage_class
!= sc_none
)
23990 decl_specs
->conflicting_specifiers_p
= true;
23994 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
23995 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
23996 && decl_specs
->gnu_thread_keyword_p
)
23998 pedwarn (decl_specs
->locations
[ds_thread
], 0,
23999 "%<__thread%> before %qD", ridpointers
[keyword
]);
24005 storage_class
= sc_auto
;
24008 storage_class
= sc_register
;
24011 storage_class
= sc_static
;
24014 storage_class
= sc_extern
;
24017 storage_class
= sc_mutable
;
24020 gcc_unreachable ();
24022 decl_specs
->storage_class
= storage_class
;
24023 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
24025 /* A storage class specifier cannot be applied alongside a typedef
24026 specifier. If there is a typedef specifier present then set
24027 conflicting_specifiers_p which will trigger an error later
24028 on in grokdeclarator. */
24029 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
24030 decl_specs
->conflicting_specifiers_p
= true;
24033 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24034 is true, the type is a class or enum definition. */
24037 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
24040 bool type_definition_p
)
24042 decl_specs
->any_specifiers_p
= true;
24044 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24045 (with, for example, in "typedef int wchar_t;") we remember that
24046 this is what happened. In system headers, we ignore these
24047 declarations so that G++ can work with system headers that are not
24049 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
24050 && !type_definition_p
24051 && (type_spec
== boolean_type_node
24052 || type_spec
== char16_type_node
24053 || type_spec
== char32_type_node
24054 || type_spec
== wchar_type_node
)
24055 && (decl_specs
->type
24056 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
24057 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
24058 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
24059 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
24061 decl_specs
->redefined_builtin_type
= type_spec
;
24062 set_and_check_decl_spec_loc (decl_specs
,
24063 ds_redefined_builtin_type_spec
,
24065 if (!decl_specs
->type
)
24067 decl_specs
->type
= type_spec
;
24068 decl_specs
->type_definition_p
= false;
24069 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
24072 else if (decl_specs
->type
)
24073 decl_specs
->multiple_types_p
= true;
24076 decl_specs
->type
= type_spec
;
24077 decl_specs
->type_definition_p
= type_definition_p
;
24078 decl_specs
->redefined_builtin_type
= NULL_TREE
;
24079 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
24083 /* True iff TOKEN is the GNU keyword __thread. */
24086 token_is__thread (cp_token
*token
)
24088 gcc_assert (token
->keyword
== RID_THREAD
);
24089 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
24092 /* Set the location for a declarator specifier and check if it is
24095 DECL_SPECS is the sequence of declarator specifiers onto which to
24098 DS is the single declarator specifier to set which location is to
24099 be set onto the existing sequence of declarators.
24101 LOCATION is the location for the declarator specifier to
24105 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
24106 cp_decl_spec ds
, cp_token
*token
)
24108 gcc_assert (ds
< ds_last
);
24110 if (decl_specs
== NULL
)
24113 source_location location
= token
->location
;
24115 if (decl_specs
->locations
[ds
] == 0)
24117 decl_specs
->locations
[ds
] = location
;
24118 if (ds
== ds_thread
)
24119 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
24125 if (decl_specs
->locations
[ds_long_long
] != 0)
24126 error_at (location
,
24127 "%<long long long%> is too long for GCC");
24130 decl_specs
->locations
[ds_long_long
] = location
;
24131 pedwarn_cxx98 (location
,
24133 "ISO C++ 1998 does not support %<long long%>");
24136 else if (ds
== ds_thread
)
24138 bool gnu
= token_is__thread (token
);
24139 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
24140 error_at (location
,
24141 "both %<__thread%> and %<thread_local%> specified");
24143 error_at (location
, "duplicate %qD", token
->u
.value
);
24147 static const char *const decl_spec_names
[] = {
24164 error_at (location
,
24165 "duplicate %qs", decl_spec_names
[ds
]);
24170 /* Return true iff the declarator specifier DS is present in the
24171 sequence of declarator specifiers DECL_SPECS. */
24174 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
24177 gcc_assert (ds
< ds_last
);
24179 if (decl_specs
== NULL
)
24182 return decl_specs
->locations
[ds
] != 0;
24185 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24186 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24189 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
24191 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
24194 /* Issue an error message indicating that TOKEN_DESC was expected.
24195 If KEYWORD is true, it indicated this function is called by
24196 cp_parser_require_keword and the required token can only be
24197 a indicated keyword. */
24200 cp_parser_required_error (cp_parser
*parser
,
24201 required_token token_desc
,
24204 switch (token_desc
)
24207 cp_parser_error (parser
, "expected %<new%>");
24210 cp_parser_error (parser
, "expected %<delete%>");
24213 cp_parser_error (parser
, "expected %<return%>");
24216 cp_parser_error (parser
, "expected %<while%>");
24219 cp_parser_error (parser
, "expected %<extern%>");
24221 case RT_STATIC_ASSERT
:
24222 cp_parser_error (parser
, "expected %<static_assert%>");
24225 cp_parser_error (parser
, "expected %<decltype%>");
24228 cp_parser_error (parser
, "expected %<operator%>");
24231 cp_parser_error (parser
, "expected %<class%>");
24234 cp_parser_error (parser
, "expected %<template%>");
24237 cp_parser_error (parser
, "expected %<namespace%>");
24240 cp_parser_error (parser
, "expected %<using%>");
24243 cp_parser_error (parser
, "expected %<asm%>");
24246 cp_parser_error (parser
, "expected %<try%>");
24249 cp_parser_error (parser
, "expected %<catch%>");
24252 cp_parser_error (parser
, "expected %<throw%>");
24255 cp_parser_error (parser
, "expected %<__label__%>");
24258 cp_parser_error (parser
, "expected %<@try%>");
24260 case RT_AT_SYNCHRONIZED
:
24261 cp_parser_error (parser
, "expected %<@synchronized%>");
24264 cp_parser_error (parser
, "expected %<@throw%>");
24266 case RT_TRANSACTION_ATOMIC
:
24267 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
24269 case RT_TRANSACTION_RELAXED
:
24270 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
24277 switch (token_desc
)
24280 cp_parser_error (parser
, "expected %<;%>");
24282 case RT_OPEN_PAREN
:
24283 cp_parser_error (parser
, "expected %<(%>");
24285 case RT_CLOSE_BRACE
:
24286 cp_parser_error (parser
, "expected %<}%>");
24288 case RT_OPEN_BRACE
:
24289 cp_parser_error (parser
, "expected %<{%>");
24291 case RT_CLOSE_SQUARE
:
24292 cp_parser_error (parser
, "expected %<]%>");
24294 case RT_OPEN_SQUARE
:
24295 cp_parser_error (parser
, "expected %<[%>");
24298 cp_parser_error (parser
, "expected %<,%>");
24301 cp_parser_error (parser
, "expected %<::%>");
24304 cp_parser_error (parser
, "expected %<<%>");
24307 cp_parser_error (parser
, "expected %<>%>");
24310 cp_parser_error (parser
, "expected %<=%>");
24313 cp_parser_error (parser
, "expected %<...%>");
24316 cp_parser_error (parser
, "expected %<*%>");
24319 cp_parser_error (parser
, "expected %<~%>");
24322 cp_parser_error (parser
, "expected %<:%>");
24324 case RT_COLON_SCOPE
:
24325 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24327 case RT_CLOSE_PAREN
:
24328 cp_parser_error (parser
, "expected %<)%>");
24330 case RT_COMMA_CLOSE_PAREN
:
24331 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24333 case RT_PRAGMA_EOL
:
24334 cp_parser_error (parser
, "expected end of line");
24337 cp_parser_error (parser
, "expected identifier");
24340 cp_parser_error (parser
, "expected selection-statement");
24342 case RT_INTERATION
:
24343 cp_parser_error (parser
, "expected iteration-statement");
24346 cp_parser_error (parser
, "expected jump-statement");
24349 cp_parser_error (parser
, "expected class-key");
24351 case RT_CLASS_TYPENAME_TEMPLATE
:
24352 cp_parser_error (parser
,
24353 "expected %<class%>, %<typename%>, or %<template%>");
24356 gcc_unreachable ();
24360 gcc_unreachable ();
24365 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24366 issue an error message indicating that TOKEN_DESC was expected.
24368 Returns the token consumed, if the token had the appropriate type.
24369 Otherwise, returns NULL. */
24372 cp_parser_require (cp_parser
* parser
,
24373 enum cpp_ttype type
,
24374 required_token token_desc
)
24376 if (cp_lexer_next_token_is (parser
->lexer
, type
))
24377 return cp_lexer_consume_token (parser
->lexer
);
24380 /* Output the MESSAGE -- unless we're parsing tentatively. */
24381 if (!cp_parser_simulate_error (parser
))
24382 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
24387 /* An error message is produced if the next token is not '>'.
24388 All further tokens are skipped until the desired token is
24389 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24392 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
24394 /* Current level of '< ... >'. */
24395 unsigned level
= 0;
24396 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24397 unsigned nesting_depth
= 0;
24399 /* Are we ready, yet? If not, issue error message. */
24400 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
24403 /* Skip tokens until the desired token is found. */
24406 /* Peek at the next token. */
24407 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24410 if (!nesting_depth
)
24415 if (cxx_dialect
== cxx98
)
24416 /* C++0x views the `>>' operator as two `>' tokens, but
24419 else if (!nesting_depth
&& level
-- == 0)
24421 /* We've hit a `>>' where the first `>' closes the
24422 template argument list, and the second `>' is
24423 spurious. Just consume the `>>' and stop; we've
24424 already produced at least one error. */
24425 cp_lexer_consume_token (parser
->lexer
);
24428 /* Fall through for C++0x, so we handle the second `>' in
24432 if (!nesting_depth
&& level
-- == 0)
24434 /* We've reached the token we want, consume it and stop. */
24435 cp_lexer_consume_token (parser
->lexer
);
24440 case CPP_OPEN_PAREN
:
24441 case CPP_OPEN_SQUARE
:
24445 case CPP_CLOSE_PAREN
:
24446 case CPP_CLOSE_SQUARE
:
24447 if (nesting_depth
-- == 0)
24452 case CPP_PRAGMA_EOL
:
24453 case CPP_SEMICOLON
:
24454 case CPP_OPEN_BRACE
:
24455 case CPP_CLOSE_BRACE
:
24456 /* The '>' was probably forgotten, don't look further. */
24463 /* Consume this token. */
24464 cp_lexer_consume_token (parser
->lexer
);
24468 /* If the next token is the indicated keyword, consume it. Otherwise,
24469 issue an error message indicating that TOKEN_DESC was expected.
24471 Returns the token consumed, if the token had the appropriate type.
24472 Otherwise, returns NULL. */
24475 cp_parser_require_keyword (cp_parser
* parser
,
24477 required_token token_desc
)
24479 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
24481 if (token
&& token
->keyword
!= keyword
)
24483 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
24490 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24491 function-definition. */
24494 cp_parser_token_starts_function_definition_p (cp_token
* token
)
24496 return (/* An ordinary function-body begins with an `{'. */
24497 token
->type
== CPP_OPEN_BRACE
24498 /* A ctor-initializer begins with a `:'. */
24499 || token
->type
== CPP_COLON
24500 /* A function-try-block begins with `try'. */
24501 || token
->keyword
== RID_TRY
24502 /* A function-transaction-block begins with `__transaction_atomic'
24503 or `__transaction_relaxed'. */
24504 || token
->keyword
== RID_TRANSACTION_ATOMIC
24505 || token
->keyword
== RID_TRANSACTION_RELAXED
24506 /* The named return value extension begins with `return'. */
24507 || token
->keyword
== RID_RETURN
);
24510 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24514 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
24518 token
= cp_lexer_peek_token (parser
->lexer
);
24519 return (token
->type
== CPP_OPEN_BRACE
24520 || (token
->type
== CPP_COLON
24521 && !parser
->colon_doesnt_start_class_def_p
));
24524 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24525 C++0x) ending a template-argument. */
24528 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
24532 token
= cp_lexer_peek_token (parser
->lexer
);
24533 return (token
->type
== CPP_COMMA
24534 || token
->type
== CPP_GREATER
24535 || token
->type
== CPP_ELLIPSIS
24536 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
24539 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24540 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24543 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
24548 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24549 if (token
->type
== CPP_LESS
)
24551 /* Check for the sequence `<::' in the original code. It would be lexed as
24552 `[:', where `[' is a digraph, and there is no whitespace before
24554 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
24557 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
24558 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
24564 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24565 or none_type otherwise. */
24567 static enum tag_types
24568 cp_parser_token_is_class_key (cp_token
* token
)
24570 switch (token
->keyword
)
24575 return record_type
;
24584 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24587 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
24589 if (type
== error_mark_node
)
24591 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
24593 if (permerror (input_location
, "%qs tag used in naming %q#T",
24594 class_key
== union_type
? "union"
24595 : class_key
== record_type
? "struct" : "class",
24597 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
24598 "%q#T was previously declared here", type
);
24602 /* Issue an error message if DECL is redeclared with different
24603 access than its original declaration [class.access.spec/3].
24604 This applies to nested classes and nested class templates.
24608 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
24610 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
24613 if ((TREE_PRIVATE (decl
)
24614 != (current_access_specifier
== access_private_node
))
24615 || (TREE_PROTECTED (decl
)
24616 != (current_access_specifier
== access_protected_node
)))
24617 error_at (location
, "%qD redeclared with different access", decl
);
24620 /* Look for the `template' keyword, as a syntactic disambiguator.
24621 Return TRUE iff it is present, in which case it will be
24625 cp_parser_optional_template_keyword (cp_parser
*parser
)
24627 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24629 /* In C++98 the `template' keyword can only be used within templates;
24630 outside templates the parser can always figure out what is a
24631 template and what is not. In C++11, per the resolution of DR 468,
24632 `template' is allowed in cases where it is not strictly necessary. */
24633 if (!processing_template_decl
24634 && pedantic
&& cxx_dialect
== cxx98
)
24636 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24637 pedwarn (token
->location
, OPT_Wpedantic
,
24638 "in C++98 %<template%> (as a disambiguator) is only "
24639 "allowed within templates");
24640 /* If this part of the token stream is rescanned, the same
24641 error message would be generated. So, we purge the token
24642 from the stream. */
24643 cp_lexer_purge_token (parser
->lexer
);
24648 /* Consume the `template' keyword. */
24649 cp_lexer_consume_token (parser
->lexer
);
24656 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24657 set PARSER->SCOPE, and perform other related actions. */
24660 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
24663 struct tree_check
*check_value
;
24664 deferred_access_check
*chk
;
24665 vec
<deferred_access_check
, va_gc
> *checks
;
24667 /* Get the stored value. */
24668 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
24669 /* Perform any access checks that were deferred. */
24670 checks
= check_value
->checks
;
24673 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
24674 perform_or_defer_access_check (chk
->binfo
,
24676 chk
->diag_decl
, tf_warning_or_error
);
24678 /* Set the scope from the stored value. */
24679 parser
->scope
= check_value
->value
;
24680 parser
->qualifying_scope
= check_value
->qualifying_scope
;
24681 parser
->object_scope
= NULL_TREE
;
24684 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24685 encounter the end of a block before what we were looking for. */
24688 cp_parser_cache_group (cp_parser
*parser
,
24689 enum cpp_ttype end
,
24694 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24696 /* Abort a parenthesized expression if we encounter a semicolon. */
24697 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
24698 && token
->type
== CPP_SEMICOLON
)
24700 /* If we've reached the end of the file, stop. */
24701 if (token
->type
== CPP_EOF
24702 || (end
!= CPP_PRAGMA_EOL
24703 && token
->type
== CPP_PRAGMA_EOL
))
24705 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
24706 /* We've hit the end of an enclosing block, so there's been some
24707 kind of syntax error. */
24710 /* Consume the token. */
24711 cp_lexer_consume_token (parser
->lexer
);
24712 /* See if it starts a new group. */
24713 if (token
->type
== CPP_OPEN_BRACE
)
24715 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
24716 /* In theory this should probably check end == '}', but
24717 cp_parser_save_member_function_body needs it to exit
24718 after either '}' or ')' when called with ')'. */
24722 else if (token
->type
== CPP_OPEN_PAREN
)
24724 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
24725 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
24728 else if (token
->type
== CPP_PRAGMA
)
24729 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
24730 else if (token
->type
== end
)
24735 /* Like above, for caching a default argument or NSDMI. Both of these are
24736 terminated by a non-nested comma, but it can be unclear whether or not a
24737 comma is nested in a template argument list unless we do more parsing.
24738 In order to handle this ambiguity, when we encounter a ',' after a '<'
24739 we try to parse what follows as a parameter-declaration-list (in the
24740 case of a default argument) or a member-declarator (in the case of an
24741 NSDMI). If that succeeds, then we stop caching. */
24744 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
24746 unsigned depth
= 0;
24747 int maybe_template_id
= 0;
24748 cp_token
*first_token
;
24750 tree default_argument
;
24752 /* Add tokens until we have processed the entire default
24753 argument. We add the range [first_token, token). */
24754 first_token
= cp_lexer_peek_token (parser
->lexer
);
24755 if (first_token
->type
== CPP_OPEN_BRACE
)
24757 /* For list-initialization, this is straightforward. */
24758 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
24759 token
= cp_lexer_peek_token (parser
->lexer
);
24765 /* Peek at the next token. */
24766 token
= cp_lexer_peek_token (parser
->lexer
);
24767 /* What we do depends on what token we have. */
24768 switch (token
->type
)
24770 /* In valid code, a default argument must be
24771 immediately followed by a `,' `)', or `...'. */
24773 if (depth
== 0 && maybe_template_id
)
24775 /* If we've seen a '<', we might be in a
24776 template-argument-list. Until Core issue 325 is
24777 resolved, we don't know how this situation ought
24778 to be handled, so try to DTRT. We check whether
24779 what comes after the comma is a valid parameter
24780 declaration list. If it is, then the comma ends
24781 the default argument; otherwise the default
24782 argument continues. */
24783 bool error
= false;
24785 /* Set ITALP so cp_parser_parameter_declaration_list
24786 doesn't decide to commit to this parse. */
24787 bool saved_italp
= parser
->in_template_argument_list_p
;
24788 parser
->in_template_argument_list_p
= true;
24790 cp_parser_parse_tentatively (parser
);
24791 cp_lexer_consume_token (parser
->lexer
);
24795 int ctor_dtor_or_conv_p
;
24796 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24797 &ctor_dtor_or_conv_p
,
24798 /*parenthesized_p=*/NULL
,
24799 /*member_p=*/true);
24803 begin_scope (sk_function_parms
, NULL_TREE
);
24804 cp_parser_parameter_declaration_list (parser
, &error
);
24805 pop_bindings_and_leave_scope ();
24807 if (!cp_parser_error_occurred (parser
) && !error
)
24809 cp_parser_abort_tentative_parse (parser
);
24811 parser
->in_template_argument_list_p
= saved_italp
;
24814 case CPP_CLOSE_PAREN
:
24816 /* If we run into a non-nested `;', `}', or `]',
24817 then the code is invalid -- but the default
24818 argument is certainly over. */
24819 case CPP_SEMICOLON
:
24820 case CPP_CLOSE_BRACE
:
24821 case CPP_CLOSE_SQUARE
:
24823 /* Handle correctly int n = sizeof ... ( p ); */
24824 && token
->type
!= CPP_ELLIPSIS
)
24826 /* Update DEPTH, if necessary. */
24827 else if (token
->type
== CPP_CLOSE_PAREN
24828 || token
->type
== CPP_CLOSE_BRACE
24829 || token
->type
== CPP_CLOSE_SQUARE
)
24833 case CPP_OPEN_PAREN
:
24834 case CPP_OPEN_SQUARE
:
24835 case CPP_OPEN_BRACE
:
24841 /* This might be the comparison operator, or it might
24842 start a template argument list. */
24843 ++maybe_template_id
;
24847 if (cxx_dialect
== cxx98
)
24849 /* Fall through for C++0x, which treats the `>>'
24850 operator like two `>' tokens in certain
24856 /* This might be an operator, or it might close a
24857 template argument list. But if a previous '<'
24858 started a template argument list, this will have
24859 closed it, so we can't be in one anymore. */
24860 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
24861 if (maybe_template_id
< 0)
24862 maybe_template_id
= 0;
24866 /* If we run out of tokens, issue an error message. */
24868 case CPP_PRAGMA_EOL
:
24869 error_at (token
->location
, "file ends in default argument");
24875 /* In these cases, we should look for template-ids.
24876 For example, if the default argument is
24877 `X<int, double>()', we need to do name lookup to
24878 figure out whether or not `X' is a template; if
24879 so, the `,' does not end the default argument.
24881 That is not yet done. */
24888 /* If we've reached the end, stop. */
24892 /* Add the token to the token block. */
24893 token
= cp_lexer_consume_token (parser
->lexer
);
24896 /* Create a DEFAULT_ARG to represent the unparsed default
24898 default_argument
= make_node (DEFAULT_ARG
);
24899 DEFARG_TOKENS (default_argument
)
24900 = cp_token_cache_new (first_token
, token
);
24901 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
24903 return default_argument
;
24906 /* Begin parsing tentatively. We always save tokens while parsing
24907 tentatively so that if the tentative parsing fails we can restore the
24911 cp_parser_parse_tentatively (cp_parser
* parser
)
24913 /* Enter a new parsing context. */
24914 parser
->context
= cp_parser_context_new (parser
->context
);
24915 /* Begin saving tokens. */
24916 cp_lexer_save_tokens (parser
->lexer
);
24917 /* In order to avoid repetitive access control error messages,
24918 access checks are queued up until we are no longer parsing
24920 push_deferring_access_checks (dk_deferred
);
24923 /* Commit to the currently active tentative parse. */
24926 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
24928 cp_parser_context
*context
;
24931 /* Mark all of the levels as committed. */
24932 lexer
= parser
->lexer
;
24933 for (context
= parser
->context
; context
->next
; context
= context
->next
)
24935 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24937 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24938 while (!cp_lexer_saving_tokens (lexer
))
24939 lexer
= lexer
->next
;
24940 cp_lexer_commit_tokens (lexer
);
24944 /* Commit to the topmost currently active tentative parse.
24946 Note that this function shouldn't be called when there are
24947 irreversible side-effects while in a tentative state. For
24948 example, we shouldn't create a permanent entry in the symbol
24949 table, or issue an error message that might not apply if the
24950 tentative parse is aborted. */
24953 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
24955 cp_parser_context
*context
= parser
->context
;
24956 cp_lexer
*lexer
= parser
->lexer
;
24960 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24962 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24964 while (!cp_lexer_saving_tokens (lexer
))
24965 lexer
= lexer
->next
;
24966 cp_lexer_commit_tokens (lexer
);
24970 /* Abort the currently active tentative parse. All consumed tokens
24971 will be rolled back, and no diagnostics will be issued. */
24974 cp_parser_abort_tentative_parse (cp_parser
* parser
)
24976 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
24977 || errorcount
> 0);
24978 cp_parser_simulate_error (parser
);
24979 /* Now, pretend that we want to see if the construct was
24980 successfully parsed. */
24981 cp_parser_parse_definitely (parser
);
24984 /* Stop parsing tentatively. If a parse error has occurred, restore the
24985 token stream. Otherwise, commit to the tokens we have consumed.
24986 Returns true if no error occurred; false otherwise. */
24989 cp_parser_parse_definitely (cp_parser
* parser
)
24991 bool error_occurred
;
24992 cp_parser_context
*context
;
24994 /* Remember whether or not an error occurred, since we are about to
24995 destroy that information. */
24996 error_occurred
= cp_parser_error_occurred (parser
);
24997 /* Remove the topmost context from the stack. */
24998 context
= parser
->context
;
24999 parser
->context
= context
->next
;
25000 /* If no parse errors occurred, commit to the tentative parse. */
25001 if (!error_occurred
)
25003 /* Commit to the tokens read tentatively, unless that was
25005 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
25006 cp_lexer_commit_tokens (parser
->lexer
);
25008 pop_to_parent_deferring_access_checks ();
25010 /* Otherwise, if errors occurred, roll back our state so that things
25011 are just as they were before we began the tentative parse. */
25014 cp_lexer_rollback_tokens (parser
->lexer
);
25015 pop_deferring_access_checks ();
25017 /* Add the context to the front of the free list. */
25018 context
->next
= cp_parser_context_free_list
;
25019 cp_parser_context_free_list
= context
;
25021 return !error_occurred
;
25024 /* Returns true if we are parsing tentatively and are not committed to
25025 this tentative parse. */
25028 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
25030 return (cp_parser_parsing_tentatively (parser
)
25031 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
25034 /* Returns nonzero iff an error has occurred during the most recent
25035 tentative parse. */
25038 cp_parser_error_occurred (cp_parser
* parser
)
25040 return (cp_parser_parsing_tentatively (parser
)
25041 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
25044 /* Returns nonzero if GNU extensions are allowed. */
25047 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
25049 return parser
->allow_gnu_extensions_p
;
25052 /* Objective-C++ Productions */
25055 /* Parse an Objective-C expression, which feeds into a primary-expression
25059 objc-message-expression
25060 objc-string-literal
25061 objc-encode-expression
25062 objc-protocol-expression
25063 objc-selector-expression
25065 Returns a tree representation of the expression. */
25068 cp_parser_objc_expression (cp_parser
* parser
)
25070 /* Try to figure out what kind of declaration is present. */
25071 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25075 case CPP_OPEN_SQUARE
:
25076 return cp_parser_objc_message_expression (parser
);
25078 case CPP_OBJC_STRING
:
25079 kwd
= cp_lexer_consume_token (parser
->lexer
);
25080 return objc_build_string_object (kwd
->u
.value
);
25083 switch (kwd
->keyword
)
25085 case RID_AT_ENCODE
:
25086 return cp_parser_objc_encode_expression (parser
);
25088 case RID_AT_PROTOCOL
:
25089 return cp_parser_objc_protocol_expression (parser
);
25091 case RID_AT_SELECTOR
:
25092 return cp_parser_objc_selector_expression (parser
);
25098 error_at (kwd
->location
,
25099 "misplaced %<@%D%> Objective-C++ construct",
25101 cp_parser_skip_to_end_of_block_or_statement (parser
);
25104 return error_mark_node
;
25107 /* Parse an Objective-C message expression.
25109 objc-message-expression:
25110 [ objc-message-receiver objc-message-args ]
25112 Returns a representation of an Objective-C message. */
25115 cp_parser_objc_message_expression (cp_parser
* parser
)
25117 tree receiver
, messageargs
;
25119 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
25120 receiver
= cp_parser_objc_message_receiver (parser
);
25121 messageargs
= cp_parser_objc_message_args (parser
);
25122 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25124 return objc_build_message_expr (receiver
, messageargs
);
25127 /* Parse an objc-message-receiver.
25129 objc-message-receiver:
25131 simple-type-specifier
25133 Returns a representation of the type or expression. */
25136 cp_parser_objc_message_receiver (cp_parser
* parser
)
25140 /* An Objective-C message receiver may be either (1) a type
25141 or (2) an expression. */
25142 cp_parser_parse_tentatively (parser
);
25143 rcv
= cp_parser_expression (parser
, false, NULL
);
25145 if (cp_parser_parse_definitely (parser
))
25148 rcv
= cp_parser_simple_type_specifier (parser
,
25149 /*decl_specs=*/NULL
,
25150 CP_PARSER_FLAGS_NONE
);
25152 return objc_get_class_reference (rcv
);
25155 /* Parse the arguments and selectors comprising an Objective-C message.
25160 objc-selector-args , objc-comma-args
25162 objc-selector-args:
25163 objc-selector [opt] : assignment-expression
25164 objc-selector-args objc-selector [opt] : assignment-expression
25167 assignment-expression
25168 objc-comma-args , assignment-expression
25170 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25171 selector arguments and TREE_VALUE containing a list of comma
25175 cp_parser_objc_message_args (cp_parser
* parser
)
25177 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
25178 bool maybe_unary_selector_p
= true;
25179 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25181 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25183 tree selector
= NULL_TREE
, arg
;
25185 if (token
->type
!= CPP_COLON
)
25186 selector
= cp_parser_objc_selector (parser
);
25188 /* Detect if we have a unary selector. */
25189 if (maybe_unary_selector_p
25190 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25191 return build_tree_list (selector
, NULL_TREE
);
25193 maybe_unary_selector_p
= false;
25194 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25195 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
25198 = chainon (sel_args
,
25199 build_tree_list (selector
, arg
));
25201 token
= cp_lexer_peek_token (parser
->lexer
);
25204 /* Handle non-selector arguments, if any. */
25205 while (token
->type
== CPP_COMMA
)
25209 cp_lexer_consume_token (parser
->lexer
);
25210 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
25213 = chainon (addl_args
,
25214 build_tree_list (NULL_TREE
, arg
));
25216 token
= cp_lexer_peek_token (parser
->lexer
);
25219 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
25221 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
25222 return build_tree_list (error_mark_node
, error_mark_node
);
25225 return build_tree_list (sel_args
, addl_args
);
25228 /* Parse an Objective-C encode expression.
25230 objc-encode-expression:
25231 @encode objc-typename
25233 Returns an encoded representation of the type argument. */
25236 cp_parser_objc_encode_expression (cp_parser
* parser
)
25241 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
25242 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25243 token
= cp_lexer_peek_token (parser
->lexer
);
25244 type
= complete_type (cp_parser_type_id (parser
));
25245 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25249 error_at (token
->location
,
25250 "%<@encode%> must specify a type as an argument");
25251 return error_mark_node
;
25254 /* This happens if we find @encode(T) (where T is a template
25255 typename or something dependent on a template typename) when
25256 parsing a template. In that case, we can't compile it
25257 immediately, but we rather create an AT_ENCODE_EXPR which will
25258 need to be instantiated when the template is used.
25260 if (dependent_type_p (type
))
25262 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
25263 TREE_READONLY (value
) = 1;
25267 return objc_build_encode_expr (type
);
25270 /* Parse an Objective-C @defs expression. */
25273 cp_parser_objc_defs_expression (cp_parser
*parser
)
25277 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
25278 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25279 name
= cp_parser_identifier (parser
);
25280 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25282 return objc_get_class_ivars (name
);
25285 /* Parse an Objective-C protocol expression.
25287 objc-protocol-expression:
25288 @protocol ( identifier )
25290 Returns a representation of the protocol expression. */
25293 cp_parser_objc_protocol_expression (cp_parser
* parser
)
25297 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25298 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25299 proto
= cp_parser_identifier (parser
);
25300 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25302 return objc_build_protocol_expr (proto
);
25305 /* Parse an Objective-C selector expression.
25307 objc-selector-expression:
25308 @selector ( objc-method-signature )
25310 objc-method-signature:
25316 objc-selector-seq objc-selector :
25318 Returns a representation of the method selector. */
25321 cp_parser_objc_selector_expression (cp_parser
* parser
)
25323 tree sel_seq
= NULL_TREE
;
25324 bool maybe_unary_selector_p
= true;
25326 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25328 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
25329 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25330 token
= cp_lexer_peek_token (parser
->lexer
);
25332 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
25333 || token
->type
== CPP_SCOPE
)
25335 tree selector
= NULL_TREE
;
25337 if (token
->type
!= CPP_COLON
25338 || token
->type
== CPP_SCOPE
)
25339 selector
= cp_parser_objc_selector (parser
);
25341 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
25342 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
25344 /* Detect if we have a unary selector. */
25345 if (maybe_unary_selector_p
)
25347 sel_seq
= selector
;
25348 goto finish_selector
;
25352 cp_parser_error (parser
, "expected %<:%>");
25355 maybe_unary_selector_p
= false;
25356 token
= cp_lexer_consume_token (parser
->lexer
);
25358 if (token
->type
== CPP_SCOPE
)
25361 = chainon (sel_seq
,
25362 build_tree_list (selector
, NULL_TREE
));
25364 = chainon (sel_seq
,
25365 build_tree_list (NULL_TREE
, NULL_TREE
));
25369 = chainon (sel_seq
,
25370 build_tree_list (selector
, NULL_TREE
));
25372 token
= cp_lexer_peek_token (parser
->lexer
);
25376 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25378 return objc_build_selector_expr (loc
, sel_seq
);
25381 /* Parse a list of identifiers.
25383 objc-identifier-list:
25385 objc-identifier-list , identifier
25387 Returns a TREE_LIST of identifier nodes. */
25390 cp_parser_objc_identifier_list (cp_parser
* parser
)
25396 identifier
= cp_parser_identifier (parser
);
25397 if (identifier
== error_mark_node
)
25398 return error_mark_node
;
25400 list
= build_tree_list (NULL_TREE
, identifier
);
25401 sep
= cp_lexer_peek_token (parser
->lexer
);
25403 while (sep
->type
== CPP_COMMA
)
25405 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25406 identifier
= cp_parser_identifier (parser
);
25407 if (identifier
== error_mark_node
)
25410 list
= chainon (list
, build_tree_list (NULL_TREE
,
25412 sep
= cp_lexer_peek_token (parser
->lexer
);
25418 /* Parse an Objective-C alias declaration.
25420 objc-alias-declaration:
25421 @compatibility_alias identifier identifier ;
25423 This function registers the alias mapping with the Objective-C front end.
25424 It returns nothing. */
25427 cp_parser_objc_alias_declaration (cp_parser
* parser
)
25431 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
25432 alias
= cp_parser_identifier (parser
);
25433 orig
= cp_parser_identifier (parser
);
25434 objc_declare_alias (alias
, orig
);
25435 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25438 /* Parse an Objective-C class forward-declaration.
25440 objc-class-declaration:
25441 @class objc-identifier-list ;
25443 The function registers the forward declarations with the Objective-C
25444 front end. It returns nothing. */
25447 cp_parser_objc_class_declaration (cp_parser
* parser
)
25449 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
25454 id
= cp_parser_identifier (parser
);
25455 if (id
== error_mark_node
)
25458 objc_declare_class (id
);
25460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25461 cp_lexer_consume_token (parser
->lexer
);
25465 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25468 /* Parse a list of Objective-C protocol references.
25470 objc-protocol-refs-opt:
25471 objc-protocol-refs [opt]
25473 objc-protocol-refs:
25474 < objc-identifier-list >
25476 Returns a TREE_LIST of identifiers, if any. */
25479 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
25481 tree protorefs
= NULL_TREE
;
25483 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
25485 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
25486 protorefs
= cp_parser_objc_identifier_list (parser
);
25487 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
25493 /* Parse a Objective-C visibility specification. */
25496 cp_parser_objc_visibility_spec (cp_parser
* parser
)
25498 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
25500 switch (vis
->keyword
)
25502 case RID_AT_PRIVATE
:
25503 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
25505 case RID_AT_PROTECTED
:
25506 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
25508 case RID_AT_PUBLIC
:
25509 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
25511 case RID_AT_PACKAGE
:
25512 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
25518 /* Eat '@private'/'@protected'/'@public'. */
25519 cp_lexer_consume_token (parser
->lexer
);
25522 /* Parse an Objective-C method type. Return 'true' if it is a class
25523 (+) method, and 'false' if it is an instance (-) method. */
25526 cp_parser_objc_method_type (cp_parser
* parser
)
25528 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
25534 /* Parse an Objective-C protocol qualifier. */
25537 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
25539 tree quals
= NULL_TREE
, node
;
25540 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25542 node
= token
->u
.value
;
25544 while (node
&& identifier_p (node
)
25545 && (node
== ridpointers
[(int) RID_IN
]
25546 || node
== ridpointers
[(int) RID_OUT
]
25547 || node
== ridpointers
[(int) RID_INOUT
]
25548 || node
== ridpointers
[(int) RID_BYCOPY
]
25549 || node
== ridpointers
[(int) RID_BYREF
]
25550 || node
== ridpointers
[(int) RID_ONEWAY
]))
25552 quals
= tree_cons (NULL_TREE
, node
, quals
);
25553 cp_lexer_consume_token (parser
->lexer
);
25554 token
= cp_lexer_peek_token (parser
->lexer
);
25555 node
= token
->u
.value
;
25561 /* Parse an Objective-C typename. */
25564 cp_parser_objc_typename (cp_parser
* parser
)
25566 tree type_name
= NULL_TREE
;
25568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25570 tree proto_quals
, cp_type
= NULL_TREE
;
25572 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
25573 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
25575 /* An ObjC type name may consist of just protocol qualifiers, in which
25576 case the type shall default to 'id'. */
25577 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
25579 cp_type
= cp_parser_type_id (parser
);
25581 /* If the type could not be parsed, an error has already
25582 been produced. For error recovery, behave as if it had
25583 not been specified, which will use the default type
25585 if (cp_type
== error_mark_node
)
25587 cp_type
= NULL_TREE
;
25588 /* We need to skip to the closing parenthesis as
25589 cp_parser_type_id() does not seem to do it for
25591 cp_parser_skip_to_closing_parenthesis (parser
,
25592 /*recovering=*/true,
25593 /*or_comma=*/false,
25594 /*consume_paren=*/false);
25598 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25599 type_name
= build_tree_list (proto_quals
, cp_type
);
25605 /* Check to see if TYPE refers to an Objective-C selector name. */
25608 cp_parser_objc_selector_p (enum cpp_ttype type
)
25610 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
25611 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
25612 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
25613 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
25614 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
25617 /* Parse an Objective-C selector. */
25620 cp_parser_objc_selector (cp_parser
* parser
)
25622 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
25624 if (!cp_parser_objc_selector_p (token
->type
))
25626 error_at (token
->location
, "invalid Objective-C++ selector name");
25627 return error_mark_node
;
25630 /* C++ operator names are allowed to appear in ObjC selectors. */
25631 switch (token
->type
)
25633 case CPP_AND_AND
: return get_identifier ("and");
25634 case CPP_AND_EQ
: return get_identifier ("and_eq");
25635 case CPP_AND
: return get_identifier ("bitand");
25636 case CPP_OR
: return get_identifier ("bitor");
25637 case CPP_COMPL
: return get_identifier ("compl");
25638 case CPP_NOT
: return get_identifier ("not");
25639 case CPP_NOT_EQ
: return get_identifier ("not_eq");
25640 case CPP_OR_OR
: return get_identifier ("or");
25641 case CPP_OR_EQ
: return get_identifier ("or_eq");
25642 case CPP_XOR
: return get_identifier ("xor");
25643 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
25644 default: return token
->u
.value
;
25648 /* Parse an Objective-C params list. */
25651 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
25653 tree params
= NULL_TREE
;
25654 bool maybe_unary_selector_p
= true;
25655 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25657 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25659 tree selector
= NULL_TREE
, type_name
, identifier
;
25660 tree parm_attr
= NULL_TREE
;
25662 if (token
->keyword
== RID_ATTRIBUTE
)
25665 if (token
->type
!= CPP_COLON
)
25666 selector
= cp_parser_objc_selector (parser
);
25668 /* Detect if we have a unary selector. */
25669 if (maybe_unary_selector_p
25670 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25672 params
= selector
; /* Might be followed by attributes. */
25676 maybe_unary_selector_p
= false;
25677 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
25679 /* Something went quite wrong. There should be a colon
25680 here, but there is not. Stop parsing parameters. */
25683 type_name
= cp_parser_objc_typename (parser
);
25684 /* New ObjC allows attributes on parameters too. */
25685 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
25686 parm_attr
= cp_parser_attributes_opt (parser
);
25687 identifier
= cp_parser_identifier (parser
);
25691 objc_build_keyword_decl (selector
,
25696 token
= cp_lexer_peek_token (parser
->lexer
);
25699 if (params
== NULL_TREE
)
25701 cp_parser_error (parser
, "objective-c++ method declaration is expected");
25702 return error_mark_node
;
25705 /* We allow tail attributes for the method. */
25706 if (token
->keyword
== RID_ATTRIBUTE
)
25708 *attributes
= cp_parser_attributes_opt (parser
);
25709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25710 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25712 cp_parser_error (parser
,
25713 "method attributes must be specified at the end");
25714 return error_mark_node
;
25717 if (params
== NULL_TREE
)
25719 cp_parser_error (parser
, "objective-c++ method declaration is expected");
25720 return error_mark_node
;
25725 /* Parse the non-keyword Objective-C params. */
25728 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
25731 tree params
= make_node (TREE_LIST
);
25732 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25733 *ellipsisp
= false; /* Initially, assume no ellipsis. */
25735 while (token
->type
== CPP_COMMA
)
25737 cp_parameter_declarator
*parmdecl
;
25740 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25741 token
= cp_lexer_peek_token (parser
->lexer
);
25743 if (token
->type
== CPP_ELLIPSIS
)
25745 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
25747 token
= cp_lexer_peek_token (parser
->lexer
);
25751 /* TODO: parse attributes for tail parameters. */
25752 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
25753 parm
= grokdeclarator (parmdecl
->declarator
,
25754 &parmdecl
->decl_specifiers
,
25755 PARM
, /*initialized=*/0,
25756 /*attrlist=*/NULL
);
25758 chainon (params
, build_tree_list (NULL_TREE
, parm
));
25759 token
= cp_lexer_peek_token (parser
->lexer
);
25762 /* We allow tail attributes for the method. */
25763 if (token
->keyword
== RID_ATTRIBUTE
)
25765 if (*attributes
== NULL_TREE
)
25767 *attributes
= cp_parser_attributes_opt (parser
);
25768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25769 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25773 /* We have an error, but parse the attributes, so that we can
25775 *attributes
= cp_parser_attributes_opt (parser
);
25777 cp_parser_error (parser
,
25778 "method attributes must be specified at the end");
25779 return error_mark_node
;
25785 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25788 cp_parser_objc_interstitial_code (cp_parser
* parser
)
25790 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25792 /* If the next token is `extern' and the following token is a string
25793 literal, then we have a linkage specification. */
25794 if (token
->keyword
== RID_EXTERN
25795 && cp_parser_is_pure_string_literal
25796 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
25797 cp_parser_linkage_specification (parser
);
25798 /* Handle #pragma, if any. */
25799 else if (token
->type
== CPP_PRAGMA
)
25800 cp_parser_pragma (parser
, pragma_objc_icode
);
25801 /* Allow stray semicolons. */
25802 else if (token
->type
== CPP_SEMICOLON
)
25803 cp_lexer_consume_token (parser
->lexer
);
25804 /* Mark methods as optional or required, when building protocols. */
25805 else if (token
->keyword
== RID_AT_OPTIONAL
)
25807 cp_lexer_consume_token (parser
->lexer
);
25808 objc_set_method_opt (true);
25810 else if (token
->keyword
== RID_AT_REQUIRED
)
25812 cp_lexer_consume_token (parser
->lexer
);
25813 objc_set_method_opt (false);
25815 else if (token
->keyword
== RID_NAMESPACE
)
25816 cp_parser_namespace_definition (parser
);
25817 /* Other stray characters must generate errors. */
25818 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
25820 cp_lexer_consume_token (parser
->lexer
);
25821 error ("stray %qs between Objective-C++ methods",
25822 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
25824 /* Finally, try to parse a block-declaration, or a function-definition. */
25826 cp_parser_block_declaration (parser
, /*statement_p=*/false);
25829 /* Parse a method signature. */
25832 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
25834 tree rettype
, kwdparms
, optparms
;
25835 bool ellipsis
= false;
25836 bool is_class_method
;
25838 is_class_method
= cp_parser_objc_method_type (parser
);
25839 rettype
= cp_parser_objc_typename (parser
);
25840 *attributes
= NULL_TREE
;
25841 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
25842 if (kwdparms
== error_mark_node
)
25843 return error_mark_node
;
25844 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
25845 if (optparms
== error_mark_node
)
25846 return error_mark_node
;
25848 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
25852 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
25855 cp_lexer_save_tokens (parser
->lexer
);
25856 tattr
= cp_parser_attributes_opt (parser
);
25857 gcc_assert (tattr
) ;
25859 /* If the attributes are followed by a method introducer, this is not allowed.
25860 Dump the attributes and flag the situation. */
25861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
25862 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
25865 /* Otherwise, the attributes introduce some interstitial code, possibly so
25866 rewind to allow that check. */
25867 cp_lexer_rollback_tokens (parser
->lexer
);
25871 /* Parse an Objective-C method prototype list. */
25874 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
25876 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25878 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25880 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
25882 tree attributes
, sig
;
25883 bool is_class_method
;
25884 if (token
->type
== CPP_PLUS
)
25885 is_class_method
= true;
25887 is_class_method
= false;
25888 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
25889 if (sig
== error_mark_node
)
25891 cp_parser_skip_to_end_of_block_or_statement (parser
);
25892 token
= cp_lexer_peek_token (parser
->lexer
);
25895 objc_add_method_declaration (is_class_method
, sig
, attributes
);
25896 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25898 else if (token
->keyword
== RID_AT_PROPERTY
)
25899 cp_parser_objc_at_property_declaration (parser
);
25900 else if (token
->keyword
== RID_ATTRIBUTE
25901 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25902 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25904 "prefix attributes are ignored for methods");
25906 /* Allow for interspersed non-ObjC++ code. */
25907 cp_parser_objc_interstitial_code (parser
);
25909 token
= cp_lexer_peek_token (parser
->lexer
);
25912 if (token
->type
!= CPP_EOF
)
25913 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25915 cp_parser_error (parser
, "expected %<@end%>");
25917 objc_finish_interface ();
25920 /* Parse an Objective-C method definition list. */
25923 cp_parser_objc_method_definition_list (cp_parser
* parser
)
25925 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25927 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25931 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
25934 tree sig
, attribute
;
25935 bool is_class_method
;
25936 if (token
->type
== CPP_PLUS
)
25937 is_class_method
= true;
25939 is_class_method
= false;
25940 push_deferring_access_checks (dk_deferred
);
25941 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
25942 if (sig
== error_mark_node
)
25944 cp_parser_skip_to_end_of_block_or_statement (parser
);
25945 token
= cp_lexer_peek_token (parser
->lexer
);
25948 objc_start_method_definition (is_class_method
, sig
, attribute
,
25951 /* For historical reasons, we accept an optional semicolon. */
25952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25953 cp_lexer_consume_token (parser
->lexer
);
25955 ptk
= cp_lexer_peek_token (parser
->lexer
);
25956 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
25957 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
25959 perform_deferred_access_checks (tf_warning_or_error
);
25960 stop_deferring_access_checks ();
25961 meth
= cp_parser_function_definition_after_declarator (parser
,
25963 pop_deferring_access_checks ();
25964 objc_finish_method_definition (meth
);
25967 /* The following case will be removed once @synthesize is
25968 completely implemented. */
25969 else if (token
->keyword
== RID_AT_PROPERTY
)
25970 cp_parser_objc_at_property_declaration (parser
);
25971 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
25972 cp_parser_objc_at_synthesize_declaration (parser
);
25973 else if (token
->keyword
== RID_AT_DYNAMIC
)
25974 cp_parser_objc_at_dynamic_declaration (parser
);
25975 else if (token
->keyword
== RID_ATTRIBUTE
25976 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25977 warning_at (token
->location
, OPT_Wattributes
,
25978 "prefix attributes are ignored for methods");
25980 /* Allow for interspersed non-ObjC++ code. */
25981 cp_parser_objc_interstitial_code (parser
);
25983 token
= cp_lexer_peek_token (parser
->lexer
);
25986 if (token
->type
!= CPP_EOF
)
25987 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25989 cp_parser_error (parser
, "expected %<@end%>");
25991 objc_finish_implementation ();
25994 /* Parse Objective-C ivars. */
25997 cp_parser_objc_class_ivars (cp_parser
* parser
)
25999 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26001 if (token
->type
!= CPP_OPEN_BRACE
)
26002 return; /* No ivars specified. */
26004 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
26005 token
= cp_lexer_peek_token (parser
->lexer
);
26007 while (token
->type
!= CPP_CLOSE_BRACE
26008 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26010 cp_decl_specifier_seq declspecs
;
26011 int decl_class_or_enum_p
;
26012 tree prefix_attributes
;
26014 cp_parser_objc_visibility_spec (parser
);
26016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26019 cp_parser_decl_specifier_seq (parser
,
26020 CP_PARSER_FLAGS_OPTIONAL
,
26022 &decl_class_or_enum_p
);
26024 /* auto, register, static, extern, mutable. */
26025 if (declspecs
.storage_class
!= sc_none
)
26027 cp_parser_error (parser
, "invalid type for instance variable");
26028 declspecs
.storage_class
= sc_none
;
26031 /* thread_local. */
26032 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26034 cp_parser_error (parser
, "invalid type for instance variable");
26035 declspecs
.locations
[ds_thread
] = 0;
26039 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26041 cp_parser_error (parser
, "invalid type for instance variable");
26042 declspecs
.locations
[ds_typedef
] = 0;
26045 prefix_attributes
= declspecs
.attributes
;
26046 declspecs
.attributes
= NULL_TREE
;
26048 /* Keep going until we hit the `;' at the end of the
26050 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26052 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
26053 cp_declarator
*declarator
= NULL
;
26054 int ctor_dtor_or_conv_p
;
26056 /* Check for a (possibly unnamed) bitfield declaration. */
26057 token
= cp_lexer_peek_token (parser
->lexer
);
26058 if (token
->type
== CPP_COLON
)
26061 if (token
->type
== CPP_NAME
26062 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
26065 /* Get the name of the bitfield. */
26066 declarator
= make_id_declarator (NULL_TREE
,
26067 cp_parser_identifier (parser
),
26071 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26072 /* Get the width of the bitfield. */
26074 = cp_parser_constant_expression (parser
,
26075 /*allow_non_constant=*/false,
26080 /* Parse the declarator. */
26082 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26083 &ctor_dtor_or_conv_p
,
26084 /*parenthesized_p=*/NULL
,
26085 /*member_p=*/false);
26088 /* Look for attributes that apply to the ivar. */
26089 attributes
= cp_parser_attributes_opt (parser
);
26090 /* Remember which attributes are prefix attributes and
26092 first_attribute
= attributes
;
26093 /* Combine the attributes. */
26094 attributes
= chainon (prefix_attributes
, attributes
);
26097 /* Create the bitfield declaration. */
26098 decl
= grokbitfield (declarator
, &declspecs
,
26102 decl
= grokfield (declarator
, &declspecs
,
26103 NULL_TREE
, /*init_const_expr_p=*/false,
26104 NULL_TREE
, attributes
);
26106 /* Add the instance variable. */
26107 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
26108 objc_add_instance_variable (decl
);
26110 /* Reset PREFIX_ATTRIBUTES. */
26111 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26112 attributes
= TREE_CHAIN (attributes
);
26114 TREE_CHAIN (attributes
) = NULL_TREE
;
26116 token
= cp_lexer_peek_token (parser
->lexer
);
26118 if (token
->type
== CPP_COMMA
)
26120 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26126 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26127 token
= cp_lexer_peek_token (parser
->lexer
);
26130 if (token
->keyword
== RID_AT_END
)
26131 cp_parser_error (parser
, "expected %<}%>");
26133 /* Do not consume the RID_AT_END, so it will be read again as terminating
26134 the @interface of @implementation. */
26135 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26136 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
26138 /* For historical reasons, we accept an optional semicolon. */
26139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26140 cp_lexer_consume_token (parser
->lexer
);
26143 /* Parse an Objective-C protocol declaration. */
26146 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
26148 tree proto
, protorefs
;
26151 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
26152 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
26154 tok
= cp_lexer_peek_token (parser
->lexer
);
26155 error_at (tok
->location
, "identifier expected after %<@protocol%>");
26156 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26160 /* See if we have a forward declaration or a definition. */
26161 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26163 /* Try a forward declaration first. */
26164 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
26170 id
= cp_parser_identifier (parser
);
26171 if (id
== error_mark_node
)
26174 objc_declare_protocol (id
, attributes
);
26176 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26177 cp_lexer_consume_token (parser
->lexer
);
26181 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26184 /* Ok, we got a full-fledged definition (or at least should). */
26187 proto
= cp_parser_identifier (parser
);
26188 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
26189 objc_start_protocol (proto
, protorefs
, attributes
);
26190 cp_parser_objc_method_prototype_list (parser
);
26194 /* Parse an Objective-C superclass or category. */
26197 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
26200 tree
*categ
, bool *is_class_extension
)
26202 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
26204 *super
= *categ
= NULL_TREE
;
26205 *is_class_extension
= false;
26206 if (next
->type
== CPP_COLON
)
26208 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26209 *super
= cp_parser_identifier (parser
);
26211 else if (next
->type
== CPP_OPEN_PAREN
)
26213 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26215 /* If there is no category name, and this is an @interface, we
26216 have a class extension. */
26217 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26219 *categ
= NULL_TREE
;
26220 *is_class_extension
= true;
26223 *categ
= cp_parser_identifier (parser
);
26225 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26229 /* Parse an Objective-C class interface. */
26232 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
26234 tree name
, super
, categ
, protos
;
26235 bool is_class_extension
;
26237 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
26238 name
= cp_parser_identifier (parser
);
26239 if (name
== error_mark_node
)
26241 /* It's hard to recover because even if valid @interface stuff
26242 is to follow, we can't compile it (or validate it) if we
26243 don't even know which class it refers to. Let's assume this
26244 was a stray '@interface' token in the stream and skip it.
26248 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
26249 &is_class_extension
);
26250 protos
= cp_parser_objc_protocol_refs_opt (parser
);
26252 /* We have either a class or a category on our hands. */
26253 if (categ
|| is_class_extension
)
26254 objc_start_category_interface (name
, categ
, protos
, attributes
);
26257 objc_start_class_interface (name
, super
, protos
, attributes
);
26258 /* Handle instance variable declarations, if any. */
26259 cp_parser_objc_class_ivars (parser
);
26260 objc_continue_interface ();
26263 cp_parser_objc_method_prototype_list (parser
);
26266 /* Parse an Objective-C class implementation. */
26269 cp_parser_objc_class_implementation (cp_parser
* parser
)
26271 tree name
, super
, categ
;
26272 bool is_class_extension
;
26274 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
26275 name
= cp_parser_identifier (parser
);
26276 if (name
== error_mark_node
)
26278 /* It's hard to recover because even if valid @implementation
26279 stuff is to follow, we can't compile it (or validate it) if
26280 we don't even know which class it refers to. Let's assume
26281 this was a stray '@implementation' token in the stream and
26286 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
26287 &is_class_extension
);
26289 /* We have either a class or a category on our hands. */
26291 objc_start_category_implementation (name
, categ
);
26294 objc_start_class_implementation (name
, super
);
26295 /* Handle instance variable declarations, if any. */
26296 cp_parser_objc_class_ivars (parser
);
26297 objc_continue_implementation ();
26300 cp_parser_objc_method_definition_list (parser
);
26303 /* Consume the @end token and finish off the implementation. */
26306 cp_parser_objc_end_implementation (cp_parser
* parser
)
26308 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26309 objc_finish_implementation ();
26312 /* Parse an Objective-C declaration. */
26315 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
26317 /* Try to figure out what kind of declaration is present. */
26318 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26321 switch (kwd
->keyword
)
26326 error_at (kwd
->location
, "attributes may not be specified before"
26327 " the %<@%D%> Objective-C++ keyword",
26331 case RID_AT_IMPLEMENTATION
:
26332 warning_at (kwd
->location
, OPT_Wattributes
,
26333 "prefix attributes are ignored before %<@%D%>",
26340 switch (kwd
->keyword
)
26343 cp_parser_objc_alias_declaration (parser
);
26346 cp_parser_objc_class_declaration (parser
);
26348 case RID_AT_PROTOCOL
:
26349 cp_parser_objc_protocol_declaration (parser
, attributes
);
26351 case RID_AT_INTERFACE
:
26352 cp_parser_objc_class_interface (parser
, attributes
);
26354 case RID_AT_IMPLEMENTATION
:
26355 cp_parser_objc_class_implementation (parser
);
26358 cp_parser_objc_end_implementation (parser
);
26361 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26363 cp_parser_skip_to_end_of_block_or_statement (parser
);
26367 /* Parse an Objective-C try-catch-finally statement.
26369 objc-try-catch-finally-stmt:
26370 @try compound-statement objc-catch-clause-seq [opt]
26371 objc-finally-clause [opt]
26373 objc-catch-clause-seq:
26374 objc-catch-clause objc-catch-clause-seq [opt]
26377 @catch ( objc-exception-declaration ) compound-statement
26379 objc-finally-clause:
26380 @finally compound-statement
26382 objc-exception-declaration:
26383 parameter-declaration
26386 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26390 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26391 for C. Keep them in sync. */
26394 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
26396 location_t location
;
26399 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
26400 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26401 objc_maybe_warn_exceptions (location
);
26402 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26403 node, lest it get absorbed into the surrounding block. */
26404 stmt
= push_stmt_list ();
26405 cp_parser_compound_statement (parser
, NULL
, false, false);
26406 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
26408 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
26410 cp_parameter_declarator
*parm
;
26411 tree parameter_declaration
= error_mark_node
;
26412 bool seen_open_paren
= false;
26414 cp_lexer_consume_token (parser
->lexer
);
26415 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26416 seen_open_paren
= true;
26417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26419 /* We have "@catch (...)" (where the '...' are literally
26420 what is in the code). Skip the '...'.
26421 parameter_declaration is set to NULL_TREE, and
26422 objc_being_catch_clauses() knows that that means
26424 cp_lexer_consume_token (parser
->lexer
);
26425 parameter_declaration
= NULL_TREE
;
26429 /* We have "@catch (NSException *exception)" or something
26430 like that. Parse the parameter declaration. */
26431 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
26433 parameter_declaration
= error_mark_node
;
26435 parameter_declaration
= grokdeclarator (parm
->declarator
,
26436 &parm
->decl_specifiers
,
26437 PARM
, /*initialized=*/0,
26438 /*attrlist=*/NULL
);
26440 if (seen_open_paren
)
26441 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26444 /* If there was no open parenthesis, we are recovering from
26445 an error, and we are trying to figure out what mistake
26446 the user has made. */
26448 /* If there is an immediate closing parenthesis, the user
26449 probably forgot the opening one (ie, they typed "@catch
26450 NSException *e)". Parse the closing parenthesis and keep
26452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26453 cp_lexer_consume_token (parser
->lexer
);
26455 /* If these is no immediate closing parenthesis, the user
26456 probably doesn't know that parenthesis are required at
26457 all (ie, they typed "@catch NSException *e"). So, just
26458 forget about the closing parenthesis and keep going. */
26460 objc_begin_catch_clause (parameter_declaration
);
26461 cp_parser_compound_statement (parser
, NULL
, false, false);
26462 objc_finish_catch_clause ();
26464 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
26466 cp_lexer_consume_token (parser
->lexer
);
26467 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26468 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26469 node, lest it get absorbed into the surrounding block. */
26470 stmt
= push_stmt_list ();
26471 cp_parser_compound_statement (parser
, NULL
, false, false);
26472 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
26475 return objc_finish_try_stmt ();
26478 /* Parse an Objective-C synchronized statement.
26480 objc-synchronized-stmt:
26481 @synchronized ( expression ) compound-statement
26483 Returns NULL_TREE. */
26486 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
26488 location_t location
;
26491 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
26493 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26494 objc_maybe_warn_exceptions (location
);
26495 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
26496 lock
= cp_parser_expression (parser
, false, NULL
);
26497 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26499 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26500 node, lest it get absorbed into the surrounding block. */
26501 stmt
= push_stmt_list ();
26502 cp_parser_compound_statement (parser
, NULL
, false, false);
26504 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
26507 /* Parse an Objective-C throw statement.
26510 @throw assignment-expression [opt] ;
26512 Returns a constructed '@throw' statement. */
26515 cp_parser_objc_throw_statement (cp_parser
*parser
)
26517 tree expr
= NULL_TREE
;
26518 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26520 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
26522 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26523 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
26525 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26527 return objc_build_throw_stmt (loc
, expr
);
26530 /* Parse an Objective-C statement. */
26533 cp_parser_objc_statement (cp_parser
* parser
)
26535 /* Try to figure out what kind of declaration is present. */
26536 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26538 switch (kwd
->keyword
)
26541 return cp_parser_objc_try_catch_finally_statement (parser
);
26542 case RID_AT_SYNCHRONIZED
:
26543 return cp_parser_objc_synchronized_statement (parser
);
26545 return cp_parser_objc_throw_statement (parser
);
26547 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26549 cp_parser_skip_to_end_of_block_or_statement (parser
);
26552 return error_mark_node
;
26555 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26556 look ahead to see if an objc keyword follows the attributes. This
26557 is to detect the use of prefix attributes on ObjC @interface and
26561 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
26563 cp_lexer_save_tokens (parser
->lexer
);
26564 *attrib
= cp_parser_attributes_opt (parser
);
26565 gcc_assert (*attrib
);
26566 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
26568 cp_lexer_commit_tokens (parser
->lexer
);
26571 cp_lexer_rollback_tokens (parser
->lexer
);
26575 /* This routine is a minimal replacement for
26576 c_parser_struct_declaration () used when parsing the list of
26577 types/names or ObjC++ properties. For example, when parsing the
26580 @property (readonly) int a, b, c;
26582 this function is responsible for parsing "int a, int b, int c" and
26583 returning the declarations as CHAIN of DECLs.
26585 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26586 similar parsing. */
26588 cp_parser_objc_struct_declaration (cp_parser
*parser
)
26590 tree decls
= NULL_TREE
;
26591 cp_decl_specifier_seq declspecs
;
26592 int decl_class_or_enum_p
;
26593 tree prefix_attributes
;
26595 cp_parser_decl_specifier_seq (parser
,
26596 CP_PARSER_FLAGS_NONE
,
26598 &decl_class_or_enum_p
);
26600 if (declspecs
.type
== error_mark_node
)
26601 return error_mark_node
;
26603 /* auto, register, static, extern, mutable. */
26604 if (declspecs
.storage_class
!= sc_none
)
26606 cp_parser_error (parser
, "invalid type for property");
26607 declspecs
.storage_class
= sc_none
;
26610 /* thread_local. */
26611 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26613 cp_parser_error (parser
, "invalid type for property");
26614 declspecs
.locations
[ds_thread
] = 0;
26618 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26620 cp_parser_error (parser
, "invalid type for property");
26621 declspecs
.locations
[ds_typedef
] = 0;
26624 prefix_attributes
= declspecs
.attributes
;
26625 declspecs
.attributes
= NULL_TREE
;
26627 /* Keep going until we hit the `;' at the end of the declaration. */
26628 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26630 tree attributes
, first_attribute
, decl
;
26631 cp_declarator
*declarator
;
26634 /* Parse the declarator. */
26635 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26636 NULL
, NULL
, false);
26638 /* Look for attributes that apply to the ivar. */
26639 attributes
= cp_parser_attributes_opt (parser
);
26640 /* Remember which attributes are prefix attributes and
26642 first_attribute
= attributes
;
26643 /* Combine the attributes. */
26644 attributes
= chainon (prefix_attributes
, attributes
);
26646 decl
= grokfield (declarator
, &declspecs
,
26647 NULL_TREE
, /*init_const_expr_p=*/false,
26648 NULL_TREE
, attributes
);
26650 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
26651 return error_mark_node
;
26653 /* Reset PREFIX_ATTRIBUTES. */
26654 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26655 attributes
= TREE_CHAIN (attributes
);
26657 TREE_CHAIN (attributes
) = NULL_TREE
;
26659 DECL_CHAIN (decl
) = decls
;
26662 token
= cp_lexer_peek_token (parser
->lexer
);
26663 if (token
->type
== CPP_COMMA
)
26665 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26674 /* Parse an Objective-C @property declaration. The syntax is:
26676 objc-property-declaration:
26677 '@property' objc-property-attributes[opt] struct-declaration ;
26679 objc-property-attributes:
26680 '(' objc-property-attribute-list ')'
26682 objc-property-attribute-list:
26683 objc-property-attribute
26684 objc-property-attribute-list, objc-property-attribute
26686 objc-property-attribute
26687 'getter' = identifier
26688 'setter' = identifier
26697 @property NSString *name;
26698 @property (readonly) id object;
26699 @property (retain, nonatomic, getter=getTheName) id name;
26700 @property int a, b, c;
26702 PS: This function is identical to
26703 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26705 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
26707 /* The following variables hold the attributes of the properties as
26708 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26709 seen. When we see an attribute, we set them to 'true' (if they
26710 are boolean properties) or to the identifier (if they have an
26711 argument, ie, for getter and setter). Note that here we only
26712 parse the list of attributes, check the syntax and accumulate the
26713 attributes that we find. objc_add_property_declaration() will
26714 then process the information. */
26715 bool property_assign
= false;
26716 bool property_copy
= false;
26717 tree property_getter_ident
= NULL_TREE
;
26718 bool property_nonatomic
= false;
26719 bool property_readonly
= false;
26720 bool property_readwrite
= false;
26721 bool property_retain
= false;
26722 tree property_setter_ident
= NULL_TREE
;
26724 /* 'properties' is the list of properties that we read. Usually a
26725 single one, but maybe more (eg, in "@property int a, b, c;" there
26730 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26732 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
26734 /* Parse the optional attribute list... */
26735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26738 cp_lexer_consume_token (parser
->lexer
);
26742 bool syntax_error
= false;
26743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26746 if (token
->type
!= CPP_NAME
)
26748 cp_parser_error (parser
, "expected identifier");
26751 keyword
= C_RID_CODE (token
->u
.value
);
26752 cp_lexer_consume_token (parser
->lexer
);
26755 case RID_ASSIGN
: property_assign
= true; break;
26756 case RID_COPY
: property_copy
= true; break;
26757 case RID_NONATOMIC
: property_nonatomic
= true; break;
26758 case RID_READONLY
: property_readonly
= true; break;
26759 case RID_READWRITE
: property_readwrite
= true; break;
26760 case RID_RETAIN
: property_retain
= true; break;
26764 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
26766 if (keyword
== RID_GETTER
)
26767 cp_parser_error (parser
,
26768 "missing %<=%> (after %<getter%> attribute)");
26770 cp_parser_error (parser
,
26771 "missing %<=%> (after %<setter%> attribute)");
26772 syntax_error
= true;
26775 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
26776 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
26778 cp_parser_error (parser
, "expected identifier");
26779 syntax_error
= true;
26782 if (keyword
== RID_SETTER
)
26784 if (property_setter_ident
!= NULL_TREE
)
26786 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
26787 cp_lexer_consume_token (parser
->lexer
);
26790 property_setter_ident
= cp_parser_objc_selector (parser
);
26791 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26792 cp_parser_error (parser
, "setter name must terminate with %<:%>");
26794 cp_lexer_consume_token (parser
->lexer
);
26798 if (property_getter_ident
!= NULL_TREE
)
26800 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
26801 cp_lexer_consume_token (parser
->lexer
);
26804 property_getter_ident
= cp_parser_objc_selector (parser
);
26808 cp_parser_error (parser
, "unknown property attribute");
26809 syntax_error
= true;
26816 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26817 cp_lexer_consume_token (parser
->lexer
);
26822 /* FIXME: "@property (setter, assign);" will generate a spurious
26823 "error: expected ‘)’ before ‘,’ token". This is because
26824 cp_parser_require, unlike the C counterpart, will produce an
26825 error even if we are in error recovery. */
26826 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26828 cp_parser_skip_to_closing_parenthesis (parser
,
26829 /*recovering=*/true,
26830 /*or_comma=*/false,
26831 /*consume_paren=*/true);
26835 /* ... and the property declaration(s). */
26836 properties
= cp_parser_objc_struct_declaration (parser
);
26838 if (properties
== error_mark_node
)
26840 cp_parser_skip_to_end_of_statement (parser
);
26841 /* If the next token is now a `;', consume it. */
26842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26843 cp_lexer_consume_token (parser
->lexer
);
26847 if (properties
== NULL_TREE
)
26848 cp_parser_error (parser
, "expected identifier");
26851 /* Comma-separated properties are chained together in
26852 reverse order; add them one by one. */
26853 properties
= nreverse (properties
);
26855 for (; properties
; properties
= TREE_CHAIN (properties
))
26856 objc_add_property_declaration (loc
, copy_node (properties
),
26857 property_readonly
, property_readwrite
,
26858 property_assign
, property_retain
,
26859 property_copy
, property_nonatomic
,
26860 property_getter_ident
, property_setter_ident
);
26863 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26866 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26868 objc-synthesize-declaration:
26869 @synthesize objc-synthesize-identifier-list ;
26871 objc-synthesize-identifier-list:
26872 objc-synthesize-identifier
26873 objc-synthesize-identifier-list, objc-synthesize-identifier
26875 objc-synthesize-identifier
26877 identifier = identifier
26880 @synthesize MyProperty;
26881 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26883 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26884 for C. Keep them in sync.
26887 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
26889 tree list
= NULL_TREE
;
26891 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26893 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
26896 tree property
, ivar
;
26897 property
= cp_parser_identifier (parser
);
26898 if (property
== error_mark_node
)
26900 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
26905 cp_lexer_consume_token (parser
->lexer
);
26906 ivar
= cp_parser_identifier (parser
);
26907 if (ivar
== error_mark_node
)
26909 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26915 list
= chainon (list
, build_tree_list (ivar
, property
));
26916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26917 cp_lexer_consume_token (parser
->lexer
);
26921 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26922 objc_add_synthesize_declaration (loc
, list
);
26925 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26927 objc-dynamic-declaration:
26928 @dynamic identifier-list ;
26931 @dynamic MyProperty;
26932 @dynamic MyProperty, AnotherProperty;
26934 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26935 for C. Keep them in sync.
26938 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
26940 tree list
= NULL_TREE
;
26942 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26944 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
26948 property
= cp_parser_identifier (parser
);
26949 if (property
== error_mark_node
)
26951 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26954 list
= chainon (list
, build_tree_list (NULL
, property
));
26955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26956 cp_lexer_consume_token (parser
->lexer
);
26960 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26961 objc_add_dynamic_declaration (loc
, list
);
26965 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26967 /* Returns name of the next clause.
26968 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26969 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26970 returned and the token is consumed. */
26972 static pragma_omp_clause
26973 cp_parser_omp_clause_name (cp_parser
*parser
)
26975 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
26977 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
26978 result
= PRAGMA_OMP_CLAUSE_IF
;
26979 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
26980 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
26981 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
26982 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
26983 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
26984 result
= PRAGMA_OMP_CLAUSE_FOR
;
26985 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26987 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26988 const char *p
= IDENTIFIER_POINTER (id
);
26993 if (!strcmp ("aligned", p
))
26994 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
26997 if (!strcmp ("collapse", p
))
26998 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
26999 else if (!strcmp ("copyin", p
))
27000 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
27001 else if (!strcmp ("copyprivate", p
))
27002 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
27005 if (!strcmp ("depend", p
))
27006 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
27007 else if (!strcmp ("device", p
))
27008 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
27009 else if (!strcmp ("dist_schedule", p
))
27010 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
27013 if (!strcmp ("final", p
))
27014 result
= PRAGMA_OMP_CLAUSE_FINAL
;
27015 else if (!strcmp ("firstprivate", p
))
27016 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
27017 else if (!strcmp ("from", p
))
27018 result
= PRAGMA_OMP_CLAUSE_FROM
;
27021 if (!strcmp ("inbranch", p
))
27022 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
27025 if (!strcmp ("lastprivate", p
))
27026 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
27027 else if (!strcmp ("linear", p
))
27028 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
27031 if (!strcmp ("map", p
))
27032 result
= PRAGMA_OMP_CLAUSE_MAP
;
27033 else if (!strcmp ("mergeable", p
))
27034 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
27035 else if (flag_cilkplus
&& !strcmp ("mask", p
))
27036 result
= PRAGMA_CILK_CLAUSE_MASK
;
27039 if (!strcmp ("notinbranch", p
))
27040 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
27041 else if (!strcmp ("nowait", p
))
27042 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
27043 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
27044 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
27045 else if (!strcmp ("num_teams", p
))
27046 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
27047 else if (!strcmp ("num_threads", p
))
27048 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
27051 if (!strcmp ("ordered", p
))
27052 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
27055 if (!strcmp ("parallel", p
))
27056 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
27057 else if (!strcmp ("proc_bind", p
))
27058 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
27061 if (!strcmp ("reduction", p
))
27062 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
27065 if (!strcmp ("safelen", p
))
27066 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
27067 else if (!strcmp ("schedule", p
))
27068 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
27069 else if (!strcmp ("sections", p
))
27070 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
27071 else if (!strcmp ("shared", p
))
27072 result
= PRAGMA_OMP_CLAUSE_SHARED
;
27073 else if (!strcmp ("simdlen", p
))
27074 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
27077 if (!strcmp ("taskgroup", p
))
27078 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
27079 else if (!strcmp ("thread_limit", p
))
27080 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
27081 else if (!strcmp ("to", p
))
27082 result
= PRAGMA_OMP_CLAUSE_TO
;
27085 if (!strcmp ("uniform", p
))
27086 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
27087 else if (!strcmp ("untied", p
))
27088 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
27091 if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
27092 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
27097 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
27098 cp_lexer_consume_token (parser
->lexer
);
27103 /* Validate that a clause of the given type does not already exist. */
27106 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
27107 const char *name
, location_t location
)
27111 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27112 if (OMP_CLAUSE_CODE (c
) == code
)
27114 error_at (location
, "too many %qs clauses", name
);
27122 variable-list , identifier
27124 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27125 colon). An opening parenthesis will have been consumed by the caller.
27127 If KIND is nonzero, create the appropriate node and install the decl
27128 in OMP_CLAUSE_DECL and add the node to the head of the list.
27130 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27131 return the list created.
27133 COLON can be NULL if only closing parenthesis should end the list,
27134 or pointer to bool which will receive false if the list is terminated
27135 by closing parenthesis or true if the list is terminated by colon. */
27138 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
27139 tree list
, bool *colon
)
27142 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27145 parser
->colon_corrects_to_scope_p
= false;
27152 token
= cp_lexer_peek_token (parser
->lexer
);
27153 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
27154 /*check_dependency_p=*/true,
27155 /*template_p=*/NULL
,
27156 /*declarator_p=*/false,
27157 /*optional_p=*/false);
27158 if (name
== error_mark_node
)
27161 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27162 if (decl
== error_mark_node
)
27163 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
27165 else if (kind
!= 0)
27169 case OMP_CLAUSE_MAP
:
27170 case OMP_CLAUSE_FROM
:
27171 case OMP_CLAUSE_TO
:
27172 case OMP_CLAUSE_DEPEND
:
27173 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27175 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
27177 parser
->colon_corrects_to_scope_p
= false;
27178 cp_lexer_consume_token (parser
->lexer
);
27179 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27180 low_bound
= cp_parser_expression (parser
, /*cast_p=*/false,
27183 parser
->colon_corrects_to_scope_p
27184 = saved_colon_corrects_to_scope_p
;
27185 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
27186 length
= integer_one_node
;
27189 /* Look for `:'. */
27190 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27192 if (!cp_lexer_next_token_is (parser
->lexer
,
27194 length
= cp_parser_expression (parser
,
27198 /* Look for the closing `]'. */
27199 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
27202 decl
= tree_cons (low_bound
, length
, decl
);
27209 tree u
= build_omp_clause (token
->location
, kind
);
27210 OMP_CLAUSE_DECL (u
) = decl
;
27211 OMP_CLAUSE_CHAIN (u
) = list
;
27215 list
= tree_cons (decl
, NULL_TREE
, list
);
27218 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27220 cp_lexer_consume_token (parser
->lexer
);
27224 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27226 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27229 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27233 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27237 /* Try to resync to an unnested comma. Copied from
27238 cp_parser_parenthesized_expression_list. */
27241 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27242 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
27243 /*recovering=*/true,
27245 /*consume_paren=*/true);
27253 /* Similarly, but expect leading and trailing parenthesis. This is a very
27254 common case for omp clauses. */
27257 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
27259 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27260 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
27265 collapse ( constant-expression ) */
27268 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
27274 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27275 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27278 num
= cp_parser_constant_expression (parser
, false, NULL
);
27280 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27281 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27282 /*or_comma=*/false,
27283 /*consume_paren=*/true);
27285 if (num
== error_mark_node
)
27287 num
= fold_non_dependent_expr (num
);
27288 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
27289 || !tree_fits_shwi_p (num
)
27290 || (n
= tree_to_shwi (num
)) <= 0
27293 error_at (loc
, "collapse argument needs positive constant integer expression");
27297 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
27298 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
27299 OMP_CLAUSE_CHAIN (c
) = list
;
27300 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
27306 default ( shared | none ) */
27309 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
27311 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
27314 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27318 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27319 const char *p
= IDENTIFIER_POINTER (id
);
27324 if (strcmp ("none", p
) != 0)
27326 kind
= OMP_CLAUSE_DEFAULT_NONE
;
27330 if (strcmp ("shared", p
) != 0)
27332 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
27339 cp_lexer_consume_token (parser
->lexer
);
27344 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
27347 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27348 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27349 /*or_comma=*/false,
27350 /*consume_paren=*/true);
27352 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
27355 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
27356 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
27357 OMP_CLAUSE_CHAIN (c
) = list
;
27358 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
27364 final ( expression ) */
27367 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
27371 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27374 t
= cp_parser_condition (parser
);
27376 if (t
== error_mark_node
27377 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27378 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27379 /*or_comma=*/false,
27380 /*consume_paren=*/true);
27382 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
27384 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
27385 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
27386 OMP_CLAUSE_CHAIN (c
) = list
;
27392 if ( expression ) */
27395 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
27399 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27402 t
= cp_parser_condition (parser
);
27404 if (t
== error_mark_node
27405 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27406 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27407 /*or_comma=*/false,
27408 /*consume_paren=*/true);
27410 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
27412 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
27413 OMP_CLAUSE_IF_EXPR (c
) = t
;
27414 OMP_CLAUSE_CHAIN (c
) = list
;
27423 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
27424 tree list
, location_t location
)
27428 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
27431 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
27432 OMP_CLAUSE_CHAIN (c
) = list
;
27440 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
27441 tree list
, location_t location
)
27445 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
27447 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
27448 OMP_CLAUSE_CHAIN (c
) = list
;
27453 num_threads ( expression ) */
27456 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
27457 location_t location
)
27461 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27464 t
= cp_parser_expression (parser
, false, NULL
);
27466 if (t
== error_mark_node
27467 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27468 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27469 /*or_comma=*/false,
27470 /*consume_paren=*/true);
27472 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
27473 "num_threads", location
);
27475 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
27476 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
27477 OMP_CLAUSE_CHAIN (c
) = list
;
27486 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
27487 tree list
, location_t location
)
27491 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
27492 "ordered", location
);
27494 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
27495 OMP_CLAUSE_CHAIN (c
) = list
;
27500 reduction ( reduction-operator : variable-list )
27502 reduction-operator:
27503 One of: + * - & ^ | && ||
27507 reduction-operator:
27508 One of: + * - & ^ | && || min max
27512 reduction-operator:
27513 One of: + * - & ^ | && ||
27517 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
27519 enum tree_code code
= ERROR_MARK
;
27520 tree nlist
, c
, id
= NULL_TREE
;
27522 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27525 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27527 case CPP_PLUS
: code
= PLUS_EXPR
; break;
27528 case CPP_MULT
: code
= MULT_EXPR
; break;
27529 case CPP_MINUS
: code
= MINUS_EXPR
; break;
27530 case CPP_AND
: code
= BIT_AND_EXPR
; break;
27531 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
27532 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
27533 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
27534 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
27538 if (code
!= ERROR_MARK
)
27539 cp_lexer_consume_token (parser
->lexer
);
27542 bool saved_colon_corrects_to_scope_p
;
27543 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27544 parser
->colon_corrects_to_scope_p
= false;
27545 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
27546 /*check_dependency_p=*/true,
27547 /*template_p=*/NULL
,
27548 /*declarator_p=*/false,
27549 /*optional_p=*/false);
27550 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27551 if (identifier_p (id
))
27553 const char *p
= IDENTIFIER_POINTER (id
);
27555 if (strcmp (p
, "min") == 0)
27557 else if (strcmp (p
, "max") == 0)
27559 else if (id
== ansi_opname (PLUS_EXPR
))
27561 else if (id
== ansi_opname (MULT_EXPR
))
27563 else if (id
== ansi_opname (MINUS_EXPR
))
27565 else if (id
== ansi_opname (BIT_AND_EXPR
))
27566 code
= BIT_AND_EXPR
;
27567 else if (id
== ansi_opname (BIT_IOR_EXPR
))
27568 code
= BIT_IOR_EXPR
;
27569 else if (id
== ansi_opname (BIT_XOR_EXPR
))
27570 code
= BIT_XOR_EXPR
;
27571 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
27572 code
= TRUTH_ANDIF_EXPR
;
27573 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
27574 code
= TRUTH_ORIF_EXPR
;
27575 id
= omp_reduction_id (code
, id
, NULL_TREE
);
27576 tree scope
= parser
->scope
;
27578 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
27579 parser
->scope
= NULL_TREE
;
27580 parser
->qualifying_scope
= NULL_TREE
;
27581 parser
->object_scope
= NULL_TREE
;
27585 error ("invalid reduction-identifier");
27587 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27588 /*or_comma=*/false,
27589 /*consume_paren=*/true);
27594 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27597 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
27599 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27601 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
27602 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
27609 schedule ( schedule-kind )
27610 schedule ( schedule-kind , expression )
27613 static | dynamic | guided | runtime | auto */
27616 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
27620 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27623 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
27625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27627 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27628 const char *p
= IDENTIFIER_POINTER (id
);
27633 if (strcmp ("dynamic", p
) != 0)
27635 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
27639 if (strcmp ("guided", p
) != 0)
27641 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
27645 if (strcmp ("runtime", p
) != 0)
27647 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
27654 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
27655 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
27656 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
27657 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
27660 cp_lexer_consume_token (parser
->lexer
);
27662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27665 cp_lexer_consume_token (parser
->lexer
);
27667 token
= cp_lexer_peek_token (parser
->lexer
);
27668 t
= cp_parser_assignment_expression (parser
, false, NULL
);
27670 if (t
== error_mark_node
)
27672 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
27673 error_at (token
->location
, "schedule %<runtime%> does not take "
27674 "a %<chunk_size%> parameter");
27675 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
27676 error_at (token
->location
, "schedule %<auto%> does not take "
27677 "a %<chunk_size%> parameter");
27679 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
27681 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27684 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
27687 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
27688 OMP_CLAUSE_CHAIN (c
) = list
;
27692 cp_parser_error (parser
, "invalid schedule kind");
27694 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27695 /*or_comma=*/false,
27696 /*consume_paren=*/true);
27704 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
27705 tree list
, location_t location
)
27709 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
27711 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
27712 OMP_CLAUSE_CHAIN (c
) = list
;
27721 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
27722 tree list
, location_t location
)
27724 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
27725 tree c
= build_omp_clause (location
, code
);
27726 OMP_CLAUSE_CHAIN (c
) = list
;
27737 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
27738 enum omp_clause_code code
,
27739 tree list
, location_t location
)
27741 tree c
= build_omp_clause (location
, code
);
27742 OMP_CLAUSE_CHAIN (c
) = list
;
27747 num_teams ( expression ) */
27750 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
27751 location_t location
)
27755 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27758 t
= cp_parser_expression (parser
, false, NULL
);
27760 if (t
== error_mark_node
27761 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27762 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27763 /*or_comma=*/false,
27764 /*consume_paren=*/true);
27766 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
27767 "num_teams", location
);
27769 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
27770 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
27771 OMP_CLAUSE_CHAIN (c
) = list
;
27777 thread_limit ( expression ) */
27780 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
27781 location_t location
)
27785 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27788 t
= cp_parser_expression (parser
, false, NULL
);
27790 if (t
== error_mark_node
27791 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27792 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27793 /*or_comma=*/false,
27794 /*consume_paren=*/true);
27796 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
27797 "thread_limit", location
);
27799 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
27800 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
27801 OMP_CLAUSE_CHAIN (c
) = list
;
27807 aligned ( variable-list )
27808 aligned ( variable-list : constant-expression ) */
27811 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
27813 tree nlist
, c
, alignment
= NULL_TREE
;
27816 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27819 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
27824 alignment
= cp_parser_constant_expression (parser
, false, NULL
);
27826 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27827 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27828 /*or_comma=*/false,
27829 /*consume_paren=*/true);
27831 if (alignment
== error_mark_node
)
27832 alignment
= NULL_TREE
;
27835 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27836 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
27842 linear ( variable-list )
27843 linear ( variable-list : expression ) */
27846 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
27847 bool is_cilk_simd_fn
)
27849 tree nlist
, c
, step
= integer_one_node
;
27852 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27855 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
27860 step
= cp_parser_expression (parser
, false, NULL
);
27862 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
27864 sorry ("using parameters for %<linear%> step is not supported yet");
27865 step
= integer_one_node
;
27867 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27868 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27869 /*or_comma=*/false,
27870 /*consume_paren=*/true);
27872 if (step
== error_mark_node
)
27876 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27877 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
27883 safelen ( constant-expression ) */
27886 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
27887 location_t location
)
27891 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27894 t
= cp_parser_constant_expression (parser
, false, NULL
);
27896 if (t
== error_mark_node
27897 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27898 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27899 /*or_comma=*/false,
27900 /*consume_paren=*/true);
27902 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
27904 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
27905 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
27906 OMP_CLAUSE_CHAIN (c
) = list
;
27912 simdlen ( constant-expression ) */
27915 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
27916 location_t location
)
27920 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27923 t
= cp_parser_constant_expression (parser
, false, NULL
);
27925 if (t
== error_mark_node
27926 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27927 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27928 /*or_comma=*/false,
27929 /*consume_paren=*/true);
27931 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
27933 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
27934 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
27935 OMP_CLAUSE_CHAIN (c
) = list
;
27941 depend ( depend-kind : variable-list )
27944 in | out | inout */
27947 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
27950 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
27952 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27957 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27958 const char *p
= IDENTIFIER_POINTER (id
);
27960 if (strcmp ("in", p
) == 0)
27961 kind
= OMP_CLAUSE_DEPEND_IN
;
27962 else if (strcmp ("inout", p
) == 0)
27963 kind
= OMP_CLAUSE_DEPEND_INOUT
;
27964 else if (strcmp ("out", p
) == 0)
27965 kind
= OMP_CLAUSE_DEPEND_OUT
;
27972 cp_lexer_consume_token (parser
->lexer
);
27973 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27976 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
27979 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27980 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
27985 cp_parser_error (parser
, "invalid depend kind");
27987 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27988 /*or_comma=*/false,
27989 /*consume_paren=*/true);
27994 map ( map-kind : variable-list )
27995 map ( variable-list )
27998 alloc | to | from | tofrom */
28001 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
28004 enum omp_clause_map_kind kind
= OMP_CLAUSE_MAP_TOFROM
;
28006 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
28010 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
28012 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28013 const char *p
= IDENTIFIER_POINTER (id
);
28015 if (strcmp ("alloc", p
) == 0)
28016 kind
= OMP_CLAUSE_MAP_ALLOC
;
28017 else if (strcmp ("to", p
) == 0)
28018 kind
= OMP_CLAUSE_MAP_TO
;
28019 else if (strcmp ("from", p
) == 0)
28020 kind
= OMP_CLAUSE_MAP_FROM
;
28021 else if (strcmp ("tofrom", p
) == 0)
28022 kind
= OMP_CLAUSE_MAP_TOFROM
;
28025 cp_parser_error (parser
, "invalid map kind");
28026 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28027 /*or_comma=*/false,
28028 /*consume_paren=*/true);
28031 cp_lexer_consume_token (parser
->lexer
);
28032 cp_lexer_consume_token (parser
->lexer
);
28035 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
28038 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28039 OMP_CLAUSE_MAP_KIND (c
) = kind
;
28045 device ( expression ) */
28048 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
28049 location_t location
)
28053 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28056 t
= cp_parser_expression (parser
, false, NULL
);
28058 if (t
== error_mark_node
28059 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28060 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28061 /*or_comma=*/false,
28062 /*consume_paren=*/true);
28064 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
28065 "device", location
);
28067 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
28068 OMP_CLAUSE_DEVICE_ID (c
) = t
;
28069 OMP_CLAUSE_CHAIN (c
) = list
;
28075 dist_schedule ( static )
28076 dist_schedule ( static , expression ) */
28079 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
28080 location_t location
)
28084 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28087 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
28089 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28091 cp_lexer_consume_token (parser
->lexer
);
28093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28095 cp_lexer_consume_token (parser
->lexer
);
28097 t
= cp_parser_assignment_expression (parser
, false, NULL
);
28099 if (t
== error_mark_node
)
28101 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
28103 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28106 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28109 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
28111 OMP_CLAUSE_CHAIN (c
) = list
;
28115 cp_parser_error (parser
, "invalid dist_schedule kind");
28117 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28118 /*or_comma=*/false,
28119 /*consume_paren=*/true);
28124 proc_bind ( proc-bind-kind )
28127 master | close | spread */
28130 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
28131 location_t location
)
28134 enum omp_clause_proc_bind_kind kind
;
28136 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28141 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28142 const char *p
= IDENTIFIER_POINTER (id
);
28144 if (strcmp ("master", p
) == 0)
28145 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
28146 else if (strcmp ("close", p
) == 0)
28147 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
28148 else if (strcmp ("spread", p
) == 0)
28149 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
28156 cp_lexer_consume_token (parser
->lexer
);
28157 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28160 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
28161 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
28163 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
28164 OMP_CLAUSE_CHAIN (c
) = list
;
28168 cp_parser_error (parser
, "invalid depend kind");
28170 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28171 /*or_comma=*/false,
28172 /*consume_paren=*/true);
28176 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28177 is a bitmask in MASK. Return the list of clauses found; the result
28178 of clause default goes in *pdefault. */
28181 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
28182 const char *where
, cp_token
*pragma_tok
,
28183 bool finish_p
= true)
28185 tree clauses
= NULL
;
28187 cp_token
*token
= NULL
;
28188 bool cilk_simd_fn
= false;
28190 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
28192 pragma_omp_clause c_kind
;
28193 const char *c_name
;
28194 tree prev
= clauses
;
28196 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28197 cp_lexer_consume_token (parser
->lexer
);
28199 token
= cp_lexer_peek_token (parser
->lexer
);
28200 c_kind
= cp_parser_omp_clause_name (parser
);
28204 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
28205 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
28207 c_name
= "collapse";
28209 case PRAGMA_OMP_CLAUSE_COPYIN
:
28210 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
28213 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
28214 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
28216 c_name
= "copyprivate";
28218 case PRAGMA_OMP_CLAUSE_DEFAULT
:
28219 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
28221 c_name
= "default";
28223 case PRAGMA_OMP_CLAUSE_FINAL
:
28224 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
28227 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
28228 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
28230 c_name
= "firstprivate";
28232 case PRAGMA_OMP_CLAUSE_IF
:
28233 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
28236 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
28237 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
28239 c_name
= "lastprivate";
28241 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
28242 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
28244 c_name
= "mergeable";
28246 case PRAGMA_OMP_CLAUSE_NOWAIT
:
28247 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
28250 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
28251 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
28253 c_name
= "num_threads";
28255 case PRAGMA_OMP_CLAUSE_ORDERED
:
28256 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
28258 c_name
= "ordered";
28260 case PRAGMA_OMP_CLAUSE_PRIVATE
:
28261 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
28263 c_name
= "private";
28265 case PRAGMA_OMP_CLAUSE_REDUCTION
:
28266 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
28267 c_name
= "reduction";
28269 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
28270 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
28272 c_name
= "schedule";
28274 case PRAGMA_OMP_CLAUSE_SHARED
:
28275 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
28279 case PRAGMA_OMP_CLAUSE_UNTIED
:
28280 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
28284 case PRAGMA_OMP_CLAUSE_INBRANCH
:
28285 case PRAGMA_CILK_CLAUSE_MASK
:
28286 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
28287 clauses
, token
->location
);
28288 c_name
= "inbranch";
28290 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
28291 case PRAGMA_CILK_CLAUSE_NOMASK
:
28292 clauses
= cp_parser_omp_clause_branch (parser
,
28293 OMP_CLAUSE_NOTINBRANCH
,
28294 clauses
, token
->location
);
28295 c_name
= "notinbranch";
28297 case PRAGMA_OMP_CLAUSE_PARALLEL
:
28298 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
28299 clauses
, token
->location
);
28300 c_name
= "parallel";
28304 error_at (token
->location
, "%qs must be the first clause of %qs",
28309 case PRAGMA_OMP_CLAUSE_FOR
:
28310 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
28311 clauses
, token
->location
);
28314 goto clause_not_first
;
28316 case PRAGMA_OMP_CLAUSE_SECTIONS
:
28317 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
28318 clauses
, token
->location
);
28319 c_name
= "sections";
28321 goto clause_not_first
;
28323 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
28324 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
28325 clauses
, token
->location
);
28326 c_name
= "taskgroup";
28328 goto clause_not_first
;
28330 case PRAGMA_OMP_CLAUSE_TO
:
28331 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
28335 case PRAGMA_OMP_CLAUSE_FROM
:
28336 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
28340 case PRAGMA_OMP_CLAUSE_UNIFORM
:
28341 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
28343 c_name
= "uniform";
28345 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
28346 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
28348 c_name
= "num_teams";
28350 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
28351 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
28353 c_name
= "thread_limit";
28355 case PRAGMA_OMP_CLAUSE_ALIGNED
:
28356 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
28357 c_name
= "aligned";
28359 case PRAGMA_OMP_CLAUSE_LINEAR
:
28360 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
28361 cilk_simd_fn
= true;
28362 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, cilk_simd_fn
);
28365 case PRAGMA_OMP_CLAUSE_DEPEND
:
28366 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
28369 case PRAGMA_OMP_CLAUSE_MAP
:
28370 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
28373 case PRAGMA_OMP_CLAUSE_DEVICE
:
28374 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
28378 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
28379 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
28381 c_name
= "dist_schedule";
28383 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
28384 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
28386 c_name
= "proc_bind";
28388 case PRAGMA_OMP_CLAUSE_SAFELEN
:
28389 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
28391 c_name
= "safelen";
28393 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
28394 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
28396 c_name
= "simdlen";
28398 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
28399 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
28400 c_name
= "simdlen";
28403 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
28409 if (((mask
>> c_kind
) & 1) == 0)
28411 /* Remove the invalid clause(s) from the list to avoid
28412 confusing the rest of the compiler. */
28414 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
28418 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28419 no reason to skip to the end. */
28420 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
28421 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
28423 return finish_omp_clauses (clauses
);
28431 In practice, we're also interested in adding the statement to an
28432 outer node. So it is convenient if we work around the fact that
28433 cp_parser_statement calls add_stmt. */
28436 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
28438 unsigned save
= parser
->in_statement
;
28440 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28441 This preserves the "not within loop or switch" style error messages
28442 for nonsense cases like
28448 if (parser
->in_statement
)
28449 parser
->in_statement
= IN_OMP_BLOCK
;
28455 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
28457 parser
->in_statement
= save
;
28461 cp_parser_omp_structured_block (cp_parser
*parser
)
28463 tree stmt
= begin_omp_structured_block ();
28464 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
28466 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
28468 cp_parser_end_omp_structured_block (parser
, save
);
28469 return finish_omp_structured_block (stmt
);
28473 # pragma omp atomic new-line
28477 x binop= expr | x++ | ++x | x-- | --x
28479 +, *, -, /, &, ^, |, <<, >>
28481 where x is an lvalue expression with scalar type.
28484 # pragma omp atomic new-line
28487 # pragma omp atomic read new-line
28490 # pragma omp atomic write new-line
28493 # pragma omp atomic update new-line
28496 # pragma omp atomic capture new-line
28499 # pragma omp atomic capture new-line
28507 expression-stmt | x = x binop expr
28509 v = expression-stmt
28511 { v = x; update-stmt; } | { update-stmt; v = x; }
28515 expression-stmt | x = x binop expr | x = expr binop x
28519 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28521 where x and v are lvalue expressions with scalar type. */
28524 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
28526 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
28527 tree rhs1
= NULL_TREE
, orig_lhs
;
28528 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
28529 bool structured_block
= false;
28530 bool seq_cst
= false;
28532 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28534 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28535 const char *p
= IDENTIFIER_POINTER (id
);
28537 if (!strcmp (p
, "read"))
28538 code
= OMP_ATOMIC_READ
;
28539 else if (!strcmp (p
, "write"))
28541 else if (!strcmp (p
, "update"))
28543 else if (!strcmp (p
, "capture"))
28544 code
= OMP_ATOMIC_CAPTURE_NEW
;
28548 cp_lexer_consume_token (parser
->lexer
);
28551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28553 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28554 const char *p
= IDENTIFIER_POINTER (id
);
28556 if (!strcmp (p
, "seq_cst"))
28559 cp_lexer_consume_token (parser
->lexer
);
28562 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28566 case OMP_ATOMIC_READ
:
28567 case NOP_EXPR
: /* atomic write */
28568 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28569 /*cast_p=*/false, NULL
);
28570 if (v
== error_mark_node
)
28572 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28574 if (code
== NOP_EXPR
)
28575 lhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28577 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28578 /*cast_p=*/false, NULL
);
28579 if (lhs
== error_mark_node
)
28581 if (code
== NOP_EXPR
)
28583 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28591 case OMP_ATOMIC_CAPTURE_NEW
:
28592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28594 cp_lexer_consume_token (parser
->lexer
);
28595 structured_block
= true;
28599 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28600 /*cast_p=*/false, NULL
);
28601 if (v
== error_mark_node
)
28603 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28611 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28612 /*cast_p=*/false, NULL
);
28614 switch (TREE_CODE (lhs
))
28619 case POSTINCREMENT_EXPR
:
28620 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
28621 code
= OMP_ATOMIC_CAPTURE_OLD
;
28623 case PREINCREMENT_EXPR
:
28624 lhs
= TREE_OPERAND (lhs
, 0);
28625 opcode
= PLUS_EXPR
;
28626 rhs
= integer_one_node
;
28629 case POSTDECREMENT_EXPR
:
28630 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
28631 code
= OMP_ATOMIC_CAPTURE_OLD
;
28633 case PREDECREMENT_EXPR
:
28634 lhs
= TREE_OPERAND (lhs
, 0);
28635 opcode
= MINUS_EXPR
;
28636 rhs
= integer_one_node
;
28639 case COMPOUND_EXPR
:
28640 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
28641 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
28642 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
28643 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
28644 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28645 (TREE_OPERAND (lhs
, 1), 0), 0)))
28647 /* Undo effects of boolean_increment for post {in,de}crement. */
28648 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
28651 if (TREE_CODE (lhs
) == MODIFY_EXPR
28652 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
28654 /* Undo effects of boolean_increment. */
28655 if (integer_onep (TREE_OPERAND (lhs
, 1)))
28657 /* This is pre or post increment. */
28658 rhs
= TREE_OPERAND (lhs
, 1);
28659 lhs
= TREE_OPERAND (lhs
, 0);
28661 if (code
== OMP_ATOMIC_CAPTURE_NEW
28662 && !structured_block
28663 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
28664 code
= OMP_ATOMIC_CAPTURE_OLD
;
28670 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28673 opcode
= MULT_EXPR
;
28676 opcode
= TRUNC_DIV_EXPR
;
28679 opcode
= PLUS_EXPR
;
28682 opcode
= MINUS_EXPR
;
28684 case CPP_LSHIFT_EQ
:
28685 opcode
= LSHIFT_EXPR
;
28687 case CPP_RSHIFT_EQ
:
28688 opcode
= RSHIFT_EXPR
;
28691 opcode
= BIT_AND_EXPR
;
28694 opcode
= BIT_IOR_EXPR
;
28697 opcode
= BIT_XOR_EXPR
;
28700 enum cp_parser_prec oprec
;
28702 cp_lexer_consume_token (parser
->lexer
);
28703 cp_parser_parse_tentatively (parser
);
28704 rhs1
= cp_parser_simple_cast_expression (parser
);
28705 if (rhs1
== error_mark_node
)
28707 cp_parser_abort_tentative_parse (parser
);
28708 cp_parser_simple_cast_expression (parser
);
28711 token
= cp_lexer_peek_token (parser
->lexer
);
28712 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
28714 cp_parser_abort_tentative_parse (parser
);
28715 cp_parser_parse_tentatively (parser
);
28716 rhs
= cp_parser_binary_expression (parser
, false, true,
28717 PREC_NOT_OPERATOR
, NULL
);
28718 if (rhs
== error_mark_node
)
28720 cp_parser_abort_tentative_parse (parser
);
28721 cp_parser_binary_expression (parser
, false, true,
28722 PREC_NOT_OPERATOR
, NULL
);
28725 switch (TREE_CODE (rhs
))
28728 case TRUNC_DIV_EXPR
:
28736 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
28738 if (cp_parser_parse_definitely (parser
))
28740 opcode
= TREE_CODE (rhs
);
28741 rhs1
= TREE_OPERAND (rhs
, 0);
28742 rhs
= TREE_OPERAND (rhs
, 1);
28752 cp_parser_abort_tentative_parse (parser
);
28753 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
28755 rhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28756 if (rhs
== error_mark_node
)
28762 cp_parser_error (parser
,
28763 "invalid form of %<#pragma omp atomic%>");
28766 if (!cp_parser_parse_definitely (parser
))
28768 switch (token
->type
)
28770 case CPP_SEMICOLON
:
28771 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
28773 code
= OMP_ATOMIC_CAPTURE_OLD
;
28778 cp_lexer_consume_token (parser
->lexer
);
28781 else if (structured_block
)
28788 cp_parser_error (parser
,
28789 "invalid form of %<#pragma omp atomic%>");
28792 opcode
= MULT_EXPR
;
28795 opcode
= TRUNC_DIV_EXPR
;
28798 opcode
= PLUS_EXPR
;
28801 opcode
= MINUS_EXPR
;
28804 opcode
= LSHIFT_EXPR
;
28807 opcode
= RSHIFT_EXPR
;
28810 opcode
= BIT_AND_EXPR
;
28813 opcode
= BIT_IOR_EXPR
;
28816 opcode
= BIT_XOR_EXPR
;
28819 cp_parser_error (parser
,
28820 "invalid operator for %<#pragma omp atomic%>");
28823 oprec
= TOKEN_PRECEDENCE (token
);
28824 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
28825 if (commutative_tree_code (opcode
))
28826 oprec
= (enum cp_parser_prec
) (oprec
- 1);
28827 cp_lexer_consume_token (parser
->lexer
);
28828 rhs
= cp_parser_binary_expression (parser
, false, false,
28830 if (rhs
== error_mark_node
)
28835 cp_parser_error (parser
,
28836 "invalid operator for %<#pragma omp atomic%>");
28839 cp_lexer_consume_token (parser
->lexer
);
28841 rhs
= cp_parser_expression (parser
, false, NULL
);
28842 if (rhs
== error_mark_node
)
28847 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
28849 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
28851 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28852 /*cast_p=*/false, NULL
);
28853 if (v
== error_mark_node
)
28855 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28857 lhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28858 /*cast_p=*/false, NULL
);
28859 if (lhs1
== error_mark_node
)
28862 if (structured_block
)
28864 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28865 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
28868 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
28869 if (!structured_block
)
28870 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28874 cp_parser_skip_to_end_of_block_or_statement (parser
);
28875 if (structured_block
)
28877 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28878 cp_lexer_consume_token (parser
->lexer
);
28879 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
28881 cp_parser_skip_to_end_of_block_or_statement (parser
);
28882 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28883 cp_lexer_consume_token (parser
->lexer
);
28890 # pragma omp barrier new-line */
28893 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
28895 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28896 finish_omp_barrier ();
28900 # pragma omp critical [(name)] new-line
28901 structured-block */
28904 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
28906 tree stmt
, name
= NULL
;
28908 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28910 cp_lexer_consume_token (parser
->lexer
);
28912 name
= cp_parser_identifier (parser
);
28914 if (name
== error_mark_node
28915 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28916 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28917 /*or_comma=*/false,
28918 /*consume_paren=*/true);
28919 if (name
== error_mark_node
)
28922 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28924 stmt
= cp_parser_omp_structured_block (parser
);
28925 return c_finish_omp_critical (input_location
, stmt
, name
);
28929 # pragma omp flush flush-vars[opt] new-line
28932 ( variable-list ) */
28935 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
28937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28938 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
28939 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28941 finish_omp_flush ();
28944 /* Helper function, to parse omp for increment expression. */
28947 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
28949 tree cond
= cp_parser_binary_expression (parser
, false, true,
28950 PREC_NOT_OPERATOR
, NULL
);
28951 if (cond
== error_mark_node
28952 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28954 cp_parser_skip_to_end_of_statement (parser
);
28955 return error_mark_node
;
28958 switch (TREE_CODE (cond
))
28966 if (code
== CILK_SIMD
)
28968 /* Fall through: OpenMP disallows NE_EXPR. */
28970 return error_mark_node
;
28973 /* If decl is an iterator, preserve LHS and RHS of the relational
28974 expr until finish_omp_for. */
28976 && (type_dependent_expression_p (decl
)
28977 || CLASS_TYPE_P (TREE_TYPE (decl
))))
28980 return build_x_binary_op (input_location
, TREE_CODE (cond
),
28981 TREE_OPERAND (cond
, 0), ERROR_MARK
,
28982 TREE_OPERAND (cond
, 1), ERROR_MARK
,
28983 /*overload=*/NULL
, tf_warning_or_error
);
28986 /* Helper function, to parse omp for increment expression. */
28989 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
28991 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28997 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
28999 op
= (token
->type
== CPP_PLUS_PLUS
29000 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
29001 cp_lexer_consume_token (parser
->lexer
);
29002 lhs
= cp_parser_simple_cast_expression (parser
);
29004 return error_mark_node
;
29005 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
29008 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
29010 return error_mark_node
;
29012 token
= cp_lexer_peek_token (parser
->lexer
);
29013 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
29015 op
= (token
->type
== CPP_PLUS_PLUS
29016 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
29017 cp_lexer_consume_token (parser
->lexer
);
29018 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
29021 op
= cp_parser_assignment_operator_opt (parser
);
29022 if (op
== ERROR_MARK
)
29023 return error_mark_node
;
29025 if (op
!= NOP_EXPR
)
29027 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
29028 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
29029 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
29032 lhs
= cp_parser_binary_expression (parser
, false, false,
29033 PREC_ADDITIVE_EXPRESSION
, NULL
);
29034 token
= cp_lexer_peek_token (parser
->lexer
);
29035 decl_first
= lhs
== decl
;
29038 if (token
->type
!= CPP_PLUS
29039 && token
->type
!= CPP_MINUS
)
29040 return error_mark_node
;
29044 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
29045 cp_lexer_consume_token (parser
->lexer
);
29046 rhs
= cp_parser_binary_expression (parser
, false, false,
29047 PREC_ADDITIVE_EXPRESSION
, NULL
);
29048 token
= cp_lexer_peek_token (parser
->lexer
);
29049 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
29051 if (lhs
== NULL_TREE
)
29053 if (op
== PLUS_EXPR
)
29056 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
29057 tf_warning_or_error
);
29060 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
29061 ERROR_MARK
, NULL
, tf_warning_or_error
);
29064 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
29068 if (rhs
!= decl
|| op
== MINUS_EXPR
)
29069 return error_mark_node
;
29070 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
29073 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
29075 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
29078 /* Parse the initialization statement of either an OpenMP for loop or
29079 a Cilk Plus for loop.
29081 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29084 Return true if the resulting construct should have an
29085 OMP_CLAUSE_PRIVATE added to it. */
29088 cp_parser_omp_for_loop_init (cp_parser
*parser
,
29089 bool parsing_openmp
,
29090 tree
&this_pre_body
,
29091 vec
<tree
, va_gc
> *for_block
,
29096 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29099 bool add_private_clause
= false;
29101 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29105 integer-type var = lb
29106 random-access-iterator-type var = lb
29107 pointer-type var = lb
29109 cp_decl_specifier_seq type_specifiers
;
29111 /* First, try to parse as an initialized declaration. See
29112 cp_parser_condition, from whence the bulk of this is copied. */
29114 cp_parser_parse_tentatively (parser
);
29115 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
29116 /*is_trailing_return=*/false,
29118 if (cp_parser_parse_definitely (parser
))
29120 /* If parsing a type specifier seq succeeded, then this
29121 MUST be a initialized declaration. */
29122 tree asm_specification
, attributes
;
29123 cp_declarator
*declarator
;
29125 declarator
= cp_parser_declarator (parser
,
29126 CP_PARSER_DECLARATOR_NAMED
,
29127 /*ctor_dtor_or_conv_p=*/NULL
,
29128 /*parenthesized_p=*/NULL
,
29129 /*member_p=*/false);
29130 attributes
= cp_parser_attributes_opt (parser
);
29131 asm_specification
= cp_parser_asm_specification_opt (parser
);
29133 if (declarator
== cp_error_declarator
)
29134 cp_parser_skip_to_end_of_statement (parser
);
29138 tree pushed_scope
, auto_node
;
29140 decl
= start_decl (declarator
, &type_specifiers
,
29141 SD_INITIALIZED
, attributes
,
29142 /*prefix_attributes=*/NULL_TREE
,
29145 auto_node
= type_uses_auto (TREE_TYPE (decl
));
29146 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
29148 if (cp_lexer_next_token_is (parser
->lexer
,
29151 if (parsing_openmp
)
29152 error ("parenthesized initialization is not allowed in "
29153 "OpenMP %<for%> loop");
29155 error ("parenthesized initialization is "
29156 "not allowed in for-loop");
29159 /* Trigger an error. */
29160 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
29162 init
= error_mark_node
;
29163 cp_parser_skip_to_end_of_statement (parser
);
29165 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
29166 || type_dependent_expression_p (decl
)
29169 bool is_direct_init
, is_non_constant_init
;
29171 init
= cp_parser_initializer (parser
,
29173 &is_non_constant_init
);
29178 = do_auto_deduction (TREE_TYPE (decl
), init
,
29181 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
29182 && !type_dependent_expression_p (decl
))
29186 cp_finish_decl (decl
, init
, !is_non_constant_init
,
29188 LOOKUP_ONLYCONVERTING
);
29189 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
29191 vec_safe_push (for_block
, this_pre_body
);
29195 init
= pop_stmt_list (this_pre_body
);
29196 this_pre_body
= NULL_TREE
;
29201 cp_lexer_consume_token (parser
->lexer
);
29202 init
= cp_parser_assignment_expression (parser
, false, NULL
);
29205 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
29206 init
= error_mark_node
;
29208 cp_finish_decl (decl
, NULL_TREE
,
29209 /*init_const_expr_p=*/false,
29211 LOOKUP_ONLYCONVERTING
);
29215 pop_scope (pushed_scope
);
29221 /* If parsing a type specifier sequence failed, then
29222 this MUST be a simple expression. */
29223 cp_parser_parse_tentatively (parser
);
29224 decl
= cp_parser_primary_expression (parser
, false, false,
29226 if (!cp_parser_error_occurred (parser
)
29229 && CLASS_TYPE_P (TREE_TYPE (decl
)))
29233 cp_parser_parse_definitely (parser
);
29234 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
29235 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
29236 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
29239 tf_warning_or_error
));
29240 add_private_clause
= true;
29245 cp_parser_abort_tentative_parse (parser
);
29246 init
= cp_parser_expression (parser
, false, NULL
);
29249 if (TREE_CODE (init
) == MODIFY_EXPR
29250 || TREE_CODE (init
) == MODOP_EXPR
)
29251 real_decl
= TREE_OPERAND (init
, 0);
29255 return add_private_clause
;
29258 /* Parse the restricted form of the for statement allowed by OpenMP. */
29261 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
29264 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
29265 tree real_decl
, initv
, condv
, incrv
, declv
;
29266 tree this_pre_body
, cl
;
29267 location_t loc_first
;
29268 bool collapse_err
= false;
29269 int i
, collapse
= 1, nbraces
= 0;
29270 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
29272 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
29273 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
29274 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
29276 gcc_assert (collapse
>= 1);
29278 declv
= make_tree_vec (collapse
);
29279 initv
= make_tree_vec (collapse
);
29280 condv
= make_tree_vec (collapse
);
29281 incrv
= make_tree_vec (collapse
);
29283 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
29285 for (i
= 0; i
< collapse
; i
++)
29287 int bracecount
= 0;
29288 bool add_private_clause
= false;
29291 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29293 cp_parser_error (parser
, "for statement expected");
29296 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
29298 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29301 init
= decl
= real_decl
= NULL
;
29302 this_pre_body
= push_stmt_list ();
29305 |= cp_parser_omp_for_loop_init (parser
,
29306 /*parsing_openmp=*/code
!= CILK_SIMD
,
29307 this_pre_body
, for_block
,
29308 init
, decl
, real_decl
);
29310 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29313 this_pre_body
= pop_stmt_list (this_pre_body
);
29317 pre_body
= push_stmt_list ();
29319 add_stmt (this_pre_body
);
29320 pre_body
= pop_stmt_list (pre_body
);
29323 pre_body
= this_pre_body
;
29328 if (cclauses
!= NULL
29329 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
29330 && real_decl
!= NULL_TREE
)
29333 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
29334 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
29335 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29337 error_at (loc
, "iteration variable %qD"
29338 " should not be firstprivate", real_decl
);
29339 *c
= OMP_CLAUSE_CHAIN (*c
);
29341 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
29342 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29344 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29345 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29346 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
29347 OMP_CLAUSE_DECL (l
) = real_decl
;
29348 OMP_CLAUSE_CHAIN (l
) = clauses
;
29349 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
29351 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
29352 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
29353 add_private_clause
= false;
29357 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
29358 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29359 add_private_clause
= false;
29360 c
= &OMP_CLAUSE_CHAIN (*c
);
29364 if (add_private_clause
)
29367 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
29369 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
29370 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
29371 && OMP_CLAUSE_DECL (c
) == decl
)
29373 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
29374 && OMP_CLAUSE_DECL (c
) == decl
)
29375 error_at (loc
, "iteration variable %qD "
29376 "should not be firstprivate",
29378 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
29379 && OMP_CLAUSE_DECL (c
) == decl
)
29380 error_at (loc
, "iteration variable %qD should not be reduction",
29385 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
29386 OMP_CLAUSE_DECL (c
) = decl
;
29387 c
= finish_omp_clauses (c
);
29390 OMP_CLAUSE_CHAIN (c
) = clauses
;
29397 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29398 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
29399 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29402 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29404 /* If decl is an iterator, preserve the operator on decl
29405 until finish_omp_for. */
29407 && ((processing_template_decl
29408 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
29409 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
29410 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
29412 incr
= cp_parser_expression (parser
, false, NULL
);
29413 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
29414 SET_EXPR_LOCATION (incr
, input_location
);
29417 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29418 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29419 /*or_comma=*/false,
29420 /*consume_paren=*/true);
29422 TREE_VEC_ELT (declv
, i
) = decl
;
29423 TREE_VEC_ELT (initv
, i
) = init
;
29424 TREE_VEC_ELT (condv
, i
) = cond
;
29425 TREE_VEC_ELT (incrv
, i
) = incr
;
29427 if (i
== collapse
- 1)
29430 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29431 in between the collapsed for loops to be still considered perfectly
29432 nested. Hopefully the final version clarifies this.
29433 For now handle (multiple) {'s and empty statements. */
29434 cp_parser_parse_tentatively (parser
);
29437 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29439 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29441 cp_lexer_consume_token (parser
->lexer
);
29444 else if (bracecount
29445 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29446 cp_lexer_consume_token (parser
->lexer
);
29449 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29450 error_at (loc
, "not enough collapsed for loops");
29451 collapse_err
= true;
29452 cp_parser_abort_tentative_parse (parser
);
29461 cp_parser_parse_definitely (parser
);
29462 nbraces
+= bracecount
;
29466 /* Note that we saved the original contents of this flag when we entered
29467 the structured block, and so we don't need to re-save it here. */
29468 if (code
== CILK_SIMD
)
29469 parser
->in_statement
= IN_CILK_SIMD_FOR
;
29471 parser
->in_statement
= IN_OMP_FOR
;
29473 /* Note that the grammar doesn't call for a structured block here,
29474 though the loop as a whole is a structured block. */
29475 body
= push_stmt_list ();
29476 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29477 body
= pop_stmt_list (body
);
29479 if (declv
== NULL_TREE
)
29482 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
29483 pre_body
, clauses
);
29487 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29489 cp_lexer_consume_token (parser
->lexer
);
29492 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29493 cp_lexer_consume_token (parser
->lexer
);
29498 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29499 "collapsed loops not perfectly nested");
29501 collapse_err
= true;
29502 cp_parser_statement_seq_opt (parser
, NULL
);
29503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
29508 while (!for_block
->is_empty ())
29509 add_stmt (pop_stmt_list (for_block
->pop ()));
29510 release_tree_vector (for_block
);
29515 /* Helper function for OpenMP parsing, split clauses and call
29516 finish_omp_clauses on each of the set of clauses afterwards. */
29519 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
29520 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
29523 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
29524 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
29526 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
29530 #pragma omp simd simd-clause[optseq] new-line
29533 #define OMP_SIMD_CLAUSE_MASK \
29534 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29543 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
29544 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29546 tree clauses
, sb
, ret
;
29548 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29550 strcat (p_name
, " simd");
29551 mask
|= OMP_SIMD_CLAUSE_MASK
;
29552 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
29554 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29558 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
29559 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
29562 sb
= begin_omp_structured_block ();
29563 save
= cp_parser_begin_omp_structured_block (parser
);
29565 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
29567 cp_parser_end_omp_structured_block (parser
, save
);
29568 add_stmt (finish_omp_structured_block (sb
));
29574 #pragma omp for for-clause[optseq] new-line
29578 #pragma omp for simd for-simd-clause[optseq] new-line
29581 #define OMP_FOR_CLAUSE_MASK \
29582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29592 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
29593 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29595 tree clauses
, sb
, ret
;
29597 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29599 strcat (p_name
, " for");
29600 mask
|= OMP_FOR_CLAUSE_MASK
;
29602 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
29604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29606 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29607 const char *p
= IDENTIFIER_POINTER (id
);
29609 if (strcmp (p
, "simd") == 0)
29611 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29612 if (cclauses
== NULL
)
29613 cclauses
= cclauses_buf
;
29615 cp_lexer_consume_token (parser
->lexer
);
29616 if (!flag_openmp
) /* flag_openmp_simd */
29617 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29619 sb
= begin_omp_structured_block ();
29620 save
= cp_parser_begin_omp_structured_block (parser
);
29621 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29623 cp_parser_end_omp_structured_block (parser
, save
);
29624 tree body
= finish_omp_structured_block (sb
);
29627 ret
= make_node (OMP_FOR
);
29628 TREE_TYPE (ret
) = void_type_node
;
29629 OMP_FOR_BODY (ret
) = body
;
29630 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29631 SET_EXPR_LOCATION (ret
, loc
);
29636 if (!flag_openmp
) /* flag_openmp_simd */
29638 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29642 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29646 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
29647 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29650 sb
= begin_omp_structured_block ();
29651 save
= cp_parser_begin_omp_structured_block (parser
);
29653 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
29655 cp_parser_end_omp_structured_block (parser
, save
);
29656 add_stmt (finish_omp_structured_block (sb
));
29662 # pragma omp master new-line
29663 structured-block */
29666 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
29668 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29669 return c_finish_omp_master (input_location
,
29670 cp_parser_omp_structured_block (parser
));
29674 # pragma omp ordered new-line
29675 structured-block */
29678 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
29680 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29681 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29682 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
29688 { section-sequence }
29691 section-directive[opt] structured-block
29692 section-sequence section-directive structured-block */
29695 cp_parser_omp_sections_scope (cp_parser
*parser
)
29697 tree stmt
, substmt
;
29698 bool error_suppress
= false;
29701 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
29704 stmt
= push_stmt_list ();
29706 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
29708 substmt
= cp_parser_omp_structured_block (parser
);
29709 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
29710 add_stmt (substmt
);
29715 tok
= cp_lexer_peek_token (parser
->lexer
);
29716 if (tok
->type
== CPP_CLOSE_BRACE
)
29718 if (tok
->type
== CPP_EOF
)
29721 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
29723 cp_lexer_consume_token (parser
->lexer
);
29724 cp_parser_require_pragma_eol (parser
, tok
);
29725 error_suppress
= false;
29727 else if (!error_suppress
)
29729 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
29730 error_suppress
= true;
29733 substmt
= cp_parser_omp_structured_block (parser
);
29734 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
29735 add_stmt (substmt
);
29737 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29739 substmt
= pop_stmt_list (stmt
);
29741 stmt
= make_node (OMP_SECTIONS
);
29742 TREE_TYPE (stmt
) = void_type_node
;
29743 OMP_SECTIONS_BODY (stmt
) = substmt
;
29750 # pragma omp sections sections-clause[optseq] newline
29753 #define OMP_SECTIONS_CLAUSE_MASK \
29754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29761 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
29762 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29765 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29767 strcat (p_name
, " sections");
29768 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
29770 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
29772 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29776 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
29777 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
29780 ret
= cp_parser_omp_sections_scope (parser
);
29782 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
29788 # pragma omp parallel parallel-clause[optseq] new-line
29790 # pragma omp parallel for parallel-for-clause[optseq] new-line
29792 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29796 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29797 structured-block */
29799 #define OMP_PARALLEL_CLAUSE_MASK \
29800 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29811 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
29812 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29814 tree stmt
, clauses
, block
;
29816 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29818 strcat (p_name
, " parallel");
29819 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
29821 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29823 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29824 if (cclauses
== NULL
)
29825 cclauses
= cclauses_buf
;
29827 cp_lexer_consume_token (parser
->lexer
);
29828 if (!flag_openmp
) /* flag_openmp_simd */
29829 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29830 block
= begin_omp_parallel ();
29831 save
= cp_parser_begin_omp_structured_block (parser
);
29832 cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29833 cp_parser_end_omp_structured_block (parser
, save
);
29834 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
29836 OMP_PARALLEL_COMBINED (stmt
) = 1;
29841 error_at (loc
, "expected %<for%> after %qs", p_name
);
29842 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29845 else if (!flag_openmp
) /* flag_openmp_simd */
29847 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29850 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29852 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29853 const char *p
= IDENTIFIER_POINTER (id
);
29854 if (strcmp (p
, "sections") == 0)
29856 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29857 cclauses
= cclauses_buf
;
29859 cp_lexer_consume_token (parser
->lexer
);
29860 block
= begin_omp_parallel ();
29861 save
= cp_parser_begin_omp_structured_block (parser
);
29862 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29863 cp_parser_end_omp_structured_block (parser
, save
);
29864 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
29866 OMP_PARALLEL_COMBINED (stmt
) = 1;
29871 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
29873 block
= begin_omp_parallel ();
29874 save
= cp_parser_begin_omp_structured_block (parser
);
29875 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29876 cp_parser_end_omp_structured_block (parser
, save
);
29877 stmt
= finish_omp_parallel (clauses
, block
);
29882 # pragma omp single single-clause[optseq] new-line
29883 structured-block */
29885 #define OMP_SINGLE_CLAUSE_MASK \
29886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29892 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
29894 tree stmt
= make_node (OMP_SINGLE
);
29895 TREE_TYPE (stmt
) = void_type_node
;
29897 OMP_SINGLE_CLAUSES (stmt
)
29898 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
29899 "#pragma omp single", pragma_tok
);
29900 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29902 return add_stmt (stmt
);
29906 # pragma omp task task-clause[optseq] new-line
29907 structured-block */
29909 #define OMP_TASK_CLAUSE_MASK \
29910 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29921 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
29923 tree clauses
, block
;
29926 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
29927 "#pragma omp task", pragma_tok
);
29928 block
= begin_omp_task ();
29929 save
= cp_parser_begin_omp_structured_block (parser
);
29930 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29931 cp_parser_end_omp_structured_block (parser
, save
);
29932 return finish_omp_task (clauses
, block
);
29936 # pragma omp taskwait new-line */
29939 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
29941 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29942 finish_omp_taskwait ();
29946 # pragma omp taskyield new-line */
29949 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
29951 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29952 finish_omp_taskyield ();
29956 # pragma omp taskgroup new-line
29957 structured-block */
29960 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
29962 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29963 return c_finish_omp_taskgroup (input_location
,
29964 cp_parser_omp_structured_block (parser
));
29969 # pragma omp threadprivate (variable-list) */
29972 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
29976 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29977 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29979 finish_omp_threadprivate (vars
);
29983 # pragma omp cancel cancel-clause[optseq] new-line */
29985 #define OMP_CANCEL_CLAUSE_MASK \
29986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29993 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
29995 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
29996 "#pragma omp cancel", pragma_tok
);
29997 finish_omp_cancel (clauses
);
30001 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30003 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30010 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
30013 bool point_seen
= false;
30015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30017 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30018 const char *p
= IDENTIFIER_POINTER (id
);
30020 if (strcmp (p
, "point") == 0)
30022 cp_lexer_consume_token (parser
->lexer
);
30028 cp_parser_error (parser
, "expected %<point%>");
30029 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30033 clauses
= cp_parser_omp_all_clauses (parser
,
30034 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
30035 "#pragma omp cancellation point",
30037 finish_omp_cancellation_point (clauses
);
30041 #pragma omp distribute distribute-clause[optseq] new-line
30044 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30045 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30051 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
30052 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30054 tree clauses
, sb
, ret
;
30056 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30058 strcat (p_name
, " distribute");
30059 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
30061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30063 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30064 const char *p
= IDENTIFIER_POINTER (id
);
30066 bool parallel
= false;
30068 if (strcmp (p
, "simd") == 0)
30071 parallel
= strcmp (p
, "parallel") == 0;
30072 if (parallel
|| simd
)
30074 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30075 if (cclauses
== NULL
)
30076 cclauses
= cclauses_buf
;
30077 cp_lexer_consume_token (parser
->lexer
);
30078 if (!flag_openmp
) /* flag_openmp_simd */
30081 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30084 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
30087 sb
= begin_omp_structured_block ();
30088 save
= cp_parser_begin_omp_structured_block (parser
);
30090 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30093 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
30095 cp_parser_end_omp_structured_block (parser
, save
);
30096 tree body
= finish_omp_structured_block (sb
);
30099 ret
= make_node (OMP_DISTRIBUTE
);
30100 TREE_TYPE (ret
) = void_type_node
;
30101 OMP_FOR_BODY (ret
) = body
;
30102 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
30103 SET_EXPR_LOCATION (ret
, loc
);
30108 if (!flag_openmp
) /* flag_openmp_simd */
30110 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30114 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30118 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
30119 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
30122 sb
= begin_omp_structured_block ();
30123 save
= cp_parser_begin_omp_structured_block (parser
);
30125 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
30127 cp_parser_end_omp_structured_block (parser
, save
);
30128 add_stmt (finish_omp_structured_block (sb
));
30134 # pragma omp teams teams-clause[optseq] new-line
30135 structured-block */
30137 #define OMP_TEAMS_CLAUSE_MASK \
30138 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30147 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
30148 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30150 tree clauses
, sb
, ret
;
30152 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30154 strcat (p_name
, " teams");
30155 mask
|= OMP_TEAMS_CLAUSE_MASK
;
30157 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30159 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30160 const char *p
= IDENTIFIER_POINTER (id
);
30161 if (strcmp (p
, "distribute") == 0)
30163 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30164 if (cclauses
== NULL
)
30165 cclauses
= cclauses_buf
;
30167 cp_lexer_consume_token (parser
->lexer
);
30168 if (!flag_openmp
) /* flag_openmp_simd */
30169 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
30171 sb
= begin_omp_structured_block ();
30172 save
= cp_parser_begin_omp_structured_block (parser
);
30173 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
30175 cp_parser_end_omp_structured_block (parser
, save
);
30176 tree body
= finish_omp_structured_block (sb
);
30179 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
30180 ret
= make_node (OMP_TEAMS
);
30181 TREE_TYPE (ret
) = void_type_node
;
30182 OMP_TEAMS_CLAUSES (ret
) = clauses
;
30183 OMP_TEAMS_BODY (ret
) = body
;
30184 return add_stmt (ret
);
30187 if (!flag_openmp
) /* flag_openmp_simd */
30189 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30193 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30197 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
30198 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
30201 tree stmt
= make_node (OMP_TEAMS
);
30202 TREE_TYPE (stmt
) = void_type_node
;
30203 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
30204 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30206 return add_stmt (stmt
);
30210 # pragma omp target data target-data-clause[optseq] new-line
30211 structured-block */
30213 #define OMP_TARGET_DATA_CLAUSE_MASK \
30214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30219 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
30221 tree stmt
= make_node (OMP_TARGET_DATA
);
30222 TREE_TYPE (stmt
) = void_type_node
;
30224 OMP_TARGET_DATA_CLAUSES (stmt
)
30225 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
30226 "#pragma omp target data", pragma_tok
);
30227 keep_next_level (true);
30228 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30230 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30231 return add_stmt (stmt
);
30235 # pragma omp target update target-update-clause[optseq] new-line */
30237 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30238 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30244 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
30245 enum pragma_context context
)
30247 if (context
== pragma_stmt
)
30249 error_at (pragma_tok
->location
,
30250 "%<#pragma omp target update%> may only be "
30251 "used in compound statements");
30252 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30257 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
30258 "#pragma omp target update", pragma_tok
);
30259 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
30260 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
30262 error_at (pragma_tok
->location
,
30263 "%<#pragma omp target update must contain at least one "
30264 "%<from%> or %<to%> clauses");
30268 tree stmt
= make_node (OMP_TARGET_UPDATE
);
30269 TREE_TYPE (stmt
) = void_type_node
;
30270 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
30271 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30277 # pragma omp target target-clause[optseq] new-line
30278 structured-block */
30280 #define OMP_TARGET_CLAUSE_MASK \
30281 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30286 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
30287 enum pragma_context context
)
30289 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
30291 cp_parser_error (parser
, "expected declaration specifiers");
30292 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30298 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30299 const char *p
= IDENTIFIER_POINTER (id
);
30301 if (strcmp (p
, "teams") == 0)
30303 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
30304 char p_name
[sizeof ("#pragma omp target teams distribute "
30305 "parallel for simd")];
30307 cp_lexer_consume_token (parser
->lexer
);
30308 strcpy (p_name
, "#pragma omp target");
30309 if (!flag_openmp
) /* flag_openmp_simd */
30310 return cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
30311 OMP_TARGET_CLAUSE_MASK
, cclauses
);
30312 keep_next_level (true);
30313 tree sb
= begin_omp_structured_block ();
30314 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
30315 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
30316 OMP_TARGET_CLAUSE_MASK
, cclauses
);
30317 cp_parser_end_omp_structured_block (parser
, save
);
30318 tree body
= finish_omp_structured_block (sb
);
30321 tree stmt
= make_node (OMP_TARGET
);
30322 TREE_TYPE (stmt
) = void_type_node
;
30323 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
30324 OMP_TARGET_BODY (stmt
) = body
;
30328 else if (!flag_openmp
) /* flag_openmp_simd */
30330 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30333 else if (strcmp (p
, "data") == 0)
30335 cp_lexer_consume_token (parser
->lexer
);
30336 cp_parser_omp_target_data (parser
, pragma_tok
);
30339 else if (strcmp (p
, "update") == 0)
30341 cp_lexer_consume_token (parser
->lexer
);
30342 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
30346 tree stmt
= make_node (OMP_TARGET
);
30347 TREE_TYPE (stmt
) = void_type_node
;
30349 OMP_TARGET_CLAUSES (stmt
)
30350 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
30351 "#pragma omp target", pragma_tok
);
30352 keep_next_level (true);
30353 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30355 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30361 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30363 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30372 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30373 enum pragma_context context
)
30375 bool first_p
= parser
->omp_declare_simd
== NULL
;
30376 cp_omp_declare_simd_data data
;
30379 data
.error_seen
= false;
30380 data
.fndecl_seen
= false;
30381 data
.tokens
= vNULL
;
30382 parser
->omp_declare_simd
= &data
;
30384 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30385 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30386 cp_lexer_consume_token (parser
->lexer
);
30387 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30388 parser
->omp_declare_simd
->error_seen
= true;
30389 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30390 struct cp_token_cache
*cp
30391 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
30392 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
30395 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
30396 cp_parser_pragma (parser
, context
);
30399 case pragma_external
:
30400 cp_parser_declaration (parser
);
30402 case pragma_member
:
30403 cp_parser_member_declaration (parser
);
30405 case pragma_objc_icode
:
30406 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30409 cp_parser_declaration_statement (parser
);
30412 if (parser
->omp_declare_simd
30413 && !parser
->omp_declare_simd
->error_seen
30414 && !parser
->omp_declare_simd
->fndecl_seen
)
30415 error_at (pragma_tok
->location
,
30416 "%<#pragma omp declare simd%> not immediately followed by "
30417 "function declaration or definition");
30418 data
.tokens
.release ();
30419 parser
->omp_declare_simd
= NULL
;
30423 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30424 This function is modelled similar to the late parsing of omp declare
30428 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
30430 struct cp_token_cache
*ce
;
30431 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
30434 if (parser
->omp_declare_simd
!= NULL
)
30436 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30437 " marked as a Cilk Plus SIMD-enabled function");
30438 XDELETE (parser
->cilk_simd_fn_info
);
30439 parser
->cilk_simd_fn_info
= NULL
;
30442 if (!info
->error_seen
&& info
->fndecl_seen
)
30444 error ("vector attribute not immediately followed by a single function"
30445 " declaration or definition");
30446 info
->error_seen
= true;
30448 if (info
->error_seen
)
30451 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
30455 cp_parser_push_lexer_for_tokens (parser
, ce
);
30456 parser
->lexer
->in_pragma
= true;
30457 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
30458 "SIMD-enabled functions attribute",
30460 cp_parser_pop_lexer (parser
);
30462 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
30464 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
30465 TREE_CHAIN (c
) = attrs
;
30468 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
30469 TREE_CHAIN (c
) = attrs
;
30470 if (processing_template_decl
)
30471 ATTR_IS_DEPENDENT (c
) = 1;
30474 info
->fndecl_seen
= true;
30475 XDELETE (parser
->cilk_simd_fn_info
);
30476 parser
->cilk_simd_fn_info
= NULL
;
30480 /* Finalize #pragma omp declare simd clauses after direct declarator has
30481 been parsed, and put that into "omp declare simd" attribute. */
30484 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
30486 struct cp_token_cache
*ce
;
30487 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
30490 if (!data
->error_seen
&& data
->fndecl_seen
)
30492 error ("%<#pragma omp declare simd%> not immediately followed by "
30493 "a single function declaration or definition");
30494 data
->error_seen
= true;
30497 if (data
->error_seen
)
30500 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
30504 cp_parser_push_lexer_for_tokens (parser
, ce
);
30505 parser
->lexer
->in_pragma
= true;
30506 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
30507 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
30508 cp_lexer_consume_token (parser
->lexer
);
30509 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
30510 "#pragma omp declare simd", pragma_tok
);
30511 cp_parser_pop_lexer (parser
);
30513 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
30514 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
30515 TREE_CHAIN (c
) = attrs
;
30516 if (processing_template_decl
)
30517 ATTR_IS_DEPENDENT (c
) = 1;
30521 data
->fndecl_seen
= true;
30527 # pragma omp declare target new-line
30528 declarations and definitions
30529 # pragma omp end declare target new-line */
30532 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
30534 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30535 scope_chain
->omp_declare_target_attribute
++;
30539 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
30541 const char *p
= "";
30542 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30544 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30545 p
= IDENTIFIER_POINTER (id
);
30547 if (strcmp (p
, "declare") == 0)
30549 cp_lexer_consume_token (parser
->lexer
);
30551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30553 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30554 p
= IDENTIFIER_POINTER (id
);
30556 if (strcmp (p
, "target") == 0)
30557 cp_lexer_consume_token (parser
->lexer
);
30560 cp_parser_error (parser
, "expected %<target%>");
30561 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30567 cp_parser_error (parser
, "expected %<declare%>");
30568 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30571 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30572 if (!scope_chain
->omp_declare_target_attribute
)
30573 error_at (pragma_tok
->location
,
30574 "%<#pragma omp end declare target%> without corresponding "
30575 "%<#pragma omp declare target%>");
30577 scope_chain
->omp_declare_target_attribute
--;
30580 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30581 expression and optional initializer clause of
30582 #pragma omp declare reduction. We store the expression(s) as
30583 either 3, 6 or 7 special statements inside of the artificial function's
30584 body. The first two statements are DECL_EXPRs for the artificial
30585 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30586 expression that uses those variables.
30587 If there was any INITIALIZER clause, this is followed by further statements,
30588 the fourth and fifth statements are DECL_EXPRs for the artificial
30589 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30590 constructor variant (first token after open paren is not omp_priv),
30591 then the sixth statement is a statement with the function call expression
30592 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30593 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30594 to initialize the OMP_PRIV artificial variable and there is seventh
30595 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30598 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
30600 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
30601 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
30602 type
= TREE_TYPE (type
);
30603 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
30604 DECL_ARTIFICIAL (omp_out
) = 1;
30605 pushdecl (omp_out
);
30606 add_decl_expr (omp_out
);
30607 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
30608 DECL_ARTIFICIAL (omp_in
) = 1;
30610 add_decl_expr (omp_in
);
30612 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
30614 keep_next_level (true);
30615 tree block
= begin_omp_structured_block ();
30616 combiner
= cp_parser_expression (parser
, false, NULL
);
30617 finish_expr_stmt (combiner
);
30618 block
= finish_omp_structured_block (block
);
30621 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30624 const char *p
= "";
30625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30627 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30628 p
= IDENTIFIER_POINTER (id
);
30631 if (strcmp (p
, "initializer") == 0)
30633 cp_lexer_consume_token (parser
->lexer
);
30634 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30640 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30641 p
= IDENTIFIER_POINTER (id
);
30644 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
30645 DECL_ARTIFICIAL (omp_priv
) = 1;
30646 pushdecl (omp_priv
);
30647 add_decl_expr (omp_priv
);
30648 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
30649 DECL_ARTIFICIAL (omp_orig
) = 1;
30650 pushdecl (omp_orig
);
30651 add_decl_expr (omp_orig
);
30653 keep_next_level (true);
30654 block
= begin_omp_structured_block ();
30657 if (strcmp (p
, "omp_priv") == 0)
30659 bool is_direct_init
, is_non_constant_init
;
30661 cp_lexer_consume_token (parser
->lexer
);
30662 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
30664 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
30665 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30667 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
30668 == CPP_CLOSE_PAREN
))
30670 finish_omp_structured_block (block
);
30671 error ("invalid initializer clause");
30674 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
30675 &is_non_constant_init
);
30676 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
30677 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
30681 cp_parser_parse_tentatively (parser
);
30682 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
30683 /*check_dependency_p=*/true,
30684 /*template_p=*/NULL
,
30685 /*declarator_p=*/false,
30686 /*optional_p=*/false);
30687 vec
<tree
, va_gc
> *args
;
30688 if (fn_name
== error_mark_node
30689 || cp_parser_error_occurred (parser
)
30690 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
30691 || ((args
= cp_parser_parenthesized_expression_list
30692 (parser
, non_attr
, /*cast_p=*/false,
30693 /*allow_expansion_p=*/true,
30694 /*non_constant_p=*/NULL
)),
30695 cp_parser_error_occurred (parser
)))
30697 finish_omp_structured_block (block
);
30698 cp_parser_abort_tentative_parse (parser
);
30699 cp_parser_error (parser
, "expected id-expression (arguments)");
30704 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
30705 if (arg
== omp_priv
30706 || (TREE_CODE (arg
) == ADDR_EXPR
30707 && TREE_OPERAND (arg
, 0) == omp_priv
))
30709 cp_parser_abort_tentative_parse (parser
);
30710 if (arg
== NULL_TREE
)
30711 error ("one of the initializer call arguments should be %<omp_priv%>"
30712 " or %<&omp_priv%>");
30713 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
30715 finish_expr_stmt (initializer
);
30718 block
= finish_omp_structured_block (block
);
30719 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
30720 finish_expr_stmt (block
);
30723 add_decl_expr (omp_orig
);
30725 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30729 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
30730 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
30736 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30737 initializer-clause[opt] new-line
30739 initializer-clause:
30740 initializer (omp_priv initializer)
30741 initializer (function-name (argument-list)) */
30744 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
30745 enum pragma_context
)
30747 auto_vec
<tree
> types
;
30748 enum tree_code reduc_code
= ERROR_MARK
;
30749 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
30751 cp_token
*first_token
;
30752 cp_token_cache
*cp
;
30756 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
30757 p
= obstack_alloc (&declarator_obstack
, 0);
30759 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30762 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
30765 reduc_code
= PLUS_EXPR
;
30768 reduc_code
= MULT_EXPR
;
30771 reduc_code
= MINUS_EXPR
;
30774 reduc_code
= BIT_AND_EXPR
;
30777 reduc_code
= BIT_XOR_EXPR
;
30780 reduc_code
= BIT_IOR_EXPR
;
30783 reduc_code
= TRUTH_ANDIF_EXPR
;
30786 reduc_code
= TRUTH_ORIF_EXPR
;
30789 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
30792 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30793 "%<|%>, %<&&%>, %<||%> or identifier");
30797 if (reduc_code
!= ERROR_MARK
)
30798 cp_lexer_consume_token (parser
->lexer
);
30800 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
30801 if (reduc_id
== error_mark_node
)
30804 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30807 /* Types may not be defined in declare reduction type list. */
30808 const char *saved_message
;
30809 saved_message
= parser
->type_definition_forbidden_message
;
30810 parser
->type_definition_forbidden_message
30811 = G_("types may not be defined in declare reduction type list");
30812 bool saved_colon_corrects_to_scope_p
;
30813 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30814 parser
->colon_corrects_to_scope_p
= false;
30815 bool saved_colon_doesnt_start_class_def_p
;
30816 saved_colon_doesnt_start_class_def_p
30817 = parser
->colon_doesnt_start_class_def_p
;
30818 parser
->colon_doesnt_start_class_def_p
= true;
30822 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30823 type
= cp_parser_type_id (parser
);
30824 if (type
== error_mark_node
)
30826 else if (ARITHMETIC_TYPE_P (type
)
30827 && (orig_reduc_id
== NULL_TREE
30828 || (TREE_CODE (type
) != COMPLEX_TYPE
30829 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
30831 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
30833 error_at (loc
, "predeclared arithmetic type %qT in "
30834 "%<#pragma omp declare reduction%>", type
);
30835 else if (TREE_CODE (type
) == FUNCTION_TYPE
30836 || TREE_CODE (type
) == METHOD_TYPE
30837 || TREE_CODE (type
) == ARRAY_TYPE
)
30838 error_at (loc
, "function or array type %qT in "
30839 "%<#pragma omp declare reduction%>", type
);
30840 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
30841 error_at (loc
, "reference type %qT in "
30842 "%<#pragma omp declare reduction%>", type
);
30843 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
30844 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
30845 "%<#pragma omp declare reduction%>", type
);
30847 types
.safe_push (type
);
30849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30850 cp_lexer_consume_token (parser
->lexer
);
30855 /* Restore the saved message. */
30856 parser
->type_definition_forbidden_message
= saved_message
;
30857 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30858 parser
->colon_doesnt_start_class_def_p
30859 = saved_colon_doesnt_start_class_def_p
;
30861 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
30862 || types
.is_empty ())
30865 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30869 first_token
= cp_lexer_peek_token (parser
->lexer
);
30872 FOR_EACH_VEC_ELT (types
, i
, type
)
30875 = build_function_type_list (void_type_node
,
30876 cp_build_reference_type (type
, false),
30878 tree this_reduc_id
= reduc_id
;
30879 if (!dependent_type_p (type
))
30880 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
30881 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
30882 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
30883 DECL_ARTIFICIAL (fndecl
) = 1;
30884 DECL_EXTERNAL (fndecl
) = 1;
30885 DECL_DECLARED_INLINE_P (fndecl
) = 1;
30886 DECL_IGNORED_P (fndecl
) = 1;
30887 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
30888 DECL_ATTRIBUTES (fndecl
)
30889 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
30890 DECL_ATTRIBUTES (fndecl
));
30891 if (processing_template_decl
)
30892 fndecl
= push_template_decl (fndecl
);
30893 bool block_scope
= false;
30894 tree block
= NULL_TREE
;
30895 if (current_function_decl
)
30897 block_scope
= true;
30898 DECL_CONTEXT (fndecl
) = global_namespace
;
30899 if (!processing_template_decl
)
30902 else if (current_class_type
)
30906 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30907 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30908 cp_lexer_consume_token (parser
->lexer
);
30909 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30911 cp
= cp_token_cache_new (first_token
,
30912 cp_lexer_peek_nth_token (parser
->lexer
,
30915 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
30916 finish_member_declaration (fndecl
);
30917 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
30918 DECL_PENDING_INLINE_P (fndecl
) = 1;
30919 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
30924 DECL_CONTEXT (fndecl
) = current_namespace
;
30928 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
30930 block
= begin_omp_structured_block ();
30933 cp_parser_push_lexer_for_tokens (parser
, cp
);
30934 parser
->lexer
->in_pragma
= true;
30936 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
30939 finish_function (0);
30941 DECL_CONTEXT (fndecl
) = current_function_decl
;
30943 cp_parser_pop_lexer (parser
);
30947 cp_parser_pop_lexer (parser
);
30949 finish_function (0);
30952 DECL_CONTEXT (fndecl
) = current_function_decl
;
30953 block
= finish_omp_structured_block (block
);
30954 if (TREE_CODE (block
) == BIND_EXPR
)
30955 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
30956 else if (TREE_CODE (block
) == STATEMENT_LIST
)
30957 DECL_SAVED_TREE (fndecl
) = block
;
30958 if (processing_template_decl
)
30959 add_decl_expr (fndecl
);
30961 cp_check_omp_declare_reduction (fndecl
);
30962 if (cp
== NULL
&& types
.length () > 1)
30963 cp
= cp_token_cache_new (first_token
,
30964 cp_lexer_peek_nth_token (parser
->lexer
, 2));
30965 if (errs
!= errorcount
)
30969 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30972 /* Free any declarators allocated. */
30973 obstack_free (&declarator_obstack
, p
);
30977 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30978 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30979 initializer-clause[opt] new-line
30980 #pragma omp declare target new-line */
30983 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
30984 enum pragma_context context
)
30986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30988 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30989 const char *p
= IDENTIFIER_POINTER (id
);
30991 if (strcmp (p
, "simd") == 0)
30993 cp_lexer_consume_token (parser
->lexer
);
30994 cp_parser_omp_declare_simd (parser
, pragma_tok
,
30998 cp_ensure_no_omp_declare_simd (parser
);
30999 if (strcmp (p
, "reduction") == 0)
31001 cp_lexer_consume_token (parser
->lexer
);
31002 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
31006 if (!flag_openmp
) /* flag_openmp_simd */
31008 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31011 if (strcmp (p
, "target") == 0)
31013 cp_lexer_consume_token (parser
->lexer
);
31014 cp_parser_omp_declare_target (parser
, pragma_tok
);
31018 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
31020 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31023 /* Main entry point to OpenMP statement pragmas. */
31026 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
31029 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
31030 omp_clause_mask
mask (0);
31032 switch (pragma_tok
->pragma_kind
)
31034 case PRAGMA_OMP_ATOMIC
:
31035 cp_parser_omp_atomic (parser
, pragma_tok
);
31037 case PRAGMA_OMP_CRITICAL
:
31038 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
31040 case PRAGMA_OMP_DISTRIBUTE
:
31041 strcpy (p_name
, "#pragma omp");
31042 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
31044 case PRAGMA_OMP_FOR
:
31045 strcpy (p_name
, "#pragma omp");
31046 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
31048 case PRAGMA_OMP_MASTER
:
31049 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
31051 case PRAGMA_OMP_ORDERED
:
31052 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
31054 case PRAGMA_OMP_PARALLEL
:
31055 strcpy (p_name
, "#pragma omp");
31056 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
31058 case PRAGMA_OMP_SECTIONS
:
31059 strcpy (p_name
, "#pragma omp");
31060 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
31062 case PRAGMA_OMP_SIMD
:
31063 strcpy (p_name
, "#pragma omp");
31064 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
31066 case PRAGMA_OMP_SINGLE
:
31067 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
31069 case PRAGMA_OMP_TASK
:
31070 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
31072 case PRAGMA_OMP_TASKGROUP
:
31073 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
31075 case PRAGMA_OMP_TEAMS
:
31076 strcpy (p_name
, "#pragma omp");
31077 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
31080 gcc_unreachable ();
31084 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31087 /* Transactional Memory parsing routines. */
31089 /* Parse a transaction attribute.
31095 ??? Simplify this when C++0x bracket attributes are
31096 implemented properly. */
31099 cp_parser_txn_attribute_opt (cp_parser
*parser
)
31102 tree attr_name
, attr
= NULL
;
31104 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
31105 return cp_parser_attributes_opt (parser
);
31107 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
31109 cp_lexer_consume_token (parser
->lexer
);
31110 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
31113 token
= cp_lexer_peek_token (parser
->lexer
);
31114 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
31116 token
= cp_lexer_consume_token (parser
->lexer
);
31118 attr_name
= (token
->type
== CPP_KEYWORD
31119 /* For keywords, use the canonical spelling,
31120 not the parsed identifier. */
31121 ? ridpointers
[(int) token
->keyword
]
31123 attr
= build_tree_list (attr_name
, NULL_TREE
);
31126 cp_parser_error (parser
, "expected identifier");
31128 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31130 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31134 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31136 transaction-statement:
31137 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31139 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31143 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
31145 unsigned char old_in
= parser
->in_transaction
;
31146 unsigned char this_in
= 1, new_in
;
31148 tree stmt
, attrs
, noex
;
31150 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31151 || keyword
== RID_TRANSACTION_RELAXED
);
31152 token
= cp_parser_require_keyword (parser
, keyword
,
31153 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31154 : RT_TRANSACTION_RELAXED
));
31155 gcc_assert (token
!= NULL
);
31157 if (keyword
== RID_TRANSACTION_RELAXED
)
31158 this_in
|= TM_STMT_ATTR_RELAXED
;
31161 attrs
= cp_parser_txn_attribute_opt (parser
);
31163 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
31166 /* Parse a noexcept specification. */
31167 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
31169 /* Keep track if we're in the lexical scope of an outer transaction. */
31170 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
31172 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
31174 parser
->in_transaction
= new_in
;
31175 cp_parser_compound_statement (parser
, NULL
, false, false);
31176 parser
->in_transaction
= old_in
;
31178 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
31183 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31185 transaction-expression:
31186 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31187 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31191 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
31193 unsigned char old_in
= parser
->in_transaction
;
31194 unsigned char this_in
= 1;
31199 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31200 || keyword
== RID_TRANSACTION_RELAXED
);
31203 error (keyword
== RID_TRANSACTION_RELAXED
31204 ? G_("%<__transaction_relaxed%> without transactional memory "
31206 : G_("%<__transaction_atomic%> without transactional memory "
31207 "support enabled"));
31209 token
= cp_parser_require_keyword (parser
, keyword
,
31210 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31211 : RT_TRANSACTION_RELAXED
));
31212 gcc_assert (token
!= NULL
);
31214 if (keyword
== RID_TRANSACTION_RELAXED
)
31215 this_in
|= TM_STMT_ATTR_RELAXED
;
31217 /* Set this early. This might mean that we allow transaction_cancel in
31218 an expression that we find out later actually has to be a constexpr.
31219 However, we expect that cxx_constant_value will be able to deal with
31220 this; also, if the noexcept has no constexpr, then what we parse next
31221 really is a transaction's body. */
31222 parser
->in_transaction
= this_in
;
31224 /* Parse a noexcept specification. */
31225 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
31228 if (!noex
|| !noex_expr
31229 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31231 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
31233 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
31234 expr
= finish_parenthesized_expr (expr
);
31236 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
31240 /* The only expression that is available got parsed for the noexcept
31241 already. noexcept is true then. */
31243 noex
= boolean_true_node
;
31246 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
31247 parser
->in_transaction
= old_in
;
31249 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
31250 return error_mark_node
;
31252 return (flag_tm
? expr
: error_mark_node
);
31255 /* Parse a function-transaction-block.
31257 function-transaction-block:
31258 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31260 __transaction_atomic txn-attribute[opt] function-try-block
31261 __transaction_relaxed ctor-initializer[opt] function-body
31262 __transaction_relaxed function-try-block
31266 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
31268 unsigned char old_in
= parser
->in_transaction
;
31269 unsigned char new_in
= 1;
31270 tree compound_stmt
, stmt
, attrs
;
31271 bool ctor_initializer_p
;
31274 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31275 || keyword
== RID_TRANSACTION_RELAXED
);
31276 token
= cp_parser_require_keyword (parser
, keyword
,
31277 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31278 : RT_TRANSACTION_RELAXED
));
31279 gcc_assert (token
!= NULL
);
31281 if (keyword
== RID_TRANSACTION_RELAXED
)
31282 new_in
|= TM_STMT_ATTR_RELAXED
;
31285 attrs
= cp_parser_txn_attribute_opt (parser
);
31287 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
31290 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
31292 parser
->in_transaction
= new_in
;
31294 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
31295 ctor_initializer_p
= cp_parser_function_try_block (parser
);
31297 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
31298 (parser
, /*in_function_try_block=*/false);
31300 parser
->in_transaction
= old_in
;
31302 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
31304 return ctor_initializer_p
;
31307 /* Parse a __transaction_cancel statement.
31310 __transaction_cancel txn-attribute[opt] ;
31311 __transaction_cancel txn-attribute[opt] throw-expression ;
31313 ??? Cancel and throw is not yet implemented. */
31316 cp_parser_transaction_cancel (cp_parser
*parser
)
31319 bool is_outer
= false;
31322 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
31323 RT_TRANSACTION_CANCEL
);
31324 gcc_assert (token
!= NULL
);
31326 attrs
= cp_parser_txn_attribute_opt (parser
);
31328 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
31330 /* ??? Parse cancel-and-throw here. */
31332 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
31336 error_at (token
->location
, "%<__transaction_cancel%> without "
31337 "transactional memory support enabled");
31338 return error_mark_node
;
31340 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
31342 error_at (token
->location
, "%<__transaction_cancel%> within a "
31343 "%<__transaction_relaxed%>");
31344 return error_mark_node
;
31348 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
31349 && !is_tm_may_cancel_outer (current_function_decl
))
31351 error_at (token
->location
, "outer %<__transaction_cancel%> not "
31352 "within outer %<__transaction_atomic%>");
31353 error_at (token
->location
,
31354 " or a %<transaction_may_cancel_outer%> function");
31355 return error_mark_node
;
31358 else if (parser
->in_transaction
== 0)
31360 error_at (token
->location
, "%<__transaction_cancel%> not within "
31361 "%<__transaction_atomic%>");
31362 return error_mark_node
;
31365 stmt
= build_tm_abort_call (token
->location
, is_outer
);
31373 static GTY (()) cp_parser
*the_parser
;
31376 /* Special handling for the first token or line in the file. The first
31377 thing in the file might be #pragma GCC pch_preprocess, which loads a
31378 PCH file, which is a GC collection point. So we need to handle this
31379 first pragma without benefit of an existing lexer structure.
31381 Always returns one token to the caller in *FIRST_TOKEN. This is
31382 either the true first token of the file, or the first token after
31383 the initial pragma. */
31386 cp_parser_initial_pragma (cp_token
*first_token
)
31390 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31391 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
31394 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31395 if (first_token
->type
== CPP_STRING
)
31397 name
= first_token
->u
.value
;
31399 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31400 if (first_token
->type
!= CPP_PRAGMA_EOL
)
31401 error_at (first_token
->location
,
31402 "junk at end of %<#pragma GCC pch_preprocess%>");
31405 error_at (first_token
->location
, "expected string literal");
31407 /* Skip to the end of the pragma. */
31408 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
31409 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31411 /* Now actually load the PCH file. */
31413 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
31415 /* Read one more token to return to our caller. We have to do this
31416 after reading the PCH file in, since its pointers have to be
31418 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31421 /* Normal parsing of a pragma token. Here we can (and must) use the
31425 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
31427 cp_token
*pragma_tok
;
31430 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
31431 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
31432 parser
->lexer
->in_pragma
= true;
31434 id
= pragma_tok
->pragma_kind
;
31435 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
31436 cp_ensure_no_omp_declare_simd (parser
);
31439 case PRAGMA_GCC_PCH_PREPROCESS
:
31440 error_at (pragma_tok
->location
,
31441 "%<#pragma GCC pch_preprocess%> must be first");
31444 case PRAGMA_OMP_BARRIER
:
31447 case pragma_compound
:
31448 cp_parser_omp_barrier (parser
, pragma_tok
);
31451 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
31452 "used in compound statements");
31459 case PRAGMA_OMP_FLUSH
:
31462 case pragma_compound
:
31463 cp_parser_omp_flush (parser
, pragma_tok
);
31466 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
31467 "used in compound statements");
31474 case PRAGMA_OMP_TASKWAIT
:
31477 case pragma_compound
:
31478 cp_parser_omp_taskwait (parser
, pragma_tok
);
31481 error_at (pragma_tok
->location
,
31482 "%<#pragma omp taskwait%> may only be "
31483 "used in compound statements");
31490 case PRAGMA_OMP_TASKYIELD
:
31493 case pragma_compound
:
31494 cp_parser_omp_taskyield (parser
, pragma_tok
);
31497 error_at (pragma_tok
->location
,
31498 "%<#pragma omp taskyield%> may only be "
31499 "used in compound statements");
31506 case PRAGMA_OMP_CANCEL
:
31509 case pragma_compound
:
31510 cp_parser_omp_cancel (parser
, pragma_tok
);
31513 error_at (pragma_tok
->location
,
31514 "%<#pragma omp cancel%> may only be "
31515 "used in compound statements");
31522 case PRAGMA_OMP_CANCELLATION_POINT
:
31525 case pragma_compound
:
31526 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
31529 error_at (pragma_tok
->location
,
31530 "%<#pragma omp cancellation point%> may only be "
31531 "used in compound statements");
31538 case PRAGMA_OMP_THREADPRIVATE
:
31539 cp_parser_omp_threadprivate (parser
, pragma_tok
);
31542 case PRAGMA_OMP_DECLARE_REDUCTION
:
31543 cp_parser_omp_declare (parser
, pragma_tok
, context
);
31546 case PRAGMA_OMP_ATOMIC
:
31547 case PRAGMA_OMP_CRITICAL
:
31548 case PRAGMA_OMP_DISTRIBUTE
:
31549 case PRAGMA_OMP_FOR
:
31550 case PRAGMA_OMP_MASTER
:
31551 case PRAGMA_OMP_ORDERED
:
31552 case PRAGMA_OMP_PARALLEL
:
31553 case PRAGMA_OMP_SECTIONS
:
31554 case PRAGMA_OMP_SIMD
:
31555 case PRAGMA_OMP_SINGLE
:
31556 case PRAGMA_OMP_TASK
:
31557 case PRAGMA_OMP_TASKGROUP
:
31558 case PRAGMA_OMP_TEAMS
:
31559 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
31561 cp_parser_omp_construct (parser
, pragma_tok
);
31564 case PRAGMA_OMP_TARGET
:
31565 return cp_parser_omp_target (parser
, pragma_tok
, context
);
31567 case PRAGMA_OMP_END_DECLARE_TARGET
:
31568 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
31571 case PRAGMA_OMP_SECTION
:
31572 error_at (pragma_tok
->location
,
31573 "%<#pragma omp section%> may only be used in "
31574 "%<#pragma omp sections%> construct");
31579 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31581 tok
= cp_lexer_peek_token (the_parser
->lexer
);
31582 if (tok
->type
!= CPP_KEYWORD
31583 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
31584 && tok
->keyword
!= RID_DO
))
31586 cp_parser_error (parser
, "for, while or do statement expected");
31589 cp_parser_iteration_statement (parser
, true);
31593 case PRAGMA_CILK_SIMD
:
31594 if (context
== pragma_external
)
31596 error_at (pragma_tok
->location
,
31597 "%<#pragma simd%> must be inside a function");
31600 cp_parser_cilk_simd (parser
, pragma_tok
);
31604 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
31605 c_invoke_pragma_handler (id
);
31609 cp_parser_error (parser
, "expected declaration specifiers");
31613 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31617 /* The interface the pragma parsers have to the lexer. */
31620 pragma_lex (tree
*value
)
31623 enum cpp_ttype ret
;
31625 tok
= cp_lexer_peek_token (the_parser
->lexer
);
31628 *value
= tok
->u
.value
;
31630 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
31632 else if (ret
== CPP_STRING
)
31633 *value
= cp_parser_string_literal (the_parser
, false, false);
31636 cp_lexer_consume_token (the_parser
->lexer
);
31637 if (ret
== CPP_KEYWORD
)
31645 /* External interface. */
31647 /* Parse one entire translation unit. */
31650 c_parse_file (void)
31652 static bool already_called
= false;
31654 if (already_called
)
31656 sorry ("inter-module optimizations not implemented for C++");
31659 already_called
= true;
31661 the_parser
= cp_parser_new ();
31662 push_deferring_access_checks (flag_access_control
31663 ? dk_no_deferred
: dk_no_check
);
31664 cp_parser_translation_unit (the_parser
);
31668 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31669 vectorlength clause:
31671 vectorlength ( constant-expression ) */
31674 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
31677 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31679 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31680 safelen clause. Thus, vectorlength is represented as OMP 4.0
31681 safelen. For SIMD-enabled function it is represented by OMP 4.0
31684 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
31687 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
31690 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31691 return error_mark_node
;
31693 expr
= cp_parser_constant_expression (parser
, false, NULL
);
31694 expr
= maybe_constant_value (expr
);
31696 /* If expr == error_mark_node, then don't emit any errors nor
31697 create a clause. if any of the above functions returns
31698 error mark node then they would have emitted an error message. */
31699 if (expr
== error_mark_node
)
31701 else if (!TREE_TYPE (expr
)
31702 || !TREE_CONSTANT (expr
)
31703 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
31704 error_at (loc
, "vectorlength must be an integer constant");
31705 else if (TREE_CONSTANT (expr
)
31706 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
31707 error_at (loc
, "vectorlength must be a power of 2");
31713 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
31714 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
31715 OMP_CLAUSE_CHAIN (c
) = clauses
;
31720 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
31721 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
31722 OMP_CLAUSE_CHAIN (c
) = clauses
;
31727 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31728 return error_mark_node
;
31732 /* Handles the Cilk Plus #pragma simd linear clause.
31734 linear ( simd-linear-variable-list )
31736 simd-linear-variable-list:
31737 simd-linear-variable
31738 simd-linear-variable-list , simd-linear-variable
31740 simd-linear-variable:
31742 id-expression : simd-linear-step
31745 conditional-expression */
31748 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
31750 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31752 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31754 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31756 cp_parser_error (parser
, "expected identifier");
31757 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31758 return error_mark_node
;
31761 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31762 parser
->colon_corrects_to_scope_p
= false;
31765 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31766 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31768 cp_parser_error (parser
, "expected variable-name");
31769 clauses
= error_mark_node
;
31773 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
31775 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
31777 if (decl
== error_mark_node
)
31779 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
31781 clauses
= error_mark_node
;
31785 tree e
= NULL_TREE
;
31786 tree step_size
= integer_one_node
;
31788 /* If present, parse the linear step. Otherwise, assume the default
31790 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
31792 cp_lexer_consume_token (parser
->lexer
);
31794 e
= cp_parser_assignment_expression (parser
, false, NULL
);
31795 e
= maybe_constant_value (e
);
31797 if (e
== error_mark_node
)
31799 /* If an error has occurred, then the whole pragma is
31800 considered ill-formed. Thus, no reason to keep
31802 clauses
= error_mark_node
;
31805 else if (type_dependent_expression_p (e
)
31806 || value_dependent_expression_p (e
)
31808 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
31809 && (TREE_CONSTANT (e
)
31813 cp_parser_error (parser
,
31814 "step size must be an integer constant "
31815 "expression or an integer variable");
31818 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31819 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
31820 OMP_CLAUSE_DECL (l
) = decl
;
31821 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
31822 OMP_CLAUSE_CHAIN (l
) = clauses
;
31825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31826 cp_lexer_consume_token (parser
->lexer
);
31827 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31831 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31832 "expected %<,%> or %<)%> after %qE", decl
);
31833 clauses
= error_mark_node
;
31837 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31838 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31842 /* Returns the name of the next clause. If the clause is not
31843 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31844 token is not consumed. Otherwise, the appropriate enum from the
31845 pragma_simd_clause is returned and the token is consumed. */
31847 static pragma_omp_clause
31848 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
31850 pragma_omp_clause clause_type
;
31851 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31853 if (token
->keyword
== RID_PRIVATE
)
31854 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
31855 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
31856 return PRAGMA_CILK_CLAUSE_NONE
;
31857 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
31858 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
31859 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
31860 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
31861 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
31862 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
31863 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
31864 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
31865 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
31866 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
31868 return PRAGMA_CILK_CLAUSE_NONE
;
31870 cp_lexer_consume_token (parser
->lexer
);
31871 return clause_type
;
31874 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31877 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
31879 tree clauses
= NULL_TREE
;
31881 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31882 && clauses
!= error_mark_node
)
31884 pragma_omp_clause c_kind
;
31885 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
31886 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
31887 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
31888 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
31889 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
31890 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
31891 /* Use the OpenMP 4.0 equivalent function. */
31892 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
31893 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
31894 /* Use the OpenMP 4.0 equivalent function. */
31895 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
31897 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
31898 /* Use the OMP 4.0 equivalent function. */
31899 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
31901 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
31902 /* Use the OMP 4.0 equivalent function. */
31903 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
31906 clauses
= error_mark_node
;
31907 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
31912 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
31914 if (clauses
== error_mark_node
)
31915 return error_mark_node
;
31917 return c_finish_cilk_clauses (clauses
);
31920 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31923 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
31925 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
31927 if (clauses
== error_mark_node
)
31930 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
31932 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31933 "for statement expected");
31937 tree sb
= begin_omp_structured_block ();
31938 int save
= cp_parser_begin_omp_structured_block (parser
);
31939 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
31941 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
31942 cp_parser_end_omp_structured_block (parser
, save
);
31943 add_stmt (finish_omp_structured_block (sb
));
31947 /* Create an identifier for a generic parameter type (a synthesized
31948 template parameter implied by `auto' or a concept identifier). */
31950 static GTY(()) int generic_parm_count
;
31952 make_generic_type_name ()
31955 sprintf (buf
, "auto:%d", ++generic_parm_count
);
31956 return get_identifier (buf
);
31959 /* Predicate that behaves as is_auto_or_concept but matches the parent
31960 node of the generic type rather than the generic type itself. This
31961 allows for type transformation in add_implicit_template_parms. */
31964 tree_type_is_auto_or_concept (const_tree t
)
31966 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
31969 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
31970 (creating a new template parameter list if necessary). Returns the newly
31971 created template type parm. */
31974 synthesize_implicit_template_parm (cp_parser
*parser
)
31976 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
31978 /* We are either continuing a function template that already contains implicit
31979 template parameters, creating a new fully-implicit function template, or
31980 extending an existing explicit function template with implicit template
31983 cp_binding_level
*const entry_scope
= current_binding_level
;
31985 bool become_template
= false;
31986 cp_binding_level
*parent_scope
= 0;
31988 if (parser
->implicit_template_scope
)
31990 gcc_assert (parser
->implicit_template_parms
);
31992 current_binding_level
= parser
->implicit_template_scope
;
31996 /* Roll back to the existing template parameter scope (in the case of
31997 extending an explicit function template) or introduce a new template
31998 parameter scope ahead of the function parameter scope (or class scope
31999 in the case of out-of-line member definitions). The function scope is
32000 added back after template parameter synthesis below. */
32002 cp_binding_level
*scope
= entry_scope
;
32004 while (scope
->kind
== sk_function_parms
)
32006 parent_scope
= scope
;
32007 scope
= scope
->level_chain
;
32009 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
32011 /* If not defining a class, then any class scope is a scope level in
32012 an out-of-line member definition. In this case simply wind back
32013 beyond the first such scope to inject the template parameter list.
32014 Otherwise wind back to the class being defined. The latter can
32015 occur in class member friend declarations such as:
32021 friend void A::foo (auto);
32024 The template parameter list synthesized for the friend declaration
32025 must be injected in the scope of 'B'. This can also occur in
32026 erroneous cases such as:
32032 void B::foo (auto) {}
32035 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32036 but, nevertheless, the template parameter list synthesized for the
32037 declarator should be injected into the scope of 'A' as if the
32038 ill-formed template was specified explicitly. */
32040 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
32042 parent_scope
= scope
;
32043 scope
= scope
->level_chain
;
32047 current_binding_level
= scope
;
32049 if (scope
->kind
!= sk_template_parms
32050 || !function_being_declared_is_template_p (parser
))
32052 /* Introduce a new template parameter list for implicit template
32055 become_template
= true;
32057 parser
->implicit_template_scope
32058 = begin_scope (sk_template_parms
, NULL
);
32060 ++processing_template_decl
;
32062 parser
->fully_implicit_function_template_p
= true;
32063 ++parser
->num_template_parameter_lists
;
32067 /* Synthesize implicit template parameters at the end of the explicit
32068 template parameter list. */
32070 gcc_assert (current_template_parms
);
32072 parser
->implicit_template_scope
= scope
;
32074 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
32075 parser
->implicit_template_parms
32076 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
32080 /* Synthesize a new template parameter and track the current template
32081 parameter chain with implicit_template_parms. */
32083 tree synth_id
= make_generic_type_name ();
32084 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
32087 = process_template_parm (parser
->implicit_template_parms
,
32089 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
32090 /*non_type=*/false,
32091 /*param_pack=*/false);
32094 if (parser
->implicit_template_parms
)
32095 parser
->implicit_template_parms
32096 = TREE_CHAIN (parser
->implicit_template_parms
);
32098 parser
->implicit_template_parms
= new_parm
;
32100 tree new_type
= TREE_TYPE (getdecls ());
32102 /* If creating a fully implicit function template, start the new implicit
32103 template parameter list with this synthesized type, otherwise grow the
32104 current template parameter list. */
32106 if (become_template
)
32108 parent_scope
->level_chain
= current_binding_level
;
32110 tree new_parms
= make_tree_vec (1);
32111 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
32112 current_template_parms
= tree_cons (size_int (processing_template_decl
),
32113 new_parms
, current_template_parms
);
32117 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
32118 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
32119 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
32120 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
32123 current_binding_level
= entry_scope
;
32128 /* Finish the declaration of a fully implicit function template. Such a
32129 template has no explicit template parameter list so has not been through the
32130 normal template head and tail processing. synthesize_implicit_template_parm
32131 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32132 provided if the declaration is a class member such that its template
32133 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32134 form is returned. Otherwise NULL_TREE is returned. */
32137 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
32139 gcc_assert (parser
->fully_implicit_function_template_p
);
32141 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
32142 && DECL_VIRTUAL_P (member_decl_opt
))
32144 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
32145 "implicit templates may not be %<virtual%>");
32146 DECL_VIRTUAL_P (member_decl_opt
) = false;
32149 if (member_decl_opt
)
32150 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
32151 end_template_decl ();
32153 parser
->fully_implicit_function_template_p
= false;
32154 --parser
->num_template_parameter_lists
;
32156 return member_decl_opt
;
32159 #include "gt-cp-parser.h"