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
1851 #define unparsed_classes \
1852 parser->unparsed_queues->last ().classes
1855 push_unparsed_function_queues (cp_parser
*parser
)
1857 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1858 vec_safe_push (parser
->unparsed_queues
, e
);
1862 pop_unparsed_function_queues (cp_parser
*parser
)
1864 release_tree_vector (unparsed_funs_with_definitions
);
1865 parser
->unparsed_queues
->pop ();
1870 /* Constructors and destructors. */
1872 static cp_parser
*cp_parser_new
1875 /* Routines to parse various constructs.
1877 Those that return `tree' will return the error_mark_node (rather
1878 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1879 Sometimes, they will return an ordinary node if error-recovery was
1880 attempted, even though a parse error occurred. So, to check
1881 whether or not a parse error occurred, you should always use
1882 cp_parser_error_occurred. If the construct is optional (indicated
1883 either by an `_opt' in the name of the function that does the
1884 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1885 the construct is not present. */
1887 /* Lexical conventions [gram.lex] */
1889 static tree cp_parser_identifier
1891 static tree cp_parser_string_literal
1892 (cp_parser
*, bool, bool);
1893 static tree cp_parser_userdef_char_literal
1895 static tree cp_parser_userdef_string_literal
1897 static tree cp_parser_userdef_numeric_literal
1900 /* Basic concepts [gram.basic] */
1902 static bool cp_parser_translation_unit
1905 /* Expressions [gram.expr] */
1907 static tree cp_parser_primary_expression
1908 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1909 static tree cp_parser_id_expression
1910 (cp_parser
*, bool, bool, bool *, bool, bool);
1911 static tree cp_parser_unqualified_id
1912 (cp_parser
*, bool, bool, bool, bool);
1913 static tree cp_parser_nested_name_specifier_opt
1914 (cp_parser
*, bool, bool, bool, bool);
1915 static tree cp_parser_nested_name_specifier
1916 (cp_parser
*, bool, bool, bool, bool);
1917 static tree cp_parser_qualifying_entity
1918 (cp_parser
*, bool, bool, bool, bool, bool);
1919 static tree cp_parser_postfix_expression
1920 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1921 static tree cp_parser_postfix_open_square_expression
1922 (cp_parser
*, tree
, bool, bool);
1923 static tree cp_parser_postfix_dot_deref_expression
1924 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1925 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1926 (cp_parser
*, int, bool, bool, bool *);
1927 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1928 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1929 static void cp_parser_pseudo_destructor_name
1930 (cp_parser
*, tree
, tree
*, tree
*);
1931 static tree cp_parser_unary_expression
1932 (cp_parser
*, bool, bool, cp_id_kind
*);
1933 static enum tree_code cp_parser_unary_operator
1935 static tree cp_parser_new_expression
1937 static vec
<tree
, va_gc
> *cp_parser_new_placement
1939 static tree cp_parser_new_type_id
1940 (cp_parser
*, tree
*);
1941 static cp_declarator
*cp_parser_new_declarator_opt
1943 static cp_declarator
*cp_parser_direct_new_declarator
1945 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1947 static tree cp_parser_delete_expression
1949 static tree cp_parser_cast_expression
1950 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1951 static tree cp_parser_binary_expression
1952 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1953 static tree cp_parser_question_colon_clause
1954 (cp_parser
*, tree
);
1955 static tree cp_parser_assignment_expression
1956 (cp_parser
*, bool, cp_id_kind
*);
1957 static enum tree_code cp_parser_assignment_operator_opt
1959 static tree cp_parser_expression
1960 (cp_parser
*, bool, cp_id_kind
*);
1961 static tree cp_parser_expression
1962 (cp_parser
*, bool, bool, cp_id_kind
*);
1963 static tree cp_parser_constant_expression
1964 (cp_parser
*, bool, bool *);
1965 static tree cp_parser_builtin_offsetof
1967 static tree cp_parser_lambda_expression
1969 static void cp_parser_lambda_introducer
1970 (cp_parser
*, tree
);
1971 static bool cp_parser_lambda_declarator_opt
1972 (cp_parser
*, tree
);
1973 static void cp_parser_lambda_body
1974 (cp_parser
*, tree
);
1976 /* Statements [gram.stmt.stmt] */
1978 static void cp_parser_statement
1979 (cp_parser
*, tree
, bool, bool *);
1980 static void cp_parser_label_for_labeled_statement
1981 (cp_parser
*, tree
);
1982 static tree cp_parser_expression_statement
1983 (cp_parser
*, tree
);
1984 static tree cp_parser_compound_statement
1985 (cp_parser
*, tree
, bool, bool);
1986 static void cp_parser_statement_seq_opt
1987 (cp_parser
*, tree
);
1988 static tree cp_parser_selection_statement
1989 (cp_parser
*, bool *);
1990 static tree cp_parser_condition
1992 static tree cp_parser_iteration_statement
1993 (cp_parser
*, bool);
1994 static bool cp_parser_for_init_statement
1995 (cp_parser
*, tree
*decl
);
1996 static tree cp_parser_for
1997 (cp_parser
*, bool);
1998 static tree cp_parser_c_for
1999 (cp_parser
*, tree
, tree
, bool);
2000 static tree cp_parser_range_for
2001 (cp_parser
*, tree
, tree
, tree
, bool);
2002 static void do_range_for_auto_deduction
2004 static tree cp_parser_perform_range_for_lookup
2005 (tree
, tree
*, tree
*);
2006 static tree cp_parser_range_for_member_function
2008 static tree cp_parser_jump_statement
2010 static void cp_parser_declaration_statement
2013 static tree cp_parser_implicitly_scoped_statement
2014 (cp_parser
*, bool *);
2015 static void cp_parser_already_scoped_statement
2018 /* Declarations [gram.dcl.dcl] */
2020 static void cp_parser_declaration_seq_opt
2022 static void cp_parser_declaration
2024 static void cp_parser_block_declaration
2025 (cp_parser
*, bool);
2026 static void cp_parser_simple_declaration
2027 (cp_parser
*, bool, tree
*);
2028 static void cp_parser_decl_specifier_seq
2029 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2030 static tree cp_parser_storage_class_specifier_opt
2032 static tree cp_parser_function_specifier_opt
2033 (cp_parser
*, cp_decl_specifier_seq
*);
2034 static tree cp_parser_type_specifier
2035 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2037 static tree cp_parser_simple_type_specifier
2038 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2039 static tree cp_parser_type_name
2041 static tree cp_parser_nonclass_name
2042 (cp_parser
* parser
);
2043 static tree cp_parser_elaborated_type_specifier
2044 (cp_parser
*, bool, bool);
2045 static tree cp_parser_enum_specifier
2047 static void cp_parser_enumerator_list
2048 (cp_parser
*, tree
);
2049 static void cp_parser_enumerator_definition
2050 (cp_parser
*, tree
);
2051 static tree cp_parser_namespace_name
2053 static void cp_parser_namespace_definition
2055 static void cp_parser_namespace_body
2057 static tree cp_parser_qualified_namespace_specifier
2059 static void cp_parser_namespace_alias_definition
2061 static bool cp_parser_using_declaration
2062 (cp_parser
*, bool);
2063 static void cp_parser_using_directive
2065 static tree cp_parser_alias_declaration
2067 static void cp_parser_asm_definition
2069 static void cp_parser_linkage_specification
2071 static void cp_parser_static_assert
2072 (cp_parser
*, bool);
2073 static tree cp_parser_decltype
2076 /* Declarators [gram.dcl.decl] */
2078 static tree cp_parser_init_declarator
2079 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*);
2080 static cp_declarator
*cp_parser_declarator
2081 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
2082 static cp_declarator
*cp_parser_direct_declarator
2083 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
2084 static enum tree_code cp_parser_ptr_operator
2085 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2086 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2088 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2090 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2092 static tree cp_parser_late_return_type_opt
2093 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2094 static tree cp_parser_declarator_id
2095 (cp_parser
*, bool);
2096 static tree cp_parser_type_id
2098 static tree cp_parser_template_type_arg
2100 static tree
cp_parser_trailing_type_id (cp_parser
*);
2101 static tree cp_parser_type_id_1
2102 (cp_parser
*, bool, bool);
2103 static void cp_parser_type_specifier_seq
2104 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2105 static tree cp_parser_parameter_declaration_clause
2107 static tree cp_parser_parameter_declaration_list
2108 (cp_parser
*, bool *);
2109 static cp_parameter_declarator
*cp_parser_parameter_declaration
2110 (cp_parser
*, bool, bool *);
2111 static tree cp_parser_default_argument
2112 (cp_parser
*, bool);
2113 static void cp_parser_function_body
2114 (cp_parser
*, bool);
2115 static tree cp_parser_initializer
2116 (cp_parser
*, bool *, bool *);
2117 static tree cp_parser_initializer_clause
2118 (cp_parser
*, bool *);
2119 static tree cp_parser_braced_list
2120 (cp_parser
*, bool*);
2121 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2122 (cp_parser
*, bool *);
2124 static bool cp_parser_ctor_initializer_opt_and_function_body
2125 (cp_parser
*, bool);
2127 static tree cp_parser_late_parsing_omp_declare_simd
2128 (cp_parser
*, tree
);
2130 static tree cp_parser_late_parsing_cilk_simd_fn_info
2131 (cp_parser
*, tree
);
2133 static tree synthesize_implicit_template_parm
2135 static tree finish_fully_implicit_template
2136 (cp_parser
*, tree
);
2138 /* Classes [gram.class] */
2140 static tree cp_parser_class_name
2141 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2142 static tree cp_parser_class_specifier
2144 static tree cp_parser_class_head
2145 (cp_parser
*, bool *);
2146 static enum tag_types cp_parser_class_key
2148 static void cp_parser_member_specification_opt
2150 static void cp_parser_member_declaration
2152 static tree cp_parser_pure_specifier
2154 static tree cp_parser_constant_initializer
2157 /* Derived classes [gram.class.derived] */
2159 static tree cp_parser_base_clause
2161 static tree cp_parser_base_specifier
2164 /* Special member functions [gram.special] */
2166 static tree cp_parser_conversion_function_id
2168 static tree cp_parser_conversion_type_id
2170 static cp_declarator
*cp_parser_conversion_declarator_opt
2172 static bool cp_parser_ctor_initializer_opt
2174 static void cp_parser_mem_initializer_list
2176 static tree cp_parser_mem_initializer
2178 static tree cp_parser_mem_initializer_id
2181 /* Overloading [gram.over] */
2183 static tree cp_parser_operator_function_id
2185 static tree cp_parser_operator
2188 /* Templates [gram.temp] */
2190 static void cp_parser_template_declaration
2191 (cp_parser
*, bool);
2192 static tree cp_parser_template_parameter_list
2194 static tree cp_parser_template_parameter
2195 (cp_parser
*, bool *, bool *);
2196 static tree cp_parser_type_parameter
2197 (cp_parser
*, bool *);
2198 static tree cp_parser_template_id
2199 (cp_parser
*, bool, bool, enum tag_types
, bool);
2200 static tree cp_parser_template_name
2201 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2202 static tree cp_parser_template_argument_list
2204 static tree cp_parser_template_argument
2206 static void cp_parser_explicit_instantiation
2208 static void cp_parser_explicit_specialization
2211 /* Exception handling [gram.exception] */
2213 static tree cp_parser_try_block
2215 static bool cp_parser_function_try_block
2217 static void cp_parser_handler_seq
2219 static void cp_parser_handler
2221 static tree cp_parser_exception_declaration
2223 static tree cp_parser_throw_expression
2225 static tree cp_parser_exception_specification_opt
2227 static tree cp_parser_type_id_list
2230 /* GNU Extensions */
2232 static tree cp_parser_asm_specification_opt
2234 static tree cp_parser_asm_operand_list
2236 static tree cp_parser_asm_clobber_list
2238 static tree cp_parser_asm_label_list
2240 static bool cp_next_tokens_can_be_attribute_p
2242 static bool cp_next_tokens_can_be_gnu_attribute_p
2244 static bool cp_next_tokens_can_be_std_attribute_p
2246 static bool cp_nth_tokens_can_be_std_attribute_p
2247 (cp_parser
*, size_t);
2248 static bool cp_nth_tokens_can_be_gnu_attribute_p
2249 (cp_parser
*, size_t);
2250 static bool cp_nth_tokens_can_be_attribute_p
2251 (cp_parser
*, size_t);
2252 static tree cp_parser_attributes_opt
2254 static tree cp_parser_gnu_attributes_opt
2256 static tree cp_parser_gnu_attribute_list
2258 static tree cp_parser_std_attribute
2260 static tree cp_parser_std_attribute_spec
2262 static tree cp_parser_std_attribute_spec_seq
2264 static bool cp_parser_extension_opt
2265 (cp_parser
*, int *);
2266 static void cp_parser_label_declaration
2269 /* Transactional Memory Extensions */
2271 static tree cp_parser_transaction
2272 (cp_parser
*, enum rid
);
2273 static tree cp_parser_transaction_expression
2274 (cp_parser
*, enum rid
);
2275 static bool cp_parser_function_transaction
2276 (cp_parser
*, enum rid
);
2277 static tree cp_parser_transaction_cancel
2280 enum pragma_context
{
2287 static bool cp_parser_pragma
2288 (cp_parser
*, enum pragma_context
);
2290 /* Objective-C++ Productions */
2292 static tree cp_parser_objc_message_receiver
2294 static tree cp_parser_objc_message_args
2296 static tree cp_parser_objc_message_expression
2298 static tree cp_parser_objc_encode_expression
2300 static tree cp_parser_objc_defs_expression
2302 static tree cp_parser_objc_protocol_expression
2304 static tree cp_parser_objc_selector_expression
2306 static tree cp_parser_objc_expression
2308 static bool cp_parser_objc_selector_p
2310 static tree cp_parser_objc_selector
2312 static tree cp_parser_objc_protocol_refs_opt
2314 static void cp_parser_objc_declaration
2315 (cp_parser
*, tree
);
2316 static tree cp_parser_objc_statement
2318 static bool cp_parser_objc_valid_prefix_attributes
2319 (cp_parser
*, tree
*);
2320 static void cp_parser_objc_at_property_declaration
2322 static void cp_parser_objc_at_synthesize_declaration
2324 static void cp_parser_objc_at_dynamic_declaration
2326 static tree cp_parser_objc_struct_declaration
2329 /* Utility Routines */
2331 static tree cp_parser_lookup_name
2332 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2333 static tree cp_parser_lookup_name_simple
2334 (cp_parser
*, tree
, location_t
);
2335 static tree cp_parser_maybe_treat_template_as_class
2337 static bool cp_parser_check_declarator_template_parameters
2338 (cp_parser
*, cp_declarator
*, location_t
);
2339 static bool cp_parser_check_template_parameters
2340 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2341 static tree cp_parser_simple_cast_expression
2343 static tree cp_parser_global_scope_opt
2344 (cp_parser
*, bool);
2345 static bool cp_parser_constructor_declarator_p
2346 (cp_parser
*, bool);
2347 static tree cp_parser_function_definition_from_specifiers_and_declarator
2348 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2349 static tree cp_parser_function_definition_after_declarator
2350 (cp_parser
*, bool);
2351 static void cp_parser_template_declaration_after_export
2352 (cp_parser
*, bool);
2353 static void cp_parser_perform_template_parameter_access_checks
2354 (vec
<deferred_access_check
, va_gc
> *);
2355 static tree cp_parser_single_declaration
2356 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2357 static tree cp_parser_functional_cast
2358 (cp_parser
*, tree
);
2359 static tree cp_parser_save_member_function_body
2360 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2361 static tree cp_parser_save_nsdmi
2363 static tree cp_parser_enclosed_template_argument_list
2365 static void cp_parser_save_default_args
2366 (cp_parser
*, tree
);
2367 static void cp_parser_late_parsing_for_member
2368 (cp_parser
*, tree
);
2369 static tree cp_parser_late_parse_one_default_arg
2370 (cp_parser
*, tree
, tree
, tree
);
2371 static void cp_parser_late_parsing_nsdmi
2372 (cp_parser
*, tree
);
2373 static void cp_parser_late_parsing_default_args
2374 (cp_parser
*, tree
);
2375 static tree cp_parser_sizeof_operand
2376 (cp_parser
*, enum rid
);
2377 static tree cp_parser_trait_expr
2378 (cp_parser
*, enum rid
);
2379 static bool cp_parser_declares_only_class_p
2381 static void cp_parser_set_storage_class
2382 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2383 static void cp_parser_set_decl_spec_type
2384 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2385 static void set_and_check_decl_spec_loc
2386 (cp_decl_specifier_seq
*decl_specs
,
2387 cp_decl_spec ds
, cp_token
*);
2388 static bool cp_parser_friend_p
2389 (const cp_decl_specifier_seq
*);
2390 static void cp_parser_required_error
2391 (cp_parser
*, required_token
, bool);
2392 static cp_token
*cp_parser_require
2393 (cp_parser
*, enum cpp_ttype
, required_token
);
2394 static cp_token
*cp_parser_require_keyword
2395 (cp_parser
*, enum rid
, required_token
);
2396 static bool cp_parser_token_starts_function_definition_p
2398 static bool cp_parser_next_token_starts_class_definition_p
2400 static bool cp_parser_next_token_ends_template_argument_p
2402 static bool cp_parser_nth_token_starts_template_argument_list_p
2403 (cp_parser
*, size_t);
2404 static enum tag_types cp_parser_token_is_class_key
2406 static void cp_parser_check_class_key
2407 (enum tag_types
, tree type
);
2408 static void cp_parser_check_access_in_redeclaration
2409 (tree type
, location_t location
);
2410 static bool cp_parser_optional_template_keyword
2412 static void cp_parser_pre_parsed_nested_name_specifier
2414 static bool cp_parser_cache_group
2415 (cp_parser
*, enum cpp_ttype
, unsigned);
2416 static tree cp_parser_cache_defarg
2417 (cp_parser
*parser
, bool nsdmi
);
2418 static void cp_parser_parse_tentatively
2420 static void cp_parser_commit_to_tentative_parse
2422 static void cp_parser_commit_to_topmost_tentative_parse
2424 static void cp_parser_abort_tentative_parse
2426 static bool cp_parser_parse_definitely
2428 static inline bool cp_parser_parsing_tentatively
2430 static bool cp_parser_uncommitted_to_tentative_parse_p
2432 static void cp_parser_error
2433 (cp_parser
*, const char *);
2434 static void cp_parser_name_lookup_error
2435 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2436 static bool cp_parser_simulate_error
2438 static bool cp_parser_check_type_definition
2440 static void cp_parser_check_for_definition_in_return_type
2441 (cp_declarator
*, tree
, location_t type_location
);
2442 static void cp_parser_check_for_invalid_template_id
2443 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2444 static bool cp_parser_non_integral_constant_expression
2445 (cp_parser
*, non_integral_constant
);
2446 static void cp_parser_diagnose_invalid_type_name
2447 (cp_parser
*, tree
, tree
, location_t
);
2448 static bool cp_parser_parse_and_diagnose_invalid_type_name
2450 static int cp_parser_skip_to_closing_parenthesis
2451 (cp_parser
*, bool, bool, bool);
2452 static void cp_parser_skip_to_end_of_statement
2454 static void cp_parser_consume_semicolon_at_end_of_statement
2456 static void cp_parser_skip_to_end_of_block_or_statement
2458 static bool cp_parser_skip_to_closing_brace
2460 static void cp_parser_skip_to_end_of_template_parameter_list
2462 static void cp_parser_skip_to_pragma_eol
2463 (cp_parser
*, cp_token
*);
2464 static bool cp_parser_error_occurred
2466 static bool cp_parser_allow_gnu_extensions_p
2468 static bool cp_parser_is_pure_string_literal
2470 static bool cp_parser_is_string_literal
2472 static bool cp_parser_is_keyword
2473 (cp_token
*, enum rid
);
2474 static tree cp_parser_make_typename_type
2475 (cp_parser
*, tree
, tree
, location_t location
);
2476 static cp_declarator
* cp_parser_make_indirect_declarator
2477 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2479 /* Returns nonzero if we are parsing tentatively. */
2482 cp_parser_parsing_tentatively (cp_parser
* parser
)
2484 return parser
->context
->next
!= NULL
;
2487 /* Returns nonzero if TOKEN is a string literal. */
2490 cp_parser_is_pure_string_literal (cp_token
* token
)
2492 return (token
->type
== CPP_STRING
||
2493 token
->type
== CPP_STRING16
||
2494 token
->type
== CPP_STRING32
||
2495 token
->type
== CPP_WSTRING
||
2496 token
->type
== CPP_UTF8STRING
);
2499 /* Returns nonzero if TOKEN is a string literal
2500 of a user-defined string literal. */
2503 cp_parser_is_string_literal (cp_token
* token
)
2505 return (cp_parser_is_pure_string_literal (token
) ||
2506 token
->type
== CPP_STRING_USERDEF
||
2507 token
->type
== CPP_STRING16_USERDEF
||
2508 token
->type
== CPP_STRING32_USERDEF
||
2509 token
->type
== CPP_WSTRING_USERDEF
||
2510 token
->type
== CPP_UTF8STRING_USERDEF
);
2513 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2516 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2518 return token
->keyword
== keyword
;
2521 /* If not parsing tentatively, issue a diagnostic of the form
2522 FILE:LINE: MESSAGE before TOKEN
2523 where TOKEN is the next token in the input stream. MESSAGE
2524 (specified by the caller) is usually of the form "expected
2528 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2530 if (!cp_parser_simulate_error (parser
))
2532 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2533 /* This diagnostic makes more sense if it is tagged to the line
2534 of the token we just peeked at. */
2535 cp_lexer_set_source_position_from_token (token
);
2537 if (token
->type
== CPP_PRAGMA
)
2539 error_at (token
->location
,
2540 "%<#pragma%> is not allowed here");
2541 cp_parser_skip_to_pragma_eol (parser
, token
);
2545 c_parse_error (gmsgid
,
2546 /* Because c_parser_error does not understand
2547 CPP_KEYWORD, keywords are treated like
2549 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2550 token
->u
.value
, token
->flags
);
2554 /* Issue an error about name-lookup failing. NAME is the
2555 IDENTIFIER_NODE DECL is the result of
2556 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2557 the thing that we hoped to find. */
2560 cp_parser_name_lookup_error (cp_parser
* parser
,
2563 name_lookup_error desired
,
2564 location_t location
)
2566 /* If name lookup completely failed, tell the user that NAME was not
2568 if (decl
== error_mark_node
)
2570 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2571 error_at (location
, "%<%E::%E%> has not been declared",
2572 parser
->scope
, name
);
2573 else if (parser
->scope
== global_namespace
)
2574 error_at (location
, "%<::%E%> has not been declared", name
);
2575 else if (parser
->object_scope
2576 && !CLASS_TYPE_P (parser
->object_scope
))
2577 error_at (location
, "request for member %qE in non-class type %qT",
2578 name
, parser
->object_scope
);
2579 else if (parser
->object_scope
)
2580 error_at (location
, "%<%T::%E%> has not been declared",
2581 parser
->object_scope
, name
);
2583 error_at (location
, "%qE has not been declared", name
);
2585 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2590 error_at (location
, "%<%E::%E%> is not a type",
2591 parser
->scope
, name
);
2594 error_at (location
, "%<%E::%E%> is not a class or namespace",
2595 parser
->scope
, name
);
2599 "%<%E::%E%> is not a class, namespace, or enumeration",
2600 parser
->scope
, name
);
2607 else if (parser
->scope
== global_namespace
)
2612 error_at (location
, "%<::%E%> is not a type", name
);
2615 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2619 "%<::%E%> is not a class, namespace, or enumeration",
2631 error_at (location
, "%qE is not a type", name
);
2634 error_at (location
, "%qE is not a class or namespace", name
);
2638 "%qE is not a class, namespace, or enumeration", name
);
2646 /* If we are parsing tentatively, remember that an error has occurred
2647 during this tentative parse. Returns true if the error was
2648 simulated; false if a message should be issued by the caller. */
2651 cp_parser_simulate_error (cp_parser
* parser
)
2653 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2655 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2661 /* This function is called when a type is defined. If type
2662 definitions are forbidden at this point, an error message is
2666 cp_parser_check_type_definition (cp_parser
* parser
)
2668 /* If types are forbidden here, issue a message. */
2669 if (parser
->type_definition_forbidden_message
)
2671 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2672 in the message need to be interpreted. */
2673 error (parser
->type_definition_forbidden_message
);
2679 /* This function is called when the DECLARATOR is processed. The TYPE
2680 was a type defined in the decl-specifiers. If it is invalid to
2681 define a type in the decl-specifiers for DECLARATOR, an error is
2682 issued. TYPE_LOCATION is the location of TYPE and is used
2683 for error reporting. */
2686 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2687 tree type
, location_t type_location
)
2689 /* [dcl.fct] forbids type definitions in return types.
2690 Unfortunately, it's not easy to know whether or not we are
2691 processing a return type until after the fact. */
2693 && (declarator
->kind
== cdk_pointer
2694 || declarator
->kind
== cdk_reference
2695 || declarator
->kind
== cdk_ptrmem
))
2696 declarator
= declarator
->declarator
;
2698 && declarator
->kind
== cdk_function
)
2700 error_at (type_location
,
2701 "new types may not be defined in a return type");
2702 inform (type_location
,
2703 "(perhaps a semicolon is missing after the definition of %qT)",
2708 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2709 "<" in any valid C++ program. If the next token is indeed "<",
2710 issue a message warning the user about what appears to be an
2711 invalid attempt to form a template-id. LOCATION is the location
2712 of the type-specifier (TYPE) */
2715 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2717 enum tag_types tag_type
,
2718 location_t location
)
2720 cp_token_position start
= 0;
2722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2725 error_at (location
, "%qT is not a template", type
);
2726 else if (identifier_p (type
))
2728 if (tag_type
!= none_type
)
2729 error_at (location
, "%qE is not a class template", type
);
2731 error_at (location
, "%qE is not a template", type
);
2734 error_at (location
, "invalid template-id");
2735 /* Remember the location of the invalid "<". */
2736 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2737 start
= cp_lexer_token_position (parser
->lexer
, true);
2738 /* Consume the "<". */
2739 cp_lexer_consume_token (parser
->lexer
);
2740 /* Parse the template arguments. */
2741 cp_parser_enclosed_template_argument_list (parser
);
2742 /* Permanently remove the invalid template arguments so that
2743 this error message is not issued again. */
2745 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2749 /* If parsing an integral constant-expression, issue an error message
2750 about the fact that THING appeared and return true. Otherwise,
2751 return false. In either case, set
2752 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2755 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2756 non_integral_constant thing
)
2758 parser
->non_integral_constant_expression_p
= true;
2759 if (parser
->integral_constant_expression_p
)
2761 if (!parser
->allow_non_integral_constant_expression_p
)
2763 const char *msg
= NULL
;
2767 error ("floating-point literal "
2768 "cannot appear in a constant-expression");
2771 error ("a cast to a type other than an integral or "
2772 "enumeration type cannot appear in a "
2773 "constant-expression");
2776 error ("%<typeid%> operator "
2777 "cannot appear in a constant-expression");
2780 error ("non-constant compound literals "
2781 "cannot appear in a constant-expression");
2784 error ("a function call "
2785 "cannot appear in a constant-expression");
2788 error ("an increment "
2789 "cannot appear in a constant-expression");
2792 error ("an decrement "
2793 "cannot appear in a constant-expression");
2796 error ("an array reference "
2797 "cannot appear in a constant-expression");
2799 case NIC_ADDR_LABEL
:
2800 error ("the address of a label "
2801 "cannot appear in a constant-expression");
2803 case NIC_OVERLOADED
:
2804 error ("calls to overloaded operators "
2805 "cannot appear in a constant-expression");
2807 case NIC_ASSIGNMENT
:
2808 error ("an assignment cannot appear in a constant-expression");
2811 error ("a comma operator "
2812 "cannot appear in a constant-expression");
2814 case NIC_CONSTRUCTOR
:
2815 error ("a call to a constructor "
2816 "cannot appear in a constant-expression");
2818 case NIC_TRANSACTION
:
2819 error ("a transaction expression "
2820 "cannot appear in a constant-expression");
2826 msg
= "__FUNCTION__";
2828 case NIC_PRETTY_FUNC
:
2829 msg
= "__PRETTY_FUNCTION__";
2849 case NIC_PREINCREMENT
:
2852 case NIC_PREDECREMENT
:
2865 error ("%qs cannot appear in a constant-expression", msg
);
2872 /* Emit a diagnostic for an invalid type name. SCOPE is the
2873 qualifying scope (or NULL, if none) for ID. This function commits
2874 to the current active tentative parse, if any. (Otherwise, the
2875 problematic construct might be encountered again later, resulting
2876 in duplicate error messages.) LOCATION is the location of ID. */
2879 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2880 tree scope
, tree id
,
2881 location_t location
)
2883 tree decl
, old_scope
, ambiguous_decls
;
2884 cp_parser_commit_to_tentative_parse (parser
);
2885 /* Try to lookup the identifier. */
2886 old_scope
= parser
->scope
;
2887 parser
->scope
= scope
;
2888 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
2889 /*is_template=*/false,
2890 /*is_namespace=*/false,
2891 /*check_dependency=*/true,
2892 &ambiguous_decls
, location
);
2893 parser
->scope
= old_scope
;
2894 if (ambiguous_decls
)
2895 /* If the lookup was ambiguous, an error will already have
2898 /* If the lookup found a template-name, it means that the user forgot
2899 to specify an argument list. Emit a useful error message. */
2900 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2902 "invalid use of template-name %qE without an argument list",
2904 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2905 error_at (location
, "invalid use of destructor %qD as a type", id
);
2906 else if (TREE_CODE (decl
) == TYPE_DECL
)
2907 /* Something like 'unsigned A a;' */
2908 error_at (location
, "invalid combination of multiple type-specifiers");
2909 else if (!parser
->scope
)
2911 /* Issue an error message. */
2912 error_at (location
, "%qE does not name a type", id
);
2913 /* If we're in a template class, it's possible that the user was
2914 referring to a type from a base class. For example:
2916 template <typename T> struct A { typedef T X; };
2917 template <typename T> struct B : public A<T> { X x; };
2919 The user should have said "typename A<T>::X". */
2920 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2921 inform (location
, "C++11 %<constexpr%> only available with "
2922 "-std=c++11 or -std=gnu++11");
2923 else if (processing_template_decl
&& current_class_type
2924 && TYPE_BINFO (current_class_type
))
2928 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2932 tree base_type
= BINFO_TYPE (b
);
2933 if (CLASS_TYPE_P (base_type
)
2934 && dependent_type_p (base_type
))
2937 /* Go from a particular instantiation of the
2938 template (which will have an empty TYPE_FIELDs),
2939 to the main version. */
2940 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2941 for (field
= TYPE_FIELDS (base_type
);
2943 field
= DECL_CHAIN (field
))
2944 if (TREE_CODE (field
) == TYPE_DECL
2945 && DECL_NAME (field
) == id
)
2948 "(perhaps %<typename %T::%E%> was intended)",
2949 BINFO_TYPE (b
), id
);
2958 /* Here we diagnose qualified-ids where the scope is actually correct,
2959 but the identifier does not resolve to a valid type name. */
2960 else if (parser
->scope
!= error_mark_node
)
2962 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2965 error_at (location_of (id
),
2966 "%qE in namespace %qE does not name a template type",
2969 error_at (location_of (id
),
2970 "%qE in namespace %qE does not name a type",
2973 else if (CLASS_TYPE_P (parser
->scope
)
2974 && constructor_name_p (id
, parser
->scope
))
2977 error_at (location
, "%<%T::%E%> names the constructor, not"
2978 " the type", parser
->scope
, id
);
2979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2980 error_at (location
, "and %qT has no template constructors",
2983 else if (TYPE_P (parser
->scope
)
2984 && dependent_scope_p (parser
->scope
))
2985 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2986 "%qT is a dependent scope",
2987 parser
->scope
, id
, parser
->scope
);
2988 else if (TYPE_P (parser
->scope
))
2990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2991 error_at (location_of (id
),
2992 "%qE in %q#T does not name a template type",
2995 error_at (location_of (id
),
2996 "%qE in %q#T does not name a type",
3004 /* Check for a common situation where a type-name should be present,
3005 but is not, and issue a sensible error message. Returns true if an
3006 invalid type-name was detected.
3008 The situation handled by this function are variable declarations of the
3009 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3010 Usually, `ID' should name a type, but if we got here it means that it
3011 does not. We try to emit the best possible error message depending on
3012 how exactly the id-expression looks like. */
3015 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3018 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3020 /* Avoid duplicate error about ambiguous lookup. */
3021 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3023 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3024 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3028 cp_parser_parse_tentatively (parser
);
3029 id
= cp_parser_id_expression (parser
,
3030 /*template_keyword_p=*/false,
3031 /*check_dependency_p=*/true,
3032 /*template_p=*/NULL
,
3033 /*declarator_p=*/true,
3034 /*optional_p=*/false);
3035 /* If the next token is a (, this is a function with no explicit return
3036 type, i.e. constructor, destructor or conversion op. */
3037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3038 || TREE_CODE (id
) == TYPE_DECL
)
3040 cp_parser_abort_tentative_parse (parser
);
3043 if (!cp_parser_parse_definitely (parser
))
3046 /* Emit a diagnostic for the invalid type. */
3047 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
3048 id
, token
->location
);
3050 /* If we aren't in the middle of a declarator (i.e. in a
3051 parameter-declaration-clause), skip to the end of the declaration;
3052 there's no point in trying to process it. */
3053 if (!parser
->in_declarator_p
)
3054 cp_parser_skip_to_end_of_block_or_statement (parser
);
3058 /* Consume tokens up to, and including, the next non-nested closing `)'.
3059 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3060 are doing error recovery. Returns -1 if OR_COMMA is true and we
3061 found an unnested comma. */
3064 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3069 unsigned paren_depth
= 0;
3070 unsigned brace_depth
= 0;
3071 unsigned square_depth
= 0;
3073 if (recovering
&& !or_comma
3074 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3079 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3081 switch (token
->type
)
3084 case CPP_PRAGMA_EOL
:
3085 /* If we've run out of tokens, then there is no closing `)'. */
3088 /* This is good for lambda expression capture-lists. */
3089 case CPP_OPEN_SQUARE
:
3092 case CPP_CLOSE_SQUARE
:
3093 if (!square_depth
--)
3098 /* This matches the processing in skip_to_end_of_statement. */
3103 case CPP_OPEN_BRACE
:
3106 case CPP_CLOSE_BRACE
:
3112 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3117 case CPP_OPEN_PAREN
:
3122 case CPP_CLOSE_PAREN
:
3123 if (!brace_depth
&& !paren_depth
--)
3126 cp_lexer_consume_token (parser
->lexer
);
3135 /* Consume the token. */
3136 cp_lexer_consume_token (parser
->lexer
);
3140 /* Consume tokens until we reach the end of the current statement.
3141 Normally, that will be just before consuming a `;'. However, if a
3142 non-nested `}' comes first, then we stop before consuming that. */
3145 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3147 unsigned nesting_depth
= 0;
3149 /* Unwind generic function template scope if necessary. */
3150 if (parser
->fully_implicit_function_template_p
)
3151 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3155 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3157 switch (token
->type
)
3160 case CPP_PRAGMA_EOL
:
3161 /* If we've run out of tokens, stop. */
3165 /* If the next token is a `;', we have reached the end of the
3171 case CPP_CLOSE_BRACE
:
3172 /* If this is a non-nested '}', stop before consuming it.
3173 That way, when confronted with something like:
3177 we stop before consuming the closing '}', even though we
3178 have not yet reached a `;'. */
3179 if (nesting_depth
== 0)
3182 /* If it is the closing '}' for a block that we have
3183 scanned, stop -- but only after consuming the token.
3189 we will stop after the body of the erroneously declared
3190 function, but before consuming the following `typedef'
3192 if (--nesting_depth
== 0)
3194 cp_lexer_consume_token (parser
->lexer
);
3198 case CPP_OPEN_BRACE
:
3206 /* Consume the token. */
3207 cp_lexer_consume_token (parser
->lexer
);
3211 /* This function is called at the end of a statement or declaration.
3212 If the next token is a semicolon, it is consumed; otherwise, error
3213 recovery is attempted. */
3216 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3218 /* Look for the trailing `;'. */
3219 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3221 /* If there is additional (erroneous) input, skip to the end of
3223 cp_parser_skip_to_end_of_statement (parser
);
3224 /* If the next token is now a `;', consume it. */
3225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3226 cp_lexer_consume_token (parser
->lexer
);
3230 /* Skip tokens until we have consumed an entire block, or until we
3231 have consumed a non-nested `;'. */
3234 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3236 int nesting_depth
= 0;
3238 /* Unwind generic function template scope if necessary. */
3239 if (parser
->fully_implicit_function_template_p
)
3240 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3242 while (nesting_depth
>= 0)
3244 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3246 switch (token
->type
)
3249 case CPP_PRAGMA_EOL
:
3250 /* If we've run out of tokens, stop. */
3254 /* Stop if this is an unnested ';'. */
3259 case CPP_CLOSE_BRACE
:
3260 /* Stop if this is an unnested '}', or closes the outermost
3263 if (nesting_depth
< 0)
3269 case CPP_OPEN_BRACE
:
3278 /* Consume the token. */
3279 cp_lexer_consume_token (parser
->lexer
);
3283 /* Skip tokens until a non-nested closing curly brace is the next
3284 token, or there are no more tokens. Return true in the first case,
3288 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3290 unsigned nesting_depth
= 0;
3294 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3296 switch (token
->type
)
3299 case CPP_PRAGMA_EOL
:
3300 /* If we've run out of tokens, stop. */
3303 case CPP_CLOSE_BRACE
:
3304 /* If the next token is a non-nested `}', then we have reached
3305 the end of the current block. */
3306 if (nesting_depth
-- == 0)
3310 case CPP_OPEN_BRACE
:
3311 /* If it the next token is a `{', then we are entering a new
3312 block. Consume the entire block. */
3320 /* Consume the token. */
3321 cp_lexer_consume_token (parser
->lexer
);
3325 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3326 parameter is the PRAGMA token, allowing us to purge the entire pragma
3330 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3334 parser
->lexer
->in_pragma
= false;
3337 token
= cp_lexer_consume_token (parser
->lexer
);
3338 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3340 /* Ensure that the pragma is not parsed again. */
3341 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3344 /* Require pragma end of line, resyncing with it as necessary. The
3345 arguments are as for cp_parser_skip_to_pragma_eol. */
3348 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3350 parser
->lexer
->in_pragma
= false;
3351 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3352 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3355 /* This is a simple wrapper around make_typename_type. When the id is
3356 an unresolved identifier node, we can provide a superior diagnostic
3357 using cp_parser_diagnose_invalid_type_name. */
3360 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3361 tree id
, location_t id_location
)
3364 if (identifier_p (id
))
3366 result
= make_typename_type (scope
, id
, typename_type
,
3367 /*complain=*/tf_none
);
3368 if (result
== error_mark_node
)
3369 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3372 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3375 /* This is a wrapper around the
3376 make_{pointer,ptrmem,reference}_declarator functions that decides
3377 which one to call based on the CODE and CLASS_TYPE arguments. The
3378 CODE argument should be one of the values returned by
3379 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3380 appertain to the pointer or reference. */
3382 static cp_declarator
*
3383 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3384 cp_cv_quals cv_qualifiers
,
3385 cp_declarator
*target
,
3388 if (code
== ERROR_MARK
)
3389 return cp_error_declarator
;
3391 if (code
== INDIRECT_REF
)
3392 if (class_type
== NULL_TREE
)
3393 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3395 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3396 target
, attributes
);
3397 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3398 return make_reference_declarator (cv_qualifiers
, target
,
3400 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3401 return make_reference_declarator (cv_qualifiers
, target
,
3406 /* Create a new C++ parser. */
3409 cp_parser_new (void)
3415 /* cp_lexer_new_main is called before doing GC allocation because
3416 cp_lexer_new_main might load a PCH file. */
3417 lexer
= cp_lexer_new_main ();
3419 /* Initialize the binops_by_token so that we can get the tree
3420 directly from the token. */
3421 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3422 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3424 parser
= ggc_alloc_cleared_cp_parser ();
3425 parser
->lexer
= lexer
;
3426 parser
->context
= cp_parser_context_new (NULL
);
3428 /* For now, we always accept GNU extensions. */
3429 parser
->allow_gnu_extensions_p
= 1;
3431 /* The `>' token is a greater-than operator, not the end of a
3433 parser
->greater_than_is_operator_p
= true;
3435 parser
->default_arg_ok_p
= true;
3437 /* We are not parsing a constant-expression. */
3438 parser
->integral_constant_expression_p
= false;
3439 parser
->allow_non_integral_constant_expression_p
= false;
3440 parser
->non_integral_constant_expression_p
= false;
3442 /* Local variable names are not forbidden. */
3443 parser
->local_variables_forbidden_p
= false;
3445 /* We are not processing an `extern "C"' declaration. */
3446 parser
->in_unbraced_linkage_specification_p
= false;
3448 /* We are not processing a declarator. */
3449 parser
->in_declarator_p
= false;
3451 /* We are not processing a template-argument-list. */
3452 parser
->in_template_argument_list_p
= false;
3454 /* We are not in an iteration statement. */
3455 parser
->in_statement
= 0;
3457 /* We are not in a switch statement. */
3458 parser
->in_switch_statement_p
= false;
3460 /* We are not parsing a type-id inside an expression. */
3461 parser
->in_type_id_in_expr_p
= false;
3463 /* Declarations aren't implicitly extern "C". */
3464 parser
->implicit_extern_c
= false;
3466 /* String literals should be translated to the execution character set. */
3467 parser
->translate_strings_p
= true;
3469 /* We are not parsing a function body. */
3470 parser
->in_function_body
= false;
3472 /* We can correct until told otherwise. */
3473 parser
->colon_corrects_to_scope_p
= true;
3475 /* The unparsed function queue is empty. */
3476 push_unparsed_function_queues (parser
);
3478 /* There are no classes being defined. */
3479 parser
->num_classes_being_defined
= 0;
3481 /* No template parameters apply. */
3482 parser
->num_template_parameter_lists
= 0;
3484 /* Not declaring an implicit function template. */
3485 parser
->auto_is_implicit_function_template_parm_p
= false;
3486 parser
->fully_implicit_function_template_p
= false;
3487 parser
->implicit_template_parms
= 0;
3488 parser
->implicit_template_scope
= 0;
3493 /* Create a cp_lexer structure which will emit the tokens in CACHE
3494 and push it onto the parser's lexer stack. This is used for delayed
3495 parsing of in-class method bodies and default arguments, and should
3496 not be confused with tentative parsing. */
3498 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3500 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3501 lexer
->next
= parser
->lexer
;
3502 parser
->lexer
= lexer
;
3504 /* Move the current source position to that of the first token in the
3506 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3509 /* Pop the top lexer off the parser stack. This is never used for the
3510 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3512 cp_parser_pop_lexer (cp_parser
*parser
)
3514 cp_lexer
*lexer
= parser
->lexer
;
3515 parser
->lexer
= lexer
->next
;
3516 cp_lexer_destroy (lexer
);
3518 /* Put the current source position back where it was before this
3519 lexer was pushed. */
3520 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3523 /* Lexical conventions [gram.lex] */
3525 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3529 cp_parser_identifier (cp_parser
* parser
)
3533 /* Look for the identifier. */
3534 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3535 /* Return the value. */
3536 return token
? token
->u
.value
: error_mark_node
;
3539 /* Parse a sequence of adjacent string constants. Returns a
3540 TREE_STRING representing the combined, nul-terminated string
3541 constant. If TRANSLATE is true, translate the string to the
3542 execution character set. If WIDE_OK is true, a wide string is
3545 C++98 [lex.string] says that if a narrow string literal token is
3546 adjacent to a wide string literal token, the behavior is undefined.
3547 However, C99 6.4.5p4 says that this results in a wide string literal.
3548 We follow C99 here, for consistency with the C front end.
3550 This code is largely lifted from lex_string() in c-lex.c.
3552 FUTURE: ObjC++ will need to handle @-strings here. */
3554 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3558 struct obstack str_ob
;
3559 cpp_string str
, istr
, *strs
;
3561 enum cpp_ttype type
, curr_type
;
3562 int have_suffix_p
= 0;
3564 tree suffix_id
= NULL_TREE
;
3565 bool curr_tok_is_userdef_p
= false;
3567 tok
= cp_lexer_peek_token (parser
->lexer
);
3568 if (!cp_parser_is_string_literal (tok
))
3570 cp_parser_error (parser
, "expected string-literal");
3571 return error_mark_node
;
3574 if (cpp_userdef_string_p (tok
->type
))
3576 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3577 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3578 curr_tok_is_userdef_p
= true;
3582 string_tree
= tok
->u
.value
;
3583 curr_type
= tok
->type
;
3587 /* Try to avoid the overhead of creating and destroying an obstack
3588 for the common case of just one string. */
3589 if (!cp_parser_is_string_literal
3590 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3592 cp_lexer_consume_token (parser
->lexer
);
3594 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3595 str
.len
= TREE_STRING_LENGTH (string_tree
);
3598 if (curr_tok_is_userdef_p
)
3600 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3602 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3605 curr_type
= tok
->type
;
3611 gcc_obstack_init (&str_ob
);
3616 cp_lexer_consume_token (parser
->lexer
);
3618 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3619 str
.len
= TREE_STRING_LENGTH (string_tree
);
3621 if (curr_tok_is_userdef_p
)
3623 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3624 if (have_suffix_p
== 0)
3626 suffix_id
= curr_suffix_id
;
3629 else if (have_suffix_p
== 1
3630 && curr_suffix_id
!= suffix_id
)
3632 error ("inconsistent user-defined literal suffixes"
3633 " %qD and %qD in string literal",
3634 suffix_id
, curr_suffix_id
);
3637 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3640 curr_type
= tok
->type
;
3642 if (type
!= curr_type
)
3644 if (type
== CPP_STRING
)
3646 else if (curr_type
!= CPP_STRING
)
3647 error_at (tok
->location
,
3648 "unsupported non-standard concatenation "
3649 "of string literals");
3652 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3654 tok
= cp_lexer_peek_token (parser
->lexer
);
3655 if (cpp_userdef_string_p (tok
->type
))
3657 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3658 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3659 curr_tok_is_userdef_p
= true;
3663 string_tree
= tok
->u
.value
;
3664 curr_type
= tok
->type
;
3665 curr_tok_is_userdef_p
= false;
3668 while (cp_parser_is_string_literal (tok
));
3670 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3673 if (type
!= CPP_STRING
&& !wide_ok
)
3675 cp_parser_error (parser
, "a wide string is invalid in this context");
3679 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3680 (parse_in
, strs
, count
, &istr
, type
))
3682 value
= build_string (istr
.len
, (const char *)istr
.text
);
3683 free (CONST_CAST (unsigned char *, istr
.text
));
3689 case CPP_UTF8STRING
:
3690 TREE_TYPE (value
) = char_array_type_node
;
3693 TREE_TYPE (value
) = char16_array_type_node
;
3696 TREE_TYPE (value
) = char32_array_type_node
;
3699 TREE_TYPE (value
) = wchar_array_type_node
;
3703 value
= fix_string_type (value
);
3707 tree literal
= build_userdef_literal (suffix_id
, value
,
3708 OT_NONE
, NULL_TREE
);
3709 tok
->u
.value
= literal
;
3710 return cp_parser_userdef_string_literal (tok
);
3714 /* cpp_interpret_string has issued an error. */
3715 value
= error_mark_node
;
3718 obstack_free (&str_ob
, 0);
3723 /* Look up a literal operator with the name and the exact arguments. */
3726 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3729 decl
= lookup_name (name
);
3730 if (!decl
|| !is_overloaded_fn (decl
))
3731 return error_mark_node
;
3733 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3737 tree fn
= OVL_CURRENT (fns
);
3738 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3739 if (parmtypes
!= NULL_TREE
)
3741 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3742 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3744 tree tparm
= TREE_VALUE (parmtypes
);
3745 tree targ
= TREE_TYPE ((*args
)[ix
]);
3746 bool ptr
= TYPE_PTR_P (tparm
);
3747 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3748 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3750 || !same_type_p (TREE_TYPE (tparm
),
3755 && ix
== vec_safe_length (args
)
3756 /* May be this should be sufficient_parms_p instead,
3757 depending on how exactly should user-defined literals
3758 work in presence of default arguments on the literal
3759 operator parameters. */
3760 && parmtypes
== void_list_node
)
3765 return error_mark_node
;
3768 /* Parse a user-defined char constant. Returns a call to a user-defined
3769 literal operator taking the character as an argument. */
3772 cp_parser_userdef_char_literal (cp_parser
*parser
)
3774 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3775 tree literal
= token
->u
.value
;
3776 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3777 tree value
= USERDEF_LITERAL_VALUE (literal
);
3778 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3781 /* Build up a call to the user-defined operator */
3782 /* Lookup the name we got back from the id-expression. */
3783 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3784 vec_safe_push (args
, value
);
3785 decl
= lookup_literal_operator (name
, args
);
3786 if (!decl
|| decl
== error_mark_node
)
3788 error ("unable to find character literal operator %qD with %qT argument",
3789 name
, TREE_TYPE (value
));
3790 release_tree_vector (args
);
3791 return error_mark_node
;
3793 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3794 release_tree_vector (args
);
3795 if (result
!= error_mark_node
)
3798 error ("unable to find character literal operator %qD with %qT argument",
3799 name
, TREE_TYPE (value
));
3800 return error_mark_node
;
3803 /* A subroutine of cp_parser_userdef_numeric_literal to
3804 create a char... template parameter pack from a string node. */
3807 make_char_string_pack (tree value
)
3810 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3811 const char *str
= TREE_STRING_POINTER (value
);
3812 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3813 tree argvec
= make_tree_vec (1);
3815 /* Fill in CHARVEC with all of the parameters. */
3816 charvec
= make_tree_vec (len
);
3817 for (i
= 0; i
< len
; ++i
)
3818 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3820 /* Build the argument packs. */
3821 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3822 TREE_TYPE (argpack
) = char_type_node
;
3824 TREE_VEC_ELT (argvec
, 0) = argpack
;
3829 /* A subroutine of cp_parser_userdef_numeric_literal to
3830 create a char... template parameter pack from a string node. */
3833 make_string_pack (tree value
)
3836 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3837 const unsigned char *str
3838 = (const unsigned char *) TREE_STRING_POINTER (value
);
3839 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3840 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3841 tree argvec
= make_tree_vec (2);
3843 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3844 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3846 /* First template parm is character type. */
3847 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3849 /* Fill in CHARVEC with all of the parameters. */
3850 charvec
= make_tree_vec (len
);
3851 for (int i
= 0; i
< len
; ++i
)
3852 TREE_VEC_ELT (charvec
, i
)
3853 = double_int_to_tree (str_char_type_node
,
3854 double_int::from_buffer (str
+ i
* sz
, sz
));
3856 /* Build the argument packs. */
3857 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3858 TREE_TYPE (argpack
) = str_char_type_node
;
3860 TREE_VEC_ELT (argvec
, 1) = argpack
;
3865 /* Parse a user-defined numeric constant. returns a call to a user-defined
3866 literal operator. */
3869 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3871 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3872 tree literal
= token
->u
.value
;
3873 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3874 tree value
= USERDEF_LITERAL_VALUE (literal
);
3875 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3876 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3877 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3879 vec
<tree
, va_gc
> *args
;
3881 /* Look for a literal operator taking the exact type of numeric argument
3882 as the literal value. */
3883 args
= make_tree_vector ();
3884 vec_safe_push (args
, value
);
3885 decl
= lookup_literal_operator (name
, args
);
3886 if (decl
&& decl
!= error_mark_node
)
3888 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3889 if (result
!= error_mark_node
)
3891 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3892 warning_at (token
->location
, OPT_Woverflow
,
3893 "integer literal exceeds range of %qT type",
3894 long_long_unsigned_type_node
);
3898 warning_at (token
->location
, OPT_Woverflow
,
3899 "floating literal exceeds range of %qT type",
3900 long_double_type_node
);
3901 else if (overflow
< 0)
3902 warning_at (token
->location
, OPT_Woverflow
,
3903 "floating literal truncated to zero");
3905 release_tree_vector (args
);
3909 release_tree_vector (args
);
3911 /* If the numeric argument didn't work, look for a raw literal
3912 operator taking a const char* argument consisting of the number
3913 in string format. */
3914 args
= make_tree_vector ();
3915 vec_safe_push (args
, num_string
);
3916 decl
= lookup_literal_operator (name
, args
);
3917 if (decl
&& decl
!= error_mark_node
)
3919 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3920 if (result
!= error_mark_node
)
3922 release_tree_vector (args
);
3926 release_tree_vector (args
);
3928 /* If the raw literal didn't work, look for a non-type template
3929 function with parameter pack char.... Call the function with
3930 template parameter characters representing the number. */
3931 args
= make_tree_vector ();
3932 decl
= lookup_literal_operator (name
, args
);
3933 if (decl
&& decl
!= error_mark_node
)
3935 tree tmpl_args
= make_char_string_pack (num_string
);
3936 decl
= lookup_template_function (decl
, tmpl_args
);
3937 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3938 if (result
!= error_mark_node
)
3940 release_tree_vector (args
);
3944 release_tree_vector (args
);
3946 error ("unable to find numeric literal operator %qD", name
);
3947 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
3948 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
3949 "to enable more built-in suffixes");
3950 return error_mark_node
;
3953 /* Parse a user-defined string constant. Returns a call to a user-defined
3954 literal operator taking a character pointer and the length of the string
3958 cp_parser_userdef_string_literal (cp_token
*token
)
3960 tree literal
= token
->u
.value
;
3961 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3962 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3963 tree value
= USERDEF_LITERAL_VALUE (literal
);
3964 int len
= TREE_STRING_LENGTH (value
)
3965 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
3967 vec
<tree
, va_gc
> *args
;
3969 /* Look for a template function with typename parameter CharT
3970 and parameter pack CharT... Call the function with
3971 template parameter characters representing the string. */
3972 args
= make_tree_vector ();
3973 decl
= lookup_literal_operator (name
, args
);
3974 if (decl
&& decl
!= error_mark_node
)
3976 tree tmpl_args
= make_string_pack (value
);
3977 decl
= lookup_template_function (decl
, tmpl_args
);
3978 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3979 if (result
!= error_mark_node
)
3981 release_tree_vector (args
);
3985 release_tree_vector (args
);
3987 /* Build up a call to the user-defined operator */
3988 /* Lookup the name we got back from the id-expression. */
3989 args
= make_tree_vector ();
3990 vec_safe_push (args
, value
);
3991 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
3992 decl
= lookup_name (name
);
3993 if (!decl
|| decl
== error_mark_node
)
3995 error ("unable to find string literal operator %qD", name
);
3996 release_tree_vector (args
);
3997 return error_mark_node
;
3999 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4000 release_tree_vector (args
);
4001 if (result
!= error_mark_node
)
4004 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4005 name
, TREE_TYPE (value
), size_type_node
);
4006 return error_mark_node
;
4010 /* Basic concepts [gram.basic] */
4012 /* Parse a translation-unit.
4015 declaration-seq [opt]
4017 Returns TRUE if all went well. */
4020 cp_parser_translation_unit (cp_parser
* parser
)
4022 /* The address of the first non-permanent object on the declarator
4024 static void *declarator_obstack_base
;
4028 /* Create the declarator obstack, if necessary. */
4029 if (!cp_error_declarator
)
4031 gcc_obstack_init (&declarator_obstack
);
4032 /* Create the error declarator. */
4033 cp_error_declarator
= make_declarator (cdk_error
);
4034 /* Create the empty parameter list. */
4035 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4036 /* Remember where the base of the declarator obstack lies. */
4037 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4040 cp_parser_declaration_seq_opt (parser
);
4042 /* If there are no tokens left then all went well. */
4043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4045 /* Get rid of the token array; we don't need it any more. */
4046 cp_lexer_destroy (parser
->lexer
);
4047 parser
->lexer
= NULL
;
4049 /* This file might have been a context that's implicitly extern
4050 "C". If so, pop the lang context. (Only relevant for PCH.) */
4051 if (parser
->implicit_extern_c
)
4053 pop_lang_context ();
4054 parser
->implicit_extern_c
= false;
4058 finish_translation_unit ();
4064 cp_parser_error (parser
, "expected declaration");
4068 /* Make sure the declarator obstack was fully cleaned up. */
4069 gcc_assert (obstack_next_free (&declarator_obstack
)
4070 == declarator_obstack_base
);
4072 /* All went well. */
4076 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4077 decltype context. */
4079 static inline tsubst_flags_t
4080 complain_flags (bool decltype_p
)
4082 tsubst_flags_t complain
= tf_warning_or_error
;
4084 complain
|= tf_decltype
;
4088 /* Expressions [gram.expr] */
4090 /* Parse a primary-expression.
4101 ( compound-statement )
4102 __builtin_va_arg ( assignment-expression , type-id )
4103 __builtin_offsetof ( type-id , offsetof-expression )
4106 __has_nothrow_assign ( type-id )
4107 __has_nothrow_constructor ( type-id )
4108 __has_nothrow_copy ( type-id )
4109 __has_trivial_assign ( type-id )
4110 __has_trivial_constructor ( type-id )
4111 __has_trivial_copy ( type-id )
4112 __has_trivial_destructor ( type-id )
4113 __has_virtual_destructor ( type-id )
4114 __is_abstract ( type-id )
4115 __is_base_of ( type-id , type-id )
4116 __is_class ( type-id )
4117 __is_convertible_to ( type-id , type-id )
4118 __is_empty ( type-id )
4119 __is_enum ( type-id )
4120 __is_final ( type-id )
4121 __is_literal_type ( type-id )
4122 __is_pod ( type-id )
4123 __is_polymorphic ( type-id )
4124 __is_std_layout ( type-id )
4125 __is_trivial ( type-id )
4126 __is_union ( type-id )
4128 Objective-C++ Extension:
4136 ADDRESS_P is true iff this expression was immediately preceded by
4137 "&" and therefore might denote a pointer-to-member. CAST_P is true
4138 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4139 true iff this expression is a template argument.
4141 Returns a representation of the expression. Upon return, *IDK
4142 indicates what kind of id-expression (if any) was present. */
4145 cp_parser_primary_expression (cp_parser
*parser
,
4148 bool template_arg_p
,
4152 cp_token
*token
= NULL
;
4154 /* Assume the primary expression is not an id-expression. */
4155 *idk
= CP_ID_KIND_NONE
;
4157 /* Peek at the next token. */
4158 token
= cp_lexer_peek_token (parser
->lexer
);
4159 switch (token
->type
)
4168 user-defined-literal */
4174 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4175 return cp_parser_userdef_numeric_literal (parser
);
4176 token
= cp_lexer_consume_token (parser
->lexer
);
4177 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4179 error_at (token
->location
,
4180 "fixed-point types not supported in C++");
4181 return error_mark_node
;
4183 /* Floating-point literals are only allowed in an integral
4184 constant expression if they are cast to an integral or
4185 enumeration type. */
4186 if (TREE_CODE (token
->u
.value
) == REAL_CST
4187 && parser
->integral_constant_expression_p
4190 /* CAST_P will be set even in invalid code like "int(2.7 +
4191 ...)". Therefore, we have to check that the next token
4192 is sure to end the cast. */
4195 cp_token
*next_token
;
4197 next_token
= cp_lexer_peek_token (parser
->lexer
);
4198 if (/* The comma at the end of an
4199 enumerator-definition. */
4200 next_token
->type
!= CPP_COMMA
4201 /* The curly brace at the end of an enum-specifier. */
4202 && next_token
->type
!= CPP_CLOSE_BRACE
4203 /* The end of a statement. */
4204 && next_token
->type
!= CPP_SEMICOLON
4205 /* The end of the cast-expression. */
4206 && next_token
->type
!= CPP_CLOSE_PAREN
4207 /* The end of an array bound. */
4208 && next_token
->type
!= CPP_CLOSE_SQUARE
4209 /* The closing ">" in a template-argument-list. */
4210 && (next_token
->type
!= CPP_GREATER
4211 || parser
->greater_than_is_operator_p
)
4212 /* C++0x only: A ">>" treated like two ">" tokens,
4213 in a template-argument-list. */
4214 && (next_token
->type
!= CPP_RSHIFT
4215 || (cxx_dialect
== cxx98
)
4216 || parser
->greater_than_is_operator_p
))
4220 /* If we are within a cast, then the constraint that the
4221 cast is to an integral or enumeration type will be
4222 checked at that point. If we are not within a cast, then
4223 this code is invalid. */
4225 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4227 return token
->u
.value
;
4229 case CPP_CHAR_USERDEF
:
4230 case CPP_CHAR16_USERDEF
:
4231 case CPP_CHAR32_USERDEF
:
4232 case CPP_WCHAR_USERDEF
:
4233 return cp_parser_userdef_char_literal (parser
);
4239 case CPP_UTF8STRING
:
4240 case CPP_STRING_USERDEF
:
4241 case CPP_STRING16_USERDEF
:
4242 case CPP_STRING32_USERDEF
:
4243 case CPP_WSTRING_USERDEF
:
4244 case CPP_UTF8STRING_USERDEF
:
4245 /* ??? Should wide strings be allowed when parser->translate_strings_p
4246 is false (i.e. in attributes)? If not, we can kill the third
4247 argument to cp_parser_string_literal. */
4248 return cp_parser_string_literal (parser
,
4249 parser
->translate_strings_p
,
4252 case CPP_OPEN_PAREN
:
4255 bool saved_greater_than_is_operator_p
;
4257 /* Consume the `('. */
4258 cp_lexer_consume_token (parser
->lexer
);
4259 /* Within a parenthesized expression, a `>' token is always
4260 the greater-than operator. */
4261 saved_greater_than_is_operator_p
4262 = parser
->greater_than_is_operator_p
;
4263 parser
->greater_than_is_operator_p
= true;
4264 /* If we see `( { ' then we are looking at the beginning of
4265 a GNU statement-expression. */
4266 if (cp_parser_allow_gnu_extensions_p (parser
)
4267 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
4269 /* Statement-expressions are not allowed by the standard. */
4270 pedwarn (token
->location
, OPT_Wpedantic
,
4271 "ISO C++ forbids braced-groups within expressions");
4273 /* And they're not allowed outside of a function-body; you
4274 cannot, for example, write:
4276 int i = ({ int j = 3; j + 1; });
4278 at class or namespace scope. */
4279 if (!parser
->in_function_body
4280 || parser
->in_template_argument_list_p
)
4282 error_at (token
->location
,
4283 "statement-expressions are not allowed outside "
4284 "functions nor in template-argument lists");
4285 cp_parser_skip_to_end_of_block_or_statement (parser
);
4286 expr
= error_mark_node
;
4290 /* Start the statement-expression. */
4291 expr
= begin_stmt_expr ();
4292 /* Parse the compound-statement. */
4293 cp_parser_compound_statement (parser
, expr
, false, false);
4295 expr
= finish_stmt_expr (expr
, false);
4300 /* Parse the parenthesized expression. */
4301 expr
= cp_parser_expression (parser
, cast_p
, decltype_p
, idk
);
4302 /* Let the front end know that this expression was
4303 enclosed in parentheses. This matters in case, for
4304 example, the expression is of the form `A::B', since
4305 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4307 expr
= finish_parenthesized_expr (expr
);
4308 /* DR 705: Wrapping an unqualified name in parentheses
4309 suppresses arg-dependent lookup. We want to pass back
4310 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4311 (c++/37862), but none of the others. */
4312 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4313 *idk
= CP_ID_KIND_NONE
;
4315 /* The `>' token might be the end of a template-id or
4316 template-parameter-list now. */
4317 parser
->greater_than_is_operator_p
4318 = saved_greater_than_is_operator_p
;
4319 /* Consume the `)'. */
4320 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4321 cp_parser_skip_to_end_of_statement (parser
);
4326 case CPP_OPEN_SQUARE
:
4327 if (c_dialect_objc ())
4328 /* We have an Objective-C++ message. */
4329 return cp_parser_objc_expression (parser
);
4331 tree lam
= cp_parser_lambda_expression (parser
);
4332 /* Don't warn about a failed tentative parse. */
4333 if (cp_parser_error_occurred (parser
))
4334 return error_mark_node
;
4335 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4339 case CPP_OBJC_STRING
:
4340 if (c_dialect_objc ())
4341 /* We have an Objective-C++ string literal. */
4342 return cp_parser_objc_expression (parser
);
4343 cp_parser_error (parser
, "expected primary-expression");
4344 return error_mark_node
;
4347 switch (token
->keyword
)
4349 /* These two are the boolean literals. */
4351 cp_lexer_consume_token (parser
->lexer
);
4352 return boolean_true_node
;
4354 cp_lexer_consume_token (parser
->lexer
);
4355 return boolean_false_node
;
4357 /* The `__null' literal. */
4359 cp_lexer_consume_token (parser
->lexer
);
4362 /* The `nullptr' literal. */
4364 cp_lexer_consume_token (parser
->lexer
);
4365 return nullptr_node
;
4367 /* Recognize the `this' keyword. */
4369 cp_lexer_consume_token (parser
->lexer
);
4370 if (parser
->local_variables_forbidden_p
)
4372 error_at (token
->location
,
4373 "%<this%> may not be used in this context");
4374 return error_mark_node
;
4376 /* Pointers cannot appear in constant-expressions. */
4377 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4378 return error_mark_node
;
4379 return finish_this_expr ();
4381 /* The `operator' keyword can be the beginning of an
4386 case RID_FUNCTION_NAME
:
4387 case RID_PRETTY_FUNCTION_NAME
:
4388 case RID_C99_FUNCTION_NAME
:
4390 non_integral_constant name
;
4392 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4393 __func__ are the names of variables -- but they are
4394 treated specially. Therefore, they are handled here,
4395 rather than relying on the generic id-expression logic
4396 below. Grammatically, these names are id-expressions.
4398 Consume the token. */
4399 token
= cp_lexer_consume_token (parser
->lexer
);
4401 switch (token
->keyword
)
4403 case RID_FUNCTION_NAME
:
4404 name
= NIC_FUNC_NAME
;
4406 case RID_PRETTY_FUNCTION_NAME
:
4407 name
= NIC_PRETTY_FUNC
;
4409 case RID_C99_FUNCTION_NAME
:
4410 name
= NIC_C99_FUNC
;
4416 if (cp_parser_non_integral_constant_expression (parser
, name
))
4417 return error_mark_node
;
4419 /* Look up the name. */
4420 return finish_fname (token
->u
.value
);
4427 source_location type_location
;
4429 /* The `__builtin_va_arg' construct is used to handle
4430 `va_arg'. Consume the `__builtin_va_arg' token. */
4431 cp_lexer_consume_token (parser
->lexer
);
4432 /* Look for the opening `('. */
4433 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4434 /* Now, parse the assignment-expression. */
4435 expression
= cp_parser_assignment_expression (parser
,
4436 /*cast_p=*/false, NULL
);
4437 /* Look for the `,'. */
4438 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4439 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4440 /* Parse the type-id. */
4441 type
= cp_parser_type_id (parser
);
4442 /* Look for the closing `)'. */
4443 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4444 /* Using `va_arg' in a constant-expression is not
4446 if (cp_parser_non_integral_constant_expression (parser
,
4448 return error_mark_node
;
4449 return build_x_va_arg (type_location
, expression
, type
);
4453 return cp_parser_builtin_offsetof (parser
);
4455 case RID_HAS_NOTHROW_ASSIGN
:
4456 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4457 case RID_HAS_NOTHROW_COPY
:
4458 case RID_HAS_TRIVIAL_ASSIGN
:
4459 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4460 case RID_HAS_TRIVIAL_COPY
:
4461 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4462 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4463 case RID_IS_ABSTRACT
:
4464 case RID_IS_BASE_OF
:
4466 case RID_IS_CONVERTIBLE_TO
:
4470 case RID_IS_LITERAL_TYPE
:
4472 case RID_IS_POLYMORPHIC
:
4473 case RID_IS_STD_LAYOUT
:
4474 case RID_IS_TRIVIAL
:
4476 return cp_parser_trait_expr (parser
, token
->keyword
);
4478 /* Objective-C++ expressions. */
4480 case RID_AT_PROTOCOL
:
4481 case RID_AT_SELECTOR
:
4482 return cp_parser_objc_expression (parser
);
4485 if (parser
->in_function_body
4486 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4489 error_at (token
->location
,
4490 "a template declaration cannot appear at block scope");
4491 cp_parser_skip_to_end_of_block_or_statement (parser
);
4492 return error_mark_node
;
4495 cp_parser_error (parser
, "expected primary-expression");
4496 return error_mark_node
;
4499 /* An id-expression can start with either an identifier, a
4500 `::' as the beginning of a qualified-id, or the "operator"
4504 case CPP_TEMPLATE_ID
:
4505 case CPP_NESTED_NAME_SPECIFIER
:
4509 const char *error_msg
;
4512 cp_token
*id_expr_token
;
4515 /* Parse the id-expression. */
4517 = cp_parser_id_expression (parser
,
4518 /*template_keyword_p=*/false,
4519 /*check_dependency_p=*/true,
4521 /*declarator_p=*/false,
4522 /*optional_p=*/false);
4523 if (id_expression
== error_mark_node
)
4524 return error_mark_node
;
4525 id_expr_token
= token
;
4526 token
= cp_lexer_peek_token (parser
->lexer
);
4527 done
= (token
->type
!= CPP_OPEN_SQUARE
4528 && token
->type
!= CPP_OPEN_PAREN
4529 && token
->type
!= CPP_DOT
4530 && token
->type
!= CPP_DEREF
4531 && token
->type
!= CPP_PLUS_PLUS
4532 && token
->type
!= CPP_MINUS_MINUS
);
4533 /* If we have a template-id, then no further lookup is
4534 required. If the template-id was for a template-class, we
4535 will sometimes have a TYPE_DECL at this point. */
4536 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4537 || TREE_CODE (id_expression
) == TYPE_DECL
)
4538 decl
= id_expression
;
4539 /* Look up the name. */
4542 tree ambiguous_decls
;
4544 /* If we already know that this lookup is ambiguous, then
4545 we've already issued an error message; there's no reason
4547 if (id_expr_token
->type
== CPP_NAME
4548 && id_expr_token
->error_reported
)
4550 cp_parser_simulate_error (parser
);
4551 return error_mark_node
;
4554 decl
= cp_parser_lookup_name (parser
, id_expression
,
4557 /*is_namespace=*/false,
4558 /*check_dependency=*/true,
4560 id_expr_token
->location
);
4561 /* If the lookup was ambiguous, an error will already have
4563 if (ambiguous_decls
)
4564 return error_mark_node
;
4566 /* In Objective-C++, we may have an Objective-C 2.0
4567 dot-syntax for classes here. */
4568 if (c_dialect_objc ()
4569 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4570 && TREE_CODE (decl
) == TYPE_DECL
4571 && objc_is_class_name (decl
))
4574 cp_lexer_consume_token (parser
->lexer
);
4575 component
= cp_parser_identifier (parser
);
4576 if (component
== error_mark_node
)
4577 return error_mark_node
;
4579 return objc_build_class_component_ref (id_expression
, component
);
4582 /* In Objective-C++, an instance variable (ivar) may be preferred
4583 to whatever cp_parser_lookup_name() found. */
4584 decl
= objc_lookup_ivar (decl
, id_expression
);
4586 /* If name lookup gives us a SCOPE_REF, then the
4587 qualifying scope was dependent. */
4588 if (TREE_CODE (decl
) == SCOPE_REF
)
4590 /* At this point, we do not know if DECL is a valid
4591 integral constant expression. We assume that it is
4592 in fact such an expression, so that code like:
4594 template <int N> struct A {
4598 is accepted. At template-instantiation time, we
4599 will check that B<N>::i is actually a constant. */
4602 /* Check to see if DECL is a local variable in a context
4603 where that is forbidden. */
4604 if (parser
->local_variables_forbidden_p
4605 && local_variable_p (decl
))
4607 /* It might be that we only found DECL because we are
4608 trying to be generous with pre-ISO scoping rules.
4609 For example, consider:
4613 for (int i = 0; i < 10; ++i) {}
4614 extern void f(int j = i);
4617 Here, name look up will originally find the out
4618 of scope `i'. We need to issue a warning message,
4619 but then use the global `i'. */
4620 decl
= check_for_out_of_scope_variable (decl
);
4621 if (local_variable_p (decl
))
4623 error_at (id_expr_token
->location
,
4624 "local variable %qD may not appear in this context",
4626 return error_mark_node
;
4631 decl
= (finish_id_expression
4632 (id_expression
, decl
, parser
->scope
,
4634 parser
->integral_constant_expression_p
,
4635 parser
->allow_non_integral_constant_expression_p
,
4636 &parser
->non_integral_constant_expression_p
,
4637 template_p
, done
, address_p
,
4640 id_expr_token
->location
));
4642 cp_parser_error (parser
, error_msg
);
4646 /* Anything else is an error. */
4648 cp_parser_error (parser
, "expected primary-expression");
4649 return error_mark_node
;
4654 cp_parser_primary_expression (cp_parser
*parser
,
4657 bool template_arg_p
,
4660 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4661 /*decltype*/false, idk
);
4664 /* Parse an id-expression.
4671 :: [opt] nested-name-specifier template [opt] unqualified-id
4673 :: operator-function-id
4676 Return a representation of the unqualified portion of the
4677 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4678 a `::' or nested-name-specifier.
4680 Often, if the id-expression was a qualified-id, the caller will
4681 want to make a SCOPE_REF to represent the qualified-id. This
4682 function does not do this in order to avoid wastefully creating
4683 SCOPE_REFs when they are not required.
4685 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4688 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4689 uninstantiated templates.
4691 If *TEMPLATE_P is non-NULL, it is set to true iff the
4692 `template' keyword is used to explicitly indicate that the entity
4693 named is a template.
4695 If DECLARATOR_P is true, the id-expression is appearing as part of
4696 a declarator, rather than as part of an expression. */
4699 cp_parser_id_expression (cp_parser
*parser
,
4700 bool template_keyword_p
,
4701 bool check_dependency_p
,
4706 bool global_scope_p
;
4707 bool nested_name_specifier_p
;
4709 /* Assume the `template' keyword was not used. */
4711 *template_p
= template_keyword_p
;
4713 /* Look for the optional `::' operator. */
4715 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4717 /* Look for the optional nested-name-specifier. */
4718 nested_name_specifier_p
4719 = (cp_parser_nested_name_specifier_opt (parser
,
4720 /*typename_keyword_p=*/false,
4725 /* If there is a nested-name-specifier, then we are looking at
4726 the first qualified-id production. */
4727 if (nested_name_specifier_p
)
4730 tree saved_object_scope
;
4731 tree saved_qualifying_scope
;
4732 tree unqualified_id
;
4735 /* See if the next token is the `template' keyword. */
4737 template_p
= &is_template
;
4738 *template_p
= cp_parser_optional_template_keyword (parser
);
4739 /* Name lookup we do during the processing of the
4740 unqualified-id might obliterate SCOPE. */
4741 saved_scope
= parser
->scope
;
4742 saved_object_scope
= parser
->object_scope
;
4743 saved_qualifying_scope
= parser
->qualifying_scope
;
4744 /* Process the final unqualified-id. */
4745 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4748 /*optional_p=*/false);
4749 /* Restore the SAVED_SCOPE for our caller. */
4750 parser
->scope
= saved_scope
;
4751 parser
->object_scope
= saved_object_scope
;
4752 parser
->qualifying_scope
= saved_qualifying_scope
;
4754 return unqualified_id
;
4756 /* Otherwise, if we are in global scope, then we are looking at one
4757 of the other qualified-id productions. */
4758 else if (global_scope_p
)
4763 /* Peek at the next token. */
4764 token
= cp_lexer_peek_token (parser
->lexer
);
4766 /* If it's an identifier, and the next token is not a "<", then
4767 we can avoid the template-id case. This is an optimization
4768 for this common case. */
4769 if (token
->type
== CPP_NAME
4770 && !cp_parser_nth_token_starts_template_argument_list_p
4772 return cp_parser_identifier (parser
);
4774 cp_parser_parse_tentatively (parser
);
4775 /* Try a template-id. */
4776 id
= cp_parser_template_id (parser
,
4777 /*template_keyword_p=*/false,
4778 /*check_dependency_p=*/true,
4781 /* If that worked, we're done. */
4782 if (cp_parser_parse_definitely (parser
))
4785 /* Peek at the next token. (Changes in the token buffer may
4786 have invalidated the pointer obtained above.) */
4787 token
= cp_lexer_peek_token (parser
->lexer
);
4789 switch (token
->type
)
4792 return cp_parser_identifier (parser
);
4795 if (token
->keyword
== RID_OPERATOR
)
4796 return cp_parser_operator_function_id (parser
);
4800 cp_parser_error (parser
, "expected id-expression");
4801 return error_mark_node
;
4805 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4806 /*check_dependency_p=*/true,
4811 /* Parse an unqualified-id.
4815 operator-function-id
4816 conversion-function-id
4820 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4821 keyword, in a construct like `A::template ...'.
4823 Returns a representation of unqualified-id. For the `identifier'
4824 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4825 production a BIT_NOT_EXPR is returned; the operand of the
4826 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4827 other productions, see the documentation accompanying the
4828 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4829 names are looked up in uninstantiated templates. If DECLARATOR_P
4830 is true, the unqualified-id is appearing as part of a declarator,
4831 rather than as part of an expression. */
4834 cp_parser_unqualified_id (cp_parser
* parser
,
4835 bool template_keyword_p
,
4836 bool check_dependency_p
,
4842 /* Peek at the next token. */
4843 token
= cp_lexer_peek_token (parser
->lexer
);
4845 switch (token
->type
)
4851 /* We don't know yet whether or not this will be a
4853 cp_parser_parse_tentatively (parser
);
4854 /* Try a template-id. */
4855 id
= cp_parser_template_id (parser
, template_keyword_p
,
4859 /* If it worked, we're done. */
4860 if (cp_parser_parse_definitely (parser
))
4862 /* Otherwise, it's an ordinary identifier. */
4863 return cp_parser_identifier (parser
);
4866 case CPP_TEMPLATE_ID
:
4867 return cp_parser_template_id (parser
, template_keyword_p
,
4875 tree qualifying_scope
;
4880 /* Consume the `~' token. */
4881 cp_lexer_consume_token (parser
->lexer
);
4882 /* Parse the class-name. The standard, as written, seems to
4885 template <typename T> struct S { ~S (); };
4886 template <typename T> S<T>::~S() {}
4888 is invalid, since `~' must be followed by a class-name, but
4889 `S<T>' is dependent, and so not known to be a class.
4890 That's not right; we need to look in uninstantiated
4891 templates. A further complication arises from:
4893 template <typename T> void f(T t) {
4897 Here, it is not possible to look up `T' in the scope of `T'
4898 itself. We must look in both the current scope, and the
4899 scope of the containing complete expression.
4901 Yet another issue is:
4910 The standard does not seem to say that the `S' in `~S'
4911 should refer to the type `S' and not the data member
4914 /* DR 244 says that we look up the name after the "~" in the
4915 same scope as we looked up the qualifying name. That idea
4916 isn't fully worked out; it's more complicated than that. */
4917 scope
= parser
->scope
;
4918 object_scope
= parser
->object_scope
;
4919 qualifying_scope
= parser
->qualifying_scope
;
4921 /* Check for invalid scopes. */
4922 if (scope
== error_mark_node
)
4924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4925 cp_lexer_consume_token (parser
->lexer
);
4926 return error_mark_node
;
4928 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4930 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4931 error_at (token
->location
,
4932 "scope %qT before %<~%> is not a class-name",
4934 cp_parser_simulate_error (parser
);
4935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4936 cp_lexer_consume_token (parser
->lexer
);
4937 return error_mark_node
;
4939 gcc_assert (!scope
|| TYPE_P (scope
));
4941 /* If the name is of the form "X::~X" it's OK even if X is a
4943 token
= cp_lexer_peek_token (parser
->lexer
);
4945 && token
->type
== CPP_NAME
4946 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4948 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4949 || (CLASS_TYPE_P (scope
)
4950 && constructor_name_p (token
->u
.value
, scope
))))
4952 cp_lexer_consume_token (parser
->lexer
);
4953 return build_nt (BIT_NOT_EXPR
, scope
);
4956 /* ~auto means the destructor of whatever the object is. */
4957 if (cp_parser_is_keyword (token
, RID_AUTO
))
4959 if (cxx_dialect
< cxx1y
)
4960 pedwarn (input_location
, 0,
4961 "%<~auto%> only available with "
4962 "-std=c++1y or -std=gnu++1y");
4963 cp_lexer_consume_token (parser
->lexer
);
4964 return build_nt (BIT_NOT_EXPR
, make_auto ());
4967 /* If there was an explicit qualification (S::~T), first look
4968 in the scope given by the qualification (i.e., S).
4970 Note: in the calls to cp_parser_class_name below we pass
4971 typename_type so that lookup finds the injected-class-name
4972 rather than the constructor. */
4974 type_decl
= NULL_TREE
;
4977 cp_parser_parse_tentatively (parser
);
4978 type_decl
= cp_parser_class_name (parser
,
4979 /*typename_keyword_p=*/false,
4980 /*template_keyword_p=*/false,
4982 /*check_dependency=*/false,
4983 /*class_head_p=*/false,
4985 if (cp_parser_parse_definitely (parser
))
4988 /* In "N::S::~S", look in "N" as well. */
4989 if (!done
&& scope
&& qualifying_scope
)
4991 cp_parser_parse_tentatively (parser
);
4992 parser
->scope
= qualifying_scope
;
4993 parser
->object_scope
= NULL_TREE
;
4994 parser
->qualifying_scope
= NULL_TREE
;
4996 = cp_parser_class_name (parser
,
4997 /*typename_keyword_p=*/false,
4998 /*template_keyword_p=*/false,
5000 /*check_dependency=*/false,
5001 /*class_head_p=*/false,
5003 if (cp_parser_parse_definitely (parser
))
5006 /* In "p->S::~T", look in the scope given by "*p" as well. */
5007 else if (!done
&& object_scope
)
5009 cp_parser_parse_tentatively (parser
);
5010 parser
->scope
= object_scope
;
5011 parser
->object_scope
= NULL_TREE
;
5012 parser
->qualifying_scope
= NULL_TREE
;
5014 = cp_parser_class_name (parser
,
5015 /*typename_keyword_p=*/false,
5016 /*template_keyword_p=*/false,
5018 /*check_dependency=*/false,
5019 /*class_head_p=*/false,
5021 if (cp_parser_parse_definitely (parser
))
5024 /* Look in the surrounding context. */
5027 parser
->scope
= NULL_TREE
;
5028 parser
->object_scope
= NULL_TREE
;
5029 parser
->qualifying_scope
= NULL_TREE
;
5030 if (processing_template_decl
)
5031 cp_parser_parse_tentatively (parser
);
5033 = cp_parser_class_name (parser
,
5034 /*typename_keyword_p=*/false,
5035 /*template_keyword_p=*/false,
5037 /*check_dependency=*/false,
5038 /*class_head_p=*/false,
5040 if (processing_template_decl
5041 && ! cp_parser_parse_definitely (parser
))
5043 /* We couldn't find a type with this name, so just accept
5044 it and check for a match at instantiation time. */
5045 type_decl
= cp_parser_identifier (parser
);
5046 if (type_decl
!= error_mark_node
)
5047 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5051 /* If an error occurred, assume that the name of the
5052 destructor is the same as the name of the qualifying
5053 class. That allows us to keep parsing after running
5054 into ill-formed destructor names. */
5055 if (type_decl
== error_mark_node
&& scope
)
5056 return build_nt (BIT_NOT_EXPR
, scope
);
5057 else if (type_decl
== error_mark_node
)
5058 return error_mark_node
;
5060 /* Check that destructor name and scope match. */
5061 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5063 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5064 error_at (token
->location
,
5065 "declaration of %<~%T%> as member of %qT",
5067 cp_parser_simulate_error (parser
);
5068 return error_mark_node
;
5073 A typedef-name that names a class shall not be used as the
5074 identifier in the declarator for a destructor declaration. */
5076 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5077 && !DECL_SELF_REFERENCE_P (type_decl
)
5078 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5079 error_at (token
->location
,
5080 "typedef-name %qD used as destructor declarator",
5083 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5087 if (token
->keyword
== RID_OPERATOR
)
5091 /* This could be a template-id, so we try that first. */
5092 cp_parser_parse_tentatively (parser
);
5093 /* Try a template-id. */
5094 id
= cp_parser_template_id (parser
, template_keyword_p
,
5095 /*check_dependency_p=*/true,
5098 /* If that worked, we're done. */
5099 if (cp_parser_parse_definitely (parser
))
5101 /* We still don't know whether we're looking at an
5102 operator-function-id or a conversion-function-id. */
5103 cp_parser_parse_tentatively (parser
);
5104 /* Try an operator-function-id. */
5105 id
= cp_parser_operator_function_id (parser
);
5106 /* If that didn't work, try a conversion-function-id. */
5107 if (!cp_parser_parse_definitely (parser
))
5108 id
= cp_parser_conversion_function_id (parser
);
5109 else if (UDLIT_OPER_P (id
))
5112 const char *name
= UDLIT_OP_SUFFIX (id
);
5113 if (name
[0] != '_' && !in_system_header_at (input_location
)
5115 warning (0, "literal operator suffixes not preceded by %<_%>"
5116 " are reserved for future standardization");
5126 cp_parser_error (parser
, "expected unqualified-id");
5127 return error_mark_node
;
5131 /* Parse an (optional) nested-name-specifier.
5133 nested-name-specifier: [C++98]
5134 class-or-namespace-name :: nested-name-specifier [opt]
5135 class-or-namespace-name :: template nested-name-specifier [opt]
5137 nested-name-specifier: [C++0x]
5140 nested-name-specifier identifier ::
5141 nested-name-specifier template [opt] simple-template-id ::
5143 PARSER->SCOPE should be set appropriately before this function is
5144 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5145 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5148 Sets PARSER->SCOPE to the class (TYPE) or namespace
5149 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5150 it unchanged if there is no nested-name-specifier. Returns the new
5151 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5153 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5154 part of a declaration and/or decl-specifier. */
5157 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5158 bool typename_keyword_p
,
5159 bool check_dependency_p
,
5161 bool is_declaration
)
5163 bool success
= false;
5164 cp_token_position start
= 0;
5167 /* Remember where the nested-name-specifier starts. */
5168 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5170 start
= cp_lexer_token_position (parser
->lexer
, false);
5171 push_deferring_access_checks (dk_deferred
);
5178 tree saved_qualifying_scope
;
5179 bool template_keyword_p
;
5181 /* Spot cases that cannot be the beginning of a
5182 nested-name-specifier. */
5183 token
= cp_lexer_peek_token (parser
->lexer
);
5185 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5186 the already parsed nested-name-specifier. */
5187 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5189 /* Grab the nested-name-specifier and continue the loop. */
5190 cp_parser_pre_parsed_nested_name_specifier (parser
);
5191 /* If we originally encountered this nested-name-specifier
5192 with IS_DECLARATION set to false, we will not have
5193 resolved TYPENAME_TYPEs, so we must do so here. */
5195 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5197 new_scope
= resolve_typename_type (parser
->scope
,
5198 /*only_current_p=*/false);
5199 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5200 parser
->scope
= new_scope
;
5206 /* Spot cases that cannot be the beginning of a
5207 nested-name-specifier. On the second and subsequent times
5208 through the loop, we look for the `template' keyword. */
5209 if (success
&& token
->keyword
== RID_TEMPLATE
)
5211 /* A template-id can start a nested-name-specifier. */
5212 else if (token
->type
== CPP_TEMPLATE_ID
)
5214 /* DR 743: decltype can be used in a nested-name-specifier. */
5215 else if (token_is_decltype (token
))
5219 /* If the next token is not an identifier, then it is
5220 definitely not a type-name or namespace-name. */
5221 if (token
->type
!= CPP_NAME
)
5223 /* If the following token is neither a `<' (to begin a
5224 template-id), nor a `::', then we are not looking at a
5225 nested-name-specifier. */
5226 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5228 if (token
->type
== CPP_COLON
5229 && parser
->colon_corrects_to_scope_p
5230 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5232 error_at (token
->location
,
5233 "found %<:%> in nested-name-specifier, expected %<::%>");
5234 token
->type
= CPP_SCOPE
;
5237 if (token
->type
!= CPP_SCOPE
5238 && !cp_parser_nth_token_starts_template_argument_list_p
5243 /* The nested-name-specifier is optional, so we parse
5245 cp_parser_parse_tentatively (parser
);
5247 /* Look for the optional `template' keyword, if this isn't the
5248 first time through the loop. */
5250 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5252 template_keyword_p
= false;
5254 /* Save the old scope since the name lookup we are about to do
5255 might destroy it. */
5256 old_scope
= parser
->scope
;
5257 saved_qualifying_scope
= parser
->qualifying_scope
;
5258 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5259 look up names in "X<T>::I" in order to determine that "Y" is
5260 a template. So, if we have a typename at this point, we make
5261 an effort to look through it. */
5263 && !typename_keyword_p
5265 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5266 parser
->scope
= resolve_typename_type (parser
->scope
,
5267 /*only_current_p=*/false);
5268 /* Parse the qualifying entity. */
5270 = cp_parser_qualifying_entity (parser
,
5276 /* Look for the `::' token. */
5277 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5279 /* If we found what we wanted, we keep going; otherwise, we're
5281 if (!cp_parser_parse_definitely (parser
))
5283 bool error_p
= false;
5285 /* Restore the OLD_SCOPE since it was valid before the
5286 failed attempt at finding the last
5287 class-or-namespace-name. */
5288 parser
->scope
= old_scope
;
5289 parser
->qualifying_scope
= saved_qualifying_scope
;
5291 /* If the next token is a decltype, and the one after that is a
5292 `::', then the decltype has failed to resolve to a class or
5293 enumeration type. Give this error even when parsing
5294 tentatively since it can't possibly be valid--and we're going
5295 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5296 won't get another chance.*/
5297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5298 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5301 token
= cp_lexer_consume_token (parser
->lexer
);
5302 error_at (token
->location
, "decltype evaluates to %qT, "
5303 "which is not a class or enumeration type",
5305 parser
->scope
= error_mark_node
;
5309 cp_lexer_consume_token (parser
->lexer
);
5312 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5314 /* If the next token is an identifier, and the one after
5315 that is a `::', then any valid interpretation would have
5316 found a class-or-namespace-name. */
5317 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5318 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5320 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5323 token
= cp_lexer_consume_token (parser
->lexer
);
5326 if (!token
->error_reported
)
5329 tree ambiguous_decls
;
5331 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5333 /*is_template=*/false,
5334 /*is_namespace=*/false,
5335 /*check_dependency=*/true,
5338 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5339 error_at (token
->location
,
5340 "%qD used without template parameters",
5342 else if (ambiguous_decls
)
5344 // cp_parser_lookup_name has the same diagnostic,
5345 // thus make sure to emit it at most once.
5346 if (cp_parser_uncommitted_to_tentative_parse_p
5349 error_at (token
->location
,
5350 "reference to %qD is ambiguous",
5352 print_candidates (ambiguous_decls
);
5354 decl
= error_mark_node
;
5358 if (cxx_dialect
!= cxx98
)
5359 cp_parser_name_lookup_error
5360 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5363 cp_parser_name_lookup_error
5364 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5368 parser
->scope
= error_mark_node
;
5370 /* Treat this as a successful nested-name-specifier
5375 If the name found is not a class-name (clause
5376 _class_) or namespace-name (_namespace.def_), the
5377 program is ill-formed. */
5380 cp_lexer_consume_token (parser
->lexer
);
5384 /* We've found one valid nested-name-specifier. */
5386 /* Name lookup always gives us a DECL. */
5387 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5388 new_scope
= TREE_TYPE (new_scope
);
5389 /* Uses of "template" must be followed by actual templates. */
5390 if (template_keyword_p
5391 && !(CLASS_TYPE_P (new_scope
)
5392 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5393 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5394 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5395 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5396 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5397 == TEMPLATE_ID_EXPR
)))
5398 permerror (input_location
, TYPE_P (new_scope
)
5399 ? G_("%qT is not a template")
5400 : G_("%qD is not a template"),
5402 /* If it is a class scope, try to complete it; we are about to
5403 be looking up names inside the class. */
5404 if (TYPE_P (new_scope
)
5405 /* Since checking types for dependency can be expensive,
5406 avoid doing it if the type is already complete. */
5407 && !COMPLETE_TYPE_P (new_scope
)
5408 /* Do not try to complete dependent types. */
5409 && !dependent_type_p (new_scope
))
5411 new_scope
= complete_type (new_scope
);
5412 /* If it is a typedef to current class, use the current
5413 class instead, as the typedef won't have any names inside
5415 if (!COMPLETE_TYPE_P (new_scope
)
5416 && currently_open_class (new_scope
))
5417 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5419 /* Make sure we look in the right scope the next time through
5421 parser
->scope
= new_scope
;
5424 /* If parsing tentatively, replace the sequence of tokens that makes
5425 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5426 token. That way, should we re-parse the token stream, we will
5427 not have to repeat the effort required to do the parse, nor will
5428 we issue duplicate error messages. */
5429 if (success
&& start
)
5433 token
= cp_lexer_token_at (parser
->lexer
, start
);
5434 /* Reset the contents of the START token. */
5435 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5436 /* Retrieve any deferred checks. Do not pop this access checks yet
5437 so the memory will not be reclaimed during token replacing below. */
5438 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
5439 token
->u
.tree_check_value
->value
= parser
->scope
;
5440 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5441 token
->u
.tree_check_value
->qualifying_scope
=
5442 parser
->qualifying_scope
;
5443 token
->keyword
= RID_MAX
;
5445 /* Purge all subsequent tokens. */
5446 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5450 pop_to_parent_deferring_access_checks ();
5452 return success
? parser
->scope
: NULL_TREE
;
5455 /* Parse a nested-name-specifier. See
5456 cp_parser_nested_name_specifier_opt for details. This function
5457 behaves identically, except that it will an issue an error if no
5458 nested-name-specifier is present. */
5461 cp_parser_nested_name_specifier (cp_parser
*parser
,
5462 bool typename_keyword_p
,
5463 bool check_dependency_p
,
5465 bool is_declaration
)
5469 /* Look for the nested-name-specifier. */
5470 scope
= cp_parser_nested_name_specifier_opt (parser
,
5475 /* If it was not present, issue an error message. */
5478 cp_parser_error (parser
, "expected nested-name-specifier");
5479 parser
->scope
= NULL_TREE
;
5485 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5486 this is either a class-name or a namespace-name (which corresponds
5487 to the class-or-namespace-name production in the grammar). For
5488 C++0x, it can also be a type-name that refers to an enumeration
5489 type or a simple-template-id.
5491 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5492 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5493 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5494 TYPE_P is TRUE iff the next name should be taken as a class-name,
5495 even the same name is declared to be another entity in the same
5498 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5499 specified by the class-or-namespace-name. If neither is found the
5500 ERROR_MARK_NODE is returned. */
5503 cp_parser_qualifying_entity (cp_parser
*parser
,
5504 bool typename_keyword_p
,
5505 bool template_keyword_p
,
5506 bool check_dependency_p
,
5508 bool is_declaration
)
5511 tree saved_qualifying_scope
;
5512 tree saved_object_scope
;
5515 bool successful_parse_p
;
5517 /* DR 743: decltype can appear in a nested-name-specifier. */
5518 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5520 scope
= cp_parser_decltype (parser
);
5521 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5522 && !MAYBE_CLASS_TYPE_P (scope
))
5524 cp_parser_simulate_error (parser
);
5525 return error_mark_node
;
5527 if (TYPE_NAME (scope
))
5528 scope
= TYPE_NAME (scope
);
5532 /* Before we try to parse the class-name, we must save away the
5533 current PARSER->SCOPE since cp_parser_class_name will destroy
5535 saved_scope
= parser
->scope
;
5536 saved_qualifying_scope
= parser
->qualifying_scope
;
5537 saved_object_scope
= parser
->object_scope
;
5538 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5539 there is no need to look for a namespace-name. */
5540 only_class_p
= template_keyword_p
5541 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5543 cp_parser_parse_tentatively (parser
);
5544 scope
= cp_parser_class_name (parser
,
5547 type_p
? class_type
: none_type
,
5549 /*class_head_p=*/false,
5551 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5552 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5554 && cxx_dialect
!= cxx98
5555 && !successful_parse_p
)
5557 /* Restore the saved scope. */
5558 parser
->scope
= saved_scope
;
5559 parser
->qualifying_scope
= saved_qualifying_scope
;
5560 parser
->object_scope
= saved_object_scope
;
5562 /* Parse tentatively. */
5563 cp_parser_parse_tentatively (parser
);
5565 /* Parse a type-name */
5566 scope
= cp_parser_type_name (parser
);
5568 /* "If the name found does not designate a namespace or a class,
5569 enumeration, or dependent type, the program is ill-formed."
5571 We cover classes and dependent types above and namespaces below,
5572 so this code is only looking for enums. */
5573 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5574 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5575 cp_parser_simulate_error (parser
);
5577 successful_parse_p
= cp_parser_parse_definitely (parser
);
5579 /* If that didn't work, try for a namespace-name. */
5580 if (!only_class_p
&& !successful_parse_p
)
5582 /* Restore the saved scope. */
5583 parser
->scope
= saved_scope
;
5584 parser
->qualifying_scope
= saved_qualifying_scope
;
5585 parser
->object_scope
= saved_object_scope
;
5586 /* If we are not looking at an identifier followed by the scope
5587 resolution operator, then this is not part of a
5588 nested-name-specifier. (Note that this function is only used
5589 to parse the components of a nested-name-specifier.) */
5590 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5591 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5592 return error_mark_node
;
5593 scope
= cp_parser_namespace_name (parser
);
5599 /* Parse a postfix-expression.
5603 postfix-expression [ expression ]
5604 postfix-expression ( expression-list [opt] )
5605 simple-type-specifier ( expression-list [opt] )
5606 typename :: [opt] nested-name-specifier identifier
5607 ( expression-list [opt] )
5608 typename :: [opt] nested-name-specifier template [opt] template-id
5609 ( expression-list [opt] )
5610 postfix-expression . template [opt] id-expression
5611 postfix-expression -> template [opt] id-expression
5612 postfix-expression . pseudo-destructor-name
5613 postfix-expression -> pseudo-destructor-name
5614 postfix-expression ++
5615 postfix-expression --
5616 dynamic_cast < type-id > ( expression )
5617 static_cast < type-id > ( expression )
5618 reinterpret_cast < type-id > ( expression )
5619 const_cast < type-id > ( expression )
5620 typeid ( expression )
5626 ( type-id ) { initializer-list , [opt] }
5628 This extension is a GNU version of the C99 compound-literal
5629 construct. (The C99 grammar uses `type-name' instead of `type-id',
5630 but they are essentially the same concept.)
5632 If ADDRESS_P is true, the postfix expression is the operand of the
5633 `&' operator. CAST_P is true if this expression is the target of a
5636 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5637 class member access expressions [expr.ref].
5639 Returns a representation of the expression. */
5642 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5643 bool member_access_only_p
, bool decltype_p
,
5644 cp_id_kind
* pidk_return
)
5649 cp_id_kind idk
= CP_ID_KIND_NONE
;
5650 tree postfix_expression
= NULL_TREE
;
5651 bool is_member_access
= false;
5652 int saved_in_statement
= -1;
5654 /* Peek at the next token. */
5655 token
= cp_lexer_peek_token (parser
->lexer
);
5656 loc
= token
->location
;
5657 /* Some of the productions are determined by keywords. */
5658 keyword
= token
->keyword
;
5668 const char *saved_message
;
5669 bool saved_in_type_id_in_expr_p
;
5671 /* All of these can be handled in the same way from the point
5672 of view of parsing. Begin by consuming the token
5673 identifying the cast. */
5674 cp_lexer_consume_token (parser
->lexer
);
5676 /* New types cannot be defined in the cast. */
5677 saved_message
= parser
->type_definition_forbidden_message
;
5678 parser
->type_definition_forbidden_message
5679 = G_("types may not be defined in casts");
5681 /* Look for the opening `<'. */
5682 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5683 /* Parse the type to which we are casting. */
5684 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5685 parser
->in_type_id_in_expr_p
= true;
5686 type
= cp_parser_type_id (parser
);
5687 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5688 /* Look for the closing `>'. */
5689 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5690 /* Restore the old message. */
5691 parser
->type_definition_forbidden_message
= saved_message
;
5693 bool saved_greater_than_is_operator_p
5694 = parser
->greater_than_is_operator_p
;
5695 parser
->greater_than_is_operator_p
= true;
5697 /* And the expression which is being cast. */
5698 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5699 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
5700 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5702 parser
->greater_than_is_operator_p
5703 = saved_greater_than_is_operator_p
;
5705 /* Only type conversions to integral or enumeration types
5706 can be used in constant-expressions. */
5707 if (!cast_valid_in_integral_constant_expression_p (type
)
5708 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5709 return error_mark_node
;
5715 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5719 = build_static_cast (type
, expression
, tf_warning_or_error
);
5723 = build_reinterpret_cast (type
, expression
,
5724 tf_warning_or_error
);
5728 = build_const_cast (type
, expression
, tf_warning_or_error
);
5739 const char *saved_message
;
5740 bool saved_in_type_id_in_expr_p
;
5742 /* Consume the `typeid' token. */
5743 cp_lexer_consume_token (parser
->lexer
);
5744 /* Look for the `(' token. */
5745 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5746 /* Types cannot be defined in a `typeid' expression. */
5747 saved_message
= parser
->type_definition_forbidden_message
;
5748 parser
->type_definition_forbidden_message
5749 = G_("types may not be defined in a %<typeid%> expression");
5750 /* We can't be sure yet whether we're looking at a type-id or an
5752 cp_parser_parse_tentatively (parser
);
5753 /* Try a type-id first. */
5754 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5755 parser
->in_type_id_in_expr_p
= true;
5756 type
= cp_parser_type_id (parser
);
5757 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5758 /* Look for the `)' token. Otherwise, we can't be sure that
5759 we're not looking at an expression: consider `typeid (int
5760 (3))', for example. */
5761 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5762 /* If all went well, simply lookup the type-id. */
5763 if (cp_parser_parse_definitely (parser
))
5764 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5765 /* Otherwise, fall back to the expression variant. */
5770 /* Look for an expression. */
5771 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
5772 /* Compute its typeid. */
5773 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5774 /* Look for the `)' token. */
5775 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5777 /* Restore the saved message. */
5778 parser
->type_definition_forbidden_message
= saved_message
;
5779 /* `typeid' may not appear in an integral constant expression. */
5780 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5781 return error_mark_node
;
5788 /* The syntax permitted here is the same permitted for an
5789 elaborated-type-specifier. */
5790 type
= cp_parser_elaborated_type_specifier (parser
,
5791 /*is_friend=*/false,
5792 /*is_declaration=*/false);
5793 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5797 case RID_CILK_SPAWN
:
5799 cp_lexer_consume_token (parser
->lexer
);
5800 token
= cp_lexer_peek_token (parser
->lexer
);
5801 if (token
->type
== CPP_SEMICOLON
)
5803 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
5805 postfix_expression
= error_mark_node
;
5808 else if (!current_function_decl
)
5810 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
5811 "inside a function");
5812 postfix_expression
= error_mark_node
;
5817 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5818 saved_in_statement
= parser
->in_statement
;
5819 parser
->in_statement
|= IN_CILK_SPAWN
;
5821 cfun
->calls_cilk_spawn
= 1;
5822 postfix_expression
=
5823 cp_parser_postfix_expression (parser
, false, false,
5824 false, false, &idk
);
5827 error_at (token
->location
, "-fcilkplus must be enabled to use"
5828 " %<_Cilk_spawn%>");
5829 cfun
->calls_cilk_spawn
= 0;
5831 else if (saved_in_statement
& IN_CILK_SPAWN
)
5833 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
5834 "are not permitted");
5835 postfix_expression
= error_mark_node
;
5836 cfun
->calls_cilk_spawn
= 0;
5840 postfix_expression
= build_cilk_spawn (token
->location
,
5841 postfix_expression
);
5842 if (postfix_expression
!= error_mark_node
)
5843 SET_EXPR_LOCATION (postfix_expression
, input_location
);
5844 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
5852 tree sync_expr
= build_cilk_sync ();
5853 SET_EXPR_LOCATION (sync_expr
,
5854 cp_lexer_peek_token (parser
->lexer
)->location
);
5855 finish_expr_stmt (sync_expr
);
5858 error_at (token
->location
, "-fcilkplus must be enabled to use"
5860 cp_lexer_consume_token (parser
->lexer
);
5863 case RID_BUILTIN_SHUFFLE
:
5865 vec
<tree
, va_gc
> *vec
;
5869 cp_lexer_consume_token (parser
->lexer
);
5870 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5871 /*cast_p=*/false, /*allow_expansion_p=*/true,
5872 /*non_constant_p=*/NULL
);
5874 return error_mark_node
;
5876 FOR_EACH_VEC_ELT (*vec
, i
, p
)
5879 if (vec
->length () == 2)
5880 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
5881 tf_warning_or_error
);
5882 else if (vec
->length () == 3)
5883 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
5884 tf_warning_or_error
);
5887 error_at (loc
, "wrong number of arguments to "
5888 "%<__builtin_shuffle%>");
5889 return error_mark_node
;
5898 /* If the next thing is a simple-type-specifier, we may be
5899 looking at a functional cast. We could also be looking at
5900 an id-expression. So, we try the functional cast, and if
5901 that doesn't work we fall back to the primary-expression. */
5902 cp_parser_parse_tentatively (parser
);
5903 /* Look for the simple-type-specifier. */
5904 type
= cp_parser_simple_type_specifier (parser
,
5905 /*decl_specs=*/NULL
,
5906 CP_PARSER_FLAGS_NONE
);
5907 /* Parse the cast itself. */
5908 if (!cp_parser_error_occurred (parser
))
5910 = cp_parser_functional_cast (parser
, type
);
5911 /* If that worked, we're done. */
5912 if (cp_parser_parse_definitely (parser
))
5915 /* If the functional-cast didn't work out, try a
5916 compound-literal. */
5917 if (cp_parser_allow_gnu_extensions_p (parser
)
5918 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5920 tree initializer
= NULL_TREE
;
5921 bool compound_literal_p
;
5923 cp_parser_parse_tentatively (parser
);
5924 /* Consume the `('. */
5925 cp_lexer_consume_token (parser
->lexer
);
5927 /* Avoid calling cp_parser_type_id pointlessly, see comment
5928 in cp_parser_cast_expression about c++/29234. */
5929 cp_lexer_save_tokens (parser
->lexer
);
5932 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5933 /*consume_paren=*/true)
5934 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5936 /* Roll back the tokens we skipped. */
5937 cp_lexer_rollback_tokens (parser
->lexer
);
5939 if (!compound_literal_p
)
5940 cp_parser_simulate_error (parser
);
5943 /* Parse the type. */
5944 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5945 parser
->in_type_id_in_expr_p
= true;
5946 type
= cp_parser_type_id (parser
);
5947 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5948 /* Look for the `)'. */
5949 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5952 /* If things aren't going well, there's no need to
5954 if (!cp_parser_error_occurred (parser
))
5956 bool non_constant_p
;
5957 /* Parse the brace-enclosed initializer list. */
5958 initializer
= cp_parser_braced_list (parser
,
5961 /* If that worked, we're definitely looking at a
5962 compound-literal expression. */
5963 if (cp_parser_parse_definitely (parser
))
5965 /* Warn the user that a compound literal is not
5966 allowed in standard C++. */
5967 pedwarn (input_location
, OPT_Wpedantic
,
5968 "ISO C++ forbids compound-literals");
5969 /* For simplicity, we disallow compound literals in
5970 constant-expressions. We could
5971 allow compound literals of integer type, whose
5972 initializer was a constant, in constant
5973 expressions. Permitting that usage, as a further
5974 extension, would not change the meaning of any
5975 currently accepted programs. (Of course, as
5976 compound literals are not part of ISO C++, the
5977 standard has nothing to say.) */
5978 if (cp_parser_non_integral_constant_expression (parser
,
5981 postfix_expression
= error_mark_node
;
5984 /* Form the representation of the compound-literal. */
5986 = finish_compound_literal (type
, initializer
,
5987 tf_warning_or_error
);
5992 /* It must be a primary-expression. */
5994 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5995 /*template_arg_p=*/false,
6002 /* Note that we don't need to worry about calling build_cplus_new on a
6003 class-valued CALL_EXPR in decltype when it isn't the end of the
6004 postfix-expression; unary_complex_lvalue will take care of that for
6007 /* Keep looping until the postfix-expression is complete. */
6010 if (idk
== CP_ID_KIND_UNQUALIFIED
6011 && identifier_p (postfix_expression
)
6012 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6013 /* It is not a Koenig lookup function call. */
6015 = unqualified_name_lookup_error (postfix_expression
);
6017 /* Peek at the next token. */
6018 token
= cp_lexer_peek_token (parser
->lexer
);
6020 switch (token
->type
)
6022 case CPP_OPEN_SQUARE
:
6023 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6025 cp_parser_error (parser
,
6026 "two consecutive %<[%> shall "
6027 "only introduce an attribute");
6028 return error_mark_node
;
6031 = cp_parser_postfix_open_square_expression (parser
,
6035 idk
= CP_ID_KIND_NONE
;
6036 is_member_access
= false;
6039 case CPP_OPEN_PAREN
:
6040 /* postfix-expression ( expression-list [opt] ) */
6043 bool is_builtin_constant_p
;
6044 bool saved_integral_constant_expression_p
= false;
6045 bool saved_non_integral_constant_expression_p
= false;
6046 tsubst_flags_t complain
= complain_flags (decltype_p
);
6047 vec
<tree
, va_gc
> *args
;
6049 is_member_access
= false;
6051 is_builtin_constant_p
6052 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6053 if (is_builtin_constant_p
)
6055 /* The whole point of __builtin_constant_p is to allow
6056 non-constant expressions to appear as arguments. */
6057 saved_integral_constant_expression_p
6058 = parser
->integral_constant_expression_p
;
6059 saved_non_integral_constant_expression_p
6060 = parser
->non_integral_constant_expression_p
;
6061 parser
->integral_constant_expression_p
= false;
6063 args
= (cp_parser_parenthesized_expression_list
6065 /*cast_p=*/false, /*allow_expansion_p=*/true,
6066 /*non_constant_p=*/NULL
));
6067 if (is_builtin_constant_p
)
6069 parser
->integral_constant_expression_p
6070 = saved_integral_constant_expression_p
;
6071 parser
->non_integral_constant_expression_p
6072 = saved_non_integral_constant_expression_p
;
6077 postfix_expression
= error_mark_node
;
6081 /* Function calls are not permitted in
6082 constant-expressions. */
6083 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6084 && cp_parser_non_integral_constant_expression (parser
,
6087 postfix_expression
= error_mark_node
;
6088 release_tree_vector (args
);
6093 if (idk
== CP_ID_KIND_UNQUALIFIED
6094 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6096 if (identifier_p (postfix_expression
))
6098 if (!args
->is_empty ())
6101 if (!any_type_dependent_arguments_p (args
))
6103 = perform_koenig_lookup (postfix_expression
, args
,
6108 = unqualified_fn_lookup_error (postfix_expression
);
6110 /* We do not perform argument-dependent lookup if
6111 normal lookup finds a non-function, in accordance
6112 with the expected resolution of DR 218. */
6113 else if (!args
->is_empty ()
6114 && is_overloaded_fn (postfix_expression
))
6116 tree fn
= get_first_fn (postfix_expression
);
6117 fn
= STRIP_TEMPLATE (fn
);
6119 /* Do not do argument dependent lookup if regular
6120 lookup finds a member function or a block-scope
6121 function declaration. [basic.lookup.argdep]/3 */
6122 if (!DECL_FUNCTION_MEMBER_P (fn
)
6123 && !DECL_LOCAL_FUNCTION_P (fn
))
6126 if (!any_type_dependent_arguments_p (args
))
6128 = perform_koenig_lookup (postfix_expression
, args
,
6134 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6136 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6137 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6139 if (processing_template_decl
6140 && (type_dependent_expression_p (instance
)
6141 || (!BASELINK_P (fn
)
6142 && TREE_CODE (fn
) != FIELD_DECL
)
6143 || type_dependent_expression_p (fn
)
6144 || any_type_dependent_arguments_p (args
)))
6147 = build_nt_call_vec (postfix_expression
, args
);
6148 release_tree_vector (args
);
6152 if (BASELINK_P (fn
))
6155 = (build_new_method_call
6156 (instance
, fn
, &args
, NULL_TREE
,
6157 (idk
== CP_ID_KIND_QUALIFIED
6158 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6165 = finish_call_expr (postfix_expression
, &args
,
6166 /*disallow_virtual=*/false,
6170 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6171 || TREE_CODE (postfix_expression
) == MEMBER_REF
6172 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6173 postfix_expression
= (build_offset_ref_call_from_tree
6174 (postfix_expression
, &args
,
6176 else if (idk
== CP_ID_KIND_QUALIFIED
)
6177 /* A call to a static class member, or a namespace-scope
6180 = finish_call_expr (postfix_expression
, &args
,
6181 /*disallow_virtual=*/true,
6185 /* All other function calls. */
6187 = finish_call_expr (postfix_expression
, &args
,
6188 /*disallow_virtual=*/false,
6192 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6193 idk
= CP_ID_KIND_NONE
;
6195 release_tree_vector (args
);
6201 /* postfix-expression . template [opt] id-expression
6202 postfix-expression . pseudo-destructor-name
6203 postfix-expression -> template [opt] id-expression
6204 postfix-expression -> pseudo-destructor-name */
6206 /* Consume the `.' or `->' operator. */
6207 cp_lexer_consume_token (parser
->lexer
);
6210 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6214 is_member_access
= true;
6218 /* postfix-expression ++ */
6219 /* Consume the `++' token. */
6220 cp_lexer_consume_token (parser
->lexer
);
6221 /* Generate a representation for the complete expression. */
6223 = finish_increment_expr (postfix_expression
,
6224 POSTINCREMENT_EXPR
);
6225 /* Increments may not appear in constant-expressions. */
6226 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6227 postfix_expression
= error_mark_node
;
6228 idk
= CP_ID_KIND_NONE
;
6229 is_member_access
= false;
6232 case CPP_MINUS_MINUS
:
6233 /* postfix-expression -- */
6234 /* Consume the `--' token. */
6235 cp_lexer_consume_token (parser
->lexer
);
6236 /* Generate a representation for the complete expression. */
6238 = finish_increment_expr (postfix_expression
,
6239 POSTDECREMENT_EXPR
);
6240 /* Decrements may not appear in constant-expressions. */
6241 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6242 postfix_expression
= error_mark_node
;
6243 idk
= CP_ID_KIND_NONE
;
6244 is_member_access
= false;
6248 if (pidk_return
!= NULL
)
6249 * pidk_return
= idk
;
6250 if (member_access_only_p
)
6251 return is_member_access
? postfix_expression
: error_mark_node
;
6253 return postfix_expression
;
6257 /* We should never get here. */
6259 return error_mark_node
;
6262 /* This function parses Cilk Plus array notations. If a normal array expr. is
6263 parsed then the array index is passed back to the caller through *INIT_INDEX
6264 and the function returns a NULL_TREE. If array notation expr. is parsed,
6265 then *INIT_INDEX is ignored by the caller and the function returns
6266 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6270 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6273 cp_token
*token
= NULL
;
6274 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6275 if (!array_value
|| array_value
== error_mark_node
)
6277 cp_parser_skip_to_end_of_statement (parser
);
6278 return error_mark_node
;
6281 array_type
= TREE_TYPE (array_value
);
6283 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6284 parser
->colon_corrects_to_scope_p
= false;
6285 token
= cp_lexer_peek_token (parser
->lexer
);
6289 cp_parser_error (parser
, "expected %<:%> or numeral");
6290 return error_mark_node
;
6292 else if (token
->type
== CPP_COLON
)
6294 /* Consume the ':'. */
6295 cp_lexer_consume_token (parser
->lexer
);
6297 /* If we are here, then we have a case like this A[:]. */
6298 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6300 cp_parser_error (parser
, "expected %<]%>");
6301 cp_parser_skip_to_end_of_statement (parser
);
6302 return error_mark_node
;
6304 *init_index
= NULL_TREE
;
6306 length_index
= NULL_TREE
;
6310 /* If we are here, then there are three valid possibilities:
6312 2. ARRAY [ EXP : EXP ]
6313 3. ARRAY [ EXP : EXP : EXP ] */
6315 *init_index
= cp_parser_expression (parser
, false, NULL
);
6316 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6318 /* This indicates that we have a normal array expression. */
6319 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6323 /* Consume the ':'. */
6324 cp_lexer_consume_token (parser
->lexer
);
6325 length_index
= cp_parser_expression (parser
, false, NULL
);
6326 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6328 cp_lexer_consume_token (parser
->lexer
);
6329 stride
= cp_parser_expression (parser
, false, NULL
);
6332 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6334 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6335 || stride
== error_mark_node
)
6337 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6338 cp_lexer_consume_token (parser
->lexer
);
6339 return error_mark_node
;
6341 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6343 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6344 length_index
, stride
, array_type
);
6348 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6349 by cp_parser_builtin_offsetof. We're looking for
6351 postfix-expression [ expression ]
6352 postfix-expression [ braced-init-list ] (C++11)
6354 FOR_OFFSETOF is set if we're being called in that context, which
6355 changes how we deal with integer constant expressions. */
6358 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6359 tree postfix_expression
,
6363 tree index
= NULL_TREE
;
6364 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6365 bool saved_greater_than_is_operator_p
;
6367 /* Consume the `[' token. */
6368 cp_lexer_consume_token (parser
->lexer
);
6370 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6371 parser
->greater_than_is_operator_p
= true;
6373 /* Parse the index expression. */
6374 /* ??? For offsetof, there is a question of what to allow here. If
6375 offsetof is not being used in an integral constant expression context,
6376 then we *could* get the right answer by computing the value at runtime.
6377 If we are in an integral constant expression context, then we might
6378 could accept any constant expression; hard to say without analysis.
6379 Rather than open the barn door too wide right away, allow only integer
6380 constant expressions here. */
6382 index
= cp_parser_constant_expression (parser
, false, NULL
);
6385 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6387 bool expr_nonconst_p
;
6388 cp_lexer_set_source_position (parser
->lexer
);
6389 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6390 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6392 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6394 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6395 "braced list index is not allowed with array "
6397 cp_parser_skip_to_end_of_statement (parser
);
6398 return error_mark_node
;
6401 else if (flag_cilkplus
)
6403 /* Here are have these two options:
6404 ARRAY[EXP : EXP] - Array notation expr with default
6406 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6408 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6409 postfix_expression
);
6414 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6417 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6419 /* Look for the closing `]'. */
6420 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6422 /* Build the ARRAY_REF. */
6423 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6426 /* When not doing offsetof, array references are not permitted in
6427 constant-expressions. */
6429 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6430 postfix_expression
= error_mark_node
;
6432 return postfix_expression
;
6435 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6436 by cp_parser_builtin_offsetof. We're looking for
6438 postfix-expression . template [opt] id-expression
6439 postfix-expression . pseudo-destructor-name
6440 postfix-expression -> template [opt] id-expression
6441 postfix-expression -> pseudo-destructor-name
6443 FOR_OFFSETOF is set if we're being called in that context. That sorta
6444 limits what of the above we'll actually accept, but nevermind.
6445 TOKEN_TYPE is the "." or "->" token, which will already have been
6446 removed from the stream. */
6449 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6450 enum cpp_ttype token_type
,
6451 tree postfix_expression
,
6452 bool for_offsetof
, cp_id_kind
*idk
,
6453 location_t location
)
6457 bool pseudo_destructor_p
;
6458 tree scope
= NULL_TREE
;
6460 /* If this is a `->' operator, dereference the pointer. */
6461 if (token_type
== CPP_DEREF
)
6462 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6463 tf_warning_or_error
);
6464 /* Check to see whether or not the expression is type-dependent. */
6465 dependent_p
= type_dependent_expression_p (postfix_expression
);
6466 /* The identifier following the `->' or `.' is not qualified. */
6467 parser
->scope
= NULL_TREE
;
6468 parser
->qualifying_scope
= NULL_TREE
;
6469 parser
->object_scope
= NULL_TREE
;
6470 *idk
= CP_ID_KIND_NONE
;
6472 /* Enter the scope corresponding to the type of the object
6473 given by the POSTFIX_EXPRESSION. */
6474 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6476 scope
= TREE_TYPE (postfix_expression
);
6477 /* According to the standard, no expression should ever have
6478 reference type. Unfortunately, we do not currently match
6479 the standard in this respect in that our internal representation
6480 of an expression may have reference type even when the standard
6481 says it does not. Therefore, we have to manually obtain the
6482 underlying type here. */
6483 scope
= non_reference (scope
);
6484 /* The type of the POSTFIX_EXPRESSION must be complete. */
6485 if (scope
== unknown_type_node
)
6487 error_at (location
, "%qE does not have class type",
6488 postfix_expression
);
6491 /* Unlike the object expression in other contexts, *this is not
6492 required to be of complete type for purposes of class member
6493 access (5.2.5) outside the member function body. */
6494 else if (postfix_expression
!= current_class_ref
6495 && !(processing_template_decl
&& scope
== current_class_type
))
6496 scope
= complete_type_or_else (scope
, NULL_TREE
);
6497 /* Let the name lookup machinery know that we are processing a
6498 class member access expression. */
6499 parser
->context
->object_type
= scope
;
6500 /* If something went wrong, we want to be able to discern that case,
6501 as opposed to the case where there was no SCOPE due to the type
6502 of expression being dependent. */
6504 scope
= error_mark_node
;
6505 /* If the SCOPE was erroneous, make the various semantic analysis
6506 functions exit quickly -- and without issuing additional error
6508 if (scope
== error_mark_node
)
6509 postfix_expression
= error_mark_node
;
6512 /* Assume this expression is not a pseudo-destructor access. */
6513 pseudo_destructor_p
= false;
6515 /* If the SCOPE is a scalar type, then, if this is a valid program,
6516 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6517 is type dependent, it can be pseudo-destructor-name or something else.
6518 Try to parse it as pseudo-destructor-name first. */
6519 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6524 cp_parser_parse_tentatively (parser
);
6525 /* Parse the pseudo-destructor-name. */
6527 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6530 && (cp_parser_error_occurred (parser
)
6531 || !SCALAR_TYPE_P (type
)))
6532 cp_parser_abort_tentative_parse (parser
);
6533 else if (cp_parser_parse_definitely (parser
))
6535 pseudo_destructor_p
= true;
6537 = finish_pseudo_destructor_expr (postfix_expression
,
6542 if (!pseudo_destructor_p
)
6544 /* If the SCOPE is not a scalar type, we are looking at an
6545 ordinary class member access expression, rather than a
6546 pseudo-destructor-name. */
6548 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6549 /* Parse the id-expression. */
6550 name
= (cp_parser_id_expression
6552 cp_parser_optional_template_keyword (parser
),
6553 /*check_dependency_p=*/true,
6555 /*declarator_p=*/false,
6556 /*optional_p=*/false));
6557 /* In general, build a SCOPE_REF if the member name is qualified.
6558 However, if the name was not dependent and has already been
6559 resolved; there is no need to build the SCOPE_REF. For example;
6561 struct X { void f(); };
6562 template <typename T> void f(T* t) { t->X::f(); }
6564 Even though "t" is dependent, "X::f" is not and has been resolved
6565 to a BASELINK; there is no need to include scope information. */
6567 /* But we do need to remember that there was an explicit scope for
6568 virtual function calls. */
6570 *idk
= CP_ID_KIND_QUALIFIED
;
6572 /* If the name is a template-id that names a type, we will get a
6573 TYPE_DECL here. That is invalid code. */
6574 if (TREE_CODE (name
) == TYPE_DECL
)
6576 error_at (token
->location
, "invalid use of %qD", name
);
6577 postfix_expression
= error_mark_node
;
6581 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6583 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6585 error_at (token
->location
, "%<%D::%D%> is not a class member",
6586 parser
->scope
, name
);
6587 postfix_expression
= error_mark_node
;
6590 name
= build_qualified_name (/*type=*/NULL_TREE
,
6594 parser
->scope
= NULL_TREE
;
6595 parser
->qualifying_scope
= NULL_TREE
;
6596 parser
->object_scope
= NULL_TREE
;
6598 if (parser
->scope
&& name
&& BASELINK_P (name
))
6599 adjust_result_of_qualified_name_lookup
6600 (name
, parser
->scope
, scope
);
6602 = finish_class_member_access_expr (postfix_expression
, name
,
6604 tf_warning_or_error
);
6608 /* We no longer need to look up names in the scope of the object on
6609 the left-hand side of the `.' or `->' operator. */
6610 parser
->context
->object_type
= NULL_TREE
;
6612 /* Outside of offsetof, these operators may not appear in
6613 constant-expressions. */
6615 && (cp_parser_non_integral_constant_expression
6616 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6617 postfix_expression
= error_mark_node
;
6619 return postfix_expression
;
6622 /* Parse a parenthesized expression-list.
6625 assignment-expression
6626 expression-list, assignment-expression
6631 identifier, expression-list
6633 CAST_P is true if this expression is the target of a cast.
6635 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6638 Returns a vector of trees. Each element is a representation of an
6639 assignment-expression. NULL is returned if the ( and or ) are
6640 missing. An empty, but allocated, vector is returned on no
6641 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6642 if we are parsing an attribute list for an attribute that wants a
6643 plain identifier argument, normal_attr for an attribute that wants
6644 an expression, or non_attr if we aren't parsing an attribute list. If
6645 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6646 not all of the expressions in the list were constant. */
6648 static vec
<tree
, va_gc
> *
6649 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6650 int is_attribute_list
,
6652 bool allow_expansion_p
,
6653 bool *non_constant_p
)
6655 vec
<tree
, va_gc
> *expression_list
;
6656 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6657 tree identifier
= NULL_TREE
;
6658 bool saved_greater_than_is_operator_p
;
6660 /* Assume all the expressions will be constant. */
6662 *non_constant_p
= false;
6664 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6667 expression_list
= make_tree_vector ();
6669 /* Within a parenthesized expression, a `>' token is always
6670 the greater-than operator. */
6671 saved_greater_than_is_operator_p
6672 = parser
->greater_than_is_operator_p
;
6673 parser
->greater_than_is_operator_p
= true;
6675 /* Consume expressions until there are no more. */
6676 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6681 /* At the beginning of attribute lists, check to see if the
6682 next token is an identifier. */
6683 if (is_attribute_list
== id_attr
6684 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6688 /* Consume the identifier. */
6689 token
= cp_lexer_consume_token (parser
->lexer
);
6690 /* Save the identifier. */
6691 identifier
= token
->u
.value
;
6695 bool expr_non_constant_p
;
6697 /* Parse the next assignment-expression. */
6698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6700 /* A braced-init-list. */
6701 cp_lexer_set_source_position (parser
->lexer
);
6702 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6703 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6704 if (non_constant_p
&& expr_non_constant_p
)
6705 *non_constant_p
= true;
6707 else if (non_constant_p
)
6709 expr
= (cp_parser_constant_expression
6710 (parser
, /*allow_non_constant_p=*/true,
6711 &expr_non_constant_p
));
6712 if (expr_non_constant_p
)
6713 *non_constant_p
= true;
6716 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
6719 expr
= fold_non_dependent_expr (expr
);
6721 /* If we have an ellipsis, then this is an expression
6723 if (allow_expansion_p
6724 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6726 /* Consume the `...'. */
6727 cp_lexer_consume_token (parser
->lexer
);
6729 /* Build the argument pack. */
6730 expr
= make_pack_expansion (expr
);
6733 /* Add it to the list. We add error_mark_node
6734 expressions to the list, so that we can still tell if
6735 the correct form for a parenthesized expression-list
6736 is found. That gives better errors. */
6737 vec_safe_push (expression_list
, expr
);
6739 if (expr
== error_mark_node
)
6743 /* After the first item, attribute lists look the same as
6744 expression lists. */
6745 is_attribute_list
= non_attr
;
6748 /* If the next token isn't a `,', then we are done. */
6749 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6752 /* Otherwise, consume the `,' and keep going. */
6753 cp_lexer_consume_token (parser
->lexer
);
6756 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6761 /* We try and resync to an unnested comma, as that will give the
6762 user better diagnostics. */
6763 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6764 /*recovering=*/true,
6766 /*consume_paren=*/true);
6771 parser
->greater_than_is_operator_p
6772 = saved_greater_than_is_operator_p
;
6777 parser
->greater_than_is_operator_p
6778 = saved_greater_than_is_operator_p
;
6781 vec_safe_insert (expression_list
, 0, identifier
);
6783 return expression_list
;
6786 /* Parse a pseudo-destructor-name.
6788 pseudo-destructor-name:
6789 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6790 :: [opt] nested-name-specifier template template-id :: ~ type-name
6791 :: [opt] nested-name-specifier [opt] ~ type-name
6793 If either of the first two productions is used, sets *SCOPE to the
6794 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6795 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6796 or ERROR_MARK_NODE if the parse fails. */
6799 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6804 bool nested_name_specifier_p
;
6807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
6808 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
6809 && !type_dependent_expression_p (object
))
6811 if (cxx_dialect
< cxx1y
)
6812 pedwarn (input_location
, 0,
6813 "%<~auto%> only available with "
6814 "-std=c++1y or -std=gnu++1y");
6815 cp_lexer_consume_token (parser
->lexer
);
6816 cp_lexer_consume_token (parser
->lexer
);
6818 *type
= TREE_TYPE (object
);
6822 /* Assume that things will not work out. */
6823 *type
= error_mark_node
;
6825 /* Look for the optional `::' operator. */
6826 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
6827 /* Look for the optional nested-name-specifier. */
6828 nested_name_specifier_p
6829 = (cp_parser_nested_name_specifier_opt (parser
,
6830 /*typename_keyword_p=*/false,
6831 /*check_dependency_p=*/true,
6833 /*is_declaration=*/false)
6835 /* Now, if we saw a nested-name-specifier, we might be doing the
6836 second production. */
6837 if (nested_name_specifier_p
6838 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
6840 /* Consume the `template' keyword. */
6841 cp_lexer_consume_token (parser
->lexer
);
6842 /* Parse the template-id. */
6843 cp_parser_template_id (parser
,
6844 /*template_keyword_p=*/true,
6845 /*check_dependency_p=*/false,
6847 /*is_declaration=*/true);
6848 /* Look for the `::' token. */
6849 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6851 /* If the next token is not a `~', then there might be some
6852 additional qualification. */
6853 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
6855 /* At this point, we're looking for "type-name :: ~". The type-name
6856 must not be a class-name, since this is a pseudo-destructor. So,
6857 it must be either an enum-name, or a typedef-name -- both of which
6858 are just identifiers. So, we peek ahead to check that the "::"
6859 and "~" tokens are present; if they are not, then we can avoid
6860 calling type_name. */
6861 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
6862 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
6863 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
6865 cp_parser_error (parser
, "non-scalar type");
6869 /* Look for the type-name. */
6870 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6871 if (*scope
== error_mark_node
)
6874 /* Look for the `::' token. */
6875 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6880 /* Look for the `~'. */
6881 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
6883 /* Once we see the ~, this has to be a pseudo-destructor. */
6884 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
6885 cp_parser_commit_to_topmost_tentative_parse (parser
);
6887 /* Look for the type-name again. We are not responsible for
6888 checking that it matches the first type-name. */
6889 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6892 /* Parse a unary-expression.
6898 unary-operator cast-expression
6899 sizeof unary-expression
6901 alignof ( type-id ) [C++0x]
6908 __extension__ cast-expression
6909 __alignof__ unary-expression
6910 __alignof__ ( type-id )
6911 alignof unary-expression [C++0x]
6912 __real__ cast-expression
6913 __imag__ cast-expression
6915 sizeof ( type-id ) { initializer-list , [opt] }
6916 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6917 __alignof__ ( type-id ) { initializer-list , [opt] }
6919 ADDRESS_P is true iff the unary-expression is appearing as the
6920 operand of the `&' operator. CAST_P is true if this expression is
6921 the target of a cast.
6923 Returns a representation of the expression. */
6926 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6927 bool decltype_p
, cp_id_kind
* pidk
)
6930 enum tree_code unary_operator
;
6932 /* Peek at the next token. */
6933 token
= cp_lexer_peek_token (parser
->lexer
);
6934 /* Some keywords give away the kind of expression. */
6935 if (token
->type
== CPP_KEYWORD
)
6937 enum rid keyword
= token
->keyword
;
6946 location_t first_loc
;
6948 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
6949 /* Consume the token. */
6950 cp_lexer_consume_token (parser
->lexer
);
6951 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6952 /* Parse the operand. */
6953 operand
= cp_parser_sizeof_operand (parser
, keyword
);
6955 if (TYPE_P (operand
))
6956 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
6959 /* ISO C++ defines alignof only with types, not with
6960 expressions. So pedwarn if alignof is used with a non-
6961 type expression. However, __alignof__ is ok. */
6962 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
6963 pedwarn (token
->location
, OPT_Wpedantic
,
6964 "ISO C++ does not allow %<alignof%> "
6967 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
6969 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6970 SIZEOF_EXPR with the original operand. */
6971 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
6973 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
6975 if (!processing_template_decl
&& TYPE_P (operand
))
6977 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
6978 build1 (NOP_EXPR
, operand
,
6980 SIZEOF_EXPR_TYPE_P (ret
) = 1;
6983 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
6984 TREE_SIDE_EFFECTS (ret
) = 0;
6985 TREE_READONLY (ret
) = 1;
6987 SET_EXPR_LOCATION (ret
, first_loc
);
6993 return cp_parser_new_expression (parser
);
6996 return cp_parser_delete_expression (parser
);
7000 /* The saved value of the PEDANTIC flag. */
7004 /* Save away the PEDANTIC flag. */
7005 cp_parser_extension_opt (parser
, &saved_pedantic
);
7006 /* Parse the cast-expression. */
7007 expr
= cp_parser_simple_cast_expression (parser
);
7008 /* Restore the PEDANTIC flag. */
7009 pedantic
= saved_pedantic
;
7019 /* Consume the `__real__' or `__imag__' token. */
7020 cp_lexer_consume_token (parser
->lexer
);
7021 /* Parse the cast-expression. */
7022 expression
= cp_parser_simple_cast_expression (parser
);
7023 /* Create the complete representation. */
7024 return build_x_unary_op (token
->location
,
7025 (keyword
== RID_REALPART
7026 ? REALPART_EXPR
: IMAGPART_EXPR
),
7028 tf_warning_or_error
);
7032 case RID_TRANSACTION_ATOMIC
:
7033 case RID_TRANSACTION_RELAXED
:
7034 return cp_parser_transaction_expression (parser
, keyword
);
7039 const char *saved_message
;
7040 bool saved_integral_constant_expression_p
;
7041 bool saved_non_integral_constant_expression_p
;
7042 bool saved_greater_than_is_operator_p
;
7044 cp_lexer_consume_token (parser
->lexer
);
7045 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7047 saved_message
= parser
->type_definition_forbidden_message
;
7048 parser
->type_definition_forbidden_message
7049 = G_("types may not be defined in %<noexcept%> expressions");
7051 saved_integral_constant_expression_p
7052 = parser
->integral_constant_expression_p
;
7053 saved_non_integral_constant_expression_p
7054 = parser
->non_integral_constant_expression_p
;
7055 parser
->integral_constant_expression_p
= false;
7057 saved_greater_than_is_operator_p
7058 = parser
->greater_than_is_operator_p
;
7059 parser
->greater_than_is_operator_p
= true;
7061 ++cp_unevaluated_operand
;
7062 ++c_inhibit_evaluation_warnings
;
7063 expr
= cp_parser_expression (parser
, false, NULL
);
7064 --c_inhibit_evaluation_warnings
;
7065 --cp_unevaluated_operand
;
7067 parser
->greater_than_is_operator_p
7068 = saved_greater_than_is_operator_p
;
7070 parser
->integral_constant_expression_p
7071 = saved_integral_constant_expression_p
;
7072 parser
->non_integral_constant_expression_p
7073 = saved_non_integral_constant_expression_p
;
7075 parser
->type_definition_forbidden_message
= saved_message
;
7077 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7078 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7086 /* Look for the `:: new' and `:: delete', which also signal the
7087 beginning of a new-expression, or delete-expression,
7088 respectively. If the next token is `::', then it might be one of
7090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7094 /* See if the token after the `::' is one of the keywords in
7095 which we're interested. */
7096 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7097 /* If it's `new', we have a new-expression. */
7098 if (keyword
== RID_NEW
)
7099 return cp_parser_new_expression (parser
);
7100 /* Similarly, for `delete'. */
7101 else if (keyword
== RID_DELETE
)
7102 return cp_parser_delete_expression (parser
);
7105 /* Look for a unary operator. */
7106 unary_operator
= cp_parser_unary_operator (token
);
7107 /* The `++' and `--' operators can be handled similarly, even though
7108 they are not technically unary-operators in the grammar. */
7109 if (unary_operator
== ERROR_MARK
)
7111 if (token
->type
== CPP_PLUS_PLUS
)
7112 unary_operator
= PREINCREMENT_EXPR
;
7113 else if (token
->type
== CPP_MINUS_MINUS
)
7114 unary_operator
= PREDECREMENT_EXPR
;
7115 /* Handle the GNU address-of-label extension. */
7116 else if (cp_parser_allow_gnu_extensions_p (parser
)
7117 && token
->type
== CPP_AND_AND
)
7121 location_t loc
= token
->location
;
7123 /* Consume the '&&' token. */
7124 cp_lexer_consume_token (parser
->lexer
);
7125 /* Look for the identifier. */
7126 identifier
= cp_parser_identifier (parser
);
7127 /* Create an expression representing the address. */
7128 expression
= finish_label_address_expr (identifier
, loc
);
7129 if (cp_parser_non_integral_constant_expression (parser
,
7131 expression
= error_mark_node
;
7135 if (unary_operator
!= ERROR_MARK
)
7137 tree cast_expression
;
7138 tree expression
= error_mark_node
;
7139 non_integral_constant non_constant_p
= NIC_NONE
;
7140 location_t loc
= token
->location
;
7141 tsubst_flags_t complain
= complain_flags (decltype_p
);
7143 /* Consume the operator token. */
7144 token
= cp_lexer_consume_token (parser
->lexer
);
7145 /* Parse the cast-expression. */
7147 = cp_parser_cast_expression (parser
,
7148 unary_operator
== ADDR_EXPR
,
7152 /* Now, build an appropriate representation. */
7153 switch (unary_operator
)
7156 non_constant_p
= NIC_STAR
;
7157 expression
= build_x_indirect_ref (loc
, cast_expression
,
7163 non_constant_p
= NIC_ADDR
;
7166 expression
= build_x_unary_op (loc
, unary_operator
,
7171 case PREINCREMENT_EXPR
:
7172 case PREDECREMENT_EXPR
:
7173 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7174 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7176 case UNARY_PLUS_EXPR
:
7178 case TRUTH_NOT_EXPR
:
7179 expression
= finish_unary_op_expr (loc
, unary_operator
,
7180 cast_expression
, complain
);
7187 if (non_constant_p
!= NIC_NONE
7188 && cp_parser_non_integral_constant_expression (parser
,
7190 expression
= error_mark_node
;
7195 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7196 /*member_access_only_p=*/false,
7202 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7205 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7206 /*decltype*/false, pidk
);
7209 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7210 unary-operator, the corresponding tree code is returned. */
7212 static enum tree_code
7213 cp_parser_unary_operator (cp_token
* token
)
7215 switch (token
->type
)
7218 return INDIRECT_REF
;
7224 return UNARY_PLUS_EXPR
;
7230 return TRUTH_NOT_EXPR
;
7233 return BIT_NOT_EXPR
;
7240 /* Parse a new-expression.
7243 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7244 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7246 Returns a representation of the expression. */
7249 cp_parser_new_expression (cp_parser
* parser
)
7251 bool global_scope_p
;
7252 vec
<tree
, va_gc
> *placement
;
7254 vec
<tree
, va_gc
> *initializer
;
7255 tree nelts
= NULL_TREE
;
7258 /* Look for the optional `::' operator. */
7260 = (cp_parser_global_scope_opt (parser
,
7261 /*current_scope_valid_p=*/false)
7263 /* Look for the `new' operator. */
7264 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7265 /* There's no easy way to tell a new-placement from the
7266 `( type-id )' construct. */
7267 cp_parser_parse_tentatively (parser
);
7268 /* Look for a new-placement. */
7269 placement
= cp_parser_new_placement (parser
);
7270 /* If that didn't work out, there's no new-placement. */
7271 if (!cp_parser_parse_definitely (parser
))
7273 if (placement
!= NULL
)
7274 release_tree_vector (placement
);
7278 /* If the next token is a `(', then we have a parenthesized
7280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7283 const char *saved_message
= parser
->type_definition_forbidden_message
;
7285 /* Consume the `('. */
7286 cp_lexer_consume_token (parser
->lexer
);
7288 /* Parse the type-id. */
7289 parser
->type_definition_forbidden_message
7290 = G_("types may not be defined in a new-expression");
7291 type
= cp_parser_type_id (parser
);
7292 parser
->type_definition_forbidden_message
= saved_message
;
7294 /* Look for the closing `)'. */
7295 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7296 token
= cp_lexer_peek_token (parser
->lexer
);
7297 /* There should not be a direct-new-declarator in this production,
7298 but GCC used to allowed this, so we check and emit a sensible error
7299 message for this case. */
7300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7302 error_at (token
->location
,
7303 "array bound forbidden after parenthesized type-id");
7304 inform (token
->location
,
7305 "try removing the parentheses around the type-id");
7306 cp_parser_direct_new_declarator (parser
);
7309 /* Otherwise, there must be a new-type-id. */
7311 type
= cp_parser_new_type_id (parser
, &nelts
);
7313 /* If the next token is a `(' or '{', then we have a new-initializer. */
7314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7315 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7316 initializer
= cp_parser_new_initializer (parser
);
7320 /* A new-expression may not appear in an integral constant
7322 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7323 ret
= error_mark_node
;
7326 /* Create a representation of the new-expression. */
7327 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7328 tf_warning_or_error
);
7331 if (placement
!= NULL
)
7332 release_tree_vector (placement
);
7333 if (initializer
!= NULL
)
7334 release_tree_vector (initializer
);
7339 /* Parse a new-placement.
7344 Returns the same representation as for an expression-list. */
7346 static vec
<tree
, va_gc
> *
7347 cp_parser_new_placement (cp_parser
* parser
)
7349 vec
<tree
, va_gc
> *expression_list
;
7351 /* Parse the expression-list. */
7352 expression_list
= (cp_parser_parenthesized_expression_list
7353 (parser
, non_attr
, /*cast_p=*/false,
7354 /*allow_expansion_p=*/true,
7355 /*non_constant_p=*/NULL
));
7357 return expression_list
;
7360 /* Parse a new-type-id.
7363 type-specifier-seq new-declarator [opt]
7365 Returns the TYPE allocated. If the new-type-id indicates an array
7366 type, *NELTS is set to the number of elements in the last array
7367 bound; the TYPE will not include the last array bound. */
7370 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7372 cp_decl_specifier_seq type_specifier_seq
;
7373 cp_declarator
*new_declarator
;
7374 cp_declarator
*declarator
;
7375 cp_declarator
*outer_declarator
;
7376 const char *saved_message
;
7378 /* The type-specifier sequence must not contain type definitions.
7379 (It cannot contain declarations of new types either, but if they
7380 are not definitions we will catch that because they are not
7382 saved_message
= parser
->type_definition_forbidden_message
;
7383 parser
->type_definition_forbidden_message
7384 = G_("types may not be defined in a new-type-id");
7385 /* Parse the type-specifier-seq. */
7386 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7387 /*is_trailing_return=*/false,
7388 &type_specifier_seq
);
7389 /* Restore the old message. */
7390 parser
->type_definition_forbidden_message
= saved_message
;
7392 if (type_specifier_seq
.type
== error_mark_node
)
7393 return error_mark_node
;
7395 /* Parse the new-declarator. */
7396 new_declarator
= cp_parser_new_declarator_opt (parser
);
7398 /* Determine the number of elements in the last array dimension, if
7401 /* Skip down to the last array dimension. */
7402 declarator
= new_declarator
;
7403 outer_declarator
= NULL
;
7404 while (declarator
&& (declarator
->kind
== cdk_pointer
7405 || declarator
->kind
== cdk_ptrmem
))
7407 outer_declarator
= declarator
;
7408 declarator
= declarator
->declarator
;
7411 && declarator
->kind
== cdk_array
7412 && declarator
->declarator
7413 && declarator
->declarator
->kind
== cdk_array
)
7415 outer_declarator
= declarator
;
7416 declarator
= declarator
->declarator
;
7419 if (declarator
&& declarator
->kind
== cdk_array
)
7421 *nelts
= declarator
->u
.array
.bounds
;
7422 if (*nelts
== error_mark_node
)
7423 *nelts
= integer_one_node
;
7425 if (outer_declarator
)
7426 outer_declarator
->declarator
= declarator
->declarator
;
7428 new_declarator
= NULL
;
7431 return groktypename (&type_specifier_seq
, new_declarator
, false);
7434 /* Parse an (optional) new-declarator.
7437 ptr-operator new-declarator [opt]
7438 direct-new-declarator
7440 Returns the declarator. */
7442 static cp_declarator
*
7443 cp_parser_new_declarator_opt (cp_parser
* parser
)
7445 enum tree_code code
;
7446 tree type
, std_attributes
= NULL_TREE
;
7447 cp_cv_quals cv_quals
;
7449 /* We don't know if there's a ptr-operator next, or not. */
7450 cp_parser_parse_tentatively (parser
);
7451 /* Look for a ptr-operator. */
7452 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7453 /* If that worked, look for more new-declarators. */
7454 if (cp_parser_parse_definitely (parser
))
7456 cp_declarator
*declarator
;
7458 /* Parse another optional declarator. */
7459 declarator
= cp_parser_new_declarator_opt (parser
);
7461 declarator
= cp_parser_make_indirect_declarator
7462 (code
, type
, cv_quals
, declarator
, std_attributes
);
7467 /* If the next token is a `[', there is a direct-new-declarator. */
7468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7469 return cp_parser_direct_new_declarator (parser
);
7474 /* Parse a direct-new-declarator.
7476 direct-new-declarator:
7478 direct-new-declarator [constant-expression]
7482 static cp_declarator
*
7483 cp_parser_direct_new_declarator (cp_parser
* parser
)
7485 cp_declarator
*declarator
= NULL
;
7492 /* Look for the opening `['. */
7493 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7495 token
= cp_lexer_peek_token (parser
->lexer
);
7496 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7497 /* The standard requires that the expression have integral
7498 type. DR 74 adds enumeration types. We believe that the
7499 real intent is that these expressions be handled like the
7500 expression in a `switch' condition, which also allows
7501 classes with a single conversion to integral or
7502 enumeration type. */
7503 if (!processing_template_decl
)
7506 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7511 error_at (token
->location
,
7512 "expression in new-declarator must have integral "
7513 "or enumeration type");
7514 expression
= error_mark_node
;
7518 /* Look for the closing `]'. */
7519 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7521 /* Add this bound to the declarator. */
7522 declarator
= make_array_declarator (declarator
, expression
);
7524 /* If the next token is not a `[', then there are no more
7526 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7533 /* Parse a new-initializer.
7536 ( expression-list [opt] )
7539 Returns a representation of the expression-list. */
7541 static vec
<tree
, va_gc
> *
7542 cp_parser_new_initializer (cp_parser
* parser
)
7544 vec
<tree
, va_gc
> *expression_list
;
7546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7549 bool expr_non_constant_p
;
7550 cp_lexer_set_source_position (parser
->lexer
);
7551 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7552 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7553 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7554 expression_list
= make_tree_vector_single (t
);
7557 expression_list
= (cp_parser_parenthesized_expression_list
7558 (parser
, non_attr
, /*cast_p=*/false,
7559 /*allow_expansion_p=*/true,
7560 /*non_constant_p=*/NULL
));
7562 return expression_list
;
7565 /* Parse a delete-expression.
7568 :: [opt] delete cast-expression
7569 :: [opt] delete [ ] cast-expression
7571 Returns a representation of the expression. */
7574 cp_parser_delete_expression (cp_parser
* parser
)
7576 bool global_scope_p
;
7580 /* Look for the optional `::' operator. */
7582 = (cp_parser_global_scope_opt (parser
,
7583 /*current_scope_valid_p=*/false)
7585 /* Look for the `delete' keyword. */
7586 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7587 /* See if the array syntax is in use. */
7588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7590 /* Consume the `[' token. */
7591 cp_lexer_consume_token (parser
->lexer
);
7592 /* Look for the `]' token. */
7593 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7594 /* Remember that this is the `[]' construct. */
7600 /* Parse the cast-expression. */
7601 expression
= cp_parser_simple_cast_expression (parser
);
7603 /* A delete-expression may not appear in an integral constant
7605 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7606 return error_mark_node
;
7608 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7609 tf_warning_or_error
);
7612 /* Returns true if TOKEN may start a cast-expression and false
7616 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7618 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7619 switch (token
->type
)
7625 case CPP_CLOSE_SQUARE
:
7626 case CPP_CLOSE_PAREN
:
7627 case CPP_CLOSE_BRACE
:
7628 case CPP_OPEN_BRACE
:
7632 case CPP_DEREF_STAR
:
7640 case CPP_GREATER_EQ
:
7660 case CPP_OPEN_PAREN
:
7661 /* In ((type ()) () the last () isn't a valid cast-expression,
7662 so the whole must be parsed as postfix-expression. */
7663 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7666 /* '[' may start a primary-expression in obj-c++. */
7667 case CPP_OPEN_SQUARE
:
7668 return c_dialect_objc ();
7675 /* Parse a cast-expression.
7679 ( type-id ) cast-expression
7681 ADDRESS_P is true iff the unary-expression is appearing as the
7682 operand of the `&' operator. CAST_P is true if this expression is
7683 the target of a cast.
7685 Returns a representation of the expression. */
7688 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7689 bool decltype_p
, cp_id_kind
* pidk
)
7691 /* If it's a `(', then we might be looking at a cast. */
7692 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7694 tree type
= NULL_TREE
;
7695 tree expr
= NULL_TREE
;
7696 bool cast_expression_p
;
7697 const char *saved_message
;
7699 /* There's no way to know yet whether or not this is a cast.
7700 For example, `(int (3))' is a unary-expression, while `(int)
7701 3' is a cast. So, we resort to parsing tentatively. */
7702 cp_parser_parse_tentatively (parser
);
7703 /* Types may not be defined in a cast. */
7704 saved_message
= parser
->type_definition_forbidden_message
;
7705 parser
->type_definition_forbidden_message
7706 = G_("types may not be defined in casts");
7707 /* Consume the `('. */
7708 cp_lexer_consume_token (parser
->lexer
);
7709 /* A very tricky bit is that `(struct S) { 3 }' is a
7710 compound-literal (which we permit in C++ as an extension).
7711 But, that construct is not a cast-expression -- it is a
7712 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7713 is legal; if the compound-literal were a cast-expression,
7714 you'd need an extra set of parentheses.) But, if we parse
7715 the type-id, and it happens to be a class-specifier, then we
7716 will commit to the parse at that point, because we cannot
7717 undo the action that is done when creating a new class. So,
7718 then we cannot back up and do a postfix-expression.
7719 Another tricky case is the following (c++/29234):
7721 struct S { void operator () (); };
7728 As a type-id we parse the parenthesized S()() as a function
7729 returning a function, groktypename complains and we cannot
7730 back up in this case either.
7732 Therefore, we scan ahead to the closing `)', and check to see
7733 if the tokens after the `)' can start a cast-expression. Otherwise
7734 we are dealing with an unary-expression, a postfix-expression
7737 Save tokens so that we can put them back. */
7738 cp_lexer_save_tokens (parser
->lexer
);
7740 /* We may be looking at a cast-expression. */
7742 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7743 /*consume_paren=*/true)
7744 && cp_parser_tokens_start_cast_expression (parser
));
7746 /* Roll back the tokens we skipped. */
7747 cp_lexer_rollback_tokens (parser
->lexer
);
7748 /* If we aren't looking at a cast-expression, simulate an error so
7749 that the call to cp_parser_parse_definitely below will fail. */
7750 if (!cast_expression_p
)
7751 cp_parser_simulate_error (parser
);
7754 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7755 parser
->in_type_id_in_expr_p
= true;
7756 /* Look for the type-id. */
7757 type
= cp_parser_type_id (parser
);
7758 /* Look for the closing `)'. */
7759 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7760 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7763 /* Restore the saved message. */
7764 parser
->type_definition_forbidden_message
= saved_message
;
7766 /* At this point this can only be either a cast or a
7767 parenthesized ctor such as `(T ())' that looks like a cast to
7768 function returning T. */
7769 if (!cp_parser_error_occurred (parser
))
7771 cp_parser_parse_definitely (parser
);
7772 expr
= cp_parser_cast_expression (parser
,
7773 /*address_p=*/false,
7775 /*decltype_p=*/false,
7778 /* Warn about old-style casts, if so requested. */
7779 if (warn_old_style_cast
7780 && !in_system_header_at (input_location
)
7781 && !VOID_TYPE_P (type
)
7782 && current_lang_name
!= lang_name_c
)
7783 warning (OPT_Wold_style_cast
, "use of old-style cast");
7785 /* Only type conversions to integral or enumeration types
7786 can be used in constant-expressions. */
7787 if (!cast_valid_in_integral_constant_expression_p (type
)
7788 && cp_parser_non_integral_constant_expression (parser
,
7790 return error_mark_node
;
7792 /* Perform the cast. */
7793 expr
= build_c_cast (input_location
, type
, expr
);
7797 cp_parser_abort_tentative_parse (parser
);
7800 /* If we get here, then it's not a cast, so it must be a
7801 unary-expression. */
7802 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7806 /* Parse a binary expression of the general form:
7810 pm-expression .* cast-expression
7811 pm-expression ->* cast-expression
7813 multiplicative-expression:
7815 multiplicative-expression * pm-expression
7816 multiplicative-expression / pm-expression
7817 multiplicative-expression % pm-expression
7819 additive-expression:
7820 multiplicative-expression
7821 additive-expression + multiplicative-expression
7822 additive-expression - multiplicative-expression
7826 shift-expression << additive-expression
7827 shift-expression >> additive-expression
7829 relational-expression:
7831 relational-expression < shift-expression
7832 relational-expression > shift-expression
7833 relational-expression <= shift-expression
7834 relational-expression >= shift-expression
7838 relational-expression:
7839 relational-expression <? shift-expression
7840 relational-expression >? shift-expression
7842 equality-expression:
7843 relational-expression
7844 equality-expression == relational-expression
7845 equality-expression != relational-expression
7849 and-expression & equality-expression
7851 exclusive-or-expression:
7853 exclusive-or-expression ^ and-expression
7855 inclusive-or-expression:
7856 exclusive-or-expression
7857 inclusive-or-expression | exclusive-or-expression
7859 logical-and-expression:
7860 inclusive-or-expression
7861 logical-and-expression && inclusive-or-expression
7863 logical-or-expression:
7864 logical-and-expression
7865 logical-or-expression || logical-and-expression
7867 All these are implemented with a single function like:
7870 simple-cast-expression
7871 binary-expression <token> binary-expression
7873 CAST_P is true if this expression is the target of a cast.
7875 The binops_by_token map is used to get the tree codes for each <token> type.
7876 binary-expressions are associated according to a precedence table. */
7878 #define TOKEN_PRECEDENCE(token) \
7879 (((token->type == CPP_GREATER \
7880 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7881 && !parser->greater_than_is_operator_p) \
7882 ? PREC_NOT_OPERATOR \
7883 : binops_by_token[token->type].prec)
7886 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7887 bool no_toplevel_fold_p
,
7889 enum cp_parser_prec prec
,
7892 cp_parser_expression_stack stack
;
7893 cp_parser_expression_stack_entry
*sp
= &stack
[0];
7894 cp_parser_expression_stack_entry current
;
7897 enum tree_code rhs_type
;
7898 enum cp_parser_prec new_prec
, lookahead_prec
;
7901 /* Parse the first expression. */
7902 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
7903 cast_p
, decltype_p
, pidk
);
7904 current
.lhs_type
= ERROR_MARK
;
7905 current
.prec
= prec
;
7907 if (cp_parser_error_occurred (parser
))
7908 return error_mark_node
;
7912 /* Get an operator token. */
7913 token
= cp_lexer_peek_token (parser
->lexer
);
7915 if (warn_cxx0x_compat
7916 && token
->type
== CPP_RSHIFT
7917 && !parser
->greater_than_is_operator_p
)
7919 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
7920 "%<>>%> operator is treated"
7921 " as two right angle brackets in C++11"))
7922 inform (token
->location
,
7923 "suggest parentheses around %<>>%> expression");
7926 new_prec
= TOKEN_PRECEDENCE (token
);
7928 /* Popping an entry off the stack means we completed a subexpression:
7929 - either we found a token which is not an operator (`>' where it is not
7930 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7931 will happen repeatedly;
7932 - or, we found an operator which has lower priority. This is the case
7933 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7935 if (new_prec
<= current
.prec
)
7944 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
7945 current
.loc
= token
->location
;
7947 /* We used the operator token. */
7948 cp_lexer_consume_token (parser
->lexer
);
7950 /* For "false && x" or "true || x", x will never be executed;
7951 disable warnings while evaluating it. */
7952 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7953 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
7954 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7955 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
7957 /* Extract another operand. It may be the RHS of this expression
7958 or the LHS of a new, higher priority expression. */
7959 rhs
= cp_parser_simple_cast_expression (parser
);
7960 rhs_type
= ERROR_MARK
;
7962 /* Get another operator token. Look up its precedence to avoid
7963 building a useless (immediately popped) stack entry for common
7964 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7965 token
= cp_lexer_peek_token (parser
->lexer
);
7966 lookahead_prec
= TOKEN_PRECEDENCE (token
);
7967 if (lookahead_prec
> new_prec
)
7969 /* ... and prepare to parse the RHS of the new, higher priority
7970 expression. Since precedence levels on the stack are
7971 monotonically increasing, we do not have to care about
7976 current
.lhs_type
= rhs_type
;
7977 current
.prec
= new_prec
;
7978 new_prec
= lookahead_prec
;
7982 lookahead_prec
= new_prec
;
7983 /* If the stack is not empty, we have parsed into LHS the right side
7984 (`4' in the example above) of an expression we had suspended.
7985 We can use the information on the stack to recover the LHS (`3')
7986 from the stack together with the tree code (`MULT_EXPR'), and
7987 the precedence of the higher level subexpression
7988 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7989 which will be used to actually build the additive expression. */
7991 rhs_type
= current
.lhs_type
;
7996 /* Undo the disabling of warnings done above. */
7997 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7998 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
7999 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8000 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
8003 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8004 ERROR_MARK for everything that is not a binary expression.
8005 This makes warn_about_parentheses miss some warnings that
8006 involve unary operators. For unary expressions we should
8007 pass the correct tree_code unless the unary expression was
8008 surrounded by parentheses.
8010 if (no_toplevel_fold_p
8011 && lookahead_prec
<= current
.prec
8013 current
.lhs
= build2 (current
.tree_type
,
8014 TREE_CODE_CLASS (current
.tree_type
)
8016 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8019 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8020 current
.lhs
, current
.lhs_type
,
8021 rhs
, rhs_type
, &overload
,
8022 complain_flags (decltype_p
));
8023 current
.lhs_type
= current
.tree_type
;
8024 if (EXPR_P (current
.lhs
))
8025 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
8027 /* If the binary operator required the use of an overloaded operator,
8028 then this expression cannot be an integral constant-expression.
8029 An overloaded operator can be used even if both operands are
8030 otherwise permissible in an integral constant-expression if at
8031 least one of the operands is of enumeration type. */
8034 && cp_parser_non_integral_constant_expression (parser
,
8036 return error_mark_node
;
8043 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8044 bool no_toplevel_fold_p
,
8045 enum cp_parser_prec prec
,
8048 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8049 /*decltype*/false, prec
, pidk
);
8052 /* Parse the `? expression : assignment-expression' part of a
8053 conditional-expression. The LOGICAL_OR_EXPR is the
8054 logical-or-expression that started the conditional-expression.
8055 Returns a representation of the entire conditional-expression.
8057 This routine is used by cp_parser_assignment_expression.
8059 ? expression : assignment-expression
8063 ? : assignment-expression */
8066 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8069 tree assignment_expr
;
8070 struct cp_token
*token
;
8071 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8073 /* Consume the `?' token. */
8074 cp_lexer_consume_token (parser
->lexer
);
8075 token
= cp_lexer_peek_token (parser
->lexer
);
8076 if (cp_parser_allow_gnu_extensions_p (parser
)
8077 && token
->type
== CPP_COLON
)
8079 pedwarn (token
->location
, OPT_Wpedantic
,
8080 "ISO C++ does not allow ?: with omitted middle operand");
8081 /* Implicit true clause. */
8083 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8084 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8088 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8089 parser
->colon_corrects_to_scope_p
= false;
8090 /* Parse the expression. */
8091 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8092 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8093 c_inhibit_evaluation_warnings
+=
8094 ((logical_or_expr
== truthvalue_true_node
)
8095 - (logical_or_expr
== truthvalue_false_node
));
8096 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8099 /* The next token should be a `:'. */
8100 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8101 /* Parse the assignment-expression. */
8102 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
8103 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8105 /* Build the conditional-expression. */
8106 return build_x_conditional_expr (loc
, logical_or_expr
,
8109 tf_warning_or_error
);
8112 /* Parse an assignment-expression.
8114 assignment-expression:
8115 conditional-expression
8116 logical-or-expression assignment-operator assignment_expression
8119 CAST_P is true if this expression is the target of a cast.
8120 DECLTYPE_P is true if this expression is the operand of decltype.
8122 Returns a representation for the expression. */
8125 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
8126 bool decltype_p
, cp_id_kind
* pidk
)
8130 /* If the next token is the `throw' keyword, then we're looking at
8131 a throw-expression. */
8132 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8133 expr
= cp_parser_throw_expression (parser
);
8134 /* Otherwise, it must be that we are looking at a
8135 logical-or-expression. */
8138 /* Parse the binary expressions (logical-or-expression). */
8139 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8141 PREC_NOT_OPERATOR
, pidk
);
8142 /* If the next token is a `?' then we're actually looking at a
8143 conditional-expression. */
8144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8145 return cp_parser_question_colon_clause (parser
, expr
);
8148 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8150 /* If it's an assignment-operator, we're using the second
8152 enum tree_code assignment_operator
8153 = cp_parser_assignment_operator_opt (parser
);
8154 if (assignment_operator
!= ERROR_MARK
)
8156 bool non_constant_p
;
8157 location_t saved_input_location
;
8159 /* Parse the right-hand side of the assignment. */
8160 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8162 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8165 /* An assignment may not appear in a
8166 constant-expression. */
8167 if (cp_parser_non_integral_constant_expression (parser
,
8169 return error_mark_node
;
8170 /* Build the assignment expression. Its default
8171 location is the location of the '=' token. */
8172 saved_input_location
= input_location
;
8173 input_location
= loc
;
8174 expr
= build_x_modify_expr (loc
, expr
,
8175 assignment_operator
,
8177 complain_flags (decltype_p
));
8178 input_location
= saved_input_location
;
8187 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
8190 return cp_parser_assignment_expression (parser
, cast_p
,
8191 /*decltype*/false, pidk
);
8194 /* Parse an (optional) assignment-operator.
8196 assignment-operator: one of
8197 = *= /= %= += -= >>= <<= &= ^= |=
8201 assignment-operator: one of
8204 If the next token is an assignment operator, the corresponding tree
8205 code is returned, and the token is consumed. For example, for
8206 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8207 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8208 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8209 operator, ERROR_MARK is returned. */
8211 static enum tree_code
8212 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8217 /* Peek at the next token. */
8218 token
= cp_lexer_peek_token (parser
->lexer
);
8220 switch (token
->type
)
8231 op
= TRUNC_DIV_EXPR
;
8235 op
= TRUNC_MOD_EXPR
;
8267 /* Nothing else is an assignment operator. */
8271 /* If it was an assignment operator, consume it. */
8272 if (op
!= ERROR_MARK
)
8273 cp_lexer_consume_token (parser
->lexer
);
8278 /* Parse an expression.
8281 assignment-expression
8282 expression , assignment-expression
8284 CAST_P is true if this expression is the target of a cast.
8285 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8286 except possibly parenthesized or on the RHS of a comma (N3276).
8288 Returns a representation of the expression. */
8291 cp_parser_expression (cp_parser
* parser
, bool cast_p
, bool decltype_p
,
8294 tree expression
= NULL_TREE
;
8295 location_t loc
= UNKNOWN_LOCATION
;
8299 tree assignment_expression
;
8301 /* Parse the next assignment-expression. */
8302 assignment_expression
8303 = cp_parser_assignment_expression (parser
, cast_p
, decltype_p
, pidk
);
8305 /* We don't create a temporary for a call that is the immediate operand
8306 of decltype or on the RHS of a comma. But when we see a comma, we
8307 need to create a temporary for a call on the LHS. */
8308 if (decltype_p
&& !processing_template_decl
8309 && TREE_CODE (assignment_expression
) == CALL_EXPR
8310 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8311 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8312 assignment_expression
8313 = build_cplus_new (TREE_TYPE (assignment_expression
),
8314 assignment_expression
, tf_warning_or_error
);
8316 /* If this is the first assignment-expression, we can just
8319 expression
= assignment_expression
;
8321 expression
= build_x_compound_expr (loc
, expression
,
8322 assignment_expression
,
8323 complain_flags (decltype_p
));
8324 /* If the next token is not a comma, then we are done with the
8326 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8328 /* Consume the `,'. */
8329 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8330 cp_lexer_consume_token (parser
->lexer
);
8331 /* A comma operator cannot appear in a constant-expression. */
8332 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8333 expression
= error_mark_node
;
8340 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
8342 return cp_parser_expression (parser
, cast_p
, /*decltype*/false, pidk
);
8345 /* Parse a constant-expression.
8347 constant-expression:
8348 conditional-expression
8350 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8351 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8352 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8353 is false, NON_CONSTANT_P should be NULL. */
8356 cp_parser_constant_expression (cp_parser
* parser
,
8357 bool allow_non_constant_p
,
8358 bool *non_constant_p
)
8360 bool saved_integral_constant_expression_p
;
8361 bool saved_allow_non_integral_constant_expression_p
;
8362 bool saved_non_integral_constant_expression_p
;
8365 /* It might seem that we could simply parse the
8366 conditional-expression, and then check to see if it were
8367 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8368 one that the compiler can figure out is constant, possibly after
8369 doing some simplifications or optimizations. The standard has a
8370 precise definition of constant-expression, and we must honor
8371 that, even though it is somewhat more restrictive.
8377 is not a legal declaration, because `(2, 3)' is not a
8378 constant-expression. The `,' operator is forbidden in a
8379 constant-expression. However, GCC's constant-folding machinery
8380 will fold this operation to an INTEGER_CST for `3'. */
8382 /* Save the old settings. */
8383 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8384 saved_allow_non_integral_constant_expression_p
8385 = parser
->allow_non_integral_constant_expression_p
;
8386 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8387 /* We are now parsing a constant-expression. */
8388 parser
->integral_constant_expression_p
= true;
8389 parser
->allow_non_integral_constant_expression_p
8390 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8391 parser
->non_integral_constant_expression_p
= false;
8392 /* Although the grammar says "conditional-expression", we parse an
8393 "assignment-expression", which also permits "throw-expression"
8394 and the use of assignment operators. In the case that
8395 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8396 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8397 actually essential that we look for an assignment-expression.
8398 For example, cp_parser_initializer_clauses uses this function to
8399 determine whether a particular assignment-expression is in fact
8401 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
8402 /* Restore the old settings. */
8403 parser
->integral_constant_expression_p
8404 = saved_integral_constant_expression_p
;
8405 parser
->allow_non_integral_constant_expression_p
8406 = saved_allow_non_integral_constant_expression_p
;
8407 if (cxx_dialect
>= cxx11
)
8409 /* Require an rvalue constant expression here; that's what our
8410 callers expect. Reference constant expressions are handled
8411 separately in e.g. cp_parser_template_argument. */
8412 bool is_const
= potential_rvalue_constant_expression (expression
);
8413 parser
->non_integral_constant_expression_p
= !is_const
;
8414 if (!is_const
&& !allow_non_constant_p
)
8415 require_potential_rvalue_constant_expression (expression
);
8417 if (allow_non_constant_p
)
8418 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8419 parser
->non_integral_constant_expression_p
8420 = saved_non_integral_constant_expression_p
;
8425 /* Parse __builtin_offsetof.
8427 offsetof-expression:
8428 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8430 offsetof-member-designator:
8432 | offsetof-member-designator "." id-expression
8433 | offsetof-member-designator "[" expression "]"
8434 | offsetof-member-designator "->" id-expression */
8437 cp_parser_builtin_offsetof (cp_parser
*parser
)
8439 int save_ice_p
, save_non_ice_p
;
8444 /* We're about to accept non-integral-constant things, but will
8445 definitely yield an integral constant expression. Save and
8446 restore these values around our local parsing. */
8447 save_ice_p
= parser
->integral_constant_expression_p
;
8448 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8450 /* Consume the "__builtin_offsetof" token. */
8451 cp_lexer_consume_token (parser
->lexer
);
8452 /* Consume the opening `('. */
8453 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8454 /* Parse the type-id. */
8455 type
= cp_parser_type_id (parser
);
8456 /* Look for the `,'. */
8457 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8458 token
= cp_lexer_peek_token (parser
->lexer
);
8460 /* Build the (type *)null that begins the traditional offsetof macro. */
8461 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8462 tf_warning_or_error
);
8464 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8465 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8466 true, &dummy
, token
->location
);
8469 token
= cp_lexer_peek_token (parser
->lexer
);
8470 switch (token
->type
)
8472 case CPP_OPEN_SQUARE
:
8473 /* offsetof-member-designator "[" expression "]" */
8474 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8479 /* offsetof-member-designator "->" identifier */
8480 expr
= grok_array_decl (token
->location
, expr
,
8481 integer_zero_node
, false);
8485 /* offsetof-member-designator "." identifier */
8486 cp_lexer_consume_token (parser
->lexer
);
8487 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8492 case CPP_CLOSE_PAREN
:
8493 /* Consume the ")" token. */
8494 cp_lexer_consume_token (parser
->lexer
);
8498 /* Error. We know the following require will fail, but
8499 that gives the proper error message. */
8500 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8501 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8502 expr
= error_mark_node
;
8508 /* If we're processing a template, we can't finish the semantics yet.
8509 Otherwise we can fold the entire expression now. */
8510 if (processing_template_decl
)
8511 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
8513 expr
= finish_offsetof (expr
);
8516 parser
->integral_constant_expression_p
= save_ice_p
;
8517 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8522 /* Parse a trait expression.
8524 Returns a representation of the expression, the underlying type
8525 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8528 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8531 tree type1
, type2
= NULL_TREE
;
8532 bool binary
= false;
8533 cp_decl_specifier_seq decl_specs
;
8537 case RID_HAS_NOTHROW_ASSIGN
:
8538 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8540 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8541 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8543 case RID_HAS_NOTHROW_COPY
:
8544 kind
= CPTK_HAS_NOTHROW_COPY
;
8546 case RID_HAS_TRIVIAL_ASSIGN
:
8547 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8549 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8550 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8552 case RID_HAS_TRIVIAL_COPY
:
8553 kind
= CPTK_HAS_TRIVIAL_COPY
;
8555 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8556 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8558 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8559 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8561 case RID_IS_ABSTRACT
:
8562 kind
= CPTK_IS_ABSTRACT
;
8564 case RID_IS_BASE_OF
:
8565 kind
= CPTK_IS_BASE_OF
;
8569 kind
= CPTK_IS_CLASS
;
8571 case RID_IS_CONVERTIBLE_TO
:
8572 kind
= CPTK_IS_CONVERTIBLE_TO
;
8576 kind
= CPTK_IS_EMPTY
;
8579 kind
= CPTK_IS_ENUM
;
8582 kind
= CPTK_IS_FINAL
;
8584 case RID_IS_LITERAL_TYPE
:
8585 kind
= CPTK_IS_LITERAL_TYPE
;
8590 case RID_IS_POLYMORPHIC
:
8591 kind
= CPTK_IS_POLYMORPHIC
;
8593 case RID_IS_STD_LAYOUT
:
8594 kind
= CPTK_IS_STD_LAYOUT
;
8596 case RID_IS_TRIVIAL
:
8597 kind
= CPTK_IS_TRIVIAL
;
8600 kind
= CPTK_IS_UNION
;
8602 case RID_UNDERLYING_TYPE
:
8603 kind
= CPTK_UNDERLYING_TYPE
;
8608 case RID_DIRECT_BASES
:
8609 kind
= CPTK_DIRECT_BASES
;
8615 /* Consume the token. */
8616 cp_lexer_consume_token (parser
->lexer
);
8618 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8620 type1
= cp_parser_type_id (parser
);
8622 if (type1
== error_mark_node
)
8623 return error_mark_node
;
8625 /* Build a trivial decl-specifier-seq. */
8626 clear_decl_specs (&decl_specs
);
8627 decl_specs
.type
= type1
;
8629 /* Call grokdeclarator to figure out what type this is. */
8630 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8631 /*initialized=*/0, /*attrlist=*/NULL
);
8635 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8637 type2
= cp_parser_type_id (parser
);
8639 if (type2
== error_mark_node
)
8640 return error_mark_node
;
8642 /* Build a trivial decl-specifier-seq. */
8643 clear_decl_specs (&decl_specs
);
8644 decl_specs
.type
= type2
;
8646 /* Call grokdeclarator to figure out what type this is. */
8647 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8648 /*initialized=*/0, /*attrlist=*/NULL
);
8651 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8653 /* Complete the trait expression, which may mean either processing
8654 the trait expr now or saving it for template instantiation. */
8657 case CPTK_UNDERLYING_TYPE
:
8658 return finish_underlying_type (type1
);
8660 return finish_bases (type1
, false);
8661 case CPTK_DIRECT_BASES
:
8662 return finish_bases (type1
, true);
8664 return finish_trait_expr (kind
, type1
, type2
);
8668 /* Lambdas that appear in variable initializer or default argument scope
8669 get that in their mangling, so we need to record it. We might as well
8670 use the count for function and namespace scopes as well. */
8671 static GTY(()) tree lambda_scope
;
8672 static GTY(()) int lambda_count
;
8673 typedef struct GTY(()) tree_int
8678 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8681 start_lambda_scope (tree decl
)
8685 /* Once we're inside a function, we ignore other scopes and just push
8686 the function again so that popping works properly. */
8687 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8688 decl
= current_function_decl
;
8689 ti
.t
= lambda_scope
;
8690 ti
.i
= lambda_count
;
8691 vec_safe_push (lambda_scope_stack
, ti
);
8692 if (lambda_scope
!= decl
)
8694 /* Don't reset the count if we're still in the same function. */
8695 lambda_scope
= decl
;
8701 record_lambda_scope (tree lambda
)
8703 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8704 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8708 finish_lambda_scope (void)
8710 tree_int
*p
= &lambda_scope_stack
->last ();
8711 if (lambda_scope
!= p
->t
)
8713 lambda_scope
= p
->t
;
8714 lambda_count
= p
->i
;
8716 lambda_scope_stack
->pop ();
8719 /* Parse a lambda expression.
8722 lambda-introducer lambda-declarator [opt] compound-statement
8724 Returns a representation of the expression. */
8727 cp_parser_lambda_expression (cp_parser
* parser
)
8729 tree lambda_expr
= build_lambda_expr ();
8732 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8734 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
8736 if (cp_unevaluated_operand
)
8738 if (!token
->error_reported
)
8740 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8741 "lambda-expression in unevaluated context");
8742 token
->error_reported
= true;
8747 /* We may be in the middle of deferred access check. Disable
8749 push_deferring_access_checks (dk_no_deferred
);
8751 cp_parser_lambda_introducer (parser
, lambda_expr
);
8753 type
= begin_lambda_type (lambda_expr
);
8754 if (type
== error_mark_node
)
8755 return error_mark_node
;
8757 record_lambda_scope (lambda_expr
);
8759 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8760 determine_visibility (TYPE_NAME (type
));
8762 /* Now that we've started the type, add the capture fields for any
8763 explicit captures. */
8764 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8767 /* Inside the class, surrounding template-parameter-lists do not apply. */
8768 unsigned int saved_num_template_parameter_lists
8769 = parser
->num_template_parameter_lists
;
8770 unsigned char in_statement
= parser
->in_statement
;
8771 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
8772 bool fully_implicit_function_template_p
8773 = parser
->fully_implicit_function_template_p
;
8774 tree implicit_template_parms
= parser
->implicit_template_parms
;
8775 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
8776 bool auto_is_implicit_function_template_parm_p
8777 = parser
->auto_is_implicit_function_template_parm_p
;
8779 parser
->num_template_parameter_lists
= 0;
8780 parser
->in_statement
= 0;
8781 parser
->in_switch_statement_p
= false;
8782 parser
->fully_implicit_function_template_p
= false;
8783 parser
->implicit_template_parms
= 0;
8784 parser
->implicit_template_scope
= 0;
8785 parser
->auto_is_implicit_function_template_parm_p
= false;
8787 /* By virtue of defining a local class, a lambda expression has access to
8788 the private variables of enclosing classes. */
8790 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
8793 cp_parser_lambda_body (parser
, lambda_expr
);
8794 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8796 if (cp_parser_skip_to_closing_brace (parser
))
8797 cp_lexer_consume_token (parser
->lexer
);
8800 /* The capture list was built up in reverse order; fix that now. */
8801 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
8802 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8805 maybe_add_lambda_conv_op (type
);
8807 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
8809 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
8810 parser
->in_statement
= in_statement
;
8811 parser
->in_switch_statement_p
= in_switch_statement_p
;
8812 parser
->fully_implicit_function_template_p
8813 = fully_implicit_function_template_p
;
8814 parser
->implicit_template_parms
= implicit_template_parms
;
8815 parser
->implicit_template_scope
= implicit_template_scope
;
8816 parser
->auto_is_implicit_function_template_parm_p
8817 = auto_is_implicit_function_template_parm_p
;
8820 pop_deferring_access_checks ();
8822 /* This field is only used during parsing of the lambda. */
8823 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
8825 /* This lambda shouldn't have any proxies left at this point. */
8826 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
8827 /* And now that we're done, push proxies for an enclosing lambda. */
8828 insert_pending_capture_proxies ();
8831 return build_lambda_object (lambda_expr
);
8833 return error_mark_node
;
8836 /* Parse the beginning of a lambda expression.
8839 [ lambda-capture [opt] ]
8841 LAMBDA_EXPR is the current representation of the lambda expression. */
8844 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
8846 /* Need commas after the first capture. */
8849 /* Eat the leading `['. */
8850 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8852 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
8854 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
8855 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
8856 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8857 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
8859 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
8861 cp_lexer_consume_token (parser
->lexer
);
8865 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
8867 cp_token
* capture_token
;
8869 tree capture_init_expr
;
8870 cp_id_kind idk
= CP_ID_KIND_NONE
;
8871 bool explicit_init_p
= false;
8873 enum capture_kind_type
8878 enum capture_kind_type capture_kind
= BY_COPY
;
8880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
8882 error ("expected end of capture-list");
8889 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8891 /* Possibly capture `this'. */
8892 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
8894 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8895 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
8896 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
8897 "with by-copy capture default");
8898 cp_lexer_consume_token (parser
->lexer
);
8899 add_capture (lambda_expr
,
8900 /*id=*/this_identifier
,
8901 /*initializer=*/finish_this_expr(),
8902 /*by_reference_p=*/false,
8907 /* Remember whether we want to capture as a reference or not. */
8908 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
8910 capture_kind
= BY_REFERENCE
;
8911 cp_lexer_consume_token (parser
->lexer
);
8914 /* Get the identifier. */
8915 capture_token
= cp_lexer_peek_token (parser
->lexer
);
8916 capture_id
= cp_parser_identifier (parser
);
8918 if (capture_id
== error_mark_node
)
8919 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8920 delimiters, but I modified this to stop on unnested ']' as well. It
8921 was already changed to stop on unnested '}', so the
8922 "closing_parenthesis" name is no more misleading with my change. */
8924 cp_parser_skip_to_closing_parenthesis (parser
,
8925 /*recovering=*/true,
8927 /*consume_paren=*/true);
8931 /* Find the initializer for this capture. */
8932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
8933 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
8934 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8936 bool direct
, non_constant
;
8937 /* An explicit initializer exists. */
8938 if (cxx_dialect
< cxx1y
)
8939 pedwarn (input_location
, 0,
8940 "lambda capture initializers "
8941 "only available with -std=c++1y or -std=gnu++1y");
8942 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
8944 explicit_init_p
= true;
8945 if (capture_init_expr
== NULL_TREE
)
8947 error ("empty initializer for lambda init-capture");
8948 capture_init_expr
= error_mark_node
;
8953 const char* error_msg
;
8955 /* Turn the identifier into an id-expression. */
8957 = cp_parser_lookup_name_simple (parser
, capture_id
,
8958 capture_token
->location
);
8960 if (capture_init_expr
== error_mark_node
)
8962 unqualified_name_lookup_error (capture_id
);
8965 else if (DECL_P (capture_init_expr
)
8966 && (!VAR_P (capture_init_expr
)
8967 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
8969 error_at (capture_token
->location
,
8970 "capture of non-variable %qD ",
8972 inform (0, "%q+#D declared here", capture_init_expr
);
8975 if (VAR_P (capture_init_expr
)
8976 && decl_storage_duration (capture_init_expr
) != dk_auto
)
8978 if (pedwarn (capture_token
->location
, 0, "capture of variable "
8979 "%qD with non-automatic storage duration",
8981 inform (0, "%q+#D declared here", capture_init_expr
);
8986 = finish_id_expression
8991 /*integral_constant_expression_p=*/false,
8992 /*allow_non_integral_constant_expression_p=*/false,
8993 /*non_integral_constant_expression_p=*/NULL
,
8994 /*template_p=*/false,
8996 /*address_p=*/false,
8997 /*template_arg_p=*/false,
8999 capture_token
->location
);
9001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9003 cp_lexer_consume_token (parser
->lexer
);
9004 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9007 check_for_bare_parameter_packs (capture_init_expr
);
9010 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9011 && !explicit_init_p
)
9013 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9014 && capture_kind
== BY_COPY
)
9015 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9016 "of %qD redundant with by-copy capture default",
9018 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9019 && capture_kind
== BY_REFERENCE
)
9020 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9021 "capture of %qD redundant with by-reference capture "
9022 "default", capture_id
);
9025 add_capture (lambda_expr
,
9028 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9032 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9035 /* Parse the (optional) middle of a lambda expression.
9038 < template-parameter-list [opt] >
9039 ( parameter-declaration-clause [opt] )
9040 attribute-specifier [opt]
9042 exception-specification [opt]
9043 lambda-return-type-clause [opt]
9045 LAMBDA_EXPR is the current representation of the lambda expression. */
9048 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9050 /* 5.1.1.4 of the standard says:
9051 If a lambda-expression does not include a lambda-declarator, it is as if
9052 the lambda-declarator were ().
9053 This means an empty parameter list, no attributes, and no exception
9055 tree param_list
= void_list_node
;
9056 tree attributes
= NULL_TREE
;
9057 tree exception_spec
= NULL_TREE
;
9058 tree template_param_list
= NULL_TREE
;
9060 /* The template-parameter-list is optional, but must begin with
9061 an opening angle if present. */
9062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9064 if (cxx_dialect
< cxx1y
)
9065 pedwarn (parser
->lexer
->next_token
->location
, 0,
9066 "lambda templates are only available with "
9067 "-std=c++1y or -std=gnu++1y");
9069 cp_lexer_consume_token (parser
->lexer
);
9071 template_param_list
= cp_parser_template_parameter_list (parser
);
9073 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9075 /* We just processed one more parameter list. */
9076 ++parser
->num_template_parameter_lists
;
9079 /* The parameter-declaration-clause is optional (unless
9080 template-parameter-list was given), but must begin with an
9081 opening parenthesis if present. */
9082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9084 cp_lexer_consume_token (parser
->lexer
);
9086 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9088 /* Parse parameters. */
9089 param_list
= cp_parser_parameter_declaration_clause (parser
);
9091 /* Default arguments shall not be specified in the
9092 parameter-declaration-clause of a lambda-declarator. */
9093 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9094 if (TREE_PURPOSE (t
))
9095 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9096 "default argument specified for lambda parameter");
9098 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9100 attributes
= cp_parser_attributes_opt (parser
);
9102 /* Parse optional `mutable' keyword. */
9103 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9105 cp_lexer_consume_token (parser
->lexer
);
9106 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9109 /* Parse optional exception specification. */
9110 exception_spec
= cp_parser_exception_specification_opt (parser
);
9112 /* Parse optional trailing return type. */
9113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9115 cp_lexer_consume_token (parser
->lexer
);
9116 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9117 = cp_parser_trailing_type_id (parser
);
9120 /* The function parameters must be in scope all the way until after the
9121 trailing-return-type in case of decltype. */
9122 pop_bindings_and_leave_scope ();
9124 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9125 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9127 /* Create the function call operator.
9129 Messing with declarators like this is no uglier than building up the
9130 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9133 cp_decl_specifier_seq return_type_specs
;
9134 cp_declarator
* declarator
;
9139 clear_decl_specs (&return_type_specs
);
9140 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9141 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9143 /* Maybe we will deduce the return type later. */
9144 return_type_specs
.type
= make_auto ();
9146 p
= obstack_alloc (&declarator_obstack
, 0);
9148 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9151 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9152 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9153 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9154 VIRT_SPEC_UNSPECIFIED
,
9157 /*late_return_type=*/NULL_TREE
);
9158 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9160 fco
= grokmethod (&return_type_specs
,
9163 if (fco
!= error_mark_node
)
9165 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9166 DECL_ARTIFICIAL (fco
) = 1;
9167 /* Give the object parameter a different name. */
9168 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9170 if (template_param_list
)
9172 fco
= finish_member_template_decl (fco
);
9173 finish_template_decl (template_param_list
);
9174 --parser
->num_template_parameter_lists
;
9176 else if (parser
->fully_implicit_function_template_p
)
9177 fco
= finish_fully_implicit_template (parser
, fco
);
9179 finish_member_declaration (fco
);
9181 obstack_free (&declarator_obstack
, p
);
9183 return (fco
!= error_mark_node
);
9187 /* Parse the body of a lambda expression, which is simply
9191 but which requires special handling.
9192 LAMBDA_EXPR is the current representation of the lambda expression. */
9195 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9197 bool nested
= (current_function_decl
!= NULL_TREE
);
9198 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9200 push_function_context ();
9202 /* Still increment function_depth so that we don't GC in the
9203 middle of an expression. */
9205 /* Clear this in case we're in the middle of a default argument. */
9206 parser
->local_variables_forbidden_p
= false;
9208 /* Finish the function call operator
9210 + late_parsing_for_member
9211 + function_definition_after_declarator
9212 + ctor_initializer_opt_and_function_body */
9214 tree fco
= lambda_function (lambda_expr
);
9220 /* Let the front end know that we are going to be defining this
9222 start_preparsed_function (fco
,
9224 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9226 start_lambda_scope (fco
);
9227 body
= begin_function_body ();
9229 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9232 /* Push the proxies for any explicit captures. */
9233 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9234 cap
= TREE_CHAIN (cap
))
9235 build_capture_proxy (TREE_PURPOSE (cap
));
9237 compound_stmt
= begin_compound_stmt (0);
9239 /* 5.1.1.4 of the standard says:
9240 If a lambda-expression does not include a trailing-return-type, it
9241 is as if the trailing-return-type denotes the following type:
9242 * if the compound-statement is of the form
9243 { return attribute-specifier [opt] expression ; }
9244 the type of the returned expression after lvalue-to-rvalue
9245 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9246 (_conv.array_ 4.2), and function-to-pointer conversion
9248 * otherwise, void. */
9250 /* In a lambda that has neither a lambda-return-type-clause
9251 nor a deducible form, errors should be reported for return statements
9252 in the body. Since we used void as the placeholder return type, parsing
9253 the body as usual will give such desired behavior. */
9254 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9255 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9256 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9258 tree expr
= NULL_TREE
;
9259 cp_id_kind idk
= CP_ID_KIND_NONE
;
9261 /* Parse tentatively in case there's more after the initial return
9263 cp_parser_parse_tentatively (parser
);
9265 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9267 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
9269 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9270 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9272 if (cp_parser_parse_definitely (parser
))
9274 if (!processing_template_decl
)
9275 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9277 /* Will get error here if type not deduced yet. */
9278 finish_return_stmt (expr
);
9286 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9287 cp_parser_label_declaration (parser
);
9288 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9289 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9292 finish_compound_stmt (compound_stmt
);
9295 finish_function_body (body
);
9296 finish_lambda_scope ();
9298 /* Finish the function and generate code for it if necessary. */
9299 tree fn
= finish_function (/*inline*/2);
9301 /* Only expand if the call op is not a template. */
9302 if (!DECL_TEMPLATE_INFO (fco
))
9303 expand_or_defer_fn (fn
);
9306 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9308 pop_function_context();
9313 /* Statements [gram.stmt.stmt] */
9315 /* Parse a statement.
9319 expression-statement
9324 declaration-statement
9331 attribute-specifier-seq (opt) expression-statement
9332 attribute-specifier-seq (opt) compound-statement
9333 attribute-specifier-seq (opt) selection-statement
9334 attribute-specifier-seq (opt) iteration-statement
9335 attribute-specifier-seq (opt) jump-statement
9336 declaration-statement
9337 attribute-specifier-seq (opt) try-block
9344 IN_COMPOUND is true when the statement is nested inside a
9345 cp_parser_compound_statement; this matters for certain pragmas.
9347 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9348 is a (possibly labeled) if statement which is not enclosed in braces
9349 and has an else clause. This is used to implement -Wparentheses. */
9352 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9353 bool in_compound
, bool *if_p
)
9355 tree statement
, std_attrs
= NULL_TREE
;
9357 location_t statement_location
, attrs_location
;
9362 /* There is no statement yet. */
9363 statement
= NULL_TREE
;
9365 cp_lexer_save_tokens (parser
->lexer
);
9366 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9367 if (c_dialect_objc ())
9368 /* In obj-c++, seeing '[[' might be the either the beginning of
9369 c++11 attributes, or a nested objc-message-expression. So
9370 let's parse the c++11 attributes tentatively. */
9371 cp_parser_parse_tentatively (parser
);
9372 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9373 if (c_dialect_objc ())
9375 if (!cp_parser_parse_definitely (parser
))
9376 std_attrs
= NULL_TREE
;
9379 /* Peek at the next token. */
9380 token
= cp_lexer_peek_token (parser
->lexer
);
9381 /* Remember the location of the first token in the statement. */
9382 statement_location
= token
->location
;
9383 /* If this is a keyword, then that will often determine what kind of
9384 statement we have. */
9385 if (token
->type
== CPP_KEYWORD
)
9387 enum rid keyword
= token
->keyword
;
9393 /* Looks like a labeled-statement with a case label.
9394 Parse the label, and then use tail recursion to parse
9396 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9401 statement
= cp_parser_selection_statement (parser
, if_p
);
9407 statement
= cp_parser_iteration_statement (parser
, false);
9414 statement
= cp_parser_jump_statement (parser
);
9417 /* Objective-C++ exception-handling constructs. */
9420 case RID_AT_FINALLY
:
9421 case RID_AT_SYNCHRONIZED
:
9423 statement
= cp_parser_objc_statement (parser
);
9427 statement
= cp_parser_try_block (parser
);
9431 /* This must be a namespace alias definition. */
9432 cp_parser_declaration_statement (parser
);
9435 case RID_TRANSACTION_ATOMIC
:
9436 case RID_TRANSACTION_RELAXED
:
9437 statement
= cp_parser_transaction (parser
, keyword
);
9439 case RID_TRANSACTION_CANCEL
:
9440 statement
= cp_parser_transaction_cancel (parser
);
9444 /* It might be a keyword like `int' that can start a
9445 declaration-statement. */
9449 else if (token
->type
== CPP_NAME
)
9451 /* If the next token is a `:', then we are looking at a
9452 labeled-statement. */
9453 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9454 if (token
->type
== CPP_COLON
)
9456 /* Looks like a labeled-statement with an ordinary label.
9457 Parse the label, and then use tail recursion to parse
9460 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9464 /* Anything that starts with a `{' must be a compound-statement. */
9465 else if (token
->type
== CPP_OPEN_BRACE
)
9466 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9467 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9468 a statement all its own. */
9469 else if (token
->type
== CPP_PRAGMA
)
9471 /* Only certain OpenMP pragmas are attached to statements, and thus
9472 are considered statements themselves. All others are not. In
9473 the context of a compound, accept the pragma as a "statement" and
9474 return so that we can check for a close brace. Otherwise we
9475 require a real statement and must go back and read one. */
9477 cp_parser_pragma (parser
, pragma_compound
);
9478 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9482 else if (token
->type
== CPP_EOF
)
9484 cp_parser_error (parser
, "expected statement");
9488 /* Everything else must be a declaration-statement or an
9489 expression-statement. Try for the declaration-statement
9490 first, unless we are looking at a `;', in which case we know that
9491 we have an expression-statement. */
9494 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9496 if (std_attrs
!= NULL_TREE
)
9498 /* Attributes should be parsed as part of the the
9499 declaration, so let's un-parse them. */
9500 cp_lexer_rollback_tokens (parser
->lexer
);
9501 std_attrs
= NULL_TREE
;
9504 cp_parser_parse_tentatively (parser
);
9505 /* Try to parse the declaration-statement. */
9506 cp_parser_declaration_statement (parser
);
9507 /* If that worked, we're done. */
9508 if (cp_parser_parse_definitely (parser
))
9511 /* Look for an expression-statement instead. */
9512 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9515 /* Set the line number for the statement. */
9516 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9517 SET_EXPR_LOCATION (statement
, statement_location
);
9519 /* Note that for now, we don't do anything with c++11 statements
9520 parsed at this level. */
9521 if (std_attrs
!= NULL_TREE
)
9522 warning_at (attrs_location
,
9524 "attributes at the beginning of statement are ignored");
9527 /* Parse the label for a labeled-statement, i.e.
9530 case constant-expression :
9534 case constant-expression ... constant-expression : statement
9536 When a label is parsed without errors, the label is added to the
9537 parse tree by the finish_* functions, so this function doesn't
9538 have to return the label. */
9541 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9544 tree label
= NULL_TREE
;
9545 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9547 /* The next token should be an identifier. */
9548 token
= cp_lexer_peek_token (parser
->lexer
);
9549 if (token
->type
!= CPP_NAME
9550 && token
->type
!= CPP_KEYWORD
)
9552 cp_parser_error (parser
, "expected labeled-statement");
9556 parser
->colon_corrects_to_scope_p
= false;
9557 switch (token
->keyword
)
9564 /* Consume the `case' token. */
9565 cp_lexer_consume_token (parser
->lexer
);
9566 /* Parse the constant-expression. */
9567 expr
= cp_parser_constant_expression (parser
,
9568 /*allow_non_constant_p=*/false,
9571 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9572 if (ellipsis
->type
== CPP_ELLIPSIS
)
9574 /* Consume the `...' token. */
9575 cp_lexer_consume_token (parser
->lexer
);
9577 cp_parser_constant_expression (parser
,
9578 /*allow_non_constant_p=*/false,
9580 /* We don't need to emit warnings here, as the common code
9581 will do this for us. */
9584 expr_hi
= NULL_TREE
;
9586 if (parser
->in_switch_statement_p
)
9587 finish_case_label (token
->location
, expr
, expr_hi
);
9589 error_at (token
->location
,
9590 "case label %qE not within a switch statement",
9596 /* Consume the `default' token. */
9597 cp_lexer_consume_token (parser
->lexer
);
9599 if (parser
->in_switch_statement_p
)
9600 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9602 error_at (token
->location
, "case label not within a switch statement");
9606 /* Anything else must be an ordinary label. */
9607 label
= finish_label_stmt (cp_parser_identifier (parser
));
9611 /* Require the `:' token. */
9612 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9614 /* An ordinary label may optionally be followed by attributes.
9615 However, this is only permitted if the attributes are then
9616 followed by a semicolon. This is because, for backward
9617 compatibility, when parsing
9618 lab: __attribute__ ((unused)) int i;
9619 we want the attribute to attach to "i", not "lab". */
9620 if (label
!= NULL_TREE
9621 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9624 cp_parser_parse_tentatively (parser
);
9625 attrs
= cp_parser_gnu_attributes_opt (parser
);
9626 if (attrs
== NULL_TREE
9627 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9628 cp_parser_abort_tentative_parse (parser
);
9629 else if (!cp_parser_parse_definitely (parser
))
9632 attributes
= chainon (attributes
, attrs
);
9635 if (attributes
!= NULL_TREE
)
9636 cplus_decl_attributes (&label
, attributes
, 0);
9638 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9641 /* Parse an expression-statement.
9643 expression-statement:
9646 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9647 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9648 indicates whether this expression-statement is part of an
9649 expression statement. */
9652 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9654 tree statement
= NULL_TREE
;
9655 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9657 /* If the next token is a ';', then there is no expression
9659 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9661 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9662 if (statement
== error_mark_node
9663 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9665 cp_parser_skip_to_end_of_block_or_statement (parser
);
9666 return error_mark_node
;
9670 /* Give a helpful message for "A<T>::type t;" and the like. */
9671 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9672 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9674 if (TREE_CODE (statement
) == SCOPE_REF
)
9675 error_at (token
->location
, "need %<typename%> before %qE because "
9676 "%qT is a dependent scope",
9677 statement
, TREE_OPERAND (statement
, 0));
9678 else if (is_overloaded_fn (statement
)
9679 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9682 tree fn
= get_first_fn (statement
);
9683 error_at (token
->location
,
9684 "%<%T::%D%> names the constructor, not the type",
9685 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9689 /* Consume the final `;'. */
9690 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9692 if (in_statement_expr
9693 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9694 /* This is the final expression statement of a statement
9696 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9698 statement
= finish_expr_stmt (statement
);
9703 /* Parse a compound-statement.
9706 { statement-seq [opt] }
9711 { label-declaration-seq [opt] statement-seq [opt] }
9713 label-declaration-seq:
9715 label-declaration-seq label-declaration
9717 Returns a tree representing the statement. */
9720 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
9721 bool in_try
, bool function_body
)
9725 /* Consume the `{'. */
9726 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9727 return error_mark_node
;
9728 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
9730 pedwarn (input_location
, OPT_Wpedantic
,
9731 "compound-statement in constexpr function");
9732 /* Begin the compound-statement. */
9733 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
9734 /* If the next keyword is `__label__' we have a label declaration. */
9735 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9736 cp_parser_label_declaration (parser
);
9737 /* Parse an (optional) statement-seq. */
9738 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
9739 /* Finish the compound-statement. */
9740 finish_compound_stmt (compound_stmt
);
9741 /* Consume the `}'. */
9742 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9744 return compound_stmt
;
9747 /* Parse an (optional) statement-seq.
9751 statement-seq [opt] statement */
9754 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
9756 /* Scan statements until there aren't any more. */
9759 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9761 /* If we are looking at a `}', then we have run out of
9762 statements; the same is true if we have reached the end
9763 of file, or have stumbled upon a stray '@end'. */
9764 if (token
->type
== CPP_CLOSE_BRACE
9765 || token
->type
== CPP_EOF
9766 || token
->type
== CPP_PRAGMA_EOL
9767 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
9770 /* If we are in a compound statement and find 'else' then
9771 something went wrong. */
9772 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
9774 if (parser
->in_statement
& IN_IF_STMT
)
9778 token
= cp_lexer_consume_token (parser
->lexer
);
9779 error_at (token
->location
, "%<else%> without a previous %<if%>");
9783 /* Parse the statement. */
9784 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
9788 /* Parse a selection-statement.
9790 selection-statement:
9791 if ( condition ) statement
9792 if ( condition ) statement else statement
9793 switch ( condition ) statement
9795 Returns the new IF_STMT or SWITCH_STMT.
9797 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9798 is a (possibly labeled) if statement which is not enclosed in
9799 braces and has an else clause. This is used to implement
9803 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
9811 /* Peek at the next token. */
9812 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
9814 /* See what kind of keyword it is. */
9815 keyword
= token
->keyword
;
9824 /* Look for the `('. */
9825 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
9827 cp_parser_skip_to_end_of_statement (parser
);
9828 return error_mark_node
;
9831 /* Begin the selection-statement. */
9832 if (keyword
== RID_IF
)
9833 statement
= begin_if_stmt ();
9835 statement
= begin_switch_stmt ();
9837 /* Parse the condition. */
9838 condition
= cp_parser_condition (parser
);
9839 /* Look for the `)'. */
9840 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
9841 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9842 /*consume_paren=*/true);
9844 if (keyword
== RID_IF
)
9847 unsigned char in_statement
;
9849 /* Add the condition. */
9850 finish_if_stmt_cond (condition
, statement
);
9852 /* Parse the then-clause. */
9853 in_statement
= parser
->in_statement
;
9854 parser
->in_statement
|= IN_IF_STMT
;
9855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9857 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9858 add_stmt (build_empty_stmt (loc
));
9859 cp_lexer_consume_token (parser
->lexer
);
9860 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
9861 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
9862 "empty body in an %<if%> statement");
9866 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
9867 parser
->in_statement
= in_statement
;
9869 finish_then_clause (statement
);
9871 /* If the next token is `else', parse the else-clause. */
9872 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9875 /* Consume the `else' keyword. */
9876 cp_lexer_consume_token (parser
->lexer
);
9877 begin_else_clause (statement
);
9878 /* Parse the else-clause. */
9879 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9882 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9884 OPT_Wempty_body
, "suggest braces around "
9885 "empty body in an %<else%> statement");
9886 add_stmt (build_empty_stmt (loc
));
9887 cp_lexer_consume_token (parser
->lexer
);
9890 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9892 finish_else_clause (statement
);
9894 /* If we are currently parsing a then-clause, then
9895 IF_P will not be NULL. We set it to true to
9896 indicate that this if statement has an else clause.
9897 This may trigger the Wparentheses warning below
9898 when we get back up to the parent if statement. */
9904 /* This if statement does not have an else clause. If
9905 NESTED_IF is true, then the then-clause is an if
9906 statement which does have an else clause. We warn
9907 about the potential ambiguity. */
9909 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
9910 "suggest explicit braces to avoid ambiguous"
9914 /* Now we're all done with the if-statement. */
9915 finish_if_stmt (statement
);
9919 bool in_switch_statement_p
;
9920 unsigned char in_statement
;
9922 /* Add the condition. */
9923 finish_switch_cond (condition
, statement
);
9925 /* Parse the body of the switch-statement. */
9926 in_switch_statement_p
= parser
->in_switch_statement_p
;
9927 in_statement
= parser
->in_statement
;
9928 parser
->in_switch_statement_p
= true;
9929 parser
->in_statement
|= IN_SWITCH_STMT
;
9930 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9931 parser
->in_switch_statement_p
= in_switch_statement_p
;
9932 parser
->in_statement
= in_statement
;
9934 /* Now we're all done with the switch-statement. */
9935 finish_switch_stmt (statement
);
9943 cp_parser_error (parser
, "expected selection-statement");
9944 return error_mark_node
;
9948 /* Parse a condition.
9952 type-specifier-seq declarator = initializer-clause
9953 type-specifier-seq declarator braced-init-list
9958 type-specifier-seq declarator asm-specification [opt]
9959 attributes [opt] = assignment-expression
9961 Returns the expression that should be tested. */
9964 cp_parser_condition (cp_parser
* parser
)
9966 cp_decl_specifier_seq type_specifiers
;
9967 const char *saved_message
;
9968 int declares_class_or_enum
;
9970 /* Try the declaration first. */
9971 cp_parser_parse_tentatively (parser
);
9972 /* New types are not allowed in the type-specifier-seq for a
9974 saved_message
= parser
->type_definition_forbidden_message
;
9975 parser
->type_definition_forbidden_message
9976 = G_("types may not be defined in conditions");
9977 /* Parse the type-specifier-seq. */
9978 cp_parser_decl_specifier_seq (parser
,
9979 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
9981 &declares_class_or_enum
);
9982 /* Restore the saved message. */
9983 parser
->type_definition_forbidden_message
= saved_message
;
9984 /* If all is well, we might be looking at a declaration. */
9985 if (!cp_parser_error_occurred (parser
))
9988 tree asm_specification
;
9990 cp_declarator
*declarator
;
9991 tree initializer
= NULL_TREE
;
9993 /* Parse the declarator. */
9994 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9995 /*ctor_dtor_or_conv_p=*/NULL
,
9996 /*parenthesized_p=*/NULL
,
9997 /*member_p=*/false);
9998 /* Parse the attributes. */
9999 attributes
= cp_parser_attributes_opt (parser
);
10000 /* Parse the asm-specification. */
10001 asm_specification
= cp_parser_asm_specification_opt (parser
);
10002 /* If the next token is not an `=' or '{', then we might still be
10003 looking at an expression. For example:
10007 looks like a decl-specifier-seq and a declarator -- but then
10008 there is no `=', so this is an expression. */
10009 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10010 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10011 cp_parser_simulate_error (parser
);
10013 /* If we did see an `=' or '{', then we are looking at a declaration
10015 if (cp_parser_parse_definitely (parser
))
10018 bool non_constant_p
;
10019 bool flags
= LOOKUP_ONLYCONVERTING
;
10021 /* Create the declaration. */
10022 decl
= start_decl (declarator
, &type_specifiers
,
10023 /*initialized_p=*/true,
10024 attributes
, /*prefix_attributes=*/NULL_TREE
,
10027 /* Parse the initializer. */
10028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10030 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10031 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10036 /* Consume the `='. */
10037 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10038 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10040 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10041 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10043 /* Process the initializer. */
10044 cp_finish_decl (decl
,
10045 initializer
, !non_constant_p
,
10050 pop_scope (pushed_scope
);
10052 return convert_from_reference (decl
);
10055 /* If we didn't even get past the declarator successfully, we are
10056 definitely not looking at a declaration. */
10058 cp_parser_abort_tentative_parse (parser
);
10060 /* Otherwise, we are looking at an expression. */
10061 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10064 /* Parses a for-statement or range-for-statement until the closing ')',
10068 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10070 tree init
, scope
, decl
;
10073 /* Begin the for-statement. */
10074 scope
= begin_for_scope (&init
);
10076 /* Parse the initialization. */
10077 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10080 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10082 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10086 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10088 /* Normal for loop */
10089 tree condition
= NULL_TREE
;
10090 tree expression
= NULL_TREE
;
10093 stmt
= begin_for_stmt (scope
, init
);
10094 /* The for-init-statement has already been parsed in
10095 cp_parser_for_init_statement, so no work is needed here. */
10096 finish_for_init_stmt (stmt
);
10098 /* If there's a condition, process it. */
10099 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10100 condition
= cp_parser_condition (parser
);
10103 cp_parser_error (parser
, "missing loop condition in loop with "
10104 "%<GCC ivdep%> pragma");
10105 condition
= error_mark_node
;
10107 finish_for_cond (condition
, stmt
, ivdep
);
10108 /* Look for the `;'. */
10109 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10111 /* If there's an expression, process it. */
10112 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10113 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10114 finish_for_expr (expression
, stmt
);
10119 /* Tries to parse a range-based for-statement:
10122 decl-specifier-seq declarator : expression
10124 The decl-specifier-seq declarator and the `:' are already parsed by
10125 cp_parser_for_init_statement. If processing_template_decl it returns a
10126 newly created RANGE_FOR_STMT; if not, it is converted to a
10127 regular FOR_STMT. */
10130 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10133 tree stmt
, range_expr
;
10135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10137 bool expr_non_constant_p
;
10138 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10141 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10143 /* If in template, STMT is converted to a normal for-statement
10144 at instantiation. If not, it is done just ahead. */
10145 if (processing_template_decl
)
10147 if (check_for_bare_parameter_packs (range_expr
))
10148 range_expr
= error_mark_node
;
10149 stmt
= begin_range_for_stmt (scope
, init
);
10151 RANGE_FOR_IVDEP (stmt
) = 1;
10152 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10153 if (!type_dependent_expression_p (range_expr
)
10154 /* do_auto_deduction doesn't mess with template init-lists. */
10155 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10156 do_range_for_auto_deduction (range_decl
, range_expr
);
10160 stmt
= begin_for_stmt (scope
, init
);
10161 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10166 /* Subroutine of cp_convert_range_for: given the initializer expression,
10167 builds up the range temporary. */
10170 build_range_temp (tree range_expr
)
10172 tree range_type
, range_temp
;
10174 /* Find out the type deduced by the declaration
10175 `auto &&__range = range_expr'. */
10176 range_type
= cp_build_reference_type (make_auto (), true);
10177 range_type
= do_auto_deduction (range_type
, range_expr
,
10178 type_uses_auto (range_type
));
10180 /* Create the __range variable. */
10181 range_temp
= build_decl (input_location
, VAR_DECL
,
10182 get_identifier ("__for_range"), range_type
);
10183 TREE_USED (range_temp
) = 1;
10184 DECL_ARTIFICIAL (range_temp
) = 1;
10189 /* Used by cp_parser_range_for in template context: we aren't going to
10190 do a full conversion yet, but we still need to resolve auto in the
10191 type of the for-range-declaration if present. This is basically
10192 a shortcut version of cp_convert_range_for. */
10195 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10197 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10200 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10201 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10202 iter_type
= (cp_parser_perform_range_for_lookup
10203 (range_temp
, &begin_dummy
, &end_dummy
));
10206 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10208 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10209 tf_warning_or_error
);
10210 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10211 iter_decl
, auto_node
);
10216 /* Converts a range-based for-statement into a normal
10217 for-statement, as per the definition.
10219 for (RANGE_DECL : RANGE_EXPR)
10222 should be equivalent to:
10225 auto &&__range = RANGE_EXPR;
10226 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10230 RANGE_DECL = *__begin;
10235 If RANGE_EXPR is an array:
10236 BEGIN_EXPR = __range
10237 END_EXPR = __range + ARRAY_SIZE(__range)
10238 Else if RANGE_EXPR has a member 'begin' or 'end':
10239 BEGIN_EXPR = __range.begin()
10240 END_EXPR = __range.end()
10242 BEGIN_EXPR = begin(__range)
10243 END_EXPR = end(__range);
10245 If __range has a member 'begin' but not 'end', or vice versa, we must
10246 still use the second alternative (it will surely fail, however).
10247 When calling begin()/end() in the third alternative we must use
10248 argument dependent lookup, but always considering 'std' as an associated
10252 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10256 tree iter_type
, begin_expr
, end_expr
;
10257 tree condition
, expression
;
10259 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10260 /* If an error happened previously do nothing or else a lot of
10261 unhelpful errors would be issued. */
10262 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10267 if (TREE_CODE (range_expr
) == VAR_DECL
10268 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10269 /* Can't bind a reference to an array of runtime bound. */
10270 range_temp
= range_expr
;
10273 range_temp
= build_range_temp (range_expr
);
10274 pushdecl (range_temp
);
10275 cp_finish_decl (range_temp
, range_expr
,
10276 /*is_constant_init*/false, NULL_TREE
,
10277 LOOKUP_ONLYCONVERTING
);
10278 range_temp
= convert_from_reference (range_temp
);
10280 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10281 &begin_expr
, &end_expr
);
10284 /* The new for initialization statement. */
10285 begin
= build_decl (input_location
, VAR_DECL
,
10286 get_identifier ("__for_begin"), iter_type
);
10287 TREE_USED (begin
) = 1;
10288 DECL_ARTIFICIAL (begin
) = 1;
10290 cp_finish_decl (begin
, begin_expr
,
10291 /*is_constant_init*/false, NULL_TREE
,
10292 LOOKUP_ONLYCONVERTING
);
10294 end
= build_decl (input_location
, VAR_DECL
,
10295 get_identifier ("__for_end"), iter_type
);
10296 TREE_USED (end
) = 1;
10297 DECL_ARTIFICIAL (end
) = 1;
10299 cp_finish_decl (end
, end_expr
,
10300 /*is_constant_init*/false, NULL_TREE
,
10301 LOOKUP_ONLYCONVERTING
);
10303 finish_for_init_stmt (statement
);
10305 /* The new for condition. */
10306 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10309 NULL
, tf_warning_or_error
);
10310 finish_for_cond (condition
, statement
, ivdep
);
10312 /* The new increment expression. */
10313 expression
= finish_unary_op_expr (input_location
,
10314 PREINCREMENT_EXPR
, begin
,
10315 tf_warning_or_error
);
10316 finish_for_expr (expression
, statement
);
10318 /* The declaration is initialized with *__begin inside the loop body. */
10319 cp_finish_decl (range_decl
,
10320 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10321 tf_warning_or_error
),
10322 /*is_constant_init*/false, NULL_TREE
,
10323 LOOKUP_ONLYCONVERTING
);
10328 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10329 We need to solve both at the same time because the method used
10330 depends on the existence of members begin or end.
10331 Returns the type deduced for the iterator expression. */
10334 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10336 if (error_operand_p (range
))
10338 *begin
= *end
= error_mark_node
;
10339 return error_mark_node
;
10342 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10344 error ("range-based %<for%> expression of type %qT "
10345 "has incomplete type", TREE_TYPE (range
));
10346 *begin
= *end
= error_mark_node
;
10347 return error_mark_node
;
10349 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10351 /* If RANGE is an array, we will use pointer arithmetic. */
10353 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10355 array_type_nelts_top (TREE_TYPE (range
)),
10357 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10361 /* If it is not an array, we must do a bit of magic. */
10362 tree id_begin
, id_end
;
10363 tree member_begin
, member_end
;
10365 *begin
= *end
= error_mark_node
;
10367 id_begin
= get_identifier ("begin");
10368 id_end
= get_identifier ("end");
10369 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10370 /*protect=*/2, /*want_type=*/false,
10371 tf_warning_or_error
);
10372 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10373 /*protect=*/2, /*want_type=*/false,
10374 tf_warning_or_error
);
10376 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10378 /* Use the member functions. */
10379 if (member_begin
!= NULL_TREE
)
10380 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10382 error ("range-based %<for%> expression of type %qT has an "
10383 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10385 if (member_end
!= NULL_TREE
)
10386 *end
= cp_parser_range_for_member_function (range
, id_end
);
10388 error ("range-based %<for%> expression of type %qT has a "
10389 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10393 /* Use global functions with ADL. */
10394 vec
<tree
, va_gc
> *vec
;
10395 vec
= make_tree_vector ();
10397 vec_safe_push (vec
, range
);
10399 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10400 tf_warning_or_error
);
10401 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10402 tf_warning_or_error
);
10403 member_end
= perform_koenig_lookup (id_end
, vec
,
10404 tf_warning_or_error
);
10405 *end
= finish_call_expr (member_end
, &vec
, false, true,
10406 tf_warning_or_error
);
10408 release_tree_vector (vec
);
10411 /* Last common checks. */
10412 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10414 /* If one of the expressions is an error do no more checks. */
10415 *begin
= *end
= error_mark_node
;
10416 return error_mark_node
;
10418 else if (type_dependent_expression_p (*begin
)
10419 || type_dependent_expression_p (*end
))
10420 /* Can happen, when, eg, in a template context, Koenig lookup
10421 can't resolve begin/end (c++/58503). */
10425 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10426 /* The unqualified type of the __begin and __end temporaries should
10427 be the same, as required by the multiple auto declaration. */
10428 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10429 error ("inconsistent begin/end types in range-based %<for%> "
10430 "statement: %qT and %qT",
10431 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10437 /* Helper function for cp_parser_perform_range_for_lookup.
10438 Builds a tree for RANGE.IDENTIFIER(). */
10441 cp_parser_range_for_member_function (tree range
, tree identifier
)
10444 vec
<tree
, va_gc
> *vec
;
10446 member
= finish_class_member_access_expr (range
, identifier
,
10447 false, tf_warning_or_error
);
10448 if (member
== error_mark_node
)
10449 return error_mark_node
;
10451 vec
= make_tree_vector ();
10452 res
= finish_call_expr (member
, &vec
,
10453 /*disallow_virtual=*/false,
10454 /*koenig_p=*/false,
10455 tf_warning_or_error
);
10456 release_tree_vector (vec
);
10460 /* Parse an iteration-statement.
10462 iteration-statement:
10463 while ( condition ) statement
10464 do statement while ( expression ) ;
10465 for ( for-init-statement condition [opt] ; expression [opt] )
10468 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10471 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10476 unsigned char in_statement
;
10478 /* Peek at the next token. */
10479 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10481 return error_mark_node
;
10483 /* Remember whether or not we are already within an iteration
10485 in_statement
= parser
->in_statement
;
10487 /* See what kind of keyword it is. */
10488 keyword
= token
->keyword
;
10495 /* Begin the while-statement. */
10496 statement
= begin_while_stmt ();
10497 /* Look for the `('. */
10498 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10499 /* Parse the condition. */
10500 condition
= cp_parser_condition (parser
);
10501 finish_while_stmt_cond (condition
, statement
, ivdep
);
10502 /* Look for the `)'. */
10503 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10504 /* Parse the dependent statement. */
10505 parser
->in_statement
= IN_ITERATION_STMT
;
10506 cp_parser_already_scoped_statement (parser
);
10507 parser
->in_statement
= in_statement
;
10508 /* We're done with the while-statement. */
10509 finish_while_stmt (statement
);
10517 /* Begin the do-statement. */
10518 statement
= begin_do_stmt ();
10519 /* Parse the body of the do-statement. */
10520 parser
->in_statement
= IN_ITERATION_STMT
;
10521 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10522 parser
->in_statement
= in_statement
;
10523 finish_do_body (statement
);
10524 /* Look for the `while' keyword. */
10525 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10526 /* Look for the `('. */
10527 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10528 /* Parse the expression. */
10529 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10530 /* We're done with the do-statement. */
10531 finish_do_stmt (expression
, statement
, ivdep
);
10532 /* Look for the `)'. */
10533 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10534 /* Look for the `;'. */
10535 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10541 /* Look for the `('. */
10542 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10544 statement
= cp_parser_for (parser
, ivdep
);
10546 /* Look for the `)'. */
10547 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10549 /* Parse the body of the for-statement. */
10550 parser
->in_statement
= IN_ITERATION_STMT
;
10551 cp_parser_already_scoped_statement (parser
);
10552 parser
->in_statement
= in_statement
;
10554 /* We're done with the for-statement. */
10555 finish_for_stmt (statement
);
10560 cp_parser_error (parser
, "expected iteration-statement");
10561 statement
= error_mark_node
;
10568 /* Parse a for-init-statement or the declarator of a range-based-for.
10569 Returns true if a range-based-for declaration is seen.
10571 for-init-statement:
10572 expression-statement
10573 simple-declaration */
10576 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10578 /* If the next token is a `;', then we have an empty
10579 expression-statement. Grammatically, this is also a
10580 simple-declaration, but an invalid one, because it does not
10581 declare anything. Therefore, if we did not handle this case
10582 specially, we would issue an error message about an invalid
10584 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10586 bool is_range_for
= false;
10587 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10589 parser
->colon_corrects_to_scope_p
= false;
10591 /* We're going to speculatively look for a declaration, falling back
10592 to an expression, if necessary. */
10593 cp_parser_parse_tentatively (parser
);
10594 /* Parse the declaration. */
10595 cp_parser_simple_declaration (parser
,
10596 /*function_definition_allowed_p=*/false,
10598 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10601 /* It is a range-for, consume the ':' */
10602 cp_lexer_consume_token (parser
->lexer
);
10603 is_range_for
= true;
10604 if (cxx_dialect
< cxx11
)
10606 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10607 "range-based %<for%> loops are not allowed "
10609 *decl
= error_mark_node
;
10613 /* The ';' is not consumed yet because we told
10614 cp_parser_simple_declaration not to. */
10615 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10617 if (cp_parser_parse_definitely (parser
))
10618 return is_range_for
;
10619 /* If the tentative parse failed, then we shall need to look for an
10620 expression-statement. */
10622 /* If we are here, it is an expression-statement. */
10623 cp_parser_expression_statement (parser
, NULL_TREE
);
10627 /* Parse a jump-statement.
10632 return expression [opt] ;
10633 return braced-init-list ;
10639 goto * expression ;
10641 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10644 cp_parser_jump_statement (cp_parser
* parser
)
10646 tree statement
= error_mark_node
;
10649 unsigned char in_statement
;
10651 /* Peek at the next token. */
10652 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10654 return error_mark_node
;
10656 /* See what kind of keyword it is. */
10657 keyword
= token
->keyword
;
10661 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
10662 switch (in_statement
)
10665 error_at (token
->location
, "break statement not within loop or switch");
10668 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
10669 || in_statement
== IN_ITERATION_STMT
);
10670 statement
= finish_break_stmt ();
10671 if (in_statement
== IN_ITERATION_STMT
)
10672 break_maybe_infinite_loop ();
10675 error_at (token
->location
, "invalid exit from OpenMP structured block");
10678 error_at (token
->location
, "break statement used with OpenMP for loop");
10680 case IN_CILK_SIMD_FOR
:
10681 error_at (token
->location
, "break statement used with Cilk Plus for loop");
10684 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10688 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10691 error_at (token
->location
, "continue statement not within a loop");
10693 case IN_CILK_SIMD_FOR
:
10694 error_at (token
->location
,
10695 "continue statement within %<#pragma simd%> loop body");
10696 /* Fall through. */
10697 case IN_ITERATION_STMT
:
10699 statement
= finish_continue_stmt ();
10702 error_at (token
->location
, "invalid exit from OpenMP structured block");
10705 gcc_unreachable ();
10707 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10713 bool expr_non_constant_p
;
10715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10717 cp_lexer_set_source_position (parser
->lexer
);
10718 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10719 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10721 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10722 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10724 /* If the next token is a `;', then there is no
10727 /* Build the return-statement. */
10728 statement
= finish_return_stmt (expr
);
10729 /* Look for the final `;'. */
10730 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10735 /* Create the goto-statement. */
10736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
10738 /* Issue a warning about this use of a GNU extension. */
10739 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
10740 /* Consume the '*' token. */
10741 cp_lexer_consume_token (parser
->lexer
);
10742 /* Parse the dependent expression. */
10743 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
10746 finish_goto_stmt (cp_parser_identifier (parser
));
10747 /* Look for the final `;'. */
10748 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10752 cp_parser_error (parser
, "expected jump-statement");
10759 /* Parse a declaration-statement.
10761 declaration-statement:
10762 block-declaration */
10765 cp_parser_declaration_statement (cp_parser
* parser
)
10769 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10770 p
= obstack_alloc (&declarator_obstack
, 0);
10772 /* Parse the block-declaration. */
10773 cp_parser_block_declaration (parser
, /*statement_p=*/true);
10775 /* Free any declarators allocated. */
10776 obstack_free (&declarator_obstack
, p
);
10779 /* Some dependent statements (like `if (cond) statement'), are
10780 implicitly in their own scope. In other words, if the statement is
10781 a single statement (as opposed to a compound-statement), it is
10782 none-the-less treated as if it were enclosed in braces. Any
10783 declarations appearing in the dependent statement are out of scope
10784 after control passes that point. This function parses a statement,
10785 but ensures that is in its own scope, even if it is not a
10786 compound-statement.
10788 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10789 is a (possibly labeled) if statement which is not enclosed in
10790 braces and has an else clause. This is used to implement
10793 Returns the new statement. */
10796 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
10803 /* Mark if () ; with a special NOP_EXPR. */
10804 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10806 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10807 cp_lexer_consume_token (parser
->lexer
);
10808 statement
= add_stmt (build_empty_stmt (loc
));
10810 /* if a compound is opened, we simply parse the statement directly. */
10811 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10812 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
10813 /* If the token is not a `{', then we must take special action. */
10816 /* Create a compound-statement. */
10817 statement
= begin_compound_stmt (0);
10818 /* Parse the dependent-statement. */
10819 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
10820 /* Finish the dummy compound-statement. */
10821 finish_compound_stmt (statement
);
10824 /* Return the statement. */
10828 /* For some dependent statements (like `while (cond) statement'), we
10829 have already created a scope. Therefore, even if the dependent
10830 statement is a compound-statement, we do not want to create another
10834 cp_parser_already_scoped_statement (cp_parser
* parser
)
10836 /* If the token is a `{', then we must take special action. */
10837 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10838 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
10841 /* Avoid calling cp_parser_compound_statement, so that we
10842 don't create a new scope. Do everything else by hand. */
10843 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
10844 /* If the next keyword is `__label__' we have a label declaration. */
10845 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10846 cp_parser_label_declaration (parser
);
10847 /* Parse an (optional) statement-seq. */
10848 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10849 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10853 /* Declarations [gram.dcl.dcl] */
10855 /* Parse an optional declaration-sequence.
10859 declaration-seq declaration */
10862 cp_parser_declaration_seq_opt (cp_parser
* parser
)
10868 token
= cp_lexer_peek_token (parser
->lexer
);
10870 if (token
->type
== CPP_CLOSE_BRACE
10871 || token
->type
== CPP_EOF
10872 || token
->type
== CPP_PRAGMA_EOL
)
10875 if (token
->type
== CPP_SEMICOLON
)
10877 /* A declaration consisting of a single semicolon is
10878 invalid. Allow it unless we're being pedantic. */
10879 cp_lexer_consume_token (parser
->lexer
);
10880 if (!in_system_header_at (input_location
))
10881 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
10885 /* If we're entering or exiting a region that's implicitly
10886 extern "C", modify the lang context appropriately. */
10887 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
10889 push_lang_context (lang_name_c
);
10890 parser
->implicit_extern_c
= true;
10892 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
10894 pop_lang_context ();
10895 parser
->implicit_extern_c
= false;
10898 if (token
->type
== CPP_PRAGMA
)
10900 /* A top-level declaration can consist solely of a #pragma.
10901 A nested declaration cannot, so this is done here and not
10902 in cp_parser_declaration. (A #pragma at block scope is
10903 handled in cp_parser_statement.) */
10904 cp_parser_pragma (parser
, pragma_external
);
10908 /* Parse the declaration itself. */
10909 cp_parser_declaration (parser
);
10913 /* Parse a declaration.
10917 function-definition
10918 template-declaration
10919 explicit-instantiation
10920 explicit-specialization
10921 linkage-specification
10922 namespace-definition
10927 __extension__ declaration */
10930 cp_parser_declaration (cp_parser
* parser
)
10934 int saved_pedantic
;
10936 tree attributes
= NULL_TREE
;
10938 /* Check for the `__extension__' keyword. */
10939 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10941 /* Parse the qualified declaration. */
10942 cp_parser_declaration (parser
);
10943 /* Restore the PEDANTIC flag. */
10944 pedantic
= saved_pedantic
;
10949 /* Try to figure out what kind of declaration is present. */
10950 token1
= *cp_lexer_peek_token (parser
->lexer
);
10952 if (token1
.type
!= CPP_EOF
)
10953 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
10956 token2
.type
= CPP_EOF
;
10957 token2
.keyword
= RID_MAX
;
10960 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10961 p
= obstack_alloc (&declarator_obstack
, 0);
10963 /* If the next token is `extern' and the following token is a string
10964 literal, then we have a linkage specification. */
10965 if (token1
.keyword
== RID_EXTERN
10966 && cp_parser_is_pure_string_literal (&token2
))
10967 cp_parser_linkage_specification (parser
);
10968 /* If the next token is `template', then we have either a template
10969 declaration, an explicit instantiation, or an explicit
10971 else if (token1
.keyword
== RID_TEMPLATE
)
10973 /* `template <>' indicates a template specialization. */
10974 if (token2
.type
== CPP_LESS
10975 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
10976 cp_parser_explicit_specialization (parser
);
10977 /* `template <' indicates a template declaration. */
10978 else if (token2
.type
== CPP_LESS
)
10979 cp_parser_template_declaration (parser
, /*member_p=*/false);
10980 /* Anything else must be an explicit instantiation. */
10982 cp_parser_explicit_instantiation (parser
);
10984 /* If the next token is `export', then we have a template
10986 else if (token1
.keyword
== RID_EXPORT
)
10987 cp_parser_template_declaration (parser
, /*member_p=*/false);
10988 /* If the next token is `extern', 'static' or 'inline' and the one
10989 after that is `template', we have a GNU extended explicit
10990 instantiation directive. */
10991 else if (cp_parser_allow_gnu_extensions_p (parser
)
10992 && (token1
.keyword
== RID_EXTERN
10993 || token1
.keyword
== RID_STATIC
10994 || token1
.keyword
== RID_INLINE
)
10995 && token2
.keyword
== RID_TEMPLATE
)
10996 cp_parser_explicit_instantiation (parser
);
10997 /* If the next token is `namespace', check for a named or unnamed
10998 namespace definition. */
10999 else if (token1
.keyword
== RID_NAMESPACE
11000 && (/* A named namespace definition. */
11001 (token2
.type
== CPP_NAME
11002 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11004 /* An unnamed namespace definition. */
11005 || token2
.type
== CPP_OPEN_BRACE
11006 || token2
.keyword
== RID_ATTRIBUTE
))
11007 cp_parser_namespace_definition (parser
);
11008 /* An inline (associated) namespace definition. */
11009 else if (token1
.keyword
== RID_INLINE
11010 && token2
.keyword
== RID_NAMESPACE
)
11011 cp_parser_namespace_definition (parser
);
11012 /* Objective-C++ declaration/definition. */
11013 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11014 cp_parser_objc_declaration (parser
, NULL_TREE
);
11015 else if (c_dialect_objc ()
11016 && token1
.keyword
== RID_ATTRIBUTE
11017 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11018 cp_parser_objc_declaration (parser
, attributes
);
11019 /* We must have either a block declaration or a function
11022 /* Try to parse a block-declaration, or a function-definition. */
11023 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11025 /* Free any declarators allocated. */
11026 obstack_free (&declarator_obstack
, p
);
11029 /* Parse a block-declaration.
11034 namespace-alias-definition
11041 __extension__ block-declaration
11046 static_assert-declaration
11048 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11049 part of a declaration-statement. */
11052 cp_parser_block_declaration (cp_parser
*parser
,
11056 int saved_pedantic
;
11058 /* Check for the `__extension__' keyword. */
11059 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11061 /* Parse the qualified declaration. */
11062 cp_parser_block_declaration (parser
, statement_p
);
11063 /* Restore the PEDANTIC flag. */
11064 pedantic
= saved_pedantic
;
11069 /* Peek at the next token to figure out which kind of declaration is
11071 token1
= cp_lexer_peek_token (parser
->lexer
);
11073 /* If the next keyword is `asm', we have an asm-definition. */
11074 if (token1
->keyword
== RID_ASM
)
11077 cp_parser_commit_to_tentative_parse (parser
);
11078 cp_parser_asm_definition (parser
);
11080 /* If the next keyword is `namespace', we have a
11081 namespace-alias-definition. */
11082 else if (token1
->keyword
== RID_NAMESPACE
)
11083 cp_parser_namespace_alias_definition (parser
);
11084 /* If the next keyword is `using', we have a
11085 using-declaration, a using-directive, or an alias-declaration. */
11086 else if (token1
->keyword
== RID_USING
)
11091 cp_parser_commit_to_tentative_parse (parser
);
11092 /* If the token after `using' is `namespace', then we have a
11093 using-directive. */
11094 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11095 if (token2
->keyword
== RID_NAMESPACE
)
11096 cp_parser_using_directive (parser
);
11097 /* If the second token after 'using' is '=', then we have an
11098 alias-declaration. */
11099 else if (cxx_dialect
>= cxx11
11100 && token2
->type
== CPP_NAME
11101 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11102 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11103 cp_parser_alias_declaration (parser
);
11104 /* Otherwise, it's a using-declaration. */
11106 cp_parser_using_declaration (parser
,
11107 /*access_declaration_p=*/false);
11109 /* If the next keyword is `__label__' we have a misplaced label
11111 else if (token1
->keyword
== RID_LABEL
)
11113 cp_lexer_consume_token (parser
->lexer
);
11114 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11115 cp_parser_skip_to_end_of_statement (parser
);
11116 /* If the next token is now a `;', consume it. */
11117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11118 cp_lexer_consume_token (parser
->lexer
);
11120 /* If the next token is `static_assert' we have a static assertion. */
11121 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11122 cp_parser_static_assert (parser
, /*member_p=*/false);
11123 /* Anything else must be a simple-declaration. */
11125 cp_parser_simple_declaration (parser
, !statement_p
,
11126 /*maybe_range_for_decl*/NULL
);
11129 /* Parse a simple-declaration.
11131 simple-declaration:
11132 decl-specifier-seq [opt] init-declarator-list [opt] ;
11134 init-declarator-list:
11136 init-declarator-list , init-declarator
11138 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11139 function-definition as a simple-declaration.
11141 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11142 parsed declaration if it is an uninitialized single declarator not followed
11143 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11144 if present, will not be consumed. */
11147 cp_parser_simple_declaration (cp_parser
* parser
,
11148 bool function_definition_allowed_p
,
11149 tree
*maybe_range_for_decl
)
11151 cp_decl_specifier_seq decl_specifiers
;
11152 int declares_class_or_enum
;
11153 bool saw_declarator
;
11155 if (maybe_range_for_decl
)
11156 *maybe_range_for_decl
= NULL_TREE
;
11158 /* Defer access checks until we know what is being declared; the
11159 checks for names appearing in the decl-specifier-seq should be
11160 done as if we were in the scope of the thing being declared. */
11161 push_deferring_access_checks (dk_deferred
);
11163 /* Parse the decl-specifier-seq. We have to keep track of whether
11164 or not the decl-specifier-seq declares a named class or
11165 enumeration type, since that is the only case in which the
11166 init-declarator-list is allowed to be empty.
11170 In a simple-declaration, the optional init-declarator-list can be
11171 omitted only when declaring a class or enumeration, that is when
11172 the decl-specifier-seq contains either a class-specifier, an
11173 elaborated-type-specifier, or an enum-specifier. */
11174 cp_parser_decl_specifier_seq (parser
,
11175 CP_PARSER_FLAGS_OPTIONAL
,
11177 &declares_class_or_enum
);
11178 /* We no longer need to defer access checks. */
11179 stop_deferring_access_checks ();
11181 /* In a block scope, a valid declaration must always have a
11182 decl-specifier-seq. By not trying to parse declarators, we can
11183 resolve the declaration/expression ambiguity more quickly. */
11184 if (!function_definition_allowed_p
11185 && !decl_specifiers
.any_specifiers_p
)
11187 cp_parser_error (parser
, "expected declaration");
11191 /* If the next two tokens are both identifiers, the code is
11192 erroneous. The usual cause of this situation is code like:
11196 where "T" should name a type -- but does not. */
11197 if (!decl_specifiers
.any_type_specifiers_p
11198 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11200 /* If parsing tentatively, we should commit; we really are
11201 looking at a declaration. */
11202 cp_parser_commit_to_tentative_parse (parser
);
11207 /* If we have seen at least one decl-specifier, and the next token
11208 is not a parenthesis, then we must be looking at a declaration.
11209 (After "int (" we might be looking at a functional cast.) */
11210 if (decl_specifiers
.any_specifiers_p
11211 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11212 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11213 && !cp_parser_error_occurred (parser
))
11214 cp_parser_commit_to_tentative_parse (parser
);
11216 /* Keep going until we hit the `;' at the end of the simple
11218 saw_declarator
= false;
11219 while (cp_lexer_next_token_is_not (parser
->lexer
,
11223 bool function_definition_p
;
11226 if (saw_declarator
)
11228 /* If we are processing next declarator, coma is expected */
11229 token
= cp_lexer_peek_token (parser
->lexer
);
11230 gcc_assert (token
->type
== CPP_COMMA
);
11231 cp_lexer_consume_token (parser
->lexer
);
11232 if (maybe_range_for_decl
)
11233 *maybe_range_for_decl
= error_mark_node
;
11236 saw_declarator
= true;
11238 /* Parse the init-declarator. */
11239 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11241 function_definition_allowed_p
,
11242 /*member_p=*/false,
11243 declares_class_or_enum
,
11244 &function_definition_p
,
11245 maybe_range_for_decl
);
11246 /* If an error occurred while parsing tentatively, exit quickly.
11247 (That usually happens when in the body of a function; each
11248 statement is treated as a declaration-statement until proven
11250 if (cp_parser_error_occurred (parser
))
11252 /* Handle function definitions specially. */
11253 if (function_definition_p
)
11255 /* If the next token is a `,', then we are probably
11256 processing something like:
11260 which is erroneous. */
11261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11263 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11264 error_at (token
->location
,
11266 " declarations and function-definitions is forbidden");
11268 /* Otherwise, we're done with the list of declarators. */
11271 pop_deferring_access_checks ();
11275 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11276 *maybe_range_for_decl
= decl
;
11277 /* The next token should be either a `,' or a `;'. */
11278 token
= cp_lexer_peek_token (parser
->lexer
);
11279 /* If it's a `,', there are more declarators to come. */
11280 if (token
->type
== CPP_COMMA
)
11281 /* will be consumed next time around */;
11282 /* If it's a `;', we are done. */
11283 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11285 /* Anything else is an error. */
11288 /* If we have already issued an error message we don't need
11289 to issue another one. */
11290 if (decl
!= error_mark_node
11291 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11292 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11293 /* Skip tokens until we reach the end of the statement. */
11294 cp_parser_skip_to_end_of_statement (parser
);
11295 /* If the next token is now a `;', consume it. */
11296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11297 cp_lexer_consume_token (parser
->lexer
);
11300 /* After the first time around, a function-definition is not
11301 allowed -- even if it was OK at first. For example:
11306 function_definition_allowed_p
= false;
11309 /* Issue an error message if no declarators are present, and the
11310 decl-specifier-seq does not itself declare a class or
11311 enumeration: [dcl.dcl]/3. */
11312 if (!saw_declarator
)
11314 if (cp_parser_declares_only_class_p (parser
))
11316 if (!declares_class_or_enum
11317 && decl_specifiers
.type
11318 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11319 /* Ensure an error is issued anyway when finish_decltype_type,
11320 called via cp_parser_decl_specifier_seq, returns a class or
11321 an enumeration (c++/51786). */
11322 decl_specifiers
.type
= NULL_TREE
;
11323 shadow_tag (&decl_specifiers
);
11325 /* Perform any deferred access checks. */
11326 perform_deferred_access_checks (tf_warning_or_error
);
11329 /* Consume the `;'. */
11330 if (!maybe_range_for_decl
)
11331 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11334 pop_deferring_access_checks ();
11337 /* Parse a decl-specifier-seq.
11339 decl-specifier-seq:
11340 decl-specifier-seq [opt] decl-specifier
11341 decl-specifier attribute-specifier-seq [opt] (C++11)
11344 storage-class-specifier
11355 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11357 The parser flags FLAGS is used to control type-specifier parsing.
11359 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11362 1: one of the decl-specifiers is an elaborated-type-specifier
11363 (i.e., a type declaration)
11364 2: one of the decl-specifiers is an enum-specifier or a
11365 class-specifier (i.e., a type definition)
11370 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11371 cp_parser_flags flags
,
11372 cp_decl_specifier_seq
*decl_specs
,
11373 int* declares_class_or_enum
)
11375 bool constructor_possible_p
= !parser
->in_declarator_p
;
11376 bool found_decl_spec
= false;
11377 cp_token
*start_token
= NULL
;
11380 /* Clear DECL_SPECS. */
11381 clear_decl_specs (decl_specs
);
11383 /* Assume no class or enumeration type is declared. */
11384 *declares_class_or_enum
= 0;
11386 /* Keep reading specifiers until there are no more to read. */
11389 bool constructor_p
;
11393 /* Peek at the next token. */
11394 token
= cp_lexer_peek_token (parser
->lexer
);
11396 /* Save the first token of the decl spec list for error
11399 start_token
= token
;
11400 /* Handle attributes. */
11401 if (cp_next_tokens_can_be_attribute_p (parser
))
11403 /* Parse the attributes. */
11404 tree attrs
= cp_parser_attributes_opt (parser
);
11406 /* In a sequence of declaration specifiers, c++11 attributes
11407 appertain to the type that precede them. In that case
11410 The attribute-specifier-seq affects the type only for
11411 the declaration it appears in, not other declarations
11412 involving the same type.
11414 But for now let's force the user to position the
11415 attribute either at the beginning of the declaration or
11416 after the declarator-id, which would clearly mean that it
11417 applies to the declarator. */
11418 if (cxx11_attribute_p (attrs
))
11420 if (!found_decl_spec
)
11421 /* The c++11 attribute is at the beginning of the
11422 declaration. It appertains to the entity being
11426 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11428 /* This is an attribute following a
11429 class-specifier. */
11430 if (decl_specs
->type_definition_p
)
11431 warn_misplaced_attr_for_class_type (token
->location
,
11437 decl_specs
->std_attributes
11438 = chainon (decl_specs
->std_attributes
,
11440 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11441 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11447 decl_specs
->attributes
11448 = chainon (decl_specs
->attributes
,
11450 if (decl_specs
->locations
[ds_attribute
] == 0)
11451 decl_specs
->locations
[ds_attribute
] = token
->location
;
11454 /* Assume we will find a decl-specifier keyword. */
11455 found_decl_spec
= true;
11456 /* If the next token is an appropriate keyword, we can simply
11457 add it to the list. */
11458 switch (token
->keyword
)
11464 if (!at_class_scope_p ())
11466 error_at (token
->location
, "%<friend%> used outside of class");
11467 cp_lexer_purge_token (parser
->lexer
);
11472 /* Consume the token. */
11473 cp_lexer_consume_token (parser
->lexer
);
11477 case RID_CONSTEXPR
:
11479 cp_lexer_consume_token (parser
->lexer
);
11482 /* function-specifier:
11489 cp_parser_function_specifier_opt (parser
, decl_specs
);
11496 /* Consume the token. */
11497 cp_lexer_consume_token (parser
->lexer
);
11498 /* A constructor declarator cannot appear in a typedef. */
11499 constructor_possible_p
= false;
11500 /* The "typedef" keyword can only occur in a declaration; we
11501 may as well commit at this point. */
11502 cp_parser_commit_to_tentative_parse (parser
);
11504 if (decl_specs
->storage_class
!= sc_none
)
11505 decl_specs
->conflicting_specifiers_p
= true;
11508 /* storage-class-specifier:
11518 if (cxx_dialect
== cxx98
)
11520 /* Consume the token. */
11521 cp_lexer_consume_token (parser
->lexer
);
11523 /* Complain about `auto' as a storage specifier, if
11524 we're complaining about C++0x compatibility. */
11525 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11526 " changes meaning in C++11; please remove it");
11528 /* Set the storage class anyway. */
11529 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11533 /* C++0x auto type-specifier. */
11534 found_decl_spec
= false;
11541 /* Consume the token. */
11542 cp_lexer_consume_token (parser
->lexer
);
11543 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11547 /* Consume the token. */
11549 cp_lexer_consume_token (parser
->lexer
);
11553 /* We did not yet find a decl-specifier yet. */
11554 found_decl_spec
= false;
11558 if (found_decl_spec
11559 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11560 && token
->keyword
!= RID_CONSTEXPR
)
11561 error ("decl-specifier invalid in condition");
11564 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11566 /* Constructors are a special case. The `S' in `S()' is not a
11567 decl-specifier; it is the beginning of the declarator. */
11569 = (!found_decl_spec
11570 && constructor_possible_p
11571 && (cp_parser_constructor_declarator_p
11572 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11574 /* If we don't have a DECL_SPEC yet, then we must be looking at
11575 a type-specifier. */
11576 if (!found_decl_spec
&& !constructor_p
)
11578 int decl_spec_declares_class_or_enum
;
11579 bool is_cv_qualifier
;
11583 = cp_parser_type_specifier (parser
, flags
,
11585 /*is_declaration=*/true,
11586 &decl_spec_declares_class_or_enum
,
11588 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11590 /* If this type-specifier referenced a user-defined type
11591 (a typedef, class-name, etc.), then we can't allow any
11592 more such type-specifiers henceforth.
11596 The longest sequence of decl-specifiers that could
11597 possibly be a type name is taken as the
11598 decl-specifier-seq of a declaration. The sequence shall
11599 be self-consistent as described below.
11603 As a general rule, at most one type-specifier is allowed
11604 in the complete decl-specifier-seq of a declaration. The
11605 only exceptions are the following:
11607 -- const or volatile can be combined with any other
11610 -- signed or unsigned can be combined with char, long,
11618 void g (const int Pc);
11620 Here, Pc is *not* part of the decl-specifier seq; it's
11621 the declarator. Therefore, once we see a type-specifier
11622 (other than a cv-qualifier), we forbid any additional
11623 user-defined types. We *do* still allow things like `int
11624 int' to be considered a decl-specifier-seq, and issue the
11625 error message later. */
11626 if (type_spec
&& !is_cv_qualifier
)
11627 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11628 /* A constructor declarator cannot follow a type-specifier. */
11631 constructor_possible_p
= false;
11632 found_decl_spec
= true;
11633 if (!is_cv_qualifier
)
11634 decl_specs
->any_type_specifiers_p
= true;
11638 /* If we still do not have a DECL_SPEC, then there are no more
11639 decl-specifiers. */
11640 if (!found_decl_spec
)
11643 decl_specs
->any_specifiers_p
= true;
11644 /* After we see one decl-specifier, further decl-specifiers are
11645 always optional. */
11646 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
11649 /* Don't allow a friend specifier with a class definition. */
11650 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
11651 && (*declares_class_or_enum
& 2))
11652 error_at (decl_specs
->locations
[ds_friend
],
11653 "class definition may not be declared a friend");
11656 /* Parse an (optional) storage-class-specifier.
11658 storage-class-specifier:
11667 storage-class-specifier:
11670 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11673 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
11675 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
11678 if (cxx_dialect
!= cxx98
)
11680 /* Fall through for C++98. */
11687 /* Consume the token. */
11688 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11695 /* Parse an (optional) function-specifier.
11697 function-specifier:
11702 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11703 Updates DECL_SPECS, if it is non-NULL. */
11706 cp_parser_function_specifier_opt (cp_parser
* parser
,
11707 cp_decl_specifier_seq
*decl_specs
)
11709 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11710 switch (token
->keyword
)
11713 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
11717 /* 14.5.2.3 [temp.mem]
11719 A member function template shall not be virtual. */
11720 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11721 error_at (token
->location
, "templates may not be %<virtual%>");
11723 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
11727 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
11734 /* Consume the token. */
11735 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11738 /* Parse a linkage-specification.
11740 linkage-specification:
11741 extern string-literal { declaration-seq [opt] }
11742 extern string-literal declaration */
11745 cp_parser_linkage_specification (cp_parser
* parser
)
11749 /* Look for the `extern' keyword. */
11750 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
11752 /* Look for the string-literal. */
11753 linkage
= cp_parser_string_literal (parser
, false, false);
11755 /* Transform the literal into an identifier. If the literal is a
11756 wide-character string, or contains embedded NULs, then we can't
11757 handle it as the user wants. */
11758 if (strlen (TREE_STRING_POINTER (linkage
))
11759 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
11761 cp_parser_error (parser
, "invalid linkage-specification");
11762 /* Assume C++ linkage. */
11763 linkage
= lang_name_cplusplus
;
11766 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
11768 /* We're now using the new linkage. */
11769 push_lang_context (linkage
);
11771 /* If the next token is a `{', then we're using the first
11773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11775 cp_ensure_no_omp_declare_simd (parser
);
11777 /* Consume the `{' token. */
11778 cp_lexer_consume_token (parser
->lexer
);
11779 /* Parse the declarations. */
11780 cp_parser_declaration_seq_opt (parser
);
11781 /* Look for the closing `}'. */
11782 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11784 /* Otherwise, there's just one declaration. */
11787 bool saved_in_unbraced_linkage_specification_p
;
11789 saved_in_unbraced_linkage_specification_p
11790 = parser
->in_unbraced_linkage_specification_p
;
11791 parser
->in_unbraced_linkage_specification_p
= true;
11792 cp_parser_declaration (parser
);
11793 parser
->in_unbraced_linkage_specification_p
11794 = saved_in_unbraced_linkage_specification_p
;
11797 /* We're done with the linkage-specification. */
11798 pop_lang_context ();
11801 /* Parse a static_assert-declaration.
11803 static_assert-declaration:
11804 static_assert ( constant-expression , string-literal ) ;
11806 If MEMBER_P, this static_assert is a class member. */
11809 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
11814 location_t saved_loc
;
11817 /* Peek at the `static_assert' token so we can keep track of exactly
11818 where the static assertion started. */
11819 token
= cp_lexer_peek_token (parser
->lexer
);
11820 saved_loc
= token
->location
;
11822 /* Look for the `static_assert' keyword. */
11823 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
11827 /* We know we are in a static assertion; commit to any tentative
11829 if (cp_parser_parsing_tentatively (parser
))
11830 cp_parser_commit_to_tentative_parse (parser
);
11832 /* Parse the `(' starting the static assertion condition. */
11833 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11835 /* Parse the constant-expression. Allow a non-constant expression
11836 here in order to give better diagnostics in finish_static_assert. */
11838 cp_parser_constant_expression (parser
,
11839 /*allow_non_constant_p=*/true,
11840 /*non_constant_p=*/&dummy
);
11842 /* Parse the separating `,'. */
11843 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11845 /* Parse the string-literal message. */
11846 message
= cp_parser_string_literal (parser
,
11847 /*translate=*/false,
11850 /* A `)' completes the static assertion. */
11851 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11852 cp_parser_skip_to_closing_parenthesis (parser
,
11853 /*recovering=*/true,
11854 /*or_comma=*/false,
11855 /*consume_paren=*/true);
11857 /* A semicolon terminates the declaration. */
11858 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11860 /* Complete the static assertion, which may mean either processing
11861 the static assert now or saving it for template instantiation. */
11862 finish_static_assert (condition
, message
, saved_loc
, member_p
);
11865 /* Parse the expression in decltype ( expression ). */
11868 cp_parser_decltype_expr (cp_parser
*parser
,
11869 bool &id_expression_or_member_access_p
)
11871 cp_token
*id_expr_start_token
;
11874 /* First, try parsing an id-expression. */
11875 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
11876 cp_parser_parse_tentatively (parser
);
11877 expr
= cp_parser_id_expression (parser
,
11878 /*template_keyword_p=*/false,
11879 /*check_dependency_p=*/true,
11880 /*template_p=*/NULL
,
11881 /*declarator_p=*/false,
11882 /*optional_p=*/false);
11884 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
11886 bool non_integral_constant_expression_p
= false;
11887 tree id_expression
= expr
;
11889 const char *error_msg
;
11891 if (identifier_p (expr
))
11892 /* Lookup the name we got back from the id-expression. */
11893 expr
= cp_parser_lookup_name_simple (parser
, expr
,
11894 id_expr_start_token
->location
);
11897 && expr
!= error_mark_node
11898 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
11899 && TREE_CODE (expr
) != TYPE_DECL
11900 && (TREE_CODE (expr
) != BIT_NOT_EXPR
11901 || !TYPE_P (TREE_OPERAND (expr
, 0)))
11902 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11904 /* Complete lookup of the id-expression. */
11905 expr
= (finish_id_expression
11906 (id_expression
, expr
, parser
->scope
, &idk
,
11907 /*integral_constant_expression_p=*/false,
11908 /*allow_non_integral_constant_expression_p=*/true,
11909 &non_integral_constant_expression_p
,
11910 /*template_p=*/false,
11912 /*address_p=*/false,
11913 /*template_arg_p=*/false,
11915 id_expr_start_token
->location
));
11917 if (expr
== error_mark_node
)
11918 /* We found an id-expression, but it was something that we
11919 should not have found. This is an error, not something
11920 we can recover from, so note that we found an
11921 id-expression and we'll recover as gracefully as
11923 id_expression_or_member_access_p
= true;
11927 && expr
!= error_mark_node
11928 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11929 /* We have an id-expression. */
11930 id_expression_or_member_access_p
= true;
11933 if (!id_expression_or_member_access_p
)
11935 /* Abort the id-expression parse. */
11936 cp_parser_abort_tentative_parse (parser
);
11938 /* Parsing tentatively, again. */
11939 cp_parser_parse_tentatively (parser
);
11941 /* Parse a class member access. */
11942 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
11943 /*cast_p=*/false, /*decltype*/true,
11944 /*member_access_only_p=*/true, NULL
);
11947 && expr
!= error_mark_node
11948 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11949 /* We have an id-expression. */
11950 id_expression_or_member_access_p
= true;
11953 if (id_expression_or_member_access_p
)
11954 /* We have parsed the complete id-expression or member access. */
11955 cp_parser_parse_definitely (parser
);
11958 /* Abort our attempt to parse an id-expression or member access
11960 cp_parser_abort_tentative_parse (parser
);
11962 /* Parse a full expression. */
11963 expr
= cp_parser_expression (parser
, /*cast_p=*/false,
11964 /*decltype*/true, NULL
);
11970 /* Parse a `decltype' type. Returns the type.
11972 simple-type-specifier:
11973 decltype ( expression )
11975 decltype ( auto ) */
11978 cp_parser_decltype (cp_parser
*parser
)
11981 bool id_expression_or_member_access_p
= false;
11982 const char *saved_message
;
11983 bool saved_integral_constant_expression_p
;
11984 bool saved_non_integral_constant_expression_p
;
11985 bool saved_greater_than_is_operator_p
;
11986 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11988 if (start_token
->type
== CPP_DECLTYPE
)
11990 /* Already parsed. */
11991 cp_lexer_consume_token (parser
->lexer
);
11992 return start_token
->u
.value
;
11995 /* Look for the `decltype' token. */
11996 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
11997 return error_mark_node
;
11999 /* Parse the opening `('. */
12000 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12001 return error_mark_node
;
12003 /* decltype (auto) */
12004 if (cxx_dialect
>= cxx1y
12005 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12007 cp_lexer_consume_token (parser
->lexer
);
12008 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12009 return error_mark_node
;
12010 expr
= make_decltype_auto ();
12011 AUTO_IS_DECLTYPE (expr
) = true;
12015 /* Types cannot be defined in a `decltype' expression. Save away the
12017 saved_message
= parser
->type_definition_forbidden_message
;
12019 /* And create the new one. */
12020 parser
->type_definition_forbidden_message
12021 = G_("types may not be defined in %<decltype%> expressions");
12023 /* The restrictions on constant-expressions do not apply inside
12024 decltype expressions. */
12025 saved_integral_constant_expression_p
12026 = parser
->integral_constant_expression_p
;
12027 saved_non_integral_constant_expression_p
12028 = parser
->non_integral_constant_expression_p
;
12029 parser
->integral_constant_expression_p
= false;
12031 /* Within a parenthesized expression, a `>' token is always
12032 the greater-than operator. */
12033 saved_greater_than_is_operator_p
12034 = parser
->greater_than_is_operator_p
;
12035 parser
->greater_than_is_operator_p
= true;
12037 /* Do not actually evaluate the expression. */
12038 ++cp_unevaluated_operand
;
12040 /* Do not warn about problems with the expression. */
12041 ++c_inhibit_evaluation_warnings
;
12043 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12045 /* Go back to evaluating expressions. */
12046 --cp_unevaluated_operand
;
12047 --c_inhibit_evaluation_warnings
;
12049 /* The `>' token might be the end of a template-id or
12050 template-parameter-list now. */
12051 parser
->greater_than_is_operator_p
12052 = saved_greater_than_is_operator_p
;
12054 /* Restore the old message and the integral constant expression
12056 parser
->type_definition_forbidden_message
= saved_message
;
12057 parser
->integral_constant_expression_p
12058 = saved_integral_constant_expression_p
;
12059 parser
->non_integral_constant_expression_p
12060 = saved_non_integral_constant_expression_p
;
12062 /* Parse to the closing `)'. */
12063 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12065 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12066 /*consume_paren=*/true);
12067 return error_mark_node
;
12070 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12071 tf_warning_or_error
);
12074 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12076 start_token
->type
= CPP_DECLTYPE
;
12077 start_token
->u
.value
= expr
;
12078 start_token
->keyword
= RID_MAX
;
12079 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12084 /* Special member functions [gram.special] */
12086 /* Parse a conversion-function-id.
12088 conversion-function-id:
12089 operator conversion-type-id
12091 Returns an IDENTIFIER_NODE representing the operator. */
12094 cp_parser_conversion_function_id (cp_parser
* parser
)
12098 tree saved_qualifying_scope
;
12099 tree saved_object_scope
;
12100 tree pushed_scope
= NULL_TREE
;
12102 /* Look for the `operator' token. */
12103 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12104 return error_mark_node
;
12105 /* When we parse the conversion-type-id, the current scope will be
12106 reset. However, we need that information in able to look up the
12107 conversion function later, so we save it here. */
12108 saved_scope
= parser
->scope
;
12109 saved_qualifying_scope
= parser
->qualifying_scope
;
12110 saved_object_scope
= parser
->object_scope
;
12111 /* We must enter the scope of the class so that the names of
12112 entities declared within the class are available in the
12113 conversion-type-id. For example, consider:
12120 S::operator I() { ... }
12122 In order to see that `I' is a type-name in the definition, we
12123 must be in the scope of `S'. */
12125 pushed_scope
= push_scope (saved_scope
);
12126 /* Parse the conversion-type-id. */
12127 type
= cp_parser_conversion_type_id (parser
);
12128 /* Leave the scope of the class, if any. */
12130 pop_scope (pushed_scope
);
12131 /* Restore the saved scope. */
12132 parser
->scope
= saved_scope
;
12133 parser
->qualifying_scope
= saved_qualifying_scope
;
12134 parser
->object_scope
= saved_object_scope
;
12135 /* If the TYPE is invalid, indicate failure. */
12136 if (type
== error_mark_node
)
12137 return error_mark_node
;
12138 return mangle_conv_op_name_for_type (type
);
12141 /* Parse a conversion-type-id:
12143 conversion-type-id:
12144 type-specifier-seq conversion-declarator [opt]
12146 Returns the TYPE specified. */
12149 cp_parser_conversion_type_id (cp_parser
* parser
)
12152 cp_decl_specifier_seq type_specifiers
;
12153 cp_declarator
*declarator
;
12154 tree type_specified
;
12155 const char *saved_message
;
12157 /* Parse the attributes. */
12158 attributes
= cp_parser_attributes_opt (parser
);
12160 saved_message
= parser
->type_definition_forbidden_message
;
12161 parser
->type_definition_forbidden_message
12162 = G_("types may not be defined in a conversion-type-id");
12164 /* Parse the type-specifiers. */
12165 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12166 /*is_trailing_return=*/false,
12169 parser
->type_definition_forbidden_message
= saved_message
;
12171 /* If that didn't work, stop. */
12172 if (type_specifiers
.type
== error_mark_node
)
12173 return error_mark_node
;
12174 /* Parse the conversion-declarator. */
12175 declarator
= cp_parser_conversion_declarator_opt (parser
);
12177 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12178 /*initialized=*/0, &attributes
);
12180 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12182 /* Don't give this error when parsing tentatively. This happens to
12183 work because we always parse this definitively once. */
12184 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12185 && type_uses_auto (type_specified
))
12187 if (cxx_dialect
< cxx1y
)
12189 error ("invalid use of %<auto%> in conversion operator");
12190 return error_mark_node
;
12192 else if (template_parm_scope_p ())
12193 warning (0, "use of %<auto%> in member template "
12194 "conversion operator can never be deduced");
12197 return type_specified
;
12200 /* Parse an (optional) conversion-declarator.
12202 conversion-declarator:
12203 ptr-operator conversion-declarator [opt]
12207 static cp_declarator
*
12208 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12210 enum tree_code code
;
12211 tree class_type
, std_attributes
= NULL_TREE
;
12212 cp_cv_quals cv_quals
;
12214 /* We don't know if there's a ptr-operator next, or not. */
12215 cp_parser_parse_tentatively (parser
);
12216 /* Try the ptr-operator. */
12217 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12219 /* If it worked, look for more conversion-declarators. */
12220 if (cp_parser_parse_definitely (parser
))
12222 cp_declarator
*declarator
;
12224 /* Parse another optional declarator. */
12225 declarator
= cp_parser_conversion_declarator_opt (parser
);
12227 declarator
= cp_parser_make_indirect_declarator
12228 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12236 /* Parse an (optional) ctor-initializer.
12239 : mem-initializer-list
12241 Returns TRUE iff the ctor-initializer was actually present. */
12244 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12246 /* If the next token is not a `:', then there is no
12247 ctor-initializer. */
12248 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12250 /* Do default initialization of any bases and members. */
12251 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12252 finish_mem_initializers (NULL_TREE
);
12257 /* Consume the `:' token. */
12258 cp_lexer_consume_token (parser
->lexer
);
12259 /* And the mem-initializer-list. */
12260 cp_parser_mem_initializer_list (parser
);
12265 /* Parse a mem-initializer-list.
12267 mem-initializer-list:
12268 mem-initializer ... [opt]
12269 mem-initializer ... [opt] , mem-initializer-list */
12272 cp_parser_mem_initializer_list (cp_parser
* parser
)
12274 tree mem_initializer_list
= NULL_TREE
;
12275 tree target_ctor
= error_mark_node
;
12276 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12278 /* Let the semantic analysis code know that we are starting the
12279 mem-initializer-list. */
12280 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12281 error_at (token
->location
,
12282 "only constructors take member initializers");
12284 /* Loop through the list. */
12287 tree mem_initializer
;
12289 token
= cp_lexer_peek_token (parser
->lexer
);
12290 /* Parse the mem-initializer. */
12291 mem_initializer
= cp_parser_mem_initializer (parser
);
12292 /* If the next token is a `...', we're expanding member initializers. */
12293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12295 /* Consume the `...'. */
12296 cp_lexer_consume_token (parser
->lexer
);
12298 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12299 can be expanded but members cannot. */
12300 if (mem_initializer
!= error_mark_node
12301 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12303 error_at (token
->location
,
12304 "cannot expand initializer for member %<%D%>",
12305 TREE_PURPOSE (mem_initializer
));
12306 mem_initializer
= error_mark_node
;
12309 /* Construct the pack expansion type. */
12310 if (mem_initializer
!= error_mark_node
)
12311 mem_initializer
= make_pack_expansion (mem_initializer
);
12313 if (target_ctor
!= error_mark_node
12314 && mem_initializer
!= error_mark_node
)
12316 error ("mem-initializer for %qD follows constructor delegation",
12317 TREE_PURPOSE (mem_initializer
));
12318 mem_initializer
= error_mark_node
;
12320 /* Look for a target constructor. */
12321 if (mem_initializer
!= error_mark_node
12322 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12323 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12325 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12326 if (mem_initializer_list
)
12328 error ("constructor delegation follows mem-initializer for %qD",
12329 TREE_PURPOSE (mem_initializer_list
));
12330 mem_initializer
= error_mark_node
;
12332 target_ctor
= mem_initializer
;
12334 /* Add it to the list, unless it was erroneous. */
12335 if (mem_initializer
!= error_mark_node
)
12337 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12338 mem_initializer_list
= mem_initializer
;
12340 /* If the next token is not a `,', we're done. */
12341 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12343 /* Consume the `,' token. */
12344 cp_lexer_consume_token (parser
->lexer
);
12347 /* Perform semantic analysis. */
12348 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12349 finish_mem_initializers (mem_initializer_list
);
12352 /* Parse a mem-initializer.
12355 mem-initializer-id ( expression-list [opt] )
12356 mem-initializer-id braced-init-list
12361 ( expression-list [opt] )
12363 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12364 class) or FIELD_DECL (for a non-static data member) to initialize;
12365 the TREE_VALUE is the expression-list. An empty initialization
12366 list is represented by void_list_node. */
12369 cp_parser_mem_initializer (cp_parser
* parser
)
12371 tree mem_initializer_id
;
12372 tree expression_list
;
12374 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12376 /* Find out what is being initialized. */
12377 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12379 permerror (token
->location
,
12380 "anachronistic old-style base class initializer");
12381 mem_initializer_id
= NULL_TREE
;
12385 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12386 if (mem_initializer_id
== error_mark_node
)
12387 return mem_initializer_id
;
12389 member
= expand_member_init (mem_initializer_id
);
12390 if (member
&& !DECL_P (member
))
12391 in_base_initializer
= 1;
12393 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12395 bool expr_non_constant_p
;
12396 cp_lexer_set_source_position (parser
->lexer
);
12397 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12398 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12399 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12400 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12404 vec
<tree
, va_gc
> *vec
;
12405 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12407 /*allow_expansion_p=*/true,
12408 /*non_constant_p=*/NULL
);
12410 return error_mark_node
;
12411 expression_list
= build_tree_list_vec (vec
);
12412 release_tree_vector (vec
);
12415 if (expression_list
== error_mark_node
)
12416 return error_mark_node
;
12417 if (!expression_list
)
12418 expression_list
= void_type_node
;
12420 in_base_initializer
= 0;
12422 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12425 /* Parse a mem-initializer-id.
12427 mem-initializer-id:
12428 :: [opt] nested-name-specifier [opt] class-name
12431 Returns a TYPE indicating the class to be initializer for the first
12432 production. Returns an IDENTIFIER_NODE indicating the data member
12433 to be initialized for the second production. */
12436 cp_parser_mem_initializer_id (cp_parser
* parser
)
12438 bool global_scope_p
;
12439 bool nested_name_specifier_p
;
12440 bool template_p
= false;
12443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12445 /* `typename' is not allowed in this context ([temp.res]). */
12446 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12448 error_at (token
->location
,
12449 "keyword %<typename%> not allowed in this context (a qualified "
12450 "member initializer is implicitly a type)");
12451 cp_lexer_consume_token (parser
->lexer
);
12453 /* Look for the optional `::' operator. */
12455 = (cp_parser_global_scope_opt (parser
,
12456 /*current_scope_valid_p=*/false)
12458 /* Look for the optional nested-name-specifier. The simplest way to
12463 The keyword `typename' is not permitted in a base-specifier or
12464 mem-initializer; in these contexts a qualified name that
12465 depends on a template-parameter is implicitly assumed to be a
12468 is to assume that we have seen the `typename' keyword at this
12470 nested_name_specifier_p
12471 = (cp_parser_nested_name_specifier_opt (parser
,
12472 /*typename_keyword_p=*/true,
12473 /*check_dependency_p=*/true,
12475 /*is_declaration=*/true)
12477 if (nested_name_specifier_p
)
12478 template_p
= cp_parser_optional_template_keyword (parser
);
12479 /* If there is a `::' operator or a nested-name-specifier, then we
12480 are definitely looking for a class-name. */
12481 if (global_scope_p
|| nested_name_specifier_p
)
12482 return cp_parser_class_name (parser
,
12483 /*typename_keyword_p=*/true,
12484 /*template_keyword_p=*/template_p
,
12486 /*check_dependency_p=*/true,
12487 /*class_head_p=*/false,
12488 /*is_declaration=*/true);
12489 /* Otherwise, we could also be looking for an ordinary identifier. */
12490 cp_parser_parse_tentatively (parser
);
12491 /* Try a class-name. */
12492 id
= cp_parser_class_name (parser
,
12493 /*typename_keyword_p=*/true,
12494 /*template_keyword_p=*/false,
12496 /*check_dependency_p=*/true,
12497 /*class_head_p=*/false,
12498 /*is_declaration=*/true);
12499 /* If we found one, we're done. */
12500 if (cp_parser_parse_definitely (parser
))
12502 /* Otherwise, look for an ordinary identifier. */
12503 return cp_parser_identifier (parser
);
12506 /* Overloading [gram.over] */
12508 /* Parse an operator-function-id.
12510 operator-function-id:
12513 Returns an IDENTIFIER_NODE for the operator which is a
12514 human-readable spelling of the identifier, e.g., `operator +'. */
12517 cp_parser_operator_function_id (cp_parser
* parser
)
12519 /* Look for the `operator' keyword. */
12520 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12521 return error_mark_node
;
12522 /* And then the name of the operator itself. */
12523 return cp_parser_operator (parser
);
12526 /* Return an identifier node for a user-defined literal operator.
12527 The suffix identifier is chained to the operator name identifier. */
12530 cp_literal_operator_id (const char* name
)
12533 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12534 + strlen (name
) + 10);
12535 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12536 identifier
= get_identifier (buffer
);
12541 /* Parse an operator.
12544 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12545 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12546 || ++ -- , ->* -> () []
12553 Returns an IDENTIFIER_NODE for the operator which is a
12554 human-readable spelling of the identifier, e.g., `operator +'. */
12557 cp_parser_operator (cp_parser
* parser
)
12559 tree id
= NULL_TREE
;
12561 bool bad_encoding_prefix
= false;
12563 /* Peek at the next token. */
12564 token
= cp_lexer_peek_token (parser
->lexer
);
12565 /* Figure out which operator we have. */
12566 switch (token
->type
)
12572 /* The keyword should be either `new' or `delete'. */
12573 if (token
->keyword
== RID_NEW
)
12575 else if (token
->keyword
== RID_DELETE
)
12580 /* Consume the `new' or `delete' token. */
12581 cp_lexer_consume_token (parser
->lexer
);
12583 /* Peek at the next token. */
12584 token
= cp_lexer_peek_token (parser
->lexer
);
12585 /* If it's a `[' token then this is the array variant of the
12587 if (token
->type
== CPP_OPEN_SQUARE
)
12589 /* Consume the `[' token. */
12590 cp_lexer_consume_token (parser
->lexer
);
12591 /* Look for the `]' token. */
12592 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12593 id
= ansi_opname (op
== NEW_EXPR
12594 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12596 /* Otherwise, we have the non-array variant. */
12598 id
= ansi_opname (op
);
12604 id
= ansi_opname (PLUS_EXPR
);
12608 id
= ansi_opname (MINUS_EXPR
);
12612 id
= ansi_opname (MULT_EXPR
);
12616 id
= ansi_opname (TRUNC_DIV_EXPR
);
12620 id
= ansi_opname (TRUNC_MOD_EXPR
);
12624 id
= ansi_opname (BIT_XOR_EXPR
);
12628 id
= ansi_opname (BIT_AND_EXPR
);
12632 id
= ansi_opname (BIT_IOR_EXPR
);
12636 id
= ansi_opname (BIT_NOT_EXPR
);
12640 id
= ansi_opname (TRUTH_NOT_EXPR
);
12644 id
= ansi_assopname (NOP_EXPR
);
12648 id
= ansi_opname (LT_EXPR
);
12652 id
= ansi_opname (GT_EXPR
);
12656 id
= ansi_assopname (PLUS_EXPR
);
12660 id
= ansi_assopname (MINUS_EXPR
);
12664 id
= ansi_assopname (MULT_EXPR
);
12668 id
= ansi_assopname (TRUNC_DIV_EXPR
);
12672 id
= ansi_assopname (TRUNC_MOD_EXPR
);
12676 id
= ansi_assopname (BIT_XOR_EXPR
);
12680 id
= ansi_assopname (BIT_AND_EXPR
);
12684 id
= ansi_assopname (BIT_IOR_EXPR
);
12688 id
= ansi_opname (LSHIFT_EXPR
);
12692 id
= ansi_opname (RSHIFT_EXPR
);
12695 case CPP_LSHIFT_EQ
:
12696 id
= ansi_assopname (LSHIFT_EXPR
);
12699 case CPP_RSHIFT_EQ
:
12700 id
= ansi_assopname (RSHIFT_EXPR
);
12704 id
= ansi_opname (EQ_EXPR
);
12708 id
= ansi_opname (NE_EXPR
);
12712 id
= ansi_opname (LE_EXPR
);
12715 case CPP_GREATER_EQ
:
12716 id
= ansi_opname (GE_EXPR
);
12720 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
12724 id
= ansi_opname (TRUTH_ORIF_EXPR
);
12727 case CPP_PLUS_PLUS
:
12728 id
= ansi_opname (POSTINCREMENT_EXPR
);
12731 case CPP_MINUS_MINUS
:
12732 id
= ansi_opname (PREDECREMENT_EXPR
);
12736 id
= ansi_opname (COMPOUND_EXPR
);
12739 case CPP_DEREF_STAR
:
12740 id
= ansi_opname (MEMBER_REF
);
12744 id
= ansi_opname (COMPONENT_REF
);
12747 case CPP_OPEN_PAREN
:
12748 /* Consume the `('. */
12749 cp_lexer_consume_token (parser
->lexer
);
12750 /* Look for the matching `)'. */
12751 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
12752 return ansi_opname (CALL_EXPR
);
12754 case CPP_OPEN_SQUARE
:
12755 /* Consume the `['. */
12756 cp_lexer_consume_token (parser
->lexer
);
12757 /* Look for the matching `]'. */
12758 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12759 return ansi_opname (ARRAY_REF
);
12764 case CPP_UTF8STRING
:
12765 bad_encoding_prefix
= true;
12766 /* Fall through. */
12769 if (cxx_dialect
== cxx98
)
12770 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12771 if (bad_encoding_prefix
)
12773 error ("invalid encoding prefix in literal operator");
12774 return error_mark_node
;
12776 if (TREE_STRING_LENGTH (token
->u
.value
) > 2)
12778 error ("expected empty string after %<operator%> keyword");
12779 return error_mark_node
;
12781 /* Consume the string. */
12782 cp_lexer_consume_token (parser
->lexer
);
12783 /* Look for the suffix identifier. */
12784 token
= cp_lexer_peek_token (parser
->lexer
);
12785 if (token
->type
== CPP_NAME
)
12787 id
= cp_parser_identifier (parser
);
12788 if (id
!= error_mark_node
)
12790 const char *name
= IDENTIFIER_POINTER (id
);
12791 return cp_literal_operator_id (name
);
12794 else if (token
->type
== CPP_KEYWORD
)
12796 error ("unexpected keyword;"
12797 " remove space between quotes and suffix identifier");
12798 return error_mark_node
;
12802 error ("expected suffix identifier");
12803 return error_mark_node
;
12806 case CPP_WSTRING_USERDEF
:
12807 case CPP_STRING16_USERDEF
:
12808 case CPP_STRING32_USERDEF
:
12809 case CPP_UTF8STRING_USERDEF
:
12810 bad_encoding_prefix
= true;
12811 /* Fall through. */
12813 case CPP_STRING_USERDEF
:
12814 if (cxx_dialect
== cxx98
)
12815 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12816 if (bad_encoding_prefix
)
12818 error ("invalid encoding prefix in literal operator");
12819 return error_mark_node
;
12822 tree string_tree
= USERDEF_LITERAL_VALUE (token
->u
.value
);
12823 if (TREE_STRING_LENGTH (string_tree
) > 2)
12825 error ("expected empty string after %<operator%> keyword");
12826 return error_mark_node
;
12828 id
= USERDEF_LITERAL_SUFFIX_ID (token
->u
.value
);
12829 /* Consume the user-defined string literal. */
12830 cp_lexer_consume_token (parser
->lexer
);
12831 if (id
!= error_mark_node
)
12833 const char *name
= IDENTIFIER_POINTER (id
);
12834 return cp_literal_operator_id (name
);
12837 return error_mark_node
;
12841 /* Anything else is an error. */
12845 /* If we have selected an identifier, we need to consume the
12848 cp_lexer_consume_token (parser
->lexer
);
12849 /* Otherwise, no valid operator name was present. */
12852 cp_parser_error (parser
, "expected operator");
12853 id
= error_mark_node
;
12859 /* Parse a template-declaration.
12861 template-declaration:
12862 export [opt] template < template-parameter-list > declaration
12864 If MEMBER_P is TRUE, this template-declaration occurs within a
12867 The grammar rule given by the standard isn't correct. What
12868 is really meant is:
12870 template-declaration:
12871 export [opt] template-parameter-list-seq
12872 decl-specifier-seq [opt] init-declarator [opt] ;
12873 export [opt] template-parameter-list-seq
12874 function-definition
12876 template-parameter-list-seq:
12877 template-parameter-list-seq [opt]
12878 template < template-parameter-list > */
12881 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
12883 /* Check for `export'. */
12884 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
12886 /* Consume the `export' token. */
12887 cp_lexer_consume_token (parser
->lexer
);
12888 /* Warn that we do not support `export'. */
12889 warning (0, "keyword %<export%> not implemented, and will be ignored");
12892 cp_parser_template_declaration_after_export (parser
, member_p
);
12895 /* Parse a template-parameter-list.
12897 template-parameter-list:
12899 template-parameter-list , template-parameter
12901 Returns a TREE_LIST. Each node represents a template parameter.
12902 The nodes are connected via their TREE_CHAINs. */
12905 cp_parser_template_parameter_list (cp_parser
* parser
)
12907 tree parameter_list
= NULL_TREE
;
12909 begin_template_parm_list ();
12911 /* The loop below parses the template parms. We first need to know
12912 the total number of template parms to be able to compute proper
12913 canonical types of each dependent type. So after the loop, when
12914 we know the total number of template parms,
12915 end_template_parm_list computes the proper canonical types and
12916 fixes up the dependent types accordingly. */
12921 bool is_parameter_pack
;
12922 location_t parm_loc
;
12924 /* Parse the template-parameter. */
12925 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12926 parameter
= cp_parser_template_parameter (parser
,
12928 &is_parameter_pack
);
12929 /* Add it to the list. */
12930 if (parameter
!= error_mark_node
)
12931 parameter_list
= process_template_parm (parameter_list
,
12935 is_parameter_pack
);
12938 tree err_parm
= build_tree_list (parameter
, parameter
);
12939 parameter_list
= chainon (parameter_list
, err_parm
);
12942 /* If the next token is not a `,', we're done. */
12943 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12945 /* Otherwise, consume the `,' token. */
12946 cp_lexer_consume_token (parser
->lexer
);
12949 return end_template_parm_list (parameter_list
);
12952 /* Parse a template-parameter.
12954 template-parameter:
12956 parameter-declaration
12958 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12959 the parameter. The TREE_PURPOSE is the default value, if any.
12960 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12961 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12962 set to true iff this parameter is a parameter pack. */
12965 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
12966 bool *is_parameter_pack
)
12969 cp_parameter_declarator
*parameter_declarator
;
12970 cp_declarator
*id_declarator
;
12973 /* Assume it is a type parameter or a template parameter. */
12974 *is_non_type
= false;
12975 /* Assume it not a parameter pack. */
12976 *is_parameter_pack
= false;
12977 /* Peek at the next token. */
12978 token
= cp_lexer_peek_token (parser
->lexer
);
12979 /* If it is `class' or `template', we have a type-parameter. */
12980 if (token
->keyword
== RID_TEMPLATE
)
12981 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12982 /* If it is `class' or `typename' we do not know yet whether it is a
12983 type parameter or a non-type parameter. Consider:
12985 template <typename T, typename T::X X> ...
12989 template <class C, class D*> ...
12991 Here, the first parameter is a type parameter, and the second is
12992 a non-type parameter. We can tell by looking at the token after
12993 the identifier -- if it is a `,', `=', or `>' then we have a type
12995 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
12997 /* Peek at the token after `class' or `typename'. */
12998 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12999 /* If it's an ellipsis, we have a template type parameter
13001 if (token
->type
== CPP_ELLIPSIS
)
13002 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13003 /* If it's an identifier, skip it. */
13004 if (token
->type
== CPP_NAME
)
13005 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
13006 /* Now, see if the token looks like the end of a template
13008 if (token
->type
== CPP_COMMA
13009 || token
->type
== CPP_EQ
13010 || token
->type
== CPP_GREATER
)
13011 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13014 /* Otherwise, it is a non-type parameter.
13018 When parsing a default template-argument for a non-type
13019 template-parameter, the first non-nested `>' is taken as the end
13020 of the template parameter-list rather than a greater-than
13022 *is_non_type
= true;
13023 parameter_declarator
13024 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
13025 /*parenthesized_p=*/NULL
);
13027 if (!parameter_declarator
)
13028 return error_mark_node
;
13030 /* If the parameter declaration is marked as a parameter pack, set
13031 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13032 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13034 if (parameter_declarator
->declarator
13035 && parameter_declarator
->declarator
->parameter_pack_p
)
13037 *is_parameter_pack
= true;
13038 parameter_declarator
->declarator
->parameter_pack_p
= false;
13041 if (parameter_declarator
->default_argument
)
13043 /* Can happen in some cases of erroneous input (c++/34892). */
13044 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13045 /* Consume the `...' for better error recovery. */
13046 cp_lexer_consume_token (parser
->lexer
);
13048 /* If the next token is an ellipsis, and we don't already have it
13049 marked as a parameter pack, then we have a parameter pack (that
13050 has no declarator). */
13051 else if (!*is_parameter_pack
13052 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13053 && (declarator_can_be_parameter_pack
13054 (parameter_declarator
->declarator
)))
13056 /* Consume the `...'. */
13057 cp_lexer_consume_token (parser
->lexer
);
13058 maybe_warn_variadic_templates ();
13060 *is_parameter_pack
= true;
13062 /* We might end up with a pack expansion as the type of the non-type
13063 template parameter, in which case this is a non-type template
13065 else if (parameter_declarator
->decl_specifiers
.type
13066 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
13068 *is_parameter_pack
= true;
13069 parameter_declarator
->decl_specifiers
.type
=
13070 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
13073 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13075 /* Parameter packs cannot have default arguments. However, a
13076 user may try to do so, so we'll parse them and give an
13077 appropriate diagnostic here. */
13079 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13081 /* Find the name of the parameter pack. */
13082 id_declarator
= parameter_declarator
->declarator
;
13083 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
13084 id_declarator
= id_declarator
->declarator
;
13086 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
13087 error_at (start_token
->location
,
13088 "template parameter pack %qD cannot have a default argument",
13089 id_declarator
->u
.id
.unqualified_name
);
13091 error_at (start_token
->location
,
13092 "template parameter pack cannot have a default argument");
13094 /* Parse the default argument, but throw away the result. */
13095 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
13098 parm
= grokdeclarator (parameter_declarator
->declarator
,
13099 ¶meter_declarator
->decl_specifiers
,
13100 TPARM
, /*initialized=*/0,
13101 /*attrlist=*/NULL
);
13102 if (parm
== error_mark_node
)
13103 return error_mark_node
;
13105 return build_tree_list (parameter_declarator
->default_argument
, parm
);
13108 /* Parse a type-parameter.
13111 class identifier [opt]
13112 class identifier [opt] = type-id
13113 typename identifier [opt]
13114 typename identifier [opt] = type-id
13115 template < template-parameter-list > class identifier [opt]
13116 template < template-parameter-list > class identifier [opt]
13119 GNU Extension (variadic templates):
13122 class ... identifier [opt]
13123 typename ... identifier [opt]
13125 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13126 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13127 the declaration of the parameter.
13129 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13132 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13137 /* Look for a keyword to tell us what kind of parameter this is. */
13138 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13140 return error_mark_node
;
13142 switch (token
->keyword
)
13148 tree default_argument
;
13150 /* If the next token is an ellipsis, we have a template
13152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13154 /* Consume the `...' token. */
13155 cp_lexer_consume_token (parser
->lexer
);
13156 maybe_warn_variadic_templates ();
13158 *is_parameter_pack
= true;
13161 /* If the next token is an identifier, then it names the
13163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13164 identifier
= cp_parser_identifier (parser
);
13166 identifier
= NULL_TREE
;
13168 /* Create the parameter. */
13169 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13171 /* If the next token is an `=', we have a default argument. */
13172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13174 /* Consume the `=' token. */
13175 cp_lexer_consume_token (parser
->lexer
);
13176 /* Parse the default-argument. */
13177 push_deferring_access_checks (dk_no_deferred
);
13178 default_argument
= cp_parser_type_id (parser
);
13180 /* Template parameter packs cannot have default
13182 if (*is_parameter_pack
)
13185 error_at (token
->location
,
13186 "template parameter pack %qD cannot have a "
13187 "default argument", identifier
);
13189 error_at (token
->location
,
13190 "template parameter packs cannot have "
13191 "default arguments");
13192 default_argument
= NULL_TREE
;
13194 pop_deferring_access_checks ();
13197 default_argument
= NULL_TREE
;
13199 /* Create the combined representation of the parameter and the
13200 default argument. */
13201 parameter
= build_tree_list (default_argument
, parameter
);
13208 tree default_argument
;
13210 /* Look for the `<'. */
13211 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13212 /* Parse the template-parameter-list. */
13213 cp_parser_template_parameter_list (parser
);
13214 /* Look for the `>'. */
13215 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13216 /* Look for the `class' keyword. */
13217 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
13218 /* If the next token is an ellipsis, we have a template
13220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13222 /* Consume the `...' token. */
13223 cp_lexer_consume_token (parser
->lexer
);
13224 maybe_warn_variadic_templates ();
13226 *is_parameter_pack
= true;
13228 /* If the next token is an `=', then there is a
13229 default-argument. If the next token is a `>', we are at
13230 the end of the parameter-list. If the next token is a `,',
13231 then we are at the end of this parameter. */
13232 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13233 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13234 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13236 identifier
= cp_parser_identifier (parser
);
13237 /* Treat invalid names as if the parameter were nameless. */
13238 if (identifier
== error_mark_node
)
13239 identifier
= NULL_TREE
;
13242 identifier
= NULL_TREE
;
13244 /* Create the template parameter. */
13245 parameter
= finish_template_template_parm (class_type_node
,
13248 /* If the next token is an `=', then there is a
13249 default-argument. */
13250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13254 /* Consume the `='. */
13255 cp_lexer_consume_token (parser
->lexer
);
13256 /* Parse the id-expression. */
13257 push_deferring_access_checks (dk_no_deferred
);
13258 /* save token before parsing the id-expression, for error
13260 token
= cp_lexer_peek_token (parser
->lexer
);
13262 = cp_parser_id_expression (parser
,
13263 /*template_keyword_p=*/false,
13264 /*check_dependency_p=*/true,
13265 /*template_p=*/&is_template
,
13266 /*declarator_p=*/false,
13267 /*optional_p=*/false);
13268 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13269 /* If the id-expression was a template-id that refers to
13270 a template-class, we already have the declaration here,
13271 so no further lookup is needed. */
13274 /* Look up the name. */
13276 = cp_parser_lookup_name (parser
, default_argument
,
13278 /*is_template=*/is_template
,
13279 /*is_namespace=*/false,
13280 /*check_dependency=*/true,
13281 /*ambiguous_decls=*/NULL
,
13283 /* See if the default argument is valid. */
13285 = check_template_template_default_arg (default_argument
);
13287 /* Template parameter packs cannot have default
13289 if (*is_parameter_pack
)
13292 error_at (token
->location
,
13293 "template parameter pack %qD cannot "
13294 "have a default argument",
13297 error_at (token
->location
, "template parameter packs cannot "
13298 "have default arguments");
13299 default_argument
= NULL_TREE
;
13301 pop_deferring_access_checks ();
13304 default_argument
= NULL_TREE
;
13306 /* Create the combined representation of the parameter and the
13307 default argument. */
13308 parameter
= build_tree_list (default_argument
, parameter
);
13313 gcc_unreachable ();
13320 /* Parse a template-id.
13323 template-name < template-argument-list [opt] >
13325 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13326 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13327 returned. Otherwise, if the template-name names a function, or set
13328 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13329 names a class, returns a TYPE_DECL for the specialization.
13331 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13332 uninstantiated templates. */
13335 cp_parser_template_id (cp_parser
*parser
,
13336 bool template_keyword_p
,
13337 bool check_dependency_p
,
13338 enum tag_types tag_type
,
13339 bool is_declaration
)
13345 cp_token_position start_of_id
= 0;
13346 deferred_access_check
*chk
;
13347 vec
<deferred_access_check
, va_gc
> *access_check
;
13348 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13349 bool is_identifier
;
13351 /* If the next token corresponds to a template-id, there is no need
13353 next_token
= cp_lexer_peek_token (parser
->lexer
);
13354 if (next_token
->type
== CPP_TEMPLATE_ID
)
13356 struct tree_check
*check_value
;
13358 /* Get the stored value. */
13359 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13360 /* Perform any access checks that were deferred. */
13361 access_check
= check_value
->checks
;
13364 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13365 perform_or_defer_access_check (chk
->binfo
,
13368 tf_warning_or_error
);
13370 /* Return the stored value. */
13371 return check_value
->value
;
13374 /* Avoid performing name lookup if there is no possibility of
13375 finding a template-id. */
13376 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13377 || (next_token
->type
== CPP_NAME
13378 && !cp_parser_nth_token_starts_template_argument_list_p
13381 cp_parser_error (parser
, "expected template-id");
13382 return error_mark_node
;
13385 /* Remember where the template-id starts. */
13386 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13387 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13389 push_deferring_access_checks (dk_deferred
);
13391 /* Parse the template-name. */
13392 is_identifier
= false;
13393 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13394 check_dependency_p
,
13398 if (templ
== error_mark_node
|| is_identifier
)
13400 pop_deferring_access_checks ();
13404 /* If we find the sequence `[:' after a template-name, it's probably
13405 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13406 parse correctly the argument list. */
13407 next_token
= cp_lexer_peek_token (parser
->lexer
);
13408 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13409 if (next_token
->type
== CPP_OPEN_SQUARE
13410 && next_token
->flags
& DIGRAPH
13411 && next_token_2
->type
== CPP_COLON
13412 && !(next_token_2
->flags
& PREV_WHITE
))
13414 cp_parser_parse_tentatively (parser
);
13415 /* Change `:' into `::'. */
13416 next_token_2
->type
= CPP_SCOPE
;
13417 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13419 cp_lexer_consume_token (parser
->lexer
);
13421 /* Parse the arguments. */
13422 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13423 if (!cp_parser_parse_definitely (parser
))
13425 /* If we couldn't parse an argument list, then we revert our changes
13426 and return simply an error. Maybe this is not a template-id
13428 next_token_2
->type
= CPP_COLON
;
13429 cp_parser_error (parser
, "expected %<<%>");
13430 pop_deferring_access_checks ();
13431 return error_mark_node
;
13433 /* Otherwise, emit an error about the invalid digraph, but continue
13434 parsing because we got our argument list. */
13435 if (permerror (next_token
->location
,
13436 "%<<::%> cannot begin a template-argument list"))
13438 static bool hint
= false;
13439 inform (next_token
->location
,
13440 "%<<:%> is an alternate spelling for %<[%>."
13441 " Insert whitespace between %<<%> and %<::%>");
13442 if (!hint
&& !flag_permissive
)
13444 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13445 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13446 "accept your code)");
13453 /* Look for the `<' that starts the template-argument-list. */
13454 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13456 pop_deferring_access_checks ();
13457 return error_mark_node
;
13459 /* Parse the arguments. */
13460 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13463 /* Build a representation of the specialization. */
13464 if (identifier_p (templ
))
13465 template_id
= build_min_nt_loc (next_token
->location
,
13468 else if (DECL_TYPE_TEMPLATE_P (templ
)
13469 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13471 bool entering_scope
;
13472 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13473 template (rather than some instantiation thereof) only if
13474 is not nested within some other construct. For example, in
13475 "template <typename T> void f(T) { A<T>::", A<T> is just an
13476 instantiation of A. */
13477 entering_scope
= (template_parm_scope_p ()
13478 && cp_lexer_next_token_is (parser
->lexer
,
13481 = finish_template_type (templ
, arguments
, entering_scope
);
13485 /* If it's not a class-template or a template-template, it should be
13486 a function-template. */
13487 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13488 || TREE_CODE (templ
) == OVERLOAD
13489 || BASELINK_P (templ
)));
13491 template_id
= lookup_template_function (templ
, arguments
);
13494 /* If parsing tentatively, replace the sequence of tokens that makes
13495 up the template-id with a CPP_TEMPLATE_ID token. That way,
13496 should we re-parse the token stream, we will not have to repeat
13497 the effort required to do the parse, nor will we issue duplicate
13498 error messages about problems during instantiation of the
13501 /* Don't do this if we had a parse error in a declarator; re-parsing
13502 might succeed if a name changes meaning (60361). */
13503 && !(cp_parser_error_occurred (parser
)
13504 && cp_parser_parsing_tentatively (parser
)
13505 && parser
->in_declarator_p
))
13507 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13509 /* Reset the contents of the START_OF_ID token. */
13510 token
->type
= CPP_TEMPLATE_ID
;
13511 /* Retrieve any deferred checks. Do not pop this access checks yet
13512 so the memory will not be reclaimed during token replacing below. */
13513 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
13514 token
->u
.tree_check_value
->value
= template_id
;
13515 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13516 token
->keyword
= RID_MAX
;
13518 /* Purge all subsequent tokens. */
13519 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13521 /* ??? Can we actually assume that, if template_id ==
13522 error_mark_node, we will have issued a diagnostic to the
13523 user, as opposed to simply marking the tentative parse as
13525 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13526 error_at (token
->location
, "parse error in template argument list");
13529 pop_to_parent_deferring_access_checks ();
13530 return template_id
;
13533 /* Parse a template-name.
13538 The standard should actually say:
13542 operator-function-id
13544 A defect report has been filed about this issue.
13546 A conversion-function-id cannot be a template name because they cannot
13547 be part of a template-id. In fact, looking at this code:
13549 a.operator K<int>()
13551 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13552 It is impossible to call a templated conversion-function-id with an
13553 explicit argument list, since the only allowed template parameter is
13554 the type to which it is converting.
13556 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13557 `template' keyword, in a construction like:
13561 In that case `f' is taken to be a template-name, even though there
13562 is no way of knowing for sure.
13564 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13565 name refers to a set of overloaded functions, at least one of which
13566 is a template, or an IDENTIFIER_NODE with the name of the template,
13567 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13568 names are looked up inside uninstantiated templates. */
13571 cp_parser_template_name (cp_parser
* parser
,
13572 bool template_keyword_p
,
13573 bool check_dependency_p
,
13574 bool is_declaration
,
13575 enum tag_types tag_type
,
13576 bool *is_identifier
)
13581 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13583 /* If the next token is `operator', then we have either an
13584 operator-function-id or a conversion-function-id. */
13585 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13587 /* We don't know whether we're looking at an
13588 operator-function-id or a conversion-function-id. */
13589 cp_parser_parse_tentatively (parser
);
13590 /* Try an operator-function-id. */
13591 identifier
= cp_parser_operator_function_id (parser
);
13592 /* If that didn't work, try a conversion-function-id. */
13593 if (!cp_parser_parse_definitely (parser
))
13595 cp_parser_error (parser
, "expected template-name");
13596 return error_mark_node
;
13599 /* Look for the identifier. */
13601 identifier
= cp_parser_identifier (parser
);
13603 /* If we didn't find an identifier, we don't have a template-id. */
13604 if (identifier
== error_mark_node
)
13605 return error_mark_node
;
13607 /* If the name immediately followed the `template' keyword, then it
13608 is a template-name. However, if the next token is not `<', then
13609 we do not treat it as a template-name, since it is not being used
13610 as part of a template-id. This enables us to handle constructs
13613 template <typename T> struct S { S(); };
13614 template <typename T> S<T>::S();
13616 correctly. We would treat `S' as a template -- if it were `S<T>'
13617 -- but we do not if there is no `<'. */
13619 if (processing_template_decl
13620 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13622 /* In a declaration, in a dependent context, we pretend that the
13623 "template" keyword was present in order to improve error
13624 recovery. For example, given:
13626 template <typename T> void f(T::X<int>);
13628 we want to treat "X<int>" as a template-id. */
13630 && !template_keyword_p
13631 && parser
->scope
&& TYPE_P (parser
->scope
)
13632 && check_dependency_p
13633 && dependent_scope_p (parser
->scope
)
13634 /* Do not do this for dtors (or ctors), since they never
13635 need the template keyword before their name. */
13636 && !constructor_name_p (identifier
, parser
->scope
))
13638 cp_token_position start
= 0;
13640 /* Explain what went wrong. */
13641 error_at (token
->location
, "non-template %qD used as template",
13643 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
13644 parser
->scope
, identifier
);
13645 /* If parsing tentatively, find the location of the "<" token. */
13646 if (cp_parser_simulate_error (parser
))
13647 start
= cp_lexer_token_position (parser
->lexer
, true);
13648 /* Parse the template arguments so that we can issue error
13649 messages about them. */
13650 cp_lexer_consume_token (parser
->lexer
);
13651 cp_parser_enclosed_template_argument_list (parser
);
13652 /* Skip tokens until we find a good place from which to
13653 continue parsing. */
13654 cp_parser_skip_to_closing_parenthesis (parser
,
13655 /*recovering=*/true,
13657 /*consume_paren=*/false);
13658 /* If parsing tentatively, permanently remove the
13659 template argument list. That will prevent duplicate
13660 error messages from being issued about the missing
13661 "template" keyword. */
13663 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
13665 *is_identifier
= true;
13669 /* If the "template" keyword is present, then there is generally
13670 no point in doing name-lookup, so we just return IDENTIFIER.
13671 But, if the qualifying scope is non-dependent then we can
13672 (and must) do name-lookup normally. */
13673 if (template_keyword_p
13675 || (TYPE_P (parser
->scope
)
13676 && dependent_type_p (parser
->scope
))))
13680 /* Look up the name. */
13681 decl
= cp_parser_lookup_name (parser
, identifier
,
13683 /*is_template=*/true,
13684 /*is_namespace=*/false,
13685 check_dependency_p
,
13686 /*ambiguous_decls=*/NULL
,
13689 /* If DECL is a template, then the name was a template-name. */
13690 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
13694 tree fn
= NULL_TREE
;
13696 /* The standard does not explicitly indicate whether a name that
13697 names a set of overloaded declarations, some of which are
13698 templates, is a template-name. However, such a name should
13699 be a template-name; otherwise, there is no way to form a
13700 template-id for the overloaded templates. */
13701 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
13702 if (TREE_CODE (fns
) == OVERLOAD
)
13703 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
13704 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
13709 /* The name does not name a template. */
13710 cp_parser_error (parser
, "expected template-name");
13711 return error_mark_node
;
13715 /* If DECL is dependent, and refers to a function, then just return
13716 its name; we will look it up again during template instantiation. */
13717 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
13719 tree scope
= ovl_scope (decl
);
13720 if (TYPE_P (scope
) && dependent_type_p (scope
))
13727 /* Parse a template-argument-list.
13729 template-argument-list:
13730 template-argument ... [opt]
13731 template-argument-list , template-argument ... [opt]
13733 Returns a TREE_VEC containing the arguments. */
13736 cp_parser_template_argument_list (cp_parser
* parser
)
13738 tree fixed_args
[10];
13739 unsigned n_args
= 0;
13740 unsigned alloced
= 10;
13741 tree
*arg_ary
= fixed_args
;
13743 bool saved_in_template_argument_list_p
;
13745 bool saved_non_ice_p
;
13747 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
13748 parser
->in_template_argument_list_p
= true;
13749 /* Even if the template-id appears in an integral
13750 constant-expression, the contents of the argument list do
13752 saved_ice_p
= parser
->integral_constant_expression_p
;
13753 parser
->integral_constant_expression_p
= false;
13754 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
13755 parser
->non_integral_constant_expression_p
= false;
13757 /* Parse the arguments. */
13763 /* Consume the comma. */
13764 cp_lexer_consume_token (parser
->lexer
);
13766 /* Parse the template-argument. */
13767 argument
= cp_parser_template_argument (parser
);
13769 /* If the next token is an ellipsis, we're expanding a template
13771 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13773 if (argument
== error_mark_node
)
13775 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13776 error_at (token
->location
,
13777 "expected parameter pack before %<...%>");
13779 /* Consume the `...' token. */
13780 cp_lexer_consume_token (parser
->lexer
);
13782 /* Make the argument into a TYPE_PACK_EXPANSION or
13783 EXPR_PACK_EXPANSION. */
13784 argument
= make_pack_expansion (argument
);
13787 if (n_args
== alloced
)
13791 if (arg_ary
== fixed_args
)
13793 arg_ary
= XNEWVEC (tree
, alloced
);
13794 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
13797 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
13799 arg_ary
[n_args
++] = argument
;
13801 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
13803 vec
= make_tree_vec (n_args
);
13806 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
13808 if (arg_ary
!= fixed_args
)
13810 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
13811 parser
->integral_constant_expression_p
= saved_ice_p
;
13812 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
13813 #ifdef ENABLE_CHECKING
13814 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
13819 /* Parse a template-argument.
13822 assignment-expression
13826 The representation is that of an assignment-expression, type-id, or
13827 id-expression -- except that the qualified id-expression is
13828 evaluated, so that the value returned is either a DECL or an
13831 Although the standard says "assignment-expression", it forbids
13832 throw-expressions or assignments in the template argument.
13833 Therefore, we use "conditional-expression" instead. */
13836 cp_parser_template_argument (cp_parser
* parser
)
13841 bool maybe_type_id
= false;
13842 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
13843 location_t loc
= 0;
13846 /* There's really no way to know what we're looking at, so we just
13847 try each alternative in order.
13851 In a template-argument, an ambiguity between a type-id and an
13852 expression is resolved to a type-id, regardless of the form of
13853 the corresponding template-parameter.
13855 Therefore, we try a type-id first. */
13856 cp_parser_parse_tentatively (parser
);
13857 argument
= cp_parser_template_type_arg (parser
);
13858 /* If there was no error parsing the type-id but the next token is a
13859 '>>', our behavior depends on which dialect of C++ we're
13860 parsing. In C++98, we probably found a typo for '> >'. But there
13861 are type-id which are also valid expressions. For instance:
13863 struct X { int operator >> (int); };
13864 template <int V> struct Foo {};
13867 Here 'X()' is a valid type-id of a function type, but the user just
13868 wanted to write the expression "X() >> 5". Thus, we remember that we
13869 found a valid type-id, but we still try to parse the argument as an
13870 expression to see what happens.
13872 In C++0x, the '>>' will be considered two separate '>'
13874 if (!cp_parser_error_occurred (parser
)
13875 && cxx_dialect
== cxx98
13876 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
13878 maybe_type_id
= true;
13879 cp_parser_abort_tentative_parse (parser
);
13883 /* If the next token isn't a `,' or a `>', then this argument wasn't
13884 really finished. This means that the argument is not a valid
13886 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13887 cp_parser_error (parser
, "expected template-argument");
13888 /* If that worked, we're done. */
13889 if (cp_parser_parse_definitely (parser
))
13892 /* We're still not sure what the argument will be. */
13893 cp_parser_parse_tentatively (parser
);
13894 /* Try a template. */
13895 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
13896 argument
= cp_parser_id_expression (parser
,
13897 /*template_keyword_p=*/false,
13898 /*check_dependency_p=*/true,
13900 /*declarator_p=*/false,
13901 /*optional_p=*/false);
13902 /* If the next token isn't a `,' or a `>', then this argument wasn't
13903 really finished. */
13904 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13905 cp_parser_error (parser
, "expected template-argument");
13906 if (!cp_parser_error_occurred (parser
))
13908 /* Figure out what is being referred to. If the id-expression
13909 was for a class template specialization, then we will have a
13910 TYPE_DECL at this point. There is no need to do name lookup
13911 at this point in that case. */
13912 if (TREE_CODE (argument
) != TYPE_DECL
)
13913 argument
= cp_parser_lookup_name (parser
, argument
,
13915 /*is_template=*/template_p
,
13916 /*is_namespace=*/false,
13917 /*check_dependency=*/true,
13918 /*ambiguous_decls=*/NULL
,
13919 argument_start_token
->location
);
13920 if (TREE_CODE (argument
) != TEMPLATE_DECL
13921 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
13922 cp_parser_error (parser
, "expected template-name");
13924 if (cp_parser_parse_definitely (parser
))
13926 /* It must be a non-type argument. There permitted cases are given
13927 in [temp.arg.nontype]:
13929 -- an integral constant-expression of integral or enumeration
13932 -- the name of a non-type template-parameter; or
13934 -- the name of an object or function with external linkage...
13936 -- the address of an object or function with external linkage...
13938 -- a pointer to member... */
13939 /* Look for a non-type template parameter. */
13940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13942 cp_parser_parse_tentatively (parser
);
13943 argument
= cp_parser_primary_expression (parser
,
13944 /*address_p=*/false,
13946 /*template_arg_p=*/true,
13948 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
13949 || !cp_parser_next_token_ends_template_argument_p (parser
))
13950 cp_parser_simulate_error (parser
);
13951 if (cp_parser_parse_definitely (parser
))
13955 /* If the next token is "&", the argument must be the address of an
13956 object or function with external linkage. */
13957 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
13960 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13961 cp_lexer_consume_token (parser
->lexer
);
13963 /* See if we might have an id-expression. */
13964 token
= cp_lexer_peek_token (parser
->lexer
);
13965 if (token
->type
== CPP_NAME
13966 || token
->keyword
== RID_OPERATOR
13967 || token
->type
== CPP_SCOPE
13968 || token
->type
== CPP_TEMPLATE_ID
13969 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
13971 cp_parser_parse_tentatively (parser
);
13972 argument
= cp_parser_primary_expression (parser
,
13975 /*template_arg_p=*/true,
13977 if (cp_parser_error_occurred (parser
)
13978 || !cp_parser_next_token_ends_template_argument_p (parser
))
13979 cp_parser_abort_tentative_parse (parser
);
13984 if (INDIRECT_REF_P (argument
))
13986 /* Strip the dereference temporarily. */
13987 gcc_assert (REFERENCE_REF_P (argument
));
13988 argument
= TREE_OPERAND (argument
, 0);
13991 /* If we're in a template, we represent a qualified-id referring
13992 to a static data member as a SCOPE_REF even if the scope isn't
13993 dependent so that we can check access control later. */
13995 if (TREE_CODE (probe
) == SCOPE_REF
)
13996 probe
= TREE_OPERAND (probe
, 1);
13999 /* A variable without external linkage might still be a
14000 valid constant-expression, so no error is issued here
14001 if the external-linkage check fails. */
14002 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
14003 cp_parser_simulate_error (parser
);
14005 else if (is_overloaded_fn (argument
))
14006 /* All overloaded functions are allowed; if the external
14007 linkage test does not pass, an error will be issued
14011 && (TREE_CODE (argument
) == OFFSET_REF
14012 || TREE_CODE (argument
) == SCOPE_REF
))
14013 /* A pointer-to-member. */
14015 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
14018 cp_parser_simulate_error (parser
);
14020 if (cp_parser_parse_definitely (parser
))
14023 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
14024 tf_warning_or_error
);
14026 argument
= convert_from_reference (argument
);
14031 /* If the argument started with "&", there are no other valid
14032 alternatives at this point. */
14035 cp_parser_error (parser
, "invalid non-type template argument");
14036 return error_mark_node
;
14039 /* If the argument wasn't successfully parsed as a type-id followed
14040 by '>>', the argument can only be a constant expression now.
14041 Otherwise, we try parsing the constant-expression tentatively,
14042 because the argument could really be a type-id. */
14044 cp_parser_parse_tentatively (parser
);
14045 argument
= cp_parser_constant_expression (parser
,
14046 /*allow_non_constant_p=*/false,
14047 /*non_constant_p=*/NULL
);
14048 if (!maybe_type_id
)
14050 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14051 cp_parser_error (parser
, "expected template-argument");
14052 if (cp_parser_parse_definitely (parser
))
14054 /* We did our best to parse the argument as a non type-id, but that
14055 was the only alternative that matched (albeit with a '>' after
14056 it). We can assume it's just a typo from the user, and a
14057 diagnostic will then be issued. */
14058 return cp_parser_template_type_arg (parser
);
14061 /* Parse an explicit-instantiation.
14063 explicit-instantiation:
14064 template declaration
14066 Although the standard says `declaration', what it really means is:
14068 explicit-instantiation:
14069 template decl-specifier-seq [opt] declarator [opt] ;
14071 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14072 supposed to be allowed. A defect report has been filed about this
14077 explicit-instantiation:
14078 storage-class-specifier template
14079 decl-specifier-seq [opt] declarator [opt] ;
14080 function-specifier template
14081 decl-specifier-seq [opt] declarator [opt] ; */
14084 cp_parser_explicit_instantiation (cp_parser
* parser
)
14086 int declares_class_or_enum
;
14087 cp_decl_specifier_seq decl_specifiers
;
14088 tree extension_specifier
= NULL_TREE
;
14090 timevar_push (TV_TEMPLATE_INST
);
14092 /* Look for an (optional) storage-class-specifier or
14093 function-specifier. */
14094 if (cp_parser_allow_gnu_extensions_p (parser
))
14096 extension_specifier
14097 = cp_parser_storage_class_specifier_opt (parser
);
14098 if (!extension_specifier
)
14099 extension_specifier
14100 = cp_parser_function_specifier_opt (parser
,
14101 /*decl_specs=*/NULL
);
14104 /* Look for the `template' keyword. */
14105 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14106 /* Let the front end know that we are processing an explicit
14108 begin_explicit_instantiation ();
14109 /* [temp.explicit] says that we are supposed to ignore access
14110 control while processing explicit instantiation directives. */
14111 push_deferring_access_checks (dk_no_check
);
14112 /* Parse a decl-specifier-seq. */
14113 cp_parser_decl_specifier_seq (parser
,
14114 CP_PARSER_FLAGS_OPTIONAL
,
14116 &declares_class_or_enum
);
14117 /* If there was exactly one decl-specifier, and it declared a class,
14118 and there's no declarator, then we have an explicit type
14120 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
14124 type
= check_tag_decl (&decl_specifiers
,
14125 /*explicit_type_instantiation_p=*/true);
14126 /* Turn access control back on for names used during
14127 template instantiation. */
14128 pop_deferring_access_checks ();
14130 do_type_instantiation (type
, extension_specifier
,
14131 /*complain=*/tf_error
);
14135 cp_declarator
*declarator
;
14138 /* Parse the declarator. */
14140 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14141 /*ctor_dtor_or_conv_p=*/NULL
,
14142 /*parenthesized_p=*/NULL
,
14143 /*member_p=*/false);
14144 if (declares_class_or_enum
& 2)
14145 cp_parser_check_for_definition_in_return_type (declarator
,
14146 decl_specifiers
.type
,
14147 decl_specifiers
.locations
[ds_type_spec
]);
14148 if (declarator
!= cp_error_declarator
)
14150 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14151 permerror (decl_specifiers
.locations
[ds_inline
],
14152 "explicit instantiation shall not use"
14153 " %<inline%> specifier");
14154 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14155 permerror (decl_specifiers
.locations
[ds_constexpr
],
14156 "explicit instantiation shall not use"
14157 " %<constexpr%> specifier");
14159 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14160 NORMAL
, 0, &decl_specifiers
.attributes
);
14161 /* Turn access control back on for names used during
14162 template instantiation. */
14163 pop_deferring_access_checks ();
14164 /* Do the explicit instantiation. */
14165 do_decl_instantiation (decl
, extension_specifier
);
14169 pop_deferring_access_checks ();
14170 /* Skip the body of the explicit instantiation. */
14171 cp_parser_skip_to_end_of_statement (parser
);
14174 /* We're done with the instantiation. */
14175 end_explicit_instantiation ();
14177 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14179 timevar_pop (TV_TEMPLATE_INST
);
14182 /* Parse an explicit-specialization.
14184 explicit-specialization:
14185 template < > declaration
14187 Although the standard says `declaration', what it really means is:
14189 explicit-specialization:
14190 template <> decl-specifier [opt] init-declarator [opt] ;
14191 template <> function-definition
14192 template <> explicit-specialization
14193 template <> template-declaration */
14196 cp_parser_explicit_specialization (cp_parser
* parser
)
14198 bool need_lang_pop
;
14199 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14201 /* Look for the `template' keyword. */
14202 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14203 /* Look for the `<'. */
14204 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14205 /* Look for the `>'. */
14206 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14207 /* We have processed another parameter list. */
14208 ++parser
->num_template_parameter_lists
;
14211 A template ... explicit specialization ... shall not have C
14213 if (current_lang_name
== lang_name_c
)
14215 error_at (token
->location
, "template specialization with C linkage");
14216 /* Give it C++ linkage to avoid confusing other parts of the
14218 push_lang_context (lang_name_cplusplus
);
14219 need_lang_pop
= true;
14222 need_lang_pop
= false;
14223 /* Let the front end know that we are beginning a specialization. */
14224 if (!begin_specialization ())
14226 end_specialization ();
14230 /* If the next keyword is `template', we need to figure out whether
14231 or not we're looking a template-declaration. */
14232 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14234 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14235 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14236 cp_parser_template_declaration_after_export (parser
,
14237 /*member_p=*/false);
14239 cp_parser_explicit_specialization (parser
);
14242 /* Parse the dependent declaration. */
14243 cp_parser_single_declaration (parser
,
14245 /*member_p=*/false,
14246 /*explicit_specialization_p=*/true,
14247 /*friend_p=*/NULL
);
14248 /* We're done with the specialization. */
14249 end_specialization ();
14250 /* For the erroneous case of a template with C linkage, we pushed an
14251 implicit C++ linkage scope; exit that scope now. */
14253 pop_lang_context ();
14254 /* We're done with this parameter list. */
14255 --parser
->num_template_parameter_lists
;
14258 /* Parse a type-specifier.
14261 simple-type-specifier
14264 elaborated-type-specifier
14272 Returns a representation of the type-specifier. For a
14273 class-specifier, enum-specifier, or elaborated-type-specifier, a
14274 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14276 The parser flags FLAGS is used to control type-specifier parsing.
14278 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14279 in a decl-specifier-seq.
14281 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14282 class-specifier, enum-specifier, or elaborated-type-specifier, then
14283 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14284 if a type is declared; 2 if it is defined. Otherwise, it is set to
14287 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14288 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14289 is set to FALSE. */
14292 cp_parser_type_specifier (cp_parser
* parser
,
14293 cp_parser_flags flags
,
14294 cp_decl_specifier_seq
*decl_specs
,
14295 bool is_declaration
,
14296 int* declares_class_or_enum
,
14297 bool* is_cv_qualifier
)
14299 tree type_spec
= NULL_TREE
;
14302 cp_decl_spec ds
= ds_last
;
14304 /* Assume this type-specifier does not declare a new type. */
14305 if (declares_class_or_enum
)
14306 *declares_class_or_enum
= 0;
14307 /* And that it does not specify a cv-qualifier. */
14308 if (is_cv_qualifier
)
14309 *is_cv_qualifier
= false;
14310 /* Peek at the next token. */
14311 token
= cp_lexer_peek_token (parser
->lexer
);
14313 /* If we're looking at a keyword, we can use that to guide the
14314 production we choose. */
14315 keyword
= token
->keyword
;
14319 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14320 goto elaborated_type_specifier
;
14322 /* Look for the enum-specifier. */
14323 type_spec
= cp_parser_enum_specifier (parser
);
14324 /* If that worked, we're done. */
14327 if (declares_class_or_enum
)
14328 *declares_class_or_enum
= 2;
14330 cp_parser_set_decl_spec_type (decl_specs
,
14333 /*type_definition_p=*/true);
14337 goto elaborated_type_specifier
;
14339 /* Any of these indicate either a class-specifier, or an
14340 elaborated-type-specifier. */
14344 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14345 goto elaborated_type_specifier
;
14347 /* Parse tentatively so that we can back up if we don't find a
14348 class-specifier. */
14349 cp_parser_parse_tentatively (parser
);
14350 /* Look for the class-specifier. */
14351 type_spec
= cp_parser_class_specifier (parser
);
14352 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14353 /* If that worked, we're done. */
14354 if (cp_parser_parse_definitely (parser
))
14356 if (declares_class_or_enum
)
14357 *declares_class_or_enum
= 2;
14359 cp_parser_set_decl_spec_type (decl_specs
,
14362 /*type_definition_p=*/true);
14366 /* Fall through. */
14367 elaborated_type_specifier
:
14368 /* We're declaring (not defining) a class or enum. */
14369 if (declares_class_or_enum
)
14370 *declares_class_or_enum
= 1;
14372 /* Fall through. */
14374 /* Look for an elaborated-type-specifier. */
14376 = (cp_parser_elaborated_type_specifier
14378 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14381 cp_parser_set_decl_spec_type (decl_specs
,
14384 /*type_definition_p=*/false);
14389 if (is_cv_qualifier
)
14390 *is_cv_qualifier
= true;
14395 if (is_cv_qualifier
)
14396 *is_cv_qualifier
= true;
14401 if (is_cv_qualifier
)
14402 *is_cv_qualifier
= true;
14406 /* The `__complex__' keyword is a GNU extension. */
14414 /* Handle simple keywords. */
14419 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14420 decl_specs
->any_specifiers_p
= true;
14422 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14425 /* If we do not already have a type-specifier, assume we are looking
14426 at a simple-type-specifier. */
14427 type_spec
= cp_parser_simple_type_specifier (parser
,
14431 /* If we didn't find a type-specifier, and a type-specifier was not
14432 optional in this context, issue an error message. */
14433 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14435 cp_parser_error (parser
, "expected type specifier");
14436 return error_mark_node
;
14442 /* Parse a simple-type-specifier.
14444 simple-type-specifier:
14445 :: [opt] nested-name-specifier [opt] type-name
14446 :: [opt] nested-name-specifier template template-id
14461 simple-type-specifier:
14463 decltype ( expression )
14466 __underlying_type ( type-id )
14470 simple-type-specifier:
14472 __typeof__ unary-expression
14473 __typeof__ ( type-id )
14474 __typeof__ ( type-id ) { initializer-list , [opt] }
14476 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14477 appropriately updated. */
14480 cp_parser_simple_type_specifier (cp_parser
* parser
,
14481 cp_decl_specifier_seq
*decl_specs
,
14482 cp_parser_flags flags
)
14484 tree type
= NULL_TREE
;
14487 /* Peek at the next token. */
14488 token
= cp_lexer_peek_token (parser
->lexer
);
14490 /* If we're looking at a keyword, things are easy. */
14491 switch (token
->keyword
)
14495 decl_specs
->explicit_char_p
= true;
14496 type
= char_type_node
;
14499 type
= char16_type_node
;
14502 type
= char32_type_node
;
14505 type
= wchar_type_node
;
14508 type
= boolean_type_node
;
14511 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14512 type
= short_integer_type_node
;
14516 decl_specs
->explicit_int_p
= true;
14517 type
= integer_type_node
;
14520 if (!int128_integer_type_node
)
14523 decl_specs
->explicit_int128_p
= true;
14524 type
= int128_integer_type_node
;
14528 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14529 type
= long_integer_type_node
;
14532 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14533 type
= integer_type_node
;
14536 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14537 type
= unsigned_type_node
;
14540 type
= float_type_node
;
14543 type
= double_type_node
;
14546 type
= void_type_node
;
14550 maybe_warn_cpp0x (CPP0X_AUTO
);
14551 if (parser
->auto_is_implicit_function_template_parm_p
)
14553 type
= synthesize_implicit_template_parm (parser
);
14555 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14557 if (cxx_dialect
< cxx1y
)
14558 pedwarn (location_of (type
), 0,
14559 "use of %<auto%> in lambda parameter declaration "
14560 "only available with "
14561 "-std=c++1y or -std=gnu++1y");
14563 else if (cxx_dialect
< cxx1y
)
14564 pedwarn (location_of (type
), 0,
14565 "use of %<auto%> in parameter declaration "
14566 "only available with "
14567 "-std=c++1y or -std=gnu++1y");
14569 pedwarn (location_of (type
), OPT_Wpedantic
,
14570 "ISO C++ forbids use of %<auto%> in parameter "
14574 type
= make_auto ();
14578 /* Since DR 743, decltype can either be a simple-type-specifier by
14579 itself or begin a nested-name-specifier. Parsing it will replace
14580 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14581 handling below decide what to do. */
14582 cp_parser_decltype (parser
);
14583 cp_lexer_set_token_position (parser
->lexer
, token
);
14587 /* Consume the `typeof' token. */
14588 cp_lexer_consume_token (parser
->lexer
);
14589 /* Parse the operand to `typeof'. */
14590 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14591 /* If it is not already a TYPE, take its type. */
14592 if (!TYPE_P (type
))
14593 type
= finish_typeof (type
);
14596 cp_parser_set_decl_spec_type (decl_specs
, type
,
14598 /*type_definition_p=*/false);
14602 case RID_UNDERLYING_TYPE
:
14603 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14605 cp_parser_set_decl_spec_type (decl_specs
, type
,
14607 /*type_definition_p=*/false);
14612 case RID_DIRECT_BASES
:
14613 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14615 cp_parser_set_decl_spec_type (decl_specs
, type
,
14617 /*type_definition_p=*/false);
14623 /* If token is an already-parsed decltype not followed by ::,
14624 it's a simple-type-specifier. */
14625 if (token
->type
== CPP_DECLTYPE
14626 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
14628 type
= token
->u
.value
;
14630 cp_parser_set_decl_spec_type (decl_specs
, type
,
14632 /*type_definition_p=*/false);
14633 cp_lexer_consume_token (parser
->lexer
);
14637 /* If the type-specifier was for a built-in type, we're done. */
14640 /* Record the type. */
14642 && (token
->keyword
!= RID_SIGNED
14643 && token
->keyword
!= RID_UNSIGNED
14644 && token
->keyword
!= RID_SHORT
14645 && token
->keyword
!= RID_LONG
))
14646 cp_parser_set_decl_spec_type (decl_specs
,
14649 /*type_definition_p=*/false);
14651 decl_specs
->any_specifiers_p
= true;
14653 /* Consume the token. */
14654 cp_lexer_consume_token (parser
->lexer
);
14656 /* There is no valid C++ program where a non-template type is
14657 followed by a "<". That usually indicates that the user thought
14658 that the type was a template. */
14659 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
14662 return TYPE_NAME (type
);
14665 /* The type-specifier must be a user-defined type. */
14666 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
14671 /* Don't gobble tokens or issue error messages if this is an
14672 optional type-specifier. */
14673 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
14674 cp_parser_parse_tentatively (parser
);
14676 /* Look for the optional `::' operator. */
14678 = (cp_parser_global_scope_opt (parser
,
14679 /*current_scope_valid_p=*/false)
14681 /* Look for the nested-name specifier. */
14683 = (cp_parser_nested_name_specifier_opt (parser
,
14684 /*typename_keyword_p=*/false,
14685 /*check_dependency_p=*/true,
14687 /*is_declaration=*/false)
14689 token
= cp_lexer_peek_token (parser
->lexer
);
14690 /* If we have seen a nested-name-specifier, and the next token
14691 is `template', then we are using the template-id production. */
14693 && cp_parser_optional_template_keyword (parser
))
14695 /* Look for the template-id. */
14696 type
= cp_parser_template_id (parser
,
14697 /*template_keyword_p=*/true,
14698 /*check_dependency_p=*/true,
14700 /*is_declaration=*/false);
14701 /* If the template-id did not name a type, we are out of
14703 if (TREE_CODE (type
) != TYPE_DECL
)
14705 cp_parser_error (parser
, "expected template-id for type");
14709 /* Otherwise, look for a type-name. */
14711 type
= cp_parser_type_name (parser
);
14712 /* Keep track of all name-lookups performed in class scopes. */
14716 && TREE_CODE (type
) == TYPE_DECL
14717 && identifier_p (DECL_NAME (type
)))
14718 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
14719 /* If it didn't work out, we don't have a TYPE. */
14720 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
14721 && !cp_parser_parse_definitely (parser
))
14723 if (type
&& decl_specs
)
14724 cp_parser_set_decl_spec_type (decl_specs
, type
,
14726 /*type_definition_p=*/false);
14729 /* If we didn't get a type-name, issue an error message. */
14730 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14732 cp_parser_error (parser
, "expected type-name");
14733 return error_mark_node
;
14736 if (type
&& type
!= error_mark_node
)
14738 /* See if TYPE is an Objective-C type, and if so, parse and
14739 accept any protocol references following it. Do this before
14740 the cp_parser_check_for_invalid_template_id() call, because
14741 Objective-C types can be followed by '<...>' which would
14742 enclose protocol names rather than template arguments, and so
14743 everything is fine. */
14744 if (c_dialect_objc () && !parser
->scope
14745 && (objc_is_id (type
) || objc_is_class_name (type
)))
14747 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14748 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
14750 /* Clobber the "unqualified" type previously entered into
14751 DECL_SPECS with the new, improved protocol-qualified version. */
14753 decl_specs
->type
= qual_type
;
14758 /* There is no valid C++ program where a non-template type is
14759 followed by a "<". That usually indicates that the user
14760 thought that the type was a template. */
14761 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
14769 /* Parse a type-name.
14775 simple-template-id [in c++0x]
14783 Returns a TYPE_DECL for the type. */
14786 cp_parser_type_name (cp_parser
* parser
)
14790 /* We can't know yet whether it is a class-name or not. */
14791 cp_parser_parse_tentatively (parser
);
14792 /* Try a class-name. */
14793 type_decl
= cp_parser_class_name (parser
,
14794 /*typename_keyword_p=*/false,
14795 /*template_keyword_p=*/false,
14797 /*check_dependency_p=*/true,
14798 /*class_head_p=*/false,
14799 /*is_declaration=*/false);
14800 /* If it's not a class-name, keep looking. */
14801 if (!cp_parser_parse_definitely (parser
))
14803 if (cxx_dialect
< cxx11
)
14804 /* It must be a typedef-name or an enum-name. */
14805 return cp_parser_nonclass_name (parser
);
14807 cp_parser_parse_tentatively (parser
);
14808 /* It is either a simple-template-id representing an
14809 instantiation of an alias template... */
14810 type_decl
= cp_parser_template_id (parser
,
14811 /*template_keyword_p=*/false,
14812 /*check_dependency_p=*/true,
14814 /*is_declaration=*/false);
14815 /* Note that this must be an instantiation of an alias template
14816 because [temp.names]/6 says:
14818 A template-id that names an alias template specialization
14821 Whereas [temp.names]/7 says:
14823 A simple-template-id that names a class template
14824 specialization is a class-name. */
14825 if (type_decl
!= NULL_TREE
14826 && TREE_CODE (type_decl
) == TYPE_DECL
14827 && TYPE_DECL_ALIAS_P (type_decl
))
14828 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
14830 cp_parser_simulate_error (parser
);
14832 if (!cp_parser_parse_definitely (parser
))
14833 /* ... Or a typedef-name or an enum-name. */
14834 return cp_parser_nonclass_name (parser
);
14840 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14848 Returns a TYPE_DECL for the type. */
14851 cp_parser_nonclass_name (cp_parser
* parser
)
14856 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14857 identifier
= cp_parser_identifier (parser
);
14858 if (identifier
== error_mark_node
)
14859 return error_mark_node
;
14861 /* Look up the type-name. */
14862 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
14864 type_decl
= strip_using_decl (type_decl
);
14866 if (TREE_CODE (type_decl
) != TYPE_DECL
14867 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
14869 /* See if this is an Objective-C type. */
14870 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14871 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
14873 type_decl
= TYPE_NAME (type
);
14876 /* Issue an error if we did not find a type-name. */
14877 if (TREE_CODE (type_decl
) != TYPE_DECL
14878 /* In Objective-C, we have the complication that class names are
14879 normally type names and start declarations (eg, the
14880 "NSObject" in "NSObject *object;"), but can be used in an
14881 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14882 is an expression. So, a classname followed by a dot is not a
14883 valid type-name. */
14884 || (objc_is_class_name (TREE_TYPE (type_decl
))
14885 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
14887 if (!cp_parser_simulate_error (parser
))
14888 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
14889 NLE_TYPE
, token
->location
);
14890 return error_mark_node
;
14892 /* Remember that the name was used in the definition of the
14893 current class so that we can check later to see if the
14894 meaning would have been different after the class was
14895 entirely defined. */
14896 else if (type_decl
!= error_mark_node
14898 maybe_note_name_used_in_class (identifier
, type_decl
);
14903 /* Parse an elaborated-type-specifier. Note that the grammar given
14904 here incorporates the resolution to DR68.
14906 elaborated-type-specifier:
14907 class-key :: [opt] nested-name-specifier [opt] identifier
14908 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14909 enum-key :: [opt] nested-name-specifier [opt] identifier
14910 typename :: [opt] nested-name-specifier identifier
14911 typename :: [opt] nested-name-specifier template [opt]
14916 elaborated-type-specifier:
14917 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14918 class-key attributes :: [opt] nested-name-specifier [opt]
14919 template [opt] template-id
14920 enum attributes :: [opt] nested-name-specifier [opt] identifier
14922 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14923 declared `friend'. If IS_DECLARATION is TRUE, then this
14924 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14925 something is being declared.
14927 Returns the TYPE specified. */
14930 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
14932 bool is_declaration
)
14934 enum tag_types tag_type
;
14936 tree type
= NULL_TREE
;
14937 tree attributes
= NULL_TREE
;
14939 cp_token
*token
= NULL
;
14941 /* See if we're looking at the `enum' keyword. */
14942 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
14944 /* Consume the `enum' token. */
14945 cp_lexer_consume_token (parser
->lexer
);
14946 /* Remember that it's an enumeration type. */
14947 tag_type
= enum_type
;
14948 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14949 enums) is used here. */
14950 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14951 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14953 pedwarn (input_location
, 0, "elaborated-type-specifier "
14954 "for a scoped enum must not use the %<%D%> keyword",
14955 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
14956 /* Consume the `struct' or `class' and parse it anyway. */
14957 cp_lexer_consume_token (parser
->lexer
);
14959 /* Parse the attributes. */
14960 attributes
= cp_parser_attributes_opt (parser
);
14962 /* Or, it might be `typename'. */
14963 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14966 /* Consume the `typename' token. */
14967 cp_lexer_consume_token (parser
->lexer
);
14968 /* Remember that it's a `typename' type. */
14969 tag_type
= typename_type
;
14971 /* Otherwise it must be a class-key. */
14974 tag_type
= cp_parser_class_key (parser
);
14975 if (tag_type
== none_type
)
14976 return error_mark_node
;
14977 /* Parse the attributes. */
14978 attributes
= cp_parser_attributes_opt (parser
);
14981 /* Look for the `::' operator. */
14982 globalscope
= cp_parser_global_scope_opt (parser
,
14983 /*current_scope_valid_p=*/false);
14984 /* Look for the nested-name-specifier. */
14985 if (tag_type
== typename_type
&& !globalscope
)
14987 if (!cp_parser_nested_name_specifier (parser
,
14988 /*typename_keyword_p=*/true,
14989 /*check_dependency_p=*/true,
14992 return error_mark_node
;
14995 /* Even though `typename' is not present, the proposed resolution
14996 to Core Issue 180 says that in `class A<T>::B', `B' should be
14997 considered a type-name, even if `A<T>' is dependent. */
14998 cp_parser_nested_name_specifier_opt (parser
,
14999 /*typename_keyword_p=*/true,
15000 /*check_dependency_p=*/true,
15003 /* For everything but enumeration types, consider a template-id.
15004 For an enumeration type, consider only a plain identifier. */
15005 if (tag_type
!= enum_type
)
15007 bool template_p
= false;
15010 /* Allow the `template' keyword. */
15011 template_p
= cp_parser_optional_template_keyword (parser
);
15012 /* If we didn't see `template', we don't know if there's a
15013 template-id or not. */
15015 cp_parser_parse_tentatively (parser
);
15016 /* Parse the template-id. */
15017 token
= cp_lexer_peek_token (parser
->lexer
);
15018 decl
= cp_parser_template_id (parser
, template_p
,
15019 /*check_dependency_p=*/true,
15022 /* If we didn't find a template-id, look for an ordinary
15024 if (!template_p
&& !cp_parser_parse_definitely (parser
))
15026 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15027 in effect, then we must assume that, upon instantiation, the
15028 template will correspond to a class. */
15029 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15030 && tag_type
== typename_type
)
15031 type
= make_typename_type (parser
->scope
, decl
,
15033 /*complain=*/tf_error
);
15034 /* If the `typename' keyword is in effect and DECL is not a type
15035 decl, then type is non existent. */
15036 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
15038 else if (TREE_CODE (decl
) == TYPE_DECL
)
15039 type
= check_elaborated_type_specifier (tag_type
, decl
,
15040 /*allow_template_p=*/true);
15041 else if (decl
== error_mark_node
)
15042 type
= error_mark_node
;
15047 token
= cp_lexer_peek_token (parser
->lexer
);
15048 identifier
= cp_parser_identifier (parser
);
15050 if (identifier
== error_mark_node
)
15052 parser
->scope
= NULL_TREE
;
15053 return error_mark_node
;
15056 /* For a `typename', we needn't call xref_tag. */
15057 if (tag_type
== typename_type
15058 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
15059 return cp_parser_make_typename_type (parser
, parser
->scope
,
15062 /* Look up a qualified name in the usual way. */
15066 tree ambiguous_decls
;
15068 decl
= cp_parser_lookup_name (parser
, identifier
,
15070 /*is_template=*/false,
15071 /*is_namespace=*/false,
15072 /*check_dependency=*/true,
15076 /* If the lookup was ambiguous, an error will already have been
15078 if (ambiguous_decls
)
15079 return error_mark_node
;
15081 /* If we are parsing friend declaration, DECL may be a
15082 TEMPLATE_DECL tree node here. However, we need to check
15083 whether this TEMPLATE_DECL results in valid code. Consider
15084 the following example:
15087 template <class T> class C {};
15090 template <class T> friend class N::C; // #1, valid code
15092 template <class T> class Y {
15093 friend class N::C; // #2, invalid code
15096 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15097 name lookup of `N::C'. We see that friend declaration must
15098 be template for the code to be valid. Note that
15099 processing_template_decl does not work here since it is
15100 always 1 for the above two cases. */
15102 decl
= (cp_parser_maybe_treat_template_as_class
15103 (decl
, /*tag_name_p=*/is_friend
15104 && parser
->num_template_parameter_lists
));
15106 if (TREE_CODE (decl
) != TYPE_DECL
)
15108 cp_parser_diagnose_invalid_type_name (parser
,
15112 return error_mark_node
;
15115 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15117 bool allow_template
= (parser
->num_template_parameter_lists
15118 || DECL_SELF_REFERENCE_P (decl
));
15119 type
= check_elaborated_type_specifier (tag_type
, decl
,
15122 if (type
== error_mark_node
)
15123 return error_mark_node
;
15126 /* Forward declarations of nested types, such as
15131 are invalid unless all components preceding the final '::'
15132 are complete. If all enclosing types are complete, these
15133 declarations become merely pointless.
15135 Invalid forward declarations of nested types are errors
15136 caught elsewhere in parsing. Those that are pointless arrive
15139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15140 && !is_friend
&& !processing_explicit_instantiation
)
15141 warning (0, "declaration %qD does not declare anything", decl
);
15143 type
= TREE_TYPE (decl
);
15147 /* An elaborated-type-specifier sometimes introduces a new type and
15148 sometimes names an existing type. Normally, the rule is that it
15149 introduces a new type only if there is not an existing type of
15150 the same name already in scope. For example, given:
15153 void f() { struct S s; }
15155 the `struct S' in the body of `f' is the same `struct S' as in
15156 the global scope; the existing definition is used. However, if
15157 there were no global declaration, this would introduce a new
15158 local class named `S'.
15160 An exception to this rule applies to the following code:
15162 namespace N { struct S; }
15164 Here, the elaborated-type-specifier names a new type
15165 unconditionally; even if there is already an `S' in the
15166 containing scope this declaration names a new type.
15167 This exception only applies if the elaborated-type-specifier
15168 forms the complete declaration:
15172 A declaration consisting solely of `class-key identifier ;' is
15173 either a redeclaration of the name in the current scope or a
15174 forward declaration of the identifier as a class name. It
15175 introduces the name into the current scope.
15177 We are in this situation precisely when the next token is a `;'.
15179 An exception to the exception is that a `friend' declaration does
15180 *not* name a new type; i.e., given:
15182 struct S { friend struct T; };
15184 `T' is not a new type in the scope of `S'.
15186 Also, `new struct S' or `sizeof (struct S)' never results in the
15187 definition of a new type; a new type can only be declared in a
15188 declaration context. */
15194 /* Friends have special name lookup rules. */
15195 ts
= ts_within_enclosing_non_class
;
15196 else if (is_declaration
15197 && cp_lexer_next_token_is (parser
->lexer
,
15199 /* This is a `class-key identifier ;' */
15205 (parser
->num_template_parameter_lists
15206 && (cp_parser_next_token_starts_class_definition_p (parser
)
15207 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15208 /* An unqualified name was used to reference this type, so
15209 there were no qualifying templates. */
15210 if (!cp_parser_check_template_parameters (parser
,
15211 /*num_templates=*/0,
15213 /*declarator=*/NULL
))
15214 return error_mark_node
;
15215 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15219 if (type
== error_mark_node
)
15220 return error_mark_node
;
15222 /* Allow attributes on forward declarations of classes. */
15225 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15226 warning (OPT_Wattributes
,
15227 "attributes ignored on uninstantiated type");
15228 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15229 && ! processing_explicit_instantiation
)
15230 warning (OPT_Wattributes
,
15231 "attributes ignored on template instantiation");
15232 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15233 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15235 warning (OPT_Wattributes
,
15236 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15239 if (tag_type
!= enum_type
)
15241 /* Indicate whether this class was declared as a `class' or as a
15243 if (TREE_CODE (type
) == RECORD_TYPE
)
15244 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15245 cp_parser_check_class_key (tag_type
, type
);
15248 /* A "<" cannot follow an elaborated type specifier. If that
15249 happens, the user was probably trying to form a template-id. */
15250 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15256 /* Parse an enum-specifier.
15259 enum-head { enumerator-list [opt] }
15260 enum-head { enumerator-list , } [C++0x]
15263 enum-key identifier [opt] enum-base [opt]
15264 enum-key nested-name-specifier identifier enum-base [opt]
15269 enum struct [C++0x]
15272 : type-specifier-seq
15274 opaque-enum-specifier:
15275 enum-key identifier enum-base [opt] ;
15278 enum-key attributes[opt] identifier [opt] enum-base [opt]
15279 { enumerator-list [opt] }attributes[opt]
15280 enum-key attributes[opt] identifier [opt] enum-base [opt]
15281 { enumerator-list, }attributes[opt] [C++0x]
15283 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15284 if the token stream isn't an enum-specifier after all. */
15287 cp_parser_enum_specifier (cp_parser
* parser
)
15290 tree type
= NULL_TREE
;
15292 tree nested_name_specifier
= NULL_TREE
;
15294 bool scoped_enum_p
= false;
15295 bool has_underlying_type
= false;
15296 bool nested_being_defined
= false;
15297 bool new_value_list
= false;
15298 bool is_new_type
= false;
15299 bool is_anonymous
= false;
15300 tree underlying_type
= NULL_TREE
;
15301 cp_token
*type_start_token
= NULL
;
15302 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15304 parser
->colon_corrects_to_scope_p
= false;
15306 /* Parse tentatively so that we can back up if we don't find a
15308 cp_parser_parse_tentatively (parser
);
15310 /* Caller guarantees that the current token is 'enum', an identifier
15311 possibly follows, and the token after that is an opening brace.
15312 If we don't have an identifier, fabricate an anonymous name for
15313 the enumeration being defined. */
15314 cp_lexer_consume_token (parser
->lexer
);
15316 /* Parse the "class" or "struct", which indicates a scoped
15317 enumeration type in C++0x. */
15318 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15319 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15321 if (cxx_dialect
< cxx11
)
15322 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15324 /* Consume the `struct' or `class' token. */
15325 cp_lexer_consume_token (parser
->lexer
);
15327 scoped_enum_p
= true;
15330 attributes
= cp_parser_attributes_opt (parser
);
15332 /* Clear the qualification. */
15333 parser
->scope
= NULL_TREE
;
15334 parser
->qualifying_scope
= NULL_TREE
;
15335 parser
->object_scope
= NULL_TREE
;
15337 /* Figure out in what scope the declaration is being placed. */
15338 prev_scope
= current_scope ();
15340 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15342 push_deferring_access_checks (dk_no_check
);
15343 nested_name_specifier
15344 = cp_parser_nested_name_specifier_opt (parser
,
15345 /*typename_keyword_p=*/true,
15346 /*check_dependency_p=*/false,
15348 /*is_declaration=*/false);
15350 if (nested_name_specifier
)
15354 identifier
= cp_parser_identifier (parser
);
15355 name
= cp_parser_lookup_name (parser
, identifier
,
15357 /*is_template=*/false,
15358 /*is_namespace=*/false,
15359 /*check_dependency=*/true,
15360 /*ambiguous_decls=*/NULL
,
15362 if (name
&& name
!= error_mark_node
)
15364 type
= TREE_TYPE (name
);
15365 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15367 /* Are template enums allowed in ISO? */
15368 if (template_parm_scope_p ())
15369 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15370 "%qD is an enumeration template", name
);
15371 /* ignore a typename reference, for it will be solved by name
15376 else if (nested_name_specifier
== error_mark_node
)
15377 /* We already issued an error. */;
15379 error_at (type_start_token
->location
,
15380 "%qD is not an enumerator-name", identifier
);
15384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15385 identifier
= cp_parser_identifier (parser
);
15388 identifier
= make_anon_name ();
15389 is_anonymous
= true;
15391 error_at (type_start_token
->location
,
15392 "anonymous scoped enum is not allowed");
15395 pop_deferring_access_checks ();
15397 /* Check for the `:' that denotes a specified underlying type in C++0x.
15398 Note that a ':' could also indicate a bitfield width, however. */
15399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15401 cp_decl_specifier_seq type_specifiers
;
15403 /* Consume the `:'. */
15404 cp_lexer_consume_token (parser
->lexer
);
15406 /* Parse the type-specifier-seq. */
15407 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15408 /*is_trailing_return=*/false,
15411 /* At this point this is surely not elaborated type specifier. */
15412 if (!cp_parser_parse_definitely (parser
))
15415 if (cxx_dialect
< cxx11
)
15416 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15418 has_underlying_type
= true;
15420 /* If that didn't work, stop. */
15421 if (type_specifiers
.type
!= error_mark_node
)
15423 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15424 /*initialized=*/0, NULL
);
15425 if (underlying_type
== error_mark_node
15426 || check_for_bare_parameter_packs (underlying_type
))
15427 underlying_type
= NULL_TREE
;
15431 /* Look for the `{' but don't consume it yet. */
15432 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15434 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15436 cp_parser_error (parser
, "expected %<{%>");
15437 if (has_underlying_type
)
15443 /* An opaque-enum-specifier must have a ';' here. */
15444 if ((scoped_enum_p
|| underlying_type
)
15445 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15447 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15448 if (has_underlying_type
)
15456 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15459 if (nested_name_specifier
)
15461 if (CLASS_TYPE_P (nested_name_specifier
))
15463 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15464 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15465 push_scope (nested_name_specifier
);
15467 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15469 push_nested_namespace (nested_name_specifier
);
15473 /* Issue an error message if type-definitions are forbidden here. */
15474 if (!cp_parser_check_type_definition (parser
))
15475 type
= error_mark_node
;
15477 /* Create the new type. We do this before consuming the opening
15478 brace so the enum will be recorded as being on the line of its
15479 tag (or the 'enum' keyword, if there is no tag). */
15480 type
= start_enum (identifier
, type
, underlying_type
,
15481 scoped_enum_p
, &is_new_type
);
15483 /* If the next token is not '{' it is an opaque-enum-specifier or an
15484 elaborated-type-specifier. */
15485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15487 timevar_push (TV_PARSE_ENUM
);
15488 if (nested_name_specifier
15489 && nested_name_specifier
!= error_mark_node
)
15491 /* The following catches invalid code such as:
15492 enum class S<int>::E { A, B, C }; */
15493 if (!processing_specialization
15494 && CLASS_TYPE_P (nested_name_specifier
)
15495 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15496 error_at (type_start_token
->location
, "cannot add an enumerator "
15497 "list to a template instantiation");
15499 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
15501 error_at (type_start_token
->location
,
15502 "%<%T::%E%> has not been declared",
15503 TYPE_CONTEXT (nested_name_specifier
),
15504 nested_name_specifier
);
15505 type
= error_mark_node
;
15507 /* If that scope does not contain the scope in which the
15508 class was originally declared, the program is invalid. */
15509 else if (prev_scope
&& !is_ancestor (prev_scope
,
15510 nested_name_specifier
))
15512 if (at_namespace_scope_p ())
15513 error_at (type_start_token
->location
,
15514 "declaration of %qD in namespace %qD which does not "
15516 type
, prev_scope
, nested_name_specifier
);
15518 error_at (type_start_token
->location
,
15519 "declaration of %qD in %qD which does not "
15521 type
, prev_scope
, nested_name_specifier
);
15522 type
= error_mark_node
;
15527 begin_scope (sk_scoped_enum
, type
);
15529 /* Consume the opening brace. */
15530 cp_lexer_consume_token (parser
->lexer
);
15532 if (type
== error_mark_node
)
15533 ; /* Nothing to add */
15534 else if (OPAQUE_ENUM_P (type
)
15535 || (cxx_dialect
> cxx98
&& processing_specialization
))
15537 new_value_list
= true;
15538 SET_OPAQUE_ENUM_P (type
, false);
15539 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15543 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
15544 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15545 "previous definition here");
15546 type
= error_mark_node
;
15549 if (type
== error_mark_node
)
15550 cp_parser_skip_to_end_of_block_or_statement (parser
);
15551 /* If the next token is not '}', then there are some enumerators. */
15552 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15554 if (is_anonymous
&& !scoped_enum_p
)
15555 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15556 "ISO C++ forbids empty anonymous enum");
15559 cp_parser_enumerator_list (parser
, type
);
15561 /* Consume the final '}'. */
15562 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15566 timevar_pop (TV_PARSE_ENUM
);
15570 /* If a ';' follows, then it is an opaque-enum-specifier
15571 and additional restrictions apply. */
15572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15575 error_at (type_start_token
->location
,
15576 "opaque-enum-specifier without name");
15577 else if (nested_name_specifier
)
15578 error_at (type_start_token
->location
,
15579 "opaque-enum-specifier must use a simple identifier");
15583 /* Look for trailing attributes to apply to this enumeration, and
15584 apply them if appropriate. */
15585 if (cp_parser_allow_gnu_extensions_p (parser
))
15587 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15588 trailing_attr
= chainon (trailing_attr
, attributes
);
15589 cplus_decl_attributes (&type
,
15591 (int) ATTR_FLAG_TYPE_IN_PLACE
);
15594 /* Finish up the enumeration. */
15595 if (type
!= error_mark_node
)
15597 if (new_value_list
)
15598 finish_enum_value_list (type
);
15600 finish_enum (type
);
15603 if (nested_name_specifier
)
15605 if (CLASS_TYPE_P (nested_name_specifier
))
15607 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
15608 pop_scope (nested_name_specifier
);
15610 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15612 pop_nested_namespace (nested_name_specifier
);
15616 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
15620 /* Parse an enumerator-list. The enumerators all have the indicated
15624 enumerator-definition
15625 enumerator-list , enumerator-definition */
15628 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
15632 /* Parse an enumerator-definition. */
15633 cp_parser_enumerator_definition (parser
, type
);
15635 /* If the next token is not a ',', we've reached the end of
15637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15639 /* Otherwise, consume the `,' and keep going. */
15640 cp_lexer_consume_token (parser
->lexer
);
15641 /* If the next token is a `}', there is a trailing comma. */
15642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15644 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
15645 pedwarn (input_location
, OPT_Wpedantic
,
15646 "comma at end of enumerator list");
15652 /* Parse an enumerator-definition. The enumerator has the indicated
15655 enumerator-definition:
15657 enumerator = constant-expression
15663 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
15669 /* Save the input location because we are interested in the location
15670 of the identifier and not the location of the explicit value. */
15671 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15673 /* Look for the identifier. */
15674 identifier
= cp_parser_identifier (parser
);
15675 if (identifier
== error_mark_node
)
15678 /* If the next token is an '=', then there is an explicit value. */
15679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15681 /* Consume the `=' token. */
15682 cp_lexer_consume_token (parser
->lexer
);
15683 /* Parse the value. */
15684 value
= cp_parser_constant_expression (parser
,
15685 /*allow_non_constant_p=*/false,
15691 /* If we are processing a template, make sure the initializer of the
15692 enumerator doesn't contain any bare template parameter pack. */
15693 if (check_for_bare_parameter_packs (value
))
15694 value
= error_mark_node
;
15696 /* integral_constant_value will pull out this expression, so make sure
15697 it's folded as appropriate. */
15698 value
= fold_non_dependent_expr (value
);
15700 /* Create the enumerator. */
15701 build_enumerator (identifier
, value
, type
, loc
);
15704 /* Parse a namespace-name.
15707 original-namespace-name
15710 Returns the NAMESPACE_DECL for the namespace. */
15713 cp_parser_namespace_name (cp_parser
* parser
)
15716 tree namespace_decl
;
15718 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15720 /* Get the name of the namespace. */
15721 identifier
= cp_parser_identifier (parser
);
15722 if (identifier
== error_mark_node
)
15723 return error_mark_node
;
15725 /* Look up the identifier in the currently active scope. Look only
15726 for namespaces, due to:
15728 [basic.lookup.udir]
15730 When looking up a namespace-name in a using-directive or alias
15731 definition, only namespace names are considered.
15735 [basic.lookup.qual]
15737 During the lookup of a name preceding the :: scope resolution
15738 operator, object, function, and enumerator names are ignored.
15740 (Note that cp_parser_qualifying_entity only calls this
15741 function if the token after the name is the scope resolution
15743 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
15745 /*is_template=*/false,
15746 /*is_namespace=*/true,
15747 /*check_dependency=*/true,
15748 /*ambiguous_decls=*/NULL
,
15750 /* If it's not a namespace, issue an error. */
15751 if (namespace_decl
== error_mark_node
15752 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
15754 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15755 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
15756 cp_parser_error (parser
, "expected namespace-name");
15757 namespace_decl
= error_mark_node
;
15760 return namespace_decl
;
15763 /* Parse a namespace-definition.
15765 namespace-definition:
15766 named-namespace-definition
15767 unnamed-namespace-definition
15769 named-namespace-definition:
15770 original-namespace-definition
15771 extension-namespace-definition
15773 original-namespace-definition:
15774 namespace identifier { namespace-body }
15776 extension-namespace-definition:
15777 namespace original-namespace-name { namespace-body }
15779 unnamed-namespace-definition:
15780 namespace { namespace-body } */
15783 cp_parser_namespace_definition (cp_parser
* parser
)
15785 tree identifier
, attribs
;
15786 bool has_visibility
;
15789 cp_ensure_no_omp_declare_simd (parser
);
15790 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
15792 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
15794 cp_lexer_consume_token (parser
->lexer
);
15799 /* Look for the `namespace' keyword. */
15800 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15802 /* Get the name of the namespace. We do not attempt to distinguish
15803 between an original-namespace-definition and an
15804 extension-namespace-definition at this point. The semantic
15805 analysis routines are responsible for that. */
15806 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15807 identifier
= cp_parser_identifier (parser
);
15809 identifier
= NULL_TREE
;
15811 /* Parse any specified attributes. */
15812 attribs
= cp_parser_attributes_opt (parser
);
15814 /* Look for the `{' to start the namespace. */
15815 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
15816 /* Start the namespace. */
15817 push_namespace (identifier
);
15819 /* "inline namespace" is equivalent to a stub namespace definition
15820 followed by a strong using directive. */
15823 tree name_space
= current_namespace
;
15824 /* Set up namespace association. */
15825 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
15826 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
15827 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
15828 /* Import the contents of the inline namespace. */
15830 do_using_directive (name_space
);
15831 push_namespace (identifier
);
15834 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
15836 /* Parse the body of the namespace. */
15837 cp_parser_namespace_body (parser
);
15839 if (has_visibility
)
15840 pop_visibility (1);
15842 /* Finish the namespace. */
15844 /* Look for the final `}'. */
15845 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15848 /* Parse a namespace-body.
15851 declaration-seq [opt] */
15854 cp_parser_namespace_body (cp_parser
* parser
)
15856 cp_parser_declaration_seq_opt (parser
);
15859 /* Parse a namespace-alias-definition.
15861 namespace-alias-definition:
15862 namespace identifier = qualified-namespace-specifier ; */
15865 cp_parser_namespace_alias_definition (cp_parser
* parser
)
15868 tree namespace_specifier
;
15870 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15872 /* Look for the `namespace' keyword. */
15873 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15874 /* Look for the identifier. */
15875 identifier
= cp_parser_identifier (parser
);
15876 if (identifier
== error_mark_node
)
15878 /* Look for the `=' token. */
15879 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
15880 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15882 error_at (token
->location
, "%<namespace%> definition is not allowed here");
15883 /* Skip the definition. */
15884 cp_lexer_consume_token (parser
->lexer
);
15885 if (cp_parser_skip_to_closing_brace (parser
))
15886 cp_lexer_consume_token (parser
->lexer
);
15889 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15890 /* Look for the qualified-namespace-specifier. */
15891 namespace_specifier
15892 = cp_parser_qualified_namespace_specifier (parser
);
15893 /* Look for the `;' token. */
15894 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15896 /* Register the alias in the symbol table. */
15897 do_namespace_alias (identifier
, namespace_specifier
);
15900 /* Parse a qualified-namespace-specifier.
15902 qualified-namespace-specifier:
15903 :: [opt] nested-name-specifier [opt] namespace-name
15905 Returns a NAMESPACE_DECL corresponding to the specified
15909 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
15911 /* Look for the optional `::'. */
15912 cp_parser_global_scope_opt (parser
,
15913 /*current_scope_valid_p=*/false);
15915 /* Look for the optional nested-name-specifier. */
15916 cp_parser_nested_name_specifier_opt (parser
,
15917 /*typename_keyword_p=*/false,
15918 /*check_dependency_p=*/true,
15920 /*is_declaration=*/true);
15922 return cp_parser_namespace_name (parser
);
15925 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15926 access declaration.
15929 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15930 using :: unqualified-id ;
15932 access-declaration:
15938 cp_parser_using_declaration (cp_parser
* parser
,
15939 bool access_declaration_p
)
15942 bool typename_p
= false;
15943 bool global_scope_p
;
15947 int oldcount
= errorcount
;
15948 cp_token
*diag_token
= NULL
;
15950 if (access_declaration_p
)
15952 diag_token
= cp_lexer_peek_token (parser
->lexer
);
15953 cp_parser_parse_tentatively (parser
);
15957 /* Look for the `using' keyword. */
15958 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15960 /* Peek at the next token. */
15961 token
= cp_lexer_peek_token (parser
->lexer
);
15962 /* See if it's `typename'. */
15963 if (token
->keyword
== RID_TYPENAME
)
15965 /* Remember that we've seen it. */
15967 /* Consume the `typename' token. */
15968 cp_lexer_consume_token (parser
->lexer
);
15972 /* Look for the optional global scope qualification. */
15974 = (cp_parser_global_scope_opt (parser
,
15975 /*current_scope_valid_p=*/false)
15978 /* If we saw `typename', or didn't see `::', then there must be a
15979 nested-name-specifier present. */
15980 if (typename_p
|| !global_scope_p
)
15982 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
15983 /*check_dependency_p=*/true,
15985 /*is_declaration=*/true);
15986 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
15988 cp_parser_skip_to_end_of_block_or_statement (parser
);
15992 /* Otherwise, we could be in either of the two productions. In that
15993 case, treat the nested-name-specifier as optional. */
15995 qscope
= cp_parser_nested_name_specifier_opt (parser
,
15996 /*typename_keyword_p=*/false,
15997 /*check_dependency_p=*/true,
15999 /*is_declaration=*/true);
16001 qscope
= global_namespace
;
16003 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
16004 /* Something has already gone wrong; there's no need to parse
16005 further. Since an error has occurred, the return value of
16006 cp_parser_parse_definitely will be false, as required. */
16007 return cp_parser_parse_definitely (parser
);
16009 token
= cp_lexer_peek_token (parser
->lexer
);
16010 /* Parse the unqualified-id. */
16011 identifier
= cp_parser_unqualified_id (parser
,
16012 /*template_keyword_p=*/false,
16013 /*check_dependency_p=*/true,
16014 /*declarator_p=*/true,
16015 /*optional_p=*/false);
16017 if (access_declaration_p
)
16019 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16020 cp_parser_simulate_error (parser
);
16021 if (!cp_parser_parse_definitely (parser
))
16025 /* The function we call to handle a using-declaration is different
16026 depending on what scope we are in. */
16027 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
16029 else if (!identifier_p (identifier
)
16030 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
16031 /* [namespace.udecl]
16033 A using declaration shall not name a template-id. */
16034 error_at (token
->location
,
16035 "a template-id may not appear in a using-declaration");
16038 if (at_class_scope_p ())
16040 /* Create the USING_DECL. */
16041 decl
= do_class_using_decl (parser
->scope
, identifier
);
16043 if (decl
&& typename_p
)
16044 USING_DECL_TYPENAME_P (decl
) = 1;
16046 if (check_for_bare_parameter_packs (decl
))
16048 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16052 /* Add it to the list of members in this class. */
16053 finish_member_declaration (decl
);
16057 decl
= cp_parser_lookup_name_simple (parser
,
16060 if (decl
== error_mark_node
)
16061 cp_parser_name_lookup_error (parser
, identifier
,
16064 else if (check_for_bare_parameter_packs (decl
))
16066 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16069 else if (!at_namespace_scope_p ())
16070 do_local_using_decl (decl
, qscope
, identifier
);
16072 do_toplevel_using_decl (decl
, qscope
, identifier
);
16076 /* Look for the final `;'. */
16077 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16079 if (access_declaration_p
&& errorcount
== oldcount
)
16080 warning_at (diag_token
->location
, OPT_Wdeprecated
,
16081 "access declarations are deprecated "
16082 "in favour of using-declarations; "
16083 "suggestion: add the %<using%> keyword");
16088 /* Parse an alias-declaration.
16091 using identifier attribute-specifier-seq [opt] = type-id */
16094 cp_parser_alias_declaration (cp_parser
* parser
)
16096 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
16097 location_t id_location
;
16098 cp_declarator
*declarator
;
16099 cp_decl_specifier_seq decl_specs
;
16101 const char *saved_message
= NULL
;
16103 /* Look for the `using' keyword. */
16104 cp_token
*using_token
16105 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16106 if (using_token
== NULL
)
16107 return error_mark_node
;
16109 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
16110 id
= cp_parser_identifier (parser
);
16111 if (id
== error_mark_node
)
16112 return error_mark_node
;
16114 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
16115 attributes
= cp_parser_attributes_opt (parser
);
16116 if (attributes
== error_mark_node
)
16117 return error_mark_node
;
16119 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16121 if (cp_parser_error_occurred (parser
))
16122 return error_mark_node
;
16124 cp_parser_commit_to_tentative_parse (parser
);
16126 /* Now we are going to parse the type-id of the declaration. */
16131 "A type-specifier-seq shall not define a class or enumeration
16132 unless it appears in the type-id of an alias-declaration (7.1.3) that
16133 is not the declaration of a template-declaration."
16135 In other words, if we currently are in an alias template, the
16136 type-id should not define a type.
16138 So let's set parser->type_definition_forbidden_message in that
16139 case; cp_parser_check_type_definition (called by
16140 cp_parser_class_specifier) will then emit an error if a type is
16141 defined in the type-id. */
16142 if (parser
->num_template_parameter_lists
)
16144 saved_message
= parser
->type_definition_forbidden_message
;
16145 parser
->type_definition_forbidden_message
=
16146 G_("types may not be defined in alias template declarations");
16149 type
= cp_parser_type_id (parser
);
16151 /* Restore the error message if need be. */
16152 if (parser
->num_template_parameter_lists
)
16153 parser
->type_definition_forbidden_message
= saved_message
;
16155 if (type
== error_mark_node
16156 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
16158 cp_parser_skip_to_end_of_block_or_statement (parser
);
16159 return error_mark_node
;
16162 /* A typedef-name can also be introduced by an alias-declaration. The
16163 identifier following the using keyword becomes a typedef-name. It has
16164 the same semantics as if it were introduced by the typedef
16165 specifier. In particular, it does not define a new type and it shall
16166 not appear in the type-id. */
16168 clear_decl_specs (&decl_specs
);
16169 decl_specs
.type
= type
;
16170 if (attributes
!= NULL_TREE
)
16172 decl_specs
.attributes
= attributes
;
16173 set_and_check_decl_spec_loc (&decl_specs
,
16177 set_and_check_decl_spec_loc (&decl_specs
,
16180 set_and_check_decl_spec_loc (&decl_specs
,
16184 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16185 declarator
->id_loc
= id_location
;
16187 member_p
= at_class_scope_p ();
16189 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16190 NULL_TREE
, attributes
);
16192 decl
= start_decl (declarator
, &decl_specs
, 0,
16193 attributes
, NULL_TREE
, &pushed_scope
);
16194 if (decl
== error_mark_node
)
16197 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16200 pop_scope (pushed_scope
);
16202 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16203 added into the symbol table; otherwise, return the TYPE_DECL. */
16204 if (DECL_LANG_SPECIFIC (decl
)
16205 && DECL_TEMPLATE_INFO (decl
)
16206 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16208 decl
= DECL_TI_TEMPLATE (decl
);
16210 check_member_template (decl
);
16216 /* Parse a using-directive.
16219 using namespace :: [opt] nested-name-specifier [opt]
16220 namespace-name ; */
16223 cp_parser_using_directive (cp_parser
* parser
)
16225 tree namespace_decl
;
16228 /* Look for the `using' keyword. */
16229 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16230 /* And the `namespace' keyword. */
16231 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16232 /* Look for the optional `::' operator. */
16233 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16234 /* And the optional nested-name-specifier. */
16235 cp_parser_nested_name_specifier_opt (parser
,
16236 /*typename_keyword_p=*/false,
16237 /*check_dependency_p=*/true,
16239 /*is_declaration=*/true);
16240 /* Get the namespace being used. */
16241 namespace_decl
= cp_parser_namespace_name (parser
);
16242 /* And any specified attributes. */
16243 attribs
= cp_parser_attributes_opt (parser
);
16244 /* Update the symbol table. */
16245 parse_using_directive (namespace_decl
, attribs
);
16246 /* Look for the final `;'. */
16247 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16250 /* Parse an asm-definition.
16253 asm ( string-literal ) ;
16258 asm volatile [opt] ( string-literal ) ;
16259 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16260 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16261 : asm-operand-list [opt] ) ;
16262 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16263 : asm-operand-list [opt]
16264 : asm-clobber-list [opt] ) ;
16265 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16266 : asm-clobber-list [opt]
16267 : asm-goto-list ) ; */
16270 cp_parser_asm_definition (cp_parser
* parser
)
16273 tree outputs
= NULL_TREE
;
16274 tree inputs
= NULL_TREE
;
16275 tree clobbers
= NULL_TREE
;
16276 tree labels
= NULL_TREE
;
16278 bool volatile_p
= false;
16279 bool extended_p
= false;
16280 bool invalid_inputs_p
= false;
16281 bool invalid_outputs_p
= false;
16282 bool goto_p
= false;
16283 required_token missing
= RT_NONE
;
16285 /* Look for the `asm' keyword. */
16286 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16287 /* See if the next token is `volatile'. */
16288 if (cp_parser_allow_gnu_extensions_p (parser
)
16289 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16291 /* Remember that we saw the `volatile' keyword. */
16293 /* Consume the token. */
16294 cp_lexer_consume_token (parser
->lexer
);
16296 if (cp_parser_allow_gnu_extensions_p (parser
)
16297 && parser
->in_function_body
16298 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16300 /* Remember that we saw the `goto' keyword. */
16302 /* Consume the token. */
16303 cp_lexer_consume_token (parser
->lexer
);
16305 /* Look for the opening `('. */
16306 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16308 /* Look for the string. */
16309 string
= cp_parser_string_literal (parser
, false, false);
16310 if (string
== error_mark_node
)
16312 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16313 /*consume_paren=*/true);
16317 /* If we're allowing GNU extensions, check for the extended assembly
16318 syntax. Unfortunately, the `:' tokens need not be separated by
16319 a space in C, and so, for compatibility, we tolerate that here
16320 too. Doing that means that we have to treat the `::' operator as
16322 if (cp_parser_allow_gnu_extensions_p (parser
)
16323 && parser
->in_function_body
16324 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16325 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16327 bool inputs_p
= false;
16328 bool clobbers_p
= false;
16329 bool labels_p
= false;
16331 /* The extended syntax was used. */
16334 /* Look for outputs. */
16335 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16337 /* Consume the `:'. */
16338 cp_lexer_consume_token (parser
->lexer
);
16339 /* Parse the output-operands. */
16340 if (cp_lexer_next_token_is_not (parser
->lexer
,
16342 && cp_lexer_next_token_is_not (parser
->lexer
,
16344 && cp_lexer_next_token_is_not (parser
->lexer
,
16347 outputs
= cp_parser_asm_operand_list (parser
);
16349 if (outputs
== error_mark_node
)
16350 invalid_outputs_p
= true;
16352 /* If the next token is `::', there are no outputs, and the
16353 next token is the beginning of the inputs. */
16354 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16355 /* The inputs are coming next. */
16358 /* Look for inputs. */
16360 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16362 /* Consume the `:' or `::'. */
16363 cp_lexer_consume_token (parser
->lexer
);
16364 /* Parse the output-operands. */
16365 if (cp_lexer_next_token_is_not (parser
->lexer
,
16367 && cp_lexer_next_token_is_not (parser
->lexer
,
16369 && cp_lexer_next_token_is_not (parser
->lexer
,
16371 inputs
= cp_parser_asm_operand_list (parser
);
16373 if (inputs
== error_mark_node
)
16374 invalid_inputs_p
= true;
16376 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16377 /* The clobbers are coming next. */
16380 /* Look for clobbers. */
16382 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16385 /* Consume the `:' or `::'. */
16386 cp_lexer_consume_token (parser
->lexer
);
16387 /* Parse the clobbers. */
16388 if (cp_lexer_next_token_is_not (parser
->lexer
,
16390 && cp_lexer_next_token_is_not (parser
->lexer
,
16392 clobbers
= cp_parser_asm_clobber_list (parser
);
16395 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16396 /* The labels are coming next. */
16399 /* Look for labels. */
16401 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16404 /* Consume the `:' or `::'. */
16405 cp_lexer_consume_token (parser
->lexer
);
16406 /* Parse the labels. */
16407 labels
= cp_parser_asm_label_list (parser
);
16410 if (goto_p
&& !labels_p
)
16411 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16414 missing
= RT_COLON_SCOPE
;
16416 /* Look for the closing `)'. */
16417 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16418 missing
? missing
: RT_CLOSE_PAREN
))
16419 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16420 /*consume_paren=*/true);
16421 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16423 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16425 /* Create the ASM_EXPR. */
16426 if (parser
->in_function_body
)
16428 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16429 inputs
, clobbers
, labels
);
16430 /* If the extended syntax was not used, mark the ASM_EXPR. */
16433 tree temp
= asm_stmt
;
16434 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16435 temp
= TREE_OPERAND (temp
, 0);
16437 ASM_INPUT_P (temp
) = 1;
16441 add_asm_node (string
);
16445 /* Declarators [gram.dcl.decl] */
16447 /* Parse an init-declarator.
16450 declarator initializer [opt]
16455 declarator asm-specification [opt] attributes [opt] initializer [opt]
16457 function-definition:
16458 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16460 decl-specifier-seq [opt] declarator function-try-block
16464 function-definition:
16465 __extension__ function-definition
16469 function-definition:
16470 decl-specifier-seq [opt] declarator function-transaction-block
16472 The DECL_SPECIFIERS apply to this declarator. Returns a
16473 representation of the entity declared. If MEMBER_P is TRUE, then
16474 this declarator appears in a class scope. The new DECL created by
16475 this declarator is returned.
16477 The CHECKS are access checks that should be performed once we know
16478 what entity is being declared (and, therefore, what classes have
16481 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16482 for a function-definition here as well. If the declarator is a
16483 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16484 be TRUE upon return. By that point, the function-definition will
16485 have been completely parsed.
16487 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16490 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16491 parsed declaration if it is an uninitialized single declarator not followed
16492 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16493 if present, will not be consumed. If returned, this declarator will be
16494 created with SD_INITIALIZED but will not call cp_finish_decl. */
16497 cp_parser_init_declarator (cp_parser
* parser
,
16498 cp_decl_specifier_seq
*decl_specifiers
,
16499 vec
<deferred_access_check
, va_gc
> *checks
,
16500 bool function_definition_allowed_p
,
16502 int declares_class_or_enum
,
16503 bool* function_definition_p
,
16504 tree
* maybe_range_for_decl
)
16506 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16507 *attributes_start_token
= NULL
;
16508 cp_declarator
*declarator
;
16509 tree prefix_attributes
;
16510 tree attributes
= NULL
;
16511 tree asm_specification
;
16513 tree decl
= NULL_TREE
;
16515 int is_initialized
;
16516 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16517 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16519 enum cpp_ttype initialization_kind
;
16520 bool is_direct_init
= false;
16521 bool is_non_constant_init
;
16522 int ctor_dtor_or_conv_p
;
16524 tree pushed_scope
= NULL_TREE
;
16525 bool range_for_decl_p
= false;
16526 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16528 /* Gather the attributes that were provided with the
16529 decl-specifiers. */
16530 prefix_attributes
= decl_specifiers
->attributes
;
16532 /* Assume that this is not the declarator for a function
16534 if (function_definition_p
)
16535 *function_definition_p
= false;
16537 /* Default arguments are only permitted for function parameters. */
16538 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16539 parser
->default_arg_ok_p
= false;
16541 /* Defer access checks while parsing the declarator; we cannot know
16542 what names are accessible until we know what is being
16544 resume_deferring_access_checks ();
16546 /* Parse the declarator. */
16547 token
= cp_lexer_peek_token (parser
->lexer
);
16549 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16550 &ctor_dtor_or_conv_p
,
16551 /*parenthesized_p=*/NULL
,
16553 /* Gather up the deferred checks. */
16554 stop_deferring_access_checks ();
16556 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16558 /* If the DECLARATOR was erroneous, there's no need to go
16560 if (declarator
== cp_error_declarator
)
16561 return error_mark_node
;
16563 /* Check that the number of template-parameter-lists is OK. */
16564 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16566 return error_mark_node
;
16568 if (declares_class_or_enum
& 2)
16569 cp_parser_check_for_definition_in_return_type (declarator
,
16570 decl_specifiers
->type
,
16571 decl_specifiers
->locations
[ds_type_spec
]);
16573 /* Figure out what scope the entity declared by the DECLARATOR is
16574 located in. `grokdeclarator' sometimes changes the scope, so
16575 we compute it now. */
16576 scope
= get_scope_of_declarator (declarator
);
16578 /* Perform any lookups in the declared type which were thought to be
16579 dependent, but are not in the scope of the declarator. */
16580 decl_specifiers
->type
16581 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
16583 /* If we're allowing GNU extensions, look for an
16584 asm-specification. */
16585 if (cp_parser_allow_gnu_extensions_p (parser
))
16587 /* Look for an asm-specification. */
16588 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
16589 asm_specification
= cp_parser_asm_specification_opt (parser
);
16592 asm_specification
= NULL_TREE
;
16594 /* Look for attributes. */
16595 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
16596 attributes
= cp_parser_attributes_opt (parser
);
16598 /* Peek at the next token. */
16599 token
= cp_lexer_peek_token (parser
->lexer
);
16601 if (function_declarator_p (declarator
))
16603 /* Check to see if the token indicates the start of a
16604 function-definition. */
16605 if (cp_parser_token_starts_function_definition_p (token
))
16607 if (!function_definition_allowed_p
)
16609 /* If a function-definition should not appear here, issue an
16611 cp_parser_error (parser
,
16612 "a function-definition is not allowed here");
16613 return error_mark_node
;
16616 location_t func_brace_location
16617 = cp_lexer_peek_token (parser
->lexer
)->location
;
16619 /* Neither attributes nor an asm-specification are allowed
16620 on a function-definition. */
16621 if (asm_specification
)
16622 error_at (asm_spec_start_token
->location
,
16623 "an asm-specification is not allowed "
16624 "on a function-definition");
16626 error_at (attributes_start_token
->location
,
16627 "attributes are not allowed "
16628 "on a function-definition");
16629 /* This is a function-definition. */
16630 *function_definition_p
= true;
16632 /* Parse the function definition. */
16634 decl
= cp_parser_save_member_function_body (parser
,
16637 prefix_attributes
);
16640 (cp_parser_function_definition_from_specifiers_and_declarator
16641 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
16643 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
16645 /* This is where the prologue starts... */
16646 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
16647 = func_brace_location
;
16656 Only in function declarations for constructors, destructors, and
16657 type conversions can the decl-specifier-seq be omitted.
16659 We explicitly postpone this check past the point where we handle
16660 function-definitions because we tolerate function-definitions
16661 that are missing their return types in some modes. */
16662 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
16664 cp_parser_error (parser
,
16665 "expected constructor, destructor, or type conversion");
16666 return error_mark_node
;
16669 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16670 if (token
->type
== CPP_EQ
16671 || token
->type
== CPP_OPEN_PAREN
16672 || token
->type
== CPP_OPEN_BRACE
)
16674 is_initialized
= SD_INITIALIZED
;
16675 initialization_kind
= token
->type
;
16676 if (maybe_range_for_decl
)
16677 *maybe_range_for_decl
= error_mark_node
;
16679 if (token
->type
== CPP_EQ
16680 && function_declarator_p (declarator
))
16682 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16683 if (t2
->keyword
== RID_DEFAULT
)
16684 is_initialized
= SD_DEFAULTED
;
16685 else if (t2
->keyword
== RID_DELETE
)
16686 is_initialized
= SD_DELETED
;
16691 /* If the init-declarator isn't initialized and isn't followed by a
16692 `,' or `;', it's not a valid init-declarator. */
16693 if (token
->type
!= CPP_COMMA
16694 && token
->type
!= CPP_SEMICOLON
)
16696 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
16697 range_for_decl_p
= true;
16700 cp_parser_error (parser
, "expected initializer");
16701 return error_mark_node
;
16704 is_initialized
= SD_UNINITIALIZED
;
16705 initialization_kind
= CPP_EOF
;
16708 /* Because start_decl has side-effects, we should only call it if we
16709 know we're going ahead. By this point, we know that we cannot
16710 possibly be looking at any other construct. */
16711 cp_parser_commit_to_tentative_parse (parser
);
16713 /* If the decl specifiers were bad, issue an error now that we're
16714 sure this was intended to be a declarator. Then continue
16715 declaring the variable(s), as int, to try to cut down on further
16717 if (decl_specifiers
->any_specifiers_p
16718 && decl_specifiers
->type
== error_mark_node
)
16720 cp_parser_error (parser
, "invalid type in declaration");
16721 decl_specifiers
->type
= integer_type_node
;
16724 /* Check to see whether or not this declaration is a friend. */
16725 friend_p
= cp_parser_friend_p (decl_specifiers
);
16727 /* Enter the newly declared entry in the symbol table. If we're
16728 processing a declaration in a class-specifier, we wait until
16729 after processing the initializer. */
16732 if (parser
->in_unbraced_linkage_specification_p
)
16733 decl_specifiers
->storage_class
= sc_extern
;
16734 decl
= start_decl (declarator
, decl_specifiers
,
16735 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
16736 attributes
, prefix_attributes
, &pushed_scope
);
16737 cp_finalize_omp_declare_simd (parser
, decl
);
16738 /* Adjust location of decl if declarator->id_loc is more appropriate:
16739 set, and decl wasn't merged with another decl, in which case its
16740 location would be different from input_location, and more accurate. */
16742 && declarator
->id_loc
!= UNKNOWN_LOCATION
16743 && DECL_SOURCE_LOCATION (decl
) == input_location
)
16744 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
16747 /* Enter the SCOPE. That way unqualified names appearing in the
16748 initializer will be looked up in SCOPE. */
16749 pushed_scope
= push_scope (scope
);
16751 /* Perform deferred access control checks, now that we know in which
16752 SCOPE the declared entity resides. */
16753 if (!member_p
&& decl
)
16755 tree saved_current_function_decl
= NULL_TREE
;
16757 /* If the entity being declared is a function, pretend that we
16758 are in its scope. If it is a `friend', it may have access to
16759 things that would not otherwise be accessible. */
16760 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16762 saved_current_function_decl
= current_function_decl
;
16763 current_function_decl
= decl
;
16766 /* Perform access checks for template parameters. */
16767 cp_parser_perform_template_parameter_access_checks (checks
);
16769 /* Perform the access control checks for the declarator and the
16770 decl-specifiers. */
16771 perform_deferred_access_checks (tf_warning_or_error
);
16773 /* Restore the saved value. */
16774 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16775 current_function_decl
= saved_current_function_decl
;
16778 /* Parse the initializer. */
16779 initializer
= NULL_TREE
;
16780 is_direct_init
= false;
16781 is_non_constant_init
= true;
16782 if (is_initialized
)
16784 if (function_declarator_p (declarator
))
16786 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
16787 if (initialization_kind
== CPP_EQ
)
16788 initializer
= cp_parser_pure_specifier (parser
);
16791 /* If the declaration was erroneous, we don't really
16792 know what the user intended, so just silently
16793 consume the initializer. */
16794 if (decl
!= error_mark_node
)
16795 error_at (initializer_start_token
->location
,
16796 "initializer provided for function");
16797 cp_parser_skip_to_closing_parenthesis (parser
,
16798 /*recovering=*/true,
16799 /*or_comma=*/false,
16800 /*consume_paren=*/true);
16805 /* We want to record the extra mangling scope for in-class
16806 initializers of class members and initializers of static data
16807 member templates. The former involves deferring
16808 parsing of the initializer until end of class as with default
16809 arguments. So right here we only handle the latter. */
16810 if (!member_p
&& processing_template_decl
)
16811 start_lambda_scope (decl
);
16812 initializer
= cp_parser_initializer (parser
,
16814 &is_non_constant_init
);
16815 if (!member_p
&& processing_template_decl
)
16816 finish_lambda_scope ();
16817 if (initializer
== error_mark_node
)
16818 cp_parser_skip_to_end_of_statement (parser
);
16822 /* The old parser allows attributes to appear after a parenthesized
16823 initializer. Mark Mitchell proposed removing this functionality
16824 on the GCC mailing lists on 2002-08-13. This parser accepts the
16825 attributes -- but ignores them. */
16826 if (cp_parser_allow_gnu_extensions_p (parser
)
16827 && initialization_kind
== CPP_OPEN_PAREN
)
16828 if (cp_parser_attributes_opt (parser
))
16829 warning (OPT_Wattributes
,
16830 "attributes after parenthesized initializer ignored");
16832 /* A non-template declaration involving a function parameter list containing
16833 an implicit template parameter will have been made into a template. If it
16834 turns out that the resulting declaration is not an actual function then
16835 finish the template declaration here. An error message will already have
16837 if (parser
->fully_implicit_function_template_p
)
16838 if (!function_declarator_p (declarator
))
16842 pop_scope (pushed_scope
);
16845 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
16848 /* For an in-class declaration, use `grokfield' to create the
16854 pop_scope (pushed_scope
);
16855 pushed_scope
= NULL_TREE
;
16857 decl
= grokfield (declarator
, decl_specifiers
,
16858 initializer
, !is_non_constant_init
,
16859 /*asmspec=*/NULL_TREE
,
16860 chainon (attributes
, prefix_attributes
));
16861 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16862 cp_parser_save_default_args (parser
, decl
);
16863 cp_finalize_omp_declare_simd (parser
, decl
);
16866 /* Finish processing the declaration. But, skip member
16868 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
16870 cp_finish_decl (decl
,
16871 initializer
, !is_non_constant_init
,
16873 /* If the initializer is in parentheses, then this is
16874 a direct-initialization, which means that an
16875 `explicit' constructor is OK. Otherwise, an
16876 `explicit' constructor cannot be used. */
16877 ((is_direct_init
|| !is_initialized
)
16878 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
16880 else if ((cxx_dialect
!= cxx98
) && friend_p
16881 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16882 /* Core issue #226 (C++0x only): A default template-argument
16883 shall not be specified in a friend class template
16885 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
16886 /*is_partial=*/false, /*is_friend_decl=*/1);
16888 if (!friend_p
&& pushed_scope
)
16889 pop_scope (pushed_scope
);
16891 if (function_declarator_p (declarator
)
16892 && parser
->fully_implicit_function_template_p
)
16895 decl
= finish_fully_implicit_template (parser
, decl
);
16897 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
16903 /* Parse a declarator.
16907 ptr-operator declarator
16909 abstract-declarator:
16910 ptr-operator abstract-declarator [opt]
16911 direct-abstract-declarator
16916 attributes [opt] direct-declarator
16917 attributes [opt] ptr-operator declarator
16919 abstract-declarator:
16920 attributes [opt] ptr-operator abstract-declarator [opt]
16921 attributes [opt] direct-abstract-declarator
16923 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16924 detect constructor, destructor or conversion operators. It is set
16925 to -1 if the declarator is a name, and +1 if it is a
16926 function. Otherwise it is set to zero. Usually you just want to
16927 test for >0, but internally the negative value is used.
16929 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16930 a decl-specifier-seq unless it declares a constructor, destructor,
16931 or conversion. It might seem that we could check this condition in
16932 semantic analysis, rather than parsing, but that makes it difficult
16933 to handle something like `f()'. We want to notice that there are
16934 no decl-specifiers, and therefore realize that this is an
16935 expression, not a declaration.)
16937 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16938 the declarator is a direct-declarator of the form "(...)".
16940 MEMBER_P is true iff this declarator is a member-declarator. */
16942 static cp_declarator
*
16943 cp_parser_declarator (cp_parser
* parser
,
16944 cp_parser_declarator_kind dcl_kind
,
16945 int* ctor_dtor_or_conv_p
,
16946 bool* parenthesized_p
,
16949 cp_declarator
*declarator
;
16950 enum tree_code code
;
16951 cp_cv_quals cv_quals
;
16953 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
16955 /* Assume this is not a constructor, destructor, or type-conversion
16957 if (ctor_dtor_or_conv_p
)
16958 *ctor_dtor_or_conv_p
= 0;
16960 if (cp_parser_allow_gnu_extensions_p (parser
))
16961 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
16963 /* Check for the ptr-operator production. */
16964 cp_parser_parse_tentatively (parser
);
16965 /* Parse the ptr-operator. */
16966 code
= cp_parser_ptr_operator (parser
,
16971 /* If that worked, then we have a ptr-operator. */
16972 if (cp_parser_parse_definitely (parser
))
16974 /* If a ptr-operator was found, then this declarator was not
16976 if (parenthesized_p
)
16977 *parenthesized_p
= true;
16978 /* The dependent declarator is optional if we are parsing an
16979 abstract-declarator. */
16980 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16981 cp_parser_parse_tentatively (parser
);
16983 /* Parse the dependent declarator. */
16984 declarator
= cp_parser_declarator (parser
, dcl_kind
,
16985 /*ctor_dtor_or_conv_p=*/NULL
,
16986 /*parenthesized_p=*/NULL
,
16987 /*member_p=*/false);
16989 /* If we are parsing an abstract-declarator, we must handle the
16990 case where the dependent declarator is absent. */
16991 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
16992 && !cp_parser_parse_definitely (parser
))
16995 declarator
= cp_parser_make_indirect_declarator
16996 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
16998 /* Everything else is a direct-declarator. */
17001 if (parenthesized_p
)
17002 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
17004 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
17005 ctor_dtor_or_conv_p
,
17009 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
17010 declarator
->attributes
= gnu_attributes
;
17014 /* Parse a direct-declarator or direct-abstract-declarator.
17018 direct-declarator ( parameter-declaration-clause )
17019 cv-qualifier-seq [opt]
17020 ref-qualifier [opt]
17021 exception-specification [opt]
17022 direct-declarator [ constant-expression [opt] ]
17025 direct-abstract-declarator:
17026 direct-abstract-declarator [opt]
17027 ( parameter-declaration-clause )
17028 cv-qualifier-seq [opt]
17029 ref-qualifier [opt]
17030 exception-specification [opt]
17031 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17032 ( abstract-declarator )
17034 Returns a representation of the declarator. DCL_KIND is
17035 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17036 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17037 we are parsing a direct-declarator. It is
17038 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17039 of ambiguity we prefer an abstract declarator, as per
17040 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17041 cp_parser_declarator. */
17043 static cp_declarator
*
17044 cp_parser_direct_declarator (cp_parser
* parser
,
17045 cp_parser_declarator_kind dcl_kind
,
17046 int* ctor_dtor_or_conv_p
,
17050 cp_declarator
*declarator
= NULL
;
17051 tree scope
= NULL_TREE
;
17052 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17053 bool saved_in_declarator_p
= parser
->in_declarator_p
;
17055 tree pushed_scope
= NULL_TREE
;
17059 /* Peek at the next token. */
17060 token
= cp_lexer_peek_token (parser
->lexer
);
17061 if (token
->type
== CPP_OPEN_PAREN
)
17063 /* This is either a parameter-declaration-clause, or a
17064 parenthesized declarator. When we know we are parsing a
17065 named declarator, it must be a parenthesized declarator
17066 if FIRST is true. For instance, `(int)' is a
17067 parameter-declaration-clause, with an omitted
17068 direct-abstract-declarator. But `((*))', is a
17069 parenthesized abstract declarator. Finally, when T is a
17070 template parameter `(T)' is a
17071 parameter-declaration-clause, and not a parenthesized
17074 We first try and parse a parameter-declaration-clause,
17075 and then try a nested declarator (if FIRST is true).
17077 It is not an error for it not to be a
17078 parameter-declaration-clause, even when FIRST is
17084 The first is the declaration of a function while the
17085 second is the definition of a variable, including its
17088 Having seen only the parenthesis, we cannot know which of
17089 these two alternatives should be selected. Even more
17090 complex are examples like:
17095 The former is a function-declaration; the latter is a
17096 variable initialization.
17098 Thus again, we try a parameter-declaration-clause, and if
17099 that fails, we back out and return. */
17101 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17104 bool is_declarator
= false;
17106 /* In a member-declarator, the only valid interpretation
17107 of a parenthesis is the start of a
17108 parameter-declaration-clause. (It is invalid to
17109 initialize a static data member with a parenthesized
17110 initializer; only the "=" form of initialization is
17113 cp_parser_parse_tentatively (parser
);
17115 /* Consume the `('. */
17116 cp_lexer_consume_token (parser
->lexer
);
17119 /* If this is going to be an abstract declarator, we're
17120 in a declarator and we can't have default args. */
17121 parser
->default_arg_ok_p
= false;
17122 parser
->in_declarator_p
= true;
17125 begin_scope (sk_function_parms
, NULL_TREE
);
17127 /* Parse the parameter-declaration-clause. */
17128 params
= cp_parser_parameter_declaration_clause (parser
);
17130 /* Consume the `)'. */
17131 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17133 /* If all went well, parse the cv-qualifier-seq,
17134 ref-qualifier and the exception-specification. */
17135 if (member_p
|| cp_parser_parse_definitely (parser
))
17137 cp_cv_quals cv_quals
;
17138 cp_virt_specifiers virt_specifiers
;
17139 cp_ref_qualifier ref_qual
;
17140 tree exception_specification
;
17143 bool memfn
= (member_p
|| (pushed_scope
17144 && CLASS_TYPE_P (pushed_scope
)));
17146 is_declarator
= true;
17148 if (ctor_dtor_or_conv_p
)
17149 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17152 /* Parse the cv-qualifier-seq. */
17153 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17154 /* Parse the ref-qualifier. */
17155 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17156 /* And the exception-specification. */
17157 exception_specification
17158 = cp_parser_exception_specification_opt (parser
);
17160 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17162 /* In here, we handle cases where attribute is used after
17163 the function declaration. For example:
17164 void func (int x) __attribute__((vector(..))); */
17166 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
17168 cp_parser_parse_tentatively (parser
);
17169 tree attr
= cp_parser_gnu_attributes_opt (parser
);
17170 if (cp_lexer_next_token_is_not (parser
->lexer
,
17172 && cp_lexer_next_token_is_not (parser
->lexer
,
17174 cp_parser_abort_tentative_parse (parser
);
17175 else if (!cp_parser_parse_definitely (parser
))
17178 attrs
= chainon (attr
, attrs
);
17180 late_return
= (cp_parser_late_return_type_opt
17181 (parser
, declarator
,
17182 memfn
? cv_quals
: -1));
17185 /* Parse the virt-specifier-seq. */
17186 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17188 /* Create the function-declarator. */
17189 declarator
= make_call_declarator (declarator
,
17194 exception_specification
,
17196 declarator
->std_attributes
= attrs
;
17197 /* Any subsequent parameter lists are to do with
17198 return type, so are not those of the declared
17200 parser
->default_arg_ok_p
= false;
17203 /* Remove the function parms from scope. */
17204 pop_bindings_and_leave_scope ();
17207 /* Repeat the main loop. */
17211 /* If this is the first, we can try a parenthesized
17215 bool saved_in_type_id_in_expr_p
;
17217 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17218 parser
->in_declarator_p
= saved_in_declarator_p
;
17220 /* Consume the `('. */
17221 cp_lexer_consume_token (parser
->lexer
);
17222 /* Parse the nested declarator. */
17223 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17224 parser
->in_type_id_in_expr_p
= true;
17226 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17227 /*parenthesized_p=*/NULL
,
17229 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17231 /* Expect a `)'. */
17232 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17233 declarator
= cp_error_declarator
;
17234 if (declarator
== cp_error_declarator
)
17237 goto handle_declarator
;
17239 /* Otherwise, we must be done. */
17243 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17244 && token
->type
== CPP_OPEN_SQUARE
17245 && !cp_next_tokens_can_be_attribute_p (parser
))
17247 /* Parse an array-declarator. */
17248 tree bounds
, attrs
;
17250 if (ctor_dtor_or_conv_p
)
17251 *ctor_dtor_or_conv_p
= 0;
17254 parser
->default_arg_ok_p
= false;
17255 parser
->in_declarator_p
= true;
17256 /* Consume the `['. */
17257 cp_lexer_consume_token (parser
->lexer
);
17258 /* Peek at the next token. */
17259 token
= cp_lexer_peek_token (parser
->lexer
);
17260 /* If the next token is `]', then there is no
17261 constant-expression. */
17262 if (token
->type
!= CPP_CLOSE_SQUARE
)
17264 bool non_constant_p
;
17266 = cp_parser_constant_expression (parser
,
17267 /*allow_non_constant=*/true,
17269 if (!non_constant_p
)
17271 else if (error_operand_p (bounds
))
17272 /* Already gave an error. */;
17273 else if (!parser
->in_function_body
17274 || current_binding_level
->kind
== sk_function_parms
)
17276 /* Normally, the array bound must be an integral constant
17277 expression. However, as an extension, we allow VLAs
17278 in function scopes as long as they aren't part of a
17279 parameter declaration. */
17280 cp_parser_error (parser
,
17281 "array bound is not an integer constant");
17282 bounds
= error_mark_node
;
17284 else if (processing_template_decl
17285 && !type_dependent_expression_p (bounds
))
17287 /* Remember this wasn't a constant-expression. */
17288 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17289 TREE_SIDE_EFFECTS (bounds
) = 1;
17293 bounds
= NULL_TREE
;
17294 /* Look for the closing `]'. */
17295 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17297 declarator
= cp_error_declarator
;
17301 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17302 declarator
= make_array_declarator (declarator
, bounds
);
17303 declarator
->std_attributes
= attrs
;
17305 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17308 tree qualifying_scope
;
17309 tree unqualified_name
;
17311 special_function_kind sfk
;
17313 bool pack_expansion_p
= false;
17314 cp_token
*declarator_id_start_token
;
17316 /* Parse a declarator-id */
17317 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17320 cp_parser_parse_tentatively (parser
);
17322 /* If we see an ellipsis, we should be looking at a
17324 if (token
->type
== CPP_ELLIPSIS
)
17326 /* Consume the `...' */
17327 cp_lexer_consume_token (parser
->lexer
);
17329 pack_expansion_p
= true;
17333 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17335 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17336 qualifying_scope
= parser
->scope
;
17341 if (!unqualified_name
&& pack_expansion_p
)
17343 /* Check whether an error occurred. */
17344 okay
= !cp_parser_error_occurred (parser
);
17346 /* We already consumed the ellipsis to mark a
17347 parameter pack, but we have no way to report it,
17348 so abort the tentative parse. We will be exiting
17349 immediately anyway. */
17350 cp_parser_abort_tentative_parse (parser
);
17353 okay
= cp_parser_parse_definitely (parser
);
17356 unqualified_name
= error_mark_node
;
17357 else if (unqualified_name
17358 && (qualifying_scope
17359 || (!identifier_p (unqualified_name
))))
17361 cp_parser_error (parser
, "expected unqualified-id");
17362 unqualified_name
= error_mark_node
;
17366 if (!unqualified_name
)
17368 if (unqualified_name
== error_mark_node
)
17370 declarator
= cp_error_declarator
;
17371 pack_expansion_p
= false;
17372 declarator
->parameter_pack_p
= false;
17376 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17378 if (qualifying_scope
&& at_namespace_scope_p ()
17379 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17381 /* In the declaration of a member of a template class
17382 outside of the class itself, the SCOPE will sometimes
17383 be a TYPENAME_TYPE. For example, given:
17385 template <typename T>
17386 int S<T>::R::i = 3;
17388 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17389 this context, we must resolve S<T>::R to an ordinary
17390 type, rather than a typename type.
17392 The reason we normally avoid resolving TYPENAME_TYPEs
17393 is that a specialization of `S' might render
17394 `S<T>::R' not a type. However, if `S' is
17395 specialized, then this `i' will not be used, so there
17396 is no harm in resolving the types here. */
17399 /* Resolve the TYPENAME_TYPE. */
17400 type
= resolve_typename_type (qualifying_scope
,
17401 /*only_current_p=*/false);
17402 /* If that failed, the declarator is invalid. */
17403 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17405 if (typedef_variant_p (type
))
17406 error_at (declarator_id_start_token
->location
,
17407 "cannot define member of dependent typedef "
17410 error_at (declarator_id_start_token
->location
,
17411 "%<%T::%E%> is not a type",
17412 TYPE_CONTEXT (qualifying_scope
),
17413 TYPE_IDENTIFIER (qualifying_scope
));
17415 qualifying_scope
= type
;
17420 if (unqualified_name
)
17424 if (qualifying_scope
17425 && CLASS_TYPE_P (qualifying_scope
))
17426 class_type
= qualifying_scope
;
17428 class_type
= current_class_type
;
17430 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17432 tree name_type
= TREE_TYPE (unqualified_name
);
17433 if (class_type
&& same_type_p (name_type
, class_type
))
17435 if (qualifying_scope
17436 && CLASSTYPE_USE_TEMPLATE (name_type
))
17438 error_at (declarator_id_start_token
->location
,
17439 "invalid use of constructor as a template");
17440 inform (declarator_id_start_token
->location
,
17441 "use %<%T::%D%> instead of %<%T::%D%> to "
17442 "name the constructor in a qualified name",
17444 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17445 class_type
, name_type
);
17446 declarator
= cp_error_declarator
;
17450 unqualified_name
= constructor_name (class_type
);
17454 /* We do not attempt to print the declarator
17455 here because we do not have enough
17456 information about its original syntactic
17458 cp_parser_error (parser
, "invalid declarator");
17459 declarator
= cp_error_declarator
;
17466 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17467 sfk
= sfk_destructor
;
17468 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17469 sfk
= sfk_conversion
;
17470 else if (/* There's no way to declare a constructor
17471 for an anonymous type, even if the type
17472 got a name for linkage purposes. */
17473 !TYPE_WAS_ANONYMOUS (class_type
)
17474 && constructor_name_p (unqualified_name
,
17477 unqualified_name
= constructor_name (class_type
);
17478 sfk
= sfk_constructor
;
17480 else if (is_overloaded_fn (unqualified_name
)
17481 && DECL_CONSTRUCTOR_P (get_first_fn
17482 (unqualified_name
)))
17483 sfk
= sfk_constructor
;
17485 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17486 *ctor_dtor_or_conv_p
= -1;
17489 declarator
= make_id_declarator (qualifying_scope
,
17492 declarator
->std_attributes
= attrs
;
17493 declarator
->id_loc
= token
->location
;
17494 declarator
->parameter_pack_p
= pack_expansion_p
;
17496 if (pack_expansion_p
)
17497 maybe_warn_variadic_templates ();
17500 handle_declarator
:;
17501 scope
= get_scope_of_declarator (declarator
);
17504 /* Any names that appear after the declarator-id for a
17505 member are looked up in the containing scope. */
17506 if (at_function_scope_p ())
17508 /* But declarations with qualified-ids can't appear in a
17510 cp_parser_error (parser
, "qualified-id in declaration");
17511 declarator
= cp_error_declarator
;
17514 pushed_scope
= push_scope (scope
);
17516 parser
->in_declarator_p
= true;
17517 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17518 || (declarator
&& declarator
->kind
== cdk_id
))
17519 /* Default args are only allowed on function
17521 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17523 parser
->default_arg_ok_p
= false;
17532 /* For an abstract declarator, we might wind up with nothing at this
17533 point. That's an error; the declarator is not optional. */
17535 cp_parser_error (parser
, "expected declarator");
17537 /* If we entered a scope, we must exit it now. */
17539 pop_scope (pushed_scope
);
17541 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17542 parser
->in_declarator_p
= saved_in_declarator_p
;
17547 /* Parse a ptr-operator.
17550 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17551 * cv-qualifier-seq [opt]
17553 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17554 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17559 & cv-qualifier-seq [opt]
17561 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17562 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17563 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17564 filled in with the TYPE containing the member. *CV_QUALS is
17565 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17566 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17567 Note that the tree codes returned by this function have nothing
17568 to do with the types of trees that will be eventually be created
17569 to represent the pointer or reference type being parsed. They are
17570 just constants with suggestive names. */
17571 static enum tree_code
17572 cp_parser_ptr_operator (cp_parser
* parser
,
17574 cp_cv_quals
*cv_quals
,
17577 enum tree_code code
= ERROR_MARK
;
17579 tree attrs
= NULL_TREE
;
17581 /* Assume that it's not a pointer-to-member. */
17583 /* And that there are no cv-qualifiers. */
17584 *cv_quals
= TYPE_UNQUALIFIED
;
17586 /* Peek at the next token. */
17587 token
= cp_lexer_peek_token (parser
->lexer
);
17589 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17590 if (token
->type
== CPP_MULT
)
17591 code
= INDIRECT_REF
;
17592 else if (token
->type
== CPP_AND
)
17594 else if ((cxx_dialect
!= cxx98
) &&
17595 token
->type
== CPP_AND_AND
) /* C++0x only */
17596 code
= NON_LVALUE_EXPR
;
17598 if (code
!= ERROR_MARK
)
17600 /* Consume the `*', `&' or `&&'. */
17601 cp_lexer_consume_token (parser
->lexer
);
17603 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17604 `&', if we are allowing GNU extensions. (The only qualifier
17605 that can legally appear after `&' is `restrict', but that is
17606 enforced during semantic analysis. */
17607 if (code
== INDIRECT_REF
17608 || cp_parser_allow_gnu_extensions_p (parser
))
17609 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17611 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17612 if (attributes
!= NULL
)
17613 *attributes
= attrs
;
17617 /* Try the pointer-to-member case. */
17618 cp_parser_parse_tentatively (parser
);
17619 /* Look for the optional `::' operator. */
17620 cp_parser_global_scope_opt (parser
,
17621 /*current_scope_valid_p=*/false);
17622 /* Look for the nested-name specifier. */
17623 token
= cp_lexer_peek_token (parser
->lexer
);
17624 cp_parser_nested_name_specifier (parser
,
17625 /*typename_keyword_p=*/false,
17626 /*check_dependency_p=*/true,
17628 /*is_declaration=*/false);
17629 /* If we found it, and the next token is a `*', then we are
17630 indeed looking at a pointer-to-member operator. */
17631 if (!cp_parser_error_occurred (parser
)
17632 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
17634 /* Indicate that the `*' operator was used. */
17635 code
= INDIRECT_REF
;
17637 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
17638 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
17639 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
17640 error_at (token
->location
, "cannot form pointer to member of "
17641 "non-class %q#T", parser
->scope
);
17644 /* The type of which the member is a member is given by the
17646 *type
= parser
->scope
;
17647 /* The next name will not be qualified. */
17648 parser
->scope
= NULL_TREE
;
17649 parser
->qualifying_scope
= NULL_TREE
;
17650 parser
->object_scope
= NULL_TREE
;
17651 /* Look for optional c++11 attributes. */
17652 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17653 if (attributes
!= NULL
)
17654 *attributes
= attrs
;
17655 /* Look for the optional cv-qualifier-seq. */
17656 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17659 /* If that didn't work we don't have a ptr-operator. */
17660 if (!cp_parser_parse_definitely (parser
))
17661 cp_parser_error (parser
, "expected ptr-operator");
17667 /* Parse an (optional) cv-qualifier-seq.
17670 cv-qualifier cv-qualifier-seq [opt]
17681 Returns a bitmask representing the cv-qualifiers. */
17684 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
17686 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
17691 cp_cv_quals cv_qualifier
;
17693 /* Peek at the next token. */
17694 token
= cp_lexer_peek_token (parser
->lexer
);
17695 /* See if it's a cv-qualifier. */
17696 switch (token
->keyword
)
17699 cv_qualifier
= TYPE_QUAL_CONST
;
17703 cv_qualifier
= TYPE_QUAL_VOLATILE
;
17707 cv_qualifier
= TYPE_QUAL_RESTRICT
;
17711 cv_qualifier
= TYPE_UNQUALIFIED
;
17718 if (cv_quals
& cv_qualifier
)
17720 error_at (token
->location
, "duplicate cv-qualifier");
17721 cp_lexer_purge_token (parser
->lexer
);
17725 cp_lexer_consume_token (parser
->lexer
);
17726 cv_quals
|= cv_qualifier
;
17733 /* Parse an (optional) ref-qualifier
17739 Returns cp_ref_qualifier representing ref-qualifier. */
17741 static cp_ref_qualifier
17742 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
17744 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
17746 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17747 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
17752 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
17753 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17755 switch (token
->type
)
17758 curr_ref_qual
= REF_QUAL_LVALUE
;
17762 curr_ref_qual
= REF_QUAL_RVALUE
;
17766 curr_ref_qual
= REF_QUAL_NONE
;
17770 if (!curr_ref_qual
)
17774 error_at (token
->location
, "multiple ref-qualifiers");
17775 cp_lexer_purge_token (parser
->lexer
);
17779 ref_qual
= curr_ref_qual
;
17780 cp_lexer_consume_token (parser
->lexer
);
17787 /* Parse an (optional) virt-specifier-seq.
17789 virt-specifier-seq:
17790 virt-specifier virt-specifier-seq [opt]
17796 Returns a bitmask representing the virt-specifiers. */
17798 static cp_virt_specifiers
17799 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
17801 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
17806 cp_virt_specifiers virt_specifier
;
17808 /* Peek at the next token. */
17809 token
= cp_lexer_peek_token (parser
->lexer
);
17810 /* See if it's a virt-specifier-qualifier. */
17811 if (token
->type
!= CPP_NAME
)
17813 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
17815 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17816 virt_specifier
= VIRT_SPEC_OVERRIDE
;
17818 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
17820 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17821 virt_specifier
= VIRT_SPEC_FINAL
;
17823 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
17825 virt_specifier
= VIRT_SPEC_FINAL
;
17830 if (virt_specifiers
& virt_specifier
)
17832 error_at (token
->location
, "duplicate virt-specifier");
17833 cp_lexer_purge_token (parser
->lexer
);
17837 cp_lexer_consume_token (parser
->lexer
);
17838 virt_specifiers
|= virt_specifier
;
17841 return virt_specifiers
;
17844 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17845 is in scope even though it isn't real. */
17848 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
17852 if (current_class_ptr
)
17854 /* We don't clear this between NSDMIs. Is it already what we want? */
17855 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
17856 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
17857 && cp_type_quals (type
) == quals
)
17861 this_parm
= build_this_parm (ctype
, quals
);
17862 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17863 current_class_ptr
= NULL_TREE
;
17865 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
17866 current_class_ptr
= this_parm
;
17869 /* Return true iff our current scope is a non-static data member
17873 parsing_nsdmi (void)
17875 /* We recognize NSDMI context by the context-less 'this' pointer set up
17876 by the function above. */
17877 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
17882 /* Parse a late-specified return type, if any. This is not a separate
17883 non-terminal, but part of a function declarator, which looks like
17885 -> trailing-type-specifier-seq abstract-declarator(opt)
17887 Returns the type indicated by the type-id.
17889 In addition to this this parses any queued up omp declare simd
17890 clauses and Cilk Plus SIMD-enabled function's vector attributes.
17892 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17896 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
17900 tree type
= NULL_TREE
;
17901 bool declare_simd_p
= (parser
->omp_declare_simd
17903 && declarator
->kind
== cdk_id
);
17905 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
17906 && declarator
&& declarator
->kind
== cdk_id
);
17908 /* Peek at the next token. */
17909 token
= cp_lexer_peek_token (parser
->lexer
);
17910 /* A late-specified return type is indicated by an initial '->'. */
17911 if (token
->type
!= CPP_DEREF
&& !(declare_simd_p
|| cilk_simd_fn_vector_p
))
17914 tree save_ccp
= current_class_ptr
;
17915 tree save_ccr
= current_class_ref
;
17918 /* DR 1207: 'this' is in scope in the trailing return type. */
17919 inject_this_parameter (current_class_type
, quals
);
17922 if (token
->type
== CPP_DEREF
)
17924 /* Consume the ->. */
17925 cp_lexer_consume_token (parser
->lexer
);
17927 type
= cp_parser_trailing_type_id (parser
);
17930 if (cilk_simd_fn_vector_p
)
17931 declarator
->std_attributes
17932 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
17933 declarator
->std_attributes
);
17934 if (declare_simd_p
)
17935 declarator
->std_attributes
17936 = cp_parser_late_parsing_omp_declare_simd (parser
,
17937 declarator
->std_attributes
);
17941 current_class_ptr
= save_ccp
;
17942 current_class_ref
= save_ccr
;
17948 /* Parse a declarator-id.
17952 :: [opt] nested-name-specifier [opt] type-name
17954 In the `id-expression' case, the value returned is as for
17955 cp_parser_id_expression if the id-expression was an unqualified-id.
17956 If the id-expression was a qualified-id, then a SCOPE_REF is
17957 returned. The first operand is the scope (either a NAMESPACE_DECL
17958 or TREE_TYPE), but the second is still just a representation of an
17962 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
17965 /* The expression must be an id-expression. Assume that qualified
17966 names are the names of types so that:
17969 int S<T>::R::i = 3;
17971 will work; we must treat `S<T>::R' as the name of a type.
17972 Similarly, assume that qualified names are templates, where
17976 int S<T>::R<T>::i = 3;
17979 id
= cp_parser_id_expression (parser
,
17980 /*template_keyword_p=*/false,
17981 /*check_dependency_p=*/false,
17982 /*template_p=*/NULL
,
17983 /*declarator_p=*/true,
17985 if (id
&& BASELINK_P (id
))
17986 id
= BASELINK_FUNCTIONS (id
);
17990 /* Parse a type-id.
17993 type-specifier-seq abstract-declarator [opt]
17995 Returns the TYPE specified. */
17998 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
17999 bool is_trailing_return
)
18001 cp_decl_specifier_seq type_specifier_seq
;
18002 cp_declarator
*abstract_declarator
;
18004 /* Parse the type-specifier-seq. */
18005 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18006 is_trailing_return
,
18007 &type_specifier_seq
);
18008 if (type_specifier_seq
.type
== error_mark_node
)
18009 return error_mark_node
;
18011 /* There might or might not be an abstract declarator. */
18012 cp_parser_parse_tentatively (parser
);
18013 /* Look for the declarator. */
18014 abstract_declarator
18015 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
18016 /*parenthesized_p=*/NULL
,
18017 /*member_p=*/false);
18018 /* Check to see if there really was a declarator. */
18019 if (!cp_parser_parse_definitely (parser
))
18020 abstract_declarator
= NULL
;
18022 if (type_specifier_seq
.type
18023 /* None of the valid uses of 'auto' in C++14 involve the type-id
18024 nonterminal, but it is valid in a trailing-return-type. */
18025 && !(cxx_dialect
>= cxx1y
&& is_trailing_return
)
18026 && type_uses_auto (type_specifier_seq
.type
))
18028 /* A type-id with type 'auto' is only ok if the abstract declarator
18029 is a function declarator with a late-specified return type. */
18030 if (abstract_declarator
18031 && abstract_declarator
->kind
== cdk_function
18032 && abstract_declarator
->u
.function
.late_return_type
)
18036 error ("invalid use of %<auto%>");
18037 return error_mark_node
;
18041 return groktypename (&type_specifier_seq
, abstract_declarator
,
18045 static tree
cp_parser_type_id (cp_parser
*parser
)
18047 return cp_parser_type_id_1 (parser
, false, false);
18050 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
18053 const char *saved_message
= parser
->type_definition_forbidden_message
;
18054 parser
->type_definition_forbidden_message
18055 = G_("types may not be defined in template arguments");
18056 r
= cp_parser_type_id_1 (parser
, true, false);
18057 parser
->type_definition_forbidden_message
= saved_message
;
18058 if (cxx_dialect
>= cxx1y
&& type_uses_auto (r
))
18060 error ("invalid use of %<auto%> in template argument");
18061 r
= error_mark_node
;
18066 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
18068 return cp_parser_type_id_1 (parser
, false, true);
18071 /* Parse a type-specifier-seq.
18073 type-specifier-seq:
18074 type-specifier type-specifier-seq [opt]
18078 type-specifier-seq:
18079 attributes type-specifier-seq [opt]
18081 If IS_DECLARATION is true, we are at the start of a "condition" or
18082 exception-declaration, so we might be followed by a declarator-id.
18084 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18085 i.e. we've just seen "->".
18087 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18090 cp_parser_type_specifier_seq (cp_parser
* parser
,
18091 bool is_declaration
,
18092 bool is_trailing_return
,
18093 cp_decl_specifier_seq
*type_specifier_seq
)
18095 bool seen_type_specifier
= false;
18096 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
18097 cp_token
*start_token
= NULL
;
18099 /* Clear the TYPE_SPECIFIER_SEQ. */
18100 clear_decl_specs (type_specifier_seq
);
18102 /* In the context of a trailing return type, enum E { } is an
18103 elaborated-type-specifier followed by a function-body, not an
18105 if (is_trailing_return
)
18106 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
18108 /* Parse the type-specifiers and attributes. */
18111 tree type_specifier
;
18112 bool is_cv_qualifier
;
18114 /* Check for attributes first. */
18115 if (cp_next_tokens_can_be_attribute_p (parser
))
18117 type_specifier_seq
->attributes
=
18118 chainon (type_specifier_seq
->attributes
,
18119 cp_parser_attributes_opt (parser
));
18123 /* record the token of the beginning of the type specifier seq,
18124 for error reporting purposes*/
18126 start_token
= cp_lexer_peek_token (parser
->lexer
);
18128 /* Look for the type-specifier. */
18129 type_specifier
= cp_parser_type_specifier (parser
,
18131 type_specifier_seq
,
18132 /*is_declaration=*/false,
18135 if (!type_specifier
)
18137 /* If the first type-specifier could not be found, this is not a
18138 type-specifier-seq at all. */
18139 if (!seen_type_specifier
)
18141 /* Set in_declarator_p to avoid skipping to the semicolon. */
18142 int in_decl
= parser
->in_declarator_p
;
18143 parser
->in_declarator_p
= true;
18145 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
18146 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18147 cp_parser_error (parser
, "expected type-specifier");
18149 parser
->in_declarator_p
= in_decl
;
18151 type_specifier_seq
->type
= error_mark_node
;
18154 /* If subsequent type-specifiers could not be found, the
18155 type-specifier-seq is complete. */
18159 seen_type_specifier
= true;
18160 /* The standard says that a condition can be:
18162 type-specifier-seq declarator = assignment-expression
18169 we should treat the "S" as a declarator, not as a
18170 type-specifier. The standard doesn't say that explicitly for
18171 type-specifier-seq, but it does say that for
18172 decl-specifier-seq in an ordinary declaration. Perhaps it
18173 would be clearer just to allow a decl-specifier-seq here, and
18174 then add a semantic restriction that if any decl-specifiers
18175 that are not type-specifiers appear, the program is invalid. */
18176 if (is_declaration
&& !is_cv_qualifier
)
18177 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18181 /* Return whether the function currently being declared has an associated
18182 template parameter list. */
18185 function_being_declared_is_template_p (cp_parser
* parser
)
18187 if (!current_template_parms
|| processing_template_parmlist
)
18190 if (parser
->implicit_template_scope
)
18193 if (at_class_scope_p ()
18194 && TYPE_BEING_DEFINED (current_class_type
))
18195 return parser
->num_template_parameter_lists
!= 0;
18197 return ((int) parser
->num_template_parameter_lists
> template_class_depth
18198 (current_class_type
));
18201 /* Parse a parameter-declaration-clause.
18203 parameter-declaration-clause:
18204 parameter-declaration-list [opt] ... [opt]
18205 parameter-declaration-list , ...
18207 Returns a representation for the parameter declarations. A return
18208 value of NULL indicates a parameter-declaration-clause consisting
18209 only of an ellipsis. */
18212 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18221 int auto_is_implicit_function_template_parm_p
;
18223 parser
->auto_is_implicit_function_template_parm_p
18224 = auto_is_implicit_function_template_parm_p
;
18226 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18230 if (!processing_specialization
18231 && !processing_template_parmlist
18232 && !processing_explicit_instantiation
)
18233 if (!current_function_decl
18234 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
18235 parser
->auto_is_implicit_function_template_parm_p
= true;
18237 /* Peek at the next token. */
18238 token
= cp_lexer_peek_token (parser
->lexer
);
18239 /* Check for trivial parameter-declaration-clauses. */
18240 if (token
->type
== CPP_ELLIPSIS
)
18242 /* Consume the `...' token. */
18243 cp_lexer_consume_token (parser
->lexer
);
18246 else if (token
->type
== CPP_CLOSE_PAREN
)
18247 /* There are no parameters. */
18249 #ifndef NO_IMPLICIT_EXTERN_C
18250 if (in_system_header_at (input_location
)
18251 && current_class_type
== NULL
18252 && current_lang_name
== lang_name_c
)
18256 return void_list_node
;
18258 /* Check for `(void)', too, which is a special case. */
18259 else if (token
->keyword
== RID_VOID
18260 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18261 == CPP_CLOSE_PAREN
))
18263 /* Consume the `void' token. */
18264 cp_lexer_consume_token (parser
->lexer
);
18265 /* There are no parameters. */
18266 return void_list_node
;
18269 /* Parse the parameter-declaration-list. */
18270 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18271 /* If a parse error occurred while parsing the
18272 parameter-declaration-list, then the entire
18273 parameter-declaration-clause is erroneous. */
18277 /* Peek at the next token. */
18278 token
= cp_lexer_peek_token (parser
->lexer
);
18279 /* If it's a `,', the clause should terminate with an ellipsis. */
18280 if (token
->type
== CPP_COMMA
)
18282 /* Consume the `,'. */
18283 cp_lexer_consume_token (parser
->lexer
);
18284 /* Expect an ellipsis. */
18286 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18288 /* It might also be `...' if the optional trailing `,' was
18290 else if (token
->type
== CPP_ELLIPSIS
)
18292 /* Consume the `...' token. */
18293 cp_lexer_consume_token (parser
->lexer
);
18294 /* And remember that we saw it. */
18298 ellipsis_p
= false;
18300 /* Finish the parameter list. */
18302 parameters
= chainon (parameters
, void_list_node
);
18307 /* Parse a parameter-declaration-list.
18309 parameter-declaration-list:
18310 parameter-declaration
18311 parameter-declaration-list , parameter-declaration
18313 Returns a representation of the parameter-declaration-list, as for
18314 cp_parser_parameter_declaration_clause. However, the
18315 `void_list_node' is never appended to the list. Upon return,
18316 *IS_ERROR will be true iff an error occurred. */
18319 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18321 tree parameters
= NULL_TREE
;
18322 tree
*tail
= ¶meters
;
18323 bool saved_in_unbraced_linkage_specification_p
;
18326 /* Assume all will go well. */
18328 /* The special considerations that apply to a function within an
18329 unbraced linkage specifications do not apply to the parameters
18330 to the function. */
18331 saved_in_unbraced_linkage_specification_p
18332 = parser
->in_unbraced_linkage_specification_p
;
18333 parser
->in_unbraced_linkage_specification_p
= false;
18335 /* Look for more parameters. */
18338 cp_parameter_declarator
*parameter
;
18339 tree decl
= error_mark_node
;
18340 bool parenthesized_p
= false;
18341 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
18342 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18343 (current_template_parms
)) : 0);
18345 /* Parse the parameter. */
18347 = cp_parser_parameter_declaration (parser
,
18348 /*template_parm_p=*/false,
18351 /* We don't know yet if the enclosing context is deprecated, so wait
18352 and warn in grokparms if appropriate. */
18353 deprecated_state
= DEPRECATED_SUPPRESS
;
18357 /* If a function parameter pack was specified and an implicit template
18358 parameter was introduced during cp_parser_parameter_declaration,
18359 change any implicit parameters introduced into packs. */
18360 if (parser
->implicit_template_parms
18361 && parameter
->declarator
18362 && parameter
->declarator
->parameter_pack_p
)
18364 int latest_template_parm_idx
= TREE_VEC_LENGTH
18365 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18367 if (latest_template_parm_idx
!= template_parm_idx
)
18368 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18369 (parameter
->decl_specifiers
.type
,
18370 template_parm_idx
, latest_template_parm_idx
);
18373 decl
= grokdeclarator (parameter
->declarator
,
18374 ¶meter
->decl_specifiers
,
18376 parameter
->default_argument
!= NULL_TREE
,
18377 ¶meter
->decl_specifiers
.attributes
);
18380 deprecated_state
= DEPRECATED_NORMAL
;
18382 /* If a parse error occurred parsing the parameter declaration,
18383 then the entire parameter-declaration-list is erroneous. */
18384 if (decl
== error_mark_node
)
18387 parameters
= error_mark_node
;
18391 if (parameter
->decl_specifiers
.attributes
)
18392 cplus_decl_attributes (&decl
,
18393 parameter
->decl_specifiers
.attributes
,
18395 if (DECL_NAME (decl
))
18396 decl
= pushdecl (decl
);
18398 if (decl
!= error_mark_node
)
18400 retrofit_lang_decl (decl
);
18401 DECL_PARM_INDEX (decl
) = ++index
;
18402 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18405 /* Add the new parameter to the list. */
18406 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18407 tail
= &TREE_CHAIN (*tail
);
18409 /* Peek at the next token. */
18410 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18411 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18412 /* These are for Objective-C++ */
18413 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18414 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18415 /* The parameter-declaration-list is complete. */
18417 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18421 /* Peek at the next token. */
18422 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18423 /* If it's an ellipsis, then the list is complete. */
18424 if (token
->type
== CPP_ELLIPSIS
)
18426 /* Otherwise, there must be more parameters. Consume the
18428 cp_lexer_consume_token (parser
->lexer
);
18429 /* When parsing something like:
18431 int i(float f, double d)
18433 we can tell after seeing the declaration for "f" that we
18434 are not looking at an initialization of a variable "i",
18435 but rather at the declaration of a function "i".
18437 Due to the fact that the parsing of template arguments
18438 (as specified to a template-id) requires backtracking we
18439 cannot use this technique when inside a template argument
18441 if (!parser
->in_template_argument_list_p
18442 && !parser
->in_type_id_in_expr_p
18443 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18444 /* However, a parameter-declaration of the form
18445 "float(f)" (which is a valid declaration of a
18446 parameter "f") can also be interpreted as an
18447 expression (the conversion of "f" to "float"). */
18448 && !parenthesized_p
)
18449 cp_parser_commit_to_tentative_parse (parser
);
18453 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18454 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18455 cp_parser_skip_to_closing_parenthesis (parser
,
18456 /*recovering=*/true,
18457 /*or_comma=*/false,
18458 /*consume_paren=*/false);
18463 parser
->in_unbraced_linkage_specification_p
18464 = saved_in_unbraced_linkage_specification_p
;
18466 /* Reset implicit_template_scope if we are about to leave the function
18467 parameter list that introduced it. Note that for out-of-line member
18468 definitions, there will be one or more class scopes before we get to
18469 the template parameter scope. */
18471 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18472 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
18474 while (maybe_its
->kind
== sk_class
)
18475 maybe_its
= maybe_its
->level_chain
;
18476 if (maybe_its
== its
)
18478 parser
->implicit_template_parms
= 0;
18479 parser
->implicit_template_scope
= 0;
18486 /* Parse a parameter declaration.
18488 parameter-declaration:
18489 decl-specifier-seq ... [opt] declarator
18490 decl-specifier-seq declarator = assignment-expression
18491 decl-specifier-seq ... [opt] abstract-declarator [opt]
18492 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18494 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18495 declares a template parameter. (In that case, a non-nested `>'
18496 token encountered during the parsing of the assignment-expression
18497 is not interpreted as a greater-than operator.)
18499 Returns a representation of the parameter, or NULL if an error
18500 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18501 true iff the declarator is of the form "(p)". */
18503 static cp_parameter_declarator
*
18504 cp_parser_parameter_declaration (cp_parser
*parser
,
18505 bool template_parm_p
,
18506 bool *parenthesized_p
)
18508 int declares_class_or_enum
;
18509 cp_decl_specifier_seq decl_specifiers
;
18510 cp_declarator
*declarator
;
18511 tree default_argument
;
18512 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18513 const char *saved_message
;
18515 /* In a template parameter, `>' is not an operator.
18519 When parsing a default template-argument for a non-type
18520 template-parameter, the first non-nested `>' is taken as the end
18521 of the template parameter-list rather than a greater-than
18524 /* Type definitions may not appear in parameter types. */
18525 saved_message
= parser
->type_definition_forbidden_message
;
18526 parser
->type_definition_forbidden_message
18527 = G_("types may not be defined in parameter types");
18529 /* Parse the declaration-specifiers. */
18530 cp_parser_decl_specifier_seq (parser
,
18531 CP_PARSER_FLAGS_NONE
,
18533 &declares_class_or_enum
);
18535 /* Complain about missing 'typename' or other invalid type names. */
18536 if (!decl_specifiers
.any_type_specifiers_p
18537 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18538 decl_specifiers
.type
= error_mark_node
;
18540 /* If an error occurred, there's no reason to attempt to parse the
18541 rest of the declaration. */
18542 if (cp_parser_error_occurred (parser
))
18544 parser
->type_definition_forbidden_message
= saved_message
;
18548 /* Peek at the next token. */
18549 token
= cp_lexer_peek_token (parser
->lexer
);
18551 /* If the next token is a `)', `,', `=', `>', or `...', then there
18552 is no declarator. However, when variadic templates are enabled,
18553 there may be a declarator following `...'. */
18554 if (token
->type
== CPP_CLOSE_PAREN
18555 || token
->type
== CPP_COMMA
18556 || token
->type
== CPP_EQ
18557 || token
->type
== CPP_GREATER
)
18560 if (parenthesized_p
)
18561 *parenthesized_p
= false;
18563 /* Otherwise, there should be a declarator. */
18566 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18567 parser
->default_arg_ok_p
= false;
18569 /* After seeing a decl-specifier-seq, if the next token is not a
18570 "(", there is no possibility that the code is a valid
18571 expression. Therefore, if parsing tentatively, we commit at
18573 if (!parser
->in_template_argument_list_p
18574 /* In an expression context, having seen:
18578 we cannot be sure whether we are looking at a
18579 function-type (taking a "char" as a parameter) or a cast
18580 of some object of type "char" to "int". */
18581 && !parser
->in_type_id_in_expr_p
18582 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18583 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
18584 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
18585 cp_parser_commit_to_tentative_parse (parser
);
18586 /* Parse the declarator. */
18587 declarator_token_start
= token
;
18588 declarator
= cp_parser_declarator (parser
,
18589 CP_PARSER_DECLARATOR_EITHER
,
18590 /*ctor_dtor_or_conv_p=*/NULL
,
18592 /*member_p=*/false);
18593 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18594 /* After the declarator, allow more attributes. */
18595 decl_specifiers
.attributes
18596 = chainon (decl_specifiers
.attributes
,
18597 cp_parser_attributes_opt (parser
));
18600 /* If the next token is an ellipsis, and we have not seen a
18601 declarator name, and the type of the declarator contains parameter
18602 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18603 a parameter pack expansion expression. Otherwise, leave the
18604 ellipsis for a C-style variadic function. */
18605 token
= cp_lexer_peek_token (parser
->lexer
);
18606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18608 tree type
= decl_specifiers
.type
;
18610 if (type
&& DECL_P (type
))
18611 type
= TREE_TYPE (type
);
18614 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
18615 && declarator_can_be_parameter_pack (declarator
)
18616 && (!declarator
|| !declarator
->parameter_pack_p
)
18617 && uses_parameter_packs (type
))
18619 /* Consume the `...'. */
18620 cp_lexer_consume_token (parser
->lexer
);
18621 maybe_warn_variadic_templates ();
18623 /* Build a pack expansion type */
18625 declarator
->parameter_pack_p
= true;
18627 decl_specifiers
.type
= make_pack_expansion (type
);
18631 /* The restriction on defining new types applies only to the type
18632 of the parameter, not to the default argument. */
18633 parser
->type_definition_forbidden_message
= saved_message
;
18635 /* If the next token is `=', then process a default argument. */
18636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18638 token
= cp_lexer_peek_token (parser
->lexer
);
18639 /* If we are defining a class, then the tokens that make up the
18640 default argument must be saved and processed later. */
18641 if (!template_parm_p
&& at_class_scope_p ()
18642 && TYPE_BEING_DEFINED (current_class_type
)
18643 && !LAMBDA_TYPE_P (current_class_type
))
18644 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
18645 /* Outside of a class definition, we can just parse the
18646 assignment-expression. */
18649 = cp_parser_default_argument (parser
, template_parm_p
);
18651 if (!parser
->default_arg_ok_p
)
18653 if (flag_permissive
)
18654 warning (0, "deprecated use of default argument for parameter of non-function");
18657 error_at (token
->location
,
18658 "default arguments are only "
18659 "permitted for function parameters");
18660 default_argument
= NULL_TREE
;
18663 else if ((declarator
&& declarator
->parameter_pack_p
)
18664 || (decl_specifiers
.type
18665 && PACK_EXPANSION_P (decl_specifiers
.type
)))
18667 /* Find the name of the parameter pack. */
18668 cp_declarator
*id_declarator
= declarator
;
18669 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
18670 id_declarator
= id_declarator
->declarator
;
18672 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
18673 error_at (declarator_token_start
->location
,
18675 ? G_("template parameter pack %qD "
18676 "cannot have a default argument")
18677 : G_("parameter pack %qD cannot have "
18678 "a default argument"),
18679 id_declarator
->u
.id
.unqualified_name
);
18681 error_at (declarator_token_start
->location
,
18683 ? G_("template parameter pack cannot have "
18684 "a default argument")
18685 : G_("parameter pack cannot have a "
18686 "default argument"));
18688 default_argument
= NULL_TREE
;
18692 default_argument
= NULL_TREE
;
18694 return make_parameter_declarator (&decl_specifiers
,
18699 /* Parse a default argument and return it.
18701 TEMPLATE_PARM_P is true if this is a default argument for a
18702 non-type template parameter. */
18704 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
18706 tree default_argument
= NULL_TREE
;
18707 bool saved_greater_than_is_operator_p
;
18708 bool saved_local_variables_forbidden_p
;
18709 bool non_constant_p
, is_direct_init
;
18711 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18713 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
18714 parser
->greater_than_is_operator_p
= !template_parm_p
;
18715 /* Local variable names (and the `this' keyword) may not
18716 appear in a default argument. */
18717 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
18718 parser
->local_variables_forbidden_p
= true;
18719 /* Parse the assignment-expression. */
18720 if (template_parm_p
)
18721 push_deferring_access_checks (dk_no_deferred
);
18722 tree saved_class_ptr
= NULL_TREE
;
18723 tree saved_class_ref
= NULL_TREE
;
18724 /* The "this" pointer is not valid in a default argument. */
18727 saved_class_ptr
= current_class_ptr
;
18728 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
18729 saved_class_ref
= current_class_ref
;
18730 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
18733 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
18734 /* Restore the "this" pointer. */
18737 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
18738 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
18740 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
18741 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18742 if (template_parm_p
)
18743 pop_deferring_access_checks ();
18744 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
18745 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
18747 return default_argument
;
18750 /* Parse a function-body.
18753 compound_statement */
18756 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
18758 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
18761 /* Parse a ctor-initializer-opt followed by a function-body. Return
18762 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18763 is true we are parsing a function-try-block. */
18766 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
18767 bool in_function_try_block
)
18770 bool ctor_initializer_p
;
18771 const bool check_body_p
=
18772 DECL_CONSTRUCTOR_P (current_function_decl
)
18773 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
18776 /* Begin the function body. */
18777 body
= begin_function_body ();
18778 /* Parse the optional ctor-initializer. */
18779 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
18781 /* If we're parsing a constexpr constructor definition, we need
18782 to check that the constructor body is indeed empty. However,
18783 before we get to cp_parser_function_body lot of junk has been
18784 generated, so we can't just check that we have an empty block.
18785 Rather we take a snapshot of the outermost block, and check whether
18786 cp_parser_function_body changed its state. */
18789 list
= cur_stmt_list
;
18790 if (STATEMENT_LIST_TAIL (list
))
18791 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
18793 /* Parse the function-body. */
18794 cp_parser_function_body (parser
, in_function_try_block
);
18796 check_constexpr_ctor_body (last
, list
);
18797 /* Finish the function body. */
18798 finish_function_body (body
);
18800 return ctor_initializer_p
;
18803 /* Parse an initializer.
18806 = initializer-clause
18807 ( expression-list )
18809 Returns an expression representing the initializer. If no
18810 initializer is present, NULL_TREE is returned.
18812 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18813 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18814 set to TRUE if there is no initializer present. If there is an
18815 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18816 is set to true; otherwise it is set to false. */
18819 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
18820 bool* non_constant_p
)
18825 /* Peek at the next token. */
18826 token
= cp_lexer_peek_token (parser
->lexer
);
18828 /* Let our caller know whether or not this initializer was
18830 *is_direct_init
= (token
->type
!= CPP_EQ
);
18831 /* Assume that the initializer is constant. */
18832 *non_constant_p
= false;
18834 if (token
->type
== CPP_EQ
)
18836 /* Consume the `='. */
18837 cp_lexer_consume_token (parser
->lexer
);
18838 /* Parse the initializer-clause. */
18839 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
18841 else if (token
->type
== CPP_OPEN_PAREN
)
18843 vec
<tree
, va_gc
> *vec
;
18844 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
18846 /*allow_expansion_p=*/true,
18849 return error_mark_node
;
18850 init
= build_tree_list_vec (vec
);
18851 release_tree_vector (vec
);
18853 else if (token
->type
== CPP_OPEN_BRACE
)
18855 cp_lexer_set_source_position (parser
->lexer
);
18856 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18857 init
= cp_parser_braced_list (parser
, non_constant_p
);
18858 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
18862 /* Anything else is an error. */
18863 cp_parser_error (parser
, "expected initializer");
18864 init
= error_mark_node
;
18870 /* Parse an initializer-clause.
18872 initializer-clause:
18873 assignment-expression
18876 Returns an expression representing the initializer.
18878 If the `assignment-expression' production is used the value
18879 returned is simply a representation for the expression.
18881 Otherwise, calls cp_parser_braced_list. */
18884 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
18888 /* Assume the expression is constant. */
18889 *non_constant_p
= false;
18891 /* If it is not a `{', then we are looking at an
18892 assignment-expression. */
18893 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
18896 = cp_parser_constant_expression (parser
,
18897 /*allow_non_constant_p=*/true,
18901 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
18903 return initializer
;
18906 /* Parse a brace-enclosed initializer list.
18909 { initializer-list , [opt] }
18912 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18913 the elements of the initializer-list (or NULL, if the last
18914 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18915 NULL_TREE. There is no way to detect whether or not the optional
18916 trailing `,' was provided. NON_CONSTANT_P is as for
18917 cp_parser_initializer. */
18920 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
18924 /* Consume the `{' token. */
18925 cp_lexer_consume_token (parser
->lexer
);
18926 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18927 initializer
= make_node (CONSTRUCTOR
);
18928 /* If it's not a `}', then there is a non-trivial initializer. */
18929 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
18931 /* Parse the initializer list. */
18932 CONSTRUCTOR_ELTS (initializer
)
18933 = cp_parser_initializer_list (parser
, non_constant_p
);
18934 /* A trailing `,' token is allowed. */
18935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18936 cp_lexer_consume_token (parser
->lexer
);
18939 *non_constant_p
= false;
18940 /* Now, there should be a trailing `}'. */
18941 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18942 TREE_TYPE (initializer
) = init_list_type_node
;
18943 return initializer
;
18946 /* Parse an initializer-list.
18949 initializer-clause ... [opt]
18950 initializer-list , initializer-clause ... [opt]
18955 designation initializer-clause ...[opt]
18956 initializer-list , designation initializer-clause ...[opt]
18961 [ constant-expression ] =
18963 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18964 for the initializer. If the INDEX of the elt is non-NULL, it is the
18965 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18966 as for cp_parser_initializer. */
18968 static vec
<constructor_elt
, va_gc
> *
18969 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
18971 vec
<constructor_elt
, va_gc
> *v
= NULL
;
18973 /* Assume all of the expressions are constant. */
18974 *non_constant_p
= false;
18976 /* Parse the rest of the list. */
18982 bool clause_non_constant_p
;
18984 /* If the next token is an identifier and the following one is a
18985 colon, we are looking at the GNU designated-initializer
18987 if (cp_parser_allow_gnu_extensions_p (parser
)
18988 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
18989 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
18991 /* Warn the user that they are using an extension. */
18992 pedwarn (input_location
, OPT_Wpedantic
,
18993 "ISO C++ does not allow designated initializers");
18994 /* Consume the identifier. */
18995 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18996 /* Consume the `:'. */
18997 cp_lexer_consume_token (parser
->lexer
);
18999 /* Also handle the C99 syntax, '. id ='. */
19000 else if (cp_parser_allow_gnu_extensions_p (parser
)
19001 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
19002 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
19003 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
19005 /* Warn the user that they are using an extension. */
19006 pedwarn (input_location
, OPT_Wpedantic
,
19007 "ISO C++ does not allow C99 designated initializers");
19008 /* Consume the `.'. */
19009 cp_lexer_consume_token (parser
->lexer
);
19010 /* Consume the identifier. */
19011 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19012 /* Consume the `='. */
19013 cp_lexer_consume_token (parser
->lexer
);
19015 /* Also handle C99 array designators, '[ const ] ='. */
19016 else if (cp_parser_allow_gnu_extensions_p (parser
)
19017 && !c_dialect_objc ()
19018 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19020 /* In C++11, [ could start a lambda-introducer. */
19021 bool non_const
= false;
19023 cp_parser_parse_tentatively (parser
);
19024 cp_lexer_consume_token (parser
->lexer
);
19025 designator
= cp_parser_constant_expression (parser
, true, &non_const
);
19026 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19027 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19028 if (!cp_parser_parse_definitely (parser
))
19029 designator
= NULL_TREE
;
19030 else if (non_const
)
19031 require_potential_rvalue_constant_expression (designator
);
19034 designator
= NULL_TREE
;
19036 /* Parse the initializer. */
19037 initializer
= cp_parser_initializer_clause (parser
,
19038 &clause_non_constant_p
);
19039 /* If any clause is non-constant, so is the entire initializer. */
19040 if (clause_non_constant_p
)
19041 *non_constant_p
= true;
19043 /* If we have an ellipsis, this is an initializer pack
19045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19047 /* Consume the `...'. */
19048 cp_lexer_consume_token (parser
->lexer
);
19050 /* Turn the initializer into an initializer expansion. */
19051 initializer
= make_pack_expansion (initializer
);
19054 /* Add it to the vector. */
19055 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
19057 /* If the next token is not a comma, we have reached the end of
19059 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19062 /* Peek at the next token. */
19063 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19064 /* If the next token is a `}', then we're still done. An
19065 initializer-clause can have a trailing `,' after the
19066 initializer-list and before the closing `}'. */
19067 if (token
->type
== CPP_CLOSE_BRACE
)
19070 /* Consume the `,' token. */
19071 cp_lexer_consume_token (parser
->lexer
);
19077 /* Classes [gram.class] */
19079 /* Parse a class-name.
19085 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19086 to indicate that names looked up in dependent types should be
19087 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19088 keyword has been used to indicate that the name that appears next
19089 is a template. TAG_TYPE indicates the explicit tag given before
19090 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19091 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19092 is the class being defined in a class-head.
19094 Returns the TYPE_DECL representing the class. */
19097 cp_parser_class_name (cp_parser
*parser
,
19098 bool typename_keyword_p
,
19099 bool template_keyword_p
,
19100 enum tag_types tag_type
,
19101 bool check_dependency_p
,
19103 bool is_declaration
)
19109 tree identifier
= NULL_TREE
;
19111 /* All class-names start with an identifier. */
19112 token
= cp_lexer_peek_token (parser
->lexer
);
19113 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
19115 cp_parser_error (parser
, "expected class-name");
19116 return error_mark_node
;
19119 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19120 to a template-id, so we save it here. */
19121 scope
= parser
->scope
;
19122 if (scope
== error_mark_node
)
19123 return error_mark_node
;
19125 /* Any name names a type if we're following the `typename' keyword
19126 in a qualified name where the enclosing scope is type-dependent. */
19127 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
19128 && dependent_type_p (scope
));
19129 /* Handle the common case (an identifier, but not a template-id)
19131 if (token
->type
== CPP_NAME
19132 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
19134 cp_token
*identifier_token
;
19137 /* Look for the identifier. */
19138 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
19139 ambiguous_p
= identifier_token
->error_reported
;
19140 identifier
= cp_parser_identifier (parser
);
19141 /* If the next token isn't an identifier, we are certainly not
19142 looking at a class-name. */
19143 if (identifier
== error_mark_node
)
19144 decl
= error_mark_node
;
19145 /* If we know this is a type-name, there's no need to look it
19147 else if (typename_p
)
19151 tree ambiguous_decls
;
19152 /* If we already know that this lookup is ambiguous, then
19153 we've already issued an error message; there's no reason
19157 cp_parser_simulate_error (parser
);
19158 return error_mark_node
;
19160 /* If the next token is a `::', then the name must be a type
19163 [basic.lookup.qual]
19165 During the lookup for a name preceding the :: scope
19166 resolution operator, object, function, and enumerator
19167 names are ignored. */
19168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19169 tag_type
= typename_type
;
19170 /* Look up the name. */
19171 decl
= cp_parser_lookup_name (parser
, identifier
,
19173 /*is_template=*/false,
19174 /*is_namespace=*/false,
19175 check_dependency_p
,
19177 identifier_token
->location
);
19178 if (ambiguous_decls
)
19180 if (cp_parser_parsing_tentatively (parser
))
19181 cp_parser_simulate_error (parser
);
19182 return error_mark_node
;
19188 /* Try a template-id. */
19189 decl
= cp_parser_template_id (parser
, template_keyword_p
,
19190 check_dependency_p
,
19193 if (decl
== error_mark_node
)
19194 return error_mark_node
;
19197 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
19199 /* If this is a typename, create a TYPENAME_TYPE. */
19200 if (typename_p
&& decl
!= error_mark_node
)
19202 decl
= make_typename_type (scope
, decl
, typename_type
,
19203 /*complain=*/tf_error
);
19204 if (decl
!= error_mark_node
)
19205 decl
= TYPE_NAME (decl
);
19208 decl
= strip_using_decl (decl
);
19210 /* Check to see that it is really the name of a class. */
19211 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19212 && identifier_p (TREE_OPERAND (decl
, 0))
19213 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19214 /* Situations like this:
19216 template <typename T> struct A {
19217 typename T::template X<int>::I i;
19220 are problematic. Is `T::template X<int>' a class-name? The
19221 standard does not seem to be definitive, but there is no other
19222 valid interpretation of the following `::'. Therefore, those
19223 names are considered class-names. */
19225 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19226 if (decl
!= error_mark_node
)
19227 decl
= TYPE_NAME (decl
);
19229 else if (TREE_CODE (decl
) != TYPE_DECL
19230 || TREE_TYPE (decl
) == error_mark_node
19231 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19232 /* In Objective-C 2.0, a classname followed by '.' starts a
19233 dot-syntax expression, and it's not a type-name. */
19234 || (c_dialect_objc ()
19235 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19236 && objc_is_class_name (decl
)))
19237 decl
= error_mark_node
;
19239 if (decl
== error_mark_node
)
19240 cp_parser_error (parser
, "expected class-name");
19241 else if (identifier
&& !parser
->scope
)
19242 maybe_note_name_used_in_class (identifier
, decl
);
19247 /* Parse a class-specifier.
19250 class-head { member-specification [opt] }
19252 Returns the TREE_TYPE representing the class. */
19255 cp_parser_class_specifier_1 (cp_parser
* parser
)
19258 tree attributes
= NULL_TREE
;
19259 bool nested_name_specifier_p
;
19260 unsigned saved_num_template_parameter_lists
;
19261 bool saved_in_function_body
;
19262 unsigned char in_statement
;
19263 bool in_switch_statement_p
;
19264 bool saved_in_unbraced_linkage_specification_p
;
19265 tree old_scope
= NULL_TREE
;
19266 tree scope
= NULL_TREE
;
19267 cp_token
*closing_brace
;
19269 push_deferring_access_checks (dk_no_deferred
);
19271 /* Parse the class-head. */
19272 type
= cp_parser_class_head (parser
,
19273 &nested_name_specifier_p
);
19274 /* If the class-head was a semantic disaster, skip the entire body
19278 cp_parser_skip_to_end_of_block_or_statement (parser
);
19279 pop_deferring_access_checks ();
19280 return error_mark_node
;
19283 /* Look for the `{'. */
19284 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19286 pop_deferring_access_checks ();
19287 return error_mark_node
;
19290 cp_ensure_no_omp_declare_simd (parser
);
19292 /* Issue an error message if type-definitions are forbidden here. */
19293 cp_parser_check_type_definition (parser
);
19294 /* Remember that we are defining one more class. */
19295 ++parser
->num_classes_being_defined
;
19296 /* Inside the class, surrounding template-parameter-lists do not
19298 saved_num_template_parameter_lists
19299 = parser
->num_template_parameter_lists
;
19300 parser
->num_template_parameter_lists
= 0;
19301 /* We are not in a function body. */
19302 saved_in_function_body
= parser
->in_function_body
;
19303 parser
->in_function_body
= false;
19304 /* Or in a loop. */
19305 in_statement
= parser
->in_statement
;
19306 parser
->in_statement
= 0;
19307 /* Or in a switch. */
19308 in_switch_statement_p
= parser
->in_switch_statement_p
;
19309 parser
->in_switch_statement_p
= false;
19310 /* We are not immediately inside an extern "lang" block. */
19311 saved_in_unbraced_linkage_specification_p
19312 = parser
->in_unbraced_linkage_specification_p
;
19313 parser
->in_unbraced_linkage_specification_p
= false;
19315 /* Start the class. */
19316 if (nested_name_specifier_p
)
19318 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19319 old_scope
= push_inner_scope (scope
);
19321 type
= begin_class_definition (type
);
19323 if (type
== error_mark_node
)
19324 /* If the type is erroneous, skip the entire body of the class. */
19325 cp_parser_skip_to_closing_brace (parser
);
19327 /* Parse the member-specification. */
19328 cp_parser_member_specification_opt (parser
);
19330 /* Look for the trailing `}'. */
19331 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19332 /* Look for trailing attributes to apply to this class. */
19333 if (cp_parser_allow_gnu_extensions_p (parser
))
19334 attributes
= cp_parser_gnu_attributes_opt (parser
);
19335 if (type
!= error_mark_node
)
19336 type
= finish_struct (type
, attributes
);
19337 if (nested_name_specifier_p
)
19338 pop_inner_scope (old_scope
, scope
);
19340 /* We've finished a type definition. Check for the common syntax
19341 error of forgetting a semicolon after the definition. We need to
19342 be careful, as we can't just check for not-a-semicolon and be done
19343 with it; the user might have typed:
19345 class X { } c = ...;
19346 class X { } *p = ...;
19348 and so forth. Instead, enumerate all the possible tokens that
19349 might follow this production; if we don't see one of them, then
19350 complain and silently insert the semicolon. */
19352 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19353 bool want_semicolon
= true;
19355 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19356 /* Don't try to parse c++11 attributes here. As per the
19357 grammar, that should be a task for
19358 cp_parser_decl_specifier_seq. */
19359 want_semicolon
= false;
19361 switch (token
->type
)
19364 case CPP_SEMICOLON
:
19367 case CPP_OPEN_PAREN
:
19368 case CPP_CLOSE_PAREN
:
19370 want_semicolon
= false;
19373 /* While it's legal for type qualifiers and storage class
19374 specifiers to follow type definitions in the grammar, only
19375 compiler testsuites contain code like that. Assume that if
19376 we see such code, then what we're really seeing is a case
19380 const <type> var = ...;
19385 static <type> func (...) ...
19387 i.e. the qualifier or specifier applies to the next
19388 declaration. To do so, however, we need to look ahead one
19389 more token to see if *that* token is a type specifier.
19391 This code could be improved to handle:
19394 static const <type> var = ...; */
19396 if (keyword_is_decl_specifier (token
->keyword
))
19398 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19400 /* Handling user-defined types here would be nice, but very
19403 = (lookahead
->type
== CPP_KEYWORD
19404 && keyword_begins_type_specifier (lookahead
->keyword
));
19411 /* If we don't have a type, then something is very wrong and we
19412 shouldn't try to do anything clever. Likewise for not seeing the
19414 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19416 cp_token_position prev
19417 = cp_lexer_previous_token_position (parser
->lexer
);
19418 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19419 location_t loc
= prev_token
->location
;
19421 if (CLASSTYPE_DECLARED_CLASS (type
))
19422 error_at (loc
, "expected %<;%> after class definition");
19423 else if (TREE_CODE (type
) == RECORD_TYPE
)
19424 error_at (loc
, "expected %<;%> after struct definition");
19425 else if (TREE_CODE (type
) == UNION_TYPE
)
19426 error_at (loc
, "expected %<;%> after union definition");
19428 gcc_unreachable ();
19430 /* Unget one token and smash it to look as though we encountered
19431 a semicolon in the input stream. */
19432 cp_lexer_set_token_position (parser
->lexer
, prev
);
19433 token
= cp_lexer_peek_token (parser
->lexer
);
19434 token
->type
= CPP_SEMICOLON
;
19435 token
->keyword
= RID_MAX
;
19439 /* If this class is not itself within the scope of another class,
19440 then we need to parse the bodies of all of the queued function
19441 definitions. Note that the queued functions defined in a class
19442 are not always processed immediately following the
19443 class-specifier for that class. Consider:
19446 struct B { void f() { sizeof (A); } };
19449 If `f' were processed before the processing of `A' were
19450 completed, there would be no way to compute the size of `A'.
19451 Note that the nesting we are interested in here is lexical --
19452 not the semantic nesting given by TYPE_CONTEXT. In particular,
19455 struct A { struct B; };
19456 struct A::B { void f() { } };
19458 there is no need to delay the parsing of `A::B::f'. */
19459 if (--parser
->num_classes_being_defined
== 0)
19462 tree class_type
= NULL_TREE
;
19463 tree pushed_scope
= NULL_TREE
;
19465 cp_default_arg_entry
*e
;
19466 tree save_ccp
, save_ccr
;
19468 /* In a first pass, parse default arguments to the functions.
19469 Then, in a second pass, parse the bodies of the functions.
19470 This two-phased approach handles cases like:
19478 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
19481 /* If there are default arguments that have not yet been processed,
19482 take care of them now. */
19483 if (class_type
!= e
->class_type
)
19486 pop_scope (pushed_scope
);
19487 class_type
= e
->class_type
;
19488 pushed_scope
= push_scope (class_type
);
19490 /* Make sure that any template parameters are in scope. */
19491 maybe_begin_member_template_processing (decl
);
19492 /* Parse the default argument expressions. */
19493 cp_parser_late_parsing_default_args (parser
, decl
);
19494 /* Remove any template parameters from the symbol table. */
19495 maybe_end_member_template_processing ();
19497 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
19498 /* Now parse any NSDMIs. */
19499 save_ccp
= current_class_ptr
;
19500 save_ccr
= current_class_ref
;
19501 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
19503 if (class_type
!= DECL_CONTEXT (decl
))
19506 pop_scope (pushed_scope
);
19507 class_type
= DECL_CONTEXT (decl
);
19508 pushed_scope
= push_scope (class_type
);
19510 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
19511 cp_parser_late_parsing_nsdmi (parser
, decl
);
19513 vec_safe_truncate (unparsed_nsdmis
, 0);
19514 current_class_ptr
= save_ccp
;
19515 current_class_ref
= save_ccr
;
19517 pop_scope (pushed_scope
);
19519 /* Now do some post-NSDMI bookkeeping. */
19520 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
19521 after_nsdmi_defaulted_late_checks (class_type
);
19522 vec_safe_truncate (unparsed_classes
, 0);
19523 after_nsdmi_defaulted_late_checks (type
);
19525 /* Now parse the body of the functions. */
19528 /* OpenMP UDRs need to be parsed before all other functions. */
19529 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19530 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
19531 cp_parser_late_parsing_for_member (parser
, decl
);
19532 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19533 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
19534 cp_parser_late_parsing_for_member (parser
, decl
);
19537 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19538 cp_parser_late_parsing_for_member (parser
, decl
);
19539 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
19542 vec_safe_push (unparsed_classes
, type
);
19544 /* Put back any saved access checks. */
19545 pop_deferring_access_checks ();
19547 /* Restore saved state. */
19548 parser
->in_switch_statement_p
= in_switch_statement_p
;
19549 parser
->in_statement
= in_statement
;
19550 parser
->in_function_body
= saved_in_function_body
;
19551 parser
->num_template_parameter_lists
19552 = saved_num_template_parameter_lists
;
19553 parser
->in_unbraced_linkage_specification_p
19554 = saved_in_unbraced_linkage_specification_p
;
19560 cp_parser_class_specifier (cp_parser
* parser
)
19563 timevar_push (TV_PARSE_STRUCT
);
19564 ret
= cp_parser_class_specifier_1 (parser
);
19565 timevar_pop (TV_PARSE_STRUCT
);
19569 /* Parse a class-head.
19572 class-key identifier [opt] base-clause [opt]
19573 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19574 class-key nested-name-specifier [opt] template-id
19577 class-virt-specifier:
19581 class-key attributes identifier [opt] base-clause [opt]
19582 class-key attributes nested-name-specifier identifier base-clause [opt]
19583 class-key attributes nested-name-specifier [opt] template-id
19586 Upon return BASES is initialized to the list of base classes (or
19587 NULL, if there are none) in the same form returned by
19588 cp_parser_base_clause.
19590 Returns the TYPE of the indicated class. Sets
19591 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19592 involving a nested-name-specifier was used, and FALSE otherwise.
19594 Returns error_mark_node if this is not a class-head.
19596 Returns NULL_TREE if the class-head is syntactically valid, but
19597 semantically invalid in a way that means we should skip the entire
19598 body of the class. */
19601 cp_parser_class_head (cp_parser
* parser
,
19602 bool* nested_name_specifier_p
)
19604 tree nested_name_specifier
;
19605 enum tag_types class_key
;
19606 tree id
= NULL_TREE
;
19607 tree type
= NULL_TREE
;
19610 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
19611 bool template_id_p
= false;
19612 bool qualified_p
= false;
19613 bool invalid_nested_name_p
= false;
19614 bool invalid_explicit_specialization_p
= false;
19615 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
19616 tree pushed_scope
= NULL_TREE
;
19617 unsigned num_templates
;
19618 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
19619 /* Assume no nested-name-specifier will be present. */
19620 *nested_name_specifier_p
= false;
19621 /* Assume no template parameter lists will be used in defining the
19624 parser
->colon_corrects_to_scope_p
= false;
19626 /* Look for the class-key. */
19627 class_key
= cp_parser_class_key (parser
);
19628 if (class_key
== none_type
)
19629 return error_mark_node
;
19631 /* Parse the attributes. */
19632 attributes
= cp_parser_attributes_opt (parser
);
19634 /* If the next token is `::', that is invalid -- but sometimes
19635 people do try to write:
19639 Handle this gracefully by accepting the extra qualifier, and then
19640 issuing an error about it later if this really is a
19641 class-head. If it turns out just to be an elaborated type
19642 specifier, remain silent. */
19643 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
19644 qualified_p
= true;
19646 push_deferring_access_checks (dk_no_check
);
19648 /* Determine the name of the class. Begin by looking for an
19649 optional nested-name-specifier. */
19650 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
19651 nested_name_specifier
19652 = cp_parser_nested_name_specifier_opt (parser
,
19653 /*typename_keyword_p=*/false,
19654 /*check_dependency_p=*/false,
19656 /*is_declaration=*/false);
19657 /* If there was a nested-name-specifier, then there *must* be an
19659 if (nested_name_specifier
)
19661 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19662 /* Although the grammar says `identifier', it really means
19663 `class-name' or `template-name'. You are only allowed to
19664 define a class that has already been declared with this
19667 The proposed resolution for Core Issue 180 says that wherever
19668 you see `class T::X' you should treat `X' as a type-name.
19670 It is OK to define an inaccessible class; for example:
19672 class A { class B; };
19675 We do not know if we will see a class-name, or a
19676 template-name. We look for a class-name first, in case the
19677 class-name is a template-id; if we looked for the
19678 template-name first we would stop after the template-name. */
19679 cp_parser_parse_tentatively (parser
);
19680 type
= cp_parser_class_name (parser
,
19681 /*typename_keyword_p=*/false,
19682 /*template_keyword_p=*/false,
19684 /*check_dependency_p=*/false,
19685 /*class_head_p=*/true,
19686 /*is_declaration=*/false);
19687 /* If that didn't work, ignore the nested-name-specifier. */
19688 if (!cp_parser_parse_definitely (parser
))
19690 invalid_nested_name_p
= true;
19691 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19692 id
= cp_parser_identifier (parser
);
19693 if (id
== error_mark_node
)
19696 /* If we could not find a corresponding TYPE, treat this
19697 declaration like an unqualified declaration. */
19698 if (type
== error_mark_node
)
19699 nested_name_specifier
= NULL_TREE
;
19700 /* Otherwise, count the number of templates used in TYPE and its
19701 containing scopes. */
19706 for (scope
= TREE_TYPE (type
);
19707 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
19708 scope
= get_containing_scope (scope
))
19710 && CLASS_TYPE_P (scope
)
19711 && CLASSTYPE_TEMPLATE_INFO (scope
)
19712 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
19713 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
19714 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
19718 /* Otherwise, the identifier is optional. */
19721 /* We don't know whether what comes next is a template-id,
19722 an identifier, or nothing at all. */
19723 cp_parser_parse_tentatively (parser
);
19724 /* Check for a template-id. */
19725 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19726 id
= cp_parser_template_id (parser
,
19727 /*template_keyword_p=*/false,
19728 /*check_dependency_p=*/true,
19730 /*is_declaration=*/true);
19731 /* If that didn't work, it could still be an identifier. */
19732 if (!cp_parser_parse_definitely (parser
))
19734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19736 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19737 id
= cp_parser_identifier (parser
);
19744 template_id_p
= true;
19749 pop_deferring_access_checks ();
19753 cp_parser_check_for_invalid_template_id (parser
, id
,
19755 type_start_token
->location
);
19757 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19759 /* If it's not a `:' or a `{' then we can't really be looking at a
19760 class-head, since a class-head only appears as part of a
19761 class-specifier. We have to detect this situation before calling
19762 xref_tag, since that has irreversible side-effects. */
19763 if (!cp_parser_next_token_starts_class_definition_p (parser
))
19765 cp_parser_error (parser
, "expected %<{%> or %<:%>");
19766 type
= error_mark_node
;
19770 /* At this point, we're going ahead with the class-specifier, even
19771 if some other problem occurs. */
19772 cp_parser_commit_to_tentative_parse (parser
);
19773 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
19775 cp_parser_error (parser
,
19776 "cannot specify %<override%> for a class");
19777 type
= error_mark_node
;
19780 /* Issue the error about the overly-qualified name now. */
19783 cp_parser_error (parser
,
19784 "global qualification of class name is invalid");
19785 type
= error_mark_node
;
19788 else if (invalid_nested_name_p
)
19790 cp_parser_error (parser
,
19791 "qualified name does not name a class");
19792 type
= error_mark_node
;
19795 else if (nested_name_specifier
)
19799 /* Reject typedef-names in class heads. */
19800 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
19802 error_at (type_start_token
->location
,
19803 "invalid class name in declaration of %qD",
19809 /* Figure out in what scope the declaration is being placed. */
19810 scope
= current_scope ();
19811 /* If that scope does not contain the scope in which the
19812 class was originally declared, the program is invalid. */
19813 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
19815 if (at_namespace_scope_p ())
19816 error_at (type_start_token
->location
,
19817 "declaration of %qD in namespace %qD which does not "
19819 type
, scope
, nested_name_specifier
);
19821 error_at (type_start_token
->location
,
19822 "declaration of %qD in %qD which does not enclose %qD",
19823 type
, scope
, nested_name_specifier
);
19829 A declarator-id shall not be qualified except for the
19830 definition of a ... nested class outside of its class
19831 ... [or] the definition or explicit instantiation of a
19832 class member of a namespace outside of its namespace. */
19833 if (scope
== nested_name_specifier
)
19835 permerror (nested_name_specifier_token_start
->location
,
19836 "extra qualification not allowed");
19837 nested_name_specifier
= NULL_TREE
;
19841 /* An explicit-specialization must be preceded by "template <>". If
19842 it is not, try to recover gracefully. */
19843 if (at_namespace_scope_p ()
19844 && parser
->num_template_parameter_lists
== 0
19847 error_at (type_start_token
->location
,
19848 "an explicit specialization must be preceded by %<template <>%>");
19849 invalid_explicit_specialization_p
= true;
19850 /* Take the same action that would have been taken by
19851 cp_parser_explicit_specialization. */
19852 ++parser
->num_template_parameter_lists
;
19853 begin_specialization ();
19855 /* There must be no "return" statements between this point and the
19856 end of this function; set "type "to the correct return value and
19857 use "goto done;" to return. */
19858 /* Make sure that the right number of template parameters were
19860 if (!cp_parser_check_template_parameters (parser
, num_templates
,
19861 type_start_token
->location
,
19862 /*declarator=*/NULL
))
19864 /* If something went wrong, there is no point in even trying to
19865 process the class-definition. */
19870 /* Look up the type. */
19873 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
19874 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
19875 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
19877 error_at (type_start_token
->location
,
19878 "function template %qD redeclared as a class template", id
);
19879 type
= error_mark_node
;
19883 type
= TREE_TYPE (id
);
19884 type
= maybe_process_partial_specialization (type
);
19886 if (nested_name_specifier
)
19887 pushed_scope
= push_scope (nested_name_specifier
);
19889 else if (nested_name_specifier
)
19895 template <typename T> struct S { struct T };
19896 template <typename T> struct S<T>::T { };
19898 we will get a TYPENAME_TYPE when processing the definition of
19899 `S::T'. We need to resolve it to the actual type before we
19900 try to define it. */
19901 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
19903 class_type
= resolve_typename_type (TREE_TYPE (type
),
19904 /*only_current_p=*/false);
19905 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
19906 type
= TYPE_NAME (class_type
);
19909 cp_parser_error (parser
, "could not resolve typename type");
19910 type
= error_mark_node
;
19914 if (maybe_process_partial_specialization (TREE_TYPE (type
))
19915 == error_mark_node
)
19921 class_type
= current_class_type
;
19922 /* Enter the scope indicated by the nested-name-specifier. */
19923 pushed_scope
= push_scope (nested_name_specifier
);
19924 /* Get the canonical version of this type. */
19925 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
19926 /* Call push_template_decl if it seems like we should be defining a
19927 template either from the template headers or the type we're
19928 defining, so that we diagnose both extra and missing headers. */
19929 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
19930 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
))
19931 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
19932 (TREE_TYPE (type
)))))
19933 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
19935 type
= push_template_decl (type
);
19936 if (type
== error_mark_node
)
19943 type
= TREE_TYPE (type
);
19944 *nested_name_specifier_p
= true;
19946 else /* The name is not a nested name. */
19948 /* If the class was unnamed, create a dummy name. */
19950 id
= make_anon_name ();
19951 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
19952 parser
->num_template_parameter_lists
);
19955 /* Indicate whether this class was declared as a `class' or as a
19957 if (TREE_CODE (type
) == RECORD_TYPE
)
19958 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
19959 cp_parser_check_class_key (class_key
, type
);
19961 /* If this type was already complete, and we see another definition,
19962 that's an error. */
19963 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
19965 error_at (type_start_token
->location
, "redefinition of %q#T",
19967 error_at (type_start_token
->location
, "previous definition of %q+#T",
19972 else if (type
== error_mark_node
)
19977 /* Apply attributes now, before any use of the class as a template
19978 argument in its base list. */
19979 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
19980 fixup_attribute_variants (type
);
19983 /* We will have entered the scope containing the class; the names of
19984 base classes should be looked up in that context. For example:
19986 struct A { struct B {}; struct C; };
19987 struct A::C : B {};
19991 /* Get the list of base-classes, if there is one. */
19992 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19994 /* PR59482: enter the class scope so that base-specifiers are looked
19998 bases
= cp_parser_base_clause (parser
);
19999 /* PR59482: get out of the previously pushed class scope so that the
20000 subsequent pops pop the right thing. */
20007 /* If we're really defining a class, process the base classes.
20008 If they're invalid, fail. */
20009 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20010 && !xref_basetypes (type
, bases
))
20014 /* Leave the scope given by the nested-name-specifier. We will
20015 enter the class scope itself while processing the members. */
20017 pop_scope (pushed_scope
);
20019 if (invalid_explicit_specialization_p
)
20021 end_specialization ();
20022 --parser
->num_template_parameter_lists
;
20026 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20027 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
20028 CLASSTYPE_FINAL (type
) = 1;
20030 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20034 /* Parse a class-key.
20041 Returns the kind of class-key specified, or none_type to indicate
20044 static enum tag_types
20045 cp_parser_class_key (cp_parser
* parser
)
20048 enum tag_types tag_type
;
20050 /* Look for the class-key. */
20051 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
20055 /* Check to see if the TOKEN is a class-key. */
20056 tag_type
= cp_parser_token_is_class_key (token
);
20058 cp_parser_error (parser
, "expected class-key");
20062 /* Parse an (optional) member-specification.
20064 member-specification:
20065 member-declaration member-specification [opt]
20066 access-specifier : member-specification [opt] */
20069 cp_parser_member_specification_opt (cp_parser
* parser
)
20076 /* Peek at the next token. */
20077 token
= cp_lexer_peek_token (parser
->lexer
);
20078 /* If it's a `}', or EOF then we've seen all the members. */
20079 if (token
->type
== CPP_CLOSE_BRACE
20080 || token
->type
== CPP_EOF
20081 || token
->type
== CPP_PRAGMA_EOL
)
20084 /* See if this token is a keyword. */
20085 keyword
= token
->keyword
;
20089 case RID_PROTECTED
:
20091 /* Consume the access-specifier. */
20092 cp_lexer_consume_token (parser
->lexer
);
20093 /* Remember which access-specifier is active. */
20094 current_access_specifier
= token
->u
.value
;
20095 /* Look for the `:'. */
20096 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20100 /* Accept #pragmas at class scope. */
20101 if (token
->type
== CPP_PRAGMA
)
20103 cp_parser_pragma (parser
, pragma_member
);
20107 /* Otherwise, the next construction must be a
20108 member-declaration. */
20109 cp_parser_member_declaration (parser
);
20114 /* Parse a member-declaration.
20116 member-declaration:
20117 decl-specifier-seq [opt] member-declarator-list [opt] ;
20118 function-definition ; [opt]
20119 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20121 template-declaration
20124 member-declarator-list:
20126 member-declarator-list , member-declarator
20129 declarator pure-specifier [opt]
20130 declarator constant-initializer [opt]
20131 identifier [opt] : constant-expression
20135 member-declaration:
20136 __extension__ member-declaration
20139 declarator attributes [opt] pure-specifier [opt]
20140 declarator attributes [opt] constant-initializer [opt]
20141 identifier [opt] attributes [opt] : constant-expression
20145 member-declaration:
20146 static_assert-declaration */
20149 cp_parser_member_declaration (cp_parser
* parser
)
20151 cp_decl_specifier_seq decl_specifiers
;
20152 tree prefix_attributes
;
20154 int declares_class_or_enum
;
20156 cp_token
*token
= NULL
;
20157 cp_token
*decl_spec_token_start
= NULL
;
20158 cp_token
*initializer_token_start
= NULL
;
20159 int saved_pedantic
;
20160 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20162 /* Check for the `__extension__' keyword. */
20163 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
20166 cp_parser_member_declaration (parser
);
20167 /* Restore the old value of the PEDANTIC flag. */
20168 pedantic
= saved_pedantic
;
20173 /* Check for a template-declaration. */
20174 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20176 /* An explicit specialization here is an error condition, and we
20177 expect the specialization handler to detect and report this. */
20178 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
20179 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
20180 cp_parser_explicit_specialization (parser
);
20182 cp_parser_template_declaration (parser
, /*member_p=*/true);
20187 /* Check for a using-declaration. */
20188 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
20190 if (cxx_dialect
< cxx11
)
20192 /* Parse the using-declaration. */
20193 cp_parser_using_declaration (parser
,
20194 /*access_declaration_p=*/false);
20200 bool alias_decl_expected
;
20201 cp_parser_parse_tentatively (parser
);
20202 decl
= cp_parser_alias_declaration (parser
);
20203 /* Note that if we actually see the '=' token after the
20204 identifier, cp_parser_alias_declaration commits the
20205 tentative parse. In that case, we really expects an
20206 alias-declaration. Otherwise, we expect a using
20208 alias_decl_expected
=
20209 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
20210 cp_parser_parse_definitely (parser
);
20212 if (alias_decl_expected
)
20213 finish_member_declaration (decl
);
20215 cp_parser_using_declaration (parser
,
20216 /*access_declaration_p=*/false);
20221 /* Check for @defs. */
20222 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
20225 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
20226 ivar
= ivar_chains
;
20230 ivar
= TREE_CHAIN (member
);
20231 TREE_CHAIN (member
) = NULL_TREE
;
20232 finish_member_declaration (member
);
20237 /* If the next token is `static_assert' we have a static assertion. */
20238 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
20240 cp_parser_static_assert (parser
, /*member_p=*/true);
20244 parser
->colon_corrects_to_scope_p
= false;
20246 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20249 /* Parse the decl-specifier-seq. */
20250 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20251 cp_parser_decl_specifier_seq (parser
,
20252 CP_PARSER_FLAGS_OPTIONAL
,
20254 &declares_class_or_enum
);
20255 /* Check for an invalid type-name. */
20256 if (!decl_specifiers
.any_type_specifiers_p
20257 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20259 /* If there is no declarator, then the decl-specifier-seq should
20261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20263 /* If there was no decl-specifier-seq, and the next token is a
20264 `;', then we have something like:
20270 Each member-declaration shall declare at least one member
20271 name of the class. */
20272 if (!decl_specifiers
.any_specifiers_p
)
20274 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20275 if (!in_system_header_at (token
->location
))
20276 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20282 /* See if this declaration is a friend. */
20283 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20284 /* If there were decl-specifiers, check to see if there was
20285 a class-declaration. */
20286 type
= check_tag_decl (&decl_specifiers
,
20287 /*explicit_type_instantiation_p=*/false);
20288 /* Nested classes have already been added to the class, but
20289 a `friend' needs to be explicitly registered. */
20292 /* If the `friend' keyword was present, the friend must
20293 be introduced with a class-key. */
20294 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20295 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20296 "in C++03 a class-key must be used "
20297 "when declaring a friend");
20300 template <typename T> struct A {
20301 friend struct A<T>::B;
20304 A<T>::B will be represented by a TYPENAME_TYPE, and
20305 therefore not recognized by check_tag_decl. */
20308 type
= decl_specifiers
.type
;
20309 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20310 type
= TREE_TYPE (type
);
20312 if (!type
|| !TYPE_P (type
))
20313 error_at (decl_spec_token_start
->location
,
20314 "friend declaration does not name a class or "
20317 make_friend_class (current_class_type
, type
,
20318 /*complain=*/true);
20320 /* If there is no TYPE, an error message will already have
20322 else if (!type
|| type
== error_mark_node
)
20324 /* An anonymous aggregate has to be handled specially; such
20325 a declaration really declares a data member (with a
20326 particular type), as opposed to a nested class. */
20327 else if (ANON_AGGR_TYPE_P (type
))
20330 if (decl_specifiers
.storage_class
!= sc_none
)
20331 error_at (decl_spec_token_start
->location
,
20332 "a storage class on an anonymous aggregate "
20333 "in class scope is not allowed");
20335 /* Remove constructors and such from TYPE, now that we
20336 know it is an anonymous aggregate. */
20337 fixup_anonymous_aggr (type
);
20338 /* And make the corresponding data member. */
20339 decl
= build_decl (decl_spec_token_start
->location
,
20340 FIELD_DECL
, NULL_TREE
, type
);
20341 /* Add it to the class. */
20342 finish_member_declaration (decl
);
20345 cp_parser_check_access_in_redeclaration
20347 decl_spec_token_start
->location
);
20352 bool assume_semicolon
= false;
20354 /* Clear attributes from the decl_specifiers but keep them
20355 around as prefix attributes that apply them to the entity
20357 prefix_attributes
= decl_specifiers
.attributes
;
20358 decl_specifiers
.attributes
= NULL_TREE
;
20360 /* See if these declarations will be friends. */
20361 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20363 /* Keep going until we hit the `;' at the end of the
20365 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20367 tree attributes
= NULL_TREE
;
20368 tree first_attribute
;
20370 /* Peek at the next token. */
20371 token
= cp_lexer_peek_token (parser
->lexer
);
20373 /* Check for a bitfield declaration. */
20374 if (token
->type
== CPP_COLON
20375 || (token
->type
== CPP_NAME
20376 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20382 /* Get the name of the bitfield. Note that we cannot just
20383 check TOKEN here because it may have been invalidated by
20384 the call to cp_lexer_peek_nth_token above. */
20385 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20386 identifier
= cp_parser_identifier (parser
);
20388 identifier
= NULL_TREE
;
20390 /* Consume the `:' token. */
20391 cp_lexer_consume_token (parser
->lexer
);
20392 /* Get the width of the bitfield. */
20394 = cp_parser_constant_expression (parser
,
20395 /*allow_non_constant=*/false,
20398 /* Look for attributes that apply to the bitfield. */
20399 attributes
= cp_parser_attributes_opt (parser
);
20400 /* Remember which attributes are prefix attributes and
20402 first_attribute
= attributes
;
20403 /* Combine the attributes. */
20404 attributes
= chainon (prefix_attributes
, attributes
);
20406 /* Create the bitfield declaration. */
20407 decl
= grokbitfield (identifier
20408 ? make_id_declarator (NULL_TREE
,
20418 cp_declarator
*declarator
;
20420 tree asm_specification
;
20421 int ctor_dtor_or_conv_p
;
20423 /* Parse the declarator. */
20425 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20426 &ctor_dtor_or_conv_p
,
20427 /*parenthesized_p=*/NULL
,
20428 /*member_p=*/true);
20430 /* If something went wrong parsing the declarator, make sure
20431 that we at least consume some tokens. */
20432 if (declarator
== cp_error_declarator
)
20434 /* Skip to the end of the statement. */
20435 cp_parser_skip_to_end_of_statement (parser
);
20436 /* If the next token is not a semicolon, that is
20437 probably because we just skipped over the body of
20438 a function. So, we consume a semicolon if
20439 present, but do not issue an error message if it
20441 if (cp_lexer_next_token_is (parser
->lexer
,
20443 cp_lexer_consume_token (parser
->lexer
);
20447 if (declares_class_or_enum
& 2)
20448 cp_parser_check_for_definition_in_return_type
20449 (declarator
, decl_specifiers
.type
,
20450 decl_specifiers
.locations
[ds_type_spec
]);
20452 /* Look for an asm-specification. */
20453 asm_specification
= cp_parser_asm_specification_opt (parser
);
20454 /* Look for attributes that apply to the declaration. */
20455 attributes
= cp_parser_attributes_opt (parser
);
20456 /* Remember which attributes are prefix attributes and
20458 first_attribute
= attributes
;
20459 /* Combine the attributes. */
20460 attributes
= chainon (prefix_attributes
, attributes
);
20462 /* If it's an `=', then we have a constant-initializer or a
20463 pure-specifier. It is not correct to parse the
20464 initializer before registering the member declaration
20465 since the member declaration should be in scope while
20466 its initializer is processed. However, the rest of the
20467 front end does not yet provide an interface that allows
20468 us to handle this correctly. */
20469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20473 A pure-specifier shall be used only in the declaration of
20474 a virtual function.
20476 A member-declarator can contain a constant-initializer
20477 only if it declares a static member of integral or
20480 Therefore, if the DECLARATOR is for a function, we look
20481 for a pure-specifier; otherwise, we look for a
20482 constant-initializer. When we call `grokfield', it will
20483 perform more stringent semantics checks. */
20484 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
20485 if (function_declarator_p (declarator
)
20486 || (decl_specifiers
.type
20487 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
20488 && declarator
->kind
== cdk_id
20489 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
20490 == FUNCTION_TYPE
)))
20491 initializer
= cp_parser_pure_specifier (parser
);
20492 else if (decl_specifiers
.storage_class
!= sc_static
)
20493 initializer
= cp_parser_save_nsdmi (parser
);
20494 else if (cxx_dialect
>= cxx11
)
20497 /* Don't require a constant rvalue in C++11, since we
20498 might want a reference constant. We'll enforce
20499 constancy later. */
20500 cp_lexer_consume_token (parser
->lexer
);
20501 /* Parse the initializer. */
20502 initializer
= cp_parser_initializer_clause (parser
,
20506 /* Parse the initializer. */
20507 initializer
= cp_parser_constant_initializer (parser
);
20509 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20510 && !function_declarator_p (declarator
))
20513 if (decl_specifiers
.storage_class
!= sc_static
)
20514 initializer
= cp_parser_save_nsdmi (parser
);
20516 initializer
= cp_parser_initializer (parser
, &x
, &x
);
20518 /* Otherwise, there is no initializer. */
20520 initializer
= NULL_TREE
;
20522 /* See if we are probably looking at a function
20523 definition. We are certainly not looking at a
20524 member-declarator. Calling `grokfield' has
20525 side-effects, so we must not do it unless we are sure
20526 that we are looking at a member-declarator. */
20527 if (cp_parser_token_starts_function_definition_p
20528 (cp_lexer_peek_token (parser
->lexer
)))
20530 /* The grammar does not allow a pure-specifier to be
20531 used when a member function is defined. (It is
20532 possible that this fact is an oversight in the
20533 standard, since a pure function may be defined
20534 outside of the class-specifier. */
20535 if (initializer
&& initializer_token_start
)
20536 error_at (initializer_token_start
->location
,
20537 "pure-specifier on function-definition");
20538 decl
= cp_parser_save_member_function_body (parser
,
20542 if (parser
->fully_implicit_function_template_p
)
20543 decl
= finish_fully_implicit_template (parser
, decl
);
20544 /* If the member was not a friend, declare it here. */
20546 finish_member_declaration (decl
);
20547 /* Peek at the next token. */
20548 token
= cp_lexer_peek_token (parser
->lexer
);
20549 /* If the next token is a semicolon, consume it. */
20550 if (token
->type
== CPP_SEMICOLON
)
20551 cp_lexer_consume_token (parser
->lexer
);
20555 if (declarator
->kind
== cdk_function
)
20556 declarator
->id_loc
= token
->location
;
20557 /* Create the declaration. */
20558 decl
= grokfield (declarator
, &decl_specifiers
,
20559 initializer
, /*init_const_expr_p=*/true,
20560 asm_specification
, attributes
);
20561 if (parser
->fully_implicit_function_template_p
)
20564 finish_fully_implicit_template (parser
, 0);
20566 decl
= finish_fully_implicit_template (parser
, decl
);
20570 cp_finalize_omp_declare_simd (parser
, decl
);
20572 /* Reset PREFIX_ATTRIBUTES. */
20573 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
20574 attributes
= TREE_CHAIN (attributes
);
20576 TREE_CHAIN (attributes
) = NULL_TREE
;
20578 /* If there is any qualification still in effect, clear it
20579 now; we will be starting fresh with the next declarator. */
20580 parser
->scope
= NULL_TREE
;
20581 parser
->qualifying_scope
= NULL_TREE
;
20582 parser
->object_scope
= NULL_TREE
;
20583 /* If it's a `,', then there are more declarators. */
20584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20586 cp_lexer_consume_token (parser
->lexer
);
20587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20589 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
20590 error_at (token
->location
,
20591 "stray %<,%> at end of member declaration");
20594 /* If the next token isn't a `;', then we have a parse error. */
20595 else if (cp_lexer_next_token_is_not (parser
->lexer
,
20598 /* The next token might be a ways away from where the
20599 actual semicolon is missing. Find the previous token
20600 and use that for our error position. */
20601 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
20602 error_at (token
->location
,
20603 "expected %<;%> at end of member declaration");
20605 /* Assume that the user meant to provide a semicolon. If
20606 we were to cp_parser_skip_to_end_of_statement, we might
20607 skip to a semicolon inside a member function definition
20608 and issue nonsensical error messages. */
20609 assume_semicolon
= true;
20614 /* Add DECL to the list of members. */
20616 finish_member_declaration (decl
);
20618 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20619 cp_parser_save_default_args (parser
, decl
);
20620 else if (TREE_CODE (decl
) == FIELD_DECL
20621 && !DECL_C_BIT_FIELD (decl
)
20622 && DECL_INITIAL (decl
))
20623 /* Add DECL to the queue of NSDMI to be parsed later. */
20624 vec_safe_push (unparsed_nsdmis
, decl
);
20627 if (assume_semicolon
)
20632 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20634 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20637 /* Parse a pure-specifier.
20642 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20643 Otherwise, ERROR_MARK_NODE is returned. */
20646 cp_parser_pure_specifier (cp_parser
* parser
)
20650 /* Look for the `=' token. */
20651 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
20652 return error_mark_node
;
20653 /* Look for the `0' token. */
20654 token
= cp_lexer_peek_token (parser
->lexer
);
20656 if (token
->type
== CPP_EOF
20657 || token
->type
== CPP_PRAGMA_EOL
)
20658 return error_mark_node
;
20660 cp_lexer_consume_token (parser
->lexer
);
20662 /* Accept = default or = delete in c++0x mode. */
20663 if (token
->keyword
== RID_DEFAULT
20664 || token
->keyword
== RID_DELETE
)
20666 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
20667 return token
->u
.value
;
20670 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20671 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
20673 cp_parser_error (parser
,
20674 "invalid pure specifier (only %<= 0%> is allowed)");
20675 cp_parser_skip_to_end_of_statement (parser
);
20676 return error_mark_node
;
20678 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20680 error_at (token
->location
, "templates may not be %<virtual%>");
20681 return error_mark_node
;
20684 return integer_zero_node
;
20687 /* Parse a constant-initializer.
20689 constant-initializer:
20690 = constant-expression
20692 Returns a representation of the constant-expression. */
20695 cp_parser_constant_initializer (cp_parser
* parser
)
20697 /* Look for the `=' token. */
20698 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
20699 return error_mark_node
;
20701 /* It is invalid to write:
20703 struct S { static const int i = { 7 }; };
20706 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20708 cp_parser_error (parser
,
20709 "a brace-enclosed initializer is not allowed here");
20710 /* Consume the opening brace. */
20711 cp_lexer_consume_token (parser
->lexer
);
20712 /* Skip the initializer. */
20713 cp_parser_skip_to_closing_brace (parser
);
20714 /* Look for the trailing `}'. */
20715 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20717 return error_mark_node
;
20720 return cp_parser_constant_expression (parser
,
20721 /*allow_non_constant=*/false,
20725 /* Derived classes [gram.class.derived] */
20727 /* Parse a base-clause.
20730 : base-specifier-list
20732 base-specifier-list:
20733 base-specifier ... [opt]
20734 base-specifier-list , base-specifier ... [opt]
20736 Returns a TREE_LIST representing the base-classes, in the order in
20737 which they were declared. The representation of each node is as
20738 described by cp_parser_base_specifier.
20740 In the case that no bases are specified, this function will return
20741 NULL_TREE, not ERROR_MARK_NODE. */
20744 cp_parser_base_clause (cp_parser
* parser
)
20746 tree bases
= NULL_TREE
;
20748 /* Look for the `:' that begins the list. */
20749 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20751 /* Scan the base-specifier-list. */
20756 bool pack_expansion_p
= false;
20758 /* Look for the base-specifier. */
20759 base
= cp_parser_base_specifier (parser
);
20760 /* Look for the (optional) ellipsis. */
20761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20763 /* Consume the `...'. */
20764 cp_lexer_consume_token (parser
->lexer
);
20766 pack_expansion_p
= true;
20769 /* Add BASE to the front of the list. */
20770 if (base
&& base
!= error_mark_node
)
20772 if (pack_expansion_p
)
20773 /* Make this a pack expansion type. */
20774 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
20776 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
20778 TREE_CHAIN (base
) = bases
;
20782 /* Peek at the next token. */
20783 token
= cp_lexer_peek_token (parser
->lexer
);
20784 /* If it's not a comma, then the list is complete. */
20785 if (token
->type
!= CPP_COMMA
)
20787 /* Consume the `,'. */
20788 cp_lexer_consume_token (parser
->lexer
);
20791 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20792 base class had a qualified name. However, the next name that
20793 appears is certainly not qualified. */
20794 parser
->scope
= NULL_TREE
;
20795 parser
->qualifying_scope
= NULL_TREE
;
20796 parser
->object_scope
= NULL_TREE
;
20798 return nreverse (bases
);
20801 /* Parse a base-specifier.
20804 :: [opt] nested-name-specifier [opt] class-name
20805 virtual access-specifier [opt] :: [opt] nested-name-specifier
20807 access-specifier virtual [opt] :: [opt] nested-name-specifier
20810 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20811 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20812 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20813 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20816 cp_parser_base_specifier (cp_parser
* parser
)
20820 bool virtual_p
= false;
20821 bool duplicate_virtual_error_issued_p
= false;
20822 bool duplicate_access_error_issued_p
= false;
20823 bool class_scope_p
, template_p
;
20824 tree access
= access_default_node
;
20827 /* Process the optional `virtual' and `access-specifier'. */
20830 /* Peek at the next token. */
20831 token
= cp_lexer_peek_token (parser
->lexer
);
20832 /* Process `virtual'. */
20833 switch (token
->keyword
)
20836 /* If `virtual' appears more than once, issue an error. */
20837 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
20839 cp_parser_error (parser
,
20840 "%<virtual%> specified more than once in base-specified");
20841 duplicate_virtual_error_issued_p
= true;
20846 /* Consume the `virtual' token. */
20847 cp_lexer_consume_token (parser
->lexer
);
20852 case RID_PROTECTED
:
20854 /* If more than one access specifier appears, issue an
20856 if (access
!= access_default_node
20857 && !duplicate_access_error_issued_p
)
20859 cp_parser_error (parser
,
20860 "more than one access specifier in base-specified");
20861 duplicate_access_error_issued_p
= true;
20864 access
= ridpointers
[(int) token
->keyword
];
20866 /* Consume the access-specifier. */
20867 cp_lexer_consume_token (parser
->lexer
);
20876 /* It is not uncommon to see programs mechanically, erroneously, use
20877 the 'typename' keyword to denote (dependent) qualified types
20878 as base classes. */
20879 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
20881 token
= cp_lexer_peek_token (parser
->lexer
);
20882 if (!processing_template_decl
)
20883 error_at (token
->location
,
20884 "keyword %<typename%> not allowed outside of templates");
20886 error_at (token
->location
,
20887 "keyword %<typename%> not allowed in this context "
20888 "(the base class is implicitly a type)");
20889 cp_lexer_consume_token (parser
->lexer
);
20892 /* Look for the optional `::' operator. */
20893 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
20894 /* Look for the nested-name-specifier. The simplest way to
20899 The keyword `typename' is not permitted in a base-specifier or
20900 mem-initializer; in these contexts a qualified name that
20901 depends on a template-parameter is implicitly assumed to be a
20904 is to pretend that we have seen the `typename' keyword at this
20906 cp_parser_nested_name_specifier_opt (parser
,
20907 /*typename_keyword_p=*/true,
20908 /*check_dependency_p=*/true,
20910 /*is_declaration=*/true);
20911 /* If the base class is given by a qualified name, assume that names
20912 we see are type names or templates, as appropriate. */
20913 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
20914 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
20917 && cp_lexer_next_token_is_decltype (parser
->lexer
))
20918 /* DR 950 allows decltype as a base-specifier. */
20919 type
= cp_parser_decltype (parser
);
20922 /* Otherwise, look for the class-name. */
20923 type
= cp_parser_class_name (parser
,
20927 /*check_dependency_p=*/true,
20928 /*class_head_p=*/false,
20929 /*is_declaration=*/true);
20930 type
= TREE_TYPE (type
);
20933 if (type
== error_mark_node
)
20934 return error_mark_node
;
20936 return finish_base_specifier (type
, access
, virtual_p
);
20939 /* Exception handling [gram.exception] */
20941 /* Parse an (optional) noexcept-specification.
20943 noexcept-specification:
20944 noexcept ( constant-expression ) [opt]
20946 If no noexcept-specification is present, returns NULL_TREE.
20947 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20948 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20949 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20950 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20951 in which case a boolean condition is returned instead. */
20954 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
20955 bool require_constexpr
,
20956 bool* consumed_expr
,
20960 const char *saved_message
;
20962 /* Peek at the next token. */
20963 token
= cp_lexer_peek_token (parser
->lexer
);
20965 /* Is it a noexcept-specification? */
20966 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
20969 cp_lexer_consume_token (parser
->lexer
);
20971 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
20973 cp_lexer_consume_token (parser
->lexer
);
20975 if (require_constexpr
)
20977 /* Types may not be defined in an exception-specification. */
20978 saved_message
= parser
->type_definition_forbidden_message
;
20979 parser
->type_definition_forbidden_message
20980 = G_("types may not be defined in an exception-specification");
20982 expr
= cp_parser_constant_expression (parser
, false, NULL
);
20984 /* Restore the saved message. */
20985 parser
->type_definition_forbidden_message
= saved_message
;
20989 expr
= cp_parser_expression (parser
, false, NULL
);
20990 *consumed_expr
= true;
20993 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20997 expr
= boolean_true_node
;
20998 if (!require_constexpr
)
20999 *consumed_expr
= false;
21002 /* We cannot build a noexcept-spec right away because this will check
21003 that expr is a constexpr. */
21005 return build_noexcept_spec (expr
, tf_warning_or_error
);
21013 /* Parse an (optional) exception-specification.
21015 exception-specification:
21016 throw ( type-id-list [opt] )
21018 Returns a TREE_LIST representing the exception-specification. The
21019 TREE_VALUE of each node is a type. */
21022 cp_parser_exception_specification_opt (cp_parser
* parser
)
21026 const char *saved_message
;
21028 /* Peek at the next token. */
21029 token
= cp_lexer_peek_token (parser
->lexer
);
21031 /* Is it a noexcept-specification? */
21032 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
21034 if (type_id_list
!= NULL_TREE
)
21035 return type_id_list
;
21037 /* If it's not `throw', then there's no exception-specification. */
21038 if (!cp_parser_is_keyword (token
, RID_THROW
))
21042 /* Enable this once a lot of code has transitioned to noexcept? */
21043 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
21044 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
21045 "deprecated in C++0x; use %<noexcept%> instead");
21048 /* Consume the `throw'. */
21049 cp_lexer_consume_token (parser
->lexer
);
21051 /* Look for the `('. */
21052 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21054 /* Peek at the next token. */
21055 token
= cp_lexer_peek_token (parser
->lexer
);
21056 /* If it's not a `)', then there is a type-id-list. */
21057 if (token
->type
!= CPP_CLOSE_PAREN
)
21059 /* Types may not be defined in an exception-specification. */
21060 saved_message
= parser
->type_definition_forbidden_message
;
21061 parser
->type_definition_forbidden_message
21062 = G_("types may not be defined in an exception-specification");
21063 /* Parse the type-id-list. */
21064 type_id_list
= cp_parser_type_id_list (parser
);
21065 /* Restore the saved message. */
21066 parser
->type_definition_forbidden_message
= saved_message
;
21069 type_id_list
= empty_except_spec
;
21071 /* Look for the `)'. */
21072 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21074 return type_id_list
;
21077 /* Parse an (optional) type-id-list.
21081 type-id-list , type-id ... [opt]
21083 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21084 in the order that the types were presented. */
21087 cp_parser_type_id_list (cp_parser
* parser
)
21089 tree types
= NULL_TREE
;
21096 /* Get the next type-id. */
21097 type
= cp_parser_type_id (parser
);
21098 /* Parse the optional ellipsis. */
21099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21101 /* Consume the `...'. */
21102 cp_lexer_consume_token (parser
->lexer
);
21104 /* Turn the type into a pack expansion expression. */
21105 type
= make_pack_expansion (type
);
21107 /* Add it to the list. */
21108 types
= add_exception_specifier (types
, type
, /*complain=*/1);
21109 /* Peek at the next token. */
21110 token
= cp_lexer_peek_token (parser
->lexer
);
21111 /* If it is not a `,', we are done. */
21112 if (token
->type
!= CPP_COMMA
)
21114 /* Consume the `,'. */
21115 cp_lexer_consume_token (parser
->lexer
);
21118 return nreverse (types
);
21121 /* Parse a try-block.
21124 try compound-statement handler-seq */
21127 cp_parser_try_block (cp_parser
* parser
)
21131 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
21132 try_block
= begin_try_block ();
21133 cp_parser_compound_statement (parser
, NULL
, true, false);
21134 finish_try_block (try_block
);
21135 cp_parser_handler_seq (parser
);
21136 finish_handler_sequence (try_block
);
21141 /* Parse a function-try-block.
21143 function-try-block:
21144 try ctor-initializer [opt] function-body handler-seq */
21147 cp_parser_function_try_block (cp_parser
* parser
)
21149 tree compound_stmt
;
21151 bool ctor_initializer_p
;
21153 /* Look for the `try' keyword. */
21154 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
21156 /* Let the rest of the front end know where we are. */
21157 try_block
= begin_function_try_block (&compound_stmt
);
21158 /* Parse the function-body. */
21159 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21160 (parser
, /*in_function_try_block=*/true);
21161 /* We're done with the `try' part. */
21162 finish_function_try_block (try_block
);
21163 /* Parse the handlers. */
21164 cp_parser_handler_seq (parser
);
21165 /* We're done with the handlers. */
21166 finish_function_handler_sequence (try_block
, compound_stmt
);
21168 return ctor_initializer_p
;
21171 /* Parse a handler-seq.
21174 handler handler-seq [opt] */
21177 cp_parser_handler_seq (cp_parser
* parser
)
21183 /* Parse the handler. */
21184 cp_parser_handler (parser
);
21185 /* Peek at the next token. */
21186 token
= cp_lexer_peek_token (parser
->lexer
);
21187 /* If it's not `catch' then there are no more handlers. */
21188 if (!cp_parser_is_keyword (token
, RID_CATCH
))
21193 /* Parse a handler.
21196 catch ( exception-declaration ) compound-statement */
21199 cp_parser_handler (cp_parser
* parser
)
21204 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
21205 handler
= begin_handler ();
21206 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21207 declaration
= cp_parser_exception_declaration (parser
);
21208 finish_handler_parms (declaration
, handler
);
21209 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21210 cp_parser_compound_statement (parser
, NULL
, false, false);
21211 finish_handler (handler
);
21214 /* Parse an exception-declaration.
21216 exception-declaration:
21217 type-specifier-seq declarator
21218 type-specifier-seq abstract-declarator
21222 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21223 ellipsis variant is used. */
21226 cp_parser_exception_declaration (cp_parser
* parser
)
21228 cp_decl_specifier_seq type_specifiers
;
21229 cp_declarator
*declarator
;
21230 const char *saved_message
;
21232 /* If it's an ellipsis, it's easy to handle. */
21233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21235 /* Consume the `...' token. */
21236 cp_lexer_consume_token (parser
->lexer
);
21240 /* Types may not be defined in exception-declarations. */
21241 saved_message
= parser
->type_definition_forbidden_message
;
21242 parser
->type_definition_forbidden_message
21243 = G_("types may not be defined in exception-declarations");
21245 /* Parse the type-specifier-seq. */
21246 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
21247 /*is_trailing_return=*/false,
21249 /* If it's a `)', then there is no declarator. */
21250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21253 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21254 /*ctor_dtor_or_conv_p=*/NULL
,
21255 /*parenthesized_p=*/NULL
,
21256 /*member_p=*/false);
21258 /* Restore the saved message. */
21259 parser
->type_definition_forbidden_message
= saved_message
;
21261 if (!type_specifiers
.any_specifiers_p
)
21262 return error_mark_node
;
21264 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21267 /* Parse a throw-expression.
21270 throw assignment-expression [opt]
21272 Returns a THROW_EXPR representing the throw-expression. */
21275 cp_parser_throw_expression (cp_parser
* parser
)
21280 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21281 token
= cp_lexer_peek_token (parser
->lexer
);
21282 /* Figure out whether or not there is an assignment-expression
21283 following the "throw" keyword. */
21284 if (token
->type
== CPP_COMMA
21285 || token
->type
== CPP_SEMICOLON
21286 || token
->type
== CPP_CLOSE_PAREN
21287 || token
->type
== CPP_CLOSE_SQUARE
21288 || token
->type
== CPP_CLOSE_BRACE
21289 || token
->type
== CPP_COLON
)
21290 expression
= NULL_TREE
;
21292 expression
= cp_parser_assignment_expression (parser
,
21293 /*cast_p=*/false, NULL
);
21295 return build_throw (expression
);
21298 /* GNU Extensions */
21300 /* Parse an (optional) asm-specification.
21303 asm ( string-literal )
21305 If the asm-specification is present, returns a STRING_CST
21306 corresponding to the string-literal. Otherwise, returns
21310 cp_parser_asm_specification_opt (cp_parser
* parser
)
21313 tree asm_specification
;
21315 /* Peek at the next token. */
21316 token
= cp_lexer_peek_token (parser
->lexer
);
21317 /* If the next token isn't the `asm' keyword, then there's no
21318 asm-specification. */
21319 if (!cp_parser_is_keyword (token
, RID_ASM
))
21322 /* Consume the `asm' token. */
21323 cp_lexer_consume_token (parser
->lexer
);
21324 /* Look for the `('. */
21325 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21327 /* Look for the string-literal. */
21328 asm_specification
= cp_parser_string_literal (parser
, false, false);
21330 /* Look for the `)'. */
21331 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21333 return asm_specification
;
21336 /* Parse an asm-operand-list.
21340 asm-operand-list , asm-operand
21343 string-literal ( expression )
21344 [ string-literal ] string-literal ( expression )
21346 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21347 each node is the expression. The TREE_PURPOSE is itself a
21348 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21349 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21350 is a STRING_CST for the string literal before the parenthesis. Returns
21351 ERROR_MARK_NODE if any of the operands are invalid. */
21354 cp_parser_asm_operand_list (cp_parser
* parser
)
21356 tree asm_operands
= NULL_TREE
;
21357 bool invalid_operands
= false;
21361 tree string_literal
;
21365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21367 /* Consume the `[' token. */
21368 cp_lexer_consume_token (parser
->lexer
);
21369 /* Read the operand name. */
21370 name
= cp_parser_identifier (parser
);
21371 if (name
!= error_mark_node
)
21372 name
= build_string (IDENTIFIER_LENGTH (name
),
21373 IDENTIFIER_POINTER (name
));
21374 /* Look for the closing `]'. */
21375 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21379 /* Look for the string-literal. */
21380 string_literal
= cp_parser_string_literal (parser
, false, false);
21382 /* Look for the `('. */
21383 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21384 /* Parse the expression. */
21385 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
21386 /* Look for the `)'. */
21387 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21389 if (name
== error_mark_node
21390 || string_literal
== error_mark_node
21391 || expression
== error_mark_node
)
21392 invalid_operands
= true;
21394 /* Add this operand to the list. */
21395 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21398 /* If the next token is not a `,', there are no more
21400 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21402 /* Consume the `,'. */
21403 cp_lexer_consume_token (parser
->lexer
);
21406 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21409 /* Parse an asm-clobber-list.
21413 asm-clobber-list , string-literal
21415 Returns a TREE_LIST, indicating the clobbers in the order that they
21416 appeared. The TREE_VALUE of each node is a STRING_CST. */
21419 cp_parser_asm_clobber_list (cp_parser
* parser
)
21421 tree clobbers
= NULL_TREE
;
21425 tree string_literal
;
21427 /* Look for the string literal. */
21428 string_literal
= cp_parser_string_literal (parser
, false, false);
21429 /* Add it to the list. */
21430 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21431 /* If the next token is not a `,', then the list is
21433 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21435 /* Consume the `,' token. */
21436 cp_lexer_consume_token (parser
->lexer
);
21442 /* Parse an asm-label-list.
21446 asm-label-list , identifier
21448 Returns a TREE_LIST, indicating the labels in the order that they
21449 appeared. The TREE_VALUE of each node is a label. */
21452 cp_parser_asm_label_list (cp_parser
* parser
)
21454 tree labels
= NULL_TREE
;
21458 tree identifier
, label
, name
;
21460 /* Look for the identifier. */
21461 identifier
= cp_parser_identifier (parser
);
21462 if (!error_operand_p (identifier
))
21464 label
= lookup_label (identifier
);
21465 if (TREE_CODE (label
) == LABEL_DECL
)
21467 TREE_USED (label
) = 1;
21468 check_goto (label
);
21469 name
= build_string (IDENTIFIER_LENGTH (identifier
),
21470 IDENTIFIER_POINTER (identifier
));
21471 labels
= tree_cons (name
, label
, labels
);
21474 /* If the next token is not a `,', then the list is
21476 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21478 /* Consume the `,' token. */
21479 cp_lexer_consume_token (parser
->lexer
);
21482 return nreverse (labels
);
21485 /* Return TRUE iff the next tokens in the stream are possibly the
21486 beginning of a GNU extension attribute. */
21489 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
21491 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
21494 /* Return TRUE iff the next tokens in the stream are possibly the
21495 beginning of a standard C++-11 attribute specifier. */
21498 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
21500 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
21503 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21504 beginning of a standard C++-11 attribute specifier. */
21507 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
21509 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21511 return (cxx_dialect
>= cxx11
21512 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
21513 || (token
->type
== CPP_OPEN_SQUARE
21514 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
21515 && token
->type
== CPP_OPEN_SQUARE
)));
21518 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21519 beginning of a GNU extension attribute. */
21522 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
21524 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21526 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
21529 /* Return true iff the next tokens can be the beginning of either a
21530 GNU attribute list, or a standard C++11 attribute sequence. */
21533 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
21535 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
21536 || cp_next_tokens_can_be_std_attribute_p (parser
));
21539 /* Return true iff the next Nth tokens can be the beginning of either
21540 a GNU attribute list, or a standard C++11 attribute sequence. */
21543 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
21545 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
21546 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
21549 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21550 of GNU attributes, or return NULL. */
21553 cp_parser_attributes_opt (cp_parser
*parser
)
21555 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
21556 return cp_parser_gnu_attributes_opt (parser
);
21557 return cp_parser_std_attribute_spec_seq (parser
);
21560 #define CILK_SIMD_FN_CLAUSE_MASK \
21561 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21562 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21563 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21564 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21565 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21567 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21568 vector [(<clauses>)] */
21571 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
21573 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
21574 cp_token
*token
= v_token
;
21577 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
21578 parser
->cilk_simd_fn_info
->error_seen
= false;
21579 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
21580 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
21582 int paren_scope
= 0;
21583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21585 cp_lexer_consume_token (parser
->lexer
);
21586 v_token
= cp_lexer_peek_token (parser
->lexer
);
21589 while (paren_scope
> 0)
21591 token
= cp_lexer_peek_token (parser
->lexer
);
21592 if (token
->type
== CPP_OPEN_PAREN
)
21594 else if (token
->type
== CPP_CLOSE_PAREN
)
21596 /* Do not push the last ')' */
21597 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
21598 cp_lexer_consume_token (parser
->lexer
);
21601 token
->type
= CPP_PRAGMA_EOL
;
21602 parser
->lexer
->next_token
= token
;
21603 cp_lexer_consume_token (parser
->lexer
);
21605 struct cp_token_cache
*cp
21606 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
21607 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
21610 /* Parse an (optional) series of attributes.
21613 attributes attribute
21616 __attribute__ (( attribute-list [opt] ))
21618 The return value is as for cp_parser_gnu_attribute_list. */
21621 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
21623 tree attributes
= NULL_TREE
;
21628 tree attribute_list
;
21631 /* Peek at the next token. */
21632 token
= cp_lexer_peek_token (parser
->lexer
);
21633 /* If it's not `__attribute__', then we're done. */
21634 if (token
->keyword
!= RID_ATTRIBUTE
)
21637 /* Consume the `__attribute__' keyword. */
21638 cp_lexer_consume_token (parser
->lexer
);
21639 /* Look for the two `(' tokens. */
21640 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21641 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21643 /* Peek at the next token. */
21644 token
= cp_lexer_peek_token (parser
->lexer
);
21645 if (token
->type
!= CPP_CLOSE_PAREN
)
21646 /* Parse the attribute-list. */
21647 attribute_list
= cp_parser_gnu_attribute_list (parser
);
21649 /* If the next token is a `)', then there is no attribute
21651 attribute_list
= NULL
;
21653 /* Look for the two `)' tokens. */
21654 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
21656 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
21659 cp_parser_skip_to_end_of_statement (parser
);
21661 /* Add these new attributes to the list. */
21662 attributes
= chainon (attributes
, attribute_list
);
21668 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21669 "__vector" or "__vector__." */
21672 is_cilkplus_vector_p (tree name
)
21674 if (flag_cilkplus
&& is_attribute_p ("vector", name
))
21679 /* Parse a GNU attribute-list.
21683 attribute-list , attribute
21687 identifier ( identifier )
21688 identifier ( identifier , expression-list )
21689 identifier ( expression-list )
21691 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21692 to an attribute. The TREE_PURPOSE of each node is the identifier
21693 indicating which attribute is in use. The TREE_VALUE represents
21694 the arguments, if any. */
21697 cp_parser_gnu_attribute_list (cp_parser
* parser
)
21699 tree attribute_list
= NULL_TREE
;
21700 bool save_translate_strings_p
= parser
->translate_strings_p
;
21702 parser
->translate_strings_p
= false;
21709 /* Look for the identifier. We also allow keywords here; for
21710 example `__attribute__ ((const))' is legal. */
21711 token
= cp_lexer_peek_token (parser
->lexer
);
21712 if (token
->type
== CPP_NAME
21713 || token
->type
== CPP_KEYWORD
)
21715 tree arguments
= NULL_TREE
;
21717 /* Consume the token, but save it since we need it for the
21718 SIMD enabled function parsing. */
21719 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
21721 /* Save away the identifier that indicates which attribute
21723 identifier
= (token
->type
== CPP_KEYWORD
)
21724 /* For keywords, use the canonical spelling, not the
21725 parsed identifier. */
21726 ? ridpointers
[(int) token
->keyword
]
21727 : id_token
->u
.value
;
21729 attribute
= build_tree_list (identifier
, NULL_TREE
);
21731 /* Peek at the next token. */
21732 token
= cp_lexer_peek_token (parser
->lexer
);
21733 /* If it's an `(', then parse the attribute arguments. */
21734 if (token
->type
== CPP_OPEN_PAREN
)
21736 vec
<tree
, va_gc
> *vec
;
21737 int attr_flag
= (attribute_takes_identifier_p (identifier
)
21738 ? id_attr
: normal_attr
);
21739 if (is_cilkplus_vector_p (identifier
))
21741 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
21745 vec
= cp_parser_parenthesized_expression_list
21746 (parser
, attr_flag
, /*cast_p=*/false,
21747 /*allow_expansion_p=*/false,
21748 /*non_constant_p=*/NULL
);
21750 arguments
= error_mark_node
;
21753 arguments
= build_tree_list_vec (vec
);
21754 release_tree_vector (vec
);
21756 /* Save the arguments away. */
21757 TREE_VALUE (attribute
) = arguments
;
21759 else if (is_cilkplus_vector_p (identifier
))
21761 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
21765 if (arguments
!= error_mark_node
)
21767 /* Add this attribute to the list. */
21768 TREE_CHAIN (attribute
) = attribute_list
;
21769 attribute_list
= attribute
;
21772 token
= cp_lexer_peek_token (parser
->lexer
);
21774 /* Now, look for more attributes. If the next token isn't a
21775 `,', we're done. */
21776 if (token
->type
!= CPP_COMMA
)
21779 /* Consume the comma and keep going. */
21780 cp_lexer_consume_token (parser
->lexer
);
21782 parser
->translate_strings_p
= save_translate_strings_p
;
21784 /* We built up the list in reverse order. */
21785 return nreverse (attribute_list
);
21788 /* Parse a standard C++11 attribute.
21790 The returned representation is a TREE_LIST which TREE_PURPOSE is
21791 the scoped name of the attribute, and the TREE_VALUE is its
21794 Note that the scoped name of the attribute is itself a TREE_LIST
21795 which TREE_PURPOSE is the namespace of the attribute, and
21796 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21797 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21798 and which TREE_PURPOSE is directly the attribute name.
21800 Clients of the attribute code should use get_attribute_namespace
21801 and get_attribute_name to get the actual namespace and name of
21802 attributes, regardless of their being GNU or C++11 attributes.
21805 attribute-token attribute-argument-clause [opt]
21809 attribute-scoped-token
21811 attribute-scoped-token:
21812 attribute-namespace :: identifier
21814 attribute-namespace:
21817 attribute-argument-clause:
21818 ( balanced-token-seq )
21820 balanced-token-seq:
21821 balanced-token [opt]
21822 balanced-token-seq balanced-token
21825 ( balanced-token-seq )
21826 [ balanced-token-seq ]
21827 { balanced-token-seq }. */
21830 cp_parser_std_attribute (cp_parser
*parser
)
21832 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
21835 /* First, parse name of the the attribute, a.k.a
21836 attribute-token. */
21838 token
= cp_lexer_peek_token (parser
->lexer
);
21839 if (token
->type
== CPP_NAME
)
21840 attr_id
= token
->u
.value
;
21841 else if (token
->type
== CPP_KEYWORD
)
21842 attr_id
= ridpointers
[(int) token
->keyword
];
21843 else if (token
->flags
& NAMED_OP
)
21844 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
21846 if (attr_id
== NULL_TREE
)
21849 cp_lexer_consume_token (parser
->lexer
);
21851 token
= cp_lexer_peek_token (parser
->lexer
);
21852 if (token
->type
== CPP_SCOPE
)
21854 /* We are seeing a scoped attribute token. */
21856 cp_lexer_consume_token (parser
->lexer
);
21859 token
= cp_lexer_consume_token (parser
->lexer
);
21860 if (token
->type
== CPP_NAME
)
21861 attr_id
= token
->u
.value
;
21862 else if (token
->type
== CPP_KEYWORD
)
21863 attr_id
= ridpointers
[(int) token
->keyword
];
21866 error_at (token
->location
,
21867 "expected an identifier for the attribute name");
21868 return error_mark_node
;
21870 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
21872 token
= cp_lexer_peek_token (parser
->lexer
);
21876 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
21878 /* C++11 noreturn attribute is equivalent to GNU's. */
21879 if (is_attribute_p ("noreturn", attr_id
))
21880 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21881 /* C++14 deprecated attribute is equivalent to GNU's. */
21882 else if (cxx_dialect
>= cxx1y
&& is_attribute_p ("deprecated", attr_id
))
21883 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21886 /* Now parse the optional argument clause of the attribute. */
21888 if (token
->type
!= CPP_OPEN_PAREN
)
21892 vec
<tree
, va_gc
> *vec
;
21893 int attr_flag
= normal_attr
;
21895 if (attr_ns
== get_identifier ("gnu")
21896 && attribute_takes_identifier_p (attr_id
))
21897 /* A GNU attribute that takes an identifier in parameter. */
21898 attr_flag
= id_attr
;
21900 vec
= cp_parser_parenthesized_expression_list
21901 (parser
, attr_flag
, /*cast_p=*/false,
21902 /*allow_expansion_p=*/true,
21903 /*non_constant_p=*/NULL
);
21905 arguments
= error_mark_node
;
21908 arguments
= build_tree_list_vec (vec
);
21909 release_tree_vector (vec
);
21912 if (arguments
== error_mark_node
)
21913 attribute
= error_mark_node
;
21915 TREE_VALUE (attribute
) = arguments
;
21921 /* Parse a list of standard C++-11 attributes.
21925 attribute-list , attribute[opt]
21927 attribute-list , attribute ...
21931 cp_parser_std_attribute_list (cp_parser
*parser
)
21933 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
21934 cp_token
*token
= NULL
;
21938 attribute
= cp_parser_std_attribute (parser
);
21939 if (attribute
== error_mark_node
)
21941 if (attribute
!= NULL_TREE
)
21943 TREE_CHAIN (attribute
) = attributes
;
21944 attributes
= attribute
;
21946 token
= cp_lexer_peek_token (parser
->lexer
);
21947 if (token
->type
!= CPP_COMMA
)
21949 cp_lexer_consume_token (parser
->lexer
);
21951 attributes
= nreverse (attributes
);
21955 /* Parse a standard C++-11 attribute specifier.
21957 attribute-specifier:
21958 [ [ attribute-list ] ]
21959 alignment-specifier
21961 alignment-specifier:
21962 alignas ( type-id ... [opt] )
21963 alignas ( alignment-expression ... [opt] ). */
21966 cp_parser_std_attribute_spec (cp_parser
*parser
)
21968 tree attributes
= NULL_TREE
;
21969 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21971 if (token
->type
== CPP_OPEN_SQUARE
21972 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
21974 cp_lexer_consume_token (parser
->lexer
);
21975 cp_lexer_consume_token (parser
->lexer
);
21977 attributes
= cp_parser_std_attribute_list (parser
);
21979 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
21980 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
21981 cp_parser_skip_to_end_of_statement (parser
);
21983 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21984 when we are sure that we have actually parsed them. */
21985 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21991 /* Look for an alignment-specifier. */
21993 token
= cp_lexer_peek_token (parser
->lexer
);
21995 if (token
->type
!= CPP_KEYWORD
21996 || token
->keyword
!= RID_ALIGNAS
)
21999 cp_lexer_consume_token (parser
->lexer
);
22000 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22002 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
22004 cp_parser_error (parser
, "expected %<(%>");
22005 return error_mark_node
;
22008 cp_parser_parse_tentatively (parser
);
22009 alignas_expr
= cp_parser_type_id (parser
);
22011 if (!cp_parser_parse_definitely (parser
))
22013 gcc_assert (alignas_expr
== error_mark_node
22014 || alignas_expr
== NULL_TREE
);
22017 cp_parser_assignment_expression (parser
, /*cast_p=*/false,
22018 /**cp_id_kind=*/NULL
);
22019 if (alignas_expr
== error_mark_node
)
22020 cp_parser_skip_to_end_of_statement (parser
);
22021 if (alignas_expr
== NULL_TREE
22022 || alignas_expr
== error_mark_node
)
22023 return alignas_expr
;
22026 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
22028 cp_parser_error (parser
, "expected %<)%>");
22029 return error_mark_node
;
22032 alignas_expr
= cxx_alignas_expr (alignas_expr
);
22034 /* Build the C++-11 representation of an 'aligned'
22037 build_tree_list (build_tree_list (get_identifier ("gnu"),
22038 get_identifier ("aligned")),
22039 build_tree_list (NULL_TREE
, alignas_expr
));
22045 /* Parse a standard C++-11 attribute-specifier-seq.
22047 attribute-specifier-seq:
22048 attribute-specifier-seq [opt] attribute-specifier
22052 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
22054 tree attr_specs
= NULL
;
22058 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
22059 if (attr_spec
== NULL_TREE
)
22061 if (attr_spec
== error_mark_node
)
22062 return error_mark_node
;
22064 TREE_CHAIN (attr_spec
) = attr_specs
;
22065 attr_specs
= attr_spec
;
22068 attr_specs
= nreverse (attr_specs
);
22072 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22073 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22074 current value of the PEDANTIC flag, regardless of whether or not
22075 the `__extension__' keyword is present. The caller is responsible
22076 for restoring the value of the PEDANTIC flag. */
22079 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
22081 /* Save the old value of the PEDANTIC flag. */
22082 *saved_pedantic
= pedantic
;
22084 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
22086 /* Consume the `__extension__' token. */
22087 cp_lexer_consume_token (parser
->lexer
);
22088 /* We're not being pedantic while the `__extension__' keyword is
22098 /* Parse a label declaration.
22101 __label__ label-declarator-seq ;
22103 label-declarator-seq:
22104 identifier , label-declarator-seq
22108 cp_parser_label_declaration (cp_parser
* parser
)
22110 /* Look for the `__label__' keyword. */
22111 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
22117 /* Look for an identifier. */
22118 identifier
= cp_parser_identifier (parser
);
22119 /* If we failed, stop. */
22120 if (identifier
== error_mark_node
)
22122 /* Declare it as a label. */
22123 finish_label_decl (identifier
);
22124 /* If the next token is a `;', stop. */
22125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22127 /* Look for the `,' separating the label declarations. */
22128 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
22131 /* Look for the final `;'. */
22132 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22135 /* Support Functions */
22137 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22138 NAME should have one of the representations used for an
22139 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22140 is returned. If PARSER->SCOPE is a dependent type, then a
22141 SCOPE_REF is returned.
22143 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22144 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22145 was formed. Abstractly, such entities should not be passed to this
22146 function, because they do not need to be looked up, but it is
22147 simpler to check for this special case here, rather than at the
22150 In cases not explicitly covered above, this function returns a
22151 DECL, OVERLOAD, or baselink representing the result of the lookup.
22152 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22155 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22156 (e.g., "struct") that was used. In that case bindings that do not
22157 refer to types are ignored.
22159 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22162 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22165 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22168 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22169 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22170 NULL_TREE otherwise. */
22173 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
22174 enum tag_types tag_type
,
22177 bool check_dependency
,
22178 tree
*ambiguous_decls
,
22179 location_t name_location
)
22182 tree object_type
= parser
->context
->object_type
;
22184 /* Assume that the lookup will be unambiguous. */
22185 if (ambiguous_decls
)
22186 *ambiguous_decls
= NULL_TREE
;
22188 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22189 no longer valid. Note that if we are parsing tentatively, and
22190 the parse fails, OBJECT_TYPE will be automatically restored. */
22191 parser
->context
->object_type
= NULL_TREE
;
22193 if (name
== error_mark_node
)
22194 return error_mark_node
;
22196 /* A template-id has already been resolved; there is no lookup to
22198 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
22200 if (BASELINK_P (name
))
22202 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
22203 == TEMPLATE_ID_EXPR
);
22207 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22208 it should already have been checked to make sure that the name
22209 used matches the type being destroyed. */
22210 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
22214 /* Figure out to which type this destructor applies. */
22216 type
= parser
->scope
;
22217 else if (object_type
)
22218 type
= object_type
;
22220 type
= current_class_type
;
22221 /* If that's not a class type, there is no destructor. */
22222 if (!type
|| !CLASS_TYPE_P (type
))
22223 return error_mark_node
;
22224 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
22225 lazily_declare_fn (sfk_destructor
, type
);
22226 if (!CLASSTYPE_DESTRUCTORS (type
))
22227 return error_mark_node
;
22228 /* If it was a class type, return the destructor. */
22229 return CLASSTYPE_DESTRUCTORS (type
);
22232 /* By this point, the NAME should be an ordinary identifier. If
22233 the id-expression was a qualified name, the qualifying scope is
22234 stored in PARSER->SCOPE at this point. */
22235 gcc_assert (identifier_p (name
));
22237 /* Perform the lookup. */
22242 if (parser
->scope
== error_mark_node
)
22243 return error_mark_node
;
22245 /* If the SCOPE is dependent, the lookup must be deferred until
22246 the template is instantiated -- unless we are explicitly
22247 looking up names in uninstantiated templates. Even then, we
22248 cannot look up the name if the scope is not a class type; it
22249 might, for example, be a template type parameter. */
22250 dependent_p
= (TYPE_P (parser
->scope
)
22251 && dependent_scope_p (parser
->scope
));
22252 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
22254 /* Defer lookup. */
22255 decl
= error_mark_node
;
22258 tree pushed_scope
= NULL_TREE
;
22260 /* If PARSER->SCOPE is a dependent type, then it must be a
22261 class type, and we must not be checking dependencies;
22262 otherwise, we would have processed this lookup above. So
22263 that PARSER->SCOPE is not considered a dependent base by
22264 lookup_member, we must enter the scope here. */
22266 pushed_scope
= push_scope (parser
->scope
);
22268 /* If the PARSER->SCOPE is a template specialization, it
22269 may be instantiated during name lookup. In that case,
22270 errors may be issued. Even if we rollback the current
22271 tentative parse, those errors are valid. */
22272 decl
= lookup_qualified_name (parser
->scope
, name
,
22273 tag_type
!= none_type
,
22274 /*complain=*/true);
22276 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22277 lookup result and the nested-name-specifier nominates a class C:
22278 * if the name specified after the nested-name-specifier, when
22279 looked up in C, is the injected-class-name of C (Clause 9), or
22280 * if the name specified after the nested-name-specifier is the
22281 same as the identifier or the simple-template-id's template-
22282 name in the last component of the nested-name-specifier,
22283 the name is instead considered to name the constructor of
22284 class C. [ Note: for example, the constructor is not an
22285 acceptable lookup result in an elaborated-type-specifier so
22286 the constructor would not be used in place of the
22287 injected-class-name. --end note ] Such a constructor name
22288 shall be used only in the declarator-id of a declaration that
22289 names a constructor or in a using-declaration. */
22290 if (tag_type
== none_type
22291 && DECL_SELF_REFERENCE_P (decl
)
22292 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
22293 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
22294 tag_type
!= none_type
,
22295 /*complain=*/true);
22297 /* If we have a single function from a using decl, pull it out. */
22298 if (TREE_CODE (decl
) == OVERLOAD
22299 && !really_overloaded_fn (decl
))
22300 decl
= OVL_FUNCTION (decl
);
22303 pop_scope (pushed_scope
);
22306 /* If the scope is a dependent type and either we deferred lookup or
22307 we did lookup but didn't find the name, rememeber the name. */
22308 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
22309 && dependent_type_p (parser
->scope
))
22315 /* The resolution to Core Issue 180 says that `struct
22316 A::B' should be considered a type-name, even if `A'
22318 type
= make_typename_type (parser
->scope
, name
, tag_type
,
22319 /*complain=*/tf_error
);
22320 if (type
!= error_mark_node
)
22321 decl
= TYPE_NAME (type
);
22323 else if (is_template
22324 && (cp_parser_next_token_ends_template_argument_p (parser
)
22325 || cp_lexer_next_token_is (parser
->lexer
,
22327 decl
= make_unbound_class_template (parser
->scope
,
22329 /*complain=*/tf_error
);
22331 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22332 parser
->scope
, name
,
22335 parser
->qualifying_scope
= parser
->scope
;
22336 parser
->object_scope
= NULL_TREE
;
22338 else if (object_type
)
22340 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22341 OBJECT_TYPE is not a class. */
22342 if (CLASS_TYPE_P (object_type
))
22343 /* If the OBJECT_TYPE is a template specialization, it may
22344 be instantiated during name lookup. In that case, errors
22345 may be issued. Even if we rollback the current tentative
22346 parse, those errors are valid. */
22347 decl
= lookup_member (object_type
,
22350 tag_type
!= none_type
,
22351 tf_warning_or_error
);
22356 /* Look it up in the enclosing context. */
22357 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22359 /*block_p=*/true, is_namespace
, 0);
22360 parser
->object_scope
= object_type
;
22361 parser
->qualifying_scope
= NULL_TREE
;
22365 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22367 /*block_p=*/true, is_namespace
, 0);
22368 parser
->qualifying_scope
= NULL_TREE
;
22369 parser
->object_scope
= NULL_TREE
;
22372 /* If the lookup failed, let our caller know. */
22373 if (!decl
|| decl
== error_mark_node
)
22374 return error_mark_node
;
22376 /* Pull out the template from an injected-class-name (or multiple). */
22378 decl
= maybe_get_template_decl_from_type_decl (decl
);
22380 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22381 if (TREE_CODE (decl
) == TREE_LIST
)
22383 if (ambiguous_decls
)
22384 *ambiguous_decls
= decl
;
22385 /* The error message we have to print is too complicated for
22386 cp_parser_error, so we incorporate its actions directly. */
22387 if (!cp_parser_simulate_error (parser
))
22389 error_at (name_location
, "reference to %qD is ambiguous",
22391 print_candidates (decl
);
22393 return error_mark_node
;
22396 gcc_assert (DECL_P (decl
)
22397 || TREE_CODE (decl
) == OVERLOAD
22398 || TREE_CODE (decl
) == SCOPE_REF
22399 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
22400 || BASELINK_P (decl
));
22402 /* If we have resolved the name of a member declaration, check to
22403 see if the declaration is accessible. When the name resolves to
22404 set of overloaded functions, accessibility is checked when
22405 overload resolution is done.
22407 During an explicit instantiation, access is not checked at all,
22408 as per [temp.explicit]. */
22410 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
22412 maybe_record_typedef_use (decl
);
22417 /* Like cp_parser_lookup_name, but for use in the typical case where
22418 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22419 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22422 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
22424 return cp_parser_lookup_name (parser
, name
,
22426 /*is_template=*/false,
22427 /*is_namespace=*/false,
22428 /*check_dependency=*/true,
22429 /*ambiguous_decls=*/NULL
,
22433 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22434 the current context, return the TYPE_DECL. If TAG_NAME_P is
22435 true, the DECL indicates the class being defined in a class-head,
22436 or declared in an elaborated-type-specifier.
22438 Otherwise, return DECL. */
22441 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22443 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22444 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22447 template <typename T> struct B;
22450 template <typename T> struct A::B {};
22452 Similarly, in an elaborated-type-specifier:
22454 namespace N { struct X{}; }
22457 template <typename T> friend struct N::X;
22460 However, if the DECL refers to a class type, and we are in
22461 the scope of the class, then the name lookup automatically
22462 finds the TYPE_DECL created by build_self_reference rather
22463 than a TEMPLATE_DECL. For example, in:
22465 template <class T> struct S {
22469 there is no need to handle such case. */
22471 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
22472 return DECL_TEMPLATE_RESULT (decl
);
22477 /* If too many, or too few, template-parameter lists apply to the
22478 declarator, issue an error message. Returns TRUE if all went well,
22479 and FALSE otherwise. */
22482 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
22483 cp_declarator
*declarator
,
22484 location_t declarator_location
)
22486 switch (declarator
->kind
)
22490 unsigned num_templates
= 0;
22491 tree scope
= declarator
->u
.id
.qualifying_scope
;
22494 num_templates
= num_template_headers_for_class (scope
);
22495 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
22496 == TEMPLATE_ID_EXPR
)
22497 /* If the DECLARATOR has the form `X<y>' then it uses one
22498 additional level of template parameters. */
22501 return cp_parser_check_template_parameters
22502 (parser
, num_templates
, declarator_location
, declarator
);
22508 case cdk_reference
:
22510 return (cp_parser_check_declarator_template_parameters
22511 (parser
, declarator
->declarator
, declarator_location
));
22517 gcc_unreachable ();
22522 /* NUM_TEMPLATES were used in the current declaration. If that is
22523 invalid, return FALSE and issue an error messages. Otherwise,
22524 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22525 declarator and we can print more accurate diagnostics. */
22528 cp_parser_check_template_parameters (cp_parser
* parser
,
22529 unsigned num_templates
,
22530 location_t location
,
22531 cp_declarator
*declarator
)
22533 /* If there are the same number of template classes and parameter
22534 lists, that's OK. */
22535 if (parser
->num_template_parameter_lists
== num_templates
)
22537 /* If there are more, but only one more, then we are referring to a
22538 member template. That's OK too. */
22539 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
22541 /* If there are more template classes than parameter lists, we have
22544 template <class T> void S<T>::R<T>::f (); */
22545 if (parser
->num_template_parameter_lists
< num_templates
)
22547 if (declarator
&& !current_function_decl
)
22548 error_at (location
, "specializing member %<%T::%E%> "
22549 "requires %<template<>%> syntax",
22550 declarator
->u
.id
.qualifying_scope
,
22551 declarator
->u
.id
.unqualified_name
);
22552 else if (declarator
)
22553 error_at (location
, "invalid declaration of %<%T::%E%>",
22554 declarator
->u
.id
.qualifying_scope
,
22555 declarator
->u
.id
.unqualified_name
);
22557 error_at (location
, "too few template-parameter-lists");
22560 /* Otherwise, there are too many template parameter lists. We have
22563 template <class T> template <class U> void S::f(); */
22564 error_at (location
, "too many template-parameter-lists");
22568 /* Parse an optional `::' token indicating that the following name is
22569 from the global namespace. If so, PARSER->SCOPE is set to the
22570 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22571 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22572 Returns the new value of PARSER->SCOPE, if the `::' token is
22573 present, and NULL_TREE otherwise. */
22576 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
22580 /* Peek at the next token. */
22581 token
= cp_lexer_peek_token (parser
->lexer
);
22582 /* If we're looking at a `::' token then we're starting from the
22583 global namespace, not our current location. */
22584 if (token
->type
== CPP_SCOPE
)
22586 /* Consume the `::' token. */
22587 cp_lexer_consume_token (parser
->lexer
);
22588 /* Set the SCOPE so that we know where to start the lookup. */
22589 parser
->scope
= global_namespace
;
22590 parser
->qualifying_scope
= global_namespace
;
22591 parser
->object_scope
= NULL_TREE
;
22593 return parser
->scope
;
22595 else if (!current_scope_valid_p
)
22597 parser
->scope
= NULL_TREE
;
22598 parser
->qualifying_scope
= NULL_TREE
;
22599 parser
->object_scope
= NULL_TREE
;
22605 /* Returns TRUE if the upcoming token sequence is the start of a
22606 constructor declarator. If FRIEND_P is true, the declarator is
22607 preceded by the `friend' specifier. */
22610 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
22612 bool constructor_p
;
22613 bool outside_class_specifier_p
;
22614 tree nested_name_specifier
;
22615 cp_token
*next_token
;
22617 /* The common case is that this is not a constructor declarator, so
22618 try to avoid doing lots of work if at all possible. It's not
22619 valid declare a constructor at function scope. */
22620 if (parser
->in_function_body
)
22622 /* And only certain tokens can begin a constructor declarator. */
22623 next_token
= cp_lexer_peek_token (parser
->lexer
);
22624 if (next_token
->type
!= CPP_NAME
22625 && next_token
->type
!= CPP_SCOPE
22626 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
22627 && next_token
->type
!= CPP_TEMPLATE_ID
)
22630 /* Parse tentatively; we are going to roll back all of the tokens
22632 cp_parser_parse_tentatively (parser
);
22633 /* Assume that we are looking at a constructor declarator. */
22634 constructor_p
= true;
22636 /* Look for the optional `::' operator. */
22637 cp_parser_global_scope_opt (parser
,
22638 /*current_scope_valid_p=*/false);
22639 /* Look for the nested-name-specifier. */
22640 nested_name_specifier
22641 = (cp_parser_nested_name_specifier_opt (parser
,
22642 /*typename_keyword_p=*/false,
22643 /*check_dependency_p=*/false,
22645 /*is_declaration=*/false));
22647 outside_class_specifier_p
= (!at_class_scope_p ()
22648 || !TYPE_BEING_DEFINED (current_class_type
)
22651 /* Outside of a class-specifier, there must be a
22652 nested-name-specifier. */
22653 if (!nested_name_specifier
&& outside_class_specifier_p
)
22654 constructor_p
= false;
22655 else if (nested_name_specifier
== error_mark_node
)
22656 constructor_p
= false;
22658 /* If we have a class scope, this is easy; DR 147 says that S::S always
22659 names the constructor, and no other qualified name could. */
22660 if (constructor_p
&& nested_name_specifier
22661 && CLASS_TYPE_P (nested_name_specifier
))
22663 tree id
= cp_parser_unqualified_id (parser
,
22664 /*template_keyword_p=*/false,
22665 /*check_dependency_p=*/false,
22666 /*declarator_p=*/true,
22667 /*optional_p=*/false);
22668 if (is_overloaded_fn (id
))
22669 id
= DECL_NAME (get_first_fn (id
));
22670 if (!constructor_name_p (id
, nested_name_specifier
))
22671 constructor_p
= false;
22673 /* If we still think that this might be a constructor-declarator,
22674 look for a class-name. */
22675 else if (constructor_p
)
22679 template <typename T> struct S {
22683 we must recognize that the nested `S' names a class. */
22685 type_decl
= cp_parser_class_name (parser
,
22686 /*typename_keyword_p=*/false,
22687 /*template_keyword_p=*/false,
22689 /*check_dependency_p=*/false,
22690 /*class_head_p=*/false,
22691 /*is_declaration=*/false);
22692 /* If there was no class-name, then this is not a constructor.
22693 Otherwise, if we are in a class-specifier and we aren't
22694 handling a friend declaration, check that its type matches
22695 current_class_type (c++/38313). Note: error_mark_node
22696 is left alone for error recovery purposes. */
22697 constructor_p
= (!cp_parser_error_occurred (parser
)
22698 && (outside_class_specifier_p
22699 || type_decl
== error_mark_node
22700 || same_type_p (current_class_type
,
22701 TREE_TYPE (type_decl
))));
22703 /* If we're still considering a constructor, we have to see a `(',
22704 to begin the parameter-declaration-clause, followed by either a
22705 `)', an `...', or a decl-specifier. We need to check for a
22706 type-specifier to avoid being fooled into thinking that:
22710 is a constructor. (It is actually a function named `f' that
22711 takes one parameter (of type `int') and returns a value of type
22714 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22715 constructor_p
= false;
22718 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
22719 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
22720 /* A parameter declaration begins with a decl-specifier,
22721 which is either the "attribute" keyword, a storage class
22722 specifier, or (usually) a type-specifier. */
22723 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
22726 tree pushed_scope
= NULL_TREE
;
22727 unsigned saved_num_template_parameter_lists
;
22729 /* Names appearing in the type-specifier should be looked up
22730 in the scope of the class. */
22731 if (current_class_type
)
22735 type
= TREE_TYPE (type_decl
);
22736 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22738 type
= resolve_typename_type (type
,
22739 /*only_current_p=*/false);
22740 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22742 cp_parser_abort_tentative_parse (parser
);
22746 pushed_scope
= push_scope (type
);
22749 /* Inside the constructor parameter list, surrounding
22750 template-parameter-lists do not apply. */
22751 saved_num_template_parameter_lists
22752 = parser
->num_template_parameter_lists
;
22753 parser
->num_template_parameter_lists
= 0;
22755 /* Look for the type-specifier. */
22756 cp_parser_type_specifier (parser
,
22757 CP_PARSER_FLAGS_NONE
,
22758 /*decl_specs=*/NULL
,
22759 /*is_declarator=*/true,
22760 /*declares_class_or_enum=*/NULL
,
22761 /*is_cv_qualifier=*/NULL
);
22763 parser
->num_template_parameter_lists
22764 = saved_num_template_parameter_lists
;
22766 /* Leave the scope of the class. */
22768 pop_scope (pushed_scope
);
22770 constructor_p
= !cp_parser_error_occurred (parser
);
22774 /* We did not really want to consume any tokens. */
22775 cp_parser_abort_tentative_parse (parser
);
22777 return constructor_p
;
22780 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22781 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22782 they must be performed once we are in the scope of the function.
22784 Returns the function defined. */
22787 cp_parser_function_definition_from_specifiers_and_declarator
22788 (cp_parser
* parser
,
22789 cp_decl_specifier_seq
*decl_specifiers
,
22791 const cp_declarator
*declarator
)
22796 /* Begin the function-definition. */
22797 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
22799 /* The things we're about to see are not directly qualified by any
22800 template headers we've seen thus far. */
22801 reset_specialization ();
22803 /* If there were names looked up in the decl-specifier-seq that we
22804 did not check, check them now. We must wait until we are in the
22805 scope of the function to perform the checks, since the function
22806 might be a friend. */
22807 perform_deferred_access_checks (tf_warning_or_error
);
22811 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
22812 parser
->omp_declare_simd
= NULL
;
22817 /* Skip the entire function. */
22818 cp_parser_skip_to_end_of_block_or_statement (parser
);
22819 fn
= error_mark_node
;
22821 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
22823 /* Seen already, skip it. An error message has already been output. */
22824 cp_parser_skip_to_end_of_block_or_statement (parser
);
22825 fn
= current_function_decl
;
22826 current_function_decl
= NULL_TREE
;
22827 /* If this is a function from a class, pop the nested class. */
22828 if (current_class_name
)
22829 pop_nested_class ();
22834 if (DECL_DECLARED_INLINE_P (current_function_decl
))
22835 tv
= TV_PARSE_INLINE
;
22837 tv
= TV_PARSE_FUNC
;
22839 fn
= cp_parser_function_definition_after_declarator (parser
,
22840 /*inline_p=*/false);
22847 /* Parse the part of a function-definition that follows the
22848 declarator. INLINE_P is TRUE iff this function is an inline
22849 function defined within a class-specifier.
22851 Returns the function defined. */
22854 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
22858 bool ctor_initializer_p
= false;
22859 bool saved_in_unbraced_linkage_specification_p
;
22860 bool saved_in_function_body
;
22861 unsigned saved_num_template_parameter_lists
;
22863 bool fully_implicit_function_template_p
22864 = parser
->fully_implicit_function_template_p
;
22865 parser
->fully_implicit_function_template_p
= false;
22866 tree implicit_template_parms
22867 = parser
->implicit_template_parms
;
22868 parser
->implicit_template_parms
= 0;
22869 cp_binding_level
* implicit_template_scope
22870 = parser
->implicit_template_scope
;
22871 parser
->implicit_template_scope
= 0;
22873 saved_in_function_body
= parser
->in_function_body
;
22874 parser
->in_function_body
= true;
22875 /* If the next token is `return', then the code may be trying to
22876 make use of the "named return value" extension that G++ used to
22878 token
= cp_lexer_peek_token (parser
->lexer
);
22879 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
22881 /* Consume the `return' keyword. */
22882 cp_lexer_consume_token (parser
->lexer
);
22883 /* Look for the identifier that indicates what value is to be
22885 cp_parser_identifier (parser
);
22886 /* Issue an error message. */
22887 error_at (token
->location
,
22888 "named return values are no longer supported");
22889 /* Skip tokens until we reach the start of the function body. */
22892 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22893 if (token
->type
== CPP_OPEN_BRACE
22894 || token
->type
== CPP_EOF
22895 || token
->type
== CPP_PRAGMA_EOL
)
22897 cp_lexer_consume_token (parser
->lexer
);
22900 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22901 anything declared inside `f'. */
22902 saved_in_unbraced_linkage_specification_p
22903 = parser
->in_unbraced_linkage_specification_p
;
22904 parser
->in_unbraced_linkage_specification_p
= false;
22905 /* Inside the function, surrounding template-parameter-lists do not
22907 saved_num_template_parameter_lists
22908 = parser
->num_template_parameter_lists
;
22909 parser
->num_template_parameter_lists
= 0;
22911 start_lambda_scope (current_function_decl
);
22913 /* If the next token is `try', `__transaction_atomic', or
22914 `__transaction_relaxed`, then we are looking at either function-try-block
22915 or function-transaction-block. Note that all of these include the
22917 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
22918 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22919 RID_TRANSACTION_ATOMIC
);
22920 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
22921 RID_TRANSACTION_RELAXED
))
22922 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22923 RID_TRANSACTION_RELAXED
);
22924 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
22925 ctor_initializer_p
= cp_parser_function_try_block (parser
);
22927 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
22928 (parser
, /*in_function_try_block=*/false);
22930 finish_lambda_scope ();
22932 /* Finish the function. */
22933 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
22934 (inline_p
? 2 : 0));
22935 /* Generate code for it, if necessary. */
22936 expand_or_defer_fn (fn
);
22937 /* Restore the saved values. */
22938 parser
->in_unbraced_linkage_specification_p
22939 = saved_in_unbraced_linkage_specification_p
;
22940 parser
->num_template_parameter_lists
22941 = saved_num_template_parameter_lists
;
22942 parser
->in_function_body
= saved_in_function_body
;
22944 parser
->fully_implicit_function_template_p
22945 = fully_implicit_function_template_p
;
22946 parser
->implicit_template_parms
22947 = implicit_template_parms
;
22948 parser
->implicit_template_scope
22949 = implicit_template_scope
;
22951 if (parser
->fully_implicit_function_template_p
)
22952 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
22957 /* Parse a template-declaration, assuming that the `export' (and
22958 `extern') keywords, if present, has already been scanned. MEMBER_P
22959 is as for cp_parser_template_declaration. */
22962 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
22964 tree decl
= NULL_TREE
;
22965 vec
<deferred_access_check
, va_gc
> *checks
;
22966 tree parameter_list
;
22967 bool friend_p
= false;
22968 bool need_lang_pop
;
22971 /* Look for the `template' keyword. */
22972 token
= cp_lexer_peek_token (parser
->lexer
);
22973 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
22977 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
22979 if (at_class_scope_p () && current_function_decl
)
22981 /* 14.5.2.2 [temp.mem]
22983 A local class shall not have member templates. */
22984 error_at (token
->location
,
22985 "invalid declaration of member template in local class");
22986 cp_parser_skip_to_end_of_block_or_statement (parser
);
22991 A template ... shall not have C linkage. */
22992 if (current_lang_name
== lang_name_c
)
22994 error_at (token
->location
, "template with C linkage");
22995 /* Give it C++ linkage to avoid confusing other parts of the
22997 push_lang_context (lang_name_cplusplus
);
22998 need_lang_pop
= true;
23001 need_lang_pop
= false;
23003 /* We cannot perform access checks on the template parameter
23004 declarations until we know what is being declared, just as we
23005 cannot check the decl-specifier list. */
23006 push_deferring_access_checks (dk_deferred
);
23008 /* If the next token is `>', then we have an invalid
23009 specialization. Rather than complain about an invalid template
23010 parameter, issue an error message here. */
23011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
23013 cp_parser_error (parser
, "invalid explicit specialization");
23014 begin_specialization ();
23015 parameter_list
= NULL_TREE
;
23019 /* Parse the template parameters. */
23020 parameter_list
= cp_parser_template_parameter_list (parser
);
23023 /* Get the deferred access checks from the parameter list. These
23024 will be checked once we know what is being declared, as for a
23025 member template the checks must be performed in the scope of the
23026 class containing the member. */
23027 checks
= get_deferred_access_checks ();
23029 /* Look for the `>'. */
23030 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23031 /* We just processed one more parameter list. */
23032 ++parser
->num_template_parameter_lists
;
23033 /* If the next token is `template', there are more template
23035 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23037 cp_parser_template_declaration_after_export (parser
, member_p
);
23038 else if (cxx_dialect
>= cxx11
23039 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23040 decl
= cp_parser_alias_declaration (parser
);
23043 /* There are no access checks when parsing a template, as we do not
23044 know if a specialization will be a friend. */
23045 push_deferring_access_checks (dk_no_check
);
23046 token
= cp_lexer_peek_token (parser
->lexer
);
23047 decl
= cp_parser_single_declaration (parser
,
23050 /*explicit_specialization_p=*/false,
23052 pop_deferring_access_checks ();
23054 /* If this is a member template declaration, let the front
23056 if (member_p
&& !friend_p
&& decl
)
23058 if (TREE_CODE (decl
) == TYPE_DECL
)
23059 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
23061 decl
= finish_member_template_decl (decl
);
23063 else if (friend_p
&& decl
23064 && DECL_DECLARES_TYPE_P (decl
))
23065 make_friend_class (current_class_type
, TREE_TYPE (decl
),
23066 /*complain=*/true);
23068 /* We are done with the current parameter list. */
23069 --parser
->num_template_parameter_lists
;
23071 pop_deferring_access_checks ();
23074 finish_template_decl (parameter_list
);
23076 /* Check the template arguments for a literal operator template. */
23078 && DECL_DECLARES_FUNCTION_P (decl
)
23079 && UDLIT_OPER_P (DECL_NAME (decl
)))
23082 if (parameter_list
== NULL_TREE
)
23086 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
23087 if (num_parms
== 1)
23089 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
23090 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23091 if (TREE_TYPE (parm
) != char_type_node
23092 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23095 else if (num_parms
== 2 && cxx_dialect
>= cxx1y
)
23097 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
23098 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
23099 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
23100 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23101 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
23102 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23109 error ("literal operator template %qD has invalid parameter list."
23110 " Expected non-type template argument pack <char...>"
23111 " or <typename CharT, CharT...>",
23114 /* Register member declarations. */
23115 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
23116 finish_member_declaration (decl
);
23117 /* For the erroneous case of a template with C linkage, we pushed an
23118 implicit C++ linkage scope; exit that scope now. */
23120 pop_lang_context ();
23121 /* If DECL is a function template, we must return to parse it later.
23122 (Even though there is no definition, there might be default
23123 arguments that need handling.) */
23124 if (member_p
&& decl
23125 && DECL_DECLARES_FUNCTION_P (decl
))
23126 vec_safe_push (unparsed_funs_with_definitions
, decl
);
23129 /* Perform the deferred access checks from a template-parameter-list.
23130 CHECKS is a TREE_LIST of access checks, as returned by
23131 get_deferred_access_checks. */
23134 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
23136 ++processing_template_parmlist
;
23137 perform_access_checks (checks
, tf_warning_or_error
);
23138 --processing_template_parmlist
;
23141 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23142 `function-definition' sequence that follows a template header.
23143 If MEMBER_P is true, this declaration appears in a class scope.
23145 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23146 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23149 cp_parser_single_declaration (cp_parser
* parser
,
23150 vec
<deferred_access_check
, va_gc
> *checks
,
23152 bool explicit_specialization_p
,
23155 int declares_class_or_enum
;
23156 tree decl
= NULL_TREE
;
23157 cp_decl_specifier_seq decl_specifiers
;
23158 bool function_definition_p
= false;
23159 cp_token
*decl_spec_token_start
;
23161 /* This function is only used when processing a template
23163 gcc_assert (innermost_scope_kind () == sk_template_parms
23164 || innermost_scope_kind () == sk_template_spec
);
23166 /* Defer access checks until we know what is being declared. */
23167 push_deferring_access_checks (dk_deferred
);
23169 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23171 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23172 cp_parser_decl_specifier_seq (parser
,
23173 CP_PARSER_FLAGS_OPTIONAL
,
23175 &declares_class_or_enum
);
23177 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
23179 /* There are no template typedefs. */
23180 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
23182 error_at (decl_spec_token_start
->location
,
23183 "template declaration of %<typedef%>");
23184 decl
= error_mark_node
;
23187 /* Gather up the access checks that occurred the
23188 decl-specifier-seq. */
23189 stop_deferring_access_checks ();
23191 /* Check for the declaration of a template class. */
23192 if (declares_class_or_enum
)
23194 if (cp_parser_declares_only_class_p (parser
))
23196 decl
= shadow_tag (&decl_specifiers
);
23201 friend template <typename T> struct A<T>::B;
23204 A<T>::B will be represented by a TYPENAME_TYPE, and
23205 therefore not recognized by shadow_tag. */
23206 if (friend_p
&& *friend_p
23208 && decl_specifiers
.type
23209 && TYPE_P (decl_specifiers
.type
))
23210 decl
= decl_specifiers
.type
;
23212 if (decl
&& decl
!= error_mark_node
)
23213 decl
= TYPE_NAME (decl
);
23215 decl
= error_mark_node
;
23217 /* Perform access checks for template parameters. */
23218 cp_parser_perform_template_parameter_access_checks (checks
);
23222 /* Complain about missing 'typename' or other invalid type names. */
23223 if (!decl_specifiers
.any_type_specifiers_p
23224 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23226 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23227 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23228 the rest of this declaration. */
23229 decl
= error_mark_node
;
23233 /* If it's not a template class, try for a template function. If
23234 the next token is a `;', then this declaration does not declare
23235 anything. But, if there were errors in the decl-specifiers, then
23236 the error might well have come from an attempted class-specifier.
23237 In that case, there's no need to warn about a missing declarator. */
23239 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
23240 || decl_specifiers
.type
!= error_mark_node
))
23242 decl
= cp_parser_init_declarator (parser
,
23245 /*function_definition_allowed_p=*/true,
23247 declares_class_or_enum
,
23248 &function_definition_p
,
23251 /* 7.1.1-1 [dcl.stc]
23253 A storage-class-specifier shall not be specified in an explicit
23254 specialization... */
23256 && explicit_specialization_p
23257 && decl_specifiers
.storage_class
!= sc_none
)
23259 error_at (decl_spec_token_start
->location
,
23260 "explicit template specialization cannot have a storage class");
23261 decl
= error_mark_node
;
23264 if (decl
&& VAR_P (decl
))
23265 check_template_variable (decl
);
23268 /* Look for a trailing `;' after the declaration. */
23269 if (!function_definition_p
23270 && (decl
== error_mark_node
23271 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
23272 cp_parser_skip_to_end_of_block_or_statement (parser
);
23275 pop_deferring_access_checks ();
23277 /* Clear any current qualification; whatever comes next is the start
23278 of something new. */
23279 parser
->scope
= NULL_TREE
;
23280 parser
->qualifying_scope
= NULL_TREE
;
23281 parser
->object_scope
= NULL_TREE
;
23286 /* Parse a cast-expression that is not the operand of a unary "&". */
23289 cp_parser_simple_cast_expression (cp_parser
*parser
)
23291 return cp_parser_cast_expression (parser
, /*address_p=*/false,
23292 /*cast_p=*/false, /*decltype*/false, NULL
);
23295 /* Parse a functional cast to TYPE. Returns an expression
23296 representing the cast. */
23299 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
23301 vec
<tree
, va_gc
> *vec
;
23302 tree expression_list
;
23307 type
= error_mark_node
;
23309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23311 cp_lexer_set_source_position (parser
->lexer
);
23312 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23313 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
23314 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
23315 if (TREE_CODE (type
) == TYPE_DECL
)
23316 type
= TREE_TYPE (type
);
23317 return finish_compound_literal (type
, expression_list
,
23318 tf_warning_or_error
);
23322 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23324 /*allow_expansion_p=*/true,
23325 /*non_constant_p=*/NULL
);
23327 expression_list
= error_mark_node
;
23330 expression_list
= build_tree_list_vec (vec
);
23331 release_tree_vector (vec
);
23334 cast
= build_functional_cast (type
, expression_list
,
23335 tf_warning_or_error
);
23336 /* [expr.const]/1: In an integral constant expression "only type
23337 conversions to integral or enumeration type can be used". */
23338 if (TREE_CODE (type
) == TYPE_DECL
)
23339 type
= TREE_TYPE (type
);
23340 if (cast
!= error_mark_node
23341 && !cast_valid_in_integral_constant_expression_p (type
)
23342 && cp_parser_non_integral_constant_expression (parser
,
23344 return error_mark_node
;
23348 /* Save the tokens that make up the body of a member function defined
23349 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23350 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23351 specifiers applied to the declaration. Returns the FUNCTION_DECL
23352 for the member function. */
23355 cp_parser_save_member_function_body (cp_parser
* parser
,
23356 cp_decl_specifier_seq
*decl_specifiers
,
23357 cp_declarator
*declarator
,
23364 /* Create the FUNCTION_DECL. */
23365 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
23366 cp_finalize_omp_declare_simd (parser
, fn
);
23367 /* If something went badly wrong, bail out now. */
23368 if (fn
== error_mark_node
)
23370 /* If there's a function-body, skip it. */
23371 if (cp_parser_token_starts_function_definition_p
23372 (cp_lexer_peek_token (parser
->lexer
)))
23373 cp_parser_skip_to_end_of_block_or_statement (parser
);
23374 return error_mark_node
;
23377 /* Remember it, if there default args to post process. */
23378 cp_parser_save_default_args (parser
, fn
);
23380 /* Save away the tokens that make up the body of the
23382 first
= parser
->lexer
->next_token
;
23383 /* Handle function try blocks. */
23384 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23385 cp_lexer_consume_token (parser
->lexer
);
23386 /* We can have braced-init-list mem-initializers before the fn body. */
23387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23389 cp_lexer_consume_token (parser
->lexer
);
23390 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23392 /* cache_group will stop after an un-nested { } pair, too. */
23393 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
23396 /* variadic mem-inits have ... after the ')'. */
23397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23398 cp_lexer_consume_token (parser
->lexer
);
23401 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23402 /* Handle function try blocks. */
23403 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
23404 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23405 last
= parser
->lexer
->next_token
;
23407 /* Save away the inline definition; we will process it when the
23408 class is complete. */
23409 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
23410 DECL_PENDING_INLINE_P (fn
) = 1;
23412 /* We need to know that this was defined in the class, so that
23413 friend templates are handled correctly. */
23414 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
23416 /* Add FN to the queue of functions to be parsed later. */
23417 vec_safe_push (unparsed_funs_with_definitions
, fn
);
23422 /* Save the tokens that make up the in-class initializer for a non-static
23423 data member. Returns a DEFAULT_ARG. */
23426 cp_parser_save_nsdmi (cp_parser
* parser
)
23428 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
23431 /* Parse a template-argument-list, as well as the trailing ">" (but
23432 not the opening "<"). See cp_parser_template_argument_list for the
23436 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
23440 tree saved_qualifying_scope
;
23441 tree saved_object_scope
;
23442 bool saved_greater_than_is_operator_p
;
23443 int saved_unevaluated_operand
;
23444 int saved_inhibit_evaluation_warnings
;
23448 When parsing a template-id, the first non-nested `>' is taken as
23449 the end of the template-argument-list rather than a greater-than
23451 saved_greater_than_is_operator_p
23452 = parser
->greater_than_is_operator_p
;
23453 parser
->greater_than_is_operator_p
= false;
23454 /* Parsing the argument list may modify SCOPE, so we save it
23456 saved_scope
= parser
->scope
;
23457 saved_qualifying_scope
= parser
->qualifying_scope
;
23458 saved_object_scope
= parser
->object_scope
;
23459 /* We need to evaluate the template arguments, even though this
23460 template-id may be nested within a "sizeof". */
23461 saved_unevaluated_operand
= cp_unevaluated_operand
;
23462 cp_unevaluated_operand
= 0;
23463 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23464 c_inhibit_evaluation_warnings
= 0;
23465 /* Parse the template-argument-list itself. */
23466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
23467 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23468 arguments
= NULL_TREE
;
23470 arguments
= cp_parser_template_argument_list (parser
);
23471 /* Look for the `>' that ends the template-argument-list. If we find
23472 a '>>' instead, it's probably just a typo. */
23473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23475 if (cxx_dialect
!= cxx98
)
23477 /* In C++0x, a `>>' in a template argument list or cast
23478 expression is considered to be two separate `>'
23479 tokens. So, change the current token to a `>', but don't
23480 consume it: it will be consumed later when the outer
23481 template argument list (or cast expression) is parsed.
23482 Note that this replacement of `>' for `>>' is necessary
23483 even if we are parsing tentatively: in the tentative
23484 case, after calling
23485 cp_parser_enclosed_template_argument_list we will always
23486 throw away all of the template arguments and the first
23487 closing `>', either because the template argument list
23488 was erroneous or because we are replacing those tokens
23489 with a CPP_TEMPLATE_ID token. The second `>' (which will
23490 not have been thrown away) is needed either to close an
23491 outer template argument list or to complete a new-style
23493 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23494 token
->type
= CPP_GREATER
;
23496 else if (!saved_greater_than_is_operator_p
)
23498 /* If we're in a nested template argument list, the '>>' has
23499 to be a typo for '> >'. We emit the error message, but we
23500 continue parsing and we push a '>' as next token, so that
23501 the argument list will be parsed correctly. Note that the
23502 global source location is still on the token before the
23503 '>>', so we need to say explicitly where we want it. */
23504 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23505 error_at (token
->location
, "%<>>%> should be %<> >%> "
23506 "within a nested template argument list");
23508 token
->type
= CPP_GREATER
;
23512 /* If this is not a nested template argument list, the '>>'
23513 is a typo for '>'. Emit an error message and continue.
23514 Same deal about the token location, but here we can get it
23515 right by consuming the '>>' before issuing the diagnostic. */
23516 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
23517 error_at (token
->location
,
23518 "spurious %<>>%>, use %<>%> to terminate "
23519 "a template argument list");
23523 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23524 /* The `>' token might be a greater-than operator again now. */
23525 parser
->greater_than_is_operator_p
23526 = saved_greater_than_is_operator_p
;
23527 /* Restore the SAVED_SCOPE. */
23528 parser
->scope
= saved_scope
;
23529 parser
->qualifying_scope
= saved_qualifying_scope
;
23530 parser
->object_scope
= saved_object_scope
;
23531 cp_unevaluated_operand
= saved_unevaluated_operand
;
23532 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23537 /* MEMBER_FUNCTION is a member function, or a friend. If default
23538 arguments, or the body of the function have not yet been parsed,
23542 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
23544 timevar_push (TV_PARSE_INMETH
);
23545 /* If this member is a template, get the underlying
23547 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
23548 member_function
= DECL_TEMPLATE_RESULT (member_function
);
23550 /* There should not be any class definitions in progress at this
23551 point; the bodies of members are only parsed outside of all class
23553 gcc_assert (parser
->num_classes_being_defined
== 0);
23554 /* While we're parsing the member functions we might encounter more
23555 classes. We want to handle them right away, but we don't want
23556 them getting mixed up with functions that are currently in the
23558 push_unparsed_function_queues (parser
);
23560 /* Make sure that any template parameters are in scope. */
23561 maybe_begin_member_template_processing (member_function
);
23563 /* If the body of the function has not yet been parsed, parse it
23565 if (DECL_PENDING_INLINE_P (member_function
))
23567 tree function_scope
;
23568 cp_token_cache
*tokens
;
23570 /* The function is no longer pending; we are processing it. */
23571 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
23572 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
23573 DECL_PENDING_INLINE_P (member_function
) = 0;
23575 /* If this is a local class, enter the scope of the containing
23577 function_scope
= current_function_decl
;
23578 if (function_scope
)
23579 push_function_context ();
23581 /* Push the body of the function onto the lexer stack. */
23582 cp_parser_push_lexer_for_tokens (parser
, tokens
);
23584 /* Let the front end know that we going to be defining this
23586 start_preparsed_function (member_function
, NULL_TREE
,
23587 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
23589 /* Don't do access checking if it is a templated function. */
23590 if (processing_template_decl
)
23591 push_deferring_access_checks (dk_no_check
);
23593 /* #pragma omp declare reduction needs special parsing. */
23594 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
23596 parser
->lexer
->in_pragma
= true;
23597 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
23598 finish_function (/*inline*/2);
23599 cp_check_omp_declare_reduction (member_function
);
23602 /* Now, parse the body of the function. */
23603 cp_parser_function_definition_after_declarator (parser
,
23604 /*inline_p=*/true);
23606 if (processing_template_decl
)
23607 pop_deferring_access_checks ();
23609 /* Leave the scope of the containing function. */
23610 if (function_scope
)
23611 pop_function_context ();
23612 cp_parser_pop_lexer (parser
);
23615 /* Remove any template parameters from the symbol table. */
23616 maybe_end_member_template_processing ();
23618 /* Restore the queue. */
23619 pop_unparsed_function_queues (parser
);
23620 timevar_pop (TV_PARSE_INMETH
);
23623 /* If DECL contains any default args, remember it on the unparsed
23624 functions queue. */
23627 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
23631 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
23633 probe
= TREE_CHAIN (probe
))
23634 if (TREE_PURPOSE (probe
))
23636 cp_default_arg_entry entry
= {current_class_type
, decl
};
23637 vec_safe_push (unparsed_funs_with_default_args
, entry
);
23642 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23643 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23644 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23645 from the parameter-type-list. */
23648 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
23649 tree default_arg
, tree parmtype
)
23651 cp_token_cache
*tokens
;
23655 if (default_arg
== error_mark_node
)
23656 return error_mark_node
;
23658 /* Push the saved tokens for the default argument onto the parser's
23660 tokens
= DEFARG_TOKENS (default_arg
);
23661 cp_parser_push_lexer_for_tokens (parser
, tokens
);
23663 start_lambda_scope (decl
);
23665 /* Parse the default argument. */
23666 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
23667 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
23668 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23670 finish_lambda_scope ();
23672 if (parsed_arg
== error_mark_node
)
23673 cp_parser_skip_to_end_of_statement (parser
);
23675 if (!processing_template_decl
)
23677 /* In a non-template class, check conversions now. In a template,
23678 we'll wait and instantiate these as needed. */
23679 if (TREE_CODE (decl
) == PARM_DECL
)
23680 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
23681 tf_warning_or_error
);
23684 int flags
= LOOKUP_IMPLICIT
;
23685 if (DIRECT_LIST_INIT_P (parsed_arg
))
23686 flags
= LOOKUP_NORMAL
;
23687 parsed_arg
= digest_init_flags (TREE_TYPE (decl
), parsed_arg
, flags
);
23688 if (TREE_CODE (parsed_arg
) == TARGET_EXPR
)
23689 /* This represents the whole initialization. */
23690 TARGET_EXPR_DIRECT_INIT_P (parsed_arg
) = true;
23694 /* If the token stream has not been completely used up, then
23695 there was extra junk after the end of the default
23697 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
23699 if (TREE_CODE (decl
) == PARM_DECL
)
23700 cp_parser_error (parser
, "expected %<,%>");
23702 cp_parser_error (parser
, "expected %<;%>");
23705 /* Revert to the main lexer. */
23706 cp_parser_pop_lexer (parser
);
23711 /* FIELD is a non-static data member with an initializer which we saved for
23712 later; parse it now. */
23715 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
23719 maybe_begin_member_template_processing (field
);
23721 push_unparsed_function_queues (parser
);
23722 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
23723 DECL_INITIAL (field
),
23725 pop_unparsed_function_queues (parser
);
23727 maybe_end_member_template_processing ();
23729 DECL_INITIAL (field
) = def
;
23732 /* FN is a FUNCTION_DECL which may contains a parameter with an
23733 unparsed DEFAULT_ARG. Parse the default args now. This function
23734 assumes that the current scope is the scope in which the default
23735 argument should be processed. */
23738 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
23740 bool saved_local_variables_forbidden_p
;
23741 tree parm
, parmdecl
;
23743 /* While we're parsing the default args, we might (due to the
23744 statement expression extension) encounter more classes. We want
23745 to handle them right away, but we don't want them getting mixed
23746 up with default args that are currently in the queue. */
23747 push_unparsed_function_queues (parser
);
23749 /* Local variable names (and the `this' keyword) may not appear
23750 in a default argument. */
23751 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
23752 parser
->local_variables_forbidden_p
= true;
23754 push_defarg_context (fn
);
23756 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
23757 parmdecl
= DECL_ARGUMENTS (fn
);
23758 parm
&& parm
!= void_list_node
;
23759 parm
= TREE_CHAIN (parm
),
23760 parmdecl
= DECL_CHAIN (parmdecl
))
23762 tree default_arg
= TREE_PURPOSE (parm
);
23764 vec
<tree
, va_gc
> *insts
;
23771 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
23772 /* This can happen for a friend declaration for a function
23773 already declared with default arguments. */
23777 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
23779 TREE_VALUE (parm
));
23780 if (parsed_arg
== error_mark_node
)
23785 TREE_PURPOSE (parm
) = parsed_arg
;
23787 /* Update any instantiations we've already created. */
23788 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
23789 vec_safe_iterate (insts
, ix
, ©
); ix
++)
23790 TREE_PURPOSE (copy
) = parsed_arg
;
23793 pop_defarg_context ();
23795 /* Make sure no default arg is missing. */
23796 check_default_args (fn
);
23798 /* Restore the state of local_variables_forbidden_p. */
23799 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
23801 /* Restore the queue. */
23802 pop_unparsed_function_queues (parser
);
23805 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23807 sizeof ... ( identifier )
23809 where the 'sizeof' token has already been consumed. */
23812 cp_parser_sizeof_pack (cp_parser
*parser
)
23814 /* Consume the `...'. */
23815 cp_lexer_consume_token (parser
->lexer
);
23816 maybe_warn_variadic_templates ();
23818 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
23820 cp_lexer_consume_token (parser
->lexer
);
23822 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
23823 "%<sizeof...%> argument must be surrounded by parentheses");
23825 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23826 tree name
= cp_parser_identifier (parser
);
23827 if (name
== error_mark_node
)
23828 return error_mark_node
;
23829 /* The name is not qualified. */
23830 parser
->scope
= NULL_TREE
;
23831 parser
->qualifying_scope
= NULL_TREE
;
23832 parser
->object_scope
= NULL_TREE
;
23833 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
23834 if (expr
== error_mark_node
)
23835 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
23837 if (TREE_CODE (expr
) == TYPE_DECL
)
23838 expr
= TREE_TYPE (expr
);
23839 else if (TREE_CODE (expr
) == CONST_DECL
)
23840 expr
= DECL_INITIAL (expr
);
23841 expr
= make_pack_expansion (expr
);
23844 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23849 /* Parse the operand of `sizeof' (or a similar operator). Returns
23850 either a TYPE or an expression, depending on the form of the
23851 input. The KEYWORD indicates which kind of expression we have
23855 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
23857 tree expr
= NULL_TREE
;
23858 const char *saved_message
;
23860 bool saved_integral_constant_expression_p
;
23861 bool saved_non_integral_constant_expression_p
;
23863 /* If it's a `...', then we are computing the length of a parameter
23865 if (keyword
== RID_SIZEOF
23866 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23867 return cp_parser_sizeof_pack (parser
);
23869 /* Types cannot be defined in a `sizeof' expression. Save away the
23871 saved_message
= parser
->type_definition_forbidden_message
;
23872 /* And create the new one. */
23873 tmp
= concat ("types may not be defined in %<",
23874 IDENTIFIER_POINTER (ridpointers
[keyword
]),
23875 "%> expressions", NULL
);
23876 parser
->type_definition_forbidden_message
= tmp
;
23878 /* The restrictions on constant-expressions do not apply inside
23879 sizeof expressions. */
23880 saved_integral_constant_expression_p
23881 = parser
->integral_constant_expression_p
;
23882 saved_non_integral_constant_expression_p
23883 = parser
->non_integral_constant_expression_p
;
23884 parser
->integral_constant_expression_p
= false;
23886 /* Do not actually evaluate the expression. */
23887 ++cp_unevaluated_operand
;
23888 ++c_inhibit_evaluation_warnings
;
23889 /* If it's a `(', then we might be looking at the type-id
23891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23893 tree type
= NULL_TREE
;
23894 bool compound_literal_p
;
23896 /* We can't be sure yet whether we're looking at a type-id or an
23898 cp_parser_parse_tentatively (parser
);
23899 /* Consume the `('. */
23900 cp_lexer_consume_token (parser
->lexer
);
23901 /* Note: as a GNU Extension, compound literals are considered
23902 postfix-expressions as they are in C99, so they are valid
23903 arguments to sizeof. See comment in cp_parser_cast_expression
23905 cp_lexer_save_tokens (parser
->lexer
);
23906 /* Skip tokens until the next token is a closing parenthesis.
23907 If we find the closing `)', and the next token is a `{', then
23908 we are looking at a compound-literal. */
23910 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
23911 /*consume_paren=*/true)
23912 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
23913 /* Roll back the tokens we skipped. */
23914 cp_lexer_rollback_tokens (parser
->lexer
);
23915 /* If we were looking at a compound-literal, simulate an error
23916 so that the call to cp_parser_parse_definitely below will
23918 if (compound_literal_p
)
23919 cp_parser_simulate_error (parser
);
23922 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
23923 parser
->in_type_id_in_expr_p
= true;
23924 /* Look for the type-id. */
23925 type
= cp_parser_type_id (parser
);
23926 /* Look for the closing `)'. */
23927 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23928 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
23931 /* If all went well, then we're done. */
23932 if (cp_parser_parse_definitely (parser
))
23934 cp_decl_specifier_seq decl_specs
;
23936 /* Build a trivial decl-specifier-seq. */
23937 clear_decl_specs (&decl_specs
);
23938 decl_specs
.type
= type
;
23940 /* Call grokdeclarator to figure out what type this is. */
23941 expr
= grokdeclarator (NULL
,
23945 /*attrlist=*/NULL
);
23949 /* If the type-id production did not work out, then we must be
23950 looking at the unary-expression production. */
23952 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
23953 /*cast_p=*/false, NULL
);
23955 /* Go back to evaluating expressions. */
23956 --cp_unevaluated_operand
;
23957 --c_inhibit_evaluation_warnings
;
23959 /* Free the message we created. */
23961 /* And restore the old one. */
23962 parser
->type_definition_forbidden_message
= saved_message
;
23963 parser
->integral_constant_expression_p
23964 = saved_integral_constant_expression_p
;
23965 parser
->non_integral_constant_expression_p
23966 = saved_non_integral_constant_expression_p
;
23971 /* If the current declaration has no declarator, return true. */
23974 cp_parser_declares_only_class_p (cp_parser
*parser
)
23976 /* If the next token is a `;' or a `,' then there is no
23978 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
23979 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
23982 /* Update the DECL_SPECS to reflect the storage class indicated by
23986 cp_parser_set_storage_class (cp_parser
*parser
,
23987 cp_decl_specifier_seq
*decl_specs
,
23991 cp_storage_class storage_class
;
23993 if (parser
->in_unbraced_linkage_specification_p
)
23995 error_at (token
->location
, "invalid use of %qD in linkage specification",
23996 ridpointers
[keyword
]);
23999 else if (decl_specs
->storage_class
!= sc_none
)
24001 decl_specs
->conflicting_specifiers_p
= true;
24005 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
24006 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
24007 && decl_specs
->gnu_thread_keyword_p
)
24009 pedwarn (decl_specs
->locations
[ds_thread
], 0,
24010 "%<__thread%> before %qD", ridpointers
[keyword
]);
24016 storage_class
= sc_auto
;
24019 storage_class
= sc_register
;
24022 storage_class
= sc_static
;
24025 storage_class
= sc_extern
;
24028 storage_class
= sc_mutable
;
24031 gcc_unreachable ();
24033 decl_specs
->storage_class
= storage_class
;
24034 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
24036 /* A storage class specifier cannot be applied alongside a typedef
24037 specifier. If there is a typedef specifier present then set
24038 conflicting_specifiers_p which will trigger an error later
24039 on in grokdeclarator. */
24040 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
24041 decl_specs
->conflicting_specifiers_p
= true;
24044 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24045 is true, the type is a class or enum definition. */
24048 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
24051 bool type_definition_p
)
24053 decl_specs
->any_specifiers_p
= true;
24055 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24056 (with, for example, in "typedef int wchar_t;") we remember that
24057 this is what happened. In system headers, we ignore these
24058 declarations so that G++ can work with system headers that are not
24060 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
24061 && !type_definition_p
24062 && (type_spec
== boolean_type_node
24063 || type_spec
== char16_type_node
24064 || type_spec
== char32_type_node
24065 || type_spec
== wchar_type_node
)
24066 && (decl_specs
->type
24067 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
24068 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
24069 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
24070 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
24072 decl_specs
->redefined_builtin_type
= type_spec
;
24073 set_and_check_decl_spec_loc (decl_specs
,
24074 ds_redefined_builtin_type_spec
,
24076 if (!decl_specs
->type
)
24078 decl_specs
->type
= type_spec
;
24079 decl_specs
->type_definition_p
= false;
24080 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
24083 else if (decl_specs
->type
)
24084 decl_specs
->multiple_types_p
= true;
24087 decl_specs
->type
= type_spec
;
24088 decl_specs
->type_definition_p
= type_definition_p
;
24089 decl_specs
->redefined_builtin_type
= NULL_TREE
;
24090 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
24094 /* True iff TOKEN is the GNU keyword __thread. */
24097 token_is__thread (cp_token
*token
)
24099 gcc_assert (token
->keyword
== RID_THREAD
);
24100 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
24103 /* Set the location for a declarator specifier and check if it is
24106 DECL_SPECS is the sequence of declarator specifiers onto which to
24109 DS is the single declarator specifier to set which location is to
24110 be set onto the existing sequence of declarators.
24112 LOCATION is the location for the declarator specifier to
24116 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
24117 cp_decl_spec ds
, cp_token
*token
)
24119 gcc_assert (ds
< ds_last
);
24121 if (decl_specs
== NULL
)
24124 source_location location
= token
->location
;
24126 if (decl_specs
->locations
[ds
] == 0)
24128 decl_specs
->locations
[ds
] = location
;
24129 if (ds
== ds_thread
)
24130 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
24136 if (decl_specs
->locations
[ds_long_long
] != 0)
24137 error_at (location
,
24138 "%<long long long%> is too long for GCC");
24141 decl_specs
->locations
[ds_long_long
] = location
;
24142 pedwarn_cxx98 (location
,
24144 "ISO C++ 1998 does not support %<long long%>");
24147 else if (ds
== ds_thread
)
24149 bool gnu
= token_is__thread (token
);
24150 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
24151 error_at (location
,
24152 "both %<__thread%> and %<thread_local%> specified");
24154 error_at (location
, "duplicate %qD", token
->u
.value
);
24158 static const char *const decl_spec_names
[] = {
24175 error_at (location
,
24176 "duplicate %qs", decl_spec_names
[ds
]);
24181 /* Return true iff the declarator specifier DS is present in the
24182 sequence of declarator specifiers DECL_SPECS. */
24185 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
24188 gcc_assert (ds
< ds_last
);
24190 if (decl_specs
== NULL
)
24193 return decl_specs
->locations
[ds
] != 0;
24196 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24197 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24200 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
24202 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
24205 /* Issue an error message indicating that TOKEN_DESC was expected.
24206 If KEYWORD is true, it indicated this function is called by
24207 cp_parser_require_keword and the required token can only be
24208 a indicated keyword. */
24211 cp_parser_required_error (cp_parser
*parser
,
24212 required_token token_desc
,
24215 switch (token_desc
)
24218 cp_parser_error (parser
, "expected %<new%>");
24221 cp_parser_error (parser
, "expected %<delete%>");
24224 cp_parser_error (parser
, "expected %<return%>");
24227 cp_parser_error (parser
, "expected %<while%>");
24230 cp_parser_error (parser
, "expected %<extern%>");
24232 case RT_STATIC_ASSERT
:
24233 cp_parser_error (parser
, "expected %<static_assert%>");
24236 cp_parser_error (parser
, "expected %<decltype%>");
24239 cp_parser_error (parser
, "expected %<operator%>");
24242 cp_parser_error (parser
, "expected %<class%>");
24245 cp_parser_error (parser
, "expected %<template%>");
24248 cp_parser_error (parser
, "expected %<namespace%>");
24251 cp_parser_error (parser
, "expected %<using%>");
24254 cp_parser_error (parser
, "expected %<asm%>");
24257 cp_parser_error (parser
, "expected %<try%>");
24260 cp_parser_error (parser
, "expected %<catch%>");
24263 cp_parser_error (parser
, "expected %<throw%>");
24266 cp_parser_error (parser
, "expected %<__label__%>");
24269 cp_parser_error (parser
, "expected %<@try%>");
24271 case RT_AT_SYNCHRONIZED
:
24272 cp_parser_error (parser
, "expected %<@synchronized%>");
24275 cp_parser_error (parser
, "expected %<@throw%>");
24277 case RT_TRANSACTION_ATOMIC
:
24278 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
24280 case RT_TRANSACTION_RELAXED
:
24281 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
24288 switch (token_desc
)
24291 cp_parser_error (parser
, "expected %<;%>");
24293 case RT_OPEN_PAREN
:
24294 cp_parser_error (parser
, "expected %<(%>");
24296 case RT_CLOSE_BRACE
:
24297 cp_parser_error (parser
, "expected %<}%>");
24299 case RT_OPEN_BRACE
:
24300 cp_parser_error (parser
, "expected %<{%>");
24302 case RT_CLOSE_SQUARE
:
24303 cp_parser_error (parser
, "expected %<]%>");
24305 case RT_OPEN_SQUARE
:
24306 cp_parser_error (parser
, "expected %<[%>");
24309 cp_parser_error (parser
, "expected %<,%>");
24312 cp_parser_error (parser
, "expected %<::%>");
24315 cp_parser_error (parser
, "expected %<<%>");
24318 cp_parser_error (parser
, "expected %<>%>");
24321 cp_parser_error (parser
, "expected %<=%>");
24324 cp_parser_error (parser
, "expected %<...%>");
24327 cp_parser_error (parser
, "expected %<*%>");
24330 cp_parser_error (parser
, "expected %<~%>");
24333 cp_parser_error (parser
, "expected %<:%>");
24335 case RT_COLON_SCOPE
:
24336 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24338 case RT_CLOSE_PAREN
:
24339 cp_parser_error (parser
, "expected %<)%>");
24341 case RT_COMMA_CLOSE_PAREN
:
24342 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24344 case RT_PRAGMA_EOL
:
24345 cp_parser_error (parser
, "expected end of line");
24348 cp_parser_error (parser
, "expected identifier");
24351 cp_parser_error (parser
, "expected selection-statement");
24353 case RT_INTERATION
:
24354 cp_parser_error (parser
, "expected iteration-statement");
24357 cp_parser_error (parser
, "expected jump-statement");
24360 cp_parser_error (parser
, "expected class-key");
24362 case RT_CLASS_TYPENAME_TEMPLATE
:
24363 cp_parser_error (parser
,
24364 "expected %<class%>, %<typename%>, or %<template%>");
24367 gcc_unreachable ();
24371 gcc_unreachable ();
24376 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24377 issue an error message indicating that TOKEN_DESC was expected.
24379 Returns the token consumed, if the token had the appropriate type.
24380 Otherwise, returns NULL. */
24383 cp_parser_require (cp_parser
* parser
,
24384 enum cpp_ttype type
,
24385 required_token token_desc
)
24387 if (cp_lexer_next_token_is (parser
->lexer
, type
))
24388 return cp_lexer_consume_token (parser
->lexer
);
24391 /* Output the MESSAGE -- unless we're parsing tentatively. */
24392 if (!cp_parser_simulate_error (parser
))
24393 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
24398 /* An error message is produced if the next token is not '>'.
24399 All further tokens are skipped until the desired token is
24400 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24403 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
24405 /* Current level of '< ... >'. */
24406 unsigned level
= 0;
24407 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24408 unsigned nesting_depth
= 0;
24410 /* Are we ready, yet? If not, issue error message. */
24411 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
24414 /* Skip tokens until the desired token is found. */
24417 /* Peek at the next token. */
24418 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24421 if (!nesting_depth
)
24426 if (cxx_dialect
== cxx98
)
24427 /* C++0x views the `>>' operator as two `>' tokens, but
24430 else if (!nesting_depth
&& level
-- == 0)
24432 /* We've hit a `>>' where the first `>' closes the
24433 template argument list, and the second `>' is
24434 spurious. Just consume the `>>' and stop; we've
24435 already produced at least one error. */
24436 cp_lexer_consume_token (parser
->lexer
);
24439 /* Fall through for C++0x, so we handle the second `>' in
24443 if (!nesting_depth
&& level
-- == 0)
24445 /* We've reached the token we want, consume it and stop. */
24446 cp_lexer_consume_token (parser
->lexer
);
24451 case CPP_OPEN_PAREN
:
24452 case CPP_OPEN_SQUARE
:
24456 case CPP_CLOSE_PAREN
:
24457 case CPP_CLOSE_SQUARE
:
24458 if (nesting_depth
-- == 0)
24463 case CPP_PRAGMA_EOL
:
24464 case CPP_SEMICOLON
:
24465 case CPP_OPEN_BRACE
:
24466 case CPP_CLOSE_BRACE
:
24467 /* The '>' was probably forgotten, don't look further. */
24474 /* Consume this token. */
24475 cp_lexer_consume_token (parser
->lexer
);
24479 /* If the next token is the indicated keyword, consume it. Otherwise,
24480 issue an error message indicating that TOKEN_DESC was expected.
24482 Returns the token consumed, if the token had the appropriate type.
24483 Otherwise, returns NULL. */
24486 cp_parser_require_keyword (cp_parser
* parser
,
24488 required_token token_desc
)
24490 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
24492 if (token
&& token
->keyword
!= keyword
)
24494 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
24501 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24502 function-definition. */
24505 cp_parser_token_starts_function_definition_p (cp_token
* token
)
24507 return (/* An ordinary function-body begins with an `{'. */
24508 token
->type
== CPP_OPEN_BRACE
24509 /* A ctor-initializer begins with a `:'. */
24510 || token
->type
== CPP_COLON
24511 /* A function-try-block begins with `try'. */
24512 || token
->keyword
== RID_TRY
24513 /* A function-transaction-block begins with `__transaction_atomic'
24514 or `__transaction_relaxed'. */
24515 || token
->keyword
== RID_TRANSACTION_ATOMIC
24516 || token
->keyword
== RID_TRANSACTION_RELAXED
24517 /* The named return value extension begins with `return'. */
24518 || token
->keyword
== RID_RETURN
);
24521 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24525 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
24529 token
= cp_lexer_peek_token (parser
->lexer
);
24530 return (token
->type
== CPP_OPEN_BRACE
24531 || (token
->type
== CPP_COLON
24532 && !parser
->colon_doesnt_start_class_def_p
));
24535 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24536 C++0x) ending a template-argument. */
24539 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
24543 token
= cp_lexer_peek_token (parser
->lexer
);
24544 return (token
->type
== CPP_COMMA
24545 || token
->type
== CPP_GREATER
24546 || token
->type
== CPP_ELLIPSIS
24547 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
24550 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24551 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24554 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
24559 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24560 if (token
->type
== CPP_LESS
)
24562 /* Check for the sequence `<::' in the original code. It would be lexed as
24563 `[:', where `[' is a digraph, and there is no whitespace before
24565 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
24568 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
24569 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
24575 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24576 or none_type otherwise. */
24578 static enum tag_types
24579 cp_parser_token_is_class_key (cp_token
* token
)
24581 switch (token
->keyword
)
24586 return record_type
;
24595 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24598 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
24600 if (type
== error_mark_node
)
24602 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
24604 if (permerror (input_location
, "%qs tag used in naming %q#T",
24605 class_key
== union_type
? "union"
24606 : class_key
== record_type
? "struct" : "class",
24608 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
24609 "%q#T was previously declared here", type
);
24613 /* Issue an error message if DECL is redeclared with different
24614 access than its original declaration [class.access.spec/3].
24615 This applies to nested classes and nested class templates.
24619 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
24621 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
24624 if ((TREE_PRIVATE (decl
)
24625 != (current_access_specifier
== access_private_node
))
24626 || (TREE_PROTECTED (decl
)
24627 != (current_access_specifier
== access_protected_node
)))
24628 error_at (location
, "%qD redeclared with different access", decl
);
24631 /* Look for the `template' keyword, as a syntactic disambiguator.
24632 Return TRUE iff it is present, in which case it will be
24636 cp_parser_optional_template_keyword (cp_parser
*parser
)
24638 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24640 /* In C++98 the `template' keyword can only be used within templates;
24641 outside templates the parser can always figure out what is a
24642 template and what is not. In C++11, per the resolution of DR 468,
24643 `template' is allowed in cases where it is not strictly necessary. */
24644 if (!processing_template_decl
24645 && pedantic
&& cxx_dialect
== cxx98
)
24647 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24648 pedwarn (token
->location
, OPT_Wpedantic
,
24649 "in C++98 %<template%> (as a disambiguator) is only "
24650 "allowed within templates");
24651 /* If this part of the token stream is rescanned, the same
24652 error message would be generated. So, we purge the token
24653 from the stream. */
24654 cp_lexer_purge_token (parser
->lexer
);
24659 /* Consume the `template' keyword. */
24660 cp_lexer_consume_token (parser
->lexer
);
24667 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24668 set PARSER->SCOPE, and perform other related actions. */
24671 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
24674 struct tree_check
*check_value
;
24675 deferred_access_check
*chk
;
24676 vec
<deferred_access_check
, va_gc
> *checks
;
24678 /* Get the stored value. */
24679 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
24680 /* Perform any access checks that were deferred. */
24681 checks
= check_value
->checks
;
24684 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
24685 perform_or_defer_access_check (chk
->binfo
,
24687 chk
->diag_decl
, tf_warning_or_error
);
24689 /* Set the scope from the stored value. */
24690 parser
->scope
= check_value
->value
;
24691 parser
->qualifying_scope
= check_value
->qualifying_scope
;
24692 parser
->object_scope
= NULL_TREE
;
24695 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24696 encounter the end of a block before what we were looking for. */
24699 cp_parser_cache_group (cp_parser
*parser
,
24700 enum cpp_ttype end
,
24705 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24707 /* Abort a parenthesized expression if we encounter a semicolon. */
24708 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
24709 && token
->type
== CPP_SEMICOLON
)
24711 /* If we've reached the end of the file, stop. */
24712 if (token
->type
== CPP_EOF
24713 || (end
!= CPP_PRAGMA_EOL
24714 && token
->type
== CPP_PRAGMA_EOL
))
24716 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
24717 /* We've hit the end of an enclosing block, so there's been some
24718 kind of syntax error. */
24721 /* Consume the token. */
24722 cp_lexer_consume_token (parser
->lexer
);
24723 /* See if it starts a new group. */
24724 if (token
->type
== CPP_OPEN_BRACE
)
24726 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
24727 /* In theory this should probably check end == '}', but
24728 cp_parser_save_member_function_body needs it to exit
24729 after either '}' or ')' when called with ')'. */
24733 else if (token
->type
== CPP_OPEN_PAREN
)
24735 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
24736 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
24739 else if (token
->type
== CPP_PRAGMA
)
24740 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
24741 else if (token
->type
== end
)
24746 /* Like above, for caching a default argument or NSDMI. Both of these are
24747 terminated by a non-nested comma, but it can be unclear whether or not a
24748 comma is nested in a template argument list unless we do more parsing.
24749 In order to handle this ambiguity, when we encounter a ',' after a '<'
24750 we try to parse what follows as a parameter-declaration-list (in the
24751 case of a default argument) or a member-declarator (in the case of an
24752 NSDMI). If that succeeds, then we stop caching. */
24755 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
24757 unsigned depth
= 0;
24758 int maybe_template_id
= 0;
24759 cp_token
*first_token
;
24761 tree default_argument
;
24763 /* Add tokens until we have processed the entire default
24764 argument. We add the range [first_token, token). */
24765 first_token
= cp_lexer_peek_token (parser
->lexer
);
24766 if (first_token
->type
== CPP_OPEN_BRACE
)
24768 /* For list-initialization, this is straightforward. */
24769 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
24770 token
= cp_lexer_peek_token (parser
->lexer
);
24776 /* Peek at the next token. */
24777 token
= cp_lexer_peek_token (parser
->lexer
);
24778 /* What we do depends on what token we have. */
24779 switch (token
->type
)
24781 /* In valid code, a default argument must be
24782 immediately followed by a `,' `)', or `...'. */
24784 if (depth
== 0 && maybe_template_id
)
24786 /* If we've seen a '<', we might be in a
24787 template-argument-list. Until Core issue 325 is
24788 resolved, we don't know how this situation ought
24789 to be handled, so try to DTRT. We check whether
24790 what comes after the comma is a valid parameter
24791 declaration list. If it is, then the comma ends
24792 the default argument; otherwise the default
24793 argument continues. */
24794 bool error
= false;
24796 /* Set ITALP so cp_parser_parameter_declaration_list
24797 doesn't decide to commit to this parse. */
24798 bool saved_italp
= parser
->in_template_argument_list_p
;
24799 parser
->in_template_argument_list_p
= true;
24801 cp_parser_parse_tentatively (parser
);
24802 cp_lexer_consume_token (parser
->lexer
);
24806 int ctor_dtor_or_conv_p
;
24807 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24808 &ctor_dtor_or_conv_p
,
24809 /*parenthesized_p=*/NULL
,
24810 /*member_p=*/true);
24814 begin_scope (sk_function_parms
, NULL_TREE
);
24815 cp_parser_parameter_declaration_list (parser
, &error
);
24816 pop_bindings_and_leave_scope ();
24818 if (!cp_parser_error_occurred (parser
) && !error
)
24820 cp_parser_abort_tentative_parse (parser
);
24822 parser
->in_template_argument_list_p
= saved_italp
;
24825 case CPP_CLOSE_PAREN
:
24827 /* If we run into a non-nested `;', `}', or `]',
24828 then the code is invalid -- but the default
24829 argument is certainly over. */
24830 case CPP_SEMICOLON
:
24831 case CPP_CLOSE_BRACE
:
24832 case CPP_CLOSE_SQUARE
:
24834 /* Handle correctly int n = sizeof ... ( p ); */
24835 && token
->type
!= CPP_ELLIPSIS
)
24837 /* Update DEPTH, if necessary. */
24838 else if (token
->type
== CPP_CLOSE_PAREN
24839 || token
->type
== CPP_CLOSE_BRACE
24840 || token
->type
== CPP_CLOSE_SQUARE
)
24844 case CPP_OPEN_PAREN
:
24845 case CPP_OPEN_SQUARE
:
24846 case CPP_OPEN_BRACE
:
24852 /* This might be the comparison operator, or it might
24853 start a template argument list. */
24854 ++maybe_template_id
;
24858 if (cxx_dialect
== cxx98
)
24860 /* Fall through for C++0x, which treats the `>>'
24861 operator like two `>' tokens in certain
24867 /* This might be an operator, or it might close a
24868 template argument list. But if a previous '<'
24869 started a template argument list, this will have
24870 closed it, so we can't be in one anymore. */
24871 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
24872 if (maybe_template_id
< 0)
24873 maybe_template_id
= 0;
24877 /* If we run out of tokens, issue an error message. */
24879 case CPP_PRAGMA_EOL
:
24880 error_at (token
->location
, "file ends in default argument");
24886 /* In these cases, we should look for template-ids.
24887 For example, if the default argument is
24888 `X<int, double>()', we need to do name lookup to
24889 figure out whether or not `X' is a template; if
24890 so, the `,' does not end the default argument.
24892 That is not yet done. */
24899 /* If we've reached the end, stop. */
24903 /* Add the token to the token block. */
24904 token
= cp_lexer_consume_token (parser
->lexer
);
24907 /* Create a DEFAULT_ARG to represent the unparsed default
24909 default_argument
= make_node (DEFAULT_ARG
);
24910 DEFARG_TOKENS (default_argument
)
24911 = cp_token_cache_new (first_token
, token
);
24912 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
24914 return default_argument
;
24917 /* Begin parsing tentatively. We always save tokens while parsing
24918 tentatively so that if the tentative parsing fails we can restore the
24922 cp_parser_parse_tentatively (cp_parser
* parser
)
24924 /* Enter a new parsing context. */
24925 parser
->context
= cp_parser_context_new (parser
->context
);
24926 /* Begin saving tokens. */
24927 cp_lexer_save_tokens (parser
->lexer
);
24928 /* In order to avoid repetitive access control error messages,
24929 access checks are queued up until we are no longer parsing
24931 push_deferring_access_checks (dk_deferred
);
24934 /* Commit to the currently active tentative parse. */
24937 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
24939 cp_parser_context
*context
;
24942 /* Mark all of the levels as committed. */
24943 lexer
= parser
->lexer
;
24944 for (context
= parser
->context
; context
->next
; context
= context
->next
)
24946 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24948 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24949 while (!cp_lexer_saving_tokens (lexer
))
24950 lexer
= lexer
->next
;
24951 cp_lexer_commit_tokens (lexer
);
24955 /* Commit to the topmost currently active tentative parse.
24957 Note that this function shouldn't be called when there are
24958 irreversible side-effects while in a tentative state. For
24959 example, we shouldn't create a permanent entry in the symbol
24960 table, or issue an error message that might not apply if the
24961 tentative parse is aborted. */
24964 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
24966 cp_parser_context
*context
= parser
->context
;
24967 cp_lexer
*lexer
= parser
->lexer
;
24971 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24973 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24975 while (!cp_lexer_saving_tokens (lexer
))
24976 lexer
= lexer
->next
;
24977 cp_lexer_commit_tokens (lexer
);
24981 /* Abort the currently active tentative parse. All consumed tokens
24982 will be rolled back, and no diagnostics will be issued. */
24985 cp_parser_abort_tentative_parse (cp_parser
* parser
)
24987 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
24988 || errorcount
> 0);
24989 cp_parser_simulate_error (parser
);
24990 /* Now, pretend that we want to see if the construct was
24991 successfully parsed. */
24992 cp_parser_parse_definitely (parser
);
24995 /* Stop parsing tentatively. If a parse error has occurred, restore the
24996 token stream. Otherwise, commit to the tokens we have consumed.
24997 Returns true if no error occurred; false otherwise. */
25000 cp_parser_parse_definitely (cp_parser
* parser
)
25002 bool error_occurred
;
25003 cp_parser_context
*context
;
25005 /* Remember whether or not an error occurred, since we are about to
25006 destroy that information. */
25007 error_occurred
= cp_parser_error_occurred (parser
);
25008 /* Remove the topmost context from the stack. */
25009 context
= parser
->context
;
25010 parser
->context
= context
->next
;
25011 /* If no parse errors occurred, commit to the tentative parse. */
25012 if (!error_occurred
)
25014 /* Commit to the tokens read tentatively, unless that was
25016 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
25017 cp_lexer_commit_tokens (parser
->lexer
);
25019 pop_to_parent_deferring_access_checks ();
25021 /* Otherwise, if errors occurred, roll back our state so that things
25022 are just as they were before we began the tentative parse. */
25025 cp_lexer_rollback_tokens (parser
->lexer
);
25026 pop_deferring_access_checks ();
25028 /* Add the context to the front of the free list. */
25029 context
->next
= cp_parser_context_free_list
;
25030 cp_parser_context_free_list
= context
;
25032 return !error_occurred
;
25035 /* Returns true if we are parsing tentatively and are not committed to
25036 this tentative parse. */
25039 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
25041 return (cp_parser_parsing_tentatively (parser
)
25042 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
25045 /* Returns nonzero iff an error has occurred during the most recent
25046 tentative parse. */
25049 cp_parser_error_occurred (cp_parser
* parser
)
25051 return (cp_parser_parsing_tentatively (parser
)
25052 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
25055 /* Returns nonzero if GNU extensions are allowed. */
25058 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
25060 return parser
->allow_gnu_extensions_p
;
25063 /* Objective-C++ Productions */
25066 /* Parse an Objective-C expression, which feeds into a primary-expression
25070 objc-message-expression
25071 objc-string-literal
25072 objc-encode-expression
25073 objc-protocol-expression
25074 objc-selector-expression
25076 Returns a tree representation of the expression. */
25079 cp_parser_objc_expression (cp_parser
* parser
)
25081 /* Try to figure out what kind of declaration is present. */
25082 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25086 case CPP_OPEN_SQUARE
:
25087 return cp_parser_objc_message_expression (parser
);
25089 case CPP_OBJC_STRING
:
25090 kwd
= cp_lexer_consume_token (parser
->lexer
);
25091 return objc_build_string_object (kwd
->u
.value
);
25094 switch (kwd
->keyword
)
25096 case RID_AT_ENCODE
:
25097 return cp_parser_objc_encode_expression (parser
);
25099 case RID_AT_PROTOCOL
:
25100 return cp_parser_objc_protocol_expression (parser
);
25102 case RID_AT_SELECTOR
:
25103 return cp_parser_objc_selector_expression (parser
);
25109 error_at (kwd
->location
,
25110 "misplaced %<@%D%> Objective-C++ construct",
25112 cp_parser_skip_to_end_of_block_or_statement (parser
);
25115 return error_mark_node
;
25118 /* Parse an Objective-C message expression.
25120 objc-message-expression:
25121 [ objc-message-receiver objc-message-args ]
25123 Returns a representation of an Objective-C message. */
25126 cp_parser_objc_message_expression (cp_parser
* parser
)
25128 tree receiver
, messageargs
;
25130 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
25131 receiver
= cp_parser_objc_message_receiver (parser
);
25132 messageargs
= cp_parser_objc_message_args (parser
);
25133 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25135 return objc_build_message_expr (receiver
, messageargs
);
25138 /* Parse an objc-message-receiver.
25140 objc-message-receiver:
25142 simple-type-specifier
25144 Returns a representation of the type or expression. */
25147 cp_parser_objc_message_receiver (cp_parser
* parser
)
25151 /* An Objective-C message receiver may be either (1) a type
25152 or (2) an expression. */
25153 cp_parser_parse_tentatively (parser
);
25154 rcv
= cp_parser_expression (parser
, false, NULL
);
25156 if (cp_parser_parse_definitely (parser
))
25159 rcv
= cp_parser_simple_type_specifier (parser
,
25160 /*decl_specs=*/NULL
,
25161 CP_PARSER_FLAGS_NONE
);
25163 return objc_get_class_reference (rcv
);
25166 /* Parse the arguments and selectors comprising an Objective-C message.
25171 objc-selector-args , objc-comma-args
25173 objc-selector-args:
25174 objc-selector [opt] : assignment-expression
25175 objc-selector-args objc-selector [opt] : assignment-expression
25178 assignment-expression
25179 objc-comma-args , assignment-expression
25181 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25182 selector arguments and TREE_VALUE containing a list of comma
25186 cp_parser_objc_message_args (cp_parser
* parser
)
25188 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
25189 bool maybe_unary_selector_p
= true;
25190 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25192 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25194 tree selector
= NULL_TREE
, arg
;
25196 if (token
->type
!= CPP_COLON
)
25197 selector
= cp_parser_objc_selector (parser
);
25199 /* Detect if we have a unary selector. */
25200 if (maybe_unary_selector_p
25201 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25202 return build_tree_list (selector
, NULL_TREE
);
25204 maybe_unary_selector_p
= false;
25205 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25206 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
25209 = chainon (sel_args
,
25210 build_tree_list (selector
, arg
));
25212 token
= cp_lexer_peek_token (parser
->lexer
);
25215 /* Handle non-selector arguments, if any. */
25216 while (token
->type
== CPP_COMMA
)
25220 cp_lexer_consume_token (parser
->lexer
);
25221 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
25224 = chainon (addl_args
,
25225 build_tree_list (NULL_TREE
, arg
));
25227 token
= cp_lexer_peek_token (parser
->lexer
);
25230 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
25232 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
25233 return build_tree_list (error_mark_node
, error_mark_node
);
25236 return build_tree_list (sel_args
, addl_args
);
25239 /* Parse an Objective-C encode expression.
25241 objc-encode-expression:
25242 @encode objc-typename
25244 Returns an encoded representation of the type argument. */
25247 cp_parser_objc_encode_expression (cp_parser
* parser
)
25252 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
25253 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25254 token
= cp_lexer_peek_token (parser
->lexer
);
25255 type
= complete_type (cp_parser_type_id (parser
));
25256 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25260 error_at (token
->location
,
25261 "%<@encode%> must specify a type as an argument");
25262 return error_mark_node
;
25265 /* This happens if we find @encode(T) (where T is a template
25266 typename or something dependent on a template typename) when
25267 parsing a template. In that case, we can't compile it
25268 immediately, but we rather create an AT_ENCODE_EXPR which will
25269 need to be instantiated when the template is used.
25271 if (dependent_type_p (type
))
25273 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
25274 TREE_READONLY (value
) = 1;
25278 return objc_build_encode_expr (type
);
25281 /* Parse an Objective-C @defs expression. */
25284 cp_parser_objc_defs_expression (cp_parser
*parser
)
25288 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
25289 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25290 name
= cp_parser_identifier (parser
);
25291 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25293 return objc_get_class_ivars (name
);
25296 /* Parse an Objective-C protocol expression.
25298 objc-protocol-expression:
25299 @protocol ( identifier )
25301 Returns a representation of the protocol expression. */
25304 cp_parser_objc_protocol_expression (cp_parser
* parser
)
25308 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25309 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25310 proto
= cp_parser_identifier (parser
);
25311 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25313 return objc_build_protocol_expr (proto
);
25316 /* Parse an Objective-C selector expression.
25318 objc-selector-expression:
25319 @selector ( objc-method-signature )
25321 objc-method-signature:
25327 objc-selector-seq objc-selector :
25329 Returns a representation of the method selector. */
25332 cp_parser_objc_selector_expression (cp_parser
* parser
)
25334 tree sel_seq
= NULL_TREE
;
25335 bool maybe_unary_selector_p
= true;
25337 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25339 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
25340 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25341 token
= cp_lexer_peek_token (parser
->lexer
);
25343 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
25344 || token
->type
== CPP_SCOPE
)
25346 tree selector
= NULL_TREE
;
25348 if (token
->type
!= CPP_COLON
25349 || token
->type
== CPP_SCOPE
)
25350 selector
= cp_parser_objc_selector (parser
);
25352 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
25353 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
25355 /* Detect if we have a unary selector. */
25356 if (maybe_unary_selector_p
)
25358 sel_seq
= selector
;
25359 goto finish_selector
;
25363 cp_parser_error (parser
, "expected %<:%>");
25366 maybe_unary_selector_p
= false;
25367 token
= cp_lexer_consume_token (parser
->lexer
);
25369 if (token
->type
== CPP_SCOPE
)
25372 = chainon (sel_seq
,
25373 build_tree_list (selector
, NULL_TREE
));
25375 = chainon (sel_seq
,
25376 build_tree_list (NULL_TREE
, NULL_TREE
));
25380 = chainon (sel_seq
,
25381 build_tree_list (selector
, NULL_TREE
));
25383 token
= cp_lexer_peek_token (parser
->lexer
);
25387 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25389 return objc_build_selector_expr (loc
, sel_seq
);
25392 /* Parse a list of identifiers.
25394 objc-identifier-list:
25396 objc-identifier-list , identifier
25398 Returns a TREE_LIST of identifier nodes. */
25401 cp_parser_objc_identifier_list (cp_parser
* parser
)
25407 identifier
= cp_parser_identifier (parser
);
25408 if (identifier
== error_mark_node
)
25409 return error_mark_node
;
25411 list
= build_tree_list (NULL_TREE
, identifier
);
25412 sep
= cp_lexer_peek_token (parser
->lexer
);
25414 while (sep
->type
== CPP_COMMA
)
25416 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25417 identifier
= cp_parser_identifier (parser
);
25418 if (identifier
== error_mark_node
)
25421 list
= chainon (list
, build_tree_list (NULL_TREE
,
25423 sep
= cp_lexer_peek_token (parser
->lexer
);
25429 /* Parse an Objective-C alias declaration.
25431 objc-alias-declaration:
25432 @compatibility_alias identifier identifier ;
25434 This function registers the alias mapping with the Objective-C front end.
25435 It returns nothing. */
25438 cp_parser_objc_alias_declaration (cp_parser
* parser
)
25442 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
25443 alias
= cp_parser_identifier (parser
);
25444 orig
= cp_parser_identifier (parser
);
25445 objc_declare_alias (alias
, orig
);
25446 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25449 /* Parse an Objective-C class forward-declaration.
25451 objc-class-declaration:
25452 @class objc-identifier-list ;
25454 The function registers the forward declarations with the Objective-C
25455 front end. It returns nothing. */
25458 cp_parser_objc_class_declaration (cp_parser
* parser
)
25460 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
25465 id
= cp_parser_identifier (parser
);
25466 if (id
== error_mark_node
)
25469 objc_declare_class (id
);
25471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25472 cp_lexer_consume_token (parser
->lexer
);
25476 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25479 /* Parse a list of Objective-C protocol references.
25481 objc-protocol-refs-opt:
25482 objc-protocol-refs [opt]
25484 objc-protocol-refs:
25485 < objc-identifier-list >
25487 Returns a TREE_LIST of identifiers, if any. */
25490 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
25492 tree protorefs
= NULL_TREE
;
25494 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
25496 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
25497 protorefs
= cp_parser_objc_identifier_list (parser
);
25498 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
25504 /* Parse a Objective-C visibility specification. */
25507 cp_parser_objc_visibility_spec (cp_parser
* parser
)
25509 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
25511 switch (vis
->keyword
)
25513 case RID_AT_PRIVATE
:
25514 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
25516 case RID_AT_PROTECTED
:
25517 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
25519 case RID_AT_PUBLIC
:
25520 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
25522 case RID_AT_PACKAGE
:
25523 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
25529 /* Eat '@private'/'@protected'/'@public'. */
25530 cp_lexer_consume_token (parser
->lexer
);
25533 /* Parse an Objective-C method type. Return 'true' if it is a class
25534 (+) method, and 'false' if it is an instance (-) method. */
25537 cp_parser_objc_method_type (cp_parser
* parser
)
25539 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
25545 /* Parse an Objective-C protocol qualifier. */
25548 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
25550 tree quals
= NULL_TREE
, node
;
25551 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25553 node
= token
->u
.value
;
25555 while (node
&& identifier_p (node
)
25556 && (node
== ridpointers
[(int) RID_IN
]
25557 || node
== ridpointers
[(int) RID_OUT
]
25558 || node
== ridpointers
[(int) RID_INOUT
]
25559 || node
== ridpointers
[(int) RID_BYCOPY
]
25560 || node
== ridpointers
[(int) RID_BYREF
]
25561 || node
== ridpointers
[(int) RID_ONEWAY
]))
25563 quals
= tree_cons (NULL_TREE
, node
, quals
);
25564 cp_lexer_consume_token (parser
->lexer
);
25565 token
= cp_lexer_peek_token (parser
->lexer
);
25566 node
= token
->u
.value
;
25572 /* Parse an Objective-C typename. */
25575 cp_parser_objc_typename (cp_parser
* parser
)
25577 tree type_name
= NULL_TREE
;
25579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25581 tree proto_quals
, cp_type
= NULL_TREE
;
25583 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
25584 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
25586 /* An ObjC type name may consist of just protocol qualifiers, in which
25587 case the type shall default to 'id'. */
25588 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
25590 cp_type
= cp_parser_type_id (parser
);
25592 /* If the type could not be parsed, an error has already
25593 been produced. For error recovery, behave as if it had
25594 not been specified, which will use the default type
25596 if (cp_type
== error_mark_node
)
25598 cp_type
= NULL_TREE
;
25599 /* We need to skip to the closing parenthesis as
25600 cp_parser_type_id() does not seem to do it for
25602 cp_parser_skip_to_closing_parenthesis (parser
,
25603 /*recovering=*/true,
25604 /*or_comma=*/false,
25605 /*consume_paren=*/false);
25609 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25610 type_name
= build_tree_list (proto_quals
, cp_type
);
25616 /* Check to see if TYPE refers to an Objective-C selector name. */
25619 cp_parser_objc_selector_p (enum cpp_ttype type
)
25621 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
25622 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
25623 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
25624 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
25625 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
25628 /* Parse an Objective-C selector. */
25631 cp_parser_objc_selector (cp_parser
* parser
)
25633 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
25635 if (!cp_parser_objc_selector_p (token
->type
))
25637 error_at (token
->location
, "invalid Objective-C++ selector name");
25638 return error_mark_node
;
25641 /* C++ operator names are allowed to appear in ObjC selectors. */
25642 switch (token
->type
)
25644 case CPP_AND_AND
: return get_identifier ("and");
25645 case CPP_AND_EQ
: return get_identifier ("and_eq");
25646 case CPP_AND
: return get_identifier ("bitand");
25647 case CPP_OR
: return get_identifier ("bitor");
25648 case CPP_COMPL
: return get_identifier ("compl");
25649 case CPP_NOT
: return get_identifier ("not");
25650 case CPP_NOT_EQ
: return get_identifier ("not_eq");
25651 case CPP_OR_OR
: return get_identifier ("or");
25652 case CPP_OR_EQ
: return get_identifier ("or_eq");
25653 case CPP_XOR
: return get_identifier ("xor");
25654 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
25655 default: return token
->u
.value
;
25659 /* Parse an Objective-C params list. */
25662 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
25664 tree params
= NULL_TREE
;
25665 bool maybe_unary_selector_p
= true;
25666 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25668 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25670 tree selector
= NULL_TREE
, type_name
, identifier
;
25671 tree parm_attr
= NULL_TREE
;
25673 if (token
->keyword
== RID_ATTRIBUTE
)
25676 if (token
->type
!= CPP_COLON
)
25677 selector
= cp_parser_objc_selector (parser
);
25679 /* Detect if we have a unary selector. */
25680 if (maybe_unary_selector_p
25681 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25683 params
= selector
; /* Might be followed by attributes. */
25687 maybe_unary_selector_p
= false;
25688 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
25690 /* Something went quite wrong. There should be a colon
25691 here, but there is not. Stop parsing parameters. */
25694 type_name
= cp_parser_objc_typename (parser
);
25695 /* New ObjC allows attributes on parameters too. */
25696 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
25697 parm_attr
= cp_parser_attributes_opt (parser
);
25698 identifier
= cp_parser_identifier (parser
);
25702 objc_build_keyword_decl (selector
,
25707 token
= cp_lexer_peek_token (parser
->lexer
);
25710 if (params
== NULL_TREE
)
25712 cp_parser_error (parser
, "objective-c++ method declaration is expected");
25713 return error_mark_node
;
25716 /* We allow tail attributes for the method. */
25717 if (token
->keyword
== RID_ATTRIBUTE
)
25719 *attributes
= cp_parser_attributes_opt (parser
);
25720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25721 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25723 cp_parser_error (parser
,
25724 "method attributes must be specified at the end");
25725 return error_mark_node
;
25728 if (params
== NULL_TREE
)
25730 cp_parser_error (parser
, "objective-c++ method declaration is expected");
25731 return error_mark_node
;
25736 /* Parse the non-keyword Objective-C params. */
25739 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
25742 tree params
= make_node (TREE_LIST
);
25743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25744 *ellipsisp
= false; /* Initially, assume no ellipsis. */
25746 while (token
->type
== CPP_COMMA
)
25748 cp_parameter_declarator
*parmdecl
;
25751 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25752 token
= cp_lexer_peek_token (parser
->lexer
);
25754 if (token
->type
== CPP_ELLIPSIS
)
25756 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
25758 token
= cp_lexer_peek_token (parser
->lexer
);
25762 /* TODO: parse attributes for tail parameters. */
25763 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
25764 parm
= grokdeclarator (parmdecl
->declarator
,
25765 &parmdecl
->decl_specifiers
,
25766 PARM
, /*initialized=*/0,
25767 /*attrlist=*/NULL
);
25769 chainon (params
, build_tree_list (NULL_TREE
, parm
));
25770 token
= cp_lexer_peek_token (parser
->lexer
);
25773 /* We allow tail attributes for the method. */
25774 if (token
->keyword
== RID_ATTRIBUTE
)
25776 if (*attributes
== NULL_TREE
)
25778 *attributes
= cp_parser_attributes_opt (parser
);
25779 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25780 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25784 /* We have an error, but parse the attributes, so that we can
25786 *attributes
= cp_parser_attributes_opt (parser
);
25788 cp_parser_error (parser
,
25789 "method attributes must be specified at the end");
25790 return error_mark_node
;
25796 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25799 cp_parser_objc_interstitial_code (cp_parser
* parser
)
25801 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25803 /* If the next token is `extern' and the following token is a string
25804 literal, then we have a linkage specification. */
25805 if (token
->keyword
== RID_EXTERN
25806 && cp_parser_is_pure_string_literal
25807 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
25808 cp_parser_linkage_specification (parser
);
25809 /* Handle #pragma, if any. */
25810 else if (token
->type
== CPP_PRAGMA
)
25811 cp_parser_pragma (parser
, pragma_objc_icode
);
25812 /* Allow stray semicolons. */
25813 else if (token
->type
== CPP_SEMICOLON
)
25814 cp_lexer_consume_token (parser
->lexer
);
25815 /* Mark methods as optional or required, when building protocols. */
25816 else if (token
->keyword
== RID_AT_OPTIONAL
)
25818 cp_lexer_consume_token (parser
->lexer
);
25819 objc_set_method_opt (true);
25821 else if (token
->keyword
== RID_AT_REQUIRED
)
25823 cp_lexer_consume_token (parser
->lexer
);
25824 objc_set_method_opt (false);
25826 else if (token
->keyword
== RID_NAMESPACE
)
25827 cp_parser_namespace_definition (parser
);
25828 /* Other stray characters must generate errors. */
25829 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
25831 cp_lexer_consume_token (parser
->lexer
);
25832 error ("stray %qs between Objective-C++ methods",
25833 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
25835 /* Finally, try to parse a block-declaration, or a function-definition. */
25837 cp_parser_block_declaration (parser
, /*statement_p=*/false);
25840 /* Parse a method signature. */
25843 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
25845 tree rettype
, kwdparms
, optparms
;
25846 bool ellipsis
= false;
25847 bool is_class_method
;
25849 is_class_method
= cp_parser_objc_method_type (parser
);
25850 rettype
= cp_parser_objc_typename (parser
);
25851 *attributes
= NULL_TREE
;
25852 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
25853 if (kwdparms
== error_mark_node
)
25854 return error_mark_node
;
25855 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
25856 if (optparms
== error_mark_node
)
25857 return error_mark_node
;
25859 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
25863 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
25866 cp_lexer_save_tokens (parser
->lexer
);
25867 tattr
= cp_parser_attributes_opt (parser
);
25868 gcc_assert (tattr
) ;
25870 /* If the attributes are followed by a method introducer, this is not allowed.
25871 Dump the attributes and flag the situation. */
25872 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
25873 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
25876 /* Otherwise, the attributes introduce some interstitial code, possibly so
25877 rewind to allow that check. */
25878 cp_lexer_rollback_tokens (parser
->lexer
);
25882 /* Parse an Objective-C method prototype list. */
25885 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
25887 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25889 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25891 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
25893 tree attributes
, sig
;
25894 bool is_class_method
;
25895 if (token
->type
== CPP_PLUS
)
25896 is_class_method
= true;
25898 is_class_method
= false;
25899 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
25900 if (sig
== error_mark_node
)
25902 cp_parser_skip_to_end_of_block_or_statement (parser
);
25903 token
= cp_lexer_peek_token (parser
->lexer
);
25906 objc_add_method_declaration (is_class_method
, sig
, attributes
);
25907 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25909 else if (token
->keyword
== RID_AT_PROPERTY
)
25910 cp_parser_objc_at_property_declaration (parser
);
25911 else if (token
->keyword
== RID_ATTRIBUTE
25912 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25913 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25915 "prefix attributes are ignored for methods");
25917 /* Allow for interspersed non-ObjC++ code. */
25918 cp_parser_objc_interstitial_code (parser
);
25920 token
= cp_lexer_peek_token (parser
->lexer
);
25923 if (token
->type
!= CPP_EOF
)
25924 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25926 cp_parser_error (parser
, "expected %<@end%>");
25928 objc_finish_interface ();
25931 /* Parse an Objective-C method definition list. */
25934 cp_parser_objc_method_definition_list (cp_parser
* parser
)
25936 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25938 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25942 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
25945 tree sig
, attribute
;
25946 bool is_class_method
;
25947 if (token
->type
== CPP_PLUS
)
25948 is_class_method
= true;
25950 is_class_method
= false;
25951 push_deferring_access_checks (dk_deferred
);
25952 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
25953 if (sig
== error_mark_node
)
25955 cp_parser_skip_to_end_of_block_or_statement (parser
);
25956 token
= cp_lexer_peek_token (parser
->lexer
);
25959 objc_start_method_definition (is_class_method
, sig
, attribute
,
25962 /* For historical reasons, we accept an optional semicolon. */
25963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25964 cp_lexer_consume_token (parser
->lexer
);
25966 ptk
= cp_lexer_peek_token (parser
->lexer
);
25967 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
25968 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
25970 perform_deferred_access_checks (tf_warning_or_error
);
25971 stop_deferring_access_checks ();
25972 meth
= cp_parser_function_definition_after_declarator (parser
,
25974 pop_deferring_access_checks ();
25975 objc_finish_method_definition (meth
);
25978 /* The following case will be removed once @synthesize is
25979 completely implemented. */
25980 else if (token
->keyword
== RID_AT_PROPERTY
)
25981 cp_parser_objc_at_property_declaration (parser
);
25982 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
25983 cp_parser_objc_at_synthesize_declaration (parser
);
25984 else if (token
->keyword
== RID_AT_DYNAMIC
)
25985 cp_parser_objc_at_dynamic_declaration (parser
);
25986 else if (token
->keyword
== RID_ATTRIBUTE
25987 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25988 warning_at (token
->location
, OPT_Wattributes
,
25989 "prefix attributes are ignored for methods");
25991 /* Allow for interspersed non-ObjC++ code. */
25992 cp_parser_objc_interstitial_code (parser
);
25994 token
= cp_lexer_peek_token (parser
->lexer
);
25997 if (token
->type
!= CPP_EOF
)
25998 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26000 cp_parser_error (parser
, "expected %<@end%>");
26002 objc_finish_implementation ();
26005 /* Parse Objective-C ivars. */
26008 cp_parser_objc_class_ivars (cp_parser
* parser
)
26010 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26012 if (token
->type
!= CPP_OPEN_BRACE
)
26013 return; /* No ivars specified. */
26015 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
26016 token
= cp_lexer_peek_token (parser
->lexer
);
26018 while (token
->type
!= CPP_CLOSE_BRACE
26019 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26021 cp_decl_specifier_seq declspecs
;
26022 int decl_class_or_enum_p
;
26023 tree prefix_attributes
;
26025 cp_parser_objc_visibility_spec (parser
);
26027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26030 cp_parser_decl_specifier_seq (parser
,
26031 CP_PARSER_FLAGS_OPTIONAL
,
26033 &decl_class_or_enum_p
);
26035 /* auto, register, static, extern, mutable. */
26036 if (declspecs
.storage_class
!= sc_none
)
26038 cp_parser_error (parser
, "invalid type for instance variable");
26039 declspecs
.storage_class
= sc_none
;
26042 /* thread_local. */
26043 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26045 cp_parser_error (parser
, "invalid type for instance variable");
26046 declspecs
.locations
[ds_thread
] = 0;
26050 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26052 cp_parser_error (parser
, "invalid type for instance variable");
26053 declspecs
.locations
[ds_typedef
] = 0;
26056 prefix_attributes
= declspecs
.attributes
;
26057 declspecs
.attributes
= NULL_TREE
;
26059 /* Keep going until we hit the `;' at the end of the
26061 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26063 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
26064 cp_declarator
*declarator
= NULL
;
26065 int ctor_dtor_or_conv_p
;
26067 /* Check for a (possibly unnamed) bitfield declaration. */
26068 token
= cp_lexer_peek_token (parser
->lexer
);
26069 if (token
->type
== CPP_COLON
)
26072 if (token
->type
== CPP_NAME
26073 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
26076 /* Get the name of the bitfield. */
26077 declarator
= make_id_declarator (NULL_TREE
,
26078 cp_parser_identifier (parser
),
26082 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26083 /* Get the width of the bitfield. */
26085 = cp_parser_constant_expression (parser
,
26086 /*allow_non_constant=*/false,
26091 /* Parse the declarator. */
26093 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26094 &ctor_dtor_or_conv_p
,
26095 /*parenthesized_p=*/NULL
,
26096 /*member_p=*/false);
26099 /* Look for attributes that apply to the ivar. */
26100 attributes
= cp_parser_attributes_opt (parser
);
26101 /* Remember which attributes are prefix attributes and
26103 first_attribute
= attributes
;
26104 /* Combine the attributes. */
26105 attributes
= chainon (prefix_attributes
, attributes
);
26108 /* Create the bitfield declaration. */
26109 decl
= grokbitfield (declarator
, &declspecs
,
26113 decl
= grokfield (declarator
, &declspecs
,
26114 NULL_TREE
, /*init_const_expr_p=*/false,
26115 NULL_TREE
, attributes
);
26117 /* Add the instance variable. */
26118 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
26119 objc_add_instance_variable (decl
);
26121 /* Reset PREFIX_ATTRIBUTES. */
26122 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26123 attributes
= TREE_CHAIN (attributes
);
26125 TREE_CHAIN (attributes
) = NULL_TREE
;
26127 token
= cp_lexer_peek_token (parser
->lexer
);
26129 if (token
->type
== CPP_COMMA
)
26131 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26137 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26138 token
= cp_lexer_peek_token (parser
->lexer
);
26141 if (token
->keyword
== RID_AT_END
)
26142 cp_parser_error (parser
, "expected %<}%>");
26144 /* Do not consume the RID_AT_END, so it will be read again as terminating
26145 the @interface of @implementation. */
26146 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26147 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
26149 /* For historical reasons, we accept an optional semicolon. */
26150 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26151 cp_lexer_consume_token (parser
->lexer
);
26154 /* Parse an Objective-C protocol declaration. */
26157 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
26159 tree proto
, protorefs
;
26162 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
26163 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
26165 tok
= cp_lexer_peek_token (parser
->lexer
);
26166 error_at (tok
->location
, "identifier expected after %<@protocol%>");
26167 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26171 /* See if we have a forward declaration or a definition. */
26172 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26174 /* Try a forward declaration first. */
26175 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
26181 id
= cp_parser_identifier (parser
);
26182 if (id
== error_mark_node
)
26185 objc_declare_protocol (id
, attributes
);
26187 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26188 cp_lexer_consume_token (parser
->lexer
);
26192 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26195 /* Ok, we got a full-fledged definition (or at least should). */
26198 proto
= cp_parser_identifier (parser
);
26199 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
26200 objc_start_protocol (proto
, protorefs
, attributes
);
26201 cp_parser_objc_method_prototype_list (parser
);
26205 /* Parse an Objective-C superclass or category. */
26208 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
26211 tree
*categ
, bool *is_class_extension
)
26213 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
26215 *super
= *categ
= NULL_TREE
;
26216 *is_class_extension
= false;
26217 if (next
->type
== CPP_COLON
)
26219 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26220 *super
= cp_parser_identifier (parser
);
26222 else if (next
->type
== CPP_OPEN_PAREN
)
26224 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26226 /* If there is no category name, and this is an @interface, we
26227 have a class extension. */
26228 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26230 *categ
= NULL_TREE
;
26231 *is_class_extension
= true;
26234 *categ
= cp_parser_identifier (parser
);
26236 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26240 /* Parse an Objective-C class interface. */
26243 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
26245 tree name
, super
, categ
, protos
;
26246 bool is_class_extension
;
26248 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
26249 name
= cp_parser_identifier (parser
);
26250 if (name
== error_mark_node
)
26252 /* It's hard to recover because even if valid @interface stuff
26253 is to follow, we can't compile it (or validate it) if we
26254 don't even know which class it refers to. Let's assume this
26255 was a stray '@interface' token in the stream and skip it.
26259 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
26260 &is_class_extension
);
26261 protos
= cp_parser_objc_protocol_refs_opt (parser
);
26263 /* We have either a class or a category on our hands. */
26264 if (categ
|| is_class_extension
)
26265 objc_start_category_interface (name
, categ
, protos
, attributes
);
26268 objc_start_class_interface (name
, super
, protos
, attributes
);
26269 /* Handle instance variable declarations, if any. */
26270 cp_parser_objc_class_ivars (parser
);
26271 objc_continue_interface ();
26274 cp_parser_objc_method_prototype_list (parser
);
26277 /* Parse an Objective-C class implementation. */
26280 cp_parser_objc_class_implementation (cp_parser
* parser
)
26282 tree name
, super
, categ
;
26283 bool is_class_extension
;
26285 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
26286 name
= cp_parser_identifier (parser
);
26287 if (name
== error_mark_node
)
26289 /* It's hard to recover because even if valid @implementation
26290 stuff is to follow, we can't compile it (or validate it) if
26291 we don't even know which class it refers to. Let's assume
26292 this was a stray '@implementation' token in the stream and
26297 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
26298 &is_class_extension
);
26300 /* We have either a class or a category on our hands. */
26302 objc_start_category_implementation (name
, categ
);
26305 objc_start_class_implementation (name
, super
);
26306 /* Handle instance variable declarations, if any. */
26307 cp_parser_objc_class_ivars (parser
);
26308 objc_continue_implementation ();
26311 cp_parser_objc_method_definition_list (parser
);
26314 /* Consume the @end token and finish off the implementation. */
26317 cp_parser_objc_end_implementation (cp_parser
* parser
)
26319 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26320 objc_finish_implementation ();
26323 /* Parse an Objective-C declaration. */
26326 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
26328 /* Try to figure out what kind of declaration is present. */
26329 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26332 switch (kwd
->keyword
)
26337 error_at (kwd
->location
, "attributes may not be specified before"
26338 " the %<@%D%> Objective-C++ keyword",
26342 case RID_AT_IMPLEMENTATION
:
26343 warning_at (kwd
->location
, OPT_Wattributes
,
26344 "prefix attributes are ignored before %<@%D%>",
26351 switch (kwd
->keyword
)
26354 cp_parser_objc_alias_declaration (parser
);
26357 cp_parser_objc_class_declaration (parser
);
26359 case RID_AT_PROTOCOL
:
26360 cp_parser_objc_protocol_declaration (parser
, attributes
);
26362 case RID_AT_INTERFACE
:
26363 cp_parser_objc_class_interface (parser
, attributes
);
26365 case RID_AT_IMPLEMENTATION
:
26366 cp_parser_objc_class_implementation (parser
);
26369 cp_parser_objc_end_implementation (parser
);
26372 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26374 cp_parser_skip_to_end_of_block_or_statement (parser
);
26378 /* Parse an Objective-C try-catch-finally statement.
26380 objc-try-catch-finally-stmt:
26381 @try compound-statement objc-catch-clause-seq [opt]
26382 objc-finally-clause [opt]
26384 objc-catch-clause-seq:
26385 objc-catch-clause objc-catch-clause-seq [opt]
26388 @catch ( objc-exception-declaration ) compound-statement
26390 objc-finally-clause:
26391 @finally compound-statement
26393 objc-exception-declaration:
26394 parameter-declaration
26397 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26401 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26402 for C. Keep them in sync. */
26405 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
26407 location_t location
;
26410 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
26411 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26412 objc_maybe_warn_exceptions (location
);
26413 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26414 node, lest it get absorbed into the surrounding block. */
26415 stmt
= push_stmt_list ();
26416 cp_parser_compound_statement (parser
, NULL
, false, false);
26417 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
26419 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
26421 cp_parameter_declarator
*parm
;
26422 tree parameter_declaration
= error_mark_node
;
26423 bool seen_open_paren
= false;
26425 cp_lexer_consume_token (parser
->lexer
);
26426 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26427 seen_open_paren
= true;
26428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26430 /* We have "@catch (...)" (where the '...' are literally
26431 what is in the code). Skip the '...'.
26432 parameter_declaration is set to NULL_TREE, and
26433 objc_being_catch_clauses() knows that that means
26435 cp_lexer_consume_token (parser
->lexer
);
26436 parameter_declaration
= NULL_TREE
;
26440 /* We have "@catch (NSException *exception)" or something
26441 like that. Parse the parameter declaration. */
26442 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
26444 parameter_declaration
= error_mark_node
;
26446 parameter_declaration
= grokdeclarator (parm
->declarator
,
26447 &parm
->decl_specifiers
,
26448 PARM
, /*initialized=*/0,
26449 /*attrlist=*/NULL
);
26451 if (seen_open_paren
)
26452 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26455 /* If there was no open parenthesis, we are recovering from
26456 an error, and we are trying to figure out what mistake
26457 the user has made. */
26459 /* If there is an immediate closing parenthesis, the user
26460 probably forgot the opening one (ie, they typed "@catch
26461 NSException *e)". Parse the closing parenthesis and keep
26463 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26464 cp_lexer_consume_token (parser
->lexer
);
26466 /* If these is no immediate closing parenthesis, the user
26467 probably doesn't know that parenthesis are required at
26468 all (ie, they typed "@catch NSException *e"). So, just
26469 forget about the closing parenthesis and keep going. */
26471 objc_begin_catch_clause (parameter_declaration
);
26472 cp_parser_compound_statement (parser
, NULL
, false, false);
26473 objc_finish_catch_clause ();
26475 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
26477 cp_lexer_consume_token (parser
->lexer
);
26478 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26479 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26480 node, lest it get absorbed into the surrounding block. */
26481 stmt
= push_stmt_list ();
26482 cp_parser_compound_statement (parser
, NULL
, false, false);
26483 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
26486 return objc_finish_try_stmt ();
26489 /* Parse an Objective-C synchronized statement.
26491 objc-synchronized-stmt:
26492 @synchronized ( expression ) compound-statement
26494 Returns NULL_TREE. */
26497 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
26499 location_t location
;
26502 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
26504 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26505 objc_maybe_warn_exceptions (location
);
26506 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
26507 lock
= cp_parser_expression (parser
, false, NULL
);
26508 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26510 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26511 node, lest it get absorbed into the surrounding block. */
26512 stmt
= push_stmt_list ();
26513 cp_parser_compound_statement (parser
, NULL
, false, false);
26515 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
26518 /* Parse an Objective-C throw statement.
26521 @throw assignment-expression [opt] ;
26523 Returns a constructed '@throw' statement. */
26526 cp_parser_objc_throw_statement (cp_parser
*parser
)
26528 tree expr
= NULL_TREE
;
26529 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26531 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
26533 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26534 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
26536 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26538 return objc_build_throw_stmt (loc
, expr
);
26541 /* Parse an Objective-C statement. */
26544 cp_parser_objc_statement (cp_parser
* parser
)
26546 /* Try to figure out what kind of declaration is present. */
26547 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26549 switch (kwd
->keyword
)
26552 return cp_parser_objc_try_catch_finally_statement (parser
);
26553 case RID_AT_SYNCHRONIZED
:
26554 return cp_parser_objc_synchronized_statement (parser
);
26556 return cp_parser_objc_throw_statement (parser
);
26558 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26560 cp_parser_skip_to_end_of_block_or_statement (parser
);
26563 return error_mark_node
;
26566 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26567 look ahead to see if an objc keyword follows the attributes. This
26568 is to detect the use of prefix attributes on ObjC @interface and
26572 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
26574 cp_lexer_save_tokens (parser
->lexer
);
26575 *attrib
= cp_parser_attributes_opt (parser
);
26576 gcc_assert (*attrib
);
26577 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
26579 cp_lexer_commit_tokens (parser
->lexer
);
26582 cp_lexer_rollback_tokens (parser
->lexer
);
26586 /* This routine is a minimal replacement for
26587 c_parser_struct_declaration () used when parsing the list of
26588 types/names or ObjC++ properties. For example, when parsing the
26591 @property (readonly) int a, b, c;
26593 this function is responsible for parsing "int a, int b, int c" and
26594 returning the declarations as CHAIN of DECLs.
26596 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26597 similar parsing. */
26599 cp_parser_objc_struct_declaration (cp_parser
*parser
)
26601 tree decls
= NULL_TREE
;
26602 cp_decl_specifier_seq declspecs
;
26603 int decl_class_or_enum_p
;
26604 tree prefix_attributes
;
26606 cp_parser_decl_specifier_seq (parser
,
26607 CP_PARSER_FLAGS_NONE
,
26609 &decl_class_or_enum_p
);
26611 if (declspecs
.type
== error_mark_node
)
26612 return error_mark_node
;
26614 /* auto, register, static, extern, mutable. */
26615 if (declspecs
.storage_class
!= sc_none
)
26617 cp_parser_error (parser
, "invalid type for property");
26618 declspecs
.storage_class
= sc_none
;
26621 /* thread_local. */
26622 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26624 cp_parser_error (parser
, "invalid type for property");
26625 declspecs
.locations
[ds_thread
] = 0;
26629 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26631 cp_parser_error (parser
, "invalid type for property");
26632 declspecs
.locations
[ds_typedef
] = 0;
26635 prefix_attributes
= declspecs
.attributes
;
26636 declspecs
.attributes
= NULL_TREE
;
26638 /* Keep going until we hit the `;' at the end of the declaration. */
26639 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26641 tree attributes
, first_attribute
, decl
;
26642 cp_declarator
*declarator
;
26645 /* Parse the declarator. */
26646 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26647 NULL
, NULL
, false);
26649 /* Look for attributes that apply to the ivar. */
26650 attributes
= cp_parser_attributes_opt (parser
);
26651 /* Remember which attributes are prefix attributes and
26653 first_attribute
= attributes
;
26654 /* Combine the attributes. */
26655 attributes
= chainon (prefix_attributes
, attributes
);
26657 decl
= grokfield (declarator
, &declspecs
,
26658 NULL_TREE
, /*init_const_expr_p=*/false,
26659 NULL_TREE
, attributes
);
26661 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
26662 return error_mark_node
;
26664 /* Reset PREFIX_ATTRIBUTES. */
26665 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26666 attributes
= TREE_CHAIN (attributes
);
26668 TREE_CHAIN (attributes
) = NULL_TREE
;
26670 DECL_CHAIN (decl
) = decls
;
26673 token
= cp_lexer_peek_token (parser
->lexer
);
26674 if (token
->type
== CPP_COMMA
)
26676 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26685 /* Parse an Objective-C @property declaration. The syntax is:
26687 objc-property-declaration:
26688 '@property' objc-property-attributes[opt] struct-declaration ;
26690 objc-property-attributes:
26691 '(' objc-property-attribute-list ')'
26693 objc-property-attribute-list:
26694 objc-property-attribute
26695 objc-property-attribute-list, objc-property-attribute
26697 objc-property-attribute
26698 'getter' = identifier
26699 'setter' = identifier
26708 @property NSString *name;
26709 @property (readonly) id object;
26710 @property (retain, nonatomic, getter=getTheName) id name;
26711 @property int a, b, c;
26713 PS: This function is identical to
26714 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26716 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
26718 /* The following variables hold the attributes of the properties as
26719 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26720 seen. When we see an attribute, we set them to 'true' (if they
26721 are boolean properties) or to the identifier (if they have an
26722 argument, ie, for getter and setter). Note that here we only
26723 parse the list of attributes, check the syntax and accumulate the
26724 attributes that we find. objc_add_property_declaration() will
26725 then process the information. */
26726 bool property_assign
= false;
26727 bool property_copy
= false;
26728 tree property_getter_ident
= NULL_TREE
;
26729 bool property_nonatomic
= false;
26730 bool property_readonly
= false;
26731 bool property_readwrite
= false;
26732 bool property_retain
= false;
26733 tree property_setter_ident
= NULL_TREE
;
26735 /* 'properties' is the list of properties that we read. Usually a
26736 single one, but maybe more (eg, in "@property int a, b, c;" there
26741 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26743 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
26745 /* Parse the optional attribute list... */
26746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26749 cp_lexer_consume_token (parser
->lexer
);
26753 bool syntax_error
= false;
26754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26757 if (token
->type
!= CPP_NAME
)
26759 cp_parser_error (parser
, "expected identifier");
26762 keyword
= C_RID_CODE (token
->u
.value
);
26763 cp_lexer_consume_token (parser
->lexer
);
26766 case RID_ASSIGN
: property_assign
= true; break;
26767 case RID_COPY
: property_copy
= true; break;
26768 case RID_NONATOMIC
: property_nonatomic
= true; break;
26769 case RID_READONLY
: property_readonly
= true; break;
26770 case RID_READWRITE
: property_readwrite
= true; break;
26771 case RID_RETAIN
: property_retain
= true; break;
26775 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
26777 if (keyword
== RID_GETTER
)
26778 cp_parser_error (parser
,
26779 "missing %<=%> (after %<getter%> attribute)");
26781 cp_parser_error (parser
,
26782 "missing %<=%> (after %<setter%> attribute)");
26783 syntax_error
= true;
26786 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
26787 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
26789 cp_parser_error (parser
, "expected identifier");
26790 syntax_error
= true;
26793 if (keyword
== RID_SETTER
)
26795 if (property_setter_ident
!= NULL_TREE
)
26797 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
26798 cp_lexer_consume_token (parser
->lexer
);
26801 property_setter_ident
= cp_parser_objc_selector (parser
);
26802 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26803 cp_parser_error (parser
, "setter name must terminate with %<:%>");
26805 cp_lexer_consume_token (parser
->lexer
);
26809 if (property_getter_ident
!= NULL_TREE
)
26811 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
26812 cp_lexer_consume_token (parser
->lexer
);
26815 property_getter_ident
= cp_parser_objc_selector (parser
);
26819 cp_parser_error (parser
, "unknown property attribute");
26820 syntax_error
= true;
26827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26828 cp_lexer_consume_token (parser
->lexer
);
26833 /* FIXME: "@property (setter, assign);" will generate a spurious
26834 "error: expected ‘)’ before ‘,’ token". This is because
26835 cp_parser_require, unlike the C counterpart, will produce an
26836 error even if we are in error recovery. */
26837 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26839 cp_parser_skip_to_closing_parenthesis (parser
,
26840 /*recovering=*/true,
26841 /*or_comma=*/false,
26842 /*consume_paren=*/true);
26846 /* ... and the property declaration(s). */
26847 properties
= cp_parser_objc_struct_declaration (parser
);
26849 if (properties
== error_mark_node
)
26851 cp_parser_skip_to_end_of_statement (parser
);
26852 /* If the next token is now a `;', consume it. */
26853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26854 cp_lexer_consume_token (parser
->lexer
);
26858 if (properties
== NULL_TREE
)
26859 cp_parser_error (parser
, "expected identifier");
26862 /* Comma-separated properties are chained together in
26863 reverse order; add them one by one. */
26864 properties
= nreverse (properties
);
26866 for (; properties
; properties
= TREE_CHAIN (properties
))
26867 objc_add_property_declaration (loc
, copy_node (properties
),
26868 property_readonly
, property_readwrite
,
26869 property_assign
, property_retain
,
26870 property_copy
, property_nonatomic
,
26871 property_getter_ident
, property_setter_ident
);
26874 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26877 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26879 objc-synthesize-declaration:
26880 @synthesize objc-synthesize-identifier-list ;
26882 objc-synthesize-identifier-list:
26883 objc-synthesize-identifier
26884 objc-synthesize-identifier-list, objc-synthesize-identifier
26886 objc-synthesize-identifier
26888 identifier = identifier
26891 @synthesize MyProperty;
26892 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26894 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26895 for C. Keep them in sync.
26898 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
26900 tree list
= NULL_TREE
;
26902 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26904 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
26907 tree property
, ivar
;
26908 property
= cp_parser_identifier (parser
);
26909 if (property
== error_mark_node
)
26911 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
26916 cp_lexer_consume_token (parser
->lexer
);
26917 ivar
= cp_parser_identifier (parser
);
26918 if (ivar
== error_mark_node
)
26920 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26926 list
= chainon (list
, build_tree_list (ivar
, property
));
26927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26928 cp_lexer_consume_token (parser
->lexer
);
26932 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26933 objc_add_synthesize_declaration (loc
, list
);
26936 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26938 objc-dynamic-declaration:
26939 @dynamic identifier-list ;
26942 @dynamic MyProperty;
26943 @dynamic MyProperty, AnotherProperty;
26945 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26946 for C. Keep them in sync.
26949 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
26951 tree list
= NULL_TREE
;
26953 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26955 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
26959 property
= cp_parser_identifier (parser
);
26960 if (property
== error_mark_node
)
26962 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26965 list
= chainon (list
, build_tree_list (NULL
, property
));
26966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26967 cp_lexer_consume_token (parser
->lexer
);
26971 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26972 objc_add_dynamic_declaration (loc
, list
);
26976 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26978 /* Returns name of the next clause.
26979 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26980 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26981 returned and the token is consumed. */
26983 static pragma_omp_clause
26984 cp_parser_omp_clause_name (cp_parser
*parser
)
26986 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
26988 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
26989 result
= PRAGMA_OMP_CLAUSE_IF
;
26990 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
26991 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
26992 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
26993 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
26994 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
26995 result
= PRAGMA_OMP_CLAUSE_FOR
;
26996 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26998 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26999 const char *p
= IDENTIFIER_POINTER (id
);
27004 if (!strcmp ("aligned", p
))
27005 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
27008 if (!strcmp ("collapse", p
))
27009 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
27010 else if (!strcmp ("copyin", p
))
27011 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
27012 else if (!strcmp ("copyprivate", p
))
27013 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
27016 if (!strcmp ("depend", p
))
27017 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
27018 else if (!strcmp ("device", p
))
27019 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
27020 else if (!strcmp ("dist_schedule", p
))
27021 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
27024 if (!strcmp ("final", p
))
27025 result
= PRAGMA_OMP_CLAUSE_FINAL
;
27026 else if (!strcmp ("firstprivate", p
))
27027 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
27028 else if (!strcmp ("from", p
))
27029 result
= PRAGMA_OMP_CLAUSE_FROM
;
27032 if (!strcmp ("inbranch", p
))
27033 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
27036 if (!strcmp ("lastprivate", p
))
27037 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
27038 else if (!strcmp ("linear", p
))
27039 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
27042 if (!strcmp ("map", p
))
27043 result
= PRAGMA_OMP_CLAUSE_MAP
;
27044 else if (!strcmp ("mergeable", p
))
27045 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
27046 else if (flag_cilkplus
&& !strcmp ("mask", p
))
27047 result
= PRAGMA_CILK_CLAUSE_MASK
;
27050 if (!strcmp ("notinbranch", p
))
27051 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
27052 else if (!strcmp ("nowait", p
))
27053 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
27054 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
27055 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
27056 else if (!strcmp ("num_teams", p
))
27057 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
27058 else if (!strcmp ("num_threads", p
))
27059 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
27062 if (!strcmp ("ordered", p
))
27063 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
27066 if (!strcmp ("parallel", p
))
27067 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
27068 else if (!strcmp ("proc_bind", p
))
27069 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
27072 if (!strcmp ("reduction", p
))
27073 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
27076 if (!strcmp ("safelen", p
))
27077 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
27078 else if (!strcmp ("schedule", p
))
27079 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
27080 else if (!strcmp ("sections", p
))
27081 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
27082 else if (!strcmp ("shared", p
))
27083 result
= PRAGMA_OMP_CLAUSE_SHARED
;
27084 else if (!strcmp ("simdlen", p
))
27085 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
27088 if (!strcmp ("taskgroup", p
))
27089 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
27090 else if (!strcmp ("thread_limit", p
))
27091 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
27092 else if (!strcmp ("to", p
))
27093 result
= PRAGMA_OMP_CLAUSE_TO
;
27096 if (!strcmp ("uniform", p
))
27097 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
27098 else if (!strcmp ("untied", p
))
27099 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
27102 if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
27103 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
27108 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
27109 cp_lexer_consume_token (parser
->lexer
);
27114 /* Validate that a clause of the given type does not already exist. */
27117 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
27118 const char *name
, location_t location
)
27122 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27123 if (OMP_CLAUSE_CODE (c
) == code
)
27125 error_at (location
, "too many %qs clauses", name
);
27133 variable-list , identifier
27135 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27136 colon). An opening parenthesis will have been consumed by the caller.
27138 If KIND is nonzero, create the appropriate node and install the decl
27139 in OMP_CLAUSE_DECL and add the node to the head of the list.
27141 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27142 return the list created.
27144 COLON can be NULL if only closing parenthesis should end the list,
27145 or pointer to bool which will receive false if the list is terminated
27146 by closing parenthesis or true if the list is terminated by colon. */
27149 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
27150 tree list
, bool *colon
)
27153 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27156 parser
->colon_corrects_to_scope_p
= false;
27163 token
= cp_lexer_peek_token (parser
->lexer
);
27164 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
27165 /*check_dependency_p=*/true,
27166 /*template_p=*/NULL
,
27167 /*declarator_p=*/false,
27168 /*optional_p=*/false);
27169 if (name
== error_mark_node
)
27172 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27173 if (decl
== error_mark_node
)
27174 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
27176 else if (kind
!= 0)
27180 case OMP_CLAUSE_MAP
:
27181 case OMP_CLAUSE_FROM
:
27182 case OMP_CLAUSE_TO
:
27183 case OMP_CLAUSE_DEPEND
:
27184 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27186 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
27188 parser
->colon_corrects_to_scope_p
= false;
27189 cp_lexer_consume_token (parser
->lexer
);
27190 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27191 low_bound
= cp_parser_expression (parser
, /*cast_p=*/false,
27194 parser
->colon_corrects_to_scope_p
27195 = saved_colon_corrects_to_scope_p
;
27196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
27197 length
= integer_one_node
;
27200 /* Look for `:'. */
27201 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27203 if (!cp_lexer_next_token_is (parser
->lexer
,
27205 length
= cp_parser_expression (parser
,
27209 /* Look for the closing `]'. */
27210 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
27213 decl
= tree_cons (low_bound
, length
, decl
);
27220 tree u
= build_omp_clause (token
->location
, kind
);
27221 OMP_CLAUSE_DECL (u
) = decl
;
27222 OMP_CLAUSE_CHAIN (u
) = list
;
27226 list
= tree_cons (decl
, NULL_TREE
, list
);
27229 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27231 cp_lexer_consume_token (parser
->lexer
);
27235 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27237 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27240 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27244 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27248 /* Try to resync to an unnested comma. Copied from
27249 cp_parser_parenthesized_expression_list. */
27252 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27253 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
27254 /*recovering=*/true,
27256 /*consume_paren=*/true);
27264 /* Similarly, but expect leading and trailing parenthesis. This is a very
27265 common case for omp clauses. */
27268 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
27270 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27271 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
27276 collapse ( constant-expression ) */
27279 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
27285 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27286 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27289 num
= cp_parser_constant_expression (parser
, false, NULL
);
27291 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27292 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27293 /*or_comma=*/false,
27294 /*consume_paren=*/true);
27296 if (num
== error_mark_node
)
27298 num
= fold_non_dependent_expr (num
);
27299 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
27300 || !tree_fits_shwi_p (num
)
27301 || (n
= tree_to_shwi (num
)) <= 0
27304 error_at (loc
, "collapse argument needs positive constant integer expression");
27308 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
27309 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
27310 OMP_CLAUSE_CHAIN (c
) = list
;
27311 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
27317 default ( shared | none ) */
27320 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
27322 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
27325 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27329 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27330 const char *p
= IDENTIFIER_POINTER (id
);
27335 if (strcmp ("none", p
) != 0)
27337 kind
= OMP_CLAUSE_DEFAULT_NONE
;
27341 if (strcmp ("shared", p
) != 0)
27343 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
27350 cp_lexer_consume_token (parser
->lexer
);
27355 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
27358 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27359 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27360 /*or_comma=*/false,
27361 /*consume_paren=*/true);
27363 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
27366 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
27367 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
27368 OMP_CLAUSE_CHAIN (c
) = list
;
27369 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
27375 final ( expression ) */
27378 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
27382 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27385 t
= cp_parser_condition (parser
);
27387 if (t
== error_mark_node
27388 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27389 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27390 /*or_comma=*/false,
27391 /*consume_paren=*/true);
27393 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
27395 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
27396 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
27397 OMP_CLAUSE_CHAIN (c
) = list
;
27403 if ( expression ) */
27406 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
27410 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27413 t
= cp_parser_condition (parser
);
27415 if (t
== error_mark_node
27416 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27417 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27418 /*or_comma=*/false,
27419 /*consume_paren=*/true);
27421 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
27423 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
27424 OMP_CLAUSE_IF_EXPR (c
) = t
;
27425 OMP_CLAUSE_CHAIN (c
) = list
;
27434 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
27435 tree list
, location_t location
)
27439 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
27442 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
27443 OMP_CLAUSE_CHAIN (c
) = list
;
27451 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
27452 tree list
, location_t location
)
27456 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
27458 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
27459 OMP_CLAUSE_CHAIN (c
) = list
;
27464 num_threads ( expression ) */
27467 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
27468 location_t location
)
27472 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27475 t
= cp_parser_expression (parser
, false, NULL
);
27477 if (t
== error_mark_node
27478 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27479 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27480 /*or_comma=*/false,
27481 /*consume_paren=*/true);
27483 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
27484 "num_threads", location
);
27486 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
27487 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
27488 OMP_CLAUSE_CHAIN (c
) = list
;
27497 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
27498 tree list
, location_t location
)
27502 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
27503 "ordered", location
);
27505 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
27506 OMP_CLAUSE_CHAIN (c
) = list
;
27511 reduction ( reduction-operator : variable-list )
27513 reduction-operator:
27514 One of: + * - & ^ | && ||
27518 reduction-operator:
27519 One of: + * - & ^ | && || min max
27523 reduction-operator:
27524 One of: + * - & ^ | && ||
27528 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
27530 enum tree_code code
= ERROR_MARK
;
27531 tree nlist
, c
, id
= NULL_TREE
;
27533 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27536 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27538 case CPP_PLUS
: code
= PLUS_EXPR
; break;
27539 case CPP_MULT
: code
= MULT_EXPR
; break;
27540 case CPP_MINUS
: code
= MINUS_EXPR
; break;
27541 case CPP_AND
: code
= BIT_AND_EXPR
; break;
27542 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
27543 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
27544 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
27545 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
27549 if (code
!= ERROR_MARK
)
27550 cp_lexer_consume_token (parser
->lexer
);
27553 bool saved_colon_corrects_to_scope_p
;
27554 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27555 parser
->colon_corrects_to_scope_p
= false;
27556 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
27557 /*check_dependency_p=*/true,
27558 /*template_p=*/NULL
,
27559 /*declarator_p=*/false,
27560 /*optional_p=*/false);
27561 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27562 if (identifier_p (id
))
27564 const char *p
= IDENTIFIER_POINTER (id
);
27566 if (strcmp (p
, "min") == 0)
27568 else if (strcmp (p
, "max") == 0)
27570 else if (id
== ansi_opname (PLUS_EXPR
))
27572 else if (id
== ansi_opname (MULT_EXPR
))
27574 else if (id
== ansi_opname (MINUS_EXPR
))
27576 else if (id
== ansi_opname (BIT_AND_EXPR
))
27577 code
= BIT_AND_EXPR
;
27578 else if (id
== ansi_opname (BIT_IOR_EXPR
))
27579 code
= BIT_IOR_EXPR
;
27580 else if (id
== ansi_opname (BIT_XOR_EXPR
))
27581 code
= BIT_XOR_EXPR
;
27582 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
27583 code
= TRUTH_ANDIF_EXPR
;
27584 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
27585 code
= TRUTH_ORIF_EXPR
;
27586 id
= omp_reduction_id (code
, id
, NULL_TREE
);
27587 tree scope
= parser
->scope
;
27589 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
27590 parser
->scope
= NULL_TREE
;
27591 parser
->qualifying_scope
= NULL_TREE
;
27592 parser
->object_scope
= NULL_TREE
;
27596 error ("invalid reduction-identifier");
27598 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27599 /*or_comma=*/false,
27600 /*consume_paren=*/true);
27605 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27608 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
27610 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27612 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
27613 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
27620 schedule ( schedule-kind )
27621 schedule ( schedule-kind , expression )
27624 static | dynamic | guided | runtime | auto */
27627 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
27631 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27634 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
27636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27638 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27639 const char *p
= IDENTIFIER_POINTER (id
);
27644 if (strcmp ("dynamic", p
) != 0)
27646 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
27650 if (strcmp ("guided", p
) != 0)
27652 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
27656 if (strcmp ("runtime", p
) != 0)
27658 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
27665 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
27666 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
27667 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
27668 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
27671 cp_lexer_consume_token (parser
->lexer
);
27673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27676 cp_lexer_consume_token (parser
->lexer
);
27678 token
= cp_lexer_peek_token (parser
->lexer
);
27679 t
= cp_parser_assignment_expression (parser
, false, NULL
);
27681 if (t
== error_mark_node
)
27683 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
27684 error_at (token
->location
, "schedule %<runtime%> does not take "
27685 "a %<chunk_size%> parameter");
27686 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
27687 error_at (token
->location
, "schedule %<auto%> does not take "
27688 "a %<chunk_size%> parameter");
27690 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
27692 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27695 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
27698 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
27699 OMP_CLAUSE_CHAIN (c
) = list
;
27703 cp_parser_error (parser
, "invalid schedule kind");
27705 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27706 /*or_comma=*/false,
27707 /*consume_paren=*/true);
27715 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
27716 tree list
, location_t location
)
27720 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
27722 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
27723 OMP_CLAUSE_CHAIN (c
) = list
;
27732 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
27733 tree list
, location_t location
)
27735 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
27736 tree c
= build_omp_clause (location
, code
);
27737 OMP_CLAUSE_CHAIN (c
) = list
;
27748 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
27749 enum omp_clause_code code
,
27750 tree list
, location_t location
)
27752 tree c
= build_omp_clause (location
, code
);
27753 OMP_CLAUSE_CHAIN (c
) = list
;
27758 num_teams ( expression ) */
27761 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
27762 location_t location
)
27766 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27769 t
= cp_parser_expression (parser
, false, NULL
);
27771 if (t
== error_mark_node
27772 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27773 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27774 /*or_comma=*/false,
27775 /*consume_paren=*/true);
27777 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
27778 "num_teams", location
);
27780 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
27781 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
27782 OMP_CLAUSE_CHAIN (c
) = list
;
27788 thread_limit ( expression ) */
27791 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
27792 location_t location
)
27796 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27799 t
= cp_parser_expression (parser
, false, NULL
);
27801 if (t
== error_mark_node
27802 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27803 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27804 /*or_comma=*/false,
27805 /*consume_paren=*/true);
27807 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
27808 "thread_limit", location
);
27810 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
27811 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
27812 OMP_CLAUSE_CHAIN (c
) = list
;
27818 aligned ( variable-list )
27819 aligned ( variable-list : constant-expression ) */
27822 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
27824 tree nlist
, c
, alignment
= NULL_TREE
;
27827 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27830 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
27835 alignment
= cp_parser_constant_expression (parser
, false, NULL
);
27837 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27838 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27839 /*or_comma=*/false,
27840 /*consume_paren=*/true);
27842 if (alignment
== error_mark_node
)
27843 alignment
= NULL_TREE
;
27846 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27847 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
27853 linear ( variable-list )
27854 linear ( variable-list : expression ) */
27857 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
27858 bool is_cilk_simd_fn
)
27860 tree nlist
, c
, step
= integer_one_node
;
27863 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27866 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
27871 step
= cp_parser_expression (parser
, false, NULL
);
27873 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
27875 sorry ("using parameters for %<linear%> step is not supported yet");
27876 step
= integer_one_node
;
27878 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27879 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27880 /*or_comma=*/false,
27881 /*consume_paren=*/true);
27883 if (step
== error_mark_node
)
27887 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27888 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
27894 safelen ( constant-expression ) */
27897 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
27898 location_t location
)
27902 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27905 t
= cp_parser_constant_expression (parser
, false, NULL
);
27907 if (t
== error_mark_node
27908 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27909 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27910 /*or_comma=*/false,
27911 /*consume_paren=*/true);
27913 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
27915 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
27916 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
27917 OMP_CLAUSE_CHAIN (c
) = list
;
27923 simdlen ( constant-expression ) */
27926 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
27927 location_t location
)
27931 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27934 t
= cp_parser_constant_expression (parser
, false, NULL
);
27936 if (t
== error_mark_node
27937 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27938 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27939 /*or_comma=*/false,
27940 /*consume_paren=*/true);
27942 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
27944 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
27945 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
27946 OMP_CLAUSE_CHAIN (c
) = list
;
27952 depend ( depend-kind : variable-list )
27955 in | out | inout */
27958 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
27961 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
27963 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27968 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27969 const char *p
= IDENTIFIER_POINTER (id
);
27971 if (strcmp ("in", p
) == 0)
27972 kind
= OMP_CLAUSE_DEPEND_IN
;
27973 else if (strcmp ("inout", p
) == 0)
27974 kind
= OMP_CLAUSE_DEPEND_INOUT
;
27975 else if (strcmp ("out", p
) == 0)
27976 kind
= OMP_CLAUSE_DEPEND_OUT
;
27983 cp_lexer_consume_token (parser
->lexer
);
27984 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27987 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
27990 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27991 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
27996 cp_parser_error (parser
, "invalid depend kind");
27998 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27999 /*or_comma=*/false,
28000 /*consume_paren=*/true);
28005 map ( map-kind : variable-list )
28006 map ( variable-list )
28009 alloc | to | from | tofrom */
28012 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
28015 enum omp_clause_map_kind kind
= OMP_CLAUSE_MAP_TOFROM
;
28017 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
28021 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
28023 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28024 const char *p
= IDENTIFIER_POINTER (id
);
28026 if (strcmp ("alloc", p
) == 0)
28027 kind
= OMP_CLAUSE_MAP_ALLOC
;
28028 else if (strcmp ("to", p
) == 0)
28029 kind
= OMP_CLAUSE_MAP_TO
;
28030 else if (strcmp ("from", p
) == 0)
28031 kind
= OMP_CLAUSE_MAP_FROM
;
28032 else if (strcmp ("tofrom", p
) == 0)
28033 kind
= OMP_CLAUSE_MAP_TOFROM
;
28036 cp_parser_error (parser
, "invalid map kind");
28037 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28038 /*or_comma=*/false,
28039 /*consume_paren=*/true);
28042 cp_lexer_consume_token (parser
->lexer
);
28043 cp_lexer_consume_token (parser
->lexer
);
28046 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
28049 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28050 OMP_CLAUSE_MAP_KIND (c
) = kind
;
28056 device ( expression ) */
28059 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
28060 location_t location
)
28064 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28067 t
= cp_parser_expression (parser
, false, NULL
);
28069 if (t
== error_mark_node
28070 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28071 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28072 /*or_comma=*/false,
28073 /*consume_paren=*/true);
28075 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
28076 "device", location
);
28078 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
28079 OMP_CLAUSE_DEVICE_ID (c
) = t
;
28080 OMP_CLAUSE_CHAIN (c
) = list
;
28086 dist_schedule ( static )
28087 dist_schedule ( static , expression ) */
28090 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
28091 location_t location
)
28095 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28098 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
28100 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28102 cp_lexer_consume_token (parser
->lexer
);
28104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28106 cp_lexer_consume_token (parser
->lexer
);
28108 t
= cp_parser_assignment_expression (parser
, false, NULL
);
28110 if (t
== error_mark_node
)
28112 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
28114 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28117 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28120 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
28122 OMP_CLAUSE_CHAIN (c
) = list
;
28126 cp_parser_error (parser
, "invalid dist_schedule kind");
28128 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28129 /*or_comma=*/false,
28130 /*consume_paren=*/true);
28135 proc_bind ( proc-bind-kind )
28138 master | close | spread */
28141 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
28142 location_t location
)
28145 enum omp_clause_proc_bind_kind kind
;
28147 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28150 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28152 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28153 const char *p
= IDENTIFIER_POINTER (id
);
28155 if (strcmp ("master", p
) == 0)
28156 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
28157 else if (strcmp ("close", p
) == 0)
28158 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
28159 else if (strcmp ("spread", p
) == 0)
28160 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
28167 cp_lexer_consume_token (parser
->lexer
);
28168 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28171 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
28172 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
28174 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
28175 OMP_CLAUSE_CHAIN (c
) = list
;
28179 cp_parser_error (parser
, "invalid depend kind");
28181 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28182 /*or_comma=*/false,
28183 /*consume_paren=*/true);
28187 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28188 is a bitmask in MASK. Return the list of clauses found; the result
28189 of clause default goes in *pdefault. */
28192 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
28193 const char *where
, cp_token
*pragma_tok
,
28194 bool finish_p
= true)
28196 tree clauses
= NULL
;
28198 cp_token
*token
= NULL
;
28199 bool cilk_simd_fn
= false;
28201 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
28203 pragma_omp_clause c_kind
;
28204 const char *c_name
;
28205 tree prev
= clauses
;
28207 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28208 cp_lexer_consume_token (parser
->lexer
);
28210 token
= cp_lexer_peek_token (parser
->lexer
);
28211 c_kind
= cp_parser_omp_clause_name (parser
);
28215 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
28216 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
28218 c_name
= "collapse";
28220 case PRAGMA_OMP_CLAUSE_COPYIN
:
28221 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
28224 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
28225 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
28227 c_name
= "copyprivate";
28229 case PRAGMA_OMP_CLAUSE_DEFAULT
:
28230 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
28232 c_name
= "default";
28234 case PRAGMA_OMP_CLAUSE_FINAL
:
28235 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
28238 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
28239 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
28241 c_name
= "firstprivate";
28243 case PRAGMA_OMP_CLAUSE_IF
:
28244 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
28247 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
28248 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
28250 c_name
= "lastprivate";
28252 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
28253 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
28255 c_name
= "mergeable";
28257 case PRAGMA_OMP_CLAUSE_NOWAIT
:
28258 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
28261 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
28262 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
28264 c_name
= "num_threads";
28266 case PRAGMA_OMP_CLAUSE_ORDERED
:
28267 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
28269 c_name
= "ordered";
28271 case PRAGMA_OMP_CLAUSE_PRIVATE
:
28272 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
28274 c_name
= "private";
28276 case PRAGMA_OMP_CLAUSE_REDUCTION
:
28277 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
28278 c_name
= "reduction";
28280 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
28281 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
28283 c_name
= "schedule";
28285 case PRAGMA_OMP_CLAUSE_SHARED
:
28286 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
28290 case PRAGMA_OMP_CLAUSE_UNTIED
:
28291 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
28295 case PRAGMA_OMP_CLAUSE_INBRANCH
:
28296 case PRAGMA_CILK_CLAUSE_MASK
:
28297 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
28298 clauses
, token
->location
);
28299 c_name
= "inbranch";
28301 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
28302 case PRAGMA_CILK_CLAUSE_NOMASK
:
28303 clauses
= cp_parser_omp_clause_branch (parser
,
28304 OMP_CLAUSE_NOTINBRANCH
,
28305 clauses
, token
->location
);
28306 c_name
= "notinbranch";
28308 case PRAGMA_OMP_CLAUSE_PARALLEL
:
28309 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
28310 clauses
, token
->location
);
28311 c_name
= "parallel";
28315 error_at (token
->location
, "%qs must be the first clause of %qs",
28320 case PRAGMA_OMP_CLAUSE_FOR
:
28321 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
28322 clauses
, token
->location
);
28325 goto clause_not_first
;
28327 case PRAGMA_OMP_CLAUSE_SECTIONS
:
28328 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
28329 clauses
, token
->location
);
28330 c_name
= "sections";
28332 goto clause_not_first
;
28334 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
28335 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
28336 clauses
, token
->location
);
28337 c_name
= "taskgroup";
28339 goto clause_not_first
;
28341 case PRAGMA_OMP_CLAUSE_TO
:
28342 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
28346 case PRAGMA_OMP_CLAUSE_FROM
:
28347 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
28351 case PRAGMA_OMP_CLAUSE_UNIFORM
:
28352 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
28354 c_name
= "uniform";
28356 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
28357 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
28359 c_name
= "num_teams";
28361 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
28362 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
28364 c_name
= "thread_limit";
28366 case PRAGMA_OMP_CLAUSE_ALIGNED
:
28367 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
28368 c_name
= "aligned";
28370 case PRAGMA_OMP_CLAUSE_LINEAR
:
28371 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
28372 cilk_simd_fn
= true;
28373 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, cilk_simd_fn
);
28376 case PRAGMA_OMP_CLAUSE_DEPEND
:
28377 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
28380 case PRAGMA_OMP_CLAUSE_MAP
:
28381 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
28384 case PRAGMA_OMP_CLAUSE_DEVICE
:
28385 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
28389 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
28390 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
28392 c_name
= "dist_schedule";
28394 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
28395 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
28397 c_name
= "proc_bind";
28399 case PRAGMA_OMP_CLAUSE_SAFELEN
:
28400 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
28402 c_name
= "safelen";
28404 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
28405 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
28407 c_name
= "simdlen";
28409 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
28410 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
28411 c_name
= "simdlen";
28414 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
28420 if (((mask
>> c_kind
) & 1) == 0)
28422 /* Remove the invalid clause(s) from the list to avoid
28423 confusing the rest of the compiler. */
28425 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
28429 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28430 no reason to skip to the end. */
28431 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
28432 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
28434 return finish_omp_clauses (clauses
);
28442 In practice, we're also interested in adding the statement to an
28443 outer node. So it is convenient if we work around the fact that
28444 cp_parser_statement calls add_stmt. */
28447 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
28449 unsigned save
= parser
->in_statement
;
28451 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28452 This preserves the "not within loop or switch" style error messages
28453 for nonsense cases like
28459 if (parser
->in_statement
)
28460 parser
->in_statement
= IN_OMP_BLOCK
;
28466 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
28468 parser
->in_statement
= save
;
28472 cp_parser_omp_structured_block (cp_parser
*parser
)
28474 tree stmt
= begin_omp_structured_block ();
28475 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
28477 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
28479 cp_parser_end_omp_structured_block (parser
, save
);
28480 return finish_omp_structured_block (stmt
);
28484 # pragma omp atomic new-line
28488 x binop= expr | x++ | ++x | x-- | --x
28490 +, *, -, /, &, ^, |, <<, >>
28492 where x is an lvalue expression with scalar type.
28495 # pragma omp atomic new-line
28498 # pragma omp atomic read new-line
28501 # pragma omp atomic write new-line
28504 # pragma omp atomic update new-line
28507 # pragma omp atomic capture new-line
28510 # pragma omp atomic capture new-line
28518 expression-stmt | x = x binop expr
28520 v = expression-stmt
28522 { v = x; update-stmt; } | { update-stmt; v = x; }
28526 expression-stmt | x = x binop expr | x = expr binop x
28530 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28532 where x and v are lvalue expressions with scalar type. */
28535 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
28537 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
28538 tree rhs1
= NULL_TREE
, orig_lhs
;
28539 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
28540 bool structured_block
= false;
28541 bool seq_cst
= false;
28543 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28545 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28546 const char *p
= IDENTIFIER_POINTER (id
);
28548 if (!strcmp (p
, "seq_cst"))
28551 cp_lexer_consume_token (parser
->lexer
);
28552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
28553 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
28554 cp_lexer_consume_token (parser
->lexer
);
28557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28559 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28560 const char *p
= IDENTIFIER_POINTER (id
);
28562 if (!strcmp (p
, "read"))
28563 code
= OMP_ATOMIC_READ
;
28564 else if (!strcmp (p
, "write"))
28566 else if (!strcmp (p
, "update"))
28568 else if (!strcmp (p
, "capture"))
28569 code
= OMP_ATOMIC_CAPTURE_NEW
;
28573 cp_lexer_consume_token (parser
->lexer
);
28577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
28578 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
28579 cp_lexer_consume_token (parser
->lexer
);
28581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28583 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28584 const char *p
= IDENTIFIER_POINTER (id
);
28586 if (!strcmp (p
, "seq_cst"))
28589 cp_lexer_consume_token (parser
->lexer
);
28593 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28597 case OMP_ATOMIC_READ
:
28598 case NOP_EXPR
: /* atomic write */
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
))
28605 if (code
== NOP_EXPR
)
28606 lhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28608 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28609 /*cast_p=*/false, NULL
);
28610 if (lhs
== error_mark_node
)
28612 if (code
== NOP_EXPR
)
28614 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28622 case OMP_ATOMIC_CAPTURE_NEW
:
28623 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28625 cp_lexer_consume_token (parser
->lexer
);
28626 structured_block
= true;
28630 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28631 /*cast_p=*/false, NULL
);
28632 if (v
== error_mark_node
)
28634 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28642 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28643 /*cast_p=*/false, NULL
);
28645 switch (TREE_CODE (lhs
))
28650 case POSTINCREMENT_EXPR
:
28651 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
28652 code
= OMP_ATOMIC_CAPTURE_OLD
;
28654 case PREINCREMENT_EXPR
:
28655 lhs
= TREE_OPERAND (lhs
, 0);
28656 opcode
= PLUS_EXPR
;
28657 rhs
= integer_one_node
;
28660 case POSTDECREMENT_EXPR
:
28661 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
28662 code
= OMP_ATOMIC_CAPTURE_OLD
;
28664 case PREDECREMENT_EXPR
:
28665 lhs
= TREE_OPERAND (lhs
, 0);
28666 opcode
= MINUS_EXPR
;
28667 rhs
= integer_one_node
;
28670 case COMPOUND_EXPR
:
28671 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
28672 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
28673 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
28674 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
28675 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28676 (TREE_OPERAND (lhs
, 1), 0), 0)))
28678 /* Undo effects of boolean_increment for post {in,de}crement. */
28679 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
28682 if (TREE_CODE (lhs
) == MODIFY_EXPR
28683 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
28685 /* Undo effects of boolean_increment. */
28686 if (integer_onep (TREE_OPERAND (lhs
, 1)))
28688 /* This is pre or post increment. */
28689 rhs
= TREE_OPERAND (lhs
, 1);
28690 lhs
= TREE_OPERAND (lhs
, 0);
28692 if (code
== OMP_ATOMIC_CAPTURE_NEW
28693 && !structured_block
28694 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
28695 code
= OMP_ATOMIC_CAPTURE_OLD
;
28701 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28704 opcode
= MULT_EXPR
;
28707 opcode
= TRUNC_DIV_EXPR
;
28710 opcode
= PLUS_EXPR
;
28713 opcode
= MINUS_EXPR
;
28715 case CPP_LSHIFT_EQ
:
28716 opcode
= LSHIFT_EXPR
;
28718 case CPP_RSHIFT_EQ
:
28719 opcode
= RSHIFT_EXPR
;
28722 opcode
= BIT_AND_EXPR
;
28725 opcode
= BIT_IOR_EXPR
;
28728 opcode
= BIT_XOR_EXPR
;
28731 enum cp_parser_prec oprec
;
28733 cp_lexer_consume_token (parser
->lexer
);
28734 cp_parser_parse_tentatively (parser
);
28735 rhs1
= cp_parser_simple_cast_expression (parser
);
28736 if (rhs1
== error_mark_node
)
28738 cp_parser_abort_tentative_parse (parser
);
28739 cp_parser_simple_cast_expression (parser
);
28742 token
= cp_lexer_peek_token (parser
->lexer
);
28743 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
28745 cp_parser_abort_tentative_parse (parser
);
28746 cp_parser_parse_tentatively (parser
);
28747 rhs
= cp_parser_binary_expression (parser
, false, true,
28748 PREC_NOT_OPERATOR
, NULL
);
28749 if (rhs
== error_mark_node
)
28751 cp_parser_abort_tentative_parse (parser
);
28752 cp_parser_binary_expression (parser
, false, true,
28753 PREC_NOT_OPERATOR
, NULL
);
28756 switch (TREE_CODE (rhs
))
28759 case TRUNC_DIV_EXPR
:
28767 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
28769 if (cp_parser_parse_definitely (parser
))
28771 opcode
= TREE_CODE (rhs
);
28772 rhs1
= TREE_OPERAND (rhs
, 0);
28773 rhs
= TREE_OPERAND (rhs
, 1);
28783 cp_parser_abort_tentative_parse (parser
);
28784 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
28786 rhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28787 if (rhs
== error_mark_node
)
28793 cp_parser_error (parser
,
28794 "invalid form of %<#pragma omp atomic%>");
28797 if (!cp_parser_parse_definitely (parser
))
28799 switch (token
->type
)
28801 case CPP_SEMICOLON
:
28802 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
28804 code
= OMP_ATOMIC_CAPTURE_OLD
;
28809 cp_lexer_consume_token (parser
->lexer
);
28812 else if (structured_block
)
28819 cp_parser_error (parser
,
28820 "invalid form of %<#pragma omp atomic%>");
28823 opcode
= MULT_EXPR
;
28826 opcode
= TRUNC_DIV_EXPR
;
28829 opcode
= PLUS_EXPR
;
28832 opcode
= MINUS_EXPR
;
28835 opcode
= LSHIFT_EXPR
;
28838 opcode
= RSHIFT_EXPR
;
28841 opcode
= BIT_AND_EXPR
;
28844 opcode
= BIT_IOR_EXPR
;
28847 opcode
= BIT_XOR_EXPR
;
28850 cp_parser_error (parser
,
28851 "invalid operator for %<#pragma omp atomic%>");
28854 oprec
= TOKEN_PRECEDENCE (token
);
28855 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
28856 if (commutative_tree_code (opcode
))
28857 oprec
= (enum cp_parser_prec
) (oprec
- 1);
28858 cp_lexer_consume_token (parser
->lexer
);
28859 rhs
= cp_parser_binary_expression (parser
, false, false,
28861 if (rhs
== error_mark_node
)
28866 cp_parser_error (parser
,
28867 "invalid operator for %<#pragma omp atomic%>");
28870 cp_lexer_consume_token (parser
->lexer
);
28872 rhs
= cp_parser_expression (parser
, false, NULL
);
28873 if (rhs
== error_mark_node
)
28878 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
28880 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
28882 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28883 /*cast_p=*/false, NULL
);
28884 if (v
== error_mark_node
)
28886 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28888 lhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
28889 /*cast_p=*/false, NULL
);
28890 if (lhs1
== error_mark_node
)
28893 if (structured_block
)
28895 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28896 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
28899 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
28900 if (!structured_block
)
28901 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28905 cp_parser_skip_to_end_of_block_or_statement (parser
);
28906 if (structured_block
)
28908 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28909 cp_lexer_consume_token (parser
->lexer
);
28910 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
28912 cp_parser_skip_to_end_of_block_or_statement (parser
);
28913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28914 cp_lexer_consume_token (parser
->lexer
);
28921 # pragma omp barrier new-line */
28924 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
28926 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28927 finish_omp_barrier ();
28931 # pragma omp critical [(name)] new-line
28932 structured-block */
28935 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
28937 tree stmt
, name
= NULL
;
28939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28941 cp_lexer_consume_token (parser
->lexer
);
28943 name
= cp_parser_identifier (parser
);
28945 if (name
== error_mark_node
28946 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28947 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28948 /*or_comma=*/false,
28949 /*consume_paren=*/true);
28950 if (name
== error_mark_node
)
28953 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28955 stmt
= cp_parser_omp_structured_block (parser
);
28956 return c_finish_omp_critical (input_location
, stmt
, name
);
28960 # pragma omp flush flush-vars[opt] new-line
28963 ( variable-list ) */
28966 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
28968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28969 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
28970 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28972 finish_omp_flush ();
28975 /* Helper function, to parse omp for increment expression. */
28978 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
28980 tree cond
= cp_parser_binary_expression (parser
, false, true,
28981 PREC_NOT_OPERATOR
, NULL
);
28982 if (cond
== error_mark_node
28983 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28985 cp_parser_skip_to_end_of_statement (parser
);
28986 return error_mark_node
;
28989 switch (TREE_CODE (cond
))
28997 if (code
== CILK_SIMD
)
28999 /* Fall through: OpenMP disallows NE_EXPR. */
29001 return error_mark_node
;
29004 /* If decl is an iterator, preserve LHS and RHS of the relational
29005 expr until finish_omp_for. */
29007 && (type_dependent_expression_p (decl
)
29008 || CLASS_TYPE_P (TREE_TYPE (decl
))))
29011 return build_x_binary_op (input_location
, TREE_CODE (cond
),
29012 TREE_OPERAND (cond
, 0), ERROR_MARK
,
29013 TREE_OPERAND (cond
, 1), ERROR_MARK
,
29014 /*overload=*/NULL
, tf_warning_or_error
);
29017 /* Helper function, to parse omp for increment expression. */
29020 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
29022 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29028 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
29030 op
= (token
->type
== CPP_PLUS_PLUS
29031 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
29032 cp_lexer_consume_token (parser
->lexer
);
29033 lhs
= cp_parser_simple_cast_expression (parser
);
29035 return error_mark_node
;
29036 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
29039 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
29041 return error_mark_node
;
29043 token
= cp_lexer_peek_token (parser
->lexer
);
29044 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
29046 op
= (token
->type
== CPP_PLUS_PLUS
29047 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
29048 cp_lexer_consume_token (parser
->lexer
);
29049 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
29052 op
= cp_parser_assignment_operator_opt (parser
);
29053 if (op
== ERROR_MARK
)
29054 return error_mark_node
;
29056 if (op
!= NOP_EXPR
)
29058 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
29059 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
29060 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
29063 lhs
= cp_parser_binary_expression (parser
, false, false,
29064 PREC_ADDITIVE_EXPRESSION
, NULL
);
29065 token
= cp_lexer_peek_token (parser
->lexer
);
29066 decl_first
= lhs
== decl
;
29069 if (token
->type
!= CPP_PLUS
29070 && token
->type
!= CPP_MINUS
)
29071 return error_mark_node
;
29075 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
29076 cp_lexer_consume_token (parser
->lexer
);
29077 rhs
= cp_parser_binary_expression (parser
, false, false,
29078 PREC_ADDITIVE_EXPRESSION
, NULL
);
29079 token
= cp_lexer_peek_token (parser
->lexer
);
29080 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
29082 if (lhs
== NULL_TREE
)
29084 if (op
== PLUS_EXPR
)
29087 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
29088 tf_warning_or_error
);
29091 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
29092 ERROR_MARK
, NULL
, tf_warning_or_error
);
29095 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
29099 if (rhs
!= decl
|| op
== MINUS_EXPR
)
29100 return error_mark_node
;
29101 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
29104 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
29106 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
29109 /* Parse the initialization statement of either an OpenMP for loop or
29110 a Cilk Plus for loop.
29112 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29115 Return true if the resulting construct should have an
29116 OMP_CLAUSE_PRIVATE added to it. */
29119 cp_parser_omp_for_loop_init (cp_parser
*parser
,
29120 bool parsing_openmp
,
29121 tree
&this_pre_body
,
29122 vec
<tree
, va_gc
> *for_block
,
29127 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29130 bool add_private_clause
= false;
29132 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29136 integer-type var = lb
29137 random-access-iterator-type var = lb
29138 pointer-type var = lb
29140 cp_decl_specifier_seq type_specifiers
;
29142 /* First, try to parse as an initialized declaration. See
29143 cp_parser_condition, from whence the bulk of this is copied. */
29145 cp_parser_parse_tentatively (parser
);
29146 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
29147 /*is_trailing_return=*/false,
29149 if (cp_parser_parse_definitely (parser
))
29151 /* If parsing a type specifier seq succeeded, then this
29152 MUST be a initialized declaration. */
29153 tree asm_specification
, attributes
;
29154 cp_declarator
*declarator
;
29156 declarator
= cp_parser_declarator (parser
,
29157 CP_PARSER_DECLARATOR_NAMED
,
29158 /*ctor_dtor_or_conv_p=*/NULL
,
29159 /*parenthesized_p=*/NULL
,
29160 /*member_p=*/false);
29161 attributes
= cp_parser_attributes_opt (parser
);
29162 asm_specification
= cp_parser_asm_specification_opt (parser
);
29164 if (declarator
== cp_error_declarator
)
29165 cp_parser_skip_to_end_of_statement (parser
);
29169 tree pushed_scope
, auto_node
;
29171 decl
= start_decl (declarator
, &type_specifiers
,
29172 SD_INITIALIZED
, attributes
,
29173 /*prefix_attributes=*/NULL_TREE
,
29176 auto_node
= type_uses_auto (TREE_TYPE (decl
));
29177 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
29179 if (cp_lexer_next_token_is (parser
->lexer
,
29182 if (parsing_openmp
)
29183 error ("parenthesized initialization is not allowed in "
29184 "OpenMP %<for%> loop");
29186 error ("parenthesized initialization is "
29187 "not allowed in for-loop");
29190 /* Trigger an error. */
29191 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
29193 init
= error_mark_node
;
29194 cp_parser_skip_to_end_of_statement (parser
);
29196 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
29197 || type_dependent_expression_p (decl
)
29200 bool is_direct_init
, is_non_constant_init
;
29202 init
= cp_parser_initializer (parser
,
29204 &is_non_constant_init
);
29209 = do_auto_deduction (TREE_TYPE (decl
), init
,
29212 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
29213 && !type_dependent_expression_p (decl
))
29217 cp_finish_decl (decl
, init
, !is_non_constant_init
,
29219 LOOKUP_ONLYCONVERTING
);
29220 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
29222 vec_safe_push (for_block
, this_pre_body
);
29226 init
= pop_stmt_list (this_pre_body
);
29227 this_pre_body
= NULL_TREE
;
29232 cp_lexer_consume_token (parser
->lexer
);
29233 init
= cp_parser_assignment_expression (parser
, false, NULL
);
29236 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
29237 init
= error_mark_node
;
29239 cp_finish_decl (decl
, NULL_TREE
,
29240 /*init_const_expr_p=*/false,
29242 LOOKUP_ONLYCONVERTING
);
29246 pop_scope (pushed_scope
);
29252 /* If parsing a type specifier sequence failed, then
29253 this MUST be a simple expression. */
29254 cp_parser_parse_tentatively (parser
);
29255 decl
= cp_parser_primary_expression (parser
, false, false,
29257 if (!cp_parser_error_occurred (parser
)
29260 && CLASS_TYPE_P (TREE_TYPE (decl
)))
29264 cp_parser_parse_definitely (parser
);
29265 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
29266 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
29267 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
29270 tf_warning_or_error
));
29271 add_private_clause
= true;
29276 cp_parser_abort_tentative_parse (parser
);
29277 init
= cp_parser_expression (parser
, false, NULL
);
29280 if (TREE_CODE (init
) == MODIFY_EXPR
29281 || TREE_CODE (init
) == MODOP_EXPR
)
29282 real_decl
= TREE_OPERAND (init
, 0);
29286 return add_private_clause
;
29289 /* Parse the restricted form of the for statement allowed by OpenMP. */
29292 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
29295 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
29296 tree real_decl
, initv
, condv
, incrv
, declv
;
29297 tree this_pre_body
, cl
;
29298 location_t loc_first
;
29299 bool collapse_err
= false;
29300 int i
, collapse
= 1, nbraces
= 0;
29301 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
29303 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
29304 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
29305 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
29307 gcc_assert (collapse
>= 1);
29309 declv
= make_tree_vec (collapse
);
29310 initv
= make_tree_vec (collapse
);
29311 condv
= make_tree_vec (collapse
);
29312 incrv
= make_tree_vec (collapse
);
29314 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
29316 for (i
= 0; i
< collapse
; i
++)
29318 int bracecount
= 0;
29319 bool add_private_clause
= false;
29322 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29324 cp_parser_error (parser
, "for statement expected");
29327 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
29329 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29332 init
= decl
= real_decl
= NULL
;
29333 this_pre_body
= push_stmt_list ();
29336 |= cp_parser_omp_for_loop_init (parser
,
29337 /*parsing_openmp=*/code
!= CILK_SIMD
,
29338 this_pre_body
, for_block
,
29339 init
, decl
, real_decl
);
29341 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29344 this_pre_body
= pop_stmt_list (this_pre_body
);
29348 pre_body
= push_stmt_list ();
29350 add_stmt (this_pre_body
);
29351 pre_body
= pop_stmt_list (pre_body
);
29354 pre_body
= this_pre_body
;
29359 if (cclauses
!= NULL
29360 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
29361 && real_decl
!= NULL_TREE
)
29364 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
29365 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
29366 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29368 error_at (loc
, "iteration variable %qD"
29369 " should not be firstprivate", real_decl
);
29370 *c
= OMP_CLAUSE_CHAIN (*c
);
29372 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
29373 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29375 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29376 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29377 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
29378 OMP_CLAUSE_DECL (l
) = real_decl
;
29379 OMP_CLAUSE_CHAIN (l
) = clauses
;
29380 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
29382 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
29383 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
29384 add_private_clause
= false;
29388 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
29389 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29390 add_private_clause
= false;
29391 c
= &OMP_CLAUSE_CHAIN (*c
);
29395 if (add_private_clause
)
29398 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
29400 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
29401 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
29402 && OMP_CLAUSE_DECL (c
) == decl
)
29404 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
29405 && OMP_CLAUSE_DECL (c
) == decl
)
29406 error_at (loc
, "iteration variable %qD "
29407 "should not be firstprivate",
29409 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
29410 && OMP_CLAUSE_DECL (c
) == decl
)
29411 error_at (loc
, "iteration variable %qD should not be reduction",
29416 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
29417 OMP_CLAUSE_DECL (c
) = decl
;
29418 c
= finish_omp_clauses (c
);
29421 OMP_CLAUSE_CHAIN (c
) = clauses
;
29428 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29429 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
29430 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29433 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29435 /* If decl is an iterator, preserve the operator on decl
29436 until finish_omp_for. */
29438 && ((processing_template_decl
29439 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
29440 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
29441 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
29443 incr
= cp_parser_expression (parser
, false, NULL
);
29444 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
29445 SET_EXPR_LOCATION (incr
, input_location
);
29448 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29449 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29450 /*or_comma=*/false,
29451 /*consume_paren=*/true);
29453 TREE_VEC_ELT (declv
, i
) = decl
;
29454 TREE_VEC_ELT (initv
, i
) = init
;
29455 TREE_VEC_ELT (condv
, i
) = cond
;
29456 TREE_VEC_ELT (incrv
, i
) = incr
;
29458 if (i
== collapse
- 1)
29461 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29462 in between the collapsed for loops to be still considered perfectly
29463 nested. Hopefully the final version clarifies this.
29464 For now handle (multiple) {'s and empty statements. */
29465 cp_parser_parse_tentatively (parser
);
29468 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29470 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29472 cp_lexer_consume_token (parser
->lexer
);
29475 else if (bracecount
29476 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29477 cp_lexer_consume_token (parser
->lexer
);
29480 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29481 error_at (loc
, "not enough collapsed for loops");
29482 collapse_err
= true;
29483 cp_parser_abort_tentative_parse (parser
);
29492 cp_parser_parse_definitely (parser
);
29493 nbraces
+= bracecount
;
29497 /* Note that we saved the original contents of this flag when we entered
29498 the structured block, and so we don't need to re-save it here. */
29499 if (code
== CILK_SIMD
)
29500 parser
->in_statement
= IN_CILK_SIMD_FOR
;
29502 parser
->in_statement
= IN_OMP_FOR
;
29504 /* Note that the grammar doesn't call for a structured block here,
29505 though the loop as a whole is a structured block. */
29506 body
= push_stmt_list ();
29507 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29508 body
= pop_stmt_list (body
);
29510 if (declv
== NULL_TREE
)
29513 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
29514 pre_body
, clauses
);
29518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29520 cp_lexer_consume_token (parser
->lexer
);
29523 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29524 cp_lexer_consume_token (parser
->lexer
);
29529 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29530 "collapsed loops not perfectly nested");
29532 collapse_err
= true;
29533 cp_parser_statement_seq_opt (parser
, NULL
);
29534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
29539 while (!for_block
->is_empty ())
29540 add_stmt (pop_stmt_list (for_block
->pop ()));
29541 release_tree_vector (for_block
);
29546 /* Helper function for OpenMP parsing, split clauses and call
29547 finish_omp_clauses on each of the set of clauses afterwards. */
29550 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
29551 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
29554 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
29555 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
29557 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
29561 #pragma omp simd simd-clause[optseq] new-line
29564 #define OMP_SIMD_CLAUSE_MASK \
29565 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29574 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
29575 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29577 tree clauses
, sb
, ret
;
29579 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29581 strcat (p_name
, " simd");
29582 mask
|= OMP_SIMD_CLAUSE_MASK
;
29583 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
29585 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29589 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
29590 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
29593 sb
= begin_omp_structured_block ();
29594 save
= cp_parser_begin_omp_structured_block (parser
);
29596 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
29598 cp_parser_end_omp_structured_block (parser
, save
);
29599 add_stmt (finish_omp_structured_block (sb
));
29605 #pragma omp for for-clause[optseq] new-line
29609 #pragma omp for simd for-simd-clause[optseq] new-line
29612 #define OMP_FOR_CLAUSE_MASK \
29613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29623 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
29624 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29626 tree clauses
, sb
, ret
;
29628 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29630 strcat (p_name
, " for");
29631 mask
|= OMP_FOR_CLAUSE_MASK
;
29633 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
29635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29637 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29638 const char *p
= IDENTIFIER_POINTER (id
);
29640 if (strcmp (p
, "simd") == 0)
29642 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29643 if (cclauses
== NULL
)
29644 cclauses
= cclauses_buf
;
29646 cp_lexer_consume_token (parser
->lexer
);
29647 if (!flag_openmp
) /* flag_openmp_simd */
29648 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29650 sb
= begin_omp_structured_block ();
29651 save
= cp_parser_begin_omp_structured_block (parser
);
29652 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
29654 cp_parser_end_omp_structured_block (parser
, save
);
29655 tree body
= finish_omp_structured_block (sb
);
29658 ret
= make_node (OMP_FOR
);
29659 TREE_TYPE (ret
) = void_type_node
;
29660 OMP_FOR_BODY (ret
) = body
;
29661 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29662 SET_EXPR_LOCATION (ret
, loc
);
29667 if (!flag_openmp
) /* flag_openmp_simd */
29669 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29673 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29677 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
29678 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29681 sb
= begin_omp_structured_block ();
29682 save
= cp_parser_begin_omp_structured_block (parser
);
29684 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
29686 cp_parser_end_omp_structured_block (parser
, save
);
29687 add_stmt (finish_omp_structured_block (sb
));
29693 # pragma omp master new-line
29694 structured-block */
29697 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
29699 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29700 return c_finish_omp_master (input_location
,
29701 cp_parser_omp_structured_block (parser
));
29705 # pragma omp ordered new-line
29706 structured-block */
29709 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
29711 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29712 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29713 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
29719 { section-sequence }
29722 section-directive[opt] structured-block
29723 section-sequence section-directive structured-block */
29726 cp_parser_omp_sections_scope (cp_parser
*parser
)
29728 tree stmt
, substmt
;
29729 bool error_suppress
= false;
29732 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
29735 stmt
= push_stmt_list ();
29737 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
29739 substmt
= cp_parser_omp_structured_block (parser
);
29740 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
29741 add_stmt (substmt
);
29746 tok
= cp_lexer_peek_token (parser
->lexer
);
29747 if (tok
->type
== CPP_CLOSE_BRACE
)
29749 if (tok
->type
== CPP_EOF
)
29752 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
29754 cp_lexer_consume_token (parser
->lexer
);
29755 cp_parser_require_pragma_eol (parser
, tok
);
29756 error_suppress
= false;
29758 else if (!error_suppress
)
29760 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
29761 error_suppress
= true;
29764 substmt
= cp_parser_omp_structured_block (parser
);
29765 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
29766 add_stmt (substmt
);
29768 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29770 substmt
= pop_stmt_list (stmt
);
29772 stmt
= make_node (OMP_SECTIONS
);
29773 TREE_TYPE (stmt
) = void_type_node
;
29774 OMP_SECTIONS_BODY (stmt
) = substmt
;
29781 # pragma omp sections sections-clause[optseq] newline
29784 #define OMP_SECTIONS_CLAUSE_MASK \
29785 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29792 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
29793 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29796 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29798 strcat (p_name
, " sections");
29799 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
29801 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
29803 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
29807 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
29808 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
29811 ret
= cp_parser_omp_sections_scope (parser
);
29813 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
29819 # pragma omp parallel parallel-clause[optseq] new-line
29821 # pragma omp parallel for parallel-for-clause[optseq] new-line
29823 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29827 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29828 structured-block */
29830 #define OMP_PARALLEL_CLAUSE_MASK \
29831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29842 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
29843 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
29845 tree stmt
, clauses
, block
;
29847 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29849 strcat (p_name
, " parallel");
29850 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
29852 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29854 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29855 if (cclauses
== NULL
)
29856 cclauses
= cclauses_buf
;
29858 cp_lexer_consume_token (parser
->lexer
);
29859 if (!flag_openmp
) /* flag_openmp_simd */
29860 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29861 block
= begin_omp_parallel ();
29862 save
= cp_parser_begin_omp_structured_block (parser
);
29863 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29864 cp_parser_end_omp_structured_block (parser
, save
);
29865 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
29867 if (ret
== NULL_TREE
)
29869 OMP_PARALLEL_COMBINED (stmt
) = 1;
29874 error_at (loc
, "expected %<for%> after %qs", p_name
);
29875 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29878 else if (!flag_openmp
) /* flag_openmp_simd */
29880 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29883 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29885 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29886 const char *p
= IDENTIFIER_POINTER (id
);
29887 if (strcmp (p
, "sections") == 0)
29889 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
29890 cclauses
= cclauses_buf
;
29892 cp_lexer_consume_token (parser
->lexer
);
29893 block
= begin_omp_parallel ();
29894 save
= cp_parser_begin_omp_structured_block (parser
);
29895 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
29896 cp_parser_end_omp_structured_block (parser
, save
);
29897 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
29899 OMP_PARALLEL_COMBINED (stmt
) = 1;
29904 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
29906 block
= begin_omp_parallel ();
29907 save
= cp_parser_begin_omp_structured_block (parser
);
29908 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29909 cp_parser_end_omp_structured_block (parser
, save
);
29910 stmt
= finish_omp_parallel (clauses
, block
);
29915 # pragma omp single single-clause[optseq] new-line
29916 structured-block */
29918 #define OMP_SINGLE_CLAUSE_MASK \
29919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29925 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
29927 tree stmt
= make_node (OMP_SINGLE
);
29928 TREE_TYPE (stmt
) = void_type_node
;
29930 OMP_SINGLE_CLAUSES (stmt
)
29931 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
29932 "#pragma omp single", pragma_tok
);
29933 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
29935 return add_stmt (stmt
);
29939 # pragma omp task task-clause[optseq] new-line
29940 structured-block */
29942 #define OMP_TASK_CLAUSE_MASK \
29943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29954 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
29956 tree clauses
, block
;
29959 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
29960 "#pragma omp task", pragma_tok
);
29961 block
= begin_omp_task ();
29962 save
= cp_parser_begin_omp_structured_block (parser
);
29963 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29964 cp_parser_end_omp_structured_block (parser
, save
);
29965 return finish_omp_task (clauses
, block
);
29969 # pragma omp taskwait new-line */
29972 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
29974 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29975 finish_omp_taskwait ();
29979 # pragma omp taskyield new-line */
29982 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
29984 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29985 finish_omp_taskyield ();
29989 # pragma omp taskgroup new-line
29990 structured-block */
29993 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
29995 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29996 return c_finish_omp_taskgroup (input_location
,
29997 cp_parser_omp_structured_block (parser
));
30002 # pragma omp threadprivate (variable-list) */
30005 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
30009 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
30010 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30012 finish_omp_threadprivate (vars
);
30016 # pragma omp cancel cancel-clause[optseq] new-line */
30018 #define OMP_CANCEL_CLAUSE_MASK \
30019 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30026 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
30028 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
30029 "#pragma omp cancel", pragma_tok
);
30030 finish_omp_cancel (clauses
);
30034 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30036 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30037 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30043 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
30046 bool point_seen
= false;
30048 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30050 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30051 const char *p
= IDENTIFIER_POINTER (id
);
30053 if (strcmp (p
, "point") == 0)
30055 cp_lexer_consume_token (parser
->lexer
);
30061 cp_parser_error (parser
, "expected %<point%>");
30062 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30066 clauses
= cp_parser_omp_all_clauses (parser
,
30067 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
30068 "#pragma omp cancellation point",
30070 finish_omp_cancellation_point (clauses
);
30074 #pragma omp distribute distribute-clause[optseq] new-line
30077 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30078 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30084 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
30085 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30087 tree clauses
, sb
, ret
;
30089 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30091 strcat (p_name
, " distribute");
30092 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
30094 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30096 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30097 const char *p
= IDENTIFIER_POINTER (id
);
30099 bool parallel
= false;
30101 if (strcmp (p
, "simd") == 0)
30104 parallel
= strcmp (p
, "parallel") == 0;
30105 if (parallel
|| simd
)
30107 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30108 if (cclauses
== NULL
)
30109 cclauses
= cclauses_buf
;
30110 cp_lexer_consume_token (parser
->lexer
);
30111 if (!flag_openmp
) /* flag_openmp_simd */
30114 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30117 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
30120 sb
= begin_omp_structured_block ();
30121 save
= cp_parser_begin_omp_structured_block (parser
);
30123 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30126 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
30128 cp_parser_end_omp_structured_block (parser
, save
);
30129 tree body
= finish_omp_structured_block (sb
);
30132 ret
= make_node (OMP_DISTRIBUTE
);
30133 TREE_TYPE (ret
) = void_type_node
;
30134 OMP_FOR_BODY (ret
) = body
;
30135 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
30136 SET_EXPR_LOCATION (ret
, loc
);
30141 if (!flag_openmp
) /* flag_openmp_simd */
30143 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30147 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30151 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
30152 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
30155 sb
= begin_omp_structured_block ();
30156 save
= cp_parser_begin_omp_structured_block (parser
);
30158 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
30160 cp_parser_end_omp_structured_block (parser
, save
);
30161 add_stmt (finish_omp_structured_block (sb
));
30167 # pragma omp teams teams-clause[optseq] new-line
30168 structured-block */
30170 #define OMP_TEAMS_CLAUSE_MASK \
30171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30180 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
30181 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30183 tree clauses
, sb
, ret
;
30185 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30187 strcat (p_name
, " teams");
30188 mask
|= OMP_TEAMS_CLAUSE_MASK
;
30190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30192 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30193 const char *p
= IDENTIFIER_POINTER (id
);
30194 if (strcmp (p
, "distribute") == 0)
30196 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30197 if (cclauses
== NULL
)
30198 cclauses
= cclauses_buf
;
30200 cp_lexer_consume_token (parser
->lexer
);
30201 if (!flag_openmp
) /* flag_openmp_simd */
30202 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
30204 sb
= begin_omp_structured_block ();
30205 save
= cp_parser_begin_omp_structured_block (parser
);
30206 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
30208 cp_parser_end_omp_structured_block (parser
, save
);
30209 tree body
= finish_omp_structured_block (sb
);
30212 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
30213 ret
= make_node (OMP_TEAMS
);
30214 TREE_TYPE (ret
) = void_type_node
;
30215 OMP_TEAMS_CLAUSES (ret
) = clauses
;
30216 OMP_TEAMS_BODY (ret
) = body
;
30217 return add_stmt (ret
);
30220 if (!flag_openmp
) /* flag_openmp_simd */
30222 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30226 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30230 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
30231 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
30234 tree stmt
= make_node (OMP_TEAMS
);
30235 TREE_TYPE (stmt
) = void_type_node
;
30236 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
30237 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30239 return add_stmt (stmt
);
30243 # pragma omp target data target-data-clause[optseq] new-line
30244 structured-block */
30246 #define OMP_TARGET_DATA_CLAUSE_MASK \
30247 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30252 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
30254 tree stmt
= make_node (OMP_TARGET_DATA
);
30255 TREE_TYPE (stmt
) = void_type_node
;
30257 OMP_TARGET_DATA_CLAUSES (stmt
)
30258 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
30259 "#pragma omp target data", pragma_tok
);
30260 keep_next_level (true);
30261 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30263 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30264 return add_stmt (stmt
);
30268 # pragma omp target update target-update-clause[optseq] new-line */
30270 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30271 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30277 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
30278 enum pragma_context context
)
30280 if (context
== pragma_stmt
)
30282 error_at (pragma_tok
->location
,
30283 "%<#pragma omp target update%> may only be "
30284 "used in compound statements");
30285 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30290 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
30291 "#pragma omp target update", pragma_tok
);
30292 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
30293 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
30295 error_at (pragma_tok
->location
,
30296 "%<#pragma omp target update must contain at least one "
30297 "%<from%> or %<to%> clauses");
30301 tree stmt
= make_node (OMP_TARGET_UPDATE
);
30302 TREE_TYPE (stmt
) = void_type_node
;
30303 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
30304 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30310 # pragma omp target target-clause[optseq] new-line
30311 structured-block */
30313 #define OMP_TARGET_CLAUSE_MASK \
30314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30319 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
30320 enum pragma_context context
)
30322 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
30324 cp_parser_error (parser
, "expected declaration specifiers");
30325 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30331 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30332 const char *p
= IDENTIFIER_POINTER (id
);
30334 if (strcmp (p
, "teams") == 0)
30336 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
30337 char p_name
[sizeof ("#pragma omp target teams distribute "
30338 "parallel for simd")];
30340 cp_lexer_consume_token (parser
->lexer
);
30341 strcpy (p_name
, "#pragma omp target");
30342 if (!flag_openmp
) /* flag_openmp_simd */
30343 return cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
30344 OMP_TARGET_CLAUSE_MASK
, cclauses
);
30345 keep_next_level (true);
30346 tree sb
= begin_omp_structured_block ();
30347 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
30348 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
30349 OMP_TARGET_CLAUSE_MASK
, cclauses
);
30350 cp_parser_end_omp_structured_block (parser
, save
);
30351 tree body
= finish_omp_structured_block (sb
);
30354 tree stmt
= make_node (OMP_TARGET
);
30355 TREE_TYPE (stmt
) = void_type_node
;
30356 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
30357 OMP_TARGET_BODY (stmt
) = body
;
30361 else if (!flag_openmp
) /* flag_openmp_simd */
30363 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30366 else if (strcmp (p
, "data") == 0)
30368 cp_lexer_consume_token (parser
->lexer
);
30369 cp_parser_omp_target_data (parser
, pragma_tok
);
30372 else if (strcmp (p
, "update") == 0)
30374 cp_lexer_consume_token (parser
->lexer
);
30375 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
30379 tree stmt
= make_node (OMP_TARGET
);
30380 TREE_TYPE (stmt
) = void_type_node
;
30382 OMP_TARGET_CLAUSES (stmt
)
30383 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
30384 "#pragma omp target", pragma_tok
);
30385 keep_next_level (true);
30386 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30388 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30394 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30396 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30397 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30405 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30406 enum pragma_context context
)
30408 bool first_p
= parser
->omp_declare_simd
== NULL
;
30409 cp_omp_declare_simd_data data
;
30412 data
.error_seen
= false;
30413 data
.fndecl_seen
= false;
30414 data
.tokens
= vNULL
;
30415 parser
->omp_declare_simd
= &data
;
30417 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30418 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30419 cp_lexer_consume_token (parser
->lexer
);
30420 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30421 parser
->omp_declare_simd
->error_seen
= true;
30422 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30423 struct cp_token_cache
*cp
30424 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
30425 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
30428 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
30429 cp_parser_pragma (parser
, context
);
30432 case pragma_external
:
30433 cp_parser_declaration (parser
);
30435 case pragma_member
:
30436 cp_parser_member_declaration (parser
);
30438 case pragma_objc_icode
:
30439 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30442 cp_parser_declaration_statement (parser
);
30445 if (parser
->omp_declare_simd
30446 && !parser
->omp_declare_simd
->error_seen
30447 && !parser
->omp_declare_simd
->fndecl_seen
)
30448 error_at (pragma_tok
->location
,
30449 "%<#pragma omp declare simd%> not immediately followed by "
30450 "function declaration or definition");
30451 data
.tokens
.release ();
30452 parser
->omp_declare_simd
= NULL
;
30456 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30457 This function is modelled similar to the late parsing of omp declare
30461 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
30463 struct cp_token_cache
*ce
;
30464 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
30467 if (parser
->omp_declare_simd
!= NULL
)
30469 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30470 " marked as a Cilk Plus SIMD-enabled function");
30471 XDELETE (parser
->cilk_simd_fn_info
);
30472 parser
->cilk_simd_fn_info
= NULL
;
30475 if (!info
->error_seen
&& info
->fndecl_seen
)
30477 error ("vector attribute not immediately followed by a single function"
30478 " declaration or definition");
30479 info
->error_seen
= true;
30481 if (info
->error_seen
)
30484 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
30488 cp_parser_push_lexer_for_tokens (parser
, ce
);
30489 parser
->lexer
->in_pragma
= true;
30490 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
30491 "SIMD-enabled functions attribute",
30493 cp_parser_pop_lexer (parser
);
30495 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
30497 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
30498 TREE_CHAIN (c
) = attrs
;
30501 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
30502 TREE_CHAIN (c
) = attrs
;
30503 if (processing_template_decl
)
30504 ATTR_IS_DEPENDENT (c
) = 1;
30507 info
->fndecl_seen
= true;
30508 XDELETE (parser
->cilk_simd_fn_info
);
30509 parser
->cilk_simd_fn_info
= NULL
;
30513 /* Finalize #pragma omp declare simd clauses after direct declarator has
30514 been parsed, and put that into "omp declare simd" attribute. */
30517 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
30519 struct cp_token_cache
*ce
;
30520 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
30523 if (!data
->error_seen
&& data
->fndecl_seen
)
30525 error ("%<#pragma omp declare simd%> not immediately followed by "
30526 "a single function declaration or definition");
30527 data
->error_seen
= true;
30530 if (data
->error_seen
)
30533 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
30537 cp_parser_push_lexer_for_tokens (parser
, ce
);
30538 parser
->lexer
->in_pragma
= true;
30539 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
30540 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
30541 cp_lexer_consume_token (parser
->lexer
);
30542 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
30543 "#pragma omp declare simd", pragma_tok
);
30544 cp_parser_pop_lexer (parser
);
30546 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
30547 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
30548 TREE_CHAIN (c
) = attrs
;
30549 if (processing_template_decl
)
30550 ATTR_IS_DEPENDENT (c
) = 1;
30554 data
->fndecl_seen
= true;
30560 # pragma omp declare target new-line
30561 declarations and definitions
30562 # pragma omp end declare target new-line */
30565 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
30567 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30568 scope_chain
->omp_declare_target_attribute
++;
30572 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
30574 const char *p
= "";
30575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30577 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30578 p
= IDENTIFIER_POINTER (id
);
30580 if (strcmp (p
, "declare") == 0)
30582 cp_lexer_consume_token (parser
->lexer
);
30584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30586 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30587 p
= IDENTIFIER_POINTER (id
);
30589 if (strcmp (p
, "target") == 0)
30590 cp_lexer_consume_token (parser
->lexer
);
30593 cp_parser_error (parser
, "expected %<target%>");
30594 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30600 cp_parser_error (parser
, "expected %<declare%>");
30601 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30604 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30605 if (!scope_chain
->omp_declare_target_attribute
)
30606 error_at (pragma_tok
->location
,
30607 "%<#pragma omp end declare target%> without corresponding "
30608 "%<#pragma omp declare target%>");
30610 scope_chain
->omp_declare_target_attribute
--;
30613 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30614 expression and optional initializer clause of
30615 #pragma omp declare reduction. We store the expression(s) as
30616 either 3, 6 or 7 special statements inside of the artificial function's
30617 body. The first two statements are DECL_EXPRs for the artificial
30618 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30619 expression that uses those variables.
30620 If there was any INITIALIZER clause, this is followed by further statements,
30621 the fourth and fifth statements are DECL_EXPRs for the artificial
30622 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30623 constructor variant (first token after open paren is not omp_priv),
30624 then the sixth statement is a statement with the function call expression
30625 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30626 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30627 to initialize the OMP_PRIV artificial variable and there is seventh
30628 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30631 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
30633 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
30634 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
30635 type
= TREE_TYPE (type
);
30636 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
30637 DECL_ARTIFICIAL (omp_out
) = 1;
30638 pushdecl (omp_out
);
30639 add_decl_expr (omp_out
);
30640 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
30641 DECL_ARTIFICIAL (omp_in
) = 1;
30643 add_decl_expr (omp_in
);
30645 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
30647 keep_next_level (true);
30648 tree block
= begin_omp_structured_block ();
30649 combiner
= cp_parser_expression (parser
, false, NULL
);
30650 finish_expr_stmt (combiner
);
30651 block
= finish_omp_structured_block (block
);
30654 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30657 const char *p
= "";
30658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30660 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30661 p
= IDENTIFIER_POINTER (id
);
30664 if (strcmp (p
, "initializer") == 0)
30666 cp_lexer_consume_token (parser
->lexer
);
30667 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30671 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30673 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30674 p
= IDENTIFIER_POINTER (id
);
30677 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
30678 DECL_ARTIFICIAL (omp_priv
) = 1;
30679 pushdecl (omp_priv
);
30680 add_decl_expr (omp_priv
);
30681 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
30682 DECL_ARTIFICIAL (omp_orig
) = 1;
30683 pushdecl (omp_orig
);
30684 add_decl_expr (omp_orig
);
30686 keep_next_level (true);
30687 block
= begin_omp_structured_block ();
30690 if (strcmp (p
, "omp_priv") == 0)
30692 bool is_direct_init
, is_non_constant_init
;
30694 cp_lexer_consume_token (parser
->lexer
);
30695 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
30697 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
30698 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30700 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
30701 == CPP_CLOSE_PAREN
))
30703 finish_omp_structured_block (block
);
30704 error ("invalid initializer clause");
30707 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
30708 &is_non_constant_init
);
30709 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
30710 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
30714 cp_parser_parse_tentatively (parser
);
30715 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
30716 /*check_dependency_p=*/true,
30717 /*template_p=*/NULL
,
30718 /*declarator_p=*/false,
30719 /*optional_p=*/false);
30720 vec
<tree
, va_gc
> *args
;
30721 if (fn_name
== error_mark_node
30722 || cp_parser_error_occurred (parser
)
30723 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
30724 || ((args
= cp_parser_parenthesized_expression_list
30725 (parser
, non_attr
, /*cast_p=*/false,
30726 /*allow_expansion_p=*/true,
30727 /*non_constant_p=*/NULL
)),
30728 cp_parser_error_occurred (parser
)))
30730 finish_omp_structured_block (block
);
30731 cp_parser_abort_tentative_parse (parser
);
30732 cp_parser_error (parser
, "expected id-expression (arguments)");
30737 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
30738 if (arg
== omp_priv
30739 || (TREE_CODE (arg
) == ADDR_EXPR
30740 && TREE_OPERAND (arg
, 0) == omp_priv
))
30742 cp_parser_abort_tentative_parse (parser
);
30743 if (arg
== NULL_TREE
)
30744 error ("one of the initializer call arguments should be %<omp_priv%>"
30745 " or %<&omp_priv%>");
30746 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
30748 finish_expr_stmt (initializer
);
30751 block
= finish_omp_structured_block (block
);
30752 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
30753 finish_expr_stmt (block
);
30756 add_decl_expr (omp_orig
);
30758 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30762 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
30763 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
30769 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30770 initializer-clause[opt] new-line
30772 initializer-clause:
30773 initializer (omp_priv initializer)
30774 initializer (function-name (argument-list)) */
30777 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
30778 enum pragma_context
)
30780 auto_vec
<tree
> types
;
30781 enum tree_code reduc_code
= ERROR_MARK
;
30782 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
30784 cp_token
*first_token
;
30785 cp_token_cache
*cp
;
30789 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
30790 p
= obstack_alloc (&declarator_obstack
, 0);
30792 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30795 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
30798 reduc_code
= PLUS_EXPR
;
30801 reduc_code
= MULT_EXPR
;
30804 reduc_code
= MINUS_EXPR
;
30807 reduc_code
= BIT_AND_EXPR
;
30810 reduc_code
= BIT_XOR_EXPR
;
30813 reduc_code
= BIT_IOR_EXPR
;
30816 reduc_code
= TRUTH_ANDIF_EXPR
;
30819 reduc_code
= TRUTH_ORIF_EXPR
;
30822 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
30825 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30826 "%<|%>, %<&&%>, %<||%> or identifier");
30830 if (reduc_code
!= ERROR_MARK
)
30831 cp_lexer_consume_token (parser
->lexer
);
30833 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
30834 if (reduc_id
== error_mark_node
)
30837 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30840 /* Types may not be defined in declare reduction type list. */
30841 const char *saved_message
;
30842 saved_message
= parser
->type_definition_forbidden_message
;
30843 parser
->type_definition_forbidden_message
30844 = G_("types may not be defined in declare reduction type list");
30845 bool saved_colon_corrects_to_scope_p
;
30846 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30847 parser
->colon_corrects_to_scope_p
= false;
30848 bool saved_colon_doesnt_start_class_def_p
;
30849 saved_colon_doesnt_start_class_def_p
30850 = parser
->colon_doesnt_start_class_def_p
;
30851 parser
->colon_doesnt_start_class_def_p
= true;
30855 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30856 type
= cp_parser_type_id (parser
);
30857 if (type
== error_mark_node
)
30859 else if (ARITHMETIC_TYPE_P (type
)
30860 && (orig_reduc_id
== NULL_TREE
30861 || (TREE_CODE (type
) != COMPLEX_TYPE
30862 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
30864 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
30866 error_at (loc
, "predeclared arithmetic type %qT in "
30867 "%<#pragma omp declare reduction%>", type
);
30868 else if (TREE_CODE (type
) == FUNCTION_TYPE
30869 || TREE_CODE (type
) == METHOD_TYPE
30870 || TREE_CODE (type
) == ARRAY_TYPE
)
30871 error_at (loc
, "function or array type %qT in "
30872 "%<#pragma omp declare reduction%>", type
);
30873 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
30874 error_at (loc
, "reference type %qT in "
30875 "%<#pragma omp declare reduction%>", type
);
30876 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
30877 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
30878 "%<#pragma omp declare reduction%>", type
);
30880 types
.safe_push (type
);
30882 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30883 cp_lexer_consume_token (parser
->lexer
);
30888 /* Restore the saved message. */
30889 parser
->type_definition_forbidden_message
= saved_message
;
30890 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30891 parser
->colon_doesnt_start_class_def_p
30892 = saved_colon_doesnt_start_class_def_p
;
30894 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
30895 || types
.is_empty ())
30898 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30902 first_token
= cp_lexer_peek_token (parser
->lexer
);
30905 FOR_EACH_VEC_ELT (types
, i
, type
)
30908 = build_function_type_list (void_type_node
,
30909 cp_build_reference_type (type
, false),
30911 tree this_reduc_id
= reduc_id
;
30912 if (!dependent_type_p (type
))
30913 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
30914 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
30915 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
30916 DECL_ARTIFICIAL (fndecl
) = 1;
30917 DECL_EXTERNAL (fndecl
) = 1;
30918 DECL_DECLARED_INLINE_P (fndecl
) = 1;
30919 DECL_IGNORED_P (fndecl
) = 1;
30920 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
30921 DECL_ATTRIBUTES (fndecl
)
30922 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
30923 DECL_ATTRIBUTES (fndecl
));
30924 if (processing_template_decl
)
30925 fndecl
= push_template_decl (fndecl
);
30926 bool block_scope
= false;
30927 tree block
= NULL_TREE
;
30928 if (current_function_decl
)
30930 block_scope
= true;
30931 DECL_CONTEXT (fndecl
) = global_namespace
;
30932 if (!processing_template_decl
)
30935 else if (current_class_type
)
30939 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30940 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30941 cp_lexer_consume_token (parser
->lexer
);
30942 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30944 cp
= cp_token_cache_new (first_token
,
30945 cp_lexer_peek_nth_token (parser
->lexer
,
30948 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
30949 finish_member_declaration (fndecl
);
30950 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
30951 DECL_PENDING_INLINE_P (fndecl
) = 1;
30952 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
30957 DECL_CONTEXT (fndecl
) = current_namespace
;
30961 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
30963 block
= begin_omp_structured_block ();
30966 cp_parser_push_lexer_for_tokens (parser
, cp
);
30967 parser
->lexer
->in_pragma
= true;
30969 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
30972 finish_function (0);
30974 DECL_CONTEXT (fndecl
) = current_function_decl
;
30976 cp_parser_pop_lexer (parser
);
30980 cp_parser_pop_lexer (parser
);
30982 finish_function (0);
30985 DECL_CONTEXT (fndecl
) = current_function_decl
;
30986 block
= finish_omp_structured_block (block
);
30987 if (TREE_CODE (block
) == BIND_EXPR
)
30988 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
30989 else if (TREE_CODE (block
) == STATEMENT_LIST
)
30990 DECL_SAVED_TREE (fndecl
) = block
;
30991 if (processing_template_decl
)
30992 add_decl_expr (fndecl
);
30994 cp_check_omp_declare_reduction (fndecl
);
30995 if (cp
== NULL
&& types
.length () > 1)
30996 cp
= cp_token_cache_new (first_token
,
30997 cp_lexer_peek_nth_token (parser
->lexer
, 2));
30998 if (errs
!= errorcount
)
31002 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31005 /* Free any declarators allocated. */
31006 obstack_free (&declarator_obstack
, p
);
31010 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31011 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31012 initializer-clause[opt] new-line
31013 #pragma omp declare target new-line */
31016 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
31017 enum pragma_context context
)
31019 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31021 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31022 const char *p
= IDENTIFIER_POINTER (id
);
31024 if (strcmp (p
, "simd") == 0)
31026 cp_lexer_consume_token (parser
->lexer
);
31027 cp_parser_omp_declare_simd (parser
, pragma_tok
,
31031 cp_ensure_no_omp_declare_simd (parser
);
31032 if (strcmp (p
, "reduction") == 0)
31034 cp_lexer_consume_token (parser
->lexer
);
31035 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
31039 if (!flag_openmp
) /* flag_openmp_simd */
31041 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31044 if (strcmp (p
, "target") == 0)
31046 cp_lexer_consume_token (parser
->lexer
);
31047 cp_parser_omp_declare_target (parser
, pragma_tok
);
31051 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
31053 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31056 /* Main entry point to OpenMP statement pragmas. */
31059 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
31062 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
31063 omp_clause_mask
mask (0);
31065 switch (pragma_tok
->pragma_kind
)
31067 case PRAGMA_OMP_ATOMIC
:
31068 cp_parser_omp_atomic (parser
, pragma_tok
);
31070 case PRAGMA_OMP_CRITICAL
:
31071 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
31073 case PRAGMA_OMP_DISTRIBUTE
:
31074 strcpy (p_name
, "#pragma omp");
31075 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
31077 case PRAGMA_OMP_FOR
:
31078 strcpy (p_name
, "#pragma omp");
31079 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
31081 case PRAGMA_OMP_MASTER
:
31082 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
31084 case PRAGMA_OMP_ORDERED
:
31085 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
31087 case PRAGMA_OMP_PARALLEL
:
31088 strcpy (p_name
, "#pragma omp");
31089 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
31091 case PRAGMA_OMP_SECTIONS
:
31092 strcpy (p_name
, "#pragma omp");
31093 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
31095 case PRAGMA_OMP_SIMD
:
31096 strcpy (p_name
, "#pragma omp");
31097 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
31099 case PRAGMA_OMP_SINGLE
:
31100 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
31102 case PRAGMA_OMP_TASK
:
31103 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
31105 case PRAGMA_OMP_TASKGROUP
:
31106 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
31108 case PRAGMA_OMP_TEAMS
:
31109 strcpy (p_name
, "#pragma omp");
31110 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
31113 gcc_unreachable ();
31117 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31120 /* Transactional Memory parsing routines. */
31122 /* Parse a transaction attribute.
31128 ??? Simplify this when C++0x bracket attributes are
31129 implemented properly. */
31132 cp_parser_txn_attribute_opt (cp_parser
*parser
)
31135 tree attr_name
, attr
= NULL
;
31137 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
31138 return cp_parser_attributes_opt (parser
);
31140 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
31142 cp_lexer_consume_token (parser
->lexer
);
31143 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
31146 token
= cp_lexer_peek_token (parser
->lexer
);
31147 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
31149 token
= cp_lexer_consume_token (parser
->lexer
);
31151 attr_name
= (token
->type
== CPP_KEYWORD
31152 /* For keywords, use the canonical spelling,
31153 not the parsed identifier. */
31154 ? ridpointers
[(int) token
->keyword
]
31156 attr
= build_tree_list (attr_name
, NULL_TREE
);
31159 cp_parser_error (parser
, "expected identifier");
31161 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31163 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31167 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31169 transaction-statement:
31170 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31172 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31176 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
31178 unsigned char old_in
= parser
->in_transaction
;
31179 unsigned char this_in
= 1, new_in
;
31181 tree stmt
, attrs
, noex
;
31183 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31184 || keyword
== RID_TRANSACTION_RELAXED
);
31185 token
= cp_parser_require_keyword (parser
, keyword
,
31186 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31187 : RT_TRANSACTION_RELAXED
));
31188 gcc_assert (token
!= NULL
);
31190 if (keyword
== RID_TRANSACTION_RELAXED
)
31191 this_in
|= TM_STMT_ATTR_RELAXED
;
31194 attrs
= cp_parser_txn_attribute_opt (parser
);
31196 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
31199 /* Parse a noexcept specification. */
31200 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
31202 /* Keep track if we're in the lexical scope of an outer transaction. */
31203 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
31205 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
31207 parser
->in_transaction
= new_in
;
31208 cp_parser_compound_statement (parser
, NULL
, false, false);
31209 parser
->in_transaction
= old_in
;
31211 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
31216 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31218 transaction-expression:
31219 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31220 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31224 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
31226 unsigned char old_in
= parser
->in_transaction
;
31227 unsigned char this_in
= 1;
31232 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31233 || keyword
== RID_TRANSACTION_RELAXED
);
31236 error (keyword
== RID_TRANSACTION_RELAXED
31237 ? G_("%<__transaction_relaxed%> without transactional memory "
31239 : G_("%<__transaction_atomic%> without transactional memory "
31240 "support enabled"));
31242 token
= cp_parser_require_keyword (parser
, keyword
,
31243 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31244 : RT_TRANSACTION_RELAXED
));
31245 gcc_assert (token
!= NULL
);
31247 if (keyword
== RID_TRANSACTION_RELAXED
)
31248 this_in
|= TM_STMT_ATTR_RELAXED
;
31250 /* Set this early. This might mean that we allow transaction_cancel in
31251 an expression that we find out later actually has to be a constexpr.
31252 However, we expect that cxx_constant_value will be able to deal with
31253 this; also, if the noexcept has no constexpr, then what we parse next
31254 really is a transaction's body. */
31255 parser
->in_transaction
= this_in
;
31257 /* Parse a noexcept specification. */
31258 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
31261 if (!noex
|| !noex_expr
31262 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31264 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
31266 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
31267 expr
= finish_parenthesized_expr (expr
);
31269 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
31273 /* The only expression that is available got parsed for the noexcept
31274 already. noexcept is true then. */
31276 noex
= boolean_true_node
;
31279 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
31280 parser
->in_transaction
= old_in
;
31282 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
31283 return error_mark_node
;
31285 return (flag_tm
? expr
: error_mark_node
);
31288 /* Parse a function-transaction-block.
31290 function-transaction-block:
31291 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31293 __transaction_atomic txn-attribute[opt] function-try-block
31294 __transaction_relaxed ctor-initializer[opt] function-body
31295 __transaction_relaxed function-try-block
31299 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
31301 unsigned char old_in
= parser
->in_transaction
;
31302 unsigned char new_in
= 1;
31303 tree compound_stmt
, stmt
, attrs
;
31304 bool ctor_initializer_p
;
31307 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31308 || keyword
== RID_TRANSACTION_RELAXED
);
31309 token
= cp_parser_require_keyword (parser
, keyword
,
31310 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31311 : RT_TRANSACTION_RELAXED
));
31312 gcc_assert (token
!= NULL
);
31314 if (keyword
== RID_TRANSACTION_RELAXED
)
31315 new_in
|= TM_STMT_ATTR_RELAXED
;
31318 attrs
= cp_parser_txn_attribute_opt (parser
);
31320 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
31323 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
31325 parser
->in_transaction
= new_in
;
31327 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
31328 ctor_initializer_p
= cp_parser_function_try_block (parser
);
31330 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
31331 (parser
, /*in_function_try_block=*/false);
31333 parser
->in_transaction
= old_in
;
31335 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
31337 return ctor_initializer_p
;
31340 /* Parse a __transaction_cancel statement.
31343 __transaction_cancel txn-attribute[opt] ;
31344 __transaction_cancel txn-attribute[opt] throw-expression ;
31346 ??? Cancel and throw is not yet implemented. */
31349 cp_parser_transaction_cancel (cp_parser
*parser
)
31352 bool is_outer
= false;
31355 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
31356 RT_TRANSACTION_CANCEL
);
31357 gcc_assert (token
!= NULL
);
31359 attrs
= cp_parser_txn_attribute_opt (parser
);
31361 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
31363 /* ??? Parse cancel-and-throw here. */
31365 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
31369 error_at (token
->location
, "%<__transaction_cancel%> without "
31370 "transactional memory support enabled");
31371 return error_mark_node
;
31373 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
31375 error_at (token
->location
, "%<__transaction_cancel%> within a "
31376 "%<__transaction_relaxed%>");
31377 return error_mark_node
;
31381 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
31382 && !is_tm_may_cancel_outer (current_function_decl
))
31384 error_at (token
->location
, "outer %<__transaction_cancel%> not "
31385 "within outer %<__transaction_atomic%>");
31386 error_at (token
->location
,
31387 " or a %<transaction_may_cancel_outer%> function");
31388 return error_mark_node
;
31391 else if (parser
->in_transaction
== 0)
31393 error_at (token
->location
, "%<__transaction_cancel%> not within "
31394 "%<__transaction_atomic%>");
31395 return error_mark_node
;
31398 stmt
= build_tm_abort_call (token
->location
, is_outer
);
31406 static GTY (()) cp_parser
*the_parser
;
31409 /* Special handling for the first token or line in the file. The first
31410 thing in the file might be #pragma GCC pch_preprocess, which loads a
31411 PCH file, which is a GC collection point. So we need to handle this
31412 first pragma without benefit of an existing lexer structure.
31414 Always returns one token to the caller in *FIRST_TOKEN. This is
31415 either the true first token of the file, or the first token after
31416 the initial pragma. */
31419 cp_parser_initial_pragma (cp_token
*first_token
)
31423 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31424 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
31427 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31428 if (first_token
->type
== CPP_STRING
)
31430 name
= first_token
->u
.value
;
31432 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31433 if (first_token
->type
!= CPP_PRAGMA_EOL
)
31434 error_at (first_token
->location
,
31435 "junk at end of %<#pragma GCC pch_preprocess%>");
31438 error_at (first_token
->location
, "expected string literal");
31440 /* Skip to the end of the pragma. */
31441 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
31442 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31444 /* Now actually load the PCH file. */
31446 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
31448 /* Read one more token to return to our caller. We have to do this
31449 after reading the PCH file in, since its pointers have to be
31451 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31454 /* Normal parsing of a pragma token. Here we can (and must) use the
31458 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
31460 cp_token
*pragma_tok
;
31463 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
31464 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
31465 parser
->lexer
->in_pragma
= true;
31467 id
= pragma_tok
->pragma_kind
;
31468 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
31469 cp_ensure_no_omp_declare_simd (parser
);
31472 case PRAGMA_GCC_PCH_PREPROCESS
:
31473 error_at (pragma_tok
->location
,
31474 "%<#pragma GCC pch_preprocess%> must be first");
31477 case PRAGMA_OMP_BARRIER
:
31480 case pragma_compound
:
31481 cp_parser_omp_barrier (parser
, pragma_tok
);
31484 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
31485 "used in compound statements");
31492 case PRAGMA_OMP_FLUSH
:
31495 case pragma_compound
:
31496 cp_parser_omp_flush (parser
, pragma_tok
);
31499 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
31500 "used in compound statements");
31507 case PRAGMA_OMP_TASKWAIT
:
31510 case pragma_compound
:
31511 cp_parser_omp_taskwait (parser
, pragma_tok
);
31514 error_at (pragma_tok
->location
,
31515 "%<#pragma omp taskwait%> may only be "
31516 "used in compound statements");
31523 case PRAGMA_OMP_TASKYIELD
:
31526 case pragma_compound
:
31527 cp_parser_omp_taskyield (parser
, pragma_tok
);
31530 error_at (pragma_tok
->location
,
31531 "%<#pragma omp taskyield%> may only be "
31532 "used in compound statements");
31539 case PRAGMA_OMP_CANCEL
:
31542 case pragma_compound
:
31543 cp_parser_omp_cancel (parser
, pragma_tok
);
31546 error_at (pragma_tok
->location
,
31547 "%<#pragma omp cancel%> may only be "
31548 "used in compound statements");
31555 case PRAGMA_OMP_CANCELLATION_POINT
:
31558 case pragma_compound
:
31559 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
31562 error_at (pragma_tok
->location
,
31563 "%<#pragma omp cancellation point%> may only be "
31564 "used in compound statements");
31571 case PRAGMA_OMP_THREADPRIVATE
:
31572 cp_parser_omp_threadprivate (parser
, pragma_tok
);
31575 case PRAGMA_OMP_DECLARE_REDUCTION
:
31576 cp_parser_omp_declare (parser
, pragma_tok
, context
);
31579 case PRAGMA_OMP_ATOMIC
:
31580 case PRAGMA_OMP_CRITICAL
:
31581 case PRAGMA_OMP_DISTRIBUTE
:
31582 case PRAGMA_OMP_FOR
:
31583 case PRAGMA_OMP_MASTER
:
31584 case PRAGMA_OMP_ORDERED
:
31585 case PRAGMA_OMP_PARALLEL
:
31586 case PRAGMA_OMP_SECTIONS
:
31587 case PRAGMA_OMP_SIMD
:
31588 case PRAGMA_OMP_SINGLE
:
31589 case PRAGMA_OMP_TASK
:
31590 case PRAGMA_OMP_TASKGROUP
:
31591 case PRAGMA_OMP_TEAMS
:
31592 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
31594 cp_parser_omp_construct (parser
, pragma_tok
);
31597 case PRAGMA_OMP_TARGET
:
31598 return cp_parser_omp_target (parser
, pragma_tok
, context
);
31600 case PRAGMA_OMP_END_DECLARE_TARGET
:
31601 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
31604 case PRAGMA_OMP_SECTION
:
31605 error_at (pragma_tok
->location
,
31606 "%<#pragma omp section%> may only be used in "
31607 "%<#pragma omp sections%> construct");
31612 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31614 tok
= cp_lexer_peek_token (the_parser
->lexer
);
31615 if (tok
->type
!= CPP_KEYWORD
31616 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
31617 && tok
->keyword
!= RID_DO
))
31619 cp_parser_error (parser
, "for, while or do statement expected");
31622 cp_parser_iteration_statement (parser
, true);
31626 case PRAGMA_CILK_SIMD
:
31627 if (context
== pragma_external
)
31629 error_at (pragma_tok
->location
,
31630 "%<#pragma simd%> must be inside a function");
31633 cp_parser_cilk_simd (parser
, pragma_tok
);
31637 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
31638 c_invoke_pragma_handler (id
);
31642 cp_parser_error (parser
, "expected declaration specifiers");
31646 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31650 /* The interface the pragma parsers have to the lexer. */
31653 pragma_lex (tree
*value
)
31656 enum cpp_ttype ret
;
31658 tok
= cp_lexer_peek_token (the_parser
->lexer
);
31661 *value
= tok
->u
.value
;
31663 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
31665 else if (ret
== CPP_STRING
)
31666 *value
= cp_parser_string_literal (the_parser
, false, false);
31669 cp_lexer_consume_token (the_parser
->lexer
);
31670 if (ret
== CPP_KEYWORD
)
31678 /* External interface. */
31680 /* Parse one entire translation unit. */
31683 c_parse_file (void)
31685 static bool already_called
= false;
31687 if (already_called
)
31689 sorry ("inter-module optimizations not implemented for C++");
31692 already_called
= true;
31694 the_parser
= cp_parser_new ();
31695 push_deferring_access_checks (flag_access_control
31696 ? dk_no_deferred
: dk_no_check
);
31697 cp_parser_translation_unit (the_parser
);
31701 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31702 vectorlength clause:
31704 vectorlength ( constant-expression ) */
31707 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
31710 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31712 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31713 safelen clause. Thus, vectorlength is represented as OMP 4.0
31714 safelen. For SIMD-enabled function it is represented by OMP 4.0
31717 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
31720 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
31723 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31724 return error_mark_node
;
31726 expr
= cp_parser_constant_expression (parser
, false, NULL
);
31727 expr
= maybe_constant_value (expr
);
31729 /* If expr == error_mark_node, then don't emit any errors nor
31730 create a clause. if any of the above functions returns
31731 error mark node then they would have emitted an error message. */
31732 if (expr
== error_mark_node
)
31734 else if (!TREE_TYPE (expr
)
31735 || !TREE_CONSTANT (expr
)
31736 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
31737 error_at (loc
, "vectorlength must be an integer constant");
31738 else if (TREE_CONSTANT (expr
)
31739 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
31740 error_at (loc
, "vectorlength must be a power of 2");
31746 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
31747 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
31748 OMP_CLAUSE_CHAIN (c
) = clauses
;
31753 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
31754 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
31755 OMP_CLAUSE_CHAIN (c
) = clauses
;
31760 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31761 return error_mark_node
;
31765 /* Handles the Cilk Plus #pragma simd linear clause.
31767 linear ( simd-linear-variable-list )
31769 simd-linear-variable-list:
31770 simd-linear-variable
31771 simd-linear-variable-list , simd-linear-variable
31773 simd-linear-variable:
31775 id-expression : simd-linear-step
31778 conditional-expression */
31781 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
31783 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31785 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31787 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31789 cp_parser_error (parser
, "expected identifier");
31790 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31791 return error_mark_node
;
31794 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31795 parser
->colon_corrects_to_scope_p
= false;
31798 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31799 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31801 cp_parser_error (parser
, "expected variable-name");
31802 clauses
= error_mark_node
;
31806 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
31808 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
31810 if (decl
== error_mark_node
)
31812 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
31814 clauses
= error_mark_node
;
31818 tree e
= NULL_TREE
;
31819 tree step_size
= integer_one_node
;
31821 /* If present, parse the linear step. Otherwise, assume the default
31823 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
31825 cp_lexer_consume_token (parser
->lexer
);
31827 e
= cp_parser_assignment_expression (parser
, false, NULL
);
31828 e
= maybe_constant_value (e
);
31830 if (e
== error_mark_node
)
31832 /* If an error has occurred, then the whole pragma is
31833 considered ill-formed. Thus, no reason to keep
31835 clauses
= error_mark_node
;
31838 else if (type_dependent_expression_p (e
)
31839 || value_dependent_expression_p (e
)
31841 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
31842 && (TREE_CONSTANT (e
)
31846 cp_parser_error (parser
,
31847 "step size must be an integer constant "
31848 "expression or an integer variable");
31851 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31852 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
31853 OMP_CLAUSE_DECL (l
) = decl
;
31854 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
31855 OMP_CLAUSE_CHAIN (l
) = clauses
;
31858 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31859 cp_lexer_consume_token (parser
->lexer
);
31860 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31864 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31865 "expected %<,%> or %<)%> after %qE", decl
);
31866 clauses
= error_mark_node
;
31870 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31871 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31875 /* Returns the name of the next clause. If the clause is not
31876 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31877 token is not consumed. Otherwise, the appropriate enum from the
31878 pragma_simd_clause is returned and the token is consumed. */
31880 static pragma_omp_clause
31881 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
31883 pragma_omp_clause clause_type
;
31884 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31886 if (token
->keyword
== RID_PRIVATE
)
31887 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
31888 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
31889 return PRAGMA_CILK_CLAUSE_NONE
;
31890 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
31891 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
31892 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
31893 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
31894 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
31895 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
31896 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
31897 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
31898 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
31899 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
31901 return PRAGMA_CILK_CLAUSE_NONE
;
31903 cp_lexer_consume_token (parser
->lexer
);
31904 return clause_type
;
31907 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31910 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
31912 tree clauses
= NULL_TREE
;
31914 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31915 && clauses
!= error_mark_node
)
31917 pragma_omp_clause c_kind
;
31918 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
31919 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
31920 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
31921 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
31922 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
31923 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
31924 /* Use the OpenMP 4.0 equivalent function. */
31925 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
31926 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
31927 /* Use the OpenMP 4.0 equivalent function. */
31928 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
31930 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
31931 /* Use the OMP 4.0 equivalent function. */
31932 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
31934 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
31935 /* Use the OMP 4.0 equivalent function. */
31936 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
31939 clauses
= error_mark_node
;
31940 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
31945 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
31947 if (clauses
== error_mark_node
)
31948 return error_mark_node
;
31950 return c_finish_cilk_clauses (clauses
);
31953 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31956 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
31958 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
31960 if (clauses
== error_mark_node
)
31963 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
31965 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31966 "for statement expected");
31970 tree sb
= begin_omp_structured_block ();
31971 int save
= cp_parser_begin_omp_structured_block (parser
);
31972 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
31974 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
31975 cp_parser_end_omp_structured_block (parser
, save
);
31976 add_stmt (finish_omp_structured_block (sb
));
31980 /* Create an identifier for a generic parameter type (a synthesized
31981 template parameter implied by `auto' or a concept identifier). */
31983 static GTY(()) int generic_parm_count
;
31985 make_generic_type_name ()
31988 sprintf (buf
, "auto:%d", ++generic_parm_count
);
31989 return get_identifier (buf
);
31992 /* Predicate that behaves as is_auto_or_concept but matches the parent
31993 node of the generic type rather than the generic type itself. This
31994 allows for type transformation in add_implicit_template_parms. */
31997 tree_type_is_auto_or_concept (const_tree t
)
31999 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
32002 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32003 (creating a new template parameter list if necessary). Returns the newly
32004 created template type parm. */
32007 synthesize_implicit_template_parm (cp_parser
*parser
)
32009 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
32011 /* We are either continuing a function template that already contains implicit
32012 template parameters, creating a new fully-implicit function template, or
32013 extending an existing explicit function template with implicit template
32016 cp_binding_level
*const entry_scope
= current_binding_level
;
32018 bool become_template
= false;
32019 cp_binding_level
*parent_scope
= 0;
32021 if (parser
->implicit_template_scope
)
32023 gcc_assert (parser
->implicit_template_parms
);
32025 current_binding_level
= parser
->implicit_template_scope
;
32029 /* Roll back to the existing template parameter scope (in the case of
32030 extending an explicit function template) or introduce a new template
32031 parameter scope ahead of the function parameter scope (or class scope
32032 in the case of out-of-line member definitions). The function scope is
32033 added back after template parameter synthesis below. */
32035 cp_binding_level
*scope
= entry_scope
;
32037 while (scope
->kind
== sk_function_parms
)
32039 parent_scope
= scope
;
32040 scope
= scope
->level_chain
;
32042 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
32044 /* If not defining a class, then any class scope is a scope level in
32045 an out-of-line member definition. In this case simply wind back
32046 beyond the first such scope to inject the template parameter list.
32047 Otherwise wind back to the class being defined. The latter can
32048 occur in class member friend declarations such as:
32054 friend void A::foo (auto);
32057 The template parameter list synthesized for the friend declaration
32058 must be injected in the scope of 'B'. This can also occur in
32059 erroneous cases such as:
32065 void B::foo (auto) {}
32068 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32069 but, nevertheless, the template parameter list synthesized for the
32070 declarator should be injected into the scope of 'A' as if the
32071 ill-formed template was specified explicitly. */
32073 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
32075 parent_scope
= scope
;
32076 scope
= scope
->level_chain
;
32080 current_binding_level
= scope
;
32082 if (scope
->kind
!= sk_template_parms
32083 || !function_being_declared_is_template_p (parser
))
32085 /* Introduce a new template parameter list for implicit template
32088 become_template
= true;
32090 parser
->implicit_template_scope
32091 = begin_scope (sk_template_parms
, NULL
);
32093 ++processing_template_decl
;
32095 parser
->fully_implicit_function_template_p
= true;
32096 ++parser
->num_template_parameter_lists
;
32100 /* Synthesize implicit template parameters at the end of the explicit
32101 template parameter list. */
32103 gcc_assert (current_template_parms
);
32105 parser
->implicit_template_scope
= scope
;
32107 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
32108 parser
->implicit_template_parms
32109 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
32113 /* Synthesize a new template parameter and track the current template
32114 parameter chain with implicit_template_parms. */
32116 tree synth_id
= make_generic_type_name ();
32117 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
32120 = process_template_parm (parser
->implicit_template_parms
,
32122 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
32123 /*non_type=*/false,
32124 /*param_pack=*/false);
32127 if (parser
->implicit_template_parms
)
32128 parser
->implicit_template_parms
32129 = TREE_CHAIN (parser
->implicit_template_parms
);
32131 parser
->implicit_template_parms
= new_parm
;
32133 tree new_type
= TREE_TYPE (getdecls ());
32135 /* If creating a fully implicit function template, start the new implicit
32136 template parameter list with this synthesized type, otherwise grow the
32137 current template parameter list. */
32139 if (become_template
)
32141 parent_scope
->level_chain
= current_binding_level
;
32143 tree new_parms
= make_tree_vec (1);
32144 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
32145 current_template_parms
= tree_cons (size_int (processing_template_decl
),
32146 new_parms
, current_template_parms
);
32150 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
32151 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
32152 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
32153 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
32156 current_binding_level
= entry_scope
;
32161 /* Finish the declaration of a fully implicit function template. Such a
32162 template has no explicit template parameter list so has not been through the
32163 normal template head and tail processing. synthesize_implicit_template_parm
32164 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32165 provided if the declaration is a class member such that its template
32166 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32167 form is returned. Otherwise NULL_TREE is returned. */
32170 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
32172 gcc_assert (parser
->fully_implicit_function_template_p
);
32174 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
32175 && DECL_VIRTUAL_P (member_decl_opt
))
32177 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
32178 "implicit templates may not be %<virtual%>");
32179 DECL_VIRTUAL_P (member_decl_opt
) = false;
32182 if (member_decl_opt
)
32183 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
32184 end_template_decl ();
32186 parser
->fully_implicit_function_template_p
= false;
32187 --parser
->num_template_parameter_lists
;
32189 return member_decl_opt
;
32192 #include "gt-cp-parser.h"