2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
30 #include "c-family/c-pragma.h"
33 #include "diagnostic-core.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
39 #include "tree-pretty-print.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 static cp_token eof_token
=
50 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
53 /* The various kinds of non integral constant we encounter. */
54 typedef enum non_integral_constant
{
56 /* floating-point literal */
60 /* %<__FUNCTION__%> */
62 /* %<__PRETTY_FUNCTION__%> */
70 /* %<typeid%> operator */
72 /* non-constant compound literals */
80 /* an array reference */
86 /* the address of a label */
100 /* calls to overloaded operators */
104 /* a comma operator */
106 /* a call to a constructor */
108 /* a transaction expression */
110 } non_integral_constant
;
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error
{
118 /* is not a class or namespace */
120 /* is not a class, namespace, or enumeration */
124 /* The various kinds of required token */
125 typedef enum required_token
{
127 RT_SEMICOLON
, /* ';' */
128 RT_OPEN_PAREN
, /* '(' */
129 RT_CLOSE_BRACE
, /* '}' */
130 RT_OPEN_BRACE
, /* '{' */
131 RT_CLOSE_SQUARE
, /* ']' */
132 RT_OPEN_SQUARE
, /* '[' */
136 RT_GREATER
, /* '>' */
138 RT_ELLIPSIS
, /* '...' */
142 RT_COLON_SCOPE
, /* ':' or '::' */
143 RT_CLOSE_PAREN
, /* ')' */
144 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
145 RT_PRAGMA_EOL
, /* end of line */
146 RT_NAME
, /* identifier */
148 /* The type is CPP_KEYWORD */
150 RT_DELETE
, /* delete */
151 RT_RETURN
, /* return */
152 RT_WHILE
, /* while */
153 RT_EXTERN
, /* extern */
154 RT_STATIC_ASSERT
, /* static_assert */
155 RT_DECLTYPE
, /* decltype */
156 RT_OPERATOR
, /* operator */
157 RT_CLASS
, /* class */
158 RT_TEMPLATE
, /* template */
159 RT_NAMESPACE
, /* namespace */
160 RT_USING
, /* using */
163 RT_CATCH
, /* catch */
164 RT_THROW
, /* throw */
165 RT_LABEL
, /* __label__ */
166 RT_AT_TRY
, /* @try */
167 RT_AT_SYNCHRONIZED
, /* @synchronized */
168 RT_AT_THROW
, /* @throw */
170 RT_SELECT
, /* selection-statement */
171 RT_INTERATION
, /* iteration-statement */
172 RT_JUMP
, /* jump-statement */
173 RT_CLASS_KEY
, /* class-key */
174 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
175 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
176 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
177 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
182 static cp_lexer
*cp_lexer_new_main
184 static cp_lexer
*cp_lexer_new_from_tokens
185 (cp_token_cache
*tokens
);
186 static void cp_lexer_destroy
188 static int cp_lexer_saving_tokens
190 static cp_token
*cp_lexer_token_at
191 (cp_lexer
*, cp_token_position
);
192 static void cp_lexer_get_preprocessor_token
193 (cp_lexer
*, cp_token
*);
194 static inline cp_token
*cp_lexer_peek_token
196 static cp_token
*cp_lexer_peek_nth_token
197 (cp_lexer
*, size_t);
198 static inline bool cp_lexer_next_token_is
199 (cp_lexer
*, enum cpp_ttype
);
200 static bool cp_lexer_next_token_is_not
201 (cp_lexer
*, enum cpp_ttype
);
202 static bool cp_lexer_next_token_is_keyword
203 (cp_lexer
*, enum rid
);
204 static cp_token
*cp_lexer_consume_token
206 static void cp_lexer_purge_token
208 static void cp_lexer_purge_tokens_after
209 (cp_lexer
*, cp_token_position
);
210 static void cp_lexer_save_tokens
212 static void cp_lexer_commit_tokens
214 static void cp_lexer_rollback_tokens
216 static void cp_lexer_print_token
217 (FILE *, cp_token
*);
218 static inline bool cp_lexer_debugging_p
220 static void cp_lexer_start_debugging
221 (cp_lexer
*) ATTRIBUTE_UNUSED
;
222 static void cp_lexer_stop_debugging
223 (cp_lexer
*) ATTRIBUTE_UNUSED
;
225 static cp_token_cache
*cp_token_cache_new
226 (cp_token
*, cp_token
*);
228 static void cp_parser_initial_pragma
231 static tree cp_literal_operator_id
234 /* Manifest constants. */
235 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
236 #define CP_SAVED_TOKEN_STACK 5
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream
;
243 /* Nonzero if we are parsing an unevaluated operand: an operand to
244 sizeof, typeof, or alignof. */
245 int cp_unevaluated_operand
;
247 /* Dump up to NUM tokens in BUFFER to FILE starting with token
248 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
249 first token in BUFFER. If NUM is 0, dump all the tokens. If
250 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
251 highlighted by surrounding it in [[ ]]. */
254 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
255 cp_token
*start_token
, unsigned num
,
256 cp_token
*curr_token
)
258 unsigned i
, nprinted
;
262 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
268 num
= buffer
->length ();
270 if (start_token
== NULL
)
271 start_token
= buffer
->address ();
273 if (start_token
> buffer
->address ())
275 cp_lexer_print_token (file
, &(*buffer
)[0]);
276 fprintf (file
, " ... ");
281 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
283 if (token
== start_token
)
290 if (token
== curr_token
)
291 fprintf (file
, "[[");
293 cp_lexer_print_token (file
, token
);
295 if (token
== curr_token
)
296 fprintf (file
, "]]");
302 case CPP_CLOSE_BRACE
:
312 if (i
== num
&& i
< buffer
->length ())
314 fprintf (file
, " ... ");
315 cp_lexer_print_token (file
, &buffer
->last ());
318 fprintf (file
, "\n");
322 /* Dump all tokens in BUFFER to stderr. */
325 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
327 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
331 debug (vec
<cp_token
, va_gc
> &ref
)
333 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
337 debug (vec
<cp_token
, va_gc
> *ptr
)
342 fprintf (stderr
, "<nil>\n");
346 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
347 description for T. */
350 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
354 fprintf (file
, "%s: ", desc
);
355 print_node_brief (file
, "", t
, 0);
360 /* Dump parser context C to FILE. */
363 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
365 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
366 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
367 print_node_brief (file
, "", c
->object_type
, 0);
368 fprintf (file
, "}\n");
372 /* Print the stack of parsing contexts to FILE starting with FIRST. */
375 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
378 cp_parser_context
*c
;
380 fprintf (file
, "Parsing context stack:\n");
381 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
383 fprintf (file
, "\t#%u: ", i
);
384 cp_debug_print_context (file
, c
);
389 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
392 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
395 fprintf (file
, "%s: true\n", desc
);
399 /* Print an unparsed function entry UF to FILE. */
402 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
405 cp_default_arg_entry
*default_arg_fn
;
408 fprintf (file
, "\tFunctions with default args:\n");
410 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
413 fprintf (file
, "\t\tClass type: ");
414 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
415 fprintf (file
, "\t\tDeclaration: ");
416 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
417 fprintf (file
, "\n");
420 fprintf (file
, "\n\tFunctions with definitions that require "
421 "post-processing\n\t\t");
422 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
424 print_node_brief (file
, "", fn
, 0);
427 fprintf (file
, "\n");
429 fprintf (file
, "\n\tNon-static data members with initializers that require "
430 "post-processing\n\t\t");
431 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
433 print_node_brief (file
, "", fn
, 0);
436 fprintf (file
, "\n");
440 /* Print the stack of unparsed member functions S to FILE. */
443 cp_debug_print_unparsed_queues (FILE *file
,
444 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
447 cp_unparsed_functions_entry
*uf
;
449 fprintf (file
, "Unparsed functions\n");
450 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
452 fprintf (file
, "#%u:\n", i
);
453 cp_debug_print_unparsed_function (file
, uf
);
458 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
459 the given PARSER. If FILE is NULL, the output is printed on stderr. */
462 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
464 cp_token
*next_token
, *first_token
, *start_token
;
469 next_token
= parser
->lexer
->next_token
;
470 first_token
= parser
->lexer
->buffer
->address ();
471 start_token
= (next_token
> first_token
+ window_size
/ 2)
472 ? next_token
- window_size
/ 2
474 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
479 /* Dump debugging information for the given PARSER. If FILE is NULL,
480 the output is printed on stderr. */
483 cp_debug_parser (FILE *file
, cp_parser
*parser
)
485 const size_t window_size
= 20;
487 expanded_location eloc
;
492 fprintf (file
, "Parser state\n\n");
493 fprintf (file
, "Number of tokens: %u\n",
494 vec_safe_length (parser
->lexer
->buffer
));
495 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
496 cp_debug_print_tree_if_set (file
, "Object scope",
497 parser
->object_scope
);
498 cp_debug_print_tree_if_set (file
, "Qualifying scope",
499 parser
->qualifying_scope
);
500 cp_debug_print_context_stack (file
, parser
->context
);
501 cp_debug_print_flag (file
, "Allow GNU extensions",
502 parser
->allow_gnu_extensions_p
);
503 cp_debug_print_flag (file
, "'>' token is greater-than",
504 parser
->greater_than_is_operator_p
);
505 cp_debug_print_flag (file
, "Default args allowed in current "
506 "parameter list", parser
->default_arg_ok_p
);
507 cp_debug_print_flag (file
, "Parsing integral constant-expression",
508 parser
->integral_constant_expression_p
);
509 cp_debug_print_flag (file
, "Allow non-constant expression in current "
510 "constant-expression",
511 parser
->allow_non_integral_constant_expression_p
);
512 cp_debug_print_flag (file
, "Seen non-constant expression",
513 parser
->non_integral_constant_expression_p
);
514 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
516 parser
->local_variables_forbidden_p
);
517 cp_debug_print_flag (file
, "In unbraced linkage specification",
518 parser
->in_unbraced_linkage_specification_p
);
519 cp_debug_print_flag (file
, "Parsing a declarator",
520 parser
->in_declarator_p
);
521 cp_debug_print_flag (file
, "In template argument list",
522 parser
->in_template_argument_list_p
);
523 cp_debug_print_flag (file
, "Parsing an iteration statement",
524 parser
->in_statement
& IN_ITERATION_STMT
);
525 cp_debug_print_flag (file
, "Parsing a switch statement",
526 parser
->in_statement
& IN_SWITCH_STMT
);
527 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
528 parser
->in_statement
& IN_OMP_BLOCK
);
529 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
530 parser
->in_statement
& IN_OMP_FOR
);
531 cp_debug_print_flag (file
, "Parsing an if statement",
532 parser
->in_statement
& IN_IF_STMT
);
533 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
534 "context", parser
->in_type_id_in_expr_p
);
535 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
536 parser
->implicit_extern_c
);
537 cp_debug_print_flag (file
, "String expressions should be translated "
538 "to execution character set",
539 parser
->translate_strings_p
);
540 cp_debug_print_flag (file
, "Parsing function body outside of a "
541 "local class", parser
->in_function_body
);
542 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
543 parser
->colon_corrects_to_scope_p
);
544 if (parser
->type_definition_forbidden_message
)
545 fprintf (file
, "Error message for forbidden type definitions: %s\n",
546 parser
->type_definition_forbidden_message
);
547 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
548 fprintf (file
, "Number of class definitions in progress: %u\n",
549 parser
->num_classes_being_defined
);
550 fprintf (file
, "Number of template parameter lists for the current "
551 "declaration: %u\n", parser
->num_template_parameter_lists
);
552 cp_debug_parser_tokens (file
, parser
, window_size
);
553 token
= parser
->lexer
->next_token
;
554 fprintf (file
, "Next token to parse:\n");
555 fprintf (file
, "\tToken: ");
556 cp_lexer_print_token (file
, token
);
557 eloc
= expand_location (token
->location
);
558 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
559 fprintf (file
, "\tLine: %d\n", eloc
.line
);
560 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
564 debug (cp_parser
&ref
)
566 cp_debug_parser (stderr
, &ref
);
570 debug (cp_parser
*ptr
)
575 fprintf (stderr
, "<nil>\n");
578 /* Allocate memory for a new lexer object and return it. */
581 cp_lexer_alloc (void)
585 c_common_no_more_pch ();
587 /* Allocate the memory. */
588 lexer
= ggc_alloc_cleared_cp_lexer ();
590 /* Initially we are not debugging. */
591 lexer
->debugging_p
= false;
593 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
595 /* Create the buffer. */
596 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
602 /* Create a new main C++ lexer, the lexer that gets tokens from the
606 cp_lexer_new_main (void)
611 /* It's possible that parsing the first pragma will load a PCH file,
612 which is a GC collection point. So we have to do that before
613 allocating any memory. */
614 cp_parser_initial_pragma (&token
);
616 lexer
= cp_lexer_alloc ();
618 /* Put the first token in the buffer. */
619 lexer
->buffer
->quick_push (token
);
621 /* Get the remaining tokens from the preprocessor. */
622 while (token
.type
!= CPP_EOF
)
624 cp_lexer_get_preprocessor_token (lexer
, &token
);
625 vec_safe_push (lexer
->buffer
, token
);
628 lexer
->last_token
= lexer
->buffer
->address ()
629 + lexer
->buffer
->length ()
631 lexer
->next_token
= lexer
->buffer
->length ()
632 ? lexer
->buffer
->address ()
635 /* Subsequent preprocessor diagnostics should use compiler
636 diagnostic functions to get the compiler source location. */
639 gcc_assert (!lexer
->next_token
->purged_p
);
643 /* Create a new lexer whose token stream is primed with the tokens in
644 CACHE. When these tokens are exhausted, no new tokens will be read. */
647 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
649 cp_token
*first
= cache
->first
;
650 cp_token
*last
= cache
->last
;
651 cp_lexer
*lexer
= ggc_alloc_cleared_cp_lexer ();
653 /* We do not own the buffer. */
654 lexer
->buffer
= NULL
;
655 lexer
->next_token
= first
== last
? &eof_token
: first
;
656 lexer
->last_token
= last
;
658 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
660 /* Initially we are not debugging. */
661 lexer
->debugging_p
= false;
663 gcc_assert (!lexer
->next_token
->purged_p
);
667 /* Frees all resources associated with LEXER. */
670 cp_lexer_destroy (cp_lexer
*lexer
)
672 vec_free (lexer
->buffer
);
673 lexer
->saved_tokens
.release ();
677 /* Returns nonzero if debugging information should be output. */
680 cp_lexer_debugging_p (cp_lexer
*lexer
)
682 return lexer
->debugging_p
;
686 static inline cp_token_position
687 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
689 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
691 return lexer
->next_token
- previous_p
;
694 static inline cp_token
*
695 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
701 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
703 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
706 static inline cp_token_position
707 cp_lexer_previous_token_position (cp_lexer
*lexer
)
709 if (lexer
->next_token
== &eof_token
)
710 return lexer
->last_token
- 1;
712 return cp_lexer_token_position (lexer
, true);
715 static inline cp_token
*
716 cp_lexer_previous_token (cp_lexer
*lexer
)
718 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
720 return cp_lexer_token_at (lexer
, tp
);
723 /* nonzero if we are presently saving tokens. */
726 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
728 return lexer
->saved_tokens
.length () != 0;
731 /* Store the next token from the preprocessor in *TOKEN. Return true
732 if we reach EOF. If LEXER is NULL, assume we are handling an
733 initial #pragma pch_preprocess, and thus want the lexer to return
734 processed strings. */
737 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
739 static int is_extern_c
= 0;
741 /* Get a new token from the preprocessor. */
743 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
744 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
745 token
->keyword
= RID_MAX
;
746 token
->pragma_kind
= PRAGMA_NONE
;
747 token
->purged_p
= false;
749 /* On some systems, some header files are surrounded by an
750 implicit extern "C" block. Set a flag in the token if it
751 comes from such a header. */
752 is_extern_c
+= pending_lang_change
;
753 pending_lang_change
= 0;
754 token
->implicit_extern_c
= is_extern_c
> 0;
756 /* Check to see if this token is a keyword. */
757 if (token
->type
== CPP_NAME
)
759 if (C_IS_RESERVED_WORD (token
->u
.value
))
761 /* Mark this token as a keyword. */
762 token
->type
= CPP_KEYWORD
;
763 /* Record which keyword. */
764 token
->keyword
= C_RID_CODE (token
->u
.value
);
768 if (warn_cxx0x_compat
769 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
770 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
772 /* Warn about the C++0x keyword (but still treat it as
774 warning (OPT_Wc__0x_compat
,
775 "identifier %qE is a keyword in C++11",
778 /* Clear out the C_RID_CODE so we don't warn about this
779 particular identifier-turned-keyword again. */
780 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
783 token
->ambiguous_p
= false;
784 token
->keyword
= RID_MAX
;
787 else if (token
->type
== CPP_AT_NAME
)
789 /* This only happens in Objective-C++; it must be a keyword. */
790 token
->type
= CPP_KEYWORD
;
791 switch (C_RID_CODE (token
->u
.value
))
793 /* Replace 'class' with '@class', 'private' with '@private',
794 etc. This prevents confusion with the C++ keyword
795 'class', and makes the tokens consistent with other
796 Objective-C 'AT' keywords. For example '@class' is
797 reported as RID_AT_CLASS which is consistent with
798 '@synchronized', which is reported as
801 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
802 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
803 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
804 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
805 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
806 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
807 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
808 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
811 else if (token
->type
== CPP_PRAGMA
)
813 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
814 token
->pragma_kind
= ((enum pragma_kind
)
815 TREE_INT_CST_LOW (token
->u
.value
));
816 token
->u
.value
= NULL_TREE
;
820 /* Update the globals input_location and the input file stack from TOKEN. */
822 cp_lexer_set_source_position_from_token (cp_token
*token
)
824 if (token
->type
!= CPP_EOF
)
826 input_location
= token
->location
;
830 /* Return a pointer to the next token in the token stream, but do not
833 static inline cp_token
*
834 cp_lexer_peek_token (cp_lexer
*lexer
)
836 if (cp_lexer_debugging_p (lexer
))
838 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
839 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
840 putc ('\n', cp_lexer_debug_stream
);
842 return lexer
->next_token
;
845 /* Return true if the next token has the indicated TYPE. */
848 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
850 return cp_lexer_peek_token (lexer
)->type
== type
;
853 /* Return true if the next token does not have the indicated TYPE. */
856 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
858 return !cp_lexer_next_token_is (lexer
, type
);
861 /* Return true if the next token is the indicated KEYWORD. */
864 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
866 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
870 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
872 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
875 /* Return true if the next token is not the indicated KEYWORD. */
878 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
880 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
883 /* Return true if the next token is a keyword for a decl-specifier. */
886 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
890 token
= cp_lexer_peek_token (lexer
);
891 switch (token
->keyword
)
893 /* auto specifier: storage-class-specifier in C++,
894 simple-type-specifier in C++0x. */
896 /* Storage classes. */
902 /* Elaborated type specifiers. */
908 /* Simple type specifiers. */
923 /* GNU extensions. */
926 /* C++0x extensions. */
928 case RID_UNDERLYING_TYPE
:
936 /* Returns TRUE iff the token T begins a decltype type. */
939 token_is_decltype (cp_token
*t
)
941 return (t
->keyword
== RID_DECLTYPE
942 || t
->type
== CPP_DECLTYPE
);
945 /* Returns TRUE iff the next token begins a decltype type. */
948 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
950 cp_token
*t
= cp_lexer_peek_token (lexer
);
951 return token_is_decltype (t
);
954 /* Return a pointer to the Nth token in the token stream. If N is 1,
955 then this is precisely equivalent to cp_lexer_peek_token (except
956 that it is not inline). One would like to disallow that case, but
957 there is one case (cp_parser_nth_token_starts_template_id) where
958 the caller passes a variable for N and it might be 1. */
961 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
965 /* N is 1-based, not zero-based. */
968 if (cp_lexer_debugging_p (lexer
))
969 fprintf (cp_lexer_debug_stream
,
970 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
973 token
= lexer
->next_token
;
974 gcc_assert (!n
|| token
!= &eof_token
);
978 if (token
== lexer
->last_token
)
984 if (!token
->purged_p
)
988 if (cp_lexer_debugging_p (lexer
))
990 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
991 putc ('\n', cp_lexer_debug_stream
);
997 /* Return the next token, and advance the lexer's next_token pointer
998 to point to the next non-purged token. */
1001 cp_lexer_consume_token (cp_lexer
* lexer
)
1003 cp_token
*token
= lexer
->next_token
;
1005 gcc_assert (token
!= &eof_token
);
1006 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1010 lexer
->next_token
++;
1011 if (lexer
->next_token
== lexer
->last_token
)
1013 lexer
->next_token
= &eof_token
;
1018 while (lexer
->next_token
->purged_p
);
1020 cp_lexer_set_source_position_from_token (token
);
1022 /* Provide debugging output. */
1023 if (cp_lexer_debugging_p (lexer
))
1025 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1026 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1027 putc ('\n', cp_lexer_debug_stream
);
1033 /* Permanently remove the next token from the token stream, and
1034 advance the next_token pointer to refer to the next non-purged
1038 cp_lexer_purge_token (cp_lexer
*lexer
)
1040 cp_token
*tok
= lexer
->next_token
;
1042 gcc_assert (tok
!= &eof_token
);
1043 tok
->purged_p
= true;
1044 tok
->location
= UNKNOWN_LOCATION
;
1045 tok
->u
.value
= NULL_TREE
;
1046 tok
->keyword
= RID_MAX
;
1051 if (tok
== lexer
->last_token
)
1057 while (tok
->purged_p
);
1058 lexer
->next_token
= tok
;
1061 /* Permanently remove all tokens after TOK, up to, but not
1062 including, the token that will be returned next by
1063 cp_lexer_peek_token. */
1066 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1068 cp_token
*peek
= lexer
->next_token
;
1070 if (peek
== &eof_token
)
1071 peek
= lexer
->last_token
;
1073 gcc_assert (tok
< peek
);
1075 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1077 tok
->purged_p
= true;
1078 tok
->location
= UNKNOWN_LOCATION
;
1079 tok
->u
.value
= NULL_TREE
;
1080 tok
->keyword
= RID_MAX
;
1084 /* Begin saving tokens. All tokens consumed after this point will be
1088 cp_lexer_save_tokens (cp_lexer
* lexer
)
1090 /* Provide debugging output. */
1091 if (cp_lexer_debugging_p (lexer
))
1092 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1094 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1097 /* Commit to the portion of the token stream most recently saved. */
1100 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1102 /* Provide debugging output. */
1103 if (cp_lexer_debugging_p (lexer
))
1104 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1106 lexer
->saved_tokens
.pop ();
1109 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1110 to the token stream. Stop saving tokens. */
1113 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer
))
1117 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1119 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1122 /* Print a representation of the TOKEN on the STREAM. */
1125 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1127 /* We don't use cpp_type2name here because the parser defines
1128 a few tokens of its own. */
1129 static const char *const token_names
[] = {
1130 /* cpplib-defined token types */
1131 #define OP(e, s) #e,
1132 #define TK(e, s) #e,
1136 /* C++ parser token types - see "Manifest constants", above. */
1139 "NESTED_NAME_SPECIFIER",
1142 /* For some tokens, print the associated data. */
1143 switch (token
->type
)
1146 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1147 For example, `struct' is mapped to an INTEGER_CST. */
1148 if (!identifier_p (token
->u
.value
))
1150 /* else fall through */
1152 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1159 case CPP_UTF8STRING
:
1160 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1164 print_generic_expr (stream
, token
->u
.value
, 0);
1168 /* If we have a name for the token, print it out. Otherwise, we
1169 simply give the numeric code. */
1170 if (token
->type
< ARRAY_SIZE(token_names
))
1171 fputs (token_names
[token
->type
], stream
);
1173 fprintf (stream
, "[%d]", token
->type
);
1179 debug (cp_token
&ref
)
1181 cp_lexer_print_token (stderr
, &ref
);
1182 fprintf (stderr
, "\n");
1186 debug (cp_token
*ptr
)
1191 fprintf (stderr
, "<nil>\n");
1195 /* Start emitting debugging information. */
1198 cp_lexer_start_debugging (cp_lexer
* lexer
)
1200 lexer
->debugging_p
= true;
1201 cp_lexer_debug_stream
= stderr
;
1204 /* Stop emitting debugging information. */
1207 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1209 lexer
->debugging_p
= false;
1210 cp_lexer_debug_stream
= NULL
;
1213 /* Create a new cp_token_cache, representing a range of tokens. */
1215 static cp_token_cache
*
1216 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1218 cp_token_cache
*cache
= ggc_alloc_cp_token_cache ();
1219 cache
->first
= first
;
1225 /* Decl-specifiers. */
1227 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1230 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1232 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1237 /* Nothing other than the parser should be creating declarators;
1238 declarators are a semi-syntactic representation of C++ entities.
1239 Other parts of the front end that need to create entities (like
1240 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1242 static cp_declarator
*make_call_declarator
1243 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1244 static cp_declarator
*make_array_declarator
1245 (cp_declarator
*, tree
);
1246 static cp_declarator
*make_pointer_declarator
1247 (cp_cv_quals
, cp_declarator
*, tree
);
1248 static cp_declarator
*make_reference_declarator
1249 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1250 static cp_parameter_declarator
*make_parameter_declarator
1251 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1252 static cp_declarator
*make_ptrmem_declarator
1253 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1255 /* An erroneous declarator. */
1256 static cp_declarator
*cp_error_declarator
;
1258 /* The obstack on which declarators and related data structures are
1260 static struct obstack declarator_obstack
;
1262 /* Alloc BYTES from the declarator memory pool. */
1264 static inline void *
1265 alloc_declarator (size_t bytes
)
1267 return obstack_alloc (&declarator_obstack
, bytes
);
1270 /* Allocate a declarator of the indicated KIND. Clear fields that are
1271 common to all declarators. */
1273 static cp_declarator
*
1274 make_declarator (cp_declarator_kind kind
)
1276 cp_declarator
*declarator
;
1278 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1279 declarator
->kind
= kind
;
1280 declarator
->attributes
= NULL_TREE
;
1281 declarator
->std_attributes
= NULL_TREE
;
1282 declarator
->declarator
= NULL
;
1283 declarator
->parameter_pack_p
= false;
1284 declarator
->id_loc
= UNKNOWN_LOCATION
;
1289 /* Make a declarator for a generalized identifier. If
1290 QUALIFYING_SCOPE is non-NULL, the identifier is
1291 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1292 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1295 static cp_declarator
*
1296 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1297 special_function_kind sfk
)
1299 cp_declarator
*declarator
;
1301 /* It is valid to write:
1303 class C { void f(); };
1307 The standard is not clear about whether `typedef const C D' is
1308 legal; as of 2002-09-15 the committee is considering that
1309 question. EDG 3.0 allows that syntax. Therefore, we do as
1311 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1312 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1314 gcc_assert (identifier_p (unqualified_name
)
1315 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1316 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1318 declarator
= make_declarator (cdk_id
);
1319 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1320 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1321 declarator
->u
.id
.sfk
= sfk
;
1326 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1327 of modifiers such as const or volatile to apply to the pointer
1328 type, represented as identifiers. ATTRIBUTES represent the attributes that
1329 appertain to the pointer or reference. */
1332 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1335 cp_declarator
*declarator
;
1337 declarator
= make_declarator (cdk_pointer
);
1338 declarator
->declarator
= target
;
1339 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1340 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1343 declarator
->id_loc
= target
->id_loc
;
1344 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1345 target
->parameter_pack_p
= false;
1348 declarator
->parameter_pack_p
= false;
1350 declarator
->std_attributes
= attributes
;
1355 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1356 represent the attributes that appertain to the pointer or
1360 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1361 bool rvalue_ref
, tree attributes
)
1363 cp_declarator
*declarator
;
1365 declarator
= make_declarator (cdk_reference
);
1366 declarator
->declarator
= target
;
1367 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1368 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1371 declarator
->id_loc
= target
->id_loc
;
1372 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1373 target
->parameter_pack_p
= false;
1376 declarator
->parameter_pack_p
= false;
1378 declarator
->std_attributes
= attributes
;
1383 /* Like make_pointer_declarator -- but for a pointer to a non-static
1384 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1385 appertain to the pointer or reference. */
1388 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1389 cp_declarator
*pointee
,
1392 cp_declarator
*declarator
;
1394 declarator
= make_declarator (cdk_ptrmem
);
1395 declarator
->declarator
= pointee
;
1396 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1397 declarator
->u
.pointer
.class_type
= class_type
;
1401 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1402 pointee
->parameter_pack_p
= false;
1405 declarator
->parameter_pack_p
= false;
1407 declarator
->std_attributes
= attributes
;
1412 /* Make a declarator for the function given by TARGET, with the
1413 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1414 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1415 indicates what exceptions can be thrown. */
1418 make_call_declarator (cp_declarator
*target
,
1420 cp_cv_quals cv_qualifiers
,
1421 cp_virt_specifiers virt_specifiers
,
1422 cp_ref_qualifier ref_qualifier
,
1423 tree exception_specification
,
1424 tree late_return_type
)
1426 cp_declarator
*declarator
;
1428 declarator
= make_declarator (cdk_function
);
1429 declarator
->declarator
= target
;
1430 declarator
->u
.function
.parameters
= parms
;
1431 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1432 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1433 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1434 declarator
->u
.function
.exception_specification
= exception_specification
;
1435 declarator
->u
.function
.late_return_type
= late_return_type
;
1438 declarator
->id_loc
= target
->id_loc
;
1439 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1440 target
->parameter_pack_p
= false;
1443 declarator
->parameter_pack_p
= false;
1448 /* Make a declarator for an array of BOUNDS elements, each of which is
1449 defined by ELEMENT. */
1452 make_array_declarator (cp_declarator
*element
, tree bounds
)
1454 cp_declarator
*declarator
;
1456 declarator
= make_declarator (cdk_array
);
1457 declarator
->declarator
= element
;
1458 declarator
->u
.array
.bounds
= bounds
;
1461 declarator
->id_loc
= element
->id_loc
;
1462 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1463 element
->parameter_pack_p
= false;
1466 declarator
->parameter_pack_p
= false;
1471 /* Determine whether the declarator we've seen so far can be a
1472 parameter pack, when followed by an ellipsis. */
1474 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1476 /* Search for a declarator name, or any other declarator that goes
1477 after the point where the ellipsis could appear in a parameter
1478 pack. If we find any of these, then this declarator can not be
1479 made into a parameter pack. */
1481 while (declarator
&& !found
)
1483 switch ((int)declarator
->kind
)
1494 declarator
= declarator
->declarator
;
1502 cp_parameter_declarator
*no_parameters
;
1504 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1505 DECLARATOR and DEFAULT_ARGUMENT. */
1507 cp_parameter_declarator
*
1508 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1509 cp_declarator
*declarator
,
1510 tree default_argument
)
1512 cp_parameter_declarator
*parameter
;
1514 parameter
= ((cp_parameter_declarator
*)
1515 alloc_declarator (sizeof (cp_parameter_declarator
)));
1516 parameter
->next
= NULL
;
1517 if (decl_specifiers
)
1518 parameter
->decl_specifiers
= *decl_specifiers
;
1520 clear_decl_specs (¶meter
->decl_specifiers
);
1521 parameter
->declarator
= declarator
;
1522 parameter
->default_argument
= default_argument
;
1523 parameter
->ellipsis_p
= false;
1528 /* Returns true iff DECLARATOR is a declaration for a function. */
1531 function_declarator_p (const cp_declarator
*declarator
)
1535 if (declarator
->kind
== cdk_function
1536 && declarator
->declarator
->kind
== cdk_id
)
1538 if (declarator
->kind
== cdk_id
1539 || declarator
->kind
== cdk_error
)
1541 declarator
= declarator
->declarator
;
1551 A cp_parser parses the token stream as specified by the C++
1552 grammar. Its job is purely parsing, not semantic analysis. For
1553 example, the parser breaks the token stream into declarators,
1554 expressions, statements, and other similar syntactic constructs.
1555 It does not check that the types of the expressions on either side
1556 of an assignment-statement are compatible, or that a function is
1557 not declared with a parameter of type `void'.
1559 The parser invokes routines elsewhere in the compiler to perform
1560 semantic analysis and to build up the abstract syntax tree for the
1563 The parser (and the template instantiation code, which is, in a
1564 way, a close relative of parsing) are the only parts of the
1565 compiler that should be calling push_scope and pop_scope, or
1566 related functions. The parser (and template instantiation code)
1567 keeps track of what scope is presently active; everything else
1568 should simply honor that. (The code that generates static
1569 initializers may also need to set the scope, in order to check
1570 access control correctly when emitting the initializers.)
1575 The parser is of the standard recursive-descent variety. Upcoming
1576 tokens in the token stream are examined in order to determine which
1577 production to use when parsing a non-terminal. Some C++ constructs
1578 require arbitrary look ahead to disambiguate. For example, it is
1579 impossible, in the general case, to tell whether a statement is an
1580 expression or declaration without scanning the entire statement.
1581 Therefore, the parser is capable of "parsing tentatively." When the
1582 parser is not sure what construct comes next, it enters this mode.
1583 Then, while we attempt to parse the construct, the parser queues up
1584 error messages, rather than issuing them immediately, and saves the
1585 tokens it consumes. If the construct is parsed successfully, the
1586 parser "commits", i.e., it issues any queued error messages and
1587 the tokens that were being preserved are permanently discarded.
1588 If, however, the construct is not parsed successfully, the parser
1589 rolls back its state completely so that it can resume parsing using
1590 a different alternative.
1595 The performance of the parser could probably be improved substantially.
1596 We could often eliminate the need to parse tentatively by looking ahead
1597 a little bit. In some places, this approach might not entirely eliminate
1598 the need to parse tentatively, but it might still speed up the average
1601 /* Flags that are passed to some parsing functions. These values can
1602 be bitwise-ored together. */
1607 CP_PARSER_FLAGS_NONE
= 0x0,
1608 /* The construct is optional. If it is not present, then no error
1609 should be issued. */
1610 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1611 /* When parsing a type-specifier, treat user-defined type-names
1612 as non-type identifiers. */
1613 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1614 /* When parsing a type-specifier, do not try to parse a class-specifier
1615 or enum-specifier. */
1616 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1617 /* When parsing a decl-specifier-seq, only allow type-specifier or
1619 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1622 /* This type is used for parameters and variables which hold
1623 combinations of the above flags. */
1624 typedef int cp_parser_flags
;
1626 /* The different kinds of declarators we want to parse. */
1628 typedef enum cp_parser_declarator_kind
1630 /* We want an abstract declarator. */
1631 CP_PARSER_DECLARATOR_ABSTRACT
,
1632 /* We want a named declarator. */
1633 CP_PARSER_DECLARATOR_NAMED
,
1634 /* We don't mind, but the name must be an unqualified-id. */
1635 CP_PARSER_DECLARATOR_EITHER
1636 } cp_parser_declarator_kind
;
1638 /* The precedence values used to parse binary expressions. The minimum value
1639 of PREC must be 1, because zero is reserved to quickly discriminate
1640 binary operators from other tokens. */
1645 PREC_LOGICAL_OR_EXPRESSION
,
1646 PREC_LOGICAL_AND_EXPRESSION
,
1647 PREC_INCLUSIVE_OR_EXPRESSION
,
1648 PREC_EXCLUSIVE_OR_EXPRESSION
,
1649 PREC_AND_EXPRESSION
,
1650 PREC_EQUALITY_EXPRESSION
,
1651 PREC_RELATIONAL_EXPRESSION
,
1652 PREC_SHIFT_EXPRESSION
,
1653 PREC_ADDITIVE_EXPRESSION
,
1654 PREC_MULTIPLICATIVE_EXPRESSION
,
1656 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1659 /* A mapping from a token type to a corresponding tree node type, with a
1660 precedence value. */
1662 typedef struct cp_parser_binary_operations_map_node
1664 /* The token type. */
1665 enum cpp_ttype token_type
;
1666 /* The corresponding tree code. */
1667 enum tree_code tree_type
;
1668 /* The precedence of this operator. */
1669 enum cp_parser_prec prec
;
1670 } cp_parser_binary_operations_map_node
;
1672 typedef struct cp_parser_expression_stack_entry
1674 /* Left hand side of the binary operation we are currently
1677 /* Original tree code for left hand side, if it was a binary
1678 expression itself (used for -Wparentheses). */
1679 enum tree_code lhs_type
;
1680 /* Tree code for the binary operation we are parsing. */
1681 enum tree_code tree_type
;
1682 /* Precedence of the binary operation we are parsing. */
1683 enum cp_parser_prec prec
;
1684 /* Location of the binary operation we are parsing. */
1686 } cp_parser_expression_stack_entry
;
1688 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1689 entries because precedence levels on the stack are monotonically
1691 typedef struct cp_parser_expression_stack_entry
1692 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1696 /* Constructors and destructors. */
1698 static cp_parser_context
*cp_parser_context_new
1699 (cp_parser_context
*);
1701 /* Class variables. */
1703 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1705 /* The operator-precedence table used by cp_parser_binary_expression.
1706 Transformed into an associative array (binops_by_token) by
1709 static const cp_parser_binary_operations_map_node binops
[] = {
1710 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1711 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1713 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1714 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1715 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1717 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1718 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1720 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1721 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1723 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1724 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1725 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1726 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1728 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1729 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1731 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1733 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1735 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1737 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1739 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1742 /* The same as binops, but initialized by cp_parser_new so that
1743 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1745 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1747 /* Constructors and destructors. */
1749 /* Construct a new context. The context below this one on the stack
1750 is given by NEXT. */
1752 static cp_parser_context
*
1753 cp_parser_context_new (cp_parser_context
* next
)
1755 cp_parser_context
*context
;
1757 /* Allocate the storage. */
1758 if (cp_parser_context_free_list
!= NULL
)
1760 /* Pull the first entry from the free list. */
1761 context
= cp_parser_context_free_list
;
1762 cp_parser_context_free_list
= context
->next
;
1763 memset (context
, 0, sizeof (*context
));
1766 context
= ggc_alloc_cleared_cp_parser_context ();
1768 /* No errors have occurred yet in this context. */
1769 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1770 /* If this is not the bottommost context, copy information that we
1771 need from the previous context. */
1774 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1775 expression, then we are parsing one in this context, too. */
1776 context
->object_type
= next
->object_type
;
1777 /* Thread the stack. */
1778 context
->next
= next
;
1784 /* Managing the unparsed function queues. */
1786 #define unparsed_funs_with_default_args \
1787 parser->unparsed_queues->last ().funs_with_default_args
1788 #define unparsed_funs_with_definitions \
1789 parser->unparsed_queues->last ().funs_with_definitions
1790 #define unparsed_nsdmis \
1791 parser->unparsed_queues->last ().nsdmis
1794 push_unparsed_function_queues (cp_parser
*parser
)
1796 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
};
1797 vec_safe_push (parser
->unparsed_queues
, e
);
1801 pop_unparsed_function_queues (cp_parser
*parser
)
1803 release_tree_vector (unparsed_funs_with_definitions
);
1804 parser
->unparsed_queues
->pop ();
1809 /* Constructors and destructors. */
1811 static cp_parser
*cp_parser_new
1814 /* Routines to parse various constructs.
1816 Those that return `tree' will return the error_mark_node (rather
1817 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1818 Sometimes, they will return an ordinary node if error-recovery was
1819 attempted, even though a parse error occurred. So, to check
1820 whether or not a parse error occurred, you should always use
1821 cp_parser_error_occurred. If the construct is optional (indicated
1822 either by an `_opt' in the name of the function that does the
1823 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1824 the construct is not present. */
1826 /* Lexical conventions [gram.lex] */
1828 static tree cp_parser_identifier
1830 static tree cp_parser_string_literal
1831 (cp_parser
*, bool, bool);
1832 static tree cp_parser_userdef_char_literal
1834 static tree cp_parser_userdef_string_literal
1836 static tree cp_parser_userdef_numeric_literal
1839 /* Basic concepts [gram.basic] */
1841 static bool cp_parser_translation_unit
1844 /* Expressions [gram.expr] */
1846 static tree cp_parser_primary_expression
1847 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1848 static tree cp_parser_id_expression
1849 (cp_parser
*, bool, bool, bool *, bool, bool);
1850 static tree cp_parser_unqualified_id
1851 (cp_parser
*, bool, bool, bool, bool);
1852 static tree cp_parser_nested_name_specifier_opt
1853 (cp_parser
*, bool, bool, bool, bool);
1854 static tree cp_parser_nested_name_specifier
1855 (cp_parser
*, bool, bool, bool, bool);
1856 static tree cp_parser_qualifying_entity
1857 (cp_parser
*, bool, bool, bool, bool, bool);
1858 static tree cp_parser_postfix_expression
1859 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1860 static tree cp_parser_postfix_open_square_expression
1861 (cp_parser
*, tree
, bool, bool);
1862 static tree cp_parser_postfix_dot_deref_expression
1863 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1864 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1865 (cp_parser
*, int, bool, bool, bool *);
1866 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1867 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1868 static void cp_parser_pseudo_destructor_name
1869 (cp_parser
*, tree
, tree
*, tree
*);
1870 static tree cp_parser_unary_expression
1871 (cp_parser
*, bool, bool, cp_id_kind
*);
1872 static enum tree_code cp_parser_unary_operator
1874 static tree cp_parser_new_expression
1876 static vec
<tree
, va_gc
> *cp_parser_new_placement
1878 static tree cp_parser_new_type_id
1879 (cp_parser
*, tree
*);
1880 static cp_declarator
*cp_parser_new_declarator_opt
1882 static cp_declarator
*cp_parser_direct_new_declarator
1884 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1886 static tree cp_parser_delete_expression
1888 static tree cp_parser_cast_expression
1889 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1890 static tree cp_parser_binary_expression
1891 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1892 static tree cp_parser_question_colon_clause
1893 (cp_parser
*, tree
);
1894 static tree cp_parser_assignment_expression
1895 (cp_parser
*, bool, cp_id_kind
*);
1896 static enum tree_code cp_parser_assignment_operator_opt
1898 static tree cp_parser_expression
1899 (cp_parser
*, bool, cp_id_kind
*);
1900 static tree cp_parser_expression
1901 (cp_parser
*, bool, bool, cp_id_kind
*);
1902 static tree cp_parser_constant_expression
1903 (cp_parser
*, bool, bool *);
1904 static tree cp_parser_builtin_offsetof
1906 static tree cp_parser_lambda_expression
1908 static void cp_parser_lambda_introducer
1909 (cp_parser
*, tree
);
1910 static bool cp_parser_lambda_declarator_opt
1911 (cp_parser
*, tree
);
1912 static void cp_parser_lambda_body
1913 (cp_parser
*, tree
);
1915 /* Statements [gram.stmt.stmt] */
1917 static void cp_parser_statement
1918 (cp_parser
*, tree
, bool, bool *);
1919 static void cp_parser_label_for_labeled_statement
1920 (cp_parser
*, tree
);
1921 static tree cp_parser_expression_statement
1922 (cp_parser
*, tree
);
1923 static tree cp_parser_compound_statement
1924 (cp_parser
*, tree
, bool, bool);
1925 static void cp_parser_statement_seq_opt
1926 (cp_parser
*, tree
);
1927 static tree cp_parser_selection_statement
1928 (cp_parser
*, bool *);
1929 static tree cp_parser_condition
1931 static tree cp_parser_iteration_statement
1933 static bool cp_parser_for_init_statement
1934 (cp_parser
*, tree
*decl
);
1935 static tree cp_parser_for
1937 static tree cp_parser_c_for
1938 (cp_parser
*, tree
, tree
);
1939 static tree cp_parser_range_for
1940 (cp_parser
*, tree
, tree
, tree
);
1941 static void do_range_for_auto_deduction
1943 static tree cp_parser_perform_range_for_lookup
1944 (tree
, tree
*, tree
*);
1945 static tree cp_parser_range_for_member_function
1947 static tree cp_parser_jump_statement
1949 static void cp_parser_declaration_statement
1952 static tree cp_parser_implicitly_scoped_statement
1953 (cp_parser
*, bool *);
1954 static void cp_parser_already_scoped_statement
1957 /* Declarations [gram.dcl.dcl] */
1959 static void cp_parser_declaration_seq_opt
1961 static void cp_parser_declaration
1963 static void cp_parser_block_declaration
1964 (cp_parser
*, bool);
1965 static void cp_parser_simple_declaration
1966 (cp_parser
*, bool, tree
*);
1967 static void cp_parser_decl_specifier_seq
1968 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1969 static tree cp_parser_storage_class_specifier_opt
1971 static tree cp_parser_function_specifier_opt
1972 (cp_parser
*, cp_decl_specifier_seq
*);
1973 static tree cp_parser_type_specifier
1974 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1976 static tree cp_parser_simple_type_specifier
1977 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1978 static tree cp_parser_type_name
1980 static tree cp_parser_nonclass_name
1981 (cp_parser
* parser
);
1982 static tree cp_parser_elaborated_type_specifier
1983 (cp_parser
*, bool, bool);
1984 static tree cp_parser_enum_specifier
1986 static void cp_parser_enumerator_list
1987 (cp_parser
*, tree
);
1988 static void cp_parser_enumerator_definition
1989 (cp_parser
*, tree
);
1990 static tree cp_parser_namespace_name
1992 static void cp_parser_namespace_definition
1994 static void cp_parser_namespace_body
1996 static tree cp_parser_qualified_namespace_specifier
1998 static void cp_parser_namespace_alias_definition
2000 static bool cp_parser_using_declaration
2001 (cp_parser
*, bool);
2002 static void cp_parser_using_directive
2004 static tree cp_parser_alias_declaration
2006 static void cp_parser_asm_definition
2008 static void cp_parser_linkage_specification
2010 static void cp_parser_static_assert
2011 (cp_parser
*, bool);
2012 static tree cp_parser_decltype
2015 /* Declarators [gram.dcl.decl] */
2017 static tree cp_parser_init_declarator
2018 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*);
2019 static cp_declarator
*cp_parser_declarator
2020 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
2021 static cp_declarator
*cp_parser_direct_declarator
2022 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
2023 static enum tree_code cp_parser_ptr_operator
2024 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2025 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2027 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2029 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2031 static tree cp_parser_late_return_type_opt
2032 (cp_parser
*, cp_cv_quals
);
2033 static tree cp_parser_declarator_id
2034 (cp_parser
*, bool);
2035 static tree cp_parser_type_id
2037 static tree cp_parser_template_type_arg
2039 static tree
cp_parser_trailing_type_id (cp_parser
*);
2040 static tree cp_parser_type_id_1
2041 (cp_parser
*, bool, bool);
2042 static void cp_parser_type_specifier_seq
2043 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2044 static tree cp_parser_parameter_declaration_clause
2046 static tree cp_parser_parameter_declaration_list
2047 (cp_parser
*, bool *);
2048 static cp_parameter_declarator
*cp_parser_parameter_declaration
2049 (cp_parser
*, bool, bool *);
2050 static tree cp_parser_default_argument
2051 (cp_parser
*, bool);
2052 static void cp_parser_function_body
2053 (cp_parser
*, bool);
2054 static tree cp_parser_initializer
2055 (cp_parser
*, bool *, bool *);
2056 static tree cp_parser_initializer_clause
2057 (cp_parser
*, bool *);
2058 static tree cp_parser_braced_list
2059 (cp_parser
*, bool*);
2060 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2061 (cp_parser
*, bool *);
2063 static bool cp_parser_ctor_initializer_opt_and_function_body
2064 (cp_parser
*, bool);
2066 /* Classes [gram.class] */
2068 static tree cp_parser_class_name
2069 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2070 static tree cp_parser_class_specifier
2072 static tree cp_parser_class_head
2073 (cp_parser
*, bool *);
2074 static enum tag_types cp_parser_class_key
2076 static void cp_parser_member_specification_opt
2078 static void cp_parser_member_declaration
2080 static tree cp_parser_pure_specifier
2082 static tree cp_parser_constant_initializer
2085 /* Derived classes [gram.class.derived] */
2087 static tree cp_parser_base_clause
2089 static tree cp_parser_base_specifier
2092 /* Special member functions [gram.special] */
2094 static tree cp_parser_conversion_function_id
2096 static tree cp_parser_conversion_type_id
2098 static cp_declarator
*cp_parser_conversion_declarator_opt
2100 static bool cp_parser_ctor_initializer_opt
2102 static void cp_parser_mem_initializer_list
2104 static tree cp_parser_mem_initializer
2106 static tree cp_parser_mem_initializer_id
2109 /* Overloading [gram.over] */
2111 static tree cp_parser_operator_function_id
2113 static tree cp_parser_operator
2116 /* Templates [gram.temp] */
2118 static void cp_parser_template_declaration
2119 (cp_parser
*, bool);
2120 static tree cp_parser_template_parameter_list
2122 static tree cp_parser_template_parameter
2123 (cp_parser
*, bool *, bool *);
2124 static tree cp_parser_type_parameter
2125 (cp_parser
*, bool *);
2126 static tree cp_parser_template_id
2127 (cp_parser
*, bool, bool, enum tag_types
, bool);
2128 static tree cp_parser_template_name
2129 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2130 static tree cp_parser_template_argument_list
2132 static tree cp_parser_template_argument
2134 static void cp_parser_explicit_instantiation
2136 static void cp_parser_explicit_specialization
2139 /* Exception handling [gram.exception] */
2141 static tree cp_parser_try_block
2143 static bool cp_parser_function_try_block
2145 static void cp_parser_handler_seq
2147 static void cp_parser_handler
2149 static tree cp_parser_exception_declaration
2151 static tree cp_parser_throw_expression
2153 static tree cp_parser_exception_specification_opt
2155 static tree cp_parser_type_id_list
2158 /* GNU Extensions */
2160 static tree cp_parser_asm_specification_opt
2162 static tree cp_parser_asm_operand_list
2164 static tree cp_parser_asm_clobber_list
2166 static tree cp_parser_asm_label_list
2168 static bool cp_next_tokens_can_be_attribute_p
2170 static bool cp_next_tokens_can_be_gnu_attribute_p
2172 static bool cp_next_tokens_can_be_std_attribute_p
2174 static bool cp_nth_tokens_can_be_std_attribute_p
2175 (cp_parser
*, size_t);
2176 static bool cp_nth_tokens_can_be_gnu_attribute_p
2177 (cp_parser
*, size_t);
2178 static bool cp_nth_tokens_can_be_attribute_p
2179 (cp_parser
*, size_t);
2180 static tree cp_parser_attributes_opt
2182 static tree cp_parser_gnu_attributes_opt
2184 static tree cp_parser_gnu_attribute_list
2186 static tree cp_parser_std_attribute
2188 static tree cp_parser_std_attribute_spec
2190 static tree cp_parser_std_attribute_spec_seq
2192 static bool cp_parser_extension_opt
2193 (cp_parser
*, int *);
2194 static void cp_parser_label_declaration
2197 /* Transactional Memory Extensions */
2199 static tree cp_parser_transaction
2200 (cp_parser
*, enum rid
);
2201 static tree cp_parser_transaction_expression
2202 (cp_parser
*, enum rid
);
2203 static bool cp_parser_function_transaction
2204 (cp_parser
*, enum rid
);
2205 static tree cp_parser_transaction_cancel
2208 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
2209 static bool cp_parser_pragma
2210 (cp_parser
*, enum pragma_context
);
2212 /* Objective-C++ Productions */
2214 static tree cp_parser_objc_message_receiver
2216 static tree cp_parser_objc_message_args
2218 static tree cp_parser_objc_message_expression
2220 static tree cp_parser_objc_encode_expression
2222 static tree cp_parser_objc_defs_expression
2224 static tree cp_parser_objc_protocol_expression
2226 static tree cp_parser_objc_selector_expression
2228 static tree cp_parser_objc_expression
2230 static bool cp_parser_objc_selector_p
2232 static tree cp_parser_objc_selector
2234 static tree cp_parser_objc_protocol_refs_opt
2236 static void cp_parser_objc_declaration
2237 (cp_parser
*, tree
);
2238 static tree cp_parser_objc_statement
2240 static bool cp_parser_objc_valid_prefix_attributes
2241 (cp_parser
*, tree
*);
2242 static void cp_parser_objc_at_property_declaration
2244 static void cp_parser_objc_at_synthesize_declaration
2246 static void cp_parser_objc_at_dynamic_declaration
2248 static tree cp_parser_objc_struct_declaration
2251 /* Utility Routines */
2253 static tree cp_parser_lookup_name
2254 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2255 static tree cp_parser_lookup_name_simple
2256 (cp_parser
*, tree
, location_t
);
2257 static tree cp_parser_maybe_treat_template_as_class
2259 static bool cp_parser_check_declarator_template_parameters
2260 (cp_parser
*, cp_declarator
*, location_t
);
2261 static bool cp_parser_check_template_parameters
2262 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2263 static tree cp_parser_simple_cast_expression
2265 static tree cp_parser_global_scope_opt
2266 (cp_parser
*, bool);
2267 static bool cp_parser_constructor_declarator_p
2268 (cp_parser
*, bool);
2269 static tree cp_parser_function_definition_from_specifiers_and_declarator
2270 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2271 static tree cp_parser_function_definition_after_declarator
2272 (cp_parser
*, bool);
2273 static void cp_parser_template_declaration_after_export
2274 (cp_parser
*, bool);
2275 static void cp_parser_perform_template_parameter_access_checks
2276 (vec
<deferred_access_check
, va_gc
> *);
2277 static tree cp_parser_single_declaration
2278 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2279 static tree cp_parser_functional_cast
2280 (cp_parser
*, tree
);
2281 static tree cp_parser_save_member_function_body
2282 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2283 static tree cp_parser_save_nsdmi
2285 static tree cp_parser_enclosed_template_argument_list
2287 static void cp_parser_save_default_args
2288 (cp_parser
*, tree
);
2289 static void cp_parser_late_parsing_for_member
2290 (cp_parser
*, tree
);
2291 static tree cp_parser_late_parse_one_default_arg
2292 (cp_parser
*, tree
, tree
, tree
);
2293 static void cp_parser_late_parsing_nsdmi
2294 (cp_parser
*, tree
);
2295 static void cp_parser_late_parsing_default_args
2296 (cp_parser
*, tree
);
2297 static tree cp_parser_sizeof_operand
2298 (cp_parser
*, enum rid
);
2299 static tree cp_parser_trait_expr
2300 (cp_parser
*, enum rid
);
2301 static bool cp_parser_declares_only_class_p
2303 static void cp_parser_set_storage_class
2304 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2305 static void cp_parser_set_decl_spec_type
2306 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2307 static void set_and_check_decl_spec_loc
2308 (cp_decl_specifier_seq
*decl_specs
,
2309 cp_decl_spec ds
, cp_token
*);
2310 static bool cp_parser_friend_p
2311 (const cp_decl_specifier_seq
*);
2312 static void cp_parser_required_error
2313 (cp_parser
*, required_token
, bool);
2314 static cp_token
*cp_parser_require
2315 (cp_parser
*, enum cpp_ttype
, required_token
);
2316 static cp_token
*cp_parser_require_keyword
2317 (cp_parser
*, enum rid
, required_token
);
2318 static bool cp_parser_token_starts_function_definition_p
2320 static bool cp_parser_next_token_starts_class_definition_p
2322 static bool cp_parser_next_token_ends_template_argument_p
2324 static bool cp_parser_nth_token_starts_template_argument_list_p
2325 (cp_parser
*, size_t);
2326 static enum tag_types cp_parser_token_is_class_key
2328 static void cp_parser_check_class_key
2329 (enum tag_types
, tree type
);
2330 static void cp_parser_check_access_in_redeclaration
2331 (tree type
, location_t location
);
2332 static bool cp_parser_optional_template_keyword
2334 static void cp_parser_pre_parsed_nested_name_specifier
2336 static bool cp_parser_cache_group
2337 (cp_parser
*, enum cpp_ttype
, unsigned);
2338 static tree cp_parser_cache_defarg
2339 (cp_parser
*parser
, bool nsdmi
);
2340 static void cp_parser_parse_tentatively
2342 static void cp_parser_commit_to_tentative_parse
2344 static void cp_parser_abort_tentative_parse
2346 static bool cp_parser_parse_definitely
2348 static inline bool cp_parser_parsing_tentatively
2350 static bool cp_parser_uncommitted_to_tentative_parse_p
2352 static void cp_parser_error
2353 (cp_parser
*, const char *);
2354 static void cp_parser_name_lookup_error
2355 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2356 static bool cp_parser_simulate_error
2358 static bool cp_parser_check_type_definition
2360 static void cp_parser_check_for_definition_in_return_type
2361 (cp_declarator
*, tree
, location_t type_location
);
2362 static void cp_parser_check_for_invalid_template_id
2363 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2364 static bool cp_parser_non_integral_constant_expression
2365 (cp_parser
*, non_integral_constant
);
2366 static void cp_parser_diagnose_invalid_type_name
2367 (cp_parser
*, tree
, tree
, location_t
);
2368 static bool cp_parser_parse_and_diagnose_invalid_type_name
2370 static int cp_parser_skip_to_closing_parenthesis
2371 (cp_parser
*, bool, bool, bool);
2372 static void cp_parser_skip_to_end_of_statement
2374 static void cp_parser_consume_semicolon_at_end_of_statement
2376 static void cp_parser_skip_to_end_of_block_or_statement
2378 static bool cp_parser_skip_to_closing_brace
2380 static void cp_parser_skip_to_end_of_template_parameter_list
2382 static void cp_parser_skip_to_pragma_eol
2383 (cp_parser
*, cp_token
*);
2384 static bool cp_parser_error_occurred
2386 static bool cp_parser_allow_gnu_extensions_p
2388 static bool cp_parser_is_pure_string_literal
2390 static bool cp_parser_is_string_literal
2392 static bool cp_parser_is_keyword
2393 (cp_token
*, enum rid
);
2394 static tree cp_parser_make_typename_type
2395 (cp_parser
*, tree
, tree
, location_t location
);
2396 static cp_declarator
* cp_parser_make_indirect_declarator
2397 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2399 /* Returns nonzero if we are parsing tentatively. */
2402 cp_parser_parsing_tentatively (cp_parser
* parser
)
2404 return parser
->context
->next
!= NULL
;
2407 /* Returns nonzero if TOKEN is a string literal. */
2410 cp_parser_is_pure_string_literal (cp_token
* token
)
2412 return (token
->type
== CPP_STRING
||
2413 token
->type
== CPP_STRING16
||
2414 token
->type
== CPP_STRING32
||
2415 token
->type
== CPP_WSTRING
||
2416 token
->type
== CPP_UTF8STRING
);
2419 /* Returns nonzero if TOKEN is a string literal
2420 of a user-defined string literal. */
2423 cp_parser_is_string_literal (cp_token
* token
)
2425 return (cp_parser_is_pure_string_literal (token
) ||
2426 token
->type
== CPP_STRING_USERDEF
||
2427 token
->type
== CPP_STRING16_USERDEF
||
2428 token
->type
== CPP_STRING32_USERDEF
||
2429 token
->type
== CPP_WSTRING_USERDEF
||
2430 token
->type
== CPP_UTF8STRING_USERDEF
);
2433 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2436 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2438 return token
->keyword
== keyword
;
2441 /* If not parsing tentatively, issue a diagnostic of the form
2442 FILE:LINE: MESSAGE before TOKEN
2443 where TOKEN is the next token in the input stream. MESSAGE
2444 (specified by the caller) is usually of the form "expected
2448 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2450 if (!cp_parser_simulate_error (parser
))
2452 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2453 /* This diagnostic makes more sense if it is tagged to the line
2454 of the token we just peeked at. */
2455 cp_lexer_set_source_position_from_token (token
);
2457 if (token
->type
== CPP_PRAGMA
)
2459 error_at (token
->location
,
2460 "%<#pragma%> is not allowed here");
2461 cp_parser_skip_to_pragma_eol (parser
, token
);
2465 c_parse_error (gmsgid
,
2466 /* Because c_parser_error does not understand
2467 CPP_KEYWORD, keywords are treated like
2469 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2470 token
->u
.value
, token
->flags
);
2474 /* Issue an error about name-lookup failing. NAME is the
2475 IDENTIFIER_NODE DECL is the result of
2476 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2477 the thing that we hoped to find. */
2480 cp_parser_name_lookup_error (cp_parser
* parser
,
2483 name_lookup_error desired
,
2484 location_t location
)
2486 /* If name lookup completely failed, tell the user that NAME was not
2488 if (decl
== error_mark_node
)
2490 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2491 error_at (location
, "%<%E::%E%> has not been declared",
2492 parser
->scope
, name
);
2493 else if (parser
->scope
== global_namespace
)
2494 error_at (location
, "%<::%E%> has not been declared", name
);
2495 else if (parser
->object_scope
2496 && !CLASS_TYPE_P (parser
->object_scope
))
2497 error_at (location
, "request for member %qE in non-class type %qT",
2498 name
, parser
->object_scope
);
2499 else if (parser
->object_scope
)
2500 error_at (location
, "%<%T::%E%> has not been declared",
2501 parser
->object_scope
, name
);
2503 error_at (location
, "%qE has not been declared", name
);
2505 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2510 error_at (location
, "%<%E::%E%> is not a type",
2511 parser
->scope
, name
);
2514 error_at (location
, "%<%E::%E%> is not a class or namespace",
2515 parser
->scope
, name
);
2519 "%<%E::%E%> is not a class, namespace, or enumeration",
2520 parser
->scope
, name
);
2527 else if (parser
->scope
== global_namespace
)
2532 error_at (location
, "%<::%E%> is not a type", name
);
2535 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2539 "%<::%E%> is not a class, namespace, or enumeration",
2551 error_at (location
, "%qE is not a type", name
);
2554 error_at (location
, "%qE is not a class or namespace", name
);
2558 "%qE is not a class, namespace, or enumeration", name
);
2566 /* If we are parsing tentatively, remember that an error has occurred
2567 during this tentative parse. Returns true if the error was
2568 simulated; false if a message should be issued by the caller. */
2571 cp_parser_simulate_error (cp_parser
* parser
)
2573 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2575 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2581 /* This function is called when a type is defined. If type
2582 definitions are forbidden at this point, an error message is
2586 cp_parser_check_type_definition (cp_parser
* parser
)
2588 /* If types are forbidden here, issue a message. */
2589 if (parser
->type_definition_forbidden_message
)
2591 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2592 in the message need to be interpreted. */
2593 error (parser
->type_definition_forbidden_message
);
2599 /* This function is called when the DECLARATOR is processed. The TYPE
2600 was a type defined in the decl-specifiers. If it is invalid to
2601 define a type in the decl-specifiers for DECLARATOR, an error is
2602 issued. TYPE_LOCATION is the location of TYPE and is used
2603 for error reporting. */
2606 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2607 tree type
, location_t type_location
)
2609 /* [dcl.fct] forbids type definitions in return types.
2610 Unfortunately, it's not easy to know whether or not we are
2611 processing a return type until after the fact. */
2613 && (declarator
->kind
== cdk_pointer
2614 || declarator
->kind
== cdk_reference
2615 || declarator
->kind
== cdk_ptrmem
))
2616 declarator
= declarator
->declarator
;
2618 && declarator
->kind
== cdk_function
)
2620 error_at (type_location
,
2621 "new types may not be defined in a return type");
2622 inform (type_location
,
2623 "(perhaps a semicolon is missing after the definition of %qT)",
2628 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2629 "<" in any valid C++ program. If the next token is indeed "<",
2630 issue a message warning the user about what appears to be an
2631 invalid attempt to form a template-id. LOCATION is the location
2632 of the type-specifier (TYPE) */
2635 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2637 enum tag_types tag_type
,
2638 location_t location
)
2640 cp_token_position start
= 0;
2642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2645 error_at (location
, "%qT is not a template", type
);
2646 else if (identifier_p (type
))
2648 if (tag_type
!= none_type
)
2649 error_at (location
, "%qE is not a class template", type
);
2651 error_at (location
, "%qE is not a template", type
);
2654 error_at (location
, "invalid template-id");
2655 /* Remember the location of the invalid "<". */
2656 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2657 start
= cp_lexer_token_position (parser
->lexer
, true);
2658 /* Consume the "<". */
2659 cp_lexer_consume_token (parser
->lexer
);
2660 /* Parse the template arguments. */
2661 cp_parser_enclosed_template_argument_list (parser
);
2662 /* Permanently remove the invalid template arguments so that
2663 this error message is not issued again. */
2665 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2669 /* If parsing an integral constant-expression, issue an error message
2670 about the fact that THING appeared and return true. Otherwise,
2671 return false. In either case, set
2672 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2675 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2676 non_integral_constant thing
)
2678 parser
->non_integral_constant_expression_p
= true;
2679 if (parser
->integral_constant_expression_p
)
2681 if (!parser
->allow_non_integral_constant_expression_p
)
2683 const char *msg
= NULL
;
2687 error ("floating-point literal "
2688 "cannot appear in a constant-expression");
2691 error ("a cast to a type other than an integral or "
2692 "enumeration type cannot appear in a "
2693 "constant-expression");
2696 error ("%<typeid%> operator "
2697 "cannot appear in a constant-expression");
2700 error ("non-constant compound literals "
2701 "cannot appear in a constant-expression");
2704 error ("a function call "
2705 "cannot appear in a constant-expression");
2708 error ("an increment "
2709 "cannot appear in a constant-expression");
2712 error ("an decrement "
2713 "cannot appear in a constant-expression");
2716 error ("an array reference "
2717 "cannot appear in a constant-expression");
2719 case NIC_ADDR_LABEL
:
2720 error ("the address of a label "
2721 "cannot appear in a constant-expression");
2723 case NIC_OVERLOADED
:
2724 error ("calls to overloaded operators "
2725 "cannot appear in a constant-expression");
2727 case NIC_ASSIGNMENT
:
2728 error ("an assignment cannot appear in a constant-expression");
2731 error ("a comma operator "
2732 "cannot appear in a constant-expression");
2734 case NIC_CONSTRUCTOR
:
2735 error ("a call to a constructor "
2736 "cannot appear in a constant-expression");
2738 case NIC_TRANSACTION
:
2739 error ("a transaction expression "
2740 "cannot appear in a constant-expression");
2746 msg
= "__FUNCTION__";
2748 case NIC_PRETTY_FUNC
:
2749 msg
= "__PRETTY_FUNCTION__";
2769 case NIC_PREINCREMENT
:
2772 case NIC_PREDECREMENT
:
2785 error ("%qs cannot appear in a constant-expression", msg
);
2792 /* Emit a diagnostic for an invalid type name. SCOPE is the
2793 qualifying scope (or NULL, if none) for ID. This function commits
2794 to the current active tentative parse, if any. (Otherwise, the
2795 problematic construct might be encountered again later, resulting
2796 in duplicate error messages.) LOCATION is the location of ID. */
2799 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2800 tree scope
, tree id
,
2801 location_t location
)
2803 tree decl
, old_scope
;
2804 cp_parser_commit_to_tentative_parse (parser
);
2805 /* Try to lookup the identifier. */
2806 old_scope
= parser
->scope
;
2807 parser
->scope
= scope
;
2808 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2809 parser
->scope
= old_scope
;
2810 /* If the lookup found a template-name, it means that the user forgot
2811 to specify an argument list. Emit a useful error message. */
2812 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2814 "invalid use of template-name %qE without an argument list",
2816 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2817 error_at (location
, "invalid use of destructor %qD as a type", id
);
2818 else if (TREE_CODE (decl
) == TYPE_DECL
)
2819 /* Something like 'unsigned A a;' */
2820 error_at (location
, "invalid combination of multiple type-specifiers");
2821 else if (!parser
->scope
)
2823 /* Issue an error message. */
2824 error_at (location
, "%qE does not name a type", id
);
2825 /* If we're in a template class, it's possible that the user was
2826 referring to a type from a base class. For example:
2828 template <typename T> struct A { typedef T X; };
2829 template <typename T> struct B : public A<T> { X x; };
2831 The user should have said "typename A<T>::X". */
2832 if (cxx_dialect
< cxx0x
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2833 inform (location
, "C++11 %<constexpr%> only available with "
2834 "-std=c++11 or -std=gnu++11");
2835 else if (processing_template_decl
&& current_class_type
2836 && TYPE_BINFO (current_class_type
))
2840 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2844 tree base_type
= BINFO_TYPE (b
);
2845 if (CLASS_TYPE_P (base_type
)
2846 && dependent_type_p (base_type
))
2849 /* Go from a particular instantiation of the
2850 template (which will have an empty TYPE_FIELDs),
2851 to the main version. */
2852 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2853 for (field
= TYPE_FIELDS (base_type
);
2855 field
= DECL_CHAIN (field
))
2856 if (TREE_CODE (field
) == TYPE_DECL
2857 && DECL_NAME (field
) == id
)
2860 "(perhaps %<typename %T::%E%> was intended)",
2861 BINFO_TYPE (b
), id
);
2870 /* Here we diagnose qualified-ids where the scope is actually correct,
2871 but the identifier does not resolve to a valid type name. */
2872 else if (parser
->scope
!= error_mark_node
)
2874 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2877 error_at (location_of (id
),
2878 "%qE in namespace %qE does not name a template type",
2881 error_at (location_of (id
),
2882 "%qE in namespace %qE does not name a type",
2885 else if (CLASS_TYPE_P (parser
->scope
)
2886 && constructor_name_p (id
, parser
->scope
))
2889 error_at (location
, "%<%T::%E%> names the constructor, not"
2890 " the type", parser
->scope
, id
);
2891 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2892 error_at (location
, "and %qT has no template constructors",
2895 else if (TYPE_P (parser
->scope
)
2896 && dependent_scope_p (parser
->scope
))
2897 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2898 "%qT is a dependent scope",
2899 parser
->scope
, id
, parser
->scope
);
2900 else if (TYPE_P (parser
->scope
))
2902 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2903 error_at (location_of (id
),
2904 "%qE in %q#T does not name a template type",
2907 error_at (location_of (id
),
2908 "%qE in %q#T does not name a type",
2916 /* Check for a common situation where a type-name should be present,
2917 but is not, and issue a sensible error message. Returns true if an
2918 invalid type-name was detected.
2920 The situation handled by this function are variable declarations of the
2921 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2922 Usually, `ID' should name a type, but if we got here it means that it
2923 does not. We try to emit the best possible error message depending on
2924 how exactly the id-expression looks like. */
2927 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2930 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2932 /* Avoid duplicate error about ambiguous lookup. */
2933 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2935 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2936 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2940 cp_parser_parse_tentatively (parser
);
2941 id
= cp_parser_id_expression (parser
,
2942 /*template_keyword_p=*/false,
2943 /*check_dependency_p=*/true,
2944 /*template_p=*/NULL
,
2945 /*declarator_p=*/true,
2946 /*optional_p=*/false);
2947 /* If the next token is a (, this is a function with no explicit return
2948 type, i.e. constructor, destructor or conversion op. */
2949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
2950 || TREE_CODE (id
) == TYPE_DECL
)
2952 cp_parser_abort_tentative_parse (parser
);
2955 if (!cp_parser_parse_definitely (parser
))
2958 /* Emit a diagnostic for the invalid type. */
2959 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2960 id
, token
->location
);
2962 /* If we aren't in the middle of a declarator (i.e. in a
2963 parameter-declaration-clause), skip to the end of the declaration;
2964 there's no point in trying to process it. */
2965 if (!parser
->in_declarator_p
)
2966 cp_parser_skip_to_end_of_block_or_statement (parser
);
2970 /* Consume tokens up to, and including, the next non-nested closing `)'.
2971 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2972 are doing error recovery. Returns -1 if OR_COMMA is true and we
2973 found an unnested comma. */
2976 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2981 unsigned paren_depth
= 0;
2982 unsigned brace_depth
= 0;
2983 unsigned square_depth
= 0;
2985 if (recovering
&& !or_comma
2986 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2991 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2993 switch (token
->type
)
2996 case CPP_PRAGMA_EOL
:
2997 /* If we've run out of tokens, then there is no closing `)'. */
3000 /* This is good for lambda expression capture-lists. */
3001 case CPP_OPEN_SQUARE
:
3004 case CPP_CLOSE_SQUARE
:
3005 if (!square_depth
--)
3010 /* This matches the processing in skip_to_end_of_statement. */
3015 case CPP_OPEN_BRACE
:
3018 case CPP_CLOSE_BRACE
:
3024 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3029 case CPP_OPEN_PAREN
:
3034 case CPP_CLOSE_PAREN
:
3035 if (!brace_depth
&& !paren_depth
--)
3038 cp_lexer_consume_token (parser
->lexer
);
3047 /* Consume the token. */
3048 cp_lexer_consume_token (parser
->lexer
);
3052 /* Consume tokens until we reach the end of the current statement.
3053 Normally, that will be just before consuming a `;'. However, if a
3054 non-nested `}' comes first, then we stop before consuming that. */
3057 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3059 unsigned nesting_depth
= 0;
3063 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3065 switch (token
->type
)
3068 case CPP_PRAGMA_EOL
:
3069 /* If we've run out of tokens, stop. */
3073 /* If the next token is a `;', we have reached the end of the
3079 case CPP_CLOSE_BRACE
:
3080 /* If this is a non-nested '}', stop before consuming it.
3081 That way, when confronted with something like:
3085 we stop before consuming the closing '}', even though we
3086 have not yet reached a `;'. */
3087 if (nesting_depth
== 0)
3090 /* If it is the closing '}' for a block that we have
3091 scanned, stop -- but only after consuming the token.
3097 we will stop after the body of the erroneously declared
3098 function, but before consuming the following `typedef'
3100 if (--nesting_depth
== 0)
3102 cp_lexer_consume_token (parser
->lexer
);
3106 case CPP_OPEN_BRACE
:
3114 /* Consume the token. */
3115 cp_lexer_consume_token (parser
->lexer
);
3119 /* This function is called at the end of a statement or declaration.
3120 If the next token is a semicolon, it is consumed; otherwise, error
3121 recovery is attempted. */
3124 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3126 /* Look for the trailing `;'. */
3127 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3129 /* If there is additional (erroneous) input, skip to the end of
3131 cp_parser_skip_to_end_of_statement (parser
);
3132 /* If the next token is now a `;', consume it. */
3133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3134 cp_lexer_consume_token (parser
->lexer
);
3138 /* Skip tokens until we have consumed an entire block, or until we
3139 have consumed a non-nested `;'. */
3142 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3144 int nesting_depth
= 0;
3146 while (nesting_depth
>= 0)
3148 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3150 switch (token
->type
)
3153 case CPP_PRAGMA_EOL
:
3154 /* If we've run out of tokens, stop. */
3158 /* Stop if this is an unnested ';'. */
3163 case CPP_CLOSE_BRACE
:
3164 /* Stop if this is an unnested '}', or closes the outermost
3167 if (nesting_depth
< 0)
3173 case CPP_OPEN_BRACE
:
3182 /* Consume the token. */
3183 cp_lexer_consume_token (parser
->lexer
);
3187 /* Skip tokens until a non-nested closing curly brace is the next
3188 token, or there are no more tokens. Return true in the first case,
3192 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3194 unsigned nesting_depth
= 0;
3198 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3200 switch (token
->type
)
3203 case CPP_PRAGMA_EOL
:
3204 /* If we've run out of tokens, stop. */
3207 case CPP_CLOSE_BRACE
:
3208 /* If the next token is a non-nested `}', then we have reached
3209 the end of the current block. */
3210 if (nesting_depth
-- == 0)
3214 case CPP_OPEN_BRACE
:
3215 /* If it the next token is a `{', then we are entering a new
3216 block. Consume the entire block. */
3224 /* Consume the token. */
3225 cp_lexer_consume_token (parser
->lexer
);
3229 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3230 parameter is the PRAGMA token, allowing us to purge the entire pragma
3234 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3238 parser
->lexer
->in_pragma
= false;
3241 token
= cp_lexer_consume_token (parser
->lexer
);
3242 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3244 /* Ensure that the pragma is not parsed again. */
3245 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3248 /* Require pragma end of line, resyncing with it as necessary. The
3249 arguments are as for cp_parser_skip_to_pragma_eol. */
3252 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3254 parser
->lexer
->in_pragma
= false;
3255 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3256 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3259 /* This is a simple wrapper around make_typename_type. When the id is
3260 an unresolved identifier node, we can provide a superior diagnostic
3261 using cp_parser_diagnose_invalid_type_name. */
3264 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3265 tree id
, location_t id_location
)
3268 if (identifier_p (id
))
3270 result
= make_typename_type (scope
, id
, typename_type
,
3271 /*complain=*/tf_none
);
3272 if (result
== error_mark_node
)
3273 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3276 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3279 /* This is a wrapper around the
3280 make_{pointer,ptrmem,reference}_declarator functions that decides
3281 which one to call based on the CODE and CLASS_TYPE arguments. The
3282 CODE argument should be one of the values returned by
3283 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3284 appertain to the pointer or reference. */
3286 static cp_declarator
*
3287 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3288 cp_cv_quals cv_qualifiers
,
3289 cp_declarator
*target
,
3292 if (code
== ERROR_MARK
)
3293 return cp_error_declarator
;
3295 if (code
== INDIRECT_REF
)
3296 if (class_type
== NULL_TREE
)
3297 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3299 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3300 target
, attributes
);
3301 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3302 return make_reference_declarator (cv_qualifiers
, target
,
3304 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3305 return make_reference_declarator (cv_qualifiers
, target
,
3310 /* Create a new C++ parser. */
3313 cp_parser_new (void)
3319 /* cp_lexer_new_main is called before doing GC allocation because
3320 cp_lexer_new_main might load a PCH file. */
3321 lexer
= cp_lexer_new_main ();
3323 /* Initialize the binops_by_token so that we can get the tree
3324 directly from the token. */
3325 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3326 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3328 parser
= ggc_alloc_cleared_cp_parser ();
3329 parser
->lexer
= lexer
;
3330 parser
->context
= cp_parser_context_new (NULL
);
3332 /* For now, we always accept GNU extensions. */
3333 parser
->allow_gnu_extensions_p
= 1;
3335 /* The `>' token is a greater-than operator, not the end of a
3337 parser
->greater_than_is_operator_p
= true;
3339 parser
->default_arg_ok_p
= true;
3341 /* We are not parsing a constant-expression. */
3342 parser
->integral_constant_expression_p
= false;
3343 parser
->allow_non_integral_constant_expression_p
= false;
3344 parser
->non_integral_constant_expression_p
= false;
3346 /* Local variable names are not forbidden. */
3347 parser
->local_variables_forbidden_p
= false;
3349 /* We are not processing an `extern "C"' declaration. */
3350 parser
->in_unbraced_linkage_specification_p
= false;
3352 /* We are not processing a declarator. */
3353 parser
->in_declarator_p
= false;
3355 /* We are not processing a template-argument-list. */
3356 parser
->in_template_argument_list_p
= false;
3358 /* We are not in an iteration statement. */
3359 parser
->in_statement
= 0;
3361 /* We are not in a switch statement. */
3362 parser
->in_switch_statement_p
= false;
3364 /* We are not parsing a type-id inside an expression. */
3365 parser
->in_type_id_in_expr_p
= false;
3367 /* Declarations aren't implicitly extern "C". */
3368 parser
->implicit_extern_c
= false;
3370 /* String literals should be translated to the execution character set. */
3371 parser
->translate_strings_p
= true;
3373 /* We are not parsing a function body. */
3374 parser
->in_function_body
= false;
3376 /* We can correct until told otherwise. */
3377 parser
->colon_corrects_to_scope_p
= true;
3379 /* The unparsed function queue is empty. */
3380 push_unparsed_function_queues (parser
);
3382 /* There are no classes being defined. */
3383 parser
->num_classes_being_defined
= 0;
3385 /* No template parameters apply. */
3386 parser
->num_template_parameter_lists
= 0;
3391 /* Create a cp_lexer structure which will emit the tokens in CACHE
3392 and push it onto the parser's lexer stack. This is used for delayed
3393 parsing of in-class method bodies and default arguments, and should
3394 not be confused with tentative parsing. */
3396 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3398 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3399 lexer
->next
= parser
->lexer
;
3400 parser
->lexer
= lexer
;
3402 /* Move the current source position to that of the first token in the
3404 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3407 /* Pop the top lexer off the parser stack. This is never used for the
3408 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3410 cp_parser_pop_lexer (cp_parser
*parser
)
3412 cp_lexer
*lexer
= parser
->lexer
;
3413 parser
->lexer
= lexer
->next
;
3414 cp_lexer_destroy (lexer
);
3416 /* Put the current source position back where it was before this
3417 lexer was pushed. */
3418 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3421 /* Lexical conventions [gram.lex] */
3423 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3427 cp_parser_identifier (cp_parser
* parser
)
3431 /* Look for the identifier. */
3432 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3433 /* Return the value. */
3434 return token
? token
->u
.value
: error_mark_node
;
3437 /* Parse a sequence of adjacent string constants. Returns a
3438 TREE_STRING representing the combined, nul-terminated string
3439 constant. If TRANSLATE is true, translate the string to the
3440 execution character set. If WIDE_OK is true, a wide string is
3443 C++98 [lex.string] says that if a narrow string literal token is
3444 adjacent to a wide string literal token, the behavior is undefined.
3445 However, C99 6.4.5p4 says that this results in a wide string literal.
3446 We follow C99 here, for consistency with the C front end.
3448 This code is largely lifted from lex_string() in c-lex.c.
3450 FUTURE: ObjC++ will need to handle @-strings here. */
3452 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3456 struct obstack str_ob
;
3457 cpp_string str
, istr
, *strs
;
3459 enum cpp_ttype type
, curr_type
;
3460 int have_suffix_p
= 0;
3462 tree suffix_id
= NULL_TREE
;
3463 bool curr_tok_is_userdef_p
= false;
3465 tok
= cp_lexer_peek_token (parser
->lexer
);
3466 if (!cp_parser_is_string_literal (tok
))
3468 cp_parser_error (parser
, "expected string-literal");
3469 return error_mark_node
;
3472 if (cpp_userdef_string_p (tok
->type
))
3474 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3475 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3476 curr_tok_is_userdef_p
= true;
3480 string_tree
= tok
->u
.value
;
3481 curr_type
= tok
->type
;
3485 /* Try to avoid the overhead of creating and destroying an obstack
3486 for the common case of just one string. */
3487 if (!cp_parser_is_string_literal
3488 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3490 cp_lexer_consume_token (parser
->lexer
);
3492 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3493 str
.len
= TREE_STRING_LENGTH (string_tree
);
3496 if (curr_tok_is_userdef_p
)
3498 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3500 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3503 curr_type
= tok
->type
;
3509 gcc_obstack_init (&str_ob
);
3514 cp_lexer_consume_token (parser
->lexer
);
3516 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3517 str
.len
= TREE_STRING_LENGTH (string_tree
);
3519 if (curr_tok_is_userdef_p
)
3521 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3522 if (have_suffix_p
== 0)
3524 suffix_id
= curr_suffix_id
;
3527 else if (have_suffix_p
== 1
3528 && curr_suffix_id
!= suffix_id
)
3530 error ("inconsistent user-defined literal suffixes"
3531 " %qD and %qD in string literal",
3532 suffix_id
, curr_suffix_id
);
3535 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3538 curr_type
= tok
->type
;
3540 if (type
!= curr_type
)
3542 if (type
== CPP_STRING
)
3544 else if (curr_type
!= CPP_STRING
)
3545 error_at (tok
->location
,
3546 "unsupported non-standard concatenation "
3547 "of string literals");
3550 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3552 tok
= cp_lexer_peek_token (parser
->lexer
);
3553 if (cpp_userdef_string_p (tok
->type
))
3555 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3556 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3557 curr_tok_is_userdef_p
= true;
3561 string_tree
= tok
->u
.value
;
3562 curr_type
= tok
->type
;
3563 curr_tok_is_userdef_p
= false;
3566 while (cp_parser_is_string_literal (tok
));
3568 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3571 if (type
!= CPP_STRING
&& !wide_ok
)
3573 cp_parser_error (parser
, "a wide string is invalid in this context");
3577 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3578 (parse_in
, strs
, count
, &istr
, type
))
3580 value
= build_string (istr
.len
, (const char *)istr
.text
);
3581 free (CONST_CAST (unsigned char *, istr
.text
));
3587 case CPP_UTF8STRING
:
3588 TREE_TYPE (value
) = char_array_type_node
;
3591 TREE_TYPE (value
) = char16_array_type_node
;
3594 TREE_TYPE (value
) = char32_array_type_node
;
3597 TREE_TYPE (value
) = wchar_array_type_node
;
3601 value
= fix_string_type (value
);
3605 tree literal
= build_userdef_literal (suffix_id
, value
,
3606 OT_NONE
, NULL_TREE
);
3607 tok
->u
.value
= literal
;
3608 return cp_parser_userdef_string_literal (tok
);
3612 /* cpp_interpret_string has issued an error. */
3613 value
= error_mark_node
;
3616 obstack_free (&str_ob
, 0);
3621 /* Look up a literal operator with the name and the exact arguments. */
3624 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3627 decl
= lookup_name (name
);
3628 if (!decl
|| !is_overloaded_fn (decl
))
3629 return error_mark_node
;
3631 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3635 tree fn
= OVL_CURRENT (fns
);
3636 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3637 if (parmtypes
!= NULL_TREE
)
3639 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3640 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3642 tree tparm
= TREE_VALUE (parmtypes
);
3643 tree targ
= TREE_TYPE ((*args
)[ix
]);
3644 bool ptr
= TYPE_PTR_P (tparm
);
3645 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3646 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3648 || !same_type_p (TREE_TYPE (tparm
),
3653 && ix
== vec_safe_length (args
)
3654 /* May be this should be sufficient_parms_p instead,
3655 depending on how exactly should user-defined literals
3656 work in presence of default arguments on the literal
3657 operator parameters. */
3658 && parmtypes
== void_list_node
)
3663 return error_mark_node
;
3666 /* Parse a user-defined char constant. Returns a call to a user-defined
3667 literal operator taking the character as an argument. */
3670 cp_parser_userdef_char_literal (cp_parser
*parser
)
3672 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3673 tree literal
= token
->u
.value
;
3674 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3675 tree value
= USERDEF_LITERAL_VALUE (literal
);
3676 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3679 /* Build up a call to the user-defined operator */
3680 /* Lookup the name we got back from the id-expression. */
3681 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3682 vec_safe_push (args
, value
);
3683 decl
= lookup_literal_operator (name
, args
);
3684 if (!decl
|| decl
== error_mark_node
)
3686 error ("unable to find character literal operator %qD with %qT argument",
3687 name
, TREE_TYPE (value
));
3688 release_tree_vector (args
);
3689 return error_mark_node
;
3691 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3692 release_tree_vector (args
);
3693 if (result
!= error_mark_node
)
3696 error ("unable to find character literal operator %qD with %qT argument",
3697 name
, TREE_TYPE (value
));
3698 return error_mark_node
;
3701 /* A subroutine of cp_parser_userdef_numeric_literal to
3702 create a char... template parameter pack from a string node. */
3705 make_char_string_pack (tree value
)
3708 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3709 const char *str
= TREE_STRING_POINTER (value
);
3710 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3711 tree argvec
= make_tree_vec (1);
3713 /* Fill in CHARVEC with all of the parameters. */
3714 charvec
= make_tree_vec (len
);
3715 for (i
= 0; i
< len
; ++i
)
3716 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3718 /* Build the argument packs. */
3719 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3720 TREE_TYPE (argpack
) = char_type_node
;
3722 TREE_VEC_ELT (argvec
, 0) = argpack
;
3727 /* A subroutine of cp_parser_userdef_numeric_literal to
3728 create a char... template parameter pack from a string node. */
3731 make_string_pack (tree value
)
3734 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3735 const char *str
= TREE_STRING_POINTER (value
);
3736 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3737 tree argvec
= make_tree_vec (2);
3739 tree string_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3741 /* First template parm is character type. */
3742 TREE_VEC_ELT (argvec
, 0) = string_char_type_node
;
3744 /* Fill in CHARVEC with all of the parameters. */
3745 charvec
= make_tree_vec (len
);
3746 for (i
= 0; i
< len
; ++i
)
3747 TREE_VEC_ELT (charvec
, i
) = build_int_cst (string_char_type_node
, str
[i
]);
3749 /* Build the argument packs. */
3750 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3751 TREE_TYPE (argpack
) = string_char_type_node
;
3753 TREE_VEC_ELT (argvec
, 1) = argpack
;
3758 /* Parse a user-defined numeric constant. returns a call to a user-defined
3759 literal operator. */
3762 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3764 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3765 tree literal
= token
->u
.value
;
3766 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3767 tree value
= USERDEF_LITERAL_VALUE (literal
);
3768 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3769 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3770 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3772 vec
<tree
, va_gc
> *args
;
3774 /* Look for a literal operator taking the exact type of numeric argument
3775 as the literal value. */
3776 args
= make_tree_vector ();
3777 vec_safe_push (args
, value
);
3778 decl
= lookup_literal_operator (name
, args
);
3779 if (decl
&& decl
!= error_mark_node
)
3781 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3782 if (result
!= error_mark_node
)
3784 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3785 warning_at (token
->location
, OPT_Woverflow
,
3786 "integer literal exceeds range of %qT type",
3787 long_long_unsigned_type_node
);
3791 warning_at (token
->location
, OPT_Woverflow
,
3792 "floating literal exceeds range of %qT type",
3793 long_double_type_node
);
3794 else if (overflow
< 0)
3795 warning_at (token
->location
, OPT_Woverflow
,
3796 "floating literal truncated to zero");
3798 release_tree_vector (args
);
3802 release_tree_vector (args
);
3804 /* If the numeric argument didn't work, look for a raw literal
3805 operator taking a const char* argument consisting of the number
3806 in string format. */
3807 args
= make_tree_vector ();
3808 vec_safe_push (args
, num_string
);
3809 decl
= lookup_literal_operator (name
, args
);
3810 if (decl
&& decl
!= error_mark_node
)
3812 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3813 if (result
!= error_mark_node
)
3815 release_tree_vector (args
);
3819 release_tree_vector (args
);
3821 /* If the raw literal didn't work, look for a non-type template
3822 function with parameter pack char.... Call the function with
3823 template parameter characters representing the number. */
3824 args
= make_tree_vector ();
3825 decl
= lookup_literal_operator (name
, args
);
3826 if (decl
&& decl
!= error_mark_node
)
3828 tree tmpl_args
= make_char_string_pack (num_string
);
3829 decl
= lookup_template_function (decl
, tmpl_args
);
3830 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3831 if (result
!= error_mark_node
)
3833 release_tree_vector (args
);
3837 release_tree_vector (args
);
3839 error ("unable to find numeric literal operator %qD", name
);
3840 return error_mark_node
;
3843 /* Parse a user-defined string constant. Returns a call to a user-defined
3844 literal operator taking a character pointer and the length of the string
3848 cp_parser_userdef_string_literal (cp_token
*token
)
3850 tree literal
= token
->u
.value
;
3851 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3852 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3853 tree value
= USERDEF_LITERAL_VALUE (literal
);
3854 int len
= TREE_STRING_LENGTH (value
)
3855 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
3857 vec
<tree
, va_gc
> *args
;
3859 /* Look for a template function with typename parameter CharT
3860 and parameter pack CharT... Call the function with
3861 template parameter characters representing the string. */
3862 args
= make_tree_vector ();
3863 decl
= lookup_literal_operator (name
, args
);
3864 if (decl
&& decl
!= error_mark_node
)
3866 tree tmpl_args
= make_string_pack (value
);
3867 decl
= lookup_template_function (decl
, tmpl_args
);
3868 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3869 if (result
!= error_mark_node
)
3871 release_tree_vector (args
);
3875 release_tree_vector (args
);
3877 /* Build up a call to the user-defined operator */
3878 /* Lookup the name we got back from the id-expression. */
3879 args
= make_tree_vector ();
3880 vec_safe_push (args
, value
);
3881 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
3882 decl
= lookup_name (name
);
3883 if (!decl
|| decl
== error_mark_node
)
3885 error ("unable to find string literal operator %qD", name
);
3886 release_tree_vector (args
);
3887 return error_mark_node
;
3889 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3890 release_tree_vector (args
);
3891 if (result
!= error_mark_node
)
3894 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3895 name
, TREE_TYPE (value
), size_type_node
);
3896 return error_mark_node
;
3900 /* Basic concepts [gram.basic] */
3902 /* Parse a translation-unit.
3905 declaration-seq [opt]
3907 Returns TRUE if all went well. */
3910 cp_parser_translation_unit (cp_parser
* parser
)
3912 /* The address of the first non-permanent object on the declarator
3914 static void *declarator_obstack_base
;
3918 /* Create the declarator obstack, if necessary. */
3919 if (!cp_error_declarator
)
3921 gcc_obstack_init (&declarator_obstack
);
3922 /* Create the error declarator. */
3923 cp_error_declarator
= make_declarator (cdk_error
);
3924 /* Create the empty parameter list. */
3925 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3926 /* Remember where the base of the declarator obstack lies. */
3927 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3930 cp_parser_declaration_seq_opt (parser
);
3932 /* If there are no tokens left then all went well. */
3933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3935 /* Get rid of the token array; we don't need it any more. */
3936 cp_lexer_destroy (parser
->lexer
);
3937 parser
->lexer
= NULL
;
3939 /* This file might have been a context that's implicitly extern
3940 "C". If so, pop the lang context. (Only relevant for PCH.) */
3941 if (parser
->implicit_extern_c
)
3943 pop_lang_context ();
3944 parser
->implicit_extern_c
= false;
3948 finish_translation_unit ();
3954 cp_parser_error (parser
, "expected declaration");
3958 /* Make sure the declarator obstack was fully cleaned up. */
3959 gcc_assert (obstack_next_free (&declarator_obstack
)
3960 == declarator_obstack_base
);
3962 /* All went well. */
3966 /* Return the appropriate tsubst flags for parsing, possibly in N3276
3967 decltype context. */
3969 static inline tsubst_flags_t
3970 complain_flags (bool decltype_p
)
3972 tsubst_flags_t complain
= tf_warning_or_error
;
3974 complain
|= tf_decltype
;
3978 /* Expressions [gram.expr] */
3980 /* Parse a primary-expression.
3991 ( compound-statement )
3992 __builtin_va_arg ( assignment-expression , type-id )
3993 __builtin_offsetof ( type-id , offsetof-expression )
3996 __has_nothrow_assign ( type-id )
3997 __has_nothrow_constructor ( type-id )
3998 __has_nothrow_copy ( type-id )
3999 __has_trivial_assign ( type-id )
4000 __has_trivial_constructor ( type-id )
4001 __has_trivial_copy ( type-id )
4002 __has_trivial_destructor ( type-id )
4003 __has_virtual_destructor ( type-id )
4004 __is_abstract ( type-id )
4005 __is_base_of ( type-id , type-id )
4006 __is_class ( type-id )
4007 __is_convertible_to ( type-id , type-id )
4008 __is_empty ( type-id )
4009 __is_enum ( type-id )
4010 __is_final ( type-id )
4011 __is_literal_type ( type-id )
4012 __is_pod ( type-id )
4013 __is_polymorphic ( type-id )
4014 __is_std_layout ( type-id )
4015 __is_trivial ( type-id )
4016 __is_union ( type-id )
4018 Objective-C++ Extension:
4026 ADDRESS_P is true iff this expression was immediately preceded by
4027 "&" and therefore might denote a pointer-to-member. CAST_P is true
4028 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4029 true iff this expression is a template argument.
4031 Returns a representation of the expression. Upon return, *IDK
4032 indicates what kind of id-expression (if any) was present. */
4035 cp_parser_primary_expression (cp_parser
*parser
,
4038 bool template_arg_p
,
4042 cp_token
*token
= NULL
;
4044 /* Assume the primary expression is not an id-expression. */
4045 *idk
= CP_ID_KIND_NONE
;
4047 /* Peek at the next token. */
4048 token
= cp_lexer_peek_token (parser
->lexer
);
4049 switch (token
->type
)
4058 user-defined-literal */
4064 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4065 return cp_parser_userdef_numeric_literal (parser
);
4066 token
= cp_lexer_consume_token (parser
->lexer
);
4067 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4069 error_at (token
->location
,
4070 "fixed-point types not supported in C++");
4071 return error_mark_node
;
4073 /* Floating-point literals are only allowed in an integral
4074 constant expression if they are cast to an integral or
4075 enumeration type. */
4076 if (TREE_CODE (token
->u
.value
) == REAL_CST
4077 && parser
->integral_constant_expression_p
4080 /* CAST_P will be set even in invalid code like "int(2.7 +
4081 ...)". Therefore, we have to check that the next token
4082 is sure to end the cast. */
4085 cp_token
*next_token
;
4087 next_token
= cp_lexer_peek_token (parser
->lexer
);
4088 if (/* The comma at the end of an
4089 enumerator-definition. */
4090 next_token
->type
!= CPP_COMMA
4091 /* The curly brace at the end of an enum-specifier. */
4092 && next_token
->type
!= CPP_CLOSE_BRACE
4093 /* The end of a statement. */
4094 && next_token
->type
!= CPP_SEMICOLON
4095 /* The end of the cast-expression. */
4096 && next_token
->type
!= CPP_CLOSE_PAREN
4097 /* The end of an array bound. */
4098 && next_token
->type
!= CPP_CLOSE_SQUARE
4099 /* The closing ">" in a template-argument-list. */
4100 && (next_token
->type
!= CPP_GREATER
4101 || parser
->greater_than_is_operator_p
)
4102 /* C++0x only: A ">>" treated like two ">" tokens,
4103 in a template-argument-list. */
4104 && (next_token
->type
!= CPP_RSHIFT
4105 || (cxx_dialect
== cxx98
)
4106 || parser
->greater_than_is_operator_p
))
4110 /* If we are within a cast, then the constraint that the
4111 cast is to an integral or enumeration type will be
4112 checked at that point. If we are not within a cast, then
4113 this code is invalid. */
4115 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4117 return token
->u
.value
;
4119 case CPP_CHAR_USERDEF
:
4120 case CPP_CHAR16_USERDEF
:
4121 case CPP_CHAR32_USERDEF
:
4122 case CPP_WCHAR_USERDEF
:
4123 return cp_parser_userdef_char_literal (parser
);
4129 case CPP_UTF8STRING
:
4130 case CPP_STRING_USERDEF
:
4131 case CPP_STRING16_USERDEF
:
4132 case CPP_STRING32_USERDEF
:
4133 case CPP_WSTRING_USERDEF
:
4134 case CPP_UTF8STRING_USERDEF
:
4135 /* ??? Should wide strings be allowed when parser->translate_strings_p
4136 is false (i.e. in attributes)? If not, we can kill the third
4137 argument to cp_parser_string_literal. */
4138 return cp_parser_string_literal (parser
,
4139 parser
->translate_strings_p
,
4142 case CPP_OPEN_PAREN
:
4145 bool saved_greater_than_is_operator_p
;
4147 /* Consume the `('. */
4148 cp_lexer_consume_token (parser
->lexer
);
4149 /* Within a parenthesized expression, a `>' token is always
4150 the greater-than operator. */
4151 saved_greater_than_is_operator_p
4152 = parser
->greater_than_is_operator_p
;
4153 parser
->greater_than_is_operator_p
= true;
4154 /* If we see `( { ' then we are looking at the beginning of
4155 a GNU statement-expression. */
4156 if (cp_parser_allow_gnu_extensions_p (parser
)
4157 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
4159 /* Statement-expressions are not allowed by the standard. */
4160 pedwarn (token
->location
, OPT_Wpedantic
,
4161 "ISO C++ forbids braced-groups within expressions");
4163 /* And they're not allowed outside of a function-body; you
4164 cannot, for example, write:
4166 int i = ({ int j = 3; j + 1; });
4168 at class or namespace scope. */
4169 if (!parser
->in_function_body
4170 || parser
->in_template_argument_list_p
)
4172 error_at (token
->location
,
4173 "statement-expressions are not allowed outside "
4174 "functions nor in template-argument lists");
4175 cp_parser_skip_to_end_of_block_or_statement (parser
);
4176 expr
= error_mark_node
;
4180 /* Start the statement-expression. */
4181 expr
= begin_stmt_expr ();
4182 /* Parse the compound-statement. */
4183 cp_parser_compound_statement (parser
, expr
, false, false);
4185 expr
= finish_stmt_expr (expr
, false);
4190 /* Parse the parenthesized expression. */
4191 expr
= cp_parser_expression (parser
, cast_p
, decltype_p
, idk
);
4192 /* Let the front end know that this expression was
4193 enclosed in parentheses. This matters in case, for
4194 example, the expression is of the form `A::B', since
4195 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4197 expr
= finish_parenthesized_expr (expr
);
4198 /* DR 705: Wrapping an unqualified name in parentheses
4199 suppresses arg-dependent lookup. We want to pass back
4200 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4201 (c++/37862), but none of the others. */
4202 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4203 *idk
= CP_ID_KIND_NONE
;
4205 /* The `>' token might be the end of a template-id or
4206 template-parameter-list now. */
4207 parser
->greater_than_is_operator_p
4208 = saved_greater_than_is_operator_p
;
4209 /* Consume the `)'. */
4210 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4211 cp_parser_skip_to_end_of_statement (parser
);
4216 case CPP_OPEN_SQUARE
:
4217 if (c_dialect_objc ())
4218 /* We have an Objective-C++ message. */
4219 return cp_parser_objc_expression (parser
);
4221 tree lam
= cp_parser_lambda_expression (parser
);
4222 /* Don't warn about a failed tentative parse. */
4223 if (cp_parser_error_occurred (parser
))
4224 return error_mark_node
;
4225 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4229 case CPP_OBJC_STRING
:
4230 if (c_dialect_objc ())
4231 /* We have an Objective-C++ string literal. */
4232 return cp_parser_objc_expression (parser
);
4233 cp_parser_error (parser
, "expected primary-expression");
4234 return error_mark_node
;
4237 switch (token
->keyword
)
4239 /* These two are the boolean literals. */
4241 cp_lexer_consume_token (parser
->lexer
);
4242 return boolean_true_node
;
4244 cp_lexer_consume_token (parser
->lexer
);
4245 return boolean_false_node
;
4247 /* The `__null' literal. */
4249 cp_lexer_consume_token (parser
->lexer
);
4252 /* The `nullptr' literal. */
4254 cp_lexer_consume_token (parser
->lexer
);
4255 return nullptr_node
;
4257 /* Recognize the `this' keyword. */
4259 cp_lexer_consume_token (parser
->lexer
);
4260 if (parser
->local_variables_forbidden_p
)
4262 error_at (token
->location
,
4263 "%<this%> may not be used in this context");
4264 return error_mark_node
;
4266 /* Pointers cannot appear in constant-expressions. */
4267 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4268 return error_mark_node
;
4269 return finish_this_expr ();
4271 /* The `operator' keyword can be the beginning of an
4276 case RID_FUNCTION_NAME
:
4277 case RID_PRETTY_FUNCTION_NAME
:
4278 case RID_C99_FUNCTION_NAME
:
4280 non_integral_constant name
;
4282 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4283 __func__ are the names of variables -- but they are
4284 treated specially. Therefore, they are handled here,
4285 rather than relying on the generic id-expression logic
4286 below. Grammatically, these names are id-expressions.
4288 Consume the token. */
4289 token
= cp_lexer_consume_token (parser
->lexer
);
4291 switch (token
->keyword
)
4293 case RID_FUNCTION_NAME
:
4294 name
= NIC_FUNC_NAME
;
4296 case RID_PRETTY_FUNCTION_NAME
:
4297 name
= NIC_PRETTY_FUNC
;
4299 case RID_C99_FUNCTION_NAME
:
4300 name
= NIC_C99_FUNC
;
4306 if (cp_parser_non_integral_constant_expression (parser
, name
))
4307 return error_mark_node
;
4309 /* Look up the name. */
4310 return finish_fname (token
->u
.value
);
4317 source_location type_location
;
4319 /* The `__builtin_va_arg' construct is used to handle
4320 `va_arg'. Consume the `__builtin_va_arg' token. */
4321 cp_lexer_consume_token (parser
->lexer
);
4322 /* Look for the opening `('. */
4323 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4324 /* Now, parse the assignment-expression. */
4325 expression
= cp_parser_assignment_expression (parser
,
4326 /*cast_p=*/false, NULL
);
4327 /* Look for the `,'. */
4328 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4329 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4330 /* Parse the type-id. */
4331 type
= cp_parser_type_id (parser
);
4332 /* Look for the closing `)'. */
4333 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4334 /* Using `va_arg' in a constant-expression is not
4336 if (cp_parser_non_integral_constant_expression (parser
,
4338 return error_mark_node
;
4339 return build_x_va_arg (type_location
, expression
, type
);
4343 return cp_parser_builtin_offsetof (parser
);
4345 case RID_HAS_NOTHROW_ASSIGN
:
4346 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4347 case RID_HAS_NOTHROW_COPY
:
4348 case RID_HAS_TRIVIAL_ASSIGN
:
4349 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4350 case RID_HAS_TRIVIAL_COPY
:
4351 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4352 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4353 case RID_IS_ABSTRACT
:
4354 case RID_IS_BASE_OF
:
4356 case RID_IS_CONVERTIBLE_TO
:
4360 case RID_IS_LITERAL_TYPE
:
4362 case RID_IS_POLYMORPHIC
:
4363 case RID_IS_STD_LAYOUT
:
4364 case RID_IS_TRIVIAL
:
4366 return cp_parser_trait_expr (parser
, token
->keyword
);
4368 /* Objective-C++ expressions. */
4370 case RID_AT_PROTOCOL
:
4371 case RID_AT_SELECTOR
:
4372 return cp_parser_objc_expression (parser
);
4375 if (parser
->in_function_body
4376 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4379 error_at (token
->location
,
4380 "a template declaration cannot appear at block scope");
4381 cp_parser_skip_to_end_of_block_or_statement (parser
);
4382 return error_mark_node
;
4385 cp_parser_error (parser
, "expected primary-expression");
4386 return error_mark_node
;
4389 /* An id-expression can start with either an identifier, a
4390 `::' as the beginning of a qualified-id, or the "operator"
4394 case CPP_TEMPLATE_ID
:
4395 case CPP_NESTED_NAME_SPECIFIER
:
4399 const char *error_msg
;
4402 cp_token
*id_expr_token
;
4405 /* Parse the id-expression. */
4407 = cp_parser_id_expression (parser
,
4408 /*template_keyword_p=*/false,
4409 /*check_dependency_p=*/true,
4411 /*declarator_p=*/false,
4412 /*optional_p=*/false);
4413 if (id_expression
== error_mark_node
)
4414 return error_mark_node
;
4415 id_expr_token
= token
;
4416 token
= cp_lexer_peek_token (parser
->lexer
);
4417 done
= (token
->type
!= CPP_OPEN_SQUARE
4418 && token
->type
!= CPP_OPEN_PAREN
4419 && token
->type
!= CPP_DOT
4420 && token
->type
!= CPP_DEREF
4421 && token
->type
!= CPP_PLUS_PLUS
4422 && token
->type
!= CPP_MINUS_MINUS
);
4423 /* If we have a template-id, then no further lookup is
4424 required. If the template-id was for a template-class, we
4425 will sometimes have a TYPE_DECL at this point. */
4426 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4427 || TREE_CODE (id_expression
) == TYPE_DECL
)
4428 decl
= id_expression
;
4429 /* Look up the name. */
4432 tree ambiguous_decls
;
4434 /* If we already know that this lookup is ambiguous, then
4435 we've already issued an error message; there's no reason
4437 if (id_expr_token
->type
== CPP_NAME
4438 && id_expr_token
->ambiguous_p
)
4440 cp_parser_simulate_error (parser
);
4441 return error_mark_node
;
4444 decl
= cp_parser_lookup_name (parser
, id_expression
,
4447 /*is_namespace=*/false,
4448 /*check_dependency=*/true,
4450 id_expr_token
->location
);
4451 /* If the lookup was ambiguous, an error will already have
4453 if (ambiguous_decls
)
4454 return error_mark_node
;
4456 /* In Objective-C++, we may have an Objective-C 2.0
4457 dot-syntax for classes here. */
4458 if (c_dialect_objc ()
4459 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4460 && TREE_CODE (decl
) == TYPE_DECL
4461 && objc_is_class_name (decl
))
4464 cp_lexer_consume_token (parser
->lexer
);
4465 component
= cp_parser_identifier (parser
);
4466 if (component
== error_mark_node
)
4467 return error_mark_node
;
4469 return objc_build_class_component_ref (id_expression
, component
);
4472 /* In Objective-C++, an instance variable (ivar) may be preferred
4473 to whatever cp_parser_lookup_name() found. */
4474 decl
= objc_lookup_ivar (decl
, id_expression
);
4476 /* If name lookup gives us a SCOPE_REF, then the
4477 qualifying scope was dependent. */
4478 if (TREE_CODE (decl
) == SCOPE_REF
)
4480 /* At this point, we do not know if DECL is a valid
4481 integral constant expression. We assume that it is
4482 in fact such an expression, so that code like:
4484 template <int N> struct A {
4488 is accepted. At template-instantiation time, we
4489 will check that B<N>::i is actually a constant. */
4492 /* Check to see if DECL is a local variable in a context
4493 where that is forbidden. */
4494 if (parser
->local_variables_forbidden_p
4495 && local_variable_p (decl
))
4497 /* It might be that we only found DECL because we are
4498 trying to be generous with pre-ISO scoping rules.
4499 For example, consider:
4503 for (int i = 0; i < 10; ++i) {}
4504 extern void f(int j = i);
4507 Here, name look up will originally find the out
4508 of scope `i'. We need to issue a warning message,
4509 but then use the global `i'. */
4510 decl
= check_for_out_of_scope_variable (decl
);
4511 if (local_variable_p (decl
))
4513 error_at (id_expr_token
->location
,
4514 "local variable %qD may not appear in this context",
4516 return error_mark_node
;
4521 decl
= (finish_id_expression
4522 (id_expression
, decl
, parser
->scope
,
4524 parser
->integral_constant_expression_p
,
4525 parser
->allow_non_integral_constant_expression_p
,
4526 &parser
->non_integral_constant_expression_p
,
4527 template_p
, done
, address_p
,
4530 id_expr_token
->location
));
4532 cp_parser_error (parser
, error_msg
);
4536 /* Anything else is an error. */
4538 cp_parser_error (parser
, "expected primary-expression");
4539 return error_mark_node
;
4544 cp_parser_primary_expression (cp_parser
*parser
,
4547 bool template_arg_p
,
4550 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4551 /*decltype*/false, idk
);
4554 /* Parse an id-expression.
4561 :: [opt] nested-name-specifier template [opt] unqualified-id
4563 :: operator-function-id
4566 Return a representation of the unqualified portion of the
4567 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4568 a `::' or nested-name-specifier.
4570 Often, if the id-expression was a qualified-id, the caller will
4571 want to make a SCOPE_REF to represent the qualified-id. This
4572 function does not do this in order to avoid wastefully creating
4573 SCOPE_REFs when they are not required.
4575 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4578 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4579 uninstantiated templates.
4581 If *TEMPLATE_P is non-NULL, it is set to true iff the
4582 `template' keyword is used to explicitly indicate that the entity
4583 named is a template.
4585 If DECLARATOR_P is true, the id-expression is appearing as part of
4586 a declarator, rather than as part of an expression. */
4589 cp_parser_id_expression (cp_parser
*parser
,
4590 bool template_keyword_p
,
4591 bool check_dependency_p
,
4596 bool global_scope_p
;
4597 bool nested_name_specifier_p
;
4599 /* Assume the `template' keyword was not used. */
4601 *template_p
= template_keyword_p
;
4603 /* Look for the optional `::' operator. */
4605 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4607 /* Look for the optional nested-name-specifier. */
4608 nested_name_specifier_p
4609 = (cp_parser_nested_name_specifier_opt (parser
,
4610 /*typename_keyword_p=*/false,
4615 /* If there is a nested-name-specifier, then we are looking at
4616 the first qualified-id production. */
4617 if (nested_name_specifier_p
)
4620 tree saved_object_scope
;
4621 tree saved_qualifying_scope
;
4622 tree unqualified_id
;
4625 /* See if the next token is the `template' keyword. */
4627 template_p
= &is_template
;
4628 *template_p
= cp_parser_optional_template_keyword (parser
);
4629 /* Name lookup we do during the processing of the
4630 unqualified-id might obliterate SCOPE. */
4631 saved_scope
= parser
->scope
;
4632 saved_object_scope
= parser
->object_scope
;
4633 saved_qualifying_scope
= parser
->qualifying_scope
;
4634 /* Process the final unqualified-id. */
4635 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4638 /*optional_p=*/false);
4639 /* Restore the SAVED_SCOPE for our caller. */
4640 parser
->scope
= saved_scope
;
4641 parser
->object_scope
= saved_object_scope
;
4642 parser
->qualifying_scope
= saved_qualifying_scope
;
4644 return unqualified_id
;
4646 /* Otherwise, if we are in global scope, then we are looking at one
4647 of the other qualified-id productions. */
4648 else if (global_scope_p
)
4653 /* Peek at the next token. */
4654 token
= cp_lexer_peek_token (parser
->lexer
);
4656 /* If it's an identifier, and the next token is not a "<", then
4657 we can avoid the template-id case. This is an optimization
4658 for this common case. */
4659 if (token
->type
== CPP_NAME
4660 && !cp_parser_nth_token_starts_template_argument_list_p
4662 return cp_parser_identifier (parser
);
4664 cp_parser_parse_tentatively (parser
);
4665 /* Try a template-id. */
4666 id
= cp_parser_template_id (parser
,
4667 /*template_keyword_p=*/false,
4668 /*check_dependency_p=*/true,
4671 /* If that worked, we're done. */
4672 if (cp_parser_parse_definitely (parser
))
4675 /* Peek at the next token. (Changes in the token buffer may
4676 have invalidated the pointer obtained above.) */
4677 token
= cp_lexer_peek_token (parser
->lexer
);
4679 switch (token
->type
)
4682 return cp_parser_identifier (parser
);
4685 if (token
->keyword
== RID_OPERATOR
)
4686 return cp_parser_operator_function_id (parser
);
4690 cp_parser_error (parser
, "expected id-expression");
4691 return error_mark_node
;
4695 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4696 /*check_dependency_p=*/true,
4701 /* Parse an unqualified-id.
4705 operator-function-id
4706 conversion-function-id
4710 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4711 keyword, in a construct like `A::template ...'.
4713 Returns a representation of unqualified-id. For the `identifier'
4714 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4715 production a BIT_NOT_EXPR is returned; the operand of the
4716 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4717 other productions, see the documentation accompanying the
4718 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4719 names are looked up in uninstantiated templates. If DECLARATOR_P
4720 is true, the unqualified-id is appearing as part of a declarator,
4721 rather than as part of an expression. */
4724 cp_parser_unqualified_id (cp_parser
* parser
,
4725 bool template_keyword_p
,
4726 bool check_dependency_p
,
4732 /* Peek at the next token. */
4733 token
= cp_lexer_peek_token (parser
->lexer
);
4735 switch (token
->type
)
4741 /* We don't know yet whether or not this will be a
4743 cp_parser_parse_tentatively (parser
);
4744 /* Try a template-id. */
4745 id
= cp_parser_template_id (parser
, template_keyword_p
,
4749 /* If it worked, we're done. */
4750 if (cp_parser_parse_definitely (parser
))
4752 /* Otherwise, it's an ordinary identifier. */
4753 return cp_parser_identifier (parser
);
4756 case CPP_TEMPLATE_ID
:
4757 return cp_parser_template_id (parser
, template_keyword_p
,
4765 tree qualifying_scope
;
4770 /* Consume the `~' token. */
4771 cp_lexer_consume_token (parser
->lexer
);
4772 /* Parse the class-name. The standard, as written, seems to
4775 template <typename T> struct S { ~S (); };
4776 template <typename T> S<T>::~S() {}
4778 is invalid, since `~' must be followed by a class-name, but
4779 `S<T>' is dependent, and so not known to be a class.
4780 That's not right; we need to look in uninstantiated
4781 templates. A further complication arises from:
4783 template <typename T> void f(T t) {
4787 Here, it is not possible to look up `T' in the scope of `T'
4788 itself. We must look in both the current scope, and the
4789 scope of the containing complete expression.
4791 Yet another issue is:
4800 The standard does not seem to say that the `S' in `~S'
4801 should refer to the type `S' and not the data member
4804 /* DR 244 says that we look up the name after the "~" in the
4805 same scope as we looked up the qualifying name. That idea
4806 isn't fully worked out; it's more complicated than that. */
4807 scope
= parser
->scope
;
4808 object_scope
= parser
->object_scope
;
4809 qualifying_scope
= parser
->qualifying_scope
;
4811 /* Check for invalid scopes. */
4812 if (scope
== error_mark_node
)
4814 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4815 cp_lexer_consume_token (parser
->lexer
);
4816 return error_mark_node
;
4818 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4820 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4821 error_at (token
->location
,
4822 "scope %qT before %<~%> is not a class-name",
4824 cp_parser_simulate_error (parser
);
4825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4826 cp_lexer_consume_token (parser
->lexer
);
4827 return error_mark_node
;
4829 gcc_assert (!scope
|| TYPE_P (scope
));
4831 /* If the name is of the form "X::~X" it's OK even if X is a
4833 token
= cp_lexer_peek_token (parser
->lexer
);
4835 && token
->type
== CPP_NAME
4836 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4838 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4839 || (CLASS_TYPE_P (scope
)
4840 && constructor_name_p (token
->u
.value
, scope
))))
4842 cp_lexer_consume_token (parser
->lexer
);
4843 return build_nt (BIT_NOT_EXPR
, scope
);
4846 /* ~auto means the destructor of whatever the object is. */
4847 if (cp_parser_is_keyword (token
, RID_AUTO
))
4849 if (cxx_dialect
< cxx1y
)
4850 pedwarn (input_location
, 0,
4851 "%<~auto%> only available with "
4852 "-std=c++1y or -std=gnu++1y");
4853 cp_lexer_consume_token (parser
->lexer
);
4854 return build_nt (BIT_NOT_EXPR
, make_auto ());
4857 /* If there was an explicit qualification (S::~T), first look
4858 in the scope given by the qualification (i.e., S).
4860 Note: in the calls to cp_parser_class_name below we pass
4861 typename_type so that lookup finds the injected-class-name
4862 rather than the constructor. */
4864 type_decl
= NULL_TREE
;
4867 cp_parser_parse_tentatively (parser
);
4868 type_decl
= cp_parser_class_name (parser
,
4869 /*typename_keyword_p=*/false,
4870 /*template_keyword_p=*/false,
4872 /*check_dependency=*/false,
4873 /*class_head_p=*/false,
4875 if (cp_parser_parse_definitely (parser
))
4878 /* In "N::S::~S", look in "N" as well. */
4879 if (!done
&& scope
&& qualifying_scope
)
4881 cp_parser_parse_tentatively (parser
);
4882 parser
->scope
= qualifying_scope
;
4883 parser
->object_scope
= NULL_TREE
;
4884 parser
->qualifying_scope
= NULL_TREE
;
4886 = cp_parser_class_name (parser
,
4887 /*typename_keyword_p=*/false,
4888 /*template_keyword_p=*/false,
4890 /*check_dependency=*/false,
4891 /*class_head_p=*/false,
4893 if (cp_parser_parse_definitely (parser
))
4896 /* In "p->S::~T", look in the scope given by "*p" as well. */
4897 else if (!done
&& object_scope
)
4899 cp_parser_parse_tentatively (parser
);
4900 parser
->scope
= object_scope
;
4901 parser
->object_scope
= NULL_TREE
;
4902 parser
->qualifying_scope
= NULL_TREE
;
4904 = cp_parser_class_name (parser
,
4905 /*typename_keyword_p=*/false,
4906 /*template_keyword_p=*/false,
4908 /*check_dependency=*/false,
4909 /*class_head_p=*/false,
4911 if (cp_parser_parse_definitely (parser
))
4914 /* Look in the surrounding context. */
4917 parser
->scope
= NULL_TREE
;
4918 parser
->object_scope
= NULL_TREE
;
4919 parser
->qualifying_scope
= NULL_TREE
;
4920 if (processing_template_decl
)
4921 cp_parser_parse_tentatively (parser
);
4923 = cp_parser_class_name (parser
,
4924 /*typename_keyword_p=*/false,
4925 /*template_keyword_p=*/false,
4927 /*check_dependency=*/false,
4928 /*class_head_p=*/false,
4930 if (processing_template_decl
4931 && ! cp_parser_parse_definitely (parser
))
4933 /* We couldn't find a type with this name, so just accept
4934 it and check for a match at instantiation time. */
4935 type_decl
= cp_parser_identifier (parser
);
4936 if (type_decl
!= error_mark_node
)
4937 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
4941 /* If an error occurred, assume that the name of the
4942 destructor is the same as the name of the qualifying
4943 class. That allows us to keep parsing after running
4944 into ill-formed destructor names. */
4945 if (type_decl
== error_mark_node
&& scope
)
4946 return build_nt (BIT_NOT_EXPR
, scope
);
4947 else if (type_decl
== error_mark_node
)
4948 return error_mark_node
;
4950 /* Check that destructor name and scope match. */
4951 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
4953 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4954 error_at (token
->location
,
4955 "declaration of %<~%T%> as member of %qT",
4957 cp_parser_simulate_error (parser
);
4958 return error_mark_node
;
4963 A typedef-name that names a class shall not be used as the
4964 identifier in the declarator for a destructor declaration. */
4966 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
4967 && !DECL_SELF_REFERENCE_P (type_decl
)
4968 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4969 error_at (token
->location
,
4970 "typedef-name %qD used as destructor declarator",
4973 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
4977 if (token
->keyword
== RID_OPERATOR
)
4981 /* This could be a template-id, so we try that first. */
4982 cp_parser_parse_tentatively (parser
);
4983 /* Try a template-id. */
4984 id
= cp_parser_template_id (parser
, template_keyword_p
,
4985 /*check_dependency_p=*/true,
4988 /* If that worked, we're done. */
4989 if (cp_parser_parse_definitely (parser
))
4991 /* We still don't know whether we're looking at an
4992 operator-function-id or a conversion-function-id. */
4993 cp_parser_parse_tentatively (parser
);
4994 /* Try an operator-function-id. */
4995 id
= cp_parser_operator_function_id (parser
);
4996 /* If that didn't work, try a conversion-function-id. */
4997 if (!cp_parser_parse_definitely (parser
))
4998 id
= cp_parser_conversion_function_id (parser
);
4999 else if (UDLIT_OPER_P (id
))
5002 const char *name
= UDLIT_OP_SUFFIX (id
);
5003 if (name
[0] != '_' && !in_system_header
)
5004 warning (0, "literal operator suffixes not preceded by %<_%>"
5005 " are reserved for future standardization");
5015 cp_parser_error (parser
, "expected unqualified-id");
5016 return error_mark_node
;
5020 /* Parse an (optional) nested-name-specifier.
5022 nested-name-specifier: [C++98]
5023 class-or-namespace-name :: nested-name-specifier [opt]
5024 class-or-namespace-name :: template nested-name-specifier [opt]
5026 nested-name-specifier: [C++0x]
5029 nested-name-specifier identifier ::
5030 nested-name-specifier template [opt] simple-template-id ::
5032 PARSER->SCOPE should be set appropriately before this function is
5033 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5034 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5037 Sets PARSER->SCOPE to the class (TYPE) or namespace
5038 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5039 it unchanged if there is no nested-name-specifier. Returns the new
5040 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5042 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5043 part of a declaration and/or decl-specifier. */
5046 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5047 bool typename_keyword_p
,
5048 bool check_dependency_p
,
5050 bool is_declaration
)
5052 bool success
= false;
5053 cp_token_position start
= 0;
5056 /* Remember where the nested-name-specifier starts. */
5057 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5059 start
= cp_lexer_token_position (parser
->lexer
, false);
5060 push_deferring_access_checks (dk_deferred
);
5067 tree saved_qualifying_scope
;
5068 bool template_keyword_p
;
5070 /* Spot cases that cannot be the beginning of a
5071 nested-name-specifier. */
5072 token
= cp_lexer_peek_token (parser
->lexer
);
5074 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5075 the already parsed nested-name-specifier. */
5076 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5078 /* Grab the nested-name-specifier and continue the loop. */
5079 cp_parser_pre_parsed_nested_name_specifier (parser
);
5080 /* If we originally encountered this nested-name-specifier
5081 with IS_DECLARATION set to false, we will not have
5082 resolved TYPENAME_TYPEs, so we must do so here. */
5084 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5086 new_scope
= resolve_typename_type (parser
->scope
,
5087 /*only_current_p=*/false);
5088 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5089 parser
->scope
= new_scope
;
5095 /* Spot cases that cannot be the beginning of a
5096 nested-name-specifier. On the second and subsequent times
5097 through the loop, we look for the `template' keyword. */
5098 if (success
&& token
->keyword
== RID_TEMPLATE
)
5100 /* A template-id can start a nested-name-specifier. */
5101 else if (token
->type
== CPP_TEMPLATE_ID
)
5103 /* DR 743: decltype can be used in a nested-name-specifier. */
5104 else if (token_is_decltype (token
))
5108 /* If the next token is not an identifier, then it is
5109 definitely not a type-name or namespace-name. */
5110 if (token
->type
!= CPP_NAME
)
5112 /* If the following token is neither a `<' (to begin a
5113 template-id), nor a `::', then we are not looking at a
5114 nested-name-specifier. */
5115 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5117 if (token
->type
== CPP_COLON
5118 && parser
->colon_corrects_to_scope_p
5119 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5121 error_at (token
->location
,
5122 "found %<:%> in nested-name-specifier, expected %<::%>");
5123 token
->type
= CPP_SCOPE
;
5126 if (token
->type
!= CPP_SCOPE
5127 && !cp_parser_nth_token_starts_template_argument_list_p
5132 /* The nested-name-specifier is optional, so we parse
5134 cp_parser_parse_tentatively (parser
);
5136 /* Look for the optional `template' keyword, if this isn't the
5137 first time through the loop. */
5139 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5141 template_keyword_p
= false;
5143 /* Save the old scope since the name lookup we are about to do
5144 might destroy it. */
5145 old_scope
= parser
->scope
;
5146 saved_qualifying_scope
= parser
->qualifying_scope
;
5147 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5148 look up names in "X<T>::I" in order to determine that "Y" is
5149 a template. So, if we have a typename at this point, we make
5150 an effort to look through it. */
5152 && !typename_keyword_p
5154 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5155 parser
->scope
= resolve_typename_type (parser
->scope
,
5156 /*only_current_p=*/false);
5157 /* Parse the qualifying entity. */
5159 = cp_parser_qualifying_entity (parser
,
5165 /* Look for the `::' token. */
5166 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5168 /* If we found what we wanted, we keep going; otherwise, we're
5170 if (!cp_parser_parse_definitely (parser
))
5172 bool error_p
= false;
5174 /* Restore the OLD_SCOPE since it was valid before the
5175 failed attempt at finding the last
5176 class-or-namespace-name. */
5177 parser
->scope
= old_scope
;
5178 parser
->qualifying_scope
= saved_qualifying_scope
;
5180 /* If the next token is a decltype, and the one after that is a
5181 `::', then the decltype has failed to resolve to a class or
5182 enumeration type. Give this error even when parsing
5183 tentatively since it can't possibly be valid--and we're going
5184 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5185 won't get another chance.*/
5186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5187 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5190 token
= cp_lexer_consume_token (parser
->lexer
);
5191 error_at (token
->location
, "decltype evaluates to %qT, "
5192 "which is not a class or enumeration type",
5194 parser
->scope
= error_mark_node
;
5198 cp_lexer_consume_token (parser
->lexer
);
5201 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5203 /* If the next token is an identifier, and the one after
5204 that is a `::', then any valid interpretation would have
5205 found a class-or-namespace-name. */
5206 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5207 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5209 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5212 token
= cp_lexer_consume_token (parser
->lexer
);
5215 if (!token
->ambiguous_p
)
5218 tree ambiguous_decls
;
5220 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5222 /*is_template=*/false,
5223 /*is_namespace=*/false,
5224 /*check_dependency=*/true,
5227 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5228 error_at (token
->location
,
5229 "%qD used without template parameters",
5231 else if (ambiguous_decls
)
5233 error_at (token
->location
,
5234 "reference to %qD is ambiguous",
5236 print_candidates (ambiguous_decls
);
5237 decl
= error_mark_node
;
5241 if (cxx_dialect
!= cxx98
)
5242 cp_parser_name_lookup_error
5243 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5246 cp_parser_name_lookup_error
5247 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5251 parser
->scope
= error_mark_node
;
5253 /* Treat this as a successful nested-name-specifier
5258 If the name found is not a class-name (clause
5259 _class_) or namespace-name (_namespace.def_), the
5260 program is ill-formed. */
5263 cp_lexer_consume_token (parser
->lexer
);
5267 /* We've found one valid nested-name-specifier. */
5269 /* Name lookup always gives us a DECL. */
5270 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5271 new_scope
= TREE_TYPE (new_scope
);
5272 /* Uses of "template" must be followed by actual templates. */
5273 if (template_keyword_p
5274 && !(CLASS_TYPE_P (new_scope
)
5275 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5276 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5277 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5278 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5279 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5280 == TEMPLATE_ID_EXPR
)))
5281 permerror (input_location
, TYPE_P (new_scope
)
5282 ? G_("%qT is not a template")
5283 : G_("%qD is not a template"),
5285 /* If it is a class scope, try to complete it; we are about to
5286 be looking up names inside the class. */
5287 if (TYPE_P (new_scope
)
5288 /* Since checking types for dependency can be expensive,
5289 avoid doing it if the type is already complete. */
5290 && !COMPLETE_TYPE_P (new_scope
)
5291 /* Do not try to complete dependent types. */
5292 && !dependent_type_p (new_scope
))
5294 new_scope
= complete_type (new_scope
);
5295 /* If it is a typedef to current class, use the current
5296 class instead, as the typedef won't have any names inside
5298 if (!COMPLETE_TYPE_P (new_scope
)
5299 && currently_open_class (new_scope
))
5300 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5302 /* Make sure we look in the right scope the next time through
5304 parser
->scope
= new_scope
;
5307 /* If parsing tentatively, replace the sequence of tokens that makes
5308 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5309 token. That way, should we re-parse the token stream, we will
5310 not have to repeat the effort required to do the parse, nor will
5311 we issue duplicate error messages. */
5312 if (success
&& start
)
5316 token
= cp_lexer_token_at (parser
->lexer
, start
);
5317 /* Reset the contents of the START token. */
5318 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5319 /* Retrieve any deferred checks. Do not pop this access checks yet
5320 so the memory will not be reclaimed during token replacing below. */
5321 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
5322 token
->u
.tree_check_value
->value
= parser
->scope
;
5323 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5324 token
->u
.tree_check_value
->qualifying_scope
=
5325 parser
->qualifying_scope
;
5326 token
->keyword
= RID_MAX
;
5328 /* Purge all subsequent tokens. */
5329 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5333 pop_to_parent_deferring_access_checks ();
5335 return success
? parser
->scope
: NULL_TREE
;
5338 /* Parse a nested-name-specifier. See
5339 cp_parser_nested_name_specifier_opt for details. This function
5340 behaves identically, except that it will an issue an error if no
5341 nested-name-specifier is present. */
5344 cp_parser_nested_name_specifier (cp_parser
*parser
,
5345 bool typename_keyword_p
,
5346 bool check_dependency_p
,
5348 bool is_declaration
)
5352 /* Look for the nested-name-specifier. */
5353 scope
= cp_parser_nested_name_specifier_opt (parser
,
5358 /* If it was not present, issue an error message. */
5361 cp_parser_error (parser
, "expected nested-name-specifier");
5362 parser
->scope
= NULL_TREE
;
5368 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5369 this is either a class-name or a namespace-name (which corresponds
5370 to the class-or-namespace-name production in the grammar). For
5371 C++0x, it can also be a type-name that refers to an enumeration
5372 type or a simple-template-id.
5374 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5375 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5376 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5377 TYPE_P is TRUE iff the next name should be taken as a class-name,
5378 even the same name is declared to be another entity in the same
5381 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5382 specified by the class-or-namespace-name. If neither is found the
5383 ERROR_MARK_NODE is returned. */
5386 cp_parser_qualifying_entity (cp_parser
*parser
,
5387 bool typename_keyword_p
,
5388 bool template_keyword_p
,
5389 bool check_dependency_p
,
5391 bool is_declaration
)
5394 tree saved_qualifying_scope
;
5395 tree saved_object_scope
;
5398 bool successful_parse_p
;
5400 /* DR 743: decltype can appear in a nested-name-specifier. */
5401 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5403 scope
= cp_parser_decltype (parser
);
5404 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5405 && !MAYBE_CLASS_TYPE_P (scope
))
5407 cp_parser_simulate_error (parser
);
5408 return error_mark_node
;
5410 if (TYPE_NAME (scope
))
5411 scope
= TYPE_NAME (scope
);
5415 /* Before we try to parse the class-name, we must save away the
5416 current PARSER->SCOPE since cp_parser_class_name will destroy
5418 saved_scope
= parser
->scope
;
5419 saved_qualifying_scope
= parser
->qualifying_scope
;
5420 saved_object_scope
= parser
->object_scope
;
5421 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5422 there is no need to look for a namespace-name. */
5423 only_class_p
= template_keyword_p
5424 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5426 cp_parser_parse_tentatively (parser
);
5427 scope
= cp_parser_class_name (parser
,
5430 type_p
? class_type
: none_type
,
5432 /*class_head_p=*/false,
5434 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5435 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5437 && cxx_dialect
!= cxx98
5438 && !successful_parse_p
)
5440 /* Restore the saved scope. */
5441 parser
->scope
= saved_scope
;
5442 parser
->qualifying_scope
= saved_qualifying_scope
;
5443 parser
->object_scope
= saved_object_scope
;
5445 /* Parse tentatively. */
5446 cp_parser_parse_tentatively (parser
);
5448 /* Parse a type-name */
5449 scope
= cp_parser_type_name (parser
);
5451 /* "If the name found does not designate a namespace or a class,
5452 enumeration, or dependent type, the program is ill-formed."
5454 We cover classes and dependent types above and namespaces below,
5455 so this code is only looking for enums. */
5456 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5457 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5458 cp_parser_simulate_error (parser
);
5460 successful_parse_p
= cp_parser_parse_definitely (parser
);
5462 /* If that didn't work, try for a namespace-name. */
5463 if (!only_class_p
&& !successful_parse_p
)
5465 /* Restore the saved scope. */
5466 parser
->scope
= saved_scope
;
5467 parser
->qualifying_scope
= saved_qualifying_scope
;
5468 parser
->object_scope
= saved_object_scope
;
5469 /* If we are not looking at an identifier followed by the scope
5470 resolution operator, then this is not part of a
5471 nested-name-specifier. (Note that this function is only used
5472 to parse the components of a nested-name-specifier.) */
5473 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5474 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5475 return error_mark_node
;
5476 scope
= cp_parser_namespace_name (parser
);
5482 /* Parse a postfix-expression.
5486 postfix-expression [ expression ]
5487 postfix-expression ( expression-list [opt] )
5488 simple-type-specifier ( expression-list [opt] )
5489 typename :: [opt] nested-name-specifier identifier
5490 ( expression-list [opt] )
5491 typename :: [opt] nested-name-specifier template [opt] template-id
5492 ( expression-list [opt] )
5493 postfix-expression . template [opt] id-expression
5494 postfix-expression -> template [opt] id-expression
5495 postfix-expression . pseudo-destructor-name
5496 postfix-expression -> pseudo-destructor-name
5497 postfix-expression ++
5498 postfix-expression --
5499 dynamic_cast < type-id > ( expression )
5500 static_cast < type-id > ( expression )
5501 reinterpret_cast < type-id > ( expression )
5502 const_cast < type-id > ( expression )
5503 typeid ( expression )
5509 ( type-id ) { initializer-list , [opt] }
5511 This extension is a GNU version of the C99 compound-literal
5512 construct. (The C99 grammar uses `type-name' instead of `type-id',
5513 but they are essentially the same concept.)
5515 If ADDRESS_P is true, the postfix expression is the operand of the
5516 `&' operator. CAST_P is true if this expression is the target of a
5519 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5520 class member access expressions [expr.ref].
5522 Returns a representation of the expression. */
5525 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5526 bool member_access_only_p
, bool decltype_p
,
5527 cp_id_kind
* pidk_return
)
5531 cp_id_kind idk
= CP_ID_KIND_NONE
;
5532 tree postfix_expression
= NULL_TREE
;
5533 bool is_member_access
= false;
5535 /* Peek at the next token. */
5536 token
= cp_lexer_peek_token (parser
->lexer
);
5537 /* Some of the productions are determined by keywords. */
5538 keyword
= token
->keyword
;
5548 const char *saved_message
;
5550 /* All of these can be handled in the same way from the point
5551 of view of parsing. Begin by consuming the token
5552 identifying the cast. */
5553 cp_lexer_consume_token (parser
->lexer
);
5555 /* New types cannot be defined in the cast. */
5556 saved_message
= parser
->type_definition_forbidden_message
;
5557 parser
->type_definition_forbidden_message
5558 = G_("types may not be defined in casts");
5560 /* Look for the opening `<'. */
5561 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5562 /* Parse the type to which we are casting. */
5563 type
= cp_parser_type_id (parser
);
5564 /* Look for the closing `>'. */
5565 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5566 /* Restore the old message. */
5567 parser
->type_definition_forbidden_message
= saved_message
;
5569 /* And the expression which is being cast. */
5570 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5571 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
5572 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5574 /* Only type conversions to integral or enumeration types
5575 can be used in constant-expressions. */
5576 if (!cast_valid_in_integral_constant_expression_p (type
)
5577 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5578 return error_mark_node
;
5584 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5588 = build_static_cast (type
, expression
, tf_warning_or_error
);
5592 = build_reinterpret_cast (type
, expression
,
5593 tf_warning_or_error
);
5597 = build_const_cast (type
, expression
, tf_warning_or_error
);
5608 const char *saved_message
;
5609 bool saved_in_type_id_in_expr_p
;
5611 /* Consume the `typeid' token. */
5612 cp_lexer_consume_token (parser
->lexer
);
5613 /* Look for the `(' token. */
5614 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5615 /* Types cannot be defined in a `typeid' expression. */
5616 saved_message
= parser
->type_definition_forbidden_message
;
5617 parser
->type_definition_forbidden_message
5618 = G_("types may not be defined in a %<typeid%> expression");
5619 /* We can't be sure yet whether we're looking at a type-id or an
5621 cp_parser_parse_tentatively (parser
);
5622 /* Try a type-id first. */
5623 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5624 parser
->in_type_id_in_expr_p
= true;
5625 type
= cp_parser_type_id (parser
);
5626 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5627 /* Look for the `)' token. Otherwise, we can't be sure that
5628 we're not looking at an expression: consider `typeid (int
5629 (3))', for example. */
5630 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5631 /* If all went well, simply lookup the type-id. */
5632 if (cp_parser_parse_definitely (parser
))
5633 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5634 /* Otherwise, fall back to the expression variant. */
5639 /* Look for an expression. */
5640 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
5641 /* Compute its typeid. */
5642 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5643 /* Look for the `)' token. */
5644 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5646 /* Restore the saved message. */
5647 parser
->type_definition_forbidden_message
= saved_message
;
5648 /* `typeid' may not appear in an integral constant expression. */
5649 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5650 return error_mark_node
;
5657 /* The syntax permitted here is the same permitted for an
5658 elaborated-type-specifier. */
5659 type
= cp_parser_elaborated_type_specifier (parser
,
5660 /*is_friend=*/false,
5661 /*is_declaration=*/false);
5662 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5666 case RID_BUILTIN_SHUFFLE
:
5668 vec
<tree
, va_gc
> *vec
;
5671 location_t loc
= token
->location
;
5673 cp_lexer_consume_token (parser
->lexer
);
5674 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5675 /*cast_p=*/false, /*allow_expansion_p=*/true,
5676 /*non_constant_p=*/NULL
);
5678 return error_mark_node
;
5680 FOR_EACH_VEC_ELT (*vec
, i
, p
)
5683 if (vec
->length () == 2)
5684 return c_build_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1]);
5685 else if (vec
->length () == 3)
5686 return c_build_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2]);
5689 error_at (loc
, "wrong number of arguments to "
5690 "%<__builtin_shuffle%>");
5691 return error_mark_node
;
5700 /* If the next thing is a simple-type-specifier, we may be
5701 looking at a functional cast. We could also be looking at
5702 an id-expression. So, we try the functional cast, and if
5703 that doesn't work we fall back to the primary-expression. */
5704 cp_parser_parse_tentatively (parser
);
5705 /* Look for the simple-type-specifier. */
5706 type
= cp_parser_simple_type_specifier (parser
,
5707 /*decl_specs=*/NULL
,
5708 CP_PARSER_FLAGS_NONE
);
5709 /* Parse the cast itself. */
5710 if (!cp_parser_error_occurred (parser
))
5712 = cp_parser_functional_cast (parser
, type
);
5713 /* If that worked, we're done. */
5714 if (cp_parser_parse_definitely (parser
))
5717 /* If the functional-cast didn't work out, try a
5718 compound-literal. */
5719 if (cp_parser_allow_gnu_extensions_p (parser
)
5720 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5722 vec
<constructor_elt
, va_gc
> *initializer_list
= NULL
;
5723 bool saved_in_type_id_in_expr_p
;
5725 cp_parser_parse_tentatively (parser
);
5726 /* Consume the `('. */
5727 cp_lexer_consume_token (parser
->lexer
);
5728 /* Parse the type. */
5729 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5730 parser
->in_type_id_in_expr_p
= true;
5731 type
= cp_parser_type_id (parser
);
5732 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5733 /* Look for the `)'. */
5734 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5735 /* Look for the `{'. */
5736 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
5737 /* If things aren't going well, there's no need to
5739 if (!cp_parser_error_occurred (parser
))
5741 bool non_constant_p
;
5742 /* Parse the initializer-list. */
5744 = cp_parser_initializer_list (parser
, &non_constant_p
);
5745 /* Allow a trailing `,'. */
5746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
5747 cp_lexer_consume_token (parser
->lexer
);
5748 /* Look for the final `}'. */
5749 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
5751 /* If that worked, we're definitely looking at a
5752 compound-literal expression. */
5753 if (cp_parser_parse_definitely (parser
))
5755 /* Warn the user that a compound literal is not
5756 allowed in standard C++. */
5757 pedwarn (input_location
, OPT_Wpedantic
, "ISO C++ forbids compound-literals");
5758 /* For simplicity, we disallow compound literals in
5759 constant-expressions. We could
5760 allow compound literals of integer type, whose
5761 initializer was a constant, in constant
5762 expressions. Permitting that usage, as a further
5763 extension, would not change the meaning of any
5764 currently accepted programs. (Of course, as
5765 compound literals are not part of ISO C++, the
5766 standard has nothing to say.) */
5767 if (cp_parser_non_integral_constant_expression (parser
,
5770 postfix_expression
= error_mark_node
;
5773 /* Form the representation of the compound-literal. */
5775 = (finish_compound_literal
5776 (type
, build_constructor (init_list_type_node
,
5778 tf_warning_or_error
));
5783 /* It must be a primary-expression. */
5785 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5786 /*template_arg_p=*/false,
5793 /* Note that we don't need to worry about calling build_cplus_new on a
5794 class-valued CALL_EXPR in decltype when it isn't the end of the
5795 postfix-expression; unary_complex_lvalue will take care of that for
5798 /* Keep looping until the postfix-expression is complete. */
5801 if (idk
== CP_ID_KIND_UNQUALIFIED
5802 && identifier_p (postfix_expression
)
5803 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
5804 /* It is not a Koenig lookup function call. */
5806 = unqualified_name_lookup_error (postfix_expression
);
5808 /* Peek at the next token. */
5809 token
= cp_lexer_peek_token (parser
->lexer
);
5811 switch (token
->type
)
5813 case CPP_OPEN_SQUARE
:
5814 if (cp_next_tokens_can_be_std_attribute_p (parser
))
5816 cp_parser_error (parser
,
5817 "two consecutive %<[%> shall "
5818 "only introduce an attribute");
5819 return error_mark_node
;
5822 = cp_parser_postfix_open_square_expression (parser
,
5826 idk
= CP_ID_KIND_NONE
;
5827 is_member_access
= false;
5830 case CPP_OPEN_PAREN
:
5831 /* postfix-expression ( expression-list [opt] ) */
5834 bool is_builtin_constant_p
;
5835 bool saved_integral_constant_expression_p
= false;
5836 bool saved_non_integral_constant_expression_p
= false;
5837 tsubst_flags_t complain
= complain_flags (decltype_p
);
5838 vec
<tree
, va_gc
> *args
;
5840 is_member_access
= false;
5842 is_builtin_constant_p
5843 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
5844 if (is_builtin_constant_p
)
5846 /* The whole point of __builtin_constant_p is to allow
5847 non-constant expressions to appear as arguments. */
5848 saved_integral_constant_expression_p
5849 = parser
->integral_constant_expression_p
;
5850 saved_non_integral_constant_expression_p
5851 = parser
->non_integral_constant_expression_p
;
5852 parser
->integral_constant_expression_p
= false;
5854 args
= (cp_parser_parenthesized_expression_list
5856 /*cast_p=*/false, /*allow_expansion_p=*/true,
5857 /*non_constant_p=*/NULL
));
5858 if (is_builtin_constant_p
)
5860 parser
->integral_constant_expression_p
5861 = saved_integral_constant_expression_p
;
5862 parser
->non_integral_constant_expression_p
5863 = saved_non_integral_constant_expression_p
;
5868 postfix_expression
= error_mark_node
;
5872 /* Function calls are not permitted in
5873 constant-expressions. */
5874 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
5875 && cp_parser_non_integral_constant_expression (parser
,
5878 postfix_expression
= error_mark_node
;
5879 release_tree_vector (args
);
5884 if (idk
== CP_ID_KIND_UNQUALIFIED
5885 || idk
== CP_ID_KIND_TEMPLATE_ID
)
5887 if (identifier_p (postfix_expression
))
5889 if (!args
->is_empty ())
5892 if (!any_type_dependent_arguments_p (args
))
5894 = perform_koenig_lookup (postfix_expression
, args
,
5895 /*include_std=*/false,
5900 = unqualified_fn_lookup_error (postfix_expression
);
5902 /* We do not perform argument-dependent lookup if
5903 normal lookup finds a non-function, in accordance
5904 with the expected resolution of DR 218. */
5905 else if (!args
->is_empty ()
5906 && is_overloaded_fn (postfix_expression
))
5908 tree fn
= get_first_fn (postfix_expression
);
5909 fn
= STRIP_TEMPLATE (fn
);
5911 /* Do not do argument dependent lookup if regular
5912 lookup finds a member function or a block-scope
5913 function declaration. [basic.lookup.argdep]/3 */
5914 if (!DECL_FUNCTION_MEMBER_P (fn
)
5915 && !DECL_LOCAL_FUNCTION_P (fn
))
5918 if (!any_type_dependent_arguments_p (args
))
5920 = perform_koenig_lookup (postfix_expression
, args
,
5921 /*include_std=*/false,
5927 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
5929 tree instance
= TREE_OPERAND (postfix_expression
, 0);
5930 tree fn
= TREE_OPERAND (postfix_expression
, 1);
5932 if (processing_template_decl
5933 && (type_dependent_expression_p (instance
)
5934 || (!BASELINK_P (fn
)
5935 && TREE_CODE (fn
) != FIELD_DECL
)
5936 || type_dependent_expression_p (fn
)
5937 || any_type_dependent_arguments_p (args
)))
5940 = build_nt_call_vec (postfix_expression
, args
);
5941 release_tree_vector (args
);
5945 if (BASELINK_P (fn
))
5948 = (build_new_method_call
5949 (instance
, fn
, &args
, NULL_TREE
,
5950 (idk
== CP_ID_KIND_QUALIFIED
5951 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
5958 = finish_call_expr (postfix_expression
, &args
,
5959 /*disallow_virtual=*/false,
5963 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
5964 || TREE_CODE (postfix_expression
) == MEMBER_REF
5965 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
5966 postfix_expression
= (build_offset_ref_call_from_tree
5967 (postfix_expression
, &args
,
5969 else if (idk
== CP_ID_KIND_QUALIFIED
)
5970 /* A call to a static class member, or a namespace-scope
5973 = finish_call_expr (postfix_expression
, &args
,
5974 /*disallow_virtual=*/true,
5978 /* All other function calls. */
5980 = finish_call_expr (postfix_expression
, &args
,
5981 /*disallow_virtual=*/false,
5985 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5986 idk
= CP_ID_KIND_NONE
;
5988 release_tree_vector (args
);
5994 /* postfix-expression . template [opt] id-expression
5995 postfix-expression . pseudo-destructor-name
5996 postfix-expression -> template [opt] id-expression
5997 postfix-expression -> pseudo-destructor-name */
5999 /* Consume the `.' or `->' operator. */
6000 cp_lexer_consume_token (parser
->lexer
);
6003 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6008 is_member_access
= true;
6012 /* postfix-expression ++ */
6013 /* Consume the `++' token. */
6014 cp_lexer_consume_token (parser
->lexer
);
6015 /* Generate a representation for the complete expression. */
6017 = finish_increment_expr (postfix_expression
,
6018 POSTINCREMENT_EXPR
);
6019 /* Increments may not appear in constant-expressions. */
6020 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6021 postfix_expression
= error_mark_node
;
6022 idk
= CP_ID_KIND_NONE
;
6023 is_member_access
= false;
6026 case CPP_MINUS_MINUS
:
6027 /* postfix-expression -- */
6028 /* Consume the `--' token. */
6029 cp_lexer_consume_token (parser
->lexer
);
6030 /* Generate a representation for the complete expression. */
6032 = finish_increment_expr (postfix_expression
,
6033 POSTDECREMENT_EXPR
);
6034 /* Decrements may not appear in constant-expressions. */
6035 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6036 postfix_expression
= error_mark_node
;
6037 idk
= CP_ID_KIND_NONE
;
6038 is_member_access
= false;
6042 if (pidk_return
!= NULL
)
6043 * pidk_return
= idk
;
6044 if (member_access_only_p
)
6045 return is_member_access
? postfix_expression
: error_mark_node
;
6047 return postfix_expression
;
6051 /* We should never get here. */
6053 return error_mark_node
;
6056 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6057 by cp_parser_builtin_offsetof. We're looking for
6059 postfix-expression [ expression ]
6060 postfix-expression [ braced-init-list ] (C++11)
6062 FOR_OFFSETOF is set if we're being called in that context, which
6063 changes how we deal with integer constant expressions. */
6066 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6067 tree postfix_expression
,
6072 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6074 /* Consume the `[' token. */
6075 cp_lexer_consume_token (parser
->lexer
);
6077 /* Parse the index expression. */
6078 /* ??? For offsetof, there is a question of what to allow here. If
6079 offsetof is not being used in an integral constant expression context,
6080 then we *could* get the right answer by computing the value at runtime.
6081 If we are in an integral constant expression context, then we might
6082 could accept any constant expression; hard to say without analysis.
6083 Rather than open the barn door too wide right away, allow only integer
6084 constant expressions here. */
6086 index
= cp_parser_constant_expression (parser
, false, NULL
);
6089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6091 bool expr_nonconst_p
;
6092 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6093 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6096 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6099 /* Look for the closing `]'. */
6100 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6102 /* Build the ARRAY_REF. */
6103 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6106 /* When not doing offsetof, array references are not permitted in
6107 constant-expressions. */
6109 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6110 postfix_expression
= error_mark_node
;
6112 return postfix_expression
;
6115 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6116 by cp_parser_builtin_offsetof. We're looking for
6118 postfix-expression . template [opt] id-expression
6119 postfix-expression . pseudo-destructor-name
6120 postfix-expression -> template [opt] id-expression
6121 postfix-expression -> pseudo-destructor-name
6123 FOR_OFFSETOF is set if we're being called in that context. That sorta
6124 limits what of the above we'll actually accept, but nevermind.
6125 TOKEN_TYPE is the "." or "->" token, which will already have been
6126 removed from the stream. */
6129 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6130 enum cpp_ttype token_type
,
6131 tree postfix_expression
,
6132 bool for_offsetof
, cp_id_kind
*idk
,
6133 location_t location
)
6137 bool pseudo_destructor_p
;
6138 tree scope
= NULL_TREE
;
6140 /* If this is a `->' operator, dereference the pointer. */
6141 if (token_type
== CPP_DEREF
)
6142 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6143 tf_warning_or_error
);
6144 /* Check to see whether or not the expression is type-dependent. */
6145 dependent_p
= type_dependent_expression_p (postfix_expression
);
6146 /* The identifier following the `->' or `.' is not qualified. */
6147 parser
->scope
= NULL_TREE
;
6148 parser
->qualifying_scope
= NULL_TREE
;
6149 parser
->object_scope
= NULL_TREE
;
6150 *idk
= CP_ID_KIND_NONE
;
6152 /* Enter the scope corresponding to the type of the object
6153 given by the POSTFIX_EXPRESSION. */
6154 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6156 scope
= TREE_TYPE (postfix_expression
);
6157 /* According to the standard, no expression should ever have
6158 reference type. Unfortunately, we do not currently match
6159 the standard in this respect in that our internal representation
6160 of an expression may have reference type even when the standard
6161 says it does not. Therefore, we have to manually obtain the
6162 underlying type here. */
6163 scope
= non_reference (scope
);
6164 /* The type of the POSTFIX_EXPRESSION must be complete. */
6165 if (scope
== unknown_type_node
)
6167 error_at (location
, "%qE does not have class type",
6168 postfix_expression
);
6171 /* Unlike the object expression in other contexts, *this is not
6172 required to be of complete type for purposes of class member
6173 access (5.2.5) outside the member function body. */
6174 else if (postfix_expression
!= current_class_ref
6175 && !(processing_template_decl
&& scope
== current_class_type
))
6176 scope
= complete_type_or_else (scope
, NULL_TREE
);
6177 /* Let the name lookup machinery know that we are processing a
6178 class member access expression. */
6179 parser
->context
->object_type
= scope
;
6180 /* If something went wrong, we want to be able to discern that case,
6181 as opposed to the case where there was no SCOPE due to the type
6182 of expression being dependent. */
6184 scope
= error_mark_node
;
6185 /* If the SCOPE was erroneous, make the various semantic analysis
6186 functions exit quickly -- and without issuing additional error
6188 if (scope
== error_mark_node
)
6189 postfix_expression
= error_mark_node
;
6192 /* Assume this expression is not a pseudo-destructor access. */
6193 pseudo_destructor_p
= false;
6195 /* If the SCOPE is a scalar type, then, if this is a valid program,
6196 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6197 is type dependent, it can be pseudo-destructor-name or something else.
6198 Try to parse it as pseudo-destructor-name first. */
6199 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6204 cp_parser_parse_tentatively (parser
);
6205 /* Parse the pseudo-destructor-name. */
6207 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6210 && (cp_parser_error_occurred (parser
)
6211 || !SCALAR_TYPE_P (type
)))
6212 cp_parser_abort_tentative_parse (parser
);
6213 else if (cp_parser_parse_definitely (parser
))
6215 pseudo_destructor_p
= true;
6217 = finish_pseudo_destructor_expr (postfix_expression
,
6222 if (!pseudo_destructor_p
)
6224 /* If the SCOPE is not a scalar type, we are looking at an
6225 ordinary class member access expression, rather than a
6226 pseudo-destructor-name. */
6228 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6229 /* Parse the id-expression. */
6230 name
= (cp_parser_id_expression
6232 cp_parser_optional_template_keyword (parser
),
6233 /*check_dependency_p=*/true,
6235 /*declarator_p=*/false,
6236 /*optional_p=*/false));
6237 /* In general, build a SCOPE_REF if the member name is qualified.
6238 However, if the name was not dependent and has already been
6239 resolved; there is no need to build the SCOPE_REF. For example;
6241 struct X { void f(); };
6242 template <typename T> void f(T* t) { t->X::f(); }
6244 Even though "t" is dependent, "X::f" is not and has been resolved
6245 to a BASELINK; there is no need to include scope information. */
6247 /* But we do need to remember that there was an explicit scope for
6248 virtual function calls. */
6250 *idk
= CP_ID_KIND_QUALIFIED
;
6252 /* If the name is a template-id that names a type, we will get a
6253 TYPE_DECL here. That is invalid code. */
6254 if (TREE_CODE (name
) == TYPE_DECL
)
6256 error_at (token
->location
, "invalid use of %qD", name
);
6257 postfix_expression
= error_mark_node
;
6261 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6263 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6265 error_at (token
->location
, "%<%D::%D%> is not a class member",
6266 parser
->scope
, name
);
6267 postfix_expression
= error_mark_node
;
6270 name
= build_qualified_name (/*type=*/NULL_TREE
,
6274 parser
->scope
= NULL_TREE
;
6275 parser
->qualifying_scope
= NULL_TREE
;
6276 parser
->object_scope
= NULL_TREE
;
6278 if (parser
->scope
&& name
&& BASELINK_P (name
))
6279 adjust_result_of_qualified_name_lookup
6280 (name
, parser
->scope
, scope
);
6282 = finish_class_member_access_expr (postfix_expression
, name
,
6284 tf_warning_or_error
);
6288 /* We no longer need to look up names in the scope of the object on
6289 the left-hand side of the `.' or `->' operator. */
6290 parser
->context
->object_type
= NULL_TREE
;
6292 /* Outside of offsetof, these operators may not appear in
6293 constant-expressions. */
6295 && (cp_parser_non_integral_constant_expression
6296 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6297 postfix_expression
= error_mark_node
;
6299 return postfix_expression
;
6302 /* Parse a parenthesized expression-list.
6305 assignment-expression
6306 expression-list, assignment-expression
6311 identifier, expression-list
6313 CAST_P is true if this expression is the target of a cast.
6315 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6318 Returns a vector of trees. Each element is a representation of an
6319 assignment-expression. NULL is returned if the ( and or ) are
6320 missing. An empty, but allocated, vector is returned on no
6321 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6322 if we are parsing an attribute list for an attribute that wants a
6323 plain identifier argument, normal_attr for an attribute that wants
6324 an expression, or non_attr if we aren't parsing an attribute list. If
6325 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6326 not all of the expressions in the list were constant. */
6328 static vec
<tree
, va_gc
> *
6329 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6330 int is_attribute_list
,
6332 bool allow_expansion_p
,
6333 bool *non_constant_p
)
6335 vec
<tree
, va_gc
> *expression_list
;
6336 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6337 tree identifier
= NULL_TREE
;
6338 bool saved_greater_than_is_operator_p
;
6340 /* Assume all the expressions will be constant. */
6342 *non_constant_p
= false;
6344 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6347 expression_list
= make_tree_vector ();
6349 /* Within a parenthesized expression, a `>' token is always
6350 the greater-than operator. */
6351 saved_greater_than_is_operator_p
6352 = parser
->greater_than_is_operator_p
;
6353 parser
->greater_than_is_operator_p
= true;
6355 /* Consume expressions until there are no more. */
6356 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6361 /* At the beginning of attribute lists, check to see if the
6362 next token is an identifier. */
6363 if (is_attribute_list
== id_attr
6364 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6368 /* Consume the identifier. */
6369 token
= cp_lexer_consume_token (parser
->lexer
);
6370 /* Save the identifier. */
6371 identifier
= token
->u
.value
;
6375 bool expr_non_constant_p
;
6377 /* Parse the next assignment-expression. */
6378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6380 /* A braced-init-list. */
6381 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6382 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6383 if (non_constant_p
&& expr_non_constant_p
)
6384 *non_constant_p
= true;
6386 else if (non_constant_p
)
6388 expr
= (cp_parser_constant_expression
6389 (parser
, /*allow_non_constant_p=*/true,
6390 &expr_non_constant_p
));
6391 if (expr_non_constant_p
)
6392 *non_constant_p
= true;
6395 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
6398 expr
= fold_non_dependent_expr (expr
);
6400 /* If we have an ellipsis, then this is an expression
6402 if (allow_expansion_p
6403 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6405 /* Consume the `...'. */
6406 cp_lexer_consume_token (parser
->lexer
);
6408 /* Build the argument pack. */
6409 expr
= make_pack_expansion (expr
);
6412 /* Add it to the list. We add error_mark_node
6413 expressions to the list, so that we can still tell if
6414 the correct form for a parenthesized expression-list
6415 is found. That gives better errors. */
6416 vec_safe_push (expression_list
, expr
);
6418 if (expr
== error_mark_node
)
6422 /* After the first item, attribute lists look the same as
6423 expression lists. */
6424 is_attribute_list
= non_attr
;
6427 /* If the next token isn't a `,', then we are done. */
6428 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6431 /* Otherwise, consume the `,' and keep going. */
6432 cp_lexer_consume_token (parser
->lexer
);
6435 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6440 /* We try and resync to an unnested comma, as that will give the
6441 user better diagnostics. */
6442 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6443 /*recovering=*/true,
6445 /*consume_paren=*/true);
6450 parser
->greater_than_is_operator_p
6451 = saved_greater_than_is_operator_p
;
6456 parser
->greater_than_is_operator_p
6457 = saved_greater_than_is_operator_p
;
6460 vec_safe_insert (expression_list
, 0, identifier
);
6462 return expression_list
;
6465 /* Parse a pseudo-destructor-name.
6467 pseudo-destructor-name:
6468 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6469 :: [opt] nested-name-specifier template template-id :: ~ type-name
6470 :: [opt] nested-name-specifier [opt] ~ type-name
6472 If either of the first two productions is used, sets *SCOPE to the
6473 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6474 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6475 or ERROR_MARK_NODE if the parse fails. */
6478 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6483 bool nested_name_specifier_p
;
6486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
6487 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
6488 && !type_dependent_expression_p (object
))
6490 if (cxx_dialect
< cxx1y
)
6491 pedwarn (input_location
, 0,
6492 "%<~auto%> only available with "
6493 "-std=c++1y or -std=gnu++1y");
6494 cp_lexer_consume_token (parser
->lexer
);
6495 cp_lexer_consume_token (parser
->lexer
);
6497 *type
= TREE_TYPE (object
);
6501 /* Assume that things will not work out. */
6502 *type
= error_mark_node
;
6504 /* Look for the optional `::' operator. */
6505 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
6506 /* Look for the optional nested-name-specifier. */
6507 nested_name_specifier_p
6508 = (cp_parser_nested_name_specifier_opt (parser
,
6509 /*typename_keyword_p=*/false,
6510 /*check_dependency_p=*/true,
6512 /*is_declaration=*/false)
6514 /* Now, if we saw a nested-name-specifier, we might be doing the
6515 second production. */
6516 if (nested_name_specifier_p
6517 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
6519 /* Consume the `template' keyword. */
6520 cp_lexer_consume_token (parser
->lexer
);
6521 /* Parse the template-id. */
6522 cp_parser_template_id (parser
,
6523 /*template_keyword_p=*/true,
6524 /*check_dependency_p=*/false,
6526 /*is_declaration=*/true);
6527 /* Look for the `::' token. */
6528 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6530 /* If the next token is not a `~', then there might be some
6531 additional qualification. */
6532 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
6534 /* At this point, we're looking for "type-name :: ~". The type-name
6535 must not be a class-name, since this is a pseudo-destructor. So,
6536 it must be either an enum-name, or a typedef-name -- both of which
6537 are just identifiers. So, we peek ahead to check that the "::"
6538 and "~" tokens are present; if they are not, then we can avoid
6539 calling type_name. */
6540 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
6541 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
6542 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
6544 cp_parser_error (parser
, "non-scalar type");
6548 /* Look for the type-name. */
6549 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6550 if (*scope
== error_mark_node
)
6553 /* Look for the `::' token. */
6554 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6559 /* Look for the `~'. */
6560 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
6562 /* Once we see the ~, this has to be a pseudo-destructor. */
6563 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
6564 cp_parser_commit_to_tentative_parse (parser
);
6566 /* Look for the type-name again. We are not responsible for
6567 checking that it matches the first type-name. */
6568 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6571 /* Parse a unary-expression.
6577 unary-operator cast-expression
6578 sizeof unary-expression
6580 alignof ( type-id ) [C++0x]
6587 __extension__ cast-expression
6588 __alignof__ unary-expression
6589 __alignof__ ( type-id )
6590 alignof unary-expression [C++0x]
6591 __real__ cast-expression
6592 __imag__ cast-expression
6595 ADDRESS_P is true iff the unary-expression is appearing as the
6596 operand of the `&' operator. CAST_P is true if this expression is
6597 the target of a cast.
6599 Returns a representation of the expression. */
6602 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6603 bool decltype_p
, cp_id_kind
* pidk
)
6606 enum tree_code unary_operator
;
6608 /* Peek at the next token. */
6609 token
= cp_lexer_peek_token (parser
->lexer
);
6610 /* Some keywords give away the kind of expression. */
6611 if (token
->type
== CPP_KEYWORD
)
6613 enum rid keyword
= token
->keyword
;
6622 location_t first_loc
;
6624 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
6625 /* Consume the token. */
6626 cp_lexer_consume_token (parser
->lexer
);
6627 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6628 /* Parse the operand. */
6629 operand
= cp_parser_sizeof_operand (parser
, keyword
);
6631 if (TYPE_P (operand
))
6632 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
6635 /* ISO C++ defines alignof only with types, not with
6636 expressions. So pedwarn if alignof is used with a non-
6637 type expression. However, __alignof__ is ok. */
6638 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
6639 pedwarn (token
->location
, OPT_Wpedantic
,
6640 "ISO C++ does not allow %<alignof%> "
6643 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
6645 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6646 SIZEOF_EXPR with the original operand. */
6647 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
6649 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
6651 if (!processing_template_decl
&& TYPE_P (operand
))
6653 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
6654 build1 (NOP_EXPR
, operand
,
6656 SIZEOF_EXPR_TYPE_P (ret
) = 1;
6659 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
6660 TREE_SIDE_EFFECTS (ret
) = 0;
6661 TREE_READONLY (ret
) = 1;
6663 SET_EXPR_LOCATION (ret
, first_loc
);
6669 return cp_parser_new_expression (parser
);
6672 return cp_parser_delete_expression (parser
);
6676 /* The saved value of the PEDANTIC flag. */
6680 /* Save away the PEDANTIC flag. */
6681 cp_parser_extension_opt (parser
, &saved_pedantic
);
6682 /* Parse the cast-expression. */
6683 expr
= cp_parser_simple_cast_expression (parser
);
6684 /* Restore the PEDANTIC flag. */
6685 pedantic
= saved_pedantic
;
6695 /* Consume the `__real__' or `__imag__' token. */
6696 cp_lexer_consume_token (parser
->lexer
);
6697 /* Parse the cast-expression. */
6698 expression
= cp_parser_simple_cast_expression (parser
);
6699 /* Create the complete representation. */
6700 return build_x_unary_op (token
->location
,
6701 (keyword
== RID_REALPART
6702 ? REALPART_EXPR
: IMAGPART_EXPR
),
6704 tf_warning_or_error
);
6708 case RID_TRANSACTION_ATOMIC
:
6709 case RID_TRANSACTION_RELAXED
:
6710 return cp_parser_transaction_expression (parser
, keyword
);
6715 const char *saved_message
;
6716 bool saved_integral_constant_expression_p
;
6717 bool saved_non_integral_constant_expression_p
;
6718 bool saved_greater_than_is_operator_p
;
6720 cp_lexer_consume_token (parser
->lexer
);
6721 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6723 saved_message
= parser
->type_definition_forbidden_message
;
6724 parser
->type_definition_forbidden_message
6725 = G_("types may not be defined in %<noexcept%> expressions");
6727 saved_integral_constant_expression_p
6728 = parser
->integral_constant_expression_p
;
6729 saved_non_integral_constant_expression_p
6730 = parser
->non_integral_constant_expression_p
;
6731 parser
->integral_constant_expression_p
= false;
6733 saved_greater_than_is_operator_p
6734 = parser
->greater_than_is_operator_p
;
6735 parser
->greater_than_is_operator_p
= true;
6737 ++cp_unevaluated_operand
;
6738 ++c_inhibit_evaluation_warnings
;
6739 expr
= cp_parser_expression (parser
, false, NULL
);
6740 --c_inhibit_evaluation_warnings
;
6741 --cp_unevaluated_operand
;
6743 parser
->greater_than_is_operator_p
6744 = saved_greater_than_is_operator_p
;
6746 parser
->integral_constant_expression_p
6747 = saved_integral_constant_expression_p
;
6748 parser
->non_integral_constant_expression_p
6749 = saved_non_integral_constant_expression_p
;
6751 parser
->type_definition_forbidden_message
= saved_message
;
6753 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6754 return finish_noexcept_expr (expr
, tf_warning_or_error
);
6762 /* Look for the `:: new' and `:: delete', which also signal the
6763 beginning of a new-expression, or delete-expression,
6764 respectively. If the next token is `::', then it might be one of
6766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
6770 /* See if the token after the `::' is one of the keywords in
6771 which we're interested. */
6772 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
6773 /* If it's `new', we have a new-expression. */
6774 if (keyword
== RID_NEW
)
6775 return cp_parser_new_expression (parser
);
6776 /* Similarly, for `delete'. */
6777 else if (keyword
== RID_DELETE
)
6778 return cp_parser_delete_expression (parser
);
6781 /* Look for a unary operator. */
6782 unary_operator
= cp_parser_unary_operator (token
);
6783 /* The `++' and `--' operators can be handled similarly, even though
6784 they are not technically unary-operators in the grammar. */
6785 if (unary_operator
== ERROR_MARK
)
6787 if (token
->type
== CPP_PLUS_PLUS
)
6788 unary_operator
= PREINCREMENT_EXPR
;
6789 else if (token
->type
== CPP_MINUS_MINUS
)
6790 unary_operator
= PREDECREMENT_EXPR
;
6791 /* Handle the GNU address-of-label extension. */
6792 else if (cp_parser_allow_gnu_extensions_p (parser
)
6793 && token
->type
== CPP_AND_AND
)
6797 location_t loc
= token
->location
;
6799 /* Consume the '&&' token. */
6800 cp_lexer_consume_token (parser
->lexer
);
6801 /* Look for the identifier. */
6802 identifier
= cp_parser_identifier (parser
);
6803 /* Create an expression representing the address. */
6804 expression
= finish_label_address_expr (identifier
, loc
);
6805 if (cp_parser_non_integral_constant_expression (parser
,
6807 expression
= error_mark_node
;
6811 if (unary_operator
!= ERROR_MARK
)
6813 tree cast_expression
;
6814 tree expression
= error_mark_node
;
6815 non_integral_constant non_constant_p
= NIC_NONE
;
6816 location_t loc
= token
->location
;
6817 tsubst_flags_t complain
= complain_flags (decltype_p
);
6819 /* Consume the operator token. */
6820 token
= cp_lexer_consume_token (parser
->lexer
);
6821 /* Parse the cast-expression. */
6823 = cp_parser_cast_expression (parser
,
6824 unary_operator
== ADDR_EXPR
,
6828 /* Now, build an appropriate representation. */
6829 switch (unary_operator
)
6832 non_constant_p
= NIC_STAR
;
6833 expression
= build_x_indirect_ref (loc
, cast_expression
,
6839 non_constant_p
= NIC_ADDR
;
6842 expression
= build_x_unary_op (loc
, unary_operator
,
6847 case PREINCREMENT_EXPR
:
6848 case PREDECREMENT_EXPR
:
6849 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
6850 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
6852 case UNARY_PLUS_EXPR
:
6854 case TRUTH_NOT_EXPR
:
6855 expression
= finish_unary_op_expr (loc
, unary_operator
,
6856 cast_expression
, complain
);
6863 if (non_constant_p
!= NIC_NONE
6864 && cp_parser_non_integral_constant_expression (parser
,
6866 expression
= error_mark_node
;
6871 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
6872 /*member_access_only_p=*/false,
6878 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6881 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
6882 /*decltype*/false, pidk
);
6885 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6886 unary-operator, the corresponding tree code is returned. */
6888 static enum tree_code
6889 cp_parser_unary_operator (cp_token
* token
)
6891 switch (token
->type
)
6894 return INDIRECT_REF
;
6900 return UNARY_PLUS_EXPR
;
6906 return TRUTH_NOT_EXPR
;
6909 return BIT_NOT_EXPR
;
6916 /* Parse a new-expression.
6919 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6920 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6922 Returns a representation of the expression. */
6925 cp_parser_new_expression (cp_parser
* parser
)
6927 bool global_scope_p
;
6928 vec
<tree
, va_gc
> *placement
;
6930 vec
<tree
, va_gc
> *initializer
;
6931 tree nelts
= NULL_TREE
;
6934 /* Look for the optional `::' operator. */
6936 = (cp_parser_global_scope_opt (parser
,
6937 /*current_scope_valid_p=*/false)
6939 /* Look for the `new' operator. */
6940 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
6941 /* There's no easy way to tell a new-placement from the
6942 `( type-id )' construct. */
6943 cp_parser_parse_tentatively (parser
);
6944 /* Look for a new-placement. */
6945 placement
= cp_parser_new_placement (parser
);
6946 /* If that didn't work out, there's no new-placement. */
6947 if (!cp_parser_parse_definitely (parser
))
6949 if (placement
!= NULL
)
6950 release_tree_vector (placement
);
6954 /* If the next token is a `(', then we have a parenthesized
6956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6959 const char *saved_message
= parser
->type_definition_forbidden_message
;
6961 /* Consume the `('. */
6962 cp_lexer_consume_token (parser
->lexer
);
6964 /* Parse the type-id. */
6965 parser
->type_definition_forbidden_message
6966 = G_("types may not be defined in a new-expression");
6967 type
= cp_parser_type_id (parser
);
6968 parser
->type_definition_forbidden_message
= saved_message
;
6970 /* Look for the closing `)'. */
6971 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6972 token
= cp_lexer_peek_token (parser
->lexer
);
6973 /* There should not be a direct-new-declarator in this production,
6974 but GCC used to allowed this, so we check and emit a sensible error
6975 message for this case. */
6976 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6978 error_at (token
->location
,
6979 "array bound forbidden after parenthesized type-id");
6980 inform (token
->location
,
6981 "try removing the parentheses around the type-id");
6982 cp_parser_direct_new_declarator (parser
);
6985 /* Otherwise, there must be a new-type-id. */
6987 type
= cp_parser_new_type_id (parser
, &nelts
);
6989 /* If the next token is a `(' or '{', then we have a new-initializer. */
6990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
6991 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6992 initializer
= cp_parser_new_initializer (parser
);
6996 /* A new-expression may not appear in an integral constant
6998 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
6999 ret
= error_mark_node
;
7002 /* Create a representation of the new-expression. */
7003 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7004 tf_warning_or_error
);
7007 if (placement
!= NULL
)
7008 release_tree_vector (placement
);
7009 if (initializer
!= NULL
)
7010 release_tree_vector (initializer
);
7015 /* Parse a new-placement.
7020 Returns the same representation as for an expression-list. */
7022 static vec
<tree
, va_gc
> *
7023 cp_parser_new_placement (cp_parser
* parser
)
7025 vec
<tree
, va_gc
> *expression_list
;
7027 /* Parse the expression-list. */
7028 expression_list
= (cp_parser_parenthesized_expression_list
7029 (parser
, non_attr
, /*cast_p=*/false,
7030 /*allow_expansion_p=*/true,
7031 /*non_constant_p=*/NULL
));
7033 return expression_list
;
7036 /* Parse a new-type-id.
7039 type-specifier-seq new-declarator [opt]
7041 Returns the TYPE allocated. If the new-type-id indicates an array
7042 type, *NELTS is set to the number of elements in the last array
7043 bound; the TYPE will not include the last array bound. */
7046 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7048 cp_decl_specifier_seq type_specifier_seq
;
7049 cp_declarator
*new_declarator
;
7050 cp_declarator
*declarator
;
7051 cp_declarator
*outer_declarator
;
7052 const char *saved_message
;
7054 /* The type-specifier sequence must not contain type definitions.
7055 (It cannot contain declarations of new types either, but if they
7056 are not definitions we will catch that because they are not
7058 saved_message
= parser
->type_definition_forbidden_message
;
7059 parser
->type_definition_forbidden_message
7060 = G_("types may not be defined in a new-type-id");
7061 /* Parse the type-specifier-seq. */
7062 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7063 /*is_trailing_return=*/false,
7064 &type_specifier_seq
);
7065 /* Restore the old message. */
7066 parser
->type_definition_forbidden_message
= saved_message
;
7068 if (type_specifier_seq
.type
== error_mark_node
)
7069 return error_mark_node
;
7071 /* Parse the new-declarator. */
7072 new_declarator
= cp_parser_new_declarator_opt (parser
);
7074 /* Determine the number of elements in the last array dimension, if
7077 /* Skip down to the last array dimension. */
7078 declarator
= new_declarator
;
7079 outer_declarator
= NULL
;
7080 while (declarator
&& (declarator
->kind
== cdk_pointer
7081 || declarator
->kind
== cdk_ptrmem
))
7083 outer_declarator
= declarator
;
7084 declarator
= declarator
->declarator
;
7087 && declarator
->kind
== cdk_array
7088 && declarator
->declarator
7089 && declarator
->declarator
->kind
== cdk_array
)
7091 outer_declarator
= declarator
;
7092 declarator
= declarator
->declarator
;
7095 if (declarator
&& declarator
->kind
== cdk_array
)
7097 *nelts
= declarator
->u
.array
.bounds
;
7098 if (*nelts
== error_mark_node
)
7099 *nelts
= integer_one_node
;
7101 if (outer_declarator
)
7102 outer_declarator
->declarator
= declarator
->declarator
;
7104 new_declarator
= NULL
;
7107 return groktypename (&type_specifier_seq
, new_declarator
, false);
7110 /* Parse an (optional) new-declarator.
7113 ptr-operator new-declarator [opt]
7114 direct-new-declarator
7116 Returns the declarator. */
7118 static cp_declarator
*
7119 cp_parser_new_declarator_opt (cp_parser
* parser
)
7121 enum tree_code code
;
7122 tree type
, std_attributes
= NULL_TREE
;
7123 cp_cv_quals cv_quals
;
7125 /* We don't know if there's a ptr-operator next, or not. */
7126 cp_parser_parse_tentatively (parser
);
7127 /* Look for a ptr-operator. */
7128 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7129 /* If that worked, look for more new-declarators. */
7130 if (cp_parser_parse_definitely (parser
))
7132 cp_declarator
*declarator
;
7134 /* Parse another optional declarator. */
7135 declarator
= cp_parser_new_declarator_opt (parser
);
7137 declarator
= cp_parser_make_indirect_declarator
7138 (code
, type
, cv_quals
, declarator
, std_attributes
);
7143 /* If the next token is a `[', there is a direct-new-declarator. */
7144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7145 return cp_parser_direct_new_declarator (parser
);
7150 /* Parse a direct-new-declarator.
7152 direct-new-declarator:
7154 direct-new-declarator [constant-expression]
7158 static cp_declarator
*
7159 cp_parser_direct_new_declarator (cp_parser
* parser
)
7161 cp_declarator
*declarator
= NULL
;
7168 /* Look for the opening `['. */
7169 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7171 token
= cp_lexer_peek_token (parser
->lexer
);
7172 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7173 /* The standard requires that the expression have integral
7174 type. DR 74 adds enumeration types. We believe that the
7175 real intent is that these expressions be handled like the
7176 expression in a `switch' condition, which also allows
7177 classes with a single conversion to integral or
7178 enumeration type. */
7179 if (!processing_template_decl
)
7182 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7187 error_at (token
->location
,
7188 "expression in new-declarator must have integral "
7189 "or enumeration type");
7190 expression
= error_mark_node
;
7194 /* Look for the closing `]'. */
7195 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7197 /* Add this bound to the declarator. */
7198 declarator
= make_array_declarator (declarator
, expression
);
7200 /* If the next token is not a `[', then there are no more
7202 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7209 /* Parse a new-initializer.
7212 ( expression-list [opt] )
7215 Returns a representation of the expression-list. */
7217 static vec
<tree
, va_gc
> *
7218 cp_parser_new_initializer (cp_parser
* parser
)
7220 vec
<tree
, va_gc
> *expression_list
;
7222 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7225 bool expr_non_constant_p
;
7226 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7227 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7228 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7229 expression_list
= make_tree_vector_single (t
);
7232 expression_list
= (cp_parser_parenthesized_expression_list
7233 (parser
, non_attr
, /*cast_p=*/false,
7234 /*allow_expansion_p=*/true,
7235 /*non_constant_p=*/NULL
));
7237 return expression_list
;
7240 /* Parse a delete-expression.
7243 :: [opt] delete cast-expression
7244 :: [opt] delete [ ] cast-expression
7246 Returns a representation of the expression. */
7249 cp_parser_delete_expression (cp_parser
* parser
)
7251 bool global_scope_p
;
7255 /* Look for the optional `::' operator. */
7257 = (cp_parser_global_scope_opt (parser
,
7258 /*current_scope_valid_p=*/false)
7260 /* Look for the `delete' keyword. */
7261 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7262 /* See if the array syntax is in use. */
7263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7265 /* Consume the `[' token. */
7266 cp_lexer_consume_token (parser
->lexer
);
7267 /* Look for the `]' token. */
7268 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7269 /* Remember that this is the `[]' construct. */
7275 /* Parse the cast-expression. */
7276 expression
= cp_parser_simple_cast_expression (parser
);
7278 /* A delete-expression may not appear in an integral constant
7280 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7281 return error_mark_node
;
7283 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7284 tf_warning_or_error
);
7287 /* Returns true if TOKEN may start a cast-expression and false
7291 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7293 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7294 switch (token
->type
)
7300 case CPP_CLOSE_SQUARE
:
7301 case CPP_CLOSE_PAREN
:
7302 case CPP_CLOSE_BRACE
:
7306 case CPP_DEREF_STAR
:
7314 case CPP_GREATER_EQ
:
7334 case CPP_OPEN_PAREN
:
7335 /* In ((type ()) () the last () isn't a valid cast-expression,
7336 so the whole must be parsed as postfix-expression. */
7337 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7340 /* '[' may start a primary-expression in obj-c++. */
7341 case CPP_OPEN_SQUARE
:
7342 return c_dialect_objc ();
7349 /* Parse a cast-expression.
7353 ( type-id ) cast-expression
7355 ADDRESS_P is true iff the unary-expression is appearing as the
7356 operand of the `&' operator. CAST_P is true if this expression is
7357 the target of a cast.
7359 Returns a representation of the expression. */
7362 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7363 bool decltype_p
, cp_id_kind
* pidk
)
7365 /* If it's a `(', then we might be looking at a cast. */
7366 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7368 tree type
= NULL_TREE
;
7369 tree expr
= NULL_TREE
;
7370 bool compound_literal_p
;
7371 const char *saved_message
;
7373 /* There's no way to know yet whether or not this is a cast.
7374 For example, `(int (3))' is a unary-expression, while `(int)
7375 3' is a cast. So, we resort to parsing tentatively. */
7376 cp_parser_parse_tentatively (parser
);
7377 /* Types may not be defined in a cast. */
7378 saved_message
= parser
->type_definition_forbidden_message
;
7379 parser
->type_definition_forbidden_message
7380 = G_("types may not be defined in casts");
7381 /* Consume the `('. */
7382 cp_lexer_consume_token (parser
->lexer
);
7383 /* A very tricky bit is that `(struct S) { 3 }' is a
7384 compound-literal (which we permit in C++ as an extension).
7385 But, that construct is not a cast-expression -- it is a
7386 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7387 is legal; if the compound-literal were a cast-expression,
7388 you'd need an extra set of parentheses.) But, if we parse
7389 the type-id, and it happens to be a class-specifier, then we
7390 will commit to the parse at that point, because we cannot
7391 undo the action that is done when creating a new class. So,
7392 then we cannot back up and do a postfix-expression.
7394 Therefore, we scan ahead to the closing `)', and check to see
7395 if the token after the `)' is a `{'. If so, we are not
7396 looking at a cast-expression.
7398 Save tokens so that we can put them back. */
7399 cp_lexer_save_tokens (parser
->lexer
);
7400 /* Skip tokens until the next token is a closing parenthesis.
7401 If we find the closing `)', and the next token is a `{', then
7402 we are looking at a compound-literal. */
7404 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7405 /*consume_paren=*/true)
7406 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
7407 /* Roll back the tokens we skipped. */
7408 cp_lexer_rollback_tokens (parser
->lexer
);
7409 /* If we were looking at a compound-literal, simulate an error
7410 so that the call to cp_parser_parse_definitely below will
7412 if (compound_literal_p
)
7413 cp_parser_simulate_error (parser
);
7416 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7417 parser
->in_type_id_in_expr_p
= true;
7418 /* Look for the type-id. */
7419 type
= cp_parser_type_id (parser
);
7420 /* Look for the closing `)'. */
7421 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7422 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7425 /* Restore the saved message. */
7426 parser
->type_definition_forbidden_message
= saved_message
;
7428 /* At this point this can only be either a cast or a
7429 parenthesized ctor such as `(T ())' that looks like a cast to
7430 function returning T. */
7431 if (!cp_parser_error_occurred (parser
)
7432 && cp_parser_tokens_start_cast_expression (parser
))
7434 cp_parser_parse_definitely (parser
);
7435 expr
= cp_parser_cast_expression (parser
,
7436 /*address_p=*/false,
7438 /*decltype_p=*/false,
7441 /* Warn about old-style casts, if so requested. */
7442 if (warn_old_style_cast
7443 && !in_system_header
7444 && !VOID_TYPE_P (type
)
7445 && current_lang_name
!= lang_name_c
)
7446 warning (OPT_Wold_style_cast
, "use of old-style cast");
7448 /* Only type conversions to integral or enumeration types
7449 can be used in constant-expressions. */
7450 if (!cast_valid_in_integral_constant_expression_p (type
)
7451 && cp_parser_non_integral_constant_expression (parser
,
7453 return error_mark_node
;
7455 /* Perform the cast. */
7456 expr
= build_c_cast (input_location
, type
, expr
);
7460 cp_parser_abort_tentative_parse (parser
);
7463 /* If we get here, then it's not a cast, so it must be a
7464 unary-expression. */
7465 return cp_parser_unary_expression (parser
, address_p
, cast_p
,
7469 /* Parse a binary expression of the general form:
7473 pm-expression .* cast-expression
7474 pm-expression ->* cast-expression
7476 multiplicative-expression:
7478 multiplicative-expression * pm-expression
7479 multiplicative-expression / pm-expression
7480 multiplicative-expression % pm-expression
7482 additive-expression:
7483 multiplicative-expression
7484 additive-expression + multiplicative-expression
7485 additive-expression - multiplicative-expression
7489 shift-expression << additive-expression
7490 shift-expression >> additive-expression
7492 relational-expression:
7494 relational-expression < shift-expression
7495 relational-expression > shift-expression
7496 relational-expression <= shift-expression
7497 relational-expression >= shift-expression
7501 relational-expression:
7502 relational-expression <? shift-expression
7503 relational-expression >? shift-expression
7505 equality-expression:
7506 relational-expression
7507 equality-expression == relational-expression
7508 equality-expression != relational-expression
7512 and-expression & equality-expression
7514 exclusive-or-expression:
7516 exclusive-or-expression ^ and-expression
7518 inclusive-or-expression:
7519 exclusive-or-expression
7520 inclusive-or-expression | exclusive-or-expression
7522 logical-and-expression:
7523 inclusive-or-expression
7524 logical-and-expression && inclusive-or-expression
7526 logical-or-expression:
7527 logical-and-expression
7528 logical-or-expression || logical-and-expression
7530 All these are implemented with a single function like:
7533 simple-cast-expression
7534 binary-expression <token> binary-expression
7536 CAST_P is true if this expression is the target of a cast.
7538 The binops_by_token map is used to get the tree codes for each <token> type.
7539 binary-expressions are associated according to a precedence table. */
7541 #define TOKEN_PRECEDENCE(token) \
7542 (((token->type == CPP_GREATER \
7543 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7544 && !parser->greater_than_is_operator_p) \
7545 ? PREC_NOT_OPERATOR \
7546 : binops_by_token[token->type].prec)
7549 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7550 bool no_toplevel_fold_p
,
7552 enum cp_parser_prec prec
,
7555 cp_parser_expression_stack stack
;
7556 cp_parser_expression_stack_entry
*sp
= &stack
[0];
7557 cp_parser_expression_stack_entry current
;
7560 enum tree_code rhs_type
;
7561 enum cp_parser_prec new_prec
, lookahead_prec
;
7564 /* Parse the first expression. */
7565 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
7566 cast_p
, decltype_p
, pidk
);
7567 current
.lhs_type
= ERROR_MARK
;
7568 current
.prec
= prec
;
7570 if (cp_parser_error_occurred (parser
))
7571 return error_mark_node
;
7575 /* Get an operator token. */
7576 token
= cp_lexer_peek_token (parser
->lexer
);
7578 if (warn_cxx0x_compat
7579 && token
->type
== CPP_RSHIFT
7580 && !parser
->greater_than_is_operator_p
)
7582 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
7583 "%<>>%> operator is treated"
7584 " as two right angle brackets in C++11"))
7585 inform (token
->location
,
7586 "suggest parentheses around %<>>%> expression");
7589 new_prec
= TOKEN_PRECEDENCE (token
);
7591 /* Popping an entry off the stack means we completed a subexpression:
7592 - either we found a token which is not an operator (`>' where it is not
7593 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7594 will happen repeatedly;
7595 - or, we found an operator which has lower priority. This is the case
7596 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7598 if (new_prec
<= current
.prec
)
7607 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
7608 current
.loc
= token
->location
;
7610 /* We used the operator token. */
7611 cp_lexer_consume_token (parser
->lexer
);
7613 /* For "false && x" or "true || x", x will never be executed;
7614 disable warnings while evaluating it. */
7615 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7616 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
7617 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7618 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
7620 /* Extract another operand. It may be the RHS of this expression
7621 or the LHS of a new, higher priority expression. */
7622 rhs
= cp_parser_simple_cast_expression (parser
);
7623 rhs_type
= ERROR_MARK
;
7625 /* Get another operator token. Look up its precedence to avoid
7626 building a useless (immediately popped) stack entry for common
7627 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7628 token
= cp_lexer_peek_token (parser
->lexer
);
7629 lookahead_prec
= TOKEN_PRECEDENCE (token
);
7630 if (lookahead_prec
> new_prec
)
7632 /* ... and prepare to parse the RHS of the new, higher priority
7633 expression. Since precedence levels on the stack are
7634 monotonically increasing, we do not have to care about
7639 current
.lhs_type
= rhs_type
;
7640 current
.prec
= new_prec
;
7641 new_prec
= lookahead_prec
;
7645 lookahead_prec
= new_prec
;
7646 /* If the stack is not empty, we have parsed into LHS the right side
7647 (`4' in the example above) of an expression we had suspended.
7648 We can use the information on the stack to recover the LHS (`3')
7649 from the stack together with the tree code (`MULT_EXPR'), and
7650 the precedence of the higher level subexpression
7651 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7652 which will be used to actually build the additive expression. */
7654 rhs_type
= current
.lhs_type
;
7659 /* Undo the disabling of warnings done above. */
7660 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7661 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
7662 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7663 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
7666 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7667 ERROR_MARK for everything that is not a binary expression.
7668 This makes warn_about_parentheses miss some warnings that
7669 involve unary operators. For unary expressions we should
7670 pass the correct tree_code unless the unary expression was
7671 surrounded by parentheses.
7673 if (no_toplevel_fold_p
7674 && lookahead_prec
<= current
.prec
7676 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
)
7677 current
.lhs
= build2 (current
.tree_type
, boolean_type_node
,
7680 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
7681 current
.lhs
, current
.lhs_type
,
7682 rhs
, rhs_type
, &overload
,
7683 complain_flags (decltype_p
));
7684 current
.lhs_type
= current
.tree_type
;
7685 if (EXPR_P (current
.lhs
))
7686 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
7688 /* If the binary operator required the use of an overloaded operator,
7689 then this expression cannot be an integral constant-expression.
7690 An overloaded operator can be used even if both operands are
7691 otherwise permissible in an integral constant-expression if at
7692 least one of the operands is of enumeration type. */
7695 && cp_parser_non_integral_constant_expression (parser
,
7697 return error_mark_node
;
7704 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7705 bool no_toplevel_fold_p
,
7706 enum cp_parser_prec prec
,
7709 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
7710 /*decltype*/false, prec
, pidk
);
7713 /* Parse the `? expression : assignment-expression' part of a
7714 conditional-expression. The LOGICAL_OR_EXPR is the
7715 logical-or-expression that started the conditional-expression.
7716 Returns a representation of the entire conditional-expression.
7718 This routine is used by cp_parser_assignment_expression.
7720 ? expression : assignment-expression
7724 ? : assignment-expression */
7727 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
7730 tree assignment_expr
;
7731 struct cp_token
*token
;
7732 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7734 /* Consume the `?' token. */
7735 cp_lexer_consume_token (parser
->lexer
);
7736 token
= cp_lexer_peek_token (parser
->lexer
);
7737 if (cp_parser_allow_gnu_extensions_p (parser
)
7738 && token
->type
== CPP_COLON
)
7740 pedwarn (token
->location
, OPT_Wpedantic
,
7741 "ISO C++ does not allow ?: with omitted middle operand");
7742 /* Implicit true clause. */
7744 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
7745 warn_for_omitted_condop (token
->location
, logical_or_expr
);
7749 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
7750 parser
->colon_corrects_to_scope_p
= false;
7751 /* Parse the expression. */
7752 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
7753 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7754 c_inhibit_evaluation_warnings
+=
7755 ((logical_or_expr
== truthvalue_true_node
)
7756 - (logical_or_expr
== truthvalue_false_node
));
7757 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
7760 /* The next token should be a `:'. */
7761 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
7762 /* Parse the assignment-expression. */
7763 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7764 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
7766 /* Build the conditional-expression. */
7767 return build_x_conditional_expr (loc
, logical_or_expr
,
7770 tf_warning_or_error
);
7773 /* Parse an assignment-expression.
7775 assignment-expression:
7776 conditional-expression
7777 logical-or-expression assignment-operator assignment_expression
7780 CAST_P is true if this expression is the target of a cast.
7781 DECLTYPE_P is true if this expression is the operand of decltype.
7783 Returns a representation for the expression. */
7786 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
7787 bool decltype_p
, cp_id_kind
* pidk
)
7791 /* If the next token is the `throw' keyword, then we're looking at
7792 a throw-expression. */
7793 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
7794 expr
= cp_parser_throw_expression (parser
);
7795 /* Otherwise, it must be that we are looking at a
7796 logical-or-expression. */
7799 /* Parse the binary expressions (logical-or-expression). */
7800 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
7802 PREC_NOT_OPERATOR
, pidk
);
7803 /* If the next token is a `?' then we're actually looking at a
7804 conditional-expression. */
7805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
7806 return cp_parser_question_colon_clause (parser
, expr
);
7809 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7811 /* If it's an assignment-operator, we're using the second
7813 enum tree_code assignment_operator
7814 = cp_parser_assignment_operator_opt (parser
);
7815 if (assignment_operator
!= ERROR_MARK
)
7817 bool non_constant_p
;
7818 location_t saved_input_location
;
7820 /* Parse the right-hand side of the assignment. */
7821 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
7823 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7824 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7826 /* An assignment may not appear in a
7827 constant-expression. */
7828 if (cp_parser_non_integral_constant_expression (parser
,
7830 return error_mark_node
;
7831 /* Build the assignment expression. Its default
7832 location is the location of the '=' token. */
7833 saved_input_location
= input_location
;
7834 input_location
= loc
;
7835 expr
= build_x_modify_expr (loc
, expr
,
7836 assignment_operator
,
7838 complain_flags (decltype_p
));
7839 input_location
= saved_input_location
;
7848 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
7851 return cp_parser_assignment_expression (parser
, cast_p
,
7852 /*decltype*/false, pidk
);
7855 /* Parse an (optional) assignment-operator.
7857 assignment-operator: one of
7858 = *= /= %= += -= >>= <<= &= ^= |=
7862 assignment-operator: one of
7865 If the next token is an assignment operator, the corresponding tree
7866 code is returned, and the token is consumed. For example, for
7867 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7868 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7869 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7870 operator, ERROR_MARK is returned. */
7872 static enum tree_code
7873 cp_parser_assignment_operator_opt (cp_parser
* parser
)
7878 /* Peek at the next token. */
7879 token
= cp_lexer_peek_token (parser
->lexer
);
7881 switch (token
->type
)
7892 op
= TRUNC_DIV_EXPR
;
7896 op
= TRUNC_MOD_EXPR
;
7928 /* Nothing else is an assignment operator. */
7932 /* If it was an assignment operator, consume it. */
7933 if (op
!= ERROR_MARK
)
7934 cp_lexer_consume_token (parser
->lexer
);
7939 /* Parse an expression.
7942 assignment-expression
7943 expression , assignment-expression
7945 CAST_P is true if this expression is the target of a cast.
7946 DECLTYPE_P is true if this expression is the immediate operand of decltype,
7947 except possibly parenthesized or on the RHS of a comma (N3276).
7949 Returns a representation of the expression. */
7952 cp_parser_expression (cp_parser
* parser
, bool cast_p
, bool decltype_p
,
7955 tree expression
= NULL_TREE
;
7956 location_t loc
= UNKNOWN_LOCATION
;
7960 tree assignment_expression
;
7962 /* Parse the next assignment-expression. */
7963 assignment_expression
7964 = cp_parser_assignment_expression (parser
, cast_p
, decltype_p
, pidk
);
7966 /* We don't create a temporary for a call that is the immediate operand
7967 of decltype or on the RHS of a comma. But when we see a comma, we
7968 need to create a temporary for a call on the LHS. */
7969 if (decltype_p
&& !processing_template_decl
7970 && TREE_CODE (assignment_expression
) == CALL_EXPR
7971 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
7972 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
7973 assignment_expression
7974 = build_cplus_new (TREE_TYPE (assignment_expression
),
7975 assignment_expression
, tf_warning_or_error
);
7977 /* If this is the first assignment-expression, we can just
7980 expression
= assignment_expression
;
7982 expression
= build_x_compound_expr (loc
, expression
,
7983 assignment_expression
,
7984 complain_flags (decltype_p
));
7985 /* If the next token is not a comma, then we are done with the
7987 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7989 /* Consume the `,'. */
7990 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7991 cp_lexer_consume_token (parser
->lexer
);
7992 /* A comma operator cannot appear in a constant-expression. */
7993 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
7994 expression
= error_mark_node
;
8001 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
8003 return cp_parser_expression (parser
, cast_p
, /*decltype*/false, pidk
);
8006 /* Parse a constant-expression.
8008 constant-expression:
8009 conditional-expression
8011 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8012 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8013 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8014 is false, NON_CONSTANT_P should be NULL. */
8017 cp_parser_constant_expression (cp_parser
* parser
,
8018 bool allow_non_constant_p
,
8019 bool *non_constant_p
)
8021 bool saved_integral_constant_expression_p
;
8022 bool saved_allow_non_integral_constant_expression_p
;
8023 bool saved_non_integral_constant_expression_p
;
8026 /* It might seem that we could simply parse the
8027 conditional-expression, and then check to see if it were
8028 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8029 one that the compiler can figure out is constant, possibly after
8030 doing some simplifications or optimizations. The standard has a
8031 precise definition of constant-expression, and we must honor
8032 that, even though it is somewhat more restrictive.
8038 is not a legal declaration, because `(2, 3)' is not a
8039 constant-expression. The `,' operator is forbidden in a
8040 constant-expression. However, GCC's constant-folding machinery
8041 will fold this operation to an INTEGER_CST for `3'. */
8043 /* Save the old settings. */
8044 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8045 saved_allow_non_integral_constant_expression_p
8046 = parser
->allow_non_integral_constant_expression_p
;
8047 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8048 /* We are now parsing a constant-expression. */
8049 parser
->integral_constant_expression_p
= true;
8050 parser
->allow_non_integral_constant_expression_p
8051 = (allow_non_constant_p
|| cxx_dialect
>= cxx0x
);
8052 parser
->non_integral_constant_expression_p
= false;
8053 /* Although the grammar says "conditional-expression", we parse an
8054 "assignment-expression", which also permits "throw-expression"
8055 and the use of assignment operators. In the case that
8056 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8057 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8058 actually essential that we look for an assignment-expression.
8059 For example, cp_parser_initializer_clauses uses this function to
8060 determine whether a particular assignment-expression is in fact
8062 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
8063 /* Restore the old settings. */
8064 parser
->integral_constant_expression_p
8065 = saved_integral_constant_expression_p
;
8066 parser
->allow_non_integral_constant_expression_p
8067 = saved_allow_non_integral_constant_expression_p
;
8068 if (cxx_dialect
>= cxx0x
)
8070 /* Require an rvalue constant expression here; that's what our
8071 callers expect. Reference constant expressions are handled
8072 separately in e.g. cp_parser_template_argument. */
8073 bool is_const
= potential_rvalue_constant_expression (expression
);
8074 parser
->non_integral_constant_expression_p
= !is_const
;
8075 if (!is_const
&& !allow_non_constant_p
)
8076 require_potential_rvalue_constant_expression (expression
);
8078 if (allow_non_constant_p
)
8079 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8080 parser
->non_integral_constant_expression_p
8081 = saved_non_integral_constant_expression_p
;
8086 /* Parse __builtin_offsetof.
8088 offsetof-expression:
8089 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8091 offsetof-member-designator:
8093 | offsetof-member-designator "." id-expression
8094 | offsetof-member-designator "[" expression "]"
8095 | offsetof-member-designator "->" id-expression */
8098 cp_parser_builtin_offsetof (cp_parser
*parser
)
8100 int save_ice_p
, save_non_ice_p
;
8105 /* We're about to accept non-integral-constant things, but will
8106 definitely yield an integral constant expression. Save and
8107 restore these values around our local parsing. */
8108 save_ice_p
= parser
->integral_constant_expression_p
;
8109 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8111 /* Consume the "__builtin_offsetof" token. */
8112 cp_lexer_consume_token (parser
->lexer
);
8113 /* Consume the opening `('. */
8114 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8115 /* Parse the type-id. */
8116 type
= cp_parser_type_id (parser
);
8117 /* Look for the `,'. */
8118 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8119 token
= cp_lexer_peek_token (parser
->lexer
);
8121 /* Build the (type *)null that begins the traditional offsetof macro. */
8122 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8123 tf_warning_or_error
);
8125 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8126 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8127 true, &dummy
, token
->location
);
8130 token
= cp_lexer_peek_token (parser
->lexer
);
8131 switch (token
->type
)
8133 case CPP_OPEN_SQUARE
:
8134 /* offsetof-member-designator "[" expression "]" */
8135 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8140 /* offsetof-member-designator "->" identifier */
8141 expr
= grok_array_decl (token
->location
, expr
,
8142 integer_zero_node
, false);
8146 /* offsetof-member-designator "." identifier */
8147 cp_lexer_consume_token (parser
->lexer
);
8148 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8153 case CPP_CLOSE_PAREN
:
8154 /* Consume the ")" token. */
8155 cp_lexer_consume_token (parser
->lexer
);
8159 /* Error. We know the following require will fail, but
8160 that gives the proper error message. */
8161 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8162 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8163 expr
= error_mark_node
;
8169 /* If we're processing a template, we can't finish the semantics yet.
8170 Otherwise we can fold the entire expression now. */
8171 if (processing_template_decl
)
8172 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
8174 expr
= finish_offsetof (expr
);
8177 parser
->integral_constant_expression_p
= save_ice_p
;
8178 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8183 /* Parse a trait expression.
8185 Returns a representation of the expression, the underlying type
8186 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8189 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8192 tree type1
, type2
= NULL_TREE
;
8193 bool binary
= false;
8194 cp_decl_specifier_seq decl_specs
;
8198 case RID_HAS_NOTHROW_ASSIGN
:
8199 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8201 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8202 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8204 case RID_HAS_NOTHROW_COPY
:
8205 kind
= CPTK_HAS_NOTHROW_COPY
;
8207 case RID_HAS_TRIVIAL_ASSIGN
:
8208 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8210 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8211 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8213 case RID_HAS_TRIVIAL_COPY
:
8214 kind
= CPTK_HAS_TRIVIAL_COPY
;
8216 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8217 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8219 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8220 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8222 case RID_IS_ABSTRACT
:
8223 kind
= CPTK_IS_ABSTRACT
;
8225 case RID_IS_BASE_OF
:
8226 kind
= CPTK_IS_BASE_OF
;
8230 kind
= CPTK_IS_CLASS
;
8232 case RID_IS_CONVERTIBLE_TO
:
8233 kind
= CPTK_IS_CONVERTIBLE_TO
;
8237 kind
= CPTK_IS_EMPTY
;
8240 kind
= CPTK_IS_ENUM
;
8243 kind
= CPTK_IS_FINAL
;
8245 case RID_IS_LITERAL_TYPE
:
8246 kind
= CPTK_IS_LITERAL_TYPE
;
8251 case RID_IS_POLYMORPHIC
:
8252 kind
= CPTK_IS_POLYMORPHIC
;
8254 case RID_IS_STD_LAYOUT
:
8255 kind
= CPTK_IS_STD_LAYOUT
;
8257 case RID_IS_TRIVIAL
:
8258 kind
= CPTK_IS_TRIVIAL
;
8261 kind
= CPTK_IS_UNION
;
8263 case RID_UNDERLYING_TYPE
:
8264 kind
= CPTK_UNDERLYING_TYPE
;
8269 case RID_DIRECT_BASES
:
8270 kind
= CPTK_DIRECT_BASES
;
8276 /* Consume the token. */
8277 cp_lexer_consume_token (parser
->lexer
);
8279 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8281 type1
= cp_parser_type_id (parser
);
8283 if (type1
== error_mark_node
)
8284 return error_mark_node
;
8286 /* Build a trivial decl-specifier-seq. */
8287 clear_decl_specs (&decl_specs
);
8288 decl_specs
.type
= type1
;
8290 /* Call grokdeclarator to figure out what type this is. */
8291 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8292 /*initialized=*/0, /*attrlist=*/NULL
);
8296 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8298 type2
= cp_parser_type_id (parser
);
8300 if (type2
== error_mark_node
)
8301 return error_mark_node
;
8303 /* Build a trivial decl-specifier-seq. */
8304 clear_decl_specs (&decl_specs
);
8305 decl_specs
.type
= type2
;
8307 /* Call grokdeclarator to figure out what type this is. */
8308 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8309 /*initialized=*/0, /*attrlist=*/NULL
);
8312 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8314 /* Complete the trait expression, which may mean either processing
8315 the trait expr now or saving it for template instantiation. */
8318 case CPTK_UNDERLYING_TYPE
:
8319 return finish_underlying_type (type1
);
8321 return finish_bases (type1
, false);
8322 case CPTK_DIRECT_BASES
:
8323 return finish_bases (type1
, true);
8325 return finish_trait_expr (kind
, type1
, type2
);
8329 /* Lambdas that appear in variable initializer or default argument scope
8330 get that in their mangling, so we need to record it. We might as well
8331 use the count for function and namespace scopes as well. */
8332 static GTY(()) tree lambda_scope
;
8333 static GTY(()) int lambda_count
;
8334 typedef struct GTY(()) tree_int
8339 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8342 start_lambda_scope (tree decl
)
8346 /* Once we're inside a function, we ignore other scopes and just push
8347 the function again so that popping works properly. */
8348 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8349 decl
= current_function_decl
;
8350 ti
.t
= lambda_scope
;
8351 ti
.i
= lambda_count
;
8352 vec_safe_push (lambda_scope_stack
, ti
);
8353 if (lambda_scope
!= decl
)
8355 /* Don't reset the count if we're still in the same function. */
8356 lambda_scope
= decl
;
8362 record_lambda_scope (tree lambda
)
8364 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8365 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8369 finish_lambda_scope (void)
8371 tree_int
*p
= &lambda_scope_stack
->last ();
8372 if (lambda_scope
!= p
->t
)
8374 lambda_scope
= p
->t
;
8375 lambda_count
= p
->i
;
8377 lambda_scope_stack
->pop ();
8380 /* Parse a lambda expression.
8383 lambda-introducer lambda-declarator [opt] compound-statement
8385 Returns a representation of the expression. */
8388 cp_parser_lambda_expression (cp_parser
* parser
)
8390 tree lambda_expr
= build_lambda_expr ();
8394 LAMBDA_EXPR_LOCATION (lambda_expr
)
8395 = cp_lexer_peek_token (parser
->lexer
)->location
;
8397 if (cp_unevaluated_operand
)
8398 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8399 "lambda-expression in unevaluated context");
8401 /* We may be in the middle of deferred access check. Disable
8403 push_deferring_access_checks (dk_no_deferred
);
8405 cp_parser_lambda_introducer (parser
, lambda_expr
);
8407 type
= begin_lambda_type (lambda_expr
);
8408 if (type
== error_mark_node
)
8409 return error_mark_node
;
8411 record_lambda_scope (lambda_expr
);
8413 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8414 determine_visibility (TYPE_NAME (type
));
8416 /* Now that we've started the type, add the capture fields for any
8417 explicit captures. */
8418 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8421 /* Inside the class, surrounding template-parameter-lists do not apply. */
8422 unsigned int saved_num_template_parameter_lists
8423 = parser
->num_template_parameter_lists
;
8424 unsigned char in_statement
= parser
->in_statement
;
8425 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
8427 parser
->num_template_parameter_lists
= 0;
8428 parser
->in_statement
= 0;
8429 parser
->in_switch_statement_p
= false;
8431 /* By virtue of defining a local class, a lambda expression has access to
8432 the private variables of enclosing classes. */
8434 ok
= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
8437 cp_parser_lambda_body (parser
, lambda_expr
);
8438 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8439 cp_parser_skip_to_end_of_block_or_statement (parser
);
8441 /* The capture list was built up in reverse order; fix that now. */
8442 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
8443 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8446 maybe_add_lambda_conv_op (type
);
8448 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
8450 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
8451 parser
->in_statement
= in_statement
;
8452 parser
->in_switch_statement_p
= in_switch_statement_p
;
8455 pop_deferring_access_checks ();
8457 /* This field is only used during parsing of the lambda. */
8458 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
8460 /* This lambda shouldn't have any proxies left at this point. */
8461 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
8462 /* And now that we're done, push proxies for an enclosing lambda. */
8463 insert_pending_capture_proxies ();
8466 return build_lambda_object (lambda_expr
);
8468 return error_mark_node
;
8471 /* Parse the beginning of a lambda expression.
8474 [ lambda-capture [opt] ]
8476 LAMBDA_EXPR is the current representation of the lambda expression. */
8479 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
8481 /* Need commas after the first capture. */
8484 /* Eat the leading `['. */
8485 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8487 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8488 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
8489 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
8490 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
8491 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8492 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
8494 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
8496 cp_lexer_consume_token (parser
->lexer
);
8500 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
8502 cp_token
* capture_token
;
8504 tree capture_init_expr
;
8505 cp_id_kind idk
= CP_ID_KIND_NONE
;
8506 bool explicit_init_p
= false;
8508 enum capture_kind_type
8513 enum capture_kind_type capture_kind
= BY_COPY
;
8515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
8517 error ("expected end of capture-list");
8524 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8526 /* Possibly capture `this'. */
8527 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
8529 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8530 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
8531 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
8532 "with by-copy capture default");
8533 cp_lexer_consume_token (parser
->lexer
);
8534 add_capture (lambda_expr
,
8535 /*id=*/this_identifier
,
8536 /*initializer=*/finish_this_expr(),
8537 /*by_reference_p=*/false,
8542 /* Remember whether we want to capture as a reference or not. */
8543 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
8545 capture_kind
= BY_REFERENCE
;
8546 cp_lexer_consume_token (parser
->lexer
);
8549 /* Get the identifier. */
8550 capture_token
= cp_lexer_peek_token (parser
->lexer
);
8551 capture_id
= cp_parser_identifier (parser
);
8553 if (capture_id
== error_mark_node
)
8554 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8555 delimiters, but I modified this to stop on unnested ']' as well. It
8556 was already changed to stop on unnested '}', so the
8557 "closing_parenthesis" name is no more misleading with my change. */
8559 cp_parser_skip_to_closing_parenthesis (parser
,
8560 /*recovering=*/true,
8562 /*consume_paren=*/true);
8566 /* Find the initializer for this capture. */
8567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
8568 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
8569 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8571 bool direct
, non_constant
;
8572 /* An explicit initializer exists. */
8573 if (cxx_dialect
< cxx1y
)
8574 pedwarn (input_location
, 0,
8575 "lambda capture initializers "
8576 "only available with -std=c++1y or -std=gnu++1y");
8577 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
8579 explicit_init_p
= true;
8583 const char* error_msg
;
8585 /* Turn the identifier into an id-expression. */
8587 = cp_parser_lookup_name
8591 /*is_template=*/false,
8592 /*is_namespace=*/false,
8593 /*check_dependency=*/true,
8594 /*ambiguous_decls=*/NULL
,
8595 capture_token
->location
);
8597 if (capture_init_expr
== error_mark_node
)
8599 unqualified_name_lookup_error (capture_id
);
8602 else if (DECL_P (capture_init_expr
)
8603 && (!VAR_P (capture_init_expr
)
8604 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
8606 error_at (capture_token
->location
,
8607 "capture of non-variable %qD ",
8609 inform (0, "%q+#D declared here", capture_init_expr
);
8612 if (VAR_P (capture_init_expr
)
8613 && decl_storage_duration (capture_init_expr
) != dk_auto
)
8615 pedwarn (capture_token
->location
, 0, "capture of variable "
8616 "%qD with non-automatic storage duration",
8618 inform (0, "%q+#D declared here", capture_init_expr
);
8623 = finish_id_expression
8628 /*integral_constant_expression_p=*/false,
8629 /*allow_non_integral_constant_expression_p=*/false,
8630 /*non_integral_constant_expression_p=*/NULL
,
8631 /*template_p=*/false,
8633 /*address_p=*/false,
8634 /*template_arg_p=*/false,
8636 capture_token
->location
);
8639 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
8640 && !explicit_init_p
)
8642 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
8643 && capture_kind
== BY_COPY
)
8644 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
8645 "of %qD redundant with by-copy capture default",
8647 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
8648 && capture_kind
== BY_REFERENCE
)
8649 pedwarn (capture_token
->location
, 0, "explicit by-reference "
8650 "capture of %qD redundant with by-reference capture "
8651 "default", capture_id
);
8654 add_capture (lambda_expr
,
8657 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
8661 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8664 /* Parse the (optional) middle of a lambda expression.
8667 ( parameter-declaration-clause [opt] )
8668 attribute-specifier [opt]
8670 exception-specification [opt]
8671 lambda-return-type-clause [opt]
8673 LAMBDA_EXPR is the current representation of the lambda expression. */
8676 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
8678 /* 5.1.1.4 of the standard says:
8679 If a lambda-expression does not include a lambda-declarator, it is as if
8680 the lambda-declarator were ().
8681 This means an empty parameter list, no attributes, and no exception
8683 tree param_list
= void_list_node
;
8684 tree attributes
= NULL_TREE
;
8685 tree exception_spec
= NULL_TREE
;
8688 /* The lambda-declarator is optional, but must begin with an opening
8689 parenthesis if present. */
8690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8692 cp_lexer_consume_token (parser
->lexer
);
8694 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
8696 /* Parse parameters. */
8697 param_list
= cp_parser_parameter_declaration_clause (parser
);
8699 /* Default arguments shall not be specified in the
8700 parameter-declaration-clause of a lambda-declarator. */
8701 for (t
= param_list
; t
; t
= TREE_CHAIN (t
))
8702 if (TREE_PURPOSE (t
))
8703 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
8704 "default argument specified for lambda parameter");
8706 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8708 attributes
= cp_parser_attributes_opt (parser
);
8710 /* Parse optional `mutable' keyword. */
8711 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
8713 cp_lexer_consume_token (parser
->lexer
);
8714 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
8717 /* Parse optional exception specification. */
8718 exception_spec
= cp_parser_exception_specification_opt (parser
);
8720 /* Parse optional trailing return type. */
8721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
8723 cp_lexer_consume_token (parser
->lexer
);
8724 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
8725 = cp_parser_trailing_type_id (parser
);
8728 /* The function parameters must be in scope all the way until after the
8729 trailing-return-type in case of decltype. */
8730 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
8731 pop_binding (DECL_NAME (t
), t
);
8736 /* Create the function call operator.
8738 Messing with declarators like this is no uglier than building up the
8739 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8742 cp_decl_specifier_seq return_type_specs
;
8743 cp_declarator
* declarator
;
8748 clear_decl_specs (&return_type_specs
);
8749 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
8750 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
8752 /* Maybe we will deduce the return type later. */
8753 return_type_specs
.type
= make_auto ();
8755 p
= obstack_alloc (&declarator_obstack
, 0);
8757 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
8760 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
8761 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
8762 declarator
= make_call_declarator (declarator
, param_list
, quals
,
8763 VIRT_SPEC_UNSPECIFIED
,
8766 /*late_return_type=*/NULL_TREE
);
8767 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
8769 fco
= grokmethod (&return_type_specs
,
8772 if (fco
!= error_mark_node
)
8774 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
8775 DECL_ARTIFICIAL (fco
) = 1;
8776 /* Give the object parameter a different name. */
8777 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
8780 finish_member_declaration (fco
);
8782 obstack_free (&declarator_obstack
, p
);
8784 return (fco
!= error_mark_node
);
8788 /* Parse the body of a lambda expression, which is simply
8792 but which requires special handling.
8793 LAMBDA_EXPR is the current representation of the lambda expression. */
8796 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
8798 bool nested
= (current_function_decl
!= NULL_TREE
);
8799 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
8801 push_function_context ();
8803 /* Still increment function_depth so that we don't GC in the
8804 middle of an expression. */
8806 /* Clear this in case we're in the middle of a default argument. */
8807 parser
->local_variables_forbidden_p
= false;
8809 /* Finish the function call operator
8811 + late_parsing_for_member
8812 + function_definition_after_declarator
8813 + ctor_initializer_opt_and_function_body */
8815 tree fco
= lambda_function (lambda_expr
);
8821 /* Let the front end know that we are going to be defining this
8823 start_preparsed_function (fco
,
8825 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
8827 start_lambda_scope (fco
);
8828 body
= begin_function_body ();
8830 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8833 /* Push the proxies for any explicit captures. */
8834 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
8835 cap
= TREE_CHAIN (cap
))
8836 build_capture_proxy (TREE_PURPOSE (cap
));
8838 compound_stmt
= begin_compound_stmt (0);
8840 /* 5.1.1.4 of the standard says:
8841 If a lambda-expression does not include a trailing-return-type, it
8842 is as if the trailing-return-type denotes the following type:
8843 * if the compound-statement is of the form
8844 { return attribute-specifier [opt] expression ; }
8845 the type of the returned expression after lvalue-to-rvalue
8846 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8847 (_conv.array_ 4.2), and function-to-pointer conversion
8849 * otherwise, void. */
8851 /* In a lambda that has neither a lambda-return-type-clause
8852 nor a deducible form, errors should be reported for return statements
8853 in the body. Since we used void as the placeholder return type, parsing
8854 the body as usual will give such desired behavior. */
8855 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
8856 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
8857 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
8859 tree expr
= NULL_TREE
;
8860 cp_id_kind idk
= CP_ID_KIND_NONE
;
8862 /* Parse tentatively in case there's more after the initial return
8864 cp_parser_parse_tentatively (parser
);
8866 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
8868 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
8870 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8871 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8873 if (cp_parser_parse_definitely (parser
))
8875 if (!processing_template_decl
)
8876 apply_deduced_return_type (fco
, lambda_return_type (expr
));
8878 /* Will get error here if type not deduced yet. */
8879 finish_return_stmt (expr
);
8887 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8888 cp_parser_label_declaration (parser
);
8889 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
8890 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8893 finish_compound_stmt (compound_stmt
);
8896 finish_function_body (body
);
8897 finish_lambda_scope ();
8899 /* Finish the function and generate code for it if necessary. */
8900 expand_or_defer_fn (finish_function (/*inline*/2));
8903 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
8905 pop_function_context();
8910 /* Statements [gram.stmt.stmt] */
8912 /* Parse a statement.
8916 expression-statement
8921 declaration-statement
8928 attribute-specifier-seq (opt) expression-statement
8929 attribute-specifier-seq (opt) compound-statement
8930 attribute-specifier-seq (opt) selection-statement
8931 attribute-specifier-seq (opt) iteration-statement
8932 attribute-specifier-seq (opt) jump-statement
8933 declaration-statement
8934 attribute-specifier-seq (opt) try-block
8941 IN_COMPOUND is true when the statement is nested inside a
8942 cp_parser_compound_statement; this matters for certain pragmas.
8944 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8945 is a (possibly labeled) if statement which is not enclosed in braces
8946 and has an else clause. This is used to implement -Wparentheses. */
8949 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
8950 bool in_compound
, bool *if_p
)
8952 tree statement
, std_attrs
= NULL_TREE
;
8954 location_t statement_location
, attrs_location
;
8959 /* There is no statement yet. */
8960 statement
= NULL_TREE
;
8962 cp_lexer_save_tokens (parser
->lexer
);
8963 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
8964 if (c_dialect_objc ())
8965 /* In obj-c++, seing '[[' might be the either the beginning of
8966 c++11 attributes, or a nested objc-message-expression. So
8967 let's parse the c++11 attributes tentatively. */
8968 cp_parser_parse_tentatively (parser
);
8969 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
8970 if (c_dialect_objc ())
8972 if (!cp_parser_parse_definitely (parser
))
8973 std_attrs
= NULL_TREE
;
8976 /* Peek at the next token. */
8977 token
= cp_lexer_peek_token (parser
->lexer
);
8978 /* Remember the location of the first token in the statement. */
8979 statement_location
= token
->location
;
8980 /* If this is a keyword, then that will often determine what kind of
8981 statement we have. */
8982 if (token
->type
== CPP_KEYWORD
)
8984 enum rid keyword
= token
->keyword
;
8990 /* Looks like a labeled-statement with a case label.
8991 Parse the label, and then use tail recursion to parse
8993 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
8998 statement
= cp_parser_selection_statement (parser
, if_p
);
9004 statement
= cp_parser_iteration_statement (parser
);
9011 statement
= cp_parser_jump_statement (parser
);
9014 /* Objective-C++ exception-handling constructs. */
9017 case RID_AT_FINALLY
:
9018 case RID_AT_SYNCHRONIZED
:
9020 statement
= cp_parser_objc_statement (parser
);
9024 statement
= cp_parser_try_block (parser
);
9028 /* This must be a namespace alias definition. */
9029 cp_parser_declaration_statement (parser
);
9032 case RID_TRANSACTION_ATOMIC
:
9033 case RID_TRANSACTION_RELAXED
:
9034 statement
= cp_parser_transaction (parser
, keyword
);
9036 case RID_TRANSACTION_CANCEL
:
9037 statement
= cp_parser_transaction_cancel (parser
);
9041 /* It might be a keyword like `int' that can start a
9042 declaration-statement. */
9046 else if (token
->type
== CPP_NAME
)
9048 /* If the next token is a `:', then we are looking at a
9049 labeled-statement. */
9050 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9051 if (token
->type
== CPP_COLON
)
9053 /* Looks like a labeled-statement with an ordinary label.
9054 Parse the label, and then use tail recursion to parse
9057 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9061 /* Anything that starts with a `{' must be a compound-statement. */
9062 else if (token
->type
== CPP_OPEN_BRACE
)
9063 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9064 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9065 a statement all its own. */
9066 else if (token
->type
== CPP_PRAGMA
)
9068 /* Only certain OpenMP pragmas are attached to statements, and thus
9069 are considered statements themselves. All others are not. In
9070 the context of a compound, accept the pragma as a "statement" and
9071 return so that we can check for a close brace. Otherwise we
9072 require a real statement and must go back and read one. */
9074 cp_parser_pragma (parser
, pragma_compound
);
9075 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9079 else if (token
->type
== CPP_EOF
)
9081 cp_parser_error (parser
, "expected statement");
9085 /* Everything else must be a declaration-statement or an
9086 expression-statement. Try for the declaration-statement
9087 first, unless we are looking at a `;', in which case we know that
9088 we have an expression-statement. */
9091 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9093 if (std_attrs
!= NULL_TREE
)
9095 /* Attributes should be parsed as part of the the
9096 declaration, so let's un-parse them. */
9097 cp_lexer_rollback_tokens (parser
->lexer
);
9098 std_attrs
= NULL_TREE
;
9101 cp_parser_parse_tentatively (parser
);
9102 /* Try to parse the declaration-statement. */
9103 cp_parser_declaration_statement (parser
);
9104 /* If that worked, we're done. */
9105 if (cp_parser_parse_definitely (parser
))
9108 /* Look for an expression-statement instead. */
9109 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9112 /* Set the line number for the statement. */
9113 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9114 SET_EXPR_LOCATION (statement
, statement_location
);
9116 /* Note that for now, we don't do anything with c++11 statements
9117 parsed at this level. */
9118 if (std_attrs
!= NULL_TREE
)
9119 warning_at (attrs_location
,
9121 "attributes at the beginning of statement are ignored");
9124 /* Parse the label for a labeled-statement, i.e.
9127 case constant-expression :
9131 case constant-expression ... constant-expression : statement
9133 When a label is parsed without errors, the label is added to the
9134 parse tree by the finish_* functions, so this function doesn't
9135 have to return the label. */
9138 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9141 tree label
= NULL_TREE
;
9142 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9144 /* The next token should be an identifier. */
9145 token
= cp_lexer_peek_token (parser
->lexer
);
9146 if (token
->type
!= CPP_NAME
9147 && token
->type
!= CPP_KEYWORD
)
9149 cp_parser_error (parser
, "expected labeled-statement");
9153 parser
->colon_corrects_to_scope_p
= false;
9154 switch (token
->keyword
)
9161 /* Consume the `case' token. */
9162 cp_lexer_consume_token (parser
->lexer
);
9163 /* Parse the constant-expression. */
9164 expr
= cp_parser_constant_expression (parser
,
9165 /*allow_non_constant_p=*/false,
9168 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9169 if (ellipsis
->type
== CPP_ELLIPSIS
)
9171 /* Consume the `...' token. */
9172 cp_lexer_consume_token (parser
->lexer
);
9174 cp_parser_constant_expression (parser
,
9175 /*allow_non_constant_p=*/false,
9177 /* We don't need to emit warnings here, as the common code
9178 will do this for us. */
9181 expr_hi
= NULL_TREE
;
9183 if (parser
->in_switch_statement_p
)
9184 finish_case_label (token
->location
, expr
, expr_hi
);
9186 error_at (token
->location
,
9187 "case label %qE not within a switch statement",
9193 /* Consume the `default' token. */
9194 cp_lexer_consume_token (parser
->lexer
);
9196 if (parser
->in_switch_statement_p
)
9197 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9199 error_at (token
->location
, "case label not within a switch statement");
9203 /* Anything else must be an ordinary label. */
9204 label
= finish_label_stmt (cp_parser_identifier (parser
));
9208 /* Require the `:' token. */
9209 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9211 /* An ordinary label may optionally be followed by attributes.
9212 However, this is only permitted if the attributes are then
9213 followed by a semicolon. This is because, for backward
9214 compatibility, when parsing
9215 lab: __attribute__ ((unused)) int i;
9216 we want the attribute to attach to "i", not "lab". */
9217 if (label
!= NULL_TREE
9218 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9221 cp_parser_parse_tentatively (parser
);
9222 attrs
= cp_parser_gnu_attributes_opt (parser
);
9223 if (attrs
== NULL_TREE
9224 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9225 cp_parser_abort_tentative_parse (parser
);
9226 else if (!cp_parser_parse_definitely (parser
))
9229 attributes
= chainon (attributes
, attrs
);
9232 if (attributes
!= NULL_TREE
)
9233 cplus_decl_attributes (&label
, attributes
, 0);
9235 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9238 /* Parse an expression-statement.
9240 expression-statement:
9243 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9244 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9245 indicates whether this expression-statement is part of an
9246 expression statement. */
9249 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9251 tree statement
= NULL_TREE
;
9252 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9254 /* If the next token is a ';', then there is no expression
9256 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9257 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9259 /* Give a helpful message for "A<T>::type t;" and the like. */
9260 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9261 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9263 if (TREE_CODE (statement
) == SCOPE_REF
)
9264 error_at (token
->location
, "need %<typename%> before %qE because "
9265 "%qT is a dependent scope",
9266 statement
, TREE_OPERAND (statement
, 0));
9267 else if (is_overloaded_fn (statement
)
9268 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9271 tree fn
= get_first_fn (statement
);
9272 error_at (token
->location
,
9273 "%<%T::%D%> names the constructor, not the type",
9274 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9278 /* Consume the final `;'. */
9279 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9281 if (in_statement_expr
9282 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9283 /* This is the final expression statement of a statement
9285 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9287 statement
= finish_expr_stmt (statement
);
9294 /* Parse a compound-statement.
9297 { statement-seq [opt] }
9302 { label-declaration-seq [opt] statement-seq [opt] }
9304 label-declaration-seq:
9306 label-declaration-seq label-declaration
9308 Returns a tree representing the statement. */
9311 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
9312 bool in_try
, bool function_body
)
9316 /* Consume the `{'. */
9317 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9318 return error_mark_node
;
9319 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
9321 pedwarn (input_location
, OPT_Wpedantic
,
9322 "compound-statement in constexpr function");
9323 /* Begin the compound-statement. */
9324 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
9325 /* If the next keyword is `__label__' we have a label declaration. */
9326 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9327 cp_parser_label_declaration (parser
);
9328 /* Parse an (optional) statement-seq. */
9329 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
9330 /* Finish the compound-statement. */
9331 finish_compound_stmt (compound_stmt
);
9332 /* Consume the `}'. */
9333 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9335 return compound_stmt
;
9338 /* Parse an (optional) statement-seq.
9342 statement-seq [opt] statement */
9345 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
9347 /* Scan statements until there aren't any more. */
9350 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9352 /* If we are looking at a `}', then we have run out of
9353 statements; the same is true if we have reached the end
9354 of file, or have stumbled upon a stray '@end'. */
9355 if (token
->type
== CPP_CLOSE_BRACE
9356 || token
->type
== CPP_EOF
9357 || token
->type
== CPP_PRAGMA_EOL
9358 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
9361 /* If we are in a compound statement and find 'else' then
9362 something went wrong. */
9363 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
9365 if (parser
->in_statement
& IN_IF_STMT
)
9369 token
= cp_lexer_consume_token (parser
->lexer
);
9370 error_at (token
->location
, "%<else%> without a previous %<if%>");
9374 /* Parse the statement. */
9375 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
9379 /* Parse a selection-statement.
9381 selection-statement:
9382 if ( condition ) statement
9383 if ( condition ) statement else statement
9384 switch ( condition ) statement
9386 Returns the new IF_STMT or SWITCH_STMT.
9388 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9389 is a (possibly labeled) if statement which is not enclosed in
9390 braces and has an else clause. This is used to implement
9394 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
9402 /* Peek at the next token. */
9403 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
9405 /* See what kind of keyword it is. */
9406 keyword
= token
->keyword
;
9415 /* Look for the `('. */
9416 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
9418 cp_parser_skip_to_end_of_statement (parser
);
9419 return error_mark_node
;
9422 /* Begin the selection-statement. */
9423 if (keyword
== RID_IF
)
9424 statement
= begin_if_stmt ();
9426 statement
= begin_switch_stmt ();
9428 /* Parse the condition. */
9429 condition
= cp_parser_condition (parser
);
9430 /* Look for the `)'. */
9431 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
9432 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9433 /*consume_paren=*/true);
9435 if (keyword
== RID_IF
)
9438 unsigned char in_statement
;
9440 /* Add the condition. */
9441 finish_if_stmt_cond (condition
, statement
);
9443 /* Parse the then-clause. */
9444 in_statement
= parser
->in_statement
;
9445 parser
->in_statement
|= IN_IF_STMT
;
9446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9448 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9449 add_stmt (build_empty_stmt (loc
));
9450 cp_lexer_consume_token (parser
->lexer
);
9451 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
9452 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
9453 "empty body in an %<if%> statement");
9457 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
9458 parser
->in_statement
= in_statement
;
9460 finish_then_clause (statement
);
9462 /* If the next token is `else', parse the else-clause. */
9463 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9466 /* Consume the `else' keyword. */
9467 cp_lexer_consume_token (parser
->lexer
);
9468 begin_else_clause (statement
);
9469 /* Parse the else-clause. */
9470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9473 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9475 OPT_Wempty_body
, "suggest braces around "
9476 "empty body in an %<else%> statement");
9477 add_stmt (build_empty_stmt (loc
));
9478 cp_lexer_consume_token (parser
->lexer
);
9481 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9483 finish_else_clause (statement
);
9485 /* If we are currently parsing a then-clause, then
9486 IF_P will not be NULL. We set it to true to
9487 indicate that this if statement has an else clause.
9488 This may trigger the Wparentheses warning below
9489 when we get back up to the parent if statement. */
9495 /* This if statement does not have an else clause. If
9496 NESTED_IF is true, then the then-clause is an if
9497 statement which does have an else clause. We warn
9498 about the potential ambiguity. */
9500 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
9501 "suggest explicit braces to avoid ambiguous"
9505 /* Now we're all done with the if-statement. */
9506 finish_if_stmt (statement
);
9510 bool in_switch_statement_p
;
9511 unsigned char in_statement
;
9513 /* Add the condition. */
9514 finish_switch_cond (condition
, statement
);
9516 /* Parse the body of the switch-statement. */
9517 in_switch_statement_p
= parser
->in_switch_statement_p
;
9518 in_statement
= parser
->in_statement
;
9519 parser
->in_switch_statement_p
= true;
9520 parser
->in_statement
|= IN_SWITCH_STMT
;
9521 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9522 parser
->in_switch_statement_p
= in_switch_statement_p
;
9523 parser
->in_statement
= in_statement
;
9525 /* Now we're all done with the switch-statement. */
9526 finish_switch_stmt (statement
);
9534 cp_parser_error (parser
, "expected selection-statement");
9535 return error_mark_node
;
9539 /* Parse a condition.
9543 type-specifier-seq declarator = initializer-clause
9544 type-specifier-seq declarator braced-init-list
9549 type-specifier-seq declarator asm-specification [opt]
9550 attributes [opt] = assignment-expression
9552 Returns the expression that should be tested. */
9555 cp_parser_condition (cp_parser
* parser
)
9557 cp_decl_specifier_seq type_specifiers
;
9558 const char *saved_message
;
9559 int declares_class_or_enum
;
9561 /* Try the declaration first. */
9562 cp_parser_parse_tentatively (parser
);
9563 /* New types are not allowed in the type-specifier-seq for a
9565 saved_message
= parser
->type_definition_forbidden_message
;
9566 parser
->type_definition_forbidden_message
9567 = G_("types may not be defined in conditions");
9568 /* Parse the type-specifier-seq. */
9569 cp_parser_decl_specifier_seq (parser
,
9570 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
9572 &declares_class_or_enum
);
9573 /* Restore the saved message. */
9574 parser
->type_definition_forbidden_message
= saved_message
;
9575 /* If all is well, we might be looking at a declaration. */
9576 if (!cp_parser_error_occurred (parser
))
9579 tree asm_specification
;
9581 cp_declarator
*declarator
;
9582 tree initializer
= NULL_TREE
;
9584 /* Parse the declarator. */
9585 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9586 /*ctor_dtor_or_conv_p=*/NULL
,
9587 /*parenthesized_p=*/NULL
,
9588 /*member_p=*/false);
9589 /* Parse the attributes. */
9590 attributes
= cp_parser_attributes_opt (parser
);
9591 /* Parse the asm-specification. */
9592 asm_specification
= cp_parser_asm_specification_opt (parser
);
9593 /* If the next token is not an `=' or '{', then we might still be
9594 looking at an expression. For example:
9598 looks like a decl-specifier-seq and a declarator -- but then
9599 there is no `=', so this is an expression. */
9600 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9601 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9602 cp_parser_simulate_error (parser
);
9604 /* If we did see an `=' or '{', then we are looking at a declaration
9606 if (cp_parser_parse_definitely (parser
))
9609 bool non_constant_p
;
9610 bool flags
= LOOKUP_ONLYCONVERTING
;
9612 /* Create the declaration. */
9613 decl
= start_decl (declarator
, &type_specifiers
,
9614 /*initialized_p=*/true,
9615 attributes
, /*prefix_attributes=*/NULL_TREE
,
9618 /* Parse the initializer. */
9619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9621 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
9622 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
9627 /* Consume the `='. */
9628 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
9629 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
9631 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
9632 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9634 /* Process the initializer. */
9635 cp_finish_decl (decl
,
9636 initializer
, !non_constant_p
,
9641 pop_scope (pushed_scope
);
9643 return convert_from_reference (decl
);
9646 /* If we didn't even get past the declarator successfully, we are
9647 definitely not looking at a declaration. */
9649 cp_parser_abort_tentative_parse (parser
);
9651 /* Otherwise, we are looking at an expression. */
9652 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9655 /* Parses a for-statement or range-for-statement until the closing ')',
9659 cp_parser_for (cp_parser
*parser
)
9661 tree init
, scope
, decl
;
9664 /* Begin the for-statement. */
9665 scope
= begin_for_scope (&init
);
9667 /* Parse the initialization. */
9668 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
9671 return cp_parser_range_for (parser
, scope
, init
, decl
);
9673 return cp_parser_c_for (parser
, scope
, init
);
9677 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
)
9679 /* Normal for loop */
9680 tree condition
= NULL_TREE
;
9681 tree expression
= NULL_TREE
;
9684 stmt
= begin_for_stmt (scope
, init
);
9685 /* The for-init-statement has already been parsed in
9686 cp_parser_for_init_statement, so no work is needed here. */
9687 finish_for_init_stmt (stmt
);
9689 /* If there's a condition, process it. */
9690 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9691 condition
= cp_parser_condition (parser
);
9692 finish_for_cond (condition
, stmt
);
9693 /* Look for the `;'. */
9694 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9696 /* If there's an expression, process it. */
9697 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
9698 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9699 finish_for_expr (expression
, stmt
);
9704 /* Tries to parse a range-based for-statement:
9707 decl-specifier-seq declarator : expression
9709 The decl-specifier-seq declarator and the `:' are already parsed by
9710 cp_parser_for_init_statement. If processing_template_decl it returns a
9711 newly created RANGE_FOR_STMT; if not, it is converted to a
9712 regular FOR_STMT. */
9715 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
)
9717 tree stmt
, range_expr
;
9719 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9721 bool expr_non_constant_p
;
9722 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9725 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9727 /* If in template, STMT is converted to a normal for-statement
9728 at instantiation. If not, it is done just ahead. */
9729 if (processing_template_decl
)
9731 if (check_for_bare_parameter_packs (range_expr
))
9732 range_expr
= error_mark_node
;
9733 stmt
= begin_range_for_stmt (scope
, init
);
9734 finish_range_for_decl (stmt
, range_decl
, range_expr
);
9735 if (range_expr
!= error_mark_node
9736 && !type_dependent_expression_p (range_expr
)
9737 /* The length of an array might be dependent. */
9738 && COMPLETE_TYPE_P (complete_type (TREE_TYPE (range_expr
)))
9739 /* do_auto_deduction doesn't mess with template init-lists. */
9740 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
9741 do_range_for_auto_deduction (range_decl
, range_expr
);
9745 stmt
= begin_for_stmt (scope
, init
);
9746 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
);
9751 /* Subroutine of cp_convert_range_for: given the initializer expression,
9752 builds up the range temporary. */
9755 build_range_temp (tree range_expr
)
9757 tree range_type
, range_temp
;
9759 /* Find out the type deduced by the declaration
9760 `auto &&__range = range_expr'. */
9761 range_type
= cp_build_reference_type (make_auto (), true);
9762 range_type
= do_auto_deduction (range_type
, range_expr
,
9763 type_uses_auto (range_type
));
9765 /* Create the __range variable. */
9766 range_temp
= build_decl (input_location
, VAR_DECL
,
9767 get_identifier ("__for_range"), range_type
);
9768 TREE_USED (range_temp
) = 1;
9769 DECL_ARTIFICIAL (range_temp
) = 1;
9774 /* Used by cp_parser_range_for in template context: we aren't going to
9775 do a full conversion yet, but we still need to resolve auto in the
9776 type of the for-range-declaration if present. This is basically
9777 a shortcut version of cp_convert_range_for. */
9780 do_range_for_auto_deduction (tree decl
, tree range_expr
)
9782 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
9785 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
9786 range_temp
= convert_from_reference (build_range_temp (range_expr
));
9787 iter_type
= (cp_parser_perform_range_for_lookup
9788 (range_temp
, &begin_dummy
, &end_dummy
));
9789 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
9790 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
9791 tf_warning_or_error
);
9792 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
9793 iter_decl
, auto_node
);
9797 /* Converts a range-based for-statement into a normal
9798 for-statement, as per the definition.
9800 for (RANGE_DECL : RANGE_EXPR)
9803 should be equivalent to:
9806 auto &&__range = RANGE_EXPR;
9807 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9811 RANGE_DECL = *__begin;
9816 If RANGE_EXPR is an array:
9817 BEGIN_EXPR = __range
9818 END_EXPR = __range + ARRAY_SIZE(__range)
9819 Else if RANGE_EXPR has a member 'begin' or 'end':
9820 BEGIN_EXPR = __range.begin()
9821 END_EXPR = __range.end()
9823 BEGIN_EXPR = begin(__range)
9824 END_EXPR = end(__range);
9826 If __range has a member 'begin' but not 'end', or vice versa, we must
9827 still use the second alternative (it will surely fail, however).
9828 When calling begin()/end() in the third alternative we must use
9829 argument dependent lookup, but always considering 'std' as an associated
9833 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
)
9836 tree iter_type
, begin_expr
, end_expr
;
9837 tree condition
, expression
;
9839 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
9840 /* If an error happened previously do nothing or else a lot of
9841 unhelpful errors would be issued. */
9842 begin_expr
= end_expr
= iter_type
= error_mark_node
;
9847 if (TREE_CODE (range_expr
) == VAR_DECL
9848 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
9849 /* Can't bind a reference to an array of runtime bound. */
9850 range_temp
= range_expr
;
9853 range_temp
= build_range_temp (range_expr
);
9854 pushdecl (range_temp
);
9855 cp_finish_decl (range_temp
, range_expr
,
9856 /*is_constant_init*/false, NULL_TREE
,
9857 LOOKUP_ONLYCONVERTING
);
9858 range_temp
= convert_from_reference (range_temp
);
9860 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
9861 &begin_expr
, &end_expr
);
9864 /* The new for initialization statement. */
9865 begin
= build_decl (input_location
, VAR_DECL
,
9866 get_identifier ("__for_begin"), iter_type
);
9867 TREE_USED (begin
) = 1;
9868 DECL_ARTIFICIAL (begin
) = 1;
9870 cp_finish_decl (begin
, begin_expr
,
9871 /*is_constant_init*/false, NULL_TREE
,
9872 LOOKUP_ONLYCONVERTING
);
9874 end
= build_decl (input_location
, VAR_DECL
,
9875 get_identifier ("__for_end"), iter_type
);
9876 TREE_USED (end
) = 1;
9877 DECL_ARTIFICIAL (end
) = 1;
9879 cp_finish_decl (end
, end_expr
,
9880 /*is_constant_init*/false, NULL_TREE
,
9881 LOOKUP_ONLYCONVERTING
);
9883 finish_for_init_stmt (statement
);
9885 /* The new for condition. */
9886 condition
= build_x_binary_op (input_location
, NE_EXPR
,
9889 NULL
, tf_warning_or_error
);
9890 finish_for_cond (condition
, statement
);
9892 /* The new increment expression. */
9893 expression
= finish_unary_op_expr (input_location
,
9894 PREINCREMENT_EXPR
, begin
,
9895 tf_warning_or_error
);
9896 finish_for_expr (expression
, statement
);
9898 /* The declaration is initialized with *__begin inside the loop body. */
9899 cp_finish_decl (range_decl
,
9900 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
9901 tf_warning_or_error
),
9902 /*is_constant_init*/false, NULL_TREE
,
9903 LOOKUP_ONLYCONVERTING
);
9908 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9909 We need to solve both at the same time because the method used
9910 depends on the existence of members begin or end.
9911 Returns the type deduced for the iterator expression. */
9914 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
9916 if (error_operand_p (range
))
9918 *begin
= *end
= error_mark_node
;
9919 return error_mark_node
;
9922 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
9924 error ("range-based %<for%> expression of type %qT "
9925 "has incomplete type", TREE_TYPE (range
));
9926 *begin
= *end
= error_mark_node
;
9927 return error_mark_node
;
9929 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
9931 /* If RANGE is an array, we will use pointer arithmetic. */
9933 *end
= build_binary_op (input_location
, PLUS_EXPR
,
9935 array_type_nelts_top (TREE_TYPE (range
)),
9937 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
9941 /* If it is not an array, we must do a bit of magic. */
9942 tree id_begin
, id_end
;
9943 tree member_begin
, member_end
;
9945 *begin
= *end
= error_mark_node
;
9947 id_begin
= get_identifier ("begin");
9948 id_end
= get_identifier ("end");
9949 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
9950 /*protect=*/2, /*want_type=*/false,
9951 tf_warning_or_error
);
9952 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
9953 /*protect=*/2, /*want_type=*/false,
9954 tf_warning_or_error
);
9956 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
9958 /* Use the member functions. */
9959 if (member_begin
!= NULL_TREE
)
9960 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
9962 error ("range-based %<for%> expression of type %qT has an "
9963 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
9965 if (member_end
!= NULL_TREE
)
9966 *end
= cp_parser_range_for_member_function (range
, id_end
);
9968 error ("range-based %<for%> expression of type %qT has a "
9969 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
9973 /* Use global functions with ADL. */
9974 vec
<tree
, va_gc
> *vec
;
9975 vec
= make_tree_vector ();
9977 vec_safe_push (vec
, range
);
9979 member_begin
= perform_koenig_lookup (id_begin
, vec
,
9980 /*include_std=*/true,
9981 tf_warning_or_error
);
9982 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
9983 tf_warning_or_error
);
9984 member_end
= perform_koenig_lookup (id_end
, vec
,
9985 /*include_std=*/true,
9986 tf_warning_or_error
);
9987 *end
= finish_call_expr (member_end
, &vec
, false, true,
9988 tf_warning_or_error
);
9990 release_tree_vector (vec
);
9993 /* Last common checks. */
9994 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
9996 /* If one of the expressions is an error do no more checks. */
9997 *begin
= *end
= error_mark_node
;
9998 return error_mark_node
;
10002 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10003 /* The unqualified type of the __begin and __end temporaries should
10004 be the same, as required by the multiple auto declaration. */
10005 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10006 error ("inconsistent begin/end types in range-based %<for%> "
10007 "statement: %qT and %qT",
10008 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10014 /* Helper function for cp_parser_perform_range_for_lookup.
10015 Builds a tree for RANGE.IDENTIFIER(). */
10018 cp_parser_range_for_member_function (tree range
, tree identifier
)
10021 vec
<tree
, va_gc
> *vec
;
10023 member
= finish_class_member_access_expr (range
, identifier
,
10024 false, tf_warning_or_error
);
10025 if (member
== error_mark_node
)
10026 return error_mark_node
;
10028 vec
= make_tree_vector ();
10029 res
= finish_call_expr (member
, &vec
,
10030 /*disallow_virtual=*/false,
10031 /*koenig_p=*/false,
10032 tf_warning_or_error
);
10033 release_tree_vector (vec
);
10037 /* Parse an iteration-statement.
10039 iteration-statement:
10040 while ( condition ) statement
10041 do statement while ( expression ) ;
10042 for ( for-init-statement condition [opt] ; expression [opt] )
10045 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10048 cp_parser_iteration_statement (cp_parser
* parser
)
10053 unsigned char in_statement
;
10055 /* Peek at the next token. */
10056 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10058 return error_mark_node
;
10060 /* Remember whether or not we are already within an iteration
10062 in_statement
= parser
->in_statement
;
10064 /* See what kind of keyword it is. */
10065 keyword
= token
->keyword
;
10072 /* Begin the while-statement. */
10073 statement
= begin_while_stmt ();
10074 /* Look for the `('. */
10075 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10076 /* Parse the condition. */
10077 condition
= cp_parser_condition (parser
);
10078 finish_while_stmt_cond (condition
, statement
);
10079 /* Look for the `)'. */
10080 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10081 /* Parse the dependent statement. */
10082 parser
->in_statement
= IN_ITERATION_STMT
;
10083 cp_parser_already_scoped_statement (parser
);
10084 parser
->in_statement
= in_statement
;
10085 /* We're done with the while-statement. */
10086 finish_while_stmt (statement
);
10094 /* Begin the do-statement. */
10095 statement
= begin_do_stmt ();
10096 /* Parse the body of the do-statement. */
10097 parser
->in_statement
= IN_ITERATION_STMT
;
10098 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10099 parser
->in_statement
= in_statement
;
10100 finish_do_body (statement
);
10101 /* Look for the `while' keyword. */
10102 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10103 /* Look for the `('. */
10104 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10105 /* Parse the expression. */
10106 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10107 /* We're done with the do-statement. */
10108 finish_do_stmt (expression
, statement
);
10109 /* Look for the `)'. */
10110 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10111 /* Look for the `;'. */
10112 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10118 /* Look for the `('. */
10119 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10121 statement
= cp_parser_for (parser
);
10123 /* Look for the `)'. */
10124 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10126 /* Parse the body of the for-statement. */
10127 parser
->in_statement
= IN_ITERATION_STMT
;
10128 cp_parser_already_scoped_statement (parser
);
10129 parser
->in_statement
= in_statement
;
10131 /* We're done with the for-statement. */
10132 finish_for_stmt (statement
);
10137 cp_parser_error (parser
, "expected iteration-statement");
10138 statement
= error_mark_node
;
10145 /* Parse a for-init-statement or the declarator of a range-based-for.
10146 Returns true if a range-based-for declaration is seen.
10148 for-init-statement:
10149 expression-statement
10150 simple-declaration */
10153 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10155 /* If the next token is a `;', then we have an empty
10156 expression-statement. Grammatically, this is also a
10157 simple-declaration, but an invalid one, because it does not
10158 declare anything. Therefore, if we did not handle this case
10159 specially, we would issue an error message about an invalid
10161 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10163 bool is_range_for
= false;
10164 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10166 parser
->colon_corrects_to_scope_p
= false;
10168 /* We're going to speculatively look for a declaration, falling back
10169 to an expression, if necessary. */
10170 cp_parser_parse_tentatively (parser
);
10171 /* Parse the declaration. */
10172 cp_parser_simple_declaration (parser
,
10173 /*function_definition_allowed_p=*/false,
10175 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10178 /* It is a range-for, consume the ':' */
10179 cp_lexer_consume_token (parser
->lexer
);
10180 is_range_for
= true;
10181 if (cxx_dialect
< cxx0x
)
10183 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10184 "range-based %<for%> loops are not allowed "
10186 *decl
= error_mark_node
;
10190 /* The ';' is not consumed yet because we told
10191 cp_parser_simple_declaration not to. */
10192 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10194 if (cp_parser_parse_definitely (parser
))
10195 return is_range_for
;
10196 /* If the tentative parse failed, then we shall need to look for an
10197 expression-statement. */
10199 /* If we are here, it is an expression-statement. */
10200 cp_parser_expression_statement (parser
, NULL_TREE
);
10204 /* Parse a jump-statement.
10209 return expression [opt] ;
10210 return braced-init-list ;
10216 goto * expression ;
10218 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10221 cp_parser_jump_statement (cp_parser
* parser
)
10223 tree statement
= error_mark_node
;
10226 unsigned char in_statement
;
10228 /* Peek at the next token. */
10229 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10231 return error_mark_node
;
10233 /* See what kind of keyword it is. */
10234 keyword
= token
->keyword
;
10238 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
10239 switch (in_statement
)
10242 error_at (token
->location
, "break statement not within loop or switch");
10245 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
10246 || in_statement
== IN_ITERATION_STMT
);
10247 statement
= finish_break_stmt ();
10250 error_at (token
->location
, "invalid exit from OpenMP structured block");
10253 error_at (token
->location
, "break statement used with OpenMP for loop");
10256 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10260 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10263 error_at (token
->location
, "continue statement not within a loop");
10265 case IN_ITERATION_STMT
:
10267 statement
= finish_continue_stmt ();
10270 error_at (token
->location
, "invalid exit from OpenMP structured block");
10273 gcc_unreachable ();
10275 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10281 bool expr_non_constant_p
;
10283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10285 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10286 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10288 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10289 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10291 /* If the next token is a `;', then there is no
10294 /* Build the return-statement. */
10295 statement
= finish_return_stmt (expr
);
10296 /* Look for the final `;'. */
10297 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10302 /* Create the goto-statement. */
10303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
10305 /* Issue a warning about this use of a GNU extension. */
10306 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
10307 /* Consume the '*' token. */
10308 cp_lexer_consume_token (parser
->lexer
);
10309 /* Parse the dependent expression. */
10310 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
10313 finish_goto_stmt (cp_parser_identifier (parser
));
10314 /* Look for the final `;'. */
10315 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10319 cp_parser_error (parser
, "expected jump-statement");
10326 /* Parse a declaration-statement.
10328 declaration-statement:
10329 block-declaration */
10332 cp_parser_declaration_statement (cp_parser
* parser
)
10336 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10337 p
= obstack_alloc (&declarator_obstack
, 0);
10339 /* Parse the block-declaration. */
10340 cp_parser_block_declaration (parser
, /*statement_p=*/true);
10342 /* Free any declarators allocated. */
10343 obstack_free (&declarator_obstack
, p
);
10345 /* Finish off the statement. */
10349 /* Some dependent statements (like `if (cond) statement'), are
10350 implicitly in their own scope. In other words, if the statement is
10351 a single statement (as opposed to a compound-statement), it is
10352 none-the-less treated as if it were enclosed in braces. Any
10353 declarations appearing in the dependent statement are out of scope
10354 after control passes that point. This function parses a statement,
10355 but ensures that is in its own scope, even if it is not a
10356 compound-statement.
10358 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10359 is a (possibly labeled) if statement which is not enclosed in
10360 braces and has an else clause. This is used to implement
10363 Returns the new statement. */
10366 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
10373 /* Mark if () ; with a special NOP_EXPR. */
10374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10376 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10377 cp_lexer_consume_token (parser
->lexer
);
10378 statement
= add_stmt (build_empty_stmt (loc
));
10380 /* if a compound is opened, we simply parse the statement directly. */
10381 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10382 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
10383 /* If the token is not a `{', then we must take special action. */
10386 /* Create a compound-statement. */
10387 statement
= begin_compound_stmt (0);
10388 /* Parse the dependent-statement. */
10389 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
10390 /* Finish the dummy compound-statement. */
10391 finish_compound_stmt (statement
);
10394 /* Return the statement. */
10398 /* For some dependent statements (like `while (cond) statement'), we
10399 have already created a scope. Therefore, even if the dependent
10400 statement is a compound-statement, we do not want to create another
10404 cp_parser_already_scoped_statement (cp_parser
* parser
)
10406 /* If the token is a `{', then we must take special action. */
10407 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10408 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
10411 /* Avoid calling cp_parser_compound_statement, so that we
10412 don't create a new scope. Do everything else by hand. */
10413 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
10414 /* If the next keyword is `__label__' we have a label declaration. */
10415 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10416 cp_parser_label_declaration (parser
);
10417 /* Parse an (optional) statement-seq. */
10418 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10419 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10423 /* Declarations [gram.dcl.dcl] */
10425 /* Parse an optional declaration-sequence.
10429 declaration-seq declaration */
10432 cp_parser_declaration_seq_opt (cp_parser
* parser
)
10438 token
= cp_lexer_peek_token (parser
->lexer
);
10440 if (token
->type
== CPP_CLOSE_BRACE
10441 || token
->type
== CPP_EOF
10442 || token
->type
== CPP_PRAGMA_EOL
)
10445 if (token
->type
== CPP_SEMICOLON
)
10447 /* A declaration consisting of a single semicolon is
10448 invalid. Allow it unless we're being pedantic. */
10449 cp_lexer_consume_token (parser
->lexer
);
10450 if (!in_system_header
)
10451 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
10455 /* If we're entering or exiting a region that's implicitly
10456 extern "C", modify the lang context appropriately. */
10457 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
10459 push_lang_context (lang_name_c
);
10460 parser
->implicit_extern_c
= true;
10462 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
10464 pop_lang_context ();
10465 parser
->implicit_extern_c
= false;
10468 if (token
->type
== CPP_PRAGMA
)
10470 /* A top-level declaration can consist solely of a #pragma.
10471 A nested declaration cannot, so this is done here and not
10472 in cp_parser_declaration. (A #pragma at block scope is
10473 handled in cp_parser_statement.) */
10474 cp_parser_pragma (parser
, pragma_external
);
10478 /* Parse the declaration itself. */
10479 cp_parser_declaration (parser
);
10483 /* Parse a declaration.
10487 function-definition
10488 template-declaration
10489 explicit-instantiation
10490 explicit-specialization
10491 linkage-specification
10492 namespace-definition
10497 __extension__ declaration */
10500 cp_parser_declaration (cp_parser
* parser
)
10504 int saved_pedantic
;
10506 tree attributes
= NULL_TREE
;
10508 /* Check for the `__extension__' keyword. */
10509 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10511 /* Parse the qualified declaration. */
10512 cp_parser_declaration (parser
);
10513 /* Restore the PEDANTIC flag. */
10514 pedantic
= saved_pedantic
;
10519 /* Try to figure out what kind of declaration is present. */
10520 token1
= *cp_lexer_peek_token (parser
->lexer
);
10522 if (token1
.type
!= CPP_EOF
)
10523 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
10526 token2
.type
= CPP_EOF
;
10527 token2
.keyword
= RID_MAX
;
10530 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10531 p
= obstack_alloc (&declarator_obstack
, 0);
10533 /* If the next token is `extern' and the following token is a string
10534 literal, then we have a linkage specification. */
10535 if (token1
.keyword
== RID_EXTERN
10536 && cp_parser_is_pure_string_literal (&token2
))
10537 cp_parser_linkage_specification (parser
);
10538 /* If the next token is `template', then we have either a template
10539 declaration, an explicit instantiation, or an explicit
10541 else if (token1
.keyword
== RID_TEMPLATE
)
10543 /* `template <>' indicates a template specialization. */
10544 if (token2
.type
== CPP_LESS
10545 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
10546 cp_parser_explicit_specialization (parser
);
10547 /* `template <' indicates a template declaration. */
10548 else if (token2
.type
== CPP_LESS
)
10549 cp_parser_template_declaration (parser
, /*member_p=*/false);
10550 /* Anything else must be an explicit instantiation. */
10552 cp_parser_explicit_instantiation (parser
);
10554 /* If the next token is `export', then we have a template
10556 else if (token1
.keyword
== RID_EXPORT
)
10557 cp_parser_template_declaration (parser
, /*member_p=*/false);
10558 /* If the next token is `extern', 'static' or 'inline' and the one
10559 after that is `template', we have a GNU extended explicit
10560 instantiation directive. */
10561 else if (cp_parser_allow_gnu_extensions_p (parser
)
10562 && (token1
.keyword
== RID_EXTERN
10563 || token1
.keyword
== RID_STATIC
10564 || token1
.keyword
== RID_INLINE
)
10565 && token2
.keyword
== RID_TEMPLATE
)
10566 cp_parser_explicit_instantiation (parser
);
10567 /* If the next token is `namespace', check for a named or unnamed
10568 namespace definition. */
10569 else if (token1
.keyword
== RID_NAMESPACE
10570 && (/* A named namespace definition. */
10571 (token2
.type
== CPP_NAME
10572 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
10574 /* An unnamed namespace definition. */
10575 || token2
.type
== CPP_OPEN_BRACE
10576 || token2
.keyword
== RID_ATTRIBUTE
))
10577 cp_parser_namespace_definition (parser
);
10578 /* An inline (associated) namespace definition. */
10579 else if (token1
.keyword
== RID_INLINE
10580 && token2
.keyword
== RID_NAMESPACE
)
10581 cp_parser_namespace_definition (parser
);
10582 /* Objective-C++ declaration/definition. */
10583 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
10584 cp_parser_objc_declaration (parser
, NULL_TREE
);
10585 else if (c_dialect_objc ()
10586 && token1
.keyword
== RID_ATTRIBUTE
10587 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
10588 cp_parser_objc_declaration (parser
, attributes
);
10589 /* We must have either a block declaration or a function
10592 /* Try to parse a block-declaration, or a function-definition. */
10593 cp_parser_block_declaration (parser
, /*statement_p=*/false);
10595 /* Free any declarators allocated. */
10596 obstack_free (&declarator_obstack
, p
);
10599 /* Parse a block-declaration.
10604 namespace-alias-definition
10611 __extension__ block-declaration
10616 static_assert-declaration
10618 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10619 part of a declaration-statement. */
10622 cp_parser_block_declaration (cp_parser
*parser
,
10626 int saved_pedantic
;
10628 /* Check for the `__extension__' keyword. */
10629 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10631 /* Parse the qualified declaration. */
10632 cp_parser_block_declaration (parser
, statement_p
);
10633 /* Restore the PEDANTIC flag. */
10634 pedantic
= saved_pedantic
;
10639 /* Peek at the next token to figure out which kind of declaration is
10641 token1
= cp_lexer_peek_token (parser
->lexer
);
10643 /* If the next keyword is `asm', we have an asm-definition. */
10644 if (token1
->keyword
== RID_ASM
)
10647 cp_parser_commit_to_tentative_parse (parser
);
10648 cp_parser_asm_definition (parser
);
10650 /* If the next keyword is `namespace', we have a
10651 namespace-alias-definition. */
10652 else if (token1
->keyword
== RID_NAMESPACE
)
10653 cp_parser_namespace_alias_definition (parser
);
10654 /* If the next keyword is `using', we have a
10655 using-declaration, a using-directive, or an alias-declaration. */
10656 else if (token1
->keyword
== RID_USING
)
10661 cp_parser_commit_to_tentative_parse (parser
);
10662 /* If the token after `using' is `namespace', then we have a
10663 using-directive. */
10664 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10665 if (token2
->keyword
== RID_NAMESPACE
)
10666 cp_parser_using_directive (parser
);
10667 /* If the second token after 'using' is '=', then we have an
10668 alias-declaration. */
10669 else if (cxx_dialect
>= cxx0x
10670 && token2
->type
== CPP_NAME
10671 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
10672 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
10673 cp_parser_alias_declaration (parser
);
10674 /* Otherwise, it's a using-declaration. */
10676 cp_parser_using_declaration (parser
,
10677 /*access_declaration_p=*/false);
10679 /* If the next keyword is `__label__' we have a misplaced label
10681 else if (token1
->keyword
== RID_LABEL
)
10683 cp_lexer_consume_token (parser
->lexer
);
10684 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
10685 cp_parser_skip_to_end_of_statement (parser
);
10686 /* If the next token is now a `;', consume it. */
10687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10688 cp_lexer_consume_token (parser
->lexer
);
10690 /* If the next token is `static_assert' we have a static assertion. */
10691 else if (token1
->keyword
== RID_STATIC_ASSERT
)
10692 cp_parser_static_assert (parser
, /*member_p=*/false);
10693 /* Anything else must be a simple-declaration. */
10695 cp_parser_simple_declaration (parser
, !statement_p
,
10696 /*maybe_range_for_decl*/NULL
);
10699 /* Parse a simple-declaration.
10701 simple-declaration:
10702 decl-specifier-seq [opt] init-declarator-list [opt] ;
10704 init-declarator-list:
10706 init-declarator-list , init-declarator
10708 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10709 function-definition as a simple-declaration.
10711 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10712 parsed declaration if it is an uninitialized single declarator not followed
10713 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10714 if present, will not be consumed. */
10717 cp_parser_simple_declaration (cp_parser
* parser
,
10718 bool function_definition_allowed_p
,
10719 tree
*maybe_range_for_decl
)
10721 cp_decl_specifier_seq decl_specifiers
;
10722 int declares_class_or_enum
;
10723 bool saw_declarator
;
10725 if (maybe_range_for_decl
)
10726 *maybe_range_for_decl
= NULL_TREE
;
10728 /* Defer access checks until we know what is being declared; the
10729 checks for names appearing in the decl-specifier-seq should be
10730 done as if we were in the scope of the thing being declared. */
10731 push_deferring_access_checks (dk_deferred
);
10733 /* Parse the decl-specifier-seq. We have to keep track of whether
10734 or not the decl-specifier-seq declares a named class or
10735 enumeration type, since that is the only case in which the
10736 init-declarator-list is allowed to be empty.
10740 In a simple-declaration, the optional init-declarator-list can be
10741 omitted only when declaring a class or enumeration, that is when
10742 the decl-specifier-seq contains either a class-specifier, an
10743 elaborated-type-specifier, or an enum-specifier. */
10744 cp_parser_decl_specifier_seq (parser
,
10745 CP_PARSER_FLAGS_OPTIONAL
,
10747 &declares_class_or_enum
);
10748 /* We no longer need to defer access checks. */
10749 stop_deferring_access_checks ();
10751 /* In a block scope, a valid declaration must always have a
10752 decl-specifier-seq. By not trying to parse declarators, we can
10753 resolve the declaration/expression ambiguity more quickly. */
10754 if (!function_definition_allowed_p
10755 && !decl_specifiers
.any_specifiers_p
)
10757 cp_parser_error (parser
, "expected declaration");
10761 /* If the next two tokens are both identifiers, the code is
10762 erroneous. The usual cause of this situation is code like:
10766 where "T" should name a type -- but does not. */
10767 if (!decl_specifiers
.any_type_specifiers_p
10768 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
10770 /* If parsing tentatively, we should commit; we really are
10771 looking at a declaration. */
10772 cp_parser_commit_to_tentative_parse (parser
);
10777 /* If we have seen at least one decl-specifier, and the next token
10778 is not a parenthesis, then we must be looking at a declaration.
10779 (After "int (" we might be looking at a functional cast.) */
10780 if (decl_specifiers
.any_specifiers_p
10781 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
10782 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
10783 && !cp_parser_error_occurred (parser
))
10784 cp_parser_commit_to_tentative_parse (parser
);
10786 /* Keep going until we hit the `;' at the end of the simple
10788 saw_declarator
= false;
10789 while (cp_lexer_next_token_is_not (parser
->lexer
,
10793 bool function_definition_p
;
10796 if (saw_declarator
)
10798 /* If we are processing next declarator, coma is expected */
10799 token
= cp_lexer_peek_token (parser
->lexer
);
10800 gcc_assert (token
->type
== CPP_COMMA
);
10801 cp_lexer_consume_token (parser
->lexer
);
10802 if (maybe_range_for_decl
)
10803 *maybe_range_for_decl
= error_mark_node
;
10806 saw_declarator
= true;
10808 /* Parse the init-declarator. */
10809 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
10811 function_definition_allowed_p
,
10812 /*member_p=*/false,
10813 declares_class_or_enum
,
10814 &function_definition_p
,
10815 maybe_range_for_decl
);
10816 /* If an error occurred while parsing tentatively, exit quickly.
10817 (That usually happens when in the body of a function; each
10818 statement is treated as a declaration-statement until proven
10820 if (cp_parser_error_occurred (parser
))
10822 /* Handle function definitions specially. */
10823 if (function_definition_p
)
10825 /* If the next token is a `,', then we are probably
10826 processing something like:
10830 which is erroneous. */
10831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10833 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10834 error_at (token
->location
,
10836 " declarations and function-definitions is forbidden");
10838 /* Otherwise, we're done with the list of declarators. */
10841 pop_deferring_access_checks ();
10845 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
10846 *maybe_range_for_decl
= decl
;
10847 /* The next token should be either a `,' or a `;'. */
10848 token
= cp_lexer_peek_token (parser
->lexer
);
10849 /* If it's a `,', there are more declarators to come. */
10850 if (token
->type
== CPP_COMMA
)
10851 /* will be consumed next time around */;
10852 /* If it's a `;', we are done. */
10853 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
10855 /* Anything else is an error. */
10858 /* If we have already issued an error message we don't need
10859 to issue another one. */
10860 if (decl
!= error_mark_node
10861 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
10862 cp_parser_error (parser
, "expected %<,%> or %<;%>");
10863 /* Skip tokens until we reach the end of the statement. */
10864 cp_parser_skip_to_end_of_statement (parser
);
10865 /* If the next token is now a `;', consume it. */
10866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10867 cp_lexer_consume_token (parser
->lexer
);
10870 /* After the first time around, a function-definition is not
10871 allowed -- even if it was OK at first. For example:
10876 function_definition_allowed_p
= false;
10879 /* Issue an error message if no declarators are present, and the
10880 decl-specifier-seq does not itself declare a class or
10882 if (!saw_declarator
)
10884 if (cp_parser_declares_only_class_p (parser
))
10885 shadow_tag (&decl_specifiers
);
10886 /* Perform any deferred access checks. */
10887 perform_deferred_access_checks (tf_warning_or_error
);
10890 /* Consume the `;'. */
10891 if (!maybe_range_for_decl
)
10892 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10895 pop_deferring_access_checks ();
10898 /* Parse a decl-specifier-seq.
10900 decl-specifier-seq:
10901 decl-specifier-seq [opt] decl-specifier
10902 decl-specifier attribute-specifier-seq [opt] (C++11)
10905 storage-class-specifier
10916 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10918 The parser flags FLAGS is used to control type-specifier parsing.
10920 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10923 1: one of the decl-specifiers is an elaborated-type-specifier
10924 (i.e., a type declaration)
10925 2: one of the decl-specifiers is an enum-specifier or a
10926 class-specifier (i.e., a type definition)
10931 cp_parser_decl_specifier_seq (cp_parser
* parser
,
10932 cp_parser_flags flags
,
10933 cp_decl_specifier_seq
*decl_specs
,
10934 int* declares_class_or_enum
)
10936 bool constructor_possible_p
= !parser
->in_declarator_p
;
10937 bool found_decl_spec
= false;
10938 cp_token
*start_token
= NULL
;
10941 /* Clear DECL_SPECS. */
10942 clear_decl_specs (decl_specs
);
10944 /* Assume no class or enumeration type is declared. */
10945 *declares_class_or_enum
= 0;
10947 /* Keep reading specifiers until there are no more to read. */
10950 bool constructor_p
;
10954 /* Peek at the next token. */
10955 token
= cp_lexer_peek_token (parser
->lexer
);
10957 /* Save the first token of the decl spec list for error
10960 start_token
= token
;
10961 /* Handle attributes. */
10962 if (cp_next_tokens_can_be_attribute_p (parser
))
10964 /* Parse the attributes. */
10965 tree attrs
= cp_parser_attributes_opt (parser
);
10967 /* In a sequence of declaration specifiers, c++11 attributes
10968 appertain to the type that precede them. In that case
10971 The attribute-specifier-seq affects the type only for
10972 the declaration it appears in, not other declarations
10973 involving the same type.
10975 But for now let's force the user to position the
10976 attribute either at the beginning of the declaration or
10977 after the declarator-id, which would clearly mean that it
10978 applies to the declarator. */
10979 if (cxx11_attribute_p (attrs
))
10981 if (!found_decl_spec
)
10982 /* The c++11 attribute is at the beginning of the
10983 declaration. It appertains to the entity being
10987 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
10989 /* This is an attribute following a
10990 class-specifier. */
10991 if (decl_specs
->type_definition_p
)
10992 warn_misplaced_attr_for_class_type (token
->location
,
10998 decl_specs
->std_attributes
10999 = chainon (decl_specs
->std_attributes
,
11001 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11002 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11008 decl_specs
->attributes
11009 = chainon (decl_specs
->attributes
,
11011 if (decl_specs
->locations
[ds_attribute
] == 0)
11012 decl_specs
->locations
[ds_attribute
] = token
->location
;
11015 /* Assume we will find a decl-specifier keyword. */
11016 found_decl_spec
= true;
11017 /* If the next token is an appropriate keyword, we can simply
11018 add it to the list. */
11019 switch (token
->keyword
)
11025 if (!at_class_scope_p ())
11027 error_at (token
->location
, "%<friend%> used outside of class");
11028 cp_lexer_purge_token (parser
->lexer
);
11033 /* Consume the token. */
11034 cp_lexer_consume_token (parser
->lexer
);
11038 case RID_CONSTEXPR
:
11040 cp_lexer_consume_token (parser
->lexer
);
11043 /* function-specifier:
11050 cp_parser_function_specifier_opt (parser
, decl_specs
);
11057 /* Consume the token. */
11058 cp_lexer_consume_token (parser
->lexer
);
11059 /* A constructor declarator cannot appear in a typedef. */
11060 constructor_possible_p
= false;
11061 /* The "typedef" keyword can only occur in a declaration; we
11062 may as well commit at this point. */
11063 cp_parser_commit_to_tentative_parse (parser
);
11065 if (decl_specs
->storage_class
!= sc_none
)
11066 decl_specs
->conflicting_specifiers_p
= true;
11069 /* storage-class-specifier:
11079 if (cxx_dialect
== cxx98
)
11081 /* Consume the token. */
11082 cp_lexer_consume_token (parser
->lexer
);
11084 /* Complain about `auto' as a storage specifier, if
11085 we're complaining about C++0x compatibility. */
11086 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11087 " changes meaning in C++11; please remove it");
11089 /* Set the storage class anyway. */
11090 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11094 /* C++0x auto type-specifier. */
11095 found_decl_spec
= false;
11102 /* Consume the token. */
11103 cp_lexer_consume_token (parser
->lexer
);
11104 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11108 /* Consume the token. */
11110 cp_lexer_consume_token (parser
->lexer
);
11114 /* We did not yet find a decl-specifier yet. */
11115 found_decl_spec
= false;
11119 if (found_decl_spec
11120 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11121 && token
->keyword
!= RID_CONSTEXPR
)
11122 error ("decl-specifier invalid in condition");
11125 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11127 /* Constructors are a special case. The `S' in `S()' is not a
11128 decl-specifier; it is the beginning of the declarator. */
11130 = (!found_decl_spec
11131 && constructor_possible_p
11132 && (cp_parser_constructor_declarator_p
11133 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11135 /* If we don't have a DECL_SPEC yet, then we must be looking at
11136 a type-specifier. */
11137 if (!found_decl_spec
&& !constructor_p
)
11139 int decl_spec_declares_class_or_enum
;
11140 bool is_cv_qualifier
;
11144 = cp_parser_type_specifier (parser
, flags
,
11146 /*is_declaration=*/true,
11147 &decl_spec_declares_class_or_enum
,
11149 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11151 /* If this type-specifier referenced a user-defined type
11152 (a typedef, class-name, etc.), then we can't allow any
11153 more such type-specifiers henceforth.
11157 The longest sequence of decl-specifiers that could
11158 possibly be a type name is taken as the
11159 decl-specifier-seq of a declaration. The sequence shall
11160 be self-consistent as described below.
11164 As a general rule, at most one type-specifier is allowed
11165 in the complete decl-specifier-seq of a declaration. The
11166 only exceptions are the following:
11168 -- const or volatile can be combined with any other
11171 -- signed or unsigned can be combined with char, long,
11179 void g (const int Pc);
11181 Here, Pc is *not* part of the decl-specifier seq; it's
11182 the declarator. Therefore, once we see a type-specifier
11183 (other than a cv-qualifier), we forbid any additional
11184 user-defined types. We *do* still allow things like `int
11185 int' to be considered a decl-specifier-seq, and issue the
11186 error message later. */
11187 if (type_spec
&& !is_cv_qualifier
)
11188 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11189 /* A constructor declarator cannot follow a type-specifier. */
11192 constructor_possible_p
= false;
11193 found_decl_spec
= true;
11194 if (!is_cv_qualifier
)
11195 decl_specs
->any_type_specifiers_p
= true;
11199 /* If we still do not have a DECL_SPEC, then there are no more
11200 decl-specifiers. */
11201 if (!found_decl_spec
)
11204 decl_specs
->any_specifiers_p
= true;
11205 /* After we see one decl-specifier, further decl-specifiers are
11206 always optional. */
11207 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
11210 /* Don't allow a friend specifier with a class definition. */
11211 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
11212 && (*declares_class_or_enum
& 2))
11213 error_at (decl_specs
->locations
[ds_friend
],
11214 "class definition may not be declared a friend");
11217 /* Parse an (optional) storage-class-specifier.
11219 storage-class-specifier:
11228 storage-class-specifier:
11231 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11234 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
11236 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
11239 if (cxx_dialect
!= cxx98
)
11241 /* Fall through for C++98. */
11248 /* Consume the token. */
11249 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11256 /* Parse an (optional) function-specifier.
11258 function-specifier:
11263 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11264 Updates DECL_SPECS, if it is non-NULL. */
11267 cp_parser_function_specifier_opt (cp_parser
* parser
,
11268 cp_decl_specifier_seq
*decl_specs
)
11270 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11271 switch (token
->keyword
)
11274 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
11278 /* 14.5.2.3 [temp.mem]
11280 A member function template shall not be virtual. */
11281 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11282 error_at (token
->location
, "templates may not be %<virtual%>");
11283 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
11287 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
11294 /* Consume the token. */
11295 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11298 /* Parse a linkage-specification.
11300 linkage-specification:
11301 extern string-literal { declaration-seq [opt] }
11302 extern string-literal declaration */
11305 cp_parser_linkage_specification (cp_parser
* parser
)
11309 /* Look for the `extern' keyword. */
11310 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
11312 /* Look for the string-literal. */
11313 linkage
= cp_parser_string_literal (parser
, false, false);
11315 /* Transform the literal into an identifier. If the literal is a
11316 wide-character string, or contains embedded NULs, then we can't
11317 handle it as the user wants. */
11318 if (strlen (TREE_STRING_POINTER (linkage
))
11319 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
11321 cp_parser_error (parser
, "invalid linkage-specification");
11322 /* Assume C++ linkage. */
11323 linkage
= lang_name_cplusplus
;
11326 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
11328 /* We're now using the new linkage. */
11329 push_lang_context (linkage
);
11331 /* If the next token is a `{', then we're using the first
11333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11335 /* Consume the `{' token. */
11336 cp_lexer_consume_token (parser
->lexer
);
11337 /* Parse the declarations. */
11338 cp_parser_declaration_seq_opt (parser
);
11339 /* Look for the closing `}'. */
11340 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11342 /* Otherwise, there's just one declaration. */
11345 bool saved_in_unbraced_linkage_specification_p
;
11347 saved_in_unbraced_linkage_specification_p
11348 = parser
->in_unbraced_linkage_specification_p
;
11349 parser
->in_unbraced_linkage_specification_p
= true;
11350 cp_parser_declaration (parser
);
11351 parser
->in_unbraced_linkage_specification_p
11352 = saved_in_unbraced_linkage_specification_p
;
11355 /* We're done with the linkage-specification. */
11356 pop_lang_context ();
11359 /* Parse a static_assert-declaration.
11361 static_assert-declaration:
11362 static_assert ( constant-expression , string-literal ) ;
11364 If MEMBER_P, this static_assert is a class member. */
11367 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
11372 location_t saved_loc
;
11375 /* Peek at the `static_assert' token so we can keep track of exactly
11376 where the static assertion started. */
11377 token
= cp_lexer_peek_token (parser
->lexer
);
11378 saved_loc
= token
->location
;
11380 /* Look for the `static_assert' keyword. */
11381 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
11385 /* We know we are in a static assertion; commit to any tentative
11387 if (cp_parser_parsing_tentatively (parser
))
11388 cp_parser_commit_to_tentative_parse (parser
);
11390 /* Parse the `(' starting the static assertion condition. */
11391 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11393 /* Parse the constant-expression. Allow a non-constant expression
11394 here in order to give better diagnostics in finish_static_assert. */
11396 cp_parser_constant_expression (parser
,
11397 /*allow_non_constant_p=*/true,
11398 /*non_constant_p=*/&dummy
);
11400 /* Parse the separating `,'. */
11401 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11403 /* Parse the string-literal message. */
11404 message
= cp_parser_string_literal (parser
,
11405 /*translate=*/false,
11408 /* A `)' completes the static assertion. */
11409 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11410 cp_parser_skip_to_closing_parenthesis (parser
,
11411 /*recovering=*/true,
11412 /*or_comma=*/false,
11413 /*consume_paren=*/true);
11415 /* A semicolon terminates the declaration. */
11416 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11418 /* Complete the static assertion, which may mean either processing
11419 the static assert now or saving it for template instantiation. */
11420 finish_static_assert (condition
, message
, saved_loc
, member_p
);
11423 /* Parse the expression in decltype ( expression ). */
11426 cp_parser_decltype_expr (cp_parser
*parser
,
11427 bool &id_expression_or_member_access_p
)
11429 cp_token
*id_expr_start_token
;
11432 /* First, try parsing an id-expression. */
11433 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
11434 cp_parser_parse_tentatively (parser
);
11435 expr
= cp_parser_id_expression (parser
,
11436 /*template_keyword_p=*/false,
11437 /*check_dependency_p=*/true,
11438 /*template_p=*/NULL
,
11439 /*declarator_p=*/false,
11440 /*optional_p=*/false);
11442 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
11444 bool non_integral_constant_expression_p
= false;
11445 tree id_expression
= expr
;
11447 const char *error_msg
;
11449 if (identifier_p (expr
))
11450 /* Lookup the name we got back from the id-expression. */
11451 expr
= cp_parser_lookup_name (parser
, expr
,
11453 /*is_template=*/false,
11454 /*is_namespace=*/false,
11455 /*check_dependency=*/true,
11456 /*ambiguous_decls=*/NULL
,
11457 id_expr_start_token
->location
);
11460 && expr
!= error_mark_node
11461 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
11462 && TREE_CODE (expr
) != TYPE_DECL
11463 && (TREE_CODE (expr
) != BIT_NOT_EXPR
11464 || !TYPE_P (TREE_OPERAND (expr
, 0)))
11465 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11467 /* Complete lookup of the id-expression. */
11468 expr
= (finish_id_expression
11469 (id_expression
, expr
, parser
->scope
, &idk
,
11470 /*integral_constant_expression_p=*/false,
11471 /*allow_non_integral_constant_expression_p=*/true,
11472 &non_integral_constant_expression_p
,
11473 /*template_p=*/false,
11475 /*address_p=*/false,
11476 /*template_arg_p=*/false,
11478 id_expr_start_token
->location
));
11480 if (expr
== error_mark_node
)
11481 /* We found an id-expression, but it was something that we
11482 should not have found. This is an error, not something
11483 we can recover from, so note that we found an
11484 id-expression and we'll recover as gracefully as
11486 id_expression_or_member_access_p
= true;
11490 && expr
!= error_mark_node
11491 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11492 /* We have an id-expression. */
11493 id_expression_or_member_access_p
= true;
11496 if (!id_expression_or_member_access_p
)
11498 /* Abort the id-expression parse. */
11499 cp_parser_abort_tentative_parse (parser
);
11501 /* Parsing tentatively, again. */
11502 cp_parser_parse_tentatively (parser
);
11504 /* Parse a class member access. */
11505 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
11506 /*cast_p=*/false, /*decltype*/true,
11507 /*member_access_only_p=*/true, NULL
);
11510 && expr
!= error_mark_node
11511 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11512 /* We have an id-expression. */
11513 id_expression_or_member_access_p
= true;
11516 if (id_expression_or_member_access_p
)
11517 /* We have parsed the complete id-expression or member access. */
11518 cp_parser_parse_definitely (parser
);
11521 /* Abort our attempt to parse an id-expression or member access
11523 cp_parser_abort_tentative_parse (parser
);
11525 /* Parse a full expression. */
11526 expr
= cp_parser_expression (parser
, /*cast_p=*/false,
11527 /*decltype*/true, NULL
);
11533 /* Parse a `decltype' type. Returns the type.
11535 simple-type-specifier:
11536 decltype ( expression )
11538 decltype ( auto ) */
11541 cp_parser_decltype (cp_parser
*parser
)
11544 bool id_expression_or_member_access_p
= false;
11545 const char *saved_message
;
11546 bool saved_integral_constant_expression_p
;
11547 bool saved_non_integral_constant_expression_p
;
11548 bool saved_greater_than_is_operator_p
;
11549 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11551 if (start_token
->type
== CPP_DECLTYPE
)
11553 /* Already parsed. */
11554 cp_lexer_consume_token (parser
->lexer
);
11555 return start_token
->u
.value
;
11558 /* Look for the `decltype' token. */
11559 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
11560 return error_mark_node
;
11562 /* Parse the opening `('. */
11563 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11564 return error_mark_node
;
11566 /* decltype (auto) */
11567 if (cxx_dialect
>= cxx1y
11568 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
11570 cp_lexer_consume_token (parser
->lexer
);
11571 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11572 return error_mark_node
;
11573 expr
= make_decltype_auto ();
11574 AUTO_IS_DECLTYPE (expr
) = true;
11578 /* Types cannot be defined in a `decltype' expression. Save away the
11580 saved_message
= parser
->type_definition_forbidden_message
;
11582 /* And create the new one. */
11583 parser
->type_definition_forbidden_message
11584 = G_("types may not be defined in %<decltype%> expressions");
11586 /* The restrictions on constant-expressions do not apply inside
11587 decltype expressions. */
11588 saved_integral_constant_expression_p
11589 = parser
->integral_constant_expression_p
;
11590 saved_non_integral_constant_expression_p
11591 = parser
->non_integral_constant_expression_p
;
11592 parser
->integral_constant_expression_p
= false;
11594 /* Within a parenthesized expression, a `>' token is always
11595 the greater-than operator. */
11596 saved_greater_than_is_operator_p
11597 = parser
->greater_than_is_operator_p
;
11598 parser
->greater_than_is_operator_p
= true;
11600 /* Do not actually evaluate the expression. */
11601 ++cp_unevaluated_operand
;
11603 /* Do not warn about problems with the expression. */
11604 ++c_inhibit_evaluation_warnings
;
11606 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
11608 /* Go back to evaluating expressions. */
11609 --cp_unevaluated_operand
;
11610 --c_inhibit_evaluation_warnings
;
11612 /* The `>' token might be the end of a template-id or
11613 template-parameter-list now. */
11614 parser
->greater_than_is_operator_p
11615 = saved_greater_than_is_operator_p
;
11617 /* Restore the old message and the integral constant expression
11619 parser
->type_definition_forbidden_message
= saved_message
;
11620 parser
->integral_constant_expression_p
11621 = saved_integral_constant_expression_p
;
11622 parser
->non_integral_constant_expression_p
11623 = saved_non_integral_constant_expression_p
;
11625 /* Parse to the closing `)'. */
11626 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11628 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11629 /*consume_paren=*/true);
11630 return error_mark_node
;
11633 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
11634 tf_warning_or_error
);
11637 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11639 start_token
->type
= CPP_DECLTYPE
;
11640 start_token
->u
.value
= expr
;
11641 start_token
->keyword
= RID_MAX
;
11642 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
11647 /* Special member functions [gram.special] */
11649 /* Parse a conversion-function-id.
11651 conversion-function-id:
11652 operator conversion-type-id
11654 Returns an IDENTIFIER_NODE representing the operator. */
11657 cp_parser_conversion_function_id (cp_parser
* parser
)
11661 tree saved_qualifying_scope
;
11662 tree saved_object_scope
;
11663 tree pushed_scope
= NULL_TREE
;
11665 /* Look for the `operator' token. */
11666 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
11667 return error_mark_node
;
11668 /* When we parse the conversion-type-id, the current scope will be
11669 reset. However, we need that information in able to look up the
11670 conversion function later, so we save it here. */
11671 saved_scope
= parser
->scope
;
11672 saved_qualifying_scope
= parser
->qualifying_scope
;
11673 saved_object_scope
= parser
->object_scope
;
11674 /* We must enter the scope of the class so that the names of
11675 entities declared within the class are available in the
11676 conversion-type-id. For example, consider:
11683 S::operator I() { ... }
11685 In order to see that `I' is a type-name in the definition, we
11686 must be in the scope of `S'. */
11688 pushed_scope
= push_scope (saved_scope
);
11689 /* Parse the conversion-type-id. */
11690 type
= cp_parser_conversion_type_id (parser
);
11691 /* Leave the scope of the class, if any. */
11693 pop_scope (pushed_scope
);
11694 /* Restore the saved scope. */
11695 parser
->scope
= saved_scope
;
11696 parser
->qualifying_scope
= saved_qualifying_scope
;
11697 parser
->object_scope
= saved_object_scope
;
11698 /* If the TYPE is invalid, indicate failure. */
11699 if (type
== error_mark_node
)
11700 return error_mark_node
;
11701 return mangle_conv_op_name_for_type (type
);
11704 /* Parse a conversion-type-id:
11706 conversion-type-id:
11707 type-specifier-seq conversion-declarator [opt]
11709 Returns the TYPE specified. */
11712 cp_parser_conversion_type_id (cp_parser
* parser
)
11715 cp_decl_specifier_seq type_specifiers
;
11716 cp_declarator
*declarator
;
11717 tree type_specified
;
11719 /* Parse the attributes. */
11720 attributes
= cp_parser_attributes_opt (parser
);
11721 /* Parse the type-specifiers. */
11722 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
11723 /*is_trailing_return=*/false,
11725 /* If that didn't work, stop. */
11726 if (type_specifiers
.type
== error_mark_node
)
11727 return error_mark_node
;
11728 /* Parse the conversion-declarator. */
11729 declarator
= cp_parser_conversion_declarator_opt (parser
);
11731 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
11732 /*initialized=*/0, &attributes
);
11734 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
11736 /* Don't give this error when parsing tentatively. This happens to
11737 work because we always parse this definitively once. */
11738 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
11739 && type_uses_auto (type_specified
))
11741 if (cxx_dialect
< cxx1y
)
11743 error ("invalid use of %<auto%> in conversion operator");
11744 return error_mark_node
;
11746 else if (template_parm_scope_p ())
11747 warning (0, "use of %<auto%> in member template "
11748 "conversion operator can never be deduced");
11751 return type_specified
;
11754 /* Parse an (optional) conversion-declarator.
11756 conversion-declarator:
11757 ptr-operator conversion-declarator [opt]
11761 static cp_declarator
*
11762 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
11764 enum tree_code code
;
11765 tree class_type
, std_attributes
= NULL_TREE
;
11766 cp_cv_quals cv_quals
;
11768 /* We don't know if there's a ptr-operator next, or not. */
11769 cp_parser_parse_tentatively (parser
);
11770 /* Try the ptr-operator. */
11771 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
11773 /* If it worked, look for more conversion-declarators. */
11774 if (cp_parser_parse_definitely (parser
))
11776 cp_declarator
*declarator
;
11778 /* Parse another optional declarator. */
11779 declarator
= cp_parser_conversion_declarator_opt (parser
);
11781 declarator
= cp_parser_make_indirect_declarator
11782 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
11790 /* Parse an (optional) ctor-initializer.
11793 : mem-initializer-list
11795 Returns TRUE iff the ctor-initializer was actually present. */
11798 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
11800 /* If the next token is not a `:', then there is no
11801 ctor-initializer. */
11802 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
11804 /* Do default initialization of any bases and members. */
11805 if (DECL_CONSTRUCTOR_P (current_function_decl
))
11806 finish_mem_initializers (NULL_TREE
);
11811 /* Consume the `:' token. */
11812 cp_lexer_consume_token (parser
->lexer
);
11813 /* And the mem-initializer-list. */
11814 cp_parser_mem_initializer_list (parser
);
11819 /* Parse a mem-initializer-list.
11821 mem-initializer-list:
11822 mem-initializer ... [opt]
11823 mem-initializer ... [opt] , mem-initializer-list */
11826 cp_parser_mem_initializer_list (cp_parser
* parser
)
11828 tree mem_initializer_list
= NULL_TREE
;
11829 tree target_ctor
= error_mark_node
;
11830 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11832 /* Let the semantic analysis code know that we are starting the
11833 mem-initializer-list. */
11834 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
11835 error_at (token
->location
,
11836 "only constructors take member initializers");
11838 /* Loop through the list. */
11841 tree mem_initializer
;
11843 token
= cp_lexer_peek_token (parser
->lexer
);
11844 /* Parse the mem-initializer. */
11845 mem_initializer
= cp_parser_mem_initializer (parser
);
11846 /* If the next token is a `...', we're expanding member initializers. */
11847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11849 /* Consume the `...'. */
11850 cp_lexer_consume_token (parser
->lexer
);
11852 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11853 can be expanded but members cannot. */
11854 if (mem_initializer
!= error_mark_node
11855 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
11857 error_at (token
->location
,
11858 "cannot expand initializer for member %<%D%>",
11859 TREE_PURPOSE (mem_initializer
));
11860 mem_initializer
= error_mark_node
;
11863 /* Construct the pack expansion type. */
11864 if (mem_initializer
!= error_mark_node
)
11865 mem_initializer
= make_pack_expansion (mem_initializer
);
11867 if (target_ctor
!= error_mark_node
11868 && mem_initializer
!= error_mark_node
)
11870 error ("mem-initializer for %qD follows constructor delegation",
11871 TREE_PURPOSE (mem_initializer
));
11872 mem_initializer
= error_mark_node
;
11874 /* Look for a target constructor. */
11875 if (mem_initializer
!= error_mark_node
11876 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
11877 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
11879 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
11880 if (mem_initializer_list
)
11882 error ("constructor delegation follows mem-initializer for %qD",
11883 TREE_PURPOSE (mem_initializer_list
));
11884 mem_initializer
= error_mark_node
;
11886 target_ctor
= mem_initializer
;
11888 /* Add it to the list, unless it was erroneous. */
11889 if (mem_initializer
!= error_mark_node
)
11891 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
11892 mem_initializer_list
= mem_initializer
;
11894 /* If the next token is not a `,', we're done. */
11895 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11897 /* Consume the `,' token. */
11898 cp_lexer_consume_token (parser
->lexer
);
11901 /* Perform semantic analysis. */
11902 if (DECL_CONSTRUCTOR_P (current_function_decl
))
11903 finish_mem_initializers (mem_initializer_list
);
11906 /* Parse a mem-initializer.
11909 mem-initializer-id ( expression-list [opt] )
11910 mem-initializer-id braced-init-list
11915 ( expression-list [opt] )
11917 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11918 class) or FIELD_DECL (for a non-static data member) to initialize;
11919 the TREE_VALUE is the expression-list. An empty initialization
11920 list is represented by void_list_node. */
11923 cp_parser_mem_initializer (cp_parser
* parser
)
11925 tree mem_initializer_id
;
11926 tree expression_list
;
11928 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11930 /* Find out what is being initialized. */
11931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
11933 permerror (token
->location
,
11934 "anachronistic old-style base class initializer");
11935 mem_initializer_id
= NULL_TREE
;
11939 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
11940 if (mem_initializer_id
== error_mark_node
)
11941 return mem_initializer_id
;
11943 member
= expand_member_init (mem_initializer_id
);
11944 if (member
&& !DECL_P (member
))
11945 in_base_initializer
= 1;
11947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11949 bool expr_non_constant_p
;
11950 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11951 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11952 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
11953 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
11957 vec
<tree
, va_gc
> *vec
;
11958 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
11960 /*allow_expansion_p=*/true,
11961 /*non_constant_p=*/NULL
);
11963 return error_mark_node
;
11964 expression_list
= build_tree_list_vec (vec
);
11965 release_tree_vector (vec
);
11968 if (expression_list
== error_mark_node
)
11969 return error_mark_node
;
11970 if (!expression_list
)
11971 expression_list
= void_type_node
;
11973 in_base_initializer
= 0;
11975 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
11978 /* Parse a mem-initializer-id.
11980 mem-initializer-id:
11981 :: [opt] nested-name-specifier [opt] class-name
11984 Returns a TYPE indicating the class to be initializer for the first
11985 production. Returns an IDENTIFIER_NODE indicating the data member
11986 to be initialized for the second production. */
11989 cp_parser_mem_initializer_id (cp_parser
* parser
)
11991 bool global_scope_p
;
11992 bool nested_name_specifier_p
;
11993 bool template_p
= false;
11996 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11998 /* `typename' is not allowed in this context ([temp.res]). */
11999 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12001 error_at (token
->location
,
12002 "keyword %<typename%> not allowed in this context (a qualified "
12003 "member initializer is implicitly a type)");
12004 cp_lexer_consume_token (parser
->lexer
);
12006 /* Look for the optional `::' operator. */
12008 = (cp_parser_global_scope_opt (parser
,
12009 /*current_scope_valid_p=*/false)
12011 /* Look for the optional nested-name-specifier. The simplest way to
12016 The keyword `typename' is not permitted in a base-specifier or
12017 mem-initializer; in these contexts a qualified name that
12018 depends on a template-parameter is implicitly assumed to be a
12021 is to assume that we have seen the `typename' keyword at this
12023 nested_name_specifier_p
12024 = (cp_parser_nested_name_specifier_opt (parser
,
12025 /*typename_keyword_p=*/true,
12026 /*check_dependency_p=*/true,
12028 /*is_declaration=*/true)
12030 if (nested_name_specifier_p
)
12031 template_p
= cp_parser_optional_template_keyword (parser
);
12032 /* If there is a `::' operator or a nested-name-specifier, then we
12033 are definitely looking for a class-name. */
12034 if (global_scope_p
|| nested_name_specifier_p
)
12035 return cp_parser_class_name (parser
,
12036 /*typename_keyword_p=*/true,
12037 /*template_keyword_p=*/template_p
,
12039 /*check_dependency_p=*/true,
12040 /*class_head_p=*/false,
12041 /*is_declaration=*/true);
12042 /* Otherwise, we could also be looking for an ordinary identifier. */
12043 cp_parser_parse_tentatively (parser
);
12044 /* Try a class-name. */
12045 id
= cp_parser_class_name (parser
,
12046 /*typename_keyword_p=*/true,
12047 /*template_keyword_p=*/false,
12049 /*check_dependency_p=*/true,
12050 /*class_head_p=*/false,
12051 /*is_declaration=*/true);
12052 /* If we found one, we're done. */
12053 if (cp_parser_parse_definitely (parser
))
12055 /* Otherwise, look for an ordinary identifier. */
12056 return cp_parser_identifier (parser
);
12059 /* Overloading [gram.over] */
12061 /* Parse an operator-function-id.
12063 operator-function-id:
12066 Returns an IDENTIFIER_NODE for the operator which is a
12067 human-readable spelling of the identifier, e.g., `operator +'. */
12070 cp_parser_operator_function_id (cp_parser
* parser
)
12072 /* Look for the `operator' keyword. */
12073 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12074 return error_mark_node
;
12075 /* And then the name of the operator itself. */
12076 return cp_parser_operator (parser
);
12079 /* Return an identifier node for a user-defined literal operator.
12080 The suffix identifier is chained to the operator name identifier. */
12083 cp_literal_operator_id (const char* name
)
12086 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12087 + strlen (name
) + 10);
12088 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12089 identifier
= get_identifier (buffer
);
12090 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
12095 /* Parse an operator.
12098 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12099 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12100 || ++ -- , ->* -> () []
12107 Returns an IDENTIFIER_NODE for the operator which is a
12108 human-readable spelling of the identifier, e.g., `operator +'. */
12111 cp_parser_operator (cp_parser
* parser
)
12113 tree id
= NULL_TREE
;
12116 /* Peek at the next token. */
12117 token
= cp_lexer_peek_token (parser
->lexer
);
12118 /* Figure out which operator we have. */
12119 switch (token
->type
)
12125 /* The keyword should be either `new' or `delete'. */
12126 if (token
->keyword
== RID_NEW
)
12128 else if (token
->keyword
== RID_DELETE
)
12133 /* Consume the `new' or `delete' token. */
12134 cp_lexer_consume_token (parser
->lexer
);
12136 /* Peek at the next token. */
12137 token
= cp_lexer_peek_token (parser
->lexer
);
12138 /* If it's a `[' token then this is the array variant of the
12140 if (token
->type
== CPP_OPEN_SQUARE
)
12142 /* Consume the `[' token. */
12143 cp_lexer_consume_token (parser
->lexer
);
12144 /* Look for the `]' token. */
12145 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12146 id
= ansi_opname (op
== NEW_EXPR
12147 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12149 /* Otherwise, we have the non-array variant. */
12151 id
= ansi_opname (op
);
12157 id
= ansi_opname (PLUS_EXPR
);
12161 id
= ansi_opname (MINUS_EXPR
);
12165 id
= ansi_opname (MULT_EXPR
);
12169 id
= ansi_opname (TRUNC_DIV_EXPR
);
12173 id
= ansi_opname (TRUNC_MOD_EXPR
);
12177 id
= ansi_opname (BIT_XOR_EXPR
);
12181 id
= ansi_opname (BIT_AND_EXPR
);
12185 id
= ansi_opname (BIT_IOR_EXPR
);
12189 id
= ansi_opname (BIT_NOT_EXPR
);
12193 id
= ansi_opname (TRUTH_NOT_EXPR
);
12197 id
= ansi_assopname (NOP_EXPR
);
12201 id
= ansi_opname (LT_EXPR
);
12205 id
= ansi_opname (GT_EXPR
);
12209 id
= ansi_assopname (PLUS_EXPR
);
12213 id
= ansi_assopname (MINUS_EXPR
);
12217 id
= ansi_assopname (MULT_EXPR
);
12221 id
= ansi_assopname (TRUNC_DIV_EXPR
);
12225 id
= ansi_assopname (TRUNC_MOD_EXPR
);
12229 id
= ansi_assopname (BIT_XOR_EXPR
);
12233 id
= ansi_assopname (BIT_AND_EXPR
);
12237 id
= ansi_assopname (BIT_IOR_EXPR
);
12241 id
= ansi_opname (LSHIFT_EXPR
);
12245 id
= ansi_opname (RSHIFT_EXPR
);
12248 case CPP_LSHIFT_EQ
:
12249 id
= ansi_assopname (LSHIFT_EXPR
);
12252 case CPP_RSHIFT_EQ
:
12253 id
= ansi_assopname (RSHIFT_EXPR
);
12257 id
= ansi_opname (EQ_EXPR
);
12261 id
= ansi_opname (NE_EXPR
);
12265 id
= ansi_opname (LE_EXPR
);
12268 case CPP_GREATER_EQ
:
12269 id
= ansi_opname (GE_EXPR
);
12273 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
12277 id
= ansi_opname (TRUTH_ORIF_EXPR
);
12280 case CPP_PLUS_PLUS
:
12281 id
= ansi_opname (POSTINCREMENT_EXPR
);
12284 case CPP_MINUS_MINUS
:
12285 id
= ansi_opname (PREDECREMENT_EXPR
);
12289 id
= ansi_opname (COMPOUND_EXPR
);
12292 case CPP_DEREF_STAR
:
12293 id
= ansi_opname (MEMBER_REF
);
12297 id
= ansi_opname (COMPONENT_REF
);
12300 case CPP_OPEN_PAREN
:
12301 /* Consume the `('. */
12302 cp_lexer_consume_token (parser
->lexer
);
12303 /* Look for the matching `)'. */
12304 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
12305 return ansi_opname (CALL_EXPR
);
12307 case CPP_OPEN_SQUARE
:
12308 /* Consume the `['. */
12309 cp_lexer_consume_token (parser
->lexer
);
12310 /* Look for the matching `]'. */
12311 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12312 return ansi_opname (ARRAY_REF
);
12315 if (cxx_dialect
== cxx98
)
12316 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12317 if (TREE_STRING_LENGTH (token
->u
.value
) > 2)
12319 error ("expected empty string after %<operator%> keyword");
12320 return error_mark_node
;
12322 /* Consume the string. */
12323 cp_lexer_consume_token (parser
->lexer
);
12324 /* Look for the suffix identifier. */
12325 token
= cp_lexer_peek_token (parser
->lexer
);
12326 if (token
->type
== CPP_NAME
)
12328 id
= cp_parser_identifier (parser
);
12329 if (id
!= error_mark_node
)
12331 const char *name
= IDENTIFIER_POINTER (id
);
12332 return cp_literal_operator_id (name
);
12337 error ("expected suffix identifier");
12338 return error_mark_node
;
12341 case CPP_STRING_USERDEF
:
12342 error ("missing space between %<\"\"%> and suffix identifier");
12343 return error_mark_node
;
12346 /* Anything else is an error. */
12350 /* If we have selected an identifier, we need to consume the
12353 cp_lexer_consume_token (parser
->lexer
);
12354 /* Otherwise, no valid operator name was present. */
12357 cp_parser_error (parser
, "expected operator");
12358 id
= error_mark_node
;
12364 /* Parse a template-declaration.
12366 template-declaration:
12367 export [opt] template < template-parameter-list > declaration
12369 If MEMBER_P is TRUE, this template-declaration occurs within a
12372 The grammar rule given by the standard isn't correct. What
12373 is really meant is:
12375 template-declaration:
12376 export [opt] template-parameter-list-seq
12377 decl-specifier-seq [opt] init-declarator [opt] ;
12378 export [opt] template-parameter-list-seq
12379 function-definition
12381 template-parameter-list-seq:
12382 template-parameter-list-seq [opt]
12383 template < template-parameter-list > */
12386 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
12388 /* Check for `export'. */
12389 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
12391 /* Consume the `export' token. */
12392 cp_lexer_consume_token (parser
->lexer
);
12393 /* Warn that we do not support `export'. */
12394 warning (0, "keyword %<export%> not implemented, and will be ignored");
12397 cp_parser_template_declaration_after_export (parser
, member_p
);
12400 /* Parse a template-parameter-list.
12402 template-parameter-list:
12404 template-parameter-list , template-parameter
12406 Returns a TREE_LIST. Each node represents a template parameter.
12407 The nodes are connected via their TREE_CHAINs. */
12410 cp_parser_template_parameter_list (cp_parser
* parser
)
12412 tree parameter_list
= NULL_TREE
;
12414 begin_template_parm_list ();
12416 /* The loop below parses the template parms. We first need to know
12417 the total number of template parms to be able to compute proper
12418 canonical types of each dependent type. So after the loop, when
12419 we know the total number of template parms,
12420 end_template_parm_list computes the proper canonical types and
12421 fixes up the dependent types accordingly. */
12426 bool is_parameter_pack
;
12427 location_t parm_loc
;
12429 /* Parse the template-parameter. */
12430 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12431 parameter
= cp_parser_template_parameter (parser
,
12433 &is_parameter_pack
);
12434 /* Add it to the list. */
12435 if (parameter
!= error_mark_node
)
12436 parameter_list
= process_template_parm (parameter_list
,
12440 is_parameter_pack
);
12443 tree err_parm
= build_tree_list (parameter
, parameter
);
12444 parameter_list
= chainon (parameter_list
, err_parm
);
12447 /* If the next token is not a `,', we're done. */
12448 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12450 /* Otherwise, consume the `,' token. */
12451 cp_lexer_consume_token (parser
->lexer
);
12454 return end_template_parm_list (parameter_list
);
12457 /* Parse a template-parameter.
12459 template-parameter:
12461 parameter-declaration
12463 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12464 the parameter. The TREE_PURPOSE is the default value, if any.
12465 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12466 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12467 set to true iff this parameter is a parameter pack. */
12470 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
12471 bool *is_parameter_pack
)
12474 cp_parameter_declarator
*parameter_declarator
;
12475 cp_declarator
*id_declarator
;
12478 /* Assume it is a type parameter or a template parameter. */
12479 *is_non_type
= false;
12480 /* Assume it not a parameter pack. */
12481 *is_parameter_pack
= false;
12482 /* Peek at the next token. */
12483 token
= cp_lexer_peek_token (parser
->lexer
);
12484 /* If it is `class' or `template', we have a type-parameter. */
12485 if (token
->keyword
== RID_TEMPLATE
)
12486 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12487 /* If it is `class' or `typename' we do not know yet whether it is a
12488 type parameter or a non-type parameter. Consider:
12490 template <typename T, typename T::X X> ...
12494 template <class C, class D*> ...
12496 Here, the first parameter is a type parameter, and the second is
12497 a non-type parameter. We can tell by looking at the token after
12498 the identifier -- if it is a `,', `=', or `>' then we have a type
12500 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
12502 /* Peek at the token after `class' or `typename'. */
12503 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12504 /* If it's an ellipsis, we have a template type parameter
12506 if (token
->type
== CPP_ELLIPSIS
)
12507 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12508 /* If it's an identifier, skip it. */
12509 if (token
->type
== CPP_NAME
)
12510 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
12511 /* Now, see if the token looks like the end of a template
12513 if (token
->type
== CPP_COMMA
12514 || token
->type
== CPP_EQ
12515 || token
->type
== CPP_GREATER
)
12516 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12519 /* Otherwise, it is a non-type parameter.
12523 When parsing a default template-argument for a non-type
12524 template-parameter, the first non-nested `>' is taken as the end
12525 of the template parameter-list rather than a greater-than
12527 *is_non_type
= true;
12528 parameter_declarator
12529 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
12530 /*parenthesized_p=*/NULL
);
12532 /* If the parameter declaration is marked as a parameter pack, set
12533 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12534 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12536 if (parameter_declarator
12537 && parameter_declarator
->declarator
12538 && parameter_declarator
->declarator
->parameter_pack_p
)
12540 *is_parameter_pack
= true;
12541 parameter_declarator
->declarator
->parameter_pack_p
= false;
12544 if (parameter_declarator
12545 && parameter_declarator
->default_argument
)
12547 /* Can happen in some cases of erroneous input (c++/34892). */
12548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12549 /* Consume the `...' for better error recovery. */
12550 cp_lexer_consume_token (parser
->lexer
);
12552 /* If the next token is an ellipsis, and we don't already have it
12553 marked as a parameter pack, then we have a parameter pack (that
12554 has no declarator). */
12555 else if (!*is_parameter_pack
12556 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12557 && (declarator_can_be_parameter_pack
12558 (parameter_declarator
->declarator
)))
12560 /* Consume the `...'. */
12561 cp_lexer_consume_token (parser
->lexer
);
12562 maybe_warn_variadic_templates ();
12564 *is_parameter_pack
= true;
12566 /* We might end up with a pack expansion as the type of the non-type
12567 template parameter, in which case this is a non-type template
12569 else if (parameter_declarator
12570 && parameter_declarator
->decl_specifiers
.type
12571 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
12573 *is_parameter_pack
= true;
12574 parameter_declarator
->decl_specifiers
.type
=
12575 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
12578 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12580 /* Parameter packs cannot have default arguments. However, a
12581 user may try to do so, so we'll parse them and give an
12582 appropriate diagnostic here. */
12584 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12586 /* Find the name of the parameter pack. */
12587 id_declarator
= parameter_declarator
->declarator
;
12588 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
12589 id_declarator
= id_declarator
->declarator
;
12591 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
12592 error_at (start_token
->location
,
12593 "template parameter pack %qD cannot have a default argument",
12594 id_declarator
->u
.id
.unqualified_name
);
12596 error_at (start_token
->location
,
12597 "template parameter pack cannot have a default argument");
12599 /* Parse the default argument, but throw away the result. */
12600 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
12603 parm
= grokdeclarator (parameter_declarator
->declarator
,
12604 ¶meter_declarator
->decl_specifiers
,
12605 TPARM
, /*initialized=*/0,
12606 /*attrlist=*/NULL
);
12607 if (parm
== error_mark_node
)
12608 return error_mark_node
;
12610 return build_tree_list (parameter_declarator
->default_argument
, parm
);
12613 /* Parse a type-parameter.
12616 class identifier [opt]
12617 class identifier [opt] = type-id
12618 typename identifier [opt]
12619 typename identifier [opt] = type-id
12620 template < template-parameter-list > class identifier [opt]
12621 template < template-parameter-list > class identifier [opt]
12624 GNU Extension (variadic templates):
12627 class ... identifier [opt]
12628 typename ... identifier [opt]
12630 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12631 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12632 the declaration of the parameter.
12634 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12637 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
12642 /* Look for a keyword to tell us what kind of parameter this is. */
12643 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
12645 return error_mark_node
;
12647 switch (token
->keyword
)
12653 tree default_argument
;
12655 /* If the next token is an ellipsis, we have a template
12657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12659 /* Consume the `...' token. */
12660 cp_lexer_consume_token (parser
->lexer
);
12661 maybe_warn_variadic_templates ();
12663 *is_parameter_pack
= true;
12666 /* If the next token is an identifier, then it names the
12668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12669 identifier
= cp_parser_identifier (parser
);
12671 identifier
= NULL_TREE
;
12673 /* Create the parameter. */
12674 parameter
= finish_template_type_parm (class_type_node
, identifier
);
12676 /* If the next token is an `=', we have a default argument. */
12677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12679 /* Consume the `=' token. */
12680 cp_lexer_consume_token (parser
->lexer
);
12681 /* Parse the default-argument. */
12682 push_deferring_access_checks (dk_no_deferred
);
12683 default_argument
= cp_parser_type_id (parser
);
12685 /* Template parameter packs cannot have default
12687 if (*is_parameter_pack
)
12690 error_at (token
->location
,
12691 "template parameter pack %qD cannot have a "
12692 "default argument", identifier
);
12694 error_at (token
->location
,
12695 "template parameter packs cannot have "
12696 "default arguments");
12697 default_argument
= NULL_TREE
;
12699 pop_deferring_access_checks ();
12702 default_argument
= NULL_TREE
;
12704 /* Create the combined representation of the parameter and the
12705 default argument. */
12706 parameter
= build_tree_list (default_argument
, parameter
);
12713 tree default_argument
;
12715 /* Look for the `<'. */
12716 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
12717 /* Parse the template-parameter-list. */
12718 cp_parser_template_parameter_list (parser
);
12719 /* Look for the `>'. */
12720 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
12721 /* Look for the `class' keyword. */
12722 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
12723 /* If the next token is an ellipsis, we have a template
12725 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12727 /* Consume the `...' token. */
12728 cp_lexer_consume_token (parser
->lexer
);
12729 maybe_warn_variadic_templates ();
12731 *is_parameter_pack
= true;
12733 /* If the next token is an `=', then there is a
12734 default-argument. If the next token is a `>', we are at
12735 the end of the parameter-list. If the next token is a `,',
12736 then we are at the end of this parameter. */
12737 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12738 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
12739 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12741 identifier
= cp_parser_identifier (parser
);
12742 /* Treat invalid names as if the parameter were nameless. */
12743 if (identifier
== error_mark_node
)
12744 identifier
= NULL_TREE
;
12747 identifier
= NULL_TREE
;
12749 /* Create the template parameter. */
12750 parameter
= finish_template_template_parm (class_type_node
,
12753 /* If the next token is an `=', then there is a
12754 default-argument. */
12755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12759 /* Consume the `='. */
12760 cp_lexer_consume_token (parser
->lexer
);
12761 /* Parse the id-expression. */
12762 push_deferring_access_checks (dk_no_deferred
);
12763 /* save token before parsing the id-expression, for error
12765 token
= cp_lexer_peek_token (parser
->lexer
);
12767 = cp_parser_id_expression (parser
,
12768 /*template_keyword_p=*/false,
12769 /*check_dependency_p=*/true,
12770 /*template_p=*/&is_template
,
12771 /*declarator_p=*/false,
12772 /*optional_p=*/false);
12773 if (TREE_CODE (default_argument
) == TYPE_DECL
)
12774 /* If the id-expression was a template-id that refers to
12775 a template-class, we already have the declaration here,
12776 so no further lookup is needed. */
12779 /* Look up the name. */
12781 = cp_parser_lookup_name (parser
, default_argument
,
12783 /*is_template=*/is_template
,
12784 /*is_namespace=*/false,
12785 /*check_dependency=*/true,
12786 /*ambiguous_decls=*/NULL
,
12788 /* See if the default argument is valid. */
12790 = check_template_template_default_arg (default_argument
);
12792 /* Template parameter packs cannot have default
12794 if (*is_parameter_pack
)
12797 error_at (token
->location
,
12798 "template parameter pack %qD cannot "
12799 "have a default argument",
12802 error_at (token
->location
, "template parameter packs cannot "
12803 "have default arguments");
12804 default_argument
= NULL_TREE
;
12806 pop_deferring_access_checks ();
12809 default_argument
= NULL_TREE
;
12811 /* Create the combined representation of the parameter and the
12812 default argument. */
12813 parameter
= build_tree_list (default_argument
, parameter
);
12818 gcc_unreachable ();
12825 /* Parse a template-id.
12828 template-name < template-argument-list [opt] >
12830 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12831 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12832 returned. Otherwise, if the template-name names a function, or set
12833 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12834 names a class, returns a TYPE_DECL for the specialization.
12836 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12837 uninstantiated templates. */
12840 cp_parser_template_id (cp_parser
*parser
,
12841 bool template_keyword_p
,
12842 bool check_dependency_p
,
12843 enum tag_types tag_type
,
12844 bool is_declaration
)
12850 cp_token_position start_of_id
= 0;
12851 deferred_access_check
*chk
;
12852 vec
<deferred_access_check
, va_gc
> *access_check
;
12853 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
12854 bool is_identifier
;
12856 /* If the next token corresponds to a template-id, there is no need
12858 next_token
= cp_lexer_peek_token (parser
->lexer
);
12859 if (next_token
->type
== CPP_TEMPLATE_ID
)
12861 struct tree_check
*check_value
;
12863 /* Get the stored value. */
12864 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
12865 /* Perform any access checks that were deferred. */
12866 access_check
= check_value
->checks
;
12869 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
12870 perform_or_defer_access_check (chk
->binfo
,
12873 tf_warning_or_error
);
12875 /* Return the stored value. */
12876 return check_value
->value
;
12879 /* Avoid performing name lookup if there is no possibility of
12880 finding a template-id. */
12881 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
12882 || (next_token
->type
== CPP_NAME
12883 && !cp_parser_nth_token_starts_template_argument_list_p
12886 cp_parser_error (parser
, "expected template-id");
12887 return error_mark_node
;
12890 /* Remember where the template-id starts. */
12891 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
12892 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
12894 push_deferring_access_checks (dk_deferred
);
12896 /* Parse the template-name. */
12897 is_identifier
= false;
12898 templ
= cp_parser_template_name (parser
, template_keyword_p
,
12899 check_dependency_p
,
12903 if (templ
== error_mark_node
|| is_identifier
)
12905 pop_deferring_access_checks ();
12909 /* If we find the sequence `[:' after a template-name, it's probably
12910 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12911 parse correctly the argument list. */
12912 next_token
= cp_lexer_peek_token (parser
->lexer
);
12913 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12914 if (next_token
->type
== CPP_OPEN_SQUARE
12915 && next_token
->flags
& DIGRAPH
12916 && next_token_2
->type
== CPP_COLON
12917 && !(next_token_2
->flags
& PREV_WHITE
))
12919 cp_parser_parse_tentatively (parser
);
12920 /* Change `:' into `::'. */
12921 next_token_2
->type
= CPP_SCOPE
;
12922 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12924 cp_lexer_consume_token (parser
->lexer
);
12926 /* Parse the arguments. */
12927 arguments
= cp_parser_enclosed_template_argument_list (parser
);
12928 if (!cp_parser_parse_definitely (parser
))
12930 /* If we couldn't parse an argument list, then we revert our changes
12931 and return simply an error. Maybe this is not a template-id
12933 next_token_2
->type
= CPP_COLON
;
12934 cp_parser_error (parser
, "expected %<<%>");
12935 pop_deferring_access_checks ();
12936 return error_mark_node
;
12938 /* Otherwise, emit an error about the invalid digraph, but continue
12939 parsing because we got our argument list. */
12940 if (permerror (next_token
->location
,
12941 "%<<::%> cannot begin a template-argument list"))
12943 static bool hint
= false;
12944 inform (next_token
->location
,
12945 "%<<:%> is an alternate spelling for %<[%>."
12946 " Insert whitespace between %<<%> and %<::%>");
12947 if (!hint
&& !flag_permissive
)
12949 inform (next_token
->location
, "(if you use %<-fpermissive%> "
12950 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12951 "accept your code)");
12958 /* Look for the `<' that starts the template-argument-list. */
12959 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
12961 pop_deferring_access_checks ();
12962 return error_mark_node
;
12964 /* Parse the arguments. */
12965 arguments
= cp_parser_enclosed_template_argument_list (parser
);
12968 /* Build a representation of the specialization. */
12969 if (identifier_p (templ
))
12970 template_id
= build_min_nt_loc (next_token
->location
,
12973 else if (DECL_TYPE_TEMPLATE_P (templ
)
12974 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
12976 bool entering_scope
;
12977 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12978 template (rather than some instantiation thereof) only if
12979 is not nested within some other construct. For example, in
12980 "template <typename T> void f(T) { A<T>::", A<T> is just an
12981 instantiation of A. */
12982 entering_scope
= (template_parm_scope_p ()
12983 && cp_lexer_next_token_is (parser
->lexer
,
12986 = finish_template_type (templ
, arguments
, entering_scope
);
12990 /* If it's not a class-template or a template-template, it should be
12991 a function-template. */
12992 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
12993 || TREE_CODE (templ
) == OVERLOAD
12994 || BASELINK_P (templ
)));
12996 template_id
= lookup_template_function (templ
, arguments
);
12999 /* If parsing tentatively, replace the sequence of tokens that makes
13000 up the template-id with a CPP_TEMPLATE_ID token. That way,
13001 should we re-parse the token stream, we will not have to repeat
13002 the effort required to do the parse, nor will we issue duplicate
13003 error messages about problems during instantiation of the
13007 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13009 /* Reset the contents of the START_OF_ID token. */
13010 token
->type
= CPP_TEMPLATE_ID
;
13011 /* Retrieve any deferred checks. Do not pop this access checks yet
13012 so the memory will not be reclaimed during token replacing below. */
13013 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
13014 token
->u
.tree_check_value
->value
= template_id
;
13015 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13016 token
->keyword
= RID_MAX
;
13018 /* Purge all subsequent tokens. */
13019 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13021 /* ??? Can we actually assume that, if template_id ==
13022 error_mark_node, we will have issued a diagnostic to the
13023 user, as opposed to simply marking the tentative parse as
13025 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13026 error_at (token
->location
, "parse error in template argument list");
13029 pop_to_parent_deferring_access_checks ();
13030 return template_id
;
13033 /* Parse a template-name.
13038 The standard should actually say:
13042 operator-function-id
13044 A defect report has been filed about this issue.
13046 A conversion-function-id cannot be a template name because they cannot
13047 be part of a template-id. In fact, looking at this code:
13049 a.operator K<int>()
13051 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13052 It is impossible to call a templated conversion-function-id with an
13053 explicit argument list, since the only allowed template parameter is
13054 the type to which it is converting.
13056 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13057 `template' keyword, in a construction like:
13061 In that case `f' is taken to be a template-name, even though there
13062 is no way of knowing for sure.
13064 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13065 name refers to a set of overloaded functions, at least one of which
13066 is a template, or an IDENTIFIER_NODE with the name of the template,
13067 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13068 names are looked up inside uninstantiated templates. */
13071 cp_parser_template_name (cp_parser
* parser
,
13072 bool template_keyword_p
,
13073 bool check_dependency_p
,
13074 bool is_declaration
,
13075 enum tag_types tag_type
,
13076 bool *is_identifier
)
13081 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13083 /* If the next token is `operator', then we have either an
13084 operator-function-id or a conversion-function-id. */
13085 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13087 /* We don't know whether we're looking at an
13088 operator-function-id or a conversion-function-id. */
13089 cp_parser_parse_tentatively (parser
);
13090 /* Try an operator-function-id. */
13091 identifier
= cp_parser_operator_function_id (parser
);
13092 /* If that didn't work, try a conversion-function-id. */
13093 if (!cp_parser_parse_definitely (parser
))
13095 cp_parser_error (parser
, "expected template-name");
13096 return error_mark_node
;
13099 /* Look for the identifier. */
13101 identifier
= cp_parser_identifier (parser
);
13103 /* If we didn't find an identifier, we don't have a template-id. */
13104 if (identifier
== error_mark_node
)
13105 return error_mark_node
;
13107 /* If the name immediately followed the `template' keyword, then it
13108 is a template-name. However, if the next token is not `<', then
13109 we do not treat it as a template-name, since it is not being used
13110 as part of a template-id. This enables us to handle constructs
13113 template <typename T> struct S { S(); };
13114 template <typename T> S<T>::S();
13116 correctly. We would treat `S' as a template -- if it were `S<T>'
13117 -- but we do not if there is no `<'. */
13119 if (processing_template_decl
13120 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13122 /* In a declaration, in a dependent context, we pretend that the
13123 "template" keyword was present in order to improve error
13124 recovery. For example, given:
13126 template <typename T> void f(T::X<int>);
13128 we want to treat "X<int>" as a template-id. */
13130 && !template_keyword_p
13131 && parser
->scope
&& TYPE_P (parser
->scope
)
13132 && check_dependency_p
13133 && dependent_scope_p (parser
->scope
)
13134 /* Do not do this for dtors (or ctors), since they never
13135 need the template keyword before their name. */
13136 && !constructor_name_p (identifier
, parser
->scope
))
13138 cp_token_position start
= 0;
13140 /* Explain what went wrong. */
13141 error_at (token
->location
, "non-template %qD used as template",
13143 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
13144 parser
->scope
, identifier
);
13145 /* If parsing tentatively, find the location of the "<" token. */
13146 if (cp_parser_simulate_error (parser
))
13147 start
= cp_lexer_token_position (parser
->lexer
, true);
13148 /* Parse the template arguments so that we can issue error
13149 messages about them. */
13150 cp_lexer_consume_token (parser
->lexer
);
13151 cp_parser_enclosed_template_argument_list (parser
);
13152 /* Skip tokens until we find a good place from which to
13153 continue parsing. */
13154 cp_parser_skip_to_closing_parenthesis (parser
,
13155 /*recovering=*/true,
13157 /*consume_paren=*/false);
13158 /* If parsing tentatively, permanently remove the
13159 template argument list. That will prevent duplicate
13160 error messages from being issued about the missing
13161 "template" keyword. */
13163 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
13165 *is_identifier
= true;
13169 /* If the "template" keyword is present, then there is generally
13170 no point in doing name-lookup, so we just return IDENTIFIER.
13171 But, if the qualifying scope is non-dependent then we can
13172 (and must) do name-lookup normally. */
13173 if (template_keyword_p
13175 || (TYPE_P (parser
->scope
)
13176 && dependent_type_p (parser
->scope
))))
13180 /* Look up the name. */
13181 decl
= cp_parser_lookup_name (parser
, identifier
,
13183 /*is_template=*/true,
13184 /*is_namespace=*/false,
13185 check_dependency_p
,
13186 /*ambiguous_decls=*/NULL
,
13189 /* If DECL is a template, then the name was a template-name. */
13190 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
13194 tree fn
= NULL_TREE
;
13196 /* The standard does not explicitly indicate whether a name that
13197 names a set of overloaded declarations, some of which are
13198 templates, is a template-name. However, such a name should
13199 be a template-name; otherwise, there is no way to form a
13200 template-id for the overloaded templates. */
13201 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
13202 if (TREE_CODE (fns
) == OVERLOAD
)
13203 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
13204 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
13209 /* The name does not name a template. */
13210 cp_parser_error (parser
, "expected template-name");
13211 return error_mark_node
;
13215 /* If DECL is dependent, and refers to a function, then just return
13216 its name; we will look it up again during template instantiation. */
13217 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
13219 tree scope
= ovl_scope (decl
);
13220 if (TYPE_P (scope
) && dependent_type_p (scope
))
13227 /* Parse a template-argument-list.
13229 template-argument-list:
13230 template-argument ... [opt]
13231 template-argument-list , template-argument ... [opt]
13233 Returns a TREE_VEC containing the arguments. */
13236 cp_parser_template_argument_list (cp_parser
* parser
)
13238 tree fixed_args
[10];
13239 unsigned n_args
= 0;
13240 unsigned alloced
= 10;
13241 tree
*arg_ary
= fixed_args
;
13243 bool saved_in_template_argument_list_p
;
13245 bool saved_non_ice_p
;
13247 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
13248 parser
->in_template_argument_list_p
= true;
13249 /* Even if the template-id appears in an integral
13250 constant-expression, the contents of the argument list do
13252 saved_ice_p
= parser
->integral_constant_expression_p
;
13253 parser
->integral_constant_expression_p
= false;
13254 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
13255 parser
->non_integral_constant_expression_p
= false;
13257 /* Parse the arguments. */
13263 /* Consume the comma. */
13264 cp_lexer_consume_token (parser
->lexer
);
13266 /* Parse the template-argument. */
13267 argument
= cp_parser_template_argument (parser
);
13269 /* If the next token is an ellipsis, we're expanding a template
13271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13273 if (argument
== error_mark_node
)
13275 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13276 error_at (token
->location
,
13277 "expected parameter pack before %<...%>");
13279 /* Consume the `...' token. */
13280 cp_lexer_consume_token (parser
->lexer
);
13282 /* Make the argument into a TYPE_PACK_EXPANSION or
13283 EXPR_PACK_EXPANSION. */
13284 argument
= make_pack_expansion (argument
);
13287 if (n_args
== alloced
)
13291 if (arg_ary
== fixed_args
)
13293 arg_ary
= XNEWVEC (tree
, alloced
);
13294 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
13297 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
13299 arg_ary
[n_args
++] = argument
;
13301 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
13303 vec
= make_tree_vec (n_args
);
13306 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
13308 if (arg_ary
!= fixed_args
)
13310 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
13311 parser
->integral_constant_expression_p
= saved_ice_p
;
13312 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
13313 #ifdef ENABLE_CHECKING
13314 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
13319 /* Parse a template-argument.
13322 assignment-expression
13326 The representation is that of an assignment-expression, type-id, or
13327 id-expression -- except that the qualified id-expression is
13328 evaluated, so that the value returned is either a DECL or an
13331 Although the standard says "assignment-expression", it forbids
13332 throw-expressions or assignments in the template argument.
13333 Therefore, we use "conditional-expression" instead. */
13336 cp_parser_template_argument (cp_parser
* parser
)
13341 bool maybe_type_id
= false;
13342 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
13343 location_t loc
= 0;
13346 /* There's really no way to know what we're looking at, so we just
13347 try each alternative in order.
13351 In a template-argument, an ambiguity between a type-id and an
13352 expression is resolved to a type-id, regardless of the form of
13353 the corresponding template-parameter.
13355 Therefore, we try a type-id first. */
13356 cp_parser_parse_tentatively (parser
);
13357 argument
= cp_parser_template_type_arg (parser
);
13358 /* If there was no error parsing the type-id but the next token is a
13359 '>>', our behavior depends on which dialect of C++ we're
13360 parsing. In C++98, we probably found a typo for '> >'. But there
13361 are type-id which are also valid expressions. For instance:
13363 struct X { int operator >> (int); };
13364 template <int V> struct Foo {};
13367 Here 'X()' is a valid type-id of a function type, but the user just
13368 wanted to write the expression "X() >> 5". Thus, we remember that we
13369 found a valid type-id, but we still try to parse the argument as an
13370 expression to see what happens.
13372 In C++0x, the '>>' will be considered two separate '>'
13374 if (!cp_parser_error_occurred (parser
)
13375 && cxx_dialect
== cxx98
13376 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
13378 maybe_type_id
= true;
13379 cp_parser_abort_tentative_parse (parser
);
13383 /* If the next token isn't a `,' or a `>', then this argument wasn't
13384 really finished. This means that the argument is not a valid
13386 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13387 cp_parser_error (parser
, "expected template-argument");
13388 /* If that worked, we're done. */
13389 if (cp_parser_parse_definitely (parser
))
13392 /* We're still not sure what the argument will be. */
13393 cp_parser_parse_tentatively (parser
);
13394 /* Try a template. */
13395 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
13396 argument
= cp_parser_id_expression (parser
,
13397 /*template_keyword_p=*/false,
13398 /*check_dependency_p=*/true,
13400 /*declarator_p=*/false,
13401 /*optional_p=*/false);
13402 /* If the next token isn't a `,' or a `>', then this argument wasn't
13403 really finished. */
13404 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13405 cp_parser_error (parser
, "expected template-argument");
13406 if (!cp_parser_error_occurred (parser
))
13408 /* Figure out what is being referred to. If the id-expression
13409 was for a class template specialization, then we will have a
13410 TYPE_DECL at this point. There is no need to do name lookup
13411 at this point in that case. */
13412 if (TREE_CODE (argument
) != TYPE_DECL
)
13413 argument
= cp_parser_lookup_name (parser
, argument
,
13415 /*is_template=*/template_p
,
13416 /*is_namespace=*/false,
13417 /*check_dependency=*/true,
13418 /*ambiguous_decls=*/NULL
,
13419 argument_start_token
->location
);
13420 if (TREE_CODE (argument
) != TEMPLATE_DECL
13421 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
13422 cp_parser_error (parser
, "expected template-name");
13424 if (cp_parser_parse_definitely (parser
))
13426 /* It must be a non-type argument. There permitted cases are given
13427 in [temp.arg.nontype]:
13429 -- an integral constant-expression of integral or enumeration
13432 -- the name of a non-type template-parameter; or
13434 -- the name of an object or function with external linkage...
13436 -- the address of an object or function with external linkage...
13438 -- a pointer to member... */
13439 /* Look for a non-type template parameter. */
13440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13442 cp_parser_parse_tentatively (parser
);
13443 argument
= cp_parser_primary_expression (parser
,
13444 /*address_p=*/false,
13446 /*template_arg_p=*/true,
13448 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
13449 || !cp_parser_next_token_ends_template_argument_p (parser
))
13450 cp_parser_simulate_error (parser
);
13451 if (cp_parser_parse_definitely (parser
))
13455 /* If the next token is "&", the argument must be the address of an
13456 object or function with external linkage. */
13457 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
13460 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13461 cp_lexer_consume_token (parser
->lexer
);
13463 /* See if we might have an id-expression. */
13464 token
= cp_lexer_peek_token (parser
->lexer
);
13465 if (token
->type
== CPP_NAME
13466 || token
->keyword
== RID_OPERATOR
13467 || token
->type
== CPP_SCOPE
13468 || token
->type
== CPP_TEMPLATE_ID
13469 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
13471 cp_parser_parse_tentatively (parser
);
13472 argument
= cp_parser_primary_expression (parser
,
13475 /*template_arg_p=*/true,
13477 if (cp_parser_error_occurred (parser
)
13478 || !cp_parser_next_token_ends_template_argument_p (parser
))
13479 cp_parser_abort_tentative_parse (parser
);
13484 if (INDIRECT_REF_P (argument
))
13486 gcc_assert (REFERENCE_REF_P (argument
));
13487 argument
= TREE_OPERAND (argument
, 0);
13490 /* If we're in a template, we represent a qualified-id referring
13491 to a static data member as a SCOPE_REF even if the scope isn't
13492 dependent so that we can check access control later. */
13494 if (TREE_CODE (probe
) == SCOPE_REF
)
13495 probe
= TREE_OPERAND (probe
, 1);
13498 /* A variable without external linkage might still be a
13499 valid constant-expression, so no error is issued here
13500 if the external-linkage check fails. */
13501 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
13502 cp_parser_simulate_error (parser
);
13504 else if (is_overloaded_fn (argument
))
13505 /* All overloaded functions are allowed; if the external
13506 linkage test does not pass, an error will be issued
13510 && (TREE_CODE (argument
) == OFFSET_REF
13511 || TREE_CODE (argument
) == SCOPE_REF
))
13512 /* A pointer-to-member. */
13514 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
13517 cp_parser_simulate_error (parser
);
13519 if (cp_parser_parse_definitely (parser
))
13522 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
13523 tf_warning_or_error
);
13528 /* If the argument started with "&", there are no other valid
13529 alternatives at this point. */
13532 cp_parser_error (parser
, "invalid non-type template argument");
13533 return error_mark_node
;
13536 /* If the argument wasn't successfully parsed as a type-id followed
13537 by '>>', the argument can only be a constant expression now.
13538 Otherwise, we try parsing the constant-expression tentatively,
13539 because the argument could really be a type-id. */
13541 cp_parser_parse_tentatively (parser
);
13542 argument
= cp_parser_constant_expression (parser
,
13543 /*allow_non_constant_p=*/false,
13544 /*non_constant_p=*/NULL
);
13545 if (!maybe_type_id
)
13547 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13548 cp_parser_error (parser
, "expected template-argument");
13549 if (cp_parser_parse_definitely (parser
))
13551 /* We did our best to parse the argument as a non type-id, but that
13552 was the only alternative that matched (albeit with a '>' after
13553 it). We can assume it's just a typo from the user, and a
13554 diagnostic will then be issued. */
13555 return cp_parser_template_type_arg (parser
);
13558 /* Parse an explicit-instantiation.
13560 explicit-instantiation:
13561 template declaration
13563 Although the standard says `declaration', what it really means is:
13565 explicit-instantiation:
13566 template decl-specifier-seq [opt] declarator [opt] ;
13568 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13569 supposed to be allowed. A defect report has been filed about this
13574 explicit-instantiation:
13575 storage-class-specifier template
13576 decl-specifier-seq [opt] declarator [opt] ;
13577 function-specifier template
13578 decl-specifier-seq [opt] declarator [opt] ; */
13581 cp_parser_explicit_instantiation (cp_parser
* parser
)
13583 int declares_class_or_enum
;
13584 cp_decl_specifier_seq decl_specifiers
;
13585 tree extension_specifier
= NULL_TREE
;
13587 timevar_push (TV_TEMPLATE_INST
);
13589 /* Look for an (optional) storage-class-specifier or
13590 function-specifier. */
13591 if (cp_parser_allow_gnu_extensions_p (parser
))
13593 extension_specifier
13594 = cp_parser_storage_class_specifier_opt (parser
);
13595 if (!extension_specifier
)
13596 extension_specifier
13597 = cp_parser_function_specifier_opt (parser
,
13598 /*decl_specs=*/NULL
);
13601 /* Look for the `template' keyword. */
13602 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
13603 /* Let the front end know that we are processing an explicit
13605 begin_explicit_instantiation ();
13606 /* [temp.explicit] says that we are supposed to ignore access
13607 control while processing explicit instantiation directives. */
13608 push_deferring_access_checks (dk_no_check
);
13609 /* Parse a decl-specifier-seq. */
13610 cp_parser_decl_specifier_seq (parser
,
13611 CP_PARSER_FLAGS_OPTIONAL
,
13613 &declares_class_or_enum
);
13614 /* If there was exactly one decl-specifier, and it declared a class,
13615 and there's no declarator, then we have an explicit type
13617 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
13621 type
= check_tag_decl (&decl_specifiers
,
13622 /*explicit_type_instantiation_p=*/true);
13623 /* Turn access control back on for names used during
13624 template instantiation. */
13625 pop_deferring_access_checks ();
13627 do_type_instantiation (type
, extension_specifier
,
13628 /*complain=*/tf_error
);
13632 cp_declarator
*declarator
;
13635 /* Parse the declarator. */
13637 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13638 /*ctor_dtor_or_conv_p=*/NULL
,
13639 /*parenthesized_p=*/NULL
,
13640 /*member_p=*/false);
13641 if (declares_class_or_enum
& 2)
13642 cp_parser_check_for_definition_in_return_type (declarator
,
13643 decl_specifiers
.type
,
13644 decl_specifiers
.locations
[ds_type_spec
]);
13645 if (declarator
!= cp_error_declarator
)
13647 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
13648 permerror (decl_specifiers
.locations
[ds_inline
],
13649 "explicit instantiation shall not use"
13650 " %<inline%> specifier");
13651 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
13652 permerror (decl_specifiers
.locations
[ds_constexpr
],
13653 "explicit instantiation shall not use"
13654 " %<constexpr%> specifier");
13656 decl
= grokdeclarator (declarator
, &decl_specifiers
,
13657 NORMAL
, 0, &decl_specifiers
.attributes
);
13658 /* Turn access control back on for names used during
13659 template instantiation. */
13660 pop_deferring_access_checks ();
13661 /* Do the explicit instantiation. */
13662 do_decl_instantiation (decl
, extension_specifier
);
13666 pop_deferring_access_checks ();
13667 /* Skip the body of the explicit instantiation. */
13668 cp_parser_skip_to_end_of_statement (parser
);
13671 /* We're done with the instantiation. */
13672 end_explicit_instantiation ();
13674 cp_parser_consume_semicolon_at_end_of_statement (parser
);
13676 timevar_pop (TV_TEMPLATE_INST
);
13679 /* Parse an explicit-specialization.
13681 explicit-specialization:
13682 template < > declaration
13684 Although the standard says `declaration', what it really means is:
13686 explicit-specialization:
13687 template <> decl-specifier [opt] init-declarator [opt] ;
13688 template <> function-definition
13689 template <> explicit-specialization
13690 template <> template-declaration */
13693 cp_parser_explicit_specialization (cp_parser
* parser
)
13695 bool need_lang_pop
;
13696 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13698 /* Look for the `template' keyword. */
13699 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
13700 /* Look for the `<'. */
13701 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13702 /* Look for the `>'. */
13703 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13704 /* We have processed another parameter list. */
13705 ++parser
->num_template_parameter_lists
;
13708 A template ... explicit specialization ... shall not have C
13710 if (current_lang_name
== lang_name_c
)
13712 error_at (token
->location
, "template specialization with C linkage");
13713 /* Give it C++ linkage to avoid confusing other parts of the
13715 push_lang_context (lang_name_cplusplus
);
13716 need_lang_pop
= true;
13719 need_lang_pop
= false;
13720 /* Let the front end know that we are beginning a specialization. */
13721 if (!begin_specialization ())
13723 end_specialization ();
13727 /* If the next keyword is `template', we need to figure out whether
13728 or not we're looking a template-declaration. */
13729 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13731 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13732 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
13733 cp_parser_template_declaration_after_export (parser
,
13734 /*member_p=*/false);
13736 cp_parser_explicit_specialization (parser
);
13739 /* Parse the dependent declaration. */
13740 cp_parser_single_declaration (parser
,
13742 /*member_p=*/false,
13743 /*explicit_specialization_p=*/true,
13744 /*friend_p=*/NULL
);
13745 /* We're done with the specialization. */
13746 end_specialization ();
13747 /* For the erroneous case of a template with C linkage, we pushed an
13748 implicit C++ linkage scope; exit that scope now. */
13750 pop_lang_context ();
13751 /* We're done with this parameter list. */
13752 --parser
->num_template_parameter_lists
;
13755 /* Parse a type-specifier.
13758 simple-type-specifier
13761 elaborated-type-specifier
13769 Returns a representation of the type-specifier. For a
13770 class-specifier, enum-specifier, or elaborated-type-specifier, a
13771 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13773 The parser flags FLAGS is used to control type-specifier parsing.
13775 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13776 in a decl-specifier-seq.
13778 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13779 class-specifier, enum-specifier, or elaborated-type-specifier, then
13780 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13781 if a type is declared; 2 if it is defined. Otherwise, it is set to
13784 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13785 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13786 is set to FALSE. */
13789 cp_parser_type_specifier (cp_parser
* parser
,
13790 cp_parser_flags flags
,
13791 cp_decl_specifier_seq
*decl_specs
,
13792 bool is_declaration
,
13793 int* declares_class_or_enum
,
13794 bool* is_cv_qualifier
)
13796 tree type_spec
= NULL_TREE
;
13799 cp_decl_spec ds
= ds_last
;
13801 /* Assume this type-specifier does not declare a new type. */
13802 if (declares_class_or_enum
)
13803 *declares_class_or_enum
= 0;
13804 /* And that it does not specify a cv-qualifier. */
13805 if (is_cv_qualifier
)
13806 *is_cv_qualifier
= false;
13807 /* Peek at the next token. */
13808 token
= cp_lexer_peek_token (parser
->lexer
);
13810 /* If we're looking at a keyword, we can use that to guide the
13811 production we choose. */
13812 keyword
= token
->keyword
;
13816 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
13817 goto elaborated_type_specifier
;
13819 /* Look for the enum-specifier. */
13820 type_spec
= cp_parser_enum_specifier (parser
);
13821 /* If that worked, we're done. */
13824 if (declares_class_or_enum
)
13825 *declares_class_or_enum
= 2;
13827 cp_parser_set_decl_spec_type (decl_specs
,
13830 /*type_definition_p=*/true);
13834 goto elaborated_type_specifier
;
13836 /* Any of these indicate either a class-specifier, or an
13837 elaborated-type-specifier. */
13841 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
13842 goto elaborated_type_specifier
;
13844 /* Parse tentatively so that we can back up if we don't find a
13845 class-specifier. */
13846 cp_parser_parse_tentatively (parser
);
13847 /* Look for the class-specifier. */
13848 type_spec
= cp_parser_class_specifier (parser
);
13849 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
13850 /* If that worked, we're done. */
13851 if (cp_parser_parse_definitely (parser
))
13853 if (declares_class_or_enum
)
13854 *declares_class_or_enum
= 2;
13856 cp_parser_set_decl_spec_type (decl_specs
,
13859 /*type_definition_p=*/true);
13863 /* Fall through. */
13864 elaborated_type_specifier
:
13865 /* We're declaring (not defining) a class or enum. */
13866 if (declares_class_or_enum
)
13867 *declares_class_or_enum
= 1;
13869 /* Fall through. */
13871 /* Look for an elaborated-type-specifier. */
13873 = (cp_parser_elaborated_type_specifier
13875 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
13878 cp_parser_set_decl_spec_type (decl_specs
,
13881 /*type_definition_p=*/false);
13886 if (is_cv_qualifier
)
13887 *is_cv_qualifier
= true;
13892 if (is_cv_qualifier
)
13893 *is_cv_qualifier
= true;
13898 if (is_cv_qualifier
)
13899 *is_cv_qualifier
= true;
13903 /* The `__complex__' keyword is a GNU extension. */
13911 /* Handle simple keywords. */
13916 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13917 decl_specs
->any_specifiers_p
= true;
13919 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13922 /* If we do not already have a type-specifier, assume we are looking
13923 at a simple-type-specifier. */
13924 type_spec
= cp_parser_simple_type_specifier (parser
,
13928 /* If we didn't find a type-specifier, and a type-specifier was not
13929 optional in this context, issue an error message. */
13930 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
13932 cp_parser_error (parser
, "expected type specifier");
13933 return error_mark_node
;
13939 /* Parse a simple-type-specifier.
13941 simple-type-specifier:
13942 :: [opt] nested-name-specifier [opt] type-name
13943 :: [opt] nested-name-specifier template template-id
13958 simple-type-specifier:
13960 decltype ( expression )
13963 __underlying_type ( type-id )
13967 simple-type-specifier:
13969 __typeof__ unary-expression
13970 __typeof__ ( type-id )
13972 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13973 appropriately updated. */
13976 cp_parser_simple_type_specifier (cp_parser
* parser
,
13977 cp_decl_specifier_seq
*decl_specs
,
13978 cp_parser_flags flags
)
13980 tree type
= NULL_TREE
;
13983 /* Peek at the next token. */
13984 token
= cp_lexer_peek_token (parser
->lexer
);
13986 /* If we're looking at a keyword, things are easy. */
13987 switch (token
->keyword
)
13991 decl_specs
->explicit_char_p
= true;
13992 type
= char_type_node
;
13995 type
= char16_type_node
;
13998 type
= char32_type_node
;
14001 type
= wchar_type_node
;
14004 type
= boolean_type_node
;
14007 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14008 type
= short_integer_type_node
;
14012 decl_specs
->explicit_int_p
= true;
14013 type
= integer_type_node
;
14016 if (!int128_integer_type_node
)
14019 decl_specs
->explicit_int128_p
= true;
14020 type
= int128_integer_type_node
;
14024 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14025 type
= long_integer_type_node
;
14028 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14029 type
= integer_type_node
;
14032 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14033 type
= unsigned_type_node
;
14036 type
= float_type_node
;
14039 type
= double_type_node
;
14042 type
= void_type_node
;
14046 maybe_warn_cpp0x (CPP0X_AUTO
);
14047 type
= make_auto ();
14051 /* Since DR 743, decltype can either be a simple-type-specifier by
14052 itself or begin a nested-name-specifier. Parsing it will replace
14053 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14054 handling below decide what to do. */
14055 cp_parser_decltype (parser
);
14056 cp_lexer_set_token_position (parser
->lexer
, token
);
14060 /* Consume the `typeof' token. */
14061 cp_lexer_consume_token (parser
->lexer
);
14062 /* Parse the operand to `typeof'. */
14063 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14064 /* If it is not already a TYPE, take its type. */
14065 if (!TYPE_P (type
))
14066 type
= finish_typeof (type
);
14069 cp_parser_set_decl_spec_type (decl_specs
, type
,
14071 /*type_definition_p=*/false);
14075 case RID_UNDERLYING_TYPE
:
14076 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14078 cp_parser_set_decl_spec_type (decl_specs
, type
,
14080 /*type_definition_p=*/false);
14085 case RID_DIRECT_BASES
:
14086 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14088 cp_parser_set_decl_spec_type (decl_specs
, type
,
14090 /*type_definition_p=*/false);
14096 /* If token is an already-parsed decltype not followed by ::,
14097 it's a simple-type-specifier. */
14098 if (token
->type
== CPP_DECLTYPE
14099 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
14101 type
= token
->u
.value
;
14103 cp_parser_set_decl_spec_type (decl_specs
, type
,
14105 /*type_definition_p=*/false);
14106 cp_lexer_consume_token (parser
->lexer
);
14110 /* If the type-specifier was for a built-in type, we're done. */
14113 /* Record the type. */
14115 && (token
->keyword
!= RID_SIGNED
14116 && token
->keyword
!= RID_UNSIGNED
14117 && token
->keyword
!= RID_SHORT
14118 && token
->keyword
!= RID_LONG
))
14119 cp_parser_set_decl_spec_type (decl_specs
,
14122 /*type_definition_p=*/false);
14124 decl_specs
->any_specifiers_p
= true;
14126 /* Consume the token. */
14127 cp_lexer_consume_token (parser
->lexer
);
14129 /* There is no valid C++ program where a non-template type is
14130 followed by a "<". That usually indicates that the user thought
14131 that the type was a template. */
14132 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
14135 return TYPE_NAME (type
);
14138 /* The type-specifier must be a user-defined type. */
14139 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
14144 /* Don't gobble tokens or issue error messages if this is an
14145 optional type-specifier. */
14146 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
14147 cp_parser_parse_tentatively (parser
);
14149 /* Look for the optional `::' operator. */
14151 = (cp_parser_global_scope_opt (parser
,
14152 /*current_scope_valid_p=*/false)
14154 /* Look for the nested-name specifier. */
14156 = (cp_parser_nested_name_specifier_opt (parser
,
14157 /*typename_keyword_p=*/false,
14158 /*check_dependency_p=*/true,
14160 /*is_declaration=*/false)
14162 token
= cp_lexer_peek_token (parser
->lexer
);
14163 /* If we have seen a nested-name-specifier, and the next token
14164 is `template', then we are using the template-id production. */
14166 && cp_parser_optional_template_keyword (parser
))
14168 /* Look for the template-id. */
14169 type
= cp_parser_template_id (parser
,
14170 /*template_keyword_p=*/true,
14171 /*check_dependency_p=*/true,
14173 /*is_declaration=*/false);
14174 /* If the template-id did not name a type, we are out of
14176 if (TREE_CODE (type
) != TYPE_DECL
)
14178 cp_parser_error (parser
, "expected template-id for type");
14182 /* Otherwise, look for a type-name. */
14184 type
= cp_parser_type_name (parser
);
14185 /* Keep track of all name-lookups performed in class scopes. */
14189 && TREE_CODE (type
) == TYPE_DECL
14190 && identifier_p (DECL_NAME (type
)))
14191 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
14192 /* If it didn't work out, we don't have a TYPE. */
14193 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
14194 && !cp_parser_parse_definitely (parser
))
14196 if (type
&& decl_specs
)
14197 cp_parser_set_decl_spec_type (decl_specs
, type
,
14199 /*type_definition_p=*/false);
14202 /* If we didn't get a type-name, issue an error message. */
14203 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14205 cp_parser_error (parser
, "expected type-name");
14206 return error_mark_node
;
14209 if (type
&& type
!= error_mark_node
)
14211 /* See if TYPE is an Objective-C type, and if so, parse and
14212 accept any protocol references following it. Do this before
14213 the cp_parser_check_for_invalid_template_id() call, because
14214 Objective-C types can be followed by '<...>' which would
14215 enclose protocol names rather than template arguments, and so
14216 everything is fine. */
14217 if (c_dialect_objc () && !parser
->scope
14218 && (objc_is_id (type
) || objc_is_class_name (type
)))
14220 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14221 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
14223 /* Clobber the "unqualified" type previously entered into
14224 DECL_SPECS with the new, improved protocol-qualified version. */
14226 decl_specs
->type
= qual_type
;
14231 /* There is no valid C++ program where a non-template type is
14232 followed by a "<". That usually indicates that the user
14233 thought that the type was a template. */
14234 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
14242 /* Parse a type-name.
14248 simple-template-id [in c++0x]
14256 Returns a TYPE_DECL for the type. */
14259 cp_parser_type_name (cp_parser
* parser
)
14263 /* We can't know yet whether it is a class-name or not. */
14264 cp_parser_parse_tentatively (parser
);
14265 /* Try a class-name. */
14266 type_decl
= cp_parser_class_name (parser
,
14267 /*typename_keyword_p=*/false,
14268 /*template_keyword_p=*/false,
14270 /*check_dependency_p=*/true,
14271 /*class_head_p=*/false,
14272 /*is_declaration=*/false);
14273 /* If it's not a class-name, keep looking. */
14274 if (!cp_parser_parse_definitely (parser
))
14276 if (cxx_dialect
< cxx0x
)
14277 /* It must be a typedef-name or an enum-name. */
14278 return cp_parser_nonclass_name (parser
);
14280 cp_parser_parse_tentatively (parser
);
14281 /* It is either a simple-template-id representing an
14282 instantiation of an alias template... */
14283 type_decl
= cp_parser_template_id (parser
,
14284 /*template_keyword_p=*/false,
14285 /*check_dependency_p=*/false,
14287 /*is_declaration=*/false);
14288 /* Note that this must be an instantiation of an alias template
14289 because [temp.names]/6 says:
14291 A template-id that names an alias template specialization
14294 Whereas [temp.names]/7 says:
14296 A simple-template-id that names a class template
14297 specialization is a class-name. */
14298 if (type_decl
!= NULL_TREE
14299 && TREE_CODE (type_decl
) == TYPE_DECL
14300 && TYPE_DECL_ALIAS_P (type_decl
))
14301 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
14303 cp_parser_simulate_error (parser
);
14305 if (!cp_parser_parse_definitely (parser
))
14306 /* ... Or a typedef-name or an enum-name. */
14307 return cp_parser_nonclass_name (parser
);
14313 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14321 Returns a TYPE_DECL for the type. */
14324 cp_parser_nonclass_name (cp_parser
* parser
)
14329 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14330 identifier
= cp_parser_identifier (parser
);
14331 if (identifier
== error_mark_node
)
14332 return error_mark_node
;
14334 /* Look up the type-name. */
14335 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
14337 if (TREE_CODE (type_decl
) == USING_DECL
)
14339 if (!DECL_DEPENDENT_P (type_decl
))
14340 type_decl
= strip_using_decl (type_decl
);
14341 else if (USING_DECL_TYPENAME_P (type_decl
))
14343 /* We have found a type introduced by a using
14344 declaration at class scope that refers to a dependent
14347 using typename :: [opt] nested-name-specifier unqualified-id ;
14349 type_decl
= make_typename_type (TREE_TYPE (type_decl
),
14350 DECL_NAME (type_decl
),
14351 typename_type
, tf_error
);
14352 if (type_decl
!= error_mark_node
)
14353 type_decl
= TYPE_NAME (type_decl
);
14357 if (TREE_CODE (type_decl
) != TYPE_DECL
14358 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
14360 /* See if this is an Objective-C type. */
14361 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14362 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
14364 type_decl
= TYPE_NAME (type
);
14367 /* Issue an error if we did not find a type-name. */
14368 if (TREE_CODE (type_decl
) != TYPE_DECL
14369 /* In Objective-C, we have the complication that class names are
14370 normally type names and start declarations (eg, the
14371 "NSObject" in "NSObject *object;"), but can be used in an
14372 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14373 is an expression. So, a classname followed by a dot is not a
14374 valid type-name. */
14375 || (objc_is_class_name (TREE_TYPE (type_decl
))
14376 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
14378 if (!cp_parser_simulate_error (parser
))
14379 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
14380 NLE_TYPE
, token
->location
);
14381 return error_mark_node
;
14383 /* Remember that the name was used in the definition of the
14384 current class so that we can check later to see if the
14385 meaning would have been different after the class was
14386 entirely defined. */
14387 else if (type_decl
!= error_mark_node
14389 maybe_note_name_used_in_class (identifier
, type_decl
);
14394 /* Parse an elaborated-type-specifier. Note that the grammar given
14395 here incorporates the resolution to DR68.
14397 elaborated-type-specifier:
14398 class-key :: [opt] nested-name-specifier [opt] identifier
14399 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14400 enum-key :: [opt] nested-name-specifier [opt] identifier
14401 typename :: [opt] nested-name-specifier identifier
14402 typename :: [opt] nested-name-specifier template [opt]
14407 elaborated-type-specifier:
14408 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14409 class-key attributes :: [opt] nested-name-specifier [opt]
14410 template [opt] template-id
14411 enum attributes :: [opt] nested-name-specifier [opt] identifier
14413 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14414 declared `friend'. If IS_DECLARATION is TRUE, then this
14415 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14416 something is being declared.
14418 Returns the TYPE specified. */
14421 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
14423 bool is_declaration
)
14425 enum tag_types tag_type
;
14427 tree type
= NULL_TREE
;
14428 tree attributes
= NULL_TREE
;
14430 cp_token
*token
= NULL
;
14432 /* See if we're looking at the `enum' keyword. */
14433 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
14435 /* Consume the `enum' token. */
14436 cp_lexer_consume_token (parser
->lexer
);
14437 /* Remember that it's an enumeration type. */
14438 tag_type
= enum_type
;
14439 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14440 enums) is used here. */
14441 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14442 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14444 pedwarn (input_location
, 0, "elaborated-type-specifier "
14445 "for a scoped enum must not use the %<%D%> keyword",
14446 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
14447 /* Consume the `struct' or `class' and parse it anyway. */
14448 cp_lexer_consume_token (parser
->lexer
);
14450 /* Parse the attributes. */
14451 attributes
= cp_parser_attributes_opt (parser
);
14453 /* Or, it might be `typename'. */
14454 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14457 /* Consume the `typename' token. */
14458 cp_lexer_consume_token (parser
->lexer
);
14459 /* Remember that it's a `typename' type. */
14460 tag_type
= typename_type
;
14462 /* Otherwise it must be a class-key. */
14465 tag_type
= cp_parser_class_key (parser
);
14466 if (tag_type
== none_type
)
14467 return error_mark_node
;
14468 /* Parse the attributes. */
14469 attributes
= cp_parser_attributes_opt (parser
);
14472 /* Look for the `::' operator. */
14473 globalscope
= cp_parser_global_scope_opt (parser
,
14474 /*current_scope_valid_p=*/false);
14475 /* Look for the nested-name-specifier. */
14476 if (tag_type
== typename_type
&& !globalscope
)
14478 if (!cp_parser_nested_name_specifier (parser
,
14479 /*typename_keyword_p=*/true,
14480 /*check_dependency_p=*/true,
14483 return error_mark_node
;
14486 /* Even though `typename' is not present, the proposed resolution
14487 to Core Issue 180 says that in `class A<T>::B', `B' should be
14488 considered a type-name, even if `A<T>' is dependent. */
14489 cp_parser_nested_name_specifier_opt (parser
,
14490 /*typename_keyword_p=*/true,
14491 /*check_dependency_p=*/true,
14494 /* For everything but enumeration types, consider a template-id.
14495 For an enumeration type, consider only a plain identifier. */
14496 if (tag_type
!= enum_type
)
14498 bool template_p
= false;
14501 /* Allow the `template' keyword. */
14502 template_p
= cp_parser_optional_template_keyword (parser
);
14503 /* If we didn't see `template', we don't know if there's a
14504 template-id or not. */
14506 cp_parser_parse_tentatively (parser
);
14507 /* Parse the template-id. */
14508 token
= cp_lexer_peek_token (parser
->lexer
);
14509 decl
= cp_parser_template_id (parser
, template_p
,
14510 /*check_dependency_p=*/true,
14513 /* If we didn't find a template-id, look for an ordinary
14515 if (!template_p
&& !cp_parser_parse_definitely (parser
))
14517 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14518 in effect, then we must assume that, upon instantiation, the
14519 template will correspond to a class. */
14520 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
14521 && tag_type
== typename_type
)
14522 type
= make_typename_type (parser
->scope
, decl
,
14524 /*complain=*/tf_error
);
14525 /* If the `typename' keyword is in effect and DECL is not a type
14526 decl, then type is non existent. */
14527 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
14529 else if (TREE_CODE (decl
) == TYPE_DECL
)
14530 type
= check_elaborated_type_specifier (tag_type
, decl
,
14531 /*allow_template_p=*/true);
14532 else if (decl
== error_mark_node
)
14533 type
= error_mark_node
;
14538 token
= cp_lexer_peek_token (parser
->lexer
);
14539 identifier
= cp_parser_identifier (parser
);
14541 if (identifier
== error_mark_node
)
14543 parser
->scope
= NULL_TREE
;
14544 return error_mark_node
;
14547 /* For a `typename', we needn't call xref_tag. */
14548 if (tag_type
== typename_type
14549 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
14550 return cp_parser_make_typename_type (parser
, parser
->scope
,
14553 /* Look up a qualified name in the usual way. */
14557 tree ambiguous_decls
;
14559 decl
= cp_parser_lookup_name (parser
, identifier
,
14561 /*is_template=*/false,
14562 /*is_namespace=*/false,
14563 /*check_dependency=*/true,
14567 /* If the lookup was ambiguous, an error will already have been
14569 if (ambiguous_decls
)
14570 return error_mark_node
;
14572 /* If we are parsing friend declaration, DECL may be a
14573 TEMPLATE_DECL tree node here. However, we need to check
14574 whether this TEMPLATE_DECL results in valid code. Consider
14575 the following example:
14578 template <class T> class C {};
14581 template <class T> friend class N::C; // #1, valid code
14583 template <class T> class Y {
14584 friend class N::C; // #2, invalid code
14587 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14588 name lookup of `N::C'. We see that friend declaration must
14589 be template for the code to be valid. Note that
14590 processing_template_decl does not work here since it is
14591 always 1 for the above two cases. */
14593 decl
= (cp_parser_maybe_treat_template_as_class
14594 (decl
, /*tag_name_p=*/is_friend
14595 && parser
->num_template_parameter_lists
));
14597 if (TREE_CODE (decl
) != TYPE_DECL
)
14599 cp_parser_diagnose_invalid_type_name (parser
,
14603 return error_mark_node
;
14606 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
14608 bool allow_template
= (parser
->num_template_parameter_lists
14609 || DECL_SELF_REFERENCE_P (decl
));
14610 type
= check_elaborated_type_specifier (tag_type
, decl
,
14613 if (type
== error_mark_node
)
14614 return error_mark_node
;
14617 /* Forward declarations of nested types, such as
14622 are invalid unless all components preceding the final '::'
14623 are complete. If all enclosing types are complete, these
14624 declarations become merely pointless.
14626 Invalid forward declarations of nested types are errors
14627 caught elsewhere in parsing. Those that are pointless arrive
14630 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14631 && !is_friend
&& !processing_explicit_instantiation
)
14632 warning (0, "declaration %qD does not declare anything", decl
);
14634 type
= TREE_TYPE (decl
);
14638 /* An elaborated-type-specifier sometimes introduces a new type and
14639 sometimes names an existing type. Normally, the rule is that it
14640 introduces a new type only if there is not an existing type of
14641 the same name already in scope. For example, given:
14644 void f() { struct S s; }
14646 the `struct S' in the body of `f' is the same `struct S' as in
14647 the global scope; the existing definition is used. However, if
14648 there were no global declaration, this would introduce a new
14649 local class named `S'.
14651 An exception to this rule applies to the following code:
14653 namespace N { struct S; }
14655 Here, the elaborated-type-specifier names a new type
14656 unconditionally; even if there is already an `S' in the
14657 containing scope this declaration names a new type.
14658 This exception only applies if the elaborated-type-specifier
14659 forms the complete declaration:
14663 A declaration consisting solely of `class-key identifier ;' is
14664 either a redeclaration of the name in the current scope or a
14665 forward declaration of the identifier as a class name. It
14666 introduces the name into the current scope.
14668 We are in this situation precisely when the next token is a `;'.
14670 An exception to the exception is that a `friend' declaration does
14671 *not* name a new type; i.e., given:
14673 struct S { friend struct T; };
14675 `T' is not a new type in the scope of `S'.
14677 Also, `new struct S' or `sizeof (struct S)' never results in the
14678 definition of a new type; a new type can only be declared in a
14679 declaration context. */
14685 /* Friends have special name lookup rules. */
14686 ts
= ts_within_enclosing_non_class
;
14687 else if (is_declaration
14688 && cp_lexer_next_token_is (parser
->lexer
,
14690 /* This is a `class-key identifier ;' */
14696 (parser
->num_template_parameter_lists
14697 && (cp_parser_next_token_starts_class_definition_p (parser
)
14698 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
14699 /* An unqualified name was used to reference this type, so
14700 there were no qualifying templates. */
14701 if (!cp_parser_check_template_parameters (parser
,
14702 /*num_templates=*/0,
14704 /*declarator=*/NULL
))
14705 return error_mark_node
;
14706 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
14710 if (type
== error_mark_node
)
14711 return error_mark_node
;
14713 /* Allow attributes on forward declarations of classes. */
14716 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14717 warning (OPT_Wattributes
,
14718 "attributes ignored on uninstantiated type");
14719 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
14720 && ! processing_explicit_instantiation
)
14721 warning (OPT_Wattributes
,
14722 "attributes ignored on template instantiation");
14723 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
14724 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
14726 warning (OPT_Wattributes
,
14727 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14730 if (tag_type
!= enum_type
)
14732 /* Indicate whether this class was declared as a `class' or as a
14734 if (TREE_CODE (type
) == RECORD_TYPE
)
14735 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
14736 cp_parser_check_class_key (tag_type
, type
);
14739 /* A "<" cannot follow an elaborated type specifier. If that
14740 happens, the user was probably trying to form a template-id. */
14741 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
14747 /* Parse an enum-specifier.
14750 enum-head { enumerator-list [opt] }
14751 enum-head { enumerator-list , } [C++0x]
14754 enum-key identifier [opt] enum-base [opt]
14755 enum-key nested-name-specifier identifier enum-base [opt]
14760 enum struct [C++0x]
14763 : type-specifier-seq
14765 opaque-enum-specifier:
14766 enum-key identifier enum-base [opt] ;
14769 enum-key attributes[opt] identifier [opt] enum-base [opt]
14770 { enumerator-list [opt] }attributes[opt]
14771 enum-key attributes[opt] identifier [opt] enum-base [opt]
14772 { enumerator-list, }attributes[opt] [C++0x]
14774 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14775 if the token stream isn't an enum-specifier after all. */
14778 cp_parser_enum_specifier (cp_parser
* parser
)
14781 tree type
= NULL_TREE
;
14783 tree nested_name_specifier
= NULL_TREE
;
14785 bool scoped_enum_p
= false;
14786 bool has_underlying_type
= false;
14787 bool nested_being_defined
= false;
14788 bool new_value_list
= false;
14789 bool is_new_type
= false;
14790 bool is_anonymous
= false;
14791 tree underlying_type
= NULL_TREE
;
14792 cp_token
*type_start_token
= NULL
;
14793 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
14795 parser
->colon_corrects_to_scope_p
= false;
14797 /* Parse tentatively so that we can back up if we don't find a
14799 cp_parser_parse_tentatively (parser
);
14801 /* Caller guarantees that the current token is 'enum', an identifier
14802 possibly follows, and the token after that is an opening brace.
14803 If we don't have an identifier, fabricate an anonymous name for
14804 the enumeration being defined. */
14805 cp_lexer_consume_token (parser
->lexer
);
14807 /* Parse the "class" or "struct", which indicates a scoped
14808 enumeration type in C++0x. */
14809 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14810 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14812 if (cxx_dialect
< cxx0x
)
14813 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
14815 /* Consume the `struct' or `class' token. */
14816 cp_lexer_consume_token (parser
->lexer
);
14818 scoped_enum_p
= true;
14821 attributes
= cp_parser_attributes_opt (parser
);
14823 /* Clear the qualification. */
14824 parser
->scope
= NULL_TREE
;
14825 parser
->qualifying_scope
= NULL_TREE
;
14826 parser
->object_scope
= NULL_TREE
;
14828 /* Figure out in what scope the declaration is being placed. */
14829 prev_scope
= current_scope ();
14831 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
14833 push_deferring_access_checks (dk_no_check
);
14834 nested_name_specifier
14835 = cp_parser_nested_name_specifier_opt (parser
,
14836 /*typename_keyword_p=*/true,
14837 /*check_dependency_p=*/false,
14839 /*is_declaration=*/false);
14841 if (nested_name_specifier
)
14845 identifier
= cp_parser_identifier (parser
);
14846 name
= cp_parser_lookup_name (parser
, identifier
,
14848 /*is_template=*/false,
14849 /*is_namespace=*/false,
14850 /*check_dependency=*/true,
14851 /*ambiguous_decls=*/NULL
,
14853 if (name
&& name
!= error_mark_node
)
14855 type
= TREE_TYPE (name
);
14856 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14858 /* Are template enums allowed in ISO? */
14859 if (template_parm_scope_p ())
14860 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
14861 "%qD is an enumeration template", name
);
14862 /* ignore a typename reference, for it will be solved by name
14867 else if (nested_name_specifier
== error_mark_node
)
14868 /* We already issued an error. */;
14870 error_at (type_start_token
->location
,
14871 "%qD is not an enumerator-name", identifier
);
14875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14876 identifier
= cp_parser_identifier (parser
);
14879 identifier
= make_anon_name ();
14880 is_anonymous
= true;
14882 error_at (type_start_token
->location
,
14883 "anonymous scoped enum is not allowed");
14886 pop_deferring_access_checks ();
14888 /* Check for the `:' that denotes a specified underlying type in C++0x.
14889 Note that a ':' could also indicate a bitfield width, however. */
14890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14892 cp_decl_specifier_seq type_specifiers
;
14894 /* Consume the `:'. */
14895 cp_lexer_consume_token (parser
->lexer
);
14897 /* Parse the type-specifier-seq. */
14898 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14899 /*is_trailing_return=*/false,
14902 /* At this point this is surely not elaborated type specifier. */
14903 if (!cp_parser_parse_definitely (parser
))
14906 if (cxx_dialect
< cxx0x
)
14907 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
14909 has_underlying_type
= true;
14911 /* If that didn't work, stop. */
14912 if (type_specifiers
.type
!= error_mark_node
)
14914 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
14915 /*initialized=*/0, NULL
);
14916 if (underlying_type
== error_mark_node
)
14917 underlying_type
= NULL_TREE
;
14921 /* Look for the `{' but don't consume it yet. */
14922 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14924 if (cxx_dialect
< cxx0x
|| (!scoped_enum_p
&& !underlying_type
))
14926 cp_parser_error (parser
, "expected %<{%>");
14927 if (has_underlying_type
)
14933 /* An opaque-enum-specifier must have a ';' here. */
14934 if ((scoped_enum_p
|| underlying_type
)
14935 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14937 cp_parser_error (parser
, "expected %<;%> or %<{%>");
14938 if (has_underlying_type
)
14946 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
14949 if (nested_name_specifier
)
14951 if (CLASS_TYPE_P (nested_name_specifier
))
14953 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
14954 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
14955 push_scope (nested_name_specifier
);
14957 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
14959 push_nested_namespace (nested_name_specifier
);
14963 /* Issue an error message if type-definitions are forbidden here. */
14964 if (!cp_parser_check_type_definition (parser
))
14965 type
= error_mark_node
;
14967 /* Create the new type. We do this before consuming the opening
14968 brace so the enum will be recorded as being on the line of its
14969 tag (or the 'enum' keyword, if there is no tag). */
14970 type
= start_enum (identifier
, type
, underlying_type
,
14971 scoped_enum_p
, &is_new_type
);
14973 /* If the next token is not '{' it is an opaque-enum-specifier or an
14974 elaborated-type-specifier. */
14975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14977 timevar_push (TV_PARSE_ENUM
);
14978 if (nested_name_specifier
14979 && nested_name_specifier
!= error_mark_node
)
14981 /* The following catches invalid code such as:
14982 enum class S<int>::E { A, B, C }; */
14983 if (!processing_specialization
14984 && CLASS_TYPE_P (nested_name_specifier
)
14985 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
14986 error_at (type_start_token
->location
, "cannot add an enumerator "
14987 "list to a template instantiation");
14989 /* If that scope does not contain the scope in which the
14990 class was originally declared, the program is invalid. */
14991 if (prev_scope
&& !is_ancestor (prev_scope
, nested_name_specifier
))
14993 if (at_namespace_scope_p ())
14994 error_at (type_start_token
->location
,
14995 "declaration of %qD in namespace %qD which does not "
14997 type
, prev_scope
, nested_name_specifier
);
14999 error_at (type_start_token
->location
,
15000 "declaration of %qD in %qD which does not enclose %qD",
15001 type
, prev_scope
, nested_name_specifier
);
15002 type
= error_mark_node
;
15007 begin_scope (sk_scoped_enum
, type
);
15009 /* Consume the opening brace. */
15010 cp_lexer_consume_token (parser
->lexer
);
15012 if (type
== error_mark_node
)
15013 ; /* Nothing to add */
15014 else if (OPAQUE_ENUM_P (type
)
15015 || (cxx_dialect
> cxx98
&& processing_specialization
))
15017 new_value_list
= true;
15018 SET_OPAQUE_ENUM_P (type
, false);
15019 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15023 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
15024 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15025 "previous definition here");
15026 type
= error_mark_node
;
15029 if (type
== error_mark_node
)
15030 cp_parser_skip_to_end_of_block_or_statement (parser
);
15031 /* If the next token is not '}', then there are some enumerators. */
15032 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15034 if (is_anonymous
&& !scoped_enum_p
)
15035 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15036 "ISO C++ forbids empty anonymous enum");
15039 cp_parser_enumerator_list (parser
, type
);
15041 /* Consume the final '}'. */
15042 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15046 timevar_pop (TV_PARSE_ENUM
);
15050 /* If a ';' follows, then it is an opaque-enum-specifier
15051 and additional restrictions apply. */
15052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15055 error_at (type_start_token
->location
,
15056 "opaque-enum-specifier without name");
15057 else if (nested_name_specifier
)
15058 error_at (type_start_token
->location
,
15059 "opaque-enum-specifier must use a simple identifier");
15063 /* Look for trailing attributes to apply to this enumeration, and
15064 apply them if appropriate. */
15065 if (cp_parser_allow_gnu_extensions_p (parser
))
15067 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15068 trailing_attr
= chainon (trailing_attr
, attributes
);
15069 cplus_decl_attributes (&type
,
15071 (int) ATTR_FLAG_TYPE_IN_PLACE
);
15074 /* Finish up the enumeration. */
15075 if (type
!= error_mark_node
)
15077 if (new_value_list
)
15078 finish_enum_value_list (type
);
15080 finish_enum (type
);
15083 if (nested_name_specifier
)
15085 if (CLASS_TYPE_P (nested_name_specifier
))
15087 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
15088 pop_scope (nested_name_specifier
);
15090 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15092 pop_nested_namespace (nested_name_specifier
);
15096 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
15100 /* Parse an enumerator-list. The enumerators all have the indicated
15104 enumerator-definition
15105 enumerator-list , enumerator-definition */
15108 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
15112 /* Parse an enumerator-definition. */
15113 cp_parser_enumerator_definition (parser
, type
);
15115 /* If the next token is not a ',', we've reached the end of
15117 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15119 /* Otherwise, consume the `,' and keep going. */
15120 cp_lexer_consume_token (parser
->lexer
);
15121 /* If the next token is a `}', there is a trailing comma. */
15122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15124 if (cxx_dialect
< cxx0x
&& !in_system_header
)
15125 pedwarn (input_location
, OPT_Wpedantic
,
15126 "comma at end of enumerator list");
15132 /* Parse an enumerator-definition. The enumerator has the indicated
15135 enumerator-definition:
15137 enumerator = constant-expression
15143 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
15149 /* Save the input location because we are interested in the location
15150 of the identifier and not the location of the explicit value. */
15151 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15153 /* Look for the identifier. */
15154 identifier
= cp_parser_identifier (parser
);
15155 if (identifier
== error_mark_node
)
15158 /* If the next token is an '=', then there is an explicit value. */
15159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15161 /* Consume the `=' token. */
15162 cp_lexer_consume_token (parser
->lexer
);
15163 /* Parse the value. */
15164 value
= cp_parser_constant_expression (parser
,
15165 /*allow_non_constant_p=*/false,
15171 /* If we are processing a template, make sure the initializer of the
15172 enumerator doesn't contain any bare template parameter pack. */
15173 if (check_for_bare_parameter_packs (value
))
15174 value
= error_mark_node
;
15176 /* integral_constant_value will pull out this expression, so make sure
15177 it's folded as appropriate. */
15178 value
= fold_non_dependent_expr (value
);
15180 /* Create the enumerator. */
15181 build_enumerator (identifier
, value
, type
, loc
);
15184 /* Parse a namespace-name.
15187 original-namespace-name
15190 Returns the NAMESPACE_DECL for the namespace. */
15193 cp_parser_namespace_name (cp_parser
* parser
)
15196 tree namespace_decl
;
15198 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15200 /* Get the name of the namespace. */
15201 identifier
= cp_parser_identifier (parser
);
15202 if (identifier
== error_mark_node
)
15203 return error_mark_node
;
15205 /* Look up the identifier in the currently active scope. Look only
15206 for namespaces, due to:
15208 [basic.lookup.udir]
15210 When looking up a namespace-name in a using-directive or alias
15211 definition, only namespace names are considered.
15215 [basic.lookup.qual]
15217 During the lookup of a name preceding the :: scope resolution
15218 operator, object, function, and enumerator names are ignored.
15220 (Note that cp_parser_qualifying_entity only calls this
15221 function if the token after the name is the scope resolution
15223 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
15225 /*is_template=*/false,
15226 /*is_namespace=*/true,
15227 /*check_dependency=*/true,
15228 /*ambiguous_decls=*/NULL
,
15230 /* If it's not a namespace, issue an error. */
15231 if (namespace_decl
== error_mark_node
15232 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
15234 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15235 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
15236 cp_parser_error (parser
, "expected namespace-name");
15237 namespace_decl
= error_mark_node
;
15240 return namespace_decl
;
15243 /* Parse a namespace-definition.
15245 namespace-definition:
15246 named-namespace-definition
15247 unnamed-namespace-definition
15249 named-namespace-definition:
15250 original-namespace-definition
15251 extension-namespace-definition
15253 original-namespace-definition:
15254 namespace identifier { namespace-body }
15256 extension-namespace-definition:
15257 namespace original-namespace-name { namespace-body }
15259 unnamed-namespace-definition:
15260 namespace { namespace-body } */
15263 cp_parser_namespace_definition (cp_parser
* parser
)
15265 tree identifier
, attribs
;
15266 bool has_visibility
;
15269 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
15271 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
15273 cp_lexer_consume_token (parser
->lexer
);
15278 /* Look for the `namespace' keyword. */
15279 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15281 /* Get the name of the namespace. We do not attempt to distinguish
15282 between an original-namespace-definition and an
15283 extension-namespace-definition at this point. The semantic
15284 analysis routines are responsible for that. */
15285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15286 identifier
= cp_parser_identifier (parser
);
15288 identifier
= NULL_TREE
;
15290 /* Parse any specified attributes. */
15291 attribs
= cp_parser_attributes_opt (parser
);
15293 /* Look for the `{' to start the namespace. */
15294 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
15295 /* Start the namespace. */
15296 push_namespace (identifier
);
15298 /* "inline namespace" is equivalent to a stub namespace definition
15299 followed by a strong using directive. */
15302 tree name_space
= current_namespace
;
15303 /* Set up namespace association. */
15304 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
15305 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
15306 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
15307 /* Import the contents of the inline namespace. */
15309 do_using_directive (name_space
);
15310 push_namespace (identifier
);
15313 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
15315 /* Parse the body of the namespace. */
15316 cp_parser_namespace_body (parser
);
15318 if (has_visibility
)
15319 pop_visibility (1);
15321 /* Finish the namespace. */
15323 /* Look for the final `}'. */
15324 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15327 /* Parse a namespace-body.
15330 declaration-seq [opt] */
15333 cp_parser_namespace_body (cp_parser
* parser
)
15335 cp_parser_declaration_seq_opt (parser
);
15338 /* Parse a namespace-alias-definition.
15340 namespace-alias-definition:
15341 namespace identifier = qualified-namespace-specifier ; */
15344 cp_parser_namespace_alias_definition (cp_parser
* parser
)
15347 tree namespace_specifier
;
15349 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15351 /* Look for the `namespace' keyword. */
15352 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15353 /* Look for the identifier. */
15354 identifier
= cp_parser_identifier (parser
);
15355 if (identifier
== error_mark_node
)
15357 /* Look for the `=' token. */
15358 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
15359 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15361 error_at (token
->location
, "%<namespace%> definition is not allowed here");
15362 /* Skip the definition. */
15363 cp_lexer_consume_token (parser
->lexer
);
15364 if (cp_parser_skip_to_closing_brace (parser
))
15365 cp_lexer_consume_token (parser
->lexer
);
15368 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15369 /* Look for the qualified-namespace-specifier. */
15370 namespace_specifier
15371 = cp_parser_qualified_namespace_specifier (parser
);
15372 /* Look for the `;' token. */
15373 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15375 /* Register the alias in the symbol table. */
15376 do_namespace_alias (identifier
, namespace_specifier
);
15379 /* Parse a qualified-namespace-specifier.
15381 qualified-namespace-specifier:
15382 :: [opt] nested-name-specifier [opt] namespace-name
15384 Returns a NAMESPACE_DECL corresponding to the specified
15388 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
15390 /* Look for the optional `::'. */
15391 cp_parser_global_scope_opt (parser
,
15392 /*current_scope_valid_p=*/false);
15394 /* Look for the optional nested-name-specifier. */
15395 cp_parser_nested_name_specifier_opt (parser
,
15396 /*typename_keyword_p=*/false,
15397 /*check_dependency_p=*/true,
15399 /*is_declaration=*/true);
15401 return cp_parser_namespace_name (parser
);
15404 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15405 access declaration.
15408 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15409 using :: unqualified-id ;
15411 access-declaration:
15417 cp_parser_using_declaration (cp_parser
* parser
,
15418 bool access_declaration_p
)
15421 bool typename_p
= false;
15422 bool global_scope_p
;
15426 int oldcount
= errorcount
;
15427 cp_token
*diag_token
= NULL
;
15429 if (access_declaration_p
)
15431 diag_token
= cp_lexer_peek_token (parser
->lexer
);
15432 cp_parser_parse_tentatively (parser
);
15436 /* Look for the `using' keyword. */
15437 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15439 /* Peek at the next token. */
15440 token
= cp_lexer_peek_token (parser
->lexer
);
15441 /* See if it's `typename'. */
15442 if (token
->keyword
== RID_TYPENAME
)
15444 /* Remember that we've seen it. */
15446 /* Consume the `typename' token. */
15447 cp_lexer_consume_token (parser
->lexer
);
15451 /* Look for the optional global scope qualification. */
15453 = (cp_parser_global_scope_opt (parser
,
15454 /*current_scope_valid_p=*/false)
15457 /* If we saw `typename', or didn't see `::', then there must be a
15458 nested-name-specifier present. */
15459 if (typename_p
|| !global_scope_p
)
15460 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
15461 /*check_dependency_p=*/true,
15463 /*is_declaration=*/true);
15464 /* Otherwise, we could be in either of the two productions. In that
15465 case, treat the nested-name-specifier as optional. */
15467 qscope
= cp_parser_nested_name_specifier_opt (parser
,
15468 /*typename_keyword_p=*/false,
15469 /*check_dependency_p=*/true,
15471 /*is_declaration=*/true);
15473 qscope
= global_namespace
;
15475 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
15476 /* Something has already gone wrong; there's no need to parse
15477 further. Since an error has occurred, the return value of
15478 cp_parser_parse_definitely will be false, as required. */
15479 return cp_parser_parse_definitely (parser
);
15481 token
= cp_lexer_peek_token (parser
->lexer
);
15482 /* Parse the unqualified-id. */
15483 identifier
= cp_parser_unqualified_id (parser
,
15484 /*template_keyword_p=*/false,
15485 /*check_dependency_p=*/true,
15486 /*declarator_p=*/true,
15487 /*optional_p=*/false);
15489 if (access_declaration_p
)
15491 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15492 cp_parser_simulate_error (parser
);
15493 if (!cp_parser_parse_definitely (parser
))
15497 /* The function we call to handle a using-declaration is different
15498 depending on what scope we are in. */
15499 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
15501 else if (!identifier_p (identifier
)
15502 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
15503 /* [namespace.udecl]
15505 A using declaration shall not name a template-id. */
15506 error_at (token
->location
,
15507 "a template-id may not appear in a using-declaration");
15510 if (at_class_scope_p ())
15512 /* Create the USING_DECL. */
15513 decl
= do_class_using_decl (parser
->scope
, identifier
);
15515 if (decl
&& typename_p
)
15516 USING_DECL_TYPENAME_P (decl
) = 1;
15518 if (check_for_bare_parameter_packs (decl
))
15521 /* Add it to the list of members in this class. */
15522 finish_member_declaration (decl
);
15526 decl
= cp_parser_lookup_name_simple (parser
,
15529 if (decl
== error_mark_node
)
15530 cp_parser_name_lookup_error (parser
, identifier
,
15533 else if (check_for_bare_parameter_packs (decl
))
15535 else if (!at_namespace_scope_p ())
15536 do_local_using_decl (decl
, qscope
, identifier
);
15538 do_toplevel_using_decl (decl
, qscope
, identifier
);
15542 /* Look for the final `;'. */
15543 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15545 if (access_declaration_p
&& errorcount
== oldcount
)
15546 warning_at (diag_token
->location
, OPT_Wdeprecated
,
15547 "access declarations are deprecated "
15548 "in favour of using-declarations; "
15549 "suggestion: add the %<using%> keyword");
15554 /* Parse an alias-declaration.
15557 using identifier attribute-specifier-seq [opt] = type-id */
15560 cp_parser_alias_declaration (cp_parser
* parser
)
15562 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
15563 location_t id_location
;
15564 cp_declarator
*declarator
;
15565 cp_decl_specifier_seq decl_specs
;
15567 const char *saved_message
= NULL
;
15569 /* Look for the `using' keyword. */
15570 cp_token
*using_token
15571 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15572 if (using_token
== NULL
)
15573 return error_mark_node
;
15575 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
15576 id
= cp_parser_identifier (parser
);
15577 if (id
== error_mark_node
)
15578 return error_mark_node
;
15580 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
15581 attributes
= cp_parser_attributes_opt (parser
);
15582 if (attributes
== error_mark_node
)
15583 return error_mark_node
;
15585 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15587 if (cp_parser_error_occurred (parser
))
15588 return error_mark_node
;
15590 cp_parser_commit_to_tentative_parse (parser
);
15592 /* Now we are going to parse the type-id of the declaration. */
15597 "A type-specifier-seq shall not define a class or enumeration
15598 unless it appears in the type-id of an alias-declaration (7.1.3) that
15599 is not the declaration of a template-declaration."
15601 In other words, if we currently are in an alias template, the
15602 type-id should not define a type.
15604 So let's set parser->type_definition_forbidden_message in that
15605 case; cp_parser_check_type_definition (called by
15606 cp_parser_class_specifier) will then emit an error if a type is
15607 defined in the type-id. */
15608 if (parser
->num_template_parameter_lists
)
15610 saved_message
= parser
->type_definition_forbidden_message
;
15611 parser
->type_definition_forbidden_message
=
15612 G_("types may not be defined in alias template declarations");
15615 type
= cp_parser_type_id (parser
);
15617 /* Restore the error message if need be. */
15618 if (parser
->num_template_parameter_lists
)
15619 parser
->type_definition_forbidden_message
= saved_message
;
15621 if (type
== error_mark_node
)
15623 cp_parser_skip_to_end_of_block_or_statement (parser
);
15624 return error_mark_node
;
15627 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15629 if (cp_parser_error_occurred (parser
))
15631 cp_parser_skip_to_end_of_block_or_statement (parser
);
15632 return error_mark_node
;
15635 /* A typedef-name can also be introduced by an alias-declaration. The
15636 identifier following the using keyword becomes a typedef-name. It has
15637 the same semantics as if it were introduced by the typedef
15638 specifier. In particular, it does not define a new type and it shall
15639 not appear in the type-id. */
15641 clear_decl_specs (&decl_specs
);
15642 decl_specs
.type
= type
;
15643 if (attributes
!= NULL_TREE
)
15645 decl_specs
.attributes
= attributes
;
15646 set_and_check_decl_spec_loc (&decl_specs
,
15650 set_and_check_decl_spec_loc (&decl_specs
,
15653 set_and_check_decl_spec_loc (&decl_specs
,
15657 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
15658 declarator
->id_loc
= id_location
;
15660 member_p
= at_class_scope_p ();
15662 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
15663 NULL_TREE
, attributes
);
15665 decl
= start_decl (declarator
, &decl_specs
, 0,
15666 attributes
, NULL_TREE
, &pushed_scope
);
15667 if (decl
== error_mark_node
)
15670 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
15673 pop_scope (pushed_scope
);
15675 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15676 added into the symbol table; otherwise, return the TYPE_DECL. */
15677 if (DECL_LANG_SPECIFIC (decl
)
15678 && DECL_TEMPLATE_INFO (decl
)
15679 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
15681 decl
= DECL_TI_TEMPLATE (decl
);
15683 check_member_template (decl
);
15689 /* Parse a using-directive.
15692 using namespace :: [opt] nested-name-specifier [opt]
15693 namespace-name ; */
15696 cp_parser_using_directive (cp_parser
* parser
)
15698 tree namespace_decl
;
15701 /* Look for the `using' keyword. */
15702 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15703 /* And the `namespace' keyword. */
15704 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15705 /* Look for the optional `::' operator. */
15706 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
15707 /* And the optional nested-name-specifier. */
15708 cp_parser_nested_name_specifier_opt (parser
,
15709 /*typename_keyword_p=*/false,
15710 /*check_dependency_p=*/true,
15712 /*is_declaration=*/true);
15713 /* Get the namespace being used. */
15714 namespace_decl
= cp_parser_namespace_name (parser
);
15715 /* And any specified attributes. */
15716 attribs
= cp_parser_attributes_opt (parser
);
15717 /* Update the symbol table. */
15718 parse_using_directive (namespace_decl
, attribs
);
15719 /* Look for the final `;'. */
15720 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15723 /* Parse an asm-definition.
15726 asm ( string-literal ) ;
15731 asm volatile [opt] ( string-literal ) ;
15732 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15733 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15734 : asm-operand-list [opt] ) ;
15735 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15736 : asm-operand-list [opt]
15737 : asm-clobber-list [opt] ) ;
15738 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15739 : asm-clobber-list [opt]
15740 : asm-goto-list ) ; */
15743 cp_parser_asm_definition (cp_parser
* parser
)
15746 tree outputs
= NULL_TREE
;
15747 tree inputs
= NULL_TREE
;
15748 tree clobbers
= NULL_TREE
;
15749 tree labels
= NULL_TREE
;
15751 bool volatile_p
= false;
15752 bool extended_p
= false;
15753 bool invalid_inputs_p
= false;
15754 bool invalid_outputs_p
= false;
15755 bool goto_p
= false;
15756 required_token missing
= RT_NONE
;
15758 /* Look for the `asm' keyword. */
15759 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
15760 /* See if the next token is `volatile'. */
15761 if (cp_parser_allow_gnu_extensions_p (parser
)
15762 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
15764 /* Remember that we saw the `volatile' keyword. */
15766 /* Consume the token. */
15767 cp_lexer_consume_token (parser
->lexer
);
15769 if (cp_parser_allow_gnu_extensions_p (parser
)
15770 && parser
->in_function_body
15771 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
15773 /* Remember that we saw the `goto' keyword. */
15775 /* Consume the token. */
15776 cp_lexer_consume_token (parser
->lexer
);
15778 /* Look for the opening `('. */
15779 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
15781 /* Look for the string. */
15782 string
= cp_parser_string_literal (parser
, false, false);
15783 if (string
== error_mark_node
)
15785 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15786 /*consume_paren=*/true);
15790 /* If we're allowing GNU extensions, check for the extended assembly
15791 syntax. Unfortunately, the `:' tokens need not be separated by
15792 a space in C, and so, for compatibility, we tolerate that here
15793 too. Doing that means that we have to treat the `::' operator as
15795 if (cp_parser_allow_gnu_extensions_p (parser
)
15796 && parser
->in_function_body
15797 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
15798 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
15800 bool inputs_p
= false;
15801 bool clobbers_p
= false;
15802 bool labels_p
= false;
15804 /* The extended syntax was used. */
15807 /* Look for outputs. */
15808 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15810 /* Consume the `:'. */
15811 cp_lexer_consume_token (parser
->lexer
);
15812 /* Parse the output-operands. */
15813 if (cp_lexer_next_token_is_not (parser
->lexer
,
15815 && cp_lexer_next_token_is_not (parser
->lexer
,
15817 && cp_lexer_next_token_is_not (parser
->lexer
,
15820 outputs
= cp_parser_asm_operand_list (parser
);
15822 if (outputs
== error_mark_node
)
15823 invalid_outputs_p
= true;
15825 /* If the next token is `::', there are no outputs, and the
15826 next token is the beginning of the inputs. */
15827 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15828 /* The inputs are coming next. */
15831 /* Look for inputs. */
15833 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15835 /* Consume the `:' or `::'. */
15836 cp_lexer_consume_token (parser
->lexer
);
15837 /* Parse the output-operands. */
15838 if (cp_lexer_next_token_is_not (parser
->lexer
,
15840 && cp_lexer_next_token_is_not (parser
->lexer
,
15842 && cp_lexer_next_token_is_not (parser
->lexer
,
15844 inputs
= cp_parser_asm_operand_list (parser
);
15846 if (inputs
== error_mark_node
)
15847 invalid_inputs_p
= true;
15849 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15850 /* The clobbers are coming next. */
15853 /* Look for clobbers. */
15855 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15858 /* Consume the `:' or `::'. */
15859 cp_lexer_consume_token (parser
->lexer
);
15860 /* Parse the clobbers. */
15861 if (cp_lexer_next_token_is_not (parser
->lexer
,
15863 && cp_lexer_next_token_is_not (parser
->lexer
,
15865 clobbers
= cp_parser_asm_clobber_list (parser
);
15868 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15869 /* The labels are coming next. */
15872 /* Look for labels. */
15874 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
15877 /* Consume the `:' or `::'. */
15878 cp_lexer_consume_token (parser
->lexer
);
15879 /* Parse the labels. */
15880 labels
= cp_parser_asm_label_list (parser
);
15883 if (goto_p
&& !labels_p
)
15884 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
15887 missing
= RT_COLON_SCOPE
;
15889 /* Look for the closing `)'. */
15890 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
15891 missing
? missing
: RT_CLOSE_PAREN
))
15892 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15893 /*consume_paren=*/true);
15894 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15896 if (!invalid_inputs_p
&& !invalid_outputs_p
)
15898 /* Create the ASM_EXPR. */
15899 if (parser
->in_function_body
)
15901 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
15902 inputs
, clobbers
, labels
);
15903 /* If the extended syntax was not used, mark the ASM_EXPR. */
15906 tree temp
= asm_stmt
;
15907 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
15908 temp
= TREE_OPERAND (temp
, 0);
15910 ASM_INPUT_P (temp
) = 1;
15914 add_asm_node (string
);
15918 /* Declarators [gram.dcl.decl] */
15920 /* Parse an init-declarator.
15923 declarator initializer [opt]
15928 declarator asm-specification [opt] attributes [opt] initializer [opt]
15930 function-definition:
15931 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15933 decl-specifier-seq [opt] declarator function-try-block
15937 function-definition:
15938 __extension__ function-definition
15942 function-definition:
15943 decl-specifier-seq [opt] declarator function-transaction-block
15945 The DECL_SPECIFIERS apply to this declarator. Returns a
15946 representation of the entity declared. If MEMBER_P is TRUE, then
15947 this declarator appears in a class scope. The new DECL created by
15948 this declarator is returned.
15950 The CHECKS are access checks that should be performed once we know
15951 what entity is being declared (and, therefore, what classes have
15954 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15955 for a function-definition here as well. If the declarator is a
15956 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15957 be TRUE upon return. By that point, the function-definition will
15958 have been completely parsed.
15960 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15963 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15964 parsed declaration if it is an uninitialized single declarator not followed
15965 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15966 if present, will not be consumed. If returned, this declarator will be
15967 created with SD_INITIALIZED but will not call cp_finish_decl. */
15970 cp_parser_init_declarator (cp_parser
* parser
,
15971 cp_decl_specifier_seq
*decl_specifiers
,
15972 vec
<deferred_access_check
, va_gc
> *checks
,
15973 bool function_definition_allowed_p
,
15975 int declares_class_or_enum
,
15976 bool* function_definition_p
,
15977 tree
* maybe_range_for_decl
)
15979 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
15980 *attributes_start_token
= NULL
;
15981 cp_declarator
*declarator
;
15982 tree prefix_attributes
;
15983 tree attributes
= NULL
;
15984 tree asm_specification
;
15986 tree decl
= NULL_TREE
;
15988 int is_initialized
;
15989 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15990 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15992 enum cpp_ttype initialization_kind
;
15993 bool is_direct_init
= false;
15994 bool is_non_constant_init
;
15995 int ctor_dtor_or_conv_p
;
15997 tree pushed_scope
= NULL_TREE
;
15998 bool range_for_decl_p
= false;
16000 /* Gather the attributes that were provided with the
16001 decl-specifiers. */
16002 prefix_attributes
= decl_specifiers
->attributes
;
16004 /* Assume that this is not the declarator for a function
16006 if (function_definition_p
)
16007 *function_definition_p
= false;
16009 /* Defer access checks while parsing the declarator; we cannot know
16010 what names are accessible until we know what is being
16012 resume_deferring_access_checks ();
16014 /* Parse the declarator. */
16015 token
= cp_lexer_peek_token (parser
->lexer
);
16017 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16018 &ctor_dtor_or_conv_p
,
16019 /*parenthesized_p=*/NULL
,
16021 /* Gather up the deferred checks. */
16022 stop_deferring_access_checks ();
16024 /* If the DECLARATOR was erroneous, there's no need to go
16026 if (declarator
== cp_error_declarator
)
16027 return error_mark_node
;
16029 /* Check that the number of template-parameter-lists is OK. */
16030 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16032 return error_mark_node
;
16034 if (declares_class_or_enum
& 2)
16035 cp_parser_check_for_definition_in_return_type (declarator
,
16036 decl_specifiers
->type
,
16037 decl_specifiers
->locations
[ds_type_spec
]);
16039 /* Figure out what scope the entity declared by the DECLARATOR is
16040 located in. `grokdeclarator' sometimes changes the scope, so
16041 we compute it now. */
16042 scope
= get_scope_of_declarator (declarator
);
16044 /* Perform any lookups in the declared type which were thought to be
16045 dependent, but are not in the scope of the declarator. */
16046 decl_specifiers
->type
16047 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
16049 /* If we're allowing GNU extensions, look for an
16050 asm-specification. */
16051 if (cp_parser_allow_gnu_extensions_p (parser
))
16053 /* Look for an asm-specification. */
16054 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
16055 asm_specification
= cp_parser_asm_specification_opt (parser
);
16058 asm_specification
= NULL_TREE
;
16060 /* Look for attributes. */
16061 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
16062 attributes
= cp_parser_attributes_opt (parser
);
16064 /* Peek at the next token. */
16065 token
= cp_lexer_peek_token (parser
->lexer
);
16066 /* Check to see if the token indicates the start of a
16067 function-definition. */
16068 if (function_declarator_p (declarator
)
16069 && cp_parser_token_starts_function_definition_p (token
))
16071 if (!function_definition_allowed_p
)
16073 /* If a function-definition should not appear here, issue an
16075 cp_parser_error (parser
,
16076 "a function-definition is not allowed here");
16077 return error_mark_node
;
16081 location_t func_brace_location
16082 = cp_lexer_peek_token (parser
->lexer
)->location
;
16084 /* Neither attributes nor an asm-specification are allowed
16085 on a function-definition. */
16086 if (asm_specification
)
16087 error_at (asm_spec_start_token
->location
,
16088 "an asm-specification is not allowed "
16089 "on a function-definition");
16091 error_at (attributes_start_token
->location
,
16092 "attributes are not allowed on a function-definition");
16093 /* This is a function-definition. */
16094 *function_definition_p
= true;
16096 /* Parse the function definition. */
16098 decl
= cp_parser_save_member_function_body (parser
,
16101 prefix_attributes
);
16104 = (cp_parser_function_definition_from_specifiers_and_declarator
16105 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
16107 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
16109 /* This is where the prologue starts... */
16110 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
16111 = func_brace_location
;
16120 Only in function declarations for constructors, destructors, and
16121 type conversions can the decl-specifier-seq be omitted.
16123 We explicitly postpone this check past the point where we handle
16124 function-definitions because we tolerate function-definitions
16125 that are missing their return types in some modes. */
16126 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
16128 cp_parser_error (parser
,
16129 "expected constructor, destructor, or type conversion");
16130 return error_mark_node
;
16133 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16134 if (token
->type
== CPP_EQ
16135 || token
->type
== CPP_OPEN_PAREN
16136 || token
->type
== CPP_OPEN_BRACE
)
16138 is_initialized
= SD_INITIALIZED
;
16139 initialization_kind
= token
->type
;
16140 if (maybe_range_for_decl
)
16141 *maybe_range_for_decl
= error_mark_node
;
16143 if (token
->type
== CPP_EQ
16144 && function_declarator_p (declarator
))
16146 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16147 if (t2
->keyword
== RID_DEFAULT
)
16148 is_initialized
= SD_DEFAULTED
;
16149 else if (t2
->keyword
== RID_DELETE
)
16150 is_initialized
= SD_DELETED
;
16155 /* If the init-declarator isn't initialized and isn't followed by a
16156 `,' or `;', it's not a valid init-declarator. */
16157 if (token
->type
!= CPP_COMMA
16158 && token
->type
!= CPP_SEMICOLON
)
16160 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
16161 range_for_decl_p
= true;
16164 cp_parser_error (parser
, "expected initializer");
16165 return error_mark_node
;
16168 is_initialized
= SD_UNINITIALIZED
;
16169 initialization_kind
= CPP_EOF
;
16172 /* Because start_decl has side-effects, we should only call it if we
16173 know we're going ahead. By this point, we know that we cannot
16174 possibly be looking at any other construct. */
16175 cp_parser_commit_to_tentative_parse (parser
);
16177 /* If the decl specifiers were bad, issue an error now that we're
16178 sure this was intended to be a declarator. Then continue
16179 declaring the variable(s), as int, to try to cut down on further
16181 if (decl_specifiers
->any_specifiers_p
16182 && decl_specifiers
->type
== error_mark_node
)
16184 cp_parser_error (parser
, "invalid type in declaration");
16185 decl_specifiers
->type
= integer_type_node
;
16188 /* Check to see whether or not this declaration is a friend. */
16189 friend_p
= cp_parser_friend_p (decl_specifiers
);
16191 /* Enter the newly declared entry in the symbol table. If we're
16192 processing a declaration in a class-specifier, we wait until
16193 after processing the initializer. */
16196 if (parser
->in_unbraced_linkage_specification_p
)
16197 decl_specifiers
->storage_class
= sc_extern
;
16198 decl
= start_decl (declarator
, decl_specifiers
,
16199 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
16200 attributes
, prefix_attributes
,
16202 /* Adjust location of decl if declarator->id_loc is more appropriate:
16203 set, and decl wasn't merged with another decl, in which case its
16204 location would be different from input_location, and more accurate. */
16206 && declarator
->id_loc
!= UNKNOWN_LOCATION
16207 && DECL_SOURCE_LOCATION (decl
) == input_location
)
16208 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
16211 /* Enter the SCOPE. That way unqualified names appearing in the
16212 initializer will be looked up in SCOPE. */
16213 pushed_scope
= push_scope (scope
);
16215 /* Perform deferred access control checks, now that we know in which
16216 SCOPE the declared entity resides. */
16217 if (!member_p
&& decl
)
16219 tree saved_current_function_decl
= NULL_TREE
;
16221 /* If the entity being declared is a function, pretend that we
16222 are in its scope. If it is a `friend', it may have access to
16223 things that would not otherwise be accessible. */
16224 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16226 saved_current_function_decl
= current_function_decl
;
16227 current_function_decl
= decl
;
16230 /* Perform access checks for template parameters. */
16231 cp_parser_perform_template_parameter_access_checks (checks
);
16233 /* Perform the access control checks for the declarator and the
16234 decl-specifiers. */
16235 perform_deferred_access_checks (tf_warning_or_error
);
16237 /* Restore the saved value. */
16238 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16239 current_function_decl
= saved_current_function_decl
;
16242 /* Parse the initializer. */
16243 initializer
= NULL_TREE
;
16244 is_direct_init
= false;
16245 is_non_constant_init
= true;
16246 if (is_initialized
)
16248 if (function_declarator_p (declarator
))
16250 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
16251 if (initialization_kind
== CPP_EQ
)
16252 initializer
= cp_parser_pure_specifier (parser
);
16255 /* If the declaration was erroneous, we don't really
16256 know what the user intended, so just silently
16257 consume the initializer. */
16258 if (decl
!= error_mark_node
)
16259 error_at (initializer_start_token
->location
,
16260 "initializer provided for function");
16261 cp_parser_skip_to_closing_parenthesis (parser
,
16262 /*recovering=*/true,
16263 /*or_comma=*/false,
16264 /*consume_paren=*/true);
16269 /* We want to record the extra mangling scope for in-class
16270 initializers of class members and initializers of static data
16271 member templates. The former involves deferring
16272 parsing of the initializer until end of class as with default
16273 arguments. So right here we only handle the latter. */
16274 if (!member_p
&& processing_template_decl
)
16275 start_lambda_scope (decl
);
16276 initializer
= cp_parser_initializer (parser
,
16278 &is_non_constant_init
);
16279 if (!member_p
&& processing_template_decl
)
16280 finish_lambda_scope ();
16281 if (initializer
== error_mark_node
)
16282 cp_parser_skip_to_end_of_statement (parser
);
16286 /* The old parser allows attributes to appear after a parenthesized
16287 initializer. Mark Mitchell proposed removing this functionality
16288 on the GCC mailing lists on 2002-08-13. This parser accepts the
16289 attributes -- but ignores them. */
16290 if (cp_parser_allow_gnu_extensions_p (parser
)
16291 && initialization_kind
== CPP_OPEN_PAREN
)
16292 if (cp_parser_attributes_opt (parser
))
16293 warning (OPT_Wattributes
,
16294 "attributes after parenthesized initializer ignored");
16296 /* For an in-class declaration, use `grokfield' to create the
16302 pop_scope (pushed_scope
);
16303 pushed_scope
= NULL_TREE
;
16305 decl
= grokfield (declarator
, decl_specifiers
,
16306 initializer
, !is_non_constant_init
,
16307 /*asmspec=*/NULL_TREE
,
16308 prefix_attributes
);
16309 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16310 cp_parser_save_default_args (parser
, decl
);
16313 /* Finish processing the declaration. But, skip member
16315 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
16317 cp_finish_decl (decl
,
16318 initializer
, !is_non_constant_init
,
16320 /* If the initializer is in parentheses, then this is
16321 a direct-initialization, which means that an
16322 `explicit' constructor is OK. Otherwise, an
16323 `explicit' constructor cannot be used. */
16324 ((is_direct_init
|| !is_initialized
)
16325 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
16327 else if ((cxx_dialect
!= cxx98
) && friend_p
16328 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16329 /* Core issue #226 (C++0x only): A default template-argument
16330 shall not be specified in a friend class template
16332 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
16333 /*is_partial=*/false, /*is_friend_decl=*/1);
16335 if (!friend_p
&& pushed_scope
)
16336 pop_scope (pushed_scope
);
16341 /* Parse a declarator.
16345 ptr-operator declarator
16347 abstract-declarator:
16348 ptr-operator abstract-declarator [opt]
16349 direct-abstract-declarator
16354 attributes [opt] direct-declarator
16355 attributes [opt] ptr-operator declarator
16357 abstract-declarator:
16358 attributes [opt] ptr-operator abstract-declarator [opt]
16359 attributes [opt] direct-abstract-declarator
16361 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16362 detect constructor, destructor or conversion operators. It is set
16363 to -1 if the declarator is a name, and +1 if it is a
16364 function. Otherwise it is set to zero. Usually you just want to
16365 test for >0, but internally the negative value is used.
16367 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16368 a decl-specifier-seq unless it declares a constructor, destructor,
16369 or conversion. It might seem that we could check this condition in
16370 semantic analysis, rather than parsing, but that makes it difficult
16371 to handle something like `f()'. We want to notice that there are
16372 no decl-specifiers, and therefore realize that this is an
16373 expression, not a declaration.)
16375 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16376 the declarator is a direct-declarator of the form "(...)".
16378 MEMBER_P is true iff this declarator is a member-declarator. */
16380 static cp_declarator
*
16381 cp_parser_declarator (cp_parser
* parser
,
16382 cp_parser_declarator_kind dcl_kind
,
16383 int* ctor_dtor_or_conv_p
,
16384 bool* parenthesized_p
,
16387 cp_declarator
*declarator
;
16388 enum tree_code code
;
16389 cp_cv_quals cv_quals
;
16391 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
16393 /* Assume this is not a constructor, destructor, or type-conversion
16395 if (ctor_dtor_or_conv_p
)
16396 *ctor_dtor_or_conv_p
= 0;
16398 if (cp_parser_allow_gnu_extensions_p (parser
))
16399 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
16401 /* Check for the ptr-operator production. */
16402 cp_parser_parse_tentatively (parser
);
16403 /* Parse the ptr-operator. */
16404 code
= cp_parser_ptr_operator (parser
,
16409 /* If that worked, then we have a ptr-operator. */
16410 if (cp_parser_parse_definitely (parser
))
16412 /* If a ptr-operator was found, then this declarator was not
16414 if (parenthesized_p
)
16415 *parenthesized_p
= true;
16416 /* The dependent declarator is optional if we are parsing an
16417 abstract-declarator. */
16418 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16419 cp_parser_parse_tentatively (parser
);
16421 /* Parse the dependent declarator. */
16422 declarator
= cp_parser_declarator (parser
, dcl_kind
,
16423 /*ctor_dtor_or_conv_p=*/NULL
,
16424 /*parenthesized_p=*/NULL
,
16425 /*member_p=*/false);
16427 /* If we are parsing an abstract-declarator, we must handle the
16428 case where the dependent declarator is absent. */
16429 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
16430 && !cp_parser_parse_definitely (parser
))
16433 declarator
= cp_parser_make_indirect_declarator
16434 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
16436 /* Everything else is a direct-declarator. */
16439 if (parenthesized_p
)
16440 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
16442 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
16443 ctor_dtor_or_conv_p
,
16447 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
16448 declarator
->attributes
= gnu_attributes
;
16452 /* Parse a direct-declarator or direct-abstract-declarator.
16456 direct-declarator ( parameter-declaration-clause )
16457 cv-qualifier-seq [opt]
16458 ref-qualifier [opt]
16459 exception-specification [opt]
16460 direct-declarator [ constant-expression [opt] ]
16463 direct-abstract-declarator:
16464 direct-abstract-declarator [opt]
16465 ( parameter-declaration-clause )
16466 cv-qualifier-seq [opt]
16467 ref-qualifier [opt]
16468 exception-specification [opt]
16469 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16470 ( abstract-declarator )
16472 Returns a representation of the declarator. DCL_KIND is
16473 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16474 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16475 we are parsing a direct-declarator. It is
16476 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16477 of ambiguity we prefer an abstract declarator, as per
16478 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16479 cp_parser_declarator. */
16481 static cp_declarator
*
16482 cp_parser_direct_declarator (cp_parser
* parser
,
16483 cp_parser_declarator_kind dcl_kind
,
16484 int* ctor_dtor_or_conv_p
,
16488 cp_declarator
*declarator
= NULL
;
16489 tree scope
= NULL_TREE
;
16490 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16491 bool saved_in_declarator_p
= parser
->in_declarator_p
;
16493 tree pushed_scope
= NULL_TREE
;
16497 /* Peek at the next token. */
16498 token
= cp_lexer_peek_token (parser
->lexer
);
16499 if (token
->type
== CPP_OPEN_PAREN
)
16501 /* This is either a parameter-declaration-clause, or a
16502 parenthesized declarator. When we know we are parsing a
16503 named declarator, it must be a parenthesized declarator
16504 if FIRST is true. For instance, `(int)' is a
16505 parameter-declaration-clause, with an omitted
16506 direct-abstract-declarator. But `((*))', is a
16507 parenthesized abstract declarator. Finally, when T is a
16508 template parameter `(T)' is a
16509 parameter-declaration-clause, and not a parenthesized
16512 We first try and parse a parameter-declaration-clause,
16513 and then try a nested declarator (if FIRST is true).
16515 It is not an error for it not to be a
16516 parameter-declaration-clause, even when FIRST is
16522 The first is the declaration of a function while the
16523 second is the definition of a variable, including its
16526 Having seen only the parenthesis, we cannot know which of
16527 these two alternatives should be selected. Even more
16528 complex are examples like:
16533 The former is a function-declaration; the latter is a
16534 variable initialization.
16536 Thus again, we try a parameter-declaration-clause, and if
16537 that fails, we back out and return. */
16539 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16542 unsigned saved_num_template_parameter_lists
;
16543 bool is_declarator
= false;
16546 /* In a member-declarator, the only valid interpretation
16547 of a parenthesis is the start of a
16548 parameter-declaration-clause. (It is invalid to
16549 initialize a static data member with a parenthesized
16550 initializer; only the "=" form of initialization is
16553 cp_parser_parse_tentatively (parser
);
16555 /* Consume the `('. */
16556 cp_lexer_consume_token (parser
->lexer
);
16559 /* If this is going to be an abstract declarator, we're
16560 in a declarator and we can't have default args. */
16561 parser
->default_arg_ok_p
= false;
16562 parser
->in_declarator_p
= true;
16565 /* Inside the function parameter list, surrounding
16566 template-parameter-lists do not apply. */
16567 saved_num_template_parameter_lists
16568 = parser
->num_template_parameter_lists
;
16569 parser
->num_template_parameter_lists
= 0;
16571 begin_scope (sk_function_parms
, NULL_TREE
);
16573 /* Parse the parameter-declaration-clause. */
16574 params
= cp_parser_parameter_declaration_clause (parser
);
16576 parser
->num_template_parameter_lists
16577 = saved_num_template_parameter_lists
;
16579 /* Consume the `)'. */
16580 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
16582 /* If all went well, parse the cv-qualifier-seq,
16583 ref-qualifier and the exception-specification. */
16584 if (member_p
|| cp_parser_parse_definitely (parser
))
16586 cp_cv_quals cv_quals
;
16587 cp_virt_specifiers virt_specifiers
;
16588 cp_ref_qualifier ref_qual
;
16589 tree exception_specification
;
16592 bool memfn
= (member_p
|| (pushed_scope
16593 && CLASS_TYPE_P (pushed_scope
)));
16595 is_declarator
= true;
16597 if (ctor_dtor_or_conv_p
)
16598 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
16601 /* Parse the cv-qualifier-seq. */
16602 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
16603 /* Parse the ref-qualifier. */
16604 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
16605 /* And the exception-specification. */
16606 exception_specification
16607 = cp_parser_exception_specification_opt (parser
);
16609 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16611 late_return
= (cp_parser_late_return_type_opt
16612 (parser
, memfn
? cv_quals
: -1));
16614 /* Parse the virt-specifier-seq. */
16615 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
16617 /* Create the function-declarator. */
16618 declarator
= make_call_declarator (declarator
,
16623 exception_specification
,
16625 declarator
->std_attributes
= attrs
;
16626 /* Any subsequent parameter lists are to do with
16627 return type, so are not those of the declared
16629 parser
->default_arg_ok_p
= false;
16632 /* Remove the function parms from scope. */
16633 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
16634 pop_binding (DECL_NAME (t
), t
);
16638 /* Repeat the main loop. */
16642 /* If this is the first, we can try a parenthesized
16646 bool saved_in_type_id_in_expr_p
;
16648 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16649 parser
->in_declarator_p
= saved_in_declarator_p
;
16651 /* Consume the `('. */
16652 cp_lexer_consume_token (parser
->lexer
);
16653 /* Parse the nested declarator. */
16654 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16655 parser
->in_type_id_in_expr_p
= true;
16657 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
16658 /*parenthesized_p=*/NULL
,
16660 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16662 /* Expect a `)'. */
16663 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
16664 declarator
= cp_error_declarator
;
16665 if (declarator
== cp_error_declarator
)
16668 goto handle_declarator
;
16670 /* Otherwise, we must be done. */
16674 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16675 && token
->type
== CPP_OPEN_SQUARE
16676 && !cp_next_tokens_can_be_attribute_p (parser
))
16678 /* Parse an array-declarator. */
16679 tree bounds
, attrs
;
16681 if (ctor_dtor_or_conv_p
)
16682 *ctor_dtor_or_conv_p
= 0;
16685 parser
->default_arg_ok_p
= false;
16686 parser
->in_declarator_p
= true;
16687 /* Consume the `['. */
16688 cp_lexer_consume_token (parser
->lexer
);
16689 /* Peek at the next token. */
16690 token
= cp_lexer_peek_token (parser
->lexer
);
16691 /* If the next token is `]', then there is no
16692 constant-expression. */
16693 if (token
->type
!= CPP_CLOSE_SQUARE
)
16695 bool non_constant_p
;
16698 = cp_parser_constant_expression (parser
,
16699 /*allow_non_constant=*/true,
16701 if (!non_constant_p
)
16703 else if (error_operand_p (bounds
))
16704 /* Already gave an error. */;
16705 else if (!parser
->in_function_body
16706 || current_binding_level
->kind
== sk_function_parms
)
16708 /* Normally, the array bound must be an integral constant
16709 expression. However, as an extension, we allow VLAs
16710 in function scopes as long as they aren't part of a
16711 parameter declaration. */
16712 cp_parser_error (parser
,
16713 "array bound is not an integer constant");
16714 bounds
= error_mark_node
;
16716 else if (processing_template_decl
)
16718 /* Remember this wasn't a constant-expression. */
16719 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
16720 TREE_SIDE_EFFECTS (bounds
) = 1;
16724 bounds
= NULL_TREE
;
16725 /* Look for the closing `]'. */
16726 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
16728 declarator
= cp_error_declarator
;
16732 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16733 declarator
= make_array_declarator (declarator
, bounds
);
16734 declarator
->std_attributes
= attrs
;
16736 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
16739 tree qualifying_scope
;
16740 tree unqualified_name
;
16742 special_function_kind sfk
;
16744 bool pack_expansion_p
= false;
16745 cp_token
*declarator_id_start_token
;
16747 /* Parse a declarator-id */
16748 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
16751 cp_parser_parse_tentatively (parser
);
16753 /* If we see an ellipsis, we should be looking at a
16755 if (token
->type
== CPP_ELLIPSIS
)
16757 /* Consume the `...' */
16758 cp_lexer_consume_token (parser
->lexer
);
16760 pack_expansion_p
= true;
16764 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
16766 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
16767 qualifying_scope
= parser
->scope
;
16772 if (!unqualified_name
&& pack_expansion_p
)
16774 /* Check whether an error occurred. */
16775 okay
= !cp_parser_error_occurred (parser
);
16777 /* We already consumed the ellipsis to mark a
16778 parameter pack, but we have no way to report it,
16779 so abort the tentative parse. We will be exiting
16780 immediately anyway. */
16781 cp_parser_abort_tentative_parse (parser
);
16784 okay
= cp_parser_parse_definitely (parser
);
16787 unqualified_name
= error_mark_node
;
16788 else if (unqualified_name
16789 && (qualifying_scope
16790 || (!identifier_p (unqualified_name
))))
16792 cp_parser_error (parser
, "expected unqualified-id");
16793 unqualified_name
= error_mark_node
;
16797 if (!unqualified_name
)
16799 if (unqualified_name
== error_mark_node
)
16801 declarator
= cp_error_declarator
;
16802 pack_expansion_p
= false;
16803 declarator
->parameter_pack_p
= false;
16807 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16809 if (qualifying_scope
&& at_namespace_scope_p ()
16810 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
16812 /* In the declaration of a member of a template class
16813 outside of the class itself, the SCOPE will sometimes
16814 be a TYPENAME_TYPE. For example, given:
16816 template <typename T>
16817 int S<T>::R::i = 3;
16819 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16820 this context, we must resolve S<T>::R to an ordinary
16821 type, rather than a typename type.
16823 The reason we normally avoid resolving TYPENAME_TYPEs
16824 is that a specialization of `S' might render
16825 `S<T>::R' not a type. However, if `S' is
16826 specialized, then this `i' will not be used, so there
16827 is no harm in resolving the types here. */
16830 /* Resolve the TYPENAME_TYPE. */
16831 type
= resolve_typename_type (qualifying_scope
,
16832 /*only_current_p=*/false);
16833 /* If that failed, the declarator is invalid. */
16834 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16836 if (typedef_variant_p (type
))
16837 error_at (declarator_id_start_token
->location
,
16838 "cannot define member of dependent typedef "
16841 error_at (declarator_id_start_token
->location
,
16842 "%<%T::%E%> is not a type",
16843 TYPE_CONTEXT (qualifying_scope
),
16844 TYPE_IDENTIFIER (qualifying_scope
));
16846 qualifying_scope
= type
;
16851 if (unqualified_name
)
16855 if (qualifying_scope
16856 && CLASS_TYPE_P (qualifying_scope
))
16857 class_type
= qualifying_scope
;
16859 class_type
= current_class_type
;
16861 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
16863 tree name_type
= TREE_TYPE (unqualified_name
);
16864 if (class_type
&& same_type_p (name_type
, class_type
))
16866 if (qualifying_scope
16867 && CLASSTYPE_USE_TEMPLATE (name_type
))
16869 error_at (declarator_id_start_token
->location
,
16870 "invalid use of constructor as a template");
16871 inform (declarator_id_start_token
->location
,
16872 "use %<%T::%D%> instead of %<%T::%D%> to "
16873 "name the constructor in a qualified name",
16875 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
16876 class_type
, name_type
);
16877 declarator
= cp_error_declarator
;
16881 unqualified_name
= constructor_name (class_type
);
16885 /* We do not attempt to print the declarator
16886 here because we do not have enough
16887 information about its original syntactic
16889 cp_parser_error (parser
, "invalid declarator");
16890 declarator
= cp_error_declarator
;
16897 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
16898 sfk
= sfk_destructor
;
16899 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
16900 sfk
= sfk_conversion
;
16901 else if (/* There's no way to declare a constructor
16902 for an anonymous type, even if the type
16903 got a name for linkage purposes. */
16904 !TYPE_WAS_ANONYMOUS (class_type
)
16905 && constructor_name_p (unqualified_name
,
16908 unqualified_name
= constructor_name (class_type
);
16909 sfk
= sfk_constructor
;
16911 else if (is_overloaded_fn (unqualified_name
)
16912 && DECL_CONSTRUCTOR_P (get_first_fn
16913 (unqualified_name
)))
16914 sfk
= sfk_constructor
;
16916 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
16917 *ctor_dtor_or_conv_p
= -1;
16920 declarator
= make_id_declarator (qualifying_scope
,
16923 declarator
->std_attributes
= attrs
;
16924 declarator
->id_loc
= token
->location
;
16925 declarator
->parameter_pack_p
= pack_expansion_p
;
16927 if (pack_expansion_p
)
16928 maybe_warn_variadic_templates ();
16931 handle_declarator
:;
16932 scope
= get_scope_of_declarator (declarator
);
16935 /* Any names that appear after the declarator-id for a
16936 member are looked up in the containing scope. */
16937 if (at_function_scope_p ())
16939 /* But declarations with qualified-ids can't appear in a
16941 cp_parser_error (parser
, "qualified-id in declaration");
16944 pushed_scope
= push_scope (scope
);
16946 parser
->in_declarator_p
= true;
16947 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
16948 || (declarator
&& declarator
->kind
== cdk_id
))
16949 /* Default args are only allowed on function
16951 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16953 parser
->default_arg_ok_p
= false;
16962 /* For an abstract declarator, we might wind up with nothing at this
16963 point. That's an error; the declarator is not optional. */
16965 cp_parser_error (parser
, "expected declarator");
16967 /* If we entered a scope, we must exit it now. */
16969 pop_scope (pushed_scope
);
16971 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16972 parser
->in_declarator_p
= saved_in_declarator_p
;
16977 /* Parse a ptr-operator.
16980 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16981 * cv-qualifier-seq [opt]
16983 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16984 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16989 & cv-qualifier-seq [opt]
16991 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16992 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16993 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16994 filled in with the TYPE containing the member. *CV_QUALS is
16995 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16996 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16997 Note that the tree codes returned by this function have nothing
16998 to do with the types of trees that will be eventually be created
16999 to represent the pointer or reference type being parsed. They are
17000 just constants with suggestive names. */
17001 static enum tree_code
17002 cp_parser_ptr_operator (cp_parser
* parser
,
17004 cp_cv_quals
*cv_quals
,
17007 enum tree_code code
= ERROR_MARK
;
17009 tree attrs
= NULL_TREE
;
17011 /* Assume that it's not a pointer-to-member. */
17013 /* And that there are no cv-qualifiers. */
17014 *cv_quals
= TYPE_UNQUALIFIED
;
17016 /* Peek at the next token. */
17017 token
= cp_lexer_peek_token (parser
->lexer
);
17019 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17020 if (token
->type
== CPP_MULT
)
17021 code
= INDIRECT_REF
;
17022 else if (token
->type
== CPP_AND
)
17024 else if ((cxx_dialect
!= cxx98
) &&
17025 token
->type
== CPP_AND_AND
) /* C++0x only */
17026 code
= NON_LVALUE_EXPR
;
17028 if (code
!= ERROR_MARK
)
17030 /* Consume the `*', `&' or `&&'. */
17031 cp_lexer_consume_token (parser
->lexer
);
17033 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17034 `&', if we are allowing GNU extensions. (The only qualifier
17035 that can legally appear after `&' is `restrict', but that is
17036 enforced during semantic analysis. */
17037 if (code
== INDIRECT_REF
17038 || cp_parser_allow_gnu_extensions_p (parser
))
17039 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17041 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17042 if (attributes
!= NULL
)
17043 *attributes
= attrs
;
17047 /* Try the pointer-to-member case. */
17048 cp_parser_parse_tentatively (parser
);
17049 /* Look for the optional `::' operator. */
17050 cp_parser_global_scope_opt (parser
,
17051 /*current_scope_valid_p=*/false);
17052 /* Look for the nested-name specifier. */
17053 token
= cp_lexer_peek_token (parser
->lexer
);
17054 cp_parser_nested_name_specifier (parser
,
17055 /*typename_keyword_p=*/false,
17056 /*check_dependency_p=*/true,
17058 /*is_declaration=*/false);
17059 /* If we found it, and the next token is a `*', then we are
17060 indeed looking at a pointer-to-member operator. */
17061 if (!cp_parser_error_occurred (parser
)
17062 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
17064 /* Indicate that the `*' operator was used. */
17065 code
= INDIRECT_REF
;
17067 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
17068 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
17069 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
17070 error_at (token
->location
, "cannot form pointer to member of "
17071 "non-class %q#T", parser
->scope
);
17074 /* The type of which the member is a member is given by the
17076 *type
= parser
->scope
;
17077 /* The next name will not be qualified. */
17078 parser
->scope
= NULL_TREE
;
17079 parser
->qualifying_scope
= NULL_TREE
;
17080 parser
->object_scope
= NULL_TREE
;
17081 /* Look for optional c++11 attributes. */
17082 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17083 if (attributes
!= NULL
)
17084 *attributes
= attrs
;
17085 /* Look for the optional cv-qualifier-seq. */
17086 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17089 /* If that didn't work we don't have a ptr-operator. */
17090 if (!cp_parser_parse_definitely (parser
))
17091 cp_parser_error (parser
, "expected ptr-operator");
17097 /* Parse an (optional) cv-qualifier-seq.
17100 cv-qualifier cv-qualifier-seq [opt]
17111 Returns a bitmask representing the cv-qualifiers. */
17114 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
17116 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
17121 cp_cv_quals cv_qualifier
;
17123 /* Peek at the next token. */
17124 token
= cp_lexer_peek_token (parser
->lexer
);
17125 /* See if it's a cv-qualifier. */
17126 switch (token
->keyword
)
17129 cv_qualifier
= TYPE_QUAL_CONST
;
17133 cv_qualifier
= TYPE_QUAL_VOLATILE
;
17137 cv_qualifier
= TYPE_QUAL_RESTRICT
;
17141 cv_qualifier
= TYPE_UNQUALIFIED
;
17148 if (cv_quals
& cv_qualifier
)
17150 error_at (token
->location
, "duplicate cv-qualifier");
17151 cp_lexer_purge_token (parser
->lexer
);
17155 cp_lexer_consume_token (parser
->lexer
);
17156 cv_quals
|= cv_qualifier
;
17163 /* Parse an (optional) ref-qualifier
17169 Returns cp_ref_qualifier representing ref-qualifier. */
17171 static cp_ref_qualifier
17172 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
17174 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
17178 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
17179 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17181 switch (token
->type
)
17184 curr_ref_qual
= REF_QUAL_LVALUE
;
17188 curr_ref_qual
= REF_QUAL_RVALUE
;
17192 curr_ref_qual
= REF_QUAL_NONE
;
17196 if (!curr_ref_qual
)
17200 error_at (token
->location
, "multiple ref-qualifiers");
17201 cp_lexer_purge_token (parser
->lexer
);
17205 ref_qual
= curr_ref_qual
;
17206 cp_lexer_consume_token (parser
->lexer
);
17213 /* Parse an (optional) virt-specifier-seq.
17215 virt-specifier-seq:
17216 virt-specifier virt-specifier-seq [opt]
17222 Returns a bitmask representing the virt-specifiers. */
17224 static cp_virt_specifiers
17225 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
17227 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
17232 cp_virt_specifiers virt_specifier
;
17234 /* Peek at the next token. */
17235 token
= cp_lexer_peek_token (parser
->lexer
);
17236 /* See if it's a virt-specifier-qualifier. */
17237 if (token
->type
!= CPP_NAME
)
17239 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
17241 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17242 virt_specifier
= VIRT_SPEC_OVERRIDE
;
17244 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
17246 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
17247 virt_specifier
= VIRT_SPEC_FINAL
;
17249 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
17251 virt_specifier
= VIRT_SPEC_FINAL
;
17256 if (virt_specifiers
& virt_specifier
)
17258 error_at (token
->location
, "duplicate virt-specifier");
17259 cp_lexer_purge_token (parser
->lexer
);
17263 cp_lexer_consume_token (parser
->lexer
);
17264 virt_specifiers
|= virt_specifier
;
17267 return virt_specifiers
;
17270 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17271 is in scope even though it isn't real. */
17274 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
17278 if (current_class_ptr
)
17280 /* We don't clear this between NSDMIs. Is it already what we want? */
17281 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
17282 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
17283 && cp_type_quals (type
) == quals
)
17287 this_parm
= build_this_parm (ctype
, quals
);
17288 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17289 current_class_ptr
= NULL_TREE
;
17291 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
17292 current_class_ptr
= this_parm
;
17295 /* Return true iff our current scope is a non-static data member
17299 parsing_nsdmi (void)
17301 /* We recognize NSDMI context by the context-less 'this' pointer set up
17302 by the function above. */
17303 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
17308 /* Parse a late-specified return type, if any. This is not a separate
17309 non-terminal, but part of a function declarator, which looks like
17311 -> trailing-type-specifier-seq abstract-declarator(opt)
17313 Returns the type indicated by the type-id.
17315 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17319 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_cv_quals quals
)
17324 /* Peek at the next token. */
17325 token
= cp_lexer_peek_token (parser
->lexer
);
17326 /* A late-specified return type is indicated by an initial '->'. */
17327 if (token
->type
!= CPP_DEREF
)
17330 /* Consume the ->. */
17331 cp_lexer_consume_token (parser
->lexer
);
17333 tree save_ccp
= current_class_ptr
;
17334 tree save_ccr
= current_class_ref
;
17337 /* DR 1207: 'this' is in scope in the trailing return type. */
17338 inject_this_parameter (current_class_type
, quals
);
17341 type
= cp_parser_trailing_type_id (parser
);
17345 current_class_ptr
= save_ccp
;
17346 current_class_ref
= save_ccr
;
17352 /* Parse a declarator-id.
17356 :: [opt] nested-name-specifier [opt] type-name
17358 In the `id-expression' case, the value returned is as for
17359 cp_parser_id_expression if the id-expression was an unqualified-id.
17360 If the id-expression was a qualified-id, then a SCOPE_REF is
17361 returned. The first operand is the scope (either a NAMESPACE_DECL
17362 or TREE_TYPE), but the second is still just a representation of an
17366 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
17369 /* The expression must be an id-expression. Assume that qualified
17370 names are the names of types so that:
17373 int S<T>::R::i = 3;
17375 will work; we must treat `S<T>::R' as the name of a type.
17376 Similarly, assume that qualified names are templates, where
17380 int S<T>::R<T>::i = 3;
17383 id
= cp_parser_id_expression (parser
,
17384 /*template_keyword_p=*/false,
17385 /*check_dependency_p=*/false,
17386 /*template_p=*/NULL
,
17387 /*declarator_p=*/true,
17389 if (id
&& BASELINK_P (id
))
17390 id
= BASELINK_FUNCTIONS (id
);
17394 /* Parse a type-id.
17397 type-specifier-seq abstract-declarator [opt]
17399 Returns the TYPE specified. */
17402 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
17403 bool is_trailing_return
)
17405 cp_decl_specifier_seq type_specifier_seq
;
17406 cp_declarator
*abstract_declarator
;
17408 /* Parse the type-specifier-seq. */
17409 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17410 is_trailing_return
,
17411 &type_specifier_seq
);
17412 if (type_specifier_seq
.type
== error_mark_node
)
17413 return error_mark_node
;
17415 /* There might or might not be an abstract declarator. */
17416 cp_parser_parse_tentatively (parser
);
17417 /* Look for the declarator. */
17418 abstract_declarator
17419 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
17420 /*parenthesized_p=*/NULL
,
17421 /*member_p=*/false);
17422 /* Check to see if there really was a declarator. */
17423 if (!cp_parser_parse_definitely (parser
))
17424 abstract_declarator
= NULL
;
17426 if (type_specifier_seq
.type
17427 && cxx_dialect
< cxx1y
17428 && type_uses_auto (type_specifier_seq
.type
))
17430 /* A type-id with type 'auto' is only ok if the abstract declarator
17431 is a function declarator with a late-specified return type. */
17432 if (abstract_declarator
17433 && abstract_declarator
->kind
== cdk_function
17434 && abstract_declarator
->u
.function
.late_return_type
)
17438 error ("invalid use of %<auto%>");
17439 return error_mark_node
;
17443 return groktypename (&type_specifier_seq
, abstract_declarator
,
17447 static tree
cp_parser_type_id (cp_parser
*parser
)
17449 return cp_parser_type_id_1 (parser
, false, false);
17452 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
17455 const char *saved_message
= parser
->type_definition_forbidden_message
;
17456 parser
->type_definition_forbidden_message
17457 = G_("types may not be defined in template arguments");
17458 r
= cp_parser_type_id_1 (parser
, true, false);
17459 parser
->type_definition_forbidden_message
= saved_message
;
17463 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
17465 return cp_parser_type_id_1 (parser
, false, true);
17468 /* Parse a type-specifier-seq.
17470 type-specifier-seq:
17471 type-specifier type-specifier-seq [opt]
17475 type-specifier-seq:
17476 attributes type-specifier-seq [opt]
17478 If IS_DECLARATION is true, we are at the start of a "condition" or
17479 exception-declaration, so we might be followed by a declarator-id.
17481 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17482 i.e. we've just seen "->".
17484 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17487 cp_parser_type_specifier_seq (cp_parser
* parser
,
17488 bool is_declaration
,
17489 bool is_trailing_return
,
17490 cp_decl_specifier_seq
*type_specifier_seq
)
17492 bool seen_type_specifier
= false;
17493 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
17494 cp_token
*start_token
= NULL
;
17496 /* Clear the TYPE_SPECIFIER_SEQ. */
17497 clear_decl_specs (type_specifier_seq
);
17499 /* In the context of a trailing return type, enum E { } is an
17500 elaborated-type-specifier followed by a function-body, not an
17502 if (is_trailing_return
)
17503 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
17505 /* Parse the type-specifiers and attributes. */
17508 tree type_specifier
;
17509 bool is_cv_qualifier
;
17511 /* Check for attributes first. */
17512 if (cp_next_tokens_can_be_attribute_p (parser
))
17514 type_specifier_seq
->attributes
=
17515 chainon (type_specifier_seq
->attributes
,
17516 cp_parser_attributes_opt (parser
));
17520 /* record the token of the beginning of the type specifier seq,
17521 for error reporting purposes*/
17523 start_token
= cp_lexer_peek_token (parser
->lexer
);
17525 /* Look for the type-specifier. */
17526 type_specifier
= cp_parser_type_specifier (parser
,
17528 type_specifier_seq
,
17529 /*is_declaration=*/false,
17532 if (!type_specifier
)
17534 /* If the first type-specifier could not be found, this is not a
17535 type-specifier-seq at all. */
17536 if (!seen_type_specifier
)
17538 cp_parser_error (parser
, "expected type-specifier");
17539 type_specifier_seq
->type
= error_mark_node
;
17542 /* If subsequent type-specifiers could not be found, the
17543 type-specifier-seq is complete. */
17547 seen_type_specifier
= true;
17548 /* The standard says that a condition can be:
17550 type-specifier-seq declarator = assignment-expression
17557 we should treat the "S" as a declarator, not as a
17558 type-specifier. The standard doesn't say that explicitly for
17559 type-specifier-seq, but it does say that for
17560 decl-specifier-seq in an ordinary declaration. Perhaps it
17561 would be clearer just to allow a decl-specifier-seq here, and
17562 then add a semantic restriction that if any decl-specifiers
17563 that are not type-specifiers appear, the program is invalid. */
17564 if (is_declaration
&& !is_cv_qualifier
)
17565 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
17569 /* Parse a parameter-declaration-clause.
17571 parameter-declaration-clause:
17572 parameter-declaration-list [opt] ... [opt]
17573 parameter-declaration-list , ...
17575 Returns a representation for the parameter declarations. A return
17576 value of NULL indicates a parameter-declaration-clause consisting
17577 only of an ellipsis. */
17580 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
17587 /* Peek at the next token. */
17588 token
= cp_lexer_peek_token (parser
->lexer
);
17589 /* Check for trivial parameter-declaration-clauses. */
17590 if (token
->type
== CPP_ELLIPSIS
)
17592 /* Consume the `...' token. */
17593 cp_lexer_consume_token (parser
->lexer
);
17596 else if (token
->type
== CPP_CLOSE_PAREN
)
17597 /* There are no parameters. */
17599 #ifndef NO_IMPLICIT_EXTERN_C
17600 if (in_system_header
&& current_class_type
== NULL
17601 && current_lang_name
== lang_name_c
)
17605 return void_list_node
;
17607 /* Check for `(void)', too, which is a special case. */
17608 else if (token
->keyword
== RID_VOID
17609 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17610 == CPP_CLOSE_PAREN
))
17612 /* Consume the `void' token. */
17613 cp_lexer_consume_token (parser
->lexer
);
17614 /* There are no parameters. */
17615 return void_list_node
;
17618 /* Parse the parameter-declaration-list. */
17619 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
17620 /* If a parse error occurred while parsing the
17621 parameter-declaration-list, then the entire
17622 parameter-declaration-clause is erroneous. */
17626 /* Peek at the next token. */
17627 token
= cp_lexer_peek_token (parser
->lexer
);
17628 /* If it's a `,', the clause should terminate with an ellipsis. */
17629 if (token
->type
== CPP_COMMA
)
17631 /* Consume the `,'. */
17632 cp_lexer_consume_token (parser
->lexer
);
17633 /* Expect an ellipsis. */
17635 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
17637 /* It might also be `...' if the optional trailing `,' was
17639 else if (token
->type
== CPP_ELLIPSIS
)
17641 /* Consume the `...' token. */
17642 cp_lexer_consume_token (parser
->lexer
);
17643 /* And remember that we saw it. */
17647 ellipsis_p
= false;
17649 /* Finish the parameter list. */
17651 parameters
= chainon (parameters
, void_list_node
);
17656 /* Parse a parameter-declaration-list.
17658 parameter-declaration-list:
17659 parameter-declaration
17660 parameter-declaration-list , parameter-declaration
17662 Returns a representation of the parameter-declaration-list, as for
17663 cp_parser_parameter_declaration_clause. However, the
17664 `void_list_node' is never appended to the list. Upon return,
17665 *IS_ERROR will be true iff an error occurred. */
17668 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
17670 tree parameters
= NULL_TREE
;
17671 tree
*tail
= ¶meters
;
17672 bool saved_in_unbraced_linkage_specification_p
;
17675 /* Assume all will go well. */
17677 /* The special considerations that apply to a function within an
17678 unbraced linkage specifications do not apply to the parameters
17679 to the function. */
17680 saved_in_unbraced_linkage_specification_p
17681 = parser
->in_unbraced_linkage_specification_p
;
17682 parser
->in_unbraced_linkage_specification_p
= false;
17684 /* Look for more parameters. */
17687 cp_parameter_declarator
*parameter
;
17688 tree decl
= error_mark_node
;
17689 bool parenthesized_p
= false;
17690 /* Parse the parameter. */
17692 = cp_parser_parameter_declaration (parser
,
17693 /*template_parm_p=*/false,
17696 /* We don't know yet if the enclosing context is deprecated, so wait
17697 and warn in grokparms if appropriate. */
17698 deprecated_state
= DEPRECATED_SUPPRESS
;
17701 decl
= grokdeclarator (parameter
->declarator
,
17702 ¶meter
->decl_specifiers
,
17704 parameter
->default_argument
!= NULL_TREE
,
17705 ¶meter
->decl_specifiers
.attributes
);
17707 deprecated_state
= DEPRECATED_NORMAL
;
17709 /* If a parse error occurred parsing the parameter declaration,
17710 then the entire parameter-declaration-list is erroneous. */
17711 if (decl
== error_mark_node
)
17714 parameters
= error_mark_node
;
17718 if (parameter
->decl_specifiers
.attributes
)
17719 cplus_decl_attributes (&decl
,
17720 parameter
->decl_specifiers
.attributes
,
17722 if (DECL_NAME (decl
))
17723 decl
= pushdecl (decl
);
17725 if (decl
!= error_mark_node
)
17727 retrofit_lang_decl (decl
);
17728 DECL_PARM_INDEX (decl
) = ++index
;
17729 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
17732 /* Add the new parameter to the list. */
17733 *tail
= build_tree_list (parameter
->default_argument
, decl
);
17734 tail
= &TREE_CHAIN (*tail
);
17736 /* Peek at the next token. */
17737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
17738 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
17739 /* These are for Objective-C++ */
17740 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17741 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17742 /* The parameter-declaration-list is complete. */
17744 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17748 /* Peek at the next token. */
17749 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17750 /* If it's an ellipsis, then the list is complete. */
17751 if (token
->type
== CPP_ELLIPSIS
)
17753 /* Otherwise, there must be more parameters. Consume the
17755 cp_lexer_consume_token (parser
->lexer
);
17756 /* When parsing something like:
17758 int i(float f, double d)
17760 we can tell after seeing the declaration for "f" that we
17761 are not looking at an initialization of a variable "i",
17762 but rather at the declaration of a function "i".
17764 Due to the fact that the parsing of template arguments
17765 (as specified to a template-id) requires backtracking we
17766 cannot use this technique when inside a template argument
17768 if (!parser
->in_template_argument_list_p
17769 && !parser
->in_type_id_in_expr_p
17770 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
17771 /* However, a parameter-declaration of the form
17772 "foat(f)" (which is a valid declaration of a
17773 parameter "f") can also be interpreted as an
17774 expression (the conversion of "f" to "float"). */
17775 && !parenthesized_p
)
17776 cp_parser_commit_to_tentative_parse (parser
);
17780 cp_parser_error (parser
, "expected %<,%> or %<...%>");
17781 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
17782 cp_parser_skip_to_closing_parenthesis (parser
,
17783 /*recovering=*/true,
17784 /*or_comma=*/false,
17785 /*consume_paren=*/false);
17790 parser
->in_unbraced_linkage_specification_p
17791 = saved_in_unbraced_linkage_specification_p
;
17796 /* Parse a parameter declaration.
17798 parameter-declaration:
17799 decl-specifier-seq ... [opt] declarator
17800 decl-specifier-seq declarator = assignment-expression
17801 decl-specifier-seq ... [opt] abstract-declarator [opt]
17802 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17804 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17805 declares a template parameter. (In that case, a non-nested `>'
17806 token encountered during the parsing of the assignment-expression
17807 is not interpreted as a greater-than operator.)
17809 Returns a representation of the parameter, or NULL if an error
17810 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17811 true iff the declarator is of the form "(p)". */
17813 static cp_parameter_declarator
*
17814 cp_parser_parameter_declaration (cp_parser
*parser
,
17815 bool template_parm_p
,
17816 bool *parenthesized_p
)
17818 int declares_class_or_enum
;
17819 cp_decl_specifier_seq decl_specifiers
;
17820 cp_declarator
*declarator
;
17821 tree default_argument
;
17822 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
17823 const char *saved_message
;
17825 /* In a template parameter, `>' is not an operator.
17829 When parsing a default template-argument for a non-type
17830 template-parameter, the first non-nested `>' is taken as the end
17831 of the template parameter-list rather than a greater-than
17834 /* Type definitions may not appear in parameter types. */
17835 saved_message
= parser
->type_definition_forbidden_message
;
17836 parser
->type_definition_forbidden_message
17837 = G_("types may not be defined in parameter types");
17839 /* Parse the declaration-specifiers. */
17840 cp_parser_decl_specifier_seq (parser
,
17841 CP_PARSER_FLAGS_NONE
,
17843 &declares_class_or_enum
);
17845 /* Complain about missing 'typename' or other invalid type names. */
17846 if (!decl_specifiers
.any_type_specifiers_p
)
17847 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
17849 /* If an error occurred, there's no reason to attempt to parse the
17850 rest of the declaration. */
17851 if (cp_parser_error_occurred (parser
))
17853 parser
->type_definition_forbidden_message
= saved_message
;
17857 /* Peek at the next token. */
17858 token
= cp_lexer_peek_token (parser
->lexer
);
17860 /* If the next token is a `)', `,', `=', `>', or `...', then there
17861 is no declarator. However, when variadic templates are enabled,
17862 there may be a declarator following `...'. */
17863 if (token
->type
== CPP_CLOSE_PAREN
17864 || token
->type
== CPP_COMMA
17865 || token
->type
== CPP_EQ
17866 || token
->type
== CPP_GREATER
)
17869 if (parenthesized_p
)
17870 *parenthesized_p
= false;
17872 /* Otherwise, there should be a declarator. */
17875 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17876 parser
->default_arg_ok_p
= false;
17878 /* After seeing a decl-specifier-seq, if the next token is not a
17879 "(", there is no possibility that the code is a valid
17880 expression. Therefore, if parsing tentatively, we commit at
17882 if (!parser
->in_template_argument_list_p
17883 /* In an expression context, having seen:
17887 we cannot be sure whether we are looking at a
17888 function-type (taking a "char" as a parameter) or a cast
17889 of some object of type "char" to "int". */
17890 && !parser
->in_type_id_in_expr_p
17891 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
17892 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
17893 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
17894 cp_parser_commit_to_tentative_parse (parser
);
17895 /* Parse the declarator. */
17896 declarator_token_start
= token
;
17897 declarator
= cp_parser_declarator (parser
,
17898 CP_PARSER_DECLARATOR_EITHER
,
17899 /*ctor_dtor_or_conv_p=*/NULL
,
17901 /*member_p=*/false);
17902 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17903 /* After the declarator, allow more attributes. */
17904 decl_specifiers
.attributes
17905 = chainon (decl_specifiers
.attributes
,
17906 cp_parser_attributes_opt (parser
));
17909 /* If the next token is an ellipsis, and we have not seen a
17910 declarator name, and the type of the declarator contains parameter
17911 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17912 a parameter pack expansion expression. Otherwise, leave the
17913 ellipsis for a C-style variadic function. */
17914 token
= cp_lexer_peek_token (parser
->lexer
);
17915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17917 tree type
= decl_specifiers
.type
;
17919 if (type
&& DECL_P (type
))
17920 type
= TREE_TYPE (type
);
17923 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
17924 && declarator_can_be_parameter_pack (declarator
)
17925 && (!declarator
|| !declarator
->parameter_pack_p
)
17926 && uses_parameter_packs (type
))
17928 /* Consume the `...'. */
17929 cp_lexer_consume_token (parser
->lexer
);
17930 maybe_warn_variadic_templates ();
17932 /* Build a pack expansion type */
17934 declarator
->parameter_pack_p
= true;
17936 decl_specifiers
.type
= make_pack_expansion (type
);
17940 /* The restriction on defining new types applies only to the type
17941 of the parameter, not to the default argument. */
17942 parser
->type_definition_forbidden_message
= saved_message
;
17944 /* If the next token is `=', then process a default argument. */
17945 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17947 token
= cp_lexer_peek_token (parser
->lexer
);
17948 /* If we are defining a class, then the tokens that make up the
17949 default argument must be saved and processed later. */
17950 if (!template_parm_p
&& at_class_scope_p ()
17951 && TYPE_BEING_DEFINED (current_class_type
)
17952 && !LAMBDA_TYPE_P (current_class_type
))
17953 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
17954 /* Outside of a class definition, we can just parse the
17955 assignment-expression. */
17958 = cp_parser_default_argument (parser
, template_parm_p
);
17960 if (!parser
->default_arg_ok_p
)
17962 if (flag_permissive
)
17963 warning (0, "deprecated use of default argument for parameter of non-function");
17966 error_at (token
->location
,
17967 "default arguments are only "
17968 "permitted for function parameters");
17969 default_argument
= NULL_TREE
;
17972 else if ((declarator
&& declarator
->parameter_pack_p
)
17973 || (decl_specifiers
.type
17974 && PACK_EXPANSION_P (decl_specifiers
.type
)))
17976 /* Find the name of the parameter pack. */
17977 cp_declarator
*id_declarator
= declarator
;
17978 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
17979 id_declarator
= id_declarator
->declarator
;
17981 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
17982 error_at (declarator_token_start
->location
,
17984 ? G_("template parameter pack %qD "
17985 "cannot have a default argument")
17986 : G_("parameter pack %qD cannot have "
17987 "a default argument"),
17988 id_declarator
->u
.id
.unqualified_name
);
17990 error_at (declarator_token_start
->location
,
17992 ? G_("template parameter pack cannot have "
17993 "a default argument")
17994 : G_("parameter pack cannot have a "
17995 "default argument"));
17997 default_argument
= NULL_TREE
;
18001 default_argument
= NULL_TREE
;
18003 return make_parameter_declarator (&decl_specifiers
,
18008 /* Parse a default argument and return it.
18010 TEMPLATE_PARM_P is true if this is a default argument for a
18011 non-type template parameter. */
18013 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
18015 tree default_argument
= NULL_TREE
;
18016 bool saved_greater_than_is_operator_p
;
18017 bool saved_local_variables_forbidden_p
;
18018 bool non_constant_p
, is_direct_init
;
18020 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18022 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
18023 parser
->greater_than_is_operator_p
= !template_parm_p
;
18024 /* Local variable names (and the `this' keyword) may not
18025 appear in a default argument. */
18026 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
18027 parser
->local_variables_forbidden_p
= true;
18028 /* Parse the assignment-expression. */
18029 if (template_parm_p
)
18030 push_deferring_access_checks (dk_no_deferred
);
18032 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
18033 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
18034 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18035 if (template_parm_p
)
18036 pop_deferring_access_checks ();
18037 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
18038 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
18040 return default_argument
;
18043 /* Parse a function-body.
18046 compound_statement */
18049 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
18051 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
18054 /* Parse a ctor-initializer-opt followed by a function-body. Return
18055 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18056 is true we are parsing a function-try-block. */
18059 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
18060 bool in_function_try_block
)
18063 bool ctor_initializer_p
;
18064 const bool check_body_p
=
18065 DECL_CONSTRUCTOR_P (current_function_decl
)
18066 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
18069 /* Begin the function body. */
18070 body
= begin_function_body ();
18071 /* Parse the optional ctor-initializer. */
18072 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
18074 /* If we're parsing a constexpr constructor definition, we need
18075 to check that the constructor body is indeed empty. However,
18076 before we get to cp_parser_function_body lot of junk has been
18077 generated, so we can't just check that we have an empty block.
18078 Rather we take a snapshot of the outermost block, and check whether
18079 cp_parser_function_body changed its state. */
18082 list
= cur_stmt_list
;
18083 if (STATEMENT_LIST_TAIL (list
))
18084 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
18086 /* Parse the function-body. */
18087 cp_parser_function_body (parser
, in_function_try_block
);
18089 check_constexpr_ctor_body (last
, list
);
18090 /* Finish the function body. */
18091 finish_function_body (body
);
18093 return ctor_initializer_p
;
18096 /* Parse an initializer.
18099 = initializer-clause
18100 ( expression-list )
18102 Returns an expression representing the initializer. If no
18103 initializer is present, NULL_TREE is returned.
18105 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18106 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18107 set to TRUE if there is no initializer present. If there is an
18108 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18109 is set to true; otherwise it is set to false. */
18112 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
18113 bool* non_constant_p
)
18118 /* Peek at the next token. */
18119 token
= cp_lexer_peek_token (parser
->lexer
);
18121 /* Let our caller know whether or not this initializer was
18123 *is_direct_init
= (token
->type
!= CPP_EQ
);
18124 /* Assume that the initializer is constant. */
18125 *non_constant_p
= false;
18127 if (token
->type
== CPP_EQ
)
18129 /* Consume the `='. */
18130 cp_lexer_consume_token (parser
->lexer
);
18131 /* Parse the initializer-clause. */
18132 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
18134 else if (token
->type
== CPP_OPEN_PAREN
)
18136 vec
<tree
, va_gc
> *vec
;
18137 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
18139 /*allow_expansion_p=*/true,
18142 return error_mark_node
;
18143 init
= build_tree_list_vec (vec
);
18144 release_tree_vector (vec
);
18146 else if (token
->type
== CPP_OPEN_BRACE
)
18148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
18149 init
= cp_parser_braced_list (parser
, non_constant_p
);
18150 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
18154 /* Anything else is an error. */
18155 cp_parser_error (parser
, "expected initializer");
18156 init
= error_mark_node
;
18162 /* Parse an initializer-clause.
18164 initializer-clause:
18165 assignment-expression
18168 Returns an expression representing the initializer.
18170 If the `assignment-expression' production is used the value
18171 returned is simply a representation for the expression.
18173 Otherwise, calls cp_parser_braced_list. */
18176 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
18180 /* Assume the expression is constant. */
18181 *non_constant_p
= false;
18183 /* If it is not a `{', then we are looking at an
18184 assignment-expression. */
18185 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
18188 = cp_parser_constant_expression (parser
,
18189 /*allow_non_constant_p=*/true,
18193 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
18195 return initializer
;
18198 /* Parse a brace-enclosed initializer list.
18201 { initializer-list , [opt] }
18204 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18205 the elements of the initializer-list (or NULL, if the last
18206 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18207 NULL_TREE. There is no way to detect whether or not the optional
18208 trailing `,' was provided. NON_CONSTANT_P is as for
18209 cp_parser_initializer. */
18212 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
18216 /* Consume the `{' token. */
18217 cp_lexer_consume_token (parser
->lexer
);
18218 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18219 initializer
= make_node (CONSTRUCTOR
);
18220 /* If it's not a `}', then there is a non-trivial initializer. */
18221 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
18223 /* Parse the initializer list. */
18224 CONSTRUCTOR_ELTS (initializer
)
18225 = cp_parser_initializer_list (parser
, non_constant_p
);
18226 /* A trailing `,' token is allowed. */
18227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18228 cp_lexer_consume_token (parser
->lexer
);
18231 *non_constant_p
= false;
18232 /* Now, there should be a trailing `}'. */
18233 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18234 TREE_TYPE (initializer
) = init_list_type_node
;
18235 return initializer
;
18238 /* Parse an initializer-list.
18241 initializer-clause ... [opt]
18242 initializer-list , initializer-clause ... [opt]
18247 designation initializer-clause ...[opt]
18248 initializer-list , designation initializer-clause ...[opt]
18253 [ constant-expression ] =
18255 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18256 for the initializer. If the INDEX of the elt is non-NULL, it is the
18257 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18258 as for cp_parser_initializer. */
18260 static vec
<constructor_elt
, va_gc
> *
18261 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
18263 vec
<constructor_elt
, va_gc
> *v
= NULL
;
18265 /* Assume all of the expressions are constant. */
18266 *non_constant_p
= false;
18268 /* Parse the rest of the list. */
18274 bool clause_non_constant_p
;
18276 /* If the next token is an identifier and the following one is a
18277 colon, we are looking at the GNU designated-initializer
18279 if (cp_parser_allow_gnu_extensions_p (parser
)
18280 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
18281 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
18283 /* Warn the user that they are using an extension. */
18284 pedwarn (input_location
, OPT_Wpedantic
,
18285 "ISO C++ does not allow designated initializers");
18286 /* Consume the identifier. */
18287 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18288 /* Consume the `:'. */
18289 cp_lexer_consume_token (parser
->lexer
);
18291 /* Also handle the C99 syntax, '. id ='. */
18292 else if (cp_parser_allow_gnu_extensions_p (parser
)
18293 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
18294 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
18295 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
18297 /* Warn the user that they are using an extension. */
18298 pedwarn (input_location
, OPT_Wpedantic
,
18299 "ISO C++ does not allow C99 designated initializers");
18300 /* Consume the `.'. */
18301 cp_lexer_consume_token (parser
->lexer
);
18302 /* Consume the identifier. */
18303 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18304 /* Consume the `='. */
18305 cp_lexer_consume_token (parser
->lexer
);
18307 /* Also handle C99 array designators, '[ const ] ='. */
18308 else if (cp_parser_allow_gnu_extensions_p (parser
)
18309 && !c_dialect_objc ()
18310 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
18312 /* In C++11, [ could start a lambda-introducer. */
18313 bool non_const
= false;
18315 cp_parser_parse_tentatively (parser
);
18316 cp_lexer_consume_token (parser
->lexer
);
18317 designator
= cp_parser_constant_expression (parser
, true, &non_const
);
18318 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
18319 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18320 if (!cp_parser_parse_definitely (parser
))
18321 designator
= NULL_TREE
;
18322 else if (non_const
)
18323 require_potential_rvalue_constant_expression (designator
);
18326 designator
= NULL_TREE
;
18328 /* Parse the initializer. */
18329 initializer
= cp_parser_initializer_clause (parser
,
18330 &clause_non_constant_p
);
18331 /* If any clause is non-constant, so is the entire initializer. */
18332 if (clause_non_constant_p
)
18333 *non_constant_p
= true;
18335 /* If we have an ellipsis, this is an initializer pack
18337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18339 /* Consume the `...'. */
18340 cp_lexer_consume_token (parser
->lexer
);
18342 /* Turn the initializer into an initializer expansion. */
18343 initializer
= make_pack_expansion (initializer
);
18346 /* Add it to the vector. */
18347 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
18349 /* If the next token is not a comma, we have reached the end of
18351 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18354 /* Peek at the next token. */
18355 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18356 /* If the next token is a `}', then we're still done. An
18357 initializer-clause can have a trailing `,' after the
18358 initializer-list and before the closing `}'. */
18359 if (token
->type
== CPP_CLOSE_BRACE
)
18362 /* Consume the `,' token. */
18363 cp_lexer_consume_token (parser
->lexer
);
18369 /* Classes [gram.class] */
18371 /* Parse a class-name.
18377 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18378 to indicate that names looked up in dependent types should be
18379 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18380 keyword has been used to indicate that the name that appears next
18381 is a template. TAG_TYPE indicates the explicit tag given before
18382 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18383 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18384 is the class being defined in a class-head.
18386 Returns the TYPE_DECL representing the class. */
18389 cp_parser_class_name (cp_parser
*parser
,
18390 bool typename_keyword_p
,
18391 bool template_keyword_p
,
18392 enum tag_types tag_type
,
18393 bool check_dependency_p
,
18395 bool is_declaration
)
18401 tree identifier
= NULL_TREE
;
18403 /* All class-names start with an identifier. */
18404 token
= cp_lexer_peek_token (parser
->lexer
);
18405 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
18407 cp_parser_error (parser
, "expected class-name");
18408 return error_mark_node
;
18411 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18412 to a template-id, so we save it here. */
18413 scope
= parser
->scope
;
18414 if (scope
== error_mark_node
)
18415 return error_mark_node
;
18417 /* Any name names a type if we're following the `typename' keyword
18418 in a qualified name where the enclosing scope is type-dependent. */
18419 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
18420 && dependent_type_p (scope
));
18421 /* Handle the common case (an identifier, but not a template-id)
18423 if (token
->type
== CPP_NAME
18424 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
18426 cp_token
*identifier_token
;
18429 /* Look for the identifier. */
18430 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
18431 ambiguous_p
= identifier_token
->ambiguous_p
;
18432 identifier
= cp_parser_identifier (parser
);
18433 /* If the next token isn't an identifier, we are certainly not
18434 looking at a class-name. */
18435 if (identifier
== error_mark_node
)
18436 decl
= error_mark_node
;
18437 /* If we know this is a type-name, there's no need to look it
18439 else if (typename_p
)
18443 tree ambiguous_decls
;
18444 /* If we already know that this lookup is ambiguous, then
18445 we've already issued an error message; there's no reason
18449 cp_parser_simulate_error (parser
);
18450 return error_mark_node
;
18452 /* If the next token is a `::', then the name must be a type
18455 [basic.lookup.qual]
18457 During the lookup for a name preceding the :: scope
18458 resolution operator, object, function, and enumerator
18459 names are ignored. */
18460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18461 tag_type
= typename_type
;
18462 /* Look up the name. */
18463 decl
= cp_parser_lookup_name (parser
, identifier
,
18465 /*is_template=*/false,
18466 /*is_namespace=*/false,
18467 check_dependency_p
,
18469 identifier_token
->location
);
18470 if (ambiguous_decls
)
18472 if (cp_parser_parsing_tentatively (parser
))
18473 cp_parser_simulate_error (parser
);
18474 return error_mark_node
;
18480 /* Try a template-id. */
18481 decl
= cp_parser_template_id (parser
, template_keyword_p
,
18482 check_dependency_p
,
18485 if (decl
== error_mark_node
)
18486 return error_mark_node
;
18489 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
18491 /* If this is a typename, create a TYPENAME_TYPE. */
18492 if (typename_p
&& decl
!= error_mark_node
)
18494 decl
= make_typename_type (scope
, decl
, typename_type
,
18495 /*complain=*/tf_error
);
18496 if (decl
!= error_mark_node
)
18497 decl
= TYPE_NAME (decl
);
18500 decl
= strip_using_decl (decl
);
18502 /* Check to see that it is really the name of a class. */
18503 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18504 && identifier_p (TREE_OPERAND (decl
, 0))
18505 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18506 /* Situations like this:
18508 template <typename T> struct A {
18509 typename T::template X<int>::I i;
18512 are problematic. Is `T::template X<int>' a class-name? The
18513 standard does not seem to be definitive, but there is no other
18514 valid interpretation of the following `::'. Therefore, those
18515 names are considered class-names. */
18517 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
18518 if (decl
!= error_mark_node
)
18519 decl
= TYPE_NAME (decl
);
18521 else if (TREE_CODE (decl
) != TYPE_DECL
18522 || TREE_TYPE (decl
) == error_mark_node
18523 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
18524 /* In Objective-C 2.0, a classname followed by '.' starts a
18525 dot-syntax expression, and it's not a type-name. */
18526 || (c_dialect_objc ()
18527 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
18528 && objc_is_class_name (decl
)))
18529 decl
= error_mark_node
;
18531 if (decl
== error_mark_node
)
18532 cp_parser_error (parser
, "expected class-name");
18533 else if (identifier
&& !parser
->scope
)
18534 maybe_note_name_used_in_class (identifier
, decl
);
18539 /* Parse a class-specifier.
18542 class-head { member-specification [opt] }
18544 Returns the TREE_TYPE representing the class. */
18547 cp_parser_class_specifier_1 (cp_parser
* parser
)
18550 tree attributes
= NULL_TREE
;
18551 bool nested_name_specifier_p
;
18552 unsigned saved_num_template_parameter_lists
;
18553 bool saved_in_function_body
;
18554 unsigned char in_statement
;
18555 bool in_switch_statement_p
;
18556 bool saved_in_unbraced_linkage_specification_p
;
18557 tree old_scope
= NULL_TREE
;
18558 tree scope
= NULL_TREE
;
18559 cp_token
*closing_brace
;
18561 push_deferring_access_checks (dk_no_deferred
);
18563 /* Parse the class-head. */
18564 type
= cp_parser_class_head (parser
,
18565 &nested_name_specifier_p
);
18566 /* If the class-head was a semantic disaster, skip the entire body
18570 cp_parser_skip_to_end_of_block_or_statement (parser
);
18571 pop_deferring_access_checks ();
18572 return error_mark_node
;
18575 /* Look for the `{'. */
18576 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
18578 pop_deferring_access_checks ();
18579 return error_mark_node
;
18582 /* Issue an error message if type-definitions are forbidden here. */
18583 cp_parser_check_type_definition (parser
);
18584 /* Remember that we are defining one more class. */
18585 ++parser
->num_classes_being_defined
;
18586 /* Inside the class, surrounding template-parameter-lists do not
18588 saved_num_template_parameter_lists
18589 = parser
->num_template_parameter_lists
;
18590 parser
->num_template_parameter_lists
= 0;
18591 /* We are not in a function body. */
18592 saved_in_function_body
= parser
->in_function_body
;
18593 parser
->in_function_body
= false;
18594 /* Or in a loop. */
18595 in_statement
= parser
->in_statement
;
18596 parser
->in_statement
= 0;
18597 /* Or in a switch. */
18598 in_switch_statement_p
= parser
->in_switch_statement_p
;
18599 parser
->in_switch_statement_p
= false;
18600 /* We are not immediately inside an extern "lang" block. */
18601 saved_in_unbraced_linkage_specification_p
18602 = parser
->in_unbraced_linkage_specification_p
;
18603 parser
->in_unbraced_linkage_specification_p
= false;
18605 /* Start the class. */
18606 if (nested_name_specifier_p
)
18608 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
18609 old_scope
= push_inner_scope (scope
);
18611 type
= begin_class_definition (type
);
18613 if (type
== error_mark_node
)
18614 /* If the type is erroneous, skip the entire body of the class. */
18615 cp_parser_skip_to_closing_brace (parser
);
18617 /* Parse the member-specification. */
18618 cp_parser_member_specification_opt (parser
);
18620 /* Look for the trailing `}'. */
18621 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18622 /* Look for trailing attributes to apply to this class. */
18623 if (cp_parser_allow_gnu_extensions_p (parser
))
18624 attributes
= cp_parser_gnu_attributes_opt (parser
);
18625 if (type
!= error_mark_node
)
18626 type
= finish_struct (type
, attributes
);
18627 if (nested_name_specifier_p
)
18628 pop_inner_scope (old_scope
, scope
);
18630 /* We've finished a type definition. Check for the common syntax
18631 error of forgetting a semicolon after the definition. We need to
18632 be careful, as we can't just check for not-a-semicolon and be done
18633 with it; the user might have typed:
18635 class X { } c = ...;
18636 class X { } *p = ...;
18638 and so forth. Instead, enumerate all the possible tokens that
18639 might follow this production; if we don't see one of them, then
18640 complain and silently insert the semicolon. */
18642 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18643 bool want_semicolon
= true;
18645 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18646 /* Don't try to parse c++11 attributes here. As per the
18647 grammar, that should be a task for
18648 cp_parser_decl_specifier_seq. */
18649 want_semicolon
= false;
18651 switch (token
->type
)
18654 case CPP_SEMICOLON
:
18657 case CPP_OPEN_PAREN
:
18658 case CPP_CLOSE_PAREN
:
18660 want_semicolon
= false;
18663 /* While it's legal for type qualifiers and storage class
18664 specifiers to follow type definitions in the grammar, only
18665 compiler testsuites contain code like that. Assume that if
18666 we see such code, then what we're really seeing is a case
18670 const <type> var = ...;
18675 static <type> func (...) ...
18677 i.e. the qualifier or specifier applies to the next
18678 declaration. To do so, however, we need to look ahead one
18679 more token to see if *that* token is a type specifier.
18681 This code could be improved to handle:
18684 static const <type> var = ...; */
18686 if (keyword_is_decl_specifier (token
->keyword
))
18688 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18690 /* Handling user-defined types here would be nice, but very
18693 = (lookahead
->type
== CPP_KEYWORD
18694 && keyword_begins_type_specifier (lookahead
->keyword
));
18701 /* If we don't have a type, then something is very wrong and we
18702 shouldn't try to do anything clever. Likewise for not seeing the
18704 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
18706 cp_token_position prev
18707 = cp_lexer_previous_token_position (parser
->lexer
);
18708 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
18709 location_t loc
= prev_token
->location
;
18711 if (CLASSTYPE_DECLARED_CLASS (type
))
18712 error_at (loc
, "expected %<;%> after class definition");
18713 else if (TREE_CODE (type
) == RECORD_TYPE
)
18714 error_at (loc
, "expected %<;%> after struct definition");
18715 else if (TREE_CODE (type
) == UNION_TYPE
)
18716 error_at (loc
, "expected %<;%> after union definition");
18718 gcc_unreachable ();
18720 /* Unget one token and smash it to look as though we encountered
18721 a semicolon in the input stream. */
18722 cp_lexer_set_token_position (parser
->lexer
, prev
);
18723 token
= cp_lexer_peek_token (parser
->lexer
);
18724 token
->type
= CPP_SEMICOLON
;
18725 token
->keyword
= RID_MAX
;
18729 /* If this class is not itself within the scope of another class,
18730 then we need to parse the bodies of all of the queued function
18731 definitions. Note that the queued functions defined in a class
18732 are not always processed immediately following the
18733 class-specifier for that class. Consider:
18736 struct B { void f() { sizeof (A); } };
18739 If `f' were processed before the processing of `A' were
18740 completed, there would be no way to compute the size of `A'.
18741 Note that the nesting we are interested in here is lexical --
18742 not the semantic nesting given by TYPE_CONTEXT. In particular,
18745 struct A { struct B; };
18746 struct A::B { void f() { } };
18748 there is no need to delay the parsing of `A::B::f'. */
18749 if (--parser
->num_classes_being_defined
== 0)
18752 tree class_type
= NULL_TREE
;
18753 tree pushed_scope
= NULL_TREE
;
18755 cp_default_arg_entry
*e
;
18756 tree save_ccp
, save_ccr
;
18758 /* In a first pass, parse default arguments to the functions.
18759 Then, in a second pass, parse the bodies of the functions.
18760 This two-phased approach handles cases like:
18768 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
18771 /* If there are default arguments that have not yet been processed,
18772 take care of them now. */
18773 if (class_type
!= e
->class_type
)
18776 pop_scope (pushed_scope
);
18777 class_type
= e
->class_type
;
18778 pushed_scope
= push_scope (class_type
);
18780 /* Make sure that any template parameters are in scope. */
18781 maybe_begin_member_template_processing (decl
);
18782 /* Parse the default argument expressions. */
18783 cp_parser_late_parsing_default_args (parser
, decl
);
18784 /* Remove any template parameters from the symbol table. */
18785 maybe_end_member_template_processing ();
18787 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
18788 /* Now parse any NSDMIs. */
18789 save_ccp
= current_class_ptr
;
18790 save_ccr
= current_class_ref
;
18791 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
18793 if (class_type
!= DECL_CONTEXT (decl
))
18796 pop_scope (pushed_scope
);
18797 class_type
= DECL_CONTEXT (decl
);
18798 pushed_scope
= push_scope (class_type
);
18800 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
18801 cp_parser_late_parsing_nsdmi (parser
, decl
);
18803 vec_safe_truncate (unparsed_nsdmis
, 0);
18804 current_class_ptr
= save_ccp
;
18805 current_class_ref
= save_ccr
;
18807 pop_scope (pushed_scope
);
18808 /* Now parse the body of the functions. */
18809 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
18810 cp_parser_late_parsing_for_member (parser
, decl
);
18811 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
18814 /* Put back any saved access checks. */
18815 pop_deferring_access_checks ();
18817 /* Restore saved state. */
18818 parser
->in_switch_statement_p
= in_switch_statement_p
;
18819 parser
->in_statement
= in_statement
;
18820 parser
->in_function_body
= saved_in_function_body
;
18821 parser
->num_template_parameter_lists
18822 = saved_num_template_parameter_lists
;
18823 parser
->in_unbraced_linkage_specification_p
18824 = saved_in_unbraced_linkage_specification_p
;
18830 cp_parser_class_specifier (cp_parser
* parser
)
18833 timevar_push (TV_PARSE_STRUCT
);
18834 ret
= cp_parser_class_specifier_1 (parser
);
18835 timevar_pop (TV_PARSE_STRUCT
);
18839 /* Parse a class-head.
18842 class-key identifier [opt] base-clause [opt]
18843 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18844 class-key nested-name-specifier [opt] template-id
18847 class-virt-specifier:
18851 class-key attributes identifier [opt] base-clause [opt]
18852 class-key attributes nested-name-specifier identifier base-clause [opt]
18853 class-key attributes nested-name-specifier [opt] template-id
18856 Upon return BASES is initialized to the list of base classes (or
18857 NULL, if there are none) in the same form returned by
18858 cp_parser_base_clause.
18860 Returns the TYPE of the indicated class. Sets
18861 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18862 involving a nested-name-specifier was used, and FALSE otherwise.
18864 Returns error_mark_node if this is not a class-head.
18866 Returns NULL_TREE if the class-head is syntactically valid, but
18867 semantically invalid in a way that means we should skip the entire
18868 body of the class. */
18871 cp_parser_class_head (cp_parser
* parser
,
18872 bool* nested_name_specifier_p
)
18874 tree nested_name_specifier
;
18875 enum tag_types class_key
;
18876 tree id
= NULL_TREE
;
18877 tree type
= NULL_TREE
;
18880 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18881 bool template_id_p
= false;
18882 bool qualified_p
= false;
18883 bool invalid_nested_name_p
= false;
18884 bool invalid_explicit_specialization_p
= false;
18885 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18886 tree pushed_scope
= NULL_TREE
;
18887 unsigned num_templates
;
18888 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
18889 /* Assume no nested-name-specifier will be present. */
18890 *nested_name_specifier_p
= false;
18891 /* Assume no template parameter lists will be used in defining the
18894 parser
->colon_corrects_to_scope_p
= false;
18896 /* Look for the class-key. */
18897 class_key
= cp_parser_class_key (parser
);
18898 if (class_key
== none_type
)
18899 return error_mark_node
;
18901 /* Parse the attributes. */
18902 attributes
= cp_parser_attributes_opt (parser
);
18904 /* If the next token is `::', that is invalid -- but sometimes
18905 people do try to write:
18909 Handle this gracefully by accepting the extra qualifier, and then
18910 issuing an error about it later if this really is a
18911 class-head. If it turns out just to be an elaborated type
18912 specifier, remain silent. */
18913 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
18914 qualified_p
= true;
18916 push_deferring_access_checks (dk_no_check
);
18918 /* Determine the name of the class. Begin by looking for an
18919 optional nested-name-specifier. */
18920 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
18921 nested_name_specifier
18922 = cp_parser_nested_name_specifier_opt (parser
,
18923 /*typename_keyword_p=*/false,
18924 /*check_dependency_p=*/false,
18926 /*is_declaration=*/false);
18927 /* If there was a nested-name-specifier, then there *must* be an
18929 if (nested_name_specifier
)
18931 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18932 /* Although the grammar says `identifier', it really means
18933 `class-name' or `template-name'. You are only allowed to
18934 define a class that has already been declared with this
18937 The proposed resolution for Core Issue 180 says that wherever
18938 you see `class T::X' you should treat `X' as a type-name.
18940 It is OK to define an inaccessible class; for example:
18942 class A { class B; };
18945 We do not know if we will see a class-name, or a
18946 template-name. We look for a class-name first, in case the
18947 class-name is a template-id; if we looked for the
18948 template-name first we would stop after the template-name. */
18949 cp_parser_parse_tentatively (parser
);
18950 type
= cp_parser_class_name (parser
,
18951 /*typename_keyword_p=*/false,
18952 /*template_keyword_p=*/false,
18954 /*check_dependency_p=*/false,
18955 /*class_head_p=*/true,
18956 /*is_declaration=*/false);
18957 /* If that didn't work, ignore the nested-name-specifier. */
18958 if (!cp_parser_parse_definitely (parser
))
18960 invalid_nested_name_p
= true;
18961 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18962 id
= cp_parser_identifier (parser
);
18963 if (id
== error_mark_node
)
18966 /* If we could not find a corresponding TYPE, treat this
18967 declaration like an unqualified declaration. */
18968 if (type
== error_mark_node
)
18969 nested_name_specifier
= NULL_TREE
;
18970 /* Otherwise, count the number of templates used in TYPE and its
18971 containing scopes. */
18976 for (scope
= TREE_TYPE (type
);
18977 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
18978 scope
= get_containing_scope (scope
))
18980 && CLASS_TYPE_P (scope
)
18981 && CLASSTYPE_TEMPLATE_INFO (scope
)
18982 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
18983 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
18984 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
18988 /* Otherwise, the identifier is optional. */
18991 /* We don't know whether what comes next is a template-id,
18992 an identifier, or nothing at all. */
18993 cp_parser_parse_tentatively (parser
);
18994 /* Check for a template-id. */
18995 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18996 id
= cp_parser_template_id (parser
,
18997 /*template_keyword_p=*/false,
18998 /*check_dependency_p=*/true,
19000 /*is_declaration=*/true);
19001 /* If that didn't work, it could still be an identifier. */
19002 if (!cp_parser_parse_definitely (parser
))
19004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19006 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19007 id
= cp_parser_identifier (parser
);
19014 template_id_p
= true;
19019 pop_deferring_access_checks ();
19023 cp_parser_check_for_invalid_template_id (parser
, id
,
19025 type_start_token
->location
);
19027 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19029 /* If it's not a `:' or a `{' then we can't really be looking at a
19030 class-head, since a class-head only appears as part of a
19031 class-specifier. We have to detect this situation before calling
19032 xref_tag, since that has irreversible side-effects. */
19033 if (!cp_parser_next_token_starts_class_definition_p (parser
))
19035 cp_parser_error (parser
, "expected %<{%> or %<:%>");
19036 type
= error_mark_node
;
19040 /* At this point, we're going ahead with the class-specifier, even
19041 if some other problem occurs. */
19042 cp_parser_commit_to_tentative_parse (parser
);
19043 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
19045 cp_parser_error (parser
,
19046 "cannot specify %<override%> for a class");
19047 type
= error_mark_node
;
19050 /* Issue the error about the overly-qualified name now. */
19053 cp_parser_error (parser
,
19054 "global qualification of class name is invalid");
19055 type
= error_mark_node
;
19058 else if (invalid_nested_name_p
)
19060 cp_parser_error (parser
,
19061 "qualified name does not name a class");
19062 type
= error_mark_node
;
19065 else if (nested_name_specifier
)
19069 /* Reject typedef-names in class heads. */
19070 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
19072 error_at (type_start_token
->location
,
19073 "invalid class name in declaration of %qD",
19079 /* Figure out in what scope the declaration is being placed. */
19080 scope
= current_scope ();
19081 /* If that scope does not contain the scope in which the
19082 class was originally declared, the program is invalid. */
19083 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
19085 if (at_namespace_scope_p ())
19086 error_at (type_start_token
->location
,
19087 "declaration of %qD in namespace %qD which does not "
19089 type
, scope
, nested_name_specifier
);
19091 error_at (type_start_token
->location
,
19092 "declaration of %qD in %qD which does not enclose %qD",
19093 type
, scope
, nested_name_specifier
);
19099 A declarator-id shall not be qualified except for the
19100 definition of a ... nested class outside of its class
19101 ... [or] the definition or explicit instantiation of a
19102 class member of a namespace outside of its namespace. */
19103 if (scope
== nested_name_specifier
)
19105 permerror (nested_name_specifier_token_start
->location
,
19106 "extra qualification not allowed");
19107 nested_name_specifier
= NULL_TREE
;
19111 /* An explicit-specialization must be preceded by "template <>". If
19112 it is not, try to recover gracefully. */
19113 if (at_namespace_scope_p ()
19114 && parser
->num_template_parameter_lists
== 0
19117 error_at (type_start_token
->location
,
19118 "an explicit specialization must be preceded by %<template <>%>");
19119 invalid_explicit_specialization_p
= true;
19120 /* Take the same action that would have been taken by
19121 cp_parser_explicit_specialization. */
19122 ++parser
->num_template_parameter_lists
;
19123 begin_specialization ();
19125 /* There must be no "return" statements between this point and the
19126 end of this function; set "type "to the correct return value and
19127 use "goto done;" to return. */
19128 /* Make sure that the right number of template parameters were
19130 if (!cp_parser_check_template_parameters (parser
, num_templates
,
19131 type_start_token
->location
,
19132 /*declarator=*/NULL
))
19134 /* If something went wrong, there is no point in even trying to
19135 process the class-definition. */
19140 /* Look up the type. */
19143 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
19144 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
19145 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
19147 error_at (type_start_token
->location
,
19148 "function template %qD redeclared as a class template", id
);
19149 type
= error_mark_node
;
19153 type
= TREE_TYPE (id
);
19154 type
= maybe_process_partial_specialization (type
);
19156 if (nested_name_specifier
)
19157 pushed_scope
= push_scope (nested_name_specifier
);
19159 else if (nested_name_specifier
)
19165 template <typename T> struct S { struct T };
19166 template <typename T> struct S<T>::T { };
19168 we will get a TYPENAME_TYPE when processing the definition of
19169 `S::T'. We need to resolve it to the actual type before we
19170 try to define it. */
19171 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
19173 class_type
= resolve_typename_type (TREE_TYPE (type
),
19174 /*only_current_p=*/false);
19175 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
19176 type
= TYPE_NAME (class_type
);
19179 cp_parser_error (parser
, "could not resolve typename type");
19180 type
= error_mark_node
;
19184 if (maybe_process_partial_specialization (TREE_TYPE (type
))
19185 == error_mark_node
)
19191 class_type
= current_class_type
;
19192 /* Enter the scope indicated by the nested-name-specifier. */
19193 pushed_scope
= push_scope (nested_name_specifier
);
19194 /* Get the canonical version of this type. */
19195 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
19196 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19197 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
19199 type
= push_template_decl (type
);
19200 if (type
== error_mark_node
)
19207 type
= TREE_TYPE (type
);
19208 *nested_name_specifier_p
= true;
19210 else /* The name is not a nested name. */
19212 /* If the class was unnamed, create a dummy name. */
19214 id
= make_anon_name ();
19215 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
19216 parser
->num_template_parameter_lists
);
19219 /* Indicate whether this class was declared as a `class' or as a
19221 if (TREE_CODE (type
) == RECORD_TYPE
)
19222 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
19223 cp_parser_check_class_key (class_key
, type
);
19225 /* If this type was already complete, and we see another definition,
19226 that's an error. */
19227 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
19229 error_at (type_start_token
->location
, "redefinition of %q#T",
19231 error_at (type_start_token
->location
, "previous definition of %q+#T",
19236 else if (type
== error_mark_node
)
19241 /* Apply attributes now, before any use of the class as a template
19242 argument in its base list. */
19243 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
19244 fixup_attribute_variants (type
);
19247 /* We will have entered the scope containing the class; the names of
19248 base classes should be looked up in that context. For example:
19250 struct A { struct B {}; struct C; };
19251 struct A::C : B {};
19255 /* Get the list of base-classes, if there is one. */
19256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19257 bases
= cp_parser_base_clause (parser
);
19261 /* If we're really defining a class, process the base classes.
19262 If they're invalid, fail. */
19263 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
19264 && !xref_basetypes (type
, bases
))
19268 /* Leave the scope given by the nested-name-specifier. We will
19269 enter the class scope itself while processing the members. */
19271 pop_scope (pushed_scope
);
19273 if (invalid_explicit_specialization_p
)
19275 end_specialization ();
19276 --parser
->num_template_parameter_lists
;
19280 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
19281 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
19282 CLASSTYPE_FINAL (type
) = 1;
19284 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19288 /* Parse a class-key.
19295 Returns the kind of class-key specified, or none_type to indicate
19298 static enum tag_types
19299 cp_parser_class_key (cp_parser
* parser
)
19302 enum tag_types tag_type
;
19304 /* Look for the class-key. */
19305 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
19309 /* Check to see if the TOKEN is a class-key. */
19310 tag_type
= cp_parser_token_is_class_key (token
);
19312 cp_parser_error (parser
, "expected class-key");
19316 /* Parse an (optional) member-specification.
19318 member-specification:
19319 member-declaration member-specification [opt]
19320 access-specifier : member-specification [opt] */
19323 cp_parser_member_specification_opt (cp_parser
* parser
)
19330 /* Peek at the next token. */
19331 token
= cp_lexer_peek_token (parser
->lexer
);
19332 /* If it's a `}', or EOF then we've seen all the members. */
19333 if (token
->type
== CPP_CLOSE_BRACE
19334 || token
->type
== CPP_EOF
19335 || token
->type
== CPP_PRAGMA_EOL
)
19338 /* See if this token is a keyword. */
19339 keyword
= token
->keyword
;
19343 case RID_PROTECTED
:
19345 /* Consume the access-specifier. */
19346 cp_lexer_consume_token (parser
->lexer
);
19347 /* Remember which access-specifier is active. */
19348 current_access_specifier
= token
->u
.value
;
19349 /* Look for the `:'. */
19350 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
19354 /* Accept #pragmas at class scope. */
19355 if (token
->type
== CPP_PRAGMA
)
19357 cp_parser_pragma (parser
, pragma_external
);
19361 /* Otherwise, the next construction must be a
19362 member-declaration. */
19363 cp_parser_member_declaration (parser
);
19368 /* Parse a member-declaration.
19370 member-declaration:
19371 decl-specifier-seq [opt] member-declarator-list [opt] ;
19372 function-definition ; [opt]
19373 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19375 template-declaration
19378 member-declarator-list:
19380 member-declarator-list , member-declarator
19383 declarator pure-specifier [opt]
19384 declarator constant-initializer [opt]
19385 identifier [opt] : constant-expression
19389 member-declaration:
19390 __extension__ member-declaration
19393 declarator attributes [opt] pure-specifier [opt]
19394 declarator attributes [opt] constant-initializer [opt]
19395 identifier [opt] attributes [opt] : constant-expression
19399 member-declaration:
19400 static_assert-declaration */
19403 cp_parser_member_declaration (cp_parser
* parser
)
19405 cp_decl_specifier_seq decl_specifiers
;
19406 tree prefix_attributes
;
19408 int declares_class_or_enum
;
19410 cp_token
*token
= NULL
;
19411 cp_token
*decl_spec_token_start
= NULL
;
19412 cp_token
*initializer_token_start
= NULL
;
19413 int saved_pedantic
;
19414 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
19416 /* Check for the `__extension__' keyword. */
19417 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
19420 cp_parser_member_declaration (parser
);
19421 /* Restore the old value of the PEDANTIC flag. */
19422 pedantic
= saved_pedantic
;
19427 /* Check for a template-declaration. */
19428 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
19430 /* An explicit specialization here is an error condition, and we
19431 expect the specialization handler to detect and report this. */
19432 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
19433 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
19434 cp_parser_explicit_specialization (parser
);
19436 cp_parser_template_declaration (parser
, /*member_p=*/true);
19441 /* Check for a using-declaration. */
19442 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
19444 if (cxx_dialect
< cxx0x
)
19446 /* Parse the using-declaration. */
19447 cp_parser_using_declaration (parser
,
19448 /*access_declaration_p=*/false);
19454 bool alias_decl_expected
;
19455 cp_parser_parse_tentatively (parser
);
19456 decl
= cp_parser_alias_declaration (parser
);
19457 /* Note that if we actually see the '=' token after the
19458 identifier, cp_parser_alias_declaration commits the
19459 tentative parse. In that case, we really expects an
19460 alias-declaration. Otherwise, we expect a using
19462 alias_decl_expected
=
19463 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
19464 cp_parser_parse_definitely (parser
);
19466 if (alias_decl_expected
)
19467 finish_member_declaration (decl
);
19469 cp_parser_using_declaration (parser
,
19470 /*access_declaration_p=*/false);
19475 /* Check for @defs. */
19476 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
19479 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
19480 ivar
= ivar_chains
;
19484 ivar
= TREE_CHAIN (member
);
19485 TREE_CHAIN (member
) = NULL_TREE
;
19486 finish_member_declaration (member
);
19491 /* If the next token is `static_assert' we have a static assertion. */
19492 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
19494 cp_parser_static_assert (parser
, /*member_p=*/true);
19498 parser
->colon_corrects_to_scope_p
= false;
19500 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
19503 /* Parse the decl-specifier-seq. */
19504 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
19505 cp_parser_decl_specifier_seq (parser
,
19506 CP_PARSER_FLAGS_OPTIONAL
,
19508 &declares_class_or_enum
);
19509 /* Check for an invalid type-name. */
19510 if (!decl_specifiers
.any_type_specifiers_p
19511 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19513 /* If there is no declarator, then the decl-specifier-seq should
19515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19517 /* If there was no decl-specifier-seq, and the next token is a
19518 `;', then we have something like:
19524 Each member-declaration shall declare at least one member
19525 name of the class. */
19526 if (!decl_specifiers
.any_specifiers_p
)
19528 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19529 if (!in_system_header_at (token
->location
))
19530 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
19536 /* See if this declaration is a friend. */
19537 friend_p
= cp_parser_friend_p (&decl_specifiers
);
19538 /* If there were decl-specifiers, check to see if there was
19539 a class-declaration. */
19540 type
= check_tag_decl (&decl_specifiers
,
19541 /*explicit_type_instantiation_p=*/false);
19542 /* Nested classes have already been added to the class, but
19543 a `friend' needs to be explicitly registered. */
19546 /* If the `friend' keyword was present, the friend must
19547 be introduced with a class-key. */
19548 if (!declares_class_or_enum
&& cxx_dialect
< cxx0x
)
19549 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
19550 "in C++03 a class-key must be used "
19551 "when declaring a friend");
19554 template <typename T> struct A {
19555 friend struct A<T>::B;
19558 A<T>::B will be represented by a TYPENAME_TYPE, and
19559 therefore not recognized by check_tag_decl. */
19562 type
= decl_specifiers
.type
;
19563 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
19564 type
= TREE_TYPE (type
);
19566 if (!type
|| !TYPE_P (type
))
19567 error_at (decl_spec_token_start
->location
,
19568 "friend declaration does not name a class or "
19571 make_friend_class (current_class_type
, type
,
19572 /*complain=*/true);
19574 /* If there is no TYPE, an error message will already have
19576 else if (!type
|| type
== error_mark_node
)
19578 /* An anonymous aggregate has to be handled specially; such
19579 a declaration really declares a data member (with a
19580 particular type), as opposed to a nested class. */
19581 else if (ANON_AGGR_TYPE_P (type
))
19584 if (decl_specifiers
.storage_class
!= sc_none
)
19585 error_at (decl_spec_token_start
->location
,
19586 "a storage class on an anonymous aggregate "
19587 "in class scope is not allowed");
19589 /* Remove constructors and such from TYPE, now that we
19590 know it is an anonymous aggregate. */
19591 fixup_anonymous_aggr (type
);
19592 /* And make the corresponding data member. */
19593 decl
= build_decl (decl_spec_token_start
->location
,
19594 FIELD_DECL
, NULL_TREE
, type
);
19595 /* Add it to the class. */
19596 finish_member_declaration (decl
);
19599 cp_parser_check_access_in_redeclaration
19601 decl_spec_token_start
->location
);
19606 bool assume_semicolon
= false;
19608 /* Clear attributes from the decl_specifiers but keep them
19609 around as prefix attributes that apply them to the entity
19611 prefix_attributes
= decl_specifiers
.attributes
;
19612 decl_specifiers
.attributes
= NULL_TREE
;
19614 /* See if these declarations will be friends. */
19615 friend_p
= cp_parser_friend_p (&decl_specifiers
);
19617 /* Keep going until we hit the `;' at the end of the
19619 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19621 tree attributes
= NULL_TREE
;
19622 tree first_attribute
;
19624 /* Peek at the next token. */
19625 token
= cp_lexer_peek_token (parser
->lexer
);
19627 /* Check for a bitfield declaration. */
19628 if (token
->type
== CPP_COLON
19629 || (token
->type
== CPP_NAME
19630 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
19636 /* Get the name of the bitfield. Note that we cannot just
19637 check TOKEN here because it may have been invalidated by
19638 the call to cp_lexer_peek_nth_token above. */
19639 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
19640 identifier
= cp_parser_identifier (parser
);
19642 identifier
= NULL_TREE
;
19644 /* Consume the `:' token. */
19645 cp_lexer_consume_token (parser
->lexer
);
19646 /* Get the width of the bitfield. */
19648 = cp_parser_constant_expression (parser
,
19649 /*allow_non_constant=*/false,
19652 /* Look for attributes that apply to the bitfield. */
19653 attributes
= cp_parser_attributes_opt (parser
);
19654 /* Remember which attributes are prefix attributes and
19656 first_attribute
= attributes
;
19657 /* Combine the attributes. */
19658 attributes
= chainon (prefix_attributes
, attributes
);
19660 /* Create the bitfield declaration. */
19661 decl
= grokbitfield (identifier
19662 ? make_id_declarator (NULL_TREE
,
19672 cp_declarator
*declarator
;
19674 tree asm_specification
;
19675 int ctor_dtor_or_conv_p
;
19677 /* Parse the declarator. */
19679 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19680 &ctor_dtor_or_conv_p
,
19681 /*parenthesized_p=*/NULL
,
19682 /*member_p=*/true);
19684 /* If something went wrong parsing the declarator, make sure
19685 that we at least consume some tokens. */
19686 if (declarator
== cp_error_declarator
)
19688 /* Skip to the end of the statement. */
19689 cp_parser_skip_to_end_of_statement (parser
);
19690 /* If the next token is not a semicolon, that is
19691 probably because we just skipped over the body of
19692 a function. So, we consume a semicolon if
19693 present, but do not issue an error message if it
19695 if (cp_lexer_next_token_is (parser
->lexer
,
19697 cp_lexer_consume_token (parser
->lexer
);
19701 if (declares_class_or_enum
& 2)
19702 cp_parser_check_for_definition_in_return_type
19703 (declarator
, decl_specifiers
.type
,
19704 decl_specifiers
.locations
[ds_type_spec
]);
19706 /* Look for an asm-specification. */
19707 asm_specification
= cp_parser_asm_specification_opt (parser
);
19708 /* Look for attributes that apply to the declaration. */
19709 attributes
= cp_parser_attributes_opt (parser
);
19710 /* Remember which attributes are prefix attributes and
19712 first_attribute
= attributes
;
19713 /* Combine the attributes. */
19714 attributes
= chainon (prefix_attributes
, attributes
);
19716 /* If it's an `=', then we have a constant-initializer or a
19717 pure-specifier. It is not correct to parse the
19718 initializer before registering the member declaration
19719 since the member declaration should be in scope while
19720 its initializer is processed. However, the rest of the
19721 front end does not yet provide an interface that allows
19722 us to handle this correctly. */
19723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19727 A pure-specifier shall be used only in the declaration of
19728 a virtual function.
19730 A member-declarator can contain a constant-initializer
19731 only if it declares a static member of integral or
19734 Therefore, if the DECLARATOR is for a function, we look
19735 for a pure-specifier; otherwise, we look for a
19736 constant-initializer. When we call `grokfield', it will
19737 perform more stringent semantics checks. */
19738 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
19739 if (function_declarator_p (declarator
)
19740 || (decl_specifiers
.type
19741 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
19742 && declarator
->kind
== cdk_id
19743 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
19744 == FUNCTION_TYPE
)))
19745 initializer
= cp_parser_pure_specifier (parser
);
19746 else if (decl_specifiers
.storage_class
!= sc_static
)
19747 initializer
= cp_parser_save_nsdmi (parser
);
19748 else if (cxx_dialect
>= cxx0x
)
19751 /* Don't require a constant rvalue in C++11, since we
19752 might want a reference constant. We'll enforce
19753 constancy later. */
19754 cp_lexer_consume_token (parser
->lexer
);
19755 /* Parse the initializer. */
19756 initializer
= cp_parser_initializer_clause (parser
,
19760 /* Parse the initializer. */
19761 initializer
= cp_parser_constant_initializer (parser
);
19763 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
19764 && !function_declarator_p (declarator
))
19767 if (decl_specifiers
.storage_class
!= sc_static
)
19768 initializer
= cp_parser_save_nsdmi (parser
);
19770 initializer
= cp_parser_initializer (parser
, &x
, &x
);
19772 /* Otherwise, there is no initializer. */
19774 initializer
= NULL_TREE
;
19776 /* See if we are probably looking at a function
19777 definition. We are certainly not looking at a
19778 member-declarator. Calling `grokfield' has
19779 side-effects, so we must not do it unless we are sure
19780 that we are looking at a member-declarator. */
19781 if (cp_parser_token_starts_function_definition_p
19782 (cp_lexer_peek_token (parser
->lexer
)))
19784 /* The grammar does not allow a pure-specifier to be
19785 used when a member function is defined. (It is
19786 possible that this fact is an oversight in the
19787 standard, since a pure function may be defined
19788 outside of the class-specifier. */
19789 if (initializer
&& initializer_token_start
)
19790 error_at (initializer_token_start
->location
,
19791 "pure-specifier on function-definition");
19792 decl
= cp_parser_save_member_function_body (parser
,
19796 /* If the member was not a friend, declare it here. */
19798 finish_member_declaration (decl
);
19799 /* Peek at the next token. */
19800 token
= cp_lexer_peek_token (parser
->lexer
);
19801 /* If the next token is a semicolon, consume it. */
19802 if (token
->type
== CPP_SEMICOLON
)
19803 cp_lexer_consume_token (parser
->lexer
);
19807 if (declarator
->kind
== cdk_function
)
19808 declarator
->id_loc
= token
->location
;
19809 /* Create the declaration. */
19810 decl
= grokfield (declarator
, &decl_specifiers
,
19811 initializer
, /*init_const_expr_p=*/true,
19816 /* Reset PREFIX_ATTRIBUTES. */
19817 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
19818 attributes
= TREE_CHAIN (attributes
);
19820 TREE_CHAIN (attributes
) = NULL_TREE
;
19822 /* If there is any qualification still in effect, clear it
19823 now; we will be starting fresh with the next declarator. */
19824 parser
->scope
= NULL_TREE
;
19825 parser
->qualifying_scope
= NULL_TREE
;
19826 parser
->object_scope
= NULL_TREE
;
19827 /* If it's a `,', then there are more declarators. */
19828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19830 cp_lexer_consume_token (parser
->lexer
);
19831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19833 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
19834 error_at (token
->location
,
19835 "stray %<,%> at end of member declaration");
19838 /* If the next token isn't a `;', then we have a parse error. */
19839 else if (cp_lexer_next_token_is_not (parser
->lexer
,
19842 /* The next token might be a ways away from where the
19843 actual semicolon is missing. Find the previous token
19844 and use that for our error position. */
19845 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
19846 error_at (token
->location
,
19847 "expected %<;%> at end of member declaration");
19849 /* Assume that the user meant to provide a semicolon. If
19850 we were to cp_parser_skip_to_end_of_statement, we might
19851 skip to a semicolon inside a member function definition
19852 and issue nonsensical error messages. */
19853 assume_semicolon
= true;
19858 /* Add DECL to the list of members. */
19860 finish_member_declaration (decl
);
19862 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19863 cp_parser_save_default_args (parser
, decl
);
19864 else if (TREE_CODE (decl
) == FIELD_DECL
19865 && !DECL_C_BIT_FIELD (decl
)
19866 && DECL_INITIAL (decl
))
19867 /* Add DECL to the queue of NSDMI to be parsed later. */
19868 vec_safe_push (unparsed_nsdmis
, decl
);
19871 if (assume_semicolon
)
19876 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19878 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19881 /* Parse a pure-specifier.
19886 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19887 Otherwise, ERROR_MARK_NODE is returned. */
19890 cp_parser_pure_specifier (cp_parser
* parser
)
19894 /* Look for the `=' token. */
19895 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
19896 return error_mark_node
;
19897 /* Look for the `0' token. */
19898 token
= cp_lexer_peek_token (parser
->lexer
);
19900 if (token
->type
== CPP_EOF
19901 || token
->type
== CPP_PRAGMA_EOL
)
19902 return error_mark_node
;
19904 cp_lexer_consume_token (parser
->lexer
);
19906 /* Accept = default or = delete in c++0x mode. */
19907 if (token
->keyword
== RID_DEFAULT
19908 || token
->keyword
== RID_DELETE
)
19910 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
19911 return token
->u
.value
;
19914 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19915 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
19917 cp_parser_error (parser
,
19918 "invalid pure specifier (only %<= 0%> is allowed)");
19919 cp_parser_skip_to_end_of_statement (parser
);
19920 return error_mark_node
;
19922 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19924 error_at (token
->location
, "templates may not be %<virtual%>");
19925 return error_mark_node
;
19928 return integer_zero_node
;
19931 /* Parse a constant-initializer.
19933 constant-initializer:
19934 = constant-expression
19936 Returns a representation of the constant-expression. */
19939 cp_parser_constant_initializer (cp_parser
* parser
)
19941 /* Look for the `=' token. */
19942 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
19943 return error_mark_node
;
19945 /* It is invalid to write:
19947 struct S { static const int i = { 7 }; };
19950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19952 cp_parser_error (parser
,
19953 "a brace-enclosed initializer is not allowed here");
19954 /* Consume the opening brace. */
19955 cp_lexer_consume_token (parser
->lexer
);
19956 /* Skip the initializer. */
19957 cp_parser_skip_to_closing_brace (parser
);
19958 /* Look for the trailing `}'. */
19959 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19961 return error_mark_node
;
19964 return cp_parser_constant_expression (parser
,
19965 /*allow_non_constant=*/false,
19969 /* Derived classes [gram.class.derived] */
19971 /* Parse a base-clause.
19974 : base-specifier-list
19976 base-specifier-list:
19977 base-specifier ... [opt]
19978 base-specifier-list , base-specifier ... [opt]
19980 Returns a TREE_LIST representing the base-classes, in the order in
19981 which they were declared. The representation of each node is as
19982 described by cp_parser_base_specifier.
19984 In the case that no bases are specified, this function will return
19985 NULL_TREE, not ERROR_MARK_NODE. */
19988 cp_parser_base_clause (cp_parser
* parser
)
19990 tree bases
= NULL_TREE
;
19992 /* Look for the `:' that begins the list. */
19993 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
19995 /* Scan the base-specifier-list. */
20000 bool pack_expansion_p
= false;
20002 /* Look for the base-specifier. */
20003 base
= cp_parser_base_specifier (parser
);
20004 /* Look for the (optional) ellipsis. */
20005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20007 /* Consume the `...'. */
20008 cp_lexer_consume_token (parser
->lexer
);
20010 pack_expansion_p
= true;
20013 /* Add BASE to the front of the list. */
20014 if (base
&& base
!= error_mark_node
)
20016 if (pack_expansion_p
)
20017 /* Make this a pack expansion type. */
20018 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
20020 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
20022 TREE_CHAIN (base
) = bases
;
20026 /* Peek at the next token. */
20027 token
= cp_lexer_peek_token (parser
->lexer
);
20028 /* If it's not a comma, then the list is complete. */
20029 if (token
->type
!= CPP_COMMA
)
20031 /* Consume the `,'. */
20032 cp_lexer_consume_token (parser
->lexer
);
20035 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20036 base class had a qualified name. However, the next name that
20037 appears is certainly not qualified. */
20038 parser
->scope
= NULL_TREE
;
20039 parser
->qualifying_scope
= NULL_TREE
;
20040 parser
->object_scope
= NULL_TREE
;
20042 return nreverse (bases
);
20045 /* Parse a base-specifier.
20048 :: [opt] nested-name-specifier [opt] class-name
20049 virtual access-specifier [opt] :: [opt] nested-name-specifier
20051 access-specifier virtual [opt] :: [opt] nested-name-specifier
20054 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20055 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20056 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20057 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20060 cp_parser_base_specifier (cp_parser
* parser
)
20064 bool virtual_p
= false;
20065 bool duplicate_virtual_error_issued_p
= false;
20066 bool duplicate_access_error_issued_p
= false;
20067 bool class_scope_p
, template_p
;
20068 tree access
= access_default_node
;
20071 /* Process the optional `virtual' and `access-specifier'. */
20074 /* Peek at the next token. */
20075 token
= cp_lexer_peek_token (parser
->lexer
);
20076 /* Process `virtual'. */
20077 switch (token
->keyword
)
20080 /* If `virtual' appears more than once, issue an error. */
20081 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
20083 cp_parser_error (parser
,
20084 "%<virtual%> specified more than once in base-specified");
20085 duplicate_virtual_error_issued_p
= true;
20090 /* Consume the `virtual' token. */
20091 cp_lexer_consume_token (parser
->lexer
);
20096 case RID_PROTECTED
:
20098 /* If more than one access specifier appears, issue an
20100 if (access
!= access_default_node
20101 && !duplicate_access_error_issued_p
)
20103 cp_parser_error (parser
,
20104 "more than one access specifier in base-specified");
20105 duplicate_access_error_issued_p
= true;
20108 access
= ridpointers
[(int) token
->keyword
];
20110 /* Consume the access-specifier. */
20111 cp_lexer_consume_token (parser
->lexer
);
20120 /* It is not uncommon to see programs mechanically, erroneously, use
20121 the 'typename' keyword to denote (dependent) qualified types
20122 as base classes. */
20123 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
20125 token
= cp_lexer_peek_token (parser
->lexer
);
20126 if (!processing_template_decl
)
20127 error_at (token
->location
,
20128 "keyword %<typename%> not allowed outside of templates");
20130 error_at (token
->location
,
20131 "keyword %<typename%> not allowed in this context "
20132 "(the base class is implicitly a type)");
20133 cp_lexer_consume_token (parser
->lexer
);
20136 /* Look for the optional `::' operator. */
20137 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
20138 /* Look for the nested-name-specifier. The simplest way to
20143 The keyword `typename' is not permitted in a base-specifier or
20144 mem-initializer; in these contexts a qualified name that
20145 depends on a template-parameter is implicitly assumed to be a
20148 is to pretend that we have seen the `typename' keyword at this
20150 cp_parser_nested_name_specifier_opt (parser
,
20151 /*typename_keyword_p=*/true,
20152 /*check_dependency_p=*/true,
20154 /*is_declaration=*/true);
20155 /* If the base class is given by a qualified name, assume that names
20156 we see are type names or templates, as appropriate. */
20157 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
20158 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
20161 && cp_lexer_next_token_is_decltype (parser
->lexer
))
20162 /* DR 950 allows decltype as a base-specifier. */
20163 type
= cp_parser_decltype (parser
);
20166 /* Otherwise, look for the class-name. */
20167 type
= cp_parser_class_name (parser
,
20171 /*check_dependency_p=*/true,
20172 /*class_head_p=*/false,
20173 /*is_declaration=*/true);
20174 type
= TREE_TYPE (type
);
20177 if (type
== error_mark_node
)
20178 return error_mark_node
;
20180 return finish_base_specifier (type
, access
, virtual_p
);
20183 /* Exception handling [gram.exception] */
20185 /* Parse an (optional) noexcept-specification.
20187 noexcept-specification:
20188 noexcept ( constant-expression ) [opt]
20190 If no noexcept-specification is present, returns NULL_TREE.
20191 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20192 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20193 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20194 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20195 in which case a boolean condition is returned instead. */
20198 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
20199 bool require_constexpr
,
20200 bool* consumed_expr
,
20204 const char *saved_message
;
20206 /* Peek at the next token. */
20207 token
= cp_lexer_peek_token (parser
->lexer
);
20209 /* Is it a noexcept-specification? */
20210 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
20213 cp_lexer_consume_token (parser
->lexer
);
20215 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
20217 cp_lexer_consume_token (parser
->lexer
);
20219 if (require_constexpr
)
20221 /* Types may not be defined in an exception-specification. */
20222 saved_message
= parser
->type_definition_forbidden_message
;
20223 parser
->type_definition_forbidden_message
20224 = G_("types may not be defined in an exception-specification");
20226 expr
= cp_parser_constant_expression (parser
, false, NULL
);
20228 /* Restore the saved message. */
20229 parser
->type_definition_forbidden_message
= saved_message
;
20233 expr
= cp_parser_expression (parser
, false, NULL
);
20234 *consumed_expr
= true;
20237 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20241 expr
= boolean_true_node
;
20242 if (!require_constexpr
)
20243 *consumed_expr
= false;
20246 /* We cannot build a noexcept-spec right away because this will check
20247 that expr is a constexpr. */
20249 return build_noexcept_spec (expr
, tf_warning_or_error
);
20257 /* Parse an (optional) exception-specification.
20259 exception-specification:
20260 throw ( type-id-list [opt] )
20262 Returns a TREE_LIST representing the exception-specification. The
20263 TREE_VALUE of each node is a type. */
20266 cp_parser_exception_specification_opt (cp_parser
* parser
)
20270 const char *saved_message
;
20272 /* Peek at the next token. */
20273 token
= cp_lexer_peek_token (parser
->lexer
);
20275 /* Is it a noexcept-specification? */
20276 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
20278 if (type_id_list
!= NULL_TREE
)
20279 return type_id_list
;
20281 /* If it's not `throw', then there's no exception-specification. */
20282 if (!cp_parser_is_keyword (token
, RID_THROW
))
20286 /* Enable this once a lot of code has transitioned to noexcept? */
20287 if (cxx_dialect
>= cxx0x
&& !in_system_header
)
20288 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
20289 "deprecated in C++0x; use %<noexcept%> instead");
20292 /* Consume the `throw'. */
20293 cp_lexer_consume_token (parser
->lexer
);
20295 /* Look for the `('. */
20296 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20298 /* Peek at the next token. */
20299 token
= cp_lexer_peek_token (parser
->lexer
);
20300 /* If it's not a `)', then there is a type-id-list. */
20301 if (token
->type
!= CPP_CLOSE_PAREN
)
20303 /* Types may not be defined in an exception-specification. */
20304 saved_message
= parser
->type_definition_forbidden_message
;
20305 parser
->type_definition_forbidden_message
20306 = G_("types may not be defined in an exception-specification");
20307 /* Parse the type-id-list. */
20308 type_id_list
= cp_parser_type_id_list (parser
);
20309 /* Restore the saved message. */
20310 parser
->type_definition_forbidden_message
= saved_message
;
20313 type_id_list
= empty_except_spec
;
20315 /* Look for the `)'. */
20316 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20318 return type_id_list
;
20321 /* Parse an (optional) type-id-list.
20325 type-id-list , type-id ... [opt]
20327 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20328 in the order that the types were presented. */
20331 cp_parser_type_id_list (cp_parser
* parser
)
20333 tree types
= NULL_TREE
;
20340 /* Get the next type-id. */
20341 type
= cp_parser_type_id (parser
);
20342 /* Parse the optional ellipsis. */
20343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20345 /* Consume the `...'. */
20346 cp_lexer_consume_token (parser
->lexer
);
20348 /* Turn the type into a pack expansion expression. */
20349 type
= make_pack_expansion (type
);
20351 /* Add it to the list. */
20352 types
= add_exception_specifier (types
, type
, /*complain=*/1);
20353 /* Peek at the next token. */
20354 token
= cp_lexer_peek_token (parser
->lexer
);
20355 /* If it is not a `,', we are done. */
20356 if (token
->type
!= CPP_COMMA
)
20358 /* Consume the `,'. */
20359 cp_lexer_consume_token (parser
->lexer
);
20362 return nreverse (types
);
20365 /* Parse a try-block.
20368 try compound-statement handler-seq */
20371 cp_parser_try_block (cp_parser
* parser
)
20375 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
20376 try_block
= begin_try_block ();
20377 cp_parser_compound_statement (parser
, NULL
, true, false);
20378 finish_try_block (try_block
);
20379 cp_parser_handler_seq (parser
);
20380 finish_handler_sequence (try_block
);
20385 /* Parse a function-try-block.
20387 function-try-block:
20388 try ctor-initializer [opt] function-body handler-seq */
20391 cp_parser_function_try_block (cp_parser
* parser
)
20393 tree compound_stmt
;
20395 bool ctor_initializer_p
;
20397 /* Look for the `try' keyword. */
20398 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
20400 /* Let the rest of the front end know where we are. */
20401 try_block
= begin_function_try_block (&compound_stmt
);
20402 /* Parse the function-body. */
20403 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
20404 (parser
, /*in_function_try_block=*/true);
20405 /* We're done with the `try' part. */
20406 finish_function_try_block (try_block
);
20407 /* Parse the handlers. */
20408 cp_parser_handler_seq (parser
);
20409 /* We're done with the handlers. */
20410 finish_function_handler_sequence (try_block
, compound_stmt
);
20412 return ctor_initializer_p
;
20415 /* Parse a handler-seq.
20418 handler handler-seq [opt] */
20421 cp_parser_handler_seq (cp_parser
* parser
)
20427 /* Parse the handler. */
20428 cp_parser_handler (parser
);
20429 /* Peek at the next token. */
20430 token
= cp_lexer_peek_token (parser
->lexer
);
20431 /* If it's not `catch' then there are no more handlers. */
20432 if (!cp_parser_is_keyword (token
, RID_CATCH
))
20437 /* Parse a handler.
20440 catch ( exception-declaration ) compound-statement */
20443 cp_parser_handler (cp_parser
* parser
)
20448 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
20449 handler
= begin_handler ();
20450 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20451 declaration
= cp_parser_exception_declaration (parser
);
20452 finish_handler_parms (declaration
, handler
);
20453 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20454 cp_parser_compound_statement (parser
, NULL
, false, false);
20455 finish_handler (handler
);
20458 /* Parse an exception-declaration.
20460 exception-declaration:
20461 type-specifier-seq declarator
20462 type-specifier-seq abstract-declarator
20466 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20467 ellipsis variant is used. */
20470 cp_parser_exception_declaration (cp_parser
* parser
)
20472 cp_decl_specifier_seq type_specifiers
;
20473 cp_declarator
*declarator
;
20474 const char *saved_message
;
20476 /* If it's an ellipsis, it's easy to handle. */
20477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20479 /* Consume the `...' token. */
20480 cp_lexer_consume_token (parser
->lexer
);
20484 /* Types may not be defined in exception-declarations. */
20485 saved_message
= parser
->type_definition_forbidden_message
;
20486 parser
->type_definition_forbidden_message
20487 = G_("types may not be defined in exception-declarations");
20489 /* Parse the type-specifier-seq. */
20490 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
20491 /*is_trailing_return=*/false,
20493 /* If it's a `)', then there is no declarator. */
20494 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
20497 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
20498 /*ctor_dtor_or_conv_p=*/NULL
,
20499 /*parenthesized_p=*/NULL
,
20500 /*member_p=*/false);
20502 /* Restore the saved message. */
20503 parser
->type_definition_forbidden_message
= saved_message
;
20505 if (!type_specifiers
.any_specifiers_p
)
20506 return error_mark_node
;
20508 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
20511 /* Parse a throw-expression.
20514 throw assignment-expression [opt]
20516 Returns a THROW_EXPR representing the throw-expression. */
20519 cp_parser_throw_expression (cp_parser
* parser
)
20524 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
20525 token
= cp_lexer_peek_token (parser
->lexer
);
20526 /* Figure out whether or not there is an assignment-expression
20527 following the "throw" keyword. */
20528 if (token
->type
== CPP_COMMA
20529 || token
->type
== CPP_SEMICOLON
20530 || token
->type
== CPP_CLOSE_PAREN
20531 || token
->type
== CPP_CLOSE_SQUARE
20532 || token
->type
== CPP_CLOSE_BRACE
20533 || token
->type
== CPP_COLON
)
20534 expression
= NULL_TREE
;
20536 expression
= cp_parser_assignment_expression (parser
,
20537 /*cast_p=*/false, NULL
);
20539 return build_throw (expression
);
20542 /* GNU Extensions */
20544 /* Parse an (optional) asm-specification.
20547 asm ( string-literal )
20549 If the asm-specification is present, returns a STRING_CST
20550 corresponding to the string-literal. Otherwise, returns
20554 cp_parser_asm_specification_opt (cp_parser
* parser
)
20557 tree asm_specification
;
20559 /* Peek at the next token. */
20560 token
= cp_lexer_peek_token (parser
->lexer
);
20561 /* If the next token isn't the `asm' keyword, then there's no
20562 asm-specification. */
20563 if (!cp_parser_is_keyword (token
, RID_ASM
))
20566 /* Consume the `asm' token. */
20567 cp_lexer_consume_token (parser
->lexer
);
20568 /* Look for the `('. */
20569 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20571 /* Look for the string-literal. */
20572 asm_specification
= cp_parser_string_literal (parser
, false, false);
20574 /* Look for the `)'. */
20575 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20577 return asm_specification
;
20580 /* Parse an asm-operand-list.
20584 asm-operand-list , asm-operand
20587 string-literal ( expression )
20588 [ string-literal ] string-literal ( expression )
20590 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20591 each node is the expression. The TREE_PURPOSE is itself a
20592 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20593 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20594 is a STRING_CST for the string literal before the parenthesis. Returns
20595 ERROR_MARK_NODE if any of the operands are invalid. */
20598 cp_parser_asm_operand_list (cp_parser
* parser
)
20600 tree asm_operands
= NULL_TREE
;
20601 bool invalid_operands
= false;
20605 tree string_literal
;
20609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
20611 /* Consume the `[' token. */
20612 cp_lexer_consume_token (parser
->lexer
);
20613 /* Read the operand name. */
20614 name
= cp_parser_identifier (parser
);
20615 if (name
!= error_mark_node
)
20616 name
= build_string (IDENTIFIER_LENGTH (name
),
20617 IDENTIFIER_POINTER (name
));
20618 /* Look for the closing `]'. */
20619 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
20623 /* Look for the string-literal. */
20624 string_literal
= cp_parser_string_literal (parser
, false, false);
20626 /* Look for the `('. */
20627 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20628 /* Parse the expression. */
20629 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
20630 /* Look for the `)'. */
20631 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20633 if (name
== error_mark_node
20634 || string_literal
== error_mark_node
20635 || expression
== error_mark_node
)
20636 invalid_operands
= true;
20638 /* Add this operand to the list. */
20639 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
20642 /* If the next token is not a `,', there are no more
20644 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20646 /* Consume the `,'. */
20647 cp_lexer_consume_token (parser
->lexer
);
20650 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
20653 /* Parse an asm-clobber-list.
20657 asm-clobber-list , string-literal
20659 Returns a TREE_LIST, indicating the clobbers in the order that they
20660 appeared. The TREE_VALUE of each node is a STRING_CST. */
20663 cp_parser_asm_clobber_list (cp_parser
* parser
)
20665 tree clobbers
= NULL_TREE
;
20669 tree string_literal
;
20671 /* Look for the string literal. */
20672 string_literal
= cp_parser_string_literal (parser
, false, false);
20673 /* Add it to the list. */
20674 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
20675 /* If the next token is not a `,', then the list is
20677 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20679 /* Consume the `,' token. */
20680 cp_lexer_consume_token (parser
->lexer
);
20686 /* Parse an asm-label-list.
20690 asm-label-list , identifier
20692 Returns a TREE_LIST, indicating the labels in the order that they
20693 appeared. The TREE_VALUE of each node is a label. */
20696 cp_parser_asm_label_list (cp_parser
* parser
)
20698 tree labels
= NULL_TREE
;
20702 tree identifier
, label
, name
;
20704 /* Look for the identifier. */
20705 identifier
= cp_parser_identifier (parser
);
20706 if (!error_operand_p (identifier
))
20708 label
= lookup_label (identifier
);
20709 if (TREE_CODE (label
) == LABEL_DECL
)
20711 TREE_USED (label
) = 1;
20712 check_goto (label
);
20713 name
= build_string (IDENTIFIER_LENGTH (identifier
),
20714 IDENTIFIER_POINTER (identifier
));
20715 labels
= tree_cons (name
, label
, labels
);
20718 /* If the next token is not a `,', then the list is
20720 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20722 /* Consume the `,' token. */
20723 cp_lexer_consume_token (parser
->lexer
);
20726 return nreverse (labels
);
20729 /* Return TRUE iff the next tokens in the stream are possibly the
20730 beginning of a GNU extension attribute. */
20733 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
20735 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
20738 /* Return TRUE iff the next tokens in the stream are possibly the
20739 beginning of a standard C++-11 attribute specifier. */
20742 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
20744 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
20747 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20748 beginning of a standard C++-11 attribute specifier. */
20751 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
20753 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
20755 return (cxx_dialect
>= cxx0x
20756 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
20757 || (token
->type
== CPP_OPEN_SQUARE
20758 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
20759 && token
->type
== CPP_OPEN_SQUARE
)));
20762 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20763 beginning of a GNU extension attribute. */
20766 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
20768 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
20770 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
20773 /* Return true iff the next tokens can be the beginning of either a
20774 GNU attribute list, or a standard C++11 attribute sequence. */
20777 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
20779 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
20780 || cp_next_tokens_can_be_std_attribute_p (parser
));
20783 /* Return true iff the next Nth tokens can be the beginning of either
20784 a GNU attribute list, or a standard C++11 attribute sequence. */
20787 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
20789 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
20790 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
20793 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20794 of GNU attributes, or return NULL. */
20797 cp_parser_attributes_opt (cp_parser
*parser
)
20799 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
20800 return cp_parser_gnu_attributes_opt (parser
);
20801 return cp_parser_std_attribute_spec_seq (parser
);
20804 /* Parse an (optional) series of attributes.
20807 attributes attribute
20810 __attribute__ (( attribute-list [opt] ))
20812 The return value is as for cp_parser_gnu_attribute_list. */
20815 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
20817 tree attributes
= NULL_TREE
;
20822 tree attribute_list
;
20825 /* Peek at the next token. */
20826 token
= cp_lexer_peek_token (parser
->lexer
);
20827 /* If it's not `__attribute__', then we're done. */
20828 if (token
->keyword
!= RID_ATTRIBUTE
)
20831 /* Consume the `__attribute__' keyword. */
20832 cp_lexer_consume_token (parser
->lexer
);
20833 /* Look for the two `(' tokens. */
20834 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20835 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20837 /* Peek at the next token. */
20838 token
= cp_lexer_peek_token (parser
->lexer
);
20839 if (token
->type
!= CPP_CLOSE_PAREN
)
20840 /* Parse the attribute-list. */
20841 attribute_list
= cp_parser_gnu_attribute_list (parser
);
20843 /* If the next token is a `)', then there is no attribute
20845 attribute_list
= NULL
;
20847 /* Look for the two `)' tokens. */
20848 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
20850 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
20853 cp_parser_skip_to_end_of_statement (parser
);
20855 /* Add these new attributes to the list. */
20856 attributes
= chainon (attributes
, attribute_list
);
20862 /* Parse a GNU attribute-list.
20866 attribute-list , attribute
20870 identifier ( identifier )
20871 identifier ( identifier , expression-list )
20872 identifier ( expression-list )
20874 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20875 to an attribute. The TREE_PURPOSE of each node is the identifier
20876 indicating which attribute is in use. The TREE_VALUE represents
20877 the arguments, if any. */
20880 cp_parser_gnu_attribute_list (cp_parser
* parser
)
20882 tree attribute_list
= NULL_TREE
;
20883 bool save_translate_strings_p
= parser
->translate_strings_p
;
20885 parser
->translate_strings_p
= false;
20892 /* Look for the identifier. We also allow keywords here; for
20893 example `__attribute__ ((const))' is legal. */
20894 token
= cp_lexer_peek_token (parser
->lexer
);
20895 if (token
->type
== CPP_NAME
20896 || token
->type
== CPP_KEYWORD
)
20898 tree arguments
= NULL_TREE
;
20900 /* Consume the token. */
20901 token
= cp_lexer_consume_token (parser
->lexer
);
20903 /* Save away the identifier that indicates which attribute
20905 identifier
= (token
->type
== CPP_KEYWORD
)
20906 /* For keywords, use the canonical spelling, not the
20907 parsed identifier. */
20908 ? ridpointers
[(int) token
->keyword
]
20911 attribute
= build_tree_list (identifier
, NULL_TREE
);
20913 /* Peek at the next token. */
20914 token
= cp_lexer_peek_token (parser
->lexer
);
20915 /* If it's an `(', then parse the attribute arguments. */
20916 if (token
->type
== CPP_OPEN_PAREN
)
20918 vec
<tree
, va_gc
> *vec
;
20919 int attr_flag
= (attribute_takes_identifier_p (identifier
)
20920 ? id_attr
: normal_attr
);
20921 vec
= cp_parser_parenthesized_expression_list
20922 (parser
, attr_flag
, /*cast_p=*/false,
20923 /*allow_expansion_p=*/false,
20924 /*non_constant_p=*/NULL
);
20926 arguments
= error_mark_node
;
20929 arguments
= build_tree_list_vec (vec
);
20930 release_tree_vector (vec
);
20932 /* Save the arguments away. */
20933 TREE_VALUE (attribute
) = arguments
;
20936 if (arguments
!= error_mark_node
)
20938 /* Add this attribute to the list. */
20939 TREE_CHAIN (attribute
) = attribute_list
;
20940 attribute_list
= attribute
;
20943 token
= cp_lexer_peek_token (parser
->lexer
);
20945 /* Now, look for more attributes. If the next token isn't a
20946 `,', we're done. */
20947 if (token
->type
!= CPP_COMMA
)
20950 /* Consume the comma and keep going. */
20951 cp_lexer_consume_token (parser
->lexer
);
20953 parser
->translate_strings_p
= save_translate_strings_p
;
20955 /* We built up the list in reverse order. */
20956 return nreverse (attribute_list
);
20959 /* Parse a standard C++11 attribute.
20961 The returned representation is a TREE_LIST which TREE_PURPOSE is
20962 the scoped name of the attribute, and the TREE_VALUE is its
20965 Note that the scoped name of the attribute is itself a TREE_LIST
20966 which TREE_PURPOSE is the namespace of the attribute, and
20967 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20968 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20969 and which TREE_PURPOSE is directly the attribute name.
20971 Clients of the attribute code should use get_attribute_namespace
20972 and get_attribute_name to get the actual namespace and name of
20973 attributes, regardless of their being GNU or C++11 attributes.
20976 attribute-token attribute-argument-clause [opt]
20980 attribute-scoped-token
20982 attribute-scoped-token:
20983 attribute-namespace :: identifier
20985 attribute-namespace:
20988 attribute-argument-clause:
20989 ( balanced-token-seq )
20991 balanced-token-seq:
20992 balanced-token [opt]
20993 balanced-token-seq balanced-token
20996 ( balanced-token-seq )
20997 [ balanced-token-seq ]
20998 { balanced-token-seq }. */
21001 cp_parser_std_attribute (cp_parser
*parser
)
21003 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
21006 /* First, parse name of the the attribute, a.k.a
21007 attribute-token. */
21009 token
= cp_lexer_peek_token (parser
->lexer
);
21010 if (token
->type
== CPP_NAME
)
21011 attr_id
= token
->u
.value
;
21012 else if (token
->type
== CPP_KEYWORD
)
21013 attr_id
= ridpointers
[(int) token
->keyword
];
21014 else if (token
->flags
& NAMED_OP
)
21015 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
21017 if (attr_id
== NULL_TREE
)
21020 cp_lexer_consume_token (parser
->lexer
);
21022 token
= cp_lexer_peek_token (parser
->lexer
);
21023 if (token
->type
== CPP_SCOPE
)
21025 /* We are seeing a scoped attribute token. */
21027 cp_lexer_consume_token (parser
->lexer
);
21030 token
= cp_lexer_consume_token (parser
->lexer
);
21031 if (token
->type
== CPP_NAME
)
21032 attr_id
= token
->u
.value
;
21033 else if (token
->type
== CPP_KEYWORD
)
21034 attr_id
= ridpointers
[(int) token
->keyword
];
21037 error_at (token
->location
,
21038 "expected an identifier for the attribute name");
21039 return error_mark_node
;
21041 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
21043 token
= cp_lexer_peek_token (parser
->lexer
);
21047 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
21049 /* C++11 noreturn attribute is equivalent to GNU's. */
21050 if (is_attribute_p ("noreturn", attr_id
))
21051 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
21054 /* Now parse the optional argument clause of the attribute. */
21056 if (token
->type
!= CPP_OPEN_PAREN
)
21060 vec
<tree
, va_gc
> *vec
;
21061 int attr_flag
= normal_attr
;
21063 if (attr_ns
== get_identifier ("gnu")
21064 && attribute_takes_identifier_p (attr_id
))
21065 /* A GNU attribute that takes an identifier in parameter. */
21066 attr_flag
= id_attr
;
21068 vec
= cp_parser_parenthesized_expression_list
21069 (parser
, attr_flag
, /*cast_p=*/false,
21070 /*allow_expansion_p=*/true,
21071 /*non_constant_p=*/NULL
);
21073 arguments
= error_mark_node
;
21076 arguments
= build_tree_list_vec (vec
);
21077 release_tree_vector (vec
);
21080 if (arguments
== error_mark_node
)
21081 attribute
= error_mark_node
;
21083 TREE_VALUE (attribute
) = arguments
;
21089 /* Parse a list of standard C++-11 attributes.
21093 attribute-list , attribute[opt]
21095 attribute-list , attribute ...
21099 cp_parser_std_attribute_list (cp_parser
*parser
)
21101 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
21102 cp_token
*token
= NULL
;
21106 attribute
= cp_parser_std_attribute (parser
);
21107 if (attribute
== error_mark_node
)
21109 if (attribute
!= NULL_TREE
)
21111 TREE_CHAIN (attribute
) = attributes
;
21112 attributes
= attribute
;
21114 token
= cp_lexer_peek_token (parser
->lexer
);
21115 if (token
->type
!= CPP_COMMA
)
21117 cp_lexer_consume_token (parser
->lexer
);
21119 attributes
= nreverse (attributes
);
21123 /* Parse a standard C++-11 attribute specifier.
21125 attribute-specifier:
21126 [ [ attribute-list ] ]
21127 alignment-specifier
21129 alignment-specifier:
21130 alignas ( type-id ... [opt] )
21131 alignas ( alignment-expression ... [opt] ). */
21134 cp_parser_std_attribute_spec (cp_parser
*parser
)
21136 tree attributes
= NULL_TREE
;
21137 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21139 if (token
->type
== CPP_OPEN_SQUARE
21140 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
21142 cp_lexer_consume_token (parser
->lexer
);
21143 cp_lexer_consume_token (parser
->lexer
);
21145 attributes
= cp_parser_std_attribute_list (parser
);
21147 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
21148 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
21149 cp_parser_skip_to_end_of_statement (parser
);
21151 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21152 when we are sure that we have actually parsed them. */
21153 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21159 /* Look for an alignment-specifier. */
21161 token
= cp_lexer_peek_token (parser
->lexer
);
21163 if (token
->type
!= CPP_KEYWORD
21164 || token
->keyword
!= RID_ALIGNAS
)
21167 cp_lexer_consume_token (parser
->lexer
);
21168 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
21170 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
21172 cp_parser_error (parser
, "expected %<(%>");
21173 return error_mark_node
;
21176 cp_parser_parse_tentatively (parser
);
21177 alignas_expr
= cp_parser_type_id (parser
);
21179 if (!cp_parser_parse_definitely (parser
))
21181 gcc_assert (alignas_expr
== error_mark_node
21182 || alignas_expr
== NULL_TREE
);
21185 cp_parser_assignment_expression (parser
, /*cast_p=*/false,
21186 /**cp_id_kind=*/NULL
);
21187 if (alignas_expr
== NULL_TREE
21188 || alignas_expr
== error_mark_node
)
21189 return alignas_expr
;
21192 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
21194 cp_parser_error (parser
, "expected %<)%>");
21195 return error_mark_node
;
21198 alignas_expr
= cxx_alignas_expr (alignas_expr
);
21200 /* Build the C++-11 representation of an 'aligned'
21203 build_tree_list (build_tree_list (get_identifier ("gnu"),
21204 get_identifier ("aligned")),
21205 build_tree_list (NULL_TREE
, alignas_expr
));
21211 /* Parse a standard C++-11 attribute-specifier-seq.
21213 attribute-specifier-seq:
21214 attribute-specifier-seq [opt] attribute-specifier
21218 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
21220 tree attr_specs
= NULL
;
21224 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
21225 if (attr_spec
== NULL_TREE
)
21227 if (attr_spec
== error_mark_node
)
21228 return error_mark_node
;
21230 TREE_CHAIN (attr_spec
) = attr_specs
;
21231 attr_specs
= attr_spec
;
21234 attr_specs
= nreverse (attr_specs
);
21238 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21239 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21240 current value of the PEDANTIC flag, regardless of whether or not
21241 the `__extension__' keyword is present. The caller is responsible
21242 for restoring the value of the PEDANTIC flag. */
21245 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
21247 /* Save the old value of the PEDANTIC flag. */
21248 *saved_pedantic
= pedantic
;
21250 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
21252 /* Consume the `__extension__' token. */
21253 cp_lexer_consume_token (parser
->lexer
);
21254 /* We're not being pedantic while the `__extension__' keyword is
21264 /* Parse a label declaration.
21267 __label__ label-declarator-seq ;
21269 label-declarator-seq:
21270 identifier , label-declarator-seq
21274 cp_parser_label_declaration (cp_parser
* parser
)
21276 /* Look for the `__label__' keyword. */
21277 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
21283 /* Look for an identifier. */
21284 identifier
= cp_parser_identifier (parser
);
21285 /* If we failed, stop. */
21286 if (identifier
== error_mark_node
)
21288 /* Declare it as a label. */
21289 finish_label_decl (identifier
);
21290 /* If the next token is a `;', stop. */
21291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21293 /* Look for the `,' separating the label declarations. */
21294 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
21297 /* Look for the final `;'. */
21298 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21301 /* Support Functions */
21303 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21304 NAME should have one of the representations used for an
21305 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21306 is returned. If PARSER->SCOPE is a dependent type, then a
21307 SCOPE_REF is returned.
21309 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21310 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21311 was formed. Abstractly, such entities should not be passed to this
21312 function, because they do not need to be looked up, but it is
21313 simpler to check for this special case here, rather than at the
21316 In cases not explicitly covered above, this function returns a
21317 DECL, OVERLOAD, or baselink representing the result of the lookup.
21318 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21321 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21322 (e.g., "struct") that was used. In that case bindings that do not
21323 refer to types are ignored.
21325 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21328 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21331 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21334 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21335 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21336 NULL_TREE otherwise. */
21339 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
21340 enum tag_types tag_type
,
21343 bool check_dependency
,
21344 tree
*ambiguous_decls
,
21345 location_t name_location
)
21348 tree object_type
= parser
->context
->object_type
;
21350 /* Assume that the lookup will be unambiguous. */
21351 if (ambiguous_decls
)
21352 *ambiguous_decls
= NULL_TREE
;
21354 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21355 no longer valid. Note that if we are parsing tentatively, and
21356 the parse fails, OBJECT_TYPE will be automatically restored. */
21357 parser
->context
->object_type
= NULL_TREE
;
21359 if (name
== error_mark_node
)
21360 return error_mark_node
;
21362 /* A template-id has already been resolved; there is no lookup to
21364 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
21366 if (BASELINK_P (name
))
21368 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
21369 == TEMPLATE_ID_EXPR
);
21373 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21374 it should already have been checked to make sure that the name
21375 used matches the type being destroyed. */
21376 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
21380 /* Figure out to which type this destructor applies. */
21382 type
= parser
->scope
;
21383 else if (object_type
)
21384 type
= object_type
;
21386 type
= current_class_type
;
21387 /* If that's not a class type, there is no destructor. */
21388 if (!type
|| !CLASS_TYPE_P (type
))
21389 return error_mark_node
;
21390 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
21391 lazily_declare_fn (sfk_destructor
, type
);
21392 if (!CLASSTYPE_DESTRUCTORS (type
))
21393 return error_mark_node
;
21394 /* If it was a class type, return the destructor. */
21395 return CLASSTYPE_DESTRUCTORS (type
);
21398 /* By this point, the NAME should be an ordinary identifier. If
21399 the id-expression was a qualified name, the qualifying scope is
21400 stored in PARSER->SCOPE at this point. */
21401 gcc_assert (identifier_p (name
));
21403 /* Perform the lookup. */
21408 if (parser
->scope
== error_mark_node
)
21409 return error_mark_node
;
21411 /* If the SCOPE is dependent, the lookup must be deferred until
21412 the template is instantiated -- unless we are explicitly
21413 looking up names in uninstantiated templates. Even then, we
21414 cannot look up the name if the scope is not a class type; it
21415 might, for example, be a template type parameter. */
21416 dependent_p
= (TYPE_P (parser
->scope
)
21417 && dependent_scope_p (parser
->scope
));
21418 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
21420 /* Defer lookup. */
21421 decl
= error_mark_node
;
21424 tree pushed_scope
= NULL_TREE
;
21426 /* If PARSER->SCOPE is a dependent type, then it must be a
21427 class type, and we must not be checking dependencies;
21428 otherwise, we would have processed this lookup above. So
21429 that PARSER->SCOPE is not considered a dependent base by
21430 lookup_member, we must enter the scope here. */
21432 pushed_scope
= push_scope (parser
->scope
);
21434 /* If the PARSER->SCOPE is a template specialization, it
21435 may be instantiated during name lookup. In that case,
21436 errors may be issued. Even if we rollback the current
21437 tentative parse, those errors are valid. */
21438 decl
= lookup_qualified_name (parser
->scope
, name
,
21439 tag_type
!= none_type
,
21440 /*complain=*/true);
21442 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21443 lookup result and the nested-name-specifier nominates a class C:
21444 * if the name specified after the nested-name-specifier, when
21445 looked up in C, is the injected-class-name of C (Clause 9), or
21446 * if the name specified after the nested-name-specifier is the
21447 same as the identifier or the simple-template-id's template-
21448 name in the last component of the nested-name-specifier,
21449 the name is instead considered to name the constructor of
21450 class C. [ Note: for example, the constructor is not an
21451 acceptable lookup result in an elaborated-type-specifier so
21452 the constructor would not be used in place of the
21453 injected-class-name. --end note ] Such a constructor name
21454 shall be used only in the declarator-id of a declaration that
21455 names a constructor or in a using-declaration. */
21456 if (tag_type
== none_type
21457 && DECL_SELF_REFERENCE_P (decl
)
21458 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
21459 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
21460 tag_type
!= none_type
,
21461 /*complain=*/true);
21463 /* If we have a single function from a using decl, pull it out. */
21464 if (TREE_CODE (decl
) == OVERLOAD
21465 && !really_overloaded_fn (decl
))
21466 decl
= OVL_FUNCTION (decl
);
21469 pop_scope (pushed_scope
);
21472 /* If the scope is a dependent type and either we deferred lookup or
21473 we did lookup but didn't find the name, rememeber the name. */
21474 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
21475 && dependent_type_p (parser
->scope
))
21481 /* The resolution to Core Issue 180 says that `struct
21482 A::B' should be considered a type-name, even if `A'
21484 type
= make_typename_type (parser
->scope
, name
, tag_type
,
21485 /*complain=*/tf_error
);
21486 decl
= TYPE_NAME (type
);
21488 else if (is_template
21489 && (cp_parser_next_token_ends_template_argument_p (parser
)
21490 || cp_lexer_next_token_is (parser
->lexer
,
21492 decl
= make_unbound_class_template (parser
->scope
,
21494 /*complain=*/tf_error
);
21496 decl
= build_qualified_name (/*type=*/NULL_TREE
,
21497 parser
->scope
, name
,
21500 parser
->qualifying_scope
= parser
->scope
;
21501 parser
->object_scope
= NULL_TREE
;
21503 else if (object_type
)
21505 tree object_decl
= NULL_TREE
;
21506 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21507 OBJECT_TYPE is not a class. */
21508 if (CLASS_TYPE_P (object_type
))
21509 /* If the OBJECT_TYPE is a template specialization, it may
21510 be instantiated during name lookup. In that case, errors
21511 may be issued. Even if we rollback the current tentative
21512 parse, those errors are valid. */
21513 object_decl
= lookup_member (object_type
,
21516 tag_type
!= none_type
,
21517 tf_warning_or_error
);
21518 /* Look it up in the enclosing context, too. */
21519 decl
= lookup_name_real (name
, tag_type
!= none_type
,
21521 /*block_p=*/true, is_namespace
, 0);
21522 parser
->object_scope
= object_type
;
21523 parser
->qualifying_scope
= NULL_TREE
;
21525 decl
= object_decl
;
21529 decl
= lookup_name_real (name
, tag_type
!= none_type
,
21531 /*block_p=*/true, is_namespace
, 0);
21532 parser
->qualifying_scope
= NULL_TREE
;
21533 parser
->object_scope
= NULL_TREE
;
21536 /* If the lookup failed, let our caller know. */
21537 if (!decl
|| decl
== error_mark_node
)
21538 return error_mark_node
;
21540 /* Pull out the template from an injected-class-name (or multiple). */
21542 decl
= maybe_get_template_decl_from_type_decl (decl
);
21544 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21545 if (TREE_CODE (decl
) == TREE_LIST
)
21547 if (ambiguous_decls
)
21548 *ambiguous_decls
= decl
;
21549 /* The error message we have to print is too complicated for
21550 cp_parser_error, so we incorporate its actions directly. */
21551 if (!cp_parser_simulate_error (parser
))
21553 error_at (name_location
, "reference to %qD is ambiguous",
21555 print_candidates (decl
);
21557 return error_mark_node
;
21560 gcc_assert (DECL_P (decl
)
21561 || TREE_CODE (decl
) == OVERLOAD
21562 || TREE_CODE (decl
) == SCOPE_REF
21563 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
21564 || BASELINK_P (decl
));
21566 /* If we have resolved the name of a member declaration, check to
21567 see if the declaration is accessible. When the name resolves to
21568 set of overloaded functions, accessibility is checked when
21569 overload resolution is done.
21571 During an explicit instantiation, access is not checked at all,
21572 as per [temp.explicit]. */
21574 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
21576 maybe_record_typedef_use (decl
);
21581 /* Like cp_parser_lookup_name, but for use in the typical case where
21582 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21583 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21586 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
21588 return cp_parser_lookup_name (parser
, name
,
21590 /*is_template=*/false,
21591 /*is_namespace=*/false,
21592 /*check_dependency=*/true,
21593 /*ambiguous_decls=*/NULL
,
21597 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21598 the current context, return the TYPE_DECL. If TAG_NAME_P is
21599 true, the DECL indicates the class being defined in a class-head,
21600 or declared in an elaborated-type-specifier.
21602 Otherwise, return DECL. */
21605 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
21607 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21608 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21611 template <typename T> struct B;
21614 template <typename T> struct A::B {};
21616 Similarly, in an elaborated-type-specifier:
21618 namespace N { struct X{}; }
21621 template <typename T> friend struct N::X;
21624 However, if the DECL refers to a class type, and we are in
21625 the scope of the class, then the name lookup automatically
21626 finds the TYPE_DECL created by build_self_reference rather
21627 than a TEMPLATE_DECL. For example, in:
21629 template <class T> struct S {
21633 there is no need to handle such case. */
21635 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
21636 return DECL_TEMPLATE_RESULT (decl
);
21641 /* If too many, or too few, template-parameter lists apply to the
21642 declarator, issue an error message. Returns TRUE if all went well,
21643 and FALSE otherwise. */
21646 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
21647 cp_declarator
*declarator
,
21648 location_t declarator_location
)
21650 switch (declarator
->kind
)
21654 unsigned num_templates
= 0;
21655 tree scope
= declarator
->u
.id
.qualifying_scope
;
21658 num_templates
= num_template_headers_for_class (scope
);
21659 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
21660 == TEMPLATE_ID_EXPR
)
21661 /* If the DECLARATOR has the form `X<y>' then it uses one
21662 additional level of template parameters. */
21665 return cp_parser_check_template_parameters
21666 (parser
, num_templates
, declarator_location
, declarator
);
21672 case cdk_reference
:
21674 return (cp_parser_check_declarator_template_parameters
21675 (parser
, declarator
->declarator
, declarator_location
));
21681 gcc_unreachable ();
21686 /* NUM_TEMPLATES were used in the current declaration. If that is
21687 invalid, return FALSE and issue an error messages. Otherwise,
21688 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21689 declarator and we can print more accurate diagnostics. */
21692 cp_parser_check_template_parameters (cp_parser
* parser
,
21693 unsigned num_templates
,
21694 location_t location
,
21695 cp_declarator
*declarator
)
21697 /* If there are the same number of template classes and parameter
21698 lists, that's OK. */
21699 if (parser
->num_template_parameter_lists
== num_templates
)
21701 /* If there are more, but only one more, then we are referring to a
21702 member template. That's OK too. */
21703 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
21705 /* If there are more template classes than parameter lists, we have
21708 template <class T> void S<T>::R<T>::f (); */
21709 if (parser
->num_template_parameter_lists
< num_templates
)
21711 if (declarator
&& !current_function_decl
)
21712 error_at (location
, "specializing member %<%T::%E%> "
21713 "requires %<template<>%> syntax",
21714 declarator
->u
.id
.qualifying_scope
,
21715 declarator
->u
.id
.unqualified_name
);
21716 else if (declarator
)
21717 error_at (location
, "invalid declaration of %<%T::%E%>",
21718 declarator
->u
.id
.qualifying_scope
,
21719 declarator
->u
.id
.unqualified_name
);
21721 error_at (location
, "too few template-parameter-lists");
21724 /* Otherwise, there are too many template parameter lists. We have
21727 template <class T> template <class U> void S::f(); */
21728 error_at (location
, "too many template-parameter-lists");
21732 /* Parse an optional `::' token indicating that the following name is
21733 from the global namespace. If so, PARSER->SCOPE is set to the
21734 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21735 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21736 Returns the new value of PARSER->SCOPE, if the `::' token is
21737 present, and NULL_TREE otherwise. */
21740 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
21744 /* Peek at the next token. */
21745 token
= cp_lexer_peek_token (parser
->lexer
);
21746 /* If we're looking at a `::' token then we're starting from the
21747 global namespace, not our current location. */
21748 if (token
->type
== CPP_SCOPE
)
21750 /* Consume the `::' token. */
21751 cp_lexer_consume_token (parser
->lexer
);
21752 /* Set the SCOPE so that we know where to start the lookup. */
21753 parser
->scope
= global_namespace
;
21754 parser
->qualifying_scope
= global_namespace
;
21755 parser
->object_scope
= NULL_TREE
;
21757 return parser
->scope
;
21759 else if (!current_scope_valid_p
)
21761 parser
->scope
= NULL_TREE
;
21762 parser
->qualifying_scope
= NULL_TREE
;
21763 parser
->object_scope
= NULL_TREE
;
21769 /* Returns TRUE if the upcoming token sequence is the start of a
21770 constructor declarator. If FRIEND_P is true, the declarator is
21771 preceded by the `friend' specifier. */
21774 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
21776 bool constructor_p
;
21777 tree nested_name_specifier
;
21778 cp_token
*next_token
;
21780 /* The common case is that this is not a constructor declarator, so
21781 try to avoid doing lots of work if at all possible. It's not
21782 valid declare a constructor at function scope. */
21783 if (parser
->in_function_body
)
21785 /* And only certain tokens can begin a constructor declarator. */
21786 next_token
= cp_lexer_peek_token (parser
->lexer
);
21787 if (next_token
->type
!= CPP_NAME
21788 && next_token
->type
!= CPP_SCOPE
21789 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
21790 && next_token
->type
!= CPP_TEMPLATE_ID
)
21793 /* Parse tentatively; we are going to roll back all of the tokens
21795 cp_parser_parse_tentatively (parser
);
21796 /* Assume that we are looking at a constructor declarator. */
21797 constructor_p
= true;
21799 /* Look for the optional `::' operator. */
21800 cp_parser_global_scope_opt (parser
,
21801 /*current_scope_valid_p=*/false);
21802 /* Look for the nested-name-specifier. */
21803 nested_name_specifier
21804 = (cp_parser_nested_name_specifier_opt (parser
,
21805 /*typename_keyword_p=*/false,
21806 /*check_dependency_p=*/false,
21808 /*is_declaration=*/false));
21809 /* Outside of a class-specifier, there must be a
21810 nested-name-specifier. */
21811 if (!nested_name_specifier
&&
21812 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
21814 constructor_p
= false;
21815 else if (nested_name_specifier
== error_mark_node
)
21816 constructor_p
= false;
21818 /* If we have a class scope, this is easy; DR 147 says that S::S always
21819 names the constructor, and no other qualified name could. */
21820 if (constructor_p
&& nested_name_specifier
21821 && CLASS_TYPE_P (nested_name_specifier
))
21823 tree id
= cp_parser_unqualified_id (parser
,
21824 /*template_keyword_p=*/false,
21825 /*check_dependency_p=*/false,
21826 /*declarator_p=*/true,
21827 /*optional_p=*/false);
21828 if (is_overloaded_fn (id
))
21829 id
= DECL_NAME (get_first_fn (id
));
21830 if (!constructor_name_p (id
, nested_name_specifier
))
21831 constructor_p
= false;
21833 /* If we still think that this might be a constructor-declarator,
21834 look for a class-name. */
21835 else if (constructor_p
)
21839 template <typename T> struct S {
21843 we must recognize that the nested `S' names a class. */
21845 type_decl
= cp_parser_class_name (parser
,
21846 /*typename_keyword_p=*/false,
21847 /*template_keyword_p=*/false,
21849 /*check_dependency_p=*/false,
21850 /*class_head_p=*/false,
21851 /*is_declaration=*/false);
21852 /* If there was no class-name, then this is not a constructor. */
21853 constructor_p
= !cp_parser_error_occurred (parser
);
21855 /* If we're still considering a constructor, we have to see a `(',
21856 to begin the parameter-declaration-clause, followed by either a
21857 `)', an `...', or a decl-specifier. We need to check for a
21858 type-specifier to avoid being fooled into thinking that:
21862 is a constructor. (It is actually a function named `f' that
21863 takes one parameter (of type `int') and returns a value of type
21866 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
21867 constructor_p
= false;
21870 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
21871 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
21872 /* A parameter declaration begins with a decl-specifier,
21873 which is either the "attribute" keyword, a storage class
21874 specifier, or (usually) a type-specifier. */
21875 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
21878 tree pushed_scope
= NULL_TREE
;
21879 unsigned saved_num_template_parameter_lists
;
21881 /* Names appearing in the type-specifier should be looked up
21882 in the scope of the class. */
21883 if (current_class_type
)
21887 type
= TREE_TYPE (type_decl
);
21888 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21890 type
= resolve_typename_type (type
,
21891 /*only_current_p=*/false);
21892 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21894 cp_parser_abort_tentative_parse (parser
);
21898 pushed_scope
= push_scope (type
);
21901 /* Inside the constructor parameter list, surrounding
21902 template-parameter-lists do not apply. */
21903 saved_num_template_parameter_lists
21904 = parser
->num_template_parameter_lists
;
21905 parser
->num_template_parameter_lists
= 0;
21907 /* Look for the type-specifier. */
21908 cp_parser_type_specifier (parser
,
21909 CP_PARSER_FLAGS_NONE
,
21910 /*decl_specs=*/NULL
,
21911 /*is_declarator=*/true,
21912 /*declares_class_or_enum=*/NULL
,
21913 /*is_cv_qualifier=*/NULL
);
21915 parser
->num_template_parameter_lists
21916 = saved_num_template_parameter_lists
;
21918 /* Leave the scope of the class. */
21920 pop_scope (pushed_scope
);
21922 constructor_p
= !cp_parser_error_occurred (parser
);
21926 /* We did not really want to consume any tokens. */
21927 cp_parser_abort_tentative_parse (parser
);
21929 return constructor_p
;
21932 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21933 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21934 they must be performed once we are in the scope of the function.
21936 Returns the function defined. */
21939 cp_parser_function_definition_from_specifiers_and_declarator
21940 (cp_parser
* parser
,
21941 cp_decl_specifier_seq
*decl_specifiers
,
21943 const cp_declarator
*declarator
)
21948 /* Begin the function-definition. */
21949 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
21951 /* The things we're about to see are not directly qualified by any
21952 template headers we've seen thus far. */
21953 reset_specialization ();
21955 /* If there were names looked up in the decl-specifier-seq that we
21956 did not check, check them now. We must wait until we are in the
21957 scope of the function to perform the checks, since the function
21958 might be a friend. */
21959 perform_deferred_access_checks (tf_warning_or_error
);
21963 /* Skip the entire function. */
21964 cp_parser_skip_to_end_of_block_or_statement (parser
);
21965 fn
= error_mark_node
;
21967 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
21969 /* Seen already, skip it. An error message has already been output. */
21970 cp_parser_skip_to_end_of_block_or_statement (parser
);
21971 fn
= current_function_decl
;
21972 current_function_decl
= NULL_TREE
;
21973 /* If this is a function from a class, pop the nested class. */
21974 if (current_class_name
)
21975 pop_nested_class ();
21980 if (DECL_DECLARED_INLINE_P (current_function_decl
))
21981 tv
= TV_PARSE_INLINE
;
21983 tv
= TV_PARSE_FUNC
;
21985 fn
= cp_parser_function_definition_after_declarator (parser
,
21986 /*inline_p=*/false);
21993 /* Parse the part of a function-definition that follows the
21994 declarator. INLINE_P is TRUE iff this function is an inline
21995 function defined within a class-specifier.
21997 Returns the function defined. */
22000 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
22004 bool ctor_initializer_p
= false;
22005 bool saved_in_unbraced_linkage_specification_p
;
22006 bool saved_in_function_body
;
22007 unsigned saved_num_template_parameter_lists
;
22010 saved_in_function_body
= parser
->in_function_body
;
22011 parser
->in_function_body
= true;
22012 /* If the next token is `return', then the code may be trying to
22013 make use of the "named return value" extension that G++ used to
22015 token
= cp_lexer_peek_token (parser
->lexer
);
22016 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
22018 /* Consume the `return' keyword. */
22019 cp_lexer_consume_token (parser
->lexer
);
22020 /* Look for the identifier that indicates what value is to be
22022 cp_parser_identifier (parser
);
22023 /* Issue an error message. */
22024 error_at (token
->location
,
22025 "named return values are no longer supported");
22026 /* Skip tokens until we reach the start of the function body. */
22029 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22030 if (token
->type
== CPP_OPEN_BRACE
22031 || token
->type
== CPP_EOF
22032 || token
->type
== CPP_PRAGMA_EOL
)
22034 cp_lexer_consume_token (parser
->lexer
);
22037 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22038 anything declared inside `f'. */
22039 saved_in_unbraced_linkage_specification_p
22040 = parser
->in_unbraced_linkage_specification_p
;
22041 parser
->in_unbraced_linkage_specification_p
= false;
22042 /* Inside the function, surrounding template-parameter-lists do not
22044 saved_num_template_parameter_lists
22045 = parser
->num_template_parameter_lists
;
22046 parser
->num_template_parameter_lists
= 0;
22048 start_lambda_scope (current_function_decl
);
22050 /* If the next token is `try', `__transaction_atomic', or
22051 `__transaction_relaxed`, then we are looking at either function-try-block
22052 or function-transaction-block. Note that all of these include the
22054 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
22055 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22056 RID_TRANSACTION_ATOMIC
);
22057 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
22058 RID_TRANSACTION_RELAXED
))
22059 ctor_initializer_p
= cp_parser_function_transaction (parser
,
22060 RID_TRANSACTION_RELAXED
);
22061 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
22062 ctor_initializer_p
= cp_parser_function_try_block (parser
);
22064 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
22065 (parser
, /*in_function_try_block=*/false);
22067 finish_lambda_scope ();
22069 /* Finish the function. */
22070 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
22071 (inline_p
? 2 : 0));
22072 /* Generate code for it, if necessary. */
22073 expand_or_defer_fn (fn
);
22074 /* Restore the saved values. */
22075 parser
->in_unbraced_linkage_specification_p
22076 = saved_in_unbraced_linkage_specification_p
;
22077 parser
->num_template_parameter_lists
22078 = saved_num_template_parameter_lists
;
22079 parser
->in_function_body
= saved_in_function_body
;
22084 /* Parse a template-declaration, assuming that the `export' (and
22085 `extern') keywords, if present, has already been scanned. MEMBER_P
22086 is as for cp_parser_template_declaration. */
22089 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
22091 tree decl
= NULL_TREE
;
22092 vec
<deferred_access_check
, va_gc
> *checks
;
22093 tree parameter_list
;
22094 bool friend_p
= false;
22095 bool need_lang_pop
;
22098 /* Look for the `template' keyword. */
22099 token
= cp_lexer_peek_token (parser
->lexer
);
22100 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
22104 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
22106 if (at_class_scope_p () && current_function_decl
)
22108 /* 14.5.2.2 [temp.mem]
22110 A local class shall not have member templates. */
22111 error_at (token
->location
,
22112 "invalid declaration of member template in local class");
22113 cp_parser_skip_to_end_of_block_or_statement (parser
);
22118 A template ... shall not have C linkage. */
22119 if (current_lang_name
== lang_name_c
)
22121 error_at (token
->location
, "template with C linkage");
22122 /* Give it C++ linkage to avoid confusing other parts of the
22124 push_lang_context (lang_name_cplusplus
);
22125 need_lang_pop
= true;
22128 need_lang_pop
= false;
22130 /* We cannot perform access checks on the template parameter
22131 declarations until we know what is being declared, just as we
22132 cannot check the decl-specifier list. */
22133 push_deferring_access_checks (dk_deferred
);
22135 /* If the next token is `>', then we have an invalid
22136 specialization. Rather than complain about an invalid template
22137 parameter, issue an error message here. */
22138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
22140 cp_parser_error (parser
, "invalid explicit specialization");
22141 begin_specialization ();
22142 parameter_list
= NULL_TREE
;
22146 /* Parse the template parameters. */
22147 parameter_list
= cp_parser_template_parameter_list (parser
);
22150 /* Get the deferred access checks from the parameter list. These
22151 will be checked once we know what is being declared, as for a
22152 member template the checks must be performed in the scope of the
22153 class containing the member. */
22154 checks
= get_deferred_access_checks ();
22156 /* Look for the `>'. */
22157 cp_parser_skip_to_end_of_template_parameter_list (parser
);
22158 /* We just processed one more parameter list. */
22159 ++parser
->num_template_parameter_lists
;
22160 /* If the next token is `template', there are more template
22162 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
22164 cp_parser_template_declaration_after_export (parser
, member_p
);
22165 else if (cxx_dialect
>= cxx0x
22166 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
22167 decl
= cp_parser_alias_declaration (parser
);
22170 /* There are no access checks when parsing a template, as we do not
22171 know if a specialization will be a friend. */
22172 push_deferring_access_checks (dk_no_check
);
22173 token
= cp_lexer_peek_token (parser
->lexer
);
22174 decl
= cp_parser_single_declaration (parser
,
22177 /*explicit_specialization_p=*/false,
22179 pop_deferring_access_checks ();
22181 /* If this is a member template declaration, let the front
22183 if (member_p
&& !friend_p
&& decl
)
22185 if (TREE_CODE (decl
) == TYPE_DECL
)
22186 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
22188 decl
= finish_member_template_decl (decl
);
22190 else if (friend_p
&& decl
22191 && DECL_DECLARES_TYPE_P (decl
))
22192 make_friend_class (current_class_type
, TREE_TYPE (decl
),
22193 /*complain=*/true);
22195 /* We are done with the current parameter list. */
22196 --parser
->num_template_parameter_lists
;
22198 pop_deferring_access_checks ();
22201 finish_template_decl (parameter_list
);
22203 /* Check the template arguments for a literal operator template. */
22205 && DECL_DECLARES_FUNCTION_P (decl
)
22206 && UDLIT_OPER_P (DECL_NAME (decl
)))
22209 if (parameter_list
== NULL_TREE
)
22213 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
22214 if (num_parms
== 1)
22216 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
22217 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
22218 if (TREE_TYPE (parm
) != char_type_node
22219 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
22222 else if (num_parms
== 2 && cxx_dialect
>= cxx1y
)
22224 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
22225 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
22226 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
22227 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
22228 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
22229 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
22236 error ("literal operator template %qD has invalid parameter list."
22237 " Expected non-type template argument pack <char...>"
22238 " or <typename CharT, CharT...>",
22241 /* Register member declarations. */
22242 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
22243 finish_member_declaration (decl
);
22244 /* For the erroneous case of a template with C linkage, we pushed an
22245 implicit C++ linkage scope; exit that scope now. */
22247 pop_lang_context ();
22248 /* If DECL is a function template, we must return to parse it later.
22249 (Even though there is no definition, there might be default
22250 arguments that need handling.) */
22251 if (member_p
&& decl
22252 && DECL_DECLARES_FUNCTION_P (decl
))
22253 vec_safe_push (unparsed_funs_with_definitions
, decl
);
22256 /* Perform the deferred access checks from a template-parameter-list.
22257 CHECKS is a TREE_LIST of access checks, as returned by
22258 get_deferred_access_checks. */
22261 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
22263 ++processing_template_parmlist
;
22264 perform_access_checks (checks
, tf_warning_or_error
);
22265 --processing_template_parmlist
;
22268 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22269 `function-definition' sequence that follows a template header.
22270 If MEMBER_P is true, this declaration appears in a class scope.
22272 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22273 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22276 cp_parser_single_declaration (cp_parser
* parser
,
22277 vec
<deferred_access_check
, va_gc
> *checks
,
22279 bool explicit_specialization_p
,
22282 int declares_class_or_enum
;
22283 tree decl
= NULL_TREE
;
22284 cp_decl_specifier_seq decl_specifiers
;
22285 bool function_definition_p
= false;
22286 cp_token
*decl_spec_token_start
;
22288 /* This function is only used when processing a template
22290 gcc_assert (innermost_scope_kind () == sk_template_parms
22291 || innermost_scope_kind () == sk_template_spec
);
22293 /* Defer access checks until we know what is being declared. */
22294 push_deferring_access_checks (dk_deferred
);
22296 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22298 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22299 cp_parser_decl_specifier_seq (parser
,
22300 CP_PARSER_FLAGS_OPTIONAL
,
22302 &declares_class_or_enum
);
22304 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
22306 /* There are no template typedefs. */
22307 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
22309 error_at (decl_spec_token_start
->location
,
22310 "template declaration of %<typedef%>");
22311 decl
= error_mark_node
;
22314 /* Gather up the access checks that occurred the
22315 decl-specifier-seq. */
22316 stop_deferring_access_checks ();
22318 /* Check for the declaration of a template class. */
22319 if (declares_class_or_enum
)
22321 if (cp_parser_declares_only_class_p (parser
))
22323 decl
= shadow_tag (&decl_specifiers
);
22328 friend template <typename T> struct A<T>::B;
22331 A<T>::B will be represented by a TYPENAME_TYPE, and
22332 therefore not recognized by shadow_tag. */
22333 if (friend_p
&& *friend_p
22335 && decl_specifiers
.type
22336 && TYPE_P (decl_specifiers
.type
))
22337 decl
= decl_specifiers
.type
;
22339 if (decl
&& decl
!= error_mark_node
)
22340 decl
= TYPE_NAME (decl
);
22342 decl
= error_mark_node
;
22344 /* Perform access checks for template parameters. */
22345 cp_parser_perform_template_parameter_access_checks (checks
);
22349 /* Complain about missing 'typename' or other invalid type names. */
22350 if (!decl_specifiers
.any_type_specifiers_p
22351 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22353 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22354 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22355 the rest of this declaration. */
22356 decl
= error_mark_node
;
22360 /* If it's not a template class, try for a template function. If
22361 the next token is a `;', then this declaration does not declare
22362 anything. But, if there were errors in the decl-specifiers, then
22363 the error might well have come from an attempted class-specifier.
22364 In that case, there's no need to warn about a missing declarator. */
22366 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
22367 || decl_specifiers
.type
!= error_mark_node
))
22369 decl
= cp_parser_init_declarator (parser
,
22372 /*function_definition_allowed_p=*/true,
22374 declares_class_or_enum
,
22375 &function_definition_p
,
22378 /* 7.1.1-1 [dcl.stc]
22380 A storage-class-specifier shall not be specified in an explicit
22381 specialization... */
22383 && explicit_specialization_p
22384 && decl_specifiers
.storage_class
!= sc_none
)
22386 error_at (decl_spec_token_start
->location
,
22387 "explicit template specialization cannot have a storage class");
22388 decl
= error_mark_node
;
22391 if (decl
&& VAR_P (decl
))
22392 check_template_variable (decl
);
22395 /* Look for a trailing `;' after the declaration. */
22396 if (!function_definition_p
22397 && (decl
== error_mark_node
22398 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
22399 cp_parser_skip_to_end_of_block_or_statement (parser
);
22402 pop_deferring_access_checks ();
22404 /* Clear any current qualification; whatever comes next is the start
22405 of something new. */
22406 parser
->scope
= NULL_TREE
;
22407 parser
->qualifying_scope
= NULL_TREE
;
22408 parser
->object_scope
= NULL_TREE
;
22413 /* Parse a cast-expression that is not the operand of a unary "&". */
22416 cp_parser_simple_cast_expression (cp_parser
*parser
)
22418 return cp_parser_cast_expression (parser
, /*address_p=*/false,
22419 /*cast_p=*/false, /*decltype*/false, NULL
);
22422 /* Parse a functional cast to TYPE. Returns an expression
22423 representing the cast. */
22426 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
22428 vec
<tree
, va_gc
> *vec
;
22429 tree expression_list
;
22433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22435 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22436 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
22437 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
22438 if (TREE_CODE (type
) == TYPE_DECL
)
22439 type
= TREE_TYPE (type
);
22440 return finish_compound_literal (type
, expression_list
,
22441 tf_warning_or_error
);
22445 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22447 /*allow_expansion_p=*/true,
22448 /*non_constant_p=*/NULL
);
22450 expression_list
= error_mark_node
;
22453 expression_list
= build_tree_list_vec (vec
);
22454 release_tree_vector (vec
);
22457 cast
= build_functional_cast (type
, expression_list
,
22458 tf_warning_or_error
);
22459 /* [expr.const]/1: In an integral constant expression "only type
22460 conversions to integral or enumeration type can be used". */
22461 if (TREE_CODE (type
) == TYPE_DECL
)
22462 type
= TREE_TYPE (type
);
22463 if (cast
!= error_mark_node
22464 && !cast_valid_in_integral_constant_expression_p (type
)
22465 && cp_parser_non_integral_constant_expression (parser
,
22467 return error_mark_node
;
22471 /* Save the tokens that make up the body of a member function defined
22472 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22473 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22474 specifiers applied to the declaration. Returns the FUNCTION_DECL
22475 for the member function. */
22478 cp_parser_save_member_function_body (cp_parser
* parser
,
22479 cp_decl_specifier_seq
*decl_specifiers
,
22480 cp_declarator
*declarator
,
22487 /* Create the FUNCTION_DECL. */
22488 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
22489 /* If something went badly wrong, bail out now. */
22490 if (fn
== error_mark_node
)
22492 /* If there's a function-body, skip it. */
22493 if (cp_parser_token_starts_function_definition_p
22494 (cp_lexer_peek_token (parser
->lexer
)))
22495 cp_parser_skip_to_end_of_block_or_statement (parser
);
22496 return error_mark_node
;
22499 /* Remember it, if there default args to post process. */
22500 cp_parser_save_default_args (parser
, fn
);
22502 /* Save away the tokens that make up the body of the
22504 first
= parser
->lexer
->next_token
;
22505 /* We can have braced-init-list mem-initializers before the fn body. */
22506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22508 cp_lexer_consume_token (parser
->lexer
);
22509 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22510 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
22512 /* cache_group will stop after an un-nested { } pair, too. */
22513 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
22516 /* variadic mem-inits have ... after the ')'. */
22517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22518 cp_lexer_consume_token (parser
->lexer
);
22521 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
22522 /* Handle function try blocks. */
22523 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
22524 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
22525 last
= parser
->lexer
->next_token
;
22527 /* Save away the inline definition; we will process it when the
22528 class is complete. */
22529 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
22530 DECL_PENDING_INLINE_P (fn
) = 1;
22532 /* We need to know that this was defined in the class, so that
22533 friend templates are handled correctly. */
22534 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
22536 /* Add FN to the queue of functions to be parsed later. */
22537 vec_safe_push (unparsed_funs_with_definitions
, fn
);
22542 /* Save the tokens that make up the in-class initializer for a non-static
22543 data member. Returns a DEFAULT_ARG. */
22546 cp_parser_save_nsdmi (cp_parser
* parser
)
22548 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
22551 /* Parse a template-argument-list, as well as the trailing ">" (but
22552 not the opening "<"). See cp_parser_template_argument_list for the
22556 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
22560 tree saved_qualifying_scope
;
22561 tree saved_object_scope
;
22562 bool saved_greater_than_is_operator_p
;
22563 int saved_unevaluated_operand
;
22564 int saved_inhibit_evaluation_warnings
;
22568 When parsing a template-id, the first non-nested `>' is taken as
22569 the end of the template-argument-list rather than a greater-than
22571 saved_greater_than_is_operator_p
22572 = parser
->greater_than_is_operator_p
;
22573 parser
->greater_than_is_operator_p
= false;
22574 /* Parsing the argument list may modify SCOPE, so we save it
22576 saved_scope
= parser
->scope
;
22577 saved_qualifying_scope
= parser
->qualifying_scope
;
22578 saved_object_scope
= parser
->object_scope
;
22579 /* We need to evaluate the template arguments, even though this
22580 template-id may be nested within a "sizeof". */
22581 saved_unevaluated_operand
= cp_unevaluated_operand
;
22582 cp_unevaluated_operand
= 0;
22583 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22584 c_inhibit_evaluation_warnings
= 0;
22585 /* Parse the template-argument-list itself. */
22586 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
22587 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
22588 arguments
= NULL_TREE
;
22590 arguments
= cp_parser_template_argument_list (parser
);
22591 /* Look for the `>' that ends the template-argument-list. If we find
22592 a '>>' instead, it's probably just a typo. */
22593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
22595 if (cxx_dialect
!= cxx98
)
22597 /* In C++0x, a `>>' in a template argument list or cast
22598 expression is considered to be two separate `>'
22599 tokens. So, change the current token to a `>', but don't
22600 consume it: it will be consumed later when the outer
22601 template argument list (or cast expression) is parsed.
22602 Note that this replacement of `>' for `>>' is necessary
22603 even if we are parsing tentatively: in the tentative
22604 case, after calling
22605 cp_parser_enclosed_template_argument_list we will always
22606 throw away all of the template arguments and the first
22607 closing `>', either because the template argument list
22608 was erroneous or because we are replacing those tokens
22609 with a CPP_TEMPLATE_ID token. The second `>' (which will
22610 not have been thrown away) is needed either to close an
22611 outer template argument list or to complete a new-style
22613 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22614 token
->type
= CPP_GREATER
;
22616 else if (!saved_greater_than_is_operator_p
)
22618 /* If we're in a nested template argument list, the '>>' has
22619 to be a typo for '> >'. We emit the error message, but we
22620 continue parsing and we push a '>' as next token, so that
22621 the argument list will be parsed correctly. Note that the
22622 global source location is still on the token before the
22623 '>>', so we need to say explicitly where we want it. */
22624 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22625 error_at (token
->location
, "%<>>%> should be %<> >%> "
22626 "within a nested template argument list");
22628 token
->type
= CPP_GREATER
;
22632 /* If this is not a nested template argument list, the '>>'
22633 is a typo for '>'. Emit an error message and continue.
22634 Same deal about the token location, but here we can get it
22635 right by consuming the '>>' before issuing the diagnostic. */
22636 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
22637 error_at (token
->location
,
22638 "spurious %<>>%>, use %<>%> to terminate "
22639 "a template argument list");
22643 cp_parser_skip_to_end_of_template_parameter_list (parser
);
22644 /* The `>' token might be a greater-than operator again now. */
22645 parser
->greater_than_is_operator_p
22646 = saved_greater_than_is_operator_p
;
22647 /* Restore the SAVED_SCOPE. */
22648 parser
->scope
= saved_scope
;
22649 parser
->qualifying_scope
= saved_qualifying_scope
;
22650 parser
->object_scope
= saved_object_scope
;
22651 cp_unevaluated_operand
= saved_unevaluated_operand
;
22652 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
22657 /* MEMBER_FUNCTION is a member function, or a friend. If default
22658 arguments, or the body of the function have not yet been parsed,
22662 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
22664 timevar_push (TV_PARSE_INMETH
);
22665 /* If this member is a template, get the underlying
22667 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
22668 member_function
= DECL_TEMPLATE_RESULT (member_function
);
22670 /* There should not be any class definitions in progress at this
22671 point; the bodies of members are only parsed outside of all class
22673 gcc_assert (parser
->num_classes_being_defined
== 0);
22674 /* While we're parsing the member functions we might encounter more
22675 classes. We want to handle them right away, but we don't want
22676 them getting mixed up with functions that are currently in the
22678 push_unparsed_function_queues (parser
);
22680 /* Make sure that any template parameters are in scope. */
22681 maybe_begin_member_template_processing (member_function
);
22683 /* If the body of the function has not yet been parsed, parse it
22685 if (DECL_PENDING_INLINE_P (member_function
))
22687 tree function_scope
;
22688 cp_token_cache
*tokens
;
22690 /* The function is no longer pending; we are processing it. */
22691 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
22692 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
22693 DECL_PENDING_INLINE_P (member_function
) = 0;
22695 /* If this is a local class, enter the scope of the containing
22697 function_scope
= current_function_decl
;
22698 if (function_scope
)
22699 push_function_context ();
22701 /* Push the body of the function onto the lexer stack. */
22702 cp_parser_push_lexer_for_tokens (parser
, tokens
);
22704 /* Let the front end know that we going to be defining this
22706 start_preparsed_function (member_function
, NULL_TREE
,
22707 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
22709 /* Don't do access checking if it is a templated function. */
22710 if (processing_template_decl
)
22711 push_deferring_access_checks (dk_no_check
);
22713 /* Now, parse the body of the function. */
22714 cp_parser_function_definition_after_declarator (parser
,
22715 /*inline_p=*/true);
22717 if (processing_template_decl
)
22718 pop_deferring_access_checks ();
22720 /* Leave the scope of the containing function. */
22721 if (function_scope
)
22722 pop_function_context ();
22723 cp_parser_pop_lexer (parser
);
22726 /* Remove any template parameters from the symbol table. */
22727 maybe_end_member_template_processing ();
22729 /* Restore the queue. */
22730 pop_unparsed_function_queues (parser
);
22731 timevar_pop (TV_PARSE_INMETH
);
22734 /* If DECL contains any default args, remember it on the unparsed
22735 functions queue. */
22738 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
22742 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
22744 probe
= TREE_CHAIN (probe
))
22745 if (TREE_PURPOSE (probe
))
22747 cp_default_arg_entry entry
= {current_class_type
, decl
};
22748 vec_safe_push (unparsed_funs_with_default_args
, entry
);
22753 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22754 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22755 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22756 from the parameter-type-list. */
22759 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
22760 tree default_arg
, tree parmtype
)
22762 cp_token_cache
*tokens
;
22766 if (default_arg
== error_mark_node
)
22767 return error_mark_node
;
22769 /* Push the saved tokens for the default argument onto the parser's
22771 tokens
= DEFARG_TOKENS (default_arg
);
22772 cp_parser_push_lexer_for_tokens (parser
, tokens
);
22774 start_lambda_scope (decl
);
22776 /* Parse the default argument. */
22777 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
22778 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
22779 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22781 finish_lambda_scope ();
22783 if (parsed_arg
== error_mark_node
)
22784 cp_parser_skip_to_end_of_statement (parser
);
22786 if (!processing_template_decl
)
22788 /* In a non-template class, check conversions now. In a template,
22789 we'll wait and instantiate these as needed. */
22790 if (TREE_CODE (decl
) == PARM_DECL
)
22791 parsed_arg
= check_default_argument (parmtype
, parsed_arg
);
22794 int flags
= LOOKUP_IMPLICIT
;
22795 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
)
22796 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg
))
22797 flags
= LOOKUP_NORMAL
;
22798 parsed_arg
= digest_init_flags (TREE_TYPE (decl
), parsed_arg
, flags
);
22802 /* If the token stream has not been completely used up, then
22803 there was extra junk after the end of the default
22805 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
22807 if (TREE_CODE (decl
) == PARM_DECL
)
22808 cp_parser_error (parser
, "expected %<,%>");
22810 cp_parser_error (parser
, "expected %<;%>");
22813 /* Revert to the main lexer. */
22814 cp_parser_pop_lexer (parser
);
22819 /* FIELD is a non-static data member with an initializer which we saved for
22820 later; parse it now. */
22823 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
22827 push_unparsed_function_queues (parser
);
22828 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
22829 DECL_INITIAL (field
),
22831 pop_unparsed_function_queues (parser
);
22833 DECL_INITIAL (field
) = def
;
22836 /* FN is a FUNCTION_DECL which may contains a parameter with an
22837 unparsed DEFAULT_ARG. Parse the default args now. This function
22838 assumes that the current scope is the scope in which the default
22839 argument should be processed. */
22842 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
22844 bool saved_local_variables_forbidden_p
;
22845 tree parm
, parmdecl
;
22847 /* While we're parsing the default args, we might (due to the
22848 statement expression extension) encounter more classes. We want
22849 to handle them right away, but we don't want them getting mixed
22850 up with default args that are currently in the queue. */
22851 push_unparsed_function_queues (parser
);
22853 /* Local variable names (and the `this' keyword) may not appear
22854 in a default argument. */
22855 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22856 parser
->local_variables_forbidden_p
= true;
22858 push_defarg_context (fn
);
22860 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
22861 parmdecl
= DECL_ARGUMENTS (fn
);
22862 parm
&& parm
!= void_list_node
;
22863 parm
= TREE_CHAIN (parm
),
22864 parmdecl
= DECL_CHAIN (parmdecl
))
22866 tree default_arg
= TREE_PURPOSE (parm
);
22868 vec
<tree
, va_gc
> *insts
;
22875 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
22876 /* This can happen for a friend declaration for a function
22877 already declared with default arguments. */
22881 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
22883 TREE_VALUE (parm
));
22884 if (parsed_arg
== error_mark_node
)
22889 TREE_PURPOSE (parm
) = parsed_arg
;
22891 /* Update any instantiations we've already created. */
22892 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
22893 vec_safe_iterate (insts
, ix
, ©
); ix
++)
22894 TREE_PURPOSE (copy
) = parsed_arg
;
22897 pop_defarg_context ();
22899 /* Make sure no default arg is missing. */
22900 check_default_args (fn
);
22902 /* Restore the state of local_variables_forbidden_p. */
22903 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22905 /* Restore the queue. */
22906 pop_unparsed_function_queues (parser
);
22909 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
22911 sizeof ... ( identifier )
22913 where the 'sizeof' token has already been consumed. */
22916 cp_parser_sizeof_pack (cp_parser
*parser
)
22918 /* Consume the `...'. */
22919 cp_lexer_consume_token (parser
->lexer
);
22920 maybe_warn_variadic_templates ();
22922 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
22924 cp_lexer_consume_token (parser
->lexer
);
22926 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
22927 "%<sizeof...%> argument must be surrounded by parentheses");
22929 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22930 tree name
= cp_parser_identifier (parser
);
22931 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
22932 if (expr
== error_mark_node
)
22933 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
22935 if (TREE_CODE (expr
) == TYPE_DECL
)
22936 expr
= TREE_TYPE (expr
);
22937 else if (TREE_CODE (expr
) == CONST_DECL
)
22938 expr
= DECL_INITIAL (expr
);
22939 expr
= make_pack_expansion (expr
);
22942 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22947 /* Parse the operand of `sizeof' (or a similar operator). Returns
22948 either a TYPE or an expression, depending on the form of the
22949 input. The KEYWORD indicates which kind of expression we have
22953 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
22955 tree expr
= NULL_TREE
;
22956 const char *saved_message
;
22958 bool saved_integral_constant_expression_p
;
22959 bool saved_non_integral_constant_expression_p
;
22961 /* If it's a `...', then we are computing the length of a parameter
22963 if (keyword
== RID_SIZEOF
22964 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22965 return cp_parser_sizeof_pack (parser
);
22967 /* Types cannot be defined in a `sizeof' expression. Save away the
22969 saved_message
= parser
->type_definition_forbidden_message
;
22970 /* And create the new one. */
22971 tmp
= concat ("types may not be defined in %<",
22972 IDENTIFIER_POINTER (ridpointers
[keyword
]),
22973 "%> expressions", NULL
);
22974 parser
->type_definition_forbidden_message
= tmp
;
22976 /* The restrictions on constant-expressions do not apply inside
22977 sizeof expressions. */
22978 saved_integral_constant_expression_p
22979 = parser
->integral_constant_expression_p
;
22980 saved_non_integral_constant_expression_p
22981 = parser
->non_integral_constant_expression_p
;
22982 parser
->integral_constant_expression_p
= false;
22984 /* Do not actually evaluate the expression. */
22985 ++cp_unevaluated_operand
;
22986 ++c_inhibit_evaluation_warnings
;
22987 /* If it's a `(', then we might be looking at the type-id
22989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22992 bool saved_in_type_id_in_expr_p
;
22994 /* We can't be sure yet whether we're looking at a type-id or an
22996 cp_parser_parse_tentatively (parser
);
22997 /* Consume the `('. */
22998 cp_lexer_consume_token (parser
->lexer
);
22999 /* Parse the type-id. */
23000 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
23001 parser
->in_type_id_in_expr_p
= true;
23002 type
= cp_parser_type_id (parser
);
23003 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
23004 /* Now, look for the trailing `)'. */
23005 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23006 /* If all went well, then we're done. */
23007 if (cp_parser_parse_definitely (parser
))
23009 cp_decl_specifier_seq decl_specs
;
23011 /* Build a trivial decl-specifier-seq. */
23012 clear_decl_specs (&decl_specs
);
23013 decl_specs
.type
= type
;
23015 /* Call grokdeclarator to figure out what type this is. */
23016 expr
= grokdeclarator (NULL
,
23020 /*attrlist=*/NULL
);
23024 /* If the type-id production did not work out, then we must be
23025 looking at the unary-expression production. */
23027 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
23028 /*cast_p=*/false, NULL
);
23030 /* Go back to evaluating expressions. */
23031 --cp_unevaluated_operand
;
23032 --c_inhibit_evaluation_warnings
;
23034 /* Free the message we created. */
23036 /* And restore the old one. */
23037 parser
->type_definition_forbidden_message
= saved_message
;
23038 parser
->integral_constant_expression_p
23039 = saved_integral_constant_expression_p
;
23040 parser
->non_integral_constant_expression_p
23041 = saved_non_integral_constant_expression_p
;
23046 /* If the current declaration has no declarator, return true. */
23049 cp_parser_declares_only_class_p (cp_parser
*parser
)
23051 /* If the next token is a `;' or a `,' then there is no
23053 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
23054 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
23057 /* Update the DECL_SPECS to reflect the storage class indicated by
23061 cp_parser_set_storage_class (cp_parser
*parser
,
23062 cp_decl_specifier_seq
*decl_specs
,
23066 cp_storage_class storage_class
;
23068 if (parser
->in_unbraced_linkage_specification_p
)
23070 error_at (token
->location
, "invalid use of %qD in linkage specification",
23071 ridpointers
[keyword
]);
23074 else if (decl_specs
->storage_class
!= sc_none
)
23076 decl_specs
->conflicting_specifiers_p
= true;
23080 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
23081 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
23082 && decl_specs
->gnu_thread_keyword_p
)
23084 pedwarn (decl_specs
->locations
[ds_thread
], 0,
23085 "%<__thread%> before %qD", ridpointers
[keyword
]);
23091 storage_class
= sc_auto
;
23094 storage_class
= sc_register
;
23097 storage_class
= sc_static
;
23100 storage_class
= sc_extern
;
23103 storage_class
= sc_mutable
;
23106 gcc_unreachable ();
23108 decl_specs
->storage_class
= storage_class
;
23109 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
23111 /* A storage class specifier cannot be applied alongside a typedef
23112 specifier. If there is a typedef specifier present then set
23113 conflicting_specifiers_p which will trigger an error later
23114 on in grokdeclarator. */
23115 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
23116 decl_specs
->conflicting_specifiers_p
= true;
23119 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23120 is true, the type is a class or enum definition. */
23123 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
23126 bool type_definition_p
)
23128 decl_specs
->any_specifiers_p
= true;
23130 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23131 (with, for example, in "typedef int wchar_t;") we remember that
23132 this is what happened. In system headers, we ignore these
23133 declarations so that G++ can work with system headers that are not
23135 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
23136 && !type_definition_p
23137 && (type_spec
== boolean_type_node
23138 || type_spec
== char16_type_node
23139 || type_spec
== char32_type_node
23140 || type_spec
== wchar_type_node
)
23141 && (decl_specs
->type
23142 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
23143 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
23144 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
23145 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
23147 decl_specs
->redefined_builtin_type
= type_spec
;
23148 set_and_check_decl_spec_loc (decl_specs
,
23149 ds_redefined_builtin_type_spec
,
23151 if (!decl_specs
->type
)
23153 decl_specs
->type
= type_spec
;
23154 decl_specs
->type_definition_p
= false;
23155 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
23158 else if (decl_specs
->type
)
23159 decl_specs
->multiple_types_p
= true;
23162 decl_specs
->type
= type_spec
;
23163 decl_specs
->type_definition_p
= type_definition_p
;
23164 decl_specs
->redefined_builtin_type
= NULL_TREE
;
23165 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
23169 /* True iff TOKEN is the GNU keyword __thread. */
23172 token_is__thread (cp_token
*token
)
23174 gcc_assert (token
->keyword
== RID_THREAD
);
23175 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
23178 /* Set the location for a declarator specifier and check if it is
23181 DECL_SPECS is the sequence of declarator specifiers onto which to
23184 DS is the single declarator specifier to set which location is to
23185 be set onto the existing sequence of declarators.
23187 LOCATION is the location for the declarator specifier to
23191 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
23192 cp_decl_spec ds
, cp_token
*token
)
23194 gcc_assert (ds
< ds_last
);
23196 if (decl_specs
== NULL
)
23199 source_location location
= token
->location
;
23201 if (decl_specs
->locations
[ds
] == 0)
23203 decl_specs
->locations
[ds
] = location
;
23204 if (ds
== ds_thread
)
23205 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
23211 if (decl_specs
->locations
[ds_long_long
] != 0)
23212 error_at (location
,
23213 "%<long long long%> is too long for GCC");
23216 decl_specs
->locations
[ds_long_long
] = location
;
23217 pedwarn_cxx98 (location
,
23219 "ISO C++ 1998 does not support %<long long%>");
23222 else if (ds
== ds_thread
)
23224 bool gnu
= token_is__thread (token
);
23225 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
23226 error_at (location
,
23227 "both %<__thread%> and %<thread_local%> specified");
23229 error_at (location
, "duplicate %qD", token
->u
.value
);
23233 static const char *const decl_spec_names
[] = {
23250 error_at (location
,
23251 "duplicate %qs", decl_spec_names
[ds
]);
23256 /* Return true iff the declarator specifier DS is present in the
23257 sequence of declarator specifiers DECL_SPECS. */
23260 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
23263 gcc_assert (ds
< ds_last
);
23265 if (decl_specs
== NULL
)
23268 return decl_specs
->locations
[ds
] != 0;
23271 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23272 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23275 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
23277 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
23280 /* Issue an error message indicating that TOKEN_DESC was expected.
23281 If KEYWORD is true, it indicated this function is called by
23282 cp_parser_require_keword and the required token can only be
23283 a indicated keyword. */
23286 cp_parser_required_error (cp_parser
*parser
,
23287 required_token token_desc
,
23290 switch (token_desc
)
23293 cp_parser_error (parser
, "expected %<new%>");
23296 cp_parser_error (parser
, "expected %<delete%>");
23299 cp_parser_error (parser
, "expected %<return%>");
23302 cp_parser_error (parser
, "expected %<while%>");
23305 cp_parser_error (parser
, "expected %<extern%>");
23307 case RT_STATIC_ASSERT
:
23308 cp_parser_error (parser
, "expected %<static_assert%>");
23311 cp_parser_error (parser
, "expected %<decltype%>");
23314 cp_parser_error (parser
, "expected %<operator%>");
23317 cp_parser_error (parser
, "expected %<class%>");
23320 cp_parser_error (parser
, "expected %<template%>");
23323 cp_parser_error (parser
, "expected %<namespace%>");
23326 cp_parser_error (parser
, "expected %<using%>");
23329 cp_parser_error (parser
, "expected %<asm%>");
23332 cp_parser_error (parser
, "expected %<try%>");
23335 cp_parser_error (parser
, "expected %<catch%>");
23338 cp_parser_error (parser
, "expected %<throw%>");
23341 cp_parser_error (parser
, "expected %<__label__%>");
23344 cp_parser_error (parser
, "expected %<@try%>");
23346 case RT_AT_SYNCHRONIZED
:
23347 cp_parser_error (parser
, "expected %<@synchronized%>");
23350 cp_parser_error (parser
, "expected %<@throw%>");
23352 case RT_TRANSACTION_ATOMIC
:
23353 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
23355 case RT_TRANSACTION_RELAXED
:
23356 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
23363 switch (token_desc
)
23366 cp_parser_error (parser
, "expected %<;%>");
23368 case RT_OPEN_PAREN
:
23369 cp_parser_error (parser
, "expected %<(%>");
23371 case RT_CLOSE_BRACE
:
23372 cp_parser_error (parser
, "expected %<}%>");
23374 case RT_OPEN_BRACE
:
23375 cp_parser_error (parser
, "expected %<{%>");
23377 case RT_CLOSE_SQUARE
:
23378 cp_parser_error (parser
, "expected %<]%>");
23380 case RT_OPEN_SQUARE
:
23381 cp_parser_error (parser
, "expected %<[%>");
23384 cp_parser_error (parser
, "expected %<,%>");
23387 cp_parser_error (parser
, "expected %<::%>");
23390 cp_parser_error (parser
, "expected %<<%>");
23393 cp_parser_error (parser
, "expected %<>%>");
23396 cp_parser_error (parser
, "expected %<=%>");
23399 cp_parser_error (parser
, "expected %<...%>");
23402 cp_parser_error (parser
, "expected %<*%>");
23405 cp_parser_error (parser
, "expected %<~%>");
23408 cp_parser_error (parser
, "expected %<:%>");
23410 case RT_COLON_SCOPE
:
23411 cp_parser_error (parser
, "expected %<:%> or %<::%>");
23413 case RT_CLOSE_PAREN
:
23414 cp_parser_error (parser
, "expected %<)%>");
23416 case RT_COMMA_CLOSE_PAREN
:
23417 cp_parser_error (parser
, "expected %<,%> or %<)%>");
23419 case RT_PRAGMA_EOL
:
23420 cp_parser_error (parser
, "expected end of line");
23423 cp_parser_error (parser
, "expected identifier");
23426 cp_parser_error (parser
, "expected selection-statement");
23428 case RT_INTERATION
:
23429 cp_parser_error (parser
, "expected iteration-statement");
23432 cp_parser_error (parser
, "expected jump-statement");
23435 cp_parser_error (parser
, "expected class-key");
23437 case RT_CLASS_TYPENAME_TEMPLATE
:
23438 cp_parser_error (parser
,
23439 "expected %<class%>, %<typename%>, or %<template%>");
23442 gcc_unreachable ();
23446 gcc_unreachable ();
23451 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23452 issue an error message indicating that TOKEN_DESC was expected.
23454 Returns the token consumed, if the token had the appropriate type.
23455 Otherwise, returns NULL. */
23458 cp_parser_require (cp_parser
* parser
,
23459 enum cpp_ttype type
,
23460 required_token token_desc
)
23462 if (cp_lexer_next_token_is (parser
->lexer
, type
))
23463 return cp_lexer_consume_token (parser
->lexer
);
23466 /* Output the MESSAGE -- unless we're parsing tentatively. */
23467 if (!cp_parser_simulate_error (parser
))
23468 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
23473 /* An error message is produced if the next token is not '>'.
23474 All further tokens are skipped until the desired token is
23475 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23478 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
23480 /* Current level of '< ... >'. */
23481 unsigned level
= 0;
23482 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23483 unsigned nesting_depth
= 0;
23485 /* Are we ready, yet? If not, issue error message. */
23486 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
23489 /* Skip tokens until the desired token is found. */
23492 /* Peek at the next token. */
23493 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
23496 if (!nesting_depth
)
23501 if (cxx_dialect
== cxx98
)
23502 /* C++0x views the `>>' operator as two `>' tokens, but
23505 else if (!nesting_depth
&& level
-- == 0)
23507 /* We've hit a `>>' where the first `>' closes the
23508 template argument list, and the second `>' is
23509 spurious. Just consume the `>>' and stop; we've
23510 already produced at least one error. */
23511 cp_lexer_consume_token (parser
->lexer
);
23514 /* Fall through for C++0x, so we handle the second `>' in
23518 if (!nesting_depth
&& level
-- == 0)
23520 /* We've reached the token we want, consume it and stop. */
23521 cp_lexer_consume_token (parser
->lexer
);
23526 case CPP_OPEN_PAREN
:
23527 case CPP_OPEN_SQUARE
:
23531 case CPP_CLOSE_PAREN
:
23532 case CPP_CLOSE_SQUARE
:
23533 if (nesting_depth
-- == 0)
23538 case CPP_PRAGMA_EOL
:
23539 case CPP_SEMICOLON
:
23540 case CPP_OPEN_BRACE
:
23541 case CPP_CLOSE_BRACE
:
23542 /* The '>' was probably forgotten, don't look further. */
23549 /* Consume this token. */
23550 cp_lexer_consume_token (parser
->lexer
);
23554 /* If the next token is the indicated keyword, consume it. Otherwise,
23555 issue an error message indicating that TOKEN_DESC was expected.
23557 Returns the token consumed, if the token had the appropriate type.
23558 Otherwise, returns NULL. */
23561 cp_parser_require_keyword (cp_parser
* parser
,
23563 required_token token_desc
)
23565 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
23567 if (token
&& token
->keyword
!= keyword
)
23569 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
23576 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23577 function-definition. */
23580 cp_parser_token_starts_function_definition_p (cp_token
* token
)
23582 return (/* An ordinary function-body begins with an `{'. */
23583 token
->type
== CPP_OPEN_BRACE
23584 /* A ctor-initializer begins with a `:'. */
23585 || token
->type
== CPP_COLON
23586 /* A function-try-block begins with `try'. */
23587 || token
->keyword
== RID_TRY
23588 /* A function-transaction-block begins with `__transaction_atomic'
23589 or `__transaction_relaxed'. */
23590 || token
->keyword
== RID_TRANSACTION_ATOMIC
23591 || token
->keyword
== RID_TRANSACTION_RELAXED
23592 /* The named return value extension begins with `return'. */
23593 || token
->keyword
== RID_RETURN
);
23596 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23600 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
23604 token
= cp_lexer_peek_token (parser
->lexer
);
23605 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
23608 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23609 C++0x) ending a template-argument. */
23612 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
23616 token
= cp_lexer_peek_token (parser
->lexer
);
23617 return (token
->type
== CPP_COMMA
23618 || token
->type
== CPP_GREATER
23619 || token
->type
== CPP_ELLIPSIS
23620 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
23623 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23624 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23627 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
23632 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23633 if (token
->type
== CPP_LESS
)
23635 /* Check for the sequence `<::' in the original code. It would be lexed as
23636 `[:', where `[' is a digraph, and there is no whitespace before
23638 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
23641 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
23642 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
23648 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23649 or none_type otherwise. */
23651 static enum tag_types
23652 cp_parser_token_is_class_key (cp_token
* token
)
23654 switch (token
->keyword
)
23659 return record_type
;
23668 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23671 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
23673 if (type
== error_mark_node
)
23675 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
23677 if (permerror (input_location
, "%qs tag used in naming %q#T",
23678 class_key
== union_type
? "union"
23679 : class_key
== record_type
? "struct" : "class",
23681 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
23682 "%q#T was previously declared here", type
);
23686 /* Issue an error message if DECL is redeclared with different
23687 access than its original declaration [class.access.spec/3].
23688 This applies to nested classes and nested class templates.
23692 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
23694 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
23697 if ((TREE_PRIVATE (decl
)
23698 != (current_access_specifier
== access_private_node
))
23699 || (TREE_PROTECTED (decl
)
23700 != (current_access_specifier
== access_protected_node
)))
23701 error_at (location
, "%qD redeclared with different access", decl
);
23704 /* Look for the `template' keyword, as a syntactic disambiguator.
23705 Return TRUE iff it is present, in which case it will be
23709 cp_parser_optional_template_keyword (cp_parser
*parser
)
23711 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23713 /* In C++98 the `template' keyword can only be used within templates;
23714 outside templates the parser can always figure out what is a
23715 template and what is not. In C++11, per the resolution of DR 468,
23716 `template' is allowed in cases where it is not strictly necessary. */
23717 if (!processing_template_decl
23718 && pedantic
&& cxx_dialect
== cxx98
)
23720 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23721 pedwarn (token
->location
, OPT_Wpedantic
,
23722 "in C++98 %<template%> (as a disambiguator) is only "
23723 "allowed within templates");
23724 /* If this part of the token stream is rescanned, the same
23725 error message would be generated. So, we purge the token
23726 from the stream. */
23727 cp_lexer_purge_token (parser
->lexer
);
23732 /* Consume the `template' keyword. */
23733 cp_lexer_consume_token (parser
->lexer
);
23740 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23741 set PARSER->SCOPE, and perform other related actions. */
23744 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
23747 struct tree_check
*check_value
;
23748 deferred_access_check
*chk
;
23749 vec
<deferred_access_check
, va_gc
> *checks
;
23751 /* Get the stored value. */
23752 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
23753 /* Perform any access checks that were deferred. */
23754 checks
= check_value
->checks
;
23757 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
23758 perform_or_defer_access_check (chk
->binfo
,
23760 chk
->diag_decl
, tf_warning_or_error
);
23762 /* Set the scope from the stored value. */
23763 parser
->scope
= check_value
->value
;
23764 parser
->qualifying_scope
= check_value
->qualifying_scope
;
23765 parser
->object_scope
= NULL_TREE
;
23768 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23769 encounter the end of a block before what we were looking for. */
23772 cp_parser_cache_group (cp_parser
*parser
,
23773 enum cpp_ttype end
,
23778 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23780 /* Abort a parenthesized expression if we encounter a semicolon. */
23781 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
23782 && token
->type
== CPP_SEMICOLON
)
23784 /* If we've reached the end of the file, stop. */
23785 if (token
->type
== CPP_EOF
23786 || (end
!= CPP_PRAGMA_EOL
23787 && token
->type
== CPP_PRAGMA_EOL
))
23789 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
23790 /* We've hit the end of an enclosing block, so there's been some
23791 kind of syntax error. */
23794 /* Consume the token. */
23795 cp_lexer_consume_token (parser
->lexer
);
23796 /* See if it starts a new group. */
23797 if (token
->type
== CPP_OPEN_BRACE
)
23799 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
23800 /* In theory this should probably check end == '}', but
23801 cp_parser_save_member_function_body needs it to exit
23802 after either '}' or ')' when called with ')'. */
23806 else if (token
->type
== CPP_OPEN_PAREN
)
23808 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
23809 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
23812 else if (token
->type
== CPP_PRAGMA
)
23813 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
23814 else if (token
->type
== end
)
23819 /* Like above, for caching a default argument or NSDMI. Both of these are
23820 terminated by a non-nested comma, but it can be unclear whether or not a
23821 comma is nested in a template argument list unless we do more parsing.
23822 In order to handle this ambiguity, when we encounter a ',' after a '<'
23823 we try to parse what follows as a parameter-declaration-list (in the
23824 case of a default argument) or a member-declarator (in the case of an
23825 NSDMI). If that succeeds, then we stop caching. */
23828 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
23830 unsigned depth
= 0;
23831 int maybe_template_id
= 0;
23832 cp_token
*first_token
;
23834 tree default_argument
;
23836 /* Add tokens until we have processed the entire default
23837 argument. We add the range [first_token, token). */
23838 first_token
= cp_lexer_peek_token (parser
->lexer
);
23839 if (first_token
->type
== CPP_OPEN_BRACE
)
23841 /* For list-initialization, this is straightforward. */
23842 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23843 token
= cp_lexer_peek_token (parser
->lexer
);
23849 /* Peek at the next token. */
23850 token
= cp_lexer_peek_token (parser
->lexer
);
23851 /* What we do depends on what token we have. */
23852 switch (token
->type
)
23854 /* In valid code, a default argument must be
23855 immediately followed by a `,' `)', or `...'. */
23857 if (depth
== 0 && maybe_template_id
)
23859 /* If we've seen a '<', we might be in a
23860 template-argument-list. Until Core issue 325 is
23861 resolved, we don't know how this situation ought
23862 to be handled, so try to DTRT. We check whether
23863 what comes after the comma is a valid parameter
23864 declaration list. If it is, then the comma ends
23865 the default argument; otherwise the default
23866 argument continues. */
23867 bool error
= false;
23870 /* Set ITALP so cp_parser_parameter_declaration_list
23871 doesn't decide to commit to this parse. */
23872 bool saved_italp
= parser
->in_template_argument_list_p
;
23873 parser
->in_template_argument_list_p
= true;
23875 cp_parser_parse_tentatively (parser
);
23876 cp_lexer_consume_token (parser
->lexer
);
23880 int ctor_dtor_or_conv_p
;
23881 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23882 &ctor_dtor_or_conv_p
,
23883 /*parenthesized_p=*/NULL
,
23884 /*member_p=*/true);
23888 begin_scope (sk_function_parms
, NULL_TREE
);
23889 cp_parser_parameter_declaration_list (parser
, &error
);
23890 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
23891 pop_binding (DECL_NAME (t
), t
);
23894 if (!cp_parser_error_occurred (parser
) && !error
)
23896 cp_parser_abort_tentative_parse (parser
);
23898 parser
->in_template_argument_list_p
= saved_italp
;
23901 case CPP_CLOSE_PAREN
:
23903 /* If we run into a non-nested `;', `}', or `]',
23904 then the code is invalid -- but the default
23905 argument is certainly over. */
23906 case CPP_SEMICOLON
:
23907 case CPP_CLOSE_BRACE
:
23908 case CPP_CLOSE_SQUARE
:
23911 /* Update DEPTH, if necessary. */
23912 else if (token
->type
== CPP_CLOSE_PAREN
23913 || token
->type
== CPP_CLOSE_BRACE
23914 || token
->type
== CPP_CLOSE_SQUARE
)
23918 case CPP_OPEN_PAREN
:
23919 case CPP_OPEN_SQUARE
:
23920 case CPP_OPEN_BRACE
:
23926 /* This might be the comparison operator, or it might
23927 start a template argument list. */
23928 ++maybe_template_id
;
23932 if (cxx_dialect
== cxx98
)
23934 /* Fall through for C++0x, which treats the `>>'
23935 operator like two `>' tokens in certain
23941 /* This might be an operator, or it might close a
23942 template argument list. But if a previous '<'
23943 started a template argument list, this will have
23944 closed it, so we can't be in one anymore. */
23945 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
23946 if (maybe_template_id
< 0)
23947 maybe_template_id
= 0;
23951 /* If we run out of tokens, issue an error message. */
23953 case CPP_PRAGMA_EOL
:
23954 error_at (token
->location
, "file ends in default argument");
23960 /* In these cases, we should look for template-ids.
23961 For example, if the default argument is
23962 `X<int, double>()', we need to do name lookup to
23963 figure out whether or not `X' is a template; if
23964 so, the `,' does not end the default argument.
23966 That is not yet done. */
23973 /* If we've reached the end, stop. */
23977 /* Add the token to the token block. */
23978 token
= cp_lexer_consume_token (parser
->lexer
);
23981 /* Create a DEFAULT_ARG to represent the unparsed default
23983 default_argument
= make_node (DEFAULT_ARG
);
23984 DEFARG_TOKENS (default_argument
)
23985 = cp_token_cache_new (first_token
, token
);
23986 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
23988 return default_argument
;
23991 /* Begin parsing tentatively. We always save tokens while parsing
23992 tentatively so that if the tentative parsing fails we can restore the
23996 cp_parser_parse_tentatively (cp_parser
* parser
)
23998 /* Enter a new parsing context. */
23999 parser
->context
= cp_parser_context_new (parser
->context
);
24000 /* Begin saving tokens. */
24001 cp_lexer_save_tokens (parser
->lexer
);
24002 /* In order to avoid repetitive access control error messages,
24003 access checks are queued up until we are no longer parsing
24005 push_deferring_access_checks (dk_deferred
);
24008 /* Commit to the currently active tentative parse. */
24011 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
24013 cp_parser_context
*context
;
24016 /* Mark all of the levels as committed. */
24017 lexer
= parser
->lexer
;
24018 for (context
= parser
->context
; context
->next
; context
= context
->next
)
24020 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
24022 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
24023 while (!cp_lexer_saving_tokens (lexer
))
24024 lexer
= lexer
->next
;
24025 cp_lexer_commit_tokens (lexer
);
24029 /* Abort the currently active tentative parse. All consumed tokens
24030 will be rolled back, and no diagnostics will be issued. */
24033 cp_parser_abort_tentative_parse (cp_parser
* parser
)
24035 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
24036 || errorcount
> 0);
24037 cp_parser_simulate_error (parser
);
24038 /* Now, pretend that we want to see if the construct was
24039 successfully parsed. */
24040 cp_parser_parse_definitely (parser
);
24043 /* Stop parsing tentatively. If a parse error has occurred, restore the
24044 token stream. Otherwise, commit to the tokens we have consumed.
24045 Returns true if no error occurred; false otherwise. */
24048 cp_parser_parse_definitely (cp_parser
* parser
)
24050 bool error_occurred
;
24051 cp_parser_context
*context
;
24053 /* Remember whether or not an error occurred, since we are about to
24054 destroy that information. */
24055 error_occurred
= cp_parser_error_occurred (parser
);
24056 /* Remove the topmost context from the stack. */
24057 context
= parser
->context
;
24058 parser
->context
= context
->next
;
24059 /* If no parse errors occurred, commit to the tentative parse. */
24060 if (!error_occurred
)
24062 /* Commit to the tokens read tentatively, unless that was
24064 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
24065 cp_lexer_commit_tokens (parser
->lexer
);
24067 pop_to_parent_deferring_access_checks ();
24069 /* Otherwise, if errors occurred, roll back our state so that things
24070 are just as they were before we began the tentative parse. */
24073 cp_lexer_rollback_tokens (parser
->lexer
);
24074 pop_deferring_access_checks ();
24076 /* Add the context to the front of the free list. */
24077 context
->next
= cp_parser_context_free_list
;
24078 cp_parser_context_free_list
= context
;
24080 return !error_occurred
;
24083 /* Returns true if we are parsing tentatively and are not committed to
24084 this tentative parse. */
24087 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
24089 return (cp_parser_parsing_tentatively (parser
)
24090 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
24093 /* Returns nonzero iff an error has occurred during the most recent
24094 tentative parse. */
24097 cp_parser_error_occurred (cp_parser
* parser
)
24099 return (cp_parser_parsing_tentatively (parser
)
24100 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
24103 /* Returns nonzero if GNU extensions are allowed. */
24106 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
24108 return parser
->allow_gnu_extensions_p
;
24111 /* Objective-C++ Productions */
24114 /* Parse an Objective-C expression, which feeds into a primary-expression
24118 objc-message-expression
24119 objc-string-literal
24120 objc-encode-expression
24121 objc-protocol-expression
24122 objc-selector-expression
24124 Returns a tree representation of the expression. */
24127 cp_parser_objc_expression (cp_parser
* parser
)
24129 /* Try to figure out what kind of declaration is present. */
24130 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
24134 case CPP_OPEN_SQUARE
:
24135 return cp_parser_objc_message_expression (parser
);
24137 case CPP_OBJC_STRING
:
24138 kwd
= cp_lexer_consume_token (parser
->lexer
);
24139 return objc_build_string_object (kwd
->u
.value
);
24142 switch (kwd
->keyword
)
24144 case RID_AT_ENCODE
:
24145 return cp_parser_objc_encode_expression (parser
);
24147 case RID_AT_PROTOCOL
:
24148 return cp_parser_objc_protocol_expression (parser
);
24150 case RID_AT_SELECTOR
:
24151 return cp_parser_objc_selector_expression (parser
);
24157 error_at (kwd
->location
,
24158 "misplaced %<@%D%> Objective-C++ construct",
24160 cp_parser_skip_to_end_of_block_or_statement (parser
);
24163 return error_mark_node
;
24166 /* Parse an Objective-C message expression.
24168 objc-message-expression:
24169 [ objc-message-receiver objc-message-args ]
24171 Returns a representation of an Objective-C message. */
24174 cp_parser_objc_message_expression (cp_parser
* parser
)
24176 tree receiver
, messageargs
;
24178 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
24179 receiver
= cp_parser_objc_message_receiver (parser
);
24180 messageargs
= cp_parser_objc_message_args (parser
);
24181 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24183 return objc_build_message_expr (receiver
, messageargs
);
24186 /* Parse an objc-message-receiver.
24188 objc-message-receiver:
24190 simple-type-specifier
24192 Returns a representation of the type or expression. */
24195 cp_parser_objc_message_receiver (cp_parser
* parser
)
24199 /* An Objective-C message receiver may be either (1) a type
24200 or (2) an expression. */
24201 cp_parser_parse_tentatively (parser
);
24202 rcv
= cp_parser_expression (parser
, false, NULL
);
24204 if (cp_parser_parse_definitely (parser
))
24207 rcv
= cp_parser_simple_type_specifier (parser
,
24208 /*decl_specs=*/NULL
,
24209 CP_PARSER_FLAGS_NONE
);
24211 return objc_get_class_reference (rcv
);
24214 /* Parse the arguments and selectors comprising an Objective-C message.
24219 objc-selector-args , objc-comma-args
24221 objc-selector-args:
24222 objc-selector [opt] : assignment-expression
24223 objc-selector-args objc-selector [opt] : assignment-expression
24226 assignment-expression
24227 objc-comma-args , assignment-expression
24229 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24230 selector arguments and TREE_VALUE containing a list of comma
24234 cp_parser_objc_message_args (cp_parser
* parser
)
24236 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
24237 bool maybe_unary_selector_p
= true;
24238 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24240 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
24242 tree selector
= NULL_TREE
, arg
;
24244 if (token
->type
!= CPP_COLON
)
24245 selector
= cp_parser_objc_selector (parser
);
24247 /* Detect if we have a unary selector. */
24248 if (maybe_unary_selector_p
24249 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
24250 return build_tree_list (selector
, NULL_TREE
);
24252 maybe_unary_selector_p
= false;
24253 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24254 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
24257 = chainon (sel_args
,
24258 build_tree_list (selector
, arg
));
24260 token
= cp_lexer_peek_token (parser
->lexer
);
24263 /* Handle non-selector arguments, if any. */
24264 while (token
->type
== CPP_COMMA
)
24268 cp_lexer_consume_token (parser
->lexer
);
24269 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
24272 = chainon (addl_args
,
24273 build_tree_list (NULL_TREE
, arg
));
24275 token
= cp_lexer_peek_token (parser
->lexer
);
24278 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
24280 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
24281 return build_tree_list (error_mark_node
, error_mark_node
);
24284 return build_tree_list (sel_args
, addl_args
);
24287 /* Parse an Objective-C encode expression.
24289 objc-encode-expression:
24290 @encode objc-typename
24292 Returns an encoded representation of the type argument. */
24295 cp_parser_objc_encode_expression (cp_parser
* parser
)
24300 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
24301 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24302 token
= cp_lexer_peek_token (parser
->lexer
);
24303 type
= complete_type (cp_parser_type_id (parser
));
24304 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24308 error_at (token
->location
,
24309 "%<@encode%> must specify a type as an argument");
24310 return error_mark_node
;
24313 /* This happens if we find @encode(T) (where T is a template
24314 typename or something dependent on a template typename) when
24315 parsing a template. In that case, we can't compile it
24316 immediately, but we rather create an AT_ENCODE_EXPR which will
24317 need to be instantiated when the template is used.
24319 if (dependent_type_p (type
))
24321 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
24322 TREE_READONLY (value
) = 1;
24326 return objc_build_encode_expr (type
);
24329 /* Parse an Objective-C @defs expression. */
24332 cp_parser_objc_defs_expression (cp_parser
*parser
)
24336 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
24337 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24338 name
= cp_parser_identifier (parser
);
24339 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24341 return objc_get_class_ivars (name
);
24344 /* Parse an Objective-C protocol expression.
24346 objc-protocol-expression:
24347 @protocol ( identifier )
24349 Returns a representation of the protocol expression. */
24352 cp_parser_objc_protocol_expression (cp_parser
* parser
)
24356 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
24357 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24358 proto
= cp_parser_identifier (parser
);
24359 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24361 return objc_build_protocol_expr (proto
);
24364 /* Parse an Objective-C selector expression.
24366 objc-selector-expression:
24367 @selector ( objc-method-signature )
24369 objc-method-signature:
24375 objc-selector-seq objc-selector :
24377 Returns a representation of the method selector. */
24380 cp_parser_objc_selector_expression (cp_parser
* parser
)
24382 tree sel_seq
= NULL_TREE
;
24383 bool maybe_unary_selector_p
= true;
24385 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24387 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
24388 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24389 token
= cp_lexer_peek_token (parser
->lexer
);
24391 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
24392 || token
->type
== CPP_SCOPE
)
24394 tree selector
= NULL_TREE
;
24396 if (token
->type
!= CPP_COLON
24397 || token
->type
== CPP_SCOPE
)
24398 selector
= cp_parser_objc_selector (parser
);
24400 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
24401 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
24403 /* Detect if we have a unary selector. */
24404 if (maybe_unary_selector_p
)
24406 sel_seq
= selector
;
24407 goto finish_selector
;
24411 cp_parser_error (parser
, "expected %<:%>");
24414 maybe_unary_selector_p
= false;
24415 token
= cp_lexer_consume_token (parser
->lexer
);
24417 if (token
->type
== CPP_SCOPE
)
24420 = chainon (sel_seq
,
24421 build_tree_list (selector
, NULL_TREE
));
24423 = chainon (sel_seq
,
24424 build_tree_list (NULL_TREE
, NULL_TREE
));
24428 = chainon (sel_seq
,
24429 build_tree_list (selector
, NULL_TREE
));
24431 token
= cp_lexer_peek_token (parser
->lexer
);
24435 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24437 return objc_build_selector_expr (loc
, sel_seq
);
24440 /* Parse a list of identifiers.
24442 objc-identifier-list:
24444 objc-identifier-list , identifier
24446 Returns a TREE_LIST of identifier nodes. */
24449 cp_parser_objc_identifier_list (cp_parser
* parser
)
24455 identifier
= cp_parser_identifier (parser
);
24456 if (identifier
== error_mark_node
)
24457 return error_mark_node
;
24459 list
= build_tree_list (NULL_TREE
, identifier
);
24460 sep
= cp_lexer_peek_token (parser
->lexer
);
24462 while (sep
->type
== CPP_COMMA
)
24464 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
24465 identifier
= cp_parser_identifier (parser
);
24466 if (identifier
== error_mark_node
)
24469 list
= chainon (list
, build_tree_list (NULL_TREE
,
24471 sep
= cp_lexer_peek_token (parser
->lexer
);
24477 /* Parse an Objective-C alias declaration.
24479 objc-alias-declaration:
24480 @compatibility_alias identifier identifier ;
24482 This function registers the alias mapping with the Objective-C front end.
24483 It returns nothing. */
24486 cp_parser_objc_alias_declaration (cp_parser
* parser
)
24490 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
24491 alias
= cp_parser_identifier (parser
);
24492 orig
= cp_parser_identifier (parser
);
24493 objc_declare_alias (alias
, orig
);
24494 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24497 /* Parse an Objective-C class forward-declaration.
24499 objc-class-declaration:
24500 @class objc-identifier-list ;
24502 The function registers the forward declarations with the Objective-C
24503 front end. It returns nothing. */
24506 cp_parser_objc_class_declaration (cp_parser
* parser
)
24508 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
24513 id
= cp_parser_identifier (parser
);
24514 if (id
== error_mark_node
)
24517 objc_declare_class (id
);
24519 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24520 cp_lexer_consume_token (parser
->lexer
);
24524 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24527 /* Parse a list of Objective-C protocol references.
24529 objc-protocol-refs-opt:
24530 objc-protocol-refs [opt]
24532 objc-protocol-refs:
24533 < objc-identifier-list >
24535 Returns a TREE_LIST of identifiers, if any. */
24538 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
24540 tree protorefs
= NULL_TREE
;
24542 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
24544 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
24545 protorefs
= cp_parser_objc_identifier_list (parser
);
24546 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
24552 /* Parse a Objective-C visibility specification. */
24555 cp_parser_objc_visibility_spec (cp_parser
* parser
)
24557 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
24559 switch (vis
->keyword
)
24561 case RID_AT_PRIVATE
:
24562 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
24564 case RID_AT_PROTECTED
:
24565 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
24567 case RID_AT_PUBLIC
:
24568 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
24570 case RID_AT_PACKAGE
:
24571 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
24577 /* Eat '@private'/'@protected'/'@public'. */
24578 cp_lexer_consume_token (parser
->lexer
);
24581 /* Parse an Objective-C method type. Return 'true' if it is a class
24582 (+) method, and 'false' if it is an instance (-) method. */
24585 cp_parser_objc_method_type (cp_parser
* parser
)
24587 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
24593 /* Parse an Objective-C protocol qualifier. */
24596 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
24598 tree quals
= NULL_TREE
, node
;
24599 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24601 node
= token
->u
.value
;
24603 while (node
&& identifier_p (node
)
24604 && (node
== ridpointers
[(int) RID_IN
]
24605 || node
== ridpointers
[(int) RID_OUT
]
24606 || node
== ridpointers
[(int) RID_INOUT
]
24607 || node
== ridpointers
[(int) RID_BYCOPY
]
24608 || node
== ridpointers
[(int) RID_BYREF
]
24609 || node
== ridpointers
[(int) RID_ONEWAY
]))
24611 quals
= tree_cons (NULL_TREE
, node
, quals
);
24612 cp_lexer_consume_token (parser
->lexer
);
24613 token
= cp_lexer_peek_token (parser
->lexer
);
24614 node
= token
->u
.value
;
24620 /* Parse an Objective-C typename. */
24623 cp_parser_objc_typename (cp_parser
* parser
)
24625 tree type_name
= NULL_TREE
;
24627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24629 tree proto_quals
, cp_type
= NULL_TREE
;
24631 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
24632 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
24634 /* An ObjC type name may consist of just protocol qualifiers, in which
24635 case the type shall default to 'id'. */
24636 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
24638 cp_type
= cp_parser_type_id (parser
);
24640 /* If the type could not be parsed, an error has already
24641 been produced. For error recovery, behave as if it had
24642 not been specified, which will use the default type
24644 if (cp_type
== error_mark_node
)
24646 cp_type
= NULL_TREE
;
24647 /* We need to skip to the closing parenthesis as
24648 cp_parser_type_id() does not seem to do it for
24650 cp_parser_skip_to_closing_parenthesis (parser
,
24651 /*recovering=*/true,
24652 /*or_comma=*/false,
24653 /*consume_paren=*/false);
24657 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24658 type_name
= build_tree_list (proto_quals
, cp_type
);
24664 /* Check to see if TYPE refers to an Objective-C selector name. */
24667 cp_parser_objc_selector_p (enum cpp_ttype type
)
24669 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
24670 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
24671 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
24672 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
24673 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
24676 /* Parse an Objective-C selector. */
24679 cp_parser_objc_selector (cp_parser
* parser
)
24681 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
24683 if (!cp_parser_objc_selector_p (token
->type
))
24685 error_at (token
->location
, "invalid Objective-C++ selector name");
24686 return error_mark_node
;
24689 /* C++ operator names are allowed to appear in ObjC selectors. */
24690 switch (token
->type
)
24692 case CPP_AND_AND
: return get_identifier ("and");
24693 case CPP_AND_EQ
: return get_identifier ("and_eq");
24694 case CPP_AND
: return get_identifier ("bitand");
24695 case CPP_OR
: return get_identifier ("bitor");
24696 case CPP_COMPL
: return get_identifier ("compl");
24697 case CPP_NOT
: return get_identifier ("not");
24698 case CPP_NOT_EQ
: return get_identifier ("not_eq");
24699 case CPP_OR_OR
: return get_identifier ("or");
24700 case CPP_OR_EQ
: return get_identifier ("or_eq");
24701 case CPP_XOR
: return get_identifier ("xor");
24702 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
24703 default: return token
->u
.value
;
24707 /* Parse an Objective-C params list. */
24710 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
24712 tree params
= NULL_TREE
;
24713 bool maybe_unary_selector_p
= true;
24714 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24716 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
24718 tree selector
= NULL_TREE
, type_name
, identifier
;
24719 tree parm_attr
= NULL_TREE
;
24721 if (token
->keyword
== RID_ATTRIBUTE
)
24724 if (token
->type
!= CPP_COLON
)
24725 selector
= cp_parser_objc_selector (parser
);
24727 /* Detect if we have a unary selector. */
24728 if (maybe_unary_selector_p
24729 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
24731 params
= selector
; /* Might be followed by attributes. */
24735 maybe_unary_selector_p
= false;
24736 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
24738 /* Something went quite wrong. There should be a colon
24739 here, but there is not. Stop parsing parameters. */
24742 type_name
= cp_parser_objc_typename (parser
);
24743 /* New ObjC allows attributes on parameters too. */
24744 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
24745 parm_attr
= cp_parser_attributes_opt (parser
);
24746 identifier
= cp_parser_identifier (parser
);
24750 objc_build_keyword_decl (selector
,
24755 token
= cp_lexer_peek_token (parser
->lexer
);
24758 if (params
== NULL_TREE
)
24760 cp_parser_error (parser
, "objective-c++ method declaration is expected");
24761 return error_mark_node
;
24764 /* We allow tail attributes for the method. */
24765 if (token
->keyword
== RID_ATTRIBUTE
)
24767 *attributes
= cp_parser_attributes_opt (parser
);
24768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24769 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24771 cp_parser_error (parser
,
24772 "method attributes must be specified at the end");
24773 return error_mark_node
;
24776 if (params
== NULL_TREE
)
24778 cp_parser_error (parser
, "objective-c++ method declaration is expected");
24779 return error_mark_node
;
24784 /* Parse the non-keyword Objective-C params. */
24787 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
24790 tree params
= make_node (TREE_LIST
);
24791 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24792 *ellipsisp
= false; /* Initially, assume no ellipsis. */
24794 while (token
->type
== CPP_COMMA
)
24796 cp_parameter_declarator
*parmdecl
;
24799 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
24800 token
= cp_lexer_peek_token (parser
->lexer
);
24802 if (token
->type
== CPP_ELLIPSIS
)
24804 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
24806 token
= cp_lexer_peek_token (parser
->lexer
);
24810 /* TODO: parse attributes for tail parameters. */
24811 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
24812 parm
= grokdeclarator (parmdecl
->declarator
,
24813 &parmdecl
->decl_specifiers
,
24814 PARM
, /*initialized=*/0,
24815 /*attrlist=*/NULL
);
24817 chainon (params
, build_tree_list (NULL_TREE
, parm
));
24818 token
= cp_lexer_peek_token (parser
->lexer
);
24821 /* We allow tail attributes for the method. */
24822 if (token
->keyword
== RID_ATTRIBUTE
)
24824 if (*attributes
== NULL_TREE
)
24826 *attributes
= cp_parser_attributes_opt (parser
);
24827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24828 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24832 /* We have an error, but parse the attributes, so that we can
24834 *attributes
= cp_parser_attributes_opt (parser
);
24836 cp_parser_error (parser
,
24837 "method attributes must be specified at the end");
24838 return error_mark_node
;
24844 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24847 cp_parser_objc_interstitial_code (cp_parser
* parser
)
24849 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24851 /* If the next token is `extern' and the following token is a string
24852 literal, then we have a linkage specification. */
24853 if (token
->keyword
== RID_EXTERN
24854 && cp_parser_is_pure_string_literal
24855 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
24856 cp_parser_linkage_specification (parser
);
24857 /* Handle #pragma, if any. */
24858 else if (token
->type
== CPP_PRAGMA
)
24859 cp_parser_pragma (parser
, pragma_external
);
24860 /* Allow stray semicolons. */
24861 else if (token
->type
== CPP_SEMICOLON
)
24862 cp_lexer_consume_token (parser
->lexer
);
24863 /* Mark methods as optional or required, when building protocols. */
24864 else if (token
->keyword
== RID_AT_OPTIONAL
)
24866 cp_lexer_consume_token (parser
->lexer
);
24867 objc_set_method_opt (true);
24869 else if (token
->keyword
== RID_AT_REQUIRED
)
24871 cp_lexer_consume_token (parser
->lexer
);
24872 objc_set_method_opt (false);
24874 else if (token
->keyword
== RID_NAMESPACE
)
24875 cp_parser_namespace_definition (parser
);
24876 /* Other stray characters must generate errors. */
24877 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
24879 cp_lexer_consume_token (parser
->lexer
);
24880 error ("stray %qs between Objective-C++ methods",
24881 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
24883 /* Finally, try to parse a block-declaration, or a function-definition. */
24885 cp_parser_block_declaration (parser
, /*statement_p=*/false);
24888 /* Parse a method signature. */
24891 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
24893 tree rettype
, kwdparms
, optparms
;
24894 bool ellipsis
= false;
24895 bool is_class_method
;
24897 is_class_method
= cp_parser_objc_method_type (parser
);
24898 rettype
= cp_parser_objc_typename (parser
);
24899 *attributes
= NULL_TREE
;
24900 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
24901 if (kwdparms
== error_mark_node
)
24902 return error_mark_node
;
24903 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
24904 if (optparms
== error_mark_node
)
24905 return error_mark_node
;
24907 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
24911 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
24914 cp_lexer_save_tokens (parser
->lexer
);
24915 tattr
= cp_parser_attributes_opt (parser
);
24916 gcc_assert (tattr
) ;
24918 /* If the attributes are followed by a method introducer, this is not allowed.
24919 Dump the attributes and flag the situation. */
24920 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
24921 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
24924 /* Otherwise, the attributes introduce some interstitial code, possibly so
24925 rewind to allow that check. */
24926 cp_lexer_rollback_tokens (parser
->lexer
);
24930 /* Parse an Objective-C method prototype list. */
24933 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
24935 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24937 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
24939 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
24941 tree attributes
, sig
;
24942 bool is_class_method
;
24943 if (token
->type
== CPP_PLUS
)
24944 is_class_method
= true;
24946 is_class_method
= false;
24947 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
24948 if (sig
== error_mark_node
)
24950 cp_parser_skip_to_end_of_block_or_statement (parser
);
24951 token
= cp_lexer_peek_token (parser
->lexer
);
24954 objc_add_method_declaration (is_class_method
, sig
, attributes
);
24955 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24957 else if (token
->keyword
== RID_AT_PROPERTY
)
24958 cp_parser_objc_at_property_declaration (parser
);
24959 else if (token
->keyword
== RID_ATTRIBUTE
24960 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
24961 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
24963 "prefix attributes are ignored for methods");
24965 /* Allow for interspersed non-ObjC++ code. */
24966 cp_parser_objc_interstitial_code (parser
);
24968 token
= cp_lexer_peek_token (parser
->lexer
);
24971 if (token
->type
!= CPP_EOF
)
24972 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
24974 cp_parser_error (parser
, "expected %<@end%>");
24976 objc_finish_interface ();
24979 /* Parse an Objective-C method definition list. */
24982 cp_parser_objc_method_definition_list (cp_parser
* parser
)
24984 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24986 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
24990 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
24993 tree sig
, attribute
;
24994 bool is_class_method
;
24995 if (token
->type
== CPP_PLUS
)
24996 is_class_method
= true;
24998 is_class_method
= false;
24999 push_deferring_access_checks (dk_deferred
);
25000 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
25001 if (sig
== error_mark_node
)
25003 cp_parser_skip_to_end_of_block_or_statement (parser
);
25004 token
= cp_lexer_peek_token (parser
->lexer
);
25007 objc_start_method_definition (is_class_method
, sig
, attribute
,
25010 /* For historical reasons, we accept an optional semicolon. */
25011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25012 cp_lexer_consume_token (parser
->lexer
);
25014 ptk
= cp_lexer_peek_token (parser
->lexer
);
25015 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
25016 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
25018 perform_deferred_access_checks (tf_warning_or_error
);
25019 stop_deferring_access_checks ();
25020 meth
= cp_parser_function_definition_after_declarator (parser
,
25022 pop_deferring_access_checks ();
25023 objc_finish_method_definition (meth
);
25026 /* The following case will be removed once @synthesize is
25027 completely implemented. */
25028 else if (token
->keyword
== RID_AT_PROPERTY
)
25029 cp_parser_objc_at_property_declaration (parser
);
25030 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
25031 cp_parser_objc_at_synthesize_declaration (parser
);
25032 else if (token
->keyword
== RID_AT_DYNAMIC
)
25033 cp_parser_objc_at_dynamic_declaration (parser
);
25034 else if (token
->keyword
== RID_ATTRIBUTE
25035 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
25036 warning_at (token
->location
, OPT_Wattributes
,
25037 "prefix attributes are ignored for methods");
25039 /* Allow for interspersed non-ObjC++ code. */
25040 cp_parser_objc_interstitial_code (parser
);
25042 token
= cp_lexer_peek_token (parser
->lexer
);
25045 if (token
->type
!= CPP_EOF
)
25046 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25048 cp_parser_error (parser
, "expected %<@end%>");
25050 objc_finish_implementation ();
25053 /* Parse Objective-C ivars. */
25056 cp_parser_objc_class_ivars (cp_parser
* parser
)
25058 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25060 if (token
->type
!= CPP_OPEN_BRACE
)
25061 return; /* No ivars specified. */
25063 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
25064 token
= cp_lexer_peek_token (parser
->lexer
);
25066 while (token
->type
!= CPP_CLOSE_BRACE
25067 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25069 cp_decl_specifier_seq declspecs
;
25070 int decl_class_or_enum_p
;
25071 tree prefix_attributes
;
25073 cp_parser_objc_visibility_spec (parser
);
25075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25078 cp_parser_decl_specifier_seq (parser
,
25079 CP_PARSER_FLAGS_OPTIONAL
,
25081 &decl_class_or_enum_p
);
25083 /* auto, register, static, extern, mutable. */
25084 if (declspecs
.storage_class
!= sc_none
)
25086 cp_parser_error (parser
, "invalid type for instance variable");
25087 declspecs
.storage_class
= sc_none
;
25090 /* thread_local. */
25091 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
25093 cp_parser_error (parser
, "invalid type for instance variable");
25094 declspecs
.locations
[ds_thread
] = 0;
25098 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
25100 cp_parser_error (parser
, "invalid type for instance variable");
25101 declspecs
.locations
[ds_typedef
] = 0;
25104 prefix_attributes
= declspecs
.attributes
;
25105 declspecs
.attributes
= NULL_TREE
;
25107 /* Keep going until we hit the `;' at the end of the
25109 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25111 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
25112 cp_declarator
*declarator
= NULL
;
25113 int ctor_dtor_or_conv_p
;
25115 /* Check for a (possibly unnamed) bitfield declaration. */
25116 token
= cp_lexer_peek_token (parser
->lexer
);
25117 if (token
->type
== CPP_COLON
)
25120 if (token
->type
== CPP_NAME
25121 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
25124 /* Get the name of the bitfield. */
25125 declarator
= make_id_declarator (NULL_TREE
,
25126 cp_parser_identifier (parser
),
25130 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
25131 /* Get the width of the bitfield. */
25133 = cp_parser_constant_expression (parser
,
25134 /*allow_non_constant=*/false,
25139 /* Parse the declarator. */
25141 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25142 &ctor_dtor_or_conv_p
,
25143 /*parenthesized_p=*/NULL
,
25144 /*member_p=*/false);
25147 /* Look for attributes that apply to the ivar. */
25148 attributes
= cp_parser_attributes_opt (parser
);
25149 /* Remember which attributes are prefix attributes and
25151 first_attribute
= attributes
;
25152 /* Combine the attributes. */
25153 attributes
= chainon (prefix_attributes
, attributes
);
25156 /* Create the bitfield declaration. */
25157 decl
= grokbitfield (declarator
, &declspecs
,
25161 decl
= grokfield (declarator
, &declspecs
,
25162 NULL_TREE
, /*init_const_expr_p=*/false,
25163 NULL_TREE
, attributes
);
25165 /* Add the instance variable. */
25166 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
25167 objc_add_instance_variable (decl
);
25169 /* Reset PREFIX_ATTRIBUTES. */
25170 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
25171 attributes
= TREE_CHAIN (attributes
);
25173 TREE_CHAIN (attributes
) = NULL_TREE
;
25175 token
= cp_lexer_peek_token (parser
->lexer
);
25177 if (token
->type
== CPP_COMMA
)
25179 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25185 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25186 token
= cp_lexer_peek_token (parser
->lexer
);
25189 if (token
->keyword
== RID_AT_END
)
25190 cp_parser_error (parser
, "expected %<}%>");
25192 /* Do not consume the RID_AT_END, so it will be read again as terminating
25193 the @interface of @implementation. */
25194 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
25195 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
25197 /* For historical reasons, we accept an optional semicolon. */
25198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25199 cp_lexer_consume_token (parser
->lexer
);
25202 /* Parse an Objective-C protocol declaration. */
25205 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
25207 tree proto
, protorefs
;
25210 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25211 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
25213 tok
= cp_lexer_peek_token (parser
->lexer
);
25214 error_at (tok
->location
, "identifier expected after %<@protocol%>");
25215 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25219 /* See if we have a forward declaration or a definition. */
25220 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25222 /* Try a forward declaration first. */
25223 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
25229 id
= cp_parser_identifier (parser
);
25230 if (id
== error_mark_node
)
25233 objc_declare_protocol (id
, attributes
);
25235 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25236 cp_lexer_consume_token (parser
->lexer
);
25240 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25243 /* Ok, we got a full-fledged definition (or at least should). */
25246 proto
= cp_parser_identifier (parser
);
25247 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
25248 objc_start_protocol (proto
, protorefs
, attributes
);
25249 cp_parser_objc_method_prototype_list (parser
);
25253 /* Parse an Objective-C superclass or category. */
25256 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
25259 tree
*categ
, bool *is_class_extension
)
25261 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
25263 *super
= *categ
= NULL_TREE
;
25264 *is_class_extension
= false;
25265 if (next
->type
== CPP_COLON
)
25267 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
25268 *super
= cp_parser_identifier (parser
);
25270 else if (next
->type
== CPP_OPEN_PAREN
)
25272 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
25274 /* If there is no category name, and this is an @interface, we
25275 have a class extension. */
25276 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25278 *categ
= NULL_TREE
;
25279 *is_class_extension
= true;
25282 *categ
= cp_parser_identifier (parser
);
25284 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25288 /* Parse an Objective-C class interface. */
25291 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
25293 tree name
, super
, categ
, protos
;
25294 bool is_class_extension
;
25296 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
25297 name
= cp_parser_identifier (parser
);
25298 if (name
== error_mark_node
)
25300 /* It's hard to recover because even if valid @interface stuff
25301 is to follow, we can't compile it (or validate it) if we
25302 don't even know which class it refers to. Let's assume this
25303 was a stray '@interface' token in the stream and skip it.
25307 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
25308 &is_class_extension
);
25309 protos
= cp_parser_objc_protocol_refs_opt (parser
);
25311 /* We have either a class or a category on our hands. */
25312 if (categ
|| is_class_extension
)
25313 objc_start_category_interface (name
, categ
, protos
, attributes
);
25316 objc_start_class_interface (name
, super
, protos
, attributes
);
25317 /* Handle instance variable declarations, if any. */
25318 cp_parser_objc_class_ivars (parser
);
25319 objc_continue_interface ();
25322 cp_parser_objc_method_prototype_list (parser
);
25325 /* Parse an Objective-C class implementation. */
25328 cp_parser_objc_class_implementation (cp_parser
* parser
)
25330 tree name
, super
, categ
;
25331 bool is_class_extension
;
25333 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
25334 name
= cp_parser_identifier (parser
);
25335 if (name
== error_mark_node
)
25337 /* It's hard to recover because even if valid @implementation
25338 stuff is to follow, we can't compile it (or validate it) if
25339 we don't even know which class it refers to. Let's assume
25340 this was a stray '@implementation' token in the stream and
25345 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
25346 &is_class_extension
);
25348 /* We have either a class or a category on our hands. */
25350 objc_start_category_implementation (name
, categ
);
25353 objc_start_class_implementation (name
, super
);
25354 /* Handle instance variable declarations, if any. */
25355 cp_parser_objc_class_ivars (parser
);
25356 objc_continue_implementation ();
25359 cp_parser_objc_method_definition_list (parser
);
25362 /* Consume the @end token and finish off the implementation. */
25365 cp_parser_objc_end_implementation (cp_parser
* parser
)
25367 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
25368 objc_finish_implementation ();
25371 /* Parse an Objective-C declaration. */
25374 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
25376 /* Try to figure out what kind of declaration is present. */
25377 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25380 switch (kwd
->keyword
)
25385 error_at (kwd
->location
, "attributes may not be specified before"
25386 " the %<@%D%> Objective-C++ keyword",
25390 case RID_AT_IMPLEMENTATION
:
25391 warning_at (kwd
->location
, OPT_Wattributes
,
25392 "prefix attributes are ignored before %<@%D%>",
25399 switch (kwd
->keyword
)
25402 cp_parser_objc_alias_declaration (parser
);
25405 cp_parser_objc_class_declaration (parser
);
25407 case RID_AT_PROTOCOL
:
25408 cp_parser_objc_protocol_declaration (parser
, attributes
);
25410 case RID_AT_INTERFACE
:
25411 cp_parser_objc_class_interface (parser
, attributes
);
25413 case RID_AT_IMPLEMENTATION
:
25414 cp_parser_objc_class_implementation (parser
);
25417 cp_parser_objc_end_implementation (parser
);
25420 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
25422 cp_parser_skip_to_end_of_block_or_statement (parser
);
25426 /* Parse an Objective-C try-catch-finally statement.
25428 objc-try-catch-finally-stmt:
25429 @try compound-statement objc-catch-clause-seq [opt]
25430 objc-finally-clause [opt]
25432 objc-catch-clause-seq:
25433 objc-catch-clause objc-catch-clause-seq [opt]
25436 @catch ( objc-exception-declaration ) compound-statement
25438 objc-finally-clause:
25439 @finally compound-statement
25441 objc-exception-declaration:
25442 parameter-declaration
25445 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25449 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25450 for C. Keep them in sync. */
25453 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
25455 location_t location
;
25458 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
25459 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
25460 objc_maybe_warn_exceptions (location
);
25461 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25462 node, lest it get absorbed into the surrounding block. */
25463 stmt
= push_stmt_list ();
25464 cp_parser_compound_statement (parser
, NULL
, false, false);
25465 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
25467 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
25469 cp_parameter_declarator
*parm
;
25470 tree parameter_declaration
= error_mark_node
;
25471 bool seen_open_paren
= false;
25473 cp_lexer_consume_token (parser
->lexer
);
25474 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25475 seen_open_paren
= true;
25476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25478 /* We have "@catch (...)" (where the '...' are literally
25479 what is in the code). Skip the '...'.
25480 parameter_declaration is set to NULL_TREE, and
25481 objc_being_catch_clauses() knows that that means
25483 cp_lexer_consume_token (parser
->lexer
);
25484 parameter_declaration
= NULL_TREE
;
25488 /* We have "@catch (NSException *exception)" or something
25489 like that. Parse the parameter declaration. */
25490 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
25492 parameter_declaration
= error_mark_node
;
25494 parameter_declaration
= grokdeclarator (parm
->declarator
,
25495 &parm
->decl_specifiers
,
25496 PARM
, /*initialized=*/0,
25497 /*attrlist=*/NULL
);
25499 if (seen_open_paren
)
25500 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25503 /* If there was no open parenthesis, we are recovering from
25504 an error, and we are trying to figure out what mistake
25505 the user has made. */
25507 /* If there is an immediate closing parenthesis, the user
25508 probably forgot the opening one (ie, they typed "@catch
25509 NSException *e)". Parse the closing parenthesis and keep
25511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25512 cp_lexer_consume_token (parser
->lexer
);
25514 /* If these is no immediate closing parenthesis, the user
25515 probably doesn't know that parenthesis are required at
25516 all (ie, they typed "@catch NSException *e"). So, just
25517 forget about the closing parenthesis and keep going. */
25519 objc_begin_catch_clause (parameter_declaration
);
25520 cp_parser_compound_statement (parser
, NULL
, false, false);
25521 objc_finish_catch_clause ();
25523 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
25525 cp_lexer_consume_token (parser
->lexer
);
25526 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
25527 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25528 node, lest it get absorbed into the surrounding block. */
25529 stmt
= push_stmt_list ();
25530 cp_parser_compound_statement (parser
, NULL
, false, false);
25531 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
25534 return objc_finish_try_stmt ();
25537 /* Parse an Objective-C synchronized statement.
25539 objc-synchronized-stmt:
25540 @synchronized ( expression ) compound-statement
25542 Returns NULL_TREE. */
25545 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
25547 location_t location
;
25550 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
25552 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
25553 objc_maybe_warn_exceptions (location
);
25554 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25555 lock
= cp_parser_expression (parser
, false, NULL
);
25556 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25558 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25559 node, lest it get absorbed into the surrounding block. */
25560 stmt
= push_stmt_list ();
25561 cp_parser_compound_statement (parser
, NULL
, false, false);
25563 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
25566 /* Parse an Objective-C throw statement.
25569 @throw assignment-expression [opt] ;
25571 Returns a constructed '@throw' statement. */
25574 cp_parser_objc_throw_statement (cp_parser
*parser
)
25576 tree expr
= NULL_TREE
;
25577 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25579 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
25581 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25582 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
25584 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25586 return objc_build_throw_stmt (loc
, expr
);
25589 /* Parse an Objective-C statement. */
25592 cp_parser_objc_statement (cp_parser
* parser
)
25594 /* Try to figure out what kind of declaration is present. */
25595 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25597 switch (kwd
->keyword
)
25600 return cp_parser_objc_try_catch_finally_statement (parser
);
25601 case RID_AT_SYNCHRONIZED
:
25602 return cp_parser_objc_synchronized_statement (parser
);
25604 return cp_parser_objc_throw_statement (parser
);
25606 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
25608 cp_parser_skip_to_end_of_block_or_statement (parser
);
25611 return error_mark_node
;
25614 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25615 look ahead to see if an objc keyword follows the attributes. This
25616 is to detect the use of prefix attributes on ObjC @interface and
25620 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
25622 cp_lexer_save_tokens (parser
->lexer
);
25623 *attrib
= cp_parser_attributes_opt (parser
);
25624 gcc_assert (*attrib
);
25625 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
25627 cp_lexer_commit_tokens (parser
->lexer
);
25630 cp_lexer_rollback_tokens (parser
->lexer
);
25634 /* This routine is a minimal replacement for
25635 c_parser_struct_declaration () used when parsing the list of
25636 types/names or ObjC++ properties. For example, when parsing the
25639 @property (readonly) int a, b, c;
25641 this function is responsible for parsing "int a, int b, int c" and
25642 returning the declarations as CHAIN of DECLs.
25644 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25645 similar parsing. */
25647 cp_parser_objc_struct_declaration (cp_parser
*parser
)
25649 tree decls
= NULL_TREE
;
25650 cp_decl_specifier_seq declspecs
;
25651 int decl_class_or_enum_p
;
25652 tree prefix_attributes
;
25654 cp_parser_decl_specifier_seq (parser
,
25655 CP_PARSER_FLAGS_NONE
,
25657 &decl_class_or_enum_p
);
25659 if (declspecs
.type
== error_mark_node
)
25660 return error_mark_node
;
25662 /* auto, register, static, extern, mutable. */
25663 if (declspecs
.storage_class
!= sc_none
)
25665 cp_parser_error (parser
, "invalid type for property");
25666 declspecs
.storage_class
= sc_none
;
25669 /* thread_local. */
25670 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
25672 cp_parser_error (parser
, "invalid type for property");
25673 declspecs
.locations
[ds_thread
] = 0;
25677 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
25679 cp_parser_error (parser
, "invalid type for property");
25680 declspecs
.locations
[ds_typedef
] = 0;
25683 prefix_attributes
= declspecs
.attributes
;
25684 declspecs
.attributes
= NULL_TREE
;
25686 /* Keep going until we hit the `;' at the end of the declaration. */
25687 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25689 tree attributes
, first_attribute
, decl
;
25690 cp_declarator
*declarator
;
25693 /* Parse the declarator. */
25694 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25695 NULL
, NULL
, false);
25697 /* Look for attributes that apply to the ivar. */
25698 attributes
= cp_parser_attributes_opt (parser
);
25699 /* Remember which attributes are prefix attributes and
25701 first_attribute
= attributes
;
25702 /* Combine the attributes. */
25703 attributes
= chainon (prefix_attributes
, attributes
);
25705 decl
= grokfield (declarator
, &declspecs
,
25706 NULL_TREE
, /*init_const_expr_p=*/false,
25707 NULL_TREE
, attributes
);
25709 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
25710 return error_mark_node
;
25712 /* Reset PREFIX_ATTRIBUTES. */
25713 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
25714 attributes
= TREE_CHAIN (attributes
);
25716 TREE_CHAIN (attributes
) = NULL_TREE
;
25718 DECL_CHAIN (decl
) = decls
;
25721 token
= cp_lexer_peek_token (parser
->lexer
);
25722 if (token
->type
== CPP_COMMA
)
25724 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25733 /* Parse an Objective-C @property declaration. The syntax is:
25735 objc-property-declaration:
25736 '@property' objc-property-attributes[opt] struct-declaration ;
25738 objc-property-attributes:
25739 '(' objc-property-attribute-list ')'
25741 objc-property-attribute-list:
25742 objc-property-attribute
25743 objc-property-attribute-list, objc-property-attribute
25745 objc-property-attribute
25746 'getter' = identifier
25747 'setter' = identifier
25756 @property NSString *name;
25757 @property (readonly) id object;
25758 @property (retain, nonatomic, getter=getTheName) id name;
25759 @property int a, b, c;
25761 PS: This function is identical to
25762 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25764 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
25766 /* The following variables hold the attributes of the properties as
25767 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25768 seen. When we see an attribute, we set them to 'true' (if they
25769 are boolean properties) or to the identifier (if they have an
25770 argument, ie, for getter and setter). Note that here we only
25771 parse the list of attributes, check the syntax and accumulate the
25772 attributes that we find. objc_add_property_declaration() will
25773 then process the information. */
25774 bool property_assign
= false;
25775 bool property_copy
= false;
25776 tree property_getter_ident
= NULL_TREE
;
25777 bool property_nonatomic
= false;
25778 bool property_readonly
= false;
25779 bool property_readwrite
= false;
25780 bool property_retain
= false;
25781 tree property_setter_ident
= NULL_TREE
;
25783 /* 'properties' is the list of properties that we read. Usually a
25784 single one, but maybe more (eg, in "@property int a, b, c;" there
25789 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25791 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
25793 /* Parse the optional attribute list... */
25794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25797 cp_lexer_consume_token (parser
->lexer
);
25801 bool syntax_error
= false;
25802 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25805 if (token
->type
!= CPP_NAME
)
25807 cp_parser_error (parser
, "expected identifier");
25810 keyword
= C_RID_CODE (token
->u
.value
);
25811 cp_lexer_consume_token (parser
->lexer
);
25814 case RID_ASSIGN
: property_assign
= true; break;
25815 case RID_COPY
: property_copy
= true; break;
25816 case RID_NONATOMIC
: property_nonatomic
= true; break;
25817 case RID_READONLY
: property_readonly
= true; break;
25818 case RID_READWRITE
: property_readwrite
= true; break;
25819 case RID_RETAIN
: property_retain
= true; break;
25823 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
25825 if (keyword
== RID_GETTER
)
25826 cp_parser_error (parser
,
25827 "missing %<=%> (after %<getter%> attribute)");
25829 cp_parser_error (parser
,
25830 "missing %<=%> (after %<setter%> attribute)");
25831 syntax_error
= true;
25834 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
25835 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
25837 cp_parser_error (parser
, "expected identifier");
25838 syntax_error
= true;
25841 if (keyword
== RID_SETTER
)
25843 if (property_setter_ident
!= NULL_TREE
)
25845 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
25846 cp_lexer_consume_token (parser
->lexer
);
25849 property_setter_ident
= cp_parser_objc_selector (parser
);
25850 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25851 cp_parser_error (parser
, "setter name must terminate with %<:%>");
25853 cp_lexer_consume_token (parser
->lexer
);
25857 if (property_getter_ident
!= NULL_TREE
)
25859 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
25860 cp_lexer_consume_token (parser
->lexer
);
25863 property_getter_ident
= cp_parser_objc_selector (parser
);
25867 cp_parser_error (parser
, "unknown property attribute");
25868 syntax_error
= true;
25875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25876 cp_lexer_consume_token (parser
->lexer
);
25881 /* FIXME: "@property (setter, assign);" will generate a spurious
25882 "error: expected ‘)’ before ‘,’ token". This is because
25883 cp_parser_require, unlike the C counterpart, will produce an
25884 error even if we are in error recovery. */
25885 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25887 cp_parser_skip_to_closing_parenthesis (parser
,
25888 /*recovering=*/true,
25889 /*or_comma=*/false,
25890 /*consume_paren=*/true);
25894 /* ... and the property declaration(s). */
25895 properties
= cp_parser_objc_struct_declaration (parser
);
25897 if (properties
== error_mark_node
)
25899 cp_parser_skip_to_end_of_statement (parser
);
25900 /* If the next token is now a `;', consume it. */
25901 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25902 cp_lexer_consume_token (parser
->lexer
);
25906 if (properties
== NULL_TREE
)
25907 cp_parser_error (parser
, "expected identifier");
25910 /* Comma-separated properties are chained together in
25911 reverse order; add them one by one. */
25912 properties
= nreverse (properties
);
25914 for (; properties
; properties
= TREE_CHAIN (properties
))
25915 objc_add_property_declaration (loc
, copy_node (properties
),
25916 property_readonly
, property_readwrite
,
25917 property_assign
, property_retain
,
25918 property_copy
, property_nonatomic
,
25919 property_getter_ident
, property_setter_ident
);
25922 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25925 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25927 objc-synthesize-declaration:
25928 @synthesize objc-synthesize-identifier-list ;
25930 objc-synthesize-identifier-list:
25931 objc-synthesize-identifier
25932 objc-synthesize-identifier-list, objc-synthesize-identifier
25934 objc-synthesize-identifier
25936 identifier = identifier
25939 @synthesize MyProperty;
25940 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25942 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25943 for C. Keep them in sync.
25946 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
25948 tree list
= NULL_TREE
;
25950 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25952 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
25955 tree property
, ivar
;
25956 property
= cp_parser_identifier (parser
);
25957 if (property
== error_mark_node
)
25959 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
25964 cp_lexer_consume_token (parser
->lexer
);
25965 ivar
= cp_parser_identifier (parser
);
25966 if (ivar
== error_mark_node
)
25968 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25974 list
= chainon (list
, build_tree_list (ivar
, property
));
25975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25976 cp_lexer_consume_token (parser
->lexer
);
25980 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25981 objc_add_synthesize_declaration (loc
, list
);
25984 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25986 objc-dynamic-declaration:
25987 @dynamic identifier-list ;
25990 @dynamic MyProperty;
25991 @dynamic MyProperty, AnotherProperty;
25993 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25994 for C. Keep them in sync.
25997 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
25999 tree list
= NULL_TREE
;
26001 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26003 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
26007 property
= cp_parser_identifier (parser
);
26008 if (property
== error_mark_node
)
26010 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26013 list
= chainon (list
, build_tree_list (NULL
, property
));
26014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26015 cp_lexer_consume_token (parser
->lexer
);
26019 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26020 objc_add_dynamic_declaration (loc
, list
);
26024 /* OpenMP 2.5 parsing routines. */
26026 /* Returns name of the next clause.
26027 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26028 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26029 returned and the token is consumed. */
26031 static pragma_omp_clause
26032 cp_parser_omp_clause_name (cp_parser
*parser
)
26034 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
26036 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
26037 result
= PRAGMA_OMP_CLAUSE_IF
;
26038 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
26039 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
26040 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
26041 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
26042 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26044 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26045 const char *p
= IDENTIFIER_POINTER (id
);
26050 if (!strcmp ("collapse", p
))
26051 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
26052 else if (!strcmp ("copyin", p
))
26053 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
26054 else if (!strcmp ("copyprivate", p
))
26055 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
26058 if (!strcmp ("final", p
))
26059 result
= PRAGMA_OMP_CLAUSE_FINAL
;
26060 else if (!strcmp ("firstprivate", p
))
26061 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
26064 if (!strcmp ("lastprivate", p
))
26065 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
26068 if (!strcmp ("mergeable", p
))
26069 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
26072 if (!strcmp ("nowait", p
))
26073 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
26074 else if (!strcmp ("num_threads", p
))
26075 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
26078 if (!strcmp ("ordered", p
))
26079 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
26082 if (!strcmp ("reduction", p
))
26083 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
26086 if (!strcmp ("schedule", p
))
26087 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
26088 else if (!strcmp ("shared", p
))
26089 result
= PRAGMA_OMP_CLAUSE_SHARED
;
26092 if (!strcmp ("untied", p
))
26093 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
26098 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
26099 cp_lexer_consume_token (parser
->lexer
);
26104 /* Validate that a clause of the given type does not already exist. */
26107 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
26108 const char *name
, location_t location
)
26112 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
26113 if (OMP_CLAUSE_CODE (c
) == code
)
26115 error_at (location
, "too many %qs clauses", name
);
26123 variable-list , identifier
26125 In addition, we match a closing parenthesis. An opening parenthesis
26126 will have been consumed by the caller.
26128 If KIND is nonzero, create the appropriate node and install the decl
26129 in OMP_CLAUSE_DECL and add the node to the head of the list.
26131 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26132 return the list created. */
26135 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
26143 token
= cp_lexer_peek_token (parser
->lexer
);
26144 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
26145 /*check_dependency_p=*/true,
26146 /*template_p=*/NULL
,
26147 /*declarator_p=*/false,
26148 /*optional_p=*/false);
26149 if (name
== error_mark_node
)
26152 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
26153 if (decl
== error_mark_node
)
26154 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
26156 else if (kind
!= 0)
26158 tree u
= build_omp_clause (token
->location
, kind
);
26159 OMP_CLAUSE_DECL (u
) = decl
;
26160 OMP_CLAUSE_CHAIN (u
) = list
;
26164 list
= tree_cons (decl
, NULL_TREE
, list
);
26167 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26169 cp_lexer_consume_token (parser
->lexer
);
26172 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26176 /* Try to resync to an unnested comma. Copied from
26177 cp_parser_parenthesized_expression_list. */
26179 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
26180 /*recovering=*/true,
26182 /*consume_paren=*/true);
26190 /* Similarly, but expect leading and trailing parenthesis. This is a very
26191 common case for omp clauses. */
26194 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
26196 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26197 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
26202 collapse ( constant-expression ) */
26205 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
26211 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26212 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26215 num
= cp_parser_constant_expression (parser
, false, NULL
);
26217 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26218 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26219 /*or_comma=*/false,
26220 /*consume_paren=*/true);
26222 if (num
== error_mark_node
)
26224 num
= fold_non_dependent_expr (num
);
26225 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
26226 || !host_integerp (num
, 0)
26227 || (n
= tree_low_cst (num
, 0)) <= 0
26230 error_at (loc
, "collapse argument needs positive constant integer expression");
26234 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
26235 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
26236 OMP_CLAUSE_CHAIN (c
) = list
;
26237 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
26243 default ( shared | none ) */
26246 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
26248 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
26251 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26255 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26256 const char *p
= IDENTIFIER_POINTER (id
);
26261 if (strcmp ("none", p
) != 0)
26263 kind
= OMP_CLAUSE_DEFAULT_NONE
;
26267 if (strcmp ("shared", p
) != 0)
26269 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
26276 cp_lexer_consume_token (parser
->lexer
);
26281 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
26284 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26285 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26286 /*or_comma=*/false,
26287 /*consume_paren=*/true);
26289 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
26292 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
26293 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
26294 OMP_CLAUSE_CHAIN (c
) = list
;
26295 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
26301 final ( expression ) */
26304 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
26308 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26311 t
= cp_parser_condition (parser
);
26313 if (t
== error_mark_node
26314 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26315 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26316 /*or_comma=*/false,
26317 /*consume_paren=*/true);
26319 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
26321 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
26322 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
26323 OMP_CLAUSE_CHAIN (c
) = list
;
26329 if ( expression ) */
26332 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
26336 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26339 t
= cp_parser_condition (parser
);
26341 if (t
== error_mark_node
26342 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26343 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26344 /*or_comma=*/false,
26345 /*consume_paren=*/true);
26347 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
26349 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
26350 OMP_CLAUSE_IF_EXPR (c
) = t
;
26351 OMP_CLAUSE_CHAIN (c
) = list
;
26360 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
26361 tree list
, location_t location
)
26365 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
26368 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
26369 OMP_CLAUSE_CHAIN (c
) = list
;
26377 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
26378 tree list
, location_t location
)
26382 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
26384 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
26385 OMP_CLAUSE_CHAIN (c
) = list
;
26390 num_threads ( expression ) */
26393 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
26394 location_t location
)
26398 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26401 t
= cp_parser_expression (parser
, false, NULL
);
26403 if (t
== error_mark_node
26404 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26405 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26406 /*or_comma=*/false,
26407 /*consume_paren=*/true);
26409 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
26410 "num_threads", location
);
26412 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
26413 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
26414 OMP_CLAUSE_CHAIN (c
) = list
;
26423 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
26424 tree list
, location_t location
)
26428 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
26429 "ordered", location
);
26431 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
26432 OMP_CLAUSE_CHAIN (c
) = list
;
26437 reduction ( reduction-operator : variable-list )
26439 reduction-operator:
26440 One of: + * - & ^ | && ||
26444 reduction-operator:
26445 One of: + * - & ^ | && || min max */
26448 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
26450 enum tree_code code
;
26453 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26456 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
26468 code
= BIT_AND_EXPR
;
26471 code
= BIT_XOR_EXPR
;
26474 code
= BIT_IOR_EXPR
;
26477 code
= TRUTH_ANDIF_EXPR
;
26480 code
= TRUTH_ORIF_EXPR
;
26484 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26485 const char *p
= IDENTIFIER_POINTER (id
);
26487 if (strcmp (p
, "min") == 0)
26492 if (strcmp (p
, "max") == 0)
26500 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26501 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26503 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26504 /*or_comma=*/false,
26505 /*consume_paren=*/true);
26508 cp_lexer_consume_token (parser
->lexer
);
26510 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26513 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
26514 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
26515 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
26521 schedule ( schedule-kind )
26522 schedule ( schedule-kind , expression )
26525 static | dynamic | guided | runtime | auto */
26528 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
26532 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26535 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
26537 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26539 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26540 const char *p
= IDENTIFIER_POINTER (id
);
26545 if (strcmp ("dynamic", p
) != 0)
26547 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
26551 if (strcmp ("guided", p
) != 0)
26553 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
26557 if (strcmp ("runtime", p
) != 0)
26559 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
26566 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
26567 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
26568 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
26569 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
26572 cp_lexer_consume_token (parser
->lexer
);
26574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26577 cp_lexer_consume_token (parser
->lexer
);
26579 token
= cp_lexer_peek_token (parser
->lexer
);
26580 t
= cp_parser_assignment_expression (parser
, false, NULL
);
26582 if (t
== error_mark_node
)
26584 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
26585 error_at (token
->location
, "schedule %<runtime%> does not take "
26586 "a %<chunk_size%> parameter");
26587 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
26588 error_at (token
->location
, "schedule %<auto%> does not take "
26589 "a %<chunk_size%> parameter");
26591 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
26593 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26596 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
26599 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
26600 OMP_CLAUSE_CHAIN (c
) = list
;
26604 cp_parser_error (parser
, "invalid schedule kind");
26606 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26607 /*or_comma=*/false,
26608 /*consume_paren=*/true);
26616 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
26617 tree list
, location_t location
)
26621 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
26623 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
26624 OMP_CLAUSE_CHAIN (c
) = list
;
26628 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26629 is a bitmask in MASK. Return the list of clauses found; the result
26630 of clause default goes in *pdefault. */
26633 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
26634 const char *where
, cp_token
*pragma_tok
)
26636 tree clauses
= NULL
;
26638 cp_token
*token
= NULL
;
26640 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
26642 pragma_omp_clause c_kind
;
26643 const char *c_name
;
26644 tree prev
= clauses
;
26646 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26647 cp_lexer_consume_token (parser
->lexer
);
26649 token
= cp_lexer_peek_token (parser
->lexer
);
26650 c_kind
= cp_parser_omp_clause_name (parser
);
26655 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
26656 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
26658 c_name
= "collapse";
26660 case PRAGMA_OMP_CLAUSE_COPYIN
:
26661 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
26664 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
26665 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
26667 c_name
= "copyprivate";
26669 case PRAGMA_OMP_CLAUSE_DEFAULT
:
26670 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
26672 c_name
= "default";
26674 case PRAGMA_OMP_CLAUSE_FINAL
:
26675 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
26678 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
26679 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
26681 c_name
= "firstprivate";
26683 case PRAGMA_OMP_CLAUSE_IF
:
26684 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
26687 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
26688 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
26690 c_name
= "lastprivate";
26692 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
26693 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
26695 c_name
= "mergeable";
26697 case PRAGMA_OMP_CLAUSE_NOWAIT
:
26698 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
26701 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
26702 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
26704 c_name
= "num_threads";
26706 case PRAGMA_OMP_CLAUSE_ORDERED
:
26707 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
26709 c_name
= "ordered";
26711 case PRAGMA_OMP_CLAUSE_PRIVATE
:
26712 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
26714 c_name
= "private";
26716 case PRAGMA_OMP_CLAUSE_REDUCTION
:
26717 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
26718 c_name
= "reduction";
26720 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
26721 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
26723 c_name
= "schedule";
26725 case PRAGMA_OMP_CLAUSE_SHARED
:
26726 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
26730 case PRAGMA_OMP_CLAUSE_UNTIED
:
26731 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
26736 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
26740 if (((mask
>> c_kind
) & 1) == 0)
26742 /* Remove the invalid clause(s) from the list to avoid
26743 confusing the rest of the compiler. */
26745 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
26749 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
26750 return finish_omp_clauses (clauses
);
26757 In practice, we're also interested in adding the statement to an
26758 outer node. So it is convenient if we work around the fact that
26759 cp_parser_statement calls add_stmt. */
26762 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
26764 unsigned save
= parser
->in_statement
;
26766 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26767 This preserves the "not within loop or switch" style error messages
26768 for nonsense cases like
26774 if (parser
->in_statement
)
26775 parser
->in_statement
= IN_OMP_BLOCK
;
26781 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
26783 parser
->in_statement
= save
;
26787 cp_parser_omp_structured_block (cp_parser
*parser
)
26789 tree stmt
= begin_omp_structured_block ();
26790 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
26792 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
26794 cp_parser_end_omp_structured_block (parser
, save
);
26795 return finish_omp_structured_block (stmt
);
26799 # pragma omp atomic new-line
26803 x binop= expr | x++ | ++x | x-- | --x
26805 +, *, -, /, &, ^, |, <<, >>
26807 where x is an lvalue expression with scalar type.
26810 # pragma omp atomic new-line
26813 # pragma omp atomic read new-line
26816 # pragma omp atomic write new-line
26819 # pragma omp atomic update new-line
26822 # pragma omp atomic capture new-line
26825 # pragma omp atomic capture new-line
26833 expression-stmt | x = x binop expr
26835 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26837 { v = x; update-stmt; } | { update-stmt; v = x; }
26839 where x and v are lvalue expressions with scalar type. */
26842 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
26844 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
26845 tree rhs1
= NULL_TREE
, orig_lhs
;
26846 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
26847 bool structured_block
= false;
26849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26851 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26852 const char *p
= IDENTIFIER_POINTER (id
);
26854 if (!strcmp (p
, "read"))
26855 code
= OMP_ATOMIC_READ
;
26856 else if (!strcmp (p
, "write"))
26858 else if (!strcmp (p
, "update"))
26860 else if (!strcmp (p
, "capture"))
26861 code
= OMP_ATOMIC_CAPTURE_NEW
;
26865 cp_lexer_consume_token (parser
->lexer
);
26867 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26871 case OMP_ATOMIC_READ
:
26872 case NOP_EXPR
: /* atomic write */
26873 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26874 /*cast_p=*/false, NULL
);
26875 if (v
== error_mark_node
)
26877 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
26879 if (code
== NOP_EXPR
)
26880 lhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
26882 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26883 /*cast_p=*/false, NULL
);
26884 if (lhs
== error_mark_node
)
26886 if (code
== NOP_EXPR
)
26888 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26896 case OMP_ATOMIC_CAPTURE_NEW
:
26897 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26899 cp_lexer_consume_token (parser
->lexer
);
26900 structured_block
= true;
26904 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26905 /*cast_p=*/false, NULL
);
26906 if (v
== error_mark_node
)
26908 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
26916 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26917 /*cast_p=*/false, NULL
);
26919 switch (TREE_CODE (lhs
))
26924 case POSTINCREMENT_EXPR
:
26925 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
26926 code
= OMP_ATOMIC_CAPTURE_OLD
;
26928 case PREINCREMENT_EXPR
:
26929 lhs
= TREE_OPERAND (lhs
, 0);
26930 opcode
= PLUS_EXPR
;
26931 rhs
= integer_one_node
;
26934 case POSTDECREMENT_EXPR
:
26935 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
26936 code
= OMP_ATOMIC_CAPTURE_OLD
;
26938 case PREDECREMENT_EXPR
:
26939 lhs
= TREE_OPERAND (lhs
, 0);
26940 opcode
= MINUS_EXPR
;
26941 rhs
= integer_one_node
;
26944 case COMPOUND_EXPR
:
26945 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
26946 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
26947 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
26948 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
26949 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26950 (TREE_OPERAND (lhs
, 1), 0), 0)))
26952 /* Undo effects of boolean_increment for post {in,de}crement. */
26953 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
26956 if (TREE_CODE (lhs
) == MODIFY_EXPR
26957 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
26959 /* Undo effects of boolean_increment. */
26960 if (integer_onep (TREE_OPERAND (lhs
, 1)))
26962 /* This is pre or post increment. */
26963 rhs
= TREE_OPERAND (lhs
, 1);
26964 lhs
= TREE_OPERAND (lhs
, 0);
26966 if (code
== OMP_ATOMIC_CAPTURE_NEW
26967 && !structured_block
26968 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
26969 code
= OMP_ATOMIC_CAPTURE_OLD
;
26975 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
26978 opcode
= MULT_EXPR
;
26981 opcode
= TRUNC_DIV_EXPR
;
26984 opcode
= PLUS_EXPR
;
26987 opcode
= MINUS_EXPR
;
26989 case CPP_LSHIFT_EQ
:
26990 opcode
= LSHIFT_EXPR
;
26992 case CPP_RSHIFT_EQ
:
26993 opcode
= RSHIFT_EXPR
;
26996 opcode
= BIT_AND_EXPR
;
26999 opcode
= BIT_IOR_EXPR
;
27002 opcode
= BIT_XOR_EXPR
;
27005 if (structured_block
|| code
== OMP_ATOMIC
)
27007 enum cp_parser_prec oprec
;
27009 cp_lexer_consume_token (parser
->lexer
);
27010 rhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
27011 /*cast_p=*/false, NULL
);
27012 if (rhs1
== error_mark_node
)
27014 token
= cp_lexer_peek_token (parser
->lexer
);
27015 switch (token
->type
)
27017 case CPP_SEMICOLON
:
27018 if (code
== OMP_ATOMIC_CAPTURE_NEW
)
27020 code
= OMP_ATOMIC_CAPTURE_OLD
;
27025 cp_lexer_consume_token (parser
->lexer
);
27028 cp_parser_error (parser
,
27029 "invalid form of %<#pragma omp atomic%>");
27032 opcode
= MULT_EXPR
;
27035 opcode
= TRUNC_DIV_EXPR
;
27038 opcode
= PLUS_EXPR
;
27041 opcode
= MINUS_EXPR
;
27044 opcode
= LSHIFT_EXPR
;
27047 opcode
= RSHIFT_EXPR
;
27050 opcode
= BIT_AND_EXPR
;
27053 opcode
= BIT_IOR_EXPR
;
27056 opcode
= BIT_XOR_EXPR
;
27059 cp_parser_error (parser
,
27060 "invalid operator for %<#pragma omp atomic%>");
27063 oprec
= TOKEN_PRECEDENCE (token
);
27064 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
27065 if (commutative_tree_code (opcode
))
27066 oprec
= (enum cp_parser_prec
) (oprec
- 1);
27067 cp_lexer_consume_token (parser
->lexer
);
27068 rhs
= cp_parser_binary_expression (parser
, false, false,
27070 if (rhs
== error_mark_node
)
27076 cp_parser_error (parser
,
27077 "invalid operator for %<#pragma omp atomic%>");
27080 cp_lexer_consume_token (parser
->lexer
);
27082 rhs
= cp_parser_expression (parser
, false, NULL
);
27083 if (rhs
== error_mark_node
)
27088 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
27090 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
27092 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
27093 /*cast_p=*/false, NULL
);
27094 if (v
== error_mark_node
)
27096 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
27098 lhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
27099 /*cast_p=*/false, NULL
);
27100 if (lhs1
== error_mark_node
)
27103 if (structured_block
)
27105 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27106 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
27109 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
27110 if (!structured_block
)
27111 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27115 cp_parser_skip_to_end_of_block_or_statement (parser
);
27116 if (structured_block
)
27118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
27119 cp_lexer_consume_token (parser
->lexer
);
27120 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
27122 cp_parser_skip_to_end_of_block_or_statement (parser
);
27123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
27124 cp_lexer_consume_token (parser
->lexer
);
27131 # pragma omp barrier new-line */
27134 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
27136 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27137 finish_omp_barrier ();
27141 # pragma omp critical [(name)] new-line
27142 structured-block */
27145 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
27147 tree stmt
, name
= NULL
;
27149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27151 cp_lexer_consume_token (parser
->lexer
);
27153 name
= cp_parser_identifier (parser
);
27155 if (name
== error_mark_node
27156 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27157 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27158 /*or_comma=*/false,
27159 /*consume_paren=*/true);
27160 if (name
== error_mark_node
)
27163 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27165 stmt
= cp_parser_omp_structured_block (parser
);
27166 return c_finish_omp_critical (input_location
, stmt
, name
);
27170 # pragma omp flush flush-vars[opt] new-line
27173 ( variable-list ) */
27176 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
27178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27179 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
27180 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27182 finish_omp_flush ();
27185 /* Helper function, to parse omp for increment expression. */
27188 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
27190 tree cond
= cp_parser_binary_expression (parser
, false, true,
27191 PREC_NOT_OPERATOR
, NULL
);
27192 if (cond
== error_mark_node
27193 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27195 cp_parser_skip_to_end_of_statement (parser
);
27196 return error_mark_node
;
27199 switch (TREE_CODE (cond
))
27207 return error_mark_node
;
27210 /* If decl is an iterator, preserve LHS and RHS of the relational
27211 expr until finish_omp_for. */
27213 && (type_dependent_expression_p (decl
)
27214 || CLASS_TYPE_P (TREE_TYPE (decl
))))
27217 return build_x_binary_op (input_location
, TREE_CODE (cond
),
27218 TREE_OPERAND (cond
, 0), ERROR_MARK
,
27219 TREE_OPERAND (cond
, 1), ERROR_MARK
,
27220 /*overload=*/NULL
, tf_warning_or_error
);
27223 /* Helper function, to parse omp for increment expression. */
27226 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
27228 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27234 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
27236 op
= (token
->type
== CPP_PLUS_PLUS
27237 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
27238 cp_lexer_consume_token (parser
->lexer
);
27239 lhs
= cp_parser_simple_cast_expression (parser
);
27241 return error_mark_node
;
27242 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
27245 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
27247 return error_mark_node
;
27249 token
= cp_lexer_peek_token (parser
->lexer
);
27250 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
27252 op
= (token
->type
== CPP_PLUS_PLUS
27253 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
27254 cp_lexer_consume_token (parser
->lexer
);
27255 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
27258 op
= cp_parser_assignment_operator_opt (parser
);
27259 if (op
== ERROR_MARK
)
27260 return error_mark_node
;
27262 if (op
!= NOP_EXPR
)
27264 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
27265 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
27266 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
27269 lhs
= cp_parser_binary_expression (parser
, false, false,
27270 PREC_ADDITIVE_EXPRESSION
, NULL
);
27271 token
= cp_lexer_peek_token (parser
->lexer
);
27272 decl_first
= lhs
== decl
;
27275 if (token
->type
!= CPP_PLUS
27276 && token
->type
!= CPP_MINUS
)
27277 return error_mark_node
;
27281 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
27282 cp_lexer_consume_token (parser
->lexer
);
27283 rhs
= cp_parser_binary_expression (parser
, false, false,
27284 PREC_ADDITIVE_EXPRESSION
, NULL
);
27285 token
= cp_lexer_peek_token (parser
->lexer
);
27286 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
27288 if (lhs
== NULL_TREE
)
27290 if (op
== PLUS_EXPR
)
27293 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
27294 tf_warning_or_error
);
27297 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
27298 ERROR_MARK
, NULL
, tf_warning_or_error
);
27301 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
27305 if (rhs
!= decl
|| op
== MINUS_EXPR
)
27306 return error_mark_node
;
27307 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
27310 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
27312 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
27315 /* Parse the restricted form of the for statement allowed by OpenMP. */
27318 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
27320 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
27321 tree real_decl
, initv
, condv
, incrv
, declv
;
27322 tree this_pre_body
, cl
;
27323 location_t loc_first
;
27324 bool collapse_err
= false;
27325 int i
, collapse
= 1, nbraces
= 0;
27326 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
27328 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
27329 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
27330 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
27332 gcc_assert (collapse
>= 1);
27334 declv
= make_tree_vec (collapse
);
27335 initv
= make_tree_vec (collapse
);
27336 condv
= make_tree_vec (collapse
);
27337 incrv
= make_tree_vec (collapse
);
27339 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
27341 for (i
= 0; i
< collapse
; i
++)
27343 int bracecount
= 0;
27344 bool add_private_clause
= false;
27347 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27349 cp_parser_error (parser
, "for statement expected");
27352 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
27354 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27357 init
= decl
= real_decl
= NULL
;
27358 this_pre_body
= push_stmt_list ();
27359 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27361 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
27365 integer-type var = lb
27366 random-access-iterator-type var = lb
27367 pointer-type var = lb
27369 cp_decl_specifier_seq type_specifiers
;
27371 /* First, try to parse as an initialized declaration. See
27372 cp_parser_condition, from whence the bulk of this is copied. */
27374 cp_parser_parse_tentatively (parser
);
27375 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
27376 /*is_trailing_return=*/false,
27378 if (cp_parser_parse_definitely (parser
))
27380 /* If parsing a type specifier seq succeeded, then this
27381 MUST be a initialized declaration. */
27382 tree asm_specification
, attributes
;
27383 cp_declarator
*declarator
;
27385 declarator
= cp_parser_declarator (parser
,
27386 CP_PARSER_DECLARATOR_NAMED
,
27387 /*ctor_dtor_or_conv_p=*/NULL
,
27388 /*parenthesized_p=*/NULL
,
27389 /*member_p=*/false);
27390 attributes
= cp_parser_attributes_opt (parser
);
27391 asm_specification
= cp_parser_asm_specification_opt (parser
);
27393 if (declarator
== cp_error_declarator
)
27394 cp_parser_skip_to_end_of_statement (parser
);
27398 tree pushed_scope
, auto_node
;
27400 decl
= start_decl (declarator
, &type_specifiers
,
27401 SD_INITIALIZED
, attributes
,
27402 /*prefix_attributes=*/NULL_TREE
,
27405 auto_node
= type_uses_auto (TREE_TYPE (decl
));
27406 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
27408 if (cp_lexer_next_token_is (parser
->lexer
,
27410 error ("parenthesized initialization is not allowed in "
27411 "OpenMP %<for%> loop");
27413 /* Trigger an error. */
27414 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
27416 init
= error_mark_node
;
27417 cp_parser_skip_to_end_of_statement (parser
);
27419 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
27420 || type_dependent_expression_p (decl
)
27423 bool is_direct_init
, is_non_constant_init
;
27425 init
= cp_parser_initializer (parser
,
27427 &is_non_constant_init
);
27432 = do_auto_deduction (TREE_TYPE (decl
), init
,
27435 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
27436 && !type_dependent_expression_p (decl
))
27440 cp_finish_decl (decl
, init
, !is_non_constant_init
,
27442 LOOKUP_ONLYCONVERTING
);
27443 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
27445 vec_safe_push (for_block
, this_pre_body
);
27449 init
= pop_stmt_list (this_pre_body
);
27450 this_pre_body
= NULL_TREE
;
27455 cp_lexer_consume_token (parser
->lexer
);
27456 init
= cp_parser_assignment_expression (parser
, false, NULL
);
27459 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
27460 init
= error_mark_node
;
27462 cp_finish_decl (decl
, NULL_TREE
,
27463 /*init_const_expr_p=*/false,
27465 LOOKUP_ONLYCONVERTING
);
27469 pop_scope (pushed_scope
);
27475 /* If parsing a type specifier sequence failed, then
27476 this MUST be a simple expression. */
27477 cp_parser_parse_tentatively (parser
);
27478 decl
= cp_parser_primary_expression (parser
, false, false,
27480 if (!cp_parser_error_occurred (parser
)
27483 && CLASS_TYPE_P (TREE_TYPE (decl
)))
27487 cp_parser_parse_definitely (parser
);
27488 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
27489 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
27490 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
27493 tf_warning_or_error
));
27494 add_private_clause
= true;
27499 cp_parser_abort_tentative_parse (parser
);
27500 init
= cp_parser_expression (parser
, false, NULL
);
27503 if (TREE_CODE (init
) == MODIFY_EXPR
27504 || TREE_CODE (init
) == MODOP_EXPR
)
27505 real_decl
= TREE_OPERAND (init
, 0);
27510 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27513 this_pre_body
= pop_stmt_list (this_pre_body
);
27517 pre_body
= push_stmt_list ();
27519 add_stmt (this_pre_body
);
27520 pre_body
= pop_stmt_list (pre_body
);
27523 pre_body
= this_pre_body
;
27528 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
27531 for (c
= par_clauses
; *c
; )
27532 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
27533 && OMP_CLAUSE_DECL (*c
) == real_decl
)
27535 error_at (loc
, "iteration variable %qD"
27536 " should not be firstprivate", real_decl
);
27537 *c
= OMP_CLAUSE_CHAIN (*c
);
27539 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
27540 && OMP_CLAUSE_DECL (*c
) == real_decl
)
27542 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27543 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27544 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
27545 OMP_CLAUSE_DECL (l
) = real_decl
;
27546 OMP_CLAUSE_CHAIN (l
) = clauses
;
27547 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
27549 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
27550 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
27551 add_private_clause
= false;
27555 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
27556 && OMP_CLAUSE_DECL (*c
) == real_decl
)
27557 add_private_clause
= false;
27558 c
= &OMP_CLAUSE_CHAIN (*c
);
27562 if (add_private_clause
)
27565 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27567 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
27568 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
27569 && OMP_CLAUSE_DECL (c
) == decl
)
27571 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
27572 && OMP_CLAUSE_DECL (c
) == decl
)
27573 error_at (loc
, "iteration variable %qD "
27574 "should not be firstprivate",
27576 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
27577 && OMP_CLAUSE_DECL (c
) == decl
)
27578 error_at (loc
, "iteration variable %qD should not be reduction",
27583 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
27584 OMP_CLAUSE_DECL (c
) = decl
;
27585 c
= finish_omp_clauses (c
);
27588 OMP_CLAUSE_CHAIN (c
) = clauses
;
27595 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27596 cond
= cp_parser_omp_for_cond (parser
, decl
);
27597 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27600 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
27602 /* If decl is an iterator, preserve the operator on decl
27603 until finish_omp_for. */
27605 && ((processing_template_decl
27606 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
27607 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
27608 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
27610 incr
= cp_parser_expression (parser
, false, NULL
);
27611 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
27612 SET_EXPR_LOCATION (incr
, input_location
);
27615 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27616 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27617 /*or_comma=*/false,
27618 /*consume_paren=*/true);
27620 TREE_VEC_ELT (declv
, i
) = decl
;
27621 TREE_VEC_ELT (initv
, i
) = init
;
27622 TREE_VEC_ELT (condv
, i
) = cond
;
27623 TREE_VEC_ELT (incrv
, i
) = incr
;
27625 if (i
== collapse
- 1)
27628 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27629 in between the collapsed for loops to be still considered perfectly
27630 nested. Hopefully the final version clarifies this.
27631 For now handle (multiple) {'s and empty statements. */
27632 cp_parser_parse_tentatively (parser
);
27635 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27637 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27639 cp_lexer_consume_token (parser
->lexer
);
27642 else if (bracecount
27643 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27644 cp_lexer_consume_token (parser
->lexer
);
27647 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27648 error_at (loc
, "not enough collapsed for loops");
27649 collapse_err
= true;
27650 cp_parser_abort_tentative_parse (parser
);
27659 cp_parser_parse_definitely (parser
);
27660 nbraces
+= bracecount
;
27664 /* Note that we saved the original contents of this flag when we entered
27665 the structured block, and so we don't need to re-save it here. */
27666 parser
->in_statement
= IN_OMP_FOR
;
27668 /* Note that the grammar doesn't call for a structured block here,
27669 though the loop as a whole is a structured block. */
27670 body
= push_stmt_list ();
27671 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
27672 body
= pop_stmt_list (body
);
27674 if (declv
== NULL_TREE
)
27677 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
27678 pre_body
, clauses
);
27682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
27684 cp_lexer_consume_token (parser
->lexer
);
27687 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27688 cp_lexer_consume_token (parser
->lexer
);
27693 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27694 "collapsed loops not perfectly nested");
27696 collapse_err
= true;
27697 cp_parser_statement_seq_opt (parser
, NULL
);
27698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27703 while (!for_block
->is_empty ())
27704 add_stmt (pop_stmt_list (for_block
->pop ()));
27705 release_tree_vector (for_block
);
27711 #pragma omp for for-clause[optseq] new-line
27714 #define OMP_FOR_CLAUSE_MASK \
27715 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27716 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27717 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27718 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27719 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27720 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27721 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27722 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27725 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
27727 tree clauses
, sb
, ret
;
27730 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
27731 "#pragma omp for", pragma_tok
);
27733 sb
= begin_omp_structured_block ();
27734 save
= cp_parser_begin_omp_structured_block (parser
);
27736 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
27738 cp_parser_end_omp_structured_block (parser
, save
);
27739 add_stmt (finish_omp_structured_block (sb
));
27745 # pragma omp master new-line
27746 structured-block */
27749 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
27751 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27752 return c_finish_omp_master (input_location
,
27753 cp_parser_omp_structured_block (parser
));
27757 # pragma omp ordered new-line
27758 structured-block */
27761 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
27763 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27764 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27765 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
27771 { section-sequence }
27774 section-directive[opt] structured-block
27775 section-sequence section-directive structured-block */
27778 cp_parser_omp_sections_scope (cp_parser
*parser
)
27780 tree stmt
, substmt
;
27781 bool error_suppress
= false;
27784 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
27787 stmt
= push_stmt_list ();
27789 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
27793 substmt
= begin_omp_structured_block ();
27794 save
= cp_parser_begin_omp_structured_block (parser
);
27798 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
27800 tok
= cp_lexer_peek_token (parser
->lexer
);
27801 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
27803 if (tok
->type
== CPP_CLOSE_BRACE
)
27805 if (tok
->type
== CPP_EOF
)
27809 cp_parser_end_omp_structured_block (parser
, save
);
27810 substmt
= finish_omp_structured_block (substmt
);
27811 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
27812 add_stmt (substmt
);
27817 tok
= cp_lexer_peek_token (parser
->lexer
);
27818 if (tok
->type
== CPP_CLOSE_BRACE
)
27820 if (tok
->type
== CPP_EOF
)
27823 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
27825 cp_lexer_consume_token (parser
->lexer
);
27826 cp_parser_require_pragma_eol (parser
, tok
);
27827 error_suppress
= false;
27829 else if (!error_suppress
)
27831 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
27832 error_suppress
= true;
27835 substmt
= cp_parser_omp_structured_block (parser
);
27836 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
27837 add_stmt (substmt
);
27839 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
27841 substmt
= pop_stmt_list (stmt
);
27843 stmt
= make_node (OMP_SECTIONS
);
27844 TREE_TYPE (stmt
) = void_type_node
;
27845 OMP_SECTIONS_BODY (stmt
) = substmt
;
27852 # pragma omp sections sections-clause[optseq] newline
27855 #define OMP_SECTIONS_CLAUSE_MASK \
27856 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27857 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27858 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27859 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27860 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27863 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
27867 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
27868 "#pragma omp sections", pragma_tok
);
27870 ret
= cp_parser_omp_sections_scope (parser
);
27872 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
27878 # pragma parallel parallel-clause new-line
27879 # pragma parallel for parallel-for-clause new-line
27880 # pragma parallel sections parallel-sections-clause new-line */
27882 #define OMP_PARALLEL_CLAUSE_MASK \
27883 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27884 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27885 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27886 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27887 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27888 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27889 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27890 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27893 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
27895 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
27896 const char *p_name
= "#pragma omp parallel";
27897 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
27898 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
27900 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27902 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27904 cp_lexer_consume_token (parser
->lexer
);
27905 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
27906 p_name
= "#pragma omp parallel for";
27907 mask
|= OMP_FOR_CLAUSE_MASK
;
27908 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
27910 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27912 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27913 const char *p
= IDENTIFIER_POINTER (id
);
27914 if (strcmp (p
, "sections") == 0)
27916 cp_lexer_consume_token (parser
->lexer
);
27917 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
27918 p_name
= "#pragma omp parallel sections";
27919 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
27920 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
27924 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
27925 block
= begin_omp_parallel ();
27926 save
= cp_parser_begin_omp_structured_block (parser
);
27930 case PRAGMA_OMP_PARALLEL
:
27931 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
27932 par_clause
= clauses
;
27935 case PRAGMA_OMP_PARALLEL_FOR
:
27936 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
27937 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
27940 case PRAGMA_OMP_PARALLEL_SECTIONS
:
27941 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
27942 stmt
= cp_parser_omp_sections_scope (parser
);
27944 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
27948 gcc_unreachable ();
27951 cp_parser_end_omp_structured_block (parser
, save
);
27952 stmt
= finish_omp_parallel (par_clause
, block
);
27953 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
27954 OMP_PARALLEL_COMBINED (stmt
) = 1;
27959 # pragma omp single single-clause[optseq] new-line
27960 structured-block */
27962 #define OMP_SINGLE_CLAUSE_MASK \
27963 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27964 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27965 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27966 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27969 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
27971 tree stmt
= make_node (OMP_SINGLE
);
27972 TREE_TYPE (stmt
) = void_type_node
;
27974 OMP_SINGLE_CLAUSES (stmt
)
27975 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
27976 "#pragma omp single", pragma_tok
);
27977 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
27979 return add_stmt (stmt
);
27983 # pragma omp task task-clause[optseq] new-line
27984 structured-block */
27986 #define OMP_TASK_CLAUSE_MASK \
27987 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27988 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27989 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27990 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27991 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27992 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27993 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27994 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27997 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
27999 tree clauses
, block
;
28002 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
28003 "#pragma omp task", pragma_tok
);
28004 block
= begin_omp_task ();
28005 save
= cp_parser_begin_omp_structured_block (parser
);
28006 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
28007 cp_parser_end_omp_structured_block (parser
, save
);
28008 return finish_omp_task (clauses
, block
);
28012 # pragma omp taskwait new-line */
28015 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
28017 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28018 finish_omp_taskwait ();
28022 # pragma omp taskyield new-line */
28025 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
28027 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28028 finish_omp_taskyield ();
28032 # pragma omp threadprivate (variable-list) */
28035 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
28039 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
28040 cp_parser_require_pragma_eol (parser
, pragma_tok
);
28042 finish_omp_threadprivate (vars
);
28045 /* Main entry point to OpenMP statement pragmas. */
28048 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
28052 switch (pragma_tok
->pragma_kind
)
28054 case PRAGMA_OMP_ATOMIC
:
28055 cp_parser_omp_atomic (parser
, pragma_tok
);
28057 case PRAGMA_OMP_CRITICAL
:
28058 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
28060 case PRAGMA_OMP_FOR
:
28061 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
28063 case PRAGMA_OMP_MASTER
:
28064 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
28066 case PRAGMA_OMP_ORDERED
:
28067 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
28069 case PRAGMA_OMP_PARALLEL
:
28070 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
28072 case PRAGMA_OMP_SECTIONS
:
28073 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
28075 case PRAGMA_OMP_SINGLE
:
28076 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
28078 case PRAGMA_OMP_TASK
:
28079 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
28082 gcc_unreachable ();
28086 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
28089 /* Transactional Memory parsing routines. */
28091 /* Parse a transaction attribute.
28097 ??? Simplify this when C++0x bracket attributes are
28098 implemented properly. */
28101 cp_parser_txn_attribute_opt (cp_parser
*parser
)
28104 tree attr_name
, attr
= NULL
;
28106 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
28107 return cp_parser_attributes_opt (parser
);
28109 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
28111 cp_lexer_consume_token (parser
->lexer
);
28112 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
28115 token
= cp_lexer_peek_token (parser
->lexer
);
28116 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
28118 token
= cp_lexer_consume_token (parser
->lexer
);
28120 attr_name
= (token
->type
== CPP_KEYWORD
28121 /* For keywords, use the canonical spelling,
28122 not the parsed identifier. */
28123 ? ridpointers
[(int) token
->keyword
]
28125 attr
= build_tree_list (attr_name
, NULL_TREE
);
28128 cp_parser_error (parser
, "expected identifier");
28130 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
28132 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
28136 /* Parse a __transaction_atomic or __transaction_relaxed statement.
28138 transaction-statement:
28139 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
28141 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
28145 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
28147 unsigned char old_in
= parser
->in_transaction
;
28148 unsigned char this_in
= 1, new_in
;
28150 tree stmt
, attrs
, noex
;
28152 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
28153 || keyword
== RID_TRANSACTION_RELAXED
);
28154 token
= cp_parser_require_keyword (parser
, keyword
,
28155 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
28156 : RT_TRANSACTION_RELAXED
));
28157 gcc_assert (token
!= NULL
);
28159 if (keyword
== RID_TRANSACTION_RELAXED
)
28160 this_in
|= TM_STMT_ATTR_RELAXED
;
28163 attrs
= cp_parser_txn_attribute_opt (parser
);
28165 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
28168 /* Parse a noexcept specification. */
28169 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
28171 /* Keep track if we're in the lexical scope of an outer transaction. */
28172 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
28174 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
28176 parser
->in_transaction
= new_in
;
28177 cp_parser_compound_statement (parser
, NULL
, false, false);
28178 parser
->in_transaction
= old_in
;
28180 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
28185 /* Parse a __transaction_atomic or __transaction_relaxed expression.
28187 transaction-expression:
28188 __transaction_atomic txn-noexcept-spec[opt] ( expression )
28189 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
28193 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
28195 unsigned char old_in
= parser
->in_transaction
;
28196 unsigned char this_in
= 1;
28201 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
28202 || keyword
== RID_TRANSACTION_RELAXED
);
28205 error (keyword
== RID_TRANSACTION_RELAXED
28206 ? G_("%<__transaction_relaxed%> without transactional memory "
28208 : G_("%<__transaction_atomic%> without transactional memory "
28209 "support enabled"));
28211 token
= cp_parser_require_keyword (parser
, keyword
,
28212 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
28213 : RT_TRANSACTION_RELAXED
));
28214 gcc_assert (token
!= NULL
);
28216 if (keyword
== RID_TRANSACTION_RELAXED
)
28217 this_in
|= TM_STMT_ATTR_RELAXED
;
28219 /* Set this early. This might mean that we allow transaction_cancel in
28220 an expression that we find out later actually has to be a constexpr.
28221 However, we expect that cxx_constant_value will be able to deal with
28222 this; also, if the noexcept has no constexpr, then what we parse next
28223 really is a transaction's body. */
28224 parser
->in_transaction
= this_in
;
28226 /* Parse a noexcept specification. */
28227 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
28230 if (!noex
|| !noex_expr
28231 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
28233 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28235 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
28236 expr
= finish_parenthesized_expr (expr
);
28238 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28242 /* The only expression that is available got parsed for the noexcept
28243 already. noexcept is true then. */
28245 noex
= boolean_true_node
;
28248 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
28249 parser
->in_transaction
= old_in
;
28251 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
28252 return error_mark_node
;
28254 return (flag_tm
? expr
: error_mark_node
);
28257 /* Parse a function-transaction-block.
28259 function-transaction-block:
28260 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
28262 __transaction_atomic txn-attribute[opt] function-try-block
28263 __transaction_relaxed ctor-initializer[opt] function-body
28264 __transaction_relaxed function-try-block
28268 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
28270 unsigned char old_in
= parser
->in_transaction
;
28271 unsigned char new_in
= 1;
28272 tree compound_stmt
, stmt
, attrs
;
28273 bool ctor_initializer_p
;
28276 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
28277 || keyword
== RID_TRANSACTION_RELAXED
);
28278 token
= cp_parser_require_keyword (parser
, keyword
,
28279 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
28280 : RT_TRANSACTION_RELAXED
));
28281 gcc_assert (token
!= NULL
);
28283 if (keyword
== RID_TRANSACTION_RELAXED
)
28284 new_in
|= TM_STMT_ATTR_RELAXED
;
28287 attrs
= cp_parser_txn_attribute_opt (parser
);
28289 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
28292 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
28294 parser
->in_transaction
= new_in
;
28296 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28297 ctor_initializer_p
= cp_parser_function_try_block (parser
);
28299 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
28300 (parser
, /*in_function_try_block=*/false);
28302 parser
->in_transaction
= old_in
;
28304 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
28306 return ctor_initializer_p
;
28309 /* Parse a __transaction_cancel statement.
28312 __transaction_cancel txn-attribute[opt] ;
28313 __transaction_cancel txn-attribute[opt] throw-expression ;
28315 ??? Cancel and throw is not yet implemented. */
28318 cp_parser_transaction_cancel (cp_parser
*parser
)
28321 bool is_outer
= false;
28324 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
28325 RT_TRANSACTION_CANCEL
);
28326 gcc_assert (token
!= NULL
);
28328 attrs
= cp_parser_txn_attribute_opt (parser
);
28330 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
28332 /* ??? Parse cancel-and-throw here. */
28334 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
28338 error_at (token
->location
, "%<__transaction_cancel%> without "
28339 "transactional memory support enabled");
28340 return error_mark_node
;
28342 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
28344 error_at (token
->location
, "%<__transaction_cancel%> within a "
28345 "%<__transaction_relaxed%>");
28346 return error_mark_node
;
28350 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
28351 && !is_tm_may_cancel_outer (current_function_decl
))
28353 error_at (token
->location
, "outer %<__transaction_cancel%> not "
28354 "within outer %<__transaction_atomic%>");
28355 error_at (token
->location
,
28356 " or a %<transaction_may_cancel_outer%> function");
28357 return error_mark_node
;
28360 else if (parser
->in_transaction
== 0)
28362 error_at (token
->location
, "%<__transaction_cancel%> not within "
28363 "%<__transaction_atomic%>");
28364 return error_mark_node
;
28367 stmt
= build_tm_abort_call (token
->location
, is_outer
);
28376 static GTY (()) cp_parser
*the_parser
;
28379 /* Special handling for the first token or line in the file. The first
28380 thing in the file might be #pragma GCC pch_preprocess, which loads a
28381 PCH file, which is a GC collection point. So we need to handle this
28382 first pragma without benefit of an existing lexer structure.
28384 Always returns one token to the caller in *FIRST_TOKEN. This is
28385 either the true first token of the file, or the first token after
28386 the initial pragma. */
28389 cp_parser_initial_pragma (cp_token
*first_token
)
28393 cp_lexer_get_preprocessor_token (NULL
, first_token
);
28394 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
28397 cp_lexer_get_preprocessor_token (NULL
, first_token
);
28398 if (first_token
->type
== CPP_STRING
)
28400 name
= first_token
->u
.value
;
28402 cp_lexer_get_preprocessor_token (NULL
, first_token
);
28403 if (first_token
->type
!= CPP_PRAGMA_EOL
)
28404 error_at (first_token
->location
,
28405 "junk at end of %<#pragma GCC pch_preprocess%>");
28408 error_at (first_token
->location
, "expected string literal");
28410 /* Skip to the end of the pragma. */
28411 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
28412 cp_lexer_get_preprocessor_token (NULL
, first_token
);
28414 /* Now actually load the PCH file. */
28416 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
28418 /* Read one more token to return to our caller. We have to do this
28419 after reading the PCH file in, since its pointers have to be
28421 cp_lexer_get_preprocessor_token (NULL
, first_token
);
28424 /* Normal parsing of a pragma token. Here we can (and must) use the
28428 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
28430 cp_token
*pragma_tok
;
28433 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
28434 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
28435 parser
->lexer
->in_pragma
= true;
28437 id
= pragma_tok
->pragma_kind
;
28440 case PRAGMA_GCC_PCH_PREPROCESS
:
28441 error_at (pragma_tok
->location
,
28442 "%<#pragma GCC pch_preprocess%> must be first");
28445 case PRAGMA_OMP_BARRIER
:
28448 case pragma_compound
:
28449 cp_parser_omp_barrier (parser
, pragma_tok
);
28452 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
28453 "used in compound statements");
28460 case PRAGMA_OMP_FLUSH
:
28463 case pragma_compound
:
28464 cp_parser_omp_flush (parser
, pragma_tok
);
28467 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
28468 "used in compound statements");
28475 case PRAGMA_OMP_TASKWAIT
:
28478 case pragma_compound
:
28479 cp_parser_omp_taskwait (parser
, pragma_tok
);
28482 error_at (pragma_tok
->location
,
28483 "%<#pragma omp taskwait%> may only be "
28484 "used in compound statements");
28491 case PRAGMA_OMP_TASKYIELD
:
28494 case pragma_compound
:
28495 cp_parser_omp_taskyield (parser
, pragma_tok
);
28498 error_at (pragma_tok
->location
,
28499 "%<#pragma omp taskyield%> may only be "
28500 "used in compound statements");
28507 case PRAGMA_OMP_THREADPRIVATE
:
28508 cp_parser_omp_threadprivate (parser
, pragma_tok
);
28511 case PRAGMA_OMP_ATOMIC
:
28512 case PRAGMA_OMP_CRITICAL
:
28513 case PRAGMA_OMP_FOR
:
28514 case PRAGMA_OMP_MASTER
:
28515 case PRAGMA_OMP_ORDERED
:
28516 case PRAGMA_OMP_PARALLEL
:
28517 case PRAGMA_OMP_SECTIONS
:
28518 case PRAGMA_OMP_SINGLE
:
28519 case PRAGMA_OMP_TASK
:
28520 if (context
== pragma_external
)
28522 cp_parser_omp_construct (parser
, pragma_tok
);
28525 case PRAGMA_OMP_SECTION
:
28526 error_at (pragma_tok
->location
,
28527 "%<#pragma omp section%> may only be used in "
28528 "%<#pragma omp sections%> construct");
28532 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
28533 c_invoke_pragma_handler (id
);
28537 cp_parser_error (parser
, "expected declaration specifiers");
28541 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
28545 /* The interface the pragma parsers have to the lexer. */
28548 pragma_lex (tree
*value
)
28551 enum cpp_ttype ret
;
28553 tok
= cp_lexer_peek_token (the_parser
->lexer
);
28556 *value
= tok
->u
.value
;
28558 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
28560 else if (ret
== CPP_STRING
)
28561 *value
= cp_parser_string_literal (the_parser
, false, false);
28564 cp_lexer_consume_token (the_parser
->lexer
);
28565 if (ret
== CPP_KEYWORD
)
28573 /* External interface. */
28575 /* Parse one entire translation unit. */
28578 c_parse_file (void)
28580 static bool already_called
= false;
28582 if (already_called
)
28584 sorry ("inter-module optimizations not implemented for C++");
28587 already_called
= true;
28589 the_parser
= cp_parser_new ();
28590 push_deferring_access_checks (flag_access_control
28591 ? dk_no_deferred
: dk_no_check
);
28592 cp_parser_translation_unit (the_parser
);
28596 #include "gt-cp-parser.h"