There can be only one ref qualifier at most.
[official-gcc.git] / gcc / cp / parser.c
blobed8eac8cc4e007a57349ac3443c340c33065877f
1 /* C++ Parser.
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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "target.h"
35 #include "cgraph.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "plugin.h"
39 #include "tree-pretty-print.h"
40 #include "parser.h"
43 /* The lexer. */
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 {
55 NIC_NONE,
56 /* floating-point literal */
57 NIC_FLOAT,
58 /* %<this%> */
59 NIC_THIS,
60 /* %<__FUNCTION__%> */
61 NIC_FUNC_NAME,
62 /* %<__PRETTY_FUNCTION__%> */
63 NIC_PRETTY_FUNC,
64 /* %<__func__%> */
65 NIC_C99_FUNC,
66 /* "%<va_arg%> */
67 NIC_VA_ARG,
68 /* a cast */
69 NIC_CAST,
70 /* %<typeid%> operator */
71 NIC_TYPEID,
72 /* non-constant compound literals */
73 NIC_NCC,
74 /* a function call */
75 NIC_FUNC_CALL,
76 /* an increment */
77 NIC_INC,
78 /* an decrement */
79 NIC_DEC,
80 /* an array reference */
81 NIC_ARRAY_REF,
82 /* %<->%> */
83 NIC_ARROW,
84 /* %<.%> */
85 NIC_POINT,
86 /* the address of a label */
87 NIC_ADDR_LABEL,
88 /* %<*%> */
89 NIC_STAR,
90 /* %<&%> */
91 NIC_ADDR,
92 /* %<++%> */
93 NIC_PREINCREMENT,
94 /* %<--%> */
95 NIC_PREDECREMENT,
96 /* %<new%> */
97 NIC_NEW,
98 /* %<delete%> */
99 NIC_DEL,
100 /* calls to overloaded operators */
101 NIC_OVERLOADED,
102 /* an assignment */
103 NIC_ASSIGNMENT,
104 /* a comma operator */
105 NIC_COMMA,
106 /* a call to a constructor */
107 NIC_CONSTRUCTOR,
108 /* a transaction expression */
109 NIC_TRANSACTION
110 } non_integral_constant;
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114 /* NULL */
115 NLE_NULL,
116 /* is not a type */
117 NLE_TYPE,
118 /* is not a class or namespace */
119 NLE_CXX98,
120 /* is not a class, namespace, or enumeration */
121 NLE_NOT_CXX98
122 } name_lookup_error;
124 /* The various kinds of required token */
125 typedef enum required_token {
126 RT_NONE,
127 RT_SEMICOLON, /* ';' */
128 RT_OPEN_PAREN, /* '(' */
129 RT_CLOSE_BRACE, /* '}' */
130 RT_OPEN_BRACE, /* '{' */
131 RT_CLOSE_SQUARE, /* ']' */
132 RT_OPEN_SQUARE, /* '[' */
133 RT_COMMA, /* ',' */
134 RT_SCOPE, /* '::' */
135 RT_LESS, /* '<' */
136 RT_GREATER, /* '>' */
137 RT_EQ, /* '=' */
138 RT_ELLIPSIS, /* '...' */
139 RT_MULT, /* '*' */
140 RT_COMPL, /* '~' */
141 RT_COLON, /* ':' */
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 */
149 RT_NEW, /* new */
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 */
161 RT_ASM, /* asm */
162 RT_TRY, /* try */
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 */
178 } required_token;
180 /* Prototypes. */
182 static cp_lexer *cp_lexer_new_main
183 (void);
184 static cp_lexer *cp_lexer_new_from_tokens
185 (cp_token_cache *tokens);
186 static void cp_lexer_destroy
187 (cp_lexer *);
188 static int cp_lexer_saving_tokens
189 (const cp_lexer *);
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
195 (cp_lexer *);
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
205 (cp_lexer *);
206 static void cp_lexer_purge_token
207 (cp_lexer *);
208 static void cp_lexer_purge_tokens_after
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_save_tokens
211 (cp_lexer *);
212 static void cp_lexer_commit_tokens
213 (cp_lexer *);
214 static void cp_lexer_rollback_tokens
215 (cp_lexer *);
216 static void cp_lexer_print_token
217 (FILE *, cp_token *);
218 static inline bool cp_lexer_debugging_p
219 (cp_lexer *);
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
229 (cp_token *);
231 static tree cp_literal_operator_id
232 (const char *);
234 /* Manifest constants. */
235 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
236 #define CP_SAVED_TOKEN_STACK 5
238 /* Variables. */
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 [[ ]]. */
253 static void
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;
259 cp_token *token;
260 bool do_print;
262 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
264 if (buffer == NULL)
265 return;
267 if (num == 0)
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, " ... ");
279 do_print = false;
280 nprinted = 0;
281 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
283 if (token == start_token)
284 do_print = true;
286 if (!do_print)
287 continue;
289 nprinted++;
290 if (token == curr_token)
291 fprintf (file, "[[");
293 cp_lexer_print_token (file, token);
295 if (token == curr_token)
296 fprintf (file, "]]");
298 switch (token->type)
300 case CPP_SEMICOLON:
301 case CPP_OPEN_BRACE:
302 case CPP_CLOSE_BRACE:
303 case CPP_EOF:
304 fputc ('\n', file);
305 break;
307 default:
308 fputc (' ', file);
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. */
324 void
325 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
327 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
330 DEBUG_FUNCTION void
331 debug (vec<cp_token, va_gc> &ref)
333 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
336 DEBUG_FUNCTION void
337 debug (vec<cp_token, va_gc> *ptr)
339 if (ptr)
340 debug (*ptr);
341 else
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. */
349 static void
350 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
352 if (t)
354 fprintf (file, "%s: ", desc);
355 print_node_brief (file, "", t, 0);
360 /* Dump parser context C to FILE. */
362 static void
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. */
374 static void
375 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
377 unsigned i;
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. */
391 static void
392 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
394 if (flag)
395 fprintf (file, "%s: true\n", desc);
399 /* Print an unparsed function entry UF to FILE. */
401 static void
402 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
404 unsigned i;
405 cp_default_arg_entry *default_arg_fn;
406 tree fn;
408 fprintf (file, "\tFunctions with default args:\n");
409 for (i = 0;
410 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
411 i++)
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);
425 fprintf (file, " ");
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);
434 fprintf (file, " ");
436 fprintf (file, "\n");
440 /* Print the stack of unparsed member functions S to FILE. */
442 static void
443 cp_debug_print_unparsed_queues (FILE *file,
444 vec<cp_unparsed_functions_entry, va_gc> *s)
446 unsigned i;
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. */
461 static void
462 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
464 cp_token *next_token, *first_token, *start_token;
466 if (file == NULL)
467 file = stderr;
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
473 : first_token;
474 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
475 next_token);
479 /* Dump debugging information for the given PARSER. If FILE is NULL,
480 the output is printed on stderr. */
482 void
483 cp_debug_parser (FILE *file, cp_parser *parser)
485 const size_t window_size = 20;
486 cp_token *token;
487 expanded_location eloc;
489 if (file == NULL)
490 file = stderr;
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 "
515 "current context",
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);
563 DEBUG_FUNCTION void
564 debug (cp_parser &ref)
566 cp_debug_parser (stderr, &ref);
569 DEBUG_FUNCTION void
570 debug (cp_parser *ptr)
572 if (ptr)
573 debug (*ptr);
574 else
575 fprintf (stderr, "<nil>\n");
578 /* Allocate memory for a new lexer object and return it. */
580 static cp_lexer *
581 cp_lexer_alloc (void)
583 cp_lexer *lexer;
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);
598 return lexer;
602 /* Create a new main C++ lexer, the lexer that gets tokens from the
603 preprocessor. */
605 static cp_lexer *
606 cp_lexer_new_main (void)
608 cp_lexer *lexer;
609 cp_token token;
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 ()
630 - 1;
631 lexer->next_token = lexer->buffer->length ()
632 ? lexer->buffer->address ()
633 : &eof_token;
635 /* Subsequent preprocessor diagnostics should use compiler
636 diagnostic functions to get the compiler source location. */
637 done_lexing = true;
639 gcc_assert (!lexer->next_token->purged_p);
640 return lexer;
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. */
646 static cp_lexer *
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);
664 return lexer;
667 /* Frees all resources associated with LEXER. */
669 static void
670 cp_lexer_destroy (cp_lexer *lexer)
672 vec_free (lexer->buffer);
673 lexer->saved_tokens.release ();
674 ggc_free (lexer);
677 /* Returns nonzero if debugging information should be output. */
679 static inline bool
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)
697 return pos;
700 static inline void
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;
711 else
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. */
725 static inline int
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. */
736 static void
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. */
742 token->type
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);
766 else
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
773 an identifier). */
774 warning (OPT_Wc__0x_compat,
775 "identifier %qE is a keyword in C++11",
776 token->u.value);
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
799 RID_AT_SYNCHRONIZED.
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. */
821 static inline void
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
831 consume it. */
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. */
847 static inline bool
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. */
855 static inline bool
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. */
863 static inline bool
864 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
866 return cp_lexer_peek_token (lexer)->keyword == keyword;
869 /* Return true if the next token is not the indicated KEYWORD. */
871 static inline bool
872 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
874 return cp_lexer_peek_token (lexer)->keyword != keyword;
877 /* Return true if the next token is a keyword for a decl-specifier. */
879 static bool
880 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
882 cp_token *token;
884 token = cp_lexer_peek_token (lexer);
885 switch (token->keyword)
887 /* auto specifier: storage-class-specifier in C++,
888 simple-type-specifier in C++0x. */
889 case RID_AUTO:
890 /* Storage classes. */
891 case RID_REGISTER:
892 case RID_STATIC:
893 case RID_EXTERN:
894 case RID_MUTABLE:
895 case RID_THREAD:
896 /* Elaborated type specifiers. */
897 case RID_ENUM:
898 case RID_CLASS:
899 case RID_STRUCT:
900 case RID_UNION:
901 case RID_TYPENAME:
902 /* Simple type specifiers. */
903 case RID_CHAR:
904 case RID_CHAR16:
905 case RID_CHAR32:
906 case RID_WCHAR:
907 case RID_BOOL:
908 case RID_SHORT:
909 case RID_INT:
910 case RID_LONG:
911 case RID_INT128:
912 case RID_SIGNED:
913 case RID_UNSIGNED:
914 case RID_FLOAT:
915 case RID_DOUBLE:
916 case RID_VOID:
917 /* GNU extensions. */
918 case RID_ATTRIBUTE:
919 case RID_TYPEOF:
920 /* C++0x extensions. */
921 case RID_DECLTYPE:
922 case RID_UNDERLYING_TYPE:
923 return true;
925 default:
926 return false;
930 /* Returns TRUE iff the token T begins a decltype type. */
932 static bool
933 token_is_decltype (cp_token *t)
935 return (t->keyword == RID_DECLTYPE
936 || t->type == CPP_DECLTYPE);
939 /* Returns TRUE iff the next token begins a decltype type. */
941 static bool
942 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
944 cp_token *t = cp_lexer_peek_token (lexer);
945 return token_is_decltype (t);
948 /* Return a pointer to the Nth token in the token stream. If N is 1,
949 then this is precisely equivalent to cp_lexer_peek_token (except
950 that it is not inline). One would like to disallow that case, but
951 there is one case (cp_parser_nth_token_starts_template_id) where
952 the caller passes a variable for N and it might be 1. */
954 static cp_token *
955 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
957 cp_token *token;
959 /* N is 1-based, not zero-based. */
960 gcc_assert (n > 0);
962 if (cp_lexer_debugging_p (lexer))
963 fprintf (cp_lexer_debug_stream,
964 "cp_lexer: peeking ahead %ld at token: ", (long)n);
966 --n;
967 token = lexer->next_token;
968 gcc_assert (!n || token != &eof_token);
969 while (n != 0)
971 ++token;
972 if (token == lexer->last_token)
974 token = &eof_token;
975 break;
978 if (!token->purged_p)
979 --n;
982 if (cp_lexer_debugging_p (lexer))
984 cp_lexer_print_token (cp_lexer_debug_stream, token);
985 putc ('\n', cp_lexer_debug_stream);
988 return token;
991 /* Return the next token, and advance the lexer's next_token pointer
992 to point to the next non-purged token. */
994 static cp_token *
995 cp_lexer_consume_token (cp_lexer* lexer)
997 cp_token *token = lexer->next_token;
999 gcc_assert (token != &eof_token);
1000 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1004 lexer->next_token++;
1005 if (lexer->next_token == lexer->last_token)
1007 lexer->next_token = &eof_token;
1008 break;
1012 while (lexer->next_token->purged_p);
1014 cp_lexer_set_source_position_from_token (token);
1016 /* Provide debugging output. */
1017 if (cp_lexer_debugging_p (lexer))
1019 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1020 cp_lexer_print_token (cp_lexer_debug_stream, token);
1021 putc ('\n', cp_lexer_debug_stream);
1024 return token;
1027 /* Permanently remove the next token from the token stream, and
1028 advance the next_token pointer to refer to the next non-purged
1029 token. */
1031 static void
1032 cp_lexer_purge_token (cp_lexer *lexer)
1034 cp_token *tok = lexer->next_token;
1036 gcc_assert (tok != &eof_token);
1037 tok->purged_p = true;
1038 tok->location = UNKNOWN_LOCATION;
1039 tok->u.value = NULL_TREE;
1040 tok->keyword = RID_MAX;
1044 tok++;
1045 if (tok == lexer->last_token)
1047 tok = &eof_token;
1048 break;
1051 while (tok->purged_p);
1052 lexer->next_token = tok;
1055 /* Permanently remove all tokens after TOK, up to, but not
1056 including, the token that will be returned next by
1057 cp_lexer_peek_token. */
1059 static void
1060 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1062 cp_token *peek = lexer->next_token;
1064 if (peek == &eof_token)
1065 peek = lexer->last_token;
1067 gcc_assert (tok < peek);
1069 for ( tok += 1; tok != peek; tok += 1)
1071 tok->purged_p = true;
1072 tok->location = UNKNOWN_LOCATION;
1073 tok->u.value = NULL_TREE;
1074 tok->keyword = RID_MAX;
1078 /* Begin saving tokens. All tokens consumed after this point will be
1079 preserved. */
1081 static void
1082 cp_lexer_save_tokens (cp_lexer* lexer)
1084 /* Provide debugging output. */
1085 if (cp_lexer_debugging_p (lexer))
1086 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1088 lexer->saved_tokens.safe_push (lexer->next_token);
1091 /* Commit to the portion of the token stream most recently saved. */
1093 static void
1094 cp_lexer_commit_tokens (cp_lexer* lexer)
1096 /* Provide debugging output. */
1097 if (cp_lexer_debugging_p (lexer))
1098 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1100 lexer->saved_tokens.pop ();
1103 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1104 to the token stream. Stop saving tokens. */
1106 static void
1107 cp_lexer_rollback_tokens (cp_lexer* lexer)
1109 /* Provide debugging output. */
1110 if (cp_lexer_debugging_p (lexer))
1111 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1113 lexer->next_token = lexer->saved_tokens.pop ();
1116 /* Print a representation of the TOKEN on the STREAM. */
1118 static void
1119 cp_lexer_print_token (FILE * stream, cp_token *token)
1121 /* We don't use cpp_type2name here because the parser defines
1122 a few tokens of its own. */
1123 static const char *const token_names[] = {
1124 /* cpplib-defined token types */
1125 #define OP(e, s) #e,
1126 #define TK(e, s) #e,
1127 TTYPE_TABLE
1128 #undef OP
1129 #undef TK
1130 /* C++ parser token types - see "Manifest constants", above. */
1131 "KEYWORD",
1132 "TEMPLATE_ID",
1133 "NESTED_NAME_SPECIFIER",
1136 /* For some tokens, print the associated data. */
1137 switch (token->type)
1139 case CPP_KEYWORD:
1140 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1141 For example, `struct' is mapped to an INTEGER_CST. */
1142 if (!identifier_p (token->u.value))
1143 break;
1144 /* else fall through */
1145 case CPP_NAME:
1146 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1147 break;
1149 case CPP_STRING:
1150 case CPP_STRING16:
1151 case CPP_STRING32:
1152 case CPP_WSTRING:
1153 case CPP_UTF8STRING:
1154 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1155 break;
1157 case CPP_NUMBER:
1158 print_generic_expr (stream, token->u.value, 0);
1159 break;
1161 default:
1162 /* If we have a name for the token, print it out. Otherwise, we
1163 simply give the numeric code. */
1164 if (token->type < ARRAY_SIZE(token_names))
1165 fputs (token_names[token->type], stream);
1166 else
1167 fprintf (stream, "[%d]", token->type);
1168 break;
1172 DEBUG_FUNCTION void
1173 debug (cp_token &ref)
1175 cp_lexer_print_token (stderr, &ref);
1176 fprintf (stderr, "\n");
1179 DEBUG_FUNCTION void
1180 debug (cp_token *ptr)
1182 if (ptr)
1183 debug (*ptr);
1184 else
1185 fprintf (stderr, "<nil>\n");
1189 /* Start emitting debugging information. */
1191 static void
1192 cp_lexer_start_debugging (cp_lexer* lexer)
1194 lexer->debugging_p = true;
1195 cp_lexer_debug_stream = stderr;
1198 /* Stop emitting debugging information. */
1200 static void
1201 cp_lexer_stop_debugging (cp_lexer* lexer)
1203 lexer->debugging_p = false;
1204 cp_lexer_debug_stream = NULL;
1207 /* Create a new cp_token_cache, representing a range of tokens. */
1209 static cp_token_cache *
1210 cp_token_cache_new (cp_token *first, cp_token *last)
1212 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1213 cache->first = first;
1214 cache->last = last;
1215 return cache;
1219 /* Decl-specifiers. */
1221 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1223 static void
1224 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1226 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1229 /* Declarators. */
1231 /* Nothing other than the parser should be creating declarators;
1232 declarators are a semi-syntactic representation of C++ entities.
1233 Other parts of the front end that need to create entities (like
1234 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1236 static cp_declarator *make_call_declarator
1237 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1238 static cp_declarator *make_array_declarator
1239 (cp_declarator *, tree);
1240 static cp_declarator *make_pointer_declarator
1241 (cp_cv_quals, cp_declarator *, tree);
1242 static cp_declarator *make_reference_declarator
1243 (cp_cv_quals, cp_declarator *, bool, tree);
1244 static cp_parameter_declarator *make_parameter_declarator
1245 (cp_decl_specifier_seq *, cp_declarator *, tree);
1246 static cp_declarator *make_ptrmem_declarator
1247 (cp_cv_quals, tree, cp_declarator *, tree);
1249 /* An erroneous declarator. */
1250 static cp_declarator *cp_error_declarator;
1252 /* The obstack on which declarators and related data structures are
1253 allocated. */
1254 static struct obstack declarator_obstack;
1256 /* Alloc BYTES from the declarator memory pool. */
1258 static inline void *
1259 alloc_declarator (size_t bytes)
1261 return obstack_alloc (&declarator_obstack, bytes);
1264 /* Allocate a declarator of the indicated KIND. Clear fields that are
1265 common to all declarators. */
1267 static cp_declarator *
1268 make_declarator (cp_declarator_kind kind)
1270 cp_declarator *declarator;
1272 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1273 declarator->kind = kind;
1274 declarator->attributes = NULL_TREE;
1275 declarator->std_attributes = NULL_TREE;
1276 declarator->declarator = NULL;
1277 declarator->parameter_pack_p = false;
1278 declarator->id_loc = UNKNOWN_LOCATION;
1280 return declarator;
1283 /* Make a declarator for a generalized identifier. If
1284 QUALIFYING_SCOPE is non-NULL, the identifier is
1285 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1286 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1287 is, if any. */
1289 static cp_declarator *
1290 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1291 special_function_kind sfk)
1293 cp_declarator *declarator;
1295 /* It is valid to write:
1297 class C { void f(); };
1298 typedef C D;
1299 void D::f();
1301 The standard is not clear about whether `typedef const C D' is
1302 legal; as of 2002-09-15 the committee is considering that
1303 question. EDG 3.0 allows that syntax. Therefore, we do as
1304 well. */
1305 if (qualifying_scope && TYPE_P (qualifying_scope))
1306 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1308 gcc_assert (identifier_p (unqualified_name)
1309 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1310 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1312 declarator = make_declarator (cdk_id);
1313 declarator->u.id.qualifying_scope = qualifying_scope;
1314 declarator->u.id.unqualified_name = unqualified_name;
1315 declarator->u.id.sfk = sfk;
1317 return declarator;
1320 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1321 of modifiers such as const or volatile to apply to the pointer
1322 type, represented as identifiers. ATTRIBUTES represent the attributes that
1323 appertain to the pointer or reference. */
1325 cp_declarator *
1326 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1327 tree attributes)
1329 cp_declarator *declarator;
1331 declarator = make_declarator (cdk_pointer);
1332 declarator->declarator = target;
1333 declarator->u.pointer.qualifiers = cv_qualifiers;
1334 declarator->u.pointer.class_type = NULL_TREE;
1335 if (target)
1337 declarator->id_loc = target->id_loc;
1338 declarator->parameter_pack_p = target->parameter_pack_p;
1339 target->parameter_pack_p = false;
1341 else
1342 declarator->parameter_pack_p = false;
1344 declarator->std_attributes = attributes;
1346 return declarator;
1349 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1350 represent the attributes that appertain to the pointer or
1351 reference. */
1353 cp_declarator *
1354 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1355 bool rvalue_ref, tree attributes)
1357 cp_declarator *declarator;
1359 declarator = make_declarator (cdk_reference);
1360 declarator->declarator = target;
1361 declarator->u.reference.qualifiers = cv_qualifiers;
1362 declarator->u.reference.rvalue_ref = rvalue_ref;
1363 if (target)
1365 declarator->id_loc = target->id_loc;
1366 declarator->parameter_pack_p = target->parameter_pack_p;
1367 target->parameter_pack_p = false;
1369 else
1370 declarator->parameter_pack_p = false;
1372 declarator->std_attributes = attributes;
1374 return declarator;
1377 /* Like make_pointer_declarator -- but for a pointer to a non-static
1378 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1379 appertain to the pointer or reference. */
1381 cp_declarator *
1382 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1383 cp_declarator *pointee,
1384 tree attributes)
1386 cp_declarator *declarator;
1388 declarator = make_declarator (cdk_ptrmem);
1389 declarator->declarator = pointee;
1390 declarator->u.pointer.qualifiers = cv_qualifiers;
1391 declarator->u.pointer.class_type = class_type;
1393 if (pointee)
1395 declarator->parameter_pack_p = pointee->parameter_pack_p;
1396 pointee->parameter_pack_p = false;
1398 else
1399 declarator->parameter_pack_p = false;
1401 declarator->std_attributes = attributes;
1403 return declarator;
1406 /* Make a declarator for the function given by TARGET, with the
1407 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1408 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1409 indicates what exceptions can be thrown. */
1411 cp_declarator *
1412 make_call_declarator (cp_declarator *target,
1413 tree parms,
1414 cp_cv_quals cv_qualifiers,
1415 cp_virt_specifiers virt_specifiers,
1416 cp_ref_qualifier ref_qualifier,
1417 tree exception_specification,
1418 tree late_return_type)
1420 cp_declarator *declarator;
1422 declarator = make_declarator (cdk_function);
1423 declarator->declarator = target;
1424 declarator->u.function.parameters = parms;
1425 declarator->u.function.qualifiers = cv_qualifiers;
1426 declarator->u.function.virt_specifiers = virt_specifiers;
1427 declarator->u.function.ref_qualifier = ref_qualifier;
1428 declarator->u.function.exception_specification = exception_specification;
1429 declarator->u.function.late_return_type = late_return_type;
1430 if (target)
1432 declarator->id_loc = target->id_loc;
1433 declarator->parameter_pack_p = target->parameter_pack_p;
1434 target->parameter_pack_p = false;
1436 else
1437 declarator->parameter_pack_p = false;
1439 return declarator;
1442 /* Make a declarator for an array of BOUNDS elements, each of which is
1443 defined by ELEMENT. */
1445 cp_declarator *
1446 make_array_declarator (cp_declarator *element, tree bounds)
1448 cp_declarator *declarator;
1450 declarator = make_declarator (cdk_array);
1451 declarator->declarator = element;
1452 declarator->u.array.bounds = bounds;
1453 if (element)
1455 declarator->id_loc = element->id_loc;
1456 declarator->parameter_pack_p = element->parameter_pack_p;
1457 element->parameter_pack_p = false;
1459 else
1460 declarator->parameter_pack_p = false;
1462 return declarator;
1465 /* Determine whether the declarator we've seen so far can be a
1466 parameter pack, when followed by an ellipsis. */
1467 static bool
1468 declarator_can_be_parameter_pack (cp_declarator *declarator)
1470 /* Search for a declarator name, or any other declarator that goes
1471 after the point where the ellipsis could appear in a parameter
1472 pack. If we find any of these, then this declarator can not be
1473 made into a parameter pack. */
1474 bool found = false;
1475 while (declarator && !found)
1477 switch ((int)declarator->kind)
1479 case cdk_id:
1480 case cdk_array:
1481 found = true;
1482 break;
1484 case cdk_error:
1485 return true;
1487 default:
1488 declarator = declarator->declarator;
1489 break;
1493 return !found;
1496 cp_parameter_declarator *no_parameters;
1498 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1499 DECLARATOR and DEFAULT_ARGUMENT. */
1501 cp_parameter_declarator *
1502 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1503 cp_declarator *declarator,
1504 tree default_argument)
1506 cp_parameter_declarator *parameter;
1508 parameter = ((cp_parameter_declarator *)
1509 alloc_declarator (sizeof (cp_parameter_declarator)));
1510 parameter->next = NULL;
1511 if (decl_specifiers)
1512 parameter->decl_specifiers = *decl_specifiers;
1513 else
1514 clear_decl_specs (&parameter->decl_specifiers);
1515 parameter->declarator = declarator;
1516 parameter->default_argument = default_argument;
1517 parameter->ellipsis_p = false;
1519 return parameter;
1522 /* Returns true iff DECLARATOR is a declaration for a function. */
1524 static bool
1525 function_declarator_p (const cp_declarator *declarator)
1527 while (declarator)
1529 if (declarator->kind == cdk_function
1530 && declarator->declarator->kind == cdk_id)
1531 return true;
1532 if (declarator->kind == cdk_id
1533 || declarator->kind == cdk_error)
1534 return false;
1535 declarator = declarator->declarator;
1537 return false;
1540 /* The parser. */
1542 /* Overview
1543 --------
1545 A cp_parser parses the token stream as specified by the C++
1546 grammar. Its job is purely parsing, not semantic analysis. For
1547 example, the parser breaks the token stream into declarators,
1548 expressions, statements, and other similar syntactic constructs.
1549 It does not check that the types of the expressions on either side
1550 of an assignment-statement are compatible, or that a function is
1551 not declared with a parameter of type `void'.
1553 The parser invokes routines elsewhere in the compiler to perform
1554 semantic analysis and to build up the abstract syntax tree for the
1555 code processed.
1557 The parser (and the template instantiation code, which is, in a
1558 way, a close relative of parsing) are the only parts of the
1559 compiler that should be calling push_scope and pop_scope, or
1560 related functions. The parser (and template instantiation code)
1561 keeps track of what scope is presently active; everything else
1562 should simply honor that. (The code that generates static
1563 initializers may also need to set the scope, in order to check
1564 access control correctly when emitting the initializers.)
1566 Methodology
1567 -----------
1569 The parser is of the standard recursive-descent variety. Upcoming
1570 tokens in the token stream are examined in order to determine which
1571 production to use when parsing a non-terminal. Some C++ constructs
1572 require arbitrary look ahead to disambiguate. For example, it is
1573 impossible, in the general case, to tell whether a statement is an
1574 expression or declaration without scanning the entire statement.
1575 Therefore, the parser is capable of "parsing tentatively." When the
1576 parser is not sure what construct comes next, it enters this mode.
1577 Then, while we attempt to parse the construct, the parser queues up
1578 error messages, rather than issuing them immediately, and saves the
1579 tokens it consumes. If the construct is parsed successfully, the
1580 parser "commits", i.e., it issues any queued error messages and
1581 the tokens that were being preserved are permanently discarded.
1582 If, however, the construct is not parsed successfully, the parser
1583 rolls back its state completely so that it can resume parsing using
1584 a different alternative.
1586 Future Improvements
1587 -------------------
1589 The performance of the parser could probably be improved substantially.
1590 We could often eliminate the need to parse tentatively by looking ahead
1591 a little bit. In some places, this approach might not entirely eliminate
1592 the need to parse tentatively, but it might still speed up the average
1593 case. */
1595 /* Flags that are passed to some parsing functions. These values can
1596 be bitwise-ored together. */
1598 enum
1600 /* No flags. */
1601 CP_PARSER_FLAGS_NONE = 0x0,
1602 /* The construct is optional. If it is not present, then no error
1603 should be issued. */
1604 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1605 /* When parsing a type-specifier, treat user-defined type-names
1606 as non-type identifiers. */
1607 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1608 /* When parsing a type-specifier, do not try to parse a class-specifier
1609 or enum-specifier. */
1610 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1611 /* When parsing a decl-specifier-seq, only allow type-specifier or
1612 constexpr. */
1613 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1616 /* This type is used for parameters and variables which hold
1617 combinations of the above flags. */
1618 typedef int cp_parser_flags;
1620 /* The different kinds of declarators we want to parse. */
1622 typedef enum cp_parser_declarator_kind
1624 /* We want an abstract declarator. */
1625 CP_PARSER_DECLARATOR_ABSTRACT,
1626 /* We want a named declarator. */
1627 CP_PARSER_DECLARATOR_NAMED,
1628 /* We don't mind, but the name must be an unqualified-id. */
1629 CP_PARSER_DECLARATOR_EITHER
1630 } cp_parser_declarator_kind;
1632 /* The precedence values used to parse binary expressions. The minimum value
1633 of PREC must be 1, because zero is reserved to quickly discriminate
1634 binary operators from other tokens. */
1636 enum cp_parser_prec
1638 PREC_NOT_OPERATOR,
1639 PREC_LOGICAL_OR_EXPRESSION,
1640 PREC_LOGICAL_AND_EXPRESSION,
1641 PREC_INCLUSIVE_OR_EXPRESSION,
1642 PREC_EXCLUSIVE_OR_EXPRESSION,
1643 PREC_AND_EXPRESSION,
1644 PREC_EQUALITY_EXPRESSION,
1645 PREC_RELATIONAL_EXPRESSION,
1646 PREC_SHIFT_EXPRESSION,
1647 PREC_ADDITIVE_EXPRESSION,
1648 PREC_MULTIPLICATIVE_EXPRESSION,
1649 PREC_PM_EXPRESSION,
1650 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1653 /* A mapping from a token type to a corresponding tree node type, with a
1654 precedence value. */
1656 typedef struct cp_parser_binary_operations_map_node
1658 /* The token type. */
1659 enum cpp_ttype token_type;
1660 /* The corresponding tree code. */
1661 enum tree_code tree_type;
1662 /* The precedence of this operator. */
1663 enum cp_parser_prec prec;
1664 } cp_parser_binary_operations_map_node;
1666 typedef struct cp_parser_expression_stack_entry
1668 /* Left hand side of the binary operation we are currently
1669 parsing. */
1670 tree lhs;
1671 /* Original tree code for left hand side, if it was a binary
1672 expression itself (used for -Wparentheses). */
1673 enum tree_code lhs_type;
1674 /* Tree code for the binary operation we are parsing. */
1675 enum tree_code tree_type;
1676 /* Precedence of the binary operation we are parsing. */
1677 enum cp_parser_prec prec;
1678 /* Location of the binary operation we are parsing. */
1679 location_t loc;
1680 } cp_parser_expression_stack_entry;
1682 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1683 entries because precedence levels on the stack are monotonically
1684 increasing. */
1685 typedef struct cp_parser_expression_stack_entry
1686 cp_parser_expression_stack[NUM_PREC_VALUES];
1688 /* Prototypes. */
1690 /* Constructors and destructors. */
1692 static cp_parser_context *cp_parser_context_new
1693 (cp_parser_context *);
1695 /* Class variables. */
1697 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1699 /* The operator-precedence table used by cp_parser_binary_expression.
1700 Transformed into an associative array (binops_by_token) by
1701 cp_parser_new. */
1703 static const cp_parser_binary_operations_map_node binops[] = {
1704 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1705 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1707 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1708 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1709 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1711 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1712 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1714 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1715 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1717 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1718 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1719 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1720 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1722 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1723 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1725 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1727 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1729 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1731 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1733 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1736 /* The same as binops, but initialized by cp_parser_new so that
1737 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1738 for speed. */
1739 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1741 /* Constructors and destructors. */
1743 /* Construct a new context. The context below this one on the stack
1744 is given by NEXT. */
1746 static cp_parser_context *
1747 cp_parser_context_new (cp_parser_context* next)
1749 cp_parser_context *context;
1751 /* Allocate the storage. */
1752 if (cp_parser_context_free_list != NULL)
1754 /* Pull the first entry from the free list. */
1755 context = cp_parser_context_free_list;
1756 cp_parser_context_free_list = context->next;
1757 memset (context, 0, sizeof (*context));
1759 else
1760 context = ggc_alloc_cleared_cp_parser_context ();
1762 /* No errors have occurred yet in this context. */
1763 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1764 /* If this is not the bottommost context, copy information that we
1765 need from the previous context. */
1766 if (next)
1768 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1769 expression, then we are parsing one in this context, too. */
1770 context->object_type = next->object_type;
1771 /* Thread the stack. */
1772 context->next = next;
1775 return context;
1778 /* Managing the unparsed function queues. */
1780 #define unparsed_funs_with_default_args \
1781 parser->unparsed_queues->last ().funs_with_default_args
1782 #define unparsed_funs_with_definitions \
1783 parser->unparsed_queues->last ().funs_with_definitions
1784 #define unparsed_nsdmis \
1785 parser->unparsed_queues->last ().nsdmis
1787 static void
1788 push_unparsed_function_queues (cp_parser *parser)
1790 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1791 vec_safe_push (parser->unparsed_queues, e);
1794 static void
1795 pop_unparsed_function_queues (cp_parser *parser)
1797 release_tree_vector (unparsed_funs_with_definitions);
1798 parser->unparsed_queues->pop ();
1801 /* Prototypes. */
1803 /* Constructors and destructors. */
1805 static cp_parser *cp_parser_new
1806 (void);
1808 /* Routines to parse various constructs.
1810 Those that return `tree' will return the error_mark_node (rather
1811 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1812 Sometimes, they will return an ordinary node if error-recovery was
1813 attempted, even though a parse error occurred. So, to check
1814 whether or not a parse error occurred, you should always use
1815 cp_parser_error_occurred. If the construct is optional (indicated
1816 either by an `_opt' in the name of the function that does the
1817 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1818 the construct is not present. */
1820 /* Lexical conventions [gram.lex] */
1822 static tree cp_parser_identifier
1823 (cp_parser *);
1824 static tree cp_parser_string_literal
1825 (cp_parser *, bool, bool);
1826 static tree cp_parser_userdef_char_literal
1827 (cp_parser *);
1828 static tree cp_parser_userdef_string_literal
1829 (cp_token *);
1830 static tree cp_parser_userdef_numeric_literal
1831 (cp_parser *);
1833 /* Basic concepts [gram.basic] */
1835 static bool cp_parser_translation_unit
1836 (cp_parser *);
1838 /* Expressions [gram.expr] */
1840 static tree cp_parser_primary_expression
1841 (cp_parser *, bool, bool, bool, cp_id_kind *);
1842 static tree cp_parser_id_expression
1843 (cp_parser *, bool, bool, bool *, bool, bool);
1844 static tree cp_parser_unqualified_id
1845 (cp_parser *, bool, bool, bool, bool);
1846 static tree cp_parser_nested_name_specifier_opt
1847 (cp_parser *, bool, bool, bool, bool);
1848 static tree cp_parser_nested_name_specifier
1849 (cp_parser *, bool, bool, bool, bool);
1850 static tree cp_parser_qualifying_entity
1851 (cp_parser *, bool, bool, bool, bool, bool);
1852 static tree cp_parser_postfix_expression
1853 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1854 static tree cp_parser_postfix_open_square_expression
1855 (cp_parser *, tree, bool);
1856 static tree cp_parser_postfix_dot_deref_expression
1857 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1858 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1859 (cp_parser *, int, bool, bool, bool *);
1860 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1861 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1862 static void cp_parser_pseudo_destructor_name
1863 (cp_parser *, tree *, tree *);
1864 static tree cp_parser_unary_expression
1865 (cp_parser *, bool, bool, cp_id_kind *);
1866 static enum tree_code cp_parser_unary_operator
1867 (cp_token *);
1868 static tree cp_parser_new_expression
1869 (cp_parser *);
1870 static vec<tree, va_gc> *cp_parser_new_placement
1871 (cp_parser *);
1872 static tree cp_parser_new_type_id
1873 (cp_parser *, tree *);
1874 static cp_declarator *cp_parser_new_declarator_opt
1875 (cp_parser *);
1876 static cp_declarator *cp_parser_direct_new_declarator
1877 (cp_parser *);
1878 static vec<tree, va_gc> *cp_parser_new_initializer
1879 (cp_parser *);
1880 static tree cp_parser_delete_expression
1881 (cp_parser *);
1882 static tree cp_parser_cast_expression
1883 (cp_parser *, bool, bool, bool, cp_id_kind *);
1884 static tree cp_parser_binary_expression
1885 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1886 static tree cp_parser_question_colon_clause
1887 (cp_parser *, tree);
1888 static tree cp_parser_assignment_expression
1889 (cp_parser *, bool, cp_id_kind *);
1890 static enum tree_code cp_parser_assignment_operator_opt
1891 (cp_parser *);
1892 static tree cp_parser_expression
1893 (cp_parser *, bool, cp_id_kind *);
1894 static tree cp_parser_expression
1895 (cp_parser *, bool, bool, cp_id_kind *);
1896 static tree cp_parser_constant_expression
1897 (cp_parser *, bool, bool *);
1898 static tree cp_parser_builtin_offsetof
1899 (cp_parser *);
1900 static tree cp_parser_lambda_expression
1901 (cp_parser *);
1902 static void cp_parser_lambda_introducer
1903 (cp_parser *, tree);
1904 static bool cp_parser_lambda_declarator_opt
1905 (cp_parser *, tree);
1906 static void cp_parser_lambda_body
1907 (cp_parser *, tree);
1909 /* Statements [gram.stmt.stmt] */
1911 static void cp_parser_statement
1912 (cp_parser *, tree, bool, bool *);
1913 static void cp_parser_label_for_labeled_statement
1914 (cp_parser *, tree);
1915 static tree cp_parser_expression_statement
1916 (cp_parser *, tree);
1917 static tree cp_parser_compound_statement
1918 (cp_parser *, tree, bool, bool);
1919 static void cp_parser_statement_seq_opt
1920 (cp_parser *, tree);
1921 static tree cp_parser_selection_statement
1922 (cp_parser *, bool *);
1923 static tree cp_parser_condition
1924 (cp_parser *);
1925 static tree cp_parser_iteration_statement
1926 (cp_parser *);
1927 static bool cp_parser_for_init_statement
1928 (cp_parser *, tree *decl);
1929 static tree cp_parser_for
1930 (cp_parser *);
1931 static tree cp_parser_c_for
1932 (cp_parser *, tree, tree);
1933 static tree cp_parser_range_for
1934 (cp_parser *, tree, tree, tree);
1935 static void do_range_for_auto_deduction
1936 (tree, tree);
1937 static tree cp_parser_perform_range_for_lookup
1938 (tree, tree *, tree *);
1939 static tree cp_parser_range_for_member_function
1940 (tree, tree);
1941 static tree cp_parser_jump_statement
1942 (cp_parser *);
1943 static void cp_parser_declaration_statement
1944 (cp_parser *);
1946 static tree cp_parser_implicitly_scoped_statement
1947 (cp_parser *, bool *);
1948 static void cp_parser_already_scoped_statement
1949 (cp_parser *);
1951 /* Declarations [gram.dcl.dcl] */
1953 static void cp_parser_declaration_seq_opt
1954 (cp_parser *);
1955 static void cp_parser_declaration
1956 (cp_parser *);
1957 static void cp_parser_block_declaration
1958 (cp_parser *, bool);
1959 static void cp_parser_simple_declaration
1960 (cp_parser *, bool, tree *);
1961 static void cp_parser_decl_specifier_seq
1962 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1963 static tree cp_parser_storage_class_specifier_opt
1964 (cp_parser *);
1965 static tree cp_parser_function_specifier_opt
1966 (cp_parser *, cp_decl_specifier_seq *);
1967 static tree cp_parser_type_specifier
1968 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1969 int *, bool *);
1970 static tree cp_parser_simple_type_specifier
1971 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1972 static tree cp_parser_type_name
1973 (cp_parser *);
1974 static tree cp_parser_nonclass_name
1975 (cp_parser* parser);
1976 static tree cp_parser_elaborated_type_specifier
1977 (cp_parser *, bool, bool);
1978 static tree cp_parser_enum_specifier
1979 (cp_parser *);
1980 static void cp_parser_enumerator_list
1981 (cp_parser *, tree);
1982 static void cp_parser_enumerator_definition
1983 (cp_parser *, tree);
1984 static tree cp_parser_namespace_name
1985 (cp_parser *);
1986 static void cp_parser_namespace_definition
1987 (cp_parser *);
1988 static void cp_parser_namespace_body
1989 (cp_parser *);
1990 static tree cp_parser_qualified_namespace_specifier
1991 (cp_parser *);
1992 static void cp_parser_namespace_alias_definition
1993 (cp_parser *);
1994 static bool cp_parser_using_declaration
1995 (cp_parser *, bool);
1996 static void cp_parser_using_directive
1997 (cp_parser *);
1998 static tree cp_parser_alias_declaration
1999 (cp_parser *);
2000 static void cp_parser_asm_definition
2001 (cp_parser *);
2002 static void cp_parser_linkage_specification
2003 (cp_parser *);
2004 static void cp_parser_static_assert
2005 (cp_parser *, bool);
2006 static tree cp_parser_decltype
2007 (cp_parser *);
2009 /* Declarators [gram.dcl.decl] */
2011 static tree cp_parser_init_declarator
2012 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2013 static cp_declarator *cp_parser_declarator
2014 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2015 static cp_declarator *cp_parser_direct_declarator
2016 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2017 static enum tree_code cp_parser_ptr_operator
2018 (cp_parser *, tree *, cp_cv_quals *, tree *);
2019 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2020 (cp_parser *);
2021 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2022 (cp_parser *);
2023 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2024 (cp_parser *);
2025 static tree cp_parser_late_return_type_opt
2026 (cp_parser *, cp_cv_quals);
2027 static tree cp_parser_declarator_id
2028 (cp_parser *, bool);
2029 static tree cp_parser_type_id
2030 (cp_parser *);
2031 static tree cp_parser_template_type_arg
2032 (cp_parser *);
2033 static tree cp_parser_trailing_type_id (cp_parser *);
2034 static tree cp_parser_type_id_1
2035 (cp_parser *, bool, bool);
2036 static void cp_parser_type_specifier_seq
2037 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2038 static tree cp_parser_parameter_declaration_clause
2039 (cp_parser *);
2040 static tree cp_parser_parameter_declaration_list
2041 (cp_parser *, bool *);
2042 static cp_parameter_declarator *cp_parser_parameter_declaration
2043 (cp_parser *, bool, bool *);
2044 static tree cp_parser_default_argument
2045 (cp_parser *, bool);
2046 static void cp_parser_function_body
2047 (cp_parser *, bool);
2048 static tree cp_parser_initializer
2049 (cp_parser *, bool *, bool *);
2050 static tree cp_parser_initializer_clause
2051 (cp_parser *, bool *);
2052 static tree cp_parser_braced_list
2053 (cp_parser*, bool*);
2054 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2055 (cp_parser *, bool *);
2057 static bool cp_parser_ctor_initializer_opt_and_function_body
2058 (cp_parser *, bool);
2060 /* Classes [gram.class] */
2062 static tree cp_parser_class_name
2063 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2064 static tree cp_parser_class_specifier
2065 (cp_parser *);
2066 static tree cp_parser_class_head
2067 (cp_parser *, bool *);
2068 static enum tag_types cp_parser_class_key
2069 (cp_parser *);
2070 static void cp_parser_member_specification_opt
2071 (cp_parser *);
2072 static void cp_parser_member_declaration
2073 (cp_parser *);
2074 static tree cp_parser_pure_specifier
2075 (cp_parser *);
2076 static tree cp_parser_constant_initializer
2077 (cp_parser *);
2079 /* Derived classes [gram.class.derived] */
2081 static tree cp_parser_base_clause
2082 (cp_parser *);
2083 static tree cp_parser_base_specifier
2084 (cp_parser *);
2086 /* Special member functions [gram.special] */
2088 static tree cp_parser_conversion_function_id
2089 (cp_parser *);
2090 static tree cp_parser_conversion_type_id
2091 (cp_parser *);
2092 static cp_declarator *cp_parser_conversion_declarator_opt
2093 (cp_parser *);
2094 static bool cp_parser_ctor_initializer_opt
2095 (cp_parser *);
2096 static void cp_parser_mem_initializer_list
2097 (cp_parser *);
2098 static tree cp_parser_mem_initializer
2099 (cp_parser *);
2100 static tree cp_parser_mem_initializer_id
2101 (cp_parser *);
2103 /* Overloading [gram.over] */
2105 static tree cp_parser_operator_function_id
2106 (cp_parser *);
2107 static tree cp_parser_operator
2108 (cp_parser *);
2110 /* Templates [gram.temp] */
2112 static void cp_parser_template_declaration
2113 (cp_parser *, bool);
2114 static tree cp_parser_template_parameter_list
2115 (cp_parser *);
2116 static tree cp_parser_template_parameter
2117 (cp_parser *, bool *, bool *);
2118 static tree cp_parser_type_parameter
2119 (cp_parser *, bool *);
2120 static tree cp_parser_template_id
2121 (cp_parser *, bool, bool, enum tag_types, bool);
2122 static tree cp_parser_template_name
2123 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2124 static tree cp_parser_template_argument_list
2125 (cp_parser *);
2126 static tree cp_parser_template_argument
2127 (cp_parser *);
2128 static void cp_parser_explicit_instantiation
2129 (cp_parser *);
2130 static void cp_parser_explicit_specialization
2131 (cp_parser *);
2133 /* Exception handling [gram.exception] */
2135 static tree cp_parser_try_block
2136 (cp_parser *);
2137 static bool cp_parser_function_try_block
2138 (cp_parser *);
2139 static void cp_parser_handler_seq
2140 (cp_parser *);
2141 static void cp_parser_handler
2142 (cp_parser *);
2143 static tree cp_parser_exception_declaration
2144 (cp_parser *);
2145 static tree cp_parser_throw_expression
2146 (cp_parser *);
2147 static tree cp_parser_exception_specification_opt
2148 (cp_parser *);
2149 static tree cp_parser_type_id_list
2150 (cp_parser *);
2152 /* GNU Extensions */
2154 static tree cp_parser_asm_specification_opt
2155 (cp_parser *);
2156 static tree cp_parser_asm_operand_list
2157 (cp_parser *);
2158 static tree cp_parser_asm_clobber_list
2159 (cp_parser *);
2160 static tree cp_parser_asm_label_list
2161 (cp_parser *);
2162 static bool cp_next_tokens_can_be_attribute_p
2163 (cp_parser *);
2164 static bool cp_next_tokens_can_be_gnu_attribute_p
2165 (cp_parser *);
2166 static bool cp_next_tokens_can_be_std_attribute_p
2167 (cp_parser *);
2168 static bool cp_nth_tokens_can_be_std_attribute_p
2169 (cp_parser *, size_t);
2170 static bool cp_nth_tokens_can_be_gnu_attribute_p
2171 (cp_parser *, size_t);
2172 static bool cp_nth_tokens_can_be_attribute_p
2173 (cp_parser *, size_t);
2174 static tree cp_parser_attributes_opt
2175 (cp_parser *);
2176 static tree cp_parser_gnu_attributes_opt
2177 (cp_parser *);
2178 static tree cp_parser_gnu_attribute_list
2179 (cp_parser *);
2180 static tree cp_parser_std_attribute
2181 (cp_parser *);
2182 static tree cp_parser_std_attribute_spec
2183 (cp_parser *);
2184 static tree cp_parser_std_attribute_spec_seq
2185 (cp_parser *);
2186 static bool cp_parser_extension_opt
2187 (cp_parser *, int *);
2188 static void cp_parser_label_declaration
2189 (cp_parser *);
2191 /* Transactional Memory Extensions */
2193 static tree cp_parser_transaction
2194 (cp_parser *, enum rid);
2195 static tree cp_parser_transaction_expression
2196 (cp_parser *, enum rid);
2197 static bool cp_parser_function_transaction
2198 (cp_parser *, enum rid);
2199 static tree cp_parser_transaction_cancel
2200 (cp_parser *);
2202 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2203 static bool cp_parser_pragma
2204 (cp_parser *, enum pragma_context);
2206 /* Objective-C++ Productions */
2208 static tree cp_parser_objc_message_receiver
2209 (cp_parser *);
2210 static tree cp_parser_objc_message_args
2211 (cp_parser *);
2212 static tree cp_parser_objc_message_expression
2213 (cp_parser *);
2214 static tree cp_parser_objc_encode_expression
2215 (cp_parser *);
2216 static tree cp_parser_objc_defs_expression
2217 (cp_parser *);
2218 static tree cp_parser_objc_protocol_expression
2219 (cp_parser *);
2220 static tree cp_parser_objc_selector_expression
2221 (cp_parser *);
2222 static tree cp_parser_objc_expression
2223 (cp_parser *);
2224 static bool cp_parser_objc_selector_p
2225 (enum cpp_ttype);
2226 static tree cp_parser_objc_selector
2227 (cp_parser *);
2228 static tree cp_parser_objc_protocol_refs_opt
2229 (cp_parser *);
2230 static void cp_parser_objc_declaration
2231 (cp_parser *, tree);
2232 static tree cp_parser_objc_statement
2233 (cp_parser *);
2234 static bool cp_parser_objc_valid_prefix_attributes
2235 (cp_parser *, tree *);
2236 static void cp_parser_objc_at_property_declaration
2237 (cp_parser *) ;
2238 static void cp_parser_objc_at_synthesize_declaration
2239 (cp_parser *) ;
2240 static void cp_parser_objc_at_dynamic_declaration
2241 (cp_parser *) ;
2242 static tree cp_parser_objc_struct_declaration
2243 (cp_parser *) ;
2245 /* Utility Routines */
2247 static tree cp_parser_lookup_name
2248 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2249 static tree cp_parser_lookup_name_simple
2250 (cp_parser *, tree, location_t);
2251 static tree cp_parser_maybe_treat_template_as_class
2252 (tree, bool);
2253 static bool cp_parser_check_declarator_template_parameters
2254 (cp_parser *, cp_declarator *, location_t);
2255 static bool cp_parser_check_template_parameters
2256 (cp_parser *, unsigned, location_t, cp_declarator *);
2257 static tree cp_parser_simple_cast_expression
2258 (cp_parser *);
2259 static tree cp_parser_global_scope_opt
2260 (cp_parser *, bool);
2261 static bool cp_parser_constructor_declarator_p
2262 (cp_parser *, bool);
2263 static tree cp_parser_function_definition_from_specifiers_and_declarator
2264 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2265 static tree cp_parser_function_definition_after_declarator
2266 (cp_parser *, bool);
2267 static void cp_parser_template_declaration_after_export
2268 (cp_parser *, bool);
2269 static void cp_parser_perform_template_parameter_access_checks
2270 (vec<deferred_access_check, va_gc> *);
2271 static tree cp_parser_single_declaration
2272 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2273 static tree cp_parser_functional_cast
2274 (cp_parser *, tree);
2275 static tree cp_parser_save_member_function_body
2276 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2277 static tree cp_parser_save_nsdmi
2278 (cp_parser *);
2279 static tree cp_parser_enclosed_template_argument_list
2280 (cp_parser *);
2281 static void cp_parser_save_default_args
2282 (cp_parser *, tree);
2283 static void cp_parser_late_parsing_for_member
2284 (cp_parser *, tree);
2285 static tree cp_parser_late_parse_one_default_arg
2286 (cp_parser *, tree, tree, tree);
2287 static void cp_parser_late_parsing_nsdmi
2288 (cp_parser *, tree);
2289 static void cp_parser_late_parsing_default_args
2290 (cp_parser *, tree);
2291 static tree cp_parser_sizeof_operand
2292 (cp_parser *, enum rid);
2293 static tree cp_parser_trait_expr
2294 (cp_parser *, enum rid);
2295 static bool cp_parser_declares_only_class_p
2296 (cp_parser *);
2297 static void cp_parser_set_storage_class
2298 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2299 static void cp_parser_set_decl_spec_type
2300 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2301 static void set_and_check_decl_spec_loc
2302 (cp_decl_specifier_seq *decl_specs,
2303 cp_decl_spec ds, cp_token *);
2304 static bool cp_parser_friend_p
2305 (const cp_decl_specifier_seq *);
2306 static void cp_parser_required_error
2307 (cp_parser *, required_token, bool);
2308 static cp_token *cp_parser_require
2309 (cp_parser *, enum cpp_ttype, required_token);
2310 static cp_token *cp_parser_require_keyword
2311 (cp_parser *, enum rid, required_token);
2312 static bool cp_parser_token_starts_function_definition_p
2313 (cp_token *);
2314 static bool cp_parser_next_token_starts_class_definition_p
2315 (cp_parser *);
2316 static bool cp_parser_next_token_ends_template_argument_p
2317 (cp_parser *);
2318 static bool cp_parser_nth_token_starts_template_argument_list_p
2319 (cp_parser *, size_t);
2320 static enum tag_types cp_parser_token_is_class_key
2321 (cp_token *);
2322 static void cp_parser_check_class_key
2323 (enum tag_types, tree type);
2324 static void cp_parser_check_access_in_redeclaration
2325 (tree type, location_t location);
2326 static bool cp_parser_optional_template_keyword
2327 (cp_parser *);
2328 static void cp_parser_pre_parsed_nested_name_specifier
2329 (cp_parser *);
2330 static bool cp_parser_cache_group
2331 (cp_parser *, enum cpp_ttype, unsigned);
2332 static tree cp_parser_cache_defarg
2333 (cp_parser *parser, bool nsdmi);
2334 static void cp_parser_parse_tentatively
2335 (cp_parser *);
2336 static void cp_parser_commit_to_tentative_parse
2337 (cp_parser *);
2338 static void cp_parser_abort_tentative_parse
2339 (cp_parser *);
2340 static bool cp_parser_parse_definitely
2341 (cp_parser *);
2342 static inline bool cp_parser_parsing_tentatively
2343 (cp_parser *);
2344 static bool cp_parser_uncommitted_to_tentative_parse_p
2345 (cp_parser *);
2346 static void cp_parser_error
2347 (cp_parser *, const char *);
2348 static void cp_parser_name_lookup_error
2349 (cp_parser *, tree, tree, name_lookup_error, location_t);
2350 static bool cp_parser_simulate_error
2351 (cp_parser *);
2352 static bool cp_parser_check_type_definition
2353 (cp_parser *);
2354 static void cp_parser_check_for_definition_in_return_type
2355 (cp_declarator *, tree, location_t type_location);
2356 static void cp_parser_check_for_invalid_template_id
2357 (cp_parser *, tree, enum tag_types, location_t location);
2358 static bool cp_parser_non_integral_constant_expression
2359 (cp_parser *, non_integral_constant);
2360 static void cp_parser_diagnose_invalid_type_name
2361 (cp_parser *, tree, tree, location_t);
2362 static bool cp_parser_parse_and_diagnose_invalid_type_name
2363 (cp_parser *);
2364 static int cp_parser_skip_to_closing_parenthesis
2365 (cp_parser *, bool, bool, bool);
2366 static void cp_parser_skip_to_end_of_statement
2367 (cp_parser *);
2368 static void cp_parser_consume_semicolon_at_end_of_statement
2369 (cp_parser *);
2370 static void cp_parser_skip_to_end_of_block_or_statement
2371 (cp_parser *);
2372 static bool cp_parser_skip_to_closing_brace
2373 (cp_parser *);
2374 static void cp_parser_skip_to_end_of_template_parameter_list
2375 (cp_parser *);
2376 static void cp_parser_skip_to_pragma_eol
2377 (cp_parser*, cp_token *);
2378 static bool cp_parser_error_occurred
2379 (cp_parser *);
2380 static bool cp_parser_allow_gnu_extensions_p
2381 (cp_parser *);
2382 static bool cp_parser_is_pure_string_literal
2383 (cp_token *);
2384 static bool cp_parser_is_string_literal
2385 (cp_token *);
2386 static bool cp_parser_is_keyword
2387 (cp_token *, enum rid);
2388 static tree cp_parser_make_typename_type
2389 (cp_parser *, tree, tree, location_t location);
2390 static cp_declarator * cp_parser_make_indirect_declarator
2391 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2393 /* Returns nonzero if we are parsing tentatively. */
2395 static inline bool
2396 cp_parser_parsing_tentatively (cp_parser* parser)
2398 return parser->context->next != NULL;
2401 /* Returns nonzero if TOKEN is a string literal. */
2403 static bool
2404 cp_parser_is_pure_string_literal (cp_token* token)
2406 return (token->type == CPP_STRING ||
2407 token->type == CPP_STRING16 ||
2408 token->type == CPP_STRING32 ||
2409 token->type == CPP_WSTRING ||
2410 token->type == CPP_UTF8STRING);
2413 /* Returns nonzero if TOKEN is a string literal
2414 of a user-defined string literal. */
2416 static bool
2417 cp_parser_is_string_literal (cp_token* token)
2419 return (cp_parser_is_pure_string_literal (token) ||
2420 token->type == CPP_STRING_USERDEF ||
2421 token->type == CPP_STRING16_USERDEF ||
2422 token->type == CPP_STRING32_USERDEF ||
2423 token->type == CPP_WSTRING_USERDEF ||
2424 token->type == CPP_UTF8STRING_USERDEF);
2427 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2429 static bool
2430 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2432 return token->keyword == keyword;
2435 /* If not parsing tentatively, issue a diagnostic of the form
2436 FILE:LINE: MESSAGE before TOKEN
2437 where TOKEN is the next token in the input stream. MESSAGE
2438 (specified by the caller) is usually of the form "expected
2439 OTHER-TOKEN". */
2441 static void
2442 cp_parser_error (cp_parser* parser, const char* gmsgid)
2444 if (!cp_parser_simulate_error (parser))
2446 cp_token *token = cp_lexer_peek_token (parser->lexer);
2447 /* This diagnostic makes more sense if it is tagged to the line
2448 of the token we just peeked at. */
2449 cp_lexer_set_source_position_from_token (token);
2451 if (token->type == CPP_PRAGMA)
2453 error_at (token->location,
2454 "%<#pragma%> is not allowed here");
2455 cp_parser_skip_to_pragma_eol (parser, token);
2456 return;
2459 c_parse_error (gmsgid,
2460 /* Because c_parser_error does not understand
2461 CPP_KEYWORD, keywords are treated like
2462 identifiers. */
2463 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2464 token->u.value, token->flags);
2468 /* Issue an error about name-lookup failing. NAME is the
2469 IDENTIFIER_NODE DECL is the result of
2470 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2471 the thing that we hoped to find. */
2473 static void
2474 cp_parser_name_lookup_error (cp_parser* parser,
2475 tree name,
2476 tree decl,
2477 name_lookup_error desired,
2478 location_t location)
2480 /* If name lookup completely failed, tell the user that NAME was not
2481 declared. */
2482 if (decl == error_mark_node)
2484 if (parser->scope && parser->scope != global_namespace)
2485 error_at (location, "%<%E::%E%> has not been declared",
2486 parser->scope, name);
2487 else if (parser->scope == global_namespace)
2488 error_at (location, "%<::%E%> has not been declared", name);
2489 else if (parser->object_scope
2490 && !CLASS_TYPE_P (parser->object_scope))
2491 error_at (location, "request for member %qE in non-class type %qT",
2492 name, parser->object_scope);
2493 else if (parser->object_scope)
2494 error_at (location, "%<%T::%E%> has not been declared",
2495 parser->object_scope, name);
2496 else
2497 error_at (location, "%qE has not been declared", name);
2499 else if (parser->scope && parser->scope != global_namespace)
2501 switch (desired)
2503 case NLE_TYPE:
2504 error_at (location, "%<%E::%E%> is not a type",
2505 parser->scope, name);
2506 break;
2507 case NLE_CXX98:
2508 error_at (location, "%<%E::%E%> is not a class or namespace",
2509 parser->scope, name);
2510 break;
2511 case NLE_NOT_CXX98:
2512 error_at (location,
2513 "%<%E::%E%> is not a class, namespace, or enumeration",
2514 parser->scope, name);
2515 break;
2516 default:
2517 gcc_unreachable ();
2521 else if (parser->scope == global_namespace)
2523 switch (desired)
2525 case NLE_TYPE:
2526 error_at (location, "%<::%E%> is not a type", name);
2527 break;
2528 case NLE_CXX98:
2529 error_at (location, "%<::%E%> is not a class or namespace", name);
2530 break;
2531 case NLE_NOT_CXX98:
2532 error_at (location,
2533 "%<::%E%> is not a class, namespace, or enumeration",
2534 name);
2535 break;
2536 default:
2537 gcc_unreachable ();
2540 else
2542 switch (desired)
2544 case NLE_TYPE:
2545 error_at (location, "%qE is not a type", name);
2546 break;
2547 case NLE_CXX98:
2548 error_at (location, "%qE is not a class or namespace", name);
2549 break;
2550 case NLE_NOT_CXX98:
2551 error_at (location,
2552 "%qE is not a class, namespace, or enumeration", name);
2553 break;
2554 default:
2555 gcc_unreachable ();
2560 /* If we are parsing tentatively, remember that an error has occurred
2561 during this tentative parse. Returns true if the error was
2562 simulated; false if a message should be issued by the caller. */
2564 static bool
2565 cp_parser_simulate_error (cp_parser* parser)
2567 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2569 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2570 return true;
2572 return false;
2575 /* This function is called when a type is defined. If type
2576 definitions are forbidden at this point, an error message is
2577 issued. */
2579 static bool
2580 cp_parser_check_type_definition (cp_parser* parser)
2582 /* If types are forbidden here, issue a message. */
2583 if (parser->type_definition_forbidden_message)
2585 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2586 in the message need to be interpreted. */
2587 error (parser->type_definition_forbidden_message);
2588 return false;
2590 return true;
2593 /* This function is called when the DECLARATOR is processed. The TYPE
2594 was a type defined in the decl-specifiers. If it is invalid to
2595 define a type in the decl-specifiers for DECLARATOR, an error is
2596 issued. TYPE_LOCATION is the location of TYPE and is used
2597 for error reporting. */
2599 static void
2600 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2601 tree type, location_t type_location)
2603 /* [dcl.fct] forbids type definitions in return types.
2604 Unfortunately, it's not easy to know whether or not we are
2605 processing a return type until after the fact. */
2606 while (declarator
2607 && (declarator->kind == cdk_pointer
2608 || declarator->kind == cdk_reference
2609 || declarator->kind == cdk_ptrmem))
2610 declarator = declarator->declarator;
2611 if (declarator
2612 && declarator->kind == cdk_function)
2614 error_at (type_location,
2615 "new types may not be defined in a return type");
2616 inform (type_location,
2617 "(perhaps a semicolon is missing after the definition of %qT)",
2618 type);
2622 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2623 "<" in any valid C++ program. If the next token is indeed "<",
2624 issue a message warning the user about what appears to be an
2625 invalid attempt to form a template-id. LOCATION is the location
2626 of the type-specifier (TYPE) */
2628 static void
2629 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2630 tree type,
2631 enum tag_types tag_type,
2632 location_t location)
2634 cp_token_position start = 0;
2636 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2638 if (TYPE_P (type))
2639 error_at (location, "%qT is not a template", type);
2640 else if (identifier_p (type))
2642 if (tag_type != none_type)
2643 error_at (location, "%qE is not a class template", type);
2644 else
2645 error_at (location, "%qE is not a template", type);
2647 else
2648 error_at (location, "invalid template-id");
2649 /* Remember the location of the invalid "<". */
2650 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2651 start = cp_lexer_token_position (parser->lexer, true);
2652 /* Consume the "<". */
2653 cp_lexer_consume_token (parser->lexer);
2654 /* Parse the template arguments. */
2655 cp_parser_enclosed_template_argument_list (parser);
2656 /* Permanently remove the invalid template arguments so that
2657 this error message is not issued again. */
2658 if (start)
2659 cp_lexer_purge_tokens_after (parser->lexer, start);
2663 /* If parsing an integral constant-expression, issue an error message
2664 about the fact that THING appeared and return true. Otherwise,
2665 return false. In either case, set
2666 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2668 static bool
2669 cp_parser_non_integral_constant_expression (cp_parser *parser,
2670 non_integral_constant thing)
2672 parser->non_integral_constant_expression_p = true;
2673 if (parser->integral_constant_expression_p)
2675 if (!parser->allow_non_integral_constant_expression_p)
2677 const char *msg = NULL;
2678 switch (thing)
2680 case NIC_FLOAT:
2681 error ("floating-point literal "
2682 "cannot appear in a constant-expression");
2683 return true;
2684 case NIC_CAST:
2685 error ("a cast to a type other than an integral or "
2686 "enumeration type cannot appear in a "
2687 "constant-expression");
2688 return true;
2689 case NIC_TYPEID:
2690 error ("%<typeid%> operator "
2691 "cannot appear in a constant-expression");
2692 return true;
2693 case NIC_NCC:
2694 error ("non-constant compound literals "
2695 "cannot appear in a constant-expression");
2696 return true;
2697 case NIC_FUNC_CALL:
2698 error ("a function call "
2699 "cannot appear in a constant-expression");
2700 return true;
2701 case NIC_INC:
2702 error ("an increment "
2703 "cannot appear in a constant-expression");
2704 return true;
2705 case NIC_DEC:
2706 error ("an decrement "
2707 "cannot appear in a constant-expression");
2708 return true;
2709 case NIC_ARRAY_REF:
2710 error ("an array reference "
2711 "cannot appear in a constant-expression");
2712 return true;
2713 case NIC_ADDR_LABEL:
2714 error ("the address of a label "
2715 "cannot appear in a constant-expression");
2716 return true;
2717 case NIC_OVERLOADED:
2718 error ("calls to overloaded operators "
2719 "cannot appear in a constant-expression");
2720 return true;
2721 case NIC_ASSIGNMENT:
2722 error ("an assignment cannot appear in a constant-expression");
2723 return true;
2724 case NIC_COMMA:
2725 error ("a comma operator "
2726 "cannot appear in a constant-expression");
2727 return true;
2728 case NIC_CONSTRUCTOR:
2729 error ("a call to a constructor "
2730 "cannot appear in a constant-expression");
2731 return true;
2732 case NIC_TRANSACTION:
2733 error ("a transaction expression "
2734 "cannot appear in a constant-expression");
2735 return true;
2736 case NIC_THIS:
2737 msg = "this";
2738 break;
2739 case NIC_FUNC_NAME:
2740 msg = "__FUNCTION__";
2741 break;
2742 case NIC_PRETTY_FUNC:
2743 msg = "__PRETTY_FUNCTION__";
2744 break;
2745 case NIC_C99_FUNC:
2746 msg = "__func__";
2747 break;
2748 case NIC_VA_ARG:
2749 msg = "va_arg";
2750 break;
2751 case NIC_ARROW:
2752 msg = "->";
2753 break;
2754 case NIC_POINT:
2755 msg = ".";
2756 break;
2757 case NIC_STAR:
2758 msg = "*";
2759 break;
2760 case NIC_ADDR:
2761 msg = "&";
2762 break;
2763 case NIC_PREINCREMENT:
2764 msg = "++";
2765 break;
2766 case NIC_PREDECREMENT:
2767 msg = "--";
2768 break;
2769 case NIC_NEW:
2770 msg = "new";
2771 break;
2772 case NIC_DEL:
2773 msg = "delete";
2774 break;
2775 default:
2776 gcc_unreachable ();
2778 if (msg)
2779 error ("%qs cannot appear in a constant-expression", msg);
2780 return true;
2783 return false;
2786 /* Emit a diagnostic for an invalid type name. SCOPE is the
2787 qualifying scope (or NULL, if none) for ID. This function commits
2788 to the current active tentative parse, if any. (Otherwise, the
2789 problematic construct might be encountered again later, resulting
2790 in duplicate error messages.) LOCATION is the location of ID. */
2792 static void
2793 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2794 tree scope, tree id,
2795 location_t location)
2797 tree decl, old_scope;
2798 cp_parser_commit_to_tentative_parse (parser);
2799 /* Try to lookup the identifier. */
2800 old_scope = parser->scope;
2801 parser->scope = scope;
2802 decl = cp_parser_lookup_name_simple (parser, id, location);
2803 parser->scope = old_scope;
2804 /* If the lookup found a template-name, it means that the user forgot
2805 to specify an argument list. Emit a useful error message. */
2806 if (TREE_CODE (decl) == TEMPLATE_DECL)
2807 error_at (location,
2808 "invalid use of template-name %qE without an argument list",
2809 decl);
2810 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2811 error_at (location, "invalid use of destructor %qD as a type", id);
2812 else if (TREE_CODE (decl) == TYPE_DECL)
2813 /* Something like 'unsigned A a;' */
2814 error_at (location, "invalid combination of multiple type-specifiers");
2815 else if (!parser->scope)
2817 /* Issue an error message. */
2818 error_at (location, "%qE does not name a type", id);
2819 /* If we're in a template class, it's possible that the user was
2820 referring to a type from a base class. For example:
2822 template <typename T> struct A { typedef T X; };
2823 template <typename T> struct B : public A<T> { X x; };
2825 The user should have said "typename A<T>::X". */
2826 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2827 inform (location, "C++11 %<constexpr%> only available with "
2828 "-std=c++11 or -std=gnu++11");
2829 else if (processing_template_decl && current_class_type
2830 && TYPE_BINFO (current_class_type))
2832 tree b;
2834 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2836 b = TREE_CHAIN (b))
2838 tree base_type = BINFO_TYPE (b);
2839 if (CLASS_TYPE_P (base_type)
2840 && dependent_type_p (base_type))
2842 tree field;
2843 /* Go from a particular instantiation of the
2844 template (which will have an empty TYPE_FIELDs),
2845 to the main version. */
2846 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2847 for (field = TYPE_FIELDS (base_type);
2848 field;
2849 field = DECL_CHAIN (field))
2850 if (TREE_CODE (field) == TYPE_DECL
2851 && DECL_NAME (field) == id)
2853 inform (location,
2854 "(perhaps %<typename %T::%E%> was intended)",
2855 BINFO_TYPE (b), id);
2856 break;
2858 if (field)
2859 break;
2864 /* Here we diagnose qualified-ids where the scope is actually correct,
2865 but the identifier does not resolve to a valid type name. */
2866 else if (parser->scope != error_mark_node)
2868 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2869 error_at (location, "%qE in namespace %qE does not name a type",
2870 id, parser->scope);
2871 else if (CLASS_TYPE_P (parser->scope)
2872 && constructor_name_p (id, parser->scope))
2874 /* A<T>::A<T>() */
2875 error_at (location, "%<%T::%E%> names the constructor, not"
2876 " the type", parser->scope, id);
2877 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2878 error_at (location, "and %qT has no template constructors",
2879 parser->scope);
2881 else if (TYPE_P (parser->scope)
2882 && dependent_scope_p (parser->scope))
2883 error_at (location, "need %<typename%> before %<%T::%E%> because "
2884 "%qT is a dependent scope",
2885 parser->scope, id, parser->scope);
2886 else if (TYPE_P (parser->scope))
2887 error_at (location, "%qE in %q#T does not name a type",
2888 id, parser->scope);
2889 else
2890 gcc_unreachable ();
2894 /* Check for a common situation where a type-name should be present,
2895 but is not, and issue a sensible error message. Returns true if an
2896 invalid type-name was detected.
2898 The situation handled by this function are variable declarations of the
2899 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2900 Usually, `ID' should name a type, but if we got here it means that it
2901 does not. We try to emit the best possible error message depending on
2902 how exactly the id-expression looks like. */
2904 static bool
2905 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2907 tree id;
2908 cp_token *token = cp_lexer_peek_token (parser->lexer);
2910 /* Avoid duplicate error about ambiguous lookup. */
2911 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2913 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2914 if (next->type == CPP_NAME && next->ambiguous_p)
2915 goto out;
2918 cp_parser_parse_tentatively (parser);
2919 id = cp_parser_id_expression (parser,
2920 /*template_keyword_p=*/false,
2921 /*check_dependency_p=*/true,
2922 /*template_p=*/NULL,
2923 /*declarator_p=*/true,
2924 /*optional_p=*/false);
2925 /* If the next token is a (, this is a function with no explicit return
2926 type, i.e. constructor, destructor or conversion op. */
2927 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2928 || TREE_CODE (id) == TYPE_DECL)
2930 cp_parser_abort_tentative_parse (parser);
2931 return false;
2933 if (!cp_parser_parse_definitely (parser))
2934 return false;
2936 /* Emit a diagnostic for the invalid type. */
2937 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2938 id, token->location);
2939 out:
2940 /* If we aren't in the middle of a declarator (i.e. in a
2941 parameter-declaration-clause), skip to the end of the declaration;
2942 there's no point in trying to process it. */
2943 if (!parser->in_declarator_p)
2944 cp_parser_skip_to_end_of_block_or_statement (parser);
2945 return true;
2948 /* Consume tokens up to, and including, the next non-nested closing `)'.
2949 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2950 are doing error recovery. Returns -1 if OR_COMMA is true and we
2951 found an unnested comma. */
2953 static int
2954 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2955 bool recovering,
2956 bool or_comma,
2957 bool consume_paren)
2959 unsigned paren_depth = 0;
2960 unsigned brace_depth = 0;
2961 unsigned square_depth = 0;
2963 if (recovering && !or_comma
2964 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2965 return 0;
2967 while (true)
2969 cp_token * token = cp_lexer_peek_token (parser->lexer);
2971 switch (token->type)
2973 case CPP_EOF:
2974 case CPP_PRAGMA_EOL:
2975 /* If we've run out of tokens, then there is no closing `)'. */
2976 return 0;
2978 /* This is good for lambda expression capture-lists. */
2979 case CPP_OPEN_SQUARE:
2980 ++square_depth;
2981 break;
2982 case CPP_CLOSE_SQUARE:
2983 if (!square_depth--)
2984 return 0;
2985 break;
2987 case CPP_SEMICOLON:
2988 /* This matches the processing in skip_to_end_of_statement. */
2989 if (!brace_depth)
2990 return 0;
2991 break;
2993 case CPP_OPEN_BRACE:
2994 ++brace_depth;
2995 break;
2996 case CPP_CLOSE_BRACE:
2997 if (!brace_depth--)
2998 return 0;
2999 break;
3001 case CPP_COMMA:
3002 if (recovering && or_comma && !brace_depth && !paren_depth
3003 && !square_depth)
3004 return -1;
3005 break;
3007 case CPP_OPEN_PAREN:
3008 if (!brace_depth)
3009 ++paren_depth;
3010 break;
3012 case CPP_CLOSE_PAREN:
3013 if (!brace_depth && !paren_depth--)
3015 if (consume_paren)
3016 cp_lexer_consume_token (parser->lexer);
3017 return 1;
3019 break;
3021 default:
3022 break;
3025 /* Consume the token. */
3026 cp_lexer_consume_token (parser->lexer);
3030 /* Consume tokens until we reach the end of the current statement.
3031 Normally, that will be just before consuming a `;'. However, if a
3032 non-nested `}' comes first, then we stop before consuming that. */
3034 static void
3035 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3037 unsigned nesting_depth = 0;
3039 while (true)
3041 cp_token *token = cp_lexer_peek_token (parser->lexer);
3043 switch (token->type)
3045 case CPP_EOF:
3046 case CPP_PRAGMA_EOL:
3047 /* If we've run out of tokens, stop. */
3048 return;
3050 case CPP_SEMICOLON:
3051 /* If the next token is a `;', we have reached the end of the
3052 statement. */
3053 if (!nesting_depth)
3054 return;
3055 break;
3057 case CPP_CLOSE_BRACE:
3058 /* If this is a non-nested '}', stop before consuming it.
3059 That way, when confronted with something like:
3061 { 3 + }
3063 we stop before consuming the closing '}', even though we
3064 have not yet reached a `;'. */
3065 if (nesting_depth == 0)
3066 return;
3068 /* If it is the closing '}' for a block that we have
3069 scanned, stop -- but only after consuming the token.
3070 That way given:
3072 void f g () { ... }
3073 typedef int I;
3075 we will stop after the body of the erroneously declared
3076 function, but before consuming the following `typedef'
3077 declaration. */
3078 if (--nesting_depth == 0)
3080 cp_lexer_consume_token (parser->lexer);
3081 return;
3084 case CPP_OPEN_BRACE:
3085 ++nesting_depth;
3086 break;
3088 default:
3089 break;
3092 /* Consume the token. */
3093 cp_lexer_consume_token (parser->lexer);
3097 /* This function is called at the end of a statement or declaration.
3098 If the next token is a semicolon, it is consumed; otherwise, error
3099 recovery is attempted. */
3101 static void
3102 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3104 /* Look for the trailing `;'. */
3105 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3107 /* If there is additional (erroneous) input, skip to the end of
3108 the statement. */
3109 cp_parser_skip_to_end_of_statement (parser);
3110 /* If the next token is now a `;', consume it. */
3111 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3112 cp_lexer_consume_token (parser->lexer);
3116 /* Skip tokens until we have consumed an entire block, or until we
3117 have consumed a non-nested `;'. */
3119 static void
3120 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3122 int nesting_depth = 0;
3124 while (nesting_depth >= 0)
3126 cp_token *token = cp_lexer_peek_token (parser->lexer);
3128 switch (token->type)
3130 case CPP_EOF:
3131 case CPP_PRAGMA_EOL:
3132 /* If we've run out of tokens, stop. */
3133 return;
3135 case CPP_SEMICOLON:
3136 /* Stop if this is an unnested ';'. */
3137 if (!nesting_depth)
3138 nesting_depth = -1;
3139 break;
3141 case CPP_CLOSE_BRACE:
3142 /* Stop if this is an unnested '}', or closes the outermost
3143 nesting level. */
3144 nesting_depth--;
3145 if (nesting_depth < 0)
3146 return;
3147 if (!nesting_depth)
3148 nesting_depth = -1;
3149 break;
3151 case CPP_OPEN_BRACE:
3152 /* Nest. */
3153 nesting_depth++;
3154 break;
3156 default:
3157 break;
3160 /* Consume the token. */
3161 cp_lexer_consume_token (parser->lexer);
3165 /* Skip tokens until a non-nested closing curly brace is the next
3166 token, or there are no more tokens. Return true in the first case,
3167 false otherwise. */
3169 static bool
3170 cp_parser_skip_to_closing_brace (cp_parser *parser)
3172 unsigned nesting_depth = 0;
3174 while (true)
3176 cp_token *token = cp_lexer_peek_token (parser->lexer);
3178 switch (token->type)
3180 case CPP_EOF:
3181 case CPP_PRAGMA_EOL:
3182 /* If we've run out of tokens, stop. */
3183 return false;
3185 case CPP_CLOSE_BRACE:
3186 /* If the next token is a non-nested `}', then we have reached
3187 the end of the current block. */
3188 if (nesting_depth-- == 0)
3189 return true;
3190 break;
3192 case CPP_OPEN_BRACE:
3193 /* If it the next token is a `{', then we are entering a new
3194 block. Consume the entire block. */
3195 ++nesting_depth;
3196 break;
3198 default:
3199 break;
3202 /* Consume the token. */
3203 cp_lexer_consume_token (parser->lexer);
3207 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3208 parameter is the PRAGMA token, allowing us to purge the entire pragma
3209 sequence. */
3211 static void
3212 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3214 cp_token *token;
3216 parser->lexer->in_pragma = false;
3219 token = cp_lexer_consume_token (parser->lexer);
3220 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3222 /* Ensure that the pragma is not parsed again. */
3223 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3226 /* Require pragma end of line, resyncing with it as necessary. The
3227 arguments are as for cp_parser_skip_to_pragma_eol. */
3229 static void
3230 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3232 parser->lexer->in_pragma = false;
3233 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3234 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3237 /* This is a simple wrapper around make_typename_type. When the id is
3238 an unresolved identifier node, we can provide a superior diagnostic
3239 using cp_parser_diagnose_invalid_type_name. */
3241 static tree
3242 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3243 tree id, location_t id_location)
3245 tree result;
3246 if (identifier_p (id))
3248 result = make_typename_type (scope, id, typename_type,
3249 /*complain=*/tf_none);
3250 if (result == error_mark_node)
3251 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3252 return result;
3254 return make_typename_type (scope, id, typename_type, tf_error);
3257 /* This is a wrapper around the
3258 make_{pointer,ptrmem,reference}_declarator functions that decides
3259 which one to call based on the CODE and CLASS_TYPE arguments. The
3260 CODE argument should be one of the values returned by
3261 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3262 appertain to the pointer or reference. */
3264 static cp_declarator *
3265 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3266 cp_cv_quals cv_qualifiers,
3267 cp_declarator *target,
3268 tree attributes)
3270 if (code == ERROR_MARK)
3271 return cp_error_declarator;
3273 if (code == INDIRECT_REF)
3274 if (class_type == NULL_TREE)
3275 return make_pointer_declarator (cv_qualifiers, target, attributes);
3276 else
3277 return make_ptrmem_declarator (cv_qualifiers, class_type,
3278 target, attributes);
3279 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3280 return make_reference_declarator (cv_qualifiers, target,
3281 false, attributes);
3282 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3283 return make_reference_declarator (cv_qualifiers, target,
3284 true, attributes);
3285 gcc_unreachable ();
3288 /* Create a new C++ parser. */
3290 static cp_parser *
3291 cp_parser_new (void)
3293 cp_parser *parser;
3294 cp_lexer *lexer;
3295 unsigned i;
3297 /* cp_lexer_new_main is called before doing GC allocation because
3298 cp_lexer_new_main might load a PCH file. */
3299 lexer = cp_lexer_new_main ();
3301 /* Initialize the binops_by_token so that we can get the tree
3302 directly from the token. */
3303 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3304 binops_by_token[binops[i].token_type] = binops[i];
3306 parser = ggc_alloc_cleared_cp_parser ();
3307 parser->lexer = lexer;
3308 parser->context = cp_parser_context_new (NULL);
3310 /* For now, we always accept GNU extensions. */
3311 parser->allow_gnu_extensions_p = 1;
3313 /* The `>' token is a greater-than operator, not the end of a
3314 template-id. */
3315 parser->greater_than_is_operator_p = true;
3317 parser->default_arg_ok_p = true;
3319 /* We are not parsing a constant-expression. */
3320 parser->integral_constant_expression_p = false;
3321 parser->allow_non_integral_constant_expression_p = false;
3322 parser->non_integral_constant_expression_p = false;
3324 /* Local variable names are not forbidden. */
3325 parser->local_variables_forbidden_p = false;
3327 /* We are not processing an `extern "C"' declaration. */
3328 parser->in_unbraced_linkage_specification_p = false;
3330 /* We are not processing a declarator. */
3331 parser->in_declarator_p = false;
3333 /* We are not processing a template-argument-list. */
3334 parser->in_template_argument_list_p = false;
3336 /* We are not in an iteration statement. */
3337 parser->in_statement = 0;
3339 /* We are not in a switch statement. */
3340 parser->in_switch_statement_p = false;
3342 /* We are not parsing a type-id inside an expression. */
3343 parser->in_type_id_in_expr_p = false;
3345 /* Declarations aren't implicitly extern "C". */
3346 parser->implicit_extern_c = false;
3348 /* String literals should be translated to the execution character set. */
3349 parser->translate_strings_p = true;
3351 /* We are not parsing a function body. */
3352 parser->in_function_body = false;
3354 /* We can correct until told otherwise. */
3355 parser->colon_corrects_to_scope_p = true;
3357 /* The unparsed function queue is empty. */
3358 push_unparsed_function_queues (parser);
3360 /* There are no classes being defined. */
3361 parser->num_classes_being_defined = 0;
3363 /* No template parameters apply. */
3364 parser->num_template_parameter_lists = 0;
3366 return parser;
3369 /* Create a cp_lexer structure which will emit the tokens in CACHE
3370 and push it onto the parser's lexer stack. This is used for delayed
3371 parsing of in-class method bodies and default arguments, and should
3372 not be confused with tentative parsing. */
3373 static void
3374 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3376 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3377 lexer->next = parser->lexer;
3378 parser->lexer = lexer;
3380 /* Move the current source position to that of the first token in the
3381 new lexer. */
3382 cp_lexer_set_source_position_from_token (lexer->next_token);
3385 /* Pop the top lexer off the parser stack. This is never used for the
3386 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3387 static void
3388 cp_parser_pop_lexer (cp_parser *parser)
3390 cp_lexer *lexer = parser->lexer;
3391 parser->lexer = lexer->next;
3392 cp_lexer_destroy (lexer);
3394 /* Put the current source position back where it was before this
3395 lexer was pushed. */
3396 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3399 /* Lexical conventions [gram.lex] */
3401 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3402 identifier. */
3404 static tree
3405 cp_parser_identifier (cp_parser* parser)
3407 cp_token *token;
3409 /* Look for the identifier. */
3410 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3411 /* Return the value. */
3412 return token ? token->u.value : error_mark_node;
3415 /* Parse a sequence of adjacent string constants. Returns a
3416 TREE_STRING representing the combined, nul-terminated string
3417 constant. If TRANSLATE is true, translate the string to the
3418 execution character set. If WIDE_OK is true, a wide string is
3419 invalid here.
3421 C++98 [lex.string] says that if a narrow string literal token is
3422 adjacent to a wide string literal token, the behavior is undefined.
3423 However, C99 6.4.5p4 says that this results in a wide string literal.
3424 We follow C99 here, for consistency with the C front end.
3426 This code is largely lifted from lex_string() in c-lex.c.
3428 FUTURE: ObjC++ will need to handle @-strings here. */
3429 static tree
3430 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3432 tree value;
3433 size_t count;
3434 struct obstack str_ob;
3435 cpp_string str, istr, *strs;
3436 cp_token *tok;
3437 enum cpp_ttype type, curr_type;
3438 int have_suffix_p = 0;
3439 tree string_tree;
3440 tree suffix_id = NULL_TREE;
3441 bool curr_tok_is_userdef_p = false;
3443 tok = cp_lexer_peek_token (parser->lexer);
3444 if (!cp_parser_is_string_literal (tok))
3446 cp_parser_error (parser, "expected string-literal");
3447 return error_mark_node;
3450 if (cpp_userdef_string_p (tok->type))
3452 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3453 curr_type = cpp_userdef_string_remove_type (tok->type);
3454 curr_tok_is_userdef_p = true;
3456 else
3458 string_tree = tok->u.value;
3459 curr_type = tok->type;
3461 type = curr_type;
3463 /* Try to avoid the overhead of creating and destroying an obstack
3464 for the common case of just one string. */
3465 if (!cp_parser_is_string_literal
3466 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3468 cp_lexer_consume_token (parser->lexer);
3470 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3471 str.len = TREE_STRING_LENGTH (string_tree);
3472 count = 1;
3474 if (curr_tok_is_userdef_p)
3476 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3477 have_suffix_p = 1;
3478 curr_type = cpp_userdef_string_remove_type (tok->type);
3480 else
3481 curr_type = tok->type;
3483 strs = &str;
3485 else
3487 gcc_obstack_init (&str_ob);
3488 count = 0;
3492 cp_lexer_consume_token (parser->lexer);
3493 count++;
3494 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3495 str.len = TREE_STRING_LENGTH (string_tree);
3497 if (curr_tok_is_userdef_p)
3499 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3500 if (have_suffix_p == 0)
3502 suffix_id = curr_suffix_id;
3503 have_suffix_p = 1;
3505 else if (have_suffix_p == 1
3506 && curr_suffix_id != suffix_id)
3508 error ("inconsistent user-defined literal suffixes"
3509 " %qD and %qD in string literal",
3510 suffix_id, curr_suffix_id);
3511 have_suffix_p = -1;
3513 curr_type = cpp_userdef_string_remove_type (tok->type);
3515 else
3516 curr_type = tok->type;
3518 if (type != curr_type)
3520 if (type == CPP_STRING)
3521 type = curr_type;
3522 else if (curr_type != CPP_STRING)
3523 error_at (tok->location,
3524 "unsupported non-standard concatenation "
3525 "of string literals");
3528 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3530 tok = cp_lexer_peek_token (parser->lexer);
3531 if (cpp_userdef_string_p (tok->type))
3533 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3534 curr_type = cpp_userdef_string_remove_type (tok->type);
3535 curr_tok_is_userdef_p = true;
3537 else
3539 string_tree = tok->u.value;
3540 curr_type = tok->type;
3541 curr_tok_is_userdef_p = false;
3544 while (cp_parser_is_string_literal (tok));
3546 strs = (cpp_string *) obstack_finish (&str_ob);
3549 if (type != CPP_STRING && !wide_ok)
3551 cp_parser_error (parser, "a wide string is invalid in this context");
3552 type = CPP_STRING;
3555 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3556 (parse_in, strs, count, &istr, type))
3558 value = build_string (istr.len, (const char *)istr.text);
3559 free (CONST_CAST (unsigned char *, istr.text));
3561 switch (type)
3563 default:
3564 case CPP_STRING:
3565 case CPP_UTF8STRING:
3566 TREE_TYPE (value) = char_array_type_node;
3567 break;
3568 case CPP_STRING16:
3569 TREE_TYPE (value) = char16_array_type_node;
3570 break;
3571 case CPP_STRING32:
3572 TREE_TYPE (value) = char32_array_type_node;
3573 break;
3574 case CPP_WSTRING:
3575 TREE_TYPE (value) = wchar_array_type_node;
3576 break;
3579 value = fix_string_type (value);
3581 if (have_suffix_p)
3583 tree literal = build_userdef_literal (suffix_id, value,
3584 OT_NONE, NULL_TREE);
3585 tok->u.value = literal;
3586 return cp_parser_userdef_string_literal (tok);
3589 else
3590 /* cpp_interpret_string has issued an error. */
3591 value = error_mark_node;
3593 if (count > 1)
3594 obstack_free (&str_ob, 0);
3596 return value;
3599 /* Look up a literal operator with the name and the exact arguments. */
3601 static tree
3602 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3604 tree decl, fns;
3605 decl = lookup_name (name);
3606 if (!decl || !is_overloaded_fn (decl))
3607 return error_mark_node;
3609 for (fns = decl; fns; fns = OVL_NEXT (fns))
3611 unsigned int ix;
3612 bool found = true;
3613 tree fn = OVL_CURRENT (fns);
3614 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3615 if (parmtypes != NULL_TREE)
3617 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3618 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3620 tree tparm = TREE_VALUE (parmtypes);
3621 tree targ = TREE_TYPE ((*args)[ix]);
3622 bool ptr = TYPE_PTR_P (tparm);
3623 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3624 if ((ptr || arr || !same_type_p (tparm, targ))
3625 && (!ptr || !arr
3626 || !same_type_p (TREE_TYPE (tparm),
3627 TREE_TYPE (targ))))
3628 found = false;
3630 if (found
3631 && ix == vec_safe_length (args)
3632 /* May be this should be sufficient_parms_p instead,
3633 depending on how exactly should user-defined literals
3634 work in presence of default arguments on the literal
3635 operator parameters. */
3636 && parmtypes == void_list_node)
3637 return fn;
3641 return error_mark_node;
3644 /* Parse a user-defined char constant. Returns a call to a user-defined
3645 literal operator taking the character as an argument. */
3647 static tree
3648 cp_parser_userdef_char_literal (cp_parser *parser)
3650 cp_token *token = cp_lexer_consume_token (parser->lexer);
3651 tree literal = token->u.value;
3652 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3653 tree value = USERDEF_LITERAL_VALUE (literal);
3654 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3655 tree decl, result;
3657 /* Build up a call to the user-defined operator */
3658 /* Lookup the name we got back from the id-expression. */
3659 vec<tree, va_gc> *args = make_tree_vector ();
3660 vec_safe_push (args, value);
3661 decl = lookup_literal_operator (name, args);
3662 if (!decl || decl == error_mark_node)
3664 error ("unable to find character literal operator %qD with %qT argument",
3665 name, TREE_TYPE (value));
3666 release_tree_vector (args);
3667 return error_mark_node;
3669 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3670 release_tree_vector (args);
3671 if (result != error_mark_node)
3672 return result;
3674 error ("unable to find character literal operator %qD with %qT argument",
3675 name, TREE_TYPE (value));
3676 return error_mark_node;
3679 /* A subroutine of cp_parser_userdef_numeric_literal to
3680 create a char... template parameter pack from a string node. */
3682 static tree
3683 make_char_string_pack (tree value)
3685 tree charvec;
3686 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3687 const char *str = TREE_STRING_POINTER (value);
3688 int i, len = TREE_STRING_LENGTH (value) - 1;
3689 tree argvec = make_tree_vec (1);
3691 /* Fill in CHARVEC with all of the parameters. */
3692 charvec = make_tree_vec (len);
3693 for (i = 0; i < len; ++i)
3694 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3696 /* Build the argument packs. */
3697 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3698 TREE_TYPE (argpack) = char_type_node;
3700 TREE_VEC_ELT (argvec, 0) = argpack;
3702 return argvec;
3705 /* Parse a user-defined numeric constant. returns a call to a user-defined
3706 literal operator. */
3708 static tree
3709 cp_parser_userdef_numeric_literal (cp_parser *parser)
3711 cp_token *token = cp_lexer_consume_token (parser->lexer);
3712 tree literal = token->u.value;
3713 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3714 tree value = USERDEF_LITERAL_VALUE (literal);
3715 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3716 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3717 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3718 tree decl, result;
3719 vec<tree, va_gc> *args;
3721 /* Look for a literal operator taking the exact type of numeric argument
3722 as the literal value. */
3723 args = make_tree_vector ();
3724 vec_safe_push (args, value);
3725 decl = lookup_literal_operator (name, args);
3726 if (decl && decl != error_mark_node)
3728 result = finish_call_expr (decl, &args, false, true, tf_none);
3729 if (result != error_mark_node)
3731 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3732 warning_at (token->location, OPT_Woverflow,
3733 "integer literal exceeds range of %qT type",
3734 long_long_unsigned_type_node);
3735 else
3737 if (overflow > 0)
3738 warning_at (token->location, OPT_Woverflow,
3739 "floating literal exceeds range of %qT type",
3740 long_double_type_node);
3741 else if (overflow < 0)
3742 warning_at (token->location, OPT_Woverflow,
3743 "floating literal truncated to zero");
3745 release_tree_vector (args);
3746 return result;
3749 release_tree_vector (args);
3751 /* If the numeric argument didn't work, look for a raw literal
3752 operator taking a const char* argument consisting of the number
3753 in string format. */
3754 args = make_tree_vector ();
3755 vec_safe_push (args, num_string);
3756 decl = lookup_literal_operator (name, args);
3757 if (decl && decl != error_mark_node)
3759 result = finish_call_expr (decl, &args, false, true, tf_none);
3760 if (result != error_mark_node)
3762 release_tree_vector (args);
3763 return result;
3766 release_tree_vector (args);
3768 /* If the raw literal didn't work, look for a non-type template
3769 function with parameter pack char.... Call the function with
3770 template parameter characters representing the number. */
3771 args = make_tree_vector ();
3772 decl = lookup_literal_operator (name, args);
3773 if (decl && decl != error_mark_node)
3775 tree tmpl_args = make_char_string_pack (num_string);
3776 decl = lookup_template_function (decl, tmpl_args);
3777 result = finish_call_expr (decl, &args, false, true, tf_none);
3778 if (result != error_mark_node)
3780 release_tree_vector (args);
3781 return result;
3784 release_tree_vector (args);
3786 error ("unable to find numeric literal operator %qD", name);
3787 return error_mark_node;
3790 /* Parse a user-defined string constant. Returns a call to a user-defined
3791 literal operator taking a character pointer and the length of the string
3792 as arguments. */
3794 static tree
3795 cp_parser_userdef_string_literal (cp_token *token)
3797 tree literal = token->u.value;
3798 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3799 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3800 tree value = USERDEF_LITERAL_VALUE (literal);
3801 int len = TREE_STRING_LENGTH (value)
3802 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3803 tree decl, result;
3805 /* Build up a call to the user-defined operator */
3806 /* Lookup the name we got back from the id-expression. */
3807 vec<tree, va_gc> *args = make_tree_vector ();
3808 vec_safe_push (args, value);
3809 vec_safe_push (args, build_int_cst (size_type_node, len));
3810 decl = lookup_name (name);
3811 if (!decl || decl == error_mark_node)
3813 error ("unable to find string literal operator %qD", name);
3814 release_tree_vector (args);
3815 return error_mark_node;
3817 result = finish_call_expr (decl, &args, false, true, tf_none);
3818 release_tree_vector (args);
3819 if (result != error_mark_node)
3820 return result;
3822 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3823 name, TREE_TYPE (value), size_type_node);
3824 return error_mark_node;
3828 /* Basic concepts [gram.basic] */
3830 /* Parse a translation-unit.
3832 translation-unit:
3833 declaration-seq [opt]
3835 Returns TRUE if all went well. */
3837 static bool
3838 cp_parser_translation_unit (cp_parser* parser)
3840 /* The address of the first non-permanent object on the declarator
3841 obstack. */
3842 static void *declarator_obstack_base;
3844 bool success;
3846 /* Create the declarator obstack, if necessary. */
3847 if (!cp_error_declarator)
3849 gcc_obstack_init (&declarator_obstack);
3850 /* Create the error declarator. */
3851 cp_error_declarator = make_declarator (cdk_error);
3852 /* Create the empty parameter list. */
3853 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3854 /* Remember where the base of the declarator obstack lies. */
3855 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3858 cp_parser_declaration_seq_opt (parser);
3860 /* If there are no tokens left then all went well. */
3861 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3863 /* Get rid of the token array; we don't need it any more. */
3864 cp_lexer_destroy (parser->lexer);
3865 parser->lexer = NULL;
3867 /* This file might have been a context that's implicitly extern
3868 "C". If so, pop the lang context. (Only relevant for PCH.) */
3869 if (parser->implicit_extern_c)
3871 pop_lang_context ();
3872 parser->implicit_extern_c = false;
3875 /* Finish up. */
3876 finish_translation_unit ();
3878 success = true;
3880 else
3882 cp_parser_error (parser, "expected declaration");
3883 success = false;
3886 /* Make sure the declarator obstack was fully cleaned up. */
3887 gcc_assert (obstack_next_free (&declarator_obstack)
3888 == declarator_obstack_base);
3890 /* All went well. */
3891 return success;
3894 /* Expressions [gram.expr] */
3896 /* Parse a primary-expression.
3898 primary-expression:
3899 literal
3900 this
3901 ( expression )
3902 id-expression
3904 GNU Extensions:
3906 primary-expression:
3907 ( compound-statement )
3908 __builtin_va_arg ( assignment-expression , type-id )
3909 __builtin_offsetof ( type-id , offsetof-expression )
3911 C++ Extensions:
3912 __has_nothrow_assign ( type-id )
3913 __has_nothrow_constructor ( type-id )
3914 __has_nothrow_copy ( type-id )
3915 __has_trivial_assign ( type-id )
3916 __has_trivial_constructor ( type-id )
3917 __has_trivial_copy ( type-id )
3918 __has_trivial_destructor ( type-id )
3919 __has_virtual_destructor ( type-id )
3920 __is_abstract ( type-id )
3921 __is_base_of ( type-id , type-id )
3922 __is_class ( type-id )
3923 __is_convertible_to ( type-id , type-id )
3924 __is_empty ( type-id )
3925 __is_enum ( type-id )
3926 __is_final ( type-id )
3927 __is_literal_type ( type-id )
3928 __is_pod ( type-id )
3929 __is_polymorphic ( type-id )
3930 __is_std_layout ( type-id )
3931 __is_trivial ( type-id )
3932 __is_union ( type-id )
3934 Objective-C++ Extension:
3936 primary-expression:
3937 objc-expression
3939 literal:
3940 __null
3942 ADDRESS_P is true iff this expression was immediately preceded by
3943 "&" and therefore might denote a pointer-to-member. CAST_P is true
3944 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3945 true iff this expression is a template argument.
3947 Returns a representation of the expression. Upon return, *IDK
3948 indicates what kind of id-expression (if any) was present. */
3950 static tree
3951 cp_parser_primary_expression (cp_parser *parser,
3952 bool address_p,
3953 bool cast_p,
3954 bool template_arg_p,
3955 bool decltype_p,
3956 cp_id_kind *idk)
3958 cp_token *token = NULL;
3960 /* Assume the primary expression is not an id-expression. */
3961 *idk = CP_ID_KIND_NONE;
3963 /* Peek at the next token. */
3964 token = cp_lexer_peek_token (parser->lexer);
3965 switch (token->type)
3967 /* literal:
3968 integer-literal
3969 character-literal
3970 floating-literal
3971 string-literal
3972 boolean-literal
3973 pointer-literal
3974 user-defined-literal */
3975 case CPP_CHAR:
3976 case CPP_CHAR16:
3977 case CPP_CHAR32:
3978 case CPP_WCHAR:
3979 case CPP_NUMBER:
3980 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3981 return cp_parser_userdef_numeric_literal (parser);
3982 token = cp_lexer_consume_token (parser->lexer);
3983 if (TREE_CODE (token->u.value) == FIXED_CST)
3985 error_at (token->location,
3986 "fixed-point types not supported in C++");
3987 return error_mark_node;
3989 /* Floating-point literals are only allowed in an integral
3990 constant expression if they are cast to an integral or
3991 enumeration type. */
3992 if (TREE_CODE (token->u.value) == REAL_CST
3993 && parser->integral_constant_expression_p
3994 && pedantic)
3996 /* CAST_P will be set even in invalid code like "int(2.7 +
3997 ...)". Therefore, we have to check that the next token
3998 is sure to end the cast. */
3999 if (cast_p)
4001 cp_token *next_token;
4003 next_token = cp_lexer_peek_token (parser->lexer);
4004 if (/* The comma at the end of an
4005 enumerator-definition. */
4006 next_token->type != CPP_COMMA
4007 /* The curly brace at the end of an enum-specifier. */
4008 && next_token->type != CPP_CLOSE_BRACE
4009 /* The end of a statement. */
4010 && next_token->type != CPP_SEMICOLON
4011 /* The end of the cast-expression. */
4012 && next_token->type != CPP_CLOSE_PAREN
4013 /* The end of an array bound. */
4014 && next_token->type != CPP_CLOSE_SQUARE
4015 /* The closing ">" in a template-argument-list. */
4016 && (next_token->type != CPP_GREATER
4017 || parser->greater_than_is_operator_p)
4018 /* C++0x only: A ">>" treated like two ">" tokens,
4019 in a template-argument-list. */
4020 && (next_token->type != CPP_RSHIFT
4021 || (cxx_dialect == cxx98)
4022 || parser->greater_than_is_operator_p))
4023 cast_p = false;
4026 /* If we are within a cast, then the constraint that the
4027 cast is to an integral or enumeration type will be
4028 checked at that point. If we are not within a cast, then
4029 this code is invalid. */
4030 if (!cast_p)
4031 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4033 return token->u.value;
4035 case CPP_CHAR_USERDEF:
4036 case CPP_CHAR16_USERDEF:
4037 case CPP_CHAR32_USERDEF:
4038 case CPP_WCHAR_USERDEF:
4039 return cp_parser_userdef_char_literal (parser);
4041 case CPP_STRING:
4042 case CPP_STRING16:
4043 case CPP_STRING32:
4044 case CPP_WSTRING:
4045 case CPP_UTF8STRING:
4046 case CPP_STRING_USERDEF:
4047 case CPP_STRING16_USERDEF:
4048 case CPP_STRING32_USERDEF:
4049 case CPP_WSTRING_USERDEF:
4050 case CPP_UTF8STRING_USERDEF:
4051 /* ??? Should wide strings be allowed when parser->translate_strings_p
4052 is false (i.e. in attributes)? If not, we can kill the third
4053 argument to cp_parser_string_literal. */
4054 return cp_parser_string_literal (parser,
4055 parser->translate_strings_p,
4056 true);
4058 case CPP_OPEN_PAREN:
4060 tree expr;
4061 bool saved_greater_than_is_operator_p;
4063 /* Consume the `('. */
4064 cp_lexer_consume_token (parser->lexer);
4065 /* Within a parenthesized expression, a `>' token is always
4066 the greater-than operator. */
4067 saved_greater_than_is_operator_p
4068 = parser->greater_than_is_operator_p;
4069 parser->greater_than_is_operator_p = true;
4070 /* If we see `( { ' then we are looking at the beginning of
4071 a GNU statement-expression. */
4072 if (cp_parser_allow_gnu_extensions_p (parser)
4073 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4075 /* Statement-expressions are not allowed by the standard. */
4076 pedwarn (token->location, OPT_Wpedantic,
4077 "ISO C++ forbids braced-groups within expressions");
4079 /* And they're not allowed outside of a function-body; you
4080 cannot, for example, write:
4082 int i = ({ int j = 3; j + 1; });
4084 at class or namespace scope. */
4085 if (!parser->in_function_body
4086 || parser->in_template_argument_list_p)
4088 error_at (token->location,
4089 "statement-expressions are not allowed outside "
4090 "functions nor in template-argument lists");
4091 cp_parser_skip_to_end_of_block_or_statement (parser);
4092 expr = error_mark_node;
4094 else
4096 /* Start the statement-expression. */
4097 expr = begin_stmt_expr ();
4098 /* Parse the compound-statement. */
4099 cp_parser_compound_statement (parser, expr, false, false);
4100 /* Finish up. */
4101 expr = finish_stmt_expr (expr, false);
4104 else
4106 /* Parse the parenthesized expression. */
4107 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4108 /* Let the front end know that this expression was
4109 enclosed in parentheses. This matters in case, for
4110 example, the expression is of the form `A::B', since
4111 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4112 not. */
4113 expr = finish_parenthesized_expr (expr);
4114 /* DR 705: Wrapping an unqualified name in parentheses
4115 suppresses arg-dependent lookup. We want to pass back
4116 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4117 (c++/37862), but none of the others. */
4118 if (*idk != CP_ID_KIND_QUALIFIED)
4119 *idk = CP_ID_KIND_NONE;
4121 /* The `>' token might be the end of a template-id or
4122 template-parameter-list now. */
4123 parser->greater_than_is_operator_p
4124 = saved_greater_than_is_operator_p;
4125 /* Consume the `)'. */
4126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4127 cp_parser_skip_to_end_of_statement (parser);
4129 return expr;
4132 case CPP_OPEN_SQUARE:
4133 if (c_dialect_objc ())
4134 /* We have an Objective-C++ message. */
4135 return cp_parser_objc_expression (parser);
4137 tree lam = cp_parser_lambda_expression (parser);
4138 /* Don't warn about a failed tentative parse. */
4139 if (cp_parser_error_occurred (parser))
4140 return error_mark_node;
4141 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4142 return lam;
4145 case CPP_OBJC_STRING:
4146 if (c_dialect_objc ())
4147 /* We have an Objective-C++ string literal. */
4148 return cp_parser_objc_expression (parser);
4149 cp_parser_error (parser, "expected primary-expression");
4150 return error_mark_node;
4152 case CPP_KEYWORD:
4153 switch (token->keyword)
4155 /* These two are the boolean literals. */
4156 case RID_TRUE:
4157 cp_lexer_consume_token (parser->lexer);
4158 return boolean_true_node;
4159 case RID_FALSE:
4160 cp_lexer_consume_token (parser->lexer);
4161 return boolean_false_node;
4163 /* The `__null' literal. */
4164 case RID_NULL:
4165 cp_lexer_consume_token (parser->lexer);
4166 return null_node;
4168 /* The `nullptr' literal. */
4169 case RID_NULLPTR:
4170 cp_lexer_consume_token (parser->lexer);
4171 return nullptr_node;
4173 /* Recognize the `this' keyword. */
4174 case RID_THIS:
4175 cp_lexer_consume_token (parser->lexer);
4176 if (parser->local_variables_forbidden_p)
4178 error_at (token->location,
4179 "%<this%> may not be used in this context");
4180 return error_mark_node;
4182 /* Pointers cannot appear in constant-expressions. */
4183 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4184 return error_mark_node;
4185 return finish_this_expr ();
4187 /* The `operator' keyword can be the beginning of an
4188 id-expression. */
4189 case RID_OPERATOR:
4190 goto id_expression;
4192 case RID_FUNCTION_NAME:
4193 case RID_PRETTY_FUNCTION_NAME:
4194 case RID_C99_FUNCTION_NAME:
4196 non_integral_constant name;
4198 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4199 __func__ are the names of variables -- but they are
4200 treated specially. Therefore, they are handled here,
4201 rather than relying on the generic id-expression logic
4202 below. Grammatically, these names are id-expressions.
4204 Consume the token. */
4205 token = cp_lexer_consume_token (parser->lexer);
4207 switch (token->keyword)
4209 case RID_FUNCTION_NAME:
4210 name = NIC_FUNC_NAME;
4211 break;
4212 case RID_PRETTY_FUNCTION_NAME:
4213 name = NIC_PRETTY_FUNC;
4214 break;
4215 case RID_C99_FUNCTION_NAME:
4216 name = NIC_C99_FUNC;
4217 break;
4218 default:
4219 gcc_unreachable ();
4222 if (cp_parser_non_integral_constant_expression (parser, name))
4223 return error_mark_node;
4225 /* Look up the name. */
4226 return finish_fname (token->u.value);
4229 case RID_VA_ARG:
4231 tree expression;
4232 tree type;
4233 source_location type_location;
4235 /* The `__builtin_va_arg' construct is used to handle
4236 `va_arg'. Consume the `__builtin_va_arg' token. */
4237 cp_lexer_consume_token (parser->lexer);
4238 /* Look for the opening `('. */
4239 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4240 /* Now, parse the assignment-expression. */
4241 expression = cp_parser_assignment_expression (parser,
4242 /*cast_p=*/false, NULL);
4243 /* Look for the `,'. */
4244 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4245 type_location = cp_lexer_peek_token (parser->lexer)->location;
4246 /* Parse the type-id. */
4247 type = cp_parser_type_id (parser);
4248 /* Look for the closing `)'. */
4249 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4250 /* Using `va_arg' in a constant-expression is not
4251 allowed. */
4252 if (cp_parser_non_integral_constant_expression (parser,
4253 NIC_VA_ARG))
4254 return error_mark_node;
4255 return build_x_va_arg (type_location, expression, type);
4258 case RID_OFFSETOF:
4259 return cp_parser_builtin_offsetof (parser);
4261 case RID_HAS_NOTHROW_ASSIGN:
4262 case RID_HAS_NOTHROW_CONSTRUCTOR:
4263 case RID_HAS_NOTHROW_COPY:
4264 case RID_HAS_TRIVIAL_ASSIGN:
4265 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4266 case RID_HAS_TRIVIAL_COPY:
4267 case RID_HAS_TRIVIAL_DESTRUCTOR:
4268 case RID_HAS_VIRTUAL_DESTRUCTOR:
4269 case RID_IS_ABSTRACT:
4270 case RID_IS_BASE_OF:
4271 case RID_IS_CLASS:
4272 case RID_IS_CONVERTIBLE_TO:
4273 case RID_IS_EMPTY:
4274 case RID_IS_ENUM:
4275 case RID_IS_FINAL:
4276 case RID_IS_LITERAL_TYPE:
4277 case RID_IS_POD:
4278 case RID_IS_POLYMORPHIC:
4279 case RID_IS_STD_LAYOUT:
4280 case RID_IS_TRIVIAL:
4281 case RID_IS_UNION:
4282 return cp_parser_trait_expr (parser, token->keyword);
4284 /* Objective-C++ expressions. */
4285 case RID_AT_ENCODE:
4286 case RID_AT_PROTOCOL:
4287 case RID_AT_SELECTOR:
4288 return cp_parser_objc_expression (parser);
4290 case RID_TEMPLATE:
4291 if (parser->in_function_body
4292 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4293 == CPP_LESS))
4295 error_at (token->location,
4296 "a template declaration cannot appear at block scope");
4297 cp_parser_skip_to_end_of_block_or_statement (parser);
4298 return error_mark_node;
4300 default:
4301 cp_parser_error (parser, "expected primary-expression");
4302 return error_mark_node;
4305 /* An id-expression can start with either an identifier, a
4306 `::' as the beginning of a qualified-id, or the "operator"
4307 keyword. */
4308 case CPP_NAME:
4309 case CPP_SCOPE:
4310 case CPP_TEMPLATE_ID:
4311 case CPP_NESTED_NAME_SPECIFIER:
4313 tree id_expression;
4314 tree decl;
4315 const char *error_msg;
4316 bool template_p;
4317 bool done;
4318 cp_token *id_expr_token;
4320 id_expression:
4321 /* Parse the id-expression. */
4322 id_expression
4323 = cp_parser_id_expression (parser,
4324 /*template_keyword_p=*/false,
4325 /*check_dependency_p=*/true,
4326 &template_p,
4327 /*declarator_p=*/false,
4328 /*optional_p=*/false);
4329 if (id_expression == error_mark_node)
4330 return error_mark_node;
4331 id_expr_token = token;
4332 token = cp_lexer_peek_token (parser->lexer);
4333 done = (token->type != CPP_OPEN_SQUARE
4334 && token->type != CPP_OPEN_PAREN
4335 && token->type != CPP_DOT
4336 && token->type != CPP_DEREF
4337 && token->type != CPP_PLUS_PLUS
4338 && token->type != CPP_MINUS_MINUS);
4339 /* If we have a template-id, then no further lookup is
4340 required. If the template-id was for a template-class, we
4341 will sometimes have a TYPE_DECL at this point. */
4342 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4343 || TREE_CODE (id_expression) == TYPE_DECL)
4344 decl = id_expression;
4345 /* Look up the name. */
4346 else
4348 tree ambiguous_decls;
4350 /* If we already know that this lookup is ambiguous, then
4351 we've already issued an error message; there's no reason
4352 to check again. */
4353 if (id_expr_token->type == CPP_NAME
4354 && id_expr_token->ambiguous_p)
4356 cp_parser_simulate_error (parser);
4357 return error_mark_node;
4360 decl = cp_parser_lookup_name (parser, id_expression,
4361 none_type,
4362 template_p,
4363 /*is_namespace=*/false,
4364 /*check_dependency=*/true,
4365 &ambiguous_decls,
4366 id_expr_token->location);
4367 /* If the lookup was ambiguous, an error will already have
4368 been issued. */
4369 if (ambiguous_decls)
4370 return error_mark_node;
4372 /* In Objective-C++, we may have an Objective-C 2.0
4373 dot-syntax for classes here. */
4374 if (c_dialect_objc ()
4375 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4376 && TREE_CODE (decl) == TYPE_DECL
4377 && objc_is_class_name (decl))
4379 tree component;
4380 cp_lexer_consume_token (parser->lexer);
4381 component = cp_parser_identifier (parser);
4382 if (component == error_mark_node)
4383 return error_mark_node;
4385 return objc_build_class_component_ref (id_expression, component);
4388 /* In Objective-C++, an instance variable (ivar) may be preferred
4389 to whatever cp_parser_lookup_name() found. */
4390 decl = objc_lookup_ivar (decl, id_expression);
4392 /* If name lookup gives us a SCOPE_REF, then the
4393 qualifying scope was dependent. */
4394 if (TREE_CODE (decl) == SCOPE_REF)
4396 /* At this point, we do not know if DECL is a valid
4397 integral constant expression. We assume that it is
4398 in fact such an expression, so that code like:
4400 template <int N> struct A {
4401 int a[B<N>::i];
4404 is accepted. At template-instantiation time, we
4405 will check that B<N>::i is actually a constant. */
4406 return decl;
4408 /* Check to see if DECL is a local variable in a context
4409 where that is forbidden. */
4410 if (parser->local_variables_forbidden_p
4411 && local_variable_p (decl))
4413 /* It might be that we only found DECL because we are
4414 trying to be generous with pre-ISO scoping rules.
4415 For example, consider:
4417 int i;
4418 void g() {
4419 for (int i = 0; i < 10; ++i) {}
4420 extern void f(int j = i);
4423 Here, name look up will originally find the out
4424 of scope `i'. We need to issue a warning message,
4425 but then use the global `i'. */
4426 decl = check_for_out_of_scope_variable (decl);
4427 if (local_variable_p (decl))
4429 error_at (id_expr_token->location,
4430 "local variable %qD may not appear in this context",
4431 decl);
4432 return error_mark_node;
4437 decl = (finish_id_expression
4438 (id_expression, decl, parser->scope,
4439 idk,
4440 parser->integral_constant_expression_p,
4441 parser->allow_non_integral_constant_expression_p,
4442 &parser->non_integral_constant_expression_p,
4443 template_p, done, address_p,
4444 template_arg_p,
4445 &error_msg,
4446 id_expr_token->location));
4447 if (error_msg)
4448 cp_parser_error (parser, error_msg);
4449 return decl;
4452 /* Anything else is an error. */
4453 default:
4454 cp_parser_error (parser, "expected primary-expression");
4455 return error_mark_node;
4459 static inline tree
4460 cp_parser_primary_expression (cp_parser *parser,
4461 bool address_p,
4462 bool cast_p,
4463 bool template_arg_p,
4464 cp_id_kind *idk)
4466 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4467 /*decltype*/false, idk);
4470 /* Parse an id-expression.
4472 id-expression:
4473 unqualified-id
4474 qualified-id
4476 qualified-id:
4477 :: [opt] nested-name-specifier template [opt] unqualified-id
4478 :: identifier
4479 :: operator-function-id
4480 :: template-id
4482 Return a representation of the unqualified portion of the
4483 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4484 a `::' or nested-name-specifier.
4486 Often, if the id-expression was a qualified-id, the caller will
4487 want to make a SCOPE_REF to represent the qualified-id. This
4488 function does not do this in order to avoid wastefully creating
4489 SCOPE_REFs when they are not required.
4491 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4492 `template' keyword.
4494 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4495 uninstantiated templates.
4497 If *TEMPLATE_P is non-NULL, it is set to true iff the
4498 `template' keyword is used to explicitly indicate that the entity
4499 named is a template.
4501 If DECLARATOR_P is true, the id-expression is appearing as part of
4502 a declarator, rather than as part of an expression. */
4504 static tree
4505 cp_parser_id_expression (cp_parser *parser,
4506 bool template_keyword_p,
4507 bool check_dependency_p,
4508 bool *template_p,
4509 bool declarator_p,
4510 bool optional_p)
4512 bool global_scope_p;
4513 bool nested_name_specifier_p;
4515 /* Assume the `template' keyword was not used. */
4516 if (template_p)
4517 *template_p = template_keyword_p;
4519 /* Look for the optional `::' operator. */
4520 global_scope_p
4521 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4522 != NULL_TREE);
4523 /* Look for the optional nested-name-specifier. */
4524 nested_name_specifier_p
4525 = (cp_parser_nested_name_specifier_opt (parser,
4526 /*typename_keyword_p=*/false,
4527 check_dependency_p,
4528 /*type_p=*/false,
4529 declarator_p)
4530 != NULL_TREE);
4531 /* If there is a nested-name-specifier, then we are looking at
4532 the first qualified-id production. */
4533 if (nested_name_specifier_p)
4535 tree saved_scope;
4536 tree saved_object_scope;
4537 tree saved_qualifying_scope;
4538 tree unqualified_id;
4539 bool is_template;
4541 /* See if the next token is the `template' keyword. */
4542 if (!template_p)
4543 template_p = &is_template;
4544 *template_p = cp_parser_optional_template_keyword (parser);
4545 /* Name lookup we do during the processing of the
4546 unqualified-id might obliterate SCOPE. */
4547 saved_scope = parser->scope;
4548 saved_object_scope = parser->object_scope;
4549 saved_qualifying_scope = parser->qualifying_scope;
4550 /* Process the final unqualified-id. */
4551 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4552 check_dependency_p,
4553 declarator_p,
4554 /*optional_p=*/false);
4555 /* Restore the SAVED_SCOPE for our caller. */
4556 parser->scope = saved_scope;
4557 parser->object_scope = saved_object_scope;
4558 parser->qualifying_scope = saved_qualifying_scope;
4560 return unqualified_id;
4562 /* Otherwise, if we are in global scope, then we are looking at one
4563 of the other qualified-id productions. */
4564 else if (global_scope_p)
4566 cp_token *token;
4567 tree id;
4569 /* Peek at the next token. */
4570 token = cp_lexer_peek_token (parser->lexer);
4572 /* If it's an identifier, and the next token is not a "<", then
4573 we can avoid the template-id case. This is an optimization
4574 for this common case. */
4575 if (token->type == CPP_NAME
4576 && !cp_parser_nth_token_starts_template_argument_list_p
4577 (parser, 2))
4578 return cp_parser_identifier (parser);
4580 cp_parser_parse_tentatively (parser);
4581 /* Try a template-id. */
4582 id = cp_parser_template_id (parser,
4583 /*template_keyword_p=*/false,
4584 /*check_dependency_p=*/true,
4585 none_type,
4586 declarator_p);
4587 /* If that worked, we're done. */
4588 if (cp_parser_parse_definitely (parser))
4589 return id;
4591 /* Peek at the next token. (Changes in the token buffer may
4592 have invalidated the pointer obtained above.) */
4593 token = cp_lexer_peek_token (parser->lexer);
4595 switch (token->type)
4597 case CPP_NAME:
4598 return cp_parser_identifier (parser);
4600 case CPP_KEYWORD:
4601 if (token->keyword == RID_OPERATOR)
4602 return cp_parser_operator_function_id (parser);
4603 /* Fall through. */
4605 default:
4606 cp_parser_error (parser, "expected id-expression");
4607 return error_mark_node;
4610 else
4611 return cp_parser_unqualified_id (parser, template_keyword_p,
4612 /*check_dependency_p=*/true,
4613 declarator_p,
4614 optional_p);
4617 /* Parse an unqualified-id.
4619 unqualified-id:
4620 identifier
4621 operator-function-id
4622 conversion-function-id
4623 ~ class-name
4624 template-id
4626 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4627 keyword, in a construct like `A::template ...'.
4629 Returns a representation of unqualified-id. For the `identifier'
4630 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4631 production a BIT_NOT_EXPR is returned; the operand of the
4632 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4633 other productions, see the documentation accompanying the
4634 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4635 names are looked up in uninstantiated templates. If DECLARATOR_P
4636 is true, the unqualified-id is appearing as part of a declarator,
4637 rather than as part of an expression. */
4639 static tree
4640 cp_parser_unqualified_id (cp_parser* parser,
4641 bool template_keyword_p,
4642 bool check_dependency_p,
4643 bool declarator_p,
4644 bool optional_p)
4646 cp_token *token;
4648 /* Peek at the next token. */
4649 token = cp_lexer_peek_token (parser->lexer);
4651 switch (token->type)
4653 case CPP_NAME:
4655 tree id;
4657 /* We don't know yet whether or not this will be a
4658 template-id. */
4659 cp_parser_parse_tentatively (parser);
4660 /* Try a template-id. */
4661 id = cp_parser_template_id (parser, template_keyword_p,
4662 check_dependency_p,
4663 none_type,
4664 declarator_p);
4665 /* If it worked, we're done. */
4666 if (cp_parser_parse_definitely (parser))
4667 return id;
4668 /* Otherwise, it's an ordinary identifier. */
4669 return cp_parser_identifier (parser);
4672 case CPP_TEMPLATE_ID:
4673 return cp_parser_template_id (parser, template_keyword_p,
4674 check_dependency_p,
4675 none_type,
4676 declarator_p);
4678 case CPP_COMPL:
4680 tree type_decl;
4681 tree qualifying_scope;
4682 tree object_scope;
4683 tree scope;
4684 bool done;
4686 /* Consume the `~' token. */
4687 cp_lexer_consume_token (parser->lexer);
4688 /* Parse the class-name. The standard, as written, seems to
4689 say that:
4691 template <typename T> struct S { ~S (); };
4692 template <typename T> S<T>::~S() {}
4694 is invalid, since `~' must be followed by a class-name, but
4695 `S<T>' is dependent, and so not known to be a class.
4696 That's not right; we need to look in uninstantiated
4697 templates. A further complication arises from:
4699 template <typename T> void f(T t) {
4700 t.T::~T();
4703 Here, it is not possible to look up `T' in the scope of `T'
4704 itself. We must look in both the current scope, and the
4705 scope of the containing complete expression.
4707 Yet another issue is:
4709 struct S {
4710 int S;
4711 ~S();
4714 S::~S() {}
4716 The standard does not seem to say that the `S' in `~S'
4717 should refer to the type `S' and not the data member
4718 `S::S'. */
4720 /* DR 244 says that we look up the name after the "~" in the
4721 same scope as we looked up the qualifying name. That idea
4722 isn't fully worked out; it's more complicated than that. */
4723 scope = parser->scope;
4724 object_scope = parser->object_scope;
4725 qualifying_scope = parser->qualifying_scope;
4727 /* Check for invalid scopes. */
4728 if (scope == error_mark_node)
4730 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4731 cp_lexer_consume_token (parser->lexer);
4732 return error_mark_node;
4734 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4736 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4737 error_at (token->location,
4738 "scope %qT before %<~%> is not a class-name",
4739 scope);
4740 cp_parser_simulate_error (parser);
4741 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4742 cp_lexer_consume_token (parser->lexer);
4743 return error_mark_node;
4745 gcc_assert (!scope || TYPE_P (scope));
4747 /* If the name is of the form "X::~X" it's OK even if X is a
4748 typedef. */
4749 token = cp_lexer_peek_token (parser->lexer);
4750 if (scope
4751 && token->type == CPP_NAME
4752 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4753 != CPP_LESS)
4754 && (token->u.value == TYPE_IDENTIFIER (scope)
4755 || (CLASS_TYPE_P (scope)
4756 && constructor_name_p (token->u.value, scope))))
4758 cp_lexer_consume_token (parser->lexer);
4759 return build_nt (BIT_NOT_EXPR, scope);
4762 /* If there was an explicit qualification (S::~T), first look
4763 in the scope given by the qualification (i.e., S).
4765 Note: in the calls to cp_parser_class_name below we pass
4766 typename_type so that lookup finds the injected-class-name
4767 rather than the constructor. */
4768 done = false;
4769 type_decl = NULL_TREE;
4770 if (scope)
4772 cp_parser_parse_tentatively (parser);
4773 type_decl = cp_parser_class_name (parser,
4774 /*typename_keyword_p=*/false,
4775 /*template_keyword_p=*/false,
4776 typename_type,
4777 /*check_dependency=*/false,
4778 /*class_head_p=*/false,
4779 declarator_p);
4780 if (cp_parser_parse_definitely (parser))
4781 done = true;
4783 /* In "N::S::~S", look in "N" as well. */
4784 if (!done && scope && qualifying_scope)
4786 cp_parser_parse_tentatively (parser);
4787 parser->scope = qualifying_scope;
4788 parser->object_scope = NULL_TREE;
4789 parser->qualifying_scope = NULL_TREE;
4790 type_decl
4791 = cp_parser_class_name (parser,
4792 /*typename_keyword_p=*/false,
4793 /*template_keyword_p=*/false,
4794 typename_type,
4795 /*check_dependency=*/false,
4796 /*class_head_p=*/false,
4797 declarator_p);
4798 if (cp_parser_parse_definitely (parser))
4799 done = true;
4801 /* In "p->S::~T", look in the scope given by "*p" as well. */
4802 else if (!done && object_scope)
4804 cp_parser_parse_tentatively (parser);
4805 parser->scope = object_scope;
4806 parser->object_scope = NULL_TREE;
4807 parser->qualifying_scope = NULL_TREE;
4808 type_decl
4809 = cp_parser_class_name (parser,
4810 /*typename_keyword_p=*/false,
4811 /*template_keyword_p=*/false,
4812 typename_type,
4813 /*check_dependency=*/false,
4814 /*class_head_p=*/false,
4815 declarator_p);
4816 if (cp_parser_parse_definitely (parser))
4817 done = true;
4819 /* Look in the surrounding context. */
4820 if (!done)
4822 parser->scope = NULL_TREE;
4823 parser->object_scope = NULL_TREE;
4824 parser->qualifying_scope = NULL_TREE;
4825 if (processing_template_decl)
4826 cp_parser_parse_tentatively (parser);
4827 type_decl
4828 = cp_parser_class_name (parser,
4829 /*typename_keyword_p=*/false,
4830 /*template_keyword_p=*/false,
4831 typename_type,
4832 /*check_dependency=*/false,
4833 /*class_head_p=*/false,
4834 declarator_p);
4835 if (processing_template_decl
4836 && ! cp_parser_parse_definitely (parser))
4838 /* We couldn't find a type with this name, so just accept
4839 it and check for a match at instantiation time. */
4840 type_decl = cp_parser_identifier (parser);
4841 if (type_decl != error_mark_node)
4842 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4843 return type_decl;
4846 /* If an error occurred, assume that the name of the
4847 destructor is the same as the name of the qualifying
4848 class. That allows us to keep parsing after running
4849 into ill-formed destructor names. */
4850 if (type_decl == error_mark_node && scope)
4851 return build_nt (BIT_NOT_EXPR, scope);
4852 else if (type_decl == error_mark_node)
4853 return error_mark_node;
4855 /* Check that destructor name and scope match. */
4856 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4858 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4859 error_at (token->location,
4860 "declaration of %<~%T%> as member of %qT",
4861 type_decl, scope);
4862 cp_parser_simulate_error (parser);
4863 return error_mark_node;
4866 /* [class.dtor]
4868 A typedef-name that names a class shall not be used as the
4869 identifier in the declarator for a destructor declaration. */
4870 if (declarator_p
4871 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4872 && !DECL_SELF_REFERENCE_P (type_decl)
4873 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4874 error_at (token->location,
4875 "typedef-name %qD used as destructor declarator",
4876 type_decl);
4878 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4881 case CPP_KEYWORD:
4882 if (token->keyword == RID_OPERATOR)
4884 tree id;
4886 /* This could be a template-id, so we try that first. */
4887 cp_parser_parse_tentatively (parser);
4888 /* Try a template-id. */
4889 id = cp_parser_template_id (parser, template_keyword_p,
4890 /*check_dependency_p=*/true,
4891 none_type,
4892 declarator_p);
4893 /* If that worked, we're done. */
4894 if (cp_parser_parse_definitely (parser))
4895 return id;
4896 /* We still don't know whether we're looking at an
4897 operator-function-id or a conversion-function-id. */
4898 cp_parser_parse_tentatively (parser);
4899 /* Try an operator-function-id. */
4900 id = cp_parser_operator_function_id (parser);
4901 /* If that didn't work, try a conversion-function-id. */
4902 if (!cp_parser_parse_definitely (parser))
4903 id = cp_parser_conversion_function_id (parser);
4904 else if (UDLIT_OPER_P (id))
4906 /* 17.6.3.3.5 */
4907 const char *name = UDLIT_OP_SUFFIX (id);
4908 if (name[0] != '_' && !in_system_header)
4909 warning (0, "literal operator suffixes not preceded by %<_%>"
4910 " are reserved for future standardization");
4913 return id;
4915 /* Fall through. */
4917 default:
4918 if (optional_p)
4919 return NULL_TREE;
4920 cp_parser_error (parser, "expected unqualified-id");
4921 return error_mark_node;
4925 /* Parse an (optional) nested-name-specifier.
4927 nested-name-specifier: [C++98]
4928 class-or-namespace-name :: nested-name-specifier [opt]
4929 class-or-namespace-name :: template nested-name-specifier [opt]
4931 nested-name-specifier: [C++0x]
4932 type-name ::
4933 namespace-name ::
4934 nested-name-specifier identifier ::
4935 nested-name-specifier template [opt] simple-template-id ::
4937 PARSER->SCOPE should be set appropriately before this function is
4938 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4939 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4940 in name lookups.
4942 Sets PARSER->SCOPE to the class (TYPE) or namespace
4943 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4944 it unchanged if there is no nested-name-specifier. Returns the new
4945 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4947 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4948 part of a declaration and/or decl-specifier. */
4950 static tree
4951 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4952 bool typename_keyword_p,
4953 bool check_dependency_p,
4954 bool type_p,
4955 bool is_declaration)
4957 bool success = false;
4958 cp_token_position start = 0;
4959 cp_token *token;
4961 /* Remember where the nested-name-specifier starts. */
4962 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4964 start = cp_lexer_token_position (parser->lexer, false);
4965 push_deferring_access_checks (dk_deferred);
4968 while (true)
4970 tree new_scope;
4971 tree old_scope;
4972 tree saved_qualifying_scope;
4973 bool template_keyword_p;
4975 /* Spot cases that cannot be the beginning of a
4976 nested-name-specifier. */
4977 token = cp_lexer_peek_token (parser->lexer);
4979 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4980 the already parsed nested-name-specifier. */
4981 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4983 /* Grab the nested-name-specifier and continue the loop. */
4984 cp_parser_pre_parsed_nested_name_specifier (parser);
4985 /* If we originally encountered this nested-name-specifier
4986 with IS_DECLARATION set to false, we will not have
4987 resolved TYPENAME_TYPEs, so we must do so here. */
4988 if (is_declaration
4989 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4991 new_scope = resolve_typename_type (parser->scope,
4992 /*only_current_p=*/false);
4993 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4994 parser->scope = new_scope;
4996 success = true;
4997 continue;
5000 /* Spot cases that cannot be the beginning of a
5001 nested-name-specifier. On the second and subsequent times
5002 through the loop, we look for the `template' keyword. */
5003 if (success && token->keyword == RID_TEMPLATE)
5005 /* A template-id can start a nested-name-specifier. */
5006 else if (token->type == CPP_TEMPLATE_ID)
5008 /* DR 743: decltype can be used in a nested-name-specifier. */
5009 else if (token_is_decltype (token))
5011 else
5013 /* If the next token is not an identifier, then it is
5014 definitely not a type-name or namespace-name. */
5015 if (token->type != CPP_NAME)
5016 break;
5017 /* If the following token is neither a `<' (to begin a
5018 template-id), nor a `::', then we are not looking at a
5019 nested-name-specifier. */
5020 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5022 if (token->type == CPP_COLON
5023 && parser->colon_corrects_to_scope_p
5024 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5026 error_at (token->location,
5027 "found %<:%> in nested-name-specifier, expected %<::%>");
5028 token->type = CPP_SCOPE;
5031 if (token->type != CPP_SCOPE
5032 && !cp_parser_nth_token_starts_template_argument_list_p
5033 (parser, 2))
5034 break;
5037 /* The nested-name-specifier is optional, so we parse
5038 tentatively. */
5039 cp_parser_parse_tentatively (parser);
5041 /* Look for the optional `template' keyword, if this isn't the
5042 first time through the loop. */
5043 if (success)
5044 template_keyword_p = cp_parser_optional_template_keyword (parser);
5045 else
5046 template_keyword_p = false;
5048 /* Save the old scope since the name lookup we are about to do
5049 might destroy it. */
5050 old_scope = parser->scope;
5051 saved_qualifying_scope = parser->qualifying_scope;
5052 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5053 look up names in "X<T>::I" in order to determine that "Y" is
5054 a template. So, if we have a typename at this point, we make
5055 an effort to look through it. */
5056 if (is_declaration
5057 && !typename_keyword_p
5058 && parser->scope
5059 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5060 parser->scope = resolve_typename_type (parser->scope,
5061 /*only_current_p=*/false);
5062 /* Parse the qualifying entity. */
5063 new_scope
5064 = cp_parser_qualifying_entity (parser,
5065 typename_keyword_p,
5066 template_keyword_p,
5067 check_dependency_p,
5068 type_p,
5069 is_declaration);
5070 /* Look for the `::' token. */
5071 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5073 /* If we found what we wanted, we keep going; otherwise, we're
5074 done. */
5075 if (!cp_parser_parse_definitely (parser))
5077 bool error_p = false;
5079 /* Restore the OLD_SCOPE since it was valid before the
5080 failed attempt at finding the last
5081 class-or-namespace-name. */
5082 parser->scope = old_scope;
5083 parser->qualifying_scope = saved_qualifying_scope;
5085 /* If the next token is a decltype, and the one after that is a
5086 `::', then the decltype has failed to resolve to a class or
5087 enumeration type. Give this error even when parsing
5088 tentatively since it can't possibly be valid--and we're going
5089 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5090 won't get another chance.*/
5091 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5092 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5093 == CPP_SCOPE))
5095 token = cp_lexer_consume_token (parser->lexer);
5096 error_at (token->location, "decltype evaluates to %qT, "
5097 "which is not a class or enumeration type",
5098 token->u.value);
5099 parser->scope = error_mark_node;
5100 error_p = true;
5101 /* As below. */
5102 success = true;
5103 cp_lexer_consume_token (parser->lexer);
5106 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5107 break;
5108 /* If the next token is an identifier, and the one after
5109 that is a `::', then any valid interpretation would have
5110 found a class-or-namespace-name. */
5111 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5112 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5113 == CPP_SCOPE)
5114 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5115 != CPP_COMPL))
5117 token = cp_lexer_consume_token (parser->lexer);
5118 if (!error_p)
5120 if (!token->ambiguous_p)
5122 tree decl;
5123 tree ambiguous_decls;
5125 decl = cp_parser_lookup_name (parser, token->u.value,
5126 none_type,
5127 /*is_template=*/false,
5128 /*is_namespace=*/false,
5129 /*check_dependency=*/true,
5130 &ambiguous_decls,
5131 token->location);
5132 if (TREE_CODE (decl) == TEMPLATE_DECL)
5133 error_at (token->location,
5134 "%qD used without template parameters",
5135 decl);
5136 else if (ambiguous_decls)
5138 error_at (token->location,
5139 "reference to %qD is ambiguous",
5140 token->u.value);
5141 print_candidates (ambiguous_decls);
5142 decl = error_mark_node;
5144 else
5146 if (cxx_dialect != cxx98)
5147 cp_parser_name_lookup_error
5148 (parser, token->u.value, decl, NLE_NOT_CXX98,
5149 token->location);
5150 else
5151 cp_parser_name_lookup_error
5152 (parser, token->u.value, decl, NLE_CXX98,
5153 token->location);
5156 parser->scope = error_mark_node;
5157 error_p = true;
5158 /* Treat this as a successful nested-name-specifier
5159 due to:
5161 [basic.lookup.qual]
5163 If the name found is not a class-name (clause
5164 _class_) or namespace-name (_namespace.def_), the
5165 program is ill-formed. */
5166 success = true;
5168 cp_lexer_consume_token (parser->lexer);
5170 break;
5172 /* We've found one valid nested-name-specifier. */
5173 success = true;
5174 /* Name lookup always gives us a DECL. */
5175 if (TREE_CODE (new_scope) == TYPE_DECL)
5176 new_scope = TREE_TYPE (new_scope);
5177 /* Uses of "template" must be followed by actual templates. */
5178 if (template_keyword_p
5179 && !(CLASS_TYPE_P (new_scope)
5180 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5181 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5182 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5183 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5184 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5185 == TEMPLATE_ID_EXPR)))
5186 permerror (input_location, TYPE_P (new_scope)
5187 ? G_("%qT is not a template")
5188 : G_("%qD is not a template"),
5189 new_scope);
5190 /* If it is a class scope, try to complete it; we are about to
5191 be looking up names inside the class. */
5192 if (TYPE_P (new_scope)
5193 /* Since checking types for dependency can be expensive,
5194 avoid doing it if the type is already complete. */
5195 && !COMPLETE_TYPE_P (new_scope)
5196 /* Do not try to complete dependent types. */
5197 && !dependent_type_p (new_scope))
5199 new_scope = complete_type (new_scope);
5200 /* If it is a typedef to current class, use the current
5201 class instead, as the typedef won't have any names inside
5202 it yet. */
5203 if (!COMPLETE_TYPE_P (new_scope)
5204 && currently_open_class (new_scope))
5205 new_scope = TYPE_MAIN_VARIANT (new_scope);
5207 /* Make sure we look in the right scope the next time through
5208 the loop. */
5209 parser->scope = new_scope;
5212 /* If parsing tentatively, replace the sequence of tokens that makes
5213 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5214 token. That way, should we re-parse the token stream, we will
5215 not have to repeat the effort required to do the parse, nor will
5216 we issue duplicate error messages. */
5217 if (success && start)
5219 cp_token *token;
5221 token = cp_lexer_token_at (parser->lexer, start);
5222 /* Reset the contents of the START token. */
5223 token->type = CPP_NESTED_NAME_SPECIFIER;
5224 /* Retrieve any deferred checks. Do not pop this access checks yet
5225 so the memory will not be reclaimed during token replacing below. */
5226 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5227 token->u.tree_check_value->value = parser->scope;
5228 token->u.tree_check_value->checks = get_deferred_access_checks ();
5229 token->u.tree_check_value->qualifying_scope =
5230 parser->qualifying_scope;
5231 token->keyword = RID_MAX;
5233 /* Purge all subsequent tokens. */
5234 cp_lexer_purge_tokens_after (parser->lexer, start);
5237 if (start)
5238 pop_to_parent_deferring_access_checks ();
5240 return success ? parser->scope : NULL_TREE;
5243 /* Parse a nested-name-specifier. See
5244 cp_parser_nested_name_specifier_opt for details. This function
5245 behaves identically, except that it will an issue an error if no
5246 nested-name-specifier is present. */
5248 static tree
5249 cp_parser_nested_name_specifier (cp_parser *parser,
5250 bool typename_keyword_p,
5251 bool check_dependency_p,
5252 bool type_p,
5253 bool is_declaration)
5255 tree scope;
5257 /* Look for the nested-name-specifier. */
5258 scope = cp_parser_nested_name_specifier_opt (parser,
5259 typename_keyword_p,
5260 check_dependency_p,
5261 type_p,
5262 is_declaration);
5263 /* If it was not present, issue an error message. */
5264 if (!scope)
5266 cp_parser_error (parser, "expected nested-name-specifier");
5267 parser->scope = NULL_TREE;
5270 return scope;
5273 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5274 this is either a class-name or a namespace-name (which corresponds
5275 to the class-or-namespace-name production in the grammar). For
5276 C++0x, it can also be a type-name that refers to an enumeration
5277 type or a simple-template-id.
5279 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5280 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5281 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5282 TYPE_P is TRUE iff the next name should be taken as a class-name,
5283 even the same name is declared to be another entity in the same
5284 scope.
5286 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5287 specified by the class-or-namespace-name. If neither is found the
5288 ERROR_MARK_NODE is returned. */
5290 static tree
5291 cp_parser_qualifying_entity (cp_parser *parser,
5292 bool typename_keyword_p,
5293 bool template_keyword_p,
5294 bool check_dependency_p,
5295 bool type_p,
5296 bool is_declaration)
5298 tree saved_scope;
5299 tree saved_qualifying_scope;
5300 tree saved_object_scope;
5301 tree scope;
5302 bool only_class_p;
5303 bool successful_parse_p;
5305 /* DR 743: decltype can appear in a nested-name-specifier. */
5306 if (cp_lexer_next_token_is_decltype (parser->lexer))
5308 scope = cp_parser_decltype (parser);
5309 if (TREE_CODE (scope) != ENUMERAL_TYPE
5310 && !MAYBE_CLASS_TYPE_P (scope))
5312 cp_parser_simulate_error (parser);
5313 return error_mark_node;
5315 if (TYPE_NAME (scope))
5316 scope = TYPE_NAME (scope);
5317 return scope;
5320 /* Before we try to parse the class-name, we must save away the
5321 current PARSER->SCOPE since cp_parser_class_name will destroy
5322 it. */
5323 saved_scope = parser->scope;
5324 saved_qualifying_scope = parser->qualifying_scope;
5325 saved_object_scope = parser->object_scope;
5326 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5327 there is no need to look for a namespace-name. */
5328 only_class_p = template_keyword_p
5329 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5330 if (!only_class_p)
5331 cp_parser_parse_tentatively (parser);
5332 scope = cp_parser_class_name (parser,
5333 typename_keyword_p,
5334 template_keyword_p,
5335 type_p ? class_type : none_type,
5336 check_dependency_p,
5337 /*class_head_p=*/false,
5338 is_declaration);
5339 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5340 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5341 if (!only_class_p
5342 && cxx_dialect != cxx98
5343 && !successful_parse_p)
5345 /* Restore the saved scope. */
5346 parser->scope = saved_scope;
5347 parser->qualifying_scope = saved_qualifying_scope;
5348 parser->object_scope = saved_object_scope;
5350 /* Parse tentatively. */
5351 cp_parser_parse_tentatively (parser);
5353 /* Parse a type-name */
5354 scope = cp_parser_type_name (parser);
5356 /* "If the name found does not designate a namespace or a class,
5357 enumeration, or dependent type, the program is ill-formed."
5359 We cover classes and dependent types above and namespaces below,
5360 so this code is only looking for enums. */
5361 if (!scope || TREE_CODE (scope) != TYPE_DECL
5362 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5363 cp_parser_simulate_error (parser);
5365 successful_parse_p = cp_parser_parse_definitely (parser);
5367 /* If that didn't work, try for a namespace-name. */
5368 if (!only_class_p && !successful_parse_p)
5370 /* Restore the saved scope. */
5371 parser->scope = saved_scope;
5372 parser->qualifying_scope = saved_qualifying_scope;
5373 parser->object_scope = saved_object_scope;
5374 /* If we are not looking at an identifier followed by the scope
5375 resolution operator, then this is not part of a
5376 nested-name-specifier. (Note that this function is only used
5377 to parse the components of a nested-name-specifier.) */
5378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5379 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5380 return error_mark_node;
5381 scope = cp_parser_namespace_name (parser);
5384 return scope;
5387 /* Parse a postfix-expression.
5389 postfix-expression:
5390 primary-expression
5391 postfix-expression [ expression ]
5392 postfix-expression ( expression-list [opt] )
5393 simple-type-specifier ( expression-list [opt] )
5394 typename :: [opt] nested-name-specifier identifier
5395 ( expression-list [opt] )
5396 typename :: [opt] nested-name-specifier template [opt] template-id
5397 ( expression-list [opt] )
5398 postfix-expression . template [opt] id-expression
5399 postfix-expression -> template [opt] id-expression
5400 postfix-expression . pseudo-destructor-name
5401 postfix-expression -> pseudo-destructor-name
5402 postfix-expression ++
5403 postfix-expression --
5404 dynamic_cast < type-id > ( expression )
5405 static_cast < type-id > ( expression )
5406 reinterpret_cast < type-id > ( expression )
5407 const_cast < type-id > ( expression )
5408 typeid ( expression )
5409 typeid ( type-id )
5411 GNU Extension:
5413 postfix-expression:
5414 ( type-id ) { initializer-list , [opt] }
5416 This extension is a GNU version of the C99 compound-literal
5417 construct. (The C99 grammar uses `type-name' instead of `type-id',
5418 but they are essentially the same concept.)
5420 If ADDRESS_P is true, the postfix expression is the operand of the
5421 `&' operator. CAST_P is true if this expression is the target of a
5422 cast.
5424 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5425 class member access expressions [expr.ref].
5427 Returns a representation of the expression. */
5429 static tree
5430 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5431 bool member_access_only_p, bool decltype_p,
5432 cp_id_kind * pidk_return)
5434 cp_token *token;
5435 enum rid keyword;
5436 cp_id_kind idk = CP_ID_KIND_NONE;
5437 tree postfix_expression = NULL_TREE;
5438 bool is_member_access = false;
5440 /* Peek at the next token. */
5441 token = cp_lexer_peek_token (parser->lexer);
5442 /* Some of the productions are determined by keywords. */
5443 keyword = token->keyword;
5444 switch (keyword)
5446 case RID_DYNCAST:
5447 case RID_STATCAST:
5448 case RID_REINTCAST:
5449 case RID_CONSTCAST:
5451 tree type;
5452 tree expression;
5453 const char *saved_message;
5455 /* All of these can be handled in the same way from the point
5456 of view of parsing. Begin by consuming the token
5457 identifying the cast. */
5458 cp_lexer_consume_token (parser->lexer);
5460 /* New types cannot be defined in the cast. */
5461 saved_message = parser->type_definition_forbidden_message;
5462 parser->type_definition_forbidden_message
5463 = G_("types may not be defined in casts");
5465 /* Look for the opening `<'. */
5466 cp_parser_require (parser, CPP_LESS, RT_LESS);
5467 /* Parse the type to which we are casting. */
5468 type = cp_parser_type_id (parser);
5469 /* Look for the closing `>'. */
5470 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5471 /* Restore the old message. */
5472 parser->type_definition_forbidden_message = saved_message;
5474 /* And the expression which is being cast. */
5475 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5476 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5477 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5479 /* Only type conversions to integral or enumeration types
5480 can be used in constant-expressions. */
5481 if (!cast_valid_in_integral_constant_expression_p (type)
5482 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5483 return error_mark_node;
5485 switch (keyword)
5487 case RID_DYNCAST:
5488 postfix_expression
5489 = build_dynamic_cast (type, expression, tf_warning_or_error);
5490 break;
5491 case RID_STATCAST:
5492 postfix_expression
5493 = build_static_cast (type, expression, tf_warning_or_error);
5494 break;
5495 case RID_REINTCAST:
5496 postfix_expression
5497 = build_reinterpret_cast (type, expression,
5498 tf_warning_or_error);
5499 break;
5500 case RID_CONSTCAST:
5501 postfix_expression
5502 = build_const_cast (type, expression, tf_warning_or_error);
5503 break;
5504 default:
5505 gcc_unreachable ();
5508 break;
5510 case RID_TYPEID:
5512 tree type;
5513 const char *saved_message;
5514 bool saved_in_type_id_in_expr_p;
5516 /* Consume the `typeid' token. */
5517 cp_lexer_consume_token (parser->lexer);
5518 /* Look for the `(' token. */
5519 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5520 /* Types cannot be defined in a `typeid' expression. */
5521 saved_message = parser->type_definition_forbidden_message;
5522 parser->type_definition_forbidden_message
5523 = G_("types may not be defined in a %<typeid%> expression");
5524 /* We can't be sure yet whether we're looking at a type-id or an
5525 expression. */
5526 cp_parser_parse_tentatively (parser);
5527 /* Try a type-id first. */
5528 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5529 parser->in_type_id_in_expr_p = true;
5530 type = cp_parser_type_id (parser);
5531 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5532 /* Look for the `)' token. Otherwise, we can't be sure that
5533 we're not looking at an expression: consider `typeid (int
5534 (3))', for example. */
5535 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5536 /* If all went well, simply lookup the type-id. */
5537 if (cp_parser_parse_definitely (parser))
5538 postfix_expression = get_typeid (type, tf_warning_or_error);
5539 /* Otherwise, fall back to the expression variant. */
5540 else
5542 tree expression;
5544 /* Look for an expression. */
5545 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5546 /* Compute its typeid. */
5547 postfix_expression = build_typeid (expression, tf_warning_or_error);
5548 /* Look for the `)' token. */
5549 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5551 /* Restore the saved message. */
5552 parser->type_definition_forbidden_message = saved_message;
5553 /* `typeid' may not appear in an integral constant expression. */
5554 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5555 return error_mark_node;
5557 break;
5559 case RID_TYPENAME:
5561 tree type;
5562 /* The syntax permitted here is the same permitted for an
5563 elaborated-type-specifier. */
5564 type = cp_parser_elaborated_type_specifier (parser,
5565 /*is_friend=*/false,
5566 /*is_declaration=*/false);
5567 postfix_expression = cp_parser_functional_cast (parser, type);
5569 break;
5571 case RID_BUILTIN_SHUFFLE:
5573 vec<tree, va_gc> *vec;
5574 unsigned int i;
5575 tree p;
5576 location_t loc = token->location;
5578 cp_lexer_consume_token (parser->lexer);
5579 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5580 /*cast_p=*/false, /*allow_expansion_p=*/true,
5581 /*non_constant_p=*/NULL);
5582 if (vec == NULL)
5583 return error_mark_node;
5585 FOR_EACH_VEC_ELT (*vec, i, p)
5586 mark_exp_read (p);
5588 if (vec->length () == 2)
5589 return c_build_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1]);
5590 else if (vec->length () == 3)
5591 return c_build_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2]);
5592 else
5594 error_at (loc, "wrong number of arguments to "
5595 "%<__builtin_shuffle%>");
5596 return error_mark_node;
5598 break;
5601 default:
5603 tree type;
5605 /* If the next thing is a simple-type-specifier, we may be
5606 looking at a functional cast. We could also be looking at
5607 an id-expression. So, we try the functional cast, and if
5608 that doesn't work we fall back to the primary-expression. */
5609 cp_parser_parse_tentatively (parser);
5610 /* Look for the simple-type-specifier. */
5611 type = cp_parser_simple_type_specifier (parser,
5612 /*decl_specs=*/NULL,
5613 CP_PARSER_FLAGS_NONE);
5614 /* Parse the cast itself. */
5615 if (!cp_parser_error_occurred (parser))
5616 postfix_expression
5617 = cp_parser_functional_cast (parser, type);
5618 /* If that worked, we're done. */
5619 if (cp_parser_parse_definitely (parser))
5620 break;
5622 /* If the functional-cast didn't work out, try a
5623 compound-literal. */
5624 if (cp_parser_allow_gnu_extensions_p (parser)
5625 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5627 vec<constructor_elt, va_gc> *initializer_list = NULL;
5628 bool saved_in_type_id_in_expr_p;
5630 cp_parser_parse_tentatively (parser);
5631 /* Consume the `('. */
5632 cp_lexer_consume_token (parser->lexer);
5633 /* Parse the type. */
5634 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5635 parser->in_type_id_in_expr_p = true;
5636 type = cp_parser_type_id (parser);
5637 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5638 /* Look for the `)'. */
5639 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5640 /* Look for the `{'. */
5641 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5642 /* If things aren't going well, there's no need to
5643 keep going. */
5644 if (!cp_parser_error_occurred (parser))
5646 bool non_constant_p;
5647 /* Parse the initializer-list. */
5648 initializer_list
5649 = cp_parser_initializer_list (parser, &non_constant_p);
5650 /* Allow a trailing `,'. */
5651 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5652 cp_lexer_consume_token (parser->lexer);
5653 /* Look for the final `}'. */
5654 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5656 /* If that worked, we're definitely looking at a
5657 compound-literal expression. */
5658 if (cp_parser_parse_definitely (parser))
5660 /* Warn the user that a compound literal is not
5661 allowed in standard C++. */
5662 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5663 /* For simplicity, we disallow compound literals in
5664 constant-expressions. We could
5665 allow compound literals of integer type, whose
5666 initializer was a constant, in constant
5667 expressions. Permitting that usage, as a further
5668 extension, would not change the meaning of any
5669 currently accepted programs. (Of course, as
5670 compound literals are not part of ISO C++, the
5671 standard has nothing to say.) */
5672 if (cp_parser_non_integral_constant_expression (parser,
5673 NIC_NCC))
5675 postfix_expression = error_mark_node;
5676 break;
5678 /* Form the representation of the compound-literal. */
5679 postfix_expression
5680 = (finish_compound_literal
5681 (type, build_constructor (init_list_type_node,
5682 initializer_list),
5683 tf_warning_or_error));
5684 break;
5688 /* It must be a primary-expression. */
5689 postfix_expression
5690 = cp_parser_primary_expression (parser, address_p, cast_p,
5691 /*template_arg_p=*/false,
5692 decltype_p,
5693 &idk);
5695 break;
5698 /* Note that we don't need to worry about calling build_cplus_new on a
5699 class-valued CALL_EXPR in decltype when it isn't the end of the
5700 postfix-expression; unary_complex_lvalue will take care of that for
5701 all these cases. */
5703 /* Keep looping until the postfix-expression is complete. */
5704 while (true)
5706 if (idk == CP_ID_KIND_UNQUALIFIED
5707 && identifier_p (postfix_expression)
5708 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5709 /* It is not a Koenig lookup function call. */
5710 postfix_expression
5711 = unqualified_name_lookup_error (postfix_expression);
5713 /* Peek at the next token. */
5714 token = cp_lexer_peek_token (parser->lexer);
5716 switch (token->type)
5718 case CPP_OPEN_SQUARE:
5719 if (cp_next_tokens_can_be_std_attribute_p (parser))
5721 cp_parser_error (parser,
5722 "two consecutive %<[%> shall "
5723 "only introduce an attribute");
5724 return error_mark_node;
5726 postfix_expression
5727 = cp_parser_postfix_open_square_expression (parser,
5728 postfix_expression,
5729 false);
5730 idk = CP_ID_KIND_NONE;
5731 is_member_access = false;
5732 break;
5734 case CPP_OPEN_PAREN:
5735 /* postfix-expression ( expression-list [opt] ) */
5737 bool koenig_p;
5738 bool is_builtin_constant_p;
5739 bool saved_integral_constant_expression_p = false;
5740 bool saved_non_integral_constant_expression_p = false;
5741 int complain = tf_warning_or_error;
5742 vec<tree, va_gc> *args;
5744 if (decltype_p)
5745 complain |= tf_decltype;
5747 is_member_access = false;
5749 is_builtin_constant_p
5750 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5751 if (is_builtin_constant_p)
5753 /* The whole point of __builtin_constant_p is to allow
5754 non-constant expressions to appear as arguments. */
5755 saved_integral_constant_expression_p
5756 = parser->integral_constant_expression_p;
5757 saved_non_integral_constant_expression_p
5758 = parser->non_integral_constant_expression_p;
5759 parser->integral_constant_expression_p = false;
5761 args = (cp_parser_parenthesized_expression_list
5762 (parser, non_attr,
5763 /*cast_p=*/false, /*allow_expansion_p=*/true,
5764 /*non_constant_p=*/NULL));
5765 if (is_builtin_constant_p)
5767 parser->integral_constant_expression_p
5768 = saved_integral_constant_expression_p;
5769 parser->non_integral_constant_expression_p
5770 = saved_non_integral_constant_expression_p;
5773 if (args == NULL)
5775 postfix_expression = error_mark_node;
5776 break;
5779 /* Function calls are not permitted in
5780 constant-expressions. */
5781 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5782 && cp_parser_non_integral_constant_expression (parser,
5783 NIC_FUNC_CALL))
5785 postfix_expression = error_mark_node;
5786 release_tree_vector (args);
5787 break;
5790 koenig_p = false;
5791 if (idk == CP_ID_KIND_UNQUALIFIED
5792 || idk == CP_ID_KIND_TEMPLATE_ID)
5794 if (identifier_p (postfix_expression))
5796 if (!args->is_empty ())
5798 koenig_p = true;
5799 if (!any_type_dependent_arguments_p (args))
5800 postfix_expression
5801 = perform_koenig_lookup (postfix_expression, args,
5802 /*include_std=*/false,
5803 complain);
5805 else
5806 postfix_expression
5807 = unqualified_fn_lookup_error (postfix_expression);
5809 /* We do not perform argument-dependent lookup if
5810 normal lookup finds a non-function, in accordance
5811 with the expected resolution of DR 218. */
5812 else if (!args->is_empty ()
5813 && is_overloaded_fn (postfix_expression))
5815 tree fn = get_first_fn (postfix_expression);
5816 fn = STRIP_TEMPLATE (fn);
5818 /* Do not do argument dependent lookup if regular
5819 lookup finds a member function or a block-scope
5820 function declaration. [basic.lookup.argdep]/3 */
5821 if (!DECL_FUNCTION_MEMBER_P (fn)
5822 && !DECL_LOCAL_FUNCTION_P (fn))
5824 koenig_p = true;
5825 if (!any_type_dependent_arguments_p (args))
5826 postfix_expression
5827 = perform_koenig_lookup (postfix_expression, args,
5828 /*include_std=*/false,
5829 complain);
5834 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5836 tree instance = TREE_OPERAND (postfix_expression, 0);
5837 tree fn = TREE_OPERAND (postfix_expression, 1);
5839 if (processing_template_decl
5840 && (type_dependent_expression_p (instance)
5841 || (!BASELINK_P (fn)
5842 && TREE_CODE (fn) != FIELD_DECL)
5843 || type_dependent_expression_p (fn)
5844 || any_type_dependent_arguments_p (args)))
5846 postfix_expression
5847 = build_nt_call_vec (postfix_expression, args);
5848 release_tree_vector (args);
5849 break;
5852 if (BASELINK_P (fn))
5854 postfix_expression
5855 = (build_new_method_call
5856 (instance, fn, &args, NULL_TREE,
5857 (idk == CP_ID_KIND_QUALIFIED
5858 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5859 : LOOKUP_NORMAL),
5860 /*fn_p=*/NULL,
5861 complain));
5863 else
5864 postfix_expression
5865 = finish_call_expr (postfix_expression, &args,
5866 /*disallow_virtual=*/false,
5867 /*koenig_p=*/false,
5868 complain);
5870 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5871 || TREE_CODE (postfix_expression) == MEMBER_REF
5872 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5873 postfix_expression = (build_offset_ref_call_from_tree
5874 (postfix_expression, &args,
5875 complain));
5876 else if (idk == CP_ID_KIND_QUALIFIED)
5877 /* A call to a static class member, or a namespace-scope
5878 function. */
5879 postfix_expression
5880 = finish_call_expr (postfix_expression, &args,
5881 /*disallow_virtual=*/true,
5882 koenig_p,
5883 complain);
5884 else
5885 /* All other function calls. */
5886 postfix_expression
5887 = finish_call_expr (postfix_expression, &args,
5888 /*disallow_virtual=*/false,
5889 koenig_p,
5890 complain);
5892 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5893 idk = CP_ID_KIND_NONE;
5895 release_tree_vector (args);
5897 break;
5899 case CPP_DOT:
5900 case CPP_DEREF:
5901 /* postfix-expression . template [opt] id-expression
5902 postfix-expression . pseudo-destructor-name
5903 postfix-expression -> template [opt] id-expression
5904 postfix-expression -> pseudo-destructor-name */
5906 /* Consume the `.' or `->' operator. */
5907 cp_lexer_consume_token (parser->lexer);
5909 postfix_expression
5910 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5911 postfix_expression,
5912 false, &idk,
5913 token->location);
5915 is_member_access = true;
5916 break;
5918 case CPP_PLUS_PLUS:
5919 /* postfix-expression ++ */
5920 /* Consume the `++' token. */
5921 cp_lexer_consume_token (parser->lexer);
5922 /* Generate a representation for the complete expression. */
5923 postfix_expression
5924 = finish_increment_expr (postfix_expression,
5925 POSTINCREMENT_EXPR);
5926 /* Increments may not appear in constant-expressions. */
5927 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5928 postfix_expression = error_mark_node;
5929 idk = CP_ID_KIND_NONE;
5930 is_member_access = false;
5931 break;
5933 case CPP_MINUS_MINUS:
5934 /* postfix-expression -- */
5935 /* Consume the `--' token. */
5936 cp_lexer_consume_token (parser->lexer);
5937 /* Generate a representation for the complete expression. */
5938 postfix_expression
5939 = finish_increment_expr (postfix_expression,
5940 POSTDECREMENT_EXPR);
5941 /* Decrements may not appear in constant-expressions. */
5942 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5943 postfix_expression = error_mark_node;
5944 idk = CP_ID_KIND_NONE;
5945 is_member_access = false;
5946 break;
5948 default:
5949 if (pidk_return != NULL)
5950 * pidk_return = idk;
5951 if (member_access_only_p)
5952 return is_member_access? postfix_expression : error_mark_node;
5953 else
5954 return postfix_expression;
5958 /* We should never get here. */
5959 gcc_unreachable ();
5960 return error_mark_node;
5963 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5964 by cp_parser_builtin_offsetof. We're looking for
5966 postfix-expression [ expression ]
5967 postfix-expression [ braced-init-list ] (C++11)
5969 FOR_OFFSETOF is set if we're being called in that context, which
5970 changes how we deal with integer constant expressions. */
5972 static tree
5973 cp_parser_postfix_open_square_expression (cp_parser *parser,
5974 tree postfix_expression,
5975 bool for_offsetof)
5977 tree index;
5978 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5980 /* Consume the `[' token. */
5981 cp_lexer_consume_token (parser->lexer);
5983 /* Parse the index expression. */
5984 /* ??? For offsetof, there is a question of what to allow here. If
5985 offsetof is not being used in an integral constant expression context,
5986 then we *could* get the right answer by computing the value at runtime.
5987 If we are in an integral constant expression context, then we might
5988 could accept any constant expression; hard to say without analysis.
5989 Rather than open the barn door too wide right away, allow only integer
5990 constant expressions here. */
5991 if (for_offsetof)
5992 index = cp_parser_constant_expression (parser, false, NULL);
5993 else
5995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5997 bool expr_nonconst_p;
5998 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5999 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6001 else
6002 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6005 /* Look for the closing `]'. */
6006 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6008 /* Build the ARRAY_REF. */
6009 postfix_expression = grok_array_decl (loc, postfix_expression, index);
6011 /* When not doing offsetof, array references are not permitted in
6012 constant-expressions. */
6013 if (!for_offsetof
6014 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6015 postfix_expression = error_mark_node;
6017 return postfix_expression;
6020 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6021 by cp_parser_builtin_offsetof. We're looking for
6023 postfix-expression . template [opt] id-expression
6024 postfix-expression . pseudo-destructor-name
6025 postfix-expression -> template [opt] id-expression
6026 postfix-expression -> pseudo-destructor-name
6028 FOR_OFFSETOF is set if we're being called in that context. That sorta
6029 limits what of the above we'll actually accept, but nevermind.
6030 TOKEN_TYPE is the "." or "->" token, which will already have been
6031 removed from the stream. */
6033 static tree
6034 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6035 enum cpp_ttype token_type,
6036 tree postfix_expression,
6037 bool for_offsetof, cp_id_kind *idk,
6038 location_t location)
6040 tree name;
6041 bool dependent_p;
6042 bool pseudo_destructor_p;
6043 tree scope = NULL_TREE;
6045 /* If this is a `->' operator, dereference the pointer. */
6046 if (token_type == CPP_DEREF)
6047 postfix_expression = build_x_arrow (location, postfix_expression,
6048 tf_warning_or_error);
6049 /* Check to see whether or not the expression is type-dependent. */
6050 dependent_p = type_dependent_expression_p (postfix_expression);
6051 /* The identifier following the `->' or `.' is not qualified. */
6052 parser->scope = NULL_TREE;
6053 parser->qualifying_scope = NULL_TREE;
6054 parser->object_scope = NULL_TREE;
6055 *idk = CP_ID_KIND_NONE;
6057 /* Enter the scope corresponding to the type of the object
6058 given by the POSTFIX_EXPRESSION. */
6059 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6061 scope = TREE_TYPE (postfix_expression);
6062 /* According to the standard, no expression should ever have
6063 reference type. Unfortunately, we do not currently match
6064 the standard in this respect in that our internal representation
6065 of an expression may have reference type even when the standard
6066 says it does not. Therefore, we have to manually obtain the
6067 underlying type here. */
6068 scope = non_reference (scope);
6069 /* The type of the POSTFIX_EXPRESSION must be complete. */
6070 if (scope == unknown_type_node)
6072 error_at (location, "%qE does not have class type",
6073 postfix_expression);
6074 scope = NULL_TREE;
6076 /* Unlike the object expression in other contexts, *this is not
6077 required to be of complete type for purposes of class member
6078 access (5.2.5) outside the member function body. */
6079 else if (scope != current_class_ref
6080 && !(processing_template_decl && scope == current_class_type))
6081 scope = complete_type_or_else (scope, NULL_TREE);
6082 /* Let the name lookup machinery know that we are processing a
6083 class member access expression. */
6084 parser->context->object_type = scope;
6085 /* If something went wrong, we want to be able to discern that case,
6086 as opposed to the case where there was no SCOPE due to the type
6087 of expression being dependent. */
6088 if (!scope)
6089 scope = error_mark_node;
6090 /* If the SCOPE was erroneous, make the various semantic analysis
6091 functions exit quickly -- and without issuing additional error
6092 messages. */
6093 if (scope == error_mark_node)
6094 postfix_expression = error_mark_node;
6097 /* Assume this expression is not a pseudo-destructor access. */
6098 pseudo_destructor_p = false;
6100 /* If the SCOPE is a scalar type, then, if this is a valid program,
6101 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6102 is type dependent, it can be pseudo-destructor-name or something else.
6103 Try to parse it as pseudo-destructor-name first. */
6104 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6106 tree s;
6107 tree type;
6109 cp_parser_parse_tentatively (parser);
6110 /* Parse the pseudo-destructor-name. */
6111 s = NULL_TREE;
6112 cp_parser_pseudo_destructor_name (parser, &s, &type);
6113 if (dependent_p
6114 && (cp_parser_error_occurred (parser)
6115 || TREE_CODE (type) != TYPE_DECL
6116 || !SCALAR_TYPE_P (TREE_TYPE (type))))
6117 cp_parser_abort_tentative_parse (parser);
6118 else if (cp_parser_parse_definitely (parser))
6120 pseudo_destructor_p = true;
6121 postfix_expression
6122 = finish_pseudo_destructor_expr (postfix_expression,
6123 s, TREE_TYPE (type));
6127 if (!pseudo_destructor_p)
6129 /* If the SCOPE is not a scalar type, we are looking at an
6130 ordinary class member access expression, rather than a
6131 pseudo-destructor-name. */
6132 bool template_p;
6133 cp_token *token = cp_lexer_peek_token (parser->lexer);
6134 /* Parse the id-expression. */
6135 name = (cp_parser_id_expression
6136 (parser,
6137 cp_parser_optional_template_keyword (parser),
6138 /*check_dependency_p=*/true,
6139 &template_p,
6140 /*declarator_p=*/false,
6141 /*optional_p=*/false));
6142 /* In general, build a SCOPE_REF if the member name is qualified.
6143 However, if the name was not dependent and has already been
6144 resolved; there is no need to build the SCOPE_REF. For example;
6146 struct X { void f(); };
6147 template <typename T> void f(T* t) { t->X::f(); }
6149 Even though "t" is dependent, "X::f" is not and has been resolved
6150 to a BASELINK; there is no need to include scope information. */
6152 /* But we do need to remember that there was an explicit scope for
6153 virtual function calls. */
6154 if (parser->scope)
6155 *idk = CP_ID_KIND_QUALIFIED;
6157 /* If the name is a template-id that names a type, we will get a
6158 TYPE_DECL here. That is invalid code. */
6159 if (TREE_CODE (name) == TYPE_DECL)
6161 error_at (token->location, "invalid use of %qD", name);
6162 postfix_expression = error_mark_node;
6164 else
6166 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6168 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6170 error_at (token->location, "%<%D::%D%> is not a class member",
6171 parser->scope, name);
6172 postfix_expression = error_mark_node;
6174 else
6175 name = build_qualified_name (/*type=*/NULL_TREE,
6176 parser->scope,
6177 name,
6178 template_p);
6179 parser->scope = NULL_TREE;
6180 parser->qualifying_scope = NULL_TREE;
6181 parser->object_scope = NULL_TREE;
6183 if (parser->scope && name && BASELINK_P (name))
6184 adjust_result_of_qualified_name_lookup
6185 (name, parser->scope, scope);
6186 postfix_expression
6187 = finish_class_member_access_expr (postfix_expression, name,
6188 template_p,
6189 tf_warning_or_error);
6193 /* We no longer need to look up names in the scope of the object on
6194 the left-hand side of the `.' or `->' operator. */
6195 parser->context->object_type = NULL_TREE;
6197 /* Outside of offsetof, these operators may not appear in
6198 constant-expressions. */
6199 if (!for_offsetof
6200 && (cp_parser_non_integral_constant_expression
6201 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6202 postfix_expression = error_mark_node;
6204 return postfix_expression;
6207 /* Parse a parenthesized expression-list.
6209 expression-list:
6210 assignment-expression
6211 expression-list, assignment-expression
6213 attribute-list:
6214 expression-list
6215 identifier
6216 identifier, expression-list
6218 CAST_P is true if this expression is the target of a cast.
6220 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6221 argument pack.
6223 Returns a vector of trees. Each element is a representation of an
6224 assignment-expression. NULL is returned if the ( and or ) are
6225 missing. An empty, but allocated, vector is returned on no
6226 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6227 if we are parsing an attribute list for an attribute that wants a
6228 plain identifier argument, normal_attr for an attribute that wants
6229 an expression, or non_attr if we aren't parsing an attribute list. If
6230 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6231 not all of the expressions in the list were constant. */
6233 static vec<tree, va_gc> *
6234 cp_parser_parenthesized_expression_list (cp_parser* parser,
6235 int is_attribute_list,
6236 bool cast_p,
6237 bool allow_expansion_p,
6238 bool *non_constant_p)
6240 vec<tree, va_gc> *expression_list;
6241 bool fold_expr_p = is_attribute_list != non_attr;
6242 tree identifier = NULL_TREE;
6243 bool saved_greater_than_is_operator_p;
6245 /* Assume all the expressions will be constant. */
6246 if (non_constant_p)
6247 *non_constant_p = false;
6249 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6250 return NULL;
6252 expression_list = make_tree_vector ();
6254 /* Within a parenthesized expression, a `>' token is always
6255 the greater-than operator. */
6256 saved_greater_than_is_operator_p
6257 = parser->greater_than_is_operator_p;
6258 parser->greater_than_is_operator_p = true;
6260 /* Consume expressions until there are no more. */
6261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6262 while (true)
6264 tree expr;
6266 /* At the beginning of attribute lists, check to see if the
6267 next token is an identifier. */
6268 if (is_attribute_list == id_attr
6269 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6271 cp_token *token;
6273 /* Consume the identifier. */
6274 token = cp_lexer_consume_token (parser->lexer);
6275 /* Save the identifier. */
6276 identifier = token->u.value;
6278 else
6280 bool expr_non_constant_p;
6282 /* Parse the next assignment-expression. */
6283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6285 /* A braced-init-list. */
6286 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6287 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6288 if (non_constant_p && expr_non_constant_p)
6289 *non_constant_p = true;
6291 else if (non_constant_p)
6293 expr = (cp_parser_constant_expression
6294 (parser, /*allow_non_constant_p=*/true,
6295 &expr_non_constant_p));
6296 if (expr_non_constant_p)
6297 *non_constant_p = true;
6299 else
6300 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6302 if (fold_expr_p)
6303 expr = fold_non_dependent_expr (expr);
6305 /* If we have an ellipsis, then this is an expression
6306 expansion. */
6307 if (allow_expansion_p
6308 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6310 /* Consume the `...'. */
6311 cp_lexer_consume_token (parser->lexer);
6313 /* Build the argument pack. */
6314 expr = make_pack_expansion (expr);
6317 /* Add it to the list. We add error_mark_node
6318 expressions to the list, so that we can still tell if
6319 the correct form for a parenthesized expression-list
6320 is found. That gives better errors. */
6321 vec_safe_push (expression_list, expr);
6323 if (expr == error_mark_node)
6324 goto skip_comma;
6327 /* After the first item, attribute lists look the same as
6328 expression lists. */
6329 is_attribute_list = non_attr;
6331 get_comma:;
6332 /* If the next token isn't a `,', then we are done. */
6333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6334 break;
6336 /* Otherwise, consume the `,' and keep going. */
6337 cp_lexer_consume_token (parser->lexer);
6340 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6342 int ending;
6344 skip_comma:;
6345 /* We try and resync to an unnested comma, as that will give the
6346 user better diagnostics. */
6347 ending = cp_parser_skip_to_closing_parenthesis (parser,
6348 /*recovering=*/true,
6349 /*or_comma=*/true,
6350 /*consume_paren=*/true);
6351 if (ending < 0)
6352 goto get_comma;
6353 if (!ending)
6355 parser->greater_than_is_operator_p
6356 = saved_greater_than_is_operator_p;
6357 return NULL;
6361 parser->greater_than_is_operator_p
6362 = saved_greater_than_is_operator_p;
6364 if (identifier)
6365 vec_safe_insert (expression_list, 0, identifier);
6367 return expression_list;
6370 /* Parse a pseudo-destructor-name.
6372 pseudo-destructor-name:
6373 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6374 :: [opt] nested-name-specifier template template-id :: ~ type-name
6375 :: [opt] nested-name-specifier [opt] ~ type-name
6377 If either of the first two productions is used, sets *SCOPE to the
6378 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6379 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6380 or ERROR_MARK_NODE if the parse fails. */
6382 static void
6383 cp_parser_pseudo_destructor_name (cp_parser* parser,
6384 tree* scope,
6385 tree* type)
6387 bool nested_name_specifier_p;
6389 /* Assume that things will not work out. */
6390 *type = error_mark_node;
6392 /* Look for the optional `::' operator. */
6393 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6394 /* Look for the optional nested-name-specifier. */
6395 nested_name_specifier_p
6396 = (cp_parser_nested_name_specifier_opt (parser,
6397 /*typename_keyword_p=*/false,
6398 /*check_dependency_p=*/true,
6399 /*type_p=*/false,
6400 /*is_declaration=*/false)
6401 != NULL_TREE);
6402 /* Now, if we saw a nested-name-specifier, we might be doing the
6403 second production. */
6404 if (nested_name_specifier_p
6405 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6407 /* Consume the `template' keyword. */
6408 cp_lexer_consume_token (parser->lexer);
6409 /* Parse the template-id. */
6410 cp_parser_template_id (parser,
6411 /*template_keyword_p=*/true,
6412 /*check_dependency_p=*/false,
6413 class_type,
6414 /*is_declaration=*/true);
6415 /* Look for the `::' token. */
6416 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6418 /* If the next token is not a `~', then there might be some
6419 additional qualification. */
6420 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6422 /* At this point, we're looking for "type-name :: ~". The type-name
6423 must not be a class-name, since this is a pseudo-destructor. So,
6424 it must be either an enum-name, or a typedef-name -- both of which
6425 are just identifiers. So, we peek ahead to check that the "::"
6426 and "~" tokens are present; if they are not, then we can avoid
6427 calling type_name. */
6428 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6429 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6430 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6432 cp_parser_error (parser, "non-scalar type");
6433 return;
6436 /* Look for the type-name. */
6437 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6438 if (*scope == error_mark_node)
6439 return;
6441 /* Look for the `::' token. */
6442 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6444 else
6445 *scope = NULL_TREE;
6447 /* Look for the `~'. */
6448 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6450 /* Once we see the ~, this has to be a pseudo-destructor. */
6451 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6452 cp_parser_commit_to_tentative_parse (parser);
6454 /* Look for the type-name again. We are not responsible for
6455 checking that it matches the first type-name. */
6456 *type = cp_parser_nonclass_name (parser);
6459 /* Parse a unary-expression.
6461 unary-expression:
6462 postfix-expression
6463 ++ cast-expression
6464 -- cast-expression
6465 unary-operator cast-expression
6466 sizeof unary-expression
6467 sizeof ( type-id )
6468 alignof ( type-id ) [C++0x]
6469 new-expression
6470 delete-expression
6472 GNU Extensions:
6474 unary-expression:
6475 __extension__ cast-expression
6476 __alignof__ unary-expression
6477 __alignof__ ( type-id )
6478 alignof unary-expression [C++0x]
6479 __real__ cast-expression
6480 __imag__ cast-expression
6481 && identifier
6483 ADDRESS_P is true iff the unary-expression is appearing as the
6484 operand of the `&' operator. CAST_P is true if this expression is
6485 the target of a cast.
6487 Returns a representation of the expression. */
6489 static tree
6490 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6491 bool decltype_p, cp_id_kind * pidk)
6493 cp_token *token;
6494 enum tree_code unary_operator;
6496 /* Peek at the next token. */
6497 token = cp_lexer_peek_token (parser->lexer);
6498 /* Some keywords give away the kind of expression. */
6499 if (token->type == CPP_KEYWORD)
6501 enum rid keyword = token->keyword;
6503 switch (keyword)
6505 case RID_ALIGNOF:
6506 case RID_SIZEOF:
6508 tree operand, ret;
6509 enum tree_code op;
6510 location_t first_loc;
6512 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6513 /* Consume the token. */
6514 cp_lexer_consume_token (parser->lexer);
6515 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6516 /* Parse the operand. */
6517 operand = cp_parser_sizeof_operand (parser, keyword);
6519 if (TYPE_P (operand))
6520 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6521 else
6523 /* ISO C++ defines alignof only with types, not with
6524 expressions. So pedwarn if alignof is used with a non-
6525 type expression. However, __alignof__ is ok. */
6526 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6527 pedwarn (token->location, OPT_Wpedantic,
6528 "ISO C++ does not allow %<alignof%> "
6529 "with a non-type");
6531 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6533 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6534 SIZEOF_EXPR with the original operand. */
6535 if (op == SIZEOF_EXPR && ret != error_mark_node)
6537 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6539 if (!processing_template_decl && TYPE_P (operand))
6541 ret = build_min (SIZEOF_EXPR, size_type_node,
6542 build1 (NOP_EXPR, operand,
6543 error_mark_node));
6544 SIZEOF_EXPR_TYPE_P (ret) = 1;
6546 else
6547 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6548 TREE_SIDE_EFFECTS (ret) = 0;
6549 TREE_READONLY (ret) = 1;
6551 SET_EXPR_LOCATION (ret, first_loc);
6553 return ret;
6556 case RID_NEW:
6557 return cp_parser_new_expression (parser);
6559 case RID_DELETE:
6560 return cp_parser_delete_expression (parser);
6562 case RID_EXTENSION:
6564 /* The saved value of the PEDANTIC flag. */
6565 int saved_pedantic;
6566 tree expr;
6568 /* Save away the PEDANTIC flag. */
6569 cp_parser_extension_opt (parser, &saved_pedantic);
6570 /* Parse the cast-expression. */
6571 expr = cp_parser_simple_cast_expression (parser);
6572 /* Restore the PEDANTIC flag. */
6573 pedantic = saved_pedantic;
6575 return expr;
6578 case RID_REALPART:
6579 case RID_IMAGPART:
6581 tree expression;
6583 /* Consume the `__real__' or `__imag__' token. */
6584 cp_lexer_consume_token (parser->lexer);
6585 /* Parse the cast-expression. */
6586 expression = cp_parser_simple_cast_expression (parser);
6587 /* Create the complete representation. */
6588 return build_x_unary_op (token->location,
6589 (keyword == RID_REALPART
6590 ? REALPART_EXPR : IMAGPART_EXPR),
6591 expression,
6592 tf_warning_or_error);
6594 break;
6596 case RID_TRANSACTION_ATOMIC:
6597 case RID_TRANSACTION_RELAXED:
6598 return cp_parser_transaction_expression (parser, keyword);
6600 case RID_NOEXCEPT:
6602 tree expr;
6603 const char *saved_message;
6604 bool saved_integral_constant_expression_p;
6605 bool saved_non_integral_constant_expression_p;
6606 bool saved_greater_than_is_operator_p;
6608 cp_lexer_consume_token (parser->lexer);
6609 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6611 saved_message = parser->type_definition_forbidden_message;
6612 parser->type_definition_forbidden_message
6613 = G_("types may not be defined in %<noexcept%> expressions");
6615 saved_integral_constant_expression_p
6616 = parser->integral_constant_expression_p;
6617 saved_non_integral_constant_expression_p
6618 = parser->non_integral_constant_expression_p;
6619 parser->integral_constant_expression_p = false;
6621 saved_greater_than_is_operator_p
6622 = parser->greater_than_is_operator_p;
6623 parser->greater_than_is_operator_p = true;
6625 ++cp_unevaluated_operand;
6626 ++c_inhibit_evaluation_warnings;
6627 expr = cp_parser_expression (parser, false, NULL);
6628 --c_inhibit_evaluation_warnings;
6629 --cp_unevaluated_operand;
6631 parser->greater_than_is_operator_p
6632 = saved_greater_than_is_operator_p;
6634 parser->integral_constant_expression_p
6635 = saved_integral_constant_expression_p;
6636 parser->non_integral_constant_expression_p
6637 = saved_non_integral_constant_expression_p;
6639 parser->type_definition_forbidden_message = saved_message;
6641 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6642 return finish_noexcept_expr (expr, tf_warning_or_error);
6645 default:
6646 break;
6650 /* Look for the `:: new' and `:: delete', which also signal the
6651 beginning of a new-expression, or delete-expression,
6652 respectively. If the next token is `::', then it might be one of
6653 these. */
6654 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6656 enum rid keyword;
6658 /* See if the token after the `::' is one of the keywords in
6659 which we're interested. */
6660 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6661 /* If it's `new', we have a new-expression. */
6662 if (keyword == RID_NEW)
6663 return cp_parser_new_expression (parser);
6664 /* Similarly, for `delete'. */
6665 else if (keyword == RID_DELETE)
6666 return cp_parser_delete_expression (parser);
6669 /* Look for a unary operator. */
6670 unary_operator = cp_parser_unary_operator (token);
6671 /* The `++' and `--' operators can be handled similarly, even though
6672 they are not technically unary-operators in the grammar. */
6673 if (unary_operator == ERROR_MARK)
6675 if (token->type == CPP_PLUS_PLUS)
6676 unary_operator = PREINCREMENT_EXPR;
6677 else if (token->type == CPP_MINUS_MINUS)
6678 unary_operator = PREDECREMENT_EXPR;
6679 /* Handle the GNU address-of-label extension. */
6680 else if (cp_parser_allow_gnu_extensions_p (parser)
6681 && token->type == CPP_AND_AND)
6683 tree identifier;
6684 tree expression;
6685 location_t loc = token->location;
6687 /* Consume the '&&' token. */
6688 cp_lexer_consume_token (parser->lexer);
6689 /* Look for the identifier. */
6690 identifier = cp_parser_identifier (parser);
6691 /* Create an expression representing the address. */
6692 expression = finish_label_address_expr (identifier, loc);
6693 if (cp_parser_non_integral_constant_expression (parser,
6694 NIC_ADDR_LABEL))
6695 expression = error_mark_node;
6696 return expression;
6699 if (unary_operator != ERROR_MARK)
6701 tree cast_expression;
6702 tree expression = error_mark_node;
6703 non_integral_constant non_constant_p = NIC_NONE;
6704 location_t loc = token->location;
6706 /* Consume the operator token. */
6707 token = cp_lexer_consume_token (parser->lexer);
6708 /* Parse the cast-expression. */
6709 cast_expression
6710 = cp_parser_cast_expression (parser,
6711 unary_operator == ADDR_EXPR,
6712 /*cast_p=*/false,
6713 /*decltype*/false,
6714 pidk);
6715 /* Now, build an appropriate representation. */
6716 switch (unary_operator)
6718 case INDIRECT_REF:
6719 non_constant_p = NIC_STAR;
6720 expression = build_x_indirect_ref (loc, cast_expression,
6721 RO_UNARY_STAR,
6722 tf_warning_or_error);
6723 break;
6725 case ADDR_EXPR:
6726 non_constant_p = NIC_ADDR;
6727 /* Fall through. */
6728 case BIT_NOT_EXPR:
6729 expression = build_x_unary_op (loc, unary_operator,
6730 cast_expression,
6731 tf_warning_or_error);
6732 break;
6734 case PREINCREMENT_EXPR:
6735 case PREDECREMENT_EXPR:
6736 non_constant_p = unary_operator == PREINCREMENT_EXPR
6737 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6738 /* Fall through. */
6739 case UNARY_PLUS_EXPR:
6740 case NEGATE_EXPR:
6741 case TRUTH_NOT_EXPR:
6742 expression = finish_unary_op_expr (loc, unary_operator,
6743 cast_expression);
6744 break;
6746 default:
6747 gcc_unreachable ();
6750 if (non_constant_p != NIC_NONE
6751 && cp_parser_non_integral_constant_expression (parser,
6752 non_constant_p))
6753 expression = error_mark_node;
6755 return expression;
6758 return cp_parser_postfix_expression (parser, address_p, cast_p,
6759 /*member_access_only_p=*/false,
6760 decltype_p,
6761 pidk);
6764 static inline tree
6765 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6766 cp_id_kind * pidk)
6768 return cp_parser_unary_expression (parser, address_p, cast_p,
6769 /*decltype*/false, pidk);
6772 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6773 unary-operator, the corresponding tree code is returned. */
6775 static enum tree_code
6776 cp_parser_unary_operator (cp_token* token)
6778 switch (token->type)
6780 case CPP_MULT:
6781 return INDIRECT_REF;
6783 case CPP_AND:
6784 return ADDR_EXPR;
6786 case CPP_PLUS:
6787 return UNARY_PLUS_EXPR;
6789 case CPP_MINUS:
6790 return NEGATE_EXPR;
6792 case CPP_NOT:
6793 return TRUTH_NOT_EXPR;
6795 case CPP_COMPL:
6796 return BIT_NOT_EXPR;
6798 default:
6799 return ERROR_MARK;
6803 /* Parse a new-expression.
6805 new-expression:
6806 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6807 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6809 Returns a representation of the expression. */
6811 static tree
6812 cp_parser_new_expression (cp_parser* parser)
6814 bool global_scope_p;
6815 vec<tree, va_gc> *placement;
6816 tree type;
6817 vec<tree, va_gc> *initializer;
6818 tree nelts = NULL_TREE;
6819 tree ret;
6821 /* Look for the optional `::' operator. */
6822 global_scope_p
6823 = (cp_parser_global_scope_opt (parser,
6824 /*current_scope_valid_p=*/false)
6825 != NULL_TREE);
6826 /* Look for the `new' operator. */
6827 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6828 /* There's no easy way to tell a new-placement from the
6829 `( type-id )' construct. */
6830 cp_parser_parse_tentatively (parser);
6831 /* Look for a new-placement. */
6832 placement = cp_parser_new_placement (parser);
6833 /* If that didn't work out, there's no new-placement. */
6834 if (!cp_parser_parse_definitely (parser))
6836 if (placement != NULL)
6837 release_tree_vector (placement);
6838 placement = NULL;
6841 /* If the next token is a `(', then we have a parenthesized
6842 type-id. */
6843 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6845 cp_token *token;
6846 const char *saved_message = parser->type_definition_forbidden_message;
6848 /* Consume the `('. */
6849 cp_lexer_consume_token (parser->lexer);
6851 /* Parse the type-id. */
6852 parser->type_definition_forbidden_message
6853 = G_("types may not be defined in a new-expression");
6854 type = cp_parser_type_id (parser);
6855 parser->type_definition_forbidden_message = saved_message;
6857 /* Look for the closing `)'. */
6858 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6859 token = cp_lexer_peek_token (parser->lexer);
6860 /* There should not be a direct-new-declarator in this production,
6861 but GCC used to allowed this, so we check and emit a sensible error
6862 message for this case. */
6863 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6865 error_at (token->location,
6866 "array bound forbidden after parenthesized type-id");
6867 inform (token->location,
6868 "try removing the parentheses around the type-id");
6869 cp_parser_direct_new_declarator (parser);
6872 /* Otherwise, there must be a new-type-id. */
6873 else
6874 type = cp_parser_new_type_id (parser, &nelts);
6876 /* If the next token is a `(' or '{', then we have a new-initializer. */
6877 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6878 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6879 initializer = cp_parser_new_initializer (parser);
6880 else
6881 initializer = NULL;
6883 /* A new-expression may not appear in an integral constant
6884 expression. */
6885 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6886 ret = error_mark_node;
6887 else
6889 /* Create a representation of the new-expression. */
6890 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6891 tf_warning_or_error);
6894 if (placement != NULL)
6895 release_tree_vector (placement);
6896 if (initializer != NULL)
6897 release_tree_vector (initializer);
6899 return ret;
6902 /* Parse a new-placement.
6904 new-placement:
6905 ( expression-list )
6907 Returns the same representation as for an expression-list. */
6909 static vec<tree, va_gc> *
6910 cp_parser_new_placement (cp_parser* parser)
6912 vec<tree, va_gc> *expression_list;
6914 /* Parse the expression-list. */
6915 expression_list = (cp_parser_parenthesized_expression_list
6916 (parser, non_attr, /*cast_p=*/false,
6917 /*allow_expansion_p=*/true,
6918 /*non_constant_p=*/NULL));
6920 return expression_list;
6923 /* Parse a new-type-id.
6925 new-type-id:
6926 type-specifier-seq new-declarator [opt]
6928 Returns the TYPE allocated. If the new-type-id indicates an array
6929 type, *NELTS is set to the number of elements in the last array
6930 bound; the TYPE will not include the last array bound. */
6932 static tree
6933 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6935 cp_decl_specifier_seq type_specifier_seq;
6936 cp_declarator *new_declarator;
6937 cp_declarator *declarator;
6938 cp_declarator *outer_declarator;
6939 const char *saved_message;
6941 /* The type-specifier sequence must not contain type definitions.
6942 (It cannot contain declarations of new types either, but if they
6943 are not definitions we will catch that because they are not
6944 complete.) */
6945 saved_message = parser->type_definition_forbidden_message;
6946 parser->type_definition_forbidden_message
6947 = G_("types may not be defined in a new-type-id");
6948 /* Parse the type-specifier-seq. */
6949 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6950 /*is_trailing_return=*/false,
6951 &type_specifier_seq);
6952 /* Restore the old message. */
6953 parser->type_definition_forbidden_message = saved_message;
6955 if (type_specifier_seq.type == error_mark_node)
6956 return error_mark_node;
6958 /* Parse the new-declarator. */
6959 new_declarator = cp_parser_new_declarator_opt (parser);
6961 /* Determine the number of elements in the last array dimension, if
6962 any. */
6963 *nelts = NULL_TREE;
6964 /* Skip down to the last array dimension. */
6965 declarator = new_declarator;
6966 outer_declarator = NULL;
6967 while (declarator && (declarator->kind == cdk_pointer
6968 || declarator->kind == cdk_ptrmem))
6970 outer_declarator = declarator;
6971 declarator = declarator->declarator;
6973 while (declarator
6974 && declarator->kind == cdk_array
6975 && declarator->declarator
6976 && declarator->declarator->kind == cdk_array)
6978 outer_declarator = declarator;
6979 declarator = declarator->declarator;
6982 if (declarator && declarator->kind == cdk_array)
6984 *nelts = declarator->u.array.bounds;
6985 if (*nelts == error_mark_node)
6986 *nelts = integer_one_node;
6988 if (outer_declarator)
6989 outer_declarator->declarator = declarator->declarator;
6990 else
6991 new_declarator = NULL;
6994 return groktypename (&type_specifier_seq, new_declarator, false);
6997 /* Parse an (optional) new-declarator.
6999 new-declarator:
7000 ptr-operator new-declarator [opt]
7001 direct-new-declarator
7003 Returns the declarator. */
7005 static cp_declarator *
7006 cp_parser_new_declarator_opt (cp_parser* parser)
7008 enum tree_code code;
7009 tree type, std_attributes = NULL_TREE;
7010 cp_cv_quals cv_quals;
7012 /* We don't know if there's a ptr-operator next, or not. */
7013 cp_parser_parse_tentatively (parser);
7014 /* Look for a ptr-operator. */
7015 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7016 /* If that worked, look for more new-declarators. */
7017 if (cp_parser_parse_definitely (parser))
7019 cp_declarator *declarator;
7021 /* Parse another optional declarator. */
7022 declarator = cp_parser_new_declarator_opt (parser);
7024 declarator = cp_parser_make_indirect_declarator
7025 (code, type, cv_quals, declarator, std_attributes);
7027 return declarator;
7030 /* If the next token is a `[', there is a direct-new-declarator. */
7031 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7032 return cp_parser_direct_new_declarator (parser);
7034 return NULL;
7037 /* Parse a direct-new-declarator.
7039 direct-new-declarator:
7040 [ expression ]
7041 direct-new-declarator [constant-expression]
7045 static cp_declarator *
7046 cp_parser_direct_new_declarator (cp_parser* parser)
7048 cp_declarator *declarator = NULL;
7050 while (true)
7052 tree expression;
7053 cp_token *token;
7055 /* Look for the opening `['. */
7056 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7058 token = cp_lexer_peek_token (parser->lexer);
7059 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7060 /* The standard requires that the expression have integral
7061 type. DR 74 adds enumeration types. We believe that the
7062 real intent is that these expressions be handled like the
7063 expression in a `switch' condition, which also allows
7064 classes with a single conversion to integral or
7065 enumeration type. */
7066 if (!processing_template_decl)
7068 expression
7069 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7070 expression,
7071 /*complain=*/true);
7072 if (!expression)
7074 error_at (token->location,
7075 "expression in new-declarator must have integral "
7076 "or enumeration type");
7077 expression = error_mark_node;
7081 /* Look for the closing `]'. */
7082 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7084 /* Add this bound to the declarator. */
7085 declarator = make_array_declarator (declarator, expression);
7087 /* If the next token is not a `[', then there are no more
7088 bounds. */
7089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7090 break;
7093 return declarator;
7096 /* Parse a new-initializer.
7098 new-initializer:
7099 ( expression-list [opt] )
7100 braced-init-list
7102 Returns a representation of the expression-list. */
7104 static vec<tree, va_gc> *
7105 cp_parser_new_initializer (cp_parser* parser)
7107 vec<tree, va_gc> *expression_list;
7109 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7111 tree t;
7112 bool expr_non_constant_p;
7113 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7114 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7115 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7116 expression_list = make_tree_vector_single (t);
7118 else
7119 expression_list = (cp_parser_parenthesized_expression_list
7120 (parser, non_attr, /*cast_p=*/false,
7121 /*allow_expansion_p=*/true,
7122 /*non_constant_p=*/NULL));
7124 return expression_list;
7127 /* Parse a delete-expression.
7129 delete-expression:
7130 :: [opt] delete cast-expression
7131 :: [opt] delete [ ] cast-expression
7133 Returns a representation of the expression. */
7135 static tree
7136 cp_parser_delete_expression (cp_parser* parser)
7138 bool global_scope_p;
7139 bool array_p;
7140 tree expression;
7142 /* Look for the optional `::' operator. */
7143 global_scope_p
7144 = (cp_parser_global_scope_opt (parser,
7145 /*current_scope_valid_p=*/false)
7146 != NULL_TREE);
7147 /* Look for the `delete' keyword. */
7148 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7149 /* See if the array syntax is in use. */
7150 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7152 /* Consume the `[' token. */
7153 cp_lexer_consume_token (parser->lexer);
7154 /* Look for the `]' token. */
7155 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7156 /* Remember that this is the `[]' construct. */
7157 array_p = true;
7159 else
7160 array_p = false;
7162 /* Parse the cast-expression. */
7163 expression = cp_parser_simple_cast_expression (parser);
7165 /* A delete-expression may not appear in an integral constant
7166 expression. */
7167 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7168 return error_mark_node;
7170 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7171 tf_warning_or_error);
7174 /* Returns true if TOKEN may start a cast-expression and false
7175 otherwise. */
7177 static bool
7178 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7180 cp_token *token = cp_lexer_peek_token (parser->lexer);
7181 switch (token->type)
7183 case CPP_COMMA:
7184 case CPP_SEMICOLON:
7185 case CPP_QUERY:
7186 case CPP_COLON:
7187 case CPP_CLOSE_SQUARE:
7188 case CPP_CLOSE_PAREN:
7189 case CPP_CLOSE_BRACE:
7190 case CPP_DOT:
7191 case CPP_DOT_STAR:
7192 case CPP_DEREF:
7193 case CPP_DEREF_STAR:
7194 case CPP_DIV:
7195 case CPP_MOD:
7196 case CPP_LSHIFT:
7197 case CPP_RSHIFT:
7198 case CPP_LESS:
7199 case CPP_GREATER:
7200 case CPP_LESS_EQ:
7201 case CPP_GREATER_EQ:
7202 case CPP_EQ_EQ:
7203 case CPP_NOT_EQ:
7204 case CPP_EQ:
7205 case CPP_MULT_EQ:
7206 case CPP_DIV_EQ:
7207 case CPP_MOD_EQ:
7208 case CPP_PLUS_EQ:
7209 case CPP_MINUS_EQ:
7210 case CPP_RSHIFT_EQ:
7211 case CPP_LSHIFT_EQ:
7212 case CPP_AND_EQ:
7213 case CPP_XOR_EQ:
7214 case CPP_OR_EQ:
7215 case CPP_XOR:
7216 case CPP_OR:
7217 case CPP_OR_OR:
7218 case CPP_EOF:
7219 return false;
7221 case CPP_OPEN_PAREN:
7222 /* In ((type ()) () the last () isn't a valid cast-expression,
7223 so the whole must be parsed as postfix-expression. */
7224 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7225 != CPP_CLOSE_PAREN;
7227 /* '[' may start a primary-expression in obj-c++. */
7228 case CPP_OPEN_SQUARE:
7229 return c_dialect_objc ();
7231 default:
7232 return true;
7236 /* Parse a cast-expression.
7238 cast-expression:
7239 unary-expression
7240 ( type-id ) cast-expression
7242 ADDRESS_P is true iff the unary-expression is appearing as the
7243 operand of the `&' operator. CAST_P is true if this expression is
7244 the target of a cast.
7246 Returns a representation of the expression. */
7248 static tree
7249 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7250 bool decltype_p, cp_id_kind * pidk)
7252 /* If it's a `(', then we might be looking at a cast. */
7253 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7255 tree type = NULL_TREE;
7256 tree expr = NULL_TREE;
7257 bool compound_literal_p;
7258 const char *saved_message;
7260 /* There's no way to know yet whether or not this is a cast.
7261 For example, `(int (3))' is a unary-expression, while `(int)
7262 3' is a cast. So, we resort to parsing tentatively. */
7263 cp_parser_parse_tentatively (parser);
7264 /* Types may not be defined in a cast. */
7265 saved_message = parser->type_definition_forbidden_message;
7266 parser->type_definition_forbidden_message
7267 = G_("types may not be defined in casts");
7268 /* Consume the `('. */
7269 cp_lexer_consume_token (parser->lexer);
7270 /* A very tricky bit is that `(struct S) { 3 }' is a
7271 compound-literal (which we permit in C++ as an extension).
7272 But, that construct is not a cast-expression -- it is a
7273 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7274 is legal; if the compound-literal were a cast-expression,
7275 you'd need an extra set of parentheses.) But, if we parse
7276 the type-id, and it happens to be a class-specifier, then we
7277 will commit to the parse at that point, because we cannot
7278 undo the action that is done when creating a new class. So,
7279 then we cannot back up and do a postfix-expression.
7281 Therefore, we scan ahead to the closing `)', and check to see
7282 if the token after the `)' is a `{'. If so, we are not
7283 looking at a cast-expression.
7285 Save tokens so that we can put them back. */
7286 cp_lexer_save_tokens (parser->lexer);
7287 /* Skip tokens until the next token is a closing parenthesis.
7288 If we find the closing `)', and the next token is a `{', then
7289 we are looking at a compound-literal. */
7290 compound_literal_p
7291 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7292 /*consume_paren=*/true)
7293 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7294 /* Roll back the tokens we skipped. */
7295 cp_lexer_rollback_tokens (parser->lexer);
7296 /* If we were looking at a compound-literal, simulate an error
7297 so that the call to cp_parser_parse_definitely below will
7298 fail. */
7299 if (compound_literal_p)
7300 cp_parser_simulate_error (parser);
7301 else
7303 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7304 parser->in_type_id_in_expr_p = true;
7305 /* Look for the type-id. */
7306 type = cp_parser_type_id (parser);
7307 /* Look for the closing `)'. */
7308 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7309 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7312 /* Restore the saved message. */
7313 parser->type_definition_forbidden_message = saved_message;
7315 /* At this point this can only be either a cast or a
7316 parenthesized ctor such as `(T ())' that looks like a cast to
7317 function returning T. */
7318 if (!cp_parser_error_occurred (parser)
7319 && cp_parser_tokens_start_cast_expression (parser))
7321 cp_parser_parse_definitely (parser);
7322 expr = cp_parser_cast_expression (parser,
7323 /*address_p=*/false,
7324 /*cast_p=*/true,
7325 /*decltype_p=*/false,
7326 pidk);
7328 /* Warn about old-style casts, if so requested. */
7329 if (warn_old_style_cast
7330 && !in_system_header
7331 && !VOID_TYPE_P (type)
7332 && current_lang_name != lang_name_c)
7333 warning (OPT_Wold_style_cast, "use of old-style cast");
7335 /* Only type conversions to integral or enumeration types
7336 can be used in constant-expressions. */
7337 if (!cast_valid_in_integral_constant_expression_p (type)
7338 && cp_parser_non_integral_constant_expression (parser,
7339 NIC_CAST))
7340 return error_mark_node;
7342 /* Perform the cast. */
7343 expr = build_c_cast (input_location, type, expr);
7344 return expr;
7346 else
7347 cp_parser_abort_tentative_parse (parser);
7350 /* If we get here, then it's not a cast, so it must be a
7351 unary-expression. */
7352 return cp_parser_unary_expression (parser, address_p, cast_p,
7353 decltype_p, pidk);
7356 /* Parse a binary expression of the general form:
7358 pm-expression:
7359 cast-expression
7360 pm-expression .* cast-expression
7361 pm-expression ->* cast-expression
7363 multiplicative-expression:
7364 pm-expression
7365 multiplicative-expression * pm-expression
7366 multiplicative-expression / pm-expression
7367 multiplicative-expression % pm-expression
7369 additive-expression:
7370 multiplicative-expression
7371 additive-expression + multiplicative-expression
7372 additive-expression - multiplicative-expression
7374 shift-expression:
7375 additive-expression
7376 shift-expression << additive-expression
7377 shift-expression >> additive-expression
7379 relational-expression:
7380 shift-expression
7381 relational-expression < shift-expression
7382 relational-expression > shift-expression
7383 relational-expression <= shift-expression
7384 relational-expression >= shift-expression
7386 GNU Extension:
7388 relational-expression:
7389 relational-expression <? shift-expression
7390 relational-expression >? shift-expression
7392 equality-expression:
7393 relational-expression
7394 equality-expression == relational-expression
7395 equality-expression != relational-expression
7397 and-expression:
7398 equality-expression
7399 and-expression & equality-expression
7401 exclusive-or-expression:
7402 and-expression
7403 exclusive-or-expression ^ and-expression
7405 inclusive-or-expression:
7406 exclusive-or-expression
7407 inclusive-or-expression | exclusive-or-expression
7409 logical-and-expression:
7410 inclusive-or-expression
7411 logical-and-expression && inclusive-or-expression
7413 logical-or-expression:
7414 logical-and-expression
7415 logical-or-expression || logical-and-expression
7417 All these are implemented with a single function like:
7419 binary-expression:
7420 simple-cast-expression
7421 binary-expression <token> binary-expression
7423 CAST_P is true if this expression is the target of a cast.
7425 The binops_by_token map is used to get the tree codes for each <token> type.
7426 binary-expressions are associated according to a precedence table. */
7428 #define TOKEN_PRECEDENCE(token) \
7429 (((token->type == CPP_GREATER \
7430 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7431 && !parser->greater_than_is_operator_p) \
7432 ? PREC_NOT_OPERATOR \
7433 : binops_by_token[token->type].prec)
7435 static tree
7436 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7437 bool no_toplevel_fold_p,
7438 bool decltype_p,
7439 enum cp_parser_prec prec,
7440 cp_id_kind * pidk)
7442 cp_parser_expression_stack stack;
7443 cp_parser_expression_stack_entry *sp = &stack[0];
7444 cp_parser_expression_stack_entry current;
7445 tree rhs;
7446 cp_token *token;
7447 enum tree_code rhs_type;
7448 enum cp_parser_prec new_prec, lookahead_prec;
7449 tree overload;
7451 /* Parse the first expression. */
7452 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7453 cast_p, decltype_p, pidk);
7454 current.lhs_type = ERROR_MARK;
7455 current.prec = prec;
7457 if (cp_parser_error_occurred (parser))
7458 return error_mark_node;
7460 for (;;)
7462 /* Get an operator token. */
7463 token = cp_lexer_peek_token (parser->lexer);
7465 if (warn_cxx0x_compat
7466 && token->type == CPP_RSHIFT
7467 && !parser->greater_than_is_operator_p)
7469 if (warning_at (token->location, OPT_Wc__0x_compat,
7470 "%<>>%> operator is treated"
7471 " as two right angle brackets in C++11"))
7472 inform (token->location,
7473 "suggest parentheses around %<>>%> expression");
7476 new_prec = TOKEN_PRECEDENCE (token);
7478 /* Popping an entry off the stack means we completed a subexpression:
7479 - either we found a token which is not an operator (`>' where it is not
7480 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7481 will happen repeatedly;
7482 - or, we found an operator which has lower priority. This is the case
7483 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7484 parsing `3 * 4'. */
7485 if (new_prec <= current.prec)
7487 if (sp == stack)
7488 break;
7489 else
7490 goto pop;
7493 get_rhs:
7494 current.tree_type = binops_by_token[token->type].tree_type;
7495 current.loc = token->location;
7497 /* We used the operator token. */
7498 cp_lexer_consume_token (parser->lexer);
7500 /* For "false && x" or "true || x", x will never be executed;
7501 disable warnings while evaluating it. */
7502 if (current.tree_type == TRUTH_ANDIF_EXPR)
7503 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7504 else if (current.tree_type == TRUTH_ORIF_EXPR)
7505 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7507 /* Extract another operand. It may be the RHS of this expression
7508 or the LHS of a new, higher priority expression. */
7509 rhs = cp_parser_simple_cast_expression (parser);
7510 rhs_type = ERROR_MARK;
7512 /* Get another operator token. Look up its precedence to avoid
7513 building a useless (immediately popped) stack entry for common
7514 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7515 token = cp_lexer_peek_token (parser->lexer);
7516 lookahead_prec = TOKEN_PRECEDENCE (token);
7517 if (lookahead_prec > new_prec)
7519 /* ... and prepare to parse the RHS of the new, higher priority
7520 expression. Since precedence levels on the stack are
7521 monotonically increasing, we do not have to care about
7522 stack overflows. */
7523 *sp = current;
7524 ++sp;
7525 current.lhs = rhs;
7526 current.lhs_type = rhs_type;
7527 current.prec = new_prec;
7528 new_prec = lookahead_prec;
7529 goto get_rhs;
7531 pop:
7532 lookahead_prec = new_prec;
7533 /* If the stack is not empty, we have parsed into LHS the right side
7534 (`4' in the example above) of an expression we had suspended.
7535 We can use the information on the stack to recover the LHS (`3')
7536 from the stack together with the tree code (`MULT_EXPR'), and
7537 the precedence of the higher level subexpression
7538 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7539 which will be used to actually build the additive expression. */
7540 rhs = current.lhs;
7541 rhs_type = current.lhs_type;
7542 --sp;
7543 current = *sp;
7546 /* Undo the disabling of warnings done above. */
7547 if (current.tree_type == TRUTH_ANDIF_EXPR)
7548 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7549 else if (current.tree_type == TRUTH_ORIF_EXPR)
7550 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7552 overload = NULL;
7553 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7554 ERROR_MARK for everything that is not a binary expression.
7555 This makes warn_about_parentheses miss some warnings that
7556 involve unary operators. For unary expressions we should
7557 pass the correct tree_code unless the unary expression was
7558 surrounded by parentheses.
7560 if (no_toplevel_fold_p
7561 && lookahead_prec <= current.prec
7562 && sp == stack
7563 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7564 current.lhs = build2 (current.tree_type, boolean_type_node,
7565 current.lhs, rhs);
7566 else
7567 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7568 current.lhs, current.lhs_type,
7569 rhs, rhs_type, &overload,
7570 tf_warning_or_error);
7571 current.lhs_type = current.tree_type;
7572 if (EXPR_P (current.lhs))
7573 SET_EXPR_LOCATION (current.lhs, current.loc);
7575 /* If the binary operator required the use of an overloaded operator,
7576 then this expression cannot be an integral constant-expression.
7577 An overloaded operator can be used even if both operands are
7578 otherwise permissible in an integral constant-expression if at
7579 least one of the operands is of enumeration type. */
7581 if (overload
7582 && cp_parser_non_integral_constant_expression (parser,
7583 NIC_OVERLOADED))
7584 return error_mark_node;
7587 return current.lhs;
7590 static tree
7591 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7592 bool no_toplevel_fold_p,
7593 enum cp_parser_prec prec,
7594 cp_id_kind * pidk)
7596 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7597 /*decltype*/false, prec, pidk);
7600 /* Parse the `? expression : assignment-expression' part of a
7601 conditional-expression. The LOGICAL_OR_EXPR is the
7602 logical-or-expression that started the conditional-expression.
7603 Returns a representation of the entire conditional-expression.
7605 This routine is used by cp_parser_assignment_expression.
7607 ? expression : assignment-expression
7609 GNU Extensions:
7611 ? : assignment-expression */
7613 static tree
7614 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7616 tree expr;
7617 tree assignment_expr;
7618 struct cp_token *token;
7619 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7621 /* Consume the `?' token. */
7622 cp_lexer_consume_token (parser->lexer);
7623 token = cp_lexer_peek_token (parser->lexer);
7624 if (cp_parser_allow_gnu_extensions_p (parser)
7625 && token->type == CPP_COLON)
7627 pedwarn (token->location, OPT_Wpedantic,
7628 "ISO C++ does not allow ?: with omitted middle operand");
7629 /* Implicit true clause. */
7630 expr = NULL_TREE;
7631 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7632 warn_for_omitted_condop (token->location, logical_or_expr);
7634 else
7636 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7637 parser->colon_corrects_to_scope_p = false;
7638 /* Parse the expression. */
7639 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7640 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7641 c_inhibit_evaluation_warnings +=
7642 ((logical_or_expr == truthvalue_true_node)
7643 - (logical_or_expr == truthvalue_false_node));
7644 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7647 /* The next token should be a `:'. */
7648 cp_parser_require (parser, CPP_COLON, RT_COLON);
7649 /* Parse the assignment-expression. */
7650 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7651 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7653 /* Build the conditional-expression. */
7654 return build_x_conditional_expr (loc, logical_or_expr,
7655 expr,
7656 assignment_expr,
7657 tf_warning_or_error);
7660 /* Parse an assignment-expression.
7662 assignment-expression:
7663 conditional-expression
7664 logical-or-expression assignment-operator assignment_expression
7665 throw-expression
7667 CAST_P is true if this expression is the target of a cast.
7668 DECLTYPE_P is true if this expression is the operand of decltype.
7670 Returns a representation for the expression. */
7672 static tree
7673 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7674 bool decltype_p, cp_id_kind * pidk)
7676 tree expr;
7678 /* If the next token is the `throw' keyword, then we're looking at
7679 a throw-expression. */
7680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7681 expr = cp_parser_throw_expression (parser);
7682 /* Otherwise, it must be that we are looking at a
7683 logical-or-expression. */
7684 else
7686 /* Parse the binary expressions (logical-or-expression). */
7687 expr = cp_parser_binary_expression (parser, cast_p, false,
7688 decltype_p,
7689 PREC_NOT_OPERATOR, pidk);
7690 /* If the next token is a `?' then we're actually looking at a
7691 conditional-expression. */
7692 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7693 return cp_parser_question_colon_clause (parser, expr);
7694 else
7696 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7698 /* If it's an assignment-operator, we're using the second
7699 production. */
7700 enum tree_code assignment_operator
7701 = cp_parser_assignment_operator_opt (parser);
7702 if (assignment_operator != ERROR_MARK)
7704 bool non_constant_p;
7705 location_t saved_input_location;
7707 /* Parse the right-hand side of the assignment. */
7708 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7710 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7711 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7713 /* An assignment may not appear in a
7714 constant-expression. */
7715 if (cp_parser_non_integral_constant_expression (parser,
7716 NIC_ASSIGNMENT))
7717 return error_mark_node;
7718 /* Build the assignment expression. Its default
7719 location is the location of the '=' token. */
7720 saved_input_location = input_location;
7721 input_location = loc;
7722 expr = build_x_modify_expr (loc, expr,
7723 assignment_operator,
7724 rhs,
7725 tf_warning_or_error);
7726 input_location = saved_input_location;
7731 return expr;
7734 static tree
7735 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7736 cp_id_kind * pidk)
7738 return cp_parser_assignment_expression (parser, cast_p,
7739 /*decltype*/false, pidk);
7742 /* Parse an (optional) assignment-operator.
7744 assignment-operator: one of
7745 = *= /= %= += -= >>= <<= &= ^= |=
7747 GNU Extension:
7749 assignment-operator: one of
7750 <?= >?=
7752 If the next token is an assignment operator, the corresponding tree
7753 code is returned, and the token is consumed. For example, for
7754 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7755 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7756 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7757 operator, ERROR_MARK is returned. */
7759 static enum tree_code
7760 cp_parser_assignment_operator_opt (cp_parser* parser)
7762 enum tree_code op;
7763 cp_token *token;
7765 /* Peek at the next token. */
7766 token = cp_lexer_peek_token (parser->lexer);
7768 switch (token->type)
7770 case CPP_EQ:
7771 op = NOP_EXPR;
7772 break;
7774 case CPP_MULT_EQ:
7775 op = MULT_EXPR;
7776 break;
7778 case CPP_DIV_EQ:
7779 op = TRUNC_DIV_EXPR;
7780 break;
7782 case CPP_MOD_EQ:
7783 op = TRUNC_MOD_EXPR;
7784 break;
7786 case CPP_PLUS_EQ:
7787 op = PLUS_EXPR;
7788 break;
7790 case CPP_MINUS_EQ:
7791 op = MINUS_EXPR;
7792 break;
7794 case CPP_RSHIFT_EQ:
7795 op = RSHIFT_EXPR;
7796 break;
7798 case CPP_LSHIFT_EQ:
7799 op = LSHIFT_EXPR;
7800 break;
7802 case CPP_AND_EQ:
7803 op = BIT_AND_EXPR;
7804 break;
7806 case CPP_XOR_EQ:
7807 op = BIT_XOR_EXPR;
7808 break;
7810 case CPP_OR_EQ:
7811 op = BIT_IOR_EXPR;
7812 break;
7814 default:
7815 /* Nothing else is an assignment operator. */
7816 op = ERROR_MARK;
7819 /* If it was an assignment operator, consume it. */
7820 if (op != ERROR_MARK)
7821 cp_lexer_consume_token (parser->lexer);
7823 return op;
7826 /* Parse an expression.
7828 expression:
7829 assignment-expression
7830 expression , assignment-expression
7832 CAST_P is true if this expression is the target of a cast.
7833 DECLTYPE_P is true if this expression is the immediate operand of decltype,
7834 except possibly parenthesized or on the RHS of a comma (N3276).
7836 Returns a representation of the expression. */
7838 static tree
7839 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
7840 cp_id_kind * pidk)
7842 tree expression = NULL_TREE;
7843 location_t loc = UNKNOWN_LOCATION;
7845 while (true)
7847 tree assignment_expression;
7849 /* Parse the next assignment-expression. */
7850 assignment_expression
7851 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
7853 /* We don't create a temporary for a call that is the immediate operand
7854 of decltype or on the RHS of a comma. But when we see a comma, we
7855 need to create a temporary for a call on the LHS. */
7856 if (decltype_p && !processing_template_decl
7857 && TREE_CODE (assignment_expression) == CALL_EXPR
7858 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
7859 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7860 assignment_expression
7861 = build_cplus_new (TREE_TYPE (assignment_expression),
7862 assignment_expression, tf_warning_or_error);
7864 /* If this is the first assignment-expression, we can just
7865 save it away. */
7866 if (!expression)
7867 expression = assignment_expression;
7868 else
7869 expression = build_x_compound_expr (loc, expression,
7870 assignment_expression,
7871 tf_warning_or_error);
7872 /* If the next token is not a comma, then we are done with the
7873 expression. */
7874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7875 break;
7876 /* Consume the `,'. */
7877 loc = cp_lexer_peek_token (parser->lexer)->location;
7878 cp_lexer_consume_token (parser->lexer);
7879 /* A comma operator cannot appear in a constant-expression. */
7880 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7881 expression = error_mark_node;
7884 return expression;
7887 static inline tree
7888 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7890 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
7893 /* Parse a constant-expression.
7895 constant-expression:
7896 conditional-expression
7898 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7899 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7900 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7901 is false, NON_CONSTANT_P should be NULL. */
7903 static tree
7904 cp_parser_constant_expression (cp_parser* parser,
7905 bool allow_non_constant_p,
7906 bool *non_constant_p)
7908 bool saved_integral_constant_expression_p;
7909 bool saved_allow_non_integral_constant_expression_p;
7910 bool saved_non_integral_constant_expression_p;
7911 tree expression;
7913 /* It might seem that we could simply parse the
7914 conditional-expression, and then check to see if it were
7915 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7916 one that the compiler can figure out is constant, possibly after
7917 doing some simplifications or optimizations. The standard has a
7918 precise definition of constant-expression, and we must honor
7919 that, even though it is somewhat more restrictive.
7921 For example:
7923 int i[(2, 3)];
7925 is not a legal declaration, because `(2, 3)' is not a
7926 constant-expression. The `,' operator is forbidden in a
7927 constant-expression. However, GCC's constant-folding machinery
7928 will fold this operation to an INTEGER_CST for `3'. */
7930 /* Save the old settings. */
7931 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7932 saved_allow_non_integral_constant_expression_p
7933 = parser->allow_non_integral_constant_expression_p;
7934 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7935 /* We are now parsing a constant-expression. */
7936 parser->integral_constant_expression_p = true;
7937 parser->allow_non_integral_constant_expression_p
7938 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7939 parser->non_integral_constant_expression_p = false;
7940 /* Although the grammar says "conditional-expression", we parse an
7941 "assignment-expression", which also permits "throw-expression"
7942 and the use of assignment operators. In the case that
7943 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7944 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7945 actually essential that we look for an assignment-expression.
7946 For example, cp_parser_initializer_clauses uses this function to
7947 determine whether a particular assignment-expression is in fact
7948 constant. */
7949 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7950 /* Restore the old settings. */
7951 parser->integral_constant_expression_p
7952 = saved_integral_constant_expression_p;
7953 parser->allow_non_integral_constant_expression_p
7954 = saved_allow_non_integral_constant_expression_p;
7955 if (cxx_dialect >= cxx0x)
7957 /* Require an rvalue constant expression here; that's what our
7958 callers expect. Reference constant expressions are handled
7959 separately in e.g. cp_parser_template_argument. */
7960 bool is_const = potential_rvalue_constant_expression (expression);
7961 parser->non_integral_constant_expression_p = !is_const;
7962 if (!is_const && !allow_non_constant_p)
7963 require_potential_rvalue_constant_expression (expression);
7965 if (allow_non_constant_p)
7966 *non_constant_p = parser->non_integral_constant_expression_p;
7967 parser->non_integral_constant_expression_p
7968 = saved_non_integral_constant_expression_p;
7970 return expression;
7973 /* Parse __builtin_offsetof.
7975 offsetof-expression:
7976 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7978 offsetof-member-designator:
7979 id-expression
7980 | offsetof-member-designator "." id-expression
7981 | offsetof-member-designator "[" expression "]"
7982 | offsetof-member-designator "->" id-expression */
7984 static tree
7985 cp_parser_builtin_offsetof (cp_parser *parser)
7987 int save_ice_p, save_non_ice_p;
7988 tree type, expr;
7989 cp_id_kind dummy;
7990 cp_token *token;
7992 /* We're about to accept non-integral-constant things, but will
7993 definitely yield an integral constant expression. Save and
7994 restore these values around our local parsing. */
7995 save_ice_p = parser->integral_constant_expression_p;
7996 save_non_ice_p = parser->non_integral_constant_expression_p;
7998 /* Consume the "__builtin_offsetof" token. */
7999 cp_lexer_consume_token (parser->lexer);
8000 /* Consume the opening `('. */
8001 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8002 /* Parse the type-id. */
8003 type = cp_parser_type_id (parser);
8004 /* Look for the `,'. */
8005 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8006 token = cp_lexer_peek_token (parser->lexer);
8008 /* Build the (type *)null that begins the traditional offsetof macro. */
8009 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8010 tf_warning_or_error);
8012 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8013 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8014 true, &dummy, token->location);
8015 while (true)
8017 token = cp_lexer_peek_token (parser->lexer);
8018 switch (token->type)
8020 case CPP_OPEN_SQUARE:
8021 /* offsetof-member-designator "[" expression "]" */
8022 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
8023 break;
8025 case CPP_DEREF:
8026 /* offsetof-member-designator "->" identifier */
8027 expr = grok_array_decl (token->location, expr, integer_zero_node);
8028 /* FALLTHRU */
8030 case CPP_DOT:
8031 /* offsetof-member-designator "." identifier */
8032 cp_lexer_consume_token (parser->lexer);
8033 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8034 expr, true, &dummy,
8035 token->location);
8036 break;
8038 case CPP_CLOSE_PAREN:
8039 /* Consume the ")" token. */
8040 cp_lexer_consume_token (parser->lexer);
8041 goto success;
8043 default:
8044 /* Error. We know the following require will fail, but
8045 that gives the proper error message. */
8046 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8047 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8048 expr = error_mark_node;
8049 goto failure;
8053 success:
8054 /* If we're processing a template, we can't finish the semantics yet.
8055 Otherwise we can fold the entire expression now. */
8056 if (processing_template_decl)
8057 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8058 else
8059 expr = finish_offsetof (expr);
8061 failure:
8062 parser->integral_constant_expression_p = save_ice_p;
8063 parser->non_integral_constant_expression_p = save_non_ice_p;
8065 return expr;
8068 /* Parse a trait expression.
8070 Returns a representation of the expression, the underlying type
8071 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8073 static tree
8074 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8076 cp_trait_kind kind;
8077 tree type1, type2 = NULL_TREE;
8078 bool binary = false;
8079 cp_decl_specifier_seq decl_specs;
8081 switch (keyword)
8083 case RID_HAS_NOTHROW_ASSIGN:
8084 kind = CPTK_HAS_NOTHROW_ASSIGN;
8085 break;
8086 case RID_HAS_NOTHROW_CONSTRUCTOR:
8087 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8088 break;
8089 case RID_HAS_NOTHROW_COPY:
8090 kind = CPTK_HAS_NOTHROW_COPY;
8091 break;
8092 case RID_HAS_TRIVIAL_ASSIGN:
8093 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8094 break;
8095 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8096 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8097 break;
8098 case RID_HAS_TRIVIAL_COPY:
8099 kind = CPTK_HAS_TRIVIAL_COPY;
8100 break;
8101 case RID_HAS_TRIVIAL_DESTRUCTOR:
8102 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8103 break;
8104 case RID_HAS_VIRTUAL_DESTRUCTOR:
8105 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8106 break;
8107 case RID_IS_ABSTRACT:
8108 kind = CPTK_IS_ABSTRACT;
8109 break;
8110 case RID_IS_BASE_OF:
8111 kind = CPTK_IS_BASE_OF;
8112 binary = true;
8113 break;
8114 case RID_IS_CLASS:
8115 kind = CPTK_IS_CLASS;
8116 break;
8117 case RID_IS_CONVERTIBLE_TO:
8118 kind = CPTK_IS_CONVERTIBLE_TO;
8119 binary = true;
8120 break;
8121 case RID_IS_EMPTY:
8122 kind = CPTK_IS_EMPTY;
8123 break;
8124 case RID_IS_ENUM:
8125 kind = CPTK_IS_ENUM;
8126 break;
8127 case RID_IS_FINAL:
8128 kind = CPTK_IS_FINAL;
8129 break;
8130 case RID_IS_LITERAL_TYPE:
8131 kind = CPTK_IS_LITERAL_TYPE;
8132 break;
8133 case RID_IS_POD:
8134 kind = CPTK_IS_POD;
8135 break;
8136 case RID_IS_POLYMORPHIC:
8137 kind = CPTK_IS_POLYMORPHIC;
8138 break;
8139 case RID_IS_STD_LAYOUT:
8140 kind = CPTK_IS_STD_LAYOUT;
8141 break;
8142 case RID_IS_TRIVIAL:
8143 kind = CPTK_IS_TRIVIAL;
8144 break;
8145 case RID_IS_UNION:
8146 kind = CPTK_IS_UNION;
8147 break;
8148 case RID_UNDERLYING_TYPE:
8149 kind = CPTK_UNDERLYING_TYPE;
8150 break;
8151 case RID_BASES:
8152 kind = CPTK_BASES;
8153 break;
8154 case RID_DIRECT_BASES:
8155 kind = CPTK_DIRECT_BASES;
8156 break;
8157 default:
8158 gcc_unreachable ();
8161 /* Consume the token. */
8162 cp_lexer_consume_token (parser->lexer);
8164 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8166 type1 = cp_parser_type_id (parser);
8168 if (type1 == error_mark_node)
8169 return error_mark_node;
8171 /* Build a trivial decl-specifier-seq. */
8172 clear_decl_specs (&decl_specs);
8173 decl_specs.type = type1;
8175 /* Call grokdeclarator to figure out what type this is. */
8176 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8177 /*initialized=*/0, /*attrlist=*/NULL);
8179 if (binary)
8181 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8183 type2 = cp_parser_type_id (parser);
8185 if (type2 == error_mark_node)
8186 return error_mark_node;
8188 /* Build a trivial decl-specifier-seq. */
8189 clear_decl_specs (&decl_specs);
8190 decl_specs.type = type2;
8192 /* Call grokdeclarator to figure out what type this is. */
8193 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8194 /*initialized=*/0, /*attrlist=*/NULL);
8197 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8199 /* Complete the trait expression, which may mean either processing
8200 the trait expr now or saving it for template instantiation. */
8201 switch(kind)
8203 case CPTK_UNDERLYING_TYPE:
8204 return finish_underlying_type (type1);
8205 case CPTK_BASES:
8206 return finish_bases (type1, false);
8207 case CPTK_DIRECT_BASES:
8208 return finish_bases (type1, true);
8209 default:
8210 return finish_trait_expr (kind, type1, type2);
8214 /* Lambdas that appear in variable initializer or default argument scope
8215 get that in their mangling, so we need to record it. We might as well
8216 use the count for function and namespace scopes as well. */
8217 static GTY(()) tree lambda_scope;
8218 static GTY(()) int lambda_count;
8219 typedef struct GTY(()) tree_int
8221 tree t;
8222 int i;
8223 } tree_int;
8224 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8226 static void
8227 start_lambda_scope (tree decl)
8229 tree_int ti;
8230 gcc_assert (decl);
8231 /* Once we're inside a function, we ignore other scopes and just push
8232 the function again so that popping works properly. */
8233 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8234 decl = current_function_decl;
8235 ti.t = lambda_scope;
8236 ti.i = lambda_count;
8237 vec_safe_push (lambda_scope_stack, ti);
8238 if (lambda_scope != decl)
8240 /* Don't reset the count if we're still in the same function. */
8241 lambda_scope = decl;
8242 lambda_count = 0;
8246 static void
8247 record_lambda_scope (tree lambda)
8249 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8250 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8253 static void
8254 finish_lambda_scope (void)
8256 tree_int *p = &lambda_scope_stack->last ();
8257 if (lambda_scope != p->t)
8259 lambda_scope = p->t;
8260 lambda_count = p->i;
8262 lambda_scope_stack->pop ();
8265 /* Parse a lambda expression.
8267 lambda-expression:
8268 lambda-introducer lambda-declarator [opt] compound-statement
8270 Returns a representation of the expression. */
8272 static tree
8273 cp_parser_lambda_expression (cp_parser* parser)
8275 tree lambda_expr = build_lambda_expr ();
8276 tree type;
8277 bool ok;
8279 LAMBDA_EXPR_LOCATION (lambda_expr)
8280 = cp_lexer_peek_token (parser->lexer)->location;
8282 if (cp_unevaluated_operand)
8283 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8284 "lambda-expression in unevaluated context");
8286 /* We may be in the middle of deferred access check. Disable
8287 it now. */
8288 push_deferring_access_checks (dk_no_deferred);
8290 cp_parser_lambda_introducer (parser, lambda_expr);
8292 type = begin_lambda_type (lambda_expr);
8293 if (type == error_mark_node)
8294 return error_mark_node;
8296 record_lambda_scope (lambda_expr);
8298 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8299 determine_visibility (TYPE_NAME (type));
8301 /* Now that we've started the type, add the capture fields for any
8302 explicit captures. */
8303 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8306 /* Inside the class, surrounding template-parameter-lists do not apply. */
8307 unsigned int saved_num_template_parameter_lists
8308 = parser->num_template_parameter_lists;
8309 unsigned char in_statement = parser->in_statement;
8310 bool in_switch_statement_p = parser->in_switch_statement_p;
8312 parser->num_template_parameter_lists = 0;
8313 parser->in_statement = 0;
8314 parser->in_switch_statement_p = false;
8316 /* By virtue of defining a local class, a lambda expression has access to
8317 the private variables of enclosing classes. */
8319 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8321 if (ok)
8322 cp_parser_lambda_body (parser, lambda_expr);
8323 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8324 cp_parser_skip_to_end_of_block_or_statement (parser);
8326 /* The capture list was built up in reverse order; fix that now. */
8327 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8328 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8330 if (ok)
8331 maybe_add_lambda_conv_op (type);
8333 type = finish_struct (type, /*attributes=*/NULL_TREE);
8335 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8336 parser->in_statement = in_statement;
8337 parser->in_switch_statement_p = in_switch_statement_p;
8340 pop_deferring_access_checks ();
8342 /* This field is only used during parsing of the lambda. */
8343 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8345 /* This lambda shouldn't have any proxies left at this point. */
8346 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8347 /* And now that we're done, push proxies for an enclosing lambda. */
8348 insert_pending_capture_proxies ();
8350 if (ok)
8351 return build_lambda_object (lambda_expr);
8352 else
8353 return error_mark_node;
8356 /* Parse the beginning of a lambda expression.
8358 lambda-introducer:
8359 [ lambda-capture [opt] ]
8361 LAMBDA_EXPR is the current representation of the lambda expression. */
8363 static void
8364 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8366 /* Need commas after the first capture. */
8367 bool first = true;
8369 /* Eat the leading `['. */
8370 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8372 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8373 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8374 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8375 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8376 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8377 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8379 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8381 cp_lexer_consume_token (parser->lexer);
8382 first = false;
8385 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8387 cp_token* capture_token;
8388 tree capture_id;
8389 tree capture_init_expr;
8390 cp_id_kind idk = CP_ID_KIND_NONE;
8391 bool explicit_init_p = false;
8393 enum capture_kind_type
8395 BY_COPY,
8396 BY_REFERENCE
8398 enum capture_kind_type capture_kind = BY_COPY;
8400 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8402 error ("expected end of capture-list");
8403 return;
8406 if (first)
8407 first = false;
8408 else
8409 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8411 /* Possibly capture `this'. */
8412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8414 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8415 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8416 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8417 "with by-copy capture default");
8418 cp_lexer_consume_token (parser->lexer);
8419 add_capture (lambda_expr,
8420 /*id=*/this_identifier,
8421 /*initializer=*/finish_this_expr(),
8422 /*by_reference_p=*/false,
8423 explicit_init_p);
8424 continue;
8427 /* Remember whether we want to capture as a reference or not. */
8428 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8430 capture_kind = BY_REFERENCE;
8431 cp_lexer_consume_token (parser->lexer);
8434 /* Get the identifier. */
8435 capture_token = cp_lexer_peek_token (parser->lexer);
8436 capture_id = cp_parser_identifier (parser);
8438 if (capture_id == error_mark_node)
8439 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8440 delimiters, but I modified this to stop on unnested ']' as well. It
8441 was already changed to stop on unnested '}', so the
8442 "closing_parenthesis" name is no more misleading with my change. */
8444 cp_parser_skip_to_closing_parenthesis (parser,
8445 /*recovering=*/true,
8446 /*or_comma=*/true,
8447 /*consume_paren=*/true);
8448 break;
8451 /* Find the initializer for this capture. */
8452 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8454 /* An explicit expression exists. */
8455 cp_lexer_consume_token (parser->lexer);
8456 pedwarn (input_location, OPT_Wpedantic,
8457 "ISO C++ does not allow initializers "
8458 "in lambda expression capture lists");
8459 capture_init_expr = cp_parser_assignment_expression (parser,
8460 /*cast_p=*/true,
8461 &idk);
8462 explicit_init_p = true;
8464 else
8466 const char* error_msg;
8468 /* Turn the identifier into an id-expression. */
8469 capture_init_expr
8470 = cp_parser_lookup_name
8471 (parser,
8472 capture_id,
8473 none_type,
8474 /*is_template=*/false,
8475 /*is_namespace=*/false,
8476 /*check_dependency=*/true,
8477 /*ambiguous_decls=*/NULL,
8478 capture_token->location);
8480 if (capture_init_expr == error_mark_node)
8482 unqualified_name_lookup_error (capture_id);
8483 continue;
8485 else if (DECL_P (capture_init_expr)
8486 && (!VAR_P (capture_init_expr)
8487 && TREE_CODE (capture_init_expr) != PARM_DECL))
8489 error_at (capture_token->location,
8490 "capture of non-variable %qD ",
8491 capture_init_expr);
8492 inform (0, "%q+#D declared here", capture_init_expr);
8493 continue;
8495 if (VAR_P (capture_init_expr)
8496 && decl_storage_duration (capture_init_expr) != dk_auto)
8498 pedwarn (capture_token->location, 0, "capture of variable "
8499 "%qD with non-automatic storage duration",
8500 capture_init_expr);
8501 inform (0, "%q+#D declared here", capture_init_expr);
8502 continue;
8505 capture_init_expr
8506 = finish_id_expression
8507 (capture_id,
8508 capture_init_expr,
8509 parser->scope,
8510 &idk,
8511 /*integral_constant_expression_p=*/false,
8512 /*allow_non_integral_constant_expression_p=*/false,
8513 /*non_integral_constant_expression_p=*/NULL,
8514 /*template_p=*/false,
8515 /*done=*/true,
8516 /*address_p=*/false,
8517 /*template_arg_p=*/false,
8518 &error_msg,
8519 capture_token->location);
8522 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8523 && !explicit_init_p)
8525 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8526 && capture_kind == BY_COPY)
8527 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8528 "of %qD redundant with by-copy capture default",
8529 capture_id);
8530 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8531 && capture_kind == BY_REFERENCE)
8532 pedwarn (capture_token->location, 0, "explicit by-reference "
8533 "capture of %qD redundant with by-reference capture "
8534 "default", capture_id);
8537 add_capture (lambda_expr,
8538 capture_id,
8539 capture_init_expr,
8540 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8541 explicit_init_p);
8544 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8547 /* Parse the (optional) middle of a lambda expression.
8549 lambda-declarator:
8550 ( parameter-declaration-clause [opt] )
8551 attribute-specifier [opt]
8552 mutable [opt]
8553 exception-specification [opt]
8554 lambda-return-type-clause [opt]
8556 LAMBDA_EXPR is the current representation of the lambda expression. */
8558 static bool
8559 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8561 /* 5.1.1.4 of the standard says:
8562 If a lambda-expression does not include a lambda-declarator, it is as if
8563 the lambda-declarator were ().
8564 This means an empty parameter list, no attributes, and no exception
8565 specification. */
8566 tree param_list = void_list_node;
8567 tree attributes = NULL_TREE;
8568 tree exception_spec = NULL_TREE;
8569 tree t;
8571 /* The lambda-declarator is optional, but must begin with an opening
8572 parenthesis if present. */
8573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8575 cp_lexer_consume_token (parser->lexer);
8577 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8579 /* Parse parameters. */
8580 param_list = cp_parser_parameter_declaration_clause (parser);
8582 /* Default arguments shall not be specified in the
8583 parameter-declaration-clause of a lambda-declarator. */
8584 for (t = param_list; t; t = TREE_CHAIN (t))
8585 if (TREE_PURPOSE (t))
8586 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8587 "default argument specified for lambda parameter");
8589 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8591 attributes = cp_parser_attributes_opt (parser);
8593 /* Parse optional `mutable' keyword. */
8594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8596 cp_lexer_consume_token (parser->lexer);
8597 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8600 /* Parse optional exception specification. */
8601 exception_spec = cp_parser_exception_specification_opt (parser);
8603 /* Parse optional trailing return type. */
8604 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8606 cp_lexer_consume_token (parser->lexer);
8607 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8608 = cp_parser_trailing_type_id (parser);
8611 /* The function parameters must be in scope all the way until after the
8612 trailing-return-type in case of decltype. */
8613 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8614 pop_binding (DECL_NAME (t), t);
8616 leave_scope ();
8619 /* Create the function call operator.
8621 Messing with declarators like this is no uglier than building up the
8622 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8623 other code. */
8625 cp_decl_specifier_seq return_type_specs;
8626 cp_declarator* declarator;
8627 tree fco;
8628 int quals;
8629 void *p;
8631 clear_decl_specs (&return_type_specs);
8632 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8633 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8634 else
8635 /* Maybe we will deduce the return type later. */
8636 return_type_specs.type = make_auto ();
8638 p = obstack_alloc (&declarator_obstack, 0);
8640 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8641 sfk_none);
8643 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8644 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8645 declarator = make_call_declarator (declarator, param_list, quals,
8646 VIRT_SPEC_UNSPECIFIED,
8647 REF_QUAL_NONE,
8648 exception_spec,
8649 /*late_return_type=*/NULL_TREE);
8650 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8652 fco = grokmethod (&return_type_specs,
8653 declarator,
8654 attributes);
8655 if (fco != error_mark_node)
8657 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8658 DECL_ARTIFICIAL (fco) = 1;
8659 /* Give the object parameter a different name. */
8660 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8663 finish_member_declaration (fco);
8665 obstack_free (&declarator_obstack, p);
8667 return (fco != error_mark_node);
8671 /* Parse the body of a lambda expression, which is simply
8673 compound-statement
8675 but which requires special handling.
8676 LAMBDA_EXPR is the current representation of the lambda expression. */
8678 static void
8679 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8681 bool nested = (current_function_decl != NULL_TREE);
8682 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8683 if (nested)
8684 push_function_context ();
8685 else
8686 /* Still increment function_depth so that we don't GC in the
8687 middle of an expression. */
8688 ++function_depth;
8689 /* Clear this in case we're in the middle of a default argument. */
8690 parser->local_variables_forbidden_p = false;
8692 /* Finish the function call operator
8693 - class_specifier
8694 + late_parsing_for_member
8695 + function_definition_after_declarator
8696 + ctor_initializer_opt_and_function_body */
8698 tree fco = lambda_function (lambda_expr);
8699 tree body;
8700 bool done = false;
8701 tree compound_stmt;
8702 tree cap;
8704 /* Let the front end know that we are going to be defining this
8705 function. */
8706 start_preparsed_function (fco,
8707 NULL_TREE,
8708 SF_PRE_PARSED | SF_INCLASS_INLINE);
8710 start_lambda_scope (fco);
8711 body = begin_function_body ();
8713 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8714 goto out;
8716 /* Push the proxies for any explicit captures. */
8717 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8718 cap = TREE_CHAIN (cap))
8719 build_capture_proxy (TREE_PURPOSE (cap));
8721 compound_stmt = begin_compound_stmt (0);
8723 /* 5.1.1.4 of the standard says:
8724 If a lambda-expression does not include a trailing-return-type, it
8725 is as if the trailing-return-type denotes the following type:
8726 * if the compound-statement is of the form
8727 { return attribute-specifier [opt] expression ; }
8728 the type of the returned expression after lvalue-to-rvalue
8729 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8730 (_conv.array_ 4.2), and function-to-pointer conversion
8731 (_conv.func_ 4.3);
8732 * otherwise, void. */
8734 /* In a lambda that has neither a lambda-return-type-clause
8735 nor a deducible form, errors should be reported for return statements
8736 in the body. Since we used void as the placeholder return type, parsing
8737 the body as usual will give such desired behavior. */
8738 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8739 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8740 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8742 tree expr = NULL_TREE;
8743 cp_id_kind idk = CP_ID_KIND_NONE;
8745 /* Parse tentatively in case there's more after the initial return
8746 statement. */
8747 cp_parser_parse_tentatively (parser);
8749 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8751 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8753 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8754 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8756 if (cp_parser_parse_definitely (parser))
8758 if (!processing_template_decl)
8759 apply_deduced_return_type (fco, lambda_return_type (expr));
8761 /* Will get error here if type not deduced yet. */
8762 finish_return_stmt (expr);
8764 done = true;
8768 if (!done)
8770 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8771 cp_parser_label_declaration (parser);
8772 cp_parser_statement_seq_opt (parser, NULL_TREE);
8773 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8776 finish_compound_stmt (compound_stmt);
8778 out:
8779 finish_function_body (body);
8780 finish_lambda_scope ();
8782 /* Finish the function and generate code for it if necessary. */
8783 expand_or_defer_fn (finish_function (/*inline*/2));
8786 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8787 if (nested)
8788 pop_function_context();
8789 else
8790 --function_depth;
8793 /* Statements [gram.stmt.stmt] */
8795 /* Parse a statement.
8797 statement:
8798 labeled-statement
8799 expression-statement
8800 compound-statement
8801 selection-statement
8802 iteration-statement
8803 jump-statement
8804 declaration-statement
8805 try-block
8807 C++11:
8809 statement:
8810 labeled-statement
8811 attribute-specifier-seq (opt) expression-statement
8812 attribute-specifier-seq (opt) compound-statement
8813 attribute-specifier-seq (opt) selection-statement
8814 attribute-specifier-seq (opt) iteration-statement
8815 attribute-specifier-seq (opt) jump-statement
8816 declaration-statement
8817 attribute-specifier-seq (opt) try-block
8819 TM Extension:
8821 statement:
8822 atomic-statement
8824 IN_COMPOUND is true when the statement is nested inside a
8825 cp_parser_compound_statement; this matters for certain pragmas.
8827 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8828 is a (possibly labeled) if statement which is not enclosed in braces
8829 and has an else clause. This is used to implement -Wparentheses. */
8831 static void
8832 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8833 bool in_compound, bool *if_p)
8835 tree statement, std_attrs = NULL_TREE;
8836 cp_token *token;
8837 location_t statement_location, attrs_location;
8839 restart:
8840 if (if_p != NULL)
8841 *if_p = false;
8842 /* There is no statement yet. */
8843 statement = NULL_TREE;
8845 cp_lexer_save_tokens (parser->lexer);
8846 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8847 if (c_dialect_objc ())
8848 /* In obj-c++, seing '[[' might be the either the beginning of
8849 c++11 attributes, or a nested objc-message-expression. So
8850 let's parse the c++11 attributes tentatively. */
8851 cp_parser_parse_tentatively (parser);
8852 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8853 if (c_dialect_objc ())
8855 if (!cp_parser_parse_definitely (parser))
8856 std_attrs = NULL_TREE;
8859 /* Peek at the next token. */
8860 token = cp_lexer_peek_token (parser->lexer);
8861 /* Remember the location of the first token in the statement. */
8862 statement_location = token->location;
8863 /* If this is a keyword, then that will often determine what kind of
8864 statement we have. */
8865 if (token->type == CPP_KEYWORD)
8867 enum rid keyword = token->keyword;
8869 switch (keyword)
8871 case RID_CASE:
8872 case RID_DEFAULT:
8873 /* Looks like a labeled-statement with a case label.
8874 Parse the label, and then use tail recursion to parse
8875 the statement. */
8876 cp_parser_label_for_labeled_statement (parser, std_attrs);
8877 goto restart;
8879 case RID_IF:
8880 case RID_SWITCH:
8881 statement = cp_parser_selection_statement (parser, if_p);
8882 break;
8884 case RID_WHILE:
8885 case RID_DO:
8886 case RID_FOR:
8887 statement = cp_parser_iteration_statement (parser);
8888 break;
8890 case RID_BREAK:
8891 case RID_CONTINUE:
8892 case RID_RETURN:
8893 case RID_GOTO:
8894 statement = cp_parser_jump_statement (parser);
8895 break;
8897 /* Objective-C++ exception-handling constructs. */
8898 case RID_AT_TRY:
8899 case RID_AT_CATCH:
8900 case RID_AT_FINALLY:
8901 case RID_AT_SYNCHRONIZED:
8902 case RID_AT_THROW:
8903 statement = cp_parser_objc_statement (parser);
8904 break;
8906 case RID_TRY:
8907 statement = cp_parser_try_block (parser);
8908 break;
8910 case RID_NAMESPACE:
8911 /* This must be a namespace alias definition. */
8912 cp_parser_declaration_statement (parser);
8913 return;
8915 case RID_TRANSACTION_ATOMIC:
8916 case RID_TRANSACTION_RELAXED:
8917 statement = cp_parser_transaction (parser, keyword);
8918 break;
8919 case RID_TRANSACTION_CANCEL:
8920 statement = cp_parser_transaction_cancel (parser);
8921 break;
8923 default:
8924 /* It might be a keyword like `int' that can start a
8925 declaration-statement. */
8926 break;
8929 else if (token->type == CPP_NAME)
8931 /* If the next token is a `:', then we are looking at a
8932 labeled-statement. */
8933 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8934 if (token->type == CPP_COLON)
8936 /* Looks like a labeled-statement with an ordinary label.
8937 Parse the label, and then use tail recursion to parse
8938 the statement. */
8940 cp_parser_label_for_labeled_statement (parser, std_attrs);
8941 goto restart;
8944 /* Anything that starts with a `{' must be a compound-statement. */
8945 else if (token->type == CPP_OPEN_BRACE)
8946 statement = cp_parser_compound_statement (parser, NULL, false, false);
8947 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8948 a statement all its own. */
8949 else if (token->type == CPP_PRAGMA)
8951 /* Only certain OpenMP pragmas are attached to statements, and thus
8952 are considered statements themselves. All others are not. In
8953 the context of a compound, accept the pragma as a "statement" and
8954 return so that we can check for a close brace. Otherwise we
8955 require a real statement and must go back and read one. */
8956 if (in_compound)
8957 cp_parser_pragma (parser, pragma_compound);
8958 else if (!cp_parser_pragma (parser, pragma_stmt))
8959 goto restart;
8960 return;
8962 else if (token->type == CPP_EOF)
8964 cp_parser_error (parser, "expected statement");
8965 return;
8968 /* Everything else must be a declaration-statement or an
8969 expression-statement. Try for the declaration-statement
8970 first, unless we are looking at a `;', in which case we know that
8971 we have an expression-statement. */
8972 if (!statement)
8974 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8976 if (std_attrs != NULL_TREE)
8978 /* Attributes should be parsed as part of the the
8979 declaration, so let's un-parse them. */
8980 cp_lexer_rollback_tokens (parser->lexer);
8981 std_attrs = NULL_TREE;
8984 cp_parser_parse_tentatively (parser);
8985 /* Try to parse the declaration-statement. */
8986 cp_parser_declaration_statement (parser);
8987 /* If that worked, we're done. */
8988 if (cp_parser_parse_definitely (parser))
8989 return;
8991 /* Look for an expression-statement instead. */
8992 statement = cp_parser_expression_statement (parser, in_statement_expr);
8995 /* Set the line number for the statement. */
8996 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8997 SET_EXPR_LOCATION (statement, statement_location);
8999 /* Note that for now, we don't do anything with c++11 statements
9000 parsed at this level. */
9001 if (std_attrs != NULL_TREE)
9002 warning_at (attrs_location,
9003 OPT_Wattributes,
9004 "attributes at the beginning of statement are ignored");
9007 /* Parse the label for a labeled-statement, i.e.
9009 identifier :
9010 case constant-expression :
9011 default :
9013 GNU Extension:
9014 case constant-expression ... constant-expression : statement
9016 When a label is parsed without errors, the label is added to the
9017 parse tree by the finish_* functions, so this function doesn't
9018 have to return the label. */
9020 static void
9021 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9023 cp_token *token;
9024 tree label = NULL_TREE;
9025 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9027 /* The next token should be an identifier. */
9028 token = cp_lexer_peek_token (parser->lexer);
9029 if (token->type != CPP_NAME
9030 && token->type != CPP_KEYWORD)
9032 cp_parser_error (parser, "expected labeled-statement");
9033 return;
9036 parser->colon_corrects_to_scope_p = false;
9037 switch (token->keyword)
9039 case RID_CASE:
9041 tree expr, expr_hi;
9042 cp_token *ellipsis;
9044 /* Consume the `case' token. */
9045 cp_lexer_consume_token (parser->lexer);
9046 /* Parse the constant-expression. */
9047 expr = cp_parser_constant_expression (parser,
9048 /*allow_non_constant_p=*/false,
9049 NULL);
9051 ellipsis = cp_lexer_peek_token (parser->lexer);
9052 if (ellipsis->type == CPP_ELLIPSIS)
9054 /* Consume the `...' token. */
9055 cp_lexer_consume_token (parser->lexer);
9056 expr_hi =
9057 cp_parser_constant_expression (parser,
9058 /*allow_non_constant_p=*/false,
9059 NULL);
9060 /* We don't need to emit warnings here, as the common code
9061 will do this for us. */
9063 else
9064 expr_hi = NULL_TREE;
9066 if (parser->in_switch_statement_p)
9067 finish_case_label (token->location, expr, expr_hi);
9068 else
9069 error_at (token->location,
9070 "case label %qE not within a switch statement",
9071 expr);
9073 break;
9075 case RID_DEFAULT:
9076 /* Consume the `default' token. */
9077 cp_lexer_consume_token (parser->lexer);
9079 if (parser->in_switch_statement_p)
9080 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9081 else
9082 error_at (token->location, "case label not within a switch statement");
9083 break;
9085 default:
9086 /* Anything else must be an ordinary label. */
9087 label = finish_label_stmt (cp_parser_identifier (parser));
9088 break;
9091 /* Require the `:' token. */
9092 cp_parser_require (parser, CPP_COLON, RT_COLON);
9094 /* An ordinary label may optionally be followed by attributes.
9095 However, this is only permitted if the attributes are then
9096 followed by a semicolon. This is because, for backward
9097 compatibility, when parsing
9098 lab: __attribute__ ((unused)) int i;
9099 we want the attribute to attach to "i", not "lab". */
9100 if (label != NULL_TREE
9101 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9103 tree attrs;
9104 cp_parser_parse_tentatively (parser);
9105 attrs = cp_parser_gnu_attributes_opt (parser);
9106 if (attrs == NULL_TREE
9107 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9108 cp_parser_abort_tentative_parse (parser);
9109 else if (!cp_parser_parse_definitely (parser))
9111 else
9112 attributes = chainon (attributes, attrs);
9115 if (attributes != NULL_TREE)
9116 cplus_decl_attributes (&label, attributes, 0);
9118 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9121 /* Parse an expression-statement.
9123 expression-statement:
9124 expression [opt] ;
9126 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9127 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9128 indicates whether this expression-statement is part of an
9129 expression statement. */
9131 static tree
9132 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9134 tree statement = NULL_TREE;
9135 cp_token *token = cp_lexer_peek_token (parser->lexer);
9137 /* If the next token is a ';', then there is no expression
9138 statement. */
9139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9140 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9142 /* Give a helpful message for "A<T>::type t;" and the like. */
9143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9144 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9146 if (TREE_CODE (statement) == SCOPE_REF)
9147 error_at (token->location, "need %<typename%> before %qE because "
9148 "%qT is a dependent scope",
9149 statement, TREE_OPERAND (statement, 0));
9150 else if (is_overloaded_fn (statement)
9151 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9153 /* A::A a; */
9154 tree fn = get_first_fn (statement);
9155 error_at (token->location,
9156 "%<%T::%D%> names the constructor, not the type",
9157 DECL_CONTEXT (fn), DECL_NAME (fn));
9161 /* Consume the final `;'. */
9162 cp_parser_consume_semicolon_at_end_of_statement (parser);
9164 if (in_statement_expr
9165 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9166 /* This is the final expression statement of a statement
9167 expression. */
9168 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9169 else if (statement)
9170 statement = finish_expr_stmt (statement);
9171 else
9172 finish_stmt ();
9174 return statement;
9177 /* Parse a compound-statement.
9179 compound-statement:
9180 { statement-seq [opt] }
9182 GNU extension:
9184 compound-statement:
9185 { label-declaration-seq [opt] statement-seq [opt] }
9187 label-declaration-seq:
9188 label-declaration
9189 label-declaration-seq label-declaration
9191 Returns a tree representing the statement. */
9193 static tree
9194 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9195 bool in_try, bool function_body)
9197 tree compound_stmt;
9199 /* Consume the `{'. */
9200 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9201 return error_mark_node;
9202 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9203 && !function_body)
9204 pedwarn (input_location, OPT_Wpedantic,
9205 "compound-statement in constexpr function");
9206 /* Begin the compound-statement. */
9207 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9208 /* If the next keyword is `__label__' we have a label declaration. */
9209 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9210 cp_parser_label_declaration (parser);
9211 /* Parse an (optional) statement-seq. */
9212 cp_parser_statement_seq_opt (parser, in_statement_expr);
9213 /* Finish the compound-statement. */
9214 finish_compound_stmt (compound_stmt);
9215 /* Consume the `}'. */
9216 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9218 return compound_stmt;
9221 /* Parse an (optional) statement-seq.
9223 statement-seq:
9224 statement
9225 statement-seq [opt] statement */
9227 static void
9228 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9230 /* Scan statements until there aren't any more. */
9231 while (true)
9233 cp_token *token = cp_lexer_peek_token (parser->lexer);
9235 /* If we are looking at a `}', then we have run out of
9236 statements; the same is true if we have reached the end
9237 of file, or have stumbled upon a stray '@end'. */
9238 if (token->type == CPP_CLOSE_BRACE
9239 || token->type == CPP_EOF
9240 || token->type == CPP_PRAGMA_EOL
9241 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9242 break;
9244 /* If we are in a compound statement and find 'else' then
9245 something went wrong. */
9246 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9248 if (parser->in_statement & IN_IF_STMT)
9249 break;
9250 else
9252 token = cp_lexer_consume_token (parser->lexer);
9253 error_at (token->location, "%<else%> without a previous %<if%>");
9257 /* Parse the statement. */
9258 cp_parser_statement (parser, in_statement_expr, true, NULL);
9262 /* Parse a selection-statement.
9264 selection-statement:
9265 if ( condition ) statement
9266 if ( condition ) statement else statement
9267 switch ( condition ) statement
9269 Returns the new IF_STMT or SWITCH_STMT.
9271 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9272 is a (possibly labeled) if statement which is not enclosed in
9273 braces and has an else clause. This is used to implement
9274 -Wparentheses. */
9276 static tree
9277 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9279 cp_token *token;
9280 enum rid keyword;
9282 if (if_p != NULL)
9283 *if_p = false;
9285 /* Peek at the next token. */
9286 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9288 /* See what kind of keyword it is. */
9289 keyword = token->keyword;
9290 switch (keyword)
9292 case RID_IF:
9293 case RID_SWITCH:
9295 tree statement;
9296 tree condition;
9298 /* Look for the `('. */
9299 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9301 cp_parser_skip_to_end_of_statement (parser);
9302 return error_mark_node;
9305 /* Begin the selection-statement. */
9306 if (keyword == RID_IF)
9307 statement = begin_if_stmt ();
9308 else
9309 statement = begin_switch_stmt ();
9311 /* Parse the condition. */
9312 condition = cp_parser_condition (parser);
9313 /* Look for the `)'. */
9314 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9315 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9316 /*consume_paren=*/true);
9318 if (keyword == RID_IF)
9320 bool nested_if;
9321 unsigned char in_statement;
9323 /* Add the condition. */
9324 finish_if_stmt_cond (condition, statement);
9326 /* Parse the then-clause. */
9327 in_statement = parser->in_statement;
9328 parser->in_statement |= IN_IF_STMT;
9329 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9331 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9332 add_stmt (build_empty_stmt (loc));
9333 cp_lexer_consume_token (parser->lexer);
9334 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9335 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9336 "empty body in an %<if%> statement");
9337 nested_if = false;
9339 else
9340 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9341 parser->in_statement = in_statement;
9343 finish_then_clause (statement);
9345 /* If the next token is `else', parse the else-clause. */
9346 if (cp_lexer_next_token_is_keyword (parser->lexer,
9347 RID_ELSE))
9349 /* Consume the `else' keyword. */
9350 cp_lexer_consume_token (parser->lexer);
9351 begin_else_clause (statement);
9352 /* Parse the else-clause. */
9353 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9355 location_t loc;
9356 loc = cp_lexer_peek_token (parser->lexer)->location;
9357 warning_at (loc,
9358 OPT_Wempty_body, "suggest braces around "
9359 "empty body in an %<else%> statement");
9360 add_stmt (build_empty_stmt (loc));
9361 cp_lexer_consume_token (parser->lexer);
9363 else
9364 cp_parser_implicitly_scoped_statement (parser, NULL);
9366 finish_else_clause (statement);
9368 /* If we are currently parsing a then-clause, then
9369 IF_P will not be NULL. We set it to true to
9370 indicate that this if statement has an else clause.
9371 This may trigger the Wparentheses warning below
9372 when we get back up to the parent if statement. */
9373 if (if_p != NULL)
9374 *if_p = true;
9376 else
9378 /* This if statement does not have an else clause. If
9379 NESTED_IF is true, then the then-clause is an if
9380 statement which does have an else clause. We warn
9381 about the potential ambiguity. */
9382 if (nested_if)
9383 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9384 "suggest explicit braces to avoid ambiguous"
9385 " %<else%>");
9388 /* Now we're all done with the if-statement. */
9389 finish_if_stmt (statement);
9391 else
9393 bool in_switch_statement_p;
9394 unsigned char in_statement;
9396 /* Add the condition. */
9397 finish_switch_cond (condition, statement);
9399 /* Parse the body of the switch-statement. */
9400 in_switch_statement_p = parser->in_switch_statement_p;
9401 in_statement = parser->in_statement;
9402 parser->in_switch_statement_p = true;
9403 parser->in_statement |= IN_SWITCH_STMT;
9404 cp_parser_implicitly_scoped_statement (parser, NULL);
9405 parser->in_switch_statement_p = in_switch_statement_p;
9406 parser->in_statement = in_statement;
9408 /* Now we're all done with the switch-statement. */
9409 finish_switch_stmt (statement);
9412 return statement;
9414 break;
9416 default:
9417 cp_parser_error (parser, "expected selection-statement");
9418 return error_mark_node;
9422 /* Parse a condition.
9424 condition:
9425 expression
9426 type-specifier-seq declarator = initializer-clause
9427 type-specifier-seq declarator braced-init-list
9429 GNU Extension:
9431 condition:
9432 type-specifier-seq declarator asm-specification [opt]
9433 attributes [opt] = assignment-expression
9435 Returns the expression that should be tested. */
9437 static tree
9438 cp_parser_condition (cp_parser* parser)
9440 cp_decl_specifier_seq type_specifiers;
9441 const char *saved_message;
9442 int declares_class_or_enum;
9444 /* Try the declaration first. */
9445 cp_parser_parse_tentatively (parser);
9446 /* New types are not allowed in the type-specifier-seq for a
9447 condition. */
9448 saved_message = parser->type_definition_forbidden_message;
9449 parser->type_definition_forbidden_message
9450 = G_("types may not be defined in conditions");
9451 /* Parse the type-specifier-seq. */
9452 cp_parser_decl_specifier_seq (parser,
9453 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9454 &type_specifiers,
9455 &declares_class_or_enum);
9456 /* Restore the saved message. */
9457 parser->type_definition_forbidden_message = saved_message;
9458 /* If all is well, we might be looking at a declaration. */
9459 if (!cp_parser_error_occurred (parser))
9461 tree decl;
9462 tree asm_specification;
9463 tree attributes;
9464 cp_declarator *declarator;
9465 tree initializer = NULL_TREE;
9467 /* Parse the declarator. */
9468 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9469 /*ctor_dtor_or_conv_p=*/NULL,
9470 /*parenthesized_p=*/NULL,
9471 /*member_p=*/false);
9472 /* Parse the attributes. */
9473 attributes = cp_parser_attributes_opt (parser);
9474 /* Parse the asm-specification. */
9475 asm_specification = cp_parser_asm_specification_opt (parser);
9476 /* If the next token is not an `=' or '{', then we might still be
9477 looking at an expression. For example:
9479 if (A(a).x)
9481 looks like a decl-specifier-seq and a declarator -- but then
9482 there is no `=', so this is an expression. */
9483 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9484 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9485 cp_parser_simulate_error (parser);
9487 /* If we did see an `=' or '{', then we are looking at a declaration
9488 for sure. */
9489 if (cp_parser_parse_definitely (parser))
9491 tree pushed_scope;
9492 bool non_constant_p;
9493 bool flags = LOOKUP_ONLYCONVERTING;
9495 /* Create the declaration. */
9496 decl = start_decl (declarator, &type_specifiers,
9497 /*initialized_p=*/true,
9498 attributes, /*prefix_attributes=*/NULL_TREE,
9499 &pushed_scope);
9501 /* Parse the initializer. */
9502 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9504 initializer = cp_parser_braced_list (parser, &non_constant_p);
9505 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9506 flags = 0;
9508 else
9510 /* Consume the `='. */
9511 cp_parser_require (parser, CPP_EQ, RT_EQ);
9512 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9514 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9515 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9517 /* Process the initializer. */
9518 cp_finish_decl (decl,
9519 initializer, !non_constant_p,
9520 asm_specification,
9521 flags);
9523 if (pushed_scope)
9524 pop_scope (pushed_scope);
9526 return convert_from_reference (decl);
9529 /* If we didn't even get past the declarator successfully, we are
9530 definitely not looking at a declaration. */
9531 else
9532 cp_parser_abort_tentative_parse (parser);
9534 /* Otherwise, we are looking at an expression. */
9535 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9538 /* Parses a for-statement or range-for-statement until the closing ')',
9539 not included. */
9541 static tree
9542 cp_parser_for (cp_parser *parser)
9544 tree init, scope, decl;
9545 bool is_range_for;
9547 /* Begin the for-statement. */
9548 scope = begin_for_scope (&init);
9550 /* Parse the initialization. */
9551 is_range_for = cp_parser_for_init_statement (parser, &decl);
9553 if (is_range_for)
9554 return cp_parser_range_for (parser, scope, init, decl);
9555 else
9556 return cp_parser_c_for (parser, scope, init);
9559 static tree
9560 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9562 /* Normal for loop */
9563 tree condition = NULL_TREE;
9564 tree expression = NULL_TREE;
9565 tree stmt;
9567 stmt = begin_for_stmt (scope, init);
9568 /* The for-init-statement has already been parsed in
9569 cp_parser_for_init_statement, so no work is needed here. */
9570 finish_for_init_stmt (stmt);
9572 /* If there's a condition, process it. */
9573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9574 condition = cp_parser_condition (parser);
9575 finish_for_cond (condition, stmt);
9576 /* Look for the `;'. */
9577 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9579 /* If there's an expression, process it. */
9580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9581 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9582 finish_for_expr (expression, stmt);
9584 return stmt;
9587 /* Tries to parse a range-based for-statement:
9589 range-based-for:
9590 decl-specifier-seq declarator : expression
9592 The decl-specifier-seq declarator and the `:' are already parsed by
9593 cp_parser_for_init_statement. If processing_template_decl it returns a
9594 newly created RANGE_FOR_STMT; if not, it is converted to a
9595 regular FOR_STMT. */
9597 static tree
9598 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9600 tree stmt, range_expr;
9602 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9604 bool expr_non_constant_p;
9605 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9607 else
9608 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9610 /* If in template, STMT is converted to a normal for-statement
9611 at instantiation. If not, it is done just ahead. */
9612 if (processing_template_decl)
9614 if (check_for_bare_parameter_packs (range_expr))
9615 range_expr = error_mark_node;
9616 stmt = begin_range_for_stmt (scope, init);
9617 finish_range_for_decl (stmt, range_decl, range_expr);
9618 if (range_expr != error_mark_node
9619 && !type_dependent_expression_p (range_expr)
9620 /* The length of an array might be dependent. */
9621 && COMPLETE_TYPE_P (TREE_TYPE (range_expr))
9622 /* do_auto_deduction doesn't mess with template init-lists. */
9623 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9624 do_range_for_auto_deduction (range_decl, range_expr);
9626 else
9628 stmt = begin_for_stmt (scope, init);
9629 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9631 return stmt;
9634 /* Subroutine of cp_convert_range_for: given the initializer expression,
9635 builds up the range temporary. */
9637 static tree
9638 build_range_temp (tree range_expr)
9640 tree range_type, range_temp;
9642 /* Find out the type deduced by the declaration
9643 `auto &&__range = range_expr'. */
9644 range_type = cp_build_reference_type (make_auto (), true);
9645 range_type = do_auto_deduction (range_type, range_expr,
9646 type_uses_auto (range_type));
9648 /* Create the __range variable. */
9649 range_temp = build_decl (input_location, VAR_DECL,
9650 get_identifier ("__for_range"), range_type);
9651 TREE_USED (range_temp) = 1;
9652 DECL_ARTIFICIAL (range_temp) = 1;
9654 return range_temp;
9657 /* Used by cp_parser_range_for in template context: we aren't going to
9658 do a full conversion yet, but we still need to resolve auto in the
9659 type of the for-range-declaration if present. This is basically
9660 a shortcut version of cp_convert_range_for. */
9662 static void
9663 do_range_for_auto_deduction (tree decl, tree range_expr)
9665 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9666 if (auto_node)
9668 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9669 range_temp = convert_from_reference (build_range_temp (range_expr));
9670 iter_type = (cp_parser_perform_range_for_lookup
9671 (range_temp, &begin_dummy, &end_dummy));
9672 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9673 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9674 tf_warning_or_error);
9675 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9676 iter_decl, auto_node);
9680 /* Converts a range-based for-statement into a normal
9681 for-statement, as per the definition.
9683 for (RANGE_DECL : RANGE_EXPR)
9684 BLOCK
9686 should be equivalent to:
9689 auto &&__range = RANGE_EXPR;
9690 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9691 __begin != __end;
9692 ++__begin)
9694 RANGE_DECL = *__begin;
9695 BLOCK
9699 If RANGE_EXPR is an array:
9700 BEGIN_EXPR = __range
9701 END_EXPR = __range + ARRAY_SIZE(__range)
9702 Else if RANGE_EXPR has a member 'begin' or 'end':
9703 BEGIN_EXPR = __range.begin()
9704 END_EXPR = __range.end()
9705 Else:
9706 BEGIN_EXPR = begin(__range)
9707 END_EXPR = end(__range);
9709 If __range has a member 'begin' but not 'end', or vice versa, we must
9710 still use the second alternative (it will surely fail, however).
9711 When calling begin()/end() in the third alternative we must use
9712 argument dependent lookup, but always considering 'std' as an associated
9713 namespace. */
9715 tree
9716 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9718 tree begin, end;
9719 tree iter_type, begin_expr, end_expr;
9720 tree condition, expression;
9722 if (range_decl == error_mark_node || range_expr == error_mark_node)
9723 /* If an error happened previously do nothing or else a lot of
9724 unhelpful errors would be issued. */
9725 begin_expr = end_expr = iter_type = error_mark_node;
9726 else
9728 tree range_temp = build_range_temp (range_expr);
9729 pushdecl (range_temp);
9730 cp_finish_decl (range_temp, range_expr,
9731 /*is_constant_init*/false, NULL_TREE,
9732 LOOKUP_ONLYCONVERTING);
9734 range_temp = convert_from_reference (range_temp);
9735 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9736 &begin_expr, &end_expr);
9739 /* The new for initialization statement. */
9740 begin = build_decl (input_location, VAR_DECL,
9741 get_identifier ("__for_begin"), iter_type);
9742 TREE_USED (begin) = 1;
9743 DECL_ARTIFICIAL (begin) = 1;
9744 pushdecl (begin);
9745 cp_finish_decl (begin, begin_expr,
9746 /*is_constant_init*/false, NULL_TREE,
9747 LOOKUP_ONLYCONVERTING);
9749 end = build_decl (input_location, VAR_DECL,
9750 get_identifier ("__for_end"), iter_type);
9751 TREE_USED (end) = 1;
9752 DECL_ARTIFICIAL (end) = 1;
9753 pushdecl (end);
9754 cp_finish_decl (end, end_expr,
9755 /*is_constant_init*/false, NULL_TREE,
9756 LOOKUP_ONLYCONVERTING);
9758 finish_for_init_stmt (statement);
9760 /* The new for condition. */
9761 condition = build_x_binary_op (input_location, NE_EXPR,
9762 begin, ERROR_MARK,
9763 end, ERROR_MARK,
9764 NULL, tf_warning_or_error);
9765 finish_for_cond (condition, statement);
9767 /* The new increment expression. */
9768 expression = finish_unary_op_expr (input_location,
9769 PREINCREMENT_EXPR, begin);
9770 finish_for_expr (expression, statement);
9772 /* The declaration is initialized with *__begin inside the loop body. */
9773 cp_finish_decl (range_decl,
9774 build_x_indirect_ref (input_location, begin, RO_NULL,
9775 tf_warning_or_error),
9776 /*is_constant_init*/false, NULL_TREE,
9777 LOOKUP_ONLYCONVERTING);
9779 return statement;
9782 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9783 We need to solve both at the same time because the method used
9784 depends on the existence of members begin or end.
9785 Returns the type deduced for the iterator expression. */
9787 static tree
9788 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9790 if (error_operand_p (range))
9792 *begin = *end = error_mark_node;
9793 return error_mark_node;
9796 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9798 error ("range-based %<for%> expression of type %qT "
9799 "has incomplete type", TREE_TYPE (range));
9800 *begin = *end = error_mark_node;
9801 return error_mark_node;
9803 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9805 /* If RANGE is an array, we will use pointer arithmetic. */
9806 *begin = range;
9807 *end = build_binary_op (input_location, PLUS_EXPR,
9808 range,
9809 array_type_nelts_top (TREE_TYPE (range)),
9811 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9813 else
9815 /* If it is not an array, we must do a bit of magic. */
9816 tree id_begin, id_end;
9817 tree member_begin, member_end;
9819 *begin = *end = error_mark_node;
9821 id_begin = get_identifier ("begin");
9822 id_end = get_identifier ("end");
9823 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9824 /*protect=*/2, /*want_type=*/false,
9825 tf_warning_or_error);
9826 member_end = lookup_member (TREE_TYPE (range), id_end,
9827 /*protect=*/2, /*want_type=*/false,
9828 tf_warning_or_error);
9830 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9832 /* Use the member functions. */
9833 if (member_begin != NULL_TREE)
9834 *begin = cp_parser_range_for_member_function (range, id_begin);
9835 else
9836 error ("range-based %<for%> expression of type %qT has an "
9837 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9839 if (member_end != NULL_TREE)
9840 *end = cp_parser_range_for_member_function (range, id_end);
9841 else
9842 error ("range-based %<for%> expression of type %qT has a "
9843 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9845 else
9847 /* Use global functions with ADL. */
9848 vec<tree, va_gc> *vec;
9849 vec = make_tree_vector ();
9851 vec_safe_push (vec, range);
9853 member_begin = perform_koenig_lookup (id_begin, vec,
9854 /*include_std=*/true,
9855 tf_warning_or_error);
9856 *begin = finish_call_expr (member_begin, &vec, false, true,
9857 tf_warning_or_error);
9858 member_end = perform_koenig_lookup (id_end, vec,
9859 /*include_std=*/true,
9860 tf_warning_or_error);
9861 *end = finish_call_expr (member_end, &vec, false, true,
9862 tf_warning_or_error);
9864 release_tree_vector (vec);
9867 /* Last common checks. */
9868 if (*begin == error_mark_node || *end == error_mark_node)
9870 /* If one of the expressions is an error do no more checks. */
9871 *begin = *end = error_mark_node;
9872 return error_mark_node;
9874 else
9876 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9877 /* The unqualified type of the __begin and __end temporaries should
9878 be the same, as required by the multiple auto declaration. */
9879 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9880 error ("inconsistent begin/end types in range-based %<for%> "
9881 "statement: %qT and %qT",
9882 TREE_TYPE (*begin), TREE_TYPE (*end));
9883 return iter_type;
9888 /* Helper function for cp_parser_perform_range_for_lookup.
9889 Builds a tree for RANGE.IDENTIFIER(). */
9891 static tree
9892 cp_parser_range_for_member_function (tree range, tree identifier)
9894 tree member, res;
9895 vec<tree, va_gc> *vec;
9897 member = finish_class_member_access_expr (range, identifier,
9898 false, tf_warning_or_error);
9899 if (member == error_mark_node)
9900 return error_mark_node;
9902 vec = make_tree_vector ();
9903 res = finish_call_expr (member, &vec,
9904 /*disallow_virtual=*/false,
9905 /*koenig_p=*/false,
9906 tf_warning_or_error);
9907 release_tree_vector (vec);
9908 return res;
9911 /* Parse an iteration-statement.
9913 iteration-statement:
9914 while ( condition ) statement
9915 do statement while ( expression ) ;
9916 for ( for-init-statement condition [opt] ; expression [opt] )
9917 statement
9919 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9921 static tree
9922 cp_parser_iteration_statement (cp_parser* parser)
9924 cp_token *token;
9925 enum rid keyword;
9926 tree statement;
9927 unsigned char in_statement;
9929 /* Peek at the next token. */
9930 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9931 if (!token)
9932 return error_mark_node;
9934 /* Remember whether or not we are already within an iteration
9935 statement. */
9936 in_statement = parser->in_statement;
9938 /* See what kind of keyword it is. */
9939 keyword = token->keyword;
9940 switch (keyword)
9942 case RID_WHILE:
9944 tree condition;
9946 /* Begin the while-statement. */
9947 statement = begin_while_stmt ();
9948 /* Look for the `('. */
9949 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9950 /* Parse the condition. */
9951 condition = cp_parser_condition (parser);
9952 finish_while_stmt_cond (condition, statement);
9953 /* Look for the `)'. */
9954 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9955 /* Parse the dependent statement. */
9956 parser->in_statement = IN_ITERATION_STMT;
9957 cp_parser_already_scoped_statement (parser);
9958 parser->in_statement = in_statement;
9959 /* We're done with the while-statement. */
9960 finish_while_stmt (statement);
9962 break;
9964 case RID_DO:
9966 tree expression;
9968 /* Begin the do-statement. */
9969 statement = begin_do_stmt ();
9970 /* Parse the body of the do-statement. */
9971 parser->in_statement = IN_ITERATION_STMT;
9972 cp_parser_implicitly_scoped_statement (parser, NULL);
9973 parser->in_statement = in_statement;
9974 finish_do_body (statement);
9975 /* Look for the `while' keyword. */
9976 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9977 /* Look for the `('. */
9978 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9979 /* Parse the expression. */
9980 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9981 /* We're done with the do-statement. */
9982 finish_do_stmt (expression, statement);
9983 /* Look for the `)'. */
9984 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9985 /* Look for the `;'. */
9986 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9988 break;
9990 case RID_FOR:
9992 /* Look for the `('. */
9993 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9995 statement = cp_parser_for (parser);
9997 /* Look for the `)'. */
9998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10000 /* Parse the body of the for-statement. */
10001 parser->in_statement = IN_ITERATION_STMT;
10002 cp_parser_already_scoped_statement (parser);
10003 parser->in_statement = in_statement;
10005 /* We're done with the for-statement. */
10006 finish_for_stmt (statement);
10008 break;
10010 default:
10011 cp_parser_error (parser, "expected iteration-statement");
10012 statement = error_mark_node;
10013 break;
10016 return statement;
10019 /* Parse a for-init-statement or the declarator of a range-based-for.
10020 Returns true if a range-based-for declaration is seen.
10022 for-init-statement:
10023 expression-statement
10024 simple-declaration */
10026 static bool
10027 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10029 /* If the next token is a `;', then we have an empty
10030 expression-statement. Grammatically, this is also a
10031 simple-declaration, but an invalid one, because it does not
10032 declare anything. Therefore, if we did not handle this case
10033 specially, we would issue an error message about an invalid
10034 declaration. */
10035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10037 bool is_range_for = false;
10038 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10040 parser->colon_corrects_to_scope_p = false;
10042 /* We're going to speculatively look for a declaration, falling back
10043 to an expression, if necessary. */
10044 cp_parser_parse_tentatively (parser);
10045 /* Parse the declaration. */
10046 cp_parser_simple_declaration (parser,
10047 /*function_definition_allowed_p=*/false,
10048 decl);
10049 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10050 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10052 /* It is a range-for, consume the ':' */
10053 cp_lexer_consume_token (parser->lexer);
10054 is_range_for = true;
10055 if (cxx_dialect < cxx0x)
10057 error_at (cp_lexer_peek_token (parser->lexer)->location,
10058 "range-based %<for%> loops are not allowed "
10059 "in C++98 mode");
10060 *decl = error_mark_node;
10063 else
10064 /* The ';' is not consumed yet because we told
10065 cp_parser_simple_declaration not to. */
10066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10068 if (cp_parser_parse_definitely (parser))
10069 return is_range_for;
10070 /* If the tentative parse failed, then we shall need to look for an
10071 expression-statement. */
10073 /* If we are here, it is an expression-statement. */
10074 cp_parser_expression_statement (parser, NULL_TREE);
10075 return false;
10078 /* Parse a jump-statement.
10080 jump-statement:
10081 break ;
10082 continue ;
10083 return expression [opt] ;
10084 return braced-init-list ;
10085 goto identifier ;
10087 GNU extension:
10089 jump-statement:
10090 goto * expression ;
10092 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10094 static tree
10095 cp_parser_jump_statement (cp_parser* parser)
10097 tree statement = error_mark_node;
10098 cp_token *token;
10099 enum rid keyword;
10100 unsigned char in_statement;
10102 /* Peek at the next token. */
10103 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10104 if (!token)
10105 return error_mark_node;
10107 /* See what kind of keyword it is. */
10108 keyword = token->keyword;
10109 switch (keyword)
10111 case RID_BREAK:
10112 in_statement = parser->in_statement & ~IN_IF_STMT;
10113 switch (in_statement)
10115 case 0:
10116 error_at (token->location, "break statement not within loop or switch");
10117 break;
10118 default:
10119 gcc_assert ((in_statement & IN_SWITCH_STMT)
10120 || in_statement == IN_ITERATION_STMT);
10121 statement = finish_break_stmt ();
10122 break;
10123 case IN_OMP_BLOCK:
10124 error_at (token->location, "invalid exit from OpenMP structured block");
10125 break;
10126 case IN_OMP_FOR:
10127 error_at (token->location, "break statement used with OpenMP for loop");
10128 break;
10130 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10131 break;
10133 case RID_CONTINUE:
10134 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10136 case 0:
10137 error_at (token->location, "continue statement not within a loop");
10138 break;
10139 case IN_ITERATION_STMT:
10140 case IN_OMP_FOR:
10141 statement = finish_continue_stmt ();
10142 break;
10143 case IN_OMP_BLOCK:
10144 error_at (token->location, "invalid exit from OpenMP structured block");
10145 break;
10146 default:
10147 gcc_unreachable ();
10149 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10150 break;
10152 case RID_RETURN:
10154 tree expr;
10155 bool expr_non_constant_p;
10157 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10159 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10160 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10162 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10163 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10164 else
10165 /* If the next token is a `;', then there is no
10166 expression. */
10167 expr = NULL_TREE;
10168 /* Build the return-statement. */
10169 statement = finish_return_stmt (expr);
10170 /* Look for the final `;'. */
10171 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10173 break;
10175 case RID_GOTO:
10176 /* Create the goto-statement. */
10177 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10179 /* Issue a warning about this use of a GNU extension. */
10180 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10181 /* Consume the '*' token. */
10182 cp_lexer_consume_token (parser->lexer);
10183 /* Parse the dependent expression. */
10184 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10186 else
10187 finish_goto_stmt (cp_parser_identifier (parser));
10188 /* Look for the final `;'. */
10189 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10190 break;
10192 default:
10193 cp_parser_error (parser, "expected jump-statement");
10194 break;
10197 return statement;
10200 /* Parse a declaration-statement.
10202 declaration-statement:
10203 block-declaration */
10205 static void
10206 cp_parser_declaration_statement (cp_parser* parser)
10208 void *p;
10210 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10211 p = obstack_alloc (&declarator_obstack, 0);
10213 /* Parse the block-declaration. */
10214 cp_parser_block_declaration (parser, /*statement_p=*/true);
10216 /* Free any declarators allocated. */
10217 obstack_free (&declarator_obstack, p);
10219 /* Finish off the statement. */
10220 finish_stmt ();
10223 /* Some dependent statements (like `if (cond) statement'), are
10224 implicitly in their own scope. In other words, if the statement is
10225 a single statement (as opposed to a compound-statement), it is
10226 none-the-less treated as if it were enclosed in braces. Any
10227 declarations appearing in the dependent statement are out of scope
10228 after control passes that point. This function parses a statement,
10229 but ensures that is in its own scope, even if it is not a
10230 compound-statement.
10232 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10233 is a (possibly labeled) if statement which is not enclosed in
10234 braces and has an else clause. This is used to implement
10235 -Wparentheses.
10237 Returns the new statement. */
10239 static tree
10240 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10242 tree statement;
10244 if (if_p != NULL)
10245 *if_p = false;
10247 /* Mark if () ; with a special NOP_EXPR. */
10248 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10250 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10251 cp_lexer_consume_token (parser->lexer);
10252 statement = add_stmt (build_empty_stmt (loc));
10254 /* if a compound is opened, we simply parse the statement directly. */
10255 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10256 statement = cp_parser_compound_statement (parser, NULL, false, false);
10257 /* If the token is not a `{', then we must take special action. */
10258 else
10260 /* Create a compound-statement. */
10261 statement = begin_compound_stmt (0);
10262 /* Parse the dependent-statement. */
10263 cp_parser_statement (parser, NULL_TREE, false, if_p);
10264 /* Finish the dummy compound-statement. */
10265 finish_compound_stmt (statement);
10268 /* Return the statement. */
10269 return statement;
10272 /* For some dependent statements (like `while (cond) statement'), we
10273 have already created a scope. Therefore, even if the dependent
10274 statement is a compound-statement, we do not want to create another
10275 scope. */
10277 static void
10278 cp_parser_already_scoped_statement (cp_parser* parser)
10280 /* If the token is a `{', then we must take special action. */
10281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10282 cp_parser_statement (parser, NULL_TREE, false, NULL);
10283 else
10285 /* Avoid calling cp_parser_compound_statement, so that we
10286 don't create a new scope. Do everything else by hand. */
10287 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10288 /* If the next keyword is `__label__' we have a label declaration. */
10289 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10290 cp_parser_label_declaration (parser);
10291 /* Parse an (optional) statement-seq. */
10292 cp_parser_statement_seq_opt (parser, NULL_TREE);
10293 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10297 /* Declarations [gram.dcl.dcl] */
10299 /* Parse an optional declaration-sequence.
10301 declaration-seq:
10302 declaration
10303 declaration-seq declaration */
10305 static void
10306 cp_parser_declaration_seq_opt (cp_parser* parser)
10308 while (true)
10310 cp_token *token;
10312 token = cp_lexer_peek_token (parser->lexer);
10314 if (token->type == CPP_CLOSE_BRACE
10315 || token->type == CPP_EOF
10316 || token->type == CPP_PRAGMA_EOL)
10317 break;
10319 if (token->type == CPP_SEMICOLON)
10321 /* A declaration consisting of a single semicolon is
10322 invalid. Allow it unless we're being pedantic. */
10323 cp_lexer_consume_token (parser->lexer);
10324 if (!in_system_header)
10325 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10326 continue;
10329 /* If we're entering or exiting a region that's implicitly
10330 extern "C", modify the lang context appropriately. */
10331 if (!parser->implicit_extern_c && token->implicit_extern_c)
10333 push_lang_context (lang_name_c);
10334 parser->implicit_extern_c = true;
10336 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10338 pop_lang_context ();
10339 parser->implicit_extern_c = false;
10342 if (token->type == CPP_PRAGMA)
10344 /* A top-level declaration can consist solely of a #pragma.
10345 A nested declaration cannot, so this is done here and not
10346 in cp_parser_declaration. (A #pragma at block scope is
10347 handled in cp_parser_statement.) */
10348 cp_parser_pragma (parser, pragma_external);
10349 continue;
10352 /* Parse the declaration itself. */
10353 cp_parser_declaration (parser);
10357 /* Parse a declaration.
10359 declaration:
10360 block-declaration
10361 function-definition
10362 template-declaration
10363 explicit-instantiation
10364 explicit-specialization
10365 linkage-specification
10366 namespace-definition
10368 GNU extension:
10370 declaration:
10371 __extension__ declaration */
10373 static void
10374 cp_parser_declaration (cp_parser* parser)
10376 cp_token token1;
10377 cp_token token2;
10378 int saved_pedantic;
10379 void *p;
10380 tree attributes = NULL_TREE;
10382 /* Check for the `__extension__' keyword. */
10383 if (cp_parser_extension_opt (parser, &saved_pedantic))
10385 /* Parse the qualified declaration. */
10386 cp_parser_declaration (parser);
10387 /* Restore the PEDANTIC flag. */
10388 pedantic = saved_pedantic;
10390 return;
10393 /* Try to figure out what kind of declaration is present. */
10394 token1 = *cp_lexer_peek_token (parser->lexer);
10396 if (token1.type != CPP_EOF)
10397 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10398 else
10400 token2.type = CPP_EOF;
10401 token2.keyword = RID_MAX;
10404 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10405 p = obstack_alloc (&declarator_obstack, 0);
10407 /* If the next token is `extern' and the following token is a string
10408 literal, then we have a linkage specification. */
10409 if (token1.keyword == RID_EXTERN
10410 && cp_parser_is_pure_string_literal (&token2))
10411 cp_parser_linkage_specification (parser);
10412 /* If the next token is `template', then we have either a template
10413 declaration, an explicit instantiation, or an explicit
10414 specialization. */
10415 else if (token1.keyword == RID_TEMPLATE)
10417 /* `template <>' indicates a template specialization. */
10418 if (token2.type == CPP_LESS
10419 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10420 cp_parser_explicit_specialization (parser);
10421 /* `template <' indicates a template declaration. */
10422 else if (token2.type == CPP_LESS)
10423 cp_parser_template_declaration (parser, /*member_p=*/false);
10424 /* Anything else must be an explicit instantiation. */
10425 else
10426 cp_parser_explicit_instantiation (parser);
10428 /* If the next token is `export', then we have a template
10429 declaration. */
10430 else if (token1.keyword == RID_EXPORT)
10431 cp_parser_template_declaration (parser, /*member_p=*/false);
10432 /* If the next token is `extern', 'static' or 'inline' and the one
10433 after that is `template', we have a GNU extended explicit
10434 instantiation directive. */
10435 else if (cp_parser_allow_gnu_extensions_p (parser)
10436 && (token1.keyword == RID_EXTERN
10437 || token1.keyword == RID_STATIC
10438 || token1.keyword == RID_INLINE)
10439 && token2.keyword == RID_TEMPLATE)
10440 cp_parser_explicit_instantiation (parser);
10441 /* If the next token is `namespace', check for a named or unnamed
10442 namespace definition. */
10443 else if (token1.keyword == RID_NAMESPACE
10444 && (/* A named namespace definition. */
10445 (token2.type == CPP_NAME
10446 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10447 != CPP_EQ))
10448 /* An unnamed namespace definition. */
10449 || token2.type == CPP_OPEN_BRACE
10450 || token2.keyword == RID_ATTRIBUTE))
10451 cp_parser_namespace_definition (parser);
10452 /* An inline (associated) namespace definition. */
10453 else if (token1.keyword == RID_INLINE
10454 && token2.keyword == RID_NAMESPACE)
10455 cp_parser_namespace_definition (parser);
10456 /* Objective-C++ declaration/definition. */
10457 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10458 cp_parser_objc_declaration (parser, NULL_TREE);
10459 else if (c_dialect_objc ()
10460 && token1.keyword == RID_ATTRIBUTE
10461 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10462 cp_parser_objc_declaration (parser, attributes);
10463 /* We must have either a block declaration or a function
10464 definition. */
10465 else
10466 /* Try to parse a block-declaration, or a function-definition. */
10467 cp_parser_block_declaration (parser, /*statement_p=*/false);
10469 /* Free any declarators allocated. */
10470 obstack_free (&declarator_obstack, p);
10473 /* Parse a block-declaration.
10475 block-declaration:
10476 simple-declaration
10477 asm-definition
10478 namespace-alias-definition
10479 using-declaration
10480 using-directive
10482 GNU Extension:
10484 block-declaration:
10485 __extension__ block-declaration
10487 C++0x Extension:
10489 block-declaration:
10490 static_assert-declaration
10492 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10493 part of a declaration-statement. */
10495 static void
10496 cp_parser_block_declaration (cp_parser *parser,
10497 bool statement_p)
10499 cp_token *token1;
10500 int saved_pedantic;
10502 /* Check for the `__extension__' keyword. */
10503 if (cp_parser_extension_opt (parser, &saved_pedantic))
10505 /* Parse the qualified declaration. */
10506 cp_parser_block_declaration (parser, statement_p);
10507 /* Restore the PEDANTIC flag. */
10508 pedantic = saved_pedantic;
10510 return;
10513 /* Peek at the next token to figure out which kind of declaration is
10514 present. */
10515 token1 = cp_lexer_peek_token (parser->lexer);
10517 /* If the next keyword is `asm', we have an asm-definition. */
10518 if (token1->keyword == RID_ASM)
10520 if (statement_p)
10521 cp_parser_commit_to_tentative_parse (parser);
10522 cp_parser_asm_definition (parser);
10524 /* If the next keyword is `namespace', we have a
10525 namespace-alias-definition. */
10526 else if (token1->keyword == RID_NAMESPACE)
10527 cp_parser_namespace_alias_definition (parser);
10528 /* If the next keyword is `using', we have a
10529 using-declaration, a using-directive, or an alias-declaration. */
10530 else if (token1->keyword == RID_USING)
10532 cp_token *token2;
10534 if (statement_p)
10535 cp_parser_commit_to_tentative_parse (parser);
10536 /* If the token after `using' is `namespace', then we have a
10537 using-directive. */
10538 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10539 if (token2->keyword == RID_NAMESPACE)
10540 cp_parser_using_directive (parser);
10541 /* If the second token after 'using' is '=', then we have an
10542 alias-declaration. */
10543 else if (cxx_dialect >= cxx0x
10544 && token2->type == CPP_NAME
10545 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10546 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10547 cp_parser_alias_declaration (parser);
10548 /* Otherwise, it's a using-declaration. */
10549 else
10550 cp_parser_using_declaration (parser,
10551 /*access_declaration_p=*/false);
10553 /* If the next keyword is `__label__' we have a misplaced label
10554 declaration. */
10555 else if (token1->keyword == RID_LABEL)
10557 cp_lexer_consume_token (parser->lexer);
10558 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10559 cp_parser_skip_to_end_of_statement (parser);
10560 /* If the next token is now a `;', consume it. */
10561 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10562 cp_lexer_consume_token (parser->lexer);
10564 /* If the next token is `static_assert' we have a static assertion. */
10565 else if (token1->keyword == RID_STATIC_ASSERT)
10566 cp_parser_static_assert (parser, /*member_p=*/false);
10567 /* Anything else must be a simple-declaration. */
10568 else
10569 cp_parser_simple_declaration (parser, !statement_p,
10570 /*maybe_range_for_decl*/NULL);
10573 /* Parse a simple-declaration.
10575 simple-declaration:
10576 decl-specifier-seq [opt] init-declarator-list [opt] ;
10578 init-declarator-list:
10579 init-declarator
10580 init-declarator-list , init-declarator
10582 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10583 function-definition as a simple-declaration.
10585 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10586 parsed declaration if it is an uninitialized single declarator not followed
10587 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10588 if present, will not be consumed. */
10590 static void
10591 cp_parser_simple_declaration (cp_parser* parser,
10592 bool function_definition_allowed_p,
10593 tree *maybe_range_for_decl)
10595 cp_decl_specifier_seq decl_specifiers;
10596 int declares_class_or_enum;
10597 bool saw_declarator;
10599 if (maybe_range_for_decl)
10600 *maybe_range_for_decl = NULL_TREE;
10602 /* Defer access checks until we know what is being declared; the
10603 checks for names appearing in the decl-specifier-seq should be
10604 done as if we were in the scope of the thing being declared. */
10605 push_deferring_access_checks (dk_deferred);
10607 /* Parse the decl-specifier-seq. We have to keep track of whether
10608 or not the decl-specifier-seq declares a named class or
10609 enumeration type, since that is the only case in which the
10610 init-declarator-list is allowed to be empty.
10612 [dcl.dcl]
10614 In a simple-declaration, the optional init-declarator-list can be
10615 omitted only when declaring a class or enumeration, that is when
10616 the decl-specifier-seq contains either a class-specifier, an
10617 elaborated-type-specifier, or an enum-specifier. */
10618 cp_parser_decl_specifier_seq (parser,
10619 CP_PARSER_FLAGS_OPTIONAL,
10620 &decl_specifiers,
10621 &declares_class_or_enum);
10622 /* We no longer need to defer access checks. */
10623 stop_deferring_access_checks ();
10625 /* In a block scope, a valid declaration must always have a
10626 decl-specifier-seq. By not trying to parse declarators, we can
10627 resolve the declaration/expression ambiguity more quickly. */
10628 if (!function_definition_allowed_p
10629 && !decl_specifiers.any_specifiers_p)
10631 cp_parser_error (parser, "expected declaration");
10632 goto done;
10635 /* If the next two tokens are both identifiers, the code is
10636 erroneous. The usual cause of this situation is code like:
10638 T t;
10640 where "T" should name a type -- but does not. */
10641 if (!decl_specifiers.any_type_specifiers_p
10642 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10644 /* If parsing tentatively, we should commit; we really are
10645 looking at a declaration. */
10646 cp_parser_commit_to_tentative_parse (parser);
10647 /* Give up. */
10648 goto done;
10651 /* If we have seen at least one decl-specifier, and the next token
10652 is not a parenthesis, then we must be looking at a declaration.
10653 (After "int (" we might be looking at a functional cast.) */
10654 if (decl_specifiers.any_specifiers_p
10655 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10656 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10657 && !cp_parser_error_occurred (parser))
10658 cp_parser_commit_to_tentative_parse (parser);
10660 /* Keep going until we hit the `;' at the end of the simple
10661 declaration. */
10662 saw_declarator = false;
10663 while (cp_lexer_next_token_is_not (parser->lexer,
10664 CPP_SEMICOLON))
10666 cp_token *token;
10667 bool function_definition_p;
10668 tree decl;
10670 if (saw_declarator)
10672 /* If we are processing next declarator, coma is expected */
10673 token = cp_lexer_peek_token (parser->lexer);
10674 gcc_assert (token->type == CPP_COMMA);
10675 cp_lexer_consume_token (parser->lexer);
10676 if (maybe_range_for_decl)
10677 *maybe_range_for_decl = error_mark_node;
10679 else
10680 saw_declarator = true;
10682 /* Parse the init-declarator. */
10683 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10684 /*checks=*/NULL,
10685 function_definition_allowed_p,
10686 /*member_p=*/false,
10687 declares_class_or_enum,
10688 &function_definition_p,
10689 maybe_range_for_decl);
10690 /* If an error occurred while parsing tentatively, exit quickly.
10691 (That usually happens when in the body of a function; each
10692 statement is treated as a declaration-statement until proven
10693 otherwise.) */
10694 if (cp_parser_error_occurred (parser))
10695 goto done;
10696 /* Handle function definitions specially. */
10697 if (function_definition_p)
10699 /* If the next token is a `,', then we are probably
10700 processing something like:
10702 void f() {}, *p;
10704 which is erroneous. */
10705 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10707 cp_token *token = cp_lexer_peek_token (parser->lexer);
10708 error_at (token->location,
10709 "mixing"
10710 " declarations and function-definitions is forbidden");
10712 /* Otherwise, we're done with the list of declarators. */
10713 else
10715 pop_deferring_access_checks ();
10716 return;
10719 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10720 *maybe_range_for_decl = decl;
10721 /* The next token should be either a `,' or a `;'. */
10722 token = cp_lexer_peek_token (parser->lexer);
10723 /* If it's a `,', there are more declarators to come. */
10724 if (token->type == CPP_COMMA)
10725 /* will be consumed next time around */;
10726 /* If it's a `;', we are done. */
10727 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10728 break;
10729 /* Anything else is an error. */
10730 else
10732 /* If we have already issued an error message we don't need
10733 to issue another one. */
10734 if (decl != error_mark_node
10735 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10736 cp_parser_error (parser, "expected %<,%> or %<;%>");
10737 /* Skip tokens until we reach the end of the statement. */
10738 cp_parser_skip_to_end_of_statement (parser);
10739 /* If the next token is now a `;', consume it. */
10740 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10741 cp_lexer_consume_token (parser->lexer);
10742 goto done;
10744 /* After the first time around, a function-definition is not
10745 allowed -- even if it was OK at first. For example:
10747 int i, f() {}
10749 is not valid. */
10750 function_definition_allowed_p = false;
10753 /* Issue an error message if no declarators are present, and the
10754 decl-specifier-seq does not itself declare a class or
10755 enumeration. */
10756 if (!saw_declarator)
10758 if (cp_parser_declares_only_class_p (parser))
10759 shadow_tag (&decl_specifiers);
10760 /* Perform any deferred access checks. */
10761 perform_deferred_access_checks (tf_warning_or_error);
10764 /* Consume the `;'. */
10765 if (!maybe_range_for_decl)
10766 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10768 done:
10769 pop_deferring_access_checks ();
10772 /* Parse a decl-specifier-seq.
10774 decl-specifier-seq:
10775 decl-specifier-seq [opt] decl-specifier
10776 decl-specifier attribute-specifier-seq [opt] (C++11)
10778 decl-specifier:
10779 storage-class-specifier
10780 type-specifier
10781 function-specifier
10782 friend
10783 typedef
10785 GNU Extension:
10787 decl-specifier:
10788 attributes
10790 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10792 The parser flags FLAGS is used to control type-specifier parsing.
10794 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10795 flags:
10797 1: one of the decl-specifiers is an elaborated-type-specifier
10798 (i.e., a type declaration)
10799 2: one of the decl-specifiers is an enum-specifier or a
10800 class-specifier (i.e., a type definition)
10804 static void
10805 cp_parser_decl_specifier_seq (cp_parser* parser,
10806 cp_parser_flags flags,
10807 cp_decl_specifier_seq *decl_specs,
10808 int* declares_class_or_enum)
10810 bool constructor_possible_p = !parser->in_declarator_p;
10811 bool found_decl_spec = false;
10812 cp_token *start_token = NULL;
10813 cp_decl_spec ds;
10815 /* Clear DECL_SPECS. */
10816 clear_decl_specs (decl_specs);
10818 /* Assume no class or enumeration type is declared. */
10819 *declares_class_or_enum = 0;
10821 /* Keep reading specifiers until there are no more to read. */
10822 while (true)
10824 bool constructor_p;
10825 cp_token *token;
10826 ds = ds_last;
10828 /* Peek at the next token. */
10829 token = cp_lexer_peek_token (parser->lexer);
10831 /* Save the first token of the decl spec list for error
10832 reporting. */
10833 if (!start_token)
10834 start_token = token;
10835 /* Handle attributes. */
10836 if (cp_next_tokens_can_be_attribute_p (parser))
10838 /* Parse the attributes. */
10839 tree attrs = cp_parser_attributes_opt (parser);
10841 /* In a sequence of declaration specifiers, c++11 attributes
10842 appertain to the type that precede them. In that case
10843 [dcl.spec]/1 says:
10845 The attribute-specifier-seq affects the type only for
10846 the declaration it appears in, not other declarations
10847 involving the same type.
10849 But for now let's force the user to position the
10850 attribute either at the beginning of the declaration or
10851 after the declarator-id, which would clearly mean that it
10852 applies to the declarator. */
10853 if (cxx11_attribute_p (attrs))
10855 if (!found_decl_spec)
10856 /* The c++11 attribute is at the beginning of the
10857 declaration. It appertains to the entity being
10858 declared. */;
10859 else
10861 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10863 /* This is an attribute following a
10864 class-specifier. */
10865 if (decl_specs->type_definition_p)
10866 warn_misplaced_attr_for_class_type (token->location,
10867 decl_specs->type);
10868 attrs = NULL_TREE;
10870 else
10872 decl_specs->std_attributes
10873 = chainon (decl_specs->std_attributes,
10874 attrs);
10875 if (decl_specs->locations[ds_std_attribute] == 0)
10876 decl_specs->locations[ds_std_attribute] = token->location;
10878 continue;
10882 decl_specs->attributes
10883 = chainon (decl_specs->attributes,
10884 attrs);
10885 if (decl_specs->locations[ds_attribute] == 0)
10886 decl_specs->locations[ds_attribute] = token->location;
10887 continue;
10889 /* Assume we will find a decl-specifier keyword. */
10890 found_decl_spec = true;
10891 /* If the next token is an appropriate keyword, we can simply
10892 add it to the list. */
10893 switch (token->keyword)
10895 /* decl-specifier:
10896 friend
10897 constexpr */
10898 case RID_FRIEND:
10899 if (!at_class_scope_p ())
10901 error_at (token->location, "%<friend%> used outside of class");
10902 cp_lexer_purge_token (parser->lexer);
10904 else
10906 ds = ds_friend;
10907 /* Consume the token. */
10908 cp_lexer_consume_token (parser->lexer);
10910 break;
10912 case RID_CONSTEXPR:
10913 ds = ds_constexpr;
10914 cp_lexer_consume_token (parser->lexer);
10915 break;
10917 /* function-specifier:
10918 inline
10919 virtual
10920 explicit */
10921 case RID_INLINE:
10922 case RID_VIRTUAL:
10923 case RID_EXPLICIT:
10924 cp_parser_function_specifier_opt (parser, decl_specs);
10925 break;
10927 /* decl-specifier:
10928 typedef */
10929 case RID_TYPEDEF:
10930 ds = ds_typedef;
10931 /* Consume the token. */
10932 cp_lexer_consume_token (parser->lexer);
10933 /* A constructor declarator cannot appear in a typedef. */
10934 constructor_possible_p = false;
10935 /* The "typedef" keyword can only occur in a declaration; we
10936 may as well commit at this point. */
10937 cp_parser_commit_to_tentative_parse (parser);
10939 if (decl_specs->storage_class != sc_none)
10940 decl_specs->conflicting_specifiers_p = true;
10941 break;
10943 /* storage-class-specifier:
10944 auto
10945 register
10946 static
10947 extern
10948 mutable
10950 GNU Extension:
10951 thread */
10952 case RID_AUTO:
10953 if (cxx_dialect == cxx98)
10955 /* Consume the token. */
10956 cp_lexer_consume_token (parser->lexer);
10958 /* Complain about `auto' as a storage specifier, if
10959 we're complaining about C++0x compatibility. */
10960 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10961 " changes meaning in C++11; please remove it");
10963 /* Set the storage class anyway. */
10964 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10965 token);
10967 else
10968 /* C++0x auto type-specifier. */
10969 found_decl_spec = false;
10970 break;
10972 case RID_REGISTER:
10973 case RID_STATIC:
10974 case RID_EXTERN:
10975 case RID_MUTABLE:
10976 /* Consume the token. */
10977 cp_lexer_consume_token (parser->lexer);
10978 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10979 token);
10980 break;
10981 case RID_THREAD:
10982 /* Consume the token. */
10983 ds = ds_thread;
10984 cp_lexer_consume_token (parser->lexer);
10985 break;
10987 default:
10988 /* We did not yet find a decl-specifier yet. */
10989 found_decl_spec = false;
10990 break;
10993 if (found_decl_spec
10994 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10995 && token->keyword != RID_CONSTEXPR)
10996 error ("decl-specifier invalid in condition");
10998 if (ds != ds_last)
10999 set_and_check_decl_spec_loc (decl_specs, ds, token);
11001 /* Constructors are a special case. The `S' in `S()' is not a
11002 decl-specifier; it is the beginning of the declarator. */
11003 constructor_p
11004 = (!found_decl_spec
11005 && constructor_possible_p
11006 && (cp_parser_constructor_declarator_p
11007 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11009 /* If we don't have a DECL_SPEC yet, then we must be looking at
11010 a type-specifier. */
11011 if (!found_decl_spec && !constructor_p)
11013 int decl_spec_declares_class_or_enum;
11014 bool is_cv_qualifier;
11015 tree type_spec;
11017 type_spec
11018 = cp_parser_type_specifier (parser, flags,
11019 decl_specs,
11020 /*is_declaration=*/true,
11021 &decl_spec_declares_class_or_enum,
11022 &is_cv_qualifier);
11023 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11025 /* If this type-specifier referenced a user-defined type
11026 (a typedef, class-name, etc.), then we can't allow any
11027 more such type-specifiers henceforth.
11029 [dcl.spec]
11031 The longest sequence of decl-specifiers that could
11032 possibly be a type name is taken as the
11033 decl-specifier-seq of a declaration. The sequence shall
11034 be self-consistent as described below.
11036 [dcl.type]
11038 As a general rule, at most one type-specifier is allowed
11039 in the complete decl-specifier-seq of a declaration. The
11040 only exceptions are the following:
11042 -- const or volatile can be combined with any other
11043 type-specifier.
11045 -- signed or unsigned can be combined with char, long,
11046 short, or int.
11048 -- ..
11050 Example:
11052 typedef char* Pc;
11053 void g (const int Pc);
11055 Here, Pc is *not* part of the decl-specifier seq; it's
11056 the declarator. Therefore, once we see a type-specifier
11057 (other than a cv-qualifier), we forbid any additional
11058 user-defined types. We *do* still allow things like `int
11059 int' to be considered a decl-specifier-seq, and issue the
11060 error message later. */
11061 if (type_spec && !is_cv_qualifier)
11062 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11063 /* A constructor declarator cannot follow a type-specifier. */
11064 if (type_spec)
11066 constructor_possible_p = false;
11067 found_decl_spec = true;
11068 if (!is_cv_qualifier)
11069 decl_specs->any_type_specifiers_p = true;
11073 /* If we still do not have a DECL_SPEC, then there are no more
11074 decl-specifiers. */
11075 if (!found_decl_spec)
11076 break;
11078 decl_specs->any_specifiers_p = true;
11079 /* After we see one decl-specifier, further decl-specifiers are
11080 always optional. */
11081 flags |= CP_PARSER_FLAGS_OPTIONAL;
11084 /* Don't allow a friend specifier with a class definition. */
11085 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11086 && (*declares_class_or_enum & 2))
11087 error_at (decl_specs->locations[ds_friend],
11088 "class definition may not be declared a friend");
11091 /* Parse an (optional) storage-class-specifier.
11093 storage-class-specifier:
11094 auto
11095 register
11096 static
11097 extern
11098 mutable
11100 GNU Extension:
11102 storage-class-specifier:
11103 thread
11105 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11107 static tree
11108 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11110 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11112 case RID_AUTO:
11113 if (cxx_dialect != cxx98)
11114 return NULL_TREE;
11115 /* Fall through for C++98. */
11117 case RID_REGISTER:
11118 case RID_STATIC:
11119 case RID_EXTERN:
11120 case RID_MUTABLE:
11121 case RID_THREAD:
11122 /* Consume the token. */
11123 return cp_lexer_consume_token (parser->lexer)->u.value;
11125 default:
11126 return NULL_TREE;
11130 /* Parse an (optional) function-specifier.
11132 function-specifier:
11133 inline
11134 virtual
11135 explicit
11137 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11138 Updates DECL_SPECS, if it is non-NULL. */
11140 static tree
11141 cp_parser_function_specifier_opt (cp_parser* parser,
11142 cp_decl_specifier_seq *decl_specs)
11144 cp_token *token = cp_lexer_peek_token (parser->lexer);
11145 switch (token->keyword)
11147 case RID_INLINE:
11148 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11149 break;
11151 case RID_VIRTUAL:
11152 /* 14.5.2.3 [temp.mem]
11154 A member function template shall not be virtual. */
11155 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11156 error_at (token->location, "templates may not be %<virtual%>");
11157 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11158 break;
11160 case RID_EXPLICIT:
11161 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11162 break;
11164 default:
11165 return NULL_TREE;
11168 /* Consume the token. */
11169 return cp_lexer_consume_token (parser->lexer)->u.value;
11172 /* Parse a linkage-specification.
11174 linkage-specification:
11175 extern string-literal { declaration-seq [opt] }
11176 extern string-literal declaration */
11178 static void
11179 cp_parser_linkage_specification (cp_parser* parser)
11181 tree linkage;
11183 /* Look for the `extern' keyword. */
11184 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11186 /* Look for the string-literal. */
11187 linkage = cp_parser_string_literal (parser, false, false);
11189 /* Transform the literal into an identifier. If the literal is a
11190 wide-character string, or contains embedded NULs, then we can't
11191 handle it as the user wants. */
11192 if (strlen (TREE_STRING_POINTER (linkage))
11193 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11195 cp_parser_error (parser, "invalid linkage-specification");
11196 /* Assume C++ linkage. */
11197 linkage = lang_name_cplusplus;
11199 else
11200 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11202 /* We're now using the new linkage. */
11203 push_lang_context (linkage);
11205 /* If the next token is a `{', then we're using the first
11206 production. */
11207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11209 /* Consume the `{' token. */
11210 cp_lexer_consume_token (parser->lexer);
11211 /* Parse the declarations. */
11212 cp_parser_declaration_seq_opt (parser);
11213 /* Look for the closing `}'. */
11214 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11216 /* Otherwise, there's just one declaration. */
11217 else
11219 bool saved_in_unbraced_linkage_specification_p;
11221 saved_in_unbraced_linkage_specification_p
11222 = parser->in_unbraced_linkage_specification_p;
11223 parser->in_unbraced_linkage_specification_p = true;
11224 cp_parser_declaration (parser);
11225 parser->in_unbraced_linkage_specification_p
11226 = saved_in_unbraced_linkage_specification_p;
11229 /* We're done with the linkage-specification. */
11230 pop_lang_context ();
11233 /* Parse a static_assert-declaration.
11235 static_assert-declaration:
11236 static_assert ( constant-expression , string-literal ) ;
11238 If MEMBER_P, this static_assert is a class member. */
11240 static void
11241 cp_parser_static_assert(cp_parser *parser, bool member_p)
11243 tree condition;
11244 tree message;
11245 cp_token *token;
11246 location_t saved_loc;
11247 bool dummy;
11249 /* Peek at the `static_assert' token so we can keep track of exactly
11250 where the static assertion started. */
11251 token = cp_lexer_peek_token (parser->lexer);
11252 saved_loc = token->location;
11254 /* Look for the `static_assert' keyword. */
11255 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11256 RT_STATIC_ASSERT))
11257 return;
11259 /* We know we are in a static assertion; commit to any tentative
11260 parse. */
11261 if (cp_parser_parsing_tentatively (parser))
11262 cp_parser_commit_to_tentative_parse (parser);
11264 /* Parse the `(' starting the static assertion condition. */
11265 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11267 /* Parse the constant-expression. Allow a non-constant expression
11268 here in order to give better diagnostics in finish_static_assert. */
11269 condition =
11270 cp_parser_constant_expression (parser,
11271 /*allow_non_constant_p=*/true,
11272 /*non_constant_p=*/&dummy);
11274 /* Parse the separating `,'. */
11275 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11277 /* Parse the string-literal message. */
11278 message = cp_parser_string_literal (parser,
11279 /*translate=*/false,
11280 /*wide_ok=*/true);
11282 /* A `)' completes the static assertion. */
11283 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11284 cp_parser_skip_to_closing_parenthesis (parser,
11285 /*recovering=*/true,
11286 /*or_comma=*/false,
11287 /*consume_paren=*/true);
11289 /* A semicolon terminates the declaration. */
11290 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11292 /* Complete the static assertion, which may mean either processing
11293 the static assert now or saving it for template instantiation. */
11294 finish_static_assert (condition, message, saved_loc, member_p);
11297 /* Parse the expression in decltype ( expression ). */
11299 static tree
11300 cp_parser_decltype_expr (cp_parser *parser,
11301 bool &id_expression_or_member_access_p)
11303 cp_token *id_expr_start_token;
11304 tree expr;
11306 /* First, try parsing an id-expression. */
11307 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11308 cp_parser_parse_tentatively (parser);
11309 expr = cp_parser_id_expression (parser,
11310 /*template_keyword_p=*/false,
11311 /*check_dependency_p=*/true,
11312 /*template_p=*/NULL,
11313 /*declarator_p=*/false,
11314 /*optional_p=*/false);
11316 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11318 bool non_integral_constant_expression_p = false;
11319 tree id_expression = expr;
11320 cp_id_kind idk;
11321 const char *error_msg;
11323 if (identifier_p (expr))
11324 /* Lookup the name we got back from the id-expression. */
11325 expr = cp_parser_lookup_name (parser, expr,
11326 none_type,
11327 /*is_template=*/false,
11328 /*is_namespace=*/false,
11329 /*check_dependency=*/true,
11330 /*ambiguous_decls=*/NULL,
11331 id_expr_start_token->location);
11333 if (expr
11334 && expr != error_mark_node
11335 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11336 && TREE_CODE (expr) != TYPE_DECL
11337 && (TREE_CODE (expr) != BIT_NOT_EXPR
11338 || !TYPE_P (TREE_OPERAND (expr, 0)))
11339 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11341 /* Complete lookup of the id-expression. */
11342 expr = (finish_id_expression
11343 (id_expression, expr, parser->scope, &idk,
11344 /*integral_constant_expression_p=*/false,
11345 /*allow_non_integral_constant_expression_p=*/true,
11346 &non_integral_constant_expression_p,
11347 /*template_p=*/false,
11348 /*done=*/true,
11349 /*address_p=*/false,
11350 /*template_arg_p=*/false,
11351 &error_msg,
11352 id_expr_start_token->location));
11354 if (expr == error_mark_node)
11355 /* We found an id-expression, but it was something that we
11356 should not have found. This is an error, not something
11357 we can recover from, so note that we found an
11358 id-expression and we'll recover as gracefully as
11359 possible. */
11360 id_expression_or_member_access_p = true;
11363 if (expr
11364 && expr != error_mark_node
11365 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11366 /* We have an id-expression. */
11367 id_expression_or_member_access_p = true;
11370 if (!id_expression_or_member_access_p)
11372 /* Abort the id-expression parse. */
11373 cp_parser_abort_tentative_parse (parser);
11375 /* Parsing tentatively, again. */
11376 cp_parser_parse_tentatively (parser);
11378 /* Parse a class member access. */
11379 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11380 /*cast_p=*/false, /*decltype*/true,
11381 /*member_access_only_p=*/true, NULL);
11383 if (expr
11384 && expr != error_mark_node
11385 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11386 /* We have an id-expression. */
11387 id_expression_or_member_access_p = true;
11390 if (id_expression_or_member_access_p)
11391 /* We have parsed the complete id-expression or member access. */
11392 cp_parser_parse_definitely (parser);
11393 else
11395 /* Abort our attempt to parse an id-expression or member access
11396 expression. */
11397 cp_parser_abort_tentative_parse (parser);
11399 /* Parse a full expression. */
11400 expr = cp_parser_expression (parser, /*cast_p=*/false,
11401 /*decltype*/true, NULL);
11404 return expr;
11407 /* Parse a `decltype' type. Returns the type.
11409 simple-type-specifier:
11410 decltype ( expression )
11411 C++14 proposal:
11412 decltype ( auto ) */
11414 static tree
11415 cp_parser_decltype (cp_parser *parser)
11417 tree expr;
11418 bool id_expression_or_member_access_p = false;
11419 const char *saved_message;
11420 bool saved_integral_constant_expression_p;
11421 bool saved_non_integral_constant_expression_p;
11422 bool saved_greater_than_is_operator_p;
11423 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11425 if (start_token->type == CPP_DECLTYPE)
11427 /* Already parsed. */
11428 cp_lexer_consume_token (parser->lexer);
11429 return start_token->u.value;
11432 /* Look for the `decltype' token. */
11433 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11434 return error_mark_node;
11436 /* Parse the opening `('. */
11437 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11438 return error_mark_node;
11440 /* decltype (auto) */
11441 if (cxx_dialect >= cxx1y
11442 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11444 cp_lexer_consume_token (parser->lexer);
11445 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11446 return error_mark_node;
11447 expr = make_auto ();
11448 AUTO_IS_DECLTYPE (expr) = true;
11449 goto rewrite;
11452 /* Types cannot be defined in a `decltype' expression. Save away the
11453 old message. */
11454 saved_message = parser->type_definition_forbidden_message;
11456 /* And create the new one. */
11457 parser->type_definition_forbidden_message
11458 = G_("types may not be defined in %<decltype%> expressions");
11460 /* The restrictions on constant-expressions do not apply inside
11461 decltype expressions. */
11462 saved_integral_constant_expression_p
11463 = parser->integral_constant_expression_p;
11464 saved_non_integral_constant_expression_p
11465 = parser->non_integral_constant_expression_p;
11466 parser->integral_constant_expression_p = false;
11468 /* Within a parenthesized expression, a `>' token is always
11469 the greater-than operator. */
11470 saved_greater_than_is_operator_p
11471 = parser->greater_than_is_operator_p;
11472 parser->greater_than_is_operator_p = true;
11474 /* Do not actually evaluate the expression. */
11475 ++cp_unevaluated_operand;
11477 /* Do not warn about problems with the expression. */
11478 ++c_inhibit_evaluation_warnings;
11480 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
11482 /* Go back to evaluating expressions. */
11483 --cp_unevaluated_operand;
11484 --c_inhibit_evaluation_warnings;
11486 /* The `>' token might be the end of a template-id or
11487 template-parameter-list now. */
11488 parser->greater_than_is_operator_p
11489 = saved_greater_than_is_operator_p;
11491 /* Restore the old message and the integral constant expression
11492 flags. */
11493 parser->type_definition_forbidden_message = saved_message;
11494 parser->integral_constant_expression_p
11495 = saved_integral_constant_expression_p;
11496 parser->non_integral_constant_expression_p
11497 = saved_non_integral_constant_expression_p;
11499 /* Parse to the closing `)'. */
11500 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11502 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11503 /*consume_paren=*/true);
11504 return error_mark_node;
11507 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11508 tf_warning_or_error);
11510 rewrite:
11511 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11512 it again. */
11513 start_token->type = CPP_DECLTYPE;
11514 start_token->u.value = expr;
11515 start_token->keyword = RID_MAX;
11516 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11518 return expr;
11521 /* Special member functions [gram.special] */
11523 /* Parse a conversion-function-id.
11525 conversion-function-id:
11526 operator conversion-type-id
11528 Returns an IDENTIFIER_NODE representing the operator. */
11530 static tree
11531 cp_parser_conversion_function_id (cp_parser* parser)
11533 tree type;
11534 tree saved_scope;
11535 tree saved_qualifying_scope;
11536 tree saved_object_scope;
11537 tree pushed_scope = NULL_TREE;
11539 /* Look for the `operator' token. */
11540 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11541 return error_mark_node;
11542 /* When we parse the conversion-type-id, the current scope will be
11543 reset. However, we need that information in able to look up the
11544 conversion function later, so we save it here. */
11545 saved_scope = parser->scope;
11546 saved_qualifying_scope = parser->qualifying_scope;
11547 saved_object_scope = parser->object_scope;
11548 /* We must enter the scope of the class so that the names of
11549 entities declared within the class are available in the
11550 conversion-type-id. For example, consider:
11552 struct S {
11553 typedef int I;
11554 operator I();
11557 S::operator I() { ... }
11559 In order to see that `I' is a type-name in the definition, we
11560 must be in the scope of `S'. */
11561 if (saved_scope)
11562 pushed_scope = push_scope (saved_scope);
11563 /* Parse the conversion-type-id. */
11564 type = cp_parser_conversion_type_id (parser);
11565 /* Leave the scope of the class, if any. */
11566 if (pushed_scope)
11567 pop_scope (pushed_scope);
11568 /* Restore the saved scope. */
11569 parser->scope = saved_scope;
11570 parser->qualifying_scope = saved_qualifying_scope;
11571 parser->object_scope = saved_object_scope;
11572 /* If the TYPE is invalid, indicate failure. */
11573 if (type == error_mark_node)
11574 return error_mark_node;
11575 return mangle_conv_op_name_for_type (type);
11578 /* Parse a conversion-type-id:
11580 conversion-type-id:
11581 type-specifier-seq conversion-declarator [opt]
11583 Returns the TYPE specified. */
11585 static tree
11586 cp_parser_conversion_type_id (cp_parser* parser)
11588 tree attributes;
11589 cp_decl_specifier_seq type_specifiers;
11590 cp_declarator *declarator;
11591 tree type_specified;
11593 /* Parse the attributes. */
11594 attributes = cp_parser_attributes_opt (parser);
11595 /* Parse the type-specifiers. */
11596 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11597 /*is_trailing_return=*/false,
11598 &type_specifiers);
11599 /* If that didn't work, stop. */
11600 if (type_specifiers.type == error_mark_node)
11601 return error_mark_node;
11602 /* Parse the conversion-declarator. */
11603 declarator = cp_parser_conversion_declarator_opt (parser);
11605 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11606 /*initialized=*/0, &attributes);
11607 if (attributes)
11608 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11610 /* Don't give this error when parsing tentatively. This happens to
11611 work because we always parse this definitively once. */
11612 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11613 && type_uses_auto (type_specified))
11615 if (cxx_dialect < cxx1y)
11617 error ("invalid use of %<auto%> in conversion operator");
11618 return error_mark_node;
11620 else if (template_parm_scope_p ())
11621 warning (0, "use of %<auto%> in member template "
11622 "conversion operator can never be deduced");
11625 return type_specified;
11628 /* Parse an (optional) conversion-declarator.
11630 conversion-declarator:
11631 ptr-operator conversion-declarator [opt]
11635 static cp_declarator *
11636 cp_parser_conversion_declarator_opt (cp_parser* parser)
11638 enum tree_code code;
11639 tree class_type, std_attributes = NULL_TREE;
11640 cp_cv_quals cv_quals;
11642 /* We don't know if there's a ptr-operator next, or not. */
11643 cp_parser_parse_tentatively (parser);
11644 /* Try the ptr-operator. */
11645 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11646 &std_attributes);
11647 /* If it worked, look for more conversion-declarators. */
11648 if (cp_parser_parse_definitely (parser))
11650 cp_declarator *declarator;
11652 /* Parse another optional declarator. */
11653 declarator = cp_parser_conversion_declarator_opt (parser);
11655 declarator = cp_parser_make_indirect_declarator
11656 (code, class_type, cv_quals, declarator, std_attributes);
11658 return declarator;
11661 return NULL;
11664 /* Parse an (optional) ctor-initializer.
11666 ctor-initializer:
11667 : mem-initializer-list
11669 Returns TRUE iff the ctor-initializer was actually present. */
11671 static bool
11672 cp_parser_ctor_initializer_opt (cp_parser* parser)
11674 /* If the next token is not a `:', then there is no
11675 ctor-initializer. */
11676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11678 /* Do default initialization of any bases and members. */
11679 if (DECL_CONSTRUCTOR_P (current_function_decl))
11680 finish_mem_initializers (NULL_TREE);
11682 return false;
11685 /* Consume the `:' token. */
11686 cp_lexer_consume_token (parser->lexer);
11687 /* And the mem-initializer-list. */
11688 cp_parser_mem_initializer_list (parser);
11690 return true;
11693 /* Parse a mem-initializer-list.
11695 mem-initializer-list:
11696 mem-initializer ... [opt]
11697 mem-initializer ... [opt] , mem-initializer-list */
11699 static void
11700 cp_parser_mem_initializer_list (cp_parser* parser)
11702 tree mem_initializer_list = NULL_TREE;
11703 tree target_ctor = error_mark_node;
11704 cp_token *token = cp_lexer_peek_token (parser->lexer);
11706 /* Let the semantic analysis code know that we are starting the
11707 mem-initializer-list. */
11708 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11709 error_at (token->location,
11710 "only constructors take member initializers");
11712 /* Loop through the list. */
11713 while (true)
11715 tree mem_initializer;
11717 token = cp_lexer_peek_token (parser->lexer);
11718 /* Parse the mem-initializer. */
11719 mem_initializer = cp_parser_mem_initializer (parser);
11720 /* If the next token is a `...', we're expanding member initializers. */
11721 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11723 /* Consume the `...'. */
11724 cp_lexer_consume_token (parser->lexer);
11726 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11727 can be expanded but members cannot. */
11728 if (mem_initializer != error_mark_node
11729 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11731 error_at (token->location,
11732 "cannot expand initializer for member %<%D%>",
11733 TREE_PURPOSE (mem_initializer));
11734 mem_initializer = error_mark_node;
11737 /* Construct the pack expansion type. */
11738 if (mem_initializer != error_mark_node)
11739 mem_initializer = make_pack_expansion (mem_initializer);
11741 if (target_ctor != error_mark_node
11742 && mem_initializer != error_mark_node)
11744 error ("mem-initializer for %qD follows constructor delegation",
11745 TREE_PURPOSE (mem_initializer));
11746 mem_initializer = error_mark_node;
11748 /* Look for a target constructor. */
11749 if (mem_initializer != error_mark_node
11750 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
11751 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11753 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11754 if (mem_initializer_list)
11756 error ("constructor delegation follows mem-initializer for %qD",
11757 TREE_PURPOSE (mem_initializer_list));
11758 mem_initializer = error_mark_node;
11760 target_ctor = mem_initializer;
11762 /* Add it to the list, unless it was erroneous. */
11763 if (mem_initializer != error_mark_node)
11765 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11766 mem_initializer_list = mem_initializer;
11768 /* If the next token is not a `,', we're done. */
11769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11770 break;
11771 /* Consume the `,' token. */
11772 cp_lexer_consume_token (parser->lexer);
11775 /* Perform semantic analysis. */
11776 if (DECL_CONSTRUCTOR_P (current_function_decl))
11777 finish_mem_initializers (mem_initializer_list);
11780 /* Parse a mem-initializer.
11782 mem-initializer:
11783 mem-initializer-id ( expression-list [opt] )
11784 mem-initializer-id braced-init-list
11786 GNU extension:
11788 mem-initializer:
11789 ( expression-list [opt] )
11791 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11792 class) or FIELD_DECL (for a non-static data member) to initialize;
11793 the TREE_VALUE is the expression-list. An empty initialization
11794 list is represented by void_list_node. */
11796 static tree
11797 cp_parser_mem_initializer (cp_parser* parser)
11799 tree mem_initializer_id;
11800 tree expression_list;
11801 tree member;
11802 cp_token *token = cp_lexer_peek_token (parser->lexer);
11804 /* Find out what is being initialized. */
11805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11807 permerror (token->location,
11808 "anachronistic old-style base class initializer");
11809 mem_initializer_id = NULL_TREE;
11811 else
11813 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11814 if (mem_initializer_id == error_mark_node)
11815 return mem_initializer_id;
11817 member = expand_member_init (mem_initializer_id);
11818 if (member && !DECL_P (member))
11819 in_base_initializer = 1;
11821 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11823 bool expr_non_constant_p;
11824 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11825 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11826 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11827 expression_list = build_tree_list (NULL_TREE, expression_list);
11829 else
11831 vec<tree, va_gc> *vec;
11832 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11833 /*cast_p=*/false,
11834 /*allow_expansion_p=*/true,
11835 /*non_constant_p=*/NULL);
11836 if (vec == NULL)
11837 return error_mark_node;
11838 expression_list = build_tree_list_vec (vec);
11839 release_tree_vector (vec);
11842 if (expression_list == error_mark_node)
11843 return error_mark_node;
11844 if (!expression_list)
11845 expression_list = void_type_node;
11847 in_base_initializer = 0;
11849 return member ? build_tree_list (member, expression_list) : error_mark_node;
11852 /* Parse a mem-initializer-id.
11854 mem-initializer-id:
11855 :: [opt] nested-name-specifier [opt] class-name
11856 identifier
11858 Returns a TYPE indicating the class to be initializer for the first
11859 production. Returns an IDENTIFIER_NODE indicating the data member
11860 to be initialized for the second production. */
11862 static tree
11863 cp_parser_mem_initializer_id (cp_parser* parser)
11865 bool global_scope_p;
11866 bool nested_name_specifier_p;
11867 bool template_p = false;
11868 tree id;
11870 cp_token *token = cp_lexer_peek_token (parser->lexer);
11872 /* `typename' is not allowed in this context ([temp.res]). */
11873 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11875 error_at (token->location,
11876 "keyword %<typename%> not allowed in this context (a qualified "
11877 "member initializer is implicitly a type)");
11878 cp_lexer_consume_token (parser->lexer);
11880 /* Look for the optional `::' operator. */
11881 global_scope_p
11882 = (cp_parser_global_scope_opt (parser,
11883 /*current_scope_valid_p=*/false)
11884 != NULL_TREE);
11885 /* Look for the optional nested-name-specifier. The simplest way to
11886 implement:
11888 [temp.res]
11890 The keyword `typename' is not permitted in a base-specifier or
11891 mem-initializer; in these contexts a qualified name that
11892 depends on a template-parameter is implicitly assumed to be a
11893 type name.
11895 is to assume that we have seen the `typename' keyword at this
11896 point. */
11897 nested_name_specifier_p
11898 = (cp_parser_nested_name_specifier_opt (parser,
11899 /*typename_keyword_p=*/true,
11900 /*check_dependency_p=*/true,
11901 /*type_p=*/true,
11902 /*is_declaration=*/true)
11903 != NULL_TREE);
11904 if (nested_name_specifier_p)
11905 template_p = cp_parser_optional_template_keyword (parser);
11906 /* If there is a `::' operator or a nested-name-specifier, then we
11907 are definitely looking for a class-name. */
11908 if (global_scope_p || nested_name_specifier_p)
11909 return cp_parser_class_name (parser,
11910 /*typename_keyword_p=*/true,
11911 /*template_keyword_p=*/template_p,
11912 typename_type,
11913 /*check_dependency_p=*/true,
11914 /*class_head_p=*/false,
11915 /*is_declaration=*/true);
11916 /* Otherwise, we could also be looking for an ordinary identifier. */
11917 cp_parser_parse_tentatively (parser);
11918 /* Try a class-name. */
11919 id = cp_parser_class_name (parser,
11920 /*typename_keyword_p=*/true,
11921 /*template_keyword_p=*/false,
11922 none_type,
11923 /*check_dependency_p=*/true,
11924 /*class_head_p=*/false,
11925 /*is_declaration=*/true);
11926 /* If we found one, we're done. */
11927 if (cp_parser_parse_definitely (parser))
11928 return id;
11929 /* Otherwise, look for an ordinary identifier. */
11930 return cp_parser_identifier (parser);
11933 /* Overloading [gram.over] */
11935 /* Parse an operator-function-id.
11937 operator-function-id:
11938 operator operator
11940 Returns an IDENTIFIER_NODE for the operator which is a
11941 human-readable spelling of the identifier, e.g., `operator +'. */
11943 static tree
11944 cp_parser_operator_function_id (cp_parser* parser)
11946 /* Look for the `operator' keyword. */
11947 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11948 return error_mark_node;
11949 /* And then the name of the operator itself. */
11950 return cp_parser_operator (parser);
11953 /* Return an identifier node for a user-defined literal operator.
11954 The suffix identifier is chained to the operator name identifier. */
11956 static tree
11957 cp_literal_operator_id (const char* name)
11959 tree identifier;
11960 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11961 + strlen (name) + 10);
11962 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11963 identifier = get_identifier (buffer);
11964 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11966 return identifier;
11969 /* Parse an operator.
11971 operator:
11972 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11973 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11974 || ++ -- , ->* -> () []
11976 GNU Extensions:
11978 operator:
11979 <? >? <?= >?=
11981 Returns an IDENTIFIER_NODE for the operator which is a
11982 human-readable spelling of the identifier, e.g., `operator +'. */
11984 static tree
11985 cp_parser_operator (cp_parser* parser)
11987 tree id = NULL_TREE;
11988 cp_token *token;
11990 /* Peek at the next token. */
11991 token = cp_lexer_peek_token (parser->lexer);
11992 /* Figure out which operator we have. */
11993 switch (token->type)
11995 case CPP_KEYWORD:
11997 enum tree_code op;
11999 /* The keyword should be either `new' or `delete'. */
12000 if (token->keyword == RID_NEW)
12001 op = NEW_EXPR;
12002 else if (token->keyword == RID_DELETE)
12003 op = DELETE_EXPR;
12004 else
12005 break;
12007 /* Consume the `new' or `delete' token. */
12008 cp_lexer_consume_token (parser->lexer);
12010 /* Peek at the next token. */
12011 token = cp_lexer_peek_token (parser->lexer);
12012 /* If it's a `[' token then this is the array variant of the
12013 operator. */
12014 if (token->type == CPP_OPEN_SQUARE)
12016 /* Consume the `[' token. */
12017 cp_lexer_consume_token (parser->lexer);
12018 /* Look for the `]' token. */
12019 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12020 id = ansi_opname (op == NEW_EXPR
12021 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12023 /* Otherwise, we have the non-array variant. */
12024 else
12025 id = ansi_opname (op);
12027 return id;
12030 case CPP_PLUS:
12031 id = ansi_opname (PLUS_EXPR);
12032 break;
12034 case CPP_MINUS:
12035 id = ansi_opname (MINUS_EXPR);
12036 break;
12038 case CPP_MULT:
12039 id = ansi_opname (MULT_EXPR);
12040 break;
12042 case CPP_DIV:
12043 id = ansi_opname (TRUNC_DIV_EXPR);
12044 break;
12046 case CPP_MOD:
12047 id = ansi_opname (TRUNC_MOD_EXPR);
12048 break;
12050 case CPP_XOR:
12051 id = ansi_opname (BIT_XOR_EXPR);
12052 break;
12054 case CPP_AND:
12055 id = ansi_opname (BIT_AND_EXPR);
12056 break;
12058 case CPP_OR:
12059 id = ansi_opname (BIT_IOR_EXPR);
12060 break;
12062 case CPP_COMPL:
12063 id = ansi_opname (BIT_NOT_EXPR);
12064 break;
12066 case CPP_NOT:
12067 id = ansi_opname (TRUTH_NOT_EXPR);
12068 break;
12070 case CPP_EQ:
12071 id = ansi_assopname (NOP_EXPR);
12072 break;
12074 case CPP_LESS:
12075 id = ansi_opname (LT_EXPR);
12076 break;
12078 case CPP_GREATER:
12079 id = ansi_opname (GT_EXPR);
12080 break;
12082 case CPP_PLUS_EQ:
12083 id = ansi_assopname (PLUS_EXPR);
12084 break;
12086 case CPP_MINUS_EQ:
12087 id = ansi_assopname (MINUS_EXPR);
12088 break;
12090 case CPP_MULT_EQ:
12091 id = ansi_assopname (MULT_EXPR);
12092 break;
12094 case CPP_DIV_EQ:
12095 id = ansi_assopname (TRUNC_DIV_EXPR);
12096 break;
12098 case CPP_MOD_EQ:
12099 id = ansi_assopname (TRUNC_MOD_EXPR);
12100 break;
12102 case CPP_XOR_EQ:
12103 id = ansi_assopname (BIT_XOR_EXPR);
12104 break;
12106 case CPP_AND_EQ:
12107 id = ansi_assopname (BIT_AND_EXPR);
12108 break;
12110 case CPP_OR_EQ:
12111 id = ansi_assopname (BIT_IOR_EXPR);
12112 break;
12114 case CPP_LSHIFT:
12115 id = ansi_opname (LSHIFT_EXPR);
12116 break;
12118 case CPP_RSHIFT:
12119 id = ansi_opname (RSHIFT_EXPR);
12120 break;
12122 case CPP_LSHIFT_EQ:
12123 id = ansi_assopname (LSHIFT_EXPR);
12124 break;
12126 case CPP_RSHIFT_EQ:
12127 id = ansi_assopname (RSHIFT_EXPR);
12128 break;
12130 case CPP_EQ_EQ:
12131 id = ansi_opname (EQ_EXPR);
12132 break;
12134 case CPP_NOT_EQ:
12135 id = ansi_opname (NE_EXPR);
12136 break;
12138 case CPP_LESS_EQ:
12139 id = ansi_opname (LE_EXPR);
12140 break;
12142 case CPP_GREATER_EQ:
12143 id = ansi_opname (GE_EXPR);
12144 break;
12146 case CPP_AND_AND:
12147 id = ansi_opname (TRUTH_ANDIF_EXPR);
12148 break;
12150 case CPP_OR_OR:
12151 id = ansi_opname (TRUTH_ORIF_EXPR);
12152 break;
12154 case CPP_PLUS_PLUS:
12155 id = ansi_opname (POSTINCREMENT_EXPR);
12156 break;
12158 case CPP_MINUS_MINUS:
12159 id = ansi_opname (PREDECREMENT_EXPR);
12160 break;
12162 case CPP_COMMA:
12163 id = ansi_opname (COMPOUND_EXPR);
12164 break;
12166 case CPP_DEREF_STAR:
12167 id = ansi_opname (MEMBER_REF);
12168 break;
12170 case CPP_DEREF:
12171 id = ansi_opname (COMPONENT_REF);
12172 break;
12174 case CPP_OPEN_PAREN:
12175 /* Consume the `('. */
12176 cp_lexer_consume_token (parser->lexer);
12177 /* Look for the matching `)'. */
12178 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12179 return ansi_opname (CALL_EXPR);
12181 case CPP_OPEN_SQUARE:
12182 /* Consume the `['. */
12183 cp_lexer_consume_token (parser->lexer);
12184 /* Look for the matching `]'. */
12185 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12186 return ansi_opname (ARRAY_REF);
12188 case CPP_STRING:
12189 if (cxx_dialect == cxx98)
12190 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12191 if (TREE_STRING_LENGTH (token->u.value) > 2)
12193 error ("expected empty string after %<operator%> keyword");
12194 return error_mark_node;
12196 /* Consume the string. */
12197 cp_lexer_consume_token (parser->lexer);
12198 /* Look for the suffix identifier. */
12199 token = cp_lexer_peek_token (parser->lexer);
12200 if (token->type == CPP_NAME)
12202 id = cp_parser_identifier (parser);
12203 if (id != error_mark_node)
12205 const char *name = IDENTIFIER_POINTER (id);
12206 return cp_literal_operator_id (name);
12209 else
12211 error ("expected suffix identifier");
12212 return error_mark_node;
12215 case CPP_STRING_USERDEF:
12216 error ("missing space between %<\"\"%> and suffix identifier");
12217 return error_mark_node;
12219 default:
12220 /* Anything else is an error. */
12221 break;
12224 /* If we have selected an identifier, we need to consume the
12225 operator token. */
12226 if (id)
12227 cp_lexer_consume_token (parser->lexer);
12228 /* Otherwise, no valid operator name was present. */
12229 else
12231 cp_parser_error (parser, "expected operator");
12232 id = error_mark_node;
12235 return id;
12238 /* Parse a template-declaration.
12240 template-declaration:
12241 export [opt] template < template-parameter-list > declaration
12243 If MEMBER_P is TRUE, this template-declaration occurs within a
12244 class-specifier.
12246 The grammar rule given by the standard isn't correct. What
12247 is really meant is:
12249 template-declaration:
12250 export [opt] template-parameter-list-seq
12251 decl-specifier-seq [opt] init-declarator [opt] ;
12252 export [opt] template-parameter-list-seq
12253 function-definition
12255 template-parameter-list-seq:
12256 template-parameter-list-seq [opt]
12257 template < template-parameter-list > */
12259 static void
12260 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12262 /* Check for `export'. */
12263 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12265 /* Consume the `export' token. */
12266 cp_lexer_consume_token (parser->lexer);
12267 /* Warn that we do not support `export'. */
12268 warning (0, "keyword %<export%> not implemented, and will be ignored");
12271 cp_parser_template_declaration_after_export (parser, member_p);
12274 /* Parse a template-parameter-list.
12276 template-parameter-list:
12277 template-parameter
12278 template-parameter-list , template-parameter
12280 Returns a TREE_LIST. Each node represents a template parameter.
12281 The nodes are connected via their TREE_CHAINs. */
12283 static tree
12284 cp_parser_template_parameter_list (cp_parser* parser)
12286 tree parameter_list = NULL_TREE;
12288 begin_template_parm_list ();
12290 /* The loop below parses the template parms. We first need to know
12291 the total number of template parms to be able to compute proper
12292 canonical types of each dependent type. So after the loop, when
12293 we know the total number of template parms,
12294 end_template_parm_list computes the proper canonical types and
12295 fixes up the dependent types accordingly. */
12296 while (true)
12298 tree parameter;
12299 bool is_non_type;
12300 bool is_parameter_pack;
12301 location_t parm_loc;
12303 /* Parse the template-parameter. */
12304 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12305 parameter = cp_parser_template_parameter (parser,
12306 &is_non_type,
12307 &is_parameter_pack);
12308 /* Add it to the list. */
12309 if (parameter != error_mark_node)
12310 parameter_list = process_template_parm (parameter_list,
12311 parm_loc,
12312 parameter,
12313 is_non_type,
12314 is_parameter_pack);
12315 else
12317 tree err_parm = build_tree_list (parameter, parameter);
12318 parameter_list = chainon (parameter_list, err_parm);
12321 /* If the next token is not a `,', we're done. */
12322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12323 break;
12324 /* Otherwise, consume the `,' token. */
12325 cp_lexer_consume_token (parser->lexer);
12328 return end_template_parm_list (parameter_list);
12331 /* Parse a template-parameter.
12333 template-parameter:
12334 type-parameter
12335 parameter-declaration
12337 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12338 the parameter. The TREE_PURPOSE is the default value, if any.
12339 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12340 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12341 set to true iff this parameter is a parameter pack. */
12343 static tree
12344 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12345 bool *is_parameter_pack)
12347 cp_token *token;
12348 cp_parameter_declarator *parameter_declarator;
12349 cp_declarator *id_declarator;
12350 tree parm;
12352 /* Assume it is a type parameter or a template parameter. */
12353 *is_non_type = false;
12354 /* Assume it not a parameter pack. */
12355 *is_parameter_pack = false;
12356 /* Peek at the next token. */
12357 token = cp_lexer_peek_token (parser->lexer);
12358 /* If it is `class' or `template', we have a type-parameter. */
12359 if (token->keyword == RID_TEMPLATE)
12360 return cp_parser_type_parameter (parser, is_parameter_pack);
12361 /* If it is `class' or `typename' we do not know yet whether it is a
12362 type parameter or a non-type parameter. Consider:
12364 template <typename T, typename T::X X> ...
12368 template <class C, class D*> ...
12370 Here, the first parameter is a type parameter, and the second is
12371 a non-type parameter. We can tell by looking at the token after
12372 the identifier -- if it is a `,', `=', or `>' then we have a type
12373 parameter. */
12374 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12376 /* Peek at the token after `class' or `typename'. */
12377 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12378 /* If it's an ellipsis, we have a template type parameter
12379 pack. */
12380 if (token->type == CPP_ELLIPSIS)
12381 return cp_parser_type_parameter (parser, is_parameter_pack);
12382 /* If it's an identifier, skip it. */
12383 if (token->type == CPP_NAME)
12384 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12385 /* Now, see if the token looks like the end of a template
12386 parameter. */
12387 if (token->type == CPP_COMMA
12388 || token->type == CPP_EQ
12389 || token->type == CPP_GREATER)
12390 return cp_parser_type_parameter (parser, is_parameter_pack);
12393 /* Otherwise, it is a non-type parameter.
12395 [temp.param]
12397 When parsing a default template-argument for a non-type
12398 template-parameter, the first non-nested `>' is taken as the end
12399 of the template parameter-list rather than a greater-than
12400 operator. */
12401 *is_non_type = true;
12402 parameter_declarator
12403 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12404 /*parenthesized_p=*/NULL);
12406 /* If the parameter declaration is marked as a parameter pack, set
12407 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12408 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12409 grokdeclarator. */
12410 if (parameter_declarator
12411 && parameter_declarator->declarator
12412 && parameter_declarator->declarator->parameter_pack_p)
12414 *is_parameter_pack = true;
12415 parameter_declarator->declarator->parameter_pack_p = false;
12418 if (parameter_declarator
12419 && parameter_declarator->default_argument)
12421 /* Can happen in some cases of erroneous input (c++/34892). */
12422 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12423 /* Consume the `...' for better error recovery. */
12424 cp_lexer_consume_token (parser->lexer);
12426 /* If the next token is an ellipsis, and we don't already have it
12427 marked as a parameter pack, then we have a parameter pack (that
12428 has no declarator). */
12429 else if (!*is_parameter_pack
12430 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12431 && (declarator_can_be_parameter_pack
12432 (parameter_declarator->declarator)))
12434 /* Consume the `...'. */
12435 cp_lexer_consume_token (parser->lexer);
12436 maybe_warn_variadic_templates ();
12438 *is_parameter_pack = true;
12440 /* We might end up with a pack expansion as the type of the non-type
12441 template parameter, in which case this is a non-type template
12442 parameter pack. */
12443 else if (parameter_declarator
12444 && parameter_declarator->decl_specifiers.type
12445 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12447 *is_parameter_pack = true;
12448 parameter_declarator->decl_specifiers.type =
12449 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12452 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12454 /* Parameter packs cannot have default arguments. However, a
12455 user may try to do so, so we'll parse them and give an
12456 appropriate diagnostic here. */
12458 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12460 /* Find the name of the parameter pack. */
12461 id_declarator = parameter_declarator->declarator;
12462 while (id_declarator && id_declarator->kind != cdk_id)
12463 id_declarator = id_declarator->declarator;
12465 if (id_declarator && id_declarator->kind == cdk_id)
12466 error_at (start_token->location,
12467 "template parameter pack %qD cannot have a default argument",
12468 id_declarator->u.id.unqualified_name);
12469 else
12470 error_at (start_token->location,
12471 "template parameter pack cannot have a default argument");
12473 /* Parse the default argument, but throw away the result. */
12474 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12477 parm = grokdeclarator (parameter_declarator->declarator,
12478 &parameter_declarator->decl_specifiers,
12479 TPARM, /*initialized=*/0,
12480 /*attrlist=*/NULL);
12481 if (parm == error_mark_node)
12482 return error_mark_node;
12484 return build_tree_list (parameter_declarator->default_argument, parm);
12487 /* Parse a type-parameter.
12489 type-parameter:
12490 class identifier [opt]
12491 class identifier [opt] = type-id
12492 typename identifier [opt]
12493 typename identifier [opt] = type-id
12494 template < template-parameter-list > class identifier [opt]
12495 template < template-parameter-list > class identifier [opt]
12496 = id-expression
12498 GNU Extension (variadic templates):
12500 type-parameter:
12501 class ... identifier [opt]
12502 typename ... identifier [opt]
12504 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12505 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12506 the declaration of the parameter.
12508 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12510 static tree
12511 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12513 cp_token *token;
12514 tree parameter;
12516 /* Look for a keyword to tell us what kind of parameter this is. */
12517 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12518 if (!token)
12519 return error_mark_node;
12521 switch (token->keyword)
12523 case RID_CLASS:
12524 case RID_TYPENAME:
12526 tree identifier;
12527 tree default_argument;
12529 /* If the next token is an ellipsis, we have a template
12530 argument pack. */
12531 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12533 /* Consume the `...' token. */
12534 cp_lexer_consume_token (parser->lexer);
12535 maybe_warn_variadic_templates ();
12537 *is_parameter_pack = true;
12540 /* If the next token is an identifier, then it names the
12541 parameter. */
12542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12543 identifier = cp_parser_identifier (parser);
12544 else
12545 identifier = NULL_TREE;
12547 /* Create the parameter. */
12548 parameter = finish_template_type_parm (class_type_node, identifier);
12550 /* If the next token is an `=', we have a default argument. */
12551 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12553 /* Consume the `=' token. */
12554 cp_lexer_consume_token (parser->lexer);
12555 /* Parse the default-argument. */
12556 push_deferring_access_checks (dk_no_deferred);
12557 default_argument = cp_parser_type_id (parser);
12559 /* Template parameter packs cannot have default
12560 arguments. */
12561 if (*is_parameter_pack)
12563 if (identifier)
12564 error_at (token->location,
12565 "template parameter pack %qD cannot have a "
12566 "default argument", identifier);
12567 else
12568 error_at (token->location,
12569 "template parameter packs cannot have "
12570 "default arguments");
12571 default_argument = NULL_TREE;
12573 pop_deferring_access_checks ();
12575 else
12576 default_argument = NULL_TREE;
12578 /* Create the combined representation of the parameter and the
12579 default argument. */
12580 parameter = build_tree_list (default_argument, parameter);
12582 break;
12584 case RID_TEMPLATE:
12586 tree identifier;
12587 tree default_argument;
12589 /* Look for the `<'. */
12590 cp_parser_require (parser, CPP_LESS, RT_LESS);
12591 /* Parse the template-parameter-list. */
12592 cp_parser_template_parameter_list (parser);
12593 /* Look for the `>'. */
12594 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12595 /* Look for the `class' keyword. */
12596 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12597 /* If the next token is an ellipsis, we have a template
12598 argument pack. */
12599 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12601 /* Consume the `...' token. */
12602 cp_lexer_consume_token (parser->lexer);
12603 maybe_warn_variadic_templates ();
12605 *is_parameter_pack = true;
12607 /* If the next token is an `=', then there is a
12608 default-argument. If the next token is a `>', we are at
12609 the end of the parameter-list. If the next token is a `,',
12610 then we are at the end of this parameter. */
12611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12612 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12613 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12615 identifier = cp_parser_identifier (parser);
12616 /* Treat invalid names as if the parameter were nameless. */
12617 if (identifier == error_mark_node)
12618 identifier = NULL_TREE;
12620 else
12621 identifier = NULL_TREE;
12623 /* Create the template parameter. */
12624 parameter = finish_template_template_parm (class_type_node,
12625 identifier);
12627 /* If the next token is an `=', then there is a
12628 default-argument. */
12629 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12631 bool is_template;
12633 /* Consume the `='. */
12634 cp_lexer_consume_token (parser->lexer);
12635 /* Parse the id-expression. */
12636 push_deferring_access_checks (dk_no_deferred);
12637 /* save token before parsing the id-expression, for error
12638 reporting */
12639 token = cp_lexer_peek_token (parser->lexer);
12640 default_argument
12641 = cp_parser_id_expression (parser,
12642 /*template_keyword_p=*/false,
12643 /*check_dependency_p=*/true,
12644 /*template_p=*/&is_template,
12645 /*declarator_p=*/false,
12646 /*optional_p=*/false);
12647 if (TREE_CODE (default_argument) == TYPE_DECL)
12648 /* If the id-expression was a template-id that refers to
12649 a template-class, we already have the declaration here,
12650 so no further lookup is needed. */
12652 else
12653 /* Look up the name. */
12654 default_argument
12655 = cp_parser_lookup_name (parser, default_argument,
12656 none_type,
12657 /*is_template=*/is_template,
12658 /*is_namespace=*/false,
12659 /*check_dependency=*/true,
12660 /*ambiguous_decls=*/NULL,
12661 token->location);
12662 /* See if the default argument is valid. */
12663 default_argument
12664 = check_template_template_default_arg (default_argument);
12666 /* Template parameter packs cannot have default
12667 arguments. */
12668 if (*is_parameter_pack)
12670 if (identifier)
12671 error_at (token->location,
12672 "template parameter pack %qD cannot "
12673 "have a default argument",
12674 identifier);
12675 else
12676 error_at (token->location, "template parameter packs cannot "
12677 "have default arguments");
12678 default_argument = NULL_TREE;
12680 pop_deferring_access_checks ();
12682 else
12683 default_argument = NULL_TREE;
12685 /* Create the combined representation of the parameter and the
12686 default argument. */
12687 parameter = build_tree_list (default_argument, parameter);
12689 break;
12691 default:
12692 gcc_unreachable ();
12693 break;
12696 return parameter;
12699 /* Parse a template-id.
12701 template-id:
12702 template-name < template-argument-list [opt] >
12704 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12705 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12706 returned. Otherwise, if the template-name names a function, or set
12707 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12708 names a class, returns a TYPE_DECL for the specialization.
12710 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12711 uninstantiated templates. */
12713 static tree
12714 cp_parser_template_id (cp_parser *parser,
12715 bool template_keyword_p,
12716 bool check_dependency_p,
12717 enum tag_types tag_type,
12718 bool is_declaration)
12720 int i;
12721 tree templ;
12722 tree arguments;
12723 tree template_id;
12724 cp_token_position start_of_id = 0;
12725 deferred_access_check *chk;
12726 vec<deferred_access_check, va_gc> *access_check;
12727 cp_token *next_token = NULL, *next_token_2 = NULL;
12728 bool is_identifier;
12730 /* If the next token corresponds to a template-id, there is no need
12731 to reparse it. */
12732 next_token = cp_lexer_peek_token (parser->lexer);
12733 if (next_token->type == CPP_TEMPLATE_ID)
12735 struct tree_check *check_value;
12737 /* Get the stored value. */
12738 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12739 /* Perform any access checks that were deferred. */
12740 access_check = check_value->checks;
12741 if (access_check)
12743 FOR_EACH_VEC_ELT (*access_check, i, chk)
12744 perform_or_defer_access_check (chk->binfo,
12745 chk->decl,
12746 chk->diag_decl,
12747 tf_warning_or_error);
12749 /* Return the stored value. */
12750 return check_value->value;
12753 /* Avoid performing name lookup if there is no possibility of
12754 finding a template-id. */
12755 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12756 || (next_token->type == CPP_NAME
12757 && !cp_parser_nth_token_starts_template_argument_list_p
12758 (parser, 2)))
12760 cp_parser_error (parser, "expected template-id");
12761 return error_mark_node;
12764 /* Remember where the template-id starts. */
12765 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12766 start_of_id = cp_lexer_token_position (parser->lexer, false);
12768 push_deferring_access_checks (dk_deferred);
12770 /* Parse the template-name. */
12771 is_identifier = false;
12772 templ = cp_parser_template_name (parser, template_keyword_p,
12773 check_dependency_p,
12774 is_declaration,
12775 tag_type,
12776 &is_identifier);
12777 if (templ == error_mark_node || is_identifier)
12779 pop_deferring_access_checks ();
12780 return templ;
12783 /* If we find the sequence `[:' after a template-name, it's probably
12784 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12785 parse correctly the argument list. */
12786 next_token = cp_lexer_peek_token (parser->lexer);
12787 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12788 if (next_token->type == CPP_OPEN_SQUARE
12789 && next_token->flags & DIGRAPH
12790 && next_token_2->type == CPP_COLON
12791 && !(next_token_2->flags & PREV_WHITE))
12793 cp_parser_parse_tentatively (parser);
12794 /* Change `:' into `::'. */
12795 next_token_2->type = CPP_SCOPE;
12796 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12797 CPP_LESS. */
12798 cp_lexer_consume_token (parser->lexer);
12800 /* Parse the arguments. */
12801 arguments = cp_parser_enclosed_template_argument_list (parser);
12802 if (!cp_parser_parse_definitely (parser))
12804 /* If we couldn't parse an argument list, then we revert our changes
12805 and return simply an error. Maybe this is not a template-id
12806 after all. */
12807 next_token_2->type = CPP_COLON;
12808 cp_parser_error (parser, "expected %<<%>");
12809 pop_deferring_access_checks ();
12810 return error_mark_node;
12812 /* Otherwise, emit an error about the invalid digraph, but continue
12813 parsing because we got our argument list. */
12814 if (permerror (next_token->location,
12815 "%<<::%> cannot begin a template-argument list"))
12817 static bool hint = false;
12818 inform (next_token->location,
12819 "%<<:%> is an alternate spelling for %<[%>."
12820 " Insert whitespace between %<<%> and %<::%>");
12821 if (!hint && !flag_permissive)
12823 inform (next_token->location, "(if you use %<-fpermissive%> "
12824 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12825 "accept your code)");
12826 hint = true;
12830 else
12832 /* Look for the `<' that starts the template-argument-list. */
12833 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12835 pop_deferring_access_checks ();
12836 return error_mark_node;
12838 /* Parse the arguments. */
12839 arguments = cp_parser_enclosed_template_argument_list (parser);
12842 /* Build a representation of the specialization. */
12843 if (identifier_p (templ))
12844 template_id = build_min_nt_loc (next_token->location,
12845 TEMPLATE_ID_EXPR,
12846 templ, arguments);
12847 else if (DECL_TYPE_TEMPLATE_P (templ)
12848 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12850 bool entering_scope;
12851 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12852 template (rather than some instantiation thereof) only if
12853 is not nested within some other construct. For example, in
12854 "template <typename T> void f(T) { A<T>::", A<T> is just an
12855 instantiation of A. */
12856 entering_scope = (template_parm_scope_p ()
12857 && cp_lexer_next_token_is (parser->lexer,
12858 CPP_SCOPE));
12859 template_id
12860 = finish_template_type (templ, arguments, entering_scope);
12862 else
12864 /* If it's not a class-template or a template-template, it should be
12865 a function-template. */
12866 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12867 || TREE_CODE (templ) == OVERLOAD
12868 || BASELINK_P (templ)));
12870 template_id = lookup_template_function (templ, arguments);
12873 /* If parsing tentatively, replace the sequence of tokens that makes
12874 up the template-id with a CPP_TEMPLATE_ID token. That way,
12875 should we re-parse the token stream, we will not have to repeat
12876 the effort required to do the parse, nor will we issue duplicate
12877 error messages about problems during instantiation of the
12878 template. */
12879 if (start_of_id)
12881 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12883 /* Reset the contents of the START_OF_ID token. */
12884 token->type = CPP_TEMPLATE_ID;
12885 /* Retrieve any deferred checks. Do not pop this access checks yet
12886 so the memory will not be reclaimed during token replacing below. */
12887 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12888 token->u.tree_check_value->value = template_id;
12889 token->u.tree_check_value->checks = get_deferred_access_checks ();
12890 token->keyword = RID_MAX;
12892 /* Purge all subsequent tokens. */
12893 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12895 /* ??? Can we actually assume that, if template_id ==
12896 error_mark_node, we will have issued a diagnostic to the
12897 user, as opposed to simply marking the tentative parse as
12898 failed? */
12899 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12900 error_at (token->location, "parse error in template argument list");
12903 pop_to_parent_deferring_access_checks ();
12904 return template_id;
12907 /* Parse a template-name.
12909 template-name:
12910 identifier
12912 The standard should actually say:
12914 template-name:
12915 identifier
12916 operator-function-id
12918 A defect report has been filed about this issue.
12920 A conversion-function-id cannot be a template name because they cannot
12921 be part of a template-id. In fact, looking at this code:
12923 a.operator K<int>()
12925 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12926 It is impossible to call a templated conversion-function-id with an
12927 explicit argument list, since the only allowed template parameter is
12928 the type to which it is converting.
12930 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12931 `template' keyword, in a construction like:
12933 T::template f<3>()
12935 In that case `f' is taken to be a template-name, even though there
12936 is no way of knowing for sure.
12938 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12939 name refers to a set of overloaded functions, at least one of which
12940 is a template, or an IDENTIFIER_NODE with the name of the template,
12941 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12942 names are looked up inside uninstantiated templates. */
12944 static tree
12945 cp_parser_template_name (cp_parser* parser,
12946 bool template_keyword_p,
12947 bool check_dependency_p,
12948 bool is_declaration,
12949 enum tag_types tag_type,
12950 bool *is_identifier)
12952 tree identifier;
12953 tree decl;
12954 tree fns;
12955 cp_token *token = cp_lexer_peek_token (parser->lexer);
12957 /* If the next token is `operator', then we have either an
12958 operator-function-id or a conversion-function-id. */
12959 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12961 /* We don't know whether we're looking at an
12962 operator-function-id or a conversion-function-id. */
12963 cp_parser_parse_tentatively (parser);
12964 /* Try an operator-function-id. */
12965 identifier = cp_parser_operator_function_id (parser);
12966 /* If that didn't work, try a conversion-function-id. */
12967 if (!cp_parser_parse_definitely (parser))
12969 cp_parser_error (parser, "expected template-name");
12970 return error_mark_node;
12973 /* Look for the identifier. */
12974 else
12975 identifier = cp_parser_identifier (parser);
12977 /* If we didn't find an identifier, we don't have a template-id. */
12978 if (identifier == error_mark_node)
12979 return error_mark_node;
12981 /* If the name immediately followed the `template' keyword, then it
12982 is a template-name. However, if the next token is not `<', then
12983 we do not treat it as a template-name, since it is not being used
12984 as part of a template-id. This enables us to handle constructs
12985 like:
12987 template <typename T> struct S { S(); };
12988 template <typename T> S<T>::S();
12990 correctly. We would treat `S' as a template -- if it were `S<T>'
12991 -- but we do not if there is no `<'. */
12993 if (processing_template_decl
12994 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12996 /* In a declaration, in a dependent context, we pretend that the
12997 "template" keyword was present in order to improve error
12998 recovery. For example, given:
13000 template <typename T> void f(T::X<int>);
13002 we want to treat "X<int>" as a template-id. */
13003 if (is_declaration
13004 && !template_keyword_p
13005 && parser->scope && TYPE_P (parser->scope)
13006 && check_dependency_p
13007 && dependent_scope_p (parser->scope)
13008 /* Do not do this for dtors (or ctors), since they never
13009 need the template keyword before their name. */
13010 && !constructor_name_p (identifier, parser->scope))
13012 cp_token_position start = 0;
13014 /* Explain what went wrong. */
13015 error_at (token->location, "non-template %qD used as template",
13016 identifier);
13017 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13018 parser->scope, identifier);
13019 /* If parsing tentatively, find the location of the "<" token. */
13020 if (cp_parser_simulate_error (parser))
13021 start = cp_lexer_token_position (parser->lexer, true);
13022 /* Parse the template arguments so that we can issue error
13023 messages about them. */
13024 cp_lexer_consume_token (parser->lexer);
13025 cp_parser_enclosed_template_argument_list (parser);
13026 /* Skip tokens until we find a good place from which to
13027 continue parsing. */
13028 cp_parser_skip_to_closing_parenthesis (parser,
13029 /*recovering=*/true,
13030 /*or_comma=*/true,
13031 /*consume_paren=*/false);
13032 /* If parsing tentatively, permanently remove the
13033 template argument list. That will prevent duplicate
13034 error messages from being issued about the missing
13035 "template" keyword. */
13036 if (start)
13037 cp_lexer_purge_tokens_after (parser->lexer, start);
13038 if (is_identifier)
13039 *is_identifier = true;
13040 return identifier;
13043 /* If the "template" keyword is present, then there is generally
13044 no point in doing name-lookup, so we just return IDENTIFIER.
13045 But, if the qualifying scope is non-dependent then we can
13046 (and must) do name-lookup normally. */
13047 if (template_keyword_p
13048 && (!parser->scope
13049 || (TYPE_P (parser->scope)
13050 && dependent_type_p (parser->scope))))
13051 return identifier;
13054 /* Look up the name. */
13055 decl = cp_parser_lookup_name (parser, identifier,
13056 tag_type,
13057 /*is_template=*/true,
13058 /*is_namespace=*/false,
13059 check_dependency_p,
13060 /*ambiguous_decls=*/NULL,
13061 token->location);
13063 /* If DECL is a template, then the name was a template-name. */
13064 if (TREE_CODE (decl) == TEMPLATE_DECL)
13066 else
13068 tree fn = NULL_TREE;
13070 /* The standard does not explicitly indicate whether a name that
13071 names a set of overloaded declarations, some of which are
13072 templates, is a template-name. However, such a name should
13073 be a template-name; otherwise, there is no way to form a
13074 template-id for the overloaded templates. */
13075 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13076 if (TREE_CODE (fns) == OVERLOAD)
13077 for (fn = fns; fn; fn = OVL_NEXT (fn))
13078 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13079 break;
13081 if (!fn)
13083 /* The name does not name a template. */
13084 cp_parser_error (parser, "expected template-name");
13085 return error_mark_node;
13089 /* If DECL is dependent, and refers to a function, then just return
13090 its name; we will look it up again during template instantiation. */
13091 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13093 tree scope = ovl_scope (decl);
13094 if (TYPE_P (scope) && dependent_type_p (scope))
13095 return identifier;
13098 return decl;
13101 /* Parse a template-argument-list.
13103 template-argument-list:
13104 template-argument ... [opt]
13105 template-argument-list , template-argument ... [opt]
13107 Returns a TREE_VEC containing the arguments. */
13109 static tree
13110 cp_parser_template_argument_list (cp_parser* parser)
13112 tree fixed_args[10];
13113 unsigned n_args = 0;
13114 unsigned alloced = 10;
13115 tree *arg_ary = fixed_args;
13116 tree vec;
13117 bool saved_in_template_argument_list_p;
13118 bool saved_ice_p;
13119 bool saved_non_ice_p;
13121 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13122 parser->in_template_argument_list_p = true;
13123 /* Even if the template-id appears in an integral
13124 constant-expression, the contents of the argument list do
13125 not. */
13126 saved_ice_p = parser->integral_constant_expression_p;
13127 parser->integral_constant_expression_p = false;
13128 saved_non_ice_p = parser->non_integral_constant_expression_p;
13129 parser->non_integral_constant_expression_p = false;
13131 /* Parse the arguments. */
13134 tree argument;
13136 if (n_args)
13137 /* Consume the comma. */
13138 cp_lexer_consume_token (parser->lexer);
13140 /* Parse the template-argument. */
13141 argument = cp_parser_template_argument (parser);
13143 /* If the next token is an ellipsis, we're expanding a template
13144 argument pack. */
13145 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13147 if (argument == error_mark_node)
13149 cp_token *token = cp_lexer_peek_token (parser->lexer);
13150 error_at (token->location,
13151 "expected parameter pack before %<...%>");
13153 /* Consume the `...' token. */
13154 cp_lexer_consume_token (parser->lexer);
13156 /* Make the argument into a TYPE_PACK_EXPANSION or
13157 EXPR_PACK_EXPANSION. */
13158 argument = make_pack_expansion (argument);
13161 if (n_args == alloced)
13163 alloced *= 2;
13165 if (arg_ary == fixed_args)
13167 arg_ary = XNEWVEC (tree, alloced);
13168 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13170 else
13171 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13173 arg_ary[n_args++] = argument;
13175 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13177 vec = make_tree_vec (n_args);
13179 while (n_args--)
13180 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13182 if (arg_ary != fixed_args)
13183 free (arg_ary);
13184 parser->non_integral_constant_expression_p = saved_non_ice_p;
13185 parser->integral_constant_expression_p = saved_ice_p;
13186 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13187 #ifdef ENABLE_CHECKING
13188 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13189 #endif
13190 return vec;
13193 /* Parse a template-argument.
13195 template-argument:
13196 assignment-expression
13197 type-id
13198 id-expression
13200 The representation is that of an assignment-expression, type-id, or
13201 id-expression -- except that the qualified id-expression is
13202 evaluated, so that the value returned is either a DECL or an
13203 OVERLOAD.
13205 Although the standard says "assignment-expression", it forbids
13206 throw-expressions or assignments in the template argument.
13207 Therefore, we use "conditional-expression" instead. */
13209 static tree
13210 cp_parser_template_argument (cp_parser* parser)
13212 tree argument;
13213 bool template_p;
13214 bool address_p;
13215 bool maybe_type_id = false;
13216 cp_token *token = NULL, *argument_start_token = NULL;
13217 location_t loc = 0;
13218 cp_id_kind idk;
13220 /* There's really no way to know what we're looking at, so we just
13221 try each alternative in order.
13223 [temp.arg]
13225 In a template-argument, an ambiguity between a type-id and an
13226 expression is resolved to a type-id, regardless of the form of
13227 the corresponding template-parameter.
13229 Therefore, we try a type-id first. */
13230 cp_parser_parse_tentatively (parser);
13231 argument = cp_parser_template_type_arg (parser);
13232 /* If there was no error parsing the type-id but the next token is a
13233 '>>', our behavior depends on which dialect of C++ we're
13234 parsing. In C++98, we probably found a typo for '> >'. But there
13235 are type-id which are also valid expressions. For instance:
13237 struct X { int operator >> (int); };
13238 template <int V> struct Foo {};
13239 Foo<X () >> 5> r;
13241 Here 'X()' is a valid type-id of a function type, but the user just
13242 wanted to write the expression "X() >> 5". Thus, we remember that we
13243 found a valid type-id, but we still try to parse the argument as an
13244 expression to see what happens.
13246 In C++0x, the '>>' will be considered two separate '>'
13247 tokens. */
13248 if (!cp_parser_error_occurred (parser)
13249 && cxx_dialect == cxx98
13250 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13252 maybe_type_id = true;
13253 cp_parser_abort_tentative_parse (parser);
13255 else
13257 /* If the next token isn't a `,' or a `>', then this argument wasn't
13258 really finished. This means that the argument is not a valid
13259 type-id. */
13260 if (!cp_parser_next_token_ends_template_argument_p (parser))
13261 cp_parser_error (parser, "expected template-argument");
13262 /* If that worked, we're done. */
13263 if (cp_parser_parse_definitely (parser))
13264 return argument;
13266 /* We're still not sure what the argument will be. */
13267 cp_parser_parse_tentatively (parser);
13268 /* Try a template. */
13269 argument_start_token = cp_lexer_peek_token (parser->lexer);
13270 argument = cp_parser_id_expression (parser,
13271 /*template_keyword_p=*/false,
13272 /*check_dependency_p=*/true,
13273 &template_p,
13274 /*declarator_p=*/false,
13275 /*optional_p=*/false);
13276 /* If the next token isn't a `,' or a `>', then this argument wasn't
13277 really finished. */
13278 if (!cp_parser_next_token_ends_template_argument_p (parser))
13279 cp_parser_error (parser, "expected template-argument");
13280 if (!cp_parser_error_occurred (parser))
13282 /* Figure out what is being referred to. If the id-expression
13283 was for a class template specialization, then we will have a
13284 TYPE_DECL at this point. There is no need to do name lookup
13285 at this point in that case. */
13286 if (TREE_CODE (argument) != TYPE_DECL)
13287 argument = cp_parser_lookup_name (parser, argument,
13288 none_type,
13289 /*is_template=*/template_p,
13290 /*is_namespace=*/false,
13291 /*check_dependency=*/true,
13292 /*ambiguous_decls=*/NULL,
13293 argument_start_token->location);
13294 if (TREE_CODE (argument) != TEMPLATE_DECL
13295 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13296 cp_parser_error (parser, "expected template-name");
13298 if (cp_parser_parse_definitely (parser))
13299 return argument;
13300 /* It must be a non-type argument. There permitted cases are given
13301 in [temp.arg.nontype]:
13303 -- an integral constant-expression of integral or enumeration
13304 type; or
13306 -- the name of a non-type template-parameter; or
13308 -- the name of an object or function with external linkage...
13310 -- the address of an object or function with external linkage...
13312 -- a pointer to member... */
13313 /* Look for a non-type template parameter. */
13314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13316 cp_parser_parse_tentatively (parser);
13317 argument = cp_parser_primary_expression (parser,
13318 /*address_p=*/false,
13319 /*cast_p=*/false,
13320 /*template_arg_p=*/true,
13321 &idk);
13322 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13323 || !cp_parser_next_token_ends_template_argument_p (parser))
13324 cp_parser_simulate_error (parser);
13325 if (cp_parser_parse_definitely (parser))
13326 return argument;
13329 /* If the next token is "&", the argument must be the address of an
13330 object or function with external linkage. */
13331 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13332 if (address_p)
13334 loc = cp_lexer_peek_token (parser->lexer)->location;
13335 cp_lexer_consume_token (parser->lexer);
13337 /* See if we might have an id-expression. */
13338 token = cp_lexer_peek_token (parser->lexer);
13339 if (token->type == CPP_NAME
13340 || token->keyword == RID_OPERATOR
13341 || token->type == CPP_SCOPE
13342 || token->type == CPP_TEMPLATE_ID
13343 || token->type == CPP_NESTED_NAME_SPECIFIER)
13345 cp_parser_parse_tentatively (parser);
13346 argument = cp_parser_primary_expression (parser,
13347 address_p,
13348 /*cast_p=*/false,
13349 /*template_arg_p=*/true,
13350 &idk);
13351 if (cp_parser_error_occurred (parser)
13352 || !cp_parser_next_token_ends_template_argument_p (parser))
13353 cp_parser_abort_tentative_parse (parser);
13354 else
13356 tree probe;
13358 if (INDIRECT_REF_P (argument))
13360 gcc_assert (REFERENCE_REF_P (argument));
13361 argument = TREE_OPERAND (argument, 0);
13364 /* If we're in a template, we represent a qualified-id referring
13365 to a static data member as a SCOPE_REF even if the scope isn't
13366 dependent so that we can check access control later. */
13367 probe = argument;
13368 if (TREE_CODE (probe) == SCOPE_REF)
13369 probe = TREE_OPERAND (probe, 1);
13370 if (VAR_P (probe))
13372 /* A variable without external linkage might still be a
13373 valid constant-expression, so no error is issued here
13374 if the external-linkage check fails. */
13375 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13376 cp_parser_simulate_error (parser);
13378 else if (is_overloaded_fn (argument))
13379 /* All overloaded functions are allowed; if the external
13380 linkage test does not pass, an error will be issued
13381 later. */
13383 else if (address_p
13384 && (TREE_CODE (argument) == OFFSET_REF
13385 || TREE_CODE (argument) == SCOPE_REF))
13386 /* A pointer-to-member. */
13388 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13390 else
13391 cp_parser_simulate_error (parser);
13393 if (cp_parser_parse_definitely (parser))
13395 if (address_p)
13396 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13397 tf_warning_or_error);
13398 return argument;
13402 /* If the argument started with "&", there are no other valid
13403 alternatives at this point. */
13404 if (address_p)
13406 cp_parser_error (parser, "invalid non-type template argument");
13407 return error_mark_node;
13410 /* If the argument wasn't successfully parsed as a type-id followed
13411 by '>>', the argument can only be a constant expression now.
13412 Otherwise, we try parsing the constant-expression tentatively,
13413 because the argument could really be a type-id. */
13414 if (maybe_type_id)
13415 cp_parser_parse_tentatively (parser);
13416 argument = cp_parser_constant_expression (parser,
13417 /*allow_non_constant_p=*/false,
13418 /*non_constant_p=*/NULL);
13419 if (!maybe_type_id)
13420 return argument;
13421 if (!cp_parser_next_token_ends_template_argument_p (parser))
13422 cp_parser_error (parser, "expected template-argument");
13423 if (cp_parser_parse_definitely (parser))
13424 return argument;
13425 /* We did our best to parse the argument as a non type-id, but that
13426 was the only alternative that matched (albeit with a '>' after
13427 it). We can assume it's just a typo from the user, and a
13428 diagnostic will then be issued. */
13429 return cp_parser_template_type_arg (parser);
13432 /* Parse an explicit-instantiation.
13434 explicit-instantiation:
13435 template declaration
13437 Although the standard says `declaration', what it really means is:
13439 explicit-instantiation:
13440 template decl-specifier-seq [opt] declarator [opt] ;
13442 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13443 supposed to be allowed. A defect report has been filed about this
13444 issue.
13446 GNU Extension:
13448 explicit-instantiation:
13449 storage-class-specifier template
13450 decl-specifier-seq [opt] declarator [opt] ;
13451 function-specifier template
13452 decl-specifier-seq [opt] declarator [opt] ; */
13454 static void
13455 cp_parser_explicit_instantiation (cp_parser* parser)
13457 int declares_class_or_enum;
13458 cp_decl_specifier_seq decl_specifiers;
13459 tree extension_specifier = NULL_TREE;
13461 timevar_push (TV_TEMPLATE_INST);
13463 /* Look for an (optional) storage-class-specifier or
13464 function-specifier. */
13465 if (cp_parser_allow_gnu_extensions_p (parser))
13467 extension_specifier
13468 = cp_parser_storage_class_specifier_opt (parser);
13469 if (!extension_specifier)
13470 extension_specifier
13471 = cp_parser_function_specifier_opt (parser,
13472 /*decl_specs=*/NULL);
13475 /* Look for the `template' keyword. */
13476 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13477 /* Let the front end know that we are processing an explicit
13478 instantiation. */
13479 begin_explicit_instantiation ();
13480 /* [temp.explicit] says that we are supposed to ignore access
13481 control while processing explicit instantiation directives. */
13482 push_deferring_access_checks (dk_no_check);
13483 /* Parse a decl-specifier-seq. */
13484 cp_parser_decl_specifier_seq (parser,
13485 CP_PARSER_FLAGS_OPTIONAL,
13486 &decl_specifiers,
13487 &declares_class_or_enum);
13488 /* If there was exactly one decl-specifier, and it declared a class,
13489 and there's no declarator, then we have an explicit type
13490 instantiation. */
13491 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13493 tree type;
13495 type = check_tag_decl (&decl_specifiers,
13496 /*explicit_type_instantiation_p=*/true);
13497 /* Turn access control back on for names used during
13498 template instantiation. */
13499 pop_deferring_access_checks ();
13500 if (type)
13501 do_type_instantiation (type, extension_specifier,
13502 /*complain=*/tf_error);
13504 else
13506 cp_declarator *declarator;
13507 tree decl;
13509 /* Parse the declarator. */
13510 declarator
13511 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13512 /*ctor_dtor_or_conv_p=*/NULL,
13513 /*parenthesized_p=*/NULL,
13514 /*member_p=*/false);
13515 if (declares_class_or_enum & 2)
13516 cp_parser_check_for_definition_in_return_type (declarator,
13517 decl_specifiers.type,
13518 decl_specifiers.locations[ds_type_spec]);
13519 if (declarator != cp_error_declarator)
13521 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13522 permerror (decl_specifiers.locations[ds_inline],
13523 "explicit instantiation shall not use"
13524 " %<inline%> specifier");
13525 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13526 permerror (decl_specifiers.locations[ds_constexpr],
13527 "explicit instantiation shall not use"
13528 " %<constexpr%> specifier");
13530 decl = grokdeclarator (declarator, &decl_specifiers,
13531 NORMAL, 0, &decl_specifiers.attributes);
13532 /* Turn access control back on for names used during
13533 template instantiation. */
13534 pop_deferring_access_checks ();
13535 /* Do the explicit instantiation. */
13536 do_decl_instantiation (decl, extension_specifier);
13538 else
13540 pop_deferring_access_checks ();
13541 /* Skip the body of the explicit instantiation. */
13542 cp_parser_skip_to_end_of_statement (parser);
13545 /* We're done with the instantiation. */
13546 end_explicit_instantiation ();
13548 cp_parser_consume_semicolon_at_end_of_statement (parser);
13550 timevar_pop (TV_TEMPLATE_INST);
13553 /* Parse an explicit-specialization.
13555 explicit-specialization:
13556 template < > declaration
13558 Although the standard says `declaration', what it really means is:
13560 explicit-specialization:
13561 template <> decl-specifier [opt] init-declarator [opt] ;
13562 template <> function-definition
13563 template <> explicit-specialization
13564 template <> template-declaration */
13566 static void
13567 cp_parser_explicit_specialization (cp_parser* parser)
13569 bool need_lang_pop;
13570 cp_token *token = cp_lexer_peek_token (parser->lexer);
13572 /* Look for the `template' keyword. */
13573 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13574 /* Look for the `<'. */
13575 cp_parser_require (parser, CPP_LESS, RT_LESS);
13576 /* Look for the `>'. */
13577 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13578 /* We have processed another parameter list. */
13579 ++parser->num_template_parameter_lists;
13580 /* [temp]
13582 A template ... explicit specialization ... shall not have C
13583 linkage. */
13584 if (current_lang_name == lang_name_c)
13586 error_at (token->location, "template specialization with C linkage");
13587 /* Give it C++ linkage to avoid confusing other parts of the
13588 front end. */
13589 push_lang_context (lang_name_cplusplus);
13590 need_lang_pop = true;
13592 else
13593 need_lang_pop = false;
13594 /* Let the front end know that we are beginning a specialization. */
13595 if (!begin_specialization ())
13597 end_specialization ();
13598 return;
13601 /* If the next keyword is `template', we need to figure out whether
13602 or not we're looking a template-declaration. */
13603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13605 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13606 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13607 cp_parser_template_declaration_after_export (parser,
13608 /*member_p=*/false);
13609 else
13610 cp_parser_explicit_specialization (parser);
13612 else
13613 /* Parse the dependent declaration. */
13614 cp_parser_single_declaration (parser,
13615 /*checks=*/NULL,
13616 /*member_p=*/false,
13617 /*explicit_specialization_p=*/true,
13618 /*friend_p=*/NULL);
13619 /* We're done with the specialization. */
13620 end_specialization ();
13621 /* For the erroneous case of a template with C linkage, we pushed an
13622 implicit C++ linkage scope; exit that scope now. */
13623 if (need_lang_pop)
13624 pop_lang_context ();
13625 /* We're done with this parameter list. */
13626 --parser->num_template_parameter_lists;
13629 /* Parse a type-specifier.
13631 type-specifier:
13632 simple-type-specifier
13633 class-specifier
13634 enum-specifier
13635 elaborated-type-specifier
13636 cv-qualifier
13638 GNU Extension:
13640 type-specifier:
13641 __complex__
13643 Returns a representation of the type-specifier. For a
13644 class-specifier, enum-specifier, or elaborated-type-specifier, a
13645 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13647 The parser flags FLAGS is used to control type-specifier parsing.
13649 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13650 in a decl-specifier-seq.
13652 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13653 class-specifier, enum-specifier, or elaborated-type-specifier, then
13654 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13655 if a type is declared; 2 if it is defined. Otherwise, it is set to
13656 zero.
13658 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13659 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13660 is set to FALSE. */
13662 static tree
13663 cp_parser_type_specifier (cp_parser* parser,
13664 cp_parser_flags flags,
13665 cp_decl_specifier_seq *decl_specs,
13666 bool is_declaration,
13667 int* declares_class_or_enum,
13668 bool* is_cv_qualifier)
13670 tree type_spec = NULL_TREE;
13671 cp_token *token;
13672 enum rid keyword;
13673 cp_decl_spec ds = ds_last;
13675 /* Assume this type-specifier does not declare a new type. */
13676 if (declares_class_or_enum)
13677 *declares_class_or_enum = 0;
13678 /* And that it does not specify a cv-qualifier. */
13679 if (is_cv_qualifier)
13680 *is_cv_qualifier = false;
13681 /* Peek at the next token. */
13682 token = cp_lexer_peek_token (parser->lexer);
13684 /* If we're looking at a keyword, we can use that to guide the
13685 production we choose. */
13686 keyword = token->keyword;
13687 switch (keyword)
13689 case RID_ENUM:
13690 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13691 goto elaborated_type_specifier;
13693 /* Look for the enum-specifier. */
13694 type_spec = cp_parser_enum_specifier (parser);
13695 /* If that worked, we're done. */
13696 if (type_spec)
13698 if (declares_class_or_enum)
13699 *declares_class_or_enum = 2;
13700 if (decl_specs)
13701 cp_parser_set_decl_spec_type (decl_specs,
13702 type_spec,
13703 token,
13704 /*type_definition_p=*/true);
13705 return type_spec;
13707 else
13708 goto elaborated_type_specifier;
13710 /* Any of these indicate either a class-specifier, or an
13711 elaborated-type-specifier. */
13712 case RID_CLASS:
13713 case RID_STRUCT:
13714 case RID_UNION:
13715 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13716 goto elaborated_type_specifier;
13718 /* Parse tentatively so that we can back up if we don't find a
13719 class-specifier. */
13720 cp_parser_parse_tentatively (parser);
13721 /* Look for the class-specifier. */
13722 type_spec = cp_parser_class_specifier (parser);
13723 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13724 /* If that worked, we're done. */
13725 if (cp_parser_parse_definitely (parser))
13727 if (declares_class_or_enum)
13728 *declares_class_or_enum = 2;
13729 if (decl_specs)
13730 cp_parser_set_decl_spec_type (decl_specs,
13731 type_spec,
13732 token,
13733 /*type_definition_p=*/true);
13734 return type_spec;
13737 /* Fall through. */
13738 elaborated_type_specifier:
13739 /* We're declaring (not defining) a class or enum. */
13740 if (declares_class_or_enum)
13741 *declares_class_or_enum = 1;
13743 /* Fall through. */
13744 case RID_TYPENAME:
13745 /* Look for an elaborated-type-specifier. */
13746 type_spec
13747 = (cp_parser_elaborated_type_specifier
13748 (parser,
13749 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13750 is_declaration));
13751 if (decl_specs)
13752 cp_parser_set_decl_spec_type (decl_specs,
13753 type_spec,
13754 token,
13755 /*type_definition_p=*/false);
13756 return type_spec;
13758 case RID_CONST:
13759 ds = ds_const;
13760 if (is_cv_qualifier)
13761 *is_cv_qualifier = true;
13762 break;
13764 case RID_VOLATILE:
13765 ds = ds_volatile;
13766 if (is_cv_qualifier)
13767 *is_cv_qualifier = true;
13768 break;
13770 case RID_RESTRICT:
13771 ds = ds_restrict;
13772 if (is_cv_qualifier)
13773 *is_cv_qualifier = true;
13774 break;
13776 case RID_COMPLEX:
13777 /* The `__complex__' keyword is a GNU extension. */
13778 ds = ds_complex;
13779 break;
13781 default:
13782 break;
13785 /* Handle simple keywords. */
13786 if (ds != ds_last)
13788 if (decl_specs)
13790 set_and_check_decl_spec_loc (decl_specs, ds, token);
13791 decl_specs->any_specifiers_p = true;
13793 return cp_lexer_consume_token (parser->lexer)->u.value;
13796 /* If we do not already have a type-specifier, assume we are looking
13797 at a simple-type-specifier. */
13798 type_spec = cp_parser_simple_type_specifier (parser,
13799 decl_specs,
13800 flags);
13802 /* If we didn't find a type-specifier, and a type-specifier was not
13803 optional in this context, issue an error message. */
13804 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13806 cp_parser_error (parser, "expected type specifier");
13807 return error_mark_node;
13810 return type_spec;
13813 /* Parse a simple-type-specifier.
13815 simple-type-specifier:
13816 :: [opt] nested-name-specifier [opt] type-name
13817 :: [opt] nested-name-specifier template template-id
13818 char
13819 wchar_t
13820 bool
13821 short
13823 long
13824 signed
13825 unsigned
13826 float
13827 double
13828 void
13830 C++0x Extension:
13832 simple-type-specifier:
13833 auto
13834 decltype ( expression )
13835 char16_t
13836 char32_t
13837 __underlying_type ( type-id )
13839 GNU Extension:
13841 simple-type-specifier:
13842 __int128
13843 __typeof__ unary-expression
13844 __typeof__ ( type-id )
13846 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13847 appropriately updated. */
13849 static tree
13850 cp_parser_simple_type_specifier (cp_parser* parser,
13851 cp_decl_specifier_seq *decl_specs,
13852 cp_parser_flags flags)
13854 tree type = NULL_TREE;
13855 cp_token *token;
13857 /* Peek at the next token. */
13858 token = cp_lexer_peek_token (parser->lexer);
13860 /* If we're looking at a keyword, things are easy. */
13861 switch (token->keyword)
13863 case RID_CHAR:
13864 if (decl_specs)
13865 decl_specs->explicit_char_p = true;
13866 type = char_type_node;
13867 break;
13868 case RID_CHAR16:
13869 type = char16_type_node;
13870 break;
13871 case RID_CHAR32:
13872 type = char32_type_node;
13873 break;
13874 case RID_WCHAR:
13875 type = wchar_type_node;
13876 break;
13877 case RID_BOOL:
13878 type = boolean_type_node;
13879 break;
13880 case RID_SHORT:
13881 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13882 type = short_integer_type_node;
13883 break;
13884 case RID_INT:
13885 if (decl_specs)
13886 decl_specs->explicit_int_p = true;
13887 type = integer_type_node;
13888 break;
13889 case RID_INT128:
13890 if (!int128_integer_type_node)
13891 break;
13892 if (decl_specs)
13893 decl_specs->explicit_int128_p = true;
13894 type = int128_integer_type_node;
13895 break;
13896 case RID_LONG:
13897 if (decl_specs)
13898 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13899 type = long_integer_type_node;
13900 break;
13901 case RID_SIGNED:
13902 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13903 type = integer_type_node;
13904 break;
13905 case RID_UNSIGNED:
13906 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13907 type = unsigned_type_node;
13908 break;
13909 case RID_FLOAT:
13910 type = float_type_node;
13911 break;
13912 case RID_DOUBLE:
13913 type = double_type_node;
13914 break;
13915 case RID_VOID:
13916 type = void_type_node;
13917 break;
13919 case RID_AUTO:
13920 maybe_warn_cpp0x (CPP0X_AUTO);
13921 type = make_auto ();
13922 break;
13924 case RID_DECLTYPE:
13925 /* Since DR 743, decltype can either be a simple-type-specifier by
13926 itself or begin a nested-name-specifier. Parsing it will replace
13927 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13928 handling below decide what to do. */
13929 cp_parser_decltype (parser);
13930 cp_lexer_set_token_position (parser->lexer, token);
13931 break;
13933 case RID_TYPEOF:
13934 /* Consume the `typeof' token. */
13935 cp_lexer_consume_token (parser->lexer);
13936 /* Parse the operand to `typeof'. */
13937 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13938 /* If it is not already a TYPE, take its type. */
13939 if (!TYPE_P (type))
13940 type = finish_typeof (type);
13942 if (decl_specs)
13943 cp_parser_set_decl_spec_type (decl_specs, type,
13944 token,
13945 /*type_definition_p=*/false);
13947 return type;
13949 case RID_UNDERLYING_TYPE:
13950 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13951 if (decl_specs)
13952 cp_parser_set_decl_spec_type (decl_specs, type,
13953 token,
13954 /*type_definition_p=*/false);
13956 return type;
13958 case RID_BASES:
13959 case RID_DIRECT_BASES:
13960 type = cp_parser_trait_expr (parser, token->keyword);
13961 if (decl_specs)
13962 cp_parser_set_decl_spec_type (decl_specs, type,
13963 token,
13964 /*type_definition_p=*/false);
13965 return type;
13966 default:
13967 break;
13970 /* If token is an already-parsed decltype not followed by ::,
13971 it's a simple-type-specifier. */
13972 if (token->type == CPP_DECLTYPE
13973 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13975 type = token->u.value;
13976 if (decl_specs)
13977 cp_parser_set_decl_spec_type (decl_specs, type,
13978 token,
13979 /*type_definition_p=*/false);
13980 cp_lexer_consume_token (parser->lexer);
13981 return type;
13984 /* If the type-specifier was for a built-in type, we're done. */
13985 if (type)
13987 /* Record the type. */
13988 if (decl_specs
13989 && (token->keyword != RID_SIGNED
13990 && token->keyword != RID_UNSIGNED
13991 && token->keyword != RID_SHORT
13992 && token->keyword != RID_LONG))
13993 cp_parser_set_decl_spec_type (decl_specs,
13994 type,
13995 token,
13996 /*type_definition_p=*/false);
13997 if (decl_specs)
13998 decl_specs->any_specifiers_p = true;
14000 /* Consume the token. */
14001 cp_lexer_consume_token (parser->lexer);
14003 /* There is no valid C++ program where a non-template type is
14004 followed by a "<". That usually indicates that the user thought
14005 that the type was a template. */
14006 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14007 token->location);
14009 return TYPE_NAME (type);
14012 /* The type-specifier must be a user-defined type. */
14013 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14015 bool qualified_p;
14016 bool global_p;
14018 /* Don't gobble tokens or issue error messages if this is an
14019 optional type-specifier. */
14020 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14021 cp_parser_parse_tentatively (parser);
14023 /* Look for the optional `::' operator. */
14024 global_p
14025 = (cp_parser_global_scope_opt (parser,
14026 /*current_scope_valid_p=*/false)
14027 != NULL_TREE);
14028 /* Look for the nested-name specifier. */
14029 qualified_p
14030 = (cp_parser_nested_name_specifier_opt (parser,
14031 /*typename_keyword_p=*/false,
14032 /*check_dependency_p=*/true,
14033 /*type_p=*/false,
14034 /*is_declaration=*/false)
14035 != NULL_TREE);
14036 token = cp_lexer_peek_token (parser->lexer);
14037 /* If we have seen a nested-name-specifier, and the next token
14038 is `template', then we are using the template-id production. */
14039 if (parser->scope
14040 && cp_parser_optional_template_keyword (parser))
14042 /* Look for the template-id. */
14043 type = cp_parser_template_id (parser,
14044 /*template_keyword_p=*/true,
14045 /*check_dependency_p=*/true,
14046 none_type,
14047 /*is_declaration=*/false);
14048 /* If the template-id did not name a type, we are out of
14049 luck. */
14050 if (TREE_CODE (type) != TYPE_DECL)
14052 cp_parser_error (parser, "expected template-id for type");
14053 type = NULL_TREE;
14056 /* Otherwise, look for a type-name. */
14057 else
14058 type = cp_parser_type_name (parser);
14059 /* Keep track of all name-lookups performed in class scopes. */
14060 if (type
14061 && !global_p
14062 && !qualified_p
14063 && TREE_CODE (type) == TYPE_DECL
14064 && identifier_p (DECL_NAME (type)))
14065 maybe_note_name_used_in_class (DECL_NAME (type), type);
14066 /* If it didn't work out, we don't have a TYPE. */
14067 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14068 && !cp_parser_parse_definitely (parser))
14069 type = NULL_TREE;
14070 if (type && decl_specs)
14071 cp_parser_set_decl_spec_type (decl_specs, type,
14072 token,
14073 /*type_definition_p=*/false);
14076 /* If we didn't get a type-name, issue an error message. */
14077 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14079 cp_parser_error (parser, "expected type-name");
14080 return error_mark_node;
14083 if (type && type != error_mark_node)
14085 /* See if TYPE is an Objective-C type, and if so, parse and
14086 accept any protocol references following it. Do this before
14087 the cp_parser_check_for_invalid_template_id() call, because
14088 Objective-C types can be followed by '<...>' which would
14089 enclose protocol names rather than template arguments, and so
14090 everything is fine. */
14091 if (c_dialect_objc () && !parser->scope
14092 && (objc_is_id (type) || objc_is_class_name (type)))
14094 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14095 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14097 /* Clobber the "unqualified" type previously entered into
14098 DECL_SPECS with the new, improved protocol-qualified version. */
14099 if (decl_specs)
14100 decl_specs->type = qual_type;
14102 return qual_type;
14105 /* There is no valid C++ program where a non-template type is
14106 followed by a "<". That usually indicates that the user
14107 thought that the type was a template. */
14108 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14109 none_type,
14110 token->location);
14113 return type;
14116 /* Parse a type-name.
14118 type-name:
14119 class-name
14120 enum-name
14121 typedef-name
14122 simple-template-id [in c++0x]
14124 enum-name:
14125 identifier
14127 typedef-name:
14128 identifier
14130 Returns a TYPE_DECL for the type. */
14132 static tree
14133 cp_parser_type_name (cp_parser* parser)
14135 tree type_decl;
14137 /* We can't know yet whether it is a class-name or not. */
14138 cp_parser_parse_tentatively (parser);
14139 /* Try a class-name. */
14140 type_decl = cp_parser_class_name (parser,
14141 /*typename_keyword_p=*/false,
14142 /*template_keyword_p=*/false,
14143 none_type,
14144 /*check_dependency_p=*/true,
14145 /*class_head_p=*/false,
14146 /*is_declaration=*/false);
14147 /* If it's not a class-name, keep looking. */
14148 if (!cp_parser_parse_definitely (parser))
14150 if (cxx_dialect < cxx0x)
14151 /* It must be a typedef-name or an enum-name. */
14152 return cp_parser_nonclass_name (parser);
14154 cp_parser_parse_tentatively (parser);
14155 /* It is either a simple-template-id representing an
14156 instantiation of an alias template... */
14157 type_decl = cp_parser_template_id (parser,
14158 /*template_keyword_p=*/false,
14159 /*check_dependency_p=*/false,
14160 none_type,
14161 /*is_declaration=*/false);
14162 /* Note that this must be an instantiation of an alias template
14163 because [temp.names]/6 says:
14165 A template-id that names an alias template specialization
14166 is a type-name.
14168 Whereas [temp.names]/7 says:
14170 A simple-template-id that names a class template
14171 specialization is a class-name. */
14172 if (type_decl != NULL_TREE
14173 && TREE_CODE (type_decl) == TYPE_DECL
14174 && TYPE_DECL_ALIAS_P (type_decl))
14175 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14176 else
14177 cp_parser_simulate_error (parser);
14179 if (!cp_parser_parse_definitely (parser))
14180 /* ... Or a typedef-name or an enum-name. */
14181 return cp_parser_nonclass_name (parser);
14184 return type_decl;
14187 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14189 enum-name:
14190 identifier
14192 typedef-name:
14193 identifier
14195 Returns a TYPE_DECL for the type. */
14197 static tree
14198 cp_parser_nonclass_name (cp_parser* parser)
14200 tree type_decl;
14201 tree identifier;
14203 cp_token *token = cp_lexer_peek_token (parser->lexer);
14204 identifier = cp_parser_identifier (parser);
14205 if (identifier == error_mark_node)
14206 return error_mark_node;
14208 /* Look up the type-name. */
14209 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14211 if (TREE_CODE (type_decl) == USING_DECL)
14213 if (!DECL_DEPENDENT_P (type_decl))
14214 type_decl = strip_using_decl (type_decl);
14215 else if (USING_DECL_TYPENAME_P (type_decl))
14217 /* We have found a type introduced by a using
14218 declaration at class scope that refers to a dependent
14219 type.
14221 using typename :: [opt] nested-name-specifier unqualified-id ;
14223 type_decl = make_typename_type (TREE_TYPE (type_decl),
14224 DECL_NAME (type_decl),
14225 typename_type, tf_error);
14226 if (type_decl != error_mark_node)
14227 type_decl = TYPE_NAME (type_decl);
14231 if (TREE_CODE (type_decl) != TYPE_DECL
14232 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14234 /* See if this is an Objective-C type. */
14235 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14236 tree type = objc_get_protocol_qualified_type (identifier, protos);
14237 if (type)
14238 type_decl = TYPE_NAME (type);
14241 /* Issue an error if we did not find a type-name. */
14242 if (TREE_CODE (type_decl) != TYPE_DECL
14243 /* In Objective-C, we have the complication that class names are
14244 normally type names and start declarations (eg, the
14245 "NSObject" in "NSObject *object;"), but can be used in an
14246 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14247 is an expression. So, a classname followed by a dot is not a
14248 valid type-name. */
14249 || (objc_is_class_name (TREE_TYPE (type_decl))
14250 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14252 if (!cp_parser_simulate_error (parser))
14253 cp_parser_name_lookup_error (parser, identifier, type_decl,
14254 NLE_TYPE, token->location);
14255 return error_mark_node;
14257 /* Remember that the name was used in the definition of the
14258 current class so that we can check later to see if the
14259 meaning would have been different after the class was
14260 entirely defined. */
14261 else if (type_decl != error_mark_node
14262 && !parser->scope)
14263 maybe_note_name_used_in_class (identifier, type_decl);
14265 return type_decl;
14268 /* Parse an elaborated-type-specifier. Note that the grammar given
14269 here incorporates the resolution to DR68.
14271 elaborated-type-specifier:
14272 class-key :: [opt] nested-name-specifier [opt] identifier
14273 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14274 enum-key :: [opt] nested-name-specifier [opt] identifier
14275 typename :: [opt] nested-name-specifier identifier
14276 typename :: [opt] nested-name-specifier template [opt]
14277 template-id
14279 GNU extension:
14281 elaborated-type-specifier:
14282 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14283 class-key attributes :: [opt] nested-name-specifier [opt]
14284 template [opt] template-id
14285 enum attributes :: [opt] nested-name-specifier [opt] identifier
14287 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14288 declared `friend'. If IS_DECLARATION is TRUE, then this
14289 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14290 something is being declared.
14292 Returns the TYPE specified. */
14294 static tree
14295 cp_parser_elaborated_type_specifier (cp_parser* parser,
14296 bool is_friend,
14297 bool is_declaration)
14299 enum tag_types tag_type;
14300 tree identifier;
14301 tree type = NULL_TREE;
14302 tree attributes = NULL_TREE;
14303 tree globalscope;
14304 cp_token *token = NULL;
14306 /* See if we're looking at the `enum' keyword. */
14307 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14309 /* Consume the `enum' token. */
14310 cp_lexer_consume_token (parser->lexer);
14311 /* Remember that it's an enumeration type. */
14312 tag_type = enum_type;
14313 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14314 enums) is used here. */
14315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14316 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14318 pedwarn (input_location, 0, "elaborated-type-specifier "
14319 "for a scoped enum must not use the %<%D%> keyword",
14320 cp_lexer_peek_token (parser->lexer)->u.value);
14321 /* Consume the `struct' or `class' and parse it anyway. */
14322 cp_lexer_consume_token (parser->lexer);
14324 /* Parse the attributes. */
14325 attributes = cp_parser_attributes_opt (parser);
14327 /* Or, it might be `typename'. */
14328 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14329 RID_TYPENAME))
14331 /* Consume the `typename' token. */
14332 cp_lexer_consume_token (parser->lexer);
14333 /* Remember that it's a `typename' type. */
14334 tag_type = typename_type;
14336 /* Otherwise it must be a class-key. */
14337 else
14339 tag_type = cp_parser_class_key (parser);
14340 if (tag_type == none_type)
14341 return error_mark_node;
14342 /* Parse the attributes. */
14343 attributes = cp_parser_attributes_opt (parser);
14346 /* Look for the `::' operator. */
14347 globalscope = cp_parser_global_scope_opt (parser,
14348 /*current_scope_valid_p=*/false);
14349 /* Look for the nested-name-specifier. */
14350 if (tag_type == typename_type && !globalscope)
14352 if (!cp_parser_nested_name_specifier (parser,
14353 /*typename_keyword_p=*/true,
14354 /*check_dependency_p=*/true,
14355 /*type_p=*/true,
14356 is_declaration))
14357 return error_mark_node;
14359 else
14360 /* Even though `typename' is not present, the proposed resolution
14361 to Core Issue 180 says that in `class A<T>::B', `B' should be
14362 considered a type-name, even if `A<T>' is dependent. */
14363 cp_parser_nested_name_specifier_opt (parser,
14364 /*typename_keyword_p=*/true,
14365 /*check_dependency_p=*/true,
14366 /*type_p=*/true,
14367 is_declaration);
14368 /* For everything but enumeration types, consider a template-id.
14369 For an enumeration type, consider only a plain identifier. */
14370 if (tag_type != enum_type)
14372 bool template_p = false;
14373 tree decl;
14375 /* Allow the `template' keyword. */
14376 template_p = cp_parser_optional_template_keyword (parser);
14377 /* If we didn't see `template', we don't know if there's a
14378 template-id or not. */
14379 if (!template_p)
14380 cp_parser_parse_tentatively (parser);
14381 /* Parse the template-id. */
14382 token = cp_lexer_peek_token (parser->lexer);
14383 decl = cp_parser_template_id (parser, template_p,
14384 /*check_dependency_p=*/true,
14385 tag_type,
14386 is_declaration);
14387 /* If we didn't find a template-id, look for an ordinary
14388 identifier. */
14389 if (!template_p && !cp_parser_parse_definitely (parser))
14391 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14392 in effect, then we must assume that, upon instantiation, the
14393 template will correspond to a class. */
14394 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14395 && tag_type == typename_type)
14396 type = make_typename_type (parser->scope, decl,
14397 typename_type,
14398 /*complain=*/tf_error);
14399 /* If the `typename' keyword is in effect and DECL is not a type
14400 decl, then type is non existent. */
14401 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14403 else if (TREE_CODE (decl) == TYPE_DECL)
14404 type = check_elaborated_type_specifier (tag_type, decl,
14405 /*allow_template_p=*/true);
14406 else if (decl == error_mark_node)
14407 type = error_mark_node;
14410 if (!type)
14412 token = cp_lexer_peek_token (parser->lexer);
14413 identifier = cp_parser_identifier (parser);
14415 if (identifier == error_mark_node)
14417 parser->scope = NULL_TREE;
14418 return error_mark_node;
14421 /* For a `typename', we needn't call xref_tag. */
14422 if (tag_type == typename_type
14423 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14424 return cp_parser_make_typename_type (parser, parser->scope,
14425 identifier,
14426 token->location);
14427 /* Look up a qualified name in the usual way. */
14428 if (parser->scope)
14430 tree decl;
14431 tree ambiguous_decls;
14433 decl = cp_parser_lookup_name (parser, identifier,
14434 tag_type,
14435 /*is_template=*/false,
14436 /*is_namespace=*/false,
14437 /*check_dependency=*/true,
14438 &ambiguous_decls,
14439 token->location);
14441 /* If the lookup was ambiguous, an error will already have been
14442 issued. */
14443 if (ambiguous_decls)
14444 return error_mark_node;
14446 /* If we are parsing friend declaration, DECL may be a
14447 TEMPLATE_DECL tree node here. However, we need to check
14448 whether this TEMPLATE_DECL results in valid code. Consider
14449 the following example:
14451 namespace N {
14452 template <class T> class C {};
14454 class X {
14455 template <class T> friend class N::C; // #1, valid code
14457 template <class T> class Y {
14458 friend class N::C; // #2, invalid code
14461 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14462 name lookup of `N::C'. We see that friend declaration must
14463 be template for the code to be valid. Note that
14464 processing_template_decl does not work here since it is
14465 always 1 for the above two cases. */
14467 decl = (cp_parser_maybe_treat_template_as_class
14468 (decl, /*tag_name_p=*/is_friend
14469 && parser->num_template_parameter_lists));
14471 if (TREE_CODE (decl) != TYPE_DECL)
14473 cp_parser_diagnose_invalid_type_name (parser,
14474 parser->scope,
14475 identifier,
14476 token->location);
14477 return error_mark_node;
14480 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14482 bool allow_template = (parser->num_template_parameter_lists
14483 || DECL_SELF_REFERENCE_P (decl));
14484 type = check_elaborated_type_specifier (tag_type, decl,
14485 allow_template);
14487 if (type == error_mark_node)
14488 return error_mark_node;
14491 /* Forward declarations of nested types, such as
14493 class C1::C2;
14494 class C1::C2::C3;
14496 are invalid unless all components preceding the final '::'
14497 are complete. If all enclosing types are complete, these
14498 declarations become merely pointless.
14500 Invalid forward declarations of nested types are errors
14501 caught elsewhere in parsing. Those that are pointless arrive
14502 here. */
14504 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14505 && !is_friend && !processing_explicit_instantiation)
14506 warning (0, "declaration %qD does not declare anything", decl);
14508 type = TREE_TYPE (decl);
14510 else
14512 /* An elaborated-type-specifier sometimes introduces a new type and
14513 sometimes names an existing type. Normally, the rule is that it
14514 introduces a new type only if there is not an existing type of
14515 the same name already in scope. For example, given:
14517 struct S {};
14518 void f() { struct S s; }
14520 the `struct S' in the body of `f' is the same `struct S' as in
14521 the global scope; the existing definition is used. However, if
14522 there were no global declaration, this would introduce a new
14523 local class named `S'.
14525 An exception to this rule applies to the following code:
14527 namespace N { struct S; }
14529 Here, the elaborated-type-specifier names a new type
14530 unconditionally; even if there is already an `S' in the
14531 containing scope this declaration names a new type.
14532 This exception only applies if the elaborated-type-specifier
14533 forms the complete declaration:
14535 [class.name]
14537 A declaration consisting solely of `class-key identifier ;' is
14538 either a redeclaration of the name in the current scope or a
14539 forward declaration of the identifier as a class name. It
14540 introduces the name into the current scope.
14542 We are in this situation precisely when the next token is a `;'.
14544 An exception to the exception is that a `friend' declaration does
14545 *not* name a new type; i.e., given:
14547 struct S { friend struct T; };
14549 `T' is not a new type in the scope of `S'.
14551 Also, `new struct S' or `sizeof (struct S)' never results in the
14552 definition of a new type; a new type can only be declared in a
14553 declaration context. */
14555 tag_scope ts;
14556 bool template_p;
14558 if (is_friend)
14559 /* Friends have special name lookup rules. */
14560 ts = ts_within_enclosing_non_class;
14561 else if (is_declaration
14562 && cp_lexer_next_token_is (parser->lexer,
14563 CPP_SEMICOLON))
14564 /* This is a `class-key identifier ;' */
14565 ts = ts_current;
14566 else
14567 ts = ts_global;
14569 template_p =
14570 (parser->num_template_parameter_lists
14571 && (cp_parser_next_token_starts_class_definition_p (parser)
14572 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14573 /* An unqualified name was used to reference this type, so
14574 there were no qualifying templates. */
14575 if (!cp_parser_check_template_parameters (parser,
14576 /*num_templates=*/0,
14577 token->location,
14578 /*declarator=*/NULL))
14579 return error_mark_node;
14580 type = xref_tag (tag_type, identifier, ts, template_p);
14584 if (type == error_mark_node)
14585 return error_mark_node;
14587 /* Allow attributes on forward declarations of classes. */
14588 if (attributes)
14590 if (TREE_CODE (type) == TYPENAME_TYPE)
14591 warning (OPT_Wattributes,
14592 "attributes ignored on uninstantiated type");
14593 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14594 && ! processing_explicit_instantiation)
14595 warning (OPT_Wattributes,
14596 "attributes ignored on template instantiation");
14597 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14598 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14599 else
14600 warning (OPT_Wattributes,
14601 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14604 if (tag_type != enum_type)
14606 /* Indicate whether this class was declared as a `class' or as a
14607 `struct'. */
14608 if (TREE_CODE (type) == RECORD_TYPE)
14609 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14610 cp_parser_check_class_key (tag_type, type);
14613 /* A "<" cannot follow an elaborated type specifier. If that
14614 happens, the user was probably trying to form a template-id. */
14615 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14616 token->location);
14618 return type;
14621 /* Parse an enum-specifier.
14623 enum-specifier:
14624 enum-head { enumerator-list [opt] }
14625 enum-head { enumerator-list , } [C++0x]
14627 enum-head:
14628 enum-key identifier [opt] enum-base [opt]
14629 enum-key nested-name-specifier identifier enum-base [opt]
14631 enum-key:
14632 enum
14633 enum class [C++0x]
14634 enum struct [C++0x]
14636 enum-base: [C++0x]
14637 : type-specifier-seq
14639 opaque-enum-specifier:
14640 enum-key identifier enum-base [opt] ;
14642 GNU Extensions:
14643 enum-key attributes[opt] identifier [opt] enum-base [opt]
14644 { enumerator-list [opt] }attributes[opt]
14645 enum-key attributes[opt] identifier [opt] enum-base [opt]
14646 { enumerator-list, }attributes[opt] [C++0x]
14648 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14649 if the token stream isn't an enum-specifier after all. */
14651 static tree
14652 cp_parser_enum_specifier (cp_parser* parser)
14654 tree identifier;
14655 tree type = NULL_TREE;
14656 tree prev_scope;
14657 tree nested_name_specifier = NULL_TREE;
14658 tree attributes;
14659 bool scoped_enum_p = false;
14660 bool has_underlying_type = false;
14661 bool nested_being_defined = false;
14662 bool new_value_list = false;
14663 bool is_new_type = false;
14664 bool is_anonymous = false;
14665 tree underlying_type = NULL_TREE;
14666 cp_token *type_start_token = NULL;
14667 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14669 parser->colon_corrects_to_scope_p = false;
14671 /* Parse tentatively so that we can back up if we don't find a
14672 enum-specifier. */
14673 cp_parser_parse_tentatively (parser);
14675 /* Caller guarantees that the current token is 'enum', an identifier
14676 possibly follows, and the token after that is an opening brace.
14677 If we don't have an identifier, fabricate an anonymous name for
14678 the enumeration being defined. */
14679 cp_lexer_consume_token (parser->lexer);
14681 /* Parse the "class" or "struct", which indicates a scoped
14682 enumeration type in C++0x. */
14683 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14684 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14686 if (cxx_dialect < cxx0x)
14687 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14689 /* Consume the `struct' or `class' token. */
14690 cp_lexer_consume_token (parser->lexer);
14692 scoped_enum_p = true;
14695 attributes = cp_parser_attributes_opt (parser);
14697 /* Clear the qualification. */
14698 parser->scope = NULL_TREE;
14699 parser->qualifying_scope = NULL_TREE;
14700 parser->object_scope = NULL_TREE;
14702 /* Figure out in what scope the declaration is being placed. */
14703 prev_scope = current_scope ();
14705 type_start_token = cp_lexer_peek_token (parser->lexer);
14707 push_deferring_access_checks (dk_no_check);
14708 nested_name_specifier
14709 = cp_parser_nested_name_specifier_opt (parser,
14710 /*typename_keyword_p=*/true,
14711 /*check_dependency_p=*/false,
14712 /*type_p=*/false,
14713 /*is_declaration=*/false);
14715 if (nested_name_specifier)
14717 tree name;
14719 identifier = cp_parser_identifier (parser);
14720 name = cp_parser_lookup_name (parser, identifier,
14721 enum_type,
14722 /*is_template=*/false,
14723 /*is_namespace=*/false,
14724 /*check_dependency=*/true,
14725 /*ambiguous_decls=*/NULL,
14726 input_location);
14727 if (name && name != error_mark_node)
14729 type = TREE_TYPE (name);
14730 if (TREE_CODE (type) == TYPENAME_TYPE)
14732 /* Are template enums allowed in ISO? */
14733 if (template_parm_scope_p ())
14734 pedwarn (type_start_token->location, OPT_Wpedantic,
14735 "%qD is an enumeration template", name);
14736 /* ignore a typename reference, for it will be solved by name
14737 in start_enum. */
14738 type = NULL_TREE;
14741 else
14742 error_at (type_start_token->location,
14743 "%qD is not an enumerator-name", identifier);
14745 else
14747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14748 identifier = cp_parser_identifier (parser);
14749 else
14751 identifier = make_anon_name ();
14752 is_anonymous = true;
14755 pop_deferring_access_checks ();
14757 /* Check for the `:' that denotes a specified underlying type in C++0x.
14758 Note that a ':' could also indicate a bitfield width, however. */
14759 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14761 cp_decl_specifier_seq type_specifiers;
14763 /* Consume the `:'. */
14764 cp_lexer_consume_token (parser->lexer);
14766 /* Parse the type-specifier-seq. */
14767 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14768 /*is_trailing_return=*/false,
14769 &type_specifiers);
14771 /* At this point this is surely not elaborated type specifier. */
14772 if (!cp_parser_parse_definitely (parser))
14773 return NULL_TREE;
14775 if (cxx_dialect < cxx0x)
14776 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14778 has_underlying_type = true;
14780 /* If that didn't work, stop. */
14781 if (type_specifiers.type != error_mark_node)
14783 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14784 /*initialized=*/0, NULL);
14785 if (underlying_type == error_mark_node)
14786 underlying_type = NULL_TREE;
14790 /* Look for the `{' but don't consume it yet. */
14791 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14793 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14795 cp_parser_error (parser, "expected %<{%>");
14796 if (has_underlying_type)
14798 type = NULL_TREE;
14799 goto out;
14802 /* An opaque-enum-specifier must have a ';' here. */
14803 if ((scoped_enum_p || underlying_type)
14804 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14806 cp_parser_error (parser, "expected %<;%> or %<{%>");
14807 if (has_underlying_type)
14809 type = NULL_TREE;
14810 goto out;
14815 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14816 return NULL_TREE;
14818 if (nested_name_specifier)
14820 if (CLASS_TYPE_P (nested_name_specifier))
14822 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14823 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14824 push_scope (nested_name_specifier);
14826 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14828 push_nested_namespace (nested_name_specifier);
14832 /* Issue an error message if type-definitions are forbidden here. */
14833 if (!cp_parser_check_type_definition (parser))
14834 type = error_mark_node;
14835 else
14836 /* Create the new type. We do this before consuming the opening
14837 brace so the enum will be recorded as being on the line of its
14838 tag (or the 'enum' keyword, if there is no tag). */
14839 type = start_enum (identifier, type, underlying_type,
14840 scoped_enum_p, &is_new_type);
14842 /* If the next token is not '{' it is an opaque-enum-specifier or an
14843 elaborated-type-specifier. */
14844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14846 timevar_push (TV_PARSE_ENUM);
14847 if (nested_name_specifier)
14849 /* The following catches invalid code such as:
14850 enum class S<int>::E { A, B, C }; */
14851 if (!processing_specialization
14852 && CLASS_TYPE_P (nested_name_specifier)
14853 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14854 error_at (type_start_token->location, "cannot add an enumerator "
14855 "list to a template instantiation");
14857 /* If that scope does not contain the scope in which the
14858 class was originally declared, the program is invalid. */
14859 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14861 if (at_namespace_scope_p ())
14862 error_at (type_start_token->location,
14863 "declaration of %qD in namespace %qD which does not "
14864 "enclose %qD",
14865 type, prev_scope, nested_name_specifier);
14866 else
14867 error_at (type_start_token->location,
14868 "declaration of %qD in %qD which does not enclose %qD",
14869 type, prev_scope, nested_name_specifier);
14870 type = error_mark_node;
14874 if (scoped_enum_p)
14875 begin_scope (sk_scoped_enum, type);
14877 /* Consume the opening brace. */
14878 cp_lexer_consume_token (parser->lexer);
14880 if (type == error_mark_node)
14881 ; /* Nothing to add */
14882 else if (OPAQUE_ENUM_P (type)
14883 || (cxx_dialect > cxx98 && processing_specialization))
14885 new_value_list = true;
14886 SET_OPAQUE_ENUM_P (type, false);
14887 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14889 else
14891 error_at (type_start_token->location, "multiple definition of %q#T", type);
14892 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14893 "previous definition here");
14894 type = error_mark_node;
14897 if (type == error_mark_node)
14898 cp_parser_skip_to_end_of_block_or_statement (parser);
14899 /* If the next token is not '}', then there are some enumerators. */
14900 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14901 cp_parser_enumerator_list (parser, type);
14903 /* Consume the final '}'. */
14904 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14906 if (scoped_enum_p)
14907 finish_scope ();
14908 timevar_pop (TV_PARSE_ENUM);
14910 else
14912 /* If a ';' follows, then it is an opaque-enum-specifier
14913 and additional restrictions apply. */
14914 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14916 if (is_anonymous)
14917 error_at (type_start_token->location,
14918 "opaque-enum-specifier without name");
14919 else if (nested_name_specifier)
14920 error_at (type_start_token->location,
14921 "opaque-enum-specifier must use a simple identifier");
14925 /* Look for trailing attributes to apply to this enumeration, and
14926 apply them if appropriate. */
14927 if (cp_parser_allow_gnu_extensions_p (parser))
14929 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14930 trailing_attr = chainon (trailing_attr, attributes);
14931 cplus_decl_attributes (&type,
14932 trailing_attr,
14933 (int) ATTR_FLAG_TYPE_IN_PLACE);
14936 /* Finish up the enumeration. */
14937 if (type != error_mark_node)
14939 if (new_value_list)
14940 finish_enum_value_list (type);
14941 if (is_new_type)
14942 finish_enum (type);
14945 if (nested_name_specifier)
14947 if (CLASS_TYPE_P (nested_name_specifier))
14949 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14950 pop_scope (nested_name_specifier);
14952 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14954 pop_nested_namespace (nested_name_specifier);
14957 out:
14958 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14959 return type;
14962 /* Parse an enumerator-list. The enumerators all have the indicated
14963 TYPE.
14965 enumerator-list:
14966 enumerator-definition
14967 enumerator-list , enumerator-definition */
14969 static void
14970 cp_parser_enumerator_list (cp_parser* parser, tree type)
14972 while (true)
14974 /* Parse an enumerator-definition. */
14975 cp_parser_enumerator_definition (parser, type);
14977 /* If the next token is not a ',', we've reached the end of
14978 the list. */
14979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14980 break;
14981 /* Otherwise, consume the `,' and keep going. */
14982 cp_lexer_consume_token (parser->lexer);
14983 /* If the next token is a `}', there is a trailing comma. */
14984 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14986 if (cxx_dialect < cxx0x && !in_system_header)
14987 pedwarn (input_location, OPT_Wpedantic,
14988 "comma at end of enumerator list");
14989 break;
14994 /* Parse an enumerator-definition. The enumerator has the indicated
14995 TYPE.
14997 enumerator-definition:
14998 enumerator
14999 enumerator = constant-expression
15001 enumerator:
15002 identifier */
15004 static void
15005 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15007 tree identifier;
15008 tree value;
15009 location_t loc;
15011 /* Save the input location because we are interested in the location
15012 of the identifier and not the location of the explicit value. */
15013 loc = cp_lexer_peek_token (parser->lexer)->location;
15015 /* Look for the identifier. */
15016 identifier = cp_parser_identifier (parser);
15017 if (identifier == error_mark_node)
15018 return;
15020 /* If the next token is an '=', then there is an explicit value. */
15021 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15023 /* Consume the `=' token. */
15024 cp_lexer_consume_token (parser->lexer);
15025 /* Parse the value. */
15026 value = cp_parser_constant_expression (parser,
15027 /*allow_non_constant_p=*/false,
15028 NULL);
15030 else
15031 value = NULL_TREE;
15033 /* If we are processing a template, make sure the initializer of the
15034 enumerator doesn't contain any bare template parameter pack. */
15035 if (check_for_bare_parameter_packs (value))
15036 value = error_mark_node;
15038 /* integral_constant_value will pull out this expression, so make sure
15039 it's folded as appropriate. */
15040 value = fold_non_dependent_expr (value);
15042 /* Create the enumerator. */
15043 build_enumerator (identifier, value, type, loc);
15046 /* Parse a namespace-name.
15048 namespace-name:
15049 original-namespace-name
15050 namespace-alias
15052 Returns the NAMESPACE_DECL for the namespace. */
15054 static tree
15055 cp_parser_namespace_name (cp_parser* parser)
15057 tree identifier;
15058 tree namespace_decl;
15060 cp_token *token = cp_lexer_peek_token (parser->lexer);
15062 /* Get the name of the namespace. */
15063 identifier = cp_parser_identifier (parser);
15064 if (identifier == error_mark_node)
15065 return error_mark_node;
15067 /* Look up the identifier in the currently active scope. Look only
15068 for namespaces, due to:
15070 [basic.lookup.udir]
15072 When looking up a namespace-name in a using-directive or alias
15073 definition, only namespace names are considered.
15075 And:
15077 [basic.lookup.qual]
15079 During the lookup of a name preceding the :: scope resolution
15080 operator, object, function, and enumerator names are ignored.
15082 (Note that cp_parser_qualifying_entity only calls this
15083 function if the token after the name is the scope resolution
15084 operator.) */
15085 namespace_decl = cp_parser_lookup_name (parser, identifier,
15086 none_type,
15087 /*is_template=*/false,
15088 /*is_namespace=*/true,
15089 /*check_dependency=*/true,
15090 /*ambiguous_decls=*/NULL,
15091 token->location);
15092 /* If it's not a namespace, issue an error. */
15093 if (namespace_decl == error_mark_node
15094 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15096 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15097 error_at (token->location, "%qD is not a namespace-name", identifier);
15098 cp_parser_error (parser, "expected namespace-name");
15099 namespace_decl = error_mark_node;
15102 return namespace_decl;
15105 /* Parse a namespace-definition.
15107 namespace-definition:
15108 named-namespace-definition
15109 unnamed-namespace-definition
15111 named-namespace-definition:
15112 original-namespace-definition
15113 extension-namespace-definition
15115 original-namespace-definition:
15116 namespace identifier { namespace-body }
15118 extension-namespace-definition:
15119 namespace original-namespace-name { namespace-body }
15121 unnamed-namespace-definition:
15122 namespace { namespace-body } */
15124 static void
15125 cp_parser_namespace_definition (cp_parser* parser)
15127 tree identifier, attribs;
15128 bool has_visibility;
15129 bool is_inline;
15131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15133 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15134 is_inline = true;
15135 cp_lexer_consume_token (parser->lexer);
15137 else
15138 is_inline = false;
15140 /* Look for the `namespace' keyword. */
15141 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15143 /* Get the name of the namespace. We do not attempt to distinguish
15144 between an original-namespace-definition and an
15145 extension-namespace-definition at this point. The semantic
15146 analysis routines are responsible for that. */
15147 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15148 identifier = cp_parser_identifier (parser);
15149 else
15150 identifier = NULL_TREE;
15152 /* Parse any specified attributes. */
15153 attribs = cp_parser_attributes_opt (parser);
15155 /* Look for the `{' to start the namespace. */
15156 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15157 /* Start the namespace. */
15158 push_namespace (identifier);
15160 /* "inline namespace" is equivalent to a stub namespace definition
15161 followed by a strong using directive. */
15162 if (is_inline)
15164 tree name_space = current_namespace;
15165 /* Set up namespace association. */
15166 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15167 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15168 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15169 /* Import the contents of the inline namespace. */
15170 pop_namespace ();
15171 do_using_directive (name_space);
15172 push_namespace (identifier);
15175 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15177 /* Parse the body of the namespace. */
15178 cp_parser_namespace_body (parser);
15180 if (has_visibility)
15181 pop_visibility (1);
15183 /* Finish the namespace. */
15184 pop_namespace ();
15185 /* Look for the final `}'. */
15186 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15189 /* Parse a namespace-body.
15191 namespace-body:
15192 declaration-seq [opt] */
15194 static void
15195 cp_parser_namespace_body (cp_parser* parser)
15197 cp_parser_declaration_seq_opt (parser);
15200 /* Parse a namespace-alias-definition.
15202 namespace-alias-definition:
15203 namespace identifier = qualified-namespace-specifier ; */
15205 static void
15206 cp_parser_namespace_alias_definition (cp_parser* parser)
15208 tree identifier;
15209 tree namespace_specifier;
15211 cp_token *token = cp_lexer_peek_token (parser->lexer);
15213 /* Look for the `namespace' keyword. */
15214 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15215 /* Look for the identifier. */
15216 identifier = cp_parser_identifier (parser);
15217 if (identifier == error_mark_node)
15218 return;
15219 /* Look for the `=' token. */
15220 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15221 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15223 error_at (token->location, "%<namespace%> definition is not allowed here");
15224 /* Skip the definition. */
15225 cp_lexer_consume_token (parser->lexer);
15226 if (cp_parser_skip_to_closing_brace (parser))
15227 cp_lexer_consume_token (parser->lexer);
15228 return;
15230 cp_parser_require (parser, CPP_EQ, RT_EQ);
15231 /* Look for the qualified-namespace-specifier. */
15232 namespace_specifier
15233 = cp_parser_qualified_namespace_specifier (parser);
15234 /* Look for the `;' token. */
15235 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15237 /* Register the alias in the symbol table. */
15238 do_namespace_alias (identifier, namespace_specifier);
15241 /* Parse a qualified-namespace-specifier.
15243 qualified-namespace-specifier:
15244 :: [opt] nested-name-specifier [opt] namespace-name
15246 Returns a NAMESPACE_DECL corresponding to the specified
15247 namespace. */
15249 static tree
15250 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15252 /* Look for the optional `::'. */
15253 cp_parser_global_scope_opt (parser,
15254 /*current_scope_valid_p=*/false);
15256 /* Look for the optional nested-name-specifier. */
15257 cp_parser_nested_name_specifier_opt (parser,
15258 /*typename_keyword_p=*/false,
15259 /*check_dependency_p=*/true,
15260 /*type_p=*/false,
15261 /*is_declaration=*/true);
15263 return cp_parser_namespace_name (parser);
15266 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15267 access declaration.
15269 using-declaration:
15270 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15271 using :: unqualified-id ;
15273 access-declaration:
15274 qualified-id ;
15278 static bool
15279 cp_parser_using_declaration (cp_parser* parser,
15280 bool access_declaration_p)
15282 cp_token *token;
15283 bool typename_p = false;
15284 bool global_scope_p;
15285 tree decl;
15286 tree identifier;
15287 tree qscope;
15288 int oldcount = errorcount;
15289 cp_token *diag_token = NULL;
15291 if (access_declaration_p)
15293 diag_token = cp_lexer_peek_token (parser->lexer);
15294 cp_parser_parse_tentatively (parser);
15296 else
15298 /* Look for the `using' keyword. */
15299 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15301 /* Peek at the next token. */
15302 token = cp_lexer_peek_token (parser->lexer);
15303 /* See if it's `typename'. */
15304 if (token->keyword == RID_TYPENAME)
15306 /* Remember that we've seen it. */
15307 typename_p = true;
15308 /* Consume the `typename' token. */
15309 cp_lexer_consume_token (parser->lexer);
15313 /* Look for the optional global scope qualification. */
15314 global_scope_p
15315 = (cp_parser_global_scope_opt (parser,
15316 /*current_scope_valid_p=*/false)
15317 != NULL_TREE);
15319 /* If we saw `typename', or didn't see `::', then there must be a
15320 nested-name-specifier present. */
15321 if (typename_p || !global_scope_p)
15322 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15323 /*check_dependency_p=*/true,
15324 /*type_p=*/false,
15325 /*is_declaration=*/true);
15326 /* Otherwise, we could be in either of the two productions. In that
15327 case, treat the nested-name-specifier as optional. */
15328 else
15329 qscope = cp_parser_nested_name_specifier_opt (parser,
15330 /*typename_keyword_p=*/false,
15331 /*check_dependency_p=*/true,
15332 /*type_p=*/false,
15333 /*is_declaration=*/true);
15334 if (!qscope)
15335 qscope = global_namespace;
15337 if (access_declaration_p && cp_parser_error_occurred (parser))
15338 /* Something has already gone wrong; there's no need to parse
15339 further. Since an error has occurred, the return value of
15340 cp_parser_parse_definitely will be false, as required. */
15341 return cp_parser_parse_definitely (parser);
15343 token = cp_lexer_peek_token (parser->lexer);
15344 /* Parse the unqualified-id. */
15345 identifier = cp_parser_unqualified_id (parser,
15346 /*template_keyword_p=*/false,
15347 /*check_dependency_p=*/true,
15348 /*declarator_p=*/true,
15349 /*optional_p=*/false);
15351 if (access_declaration_p)
15353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15354 cp_parser_simulate_error (parser);
15355 if (!cp_parser_parse_definitely (parser))
15356 return false;
15359 /* The function we call to handle a using-declaration is different
15360 depending on what scope we are in. */
15361 if (qscope == error_mark_node || identifier == error_mark_node)
15363 else if (!identifier_p (identifier)
15364 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15365 /* [namespace.udecl]
15367 A using declaration shall not name a template-id. */
15368 error_at (token->location,
15369 "a template-id may not appear in a using-declaration");
15370 else
15372 if (at_class_scope_p ())
15374 /* Create the USING_DECL. */
15375 decl = do_class_using_decl (parser->scope, identifier);
15377 if (decl && typename_p)
15378 USING_DECL_TYPENAME_P (decl) = 1;
15380 if (check_for_bare_parameter_packs (decl))
15381 return false;
15382 else
15383 /* Add it to the list of members in this class. */
15384 finish_member_declaration (decl);
15386 else
15388 decl = cp_parser_lookup_name_simple (parser,
15389 identifier,
15390 token->location);
15391 if (decl == error_mark_node)
15392 cp_parser_name_lookup_error (parser, identifier,
15393 decl, NLE_NULL,
15394 token->location);
15395 else if (check_for_bare_parameter_packs (decl))
15396 return false;
15397 else if (!at_namespace_scope_p ())
15398 do_local_using_decl (decl, qscope, identifier);
15399 else
15400 do_toplevel_using_decl (decl, qscope, identifier);
15404 /* Look for the final `;'. */
15405 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15407 if (access_declaration_p && errorcount == oldcount)
15408 warning_at (diag_token->location, OPT_Wdeprecated,
15409 "access declarations are deprecated "
15410 "in favour of using-declarations; "
15411 "suggestion: add the %<using%> keyword");
15413 return true;
15416 /* Parse an alias-declaration.
15418 alias-declaration:
15419 using identifier attribute-specifier-seq [opt] = type-id */
15421 static tree
15422 cp_parser_alias_declaration (cp_parser* parser)
15424 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15425 location_t id_location;
15426 cp_declarator *declarator;
15427 cp_decl_specifier_seq decl_specs;
15428 bool member_p;
15429 const char *saved_message = NULL;
15431 /* Look for the `using' keyword. */
15432 cp_token *using_token
15433 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15434 if (using_token == NULL)
15435 return error_mark_node;
15437 id_location = cp_lexer_peek_token (parser->lexer)->location;
15438 id = cp_parser_identifier (parser);
15439 if (id == error_mark_node)
15440 return error_mark_node;
15442 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15443 attributes = cp_parser_attributes_opt (parser);
15444 if (attributes == error_mark_node)
15445 return error_mark_node;
15447 cp_parser_require (parser, CPP_EQ, RT_EQ);
15449 if (cp_parser_error_occurred (parser))
15450 return error_mark_node;
15452 cp_parser_commit_to_tentative_parse (parser);
15454 /* Now we are going to parse the type-id of the declaration. */
15457 [dcl.type]/3 says:
15459 "A type-specifier-seq shall not define a class or enumeration
15460 unless it appears in the type-id of an alias-declaration (7.1.3) that
15461 is not the declaration of a template-declaration."
15463 In other words, if we currently are in an alias template, the
15464 type-id should not define a type.
15466 So let's set parser->type_definition_forbidden_message in that
15467 case; cp_parser_check_type_definition (called by
15468 cp_parser_class_specifier) will then emit an error if a type is
15469 defined in the type-id. */
15470 if (parser->num_template_parameter_lists)
15472 saved_message = parser->type_definition_forbidden_message;
15473 parser->type_definition_forbidden_message =
15474 G_("types may not be defined in alias template declarations");
15477 type = cp_parser_type_id (parser);
15479 /* Restore the error message if need be. */
15480 if (parser->num_template_parameter_lists)
15481 parser->type_definition_forbidden_message = saved_message;
15483 if (type == error_mark_node)
15485 cp_parser_skip_to_end_of_block_or_statement (parser);
15486 return error_mark_node;
15489 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15491 if (cp_parser_error_occurred (parser))
15493 cp_parser_skip_to_end_of_block_or_statement (parser);
15494 return error_mark_node;
15497 /* A typedef-name can also be introduced by an alias-declaration. The
15498 identifier following the using keyword becomes a typedef-name. It has
15499 the same semantics as if it were introduced by the typedef
15500 specifier. In particular, it does not define a new type and it shall
15501 not appear in the type-id. */
15503 clear_decl_specs (&decl_specs);
15504 decl_specs.type = type;
15505 if (attributes != NULL_TREE)
15507 decl_specs.attributes = attributes;
15508 set_and_check_decl_spec_loc (&decl_specs,
15509 ds_attribute,
15510 attrs_token);
15512 set_and_check_decl_spec_loc (&decl_specs,
15513 ds_typedef,
15514 using_token);
15515 set_and_check_decl_spec_loc (&decl_specs,
15516 ds_alias,
15517 using_token);
15519 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15520 declarator->id_loc = id_location;
15522 member_p = at_class_scope_p ();
15523 if (member_p)
15524 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15525 NULL_TREE, attributes);
15526 else
15527 decl = start_decl (declarator, &decl_specs, 0,
15528 attributes, NULL_TREE, &pushed_scope);
15529 if (decl == error_mark_node)
15530 return decl;
15532 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15534 if (pushed_scope)
15535 pop_scope (pushed_scope);
15537 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15538 added into the symbol table; otherwise, return the TYPE_DECL. */
15539 if (DECL_LANG_SPECIFIC (decl)
15540 && DECL_TEMPLATE_INFO (decl)
15541 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15543 decl = DECL_TI_TEMPLATE (decl);
15544 if (member_p)
15545 check_member_template (decl);
15548 return decl;
15551 /* Parse a using-directive.
15553 using-directive:
15554 using namespace :: [opt] nested-name-specifier [opt]
15555 namespace-name ; */
15557 static void
15558 cp_parser_using_directive (cp_parser* parser)
15560 tree namespace_decl;
15561 tree attribs;
15563 /* Look for the `using' keyword. */
15564 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15565 /* And the `namespace' keyword. */
15566 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15567 /* Look for the optional `::' operator. */
15568 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15569 /* And the optional nested-name-specifier. */
15570 cp_parser_nested_name_specifier_opt (parser,
15571 /*typename_keyword_p=*/false,
15572 /*check_dependency_p=*/true,
15573 /*type_p=*/false,
15574 /*is_declaration=*/true);
15575 /* Get the namespace being used. */
15576 namespace_decl = cp_parser_namespace_name (parser);
15577 /* And any specified attributes. */
15578 attribs = cp_parser_attributes_opt (parser);
15579 /* Update the symbol table. */
15580 parse_using_directive (namespace_decl, attribs);
15581 /* Look for the final `;'. */
15582 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15585 /* Parse an asm-definition.
15587 asm-definition:
15588 asm ( string-literal ) ;
15590 GNU Extension:
15592 asm-definition:
15593 asm volatile [opt] ( string-literal ) ;
15594 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15595 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15596 : asm-operand-list [opt] ) ;
15597 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15598 : asm-operand-list [opt]
15599 : asm-clobber-list [opt] ) ;
15600 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15601 : asm-clobber-list [opt]
15602 : asm-goto-list ) ; */
15604 static void
15605 cp_parser_asm_definition (cp_parser* parser)
15607 tree string;
15608 tree outputs = NULL_TREE;
15609 tree inputs = NULL_TREE;
15610 tree clobbers = NULL_TREE;
15611 tree labels = NULL_TREE;
15612 tree asm_stmt;
15613 bool volatile_p = false;
15614 bool extended_p = false;
15615 bool invalid_inputs_p = false;
15616 bool invalid_outputs_p = false;
15617 bool goto_p = false;
15618 required_token missing = RT_NONE;
15620 /* Look for the `asm' keyword. */
15621 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15622 /* See if the next token is `volatile'. */
15623 if (cp_parser_allow_gnu_extensions_p (parser)
15624 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15626 /* Remember that we saw the `volatile' keyword. */
15627 volatile_p = true;
15628 /* Consume the token. */
15629 cp_lexer_consume_token (parser->lexer);
15631 if (cp_parser_allow_gnu_extensions_p (parser)
15632 && parser->in_function_body
15633 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15635 /* Remember that we saw the `goto' keyword. */
15636 goto_p = true;
15637 /* Consume the token. */
15638 cp_lexer_consume_token (parser->lexer);
15640 /* Look for the opening `('. */
15641 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15642 return;
15643 /* Look for the string. */
15644 string = cp_parser_string_literal (parser, false, false);
15645 if (string == error_mark_node)
15647 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15648 /*consume_paren=*/true);
15649 return;
15652 /* If we're allowing GNU extensions, check for the extended assembly
15653 syntax. Unfortunately, the `:' tokens need not be separated by
15654 a space in C, and so, for compatibility, we tolerate that here
15655 too. Doing that means that we have to treat the `::' operator as
15656 two `:' tokens. */
15657 if (cp_parser_allow_gnu_extensions_p (parser)
15658 && parser->in_function_body
15659 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15660 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15662 bool inputs_p = false;
15663 bool clobbers_p = false;
15664 bool labels_p = false;
15666 /* The extended syntax was used. */
15667 extended_p = true;
15669 /* Look for outputs. */
15670 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15672 /* Consume the `:'. */
15673 cp_lexer_consume_token (parser->lexer);
15674 /* Parse the output-operands. */
15675 if (cp_lexer_next_token_is_not (parser->lexer,
15676 CPP_COLON)
15677 && cp_lexer_next_token_is_not (parser->lexer,
15678 CPP_SCOPE)
15679 && cp_lexer_next_token_is_not (parser->lexer,
15680 CPP_CLOSE_PAREN)
15681 && !goto_p)
15682 outputs = cp_parser_asm_operand_list (parser);
15684 if (outputs == error_mark_node)
15685 invalid_outputs_p = true;
15687 /* If the next token is `::', there are no outputs, and the
15688 next token is the beginning of the inputs. */
15689 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15690 /* The inputs are coming next. */
15691 inputs_p = true;
15693 /* Look for inputs. */
15694 if (inputs_p
15695 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15697 /* Consume the `:' or `::'. */
15698 cp_lexer_consume_token (parser->lexer);
15699 /* Parse the output-operands. */
15700 if (cp_lexer_next_token_is_not (parser->lexer,
15701 CPP_COLON)
15702 && cp_lexer_next_token_is_not (parser->lexer,
15703 CPP_SCOPE)
15704 && cp_lexer_next_token_is_not (parser->lexer,
15705 CPP_CLOSE_PAREN))
15706 inputs = cp_parser_asm_operand_list (parser);
15708 if (inputs == error_mark_node)
15709 invalid_inputs_p = true;
15711 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15712 /* The clobbers are coming next. */
15713 clobbers_p = true;
15715 /* Look for clobbers. */
15716 if (clobbers_p
15717 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15719 clobbers_p = true;
15720 /* Consume the `:' or `::'. */
15721 cp_lexer_consume_token (parser->lexer);
15722 /* Parse the clobbers. */
15723 if (cp_lexer_next_token_is_not (parser->lexer,
15724 CPP_COLON)
15725 && cp_lexer_next_token_is_not (parser->lexer,
15726 CPP_CLOSE_PAREN))
15727 clobbers = cp_parser_asm_clobber_list (parser);
15729 else if (goto_p
15730 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15731 /* The labels are coming next. */
15732 labels_p = true;
15734 /* Look for labels. */
15735 if (labels_p
15736 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15738 labels_p = true;
15739 /* Consume the `:' or `::'. */
15740 cp_lexer_consume_token (parser->lexer);
15741 /* Parse the labels. */
15742 labels = cp_parser_asm_label_list (parser);
15745 if (goto_p && !labels_p)
15746 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15748 else if (goto_p)
15749 missing = RT_COLON_SCOPE;
15751 /* Look for the closing `)'. */
15752 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15753 missing ? missing : RT_CLOSE_PAREN))
15754 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15755 /*consume_paren=*/true);
15756 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15758 if (!invalid_inputs_p && !invalid_outputs_p)
15760 /* Create the ASM_EXPR. */
15761 if (parser->in_function_body)
15763 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15764 inputs, clobbers, labels);
15765 /* If the extended syntax was not used, mark the ASM_EXPR. */
15766 if (!extended_p)
15768 tree temp = asm_stmt;
15769 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15770 temp = TREE_OPERAND (temp, 0);
15772 ASM_INPUT_P (temp) = 1;
15775 else
15776 add_asm_node (string);
15780 /* Declarators [gram.dcl.decl] */
15782 /* Parse an init-declarator.
15784 init-declarator:
15785 declarator initializer [opt]
15787 GNU Extension:
15789 init-declarator:
15790 declarator asm-specification [opt] attributes [opt] initializer [opt]
15792 function-definition:
15793 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15794 function-body
15795 decl-specifier-seq [opt] declarator function-try-block
15797 GNU Extension:
15799 function-definition:
15800 __extension__ function-definition
15802 TM Extension:
15804 function-definition:
15805 decl-specifier-seq [opt] declarator function-transaction-block
15807 The DECL_SPECIFIERS apply to this declarator. Returns a
15808 representation of the entity declared. If MEMBER_P is TRUE, then
15809 this declarator appears in a class scope. The new DECL created by
15810 this declarator is returned.
15812 The CHECKS are access checks that should be performed once we know
15813 what entity is being declared (and, therefore, what classes have
15814 befriended it).
15816 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15817 for a function-definition here as well. If the declarator is a
15818 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15819 be TRUE upon return. By that point, the function-definition will
15820 have been completely parsed.
15822 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15823 is FALSE.
15825 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15826 parsed declaration if it is an uninitialized single declarator not followed
15827 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15828 if present, will not be consumed. If returned, this declarator will be
15829 created with SD_INITIALIZED but will not call cp_finish_decl. */
15831 static tree
15832 cp_parser_init_declarator (cp_parser* parser,
15833 cp_decl_specifier_seq *decl_specifiers,
15834 vec<deferred_access_check, va_gc> *checks,
15835 bool function_definition_allowed_p,
15836 bool member_p,
15837 int declares_class_or_enum,
15838 bool* function_definition_p,
15839 tree* maybe_range_for_decl)
15841 cp_token *token = NULL, *asm_spec_start_token = NULL,
15842 *attributes_start_token = NULL;
15843 cp_declarator *declarator;
15844 tree prefix_attributes;
15845 tree attributes = NULL;
15846 tree asm_specification;
15847 tree initializer;
15848 tree decl = NULL_TREE;
15849 tree scope;
15850 int is_initialized;
15851 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15852 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15853 "(...)". */
15854 enum cpp_ttype initialization_kind;
15855 bool is_direct_init = false;
15856 bool is_non_constant_init;
15857 int ctor_dtor_or_conv_p;
15858 bool friend_p;
15859 tree pushed_scope = NULL_TREE;
15860 bool range_for_decl_p = false;
15862 /* Gather the attributes that were provided with the
15863 decl-specifiers. */
15864 prefix_attributes = decl_specifiers->attributes;
15866 /* Assume that this is not the declarator for a function
15867 definition. */
15868 if (function_definition_p)
15869 *function_definition_p = false;
15871 /* Defer access checks while parsing the declarator; we cannot know
15872 what names are accessible until we know what is being
15873 declared. */
15874 resume_deferring_access_checks ();
15876 /* Parse the declarator. */
15877 token = cp_lexer_peek_token (parser->lexer);
15878 declarator
15879 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15880 &ctor_dtor_or_conv_p,
15881 /*parenthesized_p=*/NULL,
15882 member_p);
15883 /* Gather up the deferred checks. */
15884 stop_deferring_access_checks ();
15886 /* If the DECLARATOR was erroneous, there's no need to go
15887 further. */
15888 if (declarator == cp_error_declarator)
15889 return error_mark_node;
15891 /* Check that the number of template-parameter-lists is OK. */
15892 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15893 token->location))
15894 return error_mark_node;
15896 if (declares_class_or_enum & 2)
15897 cp_parser_check_for_definition_in_return_type (declarator,
15898 decl_specifiers->type,
15899 decl_specifiers->locations[ds_type_spec]);
15901 /* Figure out what scope the entity declared by the DECLARATOR is
15902 located in. `grokdeclarator' sometimes changes the scope, so
15903 we compute it now. */
15904 scope = get_scope_of_declarator (declarator);
15906 /* Perform any lookups in the declared type which were thought to be
15907 dependent, but are not in the scope of the declarator. */
15908 decl_specifiers->type
15909 = maybe_update_decl_type (decl_specifiers->type, scope);
15911 /* If we're allowing GNU extensions, look for an
15912 asm-specification. */
15913 if (cp_parser_allow_gnu_extensions_p (parser))
15915 /* Look for an asm-specification. */
15916 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15917 asm_specification = cp_parser_asm_specification_opt (parser);
15919 else
15920 asm_specification = NULL_TREE;
15922 /* Look for attributes. */
15923 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15924 attributes = cp_parser_attributes_opt (parser);
15926 /* Peek at the next token. */
15927 token = cp_lexer_peek_token (parser->lexer);
15928 /* Check to see if the token indicates the start of a
15929 function-definition. */
15930 if (function_declarator_p (declarator)
15931 && cp_parser_token_starts_function_definition_p (token))
15933 if (!function_definition_allowed_p)
15935 /* If a function-definition should not appear here, issue an
15936 error message. */
15937 cp_parser_error (parser,
15938 "a function-definition is not allowed here");
15939 return error_mark_node;
15941 else
15943 location_t func_brace_location
15944 = cp_lexer_peek_token (parser->lexer)->location;
15946 /* Neither attributes nor an asm-specification are allowed
15947 on a function-definition. */
15948 if (asm_specification)
15949 error_at (asm_spec_start_token->location,
15950 "an asm-specification is not allowed "
15951 "on a function-definition");
15952 if (attributes)
15953 error_at (attributes_start_token->location,
15954 "attributes are not allowed on a function-definition");
15955 /* This is a function-definition. */
15956 *function_definition_p = true;
15958 /* Parse the function definition. */
15959 if (member_p)
15960 decl = cp_parser_save_member_function_body (parser,
15961 decl_specifiers,
15962 declarator,
15963 prefix_attributes);
15964 else
15965 decl
15966 = (cp_parser_function_definition_from_specifiers_and_declarator
15967 (parser, decl_specifiers, prefix_attributes, declarator));
15969 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15971 /* This is where the prologue starts... */
15972 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15973 = func_brace_location;
15976 return decl;
15980 /* [dcl.dcl]
15982 Only in function declarations for constructors, destructors, and
15983 type conversions can the decl-specifier-seq be omitted.
15985 We explicitly postpone this check past the point where we handle
15986 function-definitions because we tolerate function-definitions
15987 that are missing their return types in some modes. */
15988 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15990 cp_parser_error (parser,
15991 "expected constructor, destructor, or type conversion");
15992 return error_mark_node;
15995 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15996 if (token->type == CPP_EQ
15997 || token->type == CPP_OPEN_PAREN
15998 || token->type == CPP_OPEN_BRACE)
16000 is_initialized = SD_INITIALIZED;
16001 initialization_kind = token->type;
16002 if (maybe_range_for_decl)
16003 *maybe_range_for_decl = error_mark_node;
16005 if (token->type == CPP_EQ
16006 && function_declarator_p (declarator))
16008 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16009 if (t2->keyword == RID_DEFAULT)
16010 is_initialized = SD_DEFAULTED;
16011 else if (t2->keyword == RID_DELETE)
16012 is_initialized = SD_DELETED;
16015 else
16017 /* If the init-declarator isn't initialized and isn't followed by a
16018 `,' or `;', it's not a valid init-declarator. */
16019 if (token->type != CPP_COMMA
16020 && token->type != CPP_SEMICOLON)
16022 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16023 range_for_decl_p = true;
16024 else
16026 cp_parser_error (parser, "expected initializer");
16027 return error_mark_node;
16030 is_initialized = SD_UNINITIALIZED;
16031 initialization_kind = CPP_EOF;
16034 /* Because start_decl has side-effects, we should only call it if we
16035 know we're going ahead. By this point, we know that we cannot
16036 possibly be looking at any other construct. */
16037 cp_parser_commit_to_tentative_parse (parser);
16039 /* If the decl specifiers were bad, issue an error now that we're
16040 sure this was intended to be a declarator. Then continue
16041 declaring the variable(s), as int, to try to cut down on further
16042 errors. */
16043 if (decl_specifiers->any_specifiers_p
16044 && decl_specifiers->type == error_mark_node)
16046 cp_parser_error (parser, "invalid type in declaration");
16047 decl_specifiers->type = integer_type_node;
16050 /* Check to see whether or not this declaration is a friend. */
16051 friend_p = cp_parser_friend_p (decl_specifiers);
16053 /* Enter the newly declared entry in the symbol table. If we're
16054 processing a declaration in a class-specifier, we wait until
16055 after processing the initializer. */
16056 if (!member_p)
16058 if (parser->in_unbraced_linkage_specification_p)
16059 decl_specifiers->storage_class = sc_extern;
16060 decl = start_decl (declarator, decl_specifiers,
16061 range_for_decl_p? SD_INITIALIZED : is_initialized,
16062 attributes, prefix_attributes,
16063 &pushed_scope);
16064 /* Adjust location of decl if declarator->id_loc is more appropriate:
16065 set, and decl wasn't merged with another decl, in which case its
16066 location would be different from input_location, and more accurate. */
16067 if (DECL_P (decl)
16068 && declarator->id_loc != UNKNOWN_LOCATION
16069 && DECL_SOURCE_LOCATION (decl) == input_location)
16070 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16072 else if (scope)
16073 /* Enter the SCOPE. That way unqualified names appearing in the
16074 initializer will be looked up in SCOPE. */
16075 pushed_scope = push_scope (scope);
16077 /* Perform deferred access control checks, now that we know in which
16078 SCOPE the declared entity resides. */
16079 if (!member_p && decl)
16081 tree saved_current_function_decl = NULL_TREE;
16083 /* If the entity being declared is a function, pretend that we
16084 are in its scope. If it is a `friend', it may have access to
16085 things that would not otherwise be accessible. */
16086 if (TREE_CODE (decl) == FUNCTION_DECL)
16088 saved_current_function_decl = current_function_decl;
16089 current_function_decl = decl;
16092 /* Perform access checks for template parameters. */
16093 cp_parser_perform_template_parameter_access_checks (checks);
16095 /* Perform the access control checks for the declarator and the
16096 decl-specifiers. */
16097 perform_deferred_access_checks (tf_warning_or_error);
16099 /* Restore the saved value. */
16100 if (TREE_CODE (decl) == FUNCTION_DECL)
16101 current_function_decl = saved_current_function_decl;
16104 /* Parse the initializer. */
16105 initializer = NULL_TREE;
16106 is_direct_init = false;
16107 is_non_constant_init = true;
16108 if (is_initialized)
16110 if (function_declarator_p (declarator))
16112 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16113 if (initialization_kind == CPP_EQ)
16114 initializer = cp_parser_pure_specifier (parser);
16115 else
16117 /* If the declaration was erroneous, we don't really
16118 know what the user intended, so just silently
16119 consume the initializer. */
16120 if (decl != error_mark_node)
16121 error_at (initializer_start_token->location,
16122 "initializer provided for function");
16123 cp_parser_skip_to_closing_parenthesis (parser,
16124 /*recovering=*/true,
16125 /*or_comma=*/false,
16126 /*consume_paren=*/true);
16129 else
16131 /* We want to record the extra mangling scope for in-class
16132 initializers of class members and initializers of static data
16133 member templates. The former involves deferring
16134 parsing of the initializer until end of class as with default
16135 arguments. So right here we only handle the latter. */
16136 if (!member_p && processing_template_decl)
16137 start_lambda_scope (decl);
16138 initializer = cp_parser_initializer (parser,
16139 &is_direct_init,
16140 &is_non_constant_init);
16141 if (!member_p && processing_template_decl)
16142 finish_lambda_scope ();
16143 if (initializer == error_mark_node)
16144 cp_parser_skip_to_end_of_statement (parser);
16148 /* The old parser allows attributes to appear after a parenthesized
16149 initializer. Mark Mitchell proposed removing this functionality
16150 on the GCC mailing lists on 2002-08-13. This parser accepts the
16151 attributes -- but ignores them. */
16152 if (cp_parser_allow_gnu_extensions_p (parser)
16153 && initialization_kind == CPP_OPEN_PAREN)
16154 if (cp_parser_attributes_opt (parser))
16155 warning (OPT_Wattributes,
16156 "attributes after parenthesized initializer ignored");
16158 /* For an in-class declaration, use `grokfield' to create the
16159 declaration. */
16160 if (member_p)
16162 if (pushed_scope)
16164 pop_scope (pushed_scope);
16165 pushed_scope = NULL_TREE;
16167 decl = grokfield (declarator, decl_specifiers,
16168 initializer, !is_non_constant_init,
16169 /*asmspec=*/NULL_TREE,
16170 prefix_attributes);
16171 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16172 cp_parser_save_default_args (parser, decl);
16175 /* Finish processing the declaration. But, skip member
16176 declarations. */
16177 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16179 cp_finish_decl (decl,
16180 initializer, !is_non_constant_init,
16181 asm_specification,
16182 /* If the initializer is in parentheses, then this is
16183 a direct-initialization, which means that an
16184 `explicit' constructor is OK. Otherwise, an
16185 `explicit' constructor cannot be used. */
16186 ((is_direct_init || !is_initialized)
16187 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16189 else if ((cxx_dialect != cxx98) && friend_p
16190 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16191 /* Core issue #226 (C++0x only): A default template-argument
16192 shall not be specified in a friend class template
16193 declaration. */
16194 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16195 /*is_partial=*/false, /*is_friend_decl=*/1);
16197 if (!friend_p && pushed_scope)
16198 pop_scope (pushed_scope);
16200 return decl;
16203 /* Parse a declarator.
16205 declarator:
16206 direct-declarator
16207 ptr-operator declarator
16209 abstract-declarator:
16210 ptr-operator abstract-declarator [opt]
16211 direct-abstract-declarator
16213 GNU Extensions:
16215 declarator:
16216 attributes [opt] direct-declarator
16217 attributes [opt] ptr-operator declarator
16219 abstract-declarator:
16220 attributes [opt] ptr-operator abstract-declarator [opt]
16221 attributes [opt] direct-abstract-declarator
16223 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16224 detect constructor, destructor or conversion operators. It is set
16225 to -1 if the declarator is a name, and +1 if it is a
16226 function. Otherwise it is set to zero. Usually you just want to
16227 test for >0, but internally the negative value is used.
16229 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16230 a decl-specifier-seq unless it declares a constructor, destructor,
16231 or conversion. It might seem that we could check this condition in
16232 semantic analysis, rather than parsing, but that makes it difficult
16233 to handle something like `f()'. We want to notice that there are
16234 no decl-specifiers, and therefore realize that this is an
16235 expression, not a declaration.)
16237 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16238 the declarator is a direct-declarator of the form "(...)".
16240 MEMBER_P is true iff this declarator is a member-declarator. */
16242 static cp_declarator *
16243 cp_parser_declarator (cp_parser* parser,
16244 cp_parser_declarator_kind dcl_kind,
16245 int* ctor_dtor_or_conv_p,
16246 bool* parenthesized_p,
16247 bool member_p)
16249 cp_declarator *declarator;
16250 enum tree_code code;
16251 cp_cv_quals cv_quals;
16252 tree class_type;
16253 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16255 /* Assume this is not a constructor, destructor, or type-conversion
16256 operator. */
16257 if (ctor_dtor_or_conv_p)
16258 *ctor_dtor_or_conv_p = 0;
16260 if (cp_parser_allow_gnu_extensions_p (parser))
16261 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16263 /* Check for the ptr-operator production. */
16264 cp_parser_parse_tentatively (parser);
16265 /* Parse the ptr-operator. */
16266 code = cp_parser_ptr_operator (parser,
16267 &class_type,
16268 &cv_quals,
16269 &std_attributes);
16271 /* If that worked, then we have a ptr-operator. */
16272 if (cp_parser_parse_definitely (parser))
16274 /* If a ptr-operator was found, then this declarator was not
16275 parenthesized. */
16276 if (parenthesized_p)
16277 *parenthesized_p = true;
16278 /* The dependent declarator is optional if we are parsing an
16279 abstract-declarator. */
16280 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16281 cp_parser_parse_tentatively (parser);
16283 /* Parse the dependent declarator. */
16284 declarator = cp_parser_declarator (parser, dcl_kind,
16285 /*ctor_dtor_or_conv_p=*/NULL,
16286 /*parenthesized_p=*/NULL,
16287 /*member_p=*/false);
16289 /* If we are parsing an abstract-declarator, we must handle the
16290 case where the dependent declarator is absent. */
16291 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16292 && !cp_parser_parse_definitely (parser))
16293 declarator = NULL;
16295 declarator = cp_parser_make_indirect_declarator
16296 (code, class_type, cv_quals, declarator, std_attributes);
16298 /* Everything else is a direct-declarator. */
16299 else
16301 if (parenthesized_p)
16302 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16303 CPP_OPEN_PAREN);
16304 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16305 ctor_dtor_or_conv_p,
16306 member_p);
16309 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16310 declarator->attributes = gnu_attributes;
16311 return declarator;
16314 /* Parse a direct-declarator or direct-abstract-declarator.
16316 direct-declarator:
16317 declarator-id
16318 direct-declarator ( parameter-declaration-clause )
16319 cv-qualifier-seq [opt]
16320 ref-qualifier [opt]
16321 exception-specification [opt]
16322 direct-declarator [ constant-expression [opt] ]
16323 ( declarator )
16325 direct-abstract-declarator:
16326 direct-abstract-declarator [opt]
16327 ( parameter-declaration-clause )
16328 cv-qualifier-seq [opt]
16329 ref-qualifier [opt]
16330 exception-specification [opt]
16331 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16332 ( abstract-declarator )
16334 Returns a representation of the declarator. DCL_KIND is
16335 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16336 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16337 we are parsing a direct-declarator. It is
16338 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16339 of ambiguity we prefer an abstract declarator, as per
16340 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16341 cp_parser_declarator. */
16343 static cp_declarator *
16344 cp_parser_direct_declarator (cp_parser* parser,
16345 cp_parser_declarator_kind dcl_kind,
16346 int* ctor_dtor_or_conv_p,
16347 bool member_p)
16349 cp_token *token;
16350 cp_declarator *declarator = NULL;
16351 tree scope = NULL_TREE;
16352 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16353 bool saved_in_declarator_p = parser->in_declarator_p;
16354 bool first = true;
16355 tree pushed_scope = NULL_TREE;
16357 while (true)
16359 /* Peek at the next token. */
16360 token = cp_lexer_peek_token (parser->lexer);
16361 if (token->type == CPP_OPEN_PAREN)
16363 /* This is either a parameter-declaration-clause, or a
16364 parenthesized declarator. When we know we are parsing a
16365 named declarator, it must be a parenthesized declarator
16366 if FIRST is true. For instance, `(int)' is a
16367 parameter-declaration-clause, with an omitted
16368 direct-abstract-declarator. But `((*))', is a
16369 parenthesized abstract declarator. Finally, when T is a
16370 template parameter `(T)' is a
16371 parameter-declaration-clause, and not a parenthesized
16372 named declarator.
16374 We first try and parse a parameter-declaration-clause,
16375 and then try a nested declarator (if FIRST is true).
16377 It is not an error for it not to be a
16378 parameter-declaration-clause, even when FIRST is
16379 false. Consider,
16381 int i (int);
16382 int i (3);
16384 The first is the declaration of a function while the
16385 second is the definition of a variable, including its
16386 initializer.
16388 Having seen only the parenthesis, we cannot know which of
16389 these two alternatives should be selected. Even more
16390 complex are examples like:
16392 int i (int (a));
16393 int i (int (3));
16395 The former is a function-declaration; the latter is a
16396 variable initialization.
16398 Thus again, we try a parameter-declaration-clause, and if
16399 that fails, we back out and return. */
16401 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16403 tree params;
16404 unsigned saved_num_template_parameter_lists;
16405 bool is_declarator = false;
16406 tree t;
16408 /* In a member-declarator, the only valid interpretation
16409 of a parenthesis is the start of a
16410 parameter-declaration-clause. (It is invalid to
16411 initialize a static data member with a parenthesized
16412 initializer; only the "=" form of initialization is
16413 permitted.) */
16414 if (!member_p)
16415 cp_parser_parse_tentatively (parser);
16417 /* Consume the `('. */
16418 cp_lexer_consume_token (parser->lexer);
16419 if (first)
16421 /* If this is going to be an abstract declarator, we're
16422 in a declarator and we can't have default args. */
16423 parser->default_arg_ok_p = false;
16424 parser->in_declarator_p = true;
16427 /* Inside the function parameter list, surrounding
16428 template-parameter-lists do not apply. */
16429 saved_num_template_parameter_lists
16430 = parser->num_template_parameter_lists;
16431 parser->num_template_parameter_lists = 0;
16433 begin_scope (sk_function_parms, NULL_TREE);
16435 /* Parse the parameter-declaration-clause. */
16436 params = cp_parser_parameter_declaration_clause (parser);
16438 parser->num_template_parameter_lists
16439 = saved_num_template_parameter_lists;
16441 /* Consume the `)'. */
16442 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16444 /* If all went well, parse the cv-qualifier-seq,
16445 ref-qualifier and the exception-specification. */
16446 if (member_p || cp_parser_parse_definitely (parser))
16448 cp_cv_quals cv_quals;
16449 cp_virt_specifiers virt_specifiers;
16450 cp_ref_qualifier ref_qual;
16451 tree exception_specification;
16452 tree late_return;
16453 tree attrs;
16454 bool memfn = (member_p || (pushed_scope
16455 && CLASS_TYPE_P (pushed_scope)));
16457 is_declarator = true;
16459 if (ctor_dtor_or_conv_p)
16460 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16461 first = false;
16463 /* Parse the cv-qualifier-seq. */
16464 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16465 /* Parse the ref-qualifier. */
16466 ref_qual = cp_parser_ref_qualifier_opt (parser);
16467 /* And the exception-specification. */
16468 exception_specification
16469 = cp_parser_exception_specification_opt (parser);
16471 attrs = cp_parser_std_attribute_spec_seq (parser);
16473 late_return = (cp_parser_late_return_type_opt
16474 (parser, memfn ? cv_quals : -1));
16476 /* Parse the virt-specifier-seq. */
16477 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16479 /* Create the function-declarator. */
16480 declarator = make_call_declarator (declarator,
16481 params,
16482 cv_quals,
16483 virt_specifiers,
16484 ref_qual,
16485 exception_specification,
16486 late_return);
16487 declarator->std_attributes = attrs;
16488 /* Any subsequent parameter lists are to do with
16489 return type, so are not those of the declared
16490 function. */
16491 parser->default_arg_ok_p = false;
16494 /* Remove the function parms from scope. */
16495 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16496 pop_binding (DECL_NAME (t), t);
16497 leave_scope();
16499 if (is_declarator)
16500 /* Repeat the main loop. */
16501 continue;
16504 /* If this is the first, we can try a parenthesized
16505 declarator. */
16506 if (first)
16508 bool saved_in_type_id_in_expr_p;
16510 parser->default_arg_ok_p = saved_default_arg_ok_p;
16511 parser->in_declarator_p = saved_in_declarator_p;
16513 /* Consume the `('. */
16514 cp_lexer_consume_token (parser->lexer);
16515 /* Parse the nested declarator. */
16516 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16517 parser->in_type_id_in_expr_p = true;
16518 declarator
16519 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16520 /*parenthesized_p=*/NULL,
16521 member_p);
16522 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16523 first = false;
16524 /* Expect a `)'. */
16525 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16526 declarator = cp_error_declarator;
16527 if (declarator == cp_error_declarator)
16528 break;
16530 goto handle_declarator;
16532 /* Otherwise, we must be done. */
16533 else
16534 break;
16536 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16537 && token->type == CPP_OPEN_SQUARE
16538 && !cp_next_tokens_can_be_attribute_p (parser))
16540 /* Parse an array-declarator. */
16541 tree bounds, attrs;
16543 if (ctor_dtor_or_conv_p)
16544 *ctor_dtor_or_conv_p = 0;
16546 first = false;
16547 parser->default_arg_ok_p = false;
16548 parser->in_declarator_p = true;
16549 /* Consume the `['. */
16550 cp_lexer_consume_token (parser->lexer);
16551 /* Peek at the next token. */
16552 token = cp_lexer_peek_token (parser->lexer);
16553 /* If the next token is `]', then there is no
16554 constant-expression. */
16555 if (token->type != CPP_CLOSE_SQUARE)
16557 bool non_constant_p;
16559 bounds
16560 = cp_parser_constant_expression (parser,
16561 /*allow_non_constant=*/true,
16562 &non_constant_p);
16563 if (!non_constant_p)
16564 /* OK */;
16565 else if (error_operand_p (bounds))
16566 /* Already gave an error. */;
16567 else if (!parser->in_function_body
16568 || current_binding_level->kind == sk_function_parms)
16570 /* Normally, the array bound must be an integral constant
16571 expression. However, as an extension, we allow VLAs
16572 in function scopes as long as they aren't part of a
16573 parameter declaration. */
16574 cp_parser_error (parser,
16575 "array bound is not an integer constant");
16576 bounds = error_mark_node;
16578 else if (processing_template_decl)
16580 /* Remember this wasn't a constant-expression. */
16581 bounds = build_nop (TREE_TYPE (bounds), bounds);
16582 TREE_SIDE_EFFECTS (bounds) = 1;
16585 else
16586 bounds = NULL_TREE;
16587 /* Look for the closing `]'. */
16588 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16590 declarator = cp_error_declarator;
16591 break;
16594 attrs = cp_parser_std_attribute_spec_seq (parser);
16595 declarator = make_array_declarator (declarator, bounds);
16596 declarator->std_attributes = attrs;
16598 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16601 tree qualifying_scope;
16602 tree unqualified_name;
16603 tree attrs;
16604 special_function_kind sfk;
16605 bool abstract_ok;
16606 bool pack_expansion_p = false;
16607 cp_token *declarator_id_start_token;
16609 /* Parse a declarator-id */
16610 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16611 if (abstract_ok)
16613 cp_parser_parse_tentatively (parser);
16615 /* If we see an ellipsis, we should be looking at a
16616 parameter pack. */
16617 if (token->type == CPP_ELLIPSIS)
16619 /* Consume the `...' */
16620 cp_lexer_consume_token (parser->lexer);
16622 pack_expansion_p = true;
16626 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16627 unqualified_name
16628 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16629 qualifying_scope = parser->scope;
16630 if (abstract_ok)
16632 bool okay = false;
16634 if (!unqualified_name && pack_expansion_p)
16636 /* Check whether an error occurred. */
16637 okay = !cp_parser_error_occurred (parser);
16639 /* We already consumed the ellipsis to mark a
16640 parameter pack, but we have no way to report it,
16641 so abort the tentative parse. We will be exiting
16642 immediately anyway. */
16643 cp_parser_abort_tentative_parse (parser);
16645 else
16646 okay = cp_parser_parse_definitely (parser);
16648 if (!okay)
16649 unqualified_name = error_mark_node;
16650 else if (unqualified_name
16651 && (qualifying_scope
16652 || (!identifier_p (unqualified_name))))
16654 cp_parser_error (parser, "expected unqualified-id");
16655 unqualified_name = error_mark_node;
16659 if (!unqualified_name)
16660 return NULL;
16661 if (unqualified_name == error_mark_node)
16663 declarator = cp_error_declarator;
16664 pack_expansion_p = false;
16665 declarator->parameter_pack_p = false;
16666 break;
16669 attrs = cp_parser_std_attribute_spec_seq (parser);
16671 if (qualifying_scope && at_namespace_scope_p ()
16672 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16674 /* In the declaration of a member of a template class
16675 outside of the class itself, the SCOPE will sometimes
16676 be a TYPENAME_TYPE. For example, given:
16678 template <typename T>
16679 int S<T>::R::i = 3;
16681 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16682 this context, we must resolve S<T>::R to an ordinary
16683 type, rather than a typename type.
16685 The reason we normally avoid resolving TYPENAME_TYPEs
16686 is that a specialization of `S' might render
16687 `S<T>::R' not a type. However, if `S' is
16688 specialized, then this `i' will not be used, so there
16689 is no harm in resolving the types here. */
16690 tree type;
16692 /* Resolve the TYPENAME_TYPE. */
16693 type = resolve_typename_type (qualifying_scope,
16694 /*only_current_p=*/false);
16695 /* If that failed, the declarator is invalid. */
16696 if (TREE_CODE (type) == TYPENAME_TYPE)
16698 if (typedef_variant_p (type))
16699 error_at (declarator_id_start_token->location,
16700 "cannot define member of dependent typedef "
16701 "%qT", type);
16702 else
16703 error_at (declarator_id_start_token->location,
16704 "%<%T::%E%> is not a type",
16705 TYPE_CONTEXT (qualifying_scope),
16706 TYPE_IDENTIFIER (qualifying_scope));
16708 qualifying_scope = type;
16711 sfk = sfk_none;
16713 if (unqualified_name)
16715 tree class_type;
16717 if (qualifying_scope
16718 && CLASS_TYPE_P (qualifying_scope))
16719 class_type = qualifying_scope;
16720 else
16721 class_type = current_class_type;
16723 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16725 tree name_type = TREE_TYPE (unqualified_name);
16726 if (class_type && same_type_p (name_type, class_type))
16728 if (qualifying_scope
16729 && CLASSTYPE_USE_TEMPLATE (name_type))
16731 error_at (declarator_id_start_token->location,
16732 "invalid use of constructor as a template");
16733 inform (declarator_id_start_token->location,
16734 "use %<%T::%D%> instead of %<%T::%D%> to "
16735 "name the constructor in a qualified name",
16736 class_type,
16737 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16738 class_type, name_type);
16739 declarator = cp_error_declarator;
16740 break;
16742 else
16743 unqualified_name = constructor_name (class_type);
16745 else
16747 /* We do not attempt to print the declarator
16748 here because we do not have enough
16749 information about its original syntactic
16750 form. */
16751 cp_parser_error (parser, "invalid declarator");
16752 declarator = cp_error_declarator;
16753 break;
16757 if (class_type)
16759 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16760 sfk = sfk_destructor;
16761 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16762 sfk = sfk_conversion;
16763 else if (/* There's no way to declare a constructor
16764 for an anonymous type, even if the type
16765 got a name for linkage purposes. */
16766 !TYPE_WAS_ANONYMOUS (class_type)
16767 && constructor_name_p (unqualified_name,
16768 class_type))
16770 unqualified_name = constructor_name (class_type);
16771 sfk = sfk_constructor;
16773 else if (is_overloaded_fn (unqualified_name)
16774 && DECL_CONSTRUCTOR_P (get_first_fn
16775 (unqualified_name)))
16776 sfk = sfk_constructor;
16778 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16779 *ctor_dtor_or_conv_p = -1;
16782 declarator = make_id_declarator (qualifying_scope,
16783 unqualified_name,
16784 sfk);
16785 declarator->std_attributes = attrs;
16786 declarator->id_loc = token->location;
16787 declarator->parameter_pack_p = pack_expansion_p;
16789 if (pack_expansion_p)
16790 maybe_warn_variadic_templates ();
16793 handle_declarator:;
16794 scope = get_scope_of_declarator (declarator);
16795 if (scope)
16797 /* Any names that appear after the declarator-id for a
16798 member are looked up in the containing scope. */
16799 if (at_function_scope_p ())
16801 /* But declarations with qualified-ids can't appear in a
16802 function. */
16803 cp_parser_error (parser, "qualified-id in declaration");
16804 break;
16806 pushed_scope = push_scope (scope);
16808 parser->in_declarator_p = true;
16809 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16810 || (declarator && declarator->kind == cdk_id))
16811 /* Default args are only allowed on function
16812 declarations. */
16813 parser->default_arg_ok_p = saved_default_arg_ok_p;
16814 else
16815 parser->default_arg_ok_p = false;
16817 first = false;
16819 /* We're done. */
16820 else
16821 break;
16824 /* For an abstract declarator, we might wind up with nothing at this
16825 point. That's an error; the declarator is not optional. */
16826 if (!declarator)
16827 cp_parser_error (parser, "expected declarator");
16829 /* If we entered a scope, we must exit it now. */
16830 if (pushed_scope)
16831 pop_scope (pushed_scope);
16833 parser->default_arg_ok_p = saved_default_arg_ok_p;
16834 parser->in_declarator_p = saved_in_declarator_p;
16836 return declarator;
16839 /* Parse a ptr-operator.
16841 ptr-operator:
16842 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16843 * cv-qualifier-seq [opt]
16845 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16846 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16848 GNU Extension:
16850 ptr-operator:
16851 & cv-qualifier-seq [opt]
16853 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16854 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16855 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16856 filled in with the TYPE containing the member. *CV_QUALS is
16857 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16858 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16859 Note that the tree codes returned by this function have nothing
16860 to do with the types of trees that will be eventually be created
16861 to represent the pointer or reference type being parsed. They are
16862 just constants with suggestive names. */
16863 static enum tree_code
16864 cp_parser_ptr_operator (cp_parser* parser,
16865 tree* type,
16866 cp_cv_quals *cv_quals,
16867 tree *attributes)
16869 enum tree_code code = ERROR_MARK;
16870 cp_token *token;
16871 tree attrs = NULL_TREE;
16873 /* Assume that it's not a pointer-to-member. */
16874 *type = NULL_TREE;
16875 /* And that there are no cv-qualifiers. */
16876 *cv_quals = TYPE_UNQUALIFIED;
16878 /* Peek at the next token. */
16879 token = cp_lexer_peek_token (parser->lexer);
16881 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16882 if (token->type == CPP_MULT)
16883 code = INDIRECT_REF;
16884 else if (token->type == CPP_AND)
16885 code = ADDR_EXPR;
16886 else if ((cxx_dialect != cxx98) &&
16887 token->type == CPP_AND_AND) /* C++0x only */
16888 code = NON_LVALUE_EXPR;
16890 if (code != ERROR_MARK)
16892 /* Consume the `*', `&' or `&&'. */
16893 cp_lexer_consume_token (parser->lexer);
16895 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16896 `&', if we are allowing GNU extensions. (The only qualifier
16897 that can legally appear after `&' is `restrict', but that is
16898 enforced during semantic analysis. */
16899 if (code == INDIRECT_REF
16900 || cp_parser_allow_gnu_extensions_p (parser))
16901 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16903 attrs = cp_parser_std_attribute_spec_seq (parser);
16904 if (attributes != NULL)
16905 *attributes = attrs;
16907 else
16909 /* Try the pointer-to-member case. */
16910 cp_parser_parse_tentatively (parser);
16911 /* Look for the optional `::' operator. */
16912 cp_parser_global_scope_opt (parser,
16913 /*current_scope_valid_p=*/false);
16914 /* Look for the nested-name specifier. */
16915 token = cp_lexer_peek_token (parser->lexer);
16916 cp_parser_nested_name_specifier (parser,
16917 /*typename_keyword_p=*/false,
16918 /*check_dependency_p=*/true,
16919 /*type_p=*/false,
16920 /*is_declaration=*/false);
16921 /* If we found it, and the next token is a `*', then we are
16922 indeed looking at a pointer-to-member operator. */
16923 if (!cp_parser_error_occurred (parser)
16924 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16926 /* Indicate that the `*' operator was used. */
16927 code = INDIRECT_REF;
16929 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16930 error_at (token->location, "%qD is a namespace", parser->scope);
16931 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16932 error_at (token->location, "cannot form pointer to member of "
16933 "non-class %q#T", parser->scope);
16934 else
16936 /* The type of which the member is a member is given by the
16937 current SCOPE. */
16938 *type = parser->scope;
16939 /* The next name will not be qualified. */
16940 parser->scope = NULL_TREE;
16941 parser->qualifying_scope = NULL_TREE;
16942 parser->object_scope = NULL_TREE;
16943 /* Look for optional c++11 attributes. */
16944 attrs = cp_parser_std_attribute_spec_seq (parser);
16945 if (attributes != NULL)
16946 *attributes = attrs;
16947 /* Look for the optional cv-qualifier-seq. */
16948 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16951 /* If that didn't work we don't have a ptr-operator. */
16952 if (!cp_parser_parse_definitely (parser))
16953 cp_parser_error (parser, "expected ptr-operator");
16956 return code;
16959 /* Parse an (optional) cv-qualifier-seq.
16961 cv-qualifier-seq:
16962 cv-qualifier cv-qualifier-seq [opt]
16964 cv-qualifier:
16965 const
16966 volatile
16968 GNU Extension:
16970 cv-qualifier:
16971 __restrict__
16973 Returns a bitmask representing the cv-qualifiers. */
16975 static cp_cv_quals
16976 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16978 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16980 while (true)
16982 cp_token *token;
16983 cp_cv_quals cv_qualifier;
16985 /* Peek at the next token. */
16986 token = cp_lexer_peek_token (parser->lexer);
16987 /* See if it's a cv-qualifier. */
16988 switch (token->keyword)
16990 case RID_CONST:
16991 cv_qualifier = TYPE_QUAL_CONST;
16992 break;
16994 case RID_VOLATILE:
16995 cv_qualifier = TYPE_QUAL_VOLATILE;
16996 break;
16998 case RID_RESTRICT:
16999 cv_qualifier = TYPE_QUAL_RESTRICT;
17000 break;
17002 default:
17003 cv_qualifier = TYPE_UNQUALIFIED;
17004 break;
17007 if (!cv_qualifier)
17008 break;
17010 if (cv_quals & cv_qualifier)
17012 error_at (token->location, "duplicate cv-qualifier");
17013 cp_lexer_purge_token (parser->lexer);
17015 else
17017 cp_lexer_consume_token (parser->lexer);
17018 cv_quals |= cv_qualifier;
17022 return cv_quals;
17025 /* Parse an (optional) ref-qualifier
17027 ref-qualifier:
17031 Returns cp_ref_qualifier representing ref-qualifier. */
17033 static cp_ref_qualifier
17034 cp_parser_ref_qualifier_opt (cp_parser* parser)
17036 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17038 while (true)
17040 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17041 cp_token *token = cp_lexer_peek_token (parser->lexer);
17043 switch (token->type)
17045 case CPP_AND:
17046 curr_ref_qual = REF_QUAL_LVALUE;
17047 break;
17049 case CPP_AND_AND:
17050 curr_ref_qual = REF_QUAL_RVALUE;
17051 break;
17053 default:
17054 curr_ref_qual = REF_QUAL_NONE;
17055 break;
17058 if (!curr_ref_qual)
17059 break;
17060 else if (ref_qual)
17062 error_at (token->location, "multiple ref-qualifiers");
17063 cp_lexer_purge_token (parser->lexer);
17065 else
17067 ref_qual = curr_ref_qual;
17068 cp_lexer_consume_token (parser->lexer);
17072 if (ref_qual)
17073 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
17075 return ref_qual;
17078 /* Parse an (optional) virt-specifier-seq.
17080 virt-specifier-seq:
17081 virt-specifier virt-specifier-seq [opt]
17083 virt-specifier:
17084 override
17085 final
17087 Returns a bitmask representing the virt-specifiers. */
17089 static cp_virt_specifiers
17090 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17092 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17094 while (true)
17096 cp_token *token;
17097 cp_virt_specifiers virt_specifier;
17099 /* Peek at the next token. */
17100 token = cp_lexer_peek_token (parser->lexer);
17101 /* See if it's a virt-specifier-qualifier. */
17102 if (token->type != CPP_NAME)
17103 break;
17104 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17106 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17107 virt_specifier = VIRT_SPEC_OVERRIDE;
17109 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17111 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17112 virt_specifier = VIRT_SPEC_FINAL;
17114 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17116 virt_specifier = VIRT_SPEC_FINAL;
17118 else
17119 break;
17121 if (virt_specifiers & virt_specifier)
17123 error_at (token->location, "duplicate virt-specifier");
17124 cp_lexer_purge_token (parser->lexer);
17126 else
17128 cp_lexer_consume_token (parser->lexer);
17129 virt_specifiers |= virt_specifier;
17132 return virt_specifiers;
17135 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17136 is in scope even though it isn't real. */
17138 static void
17139 inject_this_parameter (tree ctype, cp_cv_quals quals)
17141 tree this_parm;
17143 if (current_class_ptr)
17145 /* We don't clear this between NSDMIs. Is it already what we want? */
17146 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17147 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17148 && cp_type_quals (type) == quals)
17149 return;
17152 this_parm = build_this_parm (ctype, quals);
17153 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17154 current_class_ptr = NULL_TREE;
17155 current_class_ref
17156 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17157 current_class_ptr = this_parm;
17160 /* Return true iff our current scope is a non-static data member
17161 initializer. */
17163 bool
17164 parsing_nsdmi (void)
17166 /* We recognize NSDMI context by the context-less 'this' pointer set up
17167 by the function above. */
17168 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17169 return true;
17170 return false;
17173 /* Parse a late-specified return type, if any. This is not a separate
17174 non-terminal, but part of a function declarator, which looks like
17176 -> trailing-type-specifier-seq abstract-declarator(opt)
17178 Returns the type indicated by the type-id.
17180 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17181 function. */
17183 static tree
17184 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
17186 cp_token *token;
17187 tree type;
17189 /* Peek at the next token. */
17190 token = cp_lexer_peek_token (parser->lexer);
17191 /* A late-specified return type is indicated by an initial '->'. */
17192 if (token->type != CPP_DEREF)
17193 return NULL_TREE;
17195 /* Consume the ->. */
17196 cp_lexer_consume_token (parser->lexer);
17198 tree save_ccp = current_class_ptr;
17199 tree save_ccr = current_class_ref;
17200 if (quals >= 0)
17202 /* DR 1207: 'this' is in scope in the trailing return type. */
17203 inject_this_parameter (current_class_type, quals);
17206 type = cp_parser_trailing_type_id (parser);
17208 if (quals >= 0)
17210 current_class_ptr = save_ccp;
17211 current_class_ref = save_ccr;
17214 return type;
17217 /* Parse a declarator-id.
17219 declarator-id:
17220 id-expression
17221 :: [opt] nested-name-specifier [opt] type-name
17223 In the `id-expression' case, the value returned is as for
17224 cp_parser_id_expression if the id-expression was an unqualified-id.
17225 If the id-expression was a qualified-id, then a SCOPE_REF is
17226 returned. The first operand is the scope (either a NAMESPACE_DECL
17227 or TREE_TYPE), but the second is still just a representation of an
17228 unqualified-id. */
17230 static tree
17231 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17233 tree id;
17234 /* The expression must be an id-expression. Assume that qualified
17235 names are the names of types so that:
17237 template <class T>
17238 int S<T>::R::i = 3;
17240 will work; we must treat `S<T>::R' as the name of a type.
17241 Similarly, assume that qualified names are templates, where
17242 required, so that:
17244 template <class T>
17245 int S<T>::R<T>::i = 3;
17247 will work, too. */
17248 id = cp_parser_id_expression (parser,
17249 /*template_keyword_p=*/false,
17250 /*check_dependency_p=*/false,
17251 /*template_p=*/NULL,
17252 /*declarator_p=*/true,
17253 optional_p);
17254 if (id && BASELINK_P (id))
17255 id = BASELINK_FUNCTIONS (id);
17256 return id;
17259 /* Parse a type-id.
17261 type-id:
17262 type-specifier-seq abstract-declarator [opt]
17264 Returns the TYPE specified. */
17266 static tree
17267 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17268 bool is_trailing_return)
17270 cp_decl_specifier_seq type_specifier_seq;
17271 cp_declarator *abstract_declarator;
17273 /* Parse the type-specifier-seq. */
17274 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17275 is_trailing_return,
17276 &type_specifier_seq);
17277 if (type_specifier_seq.type == error_mark_node)
17278 return error_mark_node;
17280 /* There might or might not be an abstract declarator. */
17281 cp_parser_parse_tentatively (parser);
17282 /* Look for the declarator. */
17283 abstract_declarator
17284 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17285 /*parenthesized_p=*/NULL,
17286 /*member_p=*/false);
17287 /* Check to see if there really was a declarator. */
17288 if (!cp_parser_parse_definitely (parser))
17289 abstract_declarator = NULL;
17291 if (type_specifier_seq.type
17292 && cxx_dialect < cxx1y
17293 && type_uses_auto (type_specifier_seq.type))
17295 /* A type-id with type 'auto' is only ok if the abstract declarator
17296 is a function declarator with a late-specified return type. */
17297 if (abstract_declarator
17298 && abstract_declarator->kind == cdk_function
17299 && abstract_declarator->u.function.late_return_type)
17300 /* OK */;
17301 else
17303 error ("invalid use of %<auto%>");
17304 return error_mark_node;
17308 return groktypename (&type_specifier_seq, abstract_declarator,
17309 is_template_arg);
17312 static tree cp_parser_type_id (cp_parser *parser)
17314 return cp_parser_type_id_1 (parser, false, false);
17317 static tree cp_parser_template_type_arg (cp_parser *parser)
17319 tree r;
17320 const char *saved_message = parser->type_definition_forbidden_message;
17321 parser->type_definition_forbidden_message
17322 = G_("types may not be defined in template arguments");
17323 r = cp_parser_type_id_1 (parser, true, false);
17324 parser->type_definition_forbidden_message = saved_message;
17325 return r;
17328 static tree cp_parser_trailing_type_id (cp_parser *parser)
17330 return cp_parser_type_id_1 (parser, false, true);
17333 /* Parse a type-specifier-seq.
17335 type-specifier-seq:
17336 type-specifier type-specifier-seq [opt]
17338 GNU extension:
17340 type-specifier-seq:
17341 attributes type-specifier-seq [opt]
17343 If IS_DECLARATION is true, we are at the start of a "condition" or
17344 exception-declaration, so we might be followed by a declarator-id.
17346 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17347 i.e. we've just seen "->".
17349 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17351 static void
17352 cp_parser_type_specifier_seq (cp_parser* parser,
17353 bool is_declaration,
17354 bool is_trailing_return,
17355 cp_decl_specifier_seq *type_specifier_seq)
17357 bool seen_type_specifier = false;
17358 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17359 cp_token *start_token = NULL;
17361 /* Clear the TYPE_SPECIFIER_SEQ. */
17362 clear_decl_specs (type_specifier_seq);
17364 /* In the context of a trailing return type, enum E { } is an
17365 elaborated-type-specifier followed by a function-body, not an
17366 enum-specifier. */
17367 if (is_trailing_return)
17368 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17370 /* Parse the type-specifiers and attributes. */
17371 while (true)
17373 tree type_specifier;
17374 bool is_cv_qualifier;
17376 /* Check for attributes first. */
17377 if (cp_next_tokens_can_be_attribute_p (parser))
17379 type_specifier_seq->attributes =
17380 chainon (type_specifier_seq->attributes,
17381 cp_parser_attributes_opt (parser));
17382 continue;
17385 /* record the token of the beginning of the type specifier seq,
17386 for error reporting purposes*/
17387 if (!start_token)
17388 start_token = cp_lexer_peek_token (parser->lexer);
17390 /* Look for the type-specifier. */
17391 type_specifier = cp_parser_type_specifier (parser,
17392 flags,
17393 type_specifier_seq,
17394 /*is_declaration=*/false,
17395 NULL,
17396 &is_cv_qualifier);
17397 if (!type_specifier)
17399 /* If the first type-specifier could not be found, this is not a
17400 type-specifier-seq at all. */
17401 if (!seen_type_specifier)
17403 cp_parser_error (parser, "expected type-specifier");
17404 type_specifier_seq->type = error_mark_node;
17405 return;
17407 /* If subsequent type-specifiers could not be found, the
17408 type-specifier-seq is complete. */
17409 break;
17412 seen_type_specifier = true;
17413 /* The standard says that a condition can be:
17415 type-specifier-seq declarator = assignment-expression
17417 However, given:
17419 struct S {};
17420 if (int S = ...)
17422 we should treat the "S" as a declarator, not as a
17423 type-specifier. The standard doesn't say that explicitly for
17424 type-specifier-seq, but it does say that for
17425 decl-specifier-seq in an ordinary declaration. Perhaps it
17426 would be clearer just to allow a decl-specifier-seq here, and
17427 then add a semantic restriction that if any decl-specifiers
17428 that are not type-specifiers appear, the program is invalid. */
17429 if (is_declaration && !is_cv_qualifier)
17430 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17434 /* Parse a parameter-declaration-clause.
17436 parameter-declaration-clause:
17437 parameter-declaration-list [opt] ... [opt]
17438 parameter-declaration-list , ...
17440 Returns a representation for the parameter declarations. A return
17441 value of NULL indicates a parameter-declaration-clause consisting
17442 only of an ellipsis. */
17444 static tree
17445 cp_parser_parameter_declaration_clause (cp_parser* parser)
17447 tree parameters;
17448 cp_token *token;
17449 bool ellipsis_p;
17450 bool is_error;
17452 /* Peek at the next token. */
17453 token = cp_lexer_peek_token (parser->lexer);
17454 /* Check for trivial parameter-declaration-clauses. */
17455 if (token->type == CPP_ELLIPSIS)
17457 /* Consume the `...' token. */
17458 cp_lexer_consume_token (parser->lexer);
17459 return NULL_TREE;
17461 else if (token->type == CPP_CLOSE_PAREN)
17462 /* There are no parameters. */
17464 #ifndef NO_IMPLICIT_EXTERN_C
17465 if (in_system_header && current_class_type == NULL
17466 && current_lang_name == lang_name_c)
17467 return NULL_TREE;
17468 else
17469 #endif
17470 return void_list_node;
17472 /* Check for `(void)', too, which is a special case. */
17473 else if (token->keyword == RID_VOID
17474 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17475 == CPP_CLOSE_PAREN))
17477 /* Consume the `void' token. */
17478 cp_lexer_consume_token (parser->lexer);
17479 /* There are no parameters. */
17480 return void_list_node;
17483 /* Parse the parameter-declaration-list. */
17484 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17485 /* If a parse error occurred while parsing the
17486 parameter-declaration-list, then the entire
17487 parameter-declaration-clause is erroneous. */
17488 if (is_error)
17489 return NULL;
17491 /* Peek at the next token. */
17492 token = cp_lexer_peek_token (parser->lexer);
17493 /* If it's a `,', the clause should terminate with an ellipsis. */
17494 if (token->type == CPP_COMMA)
17496 /* Consume the `,'. */
17497 cp_lexer_consume_token (parser->lexer);
17498 /* Expect an ellipsis. */
17499 ellipsis_p
17500 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17502 /* It might also be `...' if the optional trailing `,' was
17503 omitted. */
17504 else if (token->type == CPP_ELLIPSIS)
17506 /* Consume the `...' token. */
17507 cp_lexer_consume_token (parser->lexer);
17508 /* And remember that we saw it. */
17509 ellipsis_p = true;
17511 else
17512 ellipsis_p = false;
17514 /* Finish the parameter list. */
17515 if (!ellipsis_p)
17516 parameters = chainon (parameters, void_list_node);
17518 return parameters;
17521 /* Parse a parameter-declaration-list.
17523 parameter-declaration-list:
17524 parameter-declaration
17525 parameter-declaration-list , parameter-declaration
17527 Returns a representation of the parameter-declaration-list, as for
17528 cp_parser_parameter_declaration_clause. However, the
17529 `void_list_node' is never appended to the list. Upon return,
17530 *IS_ERROR will be true iff an error occurred. */
17532 static tree
17533 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17535 tree parameters = NULL_TREE;
17536 tree *tail = &parameters;
17537 bool saved_in_unbraced_linkage_specification_p;
17538 int index = 0;
17540 /* Assume all will go well. */
17541 *is_error = false;
17542 /* The special considerations that apply to a function within an
17543 unbraced linkage specifications do not apply to the parameters
17544 to the function. */
17545 saved_in_unbraced_linkage_specification_p
17546 = parser->in_unbraced_linkage_specification_p;
17547 parser->in_unbraced_linkage_specification_p = false;
17549 /* Look for more parameters. */
17550 while (true)
17552 cp_parameter_declarator *parameter;
17553 tree decl = error_mark_node;
17554 bool parenthesized_p = false;
17555 /* Parse the parameter. */
17556 parameter
17557 = cp_parser_parameter_declaration (parser,
17558 /*template_parm_p=*/false,
17559 &parenthesized_p);
17561 /* We don't know yet if the enclosing context is deprecated, so wait
17562 and warn in grokparms if appropriate. */
17563 deprecated_state = DEPRECATED_SUPPRESS;
17565 if (parameter)
17566 decl = grokdeclarator (parameter->declarator,
17567 &parameter->decl_specifiers,
17568 PARM,
17569 parameter->default_argument != NULL_TREE,
17570 &parameter->decl_specifiers.attributes);
17572 deprecated_state = DEPRECATED_NORMAL;
17574 /* If a parse error occurred parsing the parameter declaration,
17575 then the entire parameter-declaration-list is erroneous. */
17576 if (decl == error_mark_node)
17578 *is_error = true;
17579 parameters = error_mark_node;
17580 break;
17583 if (parameter->decl_specifiers.attributes)
17584 cplus_decl_attributes (&decl,
17585 parameter->decl_specifiers.attributes,
17587 if (DECL_NAME (decl))
17588 decl = pushdecl (decl);
17590 if (decl != error_mark_node)
17592 retrofit_lang_decl (decl);
17593 DECL_PARM_INDEX (decl) = ++index;
17594 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17597 /* Add the new parameter to the list. */
17598 *tail = build_tree_list (parameter->default_argument, decl);
17599 tail = &TREE_CHAIN (*tail);
17601 /* Peek at the next token. */
17602 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17603 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17604 /* These are for Objective-C++ */
17605 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17606 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17607 /* The parameter-declaration-list is complete. */
17608 break;
17609 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17611 cp_token *token;
17613 /* Peek at the next token. */
17614 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17615 /* If it's an ellipsis, then the list is complete. */
17616 if (token->type == CPP_ELLIPSIS)
17617 break;
17618 /* Otherwise, there must be more parameters. Consume the
17619 `,'. */
17620 cp_lexer_consume_token (parser->lexer);
17621 /* When parsing something like:
17623 int i(float f, double d)
17625 we can tell after seeing the declaration for "f" that we
17626 are not looking at an initialization of a variable "i",
17627 but rather at the declaration of a function "i".
17629 Due to the fact that the parsing of template arguments
17630 (as specified to a template-id) requires backtracking we
17631 cannot use this technique when inside a template argument
17632 list. */
17633 if (!parser->in_template_argument_list_p
17634 && !parser->in_type_id_in_expr_p
17635 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17636 /* However, a parameter-declaration of the form
17637 "foat(f)" (which is a valid declaration of a
17638 parameter "f") can also be interpreted as an
17639 expression (the conversion of "f" to "float"). */
17640 && !parenthesized_p)
17641 cp_parser_commit_to_tentative_parse (parser);
17643 else
17645 cp_parser_error (parser, "expected %<,%> or %<...%>");
17646 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17647 cp_parser_skip_to_closing_parenthesis (parser,
17648 /*recovering=*/true,
17649 /*or_comma=*/false,
17650 /*consume_paren=*/false);
17651 break;
17655 parser->in_unbraced_linkage_specification_p
17656 = saved_in_unbraced_linkage_specification_p;
17658 return parameters;
17661 /* Parse a parameter declaration.
17663 parameter-declaration:
17664 decl-specifier-seq ... [opt] declarator
17665 decl-specifier-seq declarator = assignment-expression
17666 decl-specifier-seq ... [opt] abstract-declarator [opt]
17667 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17669 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17670 declares a template parameter. (In that case, a non-nested `>'
17671 token encountered during the parsing of the assignment-expression
17672 is not interpreted as a greater-than operator.)
17674 Returns a representation of the parameter, or NULL if an error
17675 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17676 true iff the declarator is of the form "(p)". */
17678 static cp_parameter_declarator *
17679 cp_parser_parameter_declaration (cp_parser *parser,
17680 bool template_parm_p,
17681 bool *parenthesized_p)
17683 int declares_class_or_enum;
17684 cp_decl_specifier_seq decl_specifiers;
17685 cp_declarator *declarator;
17686 tree default_argument;
17687 cp_token *token = NULL, *declarator_token_start = NULL;
17688 const char *saved_message;
17690 /* In a template parameter, `>' is not an operator.
17692 [temp.param]
17694 When parsing a default template-argument for a non-type
17695 template-parameter, the first non-nested `>' is taken as the end
17696 of the template parameter-list rather than a greater-than
17697 operator. */
17699 /* Type definitions may not appear in parameter types. */
17700 saved_message = parser->type_definition_forbidden_message;
17701 parser->type_definition_forbidden_message
17702 = G_("types may not be defined in parameter types");
17704 /* Parse the declaration-specifiers. */
17705 cp_parser_decl_specifier_seq (parser,
17706 CP_PARSER_FLAGS_NONE,
17707 &decl_specifiers,
17708 &declares_class_or_enum);
17710 /* Complain about missing 'typename' or other invalid type names. */
17711 if (!decl_specifiers.any_type_specifiers_p)
17712 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17714 /* If an error occurred, there's no reason to attempt to parse the
17715 rest of the declaration. */
17716 if (cp_parser_error_occurred (parser))
17718 parser->type_definition_forbidden_message = saved_message;
17719 return NULL;
17722 /* Peek at the next token. */
17723 token = cp_lexer_peek_token (parser->lexer);
17725 /* If the next token is a `)', `,', `=', `>', or `...', then there
17726 is no declarator. However, when variadic templates are enabled,
17727 there may be a declarator following `...'. */
17728 if (token->type == CPP_CLOSE_PAREN
17729 || token->type == CPP_COMMA
17730 || token->type == CPP_EQ
17731 || token->type == CPP_GREATER)
17733 declarator = NULL;
17734 if (parenthesized_p)
17735 *parenthesized_p = false;
17737 /* Otherwise, there should be a declarator. */
17738 else
17740 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17741 parser->default_arg_ok_p = false;
17743 /* After seeing a decl-specifier-seq, if the next token is not a
17744 "(", there is no possibility that the code is a valid
17745 expression. Therefore, if parsing tentatively, we commit at
17746 this point. */
17747 if (!parser->in_template_argument_list_p
17748 /* In an expression context, having seen:
17750 (int((char ...
17752 we cannot be sure whether we are looking at a
17753 function-type (taking a "char" as a parameter) or a cast
17754 of some object of type "char" to "int". */
17755 && !parser->in_type_id_in_expr_p
17756 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17757 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17758 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17759 cp_parser_commit_to_tentative_parse (parser);
17760 /* Parse the declarator. */
17761 declarator_token_start = token;
17762 declarator = cp_parser_declarator (parser,
17763 CP_PARSER_DECLARATOR_EITHER,
17764 /*ctor_dtor_or_conv_p=*/NULL,
17765 parenthesized_p,
17766 /*member_p=*/false);
17767 parser->default_arg_ok_p = saved_default_arg_ok_p;
17768 /* After the declarator, allow more attributes. */
17769 decl_specifiers.attributes
17770 = chainon (decl_specifiers.attributes,
17771 cp_parser_attributes_opt (parser));
17774 /* If the next token is an ellipsis, and we have not seen a
17775 declarator name, and the type of the declarator contains parameter
17776 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17777 a parameter pack expansion expression. Otherwise, leave the
17778 ellipsis for a C-style variadic function. */
17779 token = cp_lexer_peek_token (parser->lexer);
17780 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17782 tree type = decl_specifiers.type;
17784 if (type && DECL_P (type))
17785 type = TREE_TYPE (type);
17787 if (type
17788 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17789 && declarator_can_be_parameter_pack (declarator)
17790 && (!declarator || !declarator->parameter_pack_p)
17791 && uses_parameter_packs (type))
17793 /* Consume the `...'. */
17794 cp_lexer_consume_token (parser->lexer);
17795 maybe_warn_variadic_templates ();
17797 /* Build a pack expansion type */
17798 if (declarator)
17799 declarator->parameter_pack_p = true;
17800 else
17801 decl_specifiers.type = make_pack_expansion (type);
17805 /* The restriction on defining new types applies only to the type
17806 of the parameter, not to the default argument. */
17807 parser->type_definition_forbidden_message = saved_message;
17809 /* If the next token is `=', then process a default argument. */
17810 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17812 token = cp_lexer_peek_token (parser->lexer);
17813 /* If we are defining a class, then the tokens that make up the
17814 default argument must be saved and processed later. */
17815 if (!template_parm_p && at_class_scope_p ()
17816 && TYPE_BEING_DEFINED (current_class_type)
17817 && !LAMBDA_TYPE_P (current_class_type))
17818 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17819 /* Outside of a class definition, we can just parse the
17820 assignment-expression. */
17821 else
17822 default_argument
17823 = cp_parser_default_argument (parser, template_parm_p);
17825 if (!parser->default_arg_ok_p)
17827 if (flag_permissive)
17828 warning (0, "deprecated use of default argument for parameter of non-function");
17829 else
17831 error_at (token->location,
17832 "default arguments are only "
17833 "permitted for function parameters");
17834 default_argument = NULL_TREE;
17837 else if ((declarator && declarator->parameter_pack_p)
17838 || (decl_specifiers.type
17839 && PACK_EXPANSION_P (decl_specifiers.type)))
17841 /* Find the name of the parameter pack. */
17842 cp_declarator *id_declarator = declarator;
17843 while (id_declarator && id_declarator->kind != cdk_id)
17844 id_declarator = id_declarator->declarator;
17846 if (id_declarator && id_declarator->kind == cdk_id)
17847 error_at (declarator_token_start->location,
17848 template_parm_p
17849 ? G_("template parameter pack %qD "
17850 "cannot have a default argument")
17851 : G_("parameter pack %qD cannot have "
17852 "a default argument"),
17853 id_declarator->u.id.unqualified_name);
17854 else
17855 error_at (declarator_token_start->location,
17856 template_parm_p
17857 ? G_("template parameter pack cannot have "
17858 "a default argument")
17859 : G_("parameter pack cannot have a "
17860 "default argument"));
17862 default_argument = NULL_TREE;
17865 else
17866 default_argument = NULL_TREE;
17868 return make_parameter_declarator (&decl_specifiers,
17869 declarator,
17870 default_argument);
17873 /* Parse a default argument and return it.
17875 TEMPLATE_PARM_P is true if this is a default argument for a
17876 non-type template parameter. */
17877 static tree
17878 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17880 tree default_argument = NULL_TREE;
17881 bool saved_greater_than_is_operator_p;
17882 bool saved_local_variables_forbidden_p;
17883 bool non_constant_p, is_direct_init;
17885 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17886 set correctly. */
17887 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17888 parser->greater_than_is_operator_p = !template_parm_p;
17889 /* Local variable names (and the `this' keyword) may not
17890 appear in a default argument. */
17891 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17892 parser->local_variables_forbidden_p = true;
17893 /* Parse the assignment-expression. */
17894 if (template_parm_p)
17895 push_deferring_access_checks (dk_no_deferred);
17896 default_argument
17897 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17898 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17899 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17900 if (template_parm_p)
17901 pop_deferring_access_checks ();
17902 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17903 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17905 return default_argument;
17908 /* Parse a function-body.
17910 function-body:
17911 compound_statement */
17913 static void
17914 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17916 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17919 /* Parse a ctor-initializer-opt followed by a function-body. Return
17920 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17921 is true we are parsing a function-try-block. */
17923 static bool
17924 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17925 bool in_function_try_block)
17927 tree body, list;
17928 bool ctor_initializer_p;
17929 const bool check_body_p =
17930 DECL_CONSTRUCTOR_P (current_function_decl)
17931 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17932 tree last = NULL;
17934 /* Begin the function body. */
17935 body = begin_function_body ();
17936 /* Parse the optional ctor-initializer. */
17937 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17939 /* If we're parsing a constexpr constructor definition, we need
17940 to check that the constructor body is indeed empty. However,
17941 before we get to cp_parser_function_body lot of junk has been
17942 generated, so we can't just check that we have an empty block.
17943 Rather we take a snapshot of the outermost block, and check whether
17944 cp_parser_function_body changed its state. */
17945 if (check_body_p)
17947 list = cur_stmt_list;
17948 if (STATEMENT_LIST_TAIL (list))
17949 last = STATEMENT_LIST_TAIL (list)->stmt;
17951 /* Parse the function-body. */
17952 cp_parser_function_body (parser, in_function_try_block);
17953 if (check_body_p)
17954 check_constexpr_ctor_body (last, list);
17955 /* Finish the function body. */
17956 finish_function_body (body);
17958 return ctor_initializer_p;
17961 /* Parse an initializer.
17963 initializer:
17964 = initializer-clause
17965 ( expression-list )
17967 Returns an expression representing the initializer. If no
17968 initializer is present, NULL_TREE is returned.
17970 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17971 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17972 set to TRUE if there is no initializer present. If there is an
17973 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17974 is set to true; otherwise it is set to false. */
17976 static tree
17977 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17978 bool* non_constant_p)
17980 cp_token *token;
17981 tree init;
17983 /* Peek at the next token. */
17984 token = cp_lexer_peek_token (parser->lexer);
17986 /* Let our caller know whether or not this initializer was
17987 parenthesized. */
17988 *is_direct_init = (token->type != CPP_EQ);
17989 /* Assume that the initializer is constant. */
17990 *non_constant_p = false;
17992 if (token->type == CPP_EQ)
17994 /* Consume the `='. */
17995 cp_lexer_consume_token (parser->lexer);
17996 /* Parse the initializer-clause. */
17997 init = cp_parser_initializer_clause (parser, non_constant_p);
17999 else if (token->type == CPP_OPEN_PAREN)
18001 vec<tree, va_gc> *vec;
18002 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18003 /*cast_p=*/false,
18004 /*allow_expansion_p=*/true,
18005 non_constant_p);
18006 if (vec == NULL)
18007 return error_mark_node;
18008 init = build_tree_list_vec (vec);
18009 release_tree_vector (vec);
18011 else if (token->type == CPP_OPEN_BRACE)
18013 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18014 init = cp_parser_braced_list (parser, non_constant_p);
18015 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18017 else
18019 /* Anything else is an error. */
18020 cp_parser_error (parser, "expected initializer");
18021 init = error_mark_node;
18024 return init;
18027 /* Parse an initializer-clause.
18029 initializer-clause:
18030 assignment-expression
18031 braced-init-list
18033 Returns an expression representing the initializer.
18035 If the `assignment-expression' production is used the value
18036 returned is simply a representation for the expression.
18038 Otherwise, calls cp_parser_braced_list. */
18040 static tree
18041 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18043 tree initializer;
18045 /* Assume the expression is constant. */
18046 *non_constant_p = false;
18048 /* If it is not a `{', then we are looking at an
18049 assignment-expression. */
18050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18052 initializer
18053 = cp_parser_constant_expression (parser,
18054 /*allow_non_constant_p=*/true,
18055 non_constant_p);
18057 else
18058 initializer = cp_parser_braced_list (parser, non_constant_p);
18060 return initializer;
18063 /* Parse a brace-enclosed initializer list.
18065 braced-init-list:
18066 { initializer-list , [opt] }
18069 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18070 the elements of the initializer-list (or NULL, if the last
18071 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18072 NULL_TREE. There is no way to detect whether or not the optional
18073 trailing `,' was provided. NON_CONSTANT_P is as for
18074 cp_parser_initializer. */
18076 static tree
18077 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18079 tree initializer;
18081 /* Consume the `{' token. */
18082 cp_lexer_consume_token (parser->lexer);
18083 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18084 initializer = make_node (CONSTRUCTOR);
18085 /* If it's not a `}', then there is a non-trivial initializer. */
18086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18088 /* Parse the initializer list. */
18089 CONSTRUCTOR_ELTS (initializer)
18090 = cp_parser_initializer_list (parser, non_constant_p);
18091 /* A trailing `,' token is allowed. */
18092 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18093 cp_lexer_consume_token (parser->lexer);
18095 else
18096 *non_constant_p = false;
18097 /* Now, there should be a trailing `}'. */
18098 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18099 TREE_TYPE (initializer) = init_list_type_node;
18100 return initializer;
18103 /* Parse an initializer-list.
18105 initializer-list:
18106 initializer-clause ... [opt]
18107 initializer-list , initializer-clause ... [opt]
18109 GNU Extension:
18111 initializer-list:
18112 designation initializer-clause ...[opt]
18113 initializer-list , designation initializer-clause ...[opt]
18115 designation:
18116 . identifier =
18117 identifier :
18118 [ constant-expression ] =
18120 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18121 for the initializer. If the INDEX of the elt is non-NULL, it is the
18122 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18123 as for cp_parser_initializer. */
18125 static vec<constructor_elt, va_gc> *
18126 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18128 vec<constructor_elt, va_gc> *v = NULL;
18130 /* Assume all of the expressions are constant. */
18131 *non_constant_p = false;
18133 /* Parse the rest of the list. */
18134 while (true)
18136 cp_token *token;
18137 tree designator;
18138 tree initializer;
18139 bool clause_non_constant_p;
18141 /* If the next token is an identifier and the following one is a
18142 colon, we are looking at the GNU designated-initializer
18143 syntax. */
18144 if (cp_parser_allow_gnu_extensions_p (parser)
18145 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18146 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18148 /* Warn the user that they are using an extension. */
18149 pedwarn (input_location, OPT_Wpedantic,
18150 "ISO C++ does not allow designated initializers");
18151 /* Consume the identifier. */
18152 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18153 /* Consume the `:'. */
18154 cp_lexer_consume_token (parser->lexer);
18156 /* Also handle the C99 syntax, '. id ='. */
18157 else if (cp_parser_allow_gnu_extensions_p (parser)
18158 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18159 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18160 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18162 /* Warn the user that they are using an extension. */
18163 pedwarn (input_location, OPT_Wpedantic,
18164 "ISO C++ does not allow C99 designated initializers");
18165 /* Consume the `.'. */
18166 cp_lexer_consume_token (parser->lexer);
18167 /* Consume the identifier. */
18168 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18169 /* Consume the `='. */
18170 cp_lexer_consume_token (parser->lexer);
18172 /* Also handle C99 array designators, '[ const ] ='. */
18173 else if (cp_parser_allow_gnu_extensions_p (parser)
18174 && !c_dialect_objc ()
18175 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18177 /* In C++11, [ could start a lambda-introducer. */
18178 bool non_const = false;
18180 cp_parser_parse_tentatively (parser);
18181 cp_lexer_consume_token (parser->lexer);
18182 designator = cp_parser_constant_expression (parser, true, &non_const);
18183 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18184 cp_parser_require (parser, CPP_EQ, RT_EQ);
18185 if (!cp_parser_parse_definitely (parser))
18186 designator = NULL_TREE;
18187 else if (non_const)
18188 require_potential_rvalue_constant_expression (designator);
18190 else
18191 designator = NULL_TREE;
18193 /* Parse the initializer. */
18194 initializer = cp_parser_initializer_clause (parser,
18195 &clause_non_constant_p);
18196 /* If any clause is non-constant, so is the entire initializer. */
18197 if (clause_non_constant_p)
18198 *non_constant_p = true;
18200 /* If we have an ellipsis, this is an initializer pack
18201 expansion. */
18202 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18204 /* Consume the `...'. */
18205 cp_lexer_consume_token (parser->lexer);
18207 /* Turn the initializer into an initializer expansion. */
18208 initializer = make_pack_expansion (initializer);
18211 /* Add it to the vector. */
18212 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18214 /* If the next token is not a comma, we have reached the end of
18215 the list. */
18216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18217 break;
18219 /* Peek at the next token. */
18220 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18221 /* If the next token is a `}', then we're still done. An
18222 initializer-clause can have a trailing `,' after the
18223 initializer-list and before the closing `}'. */
18224 if (token->type == CPP_CLOSE_BRACE)
18225 break;
18227 /* Consume the `,' token. */
18228 cp_lexer_consume_token (parser->lexer);
18231 return v;
18234 /* Classes [gram.class] */
18236 /* Parse a class-name.
18238 class-name:
18239 identifier
18240 template-id
18242 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18243 to indicate that names looked up in dependent types should be
18244 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18245 keyword has been used to indicate that the name that appears next
18246 is a template. TAG_TYPE indicates the explicit tag given before
18247 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18248 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18249 is the class being defined in a class-head.
18251 Returns the TYPE_DECL representing the class. */
18253 static tree
18254 cp_parser_class_name (cp_parser *parser,
18255 bool typename_keyword_p,
18256 bool template_keyword_p,
18257 enum tag_types tag_type,
18258 bool check_dependency_p,
18259 bool class_head_p,
18260 bool is_declaration)
18262 tree decl;
18263 tree scope;
18264 bool typename_p;
18265 cp_token *token;
18266 tree identifier = NULL_TREE;
18268 /* All class-names start with an identifier. */
18269 token = cp_lexer_peek_token (parser->lexer);
18270 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18272 cp_parser_error (parser, "expected class-name");
18273 return error_mark_node;
18276 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18277 to a template-id, so we save it here. */
18278 scope = parser->scope;
18279 if (scope == error_mark_node)
18280 return error_mark_node;
18282 /* Any name names a type if we're following the `typename' keyword
18283 in a qualified name where the enclosing scope is type-dependent. */
18284 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18285 && dependent_type_p (scope));
18286 /* Handle the common case (an identifier, but not a template-id)
18287 efficiently. */
18288 if (token->type == CPP_NAME
18289 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18291 cp_token *identifier_token;
18292 bool ambiguous_p;
18294 /* Look for the identifier. */
18295 identifier_token = cp_lexer_peek_token (parser->lexer);
18296 ambiguous_p = identifier_token->ambiguous_p;
18297 identifier = cp_parser_identifier (parser);
18298 /* If the next token isn't an identifier, we are certainly not
18299 looking at a class-name. */
18300 if (identifier == error_mark_node)
18301 decl = error_mark_node;
18302 /* If we know this is a type-name, there's no need to look it
18303 up. */
18304 else if (typename_p)
18305 decl = identifier;
18306 else
18308 tree ambiguous_decls;
18309 /* If we already know that this lookup is ambiguous, then
18310 we've already issued an error message; there's no reason
18311 to check again. */
18312 if (ambiguous_p)
18314 cp_parser_simulate_error (parser);
18315 return error_mark_node;
18317 /* If the next token is a `::', then the name must be a type
18318 name.
18320 [basic.lookup.qual]
18322 During the lookup for a name preceding the :: scope
18323 resolution operator, object, function, and enumerator
18324 names are ignored. */
18325 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18326 tag_type = typename_type;
18327 /* Look up the name. */
18328 decl = cp_parser_lookup_name (parser, identifier,
18329 tag_type,
18330 /*is_template=*/false,
18331 /*is_namespace=*/false,
18332 check_dependency_p,
18333 &ambiguous_decls,
18334 identifier_token->location);
18335 if (ambiguous_decls)
18337 if (cp_parser_parsing_tentatively (parser))
18338 cp_parser_simulate_error (parser);
18339 return error_mark_node;
18343 else
18345 /* Try a template-id. */
18346 decl = cp_parser_template_id (parser, template_keyword_p,
18347 check_dependency_p,
18348 tag_type,
18349 is_declaration);
18350 if (decl == error_mark_node)
18351 return error_mark_node;
18354 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18356 /* If this is a typename, create a TYPENAME_TYPE. */
18357 if (typename_p && decl != error_mark_node)
18359 decl = make_typename_type (scope, decl, typename_type,
18360 /*complain=*/tf_error);
18361 if (decl != error_mark_node)
18362 decl = TYPE_NAME (decl);
18365 decl = strip_using_decl (decl);
18367 /* Check to see that it is really the name of a class. */
18368 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18369 && identifier_p (TREE_OPERAND (decl, 0))
18370 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18371 /* Situations like this:
18373 template <typename T> struct A {
18374 typename T::template X<int>::I i;
18377 are problematic. Is `T::template X<int>' a class-name? The
18378 standard does not seem to be definitive, but there is no other
18379 valid interpretation of the following `::'. Therefore, those
18380 names are considered class-names. */
18382 decl = make_typename_type (scope, decl, tag_type, tf_error);
18383 if (decl != error_mark_node)
18384 decl = TYPE_NAME (decl);
18386 else if (TREE_CODE (decl) != TYPE_DECL
18387 || TREE_TYPE (decl) == error_mark_node
18388 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18389 /* In Objective-C 2.0, a classname followed by '.' starts a
18390 dot-syntax expression, and it's not a type-name. */
18391 || (c_dialect_objc ()
18392 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18393 && objc_is_class_name (decl)))
18394 decl = error_mark_node;
18396 if (decl == error_mark_node)
18397 cp_parser_error (parser, "expected class-name");
18398 else if (identifier && !parser->scope)
18399 maybe_note_name_used_in_class (identifier, decl);
18401 return decl;
18404 /* Parse a class-specifier.
18406 class-specifier:
18407 class-head { member-specification [opt] }
18409 Returns the TREE_TYPE representing the class. */
18411 static tree
18412 cp_parser_class_specifier_1 (cp_parser* parser)
18414 tree type;
18415 tree attributes = NULL_TREE;
18416 bool nested_name_specifier_p;
18417 unsigned saved_num_template_parameter_lists;
18418 bool saved_in_function_body;
18419 unsigned char in_statement;
18420 bool in_switch_statement_p;
18421 bool saved_in_unbraced_linkage_specification_p;
18422 tree old_scope = NULL_TREE;
18423 tree scope = NULL_TREE;
18424 cp_token *closing_brace;
18426 push_deferring_access_checks (dk_no_deferred);
18428 /* Parse the class-head. */
18429 type = cp_parser_class_head (parser,
18430 &nested_name_specifier_p);
18431 /* If the class-head was a semantic disaster, skip the entire body
18432 of the class. */
18433 if (!type)
18435 cp_parser_skip_to_end_of_block_or_statement (parser);
18436 pop_deferring_access_checks ();
18437 return error_mark_node;
18440 /* Look for the `{'. */
18441 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18443 pop_deferring_access_checks ();
18444 return error_mark_node;
18447 /* Issue an error message if type-definitions are forbidden here. */
18448 cp_parser_check_type_definition (parser);
18449 /* Remember that we are defining one more class. */
18450 ++parser->num_classes_being_defined;
18451 /* Inside the class, surrounding template-parameter-lists do not
18452 apply. */
18453 saved_num_template_parameter_lists
18454 = parser->num_template_parameter_lists;
18455 parser->num_template_parameter_lists = 0;
18456 /* We are not in a function body. */
18457 saved_in_function_body = parser->in_function_body;
18458 parser->in_function_body = false;
18459 /* Or in a loop. */
18460 in_statement = parser->in_statement;
18461 parser->in_statement = 0;
18462 /* Or in a switch. */
18463 in_switch_statement_p = parser->in_switch_statement_p;
18464 parser->in_switch_statement_p = false;
18465 /* We are not immediately inside an extern "lang" block. */
18466 saved_in_unbraced_linkage_specification_p
18467 = parser->in_unbraced_linkage_specification_p;
18468 parser->in_unbraced_linkage_specification_p = false;
18470 /* Start the class. */
18471 if (nested_name_specifier_p)
18473 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18474 old_scope = push_inner_scope (scope);
18476 type = begin_class_definition (type);
18478 if (type == error_mark_node)
18479 /* If the type is erroneous, skip the entire body of the class. */
18480 cp_parser_skip_to_closing_brace (parser);
18481 else
18482 /* Parse the member-specification. */
18483 cp_parser_member_specification_opt (parser);
18485 /* Look for the trailing `}'. */
18486 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18487 /* Look for trailing attributes to apply to this class. */
18488 if (cp_parser_allow_gnu_extensions_p (parser))
18489 attributes = cp_parser_gnu_attributes_opt (parser);
18490 if (type != error_mark_node)
18491 type = finish_struct (type, attributes);
18492 if (nested_name_specifier_p)
18493 pop_inner_scope (old_scope, scope);
18495 /* We've finished a type definition. Check for the common syntax
18496 error of forgetting a semicolon after the definition. We need to
18497 be careful, as we can't just check for not-a-semicolon and be done
18498 with it; the user might have typed:
18500 class X { } c = ...;
18501 class X { } *p = ...;
18503 and so forth. Instead, enumerate all the possible tokens that
18504 might follow this production; if we don't see one of them, then
18505 complain and silently insert the semicolon. */
18507 cp_token *token = cp_lexer_peek_token (parser->lexer);
18508 bool want_semicolon = true;
18510 if (cp_next_tokens_can_be_std_attribute_p (parser))
18511 /* Don't try to parse c++11 attributes here. As per the
18512 grammar, that should be a task for
18513 cp_parser_decl_specifier_seq. */
18514 want_semicolon = false;
18516 switch (token->type)
18518 case CPP_NAME:
18519 case CPP_SEMICOLON:
18520 case CPP_MULT:
18521 case CPP_AND:
18522 case CPP_OPEN_PAREN:
18523 case CPP_CLOSE_PAREN:
18524 case CPP_COMMA:
18525 want_semicolon = false;
18526 break;
18528 /* While it's legal for type qualifiers and storage class
18529 specifiers to follow type definitions in the grammar, only
18530 compiler testsuites contain code like that. Assume that if
18531 we see such code, then what we're really seeing is a case
18532 like:
18534 class X { }
18535 const <type> var = ...;
18539 class Y { }
18540 static <type> func (...) ...
18542 i.e. the qualifier or specifier applies to the next
18543 declaration. To do so, however, we need to look ahead one
18544 more token to see if *that* token is a type specifier.
18546 This code could be improved to handle:
18548 class Z { }
18549 static const <type> var = ...; */
18550 case CPP_KEYWORD:
18551 if (keyword_is_decl_specifier (token->keyword))
18553 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18555 /* Handling user-defined types here would be nice, but very
18556 tricky. */
18557 want_semicolon
18558 = (lookahead->type == CPP_KEYWORD
18559 && keyword_begins_type_specifier (lookahead->keyword));
18561 break;
18562 default:
18563 break;
18566 /* If we don't have a type, then something is very wrong and we
18567 shouldn't try to do anything clever. Likewise for not seeing the
18568 closing brace. */
18569 if (closing_brace && TYPE_P (type) && want_semicolon)
18571 cp_token_position prev
18572 = cp_lexer_previous_token_position (parser->lexer);
18573 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18574 location_t loc = prev_token->location;
18576 if (CLASSTYPE_DECLARED_CLASS (type))
18577 error_at (loc, "expected %<;%> after class definition");
18578 else if (TREE_CODE (type) == RECORD_TYPE)
18579 error_at (loc, "expected %<;%> after struct definition");
18580 else if (TREE_CODE (type) == UNION_TYPE)
18581 error_at (loc, "expected %<;%> after union definition");
18582 else
18583 gcc_unreachable ();
18585 /* Unget one token and smash it to look as though we encountered
18586 a semicolon in the input stream. */
18587 cp_lexer_set_token_position (parser->lexer, prev);
18588 token = cp_lexer_peek_token (parser->lexer);
18589 token->type = CPP_SEMICOLON;
18590 token->keyword = RID_MAX;
18594 /* If this class is not itself within the scope of another class,
18595 then we need to parse the bodies of all of the queued function
18596 definitions. Note that the queued functions defined in a class
18597 are not always processed immediately following the
18598 class-specifier for that class. Consider:
18600 struct A {
18601 struct B { void f() { sizeof (A); } };
18604 If `f' were processed before the processing of `A' were
18605 completed, there would be no way to compute the size of `A'.
18606 Note that the nesting we are interested in here is lexical --
18607 not the semantic nesting given by TYPE_CONTEXT. In particular,
18608 for:
18610 struct A { struct B; };
18611 struct A::B { void f() { } };
18613 there is no need to delay the parsing of `A::B::f'. */
18614 if (--parser->num_classes_being_defined == 0)
18616 tree decl;
18617 tree class_type = NULL_TREE;
18618 tree pushed_scope = NULL_TREE;
18619 unsigned ix;
18620 cp_default_arg_entry *e;
18621 tree save_ccp, save_ccr;
18623 /* In a first pass, parse default arguments to the functions.
18624 Then, in a second pass, parse the bodies of the functions.
18625 This two-phased approach handles cases like:
18627 struct S {
18628 void f() { g(); }
18629 void g(int i = 3);
18633 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18635 decl = e->decl;
18636 /* If there are default arguments that have not yet been processed,
18637 take care of them now. */
18638 if (class_type != e->class_type)
18640 if (pushed_scope)
18641 pop_scope (pushed_scope);
18642 class_type = e->class_type;
18643 pushed_scope = push_scope (class_type);
18645 /* Make sure that any template parameters are in scope. */
18646 maybe_begin_member_template_processing (decl);
18647 /* Parse the default argument expressions. */
18648 cp_parser_late_parsing_default_args (parser, decl);
18649 /* Remove any template parameters from the symbol table. */
18650 maybe_end_member_template_processing ();
18652 vec_safe_truncate (unparsed_funs_with_default_args, 0);
18653 /* Now parse any NSDMIs. */
18654 save_ccp = current_class_ptr;
18655 save_ccr = current_class_ref;
18656 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18658 if (class_type != DECL_CONTEXT (decl))
18660 if (pushed_scope)
18661 pop_scope (pushed_scope);
18662 class_type = DECL_CONTEXT (decl);
18663 pushed_scope = push_scope (class_type);
18665 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18666 cp_parser_late_parsing_nsdmi (parser, decl);
18668 vec_safe_truncate (unparsed_nsdmis, 0);
18669 current_class_ptr = save_ccp;
18670 current_class_ref = save_ccr;
18671 if (pushed_scope)
18672 pop_scope (pushed_scope);
18673 /* Now parse the body of the functions. */
18674 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
18675 cp_parser_late_parsing_for_member (parser, decl);
18676 vec_safe_truncate (unparsed_funs_with_definitions, 0);
18679 /* Put back any saved access checks. */
18680 pop_deferring_access_checks ();
18682 /* Restore saved state. */
18683 parser->in_switch_statement_p = in_switch_statement_p;
18684 parser->in_statement = in_statement;
18685 parser->in_function_body = saved_in_function_body;
18686 parser->num_template_parameter_lists
18687 = saved_num_template_parameter_lists;
18688 parser->in_unbraced_linkage_specification_p
18689 = saved_in_unbraced_linkage_specification_p;
18691 return type;
18694 static tree
18695 cp_parser_class_specifier (cp_parser* parser)
18697 tree ret;
18698 timevar_push (TV_PARSE_STRUCT);
18699 ret = cp_parser_class_specifier_1 (parser);
18700 timevar_pop (TV_PARSE_STRUCT);
18701 return ret;
18704 /* Parse a class-head.
18706 class-head:
18707 class-key identifier [opt] base-clause [opt]
18708 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18709 class-key nested-name-specifier [opt] template-id
18710 base-clause [opt]
18712 class-virt-specifier:
18713 final
18715 GNU Extensions:
18716 class-key attributes identifier [opt] base-clause [opt]
18717 class-key attributes nested-name-specifier identifier base-clause [opt]
18718 class-key attributes nested-name-specifier [opt] template-id
18719 base-clause [opt]
18721 Upon return BASES is initialized to the list of base classes (or
18722 NULL, if there are none) in the same form returned by
18723 cp_parser_base_clause.
18725 Returns the TYPE of the indicated class. Sets
18726 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18727 involving a nested-name-specifier was used, and FALSE otherwise.
18729 Returns error_mark_node if this is not a class-head.
18731 Returns NULL_TREE if the class-head is syntactically valid, but
18732 semantically invalid in a way that means we should skip the entire
18733 body of the class. */
18735 static tree
18736 cp_parser_class_head (cp_parser* parser,
18737 bool* nested_name_specifier_p)
18739 tree nested_name_specifier;
18740 enum tag_types class_key;
18741 tree id = NULL_TREE;
18742 tree type = NULL_TREE;
18743 tree attributes;
18744 tree bases;
18745 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18746 bool template_id_p = false;
18747 bool qualified_p = false;
18748 bool invalid_nested_name_p = false;
18749 bool invalid_explicit_specialization_p = false;
18750 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18751 tree pushed_scope = NULL_TREE;
18752 unsigned num_templates;
18753 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18754 /* Assume no nested-name-specifier will be present. */
18755 *nested_name_specifier_p = false;
18756 /* Assume no template parameter lists will be used in defining the
18757 type. */
18758 num_templates = 0;
18759 parser->colon_corrects_to_scope_p = false;
18761 /* Look for the class-key. */
18762 class_key = cp_parser_class_key (parser);
18763 if (class_key == none_type)
18764 return error_mark_node;
18766 /* Parse the attributes. */
18767 attributes = cp_parser_attributes_opt (parser);
18769 /* If the next token is `::', that is invalid -- but sometimes
18770 people do try to write:
18772 struct ::S {};
18774 Handle this gracefully by accepting the extra qualifier, and then
18775 issuing an error about it later if this really is a
18776 class-head. If it turns out just to be an elaborated type
18777 specifier, remain silent. */
18778 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18779 qualified_p = true;
18781 push_deferring_access_checks (dk_no_check);
18783 /* Determine the name of the class. Begin by looking for an
18784 optional nested-name-specifier. */
18785 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18786 nested_name_specifier
18787 = cp_parser_nested_name_specifier_opt (parser,
18788 /*typename_keyword_p=*/false,
18789 /*check_dependency_p=*/false,
18790 /*type_p=*/true,
18791 /*is_declaration=*/false);
18792 /* If there was a nested-name-specifier, then there *must* be an
18793 identifier. */
18794 if (nested_name_specifier)
18796 type_start_token = cp_lexer_peek_token (parser->lexer);
18797 /* Although the grammar says `identifier', it really means
18798 `class-name' or `template-name'. You are only allowed to
18799 define a class that has already been declared with this
18800 syntax.
18802 The proposed resolution for Core Issue 180 says that wherever
18803 you see `class T::X' you should treat `X' as a type-name.
18805 It is OK to define an inaccessible class; for example:
18807 class A { class B; };
18808 class A::B {};
18810 We do not know if we will see a class-name, or a
18811 template-name. We look for a class-name first, in case the
18812 class-name is a template-id; if we looked for the
18813 template-name first we would stop after the template-name. */
18814 cp_parser_parse_tentatively (parser);
18815 type = cp_parser_class_name (parser,
18816 /*typename_keyword_p=*/false,
18817 /*template_keyword_p=*/false,
18818 class_type,
18819 /*check_dependency_p=*/false,
18820 /*class_head_p=*/true,
18821 /*is_declaration=*/false);
18822 /* If that didn't work, ignore the nested-name-specifier. */
18823 if (!cp_parser_parse_definitely (parser))
18825 invalid_nested_name_p = true;
18826 type_start_token = cp_lexer_peek_token (parser->lexer);
18827 id = cp_parser_identifier (parser);
18828 if (id == error_mark_node)
18829 id = NULL_TREE;
18831 /* If we could not find a corresponding TYPE, treat this
18832 declaration like an unqualified declaration. */
18833 if (type == error_mark_node)
18834 nested_name_specifier = NULL_TREE;
18835 /* Otherwise, count the number of templates used in TYPE and its
18836 containing scopes. */
18837 else
18839 tree scope;
18841 for (scope = TREE_TYPE (type);
18842 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18843 scope = get_containing_scope (scope))
18844 if (TYPE_P (scope)
18845 && CLASS_TYPE_P (scope)
18846 && CLASSTYPE_TEMPLATE_INFO (scope)
18847 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18848 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
18849 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
18850 ++num_templates;
18853 /* Otherwise, the identifier is optional. */
18854 else
18856 /* We don't know whether what comes next is a template-id,
18857 an identifier, or nothing at all. */
18858 cp_parser_parse_tentatively (parser);
18859 /* Check for a template-id. */
18860 type_start_token = cp_lexer_peek_token (parser->lexer);
18861 id = cp_parser_template_id (parser,
18862 /*template_keyword_p=*/false,
18863 /*check_dependency_p=*/true,
18864 class_key,
18865 /*is_declaration=*/true);
18866 /* If that didn't work, it could still be an identifier. */
18867 if (!cp_parser_parse_definitely (parser))
18869 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18871 type_start_token = cp_lexer_peek_token (parser->lexer);
18872 id = cp_parser_identifier (parser);
18874 else
18875 id = NULL_TREE;
18877 else
18879 template_id_p = true;
18880 ++num_templates;
18884 pop_deferring_access_checks ();
18886 if (id)
18888 cp_parser_check_for_invalid_template_id (parser, id,
18889 class_key,
18890 type_start_token->location);
18892 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18894 /* If it's not a `:' or a `{' then we can't really be looking at a
18895 class-head, since a class-head only appears as part of a
18896 class-specifier. We have to detect this situation before calling
18897 xref_tag, since that has irreversible side-effects. */
18898 if (!cp_parser_next_token_starts_class_definition_p (parser))
18900 cp_parser_error (parser, "expected %<{%> or %<:%>");
18901 type = error_mark_node;
18902 goto out;
18905 /* At this point, we're going ahead with the class-specifier, even
18906 if some other problem occurs. */
18907 cp_parser_commit_to_tentative_parse (parser);
18908 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18910 cp_parser_error (parser,
18911 "cannot specify %<override%> for a class");
18912 type = error_mark_node;
18913 goto out;
18915 /* Issue the error about the overly-qualified name now. */
18916 if (qualified_p)
18918 cp_parser_error (parser,
18919 "global qualification of class name is invalid");
18920 type = error_mark_node;
18921 goto out;
18923 else if (invalid_nested_name_p)
18925 cp_parser_error (parser,
18926 "qualified name does not name a class");
18927 type = error_mark_node;
18928 goto out;
18930 else if (nested_name_specifier)
18932 tree scope;
18934 /* Reject typedef-names in class heads. */
18935 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18937 error_at (type_start_token->location,
18938 "invalid class name in declaration of %qD",
18939 type);
18940 type = NULL_TREE;
18941 goto done;
18944 /* Figure out in what scope the declaration is being placed. */
18945 scope = current_scope ();
18946 /* If that scope does not contain the scope in which the
18947 class was originally declared, the program is invalid. */
18948 if (scope && !is_ancestor (scope, nested_name_specifier))
18950 if (at_namespace_scope_p ())
18951 error_at (type_start_token->location,
18952 "declaration of %qD in namespace %qD which does not "
18953 "enclose %qD",
18954 type, scope, nested_name_specifier);
18955 else
18956 error_at (type_start_token->location,
18957 "declaration of %qD in %qD which does not enclose %qD",
18958 type, scope, nested_name_specifier);
18959 type = NULL_TREE;
18960 goto done;
18962 /* [dcl.meaning]
18964 A declarator-id shall not be qualified except for the
18965 definition of a ... nested class outside of its class
18966 ... [or] the definition or explicit instantiation of a
18967 class member of a namespace outside of its namespace. */
18968 if (scope == nested_name_specifier)
18970 permerror (nested_name_specifier_token_start->location,
18971 "extra qualification not allowed");
18972 nested_name_specifier = NULL_TREE;
18973 num_templates = 0;
18976 /* An explicit-specialization must be preceded by "template <>". If
18977 it is not, try to recover gracefully. */
18978 if (at_namespace_scope_p ()
18979 && parser->num_template_parameter_lists == 0
18980 && template_id_p)
18982 error_at (type_start_token->location,
18983 "an explicit specialization must be preceded by %<template <>%>");
18984 invalid_explicit_specialization_p = true;
18985 /* Take the same action that would have been taken by
18986 cp_parser_explicit_specialization. */
18987 ++parser->num_template_parameter_lists;
18988 begin_specialization ();
18990 /* There must be no "return" statements between this point and the
18991 end of this function; set "type "to the correct return value and
18992 use "goto done;" to return. */
18993 /* Make sure that the right number of template parameters were
18994 present. */
18995 if (!cp_parser_check_template_parameters (parser, num_templates,
18996 type_start_token->location,
18997 /*declarator=*/NULL))
18999 /* If something went wrong, there is no point in even trying to
19000 process the class-definition. */
19001 type = NULL_TREE;
19002 goto done;
19005 /* Look up the type. */
19006 if (template_id_p)
19008 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19009 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19010 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19012 error_at (type_start_token->location,
19013 "function template %qD redeclared as a class template", id);
19014 type = error_mark_node;
19016 else
19018 type = TREE_TYPE (id);
19019 type = maybe_process_partial_specialization (type);
19021 if (nested_name_specifier)
19022 pushed_scope = push_scope (nested_name_specifier);
19024 else if (nested_name_specifier)
19026 tree class_type;
19028 /* Given:
19030 template <typename T> struct S { struct T };
19031 template <typename T> struct S<T>::T { };
19033 we will get a TYPENAME_TYPE when processing the definition of
19034 `S::T'. We need to resolve it to the actual type before we
19035 try to define it. */
19036 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19038 class_type = resolve_typename_type (TREE_TYPE (type),
19039 /*only_current_p=*/false);
19040 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19041 type = TYPE_NAME (class_type);
19042 else
19044 cp_parser_error (parser, "could not resolve typename type");
19045 type = error_mark_node;
19049 if (maybe_process_partial_specialization (TREE_TYPE (type))
19050 == error_mark_node)
19052 type = NULL_TREE;
19053 goto done;
19056 class_type = current_class_type;
19057 /* Enter the scope indicated by the nested-name-specifier. */
19058 pushed_scope = push_scope (nested_name_specifier);
19059 /* Get the canonical version of this type. */
19060 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19061 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19062 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19064 type = push_template_decl (type);
19065 if (type == error_mark_node)
19067 type = NULL_TREE;
19068 goto done;
19072 type = TREE_TYPE (type);
19073 *nested_name_specifier_p = true;
19075 else /* The name is not a nested name. */
19077 /* If the class was unnamed, create a dummy name. */
19078 if (!id)
19079 id = make_anon_name ();
19080 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19081 parser->num_template_parameter_lists);
19084 /* Indicate whether this class was declared as a `class' or as a
19085 `struct'. */
19086 if (TREE_CODE (type) == RECORD_TYPE)
19087 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19088 cp_parser_check_class_key (class_key, type);
19090 /* If this type was already complete, and we see another definition,
19091 that's an error. */
19092 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19094 error_at (type_start_token->location, "redefinition of %q#T",
19095 type);
19096 error_at (type_start_token->location, "previous definition of %q+#T",
19097 type);
19098 type = NULL_TREE;
19099 goto done;
19101 else if (type == error_mark_node)
19102 type = NULL_TREE;
19104 if (type)
19106 /* Apply attributes now, before any use of the class as a template
19107 argument in its base list. */
19108 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19109 fixup_attribute_variants (type);
19112 /* We will have entered the scope containing the class; the names of
19113 base classes should be looked up in that context. For example:
19115 struct A { struct B {}; struct C; };
19116 struct A::C : B {};
19118 is valid. */
19120 /* Get the list of base-classes, if there is one. */
19121 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19122 bases = cp_parser_base_clause (parser);
19123 else
19124 bases = NULL_TREE;
19126 /* If we're really defining a class, process the base classes.
19127 If they're invalid, fail. */
19128 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19129 && !xref_basetypes (type, bases))
19130 type = NULL_TREE;
19132 done:
19133 /* Leave the scope given by the nested-name-specifier. We will
19134 enter the class scope itself while processing the members. */
19135 if (pushed_scope)
19136 pop_scope (pushed_scope);
19138 if (invalid_explicit_specialization_p)
19140 end_specialization ();
19141 --parser->num_template_parameter_lists;
19144 if (type)
19145 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19146 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19147 CLASSTYPE_FINAL (type) = 1;
19148 out:
19149 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19150 return type;
19153 /* Parse a class-key.
19155 class-key:
19156 class
19157 struct
19158 union
19160 Returns the kind of class-key specified, or none_type to indicate
19161 error. */
19163 static enum tag_types
19164 cp_parser_class_key (cp_parser* parser)
19166 cp_token *token;
19167 enum tag_types tag_type;
19169 /* Look for the class-key. */
19170 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19171 if (!token)
19172 return none_type;
19174 /* Check to see if the TOKEN is a class-key. */
19175 tag_type = cp_parser_token_is_class_key (token);
19176 if (!tag_type)
19177 cp_parser_error (parser, "expected class-key");
19178 return tag_type;
19181 /* Parse an (optional) member-specification.
19183 member-specification:
19184 member-declaration member-specification [opt]
19185 access-specifier : member-specification [opt] */
19187 static void
19188 cp_parser_member_specification_opt (cp_parser* parser)
19190 while (true)
19192 cp_token *token;
19193 enum rid keyword;
19195 /* Peek at the next token. */
19196 token = cp_lexer_peek_token (parser->lexer);
19197 /* If it's a `}', or EOF then we've seen all the members. */
19198 if (token->type == CPP_CLOSE_BRACE
19199 || token->type == CPP_EOF
19200 || token->type == CPP_PRAGMA_EOL)
19201 break;
19203 /* See if this token is a keyword. */
19204 keyword = token->keyword;
19205 switch (keyword)
19207 case RID_PUBLIC:
19208 case RID_PROTECTED:
19209 case RID_PRIVATE:
19210 /* Consume the access-specifier. */
19211 cp_lexer_consume_token (parser->lexer);
19212 /* Remember which access-specifier is active. */
19213 current_access_specifier = token->u.value;
19214 /* Look for the `:'. */
19215 cp_parser_require (parser, CPP_COLON, RT_COLON);
19216 break;
19218 default:
19219 /* Accept #pragmas at class scope. */
19220 if (token->type == CPP_PRAGMA)
19222 cp_parser_pragma (parser, pragma_external);
19223 break;
19226 /* Otherwise, the next construction must be a
19227 member-declaration. */
19228 cp_parser_member_declaration (parser);
19233 /* Parse a member-declaration.
19235 member-declaration:
19236 decl-specifier-seq [opt] member-declarator-list [opt] ;
19237 function-definition ; [opt]
19238 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19239 using-declaration
19240 template-declaration
19241 alias-declaration
19243 member-declarator-list:
19244 member-declarator
19245 member-declarator-list , member-declarator
19247 member-declarator:
19248 declarator pure-specifier [opt]
19249 declarator constant-initializer [opt]
19250 identifier [opt] : constant-expression
19252 GNU Extensions:
19254 member-declaration:
19255 __extension__ member-declaration
19257 member-declarator:
19258 declarator attributes [opt] pure-specifier [opt]
19259 declarator attributes [opt] constant-initializer [opt]
19260 identifier [opt] attributes [opt] : constant-expression
19262 C++0x Extensions:
19264 member-declaration:
19265 static_assert-declaration */
19267 static void
19268 cp_parser_member_declaration (cp_parser* parser)
19270 cp_decl_specifier_seq decl_specifiers;
19271 tree prefix_attributes;
19272 tree decl;
19273 int declares_class_or_enum;
19274 bool friend_p;
19275 cp_token *token = NULL;
19276 cp_token *decl_spec_token_start = NULL;
19277 cp_token *initializer_token_start = NULL;
19278 int saved_pedantic;
19279 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19281 /* Check for the `__extension__' keyword. */
19282 if (cp_parser_extension_opt (parser, &saved_pedantic))
19284 /* Recurse. */
19285 cp_parser_member_declaration (parser);
19286 /* Restore the old value of the PEDANTIC flag. */
19287 pedantic = saved_pedantic;
19289 return;
19292 /* Check for a template-declaration. */
19293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19295 /* An explicit specialization here is an error condition, and we
19296 expect the specialization handler to detect and report this. */
19297 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19298 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19299 cp_parser_explicit_specialization (parser);
19300 else
19301 cp_parser_template_declaration (parser, /*member_p=*/true);
19303 return;
19306 /* Check for a using-declaration. */
19307 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19309 if (cxx_dialect < cxx0x)
19311 /* Parse the using-declaration. */
19312 cp_parser_using_declaration (parser,
19313 /*access_declaration_p=*/false);
19314 return;
19316 else
19318 tree decl;
19319 bool alias_decl_expected;
19320 cp_parser_parse_tentatively (parser);
19321 decl = cp_parser_alias_declaration (parser);
19322 /* Note that if we actually see the '=' token after the
19323 identifier, cp_parser_alias_declaration commits the
19324 tentative parse. In that case, we really expects an
19325 alias-declaration. Otherwise, we expect a using
19326 declaration. */
19327 alias_decl_expected =
19328 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19329 cp_parser_parse_definitely (parser);
19331 if (alias_decl_expected)
19332 finish_member_declaration (decl);
19333 else
19334 cp_parser_using_declaration (parser,
19335 /*access_declaration_p=*/false);
19336 return;
19340 /* Check for @defs. */
19341 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19343 tree ivar, member;
19344 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19345 ivar = ivar_chains;
19346 while (ivar)
19348 member = ivar;
19349 ivar = TREE_CHAIN (member);
19350 TREE_CHAIN (member) = NULL_TREE;
19351 finish_member_declaration (member);
19353 return;
19356 /* If the next token is `static_assert' we have a static assertion. */
19357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19359 cp_parser_static_assert (parser, /*member_p=*/true);
19360 return;
19363 parser->colon_corrects_to_scope_p = false;
19365 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19366 goto out;
19368 /* Parse the decl-specifier-seq. */
19369 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19370 cp_parser_decl_specifier_seq (parser,
19371 CP_PARSER_FLAGS_OPTIONAL,
19372 &decl_specifiers,
19373 &declares_class_or_enum);
19374 /* Check for an invalid type-name. */
19375 if (!decl_specifiers.any_type_specifiers_p
19376 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19377 goto out;
19378 /* If there is no declarator, then the decl-specifier-seq should
19379 specify a type. */
19380 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19382 /* If there was no decl-specifier-seq, and the next token is a
19383 `;', then we have something like:
19385 struct S { ; };
19387 [class.mem]
19389 Each member-declaration shall declare at least one member
19390 name of the class. */
19391 if (!decl_specifiers.any_specifiers_p)
19393 cp_token *token = cp_lexer_peek_token (parser->lexer);
19394 if (!in_system_header_at (token->location))
19395 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19397 else
19399 tree type;
19401 /* See if this declaration is a friend. */
19402 friend_p = cp_parser_friend_p (&decl_specifiers);
19403 /* If there were decl-specifiers, check to see if there was
19404 a class-declaration. */
19405 type = check_tag_decl (&decl_specifiers,
19406 /*explicit_type_instantiation_p=*/false);
19407 /* Nested classes have already been added to the class, but
19408 a `friend' needs to be explicitly registered. */
19409 if (friend_p)
19411 /* If the `friend' keyword was present, the friend must
19412 be introduced with a class-key. */
19413 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19414 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19415 "in C++03 a class-key must be used "
19416 "when declaring a friend");
19417 /* In this case:
19419 template <typename T> struct A {
19420 friend struct A<T>::B;
19423 A<T>::B will be represented by a TYPENAME_TYPE, and
19424 therefore not recognized by check_tag_decl. */
19425 if (!type)
19427 type = decl_specifiers.type;
19428 if (type && TREE_CODE (type) == TYPE_DECL)
19429 type = TREE_TYPE (type);
19431 if (!type || !TYPE_P (type))
19432 error_at (decl_spec_token_start->location,
19433 "friend declaration does not name a class or "
19434 "function");
19435 else
19436 make_friend_class (current_class_type, type,
19437 /*complain=*/true);
19439 /* If there is no TYPE, an error message will already have
19440 been issued. */
19441 else if (!type || type == error_mark_node)
19443 /* An anonymous aggregate has to be handled specially; such
19444 a declaration really declares a data member (with a
19445 particular type), as opposed to a nested class. */
19446 else if (ANON_AGGR_TYPE_P (type))
19448 /* C++11 9.5/6. */
19449 if (decl_specifiers.storage_class != sc_none)
19450 error_at (decl_spec_token_start->location,
19451 "a storage class on an anonymous aggregate "
19452 "in class scope is not allowed");
19454 /* Remove constructors and such from TYPE, now that we
19455 know it is an anonymous aggregate. */
19456 fixup_anonymous_aggr (type);
19457 /* And make the corresponding data member. */
19458 decl = build_decl (decl_spec_token_start->location,
19459 FIELD_DECL, NULL_TREE, type);
19460 /* Add it to the class. */
19461 finish_member_declaration (decl);
19463 else
19464 cp_parser_check_access_in_redeclaration
19465 (TYPE_NAME (type),
19466 decl_spec_token_start->location);
19469 else
19471 bool assume_semicolon = false;
19473 /* Clear attributes from the decl_specifiers but keep them
19474 around as prefix attributes that apply them to the entity
19475 being declared. */
19476 prefix_attributes = decl_specifiers.attributes;
19477 decl_specifiers.attributes = NULL_TREE;
19479 /* See if these declarations will be friends. */
19480 friend_p = cp_parser_friend_p (&decl_specifiers);
19482 /* Keep going until we hit the `;' at the end of the
19483 declaration. */
19484 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19486 tree attributes = NULL_TREE;
19487 tree first_attribute;
19489 /* Peek at the next token. */
19490 token = cp_lexer_peek_token (parser->lexer);
19492 /* Check for a bitfield declaration. */
19493 if (token->type == CPP_COLON
19494 || (token->type == CPP_NAME
19495 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19496 == CPP_COLON))
19498 tree identifier;
19499 tree width;
19501 /* Get the name of the bitfield. Note that we cannot just
19502 check TOKEN here because it may have been invalidated by
19503 the call to cp_lexer_peek_nth_token above. */
19504 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19505 identifier = cp_parser_identifier (parser);
19506 else
19507 identifier = NULL_TREE;
19509 /* Consume the `:' token. */
19510 cp_lexer_consume_token (parser->lexer);
19511 /* Get the width of the bitfield. */
19512 width
19513 = cp_parser_constant_expression (parser,
19514 /*allow_non_constant=*/false,
19515 NULL);
19517 /* Look for attributes that apply to the bitfield. */
19518 attributes = cp_parser_attributes_opt (parser);
19519 /* Remember which attributes are prefix attributes and
19520 which are not. */
19521 first_attribute = attributes;
19522 /* Combine the attributes. */
19523 attributes = chainon (prefix_attributes, attributes);
19525 /* Create the bitfield declaration. */
19526 decl = grokbitfield (identifier
19527 ? make_id_declarator (NULL_TREE,
19528 identifier,
19529 sfk_none)
19530 : NULL,
19531 &decl_specifiers,
19532 width,
19533 attributes);
19535 else
19537 cp_declarator *declarator;
19538 tree initializer;
19539 tree asm_specification;
19540 int ctor_dtor_or_conv_p;
19542 /* Parse the declarator. */
19543 declarator
19544 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19545 &ctor_dtor_or_conv_p,
19546 /*parenthesized_p=*/NULL,
19547 /*member_p=*/true);
19549 /* If something went wrong parsing the declarator, make sure
19550 that we at least consume some tokens. */
19551 if (declarator == cp_error_declarator)
19553 /* Skip to the end of the statement. */
19554 cp_parser_skip_to_end_of_statement (parser);
19555 /* If the next token is not a semicolon, that is
19556 probably because we just skipped over the body of
19557 a function. So, we consume a semicolon if
19558 present, but do not issue an error message if it
19559 is not present. */
19560 if (cp_lexer_next_token_is (parser->lexer,
19561 CPP_SEMICOLON))
19562 cp_lexer_consume_token (parser->lexer);
19563 goto out;
19566 if (declares_class_or_enum & 2)
19567 cp_parser_check_for_definition_in_return_type
19568 (declarator, decl_specifiers.type,
19569 decl_specifiers.locations[ds_type_spec]);
19571 /* Look for an asm-specification. */
19572 asm_specification = cp_parser_asm_specification_opt (parser);
19573 /* Look for attributes that apply to the declaration. */
19574 attributes = cp_parser_attributes_opt (parser);
19575 /* Remember which attributes are prefix attributes and
19576 which are not. */
19577 first_attribute = attributes;
19578 /* Combine the attributes. */
19579 attributes = chainon (prefix_attributes, attributes);
19581 /* If it's an `=', then we have a constant-initializer or a
19582 pure-specifier. It is not correct to parse the
19583 initializer before registering the member declaration
19584 since the member declaration should be in scope while
19585 its initializer is processed. However, the rest of the
19586 front end does not yet provide an interface that allows
19587 us to handle this correctly. */
19588 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19590 /* In [class.mem]:
19592 A pure-specifier shall be used only in the declaration of
19593 a virtual function.
19595 A member-declarator can contain a constant-initializer
19596 only if it declares a static member of integral or
19597 enumeration type.
19599 Therefore, if the DECLARATOR is for a function, we look
19600 for a pure-specifier; otherwise, we look for a
19601 constant-initializer. When we call `grokfield', it will
19602 perform more stringent semantics checks. */
19603 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19604 if (function_declarator_p (declarator)
19605 || (decl_specifiers.type
19606 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19607 && declarator->kind == cdk_id
19608 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19609 == FUNCTION_TYPE)))
19610 initializer = cp_parser_pure_specifier (parser);
19611 else if (decl_specifiers.storage_class != sc_static)
19612 initializer = cp_parser_save_nsdmi (parser);
19613 else if (cxx_dialect >= cxx0x)
19615 bool nonconst;
19616 /* Don't require a constant rvalue in C++11, since we
19617 might want a reference constant. We'll enforce
19618 constancy later. */
19619 cp_lexer_consume_token (parser->lexer);
19620 /* Parse the initializer. */
19621 initializer = cp_parser_initializer_clause (parser,
19622 &nonconst);
19624 else
19625 /* Parse the initializer. */
19626 initializer = cp_parser_constant_initializer (parser);
19628 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19629 && !function_declarator_p (declarator))
19631 bool x;
19632 if (decl_specifiers.storage_class != sc_static)
19633 initializer = cp_parser_save_nsdmi (parser);
19634 else
19635 initializer = cp_parser_initializer (parser, &x, &x);
19637 /* Otherwise, there is no initializer. */
19638 else
19639 initializer = NULL_TREE;
19641 /* See if we are probably looking at a function
19642 definition. We are certainly not looking at a
19643 member-declarator. Calling `grokfield' has
19644 side-effects, so we must not do it unless we are sure
19645 that we are looking at a member-declarator. */
19646 if (cp_parser_token_starts_function_definition_p
19647 (cp_lexer_peek_token (parser->lexer)))
19649 /* The grammar does not allow a pure-specifier to be
19650 used when a member function is defined. (It is
19651 possible that this fact is an oversight in the
19652 standard, since a pure function may be defined
19653 outside of the class-specifier. */
19654 if (initializer && initializer_token_start)
19655 error_at (initializer_token_start->location,
19656 "pure-specifier on function-definition");
19657 decl = cp_parser_save_member_function_body (parser,
19658 &decl_specifiers,
19659 declarator,
19660 attributes);
19661 /* If the member was not a friend, declare it here. */
19662 if (!friend_p)
19663 finish_member_declaration (decl);
19664 /* Peek at the next token. */
19665 token = cp_lexer_peek_token (parser->lexer);
19666 /* If the next token is a semicolon, consume it. */
19667 if (token->type == CPP_SEMICOLON)
19668 cp_lexer_consume_token (parser->lexer);
19669 goto out;
19671 else
19672 if (declarator->kind == cdk_function)
19673 declarator->id_loc = token->location;
19674 /* Create the declaration. */
19675 decl = grokfield (declarator, &decl_specifiers,
19676 initializer, /*init_const_expr_p=*/true,
19677 asm_specification,
19678 attributes);
19681 /* Reset PREFIX_ATTRIBUTES. */
19682 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19683 attributes = TREE_CHAIN (attributes);
19684 if (attributes)
19685 TREE_CHAIN (attributes) = NULL_TREE;
19687 /* If there is any qualification still in effect, clear it
19688 now; we will be starting fresh with the next declarator. */
19689 parser->scope = NULL_TREE;
19690 parser->qualifying_scope = NULL_TREE;
19691 parser->object_scope = NULL_TREE;
19692 /* If it's a `,', then there are more declarators. */
19693 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19695 cp_lexer_consume_token (parser->lexer);
19696 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19698 cp_token *token = cp_lexer_previous_token (parser->lexer);
19699 error_at (token->location,
19700 "stray %<,%> at end of member declaration");
19703 /* If the next token isn't a `;', then we have a parse error. */
19704 else if (cp_lexer_next_token_is_not (parser->lexer,
19705 CPP_SEMICOLON))
19707 /* The next token might be a ways away from where the
19708 actual semicolon is missing. Find the previous token
19709 and use that for our error position. */
19710 cp_token *token = cp_lexer_previous_token (parser->lexer);
19711 error_at (token->location,
19712 "expected %<;%> at end of member declaration");
19714 /* Assume that the user meant to provide a semicolon. If
19715 we were to cp_parser_skip_to_end_of_statement, we might
19716 skip to a semicolon inside a member function definition
19717 and issue nonsensical error messages. */
19718 assume_semicolon = true;
19721 if (decl)
19723 /* Add DECL to the list of members. */
19724 if (!friend_p)
19725 finish_member_declaration (decl);
19727 if (TREE_CODE (decl) == FUNCTION_DECL)
19728 cp_parser_save_default_args (parser, decl);
19729 else if (TREE_CODE (decl) == FIELD_DECL
19730 && !DECL_C_BIT_FIELD (decl)
19731 && DECL_INITIAL (decl))
19732 /* Add DECL to the queue of NSDMI to be parsed later. */
19733 vec_safe_push (unparsed_nsdmis, decl);
19736 if (assume_semicolon)
19737 goto out;
19741 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19742 out:
19743 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19746 /* Parse a pure-specifier.
19748 pure-specifier:
19751 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19752 Otherwise, ERROR_MARK_NODE is returned. */
19754 static tree
19755 cp_parser_pure_specifier (cp_parser* parser)
19757 cp_token *token;
19759 /* Look for the `=' token. */
19760 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19761 return error_mark_node;
19762 /* Look for the `0' token. */
19763 token = cp_lexer_peek_token (parser->lexer);
19765 if (token->type == CPP_EOF
19766 || token->type == CPP_PRAGMA_EOL)
19767 return error_mark_node;
19769 cp_lexer_consume_token (parser->lexer);
19771 /* Accept = default or = delete in c++0x mode. */
19772 if (token->keyword == RID_DEFAULT
19773 || token->keyword == RID_DELETE)
19775 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19776 return token->u.value;
19779 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19780 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19782 cp_parser_error (parser,
19783 "invalid pure specifier (only %<= 0%> is allowed)");
19784 cp_parser_skip_to_end_of_statement (parser);
19785 return error_mark_node;
19787 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19789 error_at (token->location, "templates may not be %<virtual%>");
19790 return error_mark_node;
19793 return integer_zero_node;
19796 /* Parse a constant-initializer.
19798 constant-initializer:
19799 = constant-expression
19801 Returns a representation of the constant-expression. */
19803 static tree
19804 cp_parser_constant_initializer (cp_parser* parser)
19806 /* Look for the `=' token. */
19807 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19808 return error_mark_node;
19810 /* It is invalid to write:
19812 struct S { static const int i = { 7 }; };
19815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19817 cp_parser_error (parser,
19818 "a brace-enclosed initializer is not allowed here");
19819 /* Consume the opening brace. */
19820 cp_lexer_consume_token (parser->lexer);
19821 /* Skip the initializer. */
19822 cp_parser_skip_to_closing_brace (parser);
19823 /* Look for the trailing `}'. */
19824 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19826 return error_mark_node;
19829 return cp_parser_constant_expression (parser,
19830 /*allow_non_constant=*/false,
19831 NULL);
19834 /* Derived classes [gram.class.derived] */
19836 /* Parse a base-clause.
19838 base-clause:
19839 : base-specifier-list
19841 base-specifier-list:
19842 base-specifier ... [opt]
19843 base-specifier-list , base-specifier ... [opt]
19845 Returns a TREE_LIST representing the base-classes, in the order in
19846 which they were declared. The representation of each node is as
19847 described by cp_parser_base_specifier.
19849 In the case that no bases are specified, this function will return
19850 NULL_TREE, not ERROR_MARK_NODE. */
19852 static tree
19853 cp_parser_base_clause (cp_parser* parser)
19855 tree bases = NULL_TREE;
19857 /* Look for the `:' that begins the list. */
19858 cp_parser_require (parser, CPP_COLON, RT_COLON);
19860 /* Scan the base-specifier-list. */
19861 while (true)
19863 cp_token *token;
19864 tree base;
19865 bool pack_expansion_p = false;
19867 /* Look for the base-specifier. */
19868 base = cp_parser_base_specifier (parser);
19869 /* Look for the (optional) ellipsis. */
19870 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19872 /* Consume the `...'. */
19873 cp_lexer_consume_token (parser->lexer);
19875 pack_expansion_p = true;
19878 /* Add BASE to the front of the list. */
19879 if (base && base != error_mark_node)
19881 if (pack_expansion_p)
19882 /* Make this a pack expansion type. */
19883 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19885 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19887 TREE_CHAIN (base) = bases;
19888 bases = base;
19891 /* Peek at the next token. */
19892 token = cp_lexer_peek_token (parser->lexer);
19893 /* If it's not a comma, then the list is complete. */
19894 if (token->type != CPP_COMMA)
19895 break;
19896 /* Consume the `,'. */
19897 cp_lexer_consume_token (parser->lexer);
19900 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19901 base class had a qualified name. However, the next name that
19902 appears is certainly not qualified. */
19903 parser->scope = NULL_TREE;
19904 parser->qualifying_scope = NULL_TREE;
19905 parser->object_scope = NULL_TREE;
19907 return nreverse (bases);
19910 /* Parse a base-specifier.
19912 base-specifier:
19913 :: [opt] nested-name-specifier [opt] class-name
19914 virtual access-specifier [opt] :: [opt] nested-name-specifier
19915 [opt] class-name
19916 access-specifier virtual [opt] :: [opt] nested-name-specifier
19917 [opt] class-name
19919 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19920 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19921 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19922 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19924 static tree
19925 cp_parser_base_specifier (cp_parser* parser)
19927 cp_token *token;
19928 bool done = false;
19929 bool virtual_p = false;
19930 bool duplicate_virtual_error_issued_p = false;
19931 bool duplicate_access_error_issued_p = false;
19932 bool class_scope_p, template_p;
19933 tree access = access_default_node;
19934 tree type;
19936 /* Process the optional `virtual' and `access-specifier'. */
19937 while (!done)
19939 /* Peek at the next token. */
19940 token = cp_lexer_peek_token (parser->lexer);
19941 /* Process `virtual'. */
19942 switch (token->keyword)
19944 case RID_VIRTUAL:
19945 /* If `virtual' appears more than once, issue an error. */
19946 if (virtual_p && !duplicate_virtual_error_issued_p)
19948 cp_parser_error (parser,
19949 "%<virtual%> specified more than once in base-specified");
19950 duplicate_virtual_error_issued_p = true;
19953 virtual_p = true;
19955 /* Consume the `virtual' token. */
19956 cp_lexer_consume_token (parser->lexer);
19958 break;
19960 case RID_PUBLIC:
19961 case RID_PROTECTED:
19962 case RID_PRIVATE:
19963 /* If more than one access specifier appears, issue an
19964 error. */
19965 if (access != access_default_node
19966 && !duplicate_access_error_issued_p)
19968 cp_parser_error (parser,
19969 "more than one access specifier in base-specified");
19970 duplicate_access_error_issued_p = true;
19973 access = ridpointers[(int) token->keyword];
19975 /* Consume the access-specifier. */
19976 cp_lexer_consume_token (parser->lexer);
19978 break;
19980 default:
19981 done = true;
19982 break;
19985 /* It is not uncommon to see programs mechanically, erroneously, use
19986 the 'typename' keyword to denote (dependent) qualified types
19987 as base classes. */
19988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19990 token = cp_lexer_peek_token (parser->lexer);
19991 if (!processing_template_decl)
19992 error_at (token->location,
19993 "keyword %<typename%> not allowed outside of templates");
19994 else
19995 error_at (token->location,
19996 "keyword %<typename%> not allowed in this context "
19997 "(the base class is implicitly a type)");
19998 cp_lexer_consume_token (parser->lexer);
20001 /* Look for the optional `::' operator. */
20002 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20003 /* Look for the nested-name-specifier. The simplest way to
20004 implement:
20006 [temp.res]
20008 The keyword `typename' is not permitted in a base-specifier or
20009 mem-initializer; in these contexts a qualified name that
20010 depends on a template-parameter is implicitly assumed to be a
20011 type name.
20013 is to pretend that we have seen the `typename' keyword at this
20014 point. */
20015 cp_parser_nested_name_specifier_opt (parser,
20016 /*typename_keyword_p=*/true,
20017 /*check_dependency_p=*/true,
20018 typename_type,
20019 /*is_declaration=*/true);
20020 /* If the base class is given by a qualified name, assume that names
20021 we see are type names or templates, as appropriate. */
20022 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20023 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20025 if (!parser->scope
20026 && cp_lexer_next_token_is_decltype (parser->lexer))
20027 /* DR 950 allows decltype as a base-specifier. */
20028 type = cp_parser_decltype (parser);
20029 else
20031 /* Otherwise, look for the class-name. */
20032 type = cp_parser_class_name (parser,
20033 class_scope_p,
20034 template_p,
20035 typename_type,
20036 /*check_dependency_p=*/true,
20037 /*class_head_p=*/false,
20038 /*is_declaration=*/true);
20039 type = TREE_TYPE (type);
20042 if (type == error_mark_node)
20043 return error_mark_node;
20045 return finish_base_specifier (type, access, virtual_p);
20048 /* Exception handling [gram.exception] */
20050 /* Parse an (optional) noexcept-specification.
20052 noexcept-specification:
20053 noexcept ( constant-expression ) [opt]
20055 If no noexcept-specification is present, returns NULL_TREE.
20056 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20057 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20058 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20059 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20060 in which case a boolean condition is returned instead. */
20062 static tree
20063 cp_parser_noexcept_specification_opt (cp_parser* parser,
20064 bool require_constexpr,
20065 bool* consumed_expr,
20066 bool return_cond)
20068 cp_token *token;
20069 const char *saved_message;
20071 /* Peek at the next token. */
20072 token = cp_lexer_peek_token (parser->lexer);
20074 /* Is it a noexcept-specification? */
20075 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20077 tree expr;
20078 cp_lexer_consume_token (parser->lexer);
20080 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20082 cp_lexer_consume_token (parser->lexer);
20084 if (require_constexpr)
20086 /* Types may not be defined in an exception-specification. */
20087 saved_message = parser->type_definition_forbidden_message;
20088 parser->type_definition_forbidden_message
20089 = G_("types may not be defined in an exception-specification");
20091 expr = cp_parser_constant_expression (parser, false, NULL);
20093 /* Restore the saved message. */
20094 parser->type_definition_forbidden_message = saved_message;
20096 else
20098 expr = cp_parser_expression (parser, false, NULL);
20099 *consumed_expr = true;
20102 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20104 else
20106 expr = boolean_true_node;
20107 if (!require_constexpr)
20108 *consumed_expr = false;
20111 /* We cannot build a noexcept-spec right away because this will check
20112 that expr is a constexpr. */
20113 if (!return_cond)
20114 return build_noexcept_spec (expr, tf_warning_or_error);
20115 else
20116 return expr;
20118 else
20119 return NULL_TREE;
20122 /* Parse an (optional) exception-specification.
20124 exception-specification:
20125 throw ( type-id-list [opt] )
20127 Returns a TREE_LIST representing the exception-specification. The
20128 TREE_VALUE of each node is a type. */
20130 static tree
20131 cp_parser_exception_specification_opt (cp_parser* parser)
20133 cp_token *token;
20134 tree type_id_list;
20135 const char *saved_message;
20137 /* Peek at the next token. */
20138 token = cp_lexer_peek_token (parser->lexer);
20140 /* Is it a noexcept-specification? */
20141 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20142 false);
20143 if (type_id_list != NULL_TREE)
20144 return type_id_list;
20146 /* If it's not `throw', then there's no exception-specification. */
20147 if (!cp_parser_is_keyword (token, RID_THROW))
20148 return NULL_TREE;
20150 #if 0
20151 /* Enable this once a lot of code has transitioned to noexcept? */
20152 if (cxx_dialect >= cxx0x && !in_system_header)
20153 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20154 "deprecated in C++0x; use %<noexcept%> instead");
20155 #endif
20157 /* Consume the `throw'. */
20158 cp_lexer_consume_token (parser->lexer);
20160 /* Look for the `('. */
20161 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20163 /* Peek at the next token. */
20164 token = cp_lexer_peek_token (parser->lexer);
20165 /* If it's not a `)', then there is a type-id-list. */
20166 if (token->type != CPP_CLOSE_PAREN)
20168 /* Types may not be defined in an exception-specification. */
20169 saved_message = parser->type_definition_forbidden_message;
20170 parser->type_definition_forbidden_message
20171 = G_("types may not be defined in an exception-specification");
20172 /* Parse the type-id-list. */
20173 type_id_list = cp_parser_type_id_list (parser);
20174 /* Restore the saved message. */
20175 parser->type_definition_forbidden_message = saved_message;
20177 else
20178 type_id_list = empty_except_spec;
20180 /* Look for the `)'. */
20181 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20183 return type_id_list;
20186 /* Parse an (optional) type-id-list.
20188 type-id-list:
20189 type-id ... [opt]
20190 type-id-list , type-id ... [opt]
20192 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20193 in the order that the types were presented. */
20195 static tree
20196 cp_parser_type_id_list (cp_parser* parser)
20198 tree types = NULL_TREE;
20200 while (true)
20202 cp_token *token;
20203 tree type;
20205 /* Get the next type-id. */
20206 type = cp_parser_type_id (parser);
20207 /* Parse the optional ellipsis. */
20208 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20210 /* Consume the `...'. */
20211 cp_lexer_consume_token (parser->lexer);
20213 /* Turn the type into a pack expansion expression. */
20214 type = make_pack_expansion (type);
20216 /* Add it to the list. */
20217 types = add_exception_specifier (types, type, /*complain=*/1);
20218 /* Peek at the next token. */
20219 token = cp_lexer_peek_token (parser->lexer);
20220 /* If it is not a `,', we are done. */
20221 if (token->type != CPP_COMMA)
20222 break;
20223 /* Consume the `,'. */
20224 cp_lexer_consume_token (parser->lexer);
20227 return nreverse (types);
20230 /* Parse a try-block.
20232 try-block:
20233 try compound-statement handler-seq */
20235 static tree
20236 cp_parser_try_block (cp_parser* parser)
20238 tree try_block;
20240 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20241 try_block = begin_try_block ();
20242 cp_parser_compound_statement (parser, NULL, true, false);
20243 finish_try_block (try_block);
20244 cp_parser_handler_seq (parser);
20245 finish_handler_sequence (try_block);
20247 return try_block;
20250 /* Parse a function-try-block.
20252 function-try-block:
20253 try ctor-initializer [opt] function-body handler-seq */
20255 static bool
20256 cp_parser_function_try_block (cp_parser* parser)
20258 tree compound_stmt;
20259 tree try_block;
20260 bool ctor_initializer_p;
20262 /* Look for the `try' keyword. */
20263 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20264 return false;
20265 /* Let the rest of the front end know where we are. */
20266 try_block = begin_function_try_block (&compound_stmt);
20267 /* Parse the function-body. */
20268 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20269 (parser, /*in_function_try_block=*/true);
20270 /* We're done with the `try' part. */
20271 finish_function_try_block (try_block);
20272 /* Parse the handlers. */
20273 cp_parser_handler_seq (parser);
20274 /* We're done with the handlers. */
20275 finish_function_handler_sequence (try_block, compound_stmt);
20277 return ctor_initializer_p;
20280 /* Parse a handler-seq.
20282 handler-seq:
20283 handler handler-seq [opt] */
20285 static void
20286 cp_parser_handler_seq (cp_parser* parser)
20288 while (true)
20290 cp_token *token;
20292 /* Parse the handler. */
20293 cp_parser_handler (parser);
20294 /* Peek at the next token. */
20295 token = cp_lexer_peek_token (parser->lexer);
20296 /* If it's not `catch' then there are no more handlers. */
20297 if (!cp_parser_is_keyword (token, RID_CATCH))
20298 break;
20302 /* Parse a handler.
20304 handler:
20305 catch ( exception-declaration ) compound-statement */
20307 static void
20308 cp_parser_handler (cp_parser* parser)
20310 tree handler;
20311 tree declaration;
20313 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20314 handler = begin_handler ();
20315 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20316 declaration = cp_parser_exception_declaration (parser);
20317 finish_handler_parms (declaration, handler);
20318 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20319 cp_parser_compound_statement (parser, NULL, false, false);
20320 finish_handler (handler);
20323 /* Parse an exception-declaration.
20325 exception-declaration:
20326 type-specifier-seq declarator
20327 type-specifier-seq abstract-declarator
20328 type-specifier-seq
20331 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20332 ellipsis variant is used. */
20334 static tree
20335 cp_parser_exception_declaration (cp_parser* parser)
20337 cp_decl_specifier_seq type_specifiers;
20338 cp_declarator *declarator;
20339 const char *saved_message;
20341 /* If it's an ellipsis, it's easy to handle. */
20342 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20344 /* Consume the `...' token. */
20345 cp_lexer_consume_token (parser->lexer);
20346 return NULL_TREE;
20349 /* Types may not be defined in exception-declarations. */
20350 saved_message = parser->type_definition_forbidden_message;
20351 parser->type_definition_forbidden_message
20352 = G_("types may not be defined in exception-declarations");
20354 /* Parse the type-specifier-seq. */
20355 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20356 /*is_trailing_return=*/false,
20357 &type_specifiers);
20358 /* If it's a `)', then there is no declarator. */
20359 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20360 declarator = NULL;
20361 else
20362 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20363 /*ctor_dtor_or_conv_p=*/NULL,
20364 /*parenthesized_p=*/NULL,
20365 /*member_p=*/false);
20367 /* Restore the saved message. */
20368 parser->type_definition_forbidden_message = saved_message;
20370 if (!type_specifiers.any_specifiers_p)
20371 return error_mark_node;
20373 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20376 /* Parse a throw-expression.
20378 throw-expression:
20379 throw assignment-expression [opt]
20381 Returns a THROW_EXPR representing the throw-expression. */
20383 static tree
20384 cp_parser_throw_expression (cp_parser* parser)
20386 tree expression;
20387 cp_token* token;
20389 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20390 token = cp_lexer_peek_token (parser->lexer);
20391 /* Figure out whether or not there is an assignment-expression
20392 following the "throw" keyword. */
20393 if (token->type == CPP_COMMA
20394 || token->type == CPP_SEMICOLON
20395 || token->type == CPP_CLOSE_PAREN
20396 || token->type == CPP_CLOSE_SQUARE
20397 || token->type == CPP_CLOSE_BRACE
20398 || token->type == CPP_COLON)
20399 expression = NULL_TREE;
20400 else
20401 expression = cp_parser_assignment_expression (parser,
20402 /*cast_p=*/false, NULL);
20404 return build_throw (expression);
20407 /* GNU Extensions */
20409 /* Parse an (optional) asm-specification.
20411 asm-specification:
20412 asm ( string-literal )
20414 If the asm-specification is present, returns a STRING_CST
20415 corresponding to the string-literal. Otherwise, returns
20416 NULL_TREE. */
20418 static tree
20419 cp_parser_asm_specification_opt (cp_parser* parser)
20421 cp_token *token;
20422 tree asm_specification;
20424 /* Peek at the next token. */
20425 token = cp_lexer_peek_token (parser->lexer);
20426 /* If the next token isn't the `asm' keyword, then there's no
20427 asm-specification. */
20428 if (!cp_parser_is_keyword (token, RID_ASM))
20429 return NULL_TREE;
20431 /* Consume the `asm' token. */
20432 cp_lexer_consume_token (parser->lexer);
20433 /* Look for the `('. */
20434 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20436 /* Look for the string-literal. */
20437 asm_specification = cp_parser_string_literal (parser, false, false);
20439 /* Look for the `)'. */
20440 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20442 return asm_specification;
20445 /* Parse an asm-operand-list.
20447 asm-operand-list:
20448 asm-operand
20449 asm-operand-list , asm-operand
20451 asm-operand:
20452 string-literal ( expression )
20453 [ string-literal ] string-literal ( expression )
20455 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20456 each node is the expression. The TREE_PURPOSE is itself a
20457 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20458 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20459 is a STRING_CST for the string literal before the parenthesis. Returns
20460 ERROR_MARK_NODE if any of the operands are invalid. */
20462 static tree
20463 cp_parser_asm_operand_list (cp_parser* parser)
20465 tree asm_operands = NULL_TREE;
20466 bool invalid_operands = false;
20468 while (true)
20470 tree string_literal;
20471 tree expression;
20472 tree name;
20474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20476 /* Consume the `[' token. */
20477 cp_lexer_consume_token (parser->lexer);
20478 /* Read the operand name. */
20479 name = cp_parser_identifier (parser);
20480 if (name != error_mark_node)
20481 name = build_string (IDENTIFIER_LENGTH (name),
20482 IDENTIFIER_POINTER (name));
20483 /* Look for the closing `]'. */
20484 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20486 else
20487 name = NULL_TREE;
20488 /* Look for the string-literal. */
20489 string_literal = cp_parser_string_literal (parser, false, false);
20491 /* Look for the `('. */
20492 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20493 /* Parse the expression. */
20494 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20495 /* Look for the `)'. */
20496 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20498 if (name == error_mark_node
20499 || string_literal == error_mark_node
20500 || expression == error_mark_node)
20501 invalid_operands = true;
20503 /* Add this operand to the list. */
20504 asm_operands = tree_cons (build_tree_list (name, string_literal),
20505 expression,
20506 asm_operands);
20507 /* If the next token is not a `,', there are no more
20508 operands. */
20509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20510 break;
20511 /* Consume the `,'. */
20512 cp_lexer_consume_token (parser->lexer);
20515 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20518 /* Parse an asm-clobber-list.
20520 asm-clobber-list:
20521 string-literal
20522 asm-clobber-list , string-literal
20524 Returns a TREE_LIST, indicating the clobbers in the order that they
20525 appeared. The TREE_VALUE of each node is a STRING_CST. */
20527 static tree
20528 cp_parser_asm_clobber_list (cp_parser* parser)
20530 tree clobbers = NULL_TREE;
20532 while (true)
20534 tree string_literal;
20536 /* Look for the string literal. */
20537 string_literal = cp_parser_string_literal (parser, false, false);
20538 /* Add it to the list. */
20539 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20540 /* If the next token is not a `,', then the list is
20541 complete. */
20542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20543 break;
20544 /* Consume the `,' token. */
20545 cp_lexer_consume_token (parser->lexer);
20548 return clobbers;
20551 /* Parse an asm-label-list.
20553 asm-label-list:
20554 identifier
20555 asm-label-list , identifier
20557 Returns a TREE_LIST, indicating the labels in the order that they
20558 appeared. The TREE_VALUE of each node is a label. */
20560 static tree
20561 cp_parser_asm_label_list (cp_parser* parser)
20563 tree labels = NULL_TREE;
20565 while (true)
20567 tree identifier, label, name;
20569 /* Look for the identifier. */
20570 identifier = cp_parser_identifier (parser);
20571 if (!error_operand_p (identifier))
20573 label = lookup_label (identifier);
20574 if (TREE_CODE (label) == LABEL_DECL)
20576 TREE_USED (label) = 1;
20577 check_goto (label);
20578 name = build_string (IDENTIFIER_LENGTH (identifier),
20579 IDENTIFIER_POINTER (identifier));
20580 labels = tree_cons (name, label, labels);
20583 /* If the next token is not a `,', then the list is
20584 complete. */
20585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20586 break;
20587 /* Consume the `,' token. */
20588 cp_lexer_consume_token (parser->lexer);
20591 return nreverse (labels);
20594 /* Return TRUE iff the next tokens in the stream are possibly the
20595 beginning of a GNU extension attribute. */
20597 static bool
20598 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20600 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20603 /* Return TRUE iff the next tokens in the stream are possibly the
20604 beginning of a standard C++-11 attribute specifier. */
20606 static bool
20607 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20609 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20612 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20613 beginning of a standard C++-11 attribute specifier. */
20615 static bool
20616 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20618 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20620 return (cxx_dialect >= cxx0x
20621 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20622 || (token->type == CPP_OPEN_SQUARE
20623 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20624 && token->type == CPP_OPEN_SQUARE)));
20627 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20628 beginning of a GNU extension attribute. */
20630 static bool
20631 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20633 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20635 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20638 /* Return true iff the next tokens can be the beginning of either a
20639 GNU attribute list, or a standard C++11 attribute sequence. */
20641 static bool
20642 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20644 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20645 || cp_next_tokens_can_be_std_attribute_p (parser));
20648 /* Return true iff the next Nth tokens can be the beginning of either
20649 a GNU attribute list, or a standard C++11 attribute sequence. */
20651 static bool
20652 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20654 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20655 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20658 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20659 of GNU attributes, or return NULL. */
20661 static tree
20662 cp_parser_attributes_opt (cp_parser *parser)
20664 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20665 return cp_parser_gnu_attributes_opt (parser);
20666 return cp_parser_std_attribute_spec_seq (parser);
20669 /* Parse an (optional) series of attributes.
20671 attributes:
20672 attributes attribute
20674 attribute:
20675 __attribute__ (( attribute-list [opt] ))
20677 The return value is as for cp_parser_gnu_attribute_list. */
20679 static tree
20680 cp_parser_gnu_attributes_opt (cp_parser* parser)
20682 tree attributes = NULL_TREE;
20684 while (true)
20686 cp_token *token;
20687 tree attribute_list;
20688 bool ok = true;
20690 /* Peek at the next token. */
20691 token = cp_lexer_peek_token (parser->lexer);
20692 /* If it's not `__attribute__', then we're done. */
20693 if (token->keyword != RID_ATTRIBUTE)
20694 break;
20696 /* Consume the `__attribute__' keyword. */
20697 cp_lexer_consume_token (parser->lexer);
20698 /* Look for the two `(' tokens. */
20699 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20700 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20702 /* Peek at the next token. */
20703 token = cp_lexer_peek_token (parser->lexer);
20704 if (token->type != CPP_CLOSE_PAREN)
20705 /* Parse the attribute-list. */
20706 attribute_list = cp_parser_gnu_attribute_list (parser);
20707 else
20708 /* If the next token is a `)', then there is no attribute
20709 list. */
20710 attribute_list = NULL;
20712 /* Look for the two `)' tokens. */
20713 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20714 ok = false;
20715 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20716 ok = false;
20717 if (!ok)
20718 cp_parser_skip_to_end_of_statement (parser);
20720 /* Add these new attributes to the list. */
20721 attributes = chainon (attributes, attribute_list);
20724 return attributes;
20727 /* Parse a GNU attribute-list.
20729 attribute-list:
20730 attribute
20731 attribute-list , attribute
20733 attribute:
20734 identifier
20735 identifier ( identifier )
20736 identifier ( identifier , expression-list )
20737 identifier ( expression-list )
20739 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20740 to an attribute. The TREE_PURPOSE of each node is the identifier
20741 indicating which attribute is in use. The TREE_VALUE represents
20742 the arguments, if any. */
20744 static tree
20745 cp_parser_gnu_attribute_list (cp_parser* parser)
20747 tree attribute_list = NULL_TREE;
20748 bool save_translate_strings_p = parser->translate_strings_p;
20750 parser->translate_strings_p = false;
20751 while (true)
20753 cp_token *token;
20754 tree identifier;
20755 tree attribute;
20757 /* Look for the identifier. We also allow keywords here; for
20758 example `__attribute__ ((const))' is legal. */
20759 token = cp_lexer_peek_token (parser->lexer);
20760 if (token->type == CPP_NAME
20761 || token->type == CPP_KEYWORD)
20763 tree arguments = NULL_TREE;
20765 /* Consume the token. */
20766 token = cp_lexer_consume_token (parser->lexer);
20768 /* Save away the identifier that indicates which attribute
20769 this is. */
20770 identifier = (token->type == CPP_KEYWORD)
20771 /* For keywords, use the canonical spelling, not the
20772 parsed identifier. */
20773 ? ridpointers[(int) token->keyword]
20774 : token->u.value;
20776 attribute = build_tree_list (identifier, NULL_TREE);
20778 /* Peek at the next token. */
20779 token = cp_lexer_peek_token (parser->lexer);
20780 /* If it's an `(', then parse the attribute arguments. */
20781 if (token->type == CPP_OPEN_PAREN)
20783 vec<tree, va_gc> *vec;
20784 int attr_flag = (attribute_takes_identifier_p (identifier)
20785 ? id_attr : normal_attr);
20786 vec = cp_parser_parenthesized_expression_list
20787 (parser, attr_flag, /*cast_p=*/false,
20788 /*allow_expansion_p=*/false,
20789 /*non_constant_p=*/NULL);
20790 if (vec == NULL)
20791 arguments = error_mark_node;
20792 else
20794 arguments = build_tree_list_vec (vec);
20795 release_tree_vector (vec);
20797 /* Save the arguments away. */
20798 TREE_VALUE (attribute) = arguments;
20801 if (arguments != error_mark_node)
20803 /* Add this attribute to the list. */
20804 TREE_CHAIN (attribute) = attribute_list;
20805 attribute_list = attribute;
20808 token = cp_lexer_peek_token (parser->lexer);
20810 /* Now, look for more attributes. If the next token isn't a
20811 `,', we're done. */
20812 if (token->type != CPP_COMMA)
20813 break;
20815 /* Consume the comma and keep going. */
20816 cp_lexer_consume_token (parser->lexer);
20818 parser->translate_strings_p = save_translate_strings_p;
20820 /* We built up the list in reverse order. */
20821 return nreverse (attribute_list);
20824 /* Parse a standard C++11 attribute.
20826 The returned representation is a TREE_LIST which TREE_PURPOSE is
20827 the scoped name of the attribute, and the TREE_VALUE is its
20828 arguments list.
20830 Note that the scoped name of the attribute is itself a TREE_LIST
20831 which TREE_PURPOSE is the namespace of the attribute, and
20832 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20833 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20834 and which TREE_PURPOSE is directly the attribute name.
20836 Clients of the attribute code should use get_attribute_namespace
20837 and get_attribute_name to get the actual namespace and name of
20838 attributes, regardless of their being GNU or C++11 attributes.
20840 attribute:
20841 attribute-token attribute-argument-clause [opt]
20843 attribute-token:
20844 identifier
20845 attribute-scoped-token
20847 attribute-scoped-token:
20848 attribute-namespace :: identifier
20850 attribute-namespace:
20851 identifier
20853 attribute-argument-clause:
20854 ( balanced-token-seq )
20856 balanced-token-seq:
20857 balanced-token [opt]
20858 balanced-token-seq balanced-token
20860 balanced-token:
20861 ( balanced-token-seq )
20862 [ balanced-token-seq ]
20863 { balanced-token-seq }. */
20865 static tree
20866 cp_parser_std_attribute (cp_parser *parser)
20868 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20869 cp_token *token;
20871 /* First, parse name of the the attribute, a.k.a
20872 attribute-token. */
20874 token = cp_lexer_peek_token (parser->lexer);
20875 if (token->type == CPP_NAME)
20876 attr_id = token->u.value;
20877 else if (token->type == CPP_KEYWORD)
20878 attr_id = ridpointers[(int) token->keyword];
20879 else if (token->flags & NAMED_OP)
20880 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20882 if (attr_id == NULL_TREE)
20883 return NULL_TREE;
20885 cp_lexer_consume_token (parser->lexer);
20887 token = cp_lexer_peek_token (parser->lexer);
20888 if (token->type == CPP_SCOPE)
20890 /* We are seeing a scoped attribute token. */
20892 cp_lexer_consume_token (parser->lexer);
20893 attr_ns = attr_id;
20895 token = cp_lexer_consume_token (parser->lexer);
20896 if (token->type == CPP_NAME)
20897 attr_id = token->u.value;
20898 else if (token->type == CPP_KEYWORD)
20899 attr_id = ridpointers[(int) token->keyword];
20900 else
20902 error_at (token->location,
20903 "expected an identifier for the attribute name");
20904 return error_mark_node;
20906 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20907 NULL_TREE);
20908 token = cp_lexer_peek_token (parser->lexer);
20910 else
20911 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20912 NULL_TREE);
20914 /* Now parse the optional argument clause of the attribute. */
20916 if (token->type != CPP_OPEN_PAREN)
20917 return attribute;
20920 vec<tree, va_gc> *vec;
20921 int attr_flag = normal_attr;
20923 if (attr_ns == get_identifier ("gnu")
20924 && attribute_takes_identifier_p (attr_id))
20925 /* A GNU attribute that takes an identifier in parameter. */
20926 attr_flag = id_attr;
20928 vec = cp_parser_parenthesized_expression_list
20929 (parser, attr_flag, /*cast_p=*/false,
20930 /*allow_expansion_p=*/true,
20931 /*non_constant_p=*/NULL);
20932 if (vec == NULL)
20933 arguments = error_mark_node;
20934 else
20936 arguments = build_tree_list_vec (vec);
20937 release_tree_vector (vec);
20940 if (arguments == error_mark_node)
20941 attribute = error_mark_node;
20942 else
20943 TREE_VALUE (attribute) = arguments;
20946 return attribute;
20949 /* Parse a list of standard C++-11 attributes.
20951 attribute-list:
20952 attribute [opt]
20953 attribute-list , attribute[opt]
20954 attribute ...
20955 attribute-list , attribute ...
20958 static tree
20959 cp_parser_std_attribute_list (cp_parser *parser)
20961 tree attributes = NULL_TREE, attribute = NULL_TREE;
20962 cp_token *token = NULL;
20964 while (true)
20966 attribute = cp_parser_std_attribute (parser);
20967 if (attribute == error_mark_node)
20968 break;
20969 if (attribute != NULL_TREE)
20971 TREE_CHAIN (attribute) = attributes;
20972 attributes = attribute;
20974 token = cp_lexer_peek_token (parser->lexer);
20975 if (token->type != CPP_COMMA)
20976 break;
20977 cp_lexer_consume_token (parser->lexer);
20979 attributes = nreverse (attributes);
20980 return attributes;
20983 /* Parse a standard C++-11 attribute specifier.
20985 attribute-specifier:
20986 [ [ attribute-list ] ]
20987 alignment-specifier
20989 alignment-specifier:
20990 alignas ( type-id ... [opt] )
20991 alignas ( alignment-expression ... [opt] ). */
20993 static tree
20994 cp_parser_std_attribute_spec (cp_parser *parser)
20996 tree attributes = NULL_TREE;
20997 cp_token *token = cp_lexer_peek_token (parser->lexer);
20999 if (token->type == CPP_OPEN_SQUARE
21000 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21002 cp_lexer_consume_token (parser->lexer);
21003 cp_lexer_consume_token (parser->lexer);
21005 attributes = cp_parser_std_attribute_list (parser);
21007 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21008 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21009 cp_parser_skip_to_end_of_statement (parser);
21010 else
21011 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21012 when we are sure that we have actually parsed them. */
21013 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21015 else
21017 tree alignas_expr;
21019 /* Look for an alignment-specifier. */
21021 token = cp_lexer_peek_token (parser->lexer);
21023 if (token->type != CPP_KEYWORD
21024 || token->keyword != RID_ALIGNAS)
21025 return NULL_TREE;
21027 cp_lexer_consume_token (parser->lexer);
21028 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21030 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21032 cp_parser_error (parser, "expected %<(%>");
21033 return error_mark_node;
21036 cp_parser_parse_tentatively (parser);
21037 alignas_expr = cp_parser_type_id (parser);
21039 if (!cp_parser_parse_definitely (parser))
21041 gcc_assert (alignas_expr == error_mark_node
21042 || alignas_expr == NULL_TREE);
21044 alignas_expr =
21045 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21046 /**cp_id_kind=*/NULL);
21047 if (alignas_expr == NULL_TREE
21048 || alignas_expr == error_mark_node)
21049 return alignas_expr;
21052 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
21054 cp_parser_error (parser, "expected %<)%>");
21055 return error_mark_node;
21058 alignas_expr = cxx_alignas_expr (alignas_expr);
21060 /* Build the C++-11 representation of an 'aligned'
21061 attribute. */
21062 attributes =
21063 build_tree_list (build_tree_list (get_identifier ("gnu"),
21064 get_identifier ("aligned")),
21065 build_tree_list (NULL_TREE, alignas_expr));
21068 return attributes;
21071 /* Parse a standard C++-11 attribute-specifier-seq.
21073 attribute-specifier-seq:
21074 attribute-specifier-seq [opt] attribute-specifier
21077 static tree
21078 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21080 tree attr_specs = NULL;
21082 while (true)
21084 tree attr_spec = cp_parser_std_attribute_spec (parser);
21085 if (attr_spec == NULL_TREE)
21086 break;
21087 if (attr_spec == error_mark_node)
21088 return error_mark_node;
21090 TREE_CHAIN (attr_spec) = attr_specs;
21091 attr_specs = attr_spec;
21094 attr_specs = nreverse (attr_specs);
21095 return attr_specs;
21098 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21099 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21100 current value of the PEDANTIC flag, regardless of whether or not
21101 the `__extension__' keyword is present. The caller is responsible
21102 for restoring the value of the PEDANTIC flag. */
21104 static bool
21105 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21107 /* Save the old value of the PEDANTIC flag. */
21108 *saved_pedantic = pedantic;
21110 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21112 /* Consume the `__extension__' token. */
21113 cp_lexer_consume_token (parser->lexer);
21114 /* We're not being pedantic while the `__extension__' keyword is
21115 in effect. */
21116 pedantic = 0;
21118 return true;
21121 return false;
21124 /* Parse a label declaration.
21126 label-declaration:
21127 __label__ label-declarator-seq ;
21129 label-declarator-seq:
21130 identifier , label-declarator-seq
21131 identifier */
21133 static void
21134 cp_parser_label_declaration (cp_parser* parser)
21136 /* Look for the `__label__' keyword. */
21137 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21139 while (true)
21141 tree identifier;
21143 /* Look for an identifier. */
21144 identifier = cp_parser_identifier (parser);
21145 /* If we failed, stop. */
21146 if (identifier == error_mark_node)
21147 break;
21148 /* Declare it as a label. */
21149 finish_label_decl (identifier);
21150 /* If the next token is a `;', stop. */
21151 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21152 break;
21153 /* Look for the `,' separating the label declarations. */
21154 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21157 /* Look for the final `;'. */
21158 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21161 /* Support Functions */
21163 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21164 NAME should have one of the representations used for an
21165 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21166 is returned. If PARSER->SCOPE is a dependent type, then a
21167 SCOPE_REF is returned.
21169 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21170 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21171 was formed. Abstractly, such entities should not be passed to this
21172 function, because they do not need to be looked up, but it is
21173 simpler to check for this special case here, rather than at the
21174 call-sites.
21176 In cases not explicitly covered above, this function returns a
21177 DECL, OVERLOAD, or baselink representing the result of the lookup.
21178 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21179 is returned.
21181 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21182 (e.g., "struct") that was used. In that case bindings that do not
21183 refer to types are ignored.
21185 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21186 ignored.
21188 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21189 are ignored.
21191 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21192 types.
21194 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21195 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21196 NULL_TREE otherwise. */
21198 static tree
21199 cp_parser_lookup_name (cp_parser *parser, tree name,
21200 enum tag_types tag_type,
21201 bool is_template,
21202 bool is_namespace,
21203 bool check_dependency,
21204 tree *ambiguous_decls,
21205 location_t name_location)
21207 tree decl;
21208 tree object_type = parser->context->object_type;
21210 /* Assume that the lookup will be unambiguous. */
21211 if (ambiguous_decls)
21212 *ambiguous_decls = NULL_TREE;
21214 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21215 no longer valid. Note that if we are parsing tentatively, and
21216 the parse fails, OBJECT_TYPE will be automatically restored. */
21217 parser->context->object_type = NULL_TREE;
21219 if (name == error_mark_node)
21220 return error_mark_node;
21222 /* A template-id has already been resolved; there is no lookup to
21223 do. */
21224 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21225 return name;
21226 if (BASELINK_P (name))
21228 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21229 == TEMPLATE_ID_EXPR);
21230 return name;
21233 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21234 it should already have been checked to make sure that the name
21235 used matches the type being destroyed. */
21236 if (TREE_CODE (name) == BIT_NOT_EXPR)
21238 tree type;
21240 /* Figure out to which type this destructor applies. */
21241 if (parser->scope)
21242 type = parser->scope;
21243 else if (object_type)
21244 type = object_type;
21245 else
21246 type = current_class_type;
21247 /* If that's not a class type, there is no destructor. */
21248 if (!type || !CLASS_TYPE_P (type))
21249 return error_mark_node;
21250 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21251 lazily_declare_fn (sfk_destructor, type);
21252 if (!CLASSTYPE_DESTRUCTORS (type))
21253 return error_mark_node;
21254 /* If it was a class type, return the destructor. */
21255 return CLASSTYPE_DESTRUCTORS (type);
21258 /* By this point, the NAME should be an ordinary identifier. If
21259 the id-expression was a qualified name, the qualifying scope is
21260 stored in PARSER->SCOPE at this point. */
21261 gcc_assert (identifier_p (name));
21263 /* Perform the lookup. */
21264 if (parser->scope)
21266 bool dependent_p;
21268 if (parser->scope == error_mark_node)
21269 return error_mark_node;
21271 /* If the SCOPE is dependent, the lookup must be deferred until
21272 the template is instantiated -- unless we are explicitly
21273 looking up names in uninstantiated templates. Even then, we
21274 cannot look up the name if the scope is not a class type; it
21275 might, for example, be a template type parameter. */
21276 dependent_p = (TYPE_P (parser->scope)
21277 && dependent_scope_p (parser->scope));
21278 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21279 && dependent_p)
21280 /* Defer lookup. */
21281 decl = error_mark_node;
21282 else
21284 tree pushed_scope = NULL_TREE;
21286 /* If PARSER->SCOPE is a dependent type, then it must be a
21287 class type, and we must not be checking dependencies;
21288 otherwise, we would have processed this lookup above. So
21289 that PARSER->SCOPE is not considered a dependent base by
21290 lookup_member, we must enter the scope here. */
21291 if (dependent_p)
21292 pushed_scope = push_scope (parser->scope);
21294 /* If the PARSER->SCOPE is a template specialization, it
21295 may be instantiated during name lookup. In that case,
21296 errors may be issued. Even if we rollback the current
21297 tentative parse, those errors are valid. */
21298 decl = lookup_qualified_name (parser->scope, name,
21299 tag_type != none_type,
21300 /*complain=*/true);
21302 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21303 lookup result and the nested-name-specifier nominates a class C:
21304 * if the name specified after the nested-name-specifier, when
21305 looked up in C, is the injected-class-name of C (Clause 9), or
21306 * if the name specified after the nested-name-specifier is the
21307 same as the identifier or the simple-template-id's template-
21308 name in the last component of the nested-name-specifier,
21309 the name is instead considered to name the constructor of
21310 class C. [ Note: for example, the constructor is not an
21311 acceptable lookup result in an elaborated-type-specifier so
21312 the constructor would not be used in place of the
21313 injected-class-name. --end note ] Such a constructor name
21314 shall be used only in the declarator-id of a declaration that
21315 names a constructor or in a using-declaration. */
21316 if (tag_type == none_type
21317 && DECL_SELF_REFERENCE_P (decl)
21318 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21319 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21320 tag_type != none_type,
21321 /*complain=*/true);
21323 /* If we have a single function from a using decl, pull it out. */
21324 if (TREE_CODE (decl) == OVERLOAD
21325 && !really_overloaded_fn (decl))
21326 decl = OVL_FUNCTION (decl);
21328 if (pushed_scope)
21329 pop_scope (pushed_scope);
21332 /* If the scope is a dependent type and either we deferred lookup or
21333 we did lookup but didn't find the name, rememeber the name. */
21334 if (decl == error_mark_node && TYPE_P (parser->scope)
21335 && dependent_type_p (parser->scope))
21337 if (tag_type)
21339 tree type;
21341 /* The resolution to Core Issue 180 says that `struct
21342 A::B' should be considered a type-name, even if `A'
21343 is dependent. */
21344 type = make_typename_type (parser->scope, name, tag_type,
21345 /*complain=*/tf_error);
21346 decl = TYPE_NAME (type);
21348 else if (is_template
21349 && (cp_parser_next_token_ends_template_argument_p (parser)
21350 || cp_lexer_next_token_is (parser->lexer,
21351 CPP_CLOSE_PAREN)))
21352 decl = make_unbound_class_template (parser->scope,
21353 name, NULL_TREE,
21354 /*complain=*/tf_error);
21355 else
21356 decl = build_qualified_name (/*type=*/NULL_TREE,
21357 parser->scope, name,
21358 is_template);
21360 parser->qualifying_scope = parser->scope;
21361 parser->object_scope = NULL_TREE;
21363 else if (object_type)
21365 tree object_decl = NULL_TREE;
21366 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21367 OBJECT_TYPE is not a class. */
21368 if (CLASS_TYPE_P (object_type))
21369 /* If the OBJECT_TYPE is a template specialization, it may
21370 be instantiated during name lookup. In that case, errors
21371 may be issued. Even if we rollback the current tentative
21372 parse, those errors are valid. */
21373 object_decl = lookup_member (object_type,
21374 name,
21375 /*protect=*/0,
21376 tag_type != none_type,
21377 tf_warning_or_error);
21378 /* Look it up in the enclosing context, too. */
21379 decl = lookup_name_real (name, tag_type != none_type,
21380 /*nonclass=*/0,
21381 /*block_p=*/true, is_namespace, 0);
21382 parser->object_scope = object_type;
21383 parser->qualifying_scope = NULL_TREE;
21384 if (object_decl)
21385 decl = object_decl;
21387 else
21389 decl = lookup_name_real (name, tag_type != none_type,
21390 /*nonclass=*/0,
21391 /*block_p=*/true, is_namespace, 0);
21392 parser->qualifying_scope = NULL_TREE;
21393 parser->object_scope = NULL_TREE;
21396 /* If the lookup failed, let our caller know. */
21397 if (!decl || decl == error_mark_node)
21398 return error_mark_node;
21400 /* Pull out the template from an injected-class-name (or multiple). */
21401 if (is_template)
21402 decl = maybe_get_template_decl_from_type_decl (decl);
21404 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21405 if (TREE_CODE (decl) == TREE_LIST)
21407 if (ambiguous_decls)
21408 *ambiguous_decls = decl;
21409 /* The error message we have to print is too complicated for
21410 cp_parser_error, so we incorporate its actions directly. */
21411 if (!cp_parser_simulate_error (parser))
21413 error_at (name_location, "reference to %qD is ambiguous",
21414 name);
21415 print_candidates (decl);
21417 return error_mark_node;
21420 gcc_assert (DECL_P (decl)
21421 || TREE_CODE (decl) == OVERLOAD
21422 || TREE_CODE (decl) == SCOPE_REF
21423 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21424 || BASELINK_P (decl));
21426 /* If we have resolved the name of a member declaration, check to
21427 see if the declaration is accessible. When the name resolves to
21428 set of overloaded functions, accessibility is checked when
21429 overload resolution is done.
21431 During an explicit instantiation, access is not checked at all,
21432 as per [temp.explicit]. */
21433 if (DECL_P (decl))
21434 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21436 maybe_record_typedef_use (decl);
21438 return decl;
21441 /* Like cp_parser_lookup_name, but for use in the typical case where
21442 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21443 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21445 static tree
21446 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21448 return cp_parser_lookup_name (parser, name,
21449 none_type,
21450 /*is_template=*/false,
21451 /*is_namespace=*/false,
21452 /*check_dependency=*/true,
21453 /*ambiguous_decls=*/NULL,
21454 location);
21457 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21458 the current context, return the TYPE_DECL. If TAG_NAME_P is
21459 true, the DECL indicates the class being defined in a class-head,
21460 or declared in an elaborated-type-specifier.
21462 Otherwise, return DECL. */
21464 static tree
21465 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21467 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21468 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21470 struct A {
21471 template <typename T> struct B;
21474 template <typename T> struct A::B {};
21476 Similarly, in an elaborated-type-specifier:
21478 namespace N { struct X{}; }
21480 struct A {
21481 template <typename T> friend struct N::X;
21484 However, if the DECL refers to a class type, and we are in
21485 the scope of the class, then the name lookup automatically
21486 finds the TYPE_DECL created by build_self_reference rather
21487 than a TEMPLATE_DECL. For example, in:
21489 template <class T> struct S {
21490 S s;
21493 there is no need to handle such case. */
21495 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21496 return DECL_TEMPLATE_RESULT (decl);
21498 return decl;
21501 /* If too many, or too few, template-parameter lists apply to the
21502 declarator, issue an error message. Returns TRUE if all went well,
21503 and FALSE otherwise. */
21505 static bool
21506 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21507 cp_declarator *declarator,
21508 location_t declarator_location)
21510 switch (declarator->kind)
21512 case cdk_id:
21514 unsigned num_templates = 0;
21515 tree scope = declarator->u.id.qualifying_scope;
21517 if (scope)
21518 num_templates = num_template_headers_for_class (scope);
21519 else if (TREE_CODE (declarator->u.id.unqualified_name)
21520 == TEMPLATE_ID_EXPR)
21521 /* If the DECLARATOR has the form `X<y>' then it uses one
21522 additional level of template parameters. */
21523 ++num_templates;
21525 return cp_parser_check_template_parameters
21526 (parser, num_templates, declarator_location, declarator);
21529 case cdk_function:
21530 case cdk_array:
21531 case cdk_pointer:
21532 case cdk_reference:
21533 case cdk_ptrmem:
21534 return (cp_parser_check_declarator_template_parameters
21535 (parser, declarator->declarator, declarator_location));
21537 case cdk_error:
21538 return true;
21540 default:
21541 gcc_unreachable ();
21543 return false;
21546 /* NUM_TEMPLATES were used in the current declaration. If that is
21547 invalid, return FALSE and issue an error messages. Otherwise,
21548 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21549 declarator and we can print more accurate diagnostics. */
21551 static bool
21552 cp_parser_check_template_parameters (cp_parser* parser,
21553 unsigned num_templates,
21554 location_t location,
21555 cp_declarator *declarator)
21557 /* If there are the same number of template classes and parameter
21558 lists, that's OK. */
21559 if (parser->num_template_parameter_lists == num_templates)
21560 return true;
21561 /* If there are more, but only one more, then we are referring to a
21562 member template. That's OK too. */
21563 if (parser->num_template_parameter_lists == num_templates + 1)
21564 return true;
21565 /* If there are more template classes than parameter lists, we have
21566 something like:
21568 template <class T> void S<T>::R<T>::f (); */
21569 if (parser->num_template_parameter_lists < num_templates)
21571 if (declarator && !current_function_decl)
21572 error_at (location, "specializing member %<%T::%E%> "
21573 "requires %<template<>%> syntax",
21574 declarator->u.id.qualifying_scope,
21575 declarator->u.id.unqualified_name);
21576 else if (declarator)
21577 error_at (location, "invalid declaration of %<%T::%E%>",
21578 declarator->u.id.qualifying_scope,
21579 declarator->u.id.unqualified_name);
21580 else
21581 error_at (location, "too few template-parameter-lists");
21582 return false;
21584 /* Otherwise, there are too many template parameter lists. We have
21585 something like:
21587 template <class T> template <class U> void S::f(); */
21588 error_at (location, "too many template-parameter-lists");
21589 return false;
21592 /* Parse an optional `::' token indicating that the following name is
21593 from the global namespace. If so, PARSER->SCOPE is set to the
21594 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21595 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21596 Returns the new value of PARSER->SCOPE, if the `::' token is
21597 present, and NULL_TREE otherwise. */
21599 static tree
21600 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21602 cp_token *token;
21604 /* Peek at the next token. */
21605 token = cp_lexer_peek_token (parser->lexer);
21606 /* If we're looking at a `::' token then we're starting from the
21607 global namespace, not our current location. */
21608 if (token->type == CPP_SCOPE)
21610 /* Consume the `::' token. */
21611 cp_lexer_consume_token (parser->lexer);
21612 /* Set the SCOPE so that we know where to start the lookup. */
21613 parser->scope = global_namespace;
21614 parser->qualifying_scope = global_namespace;
21615 parser->object_scope = NULL_TREE;
21617 return parser->scope;
21619 else if (!current_scope_valid_p)
21621 parser->scope = NULL_TREE;
21622 parser->qualifying_scope = NULL_TREE;
21623 parser->object_scope = NULL_TREE;
21626 return NULL_TREE;
21629 /* Returns TRUE if the upcoming token sequence is the start of a
21630 constructor declarator. If FRIEND_P is true, the declarator is
21631 preceded by the `friend' specifier. */
21633 static bool
21634 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21636 bool constructor_p;
21637 tree nested_name_specifier;
21638 cp_token *next_token;
21640 /* The common case is that this is not a constructor declarator, so
21641 try to avoid doing lots of work if at all possible. It's not
21642 valid declare a constructor at function scope. */
21643 if (parser->in_function_body)
21644 return false;
21645 /* And only certain tokens can begin a constructor declarator. */
21646 next_token = cp_lexer_peek_token (parser->lexer);
21647 if (next_token->type != CPP_NAME
21648 && next_token->type != CPP_SCOPE
21649 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21650 && next_token->type != CPP_TEMPLATE_ID)
21651 return false;
21653 /* Parse tentatively; we are going to roll back all of the tokens
21654 consumed here. */
21655 cp_parser_parse_tentatively (parser);
21656 /* Assume that we are looking at a constructor declarator. */
21657 constructor_p = true;
21659 /* Look for the optional `::' operator. */
21660 cp_parser_global_scope_opt (parser,
21661 /*current_scope_valid_p=*/false);
21662 /* Look for the nested-name-specifier. */
21663 nested_name_specifier
21664 = (cp_parser_nested_name_specifier_opt (parser,
21665 /*typename_keyword_p=*/false,
21666 /*check_dependency_p=*/false,
21667 /*type_p=*/false,
21668 /*is_declaration=*/false));
21669 /* Outside of a class-specifier, there must be a
21670 nested-name-specifier. */
21671 if (!nested_name_specifier &&
21672 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21673 || friend_p))
21674 constructor_p = false;
21675 else if (nested_name_specifier == error_mark_node)
21676 constructor_p = false;
21678 /* If we have a class scope, this is easy; DR 147 says that S::S always
21679 names the constructor, and no other qualified name could. */
21680 if (constructor_p && nested_name_specifier
21681 && CLASS_TYPE_P (nested_name_specifier))
21683 tree id = cp_parser_unqualified_id (parser,
21684 /*template_keyword_p=*/false,
21685 /*check_dependency_p=*/false,
21686 /*declarator_p=*/true,
21687 /*optional_p=*/false);
21688 if (is_overloaded_fn (id))
21689 id = DECL_NAME (get_first_fn (id));
21690 if (!constructor_name_p (id, nested_name_specifier))
21691 constructor_p = false;
21693 /* If we still think that this might be a constructor-declarator,
21694 look for a class-name. */
21695 else if (constructor_p)
21697 /* If we have:
21699 template <typename T> struct S {
21700 S();
21703 we must recognize that the nested `S' names a class. */
21704 tree type_decl;
21705 type_decl = cp_parser_class_name (parser,
21706 /*typename_keyword_p=*/false,
21707 /*template_keyword_p=*/false,
21708 none_type,
21709 /*check_dependency_p=*/false,
21710 /*class_head_p=*/false,
21711 /*is_declaration=*/false);
21712 /* If there was no class-name, then this is not a constructor. */
21713 constructor_p = !cp_parser_error_occurred (parser);
21715 /* If we're still considering a constructor, we have to see a `(',
21716 to begin the parameter-declaration-clause, followed by either a
21717 `)', an `...', or a decl-specifier. We need to check for a
21718 type-specifier to avoid being fooled into thinking that:
21720 S (f) (int);
21722 is a constructor. (It is actually a function named `f' that
21723 takes one parameter (of type `int') and returns a value of type
21724 `S'. */
21725 if (constructor_p
21726 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21727 constructor_p = false;
21729 if (constructor_p
21730 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21731 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21732 /* A parameter declaration begins with a decl-specifier,
21733 which is either the "attribute" keyword, a storage class
21734 specifier, or (usually) a type-specifier. */
21735 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21737 tree type;
21738 tree pushed_scope = NULL_TREE;
21739 unsigned saved_num_template_parameter_lists;
21741 /* Names appearing in the type-specifier should be looked up
21742 in the scope of the class. */
21743 if (current_class_type)
21744 type = NULL_TREE;
21745 else
21747 type = TREE_TYPE (type_decl);
21748 if (TREE_CODE (type) == TYPENAME_TYPE)
21750 type = resolve_typename_type (type,
21751 /*only_current_p=*/false);
21752 if (TREE_CODE (type) == TYPENAME_TYPE)
21754 cp_parser_abort_tentative_parse (parser);
21755 return false;
21758 pushed_scope = push_scope (type);
21761 /* Inside the constructor parameter list, surrounding
21762 template-parameter-lists do not apply. */
21763 saved_num_template_parameter_lists
21764 = parser->num_template_parameter_lists;
21765 parser->num_template_parameter_lists = 0;
21767 /* Look for the type-specifier. */
21768 cp_parser_type_specifier (parser,
21769 CP_PARSER_FLAGS_NONE,
21770 /*decl_specs=*/NULL,
21771 /*is_declarator=*/true,
21772 /*declares_class_or_enum=*/NULL,
21773 /*is_cv_qualifier=*/NULL);
21775 parser->num_template_parameter_lists
21776 = saved_num_template_parameter_lists;
21778 /* Leave the scope of the class. */
21779 if (pushed_scope)
21780 pop_scope (pushed_scope);
21782 constructor_p = !cp_parser_error_occurred (parser);
21786 /* We did not really want to consume any tokens. */
21787 cp_parser_abort_tentative_parse (parser);
21789 return constructor_p;
21792 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21793 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21794 they must be performed once we are in the scope of the function.
21796 Returns the function defined. */
21798 static tree
21799 cp_parser_function_definition_from_specifiers_and_declarator
21800 (cp_parser* parser,
21801 cp_decl_specifier_seq *decl_specifiers,
21802 tree attributes,
21803 const cp_declarator *declarator)
21805 tree fn;
21806 bool success_p;
21808 /* Begin the function-definition. */
21809 success_p = start_function (decl_specifiers, declarator, attributes);
21811 /* The things we're about to see are not directly qualified by any
21812 template headers we've seen thus far. */
21813 reset_specialization ();
21815 /* If there were names looked up in the decl-specifier-seq that we
21816 did not check, check them now. We must wait until we are in the
21817 scope of the function to perform the checks, since the function
21818 might be a friend. */
21819 perform_deferred_access_checks (tf_warning_or_error);
21821 if (!success_p)
21823 /* Skip the entire function. */
21824 cp_parser_skip_to_end_of_block_or_statement (parser);
21825 fn = error_mark_node;
21827 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21829 /* Seen already, skip it. An error message has already been output. */
21830 cp_parser_skip_to_end_of_block_or_statement (parser);
21831 fn = current_function_decl;
21832 current_function_decl = NULL_TREE;
21833 /* If this is a function from a class, pop the nested class. */
21834 if (current_class_name)
21835 pop_nested_class ();
21837 else
21839 timevar_id_t tv;
21840 if (DECL_DECLARED_INLINE_P (current_function_decl))
21841 tv = TV_PARSE_INLINE;
21842 else
21843 tv = TV_PARSE_FUNC;
21844 timevar_push (tv);
21845 fn = cp_parser_function_definition_after_declarator (parser,
21846 /*inline_p=*/false);
21847 timevar_pop (tv);
21850 return fn;
21853 /* Parse the part of a function-definition that follows the
21854 declarator. INLINE_P is TRUE iff this function is an inline
21855 function defined within a class-specifier.
21857 Returns the function defined. */
21859 static tree
21860 cp_parser_function_definition_after_declarator (cp_parser* parser,
21861 bool inline_p)
21863 tree fn;
21864 bool ctor_initializer_p = false;
21865 bool saved_in_unbraced_linkage_specification_p;
21866 bool saved_in_function_body;
21867 unsigned saved_num_template_parameter_lists;
21868 cp_token *token;
21870 saved_in_function_body = parser->in_function_body;
21871 parser->in_function_body = true;
21872 /* If the next token is `return', then the code may be trying to
21873 make use of the "named return value" extension that G++ used to
21874 support. */
21875 token = cp_lexer_peek_token (parser->lexer);
21876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21878 /* Consume the `return' keyword. */
21879 cp_lexer_consume_token (parser->lexer);
21880 /* Look for the identifier that indicates what value is to be
21881 returned. */
21882 cp_parser_identifier (parser);
21883 /* Issue an error message. */
21884 error_at (token->location,
21885 "named return values are no longer supported");
21886 /* Skip tokens until we reach the start of the function body. */
21887 while (true)
21889 cp_token *token = cp_lexer_peek_token (parser->lexer);
21890 if (token->type == CPP_OPEN_BRACE
21891 || token->type == CPP_EOF
21892 || token->type == CPP_PRAGMA_EOL)
21893 break;
21894 cp_lexer_consume_token (parser->lexer);
21897 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21898 anything declared inside `f'. */
21899 saved_in_unbraced_linkage_specification_p
21900 = parser->in_unbraced_linkage_specification_p;
21901 parser->in_unbraced_linkage_specification_p = false;
21902 /* Inside the function, surrounding template-parameter-lists do not
21903 apply. */
21904 saved_num_template_parameter_lists
21905 = parser->num_template_parameter_lists;
21906 parser->num_template_parameter_lists = 0;
21908 start_lambda_scope (current_function_decl);
21910 /* If the next token is `try', `__transaction_atomic', or
21911 `__transaction_relaxed`, then we are looking at either function-try-block
21912 or function-transaction-block. Note that all of these include the
21913 function-body. */
21914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21915 ctor_initializer_p = cp_parser_function_transaction (parser,
21916 RID_TRANSACTION_ATOMIC);
21917 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21918 RID_TRANSACTION_RELAXED))
21919 ctor_initializer_p = cp_parser_function_transaction (parser,
21920 RID_TRANSACTION_RELAXED);
21921 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21922 ctor_initializer_p = cp_parser_function_try_block (parser);
21923 else
21924 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21925 (parser, /*in_function_try_block=*/false);
21927 finish_lambda_scope ();
21929 /* Finish the function. */
21930 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21931 (inline_p ? 2 : 0));
21932 /* Generate code for it, if necessary. */
21933 expand_or_defer_fn (fn);
21934 /* Restore the saved values. */
21935 parser->in_unbraced_linkage_specification_p
21936 = saved_in_unbraced_linkage_specification_p;
21937 parser->num_template_parameter_lists
21938 = saved_num_template_parameter_lists;
21939 parser->in_function_body = saved_in_function_body;
21941 return fn;
21944 /* Parse a template-declaration, assuming that the `export' (and
21945 `extern') keywords, if present, has already been scanned. MEMBER_P
21946 is as for cp_parser_template_declaration. */
21948 static void
21949 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21951 tree decl = NULL_TREE;
21952 vec<deferred_access_check, va_gc> *checks;
21953 tree parameter_list;
21954 bool friend_p = false;
21955 bool need_lang_pop;
21956 cp_token *token;
21958 /* Look for the `template' keyword. */
21959 token = cp_lexer_peek_token (parser->lexer);
21960 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21961 return;
21963 /* And the `<'. */
21964 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21965 return;
21966 if (at_class_scope_p () && current_function_decl)
21968 /* 14.5.2.2 [temp.mem]
21970 A local class shall not have member templates. */
21971 error_at (token->location,
21972 "invalid declaration of member template in local class");
21973 cp_parser_skip_to_end_of_block_or_statement (parser);
21974 return;
21976 /* [temp]
21978 A template ... shall not have C linkage. */
21979 if (current_lang_name == lang_name_c)
21981 error_at (token->location, "template with C linkage");
21982 /* Give it C++ linkage to avoid confusing other parts of the
21983 front end. */
21984 push_lang_context (lang_name_cplusplus);
21985 need_lang_pop = true;
21987 else
21988 need_lang_pop = false;
21990 /* We cannot perform access checks on the template parameter
21991 declarations until we know what is being declared, just as we
21992 cannot check the decl-specifier list. */
21993 push_deferring_access_checks (dk_deferred);
21995 /* If the next token is `>', then we have an invalid
21996 specialization. Rather than complain about an invalid template
21997 parameter, issue an error message here. */
21998 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22000 cp_parser_error (parser, "invalid explicit specialization");
22001 begin_specialization ();
22002 parameter_list = NULL_TREE;
22004 else
22006 /* Parse the template parameters. */
22007 parameter_list = cp_parser_template_parameter_list (parser);
22010 /* Get the deferred access checks from the parameter list. These
22011 will be checked once we know what is being declared, as for a
22012 member template the checks must be performed in the scope of the
22013 class containing the member. */
22014 checks = get_deferred_access_checks ();
22016 /* Look for the `>'. */
22017 cp_parser_skip_to_end_of_template_parameter_list (parser);
22018 /* We just processed one more parameter list. */
22019 ++parser->num_template_parameter_lists;
22020 /* If the next token is `template', there are more template
22021 parameters. */
22022 if (cp_lexer_next_token_is_keyword (parser->lexer,
22023 RID_TEMPLATE))
22024 cp_parser_template_declaration_after_export (parser, member_p);
22025 else if (cxx_dialect >= cxx0x
22026 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22027 decl = cp_parser_alias_declaration (parser);
22028 else
22030 /* There are no access checks when parsing a template, as we do not
22031 know if a specialization will be a friend. */
22032 push_deferring_access_checks (dk_no_check);
22033 token = cp_lexer_peek_token (parser->lexer);
22034 decl = cp_parser_single_declaration (parser,
22035 checks,
22036 member_p,
22037 /*explicit_specialization_p=*/false,
22038 &friend_p);
22039 pop_deferring_access_checks ();
22041 /* If this is a member template declaration, let the front
22042 end know. */
22043 if (member_p && !friend_p && decl)
22045 if (TREE_CODE (decl) == TYPE_DECL)
22046 cp_parser_check_access_in_redeclaration (decl, token->location);
22048 decl = finish_member_template_decl (decl);
22050 else if (friend_p && decl
22051 && DECL_DECLARES_TYPE_P (decl))
22052 make_friend_class (current_class_type, TREE_TYPE (decl),
22053 /*complain=*/true);
22055 /* We are done with the current parameter list. */
22056 --parser->num_template_parameter_lists;
22058 pop_deferring_access_checks ();
22060 /* Finish up. */
22061 finish_template_decl (parameter_list);
22063 /* Check the template arguments for a literal operator template. */
22064 if (decl
22065 && DECL_DECLARES_FUNCTION_P (decl)
22066 && UDLIT_OPER_P (DECL_NAME (decl)))
22068 bool ok = true;
22069 if (parameter_list == NULL_TREE)
22070 ok = false;
22071 else
22073 int num_parms = TREE_VEC_LENGTH (parameter_list);
22074 if (num_parms != 1)
22075 ok = false;
22076 else
22078 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22079 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22080 if (TREE_TYPE (parm) != char_type_node
22081 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22082 ok = false;
22085 if (!ok)
22086 error ("literal operator template %qD has invalid parameter list."
22087 " Expected non-type template argument pack <char...>",
22088 decl);
22090 /* Register member declarations. */
22091 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22092 finish_member_declaration (decl);
22093 /* For the erroneous case of a template with C linkage, we pushed an
22094 implicit C++ linkage scope; exit that scope now. */
22095 if (need_lang_pop)
22096 pop_lang_context ();
22097 /* If DECL is a function template, we must return to parse it later.
22098 (Even though there is no definition, there might be default
22099 arguments that need handling.) */
22100 if (member_p && decl
22101 && DECL_DECLARES_FUNCTION_P (decl))
22102 vec_safe_push (unparsed_funs_with_definitions, decl);
22105 /* Perform the deferred access checks from a template-parameter-list.
22106 CHECKS is a TREE_LIST of access checks, as returned by
22107 get_deferred_access_checks. */
22109 static void
22110 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22112 ++processing_template_parmlist;
22113 perform_access_checks (checks, tf_warning_or_error);
22114 --processing_template_parmlist;
22117 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22118 `function-definition' sequence that follows a template header.
22119 If MEMBER_P is true, this declaration appears in a class scope.
22121 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22122 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22124 static tree
22125 cp_parser_single_declaration (cp_parser* parser,
22126 vec<deferred_access_check, va_gc> *checks,
22127 bool member_p,
22128 bool explicit_specialization_p,
22129 bool* friend_p)
22131 int declares_class_or_enum;
22132 tree decl = NULL_TREE;
22133 cp_decl_specifier_seq decl_specifiers;
22134 bool function_definition_p = false;
22135 cp_token *decl_spec_token_start;
22137 /* This function is only used when processing a template
22138 declaration. */
22139 gcc_assert (innermost_scope_kind () == sk_template_parms
22140 || innermost_scope_kind () == sk_template_spec);
22142 /* Defer access checks until we know what is being declared. */
22143 push_deferring_access_checks (dk_deferred);
22145 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22146 alternative. */
22147 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22148 cp_parser_decl_specifier_seq (parser,
22149 CP_PARSER_FLAGS_OPTIONAL,
22150 &decl_specifiers,
22151 &declares_class_or_enum);
22152 if (friend_p)
22153 *friend_p = cp_parser_friend_p (&decl_specifiers);
22155 /* There are no template typedefs. */
22156 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22158 error_at (decl_spec_token_start->location,
22159 "template declaration of %<typedef%>");
22160 decl = error_mark_node;
22163 /* Gather up the access checks that occurred the
22164 decl-specifier-seq. */
22165 stop_deferring_access_checks ();
22167 /* Check for the declaration of a template class. */
22168 if (declares_class_or_enum)
22170 if (cp_parser_declares_only_class_p (parser))
22172 decl = shadow_tag (&decl_specifiers);
22174 /* In this case:
22176 struct C {
22177 friend template <typename T> struct A<T>::B;
22180 A<T>::B will be represented by a TYPENAME_TYPE, and
22181 therefore not recognized by shadow_tag. */
22182 if (friend_p && *friend_p
22183 && !decl
22184 && decl_specifiers.type
22185 && TYPE_P (decl_specifiers.type))
22186 decl = decl_specifiers.type;
22188 if (decl && decl != error_mark_node)
22189 decl = TYPE_NAME (decl);
22190 else
22191 decl = error_mark_node;
22193 /* Perform access checks for template parameters. */
22194 cp_parser_perform_template_parameter_access_checks (checks);
22198 /* Complain about missing 'typename' or other invalid type names. */
22199 if (!decl_specifiers.any_type_specifiers_p
22200 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22202 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22203 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22204 the rest of this declaration. */
22205 decl = error_mark_node;
22206 goto out;
22209 /* If it's not a template class, try for a template function. If
22210 the next token is a `;', then this declaration does not declare
22211 anything. But, if there were errors in the decl-specifiers, then
22212 the error might well have come from an attempted class-specifier.
22213 In that case, there's no need to warn about a missing declarator. */
22214 if (!decl
22215 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22216 || decl_specifiers.type != error_mark_node))
22218 decl = cp_parser_init_declarator (parser,
22219 &decl_specifiers,
22220 checks,
22221 /*function_definition_allowed_p=*/true,
22222 member_p,
22223 declares_class_or_enum,
22224 &function_definition_p,
22225 NULL);
22227 /* 7.1.1-1 [dcl.stc]
22229 A storage-class-specifier shall not be specified in an explicit
22230 specialization... */
22231 if (decl
22232 && explicit_specialization_p
22233 && decl_specifiers.storage_class != sc_none)
22235 error_at (decl_spec_token_start->location,
22236 "explicit template specialization cannot have a storage class");
22237 decl = error_mark_node;
22240 if (decl && VAR_P (decl))
22241 check_template_variable (decl);
22244 /* Look for a trailing `;' after the declaration. */
22245 if (!function_definition_p
22246 && (decl == error_mark_node
22247 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22248 cp_parser_skip_to_end_of_block_or_statement (parser);
22250 out:
22251 pop_deferring_access_checks ();
22253 /* Clear any current qualification; whatever comes next is the start
22254 of something new. */
22255 parser->scope = NULL_TREE;
22256 parser->qualifying_scope = NULL_TREE;
22257 parser->object_scope = NULL_TREE;
22259 return decl;
22262 /* Parse a cast-expression that is not the operand of a unary "&". */
22264 static tree
22265 cp_parser_simple_cast_expression (cp_parser *parser)
22267 return cp_parser_cast_expression (parser, /*address_p=*/false,
22268 /*cast_p=*/false, /*decltype*/false, NULL);
22271 /* Parse a functional cast to TYPE. Returns an expression
22272 representing the cast. */
22274 static tree
22275 cp_parser_functional_cast (cp_parser* parser, tree type)
22277 vec<tree, va_gc> *vec;
22278 tree expression_list;
22279 tree cast;
22280 bool nonconst_p;
22282 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22284 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22285 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22286 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22287 if (TREE_CODE (type) == TYPE_DECL)
22288 type = TREE_TYPE (type);
22289 return finish_compound_literal (type, expression_list,
22290 tf_warning_or_error);
22294 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22295 /*cast_p=*/true,
22296 /*allow_expansion_p=*/true,
22297 /*non_constant_p=*/NULL);
22298 if (vec == NULL)
22299 expression_list = error_mark_node;
22300 else
22302 expression_list = build_tree_list_vec (vec);
22303 release_tree_vector (vec);
22306 cast = build_functional_cast (type, expression_list,
22307 tf_warning_or_error);
22308 /* [expr.const]/1: In an integral constant expression "only type
22309 conversions to integral or enumeration type can be used". */
22310 if (TREE_CODE (type) == TYPE_DECL)
22311 type = TREE_TYPE (type);
22312 if (cast != error_mark_node
22313 && !cast_valid_in_integral_constant_expression_p (type)
22314 && cp_parser_non_integral_constant_expression (parser,
22315 NIC_CONSTRUCTOR))
22316 return error_mark_node;
22317 return cast;
22320 /* Save the tokens that make up the body of a member function defined
22321 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22322 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22323 specifiers applied to the declaration. Returns the FUNCTION_DECL
22324 for the member function. */
22326 static tree
22327 cp_parser_save_member_function_body (cp_parser* parser,
22328 cp_decl_specifier_seq *decl_specifiers,
22329 cp_declarator *declarator,
22330 tree attributes)
22332 cp_token *first;
22333 cp_token *last;
22334 tree fn;
22336 /* Create the FUNCTION_DECL. */
22337 fn = grokmethod (decl_specifiers, declarator, attributes);
22338 /* If something went badly wrong, bail out now. */
22339 if (fn == error_mark_node)
22341 /* If there's a function-body, skip it. */
22342 if (cp_parser_token_starts_function_definition_p
22343 (cp_lexer_peek_token (parser->lexer)))
22344 cp_parser_skip_to_end_of_block_or_statement (parser);
22345 return error_mark_node;
22348 /* Remember it, if there default args to post process. */
22349 cp_parser_save_default_args (parser, fn);
22351 /* Save away the tokens that make up the body of the
22352 function. */
22353 first = parser->lexer->next_token;
22354 /* We can have braced-init-list mem-initializers before the fn body. */
22355 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22357 cp_lexer_consume_token (parser->lexer);
22358 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22359 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22361 /* cache_group will stop after an un-nested { } pair, too. */
22362 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22363 break;
22365 /* variadic mem-inits have ... after the ')'. */
22366 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22367 cp_lexer_consume_token (parser->lexer);
22370 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22371 /* Handle function try blocks. */
22372 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22373 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22374 last = parser->lexer->next_token;
22376 /* Save away the inline definition; we will process it when the
22377 class is complete. */
22378 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22379 DECL_PENDING_INLINE_P (fn) = 1;
22381 /* We need to know that this was defined in the class, so that
22382 friend templates are handled correctly. */
22383 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22385 /* Add FN to the queue of functions to be parsed later. */
22386 vec_safe_push (unparsed_funs_with_definitions, fn);
22388 return fn;
22391 /* Save the tokens that make up the in-class initializer for a non-static
22392 data member. Returns a DEFAULT_ARG. */
22394 static tree
22395 cp_parser_save_nsdmi (cp_parser* parser)
22397 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22400 /* Parse a template-argument-list, as well as the trailing ">" (but
22401 not the opening "<"). See cp_parser_template_argument_list for the
22402 return value. */
22404 static tree
22405 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22407 tree arguments;
22408 tree saved_scope;
22409 tree saved_qualifying_scope;
22410 tree saved_object_scope;
22411 bool saved_greater_than_is_operator_p;
22412 int saved_unevaluated_operand;
22413 int saved_inhibit_evaluation_warnings;
22415 /* [temp.names]
22417 When parsing a template-id, the first non-nested `>' is taken as
22418 the end of the template-argument-list rather than a greater-than
22419 operator. */
22420 saved_greater_than_is_operator_p
22421 = parser->greater_than_is_operator_p;
22422 parser->greater_than_is_operator_p = false;
22423 /* Parsing the argument list may modify SCOPE, so we save it
22424 here. */
22425 saved_scope = parser->scope;
22426 saved_qualifying_scope = parser->qualifying_scope;
22427 saved_object_scope = parser->object_scope;
22428 /* We need to evaluate the template arguments, even though this
22429 template-id may be nested within a "sizeof". */
22430 saved_unevaluated_operand = cp_unevaluated_operand;
22431 cp_unevaluated_operand = 0;
22432 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22433 c_inhibit_evaluation_warnings = 0;
22434 /* Parse the template-argument-list itself. */
22435 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22436 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22437 arguments = NULL_TREE;
22438 else
22439 arguments = cp_parser_template_argument_list (parser);
22440 /* Look for the `>' that ends the template-argument-list. If we find
22441 a '>>' instead, it's probably just a typo. */
22442 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22444 if (cxx_dialect != cxx98)
22446 /* In C++0x, a `>>' in a template argument list or cast
22447 expression is considered to be two separate `>'
22448 tokens. So, change the current token to a `>', but don't
22449 consume it: it will be consumed later when the outer
22450 template argument list (or cast expression) is parsed.
22451 Note that this replacement of `>' for `>>' is necessary
22452 even if we are parsing tentatively: in the tentative
22453 case, after calling
22454 cp_parser_enclosed_template_argument_list we will always
22455 throw away all of the template arguments and the first
22456 closing `>', either because the template argument list
22457 was erroneous or because we are replacing those tokens
22458 with a CPP_TEMPLATE_ID token. The second `>' (which will
22459 not have been thrown away) is needed either to close an
22460 outer template argument list or to complete a new-style
22461 cast. */
22462 cp_token *token = cp_lexer_peek_token (parser->lexer);
22463 token->type = CPP_GREATER;
22465 else if (!saved_greater_than_is_operator_p)
22467 /* If we're in a nested template argument list, the '>>' has
22468 to be a typo for '> >'. We emit the error message, but we
22469 continue parsing and we push a '>' as next token, so that
22470 the argument list will be parsed correctly. Note that the
22471 global source location is still on the token before the
22472 '>>', so we need to say explicitly where we want it. */
22473 cp_token *token = cp_lexer_peek_token (parser->lexer);
22474 error_at (token->location, "%<>>%> should be %<> >%> "
22475 "within a nested template argument list");
22477 token->type = CPP_GREATER;
22479 else
22481 /* If this is not a nested template argument list, the '>>'
22482 is a typo for '>'. Emit an error message and continue.
22483 Same deal about the token location, but here we can get it
22484 right by consuming the '>>' before issuing the diagnostic. */
22485 cp_token *token = cp_lexer_consume_token (parser->lexer);
22486 error_at (token->location,
22487 "spurious %<>>%>, use %<>%> to terminate "
22488 "a template argument list");
22491 else
22492 cp_parser_skip_to_end_of_template_parameter_list (parser);
22493 /* The `>' token might be a greater-than operator again now. */
22494 parser->greater_than_is_operator_p
22495 = saved_greater_than_is_operator_p;
22496 /* Restore the SAVED_SCOPE. */
22497 parser->scope = saved_scope;
22498 parser->qualifying_scope = saved_qualifying_scope;
22499 parser->object_scope = saved_object_scope;
22500 cp_unevaluated_operand = saved_unevaluated_operand;
22501 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22503 return arguments;
22506 /* MEMBER_FUNCTION is a member function, or a friend. If default
22507 arguments, or the body of the function have not yet been parsed,
22508 parse them now. */
22510 static void
22511 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22513 timevar_push (TV_PARSE_INMETH);
22514 /* If this member is a template, get the underlying
22515 FUNCTION_DECL. */
22516 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22517 member_function = DECL_TEMPLATE_RESULT (member_function);
22519 /* There should not be any class definitions in progress at this
22520 point; the bodies of members are only parsed outside of all class
22521 definitions. */
22522 gcc_assert (parser->num_classes_being_defined == 0);
22523 /* While we're parsing the member functions we might encounter more
22524 classes. We want to handle them right away, but we don't want
22525 them getting mixed up with functions that are currently in the
22526 queue. */
22527 push_unparsed_function_queues (parser);
22529 /* Make sure that any template parameters are in scope. */
22530 maybe_begin_member_template_processing (member_function);
22532 /* If the body of the function has not yet been parsed, parse it
22533 now. */
22534 if (DECL_PENDING_INLINE_P (member_function))
22536 tree function_scope;
22537 cp_token_cache *tokens;
22539 /* The function is no longer pending; we are processing it. */
22540 tokens = DECL_PENDING_INLINE_INFO (member_function);
22541 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22542 DECL_PENDING_INLINE_P (member_function) = 0;
22544 /* If this is a local class, enter the scope of the containing
22545 function. */
22546 function_scope = current_function_decl;
22547 if (function_scope)
22548 push_function_context ();
22550 /* Push the body of the function onto the lexer stack. */
22551 cp_parser_push_lexer_for_tokens (parser, tokens);
22553 /* Let the front end know that we going to be defining this
22554 function. */
22555 start_preparsed_function (member_function, NULL_TREE,
22556 SF_PRE_PARSED | SF_INCLASS_INLINE);
22558 /* Don't do access checking if it is a templated function. */
22559 if (processing_template_decl)
22560 push_deferring_access_checks (dk_no_check);
22562 /* Now, parse the body of the function. */
22563 cp_parser_function_definition_after_declarator (parser,
22564 /*inline_p=*/true);
22566 if (processing_template_decl)
22567 pop_deferring_access_checks ();
22569 /* Leave the scope of the containing function. */
22570 if (function_scope)
22571 pop_function_context ();
22572 cp_parser_pop_lexer (parser);
22575 /* Remove any template parameters from the symbol table. */
22576 maybe_end_member_template_processing ();
22578 /* Restore the queue. */
22579 pop_unparsed_function_queues (parser);
22580 timevar_pop (TV_PARSE_INMETH);
22583 /* If DECL contains any default args, remember it on the unparsed
22584 functions queue. */
22586 static void
22587 cp_parser_save_default_args (cp_parser* parser, tree decl)
22589 tree probe;
22591 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22592 probe;
22593 probe = TREE_CHAIN (probe))
22594 if (TREE_PURPOSE (probe))
22596 cp_default_arg_entry entry = {current_class_type, decl};
22597 vec_safe_push (unparsed_funs_with_default_args, entry);
22598 break;
22602 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22603 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22604 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22605 from the parameter-type-list. */
22607 static tree
22608 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22609 tree default_arg, tree parmtype)
22611 cp_token_cache *tokens;
22612 tree parsed_arg;
22613 bool dummy;
22615 if (default_arg == error_mark_node)
22616 return error_mark_node;
22618 /* Push the saved tokens for the default argument onto the parser's
22619 lexer stack. */
22620 tokens = DEFARG_TOKENS (default_arg);
22621 cp_parser_push_lexer_for_tokens (parser, tokens);
22623 start_lambda_scope (decl);
22625 /* Parse the default argument. */
22626 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22627 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22628 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22630 finish_lambda_scope ();
22632 if (parsed_arg == error_mark_node)
22633 cp_parser_skip_to_end_of_statement (parser);
22635 if (!processing_template_decl)
22637 /* In a non-template class, check conversions now. In a template,
22638 we'll wait and instantiate these as needed. */
22639 if (TREE_CODE (decl) == PARM_DECL)
22640 parsed_arg = check_default_argument (parmtype, parsed_arg);
22641 else
22643 int flags = LOOKUP_IMPLICIT;
22644 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22645 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22646 flags = LOOKUP_NORMAL;
22647 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22651 /* If the token stream has not been completely used up, then
22652 there was extra junk after the end of the default
22653 argument. */
22654 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22656 if (TREE_CODE (decl) == PARM_DECL)
22657 cp_parser_error (parser, "expected %<,%>");
22658 else
22659 cp_parser_error (parser, "expected %<;%>");
22662 /* Revert to the main lexer. */
22663 cp_parser_pop_lexer (parser);
22665 return parsed_arg;
22668 /* FIELD is a non-static data member with an initializer which we saved for
22669 later; parse it now. */
22671 static void
22672 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22674 tree def;
22676 push_unparsed_function_queues (parser);
22677 def = cp_parser_late_parse_one_default_arg (parser, field,
22678 DECL_INITIAL (field),
22679 NULL_TREE);
22680 pop_unparsed_function_queues (parser);
22682 DECL_INITIAL (field) = def;
22685 /* FN is a FUNCTION_DECL which may contains a parameter with an
22686 unparsed DEFAULT_ARG. Parse the default args now. This function
22687 assumes that the current scope is the scope in which the default
22688 argument should be processed. */
22690 static void
22691 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22693 bool saved_local_variables_forbidden_p;
22694 tree parm, parmdecl;
22696 /* While we're parsing the default args, we might (due to the
22697 statement expression extension) encounter more classes. We want
22698 to handle them right away, but we don't want them getting mixed
22699 up with default args that are currently in the queue. */
22700 push_unparsed_function_queues (parser);
22702 /* Local variable names (and the `this' keyword) may not appear
22703 in a default argument. */
22704 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22705 parser->local_variables_forbidden_p = true;
22707 push_defarg_context (fn);
22709 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22710 parmdecl = DECL_ARGUMENTS (fn);
22711 parm && parm != void_list_node;
22712 parm = TREE_CHAIN (parm),
22713 parmdecl = DECL_CHAIN (parmdecl))
22715 tree default_arg = TREE_PURPOSE (parm);
22716 tree parsed_arg;
22717 vec<tree, va_gc> *insts;
22718 tree copy;
22719 unsigned ix;
22721 if (!default_arg)
22722 continue;
22724 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22725 /* This can happen for a friend declaration for a function
22726 already declared with default arguments. */
22727 continue;
22729 parsed_arg
22730 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22731 default_arg,
22732 TREE_VALUE (parm));
22733 if (parsed_arg == error_mark_node)
22735 continue;
22738 TREE_PURPOSE (parm) = parsed_arg;
22740 /* Update any instantiations we've already created. */
22741 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22742 vec_safe_iterate (insts, ix, &copy); ix++)
22743 TREE_PURPOSE (copy) = parsed_arg;
22746 pop_defarg_context ();
22748 /* Make sure no default arg is missing. */
22749 check_default_args (fn);
22751 /* Restore the state of local_variables_forbidden_p. */
22752 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22754 /* Restore the queue. */
22755 pop_unparsed_function_queues (parser);
22758 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
22760 sizeof ... ( identifier )
22762 where the 'sizeof' token has already been consumed. */
22764 static tree
22765 cp_parser_sizeof_pack (cp_parser *parser)
22767 /* Consume the `...'. */
22768 cp_lexer_consume_token (parser->lexer);
22769 maybe_warn_variadic_templates ();
22771 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
22772 if (paren)
22773 cp_lexer_consume_token (parser->lexer);
22774 else
22775 permerror (cp_lexer_peek_token (parser->lexer)->location,
22776 "%<sizeof...%> argument must be surrounded by parentheses");
22778 cp_token *token = cp_lexer_peek_token (parser->lexer);
22779 tree name = cp_parser_identifier (parser);
22780 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
22781 if (expr == error_mark_node)
22782 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
22783 token->location);
22784 if (TREE_CODE (expr) == TYPE_DECL)
22785 expr = TREE_TYPE (expr);
22786 else if (TREE_CODE (expr) == CONST_DECL)
22787 expr = DECL_INITIAL (expr);
22788 expr = make_pack_expansion (expr);
22790 if (paren)
22791 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22793 return expr;
22796 /* Parse the operand of `sizeof' (or a similar operator). Returns
22797 either a TYPE or an expression, depending on the form of the
22798 input. The KEYWORD indicates which kind of expression we have
22799 encountered. */
22801 static tree
22802 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22804 tree expr = NULL_TREE;
22805 const char *saved_message;
22806 char *tmp;
22807 bool saved_integral_constant_expression_p;
22808 bool saved_non_integral_constant_expression_p;
22810 /* If it's a `...', then we are computing the length of a parameter
22811 pack. */
22812 if (keyword == RID_SIZEOF
22813 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22814 return cp_parser_sizeof_pack (parser);
22816 /* Types cannot be defined in a `sizeof' expression. Save away the
22817 old message. */
22818 saved_message = parser->type_definition_forbidden_message;
22819 /* And create the new one. */
22820 tmp = concat ("types may not be defined in %<",
22821 IDENTIFIER_POINTER (ridpointers[keyword]),
22822 "%> expressions", NULL);
22823 parser->type_definition_forbidden_message = tmp;
22825 /* The restrictions on constant-expressions do not apply inside
22826 sizeof expressions. */
22827 saved_integral_constant_expression_p
22828 = parser->integral_constant_expression_p;
22829 saved_non_integral_constant_expression_p
22830 = parser->non_integral_constant_expression_p;
22831 parser->integral_constant_expression_p = false;
22833 /* Do not actually evaluate the expression. */
22834 ++cp_unevaluated_operand;
22835 ++c_inhibit_evaluation_warnings;
22836 /* If it's a `(', then we might be looking at the type-id
22837 construction. */
22838 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22840 tree type;
22841 bool saved_in_type_id_in_expr_p;
22843 /* We can't be sure yet whether we're looking at a type-id or an
22844 expression. */
22845 cp_parser_parse_tentatively (parser);
22846 /* Consume the `('. */
22847 cp_lexer_consume_token (parser->lexer);
22848 /* Parse the type-id. */
22849 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22850 parser->in_type_id_in_expr_p = true;
22851 type = cp_parser_type_id (parser);
22852 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22853 /* Now, look for the trailing `)'. */
22854 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22855 /* If all went well, then we're done. */
22856 if (cp_parser_parse_definitely (parser))
22858 cp_decl_specifier_seq decl_specs;
22860 /* Build a trivial decl-specifier-seq. */
22861 clear_decl_specs (&decl_specs);
22862 decl_specs.type = type;
22864 /* Call grokdeclarator to figure out what type this is. */
22865 expr = grokdeclarator (NULL,
22866 &decl_specs,
22867 TYPENAME,
22868 /*initialized=*/0,
22869 /*attrlist=*/NULL);
22873 /* If the type-id production did not work out, then we must be
22874 looking at the unary-expression production. */
22875 if (!expr)
22876 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22877 /*cast_p=*/false, NULL);
22879 /* Go back to evaluating expressions. */
22880 --cp_unevaluated_operand;
22881 --c_inhibit_evaluation_warnings;
22883 /* Free the message we created. */
22884 free (tmp);
22885 /* And restore the old one. */
22886 parser->type_definition_forbidden_message = saved_message;
22887 parser->integral_constant_expression_p
22888 = saved_integral_constant_expression_p;
22889 parser->non_integral_constant_expression_p
22890 = saved_non_integral_constant_expression_p;
22892 return expr;
22895 /* If the current declaration has no declarator, return true. */
22897 static bool
22898 cp_parser_declares_only_class_p (cp_parser *parser)
22900 /* If the next token is a `;' or a `,' then there is no
22901 declarator. */
22902 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22903 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22906 /* Update the DECL_SPECS to reflect the storage class indicated by
22907 KEYWORD. */
22909 static void
22910 cp_parser_set_storage_class (cp_parser *parser,
22911 cp_decl_specifier_seq *decl_specs,
22912 enum rid keyword,
22913 cp_token *token)
22915 cp_storage_class storage_class;
22917 if (parser->in_unbraced_linkage_specification_p)
22919 error_at (token->location, "invalid use of %qD in linkage specification",
22920 ridpointers[keyword]);
22921 return;
22923 else if (decl_specs->storage_class != sc_none)
22925 decl_specs->conflicting_specifiers_p = true;
22926 return;
22929 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22930 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22931 && decl_specs->gnu_thread_keyword_p)
22933 pedwarn (decl_specs->locations[ds_thread], 0,
22934 "%<__thread%> before %qD", ridpointers[keyword]);
22937 switch (keyword)
22939 case RID_AUTO:
22940 storage_class = sc_auto;
22941 break;
22942 case RID_REGISTER:
22943 storage_class = sc_register;
22944 break;
22945 case RID_STATIC:
22946 storage_class = sc_static;
22947 break;
22948 case RID_EXTERN:
22949 storage_class = sc_extern;
22950 break;
22951 case RID_MUTABLE:
22952 storage_class = sc_mutable;
22953 break;
22954 default:
22955 gcc_unreachable ();
22957 decl_specs->storage_class = storage_class;
22958 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22960 /* A storage class specifier cannot be applied alongside a typedef
22961 specifier. If there is a typedef specifier present then set
22962 conflicting_specifiers_p which will trigger an error later
22963 on in grokdeclarator. */
22964 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22965 decl_specs->conflicting_specifiers_p = true;
22968 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22969 is true, the type is a class or enum definition. */
22971 static void
22972 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22973 tree type_spec,
22974 cp_token *token,
22975 bool type_definition_p)
22977 decl_specs->any_specifiers_p = true;
22979 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22980 (with, for example, in "typedef int wchar_t;") we remember that
22981 this is what happened. In system headers, we ignore these
22982 declarations so that G++ can work with system headers that are not
22983 C++-safe. */
22984 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22985 && !type_definition_p
22986 && (type_spec == boolean_type_node
22987 || type_spec == char16_type_node
22988 || type_spec == char32_type_node
22989 || type_spec == wchar_type_node)
22990 && (decl_specs->type
22991 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22992 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22993 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22994 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22996 decl_specs->redefined_builtin_type = type_spec;
22997 set_and_check_decl_spec_loc (decl_specs,
22998 ds_redefined_builtin_type_spec,
22999 token);
23000 if (!decl_specs->type)
23002 decl_specs->type = type_spec;
23003 decl_specs->type_definition_p = false;
23004 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
23007 else if (decl_specs->type)
23008 decl_specs->multiple_types_p = true;
23009 else
23011 decl_specs->type = type_spec;
23012 decl_specs->type_definition_p = type_definition_p;
23013 decl_specs->redefined_builtin_type = NULL_TREE;
23014 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
23018 /* True iff TOKEN is the GNU keyword __thread. */
23020 static bool
23021 token_is__thread (cp_token *token)
23023 gcc_assert (token->keyword == RID_THREAD);
23024 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
23027 /* Set the location for a declarator specifier and check if it is
23028 duplicated.
23030 DECL_SPECS is the sequence of declarator specifiers onto which to
23031 set the location.
23033 DS is the single declarator specifier to set which location is to
23034 be set onto the existing sequence of declarators.
23036 LOCATION is the location for the declarator specifier to
23037 consider. */
23039 static void
23040 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
23041 cp_decl_spec ds, cp_token *token)
23043 gcc_assert (ds < ds_last);
23045 if (decl_specs == NULL)
23046 return;
23048 source_location location = token->location;
23050 if (decl_specs->locations[ds] == 0)
23052 decl_specs->locations[ds] = location;
23053 if (ds == ds_thread)
23054 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
23056 else
23058 if (ds == ds_long)
23060 if (decl_specs->locations[ds_long_long] != 0)
23061 error_at (location,
23062 "%<long long long%> is too long for GCC");
23063 else
23065 decl_specs->locations[ds_long_long] = location;
23066 pedwarn_cxx98 (location,
23067 OPT_Wlong_long,
23068 "ISO C++ 1998 does not support %<long long%>");
23071 else if (ds == ds_thread)
23073 bool gnu = token_is__thread (token);
23074 if (gnu != decl_specs->gnu_thread_keyword_p)
23075 error_at (location,
23076 "both %<__thread%> and %<thread_local%> specified");
23077 else
23078 error_at (location, "duplicate %qD", token->u.value);
23080 else
23082 static const char *const decl_spec_names[] = {
23083 "signed",
23084 "unsigned",
23085 "short",
23086 "long",
23087 "const",
23088 "volatile",
23089 "restrict",
23090 "inline",
23091 "virtual",
23092 "explicit",
23093 "friend",
23094 "typedef",
23095 "using",
23096 "constexpr",
23097 "__complex"
23099 error_at (location,
23100 "duplicate %qs", decl_spec_names[ds]);
23105 /* Return true iff the declarator specifier DS is present in the
23106 sequence of declarator specifiers DECL_SPECS. */
23108 bool
23109 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23110 cp_decl_spec ds)
23112 gcc_assert (ds < ds_last);
23114 if (decl_specs == NULL)
23115 return false;
23117 return decl_specs->locations[ds] != 0;
23120 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23121 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23123 static bool
23124 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23126 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23129 /* Issue an error message indicating that TOKEN_DESC was expected.
23130 If KEYWORD is true, it indicated this function is called by
23131 cp_parser_require_keword and the required token can only be
23132 a indicated keyword. */
23134 static void
23135 cp_parser_required_error (cp_parser *parser,
23136 required_token token_desc,
23137 bool keyword)
23139 switch (token_desc)
23141 case RT_NEW:
23142 cp_parser_error (parser, "expected %<new%>");
23143 return;
23144 case RT_DELETE:
23145 cp_parser_error (parser, "expected %<delete%>");
23146 return;
23147 case RT_RETURN:
23148 cp_parser_error (parser, "expected %<return%>");
23149 return;
23150 case RT_WHILE:
23151 cp_parser_error (parser, "expected %<while%>");
23152 return;
23153 case RT_EXTERN:
23154 cp_parser_error (parser, "expected %<extern%>");
23155 return;
23156 case RT_STATIC_ASSERT:
23157 cp_parser_error (parser, "expected %<static_assert%>");
23158 return;
23159 case RT_DECLTYPE:
23160 cp_parser_error (parser, "expected %<decltype%>");
23161 return;
23162 case RT_OPERATOR:
23163 cp_parser_error (parser, "expected %<operator%>");
23164 return;
23165 case RT_CLASS:
23166 cp_parser_error (parser, "expected %<class%>");
23167 return;
23168 case RT_TEMPLATE:
23169 cp_parser_error (parser, "expected %<template%>");
23170 return;
23171 case RT_NAMESPACE:
23172 cp_parser_error (parser, "expected %<namespace%>");
23173 return;
23174 case RT_USING:
23175 cp_parser_error (parser, "expected %<using%>");
23176 return;
23177 case RT_ASM:
23178 cp_parser_error (parser, "expected %<asm%>");
23179 return;
23180 case RT_TRY:
23181 cp_parser_error (parser, "expected %<try%>");
23182 return;
23183 case RT_CATCH:
23184 cp_parser_error (parser, "expected %<catch%>");
23185 return;
23186 case RT_THROW:
23187 cp_parser_error (parser, "expected %<throw%>");
23188 return;
23189 case RT_LABEL:
23190 cp_parser_error (parser, "expected %<__label__%>");
23191 return;
23192 case RT_AT_TRY:
23193 cp_parser_error (parser, "expected %<@try%>");
23194 return;
23195 case RT_AT_SYNCHRONIZED:
23196 cp_parser_error (parser, "expected %<@synchronized%>");
23197 return;
23198 case RT_AT_THROW:
23199 cp_parser_error (parser, "expected %<@throw%>");
23200 return;
23201 case RT_TRANSACTION_ATOMIC:
23202 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23203 return;
23204 case RT_TRANSACTION_RELAXED:
23205 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23206 return;
23207 default:
23208 break;
23210 if (!keyword)
23212 switch (token_desc)
23214 case RT_SEMICOLON:
23215 cp_parser_error (parser, "expected %<;%>");
23216 return;
23217 case RT_OPEN_PAREN:
23218 cp_parser_error (parser, "expected %<(%>");
23219 return;
23220 case RT_CLOSE_BRACE:
23221 cp_parser_error (parser, "expected %<}%>");
23222 return;
23223 case RT_OPEN_BRACE:
23224 cp_parser_error (parser, "expected %<{%>");
23225 return;
23226 case RT_CLOSE_SQUARE:
23227 cp_parser_error (parser, "expected %<]%>");
23228 return;
23229 case RT_OPEN_SQUARE:
23230 cp_parser_error (parser, "expected %<[%>");
23231 return;
23232 case RT_COMMA:
23233 cp_parser_error (parser, "expected %<,%>");
23234 return;
23235 case RT_SCOPE:
23236 cp_parser_error (parser, "expected %<::%>");
23237 return;
23238 case RT_LESS:
23239 cp_parser_error (parser, "expected %<<%>");
23240 return;
23241 case RT_GREATER:
23242 cp_parser_error (parser, "expected %<>%>");
23243 return;
23244 case RT_EQ:
23245 cp_parser_error (parser, "expected %<=%>");
23246 return;
23247 case RT_ELLIPSIS:
23248 cp_parser_error (parser, "expected %<...%>");
23249 return;
23250 case RT_MULT:
23251 cp_parser_error (parser, "expected %<*%>");
23252 return;
23253 case RT_COMPL:
23254 cp_parser_error (parser, "expected %<~%>");
23255 return;
23256 case RT_COLON:
23257 cp_parser_error (parser, "expected %<:%>");
23258 return;
23259 case RT_COLON_SCOPE:
23260 cp_parser_error (parser, "expected %<:%> or %<::%>");
23261 return;
23262 case RT_CLOSE_PAREN:
23263 cp_parser_error (parser, "expected %<)%>");
23264 return;
23265 case RT_COMMA_CLOSE_PAREN:
23266 cp_parser_error (parser, "expected %<,%> or %<)%>");
23267 return;
23268 case RT_PRAGMA_EOL:
23269 cp_parser_error (parser, "expected end of line");
23270 return;
23271 case RT_NAME:
23272 cp_parser_error (parser, "expected identifier");
23273 return;
23274 case RT_SELECT:
23275 cp_parser_error (parser, "expected selection-statement");
23276 return;
23277 case RT_INTERATION:
23278 cp_parser_error (parser, "expected iteration-statement");
23279 return;
23280 case RT_JUMP:
23281 cp_parser_error (parser, "expected jump-statement");
23282 return;
23283 case RT_CLASS_KEY:
23284 cp_parser_error (parser, "expected class-key");
23285 return;
23286 case RT_CLASS_TYPENAME_TEMPLATE:
23287 cp_parser_error (parser,
23288 "expected %<class%>, %<typename%>, or %<template%>");
23289 return;
23290 default:
23291 gcc_unreachable ();
23294 else
23295 gcc_unreachable ();
23300 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23301 issue an error message indicating that TOKEN_DESC was expected.
23303 Returns the token consumed, if the token had the appropriate type.
23304 Otherwise, returns NULL. */
23306 static cp_token *
23307 cp_parser_require (cp_parser* parser,
23308 enum cpp_ttype type,
23309 required_token token_desc)
23311 if (cp_lexer_next_token_is (parser->lexer, type))
23312 return cp_lexer_consume_token (parser->lexer);
23313 else
23315 /* Output the MESSAGE -- unless we're parsing tentatively. */
23316 if (!cp_parser_simulate_error (parser))
23317 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23318 return NULL;
23322 /* An error message is produced if the next token is not '>'.
23323 All further tokens are skipped until the desired token is
23324 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23326 static void
23327 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23329 /* Current level of '< ... >'. */
23330 unsigned level = 0;
23331 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23332 unsigned nesting_depth = 0;
23334 /* Are we ready, yet? If not, issue error message. */
23335 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23336 return;
23338 /* Skip tokens until the desired token is found. */
23339 while (true)
23341 /* Peek at the next token. */
23342 switch (cp_lexer_peek_token (parser->lexer)->type)
23344 case CPP_LESS:
23345 if (!nesting_depth)
23346 ++level;
23347 break;
23349 case CPP_RSHIFT:
23350 if (cxx_dialect == cxx98)
23351 /* C++0x views the `>>' operator as two `>' tokens, but
23352 C++98 does not. */
23353 break;
23354 else if (!nesting_depth && level-- == 0)
23356 /* We've hit a `>>' where the first `>' closes the
23357 template argument list, and the second `>' is
23358 spurious. Just consume the `>>' and stop; we've
23359 already produced at least one error. */
23360 cp_lexer_consume_token (parser->lexer);
23361 return;
23363 /* Fall through for C++0x, so we handle the second `>' in
23364 the `>>'. */
23366 case CPP_GREATER:
23367 if (!nesting_depth && level-- == 0)
23369 /* We've reached the token we want, consume it and stop. */
23370 cp_lexer_consume_token (parser->lexer);
23371 return;
23373 break;
23375 case CPP_OPEN_PAREN:
23376 case CPP_OPEN_SQUARE:
23377 ++nesting_depth;
23378 break;
23380 case CPP_CLOSE_PAREN:
23381 case CPP_CLOSE_SQUARE:
23382 if (nesting_depth-- == 0)
23383 return;
23384 break;
23386 case CPP_EOF:
23387 case CPP_PRAGMA_EOL:
23388 case CPP_SEMICOLON:
23389 case CPP_OPEN_BRACE:
23390 case CPP_CLOSE_BRACE:
23391 /* The '>' was probably forgotten, don't look further. */
23392 return;
23394 default:
23395 break;
23398 /* Consume this token. */
23399 cp_lexer_consume_token (parser->lexer);
23403 /* If the next token is the indicated keyword, consume it. Otherwise,
23404 issue an error message indicating that TOKEN_DESC was expected.
23406 Returns the token consumed, if the token had the appropriate type.
23407 Otherwise, returns NULL. */
23409 static cp_token *
23410 cp_parser_require_keyword (cp_parser* parser,
23411 enum rid keyword,
23412 required_token token_desc)
23414 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23416 if (token && token->keyword != keyword)
23418 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23419 return NULL;
23422 return token;
23425 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23426 function-definition. */
23428 static bool
23429 cp_parser_token_starts_function_definition_p (cp_token* token)
23431 return (/* An ordinary function-body begins with an `{'. */
23432 token->type == CPP_OPEN_BRACE
23433 /* A ctor-initializer begins with a `:'. */
23434 || token->type == CPP_COLON
23435 /* A function-try-block begins with `try'. */
23436 || token->keyword == RID_TRY
23437 /* A function-transaction-block begins with `__transaction_atomic'
23438 or `__transaction_relaxed'. */
23439 || token->keyword == RID_TRANSACTION_ATOMIC
23440 || token->keyword == RID_TRANSACTION_RELAXED
23441 /* The named return value extension begins with `return'. */
23442 || token->keyword == RID_RETURN);
23445 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23446 definition. */
23448 static bool
23449 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23451 cp_token *token;
23453 token = cp_lexer_peek_token (parser->lexer);
23454 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23457 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23458 C++0x) ending a template-argument. */
23460 static bool
23461 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23463 cp_token *token;
23465 token = cp_lexer_peek_token (parser->lexer);
23466 return (token->type == CPP_COMMA
23467 || token->type == CPP_GREATER
23468 || token->type == CPP_ELLIPSIS
23469 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23472 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23473 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23475 static bool
23476 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23477 size_t n)
23479 cp_token *token;
23481 token = cp_lexer_peek_nth_token (parser->lexer, n);
23482 if (token->type == CPP_LESS)
23483 return true;
23484 /* Check for the sequence `<::' in the original code. It would be lexed as
23485 `[:', where `[' is a digraph, and there is no whitespace before
23486 `:'. */
23487 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23489 cp_token *token2;
23490 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23491 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23492 return true;
23494 return false;
23497 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23498 or none_type otherwise. */
23500 static enum tag_types
23501 cp_parser_token_is_class_key (cp_token* token)
23503 switch (token->keyword)
23505 case RID_CLASS:
23506 return class_type;
23507 case RID_STRUCT:
23508 return record_type;
23509 case RID_UNION:
23510 return union_type;
23512 default:
23513 return none_type;
23517 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23519 static void
23520 cp_parser_check_class_key (enum tag_types class_key, tree type)
23522 if (type == error_mark_node)
23523 return;
23524 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23526 if (permerror (input_location, "%qs tag used in naming %q#T",
23527 class_key == union_type ? "union"
23528 : class_key == record_type ? "struct" : "class",
23529 type))
23530 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23531 "%q#T was previously declared here", type);
23535 /* Issue an error message if DECL is redeclared with different
23536 access than its original declaration [class.access.spec/3].
23537 This applies to nested classes and nested class templates.
23538 [class.mem/1]. */
23540 static void
23541 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23543 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23544 return;
23546 if ((TREE_PRIVATE (decl)
23547 != (current_access_specifier == access_private_node))
23548 || (TREE_PROTECTED (decl)
23549 != (current_access_specifier == access_protected_node)))
23550 error_at (location, "%qD redeclared with different access", decl);
23553 /* Look for the `template' keyword, as a syntactic disambiguator.
23554 Return TRUE iff it is present, in which case it will be
23555 consumed. */
23557 static bool
23558 cp_parser_optional_template_keyword (cp_parser *parser)
23560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23562 /* In C++98 the `template' keyword can only be used within templates;
23563 outside templates the parser can always figure out what is a
23564 template and what is not. In C++11, per the resolution of DR 468,
23565 `template' is allowed in cases where it is not strictly necessary. */
23566 if (!processing_template_decl
23567 && pedantic && cxx_dialect == cxx98)
23569 cp_token *token = cp_lexer_peek_token (parser->lexer);
23570 pedwarn (token->location, OPT_Wpedantic,
23571 "in C++98 %<template%> (as a disambiguator) is only "
23572 "allowed within templates");
23573 /* If this part of the token stream is rescanned, the same
23574 error message would be generated. So, we purge the token
23575 from the stream. */
23576 cp_lexer_purge_token (parser->lexer);
23577 return false;
23579 else
23581 /* Consume the `template' keyword. */
23582 cp_lexer_consume_token (parser->lexer);
23583 return true;
23586 return false;
23589 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23590 set PARSER->SCOPE, and perform other related actions. */
23592 static void
23593 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23595 int i;
23596 struct tree_check *check_value;
23597 deferred_access_check *chk;
23598 vec<deferred_access_check, va_gc> *checks;
23600 /* Get the stored value. */
23601 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23602 /* Perform any access checks that were deferred. */
23603 checks = check_value->checks;
23604 if (checks)
23606 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23607 perform_or_defer_access_check (chk->binfo,
23608 chk->decl,
23609 chk->diag_decl, tf_warning_or_error);
23611 /* Set the scope from the stored value. */
23612 parser->scope = check_value->value;
23613 parser->qualifying_scope = check_value->qualifying_scope;
23614 parser->object_scope = NULL_TREE;
23617 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23618 encounter the end of a block before what we were looking for. */
23620 static bool
23621 cp_parser_cache_group (cp_parser *parser,
23622 enum cpp_ttype end,
23623 unsigned depth)
23625 while (true)
23627 cp_token *token = cp_lexer_peek_token (parser->lexer);
23629 /* Abort a parenthesized expression if we encounter a semicolon. */
23630 if ((end == CPP_CLOSE_PAREN || depth == 0)
23631 && token->type == CPP_SEMICOLON)
23632 return true;
23633 /* If we've reached the end of the file, stop. */
23634 if (token->type == CPP_EOF
23635 || (end != CPP_PRAGMA_EOL
23636 && token->type == CPP_PRAGMA_EOL))
23637 return true;
23638 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23639 /* We've hit the end of an enclosing block, so there's been some
23640 kind of syntax error. */
23641 return true;
23643 /* Consume the token. */
23644 cp_lexer_consume_token (parser->lexer);
23645 /* See if it starts a new group. */
23646 if (token->type == CPP_OPEN_BRACE)
23648 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23649 /* In theory this should probably check end == '}', but
23650 cp_parser_save_member_function_body needs it to exit
23651 after either '}' or ')' when called with ')'. */
23652 if (depth == 0)
23653 return false;
23655 else if (token->type == CPP_OPEN_PAREN)
23657 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23658 if (depth == 0 && end == CPP_CLOSE_PAREN)
23659 return false;
23661 else if (token->type == CPP_PRAGMA)
23662 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23663 else if (token->type == end)
23664 return false;
23668 /* Like above, for caching a default argument or NSDMI. Both of these are
23669 terminated by a non-nested comma, but it can be unclear whether or not a
23670 comma is nested in a template argument list unless we do more parsing.
23671 In order to handle this ambiguity, when we encounter a ',' after a '<'
23672 we try to parse what follows as a parameter-declaration-list (in the
23673 case of a default argument) or a member-declarator (in the case of an
23674 NSDMI). If that succeeds, then we stop caching. */
23676 static tree
23677 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23679 unsigned depth = 0;
23680 int maybe_template_id = 0;
23681 cp_token *first_token;
23682 cp_token *token;
23683 tree default_argument;
23685 /* Add tokens until we have processed the entire default
23686 argument. We add the range [first_token, token). */
23687 first_token = cp_lexer_peek_token (parser->lexer);
23688 if (first_token->type == CPP_OPEN_BRACE)
23690 /* For list-initialization, this is straightforward. */
23691 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23692 token = cp_lexer_peek_token (parser->lexer);
23694 else while (true)
23696 bool done = false;
23698 /* Peek at the next token. */
23699 token = cp_lexer_peek_token (parser->lexer);
23700 /* What we do depends on what token we have. */
23701 switch (token->type)
23703 /* In valid code, a default argument must be
23704 immediately followed by a `,' `)', or `...'. */
23705 case CPP_COMMA:
23706 if (depth == 0 && maybe_template_id)
23708 /* If we've seen a '<', we might be in a
23709 template-argument-list. Until Core issue 325 is
23710 resolved, we don't know how this situation ought
23711 to be handled, so try to DTRT. We check whether
23712 what comes after the comma is a valid parameter
23713 declaration list. If it is, then the comma ends
23714 the default argument; otherwise the default
23715 argument continues. */
23716 bool error = false;
23717 tree t;
23719 /* Set ITALP so cp_parser_parameter_declaration_list
23720 doesn't decide to commit to this parse. */
23721 bool saved_italp = parser->in_template_argument_list_p;
23722 parser->in_template_argument_list_p = true;
23724 cp_parser_parse_tentatively (parser);
23725 cp_lexer_consume_token (parser->lexer);
23727 if (nsdmi)
23729 int ctor_dtor_or_conv_p;
23730 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23731 &ctor_dtor_or_conv_p,
23732 /*parenthesized_p=*/NULL,
23733 /*member_p=*/true);
23735 else
23737 begin_scope (sk_function_parms, NULL_TREE);
23738 cp_parser_parameter_declaration_list (parser, &error);
23739 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23740 pop_binding (DECL_NAME (t), t);
23741 leave_scope ();
23743 if (!cp_parser_error_occurred (parser) && !error)
23744 done = true;
23745 cp_parser_abort_tentative_parse (parser);
23747 parser->in_template_argument_list_p = saved_italp;
23748 break;
23750 case CPP_CLOSE_PAREN:
23751 case CPP_ELLIPSIS:
23752 /* If we run into a non-nested `;', `}', or `]',
23753 then the code is invalid -- but the default
23754 argument is certainly over. */
23755 case CPP_SEMICOLON:
23756 case CPP_CLOSE_BRACE:
23757 case CPP_CLOSE_SQUARE:
23758 if (depth == 0)
23759 done = true;
23760 /* Update DEPTH, if necessary. */
23761 else if (token->type == CPP_CLOSE_PAREN
23762 || token->type == CPP_CLOSE_BRACE
23763 || token->type == CPP_CLOSE_SQUARE)
23764 --depth;
23765 break;
23767 case CPP_OPEN_PAREN:
23768 case CPP_OPEN_SQUARE:
23769 case CPP_OPEN_BRACE:
23770 ++depth;
23771 break;
23773 case CPP_LESS:
23774 if (depth == 0)
23775 /* This might be the comparison operator, or it might
23776 start a template argument list. */
23777 ++maybe_template_id;
23778 break;
23780 case CPP_RSHIFT:
23781 if (cxx_dialect == cxx98)
23782 break;
23783 /* Fall through for C++0x, which treats the `>>'
23784 operator like two `>' tokens in certain
23785 cases. */
23787 case CPP_GREATER:
23788 if (depth == 0)
23790 /* This might be an operator, or it might close a
23791 template argument list. But if a previous '<'
23792 started a template argument list, this will have
23793 closed it, so we can't be in one anymore. */
23794 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23795 if (maybe_template_id < 0)
23796 maybe_template_id = 0;
23798 break;
23800 /* If we run out of tokens, issue an error message. */
23801 case CPP_EOF:
23802 case CPP_PRAGMA_EOL:
23803 error_at (token->location, "file ends in default argument");
23804 done = true;
23805 break;
23807 case CPP_NAME:
23808 case CPP_SCOPE:
23809 /* In these cases, we should look for template-ids.
23810 For example, if the default argument is
23811 `X<int, double>()', we need to do name lookup to
23812 figure out whether or not `X' is a template; if
23813 so, the `,' does not end the default argument.
23815 That is not yet done. */
23816 break;
23818 default:
23819 break;
23822 /* If we've reached the end, stop. */
23823 if (done)
23824 break;
23826 /* Add the token to the token block. */
23827 token = cp_lexer_consume_token (parser->lexer);
23830 /* Create a DEFAULT_ARG to represent the unparsed default
23831 argument. */
23832 default_argument = make_node (DEFAULT_ARG);
23833 DEFARG_TOKENS (default_argument)
23834 = cp_token_cache_new (first_token, token);
23835 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23837 return default_argument;
23840 /* Begin parsing tentatively. We always save tokens while parsing
23841 tentatively so that if the tentative parsing fails we can restore the
23842 tokens. */
23844 static void
23845 cp_parser_parse_tentatively (cp_parser* parser)
23847 /* Enter a new parsing context. */
23848 parser->context = cp_parser_context_new (parser->context);
23849 /* Begin saving tokens. */
23850 cp_lexer_save_tokens (parser->lexer);
23851 /* In order to avoid repetitive access control error messages,
23852 access checks are queued up until we are no longer parsing
23853 tentatively. */
23854 push_deferring_access_checks (dk_deferred);
23857 /* Commit to the currently active tentative parse. */
23859 static void
23860 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23862 cp_parser_context *context;
23863 cp_lexer *lexer;
23865 /* Mark all of the levels as committed. */
23866 lexer = parser->lexer;
23867 for (context = parser->context; context->next; context = context->next)
23869 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23870 break;
23871 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23872 while (!cp_lexer_saving_tokens (lexer))
23873 lexer = lexer->next;
23874 cp_lexer_commit_tokens (lexer);
23878 /* Abort the currently active tentative parse. All consumed tokens
23879 will be rolled back, and no diagnostics will be issued. */
23881 static void
23882 cp_parser_abort_tentative_parse (cp_parser* parser)
23884 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23885 || errorcount > 0);
23886 cp_parser_simulate_error (parser);
23887 /* Now, pretend that we want to see if the construct was
23888 successfully parsed. */
23889 cp_parser_parse_definitely (parser);
23892 /* Stop parsing tentatively. If a parse error has occurred, restore the
23893 token stream. Otherwise, commit to the tokens we have consumed.
23894 Returns true if no error occurred; false otherwise. */
23896 static bool
23897 cp_parser_parse_definitely (cp_parser* parser)
23899 bool error_occurred;
23900 cp_parser_context *context;
23902 /* Remember whether or not an error occurred, since we are about to
23903 destroy that information. */
23904 error_occurred = cp_parser_error_occurred (parser);
23905 /* Remove the topmost context from the stack. */
23906 context = parser->context;
23907 parser->context = context->next;
23908 /* If no parse errors occurred, commit to the tentative parse. */
23909 if (!error_occurred)
23911 /* Commit to the tokens read tentatively, unless that was
23912 already done. */
23913 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23914 cp_lexer_commit_tokens (parser->lexer);
23916 pop_to_parent_deferring_access_checks ();
23918 /* Otherwise, if errors occurred, roll back our state so that things
23919 are just as they were before we began the tentative parse. */
23920 else
23922 cp_lexer_rollback_tokens (parser->lexer);
23923 pop_deferring_access_checks ();
23925 /* Add the context to the front of the free list. */
23926 context->next = cp_parser_context_free_list;
23927 cp_parser_context_free_list = context;
23929 return !error_occurred;
23932 /* Returns true if we are parsing tentatively and are not committed to
23933 this tentative parse. */
23935 static bool
23936 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23938 return (cp_parser_parsing_tentatively (parser)
23939 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23942 /* Returns nonzero iff an error has occurred during the most recent
23943 tentative parse. */
23945 static bool
23946 cp_parser_error_occurred (cp_parser* parser)
23948 return (cp_parser_parsing_tentatively (parser)
23949 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23952 /* Returns nonzero if GNU extensions are allowed. */
23954 static bool
23955 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23957 return parser->allow_gnu_extensions_p;
23960 /* Objective-C++ Productions */
23963 /* Parse an Objective-C expression, which feeds into a primary-expression
23964 above.
23966 objc-expression:
23967 objc-message-expression
23968 objc-string-literal
23969 objc-encode-expression
23970 objc-protocol-expression
23971 objc-selector-expression
23973 Returns a tree representation of the expression. */
23975 static tree
23976 cp_parser_objc_expression (cp_parser* parser)
23978 /* Try to figure out what kind of declaration is present. */
23979 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23981 switch (kwd->type)
23983 case CPP_OPEN_SQUARE:
23984 return cp_parser_objc_message_expression (parser);
23986 case CPP_OBJC_STRING:
23987 kwd = cp_lexer_consume_token (parser->lexer);
23988 return objc_build_string_object (kwd->u.value);
23990 case CPP_KEYWORD:
23991 switch (kwd->keyword)
23993 case RID_AT_ENCODE:
23994 return cp_parser_objc_encode_expression (parser);
23996 case RID_AT_PROTOCOL:
23997 return cp_parser_objc_protocol_expression (parser);
23999 case RID_AT_SELECTOR:
24000 return cp_parser_objc_selector_expression (parser);
24002 default:
24003 break;
24005 default:
24006 error_at (kwd->location,
24007 "misplaced %<@%D%> Objective-C++ construct",
24008 kwd->u.value);
24009 cp_parser_skip_to_end_of_block_or_statement (parser);
24012 return error_mark_node;
24015 /* Parse an Objective-C message expression.
24017 objc-message-expression:
24018 [ objc-message-receiver objc-message-args ]
24020 Returns a representation of an Objective-C message. */
24022 static tree
24023 cp_parser_objc_message_expression (cp_parser* parser)
24025 tree receiver, messageargs;
24027 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
24028 receiver = cp_parser_objc_message_receiver (parser);
24029 messageargs = cp_parser_objc_message_args (parser);
24030 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24032 return objc_build_message_expr (receiver, messageargs);
24035 /* Parse an objc-message-receiver.
24037 objc-message-receiver:
24038 expression
24039 simple-type-specifier
24041 Returns a representation of the type or expression. */
24043 static tree
24044 cp_parser_objc_message_receiver (cp_parser* parser)
24046 tree rcv;
24048 /* An Objective-C message receiver may be either (1) a type
24049 or (2) an expression. */
24050 cp_parser_parse_tentatively (parser);
24051 rcv = cp_parser_expression (parser, false, NULL);
24053 if (cp_parser_parse_definitely (parser))
24054 return rcv;
24056 rcv = cp_parser_simple_type_specifier (parser,
24057 /*decl_specs=*/NULL,
24058 CP_PARSER_FLAGS_NONE);
24060 return objc_get_class_reference (rcv);
24063 /* Parse the arguments and selectors comprising an Objective-C message.
24065 objc-message-args:
24066 objc-selector
24067 objc-selector-args
24068 objc-selector-args , objc-comma-args
24070 objc-selector-args:
24071 objc-selector [opt] : assignment-expression
24072 objc-selector-args objc-selector [opt] : assignment-expression
24074 objc-comma-args:
24075 assignment-expression
24076 objc-comma-args , assignment-expression
24078 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24079 selector arguments and TREE_VALUE containing a list of comma
24080 arguments. */
24082 static tree
24083 cp_parser_objc_message_args (cp_parser* parser)
24085 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
24086 bool maybe_unary_selector_p = true;
24087 cp_token *token = cp_lexer_peek_token (parser->lexer);
24089 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24091 tree selector = NULL_TREE, arg;
24093 if (token->type != CPP_COLON)
24094 selector = cp_parser_objc_selector (parser);
24096 /* Detect if we have a unary selector. */
24097 if (maybe_unary_selector_p
24098 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24099 return build_tree_list (selector, NULL_TREE);
24101 maybe_unary_selector_p = false;
24102 cp_parser_require (parser, CPP_COLON, RT_COLON);
24103 arg = cp_parser_assignment_expression (parser, false, NULL);
24105 sel_args
24106 = chainon (sel_args,
24107 build_tree_list (selector, arg));
24109 token = cp_lexer_peek_token (parser->lexer);
24112 /* Handle non-selector arguments, if any. */
24113 while (token->type == CPP_COMMA)
24115 tree arg;
24117 cp_lexer_consume_token (parser->lexer);
24118 arg = cp_parser_assignment_expression (parser, false, NULL);
24120 addl_args
24121 = chainon (addl_args,
24122 build_tree_list (NULL_TREE, arg));
24124 token = cp_lexer_peek_token (parser->lexer);
24127 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24129 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24130 return build_tree_list (error_mark_node, error_mark_node);
24133 return build_tree_list (sel_args, addl_args);
24136 /* Parse an Objective-C encode expression.
24138 objc-encode-expression:
24139 @encode objc-typename
24141 Returns an encoded representation of the type argument. */
24143 static tree
24144 cp_parser_objc_encode_expression (cp_parser* parser)
24146 tree type;
24147 cp_token *token;
24149 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24150 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24151 token = cp_lexer_peek_token (parser->lexer);
24152 type = complete_type (cp_parser_type_id (parser));
24153 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24155 if (!type)
24157 error_at (token->location,
24158 "%<@encode%> must specify a type as an argument");
24159 return error_mark_node;
24162 /* This happens if we find @encode(T) (where T is a template
24163 typename or something dependent on a template typename) when
24164 parsing a template. In that case, we can't compile it
24165 immediately, but we rather create an AT_ENCODE_EXPR which will
24166 need to be instantiated when the template is used.
24168 if (dependent_type_p (type))
24170 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24171 TREE_READONLY (value) = 1;
24172 return value;
24175 return objc_build_encode_expr (type);
24178 /* Parse an Objective-C @defs expression. */
24180 static tree
24181 cp_parser_objc_defs_expression (cp_parser *parser)
24183 tree name;
24185 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24186 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24187 name = cp_parser_identifier (parser);
24188 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24190 return objc_get_class_ivars (name);
24193 /* Parse an Objective-C protocol expression.
24195 objc-protocol-expression:
24196 @protocol ( identifier )
24198 Returns a representation of the protocol expression. */
24200 static tree
24201 cp_parser_objc_protocol_expression (cp_parser* parser)
24203 tree proto;
24205 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24207 proto = cp_parser_identifier (parser);
24208 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24210 return objc_build_protocol_expr (proto);
24213 /* Parse an Objective-C selector expression.
24215 objc-selector-expression:
24216 @selector ( objc-method-signature )
24218 objc-method-signature:
24219 objc-selector
24220 objc-selector-seq
24222 objc-selector-seq:
24223 objc-selector :
24224 objc-selector-seq objc-selector :
24226 Returns a representation of the method selector. */
24228 static tree
24229 cp_parser_objc_selector_expression (cp_parser* parser)
24231 tree sel_seq = NULL_TREE;
24232 bool maybe_unary_selector_p = true;
24233 cp_token *token;
24234 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24236 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
24237 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24238 token = cp_lexer_peek_token (parser->lexer);
24240 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24241 || token->type == CPP_SCOPE)
24243 tree selector = NULL_TREE;
24245 if (token->type != CPP_COLON
24246 || token->type == CPP_SCOPE)
24247 selector = cp_parser_objc_selector (parser);
24249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24250 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24252 /* Detect if we have a unary selector. */
24253 if (maybe_unary_selector_p)
24255 sel_seq = selector;
24256 goto finish_selector;
24258 else
24260 cp_parser_error (parser, "expected %<:%>");
24263 maybe_unary_selector_p = false;
24264 token = cp_lexer_consume_token (parser->lexer);
24266 if (token->type == CPP_SCOPE)
24268 sel_seq
24269 = chainon (sel_seq,
24270 build_tree_list (selector, NULL_TREE));
24271 sel_seq
24272 = chainon (sel_seq,
24273 build_tree_list (NULL_TREE, NULL_TREE));
24275 else
24276 sel_seq
24277 = chainon (sel_seq,
24278 build_tree_list (selector, NULL_TREE));
24280 token = cp_lexer_peek_token (parser->lexer);
24283 finish_selector:
24284 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24286 return objc_build_selector_expr (loc, sel_seq);
24289 /* Parse a list of identifiers.
24291 objc-identifier-list:
24292 identifier
24293 objc-identifier-list , identifier
24295 Returns a TREE_LIST of identifier nodes. */
24297 static tree
24298 cp_parser_objc_identifier_list (cp_parser* parser)
24300 tree identifier;
24301 tree list;
24302 cp_token *sep;
24304 identifier = cp_parser_identifier (parser);
24305 if (identifier == error_mark_node)
24306 return error_mark_node;
24308 list = build_tree_list (NULL_TREE, identifier);
24309 sep = cp_lexer_peek_token (parser->lexer);
24311 while (sep->type == CPP_COMMA)
24313 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24314 identifier = cp_parser_identifier (parser);
24315 if (identifier == error_mark_node)
24316 return list;
24318 list = chainon (list, build_tree_list (NULL_TREE,
24319 identifier));
24320 sep = cp_lexer_peek_token (parser->lexer);
24323 return list;
24326 /* Parse an Objective-C alias declaration.
24328 objc-alias-declaration:
24329 @compatibility_alias identifier identifier ;
24331 This function registers the alias mapping with the Objective-C front end.
24332 It returns nothing. */
24334 static void
24335 cp_parser_objc_alias_declaration (cp_parser* parser)
24337 tree alias, orig;
24339 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24340 alias = cp_parser_identifier (parser);
24341 orig = cp_parser_identifier (parser);
24342 objc_declare_alias (alias, orig);
24343 cp_parser_consume_semicolon_at_end_of_statement (parser);
24346 /* Parse an Objective-C class forward-declaration.
24348 objc-class-declaration:
24349 @class objc-identifier-list ;
24351 The function registers the forward declarations with the Objective-C
24352 front end. It returns nothing. */
24354 static void
24355 cp_parser_objc_class_declaration (cp_parser* parser)
24357 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24358 while (true)
24360 tree id;
24362 id = cp_parser_identifier (parser);
24363 if (id == error_mark_node)
24364 break;
24366 objc_declare_class (id);
24368 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24369 cp_lexer_consume_token (parser->lexer);
24370 else
24371 break;
24373 cp_parser_consume_semicolon_at_end_of_statement (parser);
24376 /* Parse a list of Objective-C protocol references.
24378 objc-protocol-refs-opt:
24379 objc-protocol-refs [opt]
24381 objc-protocol-refs:
24382 < objc-identifier-list >
24384 Returns a TREE_LIST of identifiers, if any. */
24386 static tree
24387 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24389 tree protorefs = NULL_TREE;
24391 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24393 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24394 protorefs = cp_parser_objc_identifier_list (parser);
24395 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24398 return protorefs;
24401 /* Parse a Objective-C visibility specification. */
24403 static void
24404 cp_parser_objc_visibility_spec (cp_parser* parser)
24406 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24408 switch (vis->keyword)
24410 case RID_AT_PRIVATE:
24411 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24412 break;
24413 case RID_AT_PROTECTED:
24414 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24415 break;
24416 case RID_AT_PUBLIC:
24417 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24418 break;
24419 case RID_AT_PACKAGE:
24420 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24421 break;
24422 default:
24423 return;
24426 /* Eat '@private'/'@protected'/'@public'. */
24427 cp_lexer_consume_token (parser->lexer);
24430 /* Parse an Objective-C method type. Return 'true' if it is a class
24431 (+) method, and 'false' if it is an instance (-) method. */
24433 static inline bool
24434 cp_parser_objc_method_type (cp_parser* parser)
24436 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24437 return true;
24438 else
24439 return false;
24442 /* Parse an Objective-C protocol qualifier. */
24444 static tree
24445 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24447 tree quals = NULL_TREE, node;
24448 cp_token *token = cp_lexer_peek_token (parser->lexer);
24450 node = token->u.value;
24452 while (node && identifier_p (node)
24453 && (node == ridpointers [(int) RID_IN]
24454 || node == ridpointers [(int) RID_OUT]
24455 || node == ridpointers [(int) RID_INOUT]
24456 || node == ridpointers [(int) RID_BYCOPY]
24457 || node == ridpointers [(int) RID_BYREF]
24458 || node == ridpointers [(int) RID_ONEWAY]))
24460 quals = tree_cons (NULL_TREE, node, quals);
24461 cp_lexer_consume_token (parser->lexer);
24462 token = cp_lexer_peek_token (parser->lexer);
24463 node = token->u.value;
24466 return quals;
24469 /* Parse an Objective-C typename. */
24471 static tree
24472 cp_parser_objc_typename (cp_parser* parser)
24474 tree type_name = NULL_TREE;
24476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24478 tree proto_quals, cp_type = NULL_TREE;
24480 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24481 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24483 /* An ObjC type name may consist of just protocol qualifiers, in which
24484 case the type shall default to 'id'. */
24485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24487 cp_type = cp_parser_type_id (parser);
24489 /* If the type could not be parsed, an error has already
24490 been produced. For error recovery, behave as if it had
24491 not been specified, which will use the default type
24492 'id'. */
24493 if (cp_type == error_mark_node)
24495 cp_type = NULL_TREE;
24496 /* We need to skip to the closing parenthesis as
24497 cp_parser_type_id() does not seem to do it for
24498 us. */
24499 cp_parser_skip_to_closing_parenthesis (parser,
24500 /*recovering=*/true,
24501 /*or_comma=*/false,
24502 /*consume_paren=*/false);
24506 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24507 type_name = build_tree_list (proto_quals, cp_type);
24510 return type_name;
24513 /* Check to see if TYPE refers to an Objective-C selector name. */
24515 static bool
24516 cp_parser_objc_selector_p (enum cpp_ttype type)
24518 return (type == CPP_NAME || type == CPP_KEYWORD
24519 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24520 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24521 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24522 || type == CPP_XOR || type == CPP_XOR_EQ);
24525 /* Parse an Objective-C selector. */
24527 static tree
24528 cp_parser_objc_selector (cp_parser* parser)
24530 cp_token *token = cp_lexer_consume_token (parser->lexer);
24532 if (!cp_parser_objc_selector_p (token->type))
24534 error_at (token->location, "invalid Objective-C++ selector name");
24535 return error_mark_node;
24538 /* C++ operator names are allowed to appear in ObjC selectors. */
24539 switch (token->type)
24541 case CPP_AND_AND: return get_identifier ("and");
24542 case CPP_AND_EQ: return get_identifier ("and_eq");
24543 case CPP_AND: return get_identifier ("bitand");
24544 case CPP_OR: return get_identifier ("bitor");
24545 case CPP_COMPL: return get_identifier ("compl");
24546 case CPP_NOT: return get_identifier ("not");
24547 case CPP_NOT_EQ: return get_identifier ("not_eq");
24548 case CPP_OR_OR: return get_identifier ("or");
24549 case CPP_OR_EQ: return get_identifier ("or_eq");
24550 case CPP_XOR: return get_identifier ("xor");
24551 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24552 default: return token->u.value;
24556 /* Parse an Objective-C params list. */
24558 static tree
24559 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24561 tree params = NULL_TREE;
24562 bool maybe_unary_selector_p = true;
24563 cp_token *token = cp_lexer_peek_token (parser->lexer);
24565 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24567 tree selector = NULL_TREE, type_name, identifier;
24568 tree parm_attr = NULL_TREE;
24570 if (token->keyword == RID_ATTRIBUTE)
24571 break;
24573 if (token->type != CPP_COLON)
24574 selector = cp_parser_objc_selector (parser);
24576 /* Detect if we have a unary selector. */
24577 if (maybe_unary_selector_p
24578 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24580 params = selector; /* Might be followed by attributes. */
24581 break;
24584 maybe_unary_selector_p = false;
24585 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24587 /* Something went quite wrong. There should be a colon
24588 here, but there is not. Stop parsing parameters. */
24589 break;
24591 type_name = cp_parser_objc_typename (parser);
24592 /* New ObjC allows attributes on parameters too. */
24593 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24594 parm_attr = cp_parser_attributes_opt (parser);
24595 identifier = cp_parser_identifier (parser);
24597 params
24598 = chainon (params,
24599 objc_build_keyword_decl (selector,
24600 type_name,
24601 identifier,
24602 parm_attr));
24604 token = cp_lexer_peek_token (parser->lexer);
24607 if (params == NULL_TREE)
24609 cp_parser_error (parser, "objective-c++ method declaration is expected");
24610 return error_mark_node;
24613 /* We allow tail attributes for the method. */
24614 if (token->keyword == RID_ATTRIBUTE)
24616 *attributes = cp_parser_attributes_opt (parser);
24617 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24618 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24619 return params;
24620 cp_parser_error (parser,
24621 "method attributes must be specified at the end");
24622 return error_mark_node;
24625 if (params == NULL_TREE)
24627 cp_parser_error (parser, "objective-c++ method declaration is expected");
24628 return error_mark_node;
24630 return params;
24633 /* Parse the non-keyword Objective-C params. */
24635 static tree
24636 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24637 tree* attributes)
24639 tree params = make_node (TREE_LIST);
24640 cp_token *token = cp_lexer_peek_token (parser->lexer);
24641 *ellipsisp = false; /* Initially, assume no ellipsis. */
24643 while (token->type == CPP_COMMA)
24645 cp_parameter_declarator *parmdecl;
24646 tree parm;
24648 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24649 token = cp_lexer_peek_token (parser->lexer);
24651 if (token->type == CPP_ELLIPSIS)
24653 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24654 *ellipsisp = true;
24655 token = cp_lexer_peek_token (parser->lexer);
24656 break;
24659 /* TODO: parse attributes for tail parameters. */
24660 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24661 parm = grokdeclarator (parmdecl->declarator,
24662 &parmdecl->decl_specifiers,
24663 PARM, /*initialized=*/0,
24664 /*attrlist=*/NULL);
24666 chainon (params, build_tree_list (NULL_TREE, parm));
24667 token = cp_lexer_peek_token (parser->lexer);
24670 /* We allow tail attributes for the method. */
24671 if (token->keyword == RID_ATTRIBUTE)
24673 if (*attributes == NULL_TREE)
24675 *attributes = cp_parser_attributes_opt (parser);
24676 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24677 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24678 return params;
24680 else
24681 /* We have an error, but parse the attributes, so that we can
24682 carry on. */
24683 *attributes = cp_parser_attributes_opt (parser);
24685 cp_parser_error (parser,
24686 "method attributes must be specified at the end");
24687 return error_mark_node;
24690 return params;
24693 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24695 static void
24696 cp_parser_objc_interstitial_code (cp_parser* parser)
24698 cp_token *token = cp_lexer_peek_token (parser->lexer);
24700 /* If the next token is `extern' and the following token is a string
24701 literal, then we have a linkage specification. */
24702 if (token->keyword == RID_EXTERN
24703 && cp_parser_is_pure_string_literal
24704 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24705 cp_parser_linkage_specification (parser);
24706 /* Handle #pragma, if any. */
24707 else if (token->type == CPP_PRAGMA)
24708 cp_parser_pragma (parser, pragma_external);
24709 /* Allow stray semicolons. */
24710 else if (token->type == CPP_SEMICOLON)
24711 cp_lexer_consume_token (parser->lexer);
24712 /* Mark methods as optional or required, when building protocols. */
24713 else if (token->keyword == RID_AT_OPTIONAL)
24715 cp_lexer_consume_token (parser->lexer);
24716 objc_set_method_opt (true);
24718 else if (token->keyword == RID_AT_REQUIRED)
24720 cp_lexer_consume_token (parser->lexer);
24721 objc_set_method_opt (false);
24723 else if (token->keyword == RID_NAMESPACE)
24724 cp_parser_namespace_definition (parser);
24725 /* Other stray characters must generate errors. */
24726 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24728 cp_lexer_consume_token (parser->lexer);
24729 error ("stray %qs between Objective-C++ methods",
24730 token->type == CPP_OPEN_BRACE ? "{" : "}");
24732 /* Finally, try to parse a block-declaration, or a function-definition. */
24733 else
24734 cp_parser_block_declaration (parser, /*statement_p=*/false);
24737 /* Parse a method signature. */
24739 static tree
24740 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24742 tree rettype, kwdparms, optparms;
24743 bool ellipsis = false;
24744 bool is_class_method;
24746 is_class_method = cp_parser_objc_method_type (parser);
24747 rettype = cp_parser_objc_typename (parser);
24748 *attributes = NULL_TREE;
24749 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24750 if (kwdparms == error_mark_node)
24751 return error_mark_node;
24752 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24753 if (optparms == error_mark_node)
24754 return error_mark_node;
24756 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24759 static bool
24760 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24762 tree tattr;
24763 cp_lexer_save_tokens (parser->lexer);
24764 tattr = cp_parser_attributes_opt (parser);
24765 gcc_assert (tattr) ;
24767 /* If the attributes are followed by a method introducer, this is not allowed.
24768 Dump the attributes and flag the situation. */
24769 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24770 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24771 return true;
24773 /* Otherwise, the attributes introduce some interstitial code, possibly so
24774 rewind to allow that check. */
24775 cp_lexer_rollback_tokens (parser->lexer);
24776 return false;
24779 /* Parse an Objective-C method prototype list. */
24781 static void
24782 cp_parser_objc_method_prototype_list (cp_parser* parser)
24784 cp_token *token = cp_lexer_peek_token (parser->lexer);
24786 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24788 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24790 tree attributes, sig;
24791 bool is_class_method;
24792 if (token->type == CPP_PLUS)
24793 is_class_method = true;
24794 else
24795 is_class_method = false;
24796 sig = cp_parser_objc_method_signature (parser, &attributes);
24797 if (sig == error_mark_node)
24799 cp_parser_skip_to_end_of_block_or_statement (parser);
24800 token = cp_lexer_peek_token (parser->lexer);
24801 continue;
24803 objc_add_method_declaration (is_class_method, sig, attributes);
24804 cp_parser_consume_semicolon_at_end_of_statement (parser);
24806 else if (token->keyword == RID_AT_PROPERTY)
24807 cp_parser_objc_at_property_declaration (parser);
24808 else if (token->keyword == RID_ATTRIBUTE
24809 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24810 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24811 OPT_Wattributes,
24812 "prefix attributes are ignored for methods");
24813 else
24814 /* Allow for interspersed non-ObjC++ code. */
24815 cp_parser_objc_interstitial_code (parser);
24817 token = cp_lexer_peek_token (parser->lexer);
24820 if (token->type != CPP_EOF)
24821 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24822 else
24823 cp_parser_error (parser, "expected %<@end%>");
24825 objc_finish_interface ();
24828 /* Parse an Objective-C method definition list. */
24830 static void
24831 cp_parser_objc_method_definition_list (cp_parser* parser)
24833 cp_token *token = cp_lexer_peek_token (parser->lexer);
24835 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24837 tree meth;
24839 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24841 cp_token *ptk;
24842 tree sig, attribute;
24843 bool is_class_method;
24844 if (token->type == CPP_PLUS)
24845 is_class_method = true;
24846 else
24847 is_class_method = false;
24848 push_deferring_access_checks (dk_deferred);
24849 sig = cp_parser_objc_method_signature (parser, &attribute);
24850 if (sig == error_mark_node)
24852 cp_parser_skip_to_end_of_block_or_statement (parser);
24853 token = cp_lexer_peek_token (parser->lexer);
24854 continue;
24856 objc_start_method_definition (is_class_method, sig, attribute,
24857 NULL_TREE);
24859 /* For historical reasons, we accept an optional semicolon. */
24860 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24861 cp_lexer_consume_token (parser->lexer);
24863 ptk = cp_lexer_peek_token (parser->lexer);
24864 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24865 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24867 perform_deferred_access_checks (tf_warning_or_error);
24868 stop_deferring_access_checks ();
24869 meth = cp_parser_function_definition_after_declarator (parser,
24870 false);
24871 pop_deferring_access_checks ();
24872 objc_finish_method_definition (meth);
24875 /* The following case will be removed once @synthesize is
24876 completely implemented. */
24877 else if (token->keyword == RID_AT_PROPERTY)
24878 cp_parser_objc_at_property_declaration (parser);
24879 else if (token->keyword == RID_AT_SYNTHESIZE)
24880 cp_parser_objc_at_synthesize_declaration (parser);
24881 else if (token->keyword == RID_AT_DYNAMIC)
24882 cp_parser_objc_at_dynamic_declaration (parser);
24883 else if (token->keyword == RID_ATTRIBUTE
24884 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24885 warning_at (token->location, OPT_Wattributes,
24886 "prefix attributes are ignored for methods");
24887 else
24888 /* Allow for interspersed non-ObjC++ code. */
24889 cp_parser_objc_interstitial_code (parser);
24891 token = cp_lexer_peek_token (parser->lexer);
24894 if (token->type != CPP_EOF)
24895 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24896 else
24897 cp_parser_error (parser, "expected %<@end%>");
24899 objc_finish_implementation ();
24902 /* Parse Objective-C ivars. */
24904 static void
24905 cp_parser_objc_class_ivars (cp_parser* parser)
24907 cp_token *token = cp_lexer_peek_token (parser->lexer);
24909 if (token->type != CPP_OPEN_BRACE)
24910 return; /* No ivars specified. */
24912 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24913 token = cp_lexer_peek_token (parser->lexer);
24915 while (token->type != CPP_CLOSE_BRACE
24916 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24918 cp_decl_specifier_seq declspecs;
24919 int decl_class_or_enum_p;
24920 tree prefix_attributes;
24922 cp_parser_objc_visibility_spec (parser);
24924 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24925 break;
24927 cp_parser_decl_specifier_seq (parser,
24928 CP_PARSER_FLAGS_OPTIONAL,
24929 &declspecs,
24930 &decl_class_or_enum_p);
24932 /* auto, register, static, extern, mutable. */
24933 if (declspecs.storage_class != sc_none)
24935 cp_parser_error (parser, "invalid type for instance variable");
24936 declspecs.storage_class = sc_none;
24939 /* thread_local. */
24940 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24942 cp_parser_error (parser, "invalid type for instance variable");
24943 declspecs.locations[ds_thread] = 0;
24946 /* typedef. */
24947 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24949 cp_parser_error (parser, "invalid type for instance variable");
24950 declspecs.locations[ds_typedef] = 0;
24953 prefix_attributes = declspecs.attributes;
24954 declspecs.attributes = NULL_TREE;
24956 /* Keep going until we hit the `;' at the end of the
24957 declaration. */
24958 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24960 tree width = NULL_TREE, attributes, first_attribute, decl;
24961 cp_declarator *declarator = NULL;
24962 int ctor_dtor_or_conv_p;
24964 /* Check for a (possibly unnamed) bitfield declaration. */
24965 token = cp_lexer_peek_token (parser->lexer);
24966 if (token->type == CPP_COLON)
24967 goto eat_colon;
24969 if (token->type == CPP_NAME
24970 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24971 == CPP_COLON))
24973 /* Get the name of the bitfield. */
24974 declarator = make_id_declarator (NULL_TREE,
24975 cp_parser_identifier (parser),
24976 sfk_none);
24978 eat_colon:
24979 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24980 /* Get the width of the bitfield. */
24981 width
24982 = cp_parser_constant_expression (parser,
24983 /*allow_non_constant=*/false,
24984 NULL);
24986 else
24988 /* Parse the declarator. */
24989 declarator
24990 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24991 &ctor_dtor_or_conv_p,
24992 /*parenthesized_p=*/NULL,
24993 /*member_p=*/false);
24996 /* Look for attributes that apply to the ivar. */
24997 attributes = cp_parser_attributes_opt (parser);
24998 /* Remember which attributes are prefix attributes and
24999 which are not. */
25000 first_attribute = attributes;
25001 /* Combine the attributes. */
25002 attributes = chainon (prefix_attributes, attributes);
25004 if (width)
25005 /* Create the bitfield declaration. */
25006 decl = grokbitfield (declarator, &declspecs,
25007 width,
25008 attributes);
25009 else
25010 decl = grokfield (declarator, &declspecs,
25011 NULL_TREE, /*init_const_expr_p=*/false,
25012 NULL_TREE, attributes);
25014 /* Add the instance variable. */
25015 if (decl != error_mark_node && decl != NULL_TREE)
25016 objc_add_instance_variable (decl);
25018 /* Reset PREFIX_ATTRIBUTES. */
25019 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25020 attributes = TREE_CHAIN (attributes);
25021 if (attributes)
25022 TREE_CHAIN (attributes) = NULL_TREE;
25024 token = cp_lexer_peek_token (parser->lexer);
25026 if (token->type == CPP_COMMA)
25028 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25029 continue;
25031 break;
25034 cp_parser_consume_semicolon_at_end_of_statement (parser);
25035 token = cp_lexer_peek_token (parser->lexer);
25038 if (token->keyword == RID_AT_END)
25039 cp_parser_error (parser, "expected %<}%>");
25041 /* Do not consume the RID_AT_END, so it will be read again as terminating
25042 the @interface of @implementation. */
25043 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
25044 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
25046 /* For historical reasons, we accept an optional semicolon. */
25047 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25048 cp_lexer_consume_token (parser->lexer);
25051 /* Parse an Objective-C protocol declaration. */
25053 static void
25054 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
25056 tree proto, protorefs;
25057 cp_token *tok;
25059 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
25062 tok = cp_lexer_peek_token (parser->lexer);
25063 error_at (tok->location, "identifier expected after %<@protocol%>");
25064 cp_parser_consume_semicolon_at_end_of_statement (parser);
25065 return;
25068 /* See if we have a forward declaration or a definition. */
25069 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
25071 /* Try a forward declaration first. */
25072 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
25074 while (true)
25076 tree id;
25078 id = cp_parser_identifier (parser);
25079 if (id == error_mark_node)
25080 break;
25082 objc_declare_protocol (id, attributes);
25084 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25085 cp_lexer_consume_token (parser->lexer);
25086 else
25087 break;
25089 cp_parser_consume_semicolon_at_end_of_statement (parser);
25092 /* Ok, we got a full-fledged definition (or at least should). */
25093 else
25095 proto = cp_parser_identifier (parser);
25096 protorefs = cp_parser_objc_protocol_refs_opt (parser);
25097 objc_start_protocol (proto, protorefs, attributes);
25098 cp_parser_objc_method_prototype_list (parser);
25102 /* Parse an Objective-C superclass or category. */
25104 static void
25105 cp_parser_objc_superclass_or_category (cp_parser *parser,
25106 bool iface_p,
25107 tree *super,
25108 tree *categ, bool *is_class_extension)
25110 cp_token *next = cp_lexer_peek_token (parser->lexer);
25112 *super = *categ = NULL_TREE;
25113 *is_class_extension = false;
25114 if (next->type == CPP_COLON)
25116 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25117 *super = cp_parser_identifier (parser);
25119 else if (next->type == CPP_OPEN_PAREN)
25121 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25123 /* If there is no category name, and this is an @interface, we
25124 have a class extension. */
25125 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25127 *categ = NULL_TREE;
25128 *is_class_extension = true;
25130 else
25131 *categ = cp_parser_identifier (parser);
25133 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25137 /* Parse an Objective-C class interface. */
25139 static void
25140 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25142 tree name, super, categ, protos;
25143 bool is_class_extension;
25145 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25146 name = cp_parser_identifier (parser);
25147 if (name == error_mark_node)
25149 /* It's hard to recover because even if valid @interface stuff
25150 is to follow, we can't compile it (or validate it) if we
25151 don't even know which class it refers to. Let's assume this
25152 was a stray '@interface' token in the stream and skip it.
25154 return;
25156 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25157 &is_class_extension);
25158 protos = cp_parser_objc_protocol_refs_opt (parser);
25160 /* We have either a class or a category on our hands. */
25161 if (categ || is_class_extension)
25162 objc_start_category_interface (name, categ, protos, attributes);
25163 else
25165 objc_start_class_interface (name, super, protos, attributes);
25166 /* Handle instance variable declarations, if any. */
25167 cp_parser_objc_class_ivars (parser);
25168 objc_continue_interface ();
25171 cp_parser_objc_method_prototype_list (parser);
25174 /* Parse an Objective-C class implementation. */
25176 static void
25177 cp_parser_objc_class_implementation (cp_parser* parser)
25179 tree name, super, categ;
25180 bool is_class_extension;
25182 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25183 name = cp_parser_identifier (parser);
25184 if (name == error_mark_node)
25186 /* It's hard to recover because even if valid @implementation
25187 stuff is to follow, we can't compile it (or validate it) if
25188 we don't even know which class it refers to. Let's assume
25189 this was a stray '@implementation' token in the stream and
25190 skip it.
25192 return;
25194 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25195 &is_class_extension);
25197 /* We have either a class or a category on our hands. */
25198 if (categ)
25199 objc_start_category_implementation (name, categ);
25200 else
25202 objc_start_class_implementation (name, super);
25203 /* Handle instance variable declarations, if any. */
25204 cp_parser_objc_class_ivars (parser);
25205 objc_continue_implementation ();
25208 cp_parser_objc_method_definition_list (parser);
25211 /* Consume the @end token and finish off the implementation. */
25213 static void
25214 cp_parser_objc_end_implementation (cp_parser* parser)
25216 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25217 objc_finish_implementation ();
25220 /* Parse an Objective-C declaration. */
25222 static void
25223 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25225 /* Try to figure out what kind of declaration is present. */
25226 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25228 if (attributes)
25229 switch (kwd->keyword)
25231 case RID_AT_ALIAS:
25232 case RID_AT_CLASS:
25233 case RID_AT_END:
25234 error_at (kwd->location, "attributes may not be specified before"
25235 " the %<@%D%> Objective-C++ keyword",
25236 kwd->u.value);
25237 attributes = NULL;
25238 break;
25239 case RID_AT_IMPLEMENTATION:
25240 warning_at (kwd->location, OPT_Wattributes,
25241 "prefix attributes are ignored before %<@%D%>",
25242 kwd->u.value);
25243 attributes = NULL;
25244 default:
25245 break;
25248 switch (kwd->keyword)
25250 case RID_AT_ALIAS:
25251 cp_parser_objc_alias_declaration (parser);
25252 break;
25253 case RID_AT_CLASS:
25254 cp_parser_objc_class_declaration (parser);
25255 break;
25256 case RID_AT_PROTOCOL:
25257 cp_parser_objc_protocol_declaration (parser, attributes);
25258 break;
25259 case RID_AT_INTERFACE:
25260 cp_parser_objc_class_interface (parser, attributes);
25261 break;
25262 case RID_AT_IMPLEMENTATION:
25263 cp_parser_objc_class_implementation (parser);
25264 break;
25265 case RID_AT_END:
25266 cp_parser_objc_end_implementation (parser);
25267 break;
25268 default:
25269 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25270 kwd->u.value);
25271 cp_parser_skip_to_end_of_block_or_statement (parser);
25275 /* Parse an Objective-C try-catch-finally statement.
25277 objc-try-catch-finally-stmt:
25278 @try compound-statement objc-catch-clause-seq [opt]
25279 objc-finally-clause [opt]
25281 objc-catch-clause-seq:
25282 objc-catch-clause objc-catch-clause-seq [opt]
25284 objc-catch-clause:
25285 @catch ( objc-exception-declaration ) compound-statement
25287 objc-finally-clause:
25288 @finally compound-statement
25290 objc-exception-declaration:
25291 parameter-declaration
25292 '...'
25294 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25296 Returns NULL_TREE.
25298 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25299 for C. Keep them in sync. */
25301 static tree
25302 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25304 location_t location;
25305 tree stmt;
25307 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25308 location = cp_lexer_peek_token (parser->lexer)->location;
25309 objc_maybe_warn_exceptions (location);
25310 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25311 node, lest it get absorbed into the surrounding block. */
25312 stmt = push_stmt_list ();
25313 cp_parser_compound_statement (parser, NULL, false, false);
25314 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25316 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25318 cp_parameter_declarator *parm;
25319 tree parameter_declaration = error_mark_node;
25320 bool seen_open_paren = false;
25322 cp_lexer_consume_token (parser->lexer);
25323 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25324 seen_open_paren = true;
25325 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25327 /* We have "@catch (...)" (where the '...' are literally
25328 what is in the code). Skip the '...'.
25329 parameter_declaration is set to NULL_TREE, and
25330 objc_being_catch_clauses() knows that that means
25331 '...'. */
25332 cp_lexer_consume_token (parser->lexer);
25333 parameter_declaration = NULL_TREE;
25335 else
25337 /* We have "@catch (NSException *exception)" or something
25338 like that. Parse the parameter declaration. */
25339 parm = cp_parser_parameter_declaration (parser, false, NULL);
25340 if (parm == NULL)
25341 parameter_declaration = error_mark_node;
25342 else
25343 parameter_declaration = grokdeclarator (parm->declarator,
25344 &parm->decl_specifiers,
25345 PARM, /*initialized=*/0,
25346 /*attrlist=*/NULL);
25348 if (seen_open_paren)
25349 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25350 else
25352 /* If there was no open parenthesis, we are recovering from
25353 an error, and we are trying to figure out what mistake
25354 the user has made. */
25356 /* If there is an immediate closing parenthesis, the user
25357 probably forgot the opening one (ie, they typed "@catch
25358 NSException *e)". Parse the closing parenthesis and keep
25359 going. */
25360 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25361 cp_lexer_consume_token (parser->lexer);
25363 /* If these is no immediate closing parenthesis, the user
25364 probably doesn't know that parenthesis are required at
25365 all (ie, they typed "@catch NSException *e"). So, just
25366 forget about the closing parenthesis and keep going. */
25368 objc_begin_catch_clause (parameter_declaration);
25369 cp_parser_compound_statement (parser, NULL, false, false);
25370 objc_finish_catch_clause ();
25372 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25374 cp_lexer_consume_token (parser->lexer);
25375 location = cp_lexer_peek_token (parser->lexer)->location;
25376 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25377 node, lest it get absorbed into the surrounding block. */
25378 stmt = push_stmt_list ();
25379 cp_parser_compound_statement (parser, NULL, false, false);
25380 objc_build_finally_clause (location, pop_stmt_list (stmt));
25383 return objc_finish_try_stmt ();
25386 /* Parse an Objective-C synchronized statement.
25388 objc-synchronized-stmt:
25389 @synchronized ( expression ) compound-statement
25391 Returns NULL_TREE. */
25393 static tree
25394 cp_parser_objc_synchronized_statement (cp_parser *parser)
25396 location_t location;
25397 tree lock, stmt;
25399 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25401 location = cp_lexer_peek_token (parser->lexer)->location;
25402 objc_maybe_warn_exceptions (location);
25403 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25404 lock = cp_parser_expression (parser, false, NULL);
25405 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25407 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25408 node, lest it get absorbed into the surrounding block. */
25409 stmt = push_stmt_list ();
25410 cp_parser_compound_statement (parser, NULL, false, false);
25412 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25415 /* Parse an Objective-C throw statement.
25417 objc-throw-stmt:
25418 @throw assignment-expression [opt] ;
25420 Returns a constructed '@throw' statement. */
25422 static tree
25423 cp_parser_objc_throw_statement (cp_parser *parser)
25425 tree expr = NULL_TREE;
25426 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25428 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25431 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25433 cp_parser_consume_semicolon_at_end_of_statement (parser);
25435 return objc_build_throw_stmt (loc, expr);
25438 /* Parse an Objective-C statement. */
25440 static tree
25441 cp_parser_objc_statement (cp_parser * parser)
25443 /* Try to figure out what kind of declaration is present. */
25444 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25446 switch (kwd->keyword)
25448 case RID_AT_TRY:
25449 return cp_parser_objc_try_catch_finally_statement (parser);
25450 case RID_AT_SYNCHRONIZED:
25451 return cp_parser_objc_synchronized_statement (parser);
25452 case RID_AT_THROW:
25453 return cp_parser_objc_throw_statement (parser);
25454 default:
25455 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25456 kwd->u.value);
25457 cp_parser_skip_to_end_of_block_or_statement (parser);
25460 return error_mark_node;
25463 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25464 look ahead to see if an objc keyword follows the attributes. This
25465 is to detect the use of prefix attributes on ObjC @interface and
25466 @protocol. */
25468 static bool
25469 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25471 cp_lexer_save_tokens (parser->lexer);
25472 *attrib = cp_parser_attributes_opt (parser);
25473 gcc_assert (*attrib);
25474 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25476 cp_lexer_commit_tokens (parser->lexer);
25477 return true;
25479 cp_lexer_rollback_tokens (parser->lexer);
25480 return false;
25483 /* This routine is a minimal replacement for
25484 c_parser_struct_declaration () used when parsing the list of
25485 types/names or ObjC++ properties. For example, when parsing the
25486 code
25488 @property (readonly) int a, b, c;
25490 this function is responsible for parsing "int a, int b, int c" and
25491 returning the declarations as CHAIN of DECLs.
25493 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25494 similar parsing. */
25495 static tree
25496 cp_parser_objc_struct_declaration (cp_parser *parser)
25498 tree decls = NULL_TREE;
25499 cp_decl_specifier_seq declspecs;
25500 int decl_class_or_enum_p;
25501 tree prefix_attributes;
25503 cp_parser_decl_specifier_seq (parser,
25504 CP_PARSER_FLAGS_NONE,
25505 &declspecs,
25506 &decl_class_or_enum_p);
25508 if (declspecs.type == error_mark_node)
25509 return error_mark_node;
25511 /* auto, register, static, extern, mutable. */
25512 if (declspecs.storage_class != sc_none)
25514 cp_parser_error (parser, "invalid type for property");
25515 declspecs.storage_class = sc_none;
25518 /* thread_local. */
25519 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25521 cp_parser_error (parser, "invalid type for property");
25522 declspecs.locations[ds_thread] = 0;
25525 /* typedef. */
25526 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25528 cp_parser_error (parser, "invalid type for property");
25529 declspecs.locations[ds_typedef] = 0;
25532 prefix_attributes = declspecs.attributes;
25533 declspecs.attributes = NULL_TREE;
25535 /* Keep going until we hit the `;' at the end of the declaration. */
25536 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25538 tree attributes, first_attribute, decl;
25539 cp_declarator *declarator;
25540 cp_token *token;
25542 /* Parse the declarator. */
25543 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25544 NULL, NULL, false);
25546 /* Look for attributes that apply to the ivar. */
25547 attributes = cp_parser_attributes_opt (parser);
25548 /* Remember which attributes are prefix attributes and
25549 which are not. */
25550 first_attribute = attributes;
25551 /* Combine the attributes. */
25552 attributes = chainon (prefix_attributes, attributes);
25554 decl = grokfield (declarator, &declspecs,
25555 NULL_TREE, /*init_const_expr_p=*/false,
25556 NULL_TREE, attributes);
25558 if (decl == error_mark_node || decl == NULL_TREE)
25559 return error_mark_node;
25561 /* Reset PREFIX_ATTRIBUTES. */
25562 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25563 attributes = TREE_CHAIN (attributes);
25564 if (attributes)
25565 TREE_CHAIN (attributes) = NULL_TREE;
25567 DECL_CHAIN (decl) = decls;
25568 decls = decl;
25570 token = cp_lexer_peek_token (parser->lexer);
25571 if (token->type == CPP_COMMA)
25573 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25574 continue;
25576 else
25577 break;
25579 return decls;
25582 /* Parse an Objective-C @property declaration. The syntax is:
25584 objc-property-declaration:
25585 '@property' objc-property-attributes[opt] struct-declaration ;
25587 objc-property-attributes:
25588 '(' objc-property-attribute-list ')'
25590 objc-property-attribute-list:
25591 objc-property-attribute
25592 objc-property-attribute-list, objc-property-attribute
25594 objc-property-attribute
25595 'getter' = identifier
25596 'setter' = identifier
25597 'readonly'
25598 'readwrite'
25599 'assign'
25600 'retain'
25601 'copy'
25602 'nonatomic'
25604 For example:
25605 @property NSString *name;
25606 @property (readonly) id object;
25607 @property (retain, nonatomic, getter=getTheName) id name;
25608 @property int a, b, c;
25610 PS: This function is identical to
25611 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25612 static void
25613 cp_parser_objc_at_property_declaration (cp_parser *parser)
25615 /* The following variables hold the attributes of the properties as
25616 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25617 seen. When we see an attribute, we set them to 'true' (if they
25618 are boolean properties) or to the identifier (if they have an
25619 argument, ie, for getter and setter). Note that here we only
25620 parse the list of attributes, check the syntax and accumulate the
25621 attributes that we find. objc_add_property_declaration() will
25622 then process the information. */
25623 bool property_assign = false;
25624 bool property_copy = false;
25625 tree property_getter_ident = NULL_TREE;
25626 bool property_nonatomic = false;
25627 bool property_readonly = false;
25628 bool property_readwrite = false;
25629 bool property_retain = false;
25630 tree property_setter_ident = NULL_TREE;
25632 /* 'properties' is the list of properties that we read. Usually a
25633 single one, but maybe more (eg, in "@property int a, b, c;" there
25634 are three). */
25635 tree properties;
25636 location_t loc;
25638 loc = cp_lexer_peek_token (parser->lexer)->location;
25640 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25642 /* Parse the optional attribute list... */
25643 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25645 /* Eat the '('. */
25646 cp_lexer_consume_token (parser->lexer);
25648 while (true)
25650 bool syntax_error = false;
25651 cp_token *token = cp_lexer_peek_token (parser->lexer);
25652 enum rid keyword;
25654 if (token->type != CPP_NAME)
25656 cp_parser_error (parser, "expected identifier");
25657 break;
25659 keyword = C_RID_CODE (token->u.value);
25660 cp_lexer_consume_token (parser->lexer);
25661 switch (keyword)
25663 case RID_ASSIGN: property_assign = true; break;
25664 case RID_COPY: property_copy = true; break;
25665 case RID_NONATOMIC: property_nonatomic = true; break;
25666 case RID_READONLY: property_readonly = true; break;
25667 case RID_READWRITE: property_readwrite = true; break;
25668 case RID_RETAIN: property_retain = true; break;
25670 case RID_GETTER:
25671 case RID_SETTER:
25672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25674 if (keyword == RID_GETTER)
25675 cp_parser_error (parser,
25676 "missing %<=%> (after %<getter%> attribute)");
25677 else
25678 cp_parser_error (parser,
25679 "missing %<=%> (after %<setter%> attribute)");
25680 syntax_error = true;
25681 break;
25683 cp_lexer_consume_token (parser->lexer); /* eat the = */
25684 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25686 cp_parser_error (parser, "expected identifier");
25687 syntax_error = true;
25688 break;
25690 if (keyword == RID_SETTER)
25692 if (property_setter_ident != NULL_TREE)
25694 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25695 cp_lexer_consume_token (parser->lexer);
25697 else
25698 property_setter_ident = cp_parser_objc_selector (parser);
25699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25700 cp_parser_error (parser, "setter name must terminate with %<:%>");
25701 else
25702 cp_lexer_consume_token (parser->lexer);
25704 else
25706 if (property_getter_ident != NULL_TREE)
25708 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25709 cp_lexer_consume_token (parser->lexer);
25711 else
25712 property_getter_ident = cp_parser_objc_selector (parser);
25714 break;
25715 default:
25716 cp_parser_error (parser, "unknown property attribute");
25717 syntax_error = true;
25718 break;
25721 if (syntax_error)
25722 break;
25724 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25725 cp_lexer_consume_token (parser->lexer);
25726 else
25727 break;
25730 /* FIXME: "@property (setter, assign);" will generate a spurious
25731 "error: expected ‘)’ before ‘,’ token". This is because
25732 cp_parser_require, unlike the C counterpart, will produce an
25733 error even if we are in error recovery. */
25734 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25736 cp_parser_skip_to_closing_parenthesis (parser,
25737 /*recovering=*/true,
25738 /*or_comma=*/false,
25739 /*consume_paren=*/true);
25743 /* ... and the property declaration(s). */
25744 properties = cp_parser_objc_struct_declaration (parser);
25746 if (properties == error_mark_node)
25748 cp_parser_skip_to_end_of_statement (parser);
25749 /* If the next token is now a `;', consume it. */
25750 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25751 cp_lexer_consume_token (parser->lexer);
25752 return;
25755 if (properties == NULL_TREE)
25756 cp_parser_error (parser, "expected identifier");
25757 else
25759 /* Comma-separated properties are chained together in
25760 reverse order; add them one by one. */
25761 properties = nreverse (properties);
25763 for (; properties; properties = TREE_CHAIN (properties))
25764 objc_add_property_declaration (loc, copy_node (properties),
25765 property_readonly, property_readwrite,
25766 property_assign, property_retain,
25767 property_copy, property_nonatomic,
25768 property_getter_ident, property_setter_ident);
25771 cp_parser_consume_semicolon_at_end_of_statement (parser);
25774 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25776 objc-synthesize-declaration:
25777 @synthesize objc-synthesize-identifier-list ;
25779 objc-synthesize-identifier-list:
25780 objc-synthesize-identifier
25781 objc-synthesize-identifier-list, objc-synthesize-identifier
25783 objc-synthesize-identifier
25784 identifier
25785 identifier = identifier
25787 For example:
25788 @synthesize MyProperty;
25789 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25791 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25792 for C. Keep them in sync.
25794 static void
25795 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25797 tree list = NULL_TREE;
25798 location_t loc;
25799 loc = cp_lexer_peek_token (parser->lexer)->location;
25801 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25802 while (true)
25804 tree property, ivar;
25805 property = cp_parser_identifier (parser);
25806 if (property == error_mark_node)
25808 cp_parser_consume_semicolon_at_end_of_statement (parser);
25809 return;
25811 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25813 cp_lexer_consume_token (parser->lexer);
25814 ivar = cp_parser_identifier (parser);
25815 if (ivar == error_mark_node)
25817 cp_parser_consume_semicolon_at_end_of_statement (parser);
25818 return;
25821 else
25822 ivar = NULL_TREE;
25823 list = chainon (list, build_tree_list (ivar, property));
25824 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25825 cp_lexer_consume_token (parser->lexer);
25826 else
25827 break;
25829 cp_parser_consume_semicolon_at_end_of_statement (parser);
25830 objc_add_synthesize_declaration (loc, list);
25833 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25835 objc-dynamic-declaration:
25836 @dynamic identifier-list ;
25838 For example:
25839 @dynamic MyProperty;
25840 @dynamic MyProperty, AnotherProperty;
25842 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25843 for C. Keep them in sync.
25845 static void
25846 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25848 tree list = NULL_TREE;
25849 location_t loc;
25850 loc = cp_lexer_peek_token (parser->lexer)->location;
25852 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25853 while (true)
25855 tree property;
25856 property = cp_parser_identifier (parser);
25857 if (property == error_mark_node)
25859 cp_parser_consume_semicolon_at_end_of_statement (parser);
25860 return;
25862 list = chainon (list, build_tree_list (NULL, property));
25863 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25864 cp_lexer_consume_token (parser->lexer);
25865 else
25866 break;
25868 cp_parser_consume_semicolon_at_end_of_statement (parser);
25869 objc_add_dynamic_declaration (loc, list);
25873 /* OpenMP 2.5 parsing routines. */
25875 /* Returns name of the next clause.
25876 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25877 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25878 returned and the token is consumed. */
25880 static pragma_omp_clause
25881 cp_parser_omp_clause_name (cp_parser *parser)
25883 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25885 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25886 result = PRAGMA_OMP_CLAUSE_IF;
25887 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25888 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25889 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25890 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25891 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25893 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25894 const char *p = IDENTIFIER_POINTER (id);
25896 switch (p[0])
25898 case 'c':
25899 if (!strcmp ("collapse", p))
25900 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25901 else if (!strcmp ("copyin", p))
25902 result = PRAGMA_OMP_CLAUSE_COPYIN;
25903 else if (!strcmp ("copyprivate", p))
25904 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25905 break;
25906 case 'f':
25907 if (!strcmp ("final", p))
25908 result = PRAGMA_OMP_CLAUSE_FINAL;
25909 else if (!strcmp ("firstprivate", p))
25910 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25911 break;
25912 case 'l':
25913 if (!strcmp ("lastprivate", p))
25914 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25915 break;
25916 case 'm':
25917 if (!strcmp ("mergeable", p))
25918 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25919 break;
25920 case 'n':
25921 if (!strcmp ("nowait", p))
25922 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25923 else if (!strcmp ("num_threads", p))
25924 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25925 break;
25926 case 'o':
25927 if (!strcmp ("ordered", p))
25928 result = PRAGMA_OMP_CLAUSE_ORDERED;
25929 break;
25930 case 'r':
25931 if (!strcmp ("reduction", p))
25932 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25933 break;
25934 case 's':
25935 if (!strcmp ("schedule", p))
25936 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25937 else if (!strcmp ("shared", p))
25938 result = PRAGMA_OMP_CLAUSE_SHARED;
25939 break;
25940 case 'u':
25941 if (!strcmp ("untied", p))
25942 result = PRAGMA_OMP_CLAUSE_UNTIED;
25943 break;
25947 if (result != PRAGMA_OMP_CLAUSE_NONE)
25948 cp_lexer_consume_token (parser->lexer);
25950 return result;
25953 /* Validate that a clause of the given type does not already exist. */
25955 static void
25956 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25957 const char *name, location_t location)
25959 tree c;
25961 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25962 if (OMP_CLAUSE_CODE (c) == code)
25964 error_at (location, "too many %qs clauses", name);
25965 break;
25969 /* OpenMP 2.5:
25970 variable-list:
25971 identifier
25972 variable-list , identifier
25974 In addition, we match a closing parenthesis. An opening parenthesis
25975 will have been consumed by the caller.
25977 If KIND is nonzero, create the appropriate node and install the decl
25978 in OMP_CLAUSE_DECL and add the node to the head of the list.
25980 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25981 return the list created. */
25983 static tree
25984 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25985 tree list)
25987 cp_token *token;
25988 while (1)
25990 tree name, decl;
25992 token = cp_lexer_peek_token (parser->lexer);
25993 name = cp_parser_id_expression (parser, /*template_p=*/false,
25994 /*check_dependency_p=*/true,
25995 /*template_p=*/NULL,
25996 /*declarator_p=*/false,
25997 /*optional_p=*/false);
25998 if (name == error_mark_node)
25999 goto skip_comma;
26001 decl = cp_parser_lookup_name_simple (parser, name, token->location);
26002 if (decl == error_mark_node)
26003 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
26004 token->location);
26005 else if (kind != 0)
26007 tree u = build_omp_clause (token->location, kind);
26008 OMP_CLAUSE_DECL (u) = decl;
26009 OMP_CLAUSE_CHAIN (u) = list;
26010 list = u;
26012 else
26013 list = tree_cons (decl, NULL_TREE, list);
26015 get_comma:
26016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26017 break;
26018 cp_lexer_consume_token (parser->lexer);
26021 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26023 int ending;
26025 /* Try to resync to an unnested comma. Copied from
26026 cp_parser_parenthesized_expression_list. */
26027 skip_comma:
26028 ending = cp_parser_skip_to_closing_parenthesis (parser,
26029 /*recovering=*/true,
26030 /*or_comma=*/true,
26031 /*consume_paren=*/true);
26032 if (ending < 0)
26033 goto get_comma;
26036 return list;
26039 /* Similarly, but expect leading and trailing parenthesis. This is a very
26040 common case for omp clauses. */
26042 static tree
26043 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
26045 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26046 return cp_parser_omp_var_list_no_open (parser, kind, list);
26047 return list;
26050 /* OpenMP 3.0:
26051 collapse ( constant-expression ) */
26053 static tree
26054 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
26056 tree c, num;
26057 location_t loc;
26058 HOST_WIDE_INT n;
26060 loc = cp_lexer_peek_token (parser->lexer)->location;
26061 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26062 return list;
26064 num = cp_parser_constant_expression (parser, false, NULL);
26066 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26067 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26068 /*or_comma=*/false,
26069 /*consume_paren=*/true);
26071 if (num == error_mark_node)
26072 return list;
26073 num = fold_non_dependent_expr (num);
26074 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
26075 || !host_integerp (num, 0)
26076 || (n = tree_low_cst (num, 0)) <= 0
26077 || (int) n != n)
26079 error_at (loc, "collapse argument needs positive constant integer expression");
26080 return list;
26083 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
26084 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
26085 OMP_CLAUSE_CHAIN (c) = list;
26086 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
26088 return c;
26091 /* OpenMP 2.5:
26092 default ( shared | none ) */
26094 static tree
26095 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26097 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26098 tree c;
26100 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26101 return list;
26102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26104 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26105 const char *p = IDENTIFIER_POINTER (id);
26107 switch (p[0])
26109 case 'n':
26110 if (strcmp ("none", p) != 0)
26111 goto invalid_kind;
26112 kind = OMP_CLAUSE_DEFAULT_NONE;
26113 break;
26115 case 's':
26116 if (strcmp ("shared", p) != 0)
26117 goto invalid_kind;
26118 kind = OMP_CLAUSE_DEFAULT_SHARED;
26119 break;
26121 default:
26122 goto invalid_kind;
26125 cp_lexer_consume_token (parser->lexer);
26127 else
26129 invalid_kind:
26130 cp_parser_error (parser, "expected %<none%> or %<shared%>");
26133 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26134 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26135 /*or_comma=*/false,
26136 /*consume_paren=*/true);
26138 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26139 return list;
26141 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26142 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26143 OMP_CLAUSE_CHAIN (c) = list;
26144 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26146 return c;
26149 /* OpenMP 3.1:
26150 final ( expression ) */
26152 static tree
26153 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26155 tree t, c;
26157 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26158 return list;
26160 t = cp_parser_condition (parser);
26162 if (t == error_mark_node
26163 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26165 /*or_comma=*/false,
26166 /*consume_paren=*/true);
26168 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26170 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26171 OMP_CLAUSE_FINAL_EXPR (c) = t;
26172 OMP_CLAUSE_CHAIN (c) = list;
26174 return c;
26177 /* OpenMP 2.5:
26178 if ( expression ) */
26180 static tree
26181 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26183 tree t, c;
26185 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26186 return list;
26188 t = cp_parser_condition (parser);
26190 if (t == error_mark_node
26191 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26193 /*or_comma=*/false,
26194 /*consume_paren=*/true);
26196 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26198 c = build_omp_clause (location, OMP_CLAUSE_IF);
26199 OMP_CLAUSE_IF_EXPR (c) = t;
26200 OMP_CLAUSE_CHAIN (c) = list;
26202 return c;
26205 /* OpenMP 3.1:
26206 mergeable */
26208 static tree
26209 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26210 tree list, location_t location)
26212 tree c;
26214 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26215 location);
26217 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
26218 OMP_CLAUSE_CHAIN (c) = list;
26219 return c;
26222 /* OpenMP 2.5:
26223 nowait */
26225 static tree
26226 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
26227 tree list, location_t location)
26229 tree c;
26231 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
26233 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
26234 OMP_CLAUSE_CHAIN (c) = list;
26235 return c;
26238 /* OpenMP 2.5:
26239 num_threads ( expression ) */
26241 static tree
26242 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
26243 location_t location)
26245 tree t, c;
26247 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26248 return list;
26250 t = cp_parser_expression (parser, false, NULL);
26252 if (t == error_mark_node
26253 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26254 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26255 /*or_comma=*/false,
26256 /*consume_paren=*/true);
26258 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
26259 "num_threads", location);
26261 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
26262 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
26263 OMP_CLAUSE_CHAIN (c) = list;
26265 return c;
26268 /* OpenMP 2.5:
26269 ordered */
26271 static tree
26272 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
26273 tree list, location_t location)
26275 tree c;
26277 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
26278 "ordered", location);
26280 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
26281 OMP_CLAUSE_CHAIN (c) = list;
26282 return c;
26285 /* OpenMP 2.5:
26286 reduction ( reduction-operator : variable-list )
26288 reduction-operator:
26289 One of: + * - & ^ | && ||
26291 OpenMP 3.1:
26293 reduction-operator:
26294 One of: + * - & ^ | && || min max */
26296 static tree
26297 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26299 enum tree_code code;
26300 tree nlist, c;
26302 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26303 return list;
26305 switch (cp_lexer_peek_token (parser->lexer)->type)
26307 case CPP_PLUS:
26308 code = PLUS_EXPR;
26309 break;
26310 case CPP_MULT:
26311 code = MULT_EXPR;
26312 break;
26313 case CPP_MINUS:
26314 code = MINUS_EXPR;
26315 break;
26316 case CPP_AND:
26317 code = BIT_AND_EXPR;
26318 break;
26319 case CPP_XOR:
26320 code = BIT_XOR_EXPR;
26321 break;
26322 case CPP_OR:
26323 code = BIT_IOR_EXPR;
26324 break;
26325 case CPP_AND_AND:
26326 code = TRUTH_ANDIF_EXPR;
26327 break;
26328 case CPP_OR_OR:
26329 code = TRUTH_ORIF_EXPR;
26330 break;
26331 case CPP_NAME:
26333 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26334 const char *p = IDENTIFIER_POINTER (id);
26336 if (strcmp (p, "min") == 0)
26338 code = MIN_EXPR;
26339 break;
26341 if (strcmp (p, "max") == 0)
26343 code = MAX_EXPR;
26344 break;
26347 /* FALLTHROUGH */
26348 default:
26349 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26350 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26351 resync_fail:
26352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26353 /*or_comma=*/false,
26354 /*consume_paren=*/true);
26355 return list;
26357 cp_lexer_consume_token (parser->lexer);
26359 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26360 goto resync_fail;
26362 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26363 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26364 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26366 return nlist;
26369 /* OpenMP 2.5:
26370 schedule ( schedule-kind )
26371 schedule ( schedule-kind , expression )
26373 schedule-kind:
26374 static | dynamic | guided | runtime | auto */
26376 static tree
26377 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26379 tree c, t;
26381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26382 return list;
26384 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26386 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26388 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26389 const char *p = IDENTIFIER_POINTER (id);
26391 switch (p[0])
26393 case 'd':
26394 if (strcmp ("dynamic", p) != 0)
26395 goto invalid_kind;
26396 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26397 break;
26399 case 'g':
26400 if (strcmp ("guided", p) != 0)
26401 goto invalid_kind;
26402 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26403 break;
26405 case 'r':
26406 if (strcmp ("runtime", p) != 0)
26407 goto invalid_kind;
26408 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26409 break;
26411 default:
26412 goto invalid_kind;
26415 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26416 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26417 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26418 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26419 else
26420 goto invalid_kind;
26421 cp_lexer_consume_token (parser->lexer);
26423 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26425 cp_token *token;
26426 cp_lexer_consume_token (parser->lexer);
26428 token = cp_lexer_peek_token (parser->lexer);
26429 t = cp_parser_assignment_expression (parser, false, NULL);
26431 if (t == error_mark_node)
26432 goto resync_fail;
26433 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26434 error_at (token->location, "schedule %<runtime%> does not take "
26435 "a %<chunk_size%> parameter");
26436 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26437 error_at (token->location, "schedule %<auto%> does not take "
26438 "a %<chunk_size%> parameter");
26439 else
26440 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26442 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26443 goto resync_fail;
26445 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26446 goto resync_fail;
26448 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26449 OMP_CLAUSE_CHAIN (c) = list;
26450 return c;
26452 invalid_kind:
26453 cp_parser_error (parser, "invalid schedule kind");
26454 resync_fail:
26455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26456 /*or_comma=*/false,
26457 /*consume_paren=*/true);
26458 return list;
26461 /* OpenMP 3.0:
26462 untied */
26464 static tree
26465 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26466 tree list, location_t location)
26468 tree c;
26470 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26472 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26473 OMP_CLAUSE_CHAIN (c) = list;
26474 return c;
26477 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26478 is a bitmask in MASK. Return the list of clauses found; the result
26479 of clause default goes in *pdefault. */
26481 static tree
26482 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26483 const char *where, cp_token *pragma_tok)
26485 tree clauses = NULL;
26486 bool first = true;
26487 cp_token *token = NULL;
26489 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26491 pragma_omp_clause c_kind;
26492 const char *c_name;
26493 tree prev = clauses;
26495 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26496 cp_lexer_consume_token (parser->lexer);
26498 token = cp_lexer_peek_token (parser->lexer);
26499 c_kind = cp_parser_omp_clause_name (parser);
26500 first = false;
26502 switch (c_kind)
26504 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26505 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26506 token->location);
26507 c_name = "collapse";
26508 break;
26509 case PRAGMA_OMP_CLAUSE_COPYIN:
26510 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26511 c_name = "copyin";
26512 break;
26513 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26514 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26515 clauses);
26516 c_name = "copyprivate";
26517 break;
26518 case PRAGMA_OMP_CLAUSE_DEFAULT:
26519 clauses = cp_parser_omp_clause_default (parser, clauses,
26520 token->location);
26521 c_name = "default";
26522 break;
26523 case PRAGMA_OMP_CLAUSE_FINAL:
26524 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26525 c_name = "final";
26526 break;
26527 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26528 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26529 clauses);
26530 c_name = "firstprivate";
26531 break;
26532 case PRAGMA_OMP_CLAUSE_IF:
26533 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26534 c_name = "if";
26535 break;
26536 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26537 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26538 clauses);
26539 c_name = "lastprivate";
26540 break;
26541 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26542 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26543 token->location);
26544 c_name = "mergeable";
26545 break;
26546 case PRAGMA_OMP_CLAUSE_NOWAIT:
26547 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26548 c_name = "nowait";
26549 break;
26550 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26551 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26552 token->location);
26553 c_name = "num_threads";
26554 break;
26555 case PRAGMA_OMP_CLAUSE_ORDERED:
26556 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26557 token->location);
26558 c_name = "ordered";
26559 break;
26560 case PRAGMA_OMP_CLAUSE_PRIVATE:
26561 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26562 clauses);
26563 c_name = "private";
26564 break;
26565 case PRAGMA_OMP_CLAUSE_REDUCTION:
26566 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26567 c_name = "reduction";
26568 break;
26569 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26570 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26571 token->location);
26572 c_name = "schedule";
26573 break;
26574 case PRAGMA_OMP_CLAUSE_SHARED:
26575 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26576 clauses);
26577 c_name = "shared";
26578 break;
26579 case PRAGMA_OMP_CLAUSE_UNTIED:
26580 clauses = cp_parser_omp_clause_untied (parser, clauses,
26581 token->location);
26582 c_name = "nowait";
26583 break;
26584 default:
26585 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26586 goto saw_error;
26589 if (((mask >> c_kind) & 1) == 0)
26591 /* Remove the invalid clause(s) from the list to avoid
26592 confusing the rest of the compiler. */
26593 clauses = prev;
26594 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26597 saw_error:
26598 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26599 return finish_omp_clauses (clauses);
26602 /* OpenMP 2.5:
26603 structured-block:
26604 statement
26606 In practice, we're also interested in adding the statement to an
26607 outer node. So it is convenient if we work around the fact that
26608 cp_parser_statement calls add_stmt. */
26610 static unsigned
26611 cp_parser_begin_omp_structured_block (cp_parser *parser)
26613 unsigned save = parser->in_statement;
26615 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26616 This preserves the "not within loop or switch" style error messages
26617 for nonsense cases like
26618 void foo() {
26619 #pragma omp single
26620 break;
26623 if (parser->in_statement)
26624 parser->in_statement = IN_OMP_BLOCK;
26626 return save;
26629 static void
26630 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26632 parser->in_statement = save;
26635 static tree
26636 cp_parser_omp_structured_block (cp_parser *parser)
26638 tree stmt = begin_omp_structured_block ();
26639 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26641 cp_parser_statement (parser, NULL_TREE, false, NULL);
26643 cp_parser_end_omp_structured_block (parser, save);
26644 return finish_omp_structured_block (stmt);
26647 /* OpenMP 2.5:
26648 # pragma omp atomic new-line
26649 expression-stmt
26651 expression-stmt:
26652 x binop= expr | x++ | ++x | x-- | --x
26653 binop:
26654 +, *, -, /, &, ^, |, <<, >>
26656 where x is an lvalue expression with scalar type.
26658 OpenMP 3.1:
26659 # pragma omp atomic new-line
26660 update-stmt
26662 # pragma omp atomic read new-line
26663 read-stmt
26665 # pragma omp atomic write new-line
26666 write-stmt
26668 # pragma omp atomic update new-line
26669 update-stmt
26671 # pragma omp atomic capture new-line
26672 capture-stmt
26674 # pragma omp atomic capture new-line
26675 capture-block
26677 read-stmt:
26678 v = x
26679 write-stmt:
26680 x = expr
26681 update-stmt:
26682 expression-stmt | x = x binop expr
26683 capture-stmt:
26684 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26685 capture-block:
26686 { v = x; update-stmt; } | { update-stmt; v = x; }
26688 where x and v are lvalue expressions with scalar type. */
26690 static void
26691 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26693 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26694 tree rhs1 = NULL_TREE, orig_lhs;
26695 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26696 bool structured_block = false;
26698 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26700 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26701 const char *p = IDENTIFIER_POINTER (id);
26703 if (!strcmp (p, "read"))
26704 code = OMP_ATOMIC_READ;
26705 else if (!strcmp (p, "write"))
26706 code = NOP_EXPR;
26707 else if (!strcmp (p, "update"))
26708 code = OMP_ATOMIC;
26709 else if (!strcmp (p, "capture"))
26710 code = OMP_ATOMIC_CAPTURE_NEW;
26711 else
26712 p = NULL;
26713 if (p)
26714 cp_lexer_consume_token (parser->lexer);
26716 cp_parser_require_pragma_eol (parser, pragma_tok);
26718 switch (code)
26720 case OMP_ATOMIC_READ:
26721 case NOP_EXPR: /* atomic write */
26722 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26723 /*cast_p=*/false, NULL);
26724 if (v == error_mark_node)
26725 goto saw_error;
26726 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26727 goto saw_error;
26728 if (code == NOP_EXPR)
26729 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26730 else
26731 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26732 /*cast_p=*/false, NULL);
26733 if (lhs == error_mark_node)
26734 goto saw_error;
26735 if (code == NOP_EXPR)
26737 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26738 opcode. */
26739 code = OMP_ATOMIC;
26740 rhs = lhs;
26741 lhs = v;
26742 v = NULL_TREE;
26744 goto done;
26745 case OMP_ATOMIC_CAPTURE_NEW:
26746 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26748 cp_lexer_consume_token (parser->lexer);
26749 structured_block = true;
26751 else
26753 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26754 /*cast_p=*/false, NULL);
26755 if (v == error_mark_node)
26756 goto saw_error;
26757 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26758 goto saw_error;
26760 default:
26761 break;
26764 restart:
26765 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26766 /*cast_p=*/false, NULL);
26767 orig_lhs = lhs;
26768 switch (TREE_CODE (lhs))
26770 case ERROR_MARK:
26771 goto saw_error;
26773 case POSTINCREMENT_EXPR:
26774 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26775 code = OMP_ATOMIC_CAPTURE_OLD;
26776 /* FALLTHROUGH */
26777 case PREINCREMENT_EXPR:
26778 lhs = TREE_OPERAND (lhs, 0);
26779 opcode = PLUS_EXPR;
26780 rhs = integer_one_node;
26781 break;
26783 case POSTDECREMENT_EXPR:
26784 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26785 code = OMP_ATOMIC_CAPTURE_OLD;
26786 /* FALLTHROUGH */
26787 case PREDECREMENT_EXPR:
26788 lhs = TREE_OPERAND (lhs, 0);
26789 opcode = MINUS_EXPR;
26790 rhs = integer_one_node;
26791 break;
26793 case COMPOUND_EXPR:
26794 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26795 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26796 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26797 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26798 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26799 (TREE_OPERAND (lhs, 1), 0), 0)))
26800 == BOOLEAN_TYPE)
26801 /* Undo effects of boolean_increment for post {in,de}crement. */
26802 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26803 /* FALLTHRU */
26804 case MODIFY_EXPR:
26805 if (TREE_CODE (lhs) == MODIFY_EXPR
26806 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26808 /* Undo effects of boolean_increment. */
26809 if (integer_onep (TREE_OPERAND (lhs, 1)))
26811 /* This is pre or post increment. */
26812 rhs = TREE_OPERAND (lhs, 1);
26813 lhs = TREE_OPERAND (lhs, 0);
26814 opcode = NOP_EXPR;
26815 if (code == OMP_ATOMIC_CAPTURE_NEW
26816 && !structured_block
26817 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26818 code = OMP_ATOMIC_CAPTURE_OLD;
26819 break;
26822 /* FALLTHRU */
26823 default:
26824 switch (cp_lexer_peek_token (parser->lexer)->type)
26826 case CPP_MULT_EQ:
26827 opcode = MULT_EXPR;
26828 break;
26829 case CPP_DIV_EQ:
26830 opcode = TRUNC_DIV_EXPR;
26831 break;
26832 case CPP_PLUS_EQ:
26833 opcode = PLUS_EXPR;
26834 break;
26835 case CPP_MINUS_EQ:
26836 opcode = MINUS_EXPR;
26837 break;
26838 case CPP_LSHIFT_EQ:
26839 opcode = LSHIFT_EXPR;
26840 break;
26841 case CPP_RSHIFT_EQ:
26842 opcode = RSHIFT_EXPR;
26843 break;
26844 case CPP_AND_EQ:
26845 opcode = BIT_AND_EXPR;
26846 break;
26847 case CPP_OR_EQ:
26848 opcode = BIT_IOR_EXPR;
26849 break;
26850 case CPP_XOR_EQ:
26851 opcode = BIT_XOR_EXPR;
26852 break;
26853 case CPP_EQ:
26854 if (structured_block || code == OMP_ATOMIC)
26856 enum cp_parser_prec oprec;
26857 cp_token *token;
26858 cp_lexer_consume_token (parser->lexer);
26859 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26860 /*cast_p=*/false, NULL);
26861 if (rhs1 == error_mark_node)
26862 goto saw_error;
26863 token = cp_lexer_peek_token (parser->lexer);
26864 switch (token->type)
26866 case CPP_SEMICOLON:
26867 if (code == OMP_ATOMIC_CAPTURE_NEW)
26869 code = OMP_ATOMIC_CAPTURE_OLD;
26870 v = lhs;
26871 lhs = NULL_TREE;
26872 lhs1 = rhs1;
26873 rhs1 = NULL_TREE;
26874 cp_lexer_consume_token (parser->lexer);
26875 goto restart;
26877 cp_parser_error (parser,
26878 "invalid form of %<#pragma omp atomic%>");
26879 goto saw_error;
26880 case CPP_MULT:
26881 opcode = MULT_EXPR;
26882 break;
26883 case CPP_DIV:
26884 opcode = TRUNC_DIV_EXPR;
26885 break;
26886 case CPP_PLUS:
26887 opcode = PLUS_EXPR;
26888 break;
26889 case CPP_MINUS:
26890 opcode = MINUS_EXPR;
26891 break;
26892 case CPP_LSHIFT:
26893 opcode = LSHIFT_EXPR;
26894 break;
26895 case CPP_RSHIFT:
26896 opcode = RSHIFT_EXPR;
26897 break;
26898 case CPP_AND:
26899 opcode = BIT_AND_EXPR;
26900 break;
26901 case CPP_OR:
26902 opcode = BIT_IOR_EXPR;
26903 break;
26904 case CPP_XOR:
26905 opcode = BIT_XOR_EXPR;
26906 break;
26907 default:
26908 cp_parser_error (parser,
26909 "invalid operator for %<#pragma omp atomic%>");
26910 goto saw_error;
26912 oprec = TOKEN_PRECEDENCE (token);
26913 gcc_assert (oprec != PREC_NOT_OPERATOR);
26914 if (commutative_tree_code (opcode))
26915 oprec = (enum cp_parser_prec) (oprec - 1);
26916 cp_lexer_consume_token (parser->lexer);
26917 rhs = cp_parser_binary_expression (parser, false, false,
26918 oprec, NULL);
26919 if (rhs == error_mark_node)
26920 goto saw_error;
26921 goto stmt_done;
26923 /* FALLTHROUGH */
26924 default:
26925 cp_parser_error (parser,
26926 "invalid operator for %<#pragma omp atomic%>");
26927 goto saw_error;
26929 cp_lexer_consume_token (parser->lexer);
26931 rhs = cp_parser_expression (parser, false, NULL);
26932 if (rhs == error_mark_node)
26933 goto saw_error;
26934 break;
26936 stmt_done:
26937 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26939 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26940 goto saw_error;
26941 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26942 /*cast_p=*/false, NULL);
26943 if (v == error_mark_node)
26944 goto saw_error;
26945 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26946 goto saw_error;
26947 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26948 /*cast_p=*/false, NULL);
26949 if (lhs1 == error_mark_node)
26950 goto saw_error;
26952 if (structured_block)
26954 cp_parser_consume_semicolon_at_end_of_statement (parser);
26955 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26957 done:
26958 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26959 if (!structured_block)
26960 cp_parser_consume_semicolon_at_end_of_statement (parser);
26961 return;
26963 saw_error:
26964 cp_parser_skip_to_end_of_block_or_statement (parser);
26965 if (structured_block)
26967 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26968 cp_lexer_consume_token (parser->lexer);
26969 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26971 cp_parser_skip_to_end_of_block_or_statement (parser);
26972 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26973 cp_lexer_consume_token (parser->lexer);
26979 /* OpenMP 2.5:
26980 # pragma omp barrier new-line */
26982 static void
26983 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26985 cp_parser_require_pragma_eol (parser, pragma_tok);
26986 finish_omp_barrier ();
26989 /* OpenMP 2.5:
26990 # pragma omp critical [(name)] new-line
26991 structured-block */
26993 static tree
26994 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26996 tree stmt, name = NULL;
26998 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27000 cp_lexer_consume_token (parser->lexer);
27002 name = cp_parser_identifier (parser);
27004 if (name == error_mark_node
27005 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27006 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27007 /*or_comma=*/false,
27008 /*consume_paren=*/true);
27009 if (name == error_mark_node)
27010 name = NULL;
27012 cp_parser_require_pragma_eol (parser, pragma_tok);
27014 stmt = cp_parser_omp_structured_block (parser);
27015 return c_finish_omp_critical (input_location, stmt, name);
27018 /* OpenMP 2.5:
27019 # pragma omp flush flush-vars[opt] new-line
27021 flush-vars:
27022 ( variable-list ) */
27024 static void
27025 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
27027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27028 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27029 cp_parser_require_pragma_eol (parser, pragma_tok);
27031 finish_omp_flush ();
27034 /* Helper function, to parse omp for increment expression. */
27036 static tree
27037 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
27039 tree cond = cp_parser_binary_expression (parser, false, true,
27040 PREC_NOT_OPERATOR, NULL);
27041 if (cond == error_mark_node
27042 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27044 cp_parser_skip_to_end_of_statement (parser);
27045 return error_mark_node;
27048 switch (TREE_CODE (cond))
27050 case GT_EXPR:
27051 case GE_EXPR:
27052 case LT_EXPR:
27053 case LE_EXPR:
27054 break;
27055 default:
27056 return error_mark_node;
27059 /* If decl is an iterator, preserve LHS and RHS of the relational
27060 expr until finish_omp_for. */
27061 if (decl
27062 && (type_dependent_expression_p (decl)
27063 || CLASS_TYPE_P (TREE_TYPE (decl))))
27064 return cond;
27066 return build_x_binary_op (input_location, TREE_CODE (cond),
27067 TREE_OPERAND (cond, 0), ERROR_MARK,
27068 TREE_OPERAND (cond, 1), ERROR_MARK,
27069 /*overload=*/NULL, tf_warning_or_error);
27072 /* Helper function, to parse omp for increment expression. */
27074 static tree
27075 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
27077 cp_token *token = cp_lexer_peek_token (parser->lexer);
27078 enum tree_code op;
27079 tree lhs, rhs;
27080 cp_id_kind idk;
27081 bool decl_first;
27083 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
27085 op = (token->type == CPP_PLUS_PLUS
27086 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
27087 cp_lexer_consume_token (parser->lexer);
27088 lhs = cp_parser_simple_cast_expression (parser);
27089 if (lhs != decl)
27090 return error_mark_node;
27091 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27094 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
27095 if (lhs != decl)
27096 return error_mark_node;
27098 token = cp_lexer_peek_token (parser->lexer);
27099 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
27101 op = (token->type == CPP_PLUS_PLUS
27102 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
27103 cp_lexer_consume_token (parser->lexer);
27104 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27107 op = cp_parser_assignment_operator_opt (parser);
27108 if (op == ERROR_MARK)
27109 return error_mark_node;
27111 if (op != NOP_EXPR)
27113 rhs = cp_parser_assignment_expression (parser, false, NULL);
27114 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
27115 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27118 lhs = cp_parser_binary_expression (parser, false, false,
27119 PREC_ADDITIVE_EXPRESSION, NULL);
27120 token = cp_lexer_peek_token (parser->lexer);
27121 decl_first = lhs == decl;
27122 if (decl_first)
27123 lhs = NULL_TREE;
27124 if (token->type != CPP_PLUS
27125 && token->type != CPP_MINUS)
27126 return error_mark_node;
27130 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
27131 cp_lexer_consume_token (parser->lexer);
27132 rhs = cp_parser_binary_expression (parser, false, false,
27133 PREC_ADDITIVE_EXPRESSION, NULL);
27134 token = cp_lexer_peek_token (parser->lexer);
27135 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
27137 if (lhs == NULL_TREE)
27139 if (op == PLUS_EXPR)
27140 lhs = rhs;
27141 else
27142 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
27143 tf_warning_or_error);
27145 else
27146 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
27147 ERROR_MARK, NULL, tf_warning_or_error);
27150 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
27152 if (!decl_first)
27154 if (rhs != decl || op == MINUS_EXPR)
27155 return error_mark_node;
27156 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
27158 else
27159 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
27161 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27164 /* Parse the restricted form of the for statement allowed by OpenMP. */
27166 static tree
27167 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
27169 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
27170 tree real_decl, initv, condv, incrv, declv;
27171 tree this_pre_body, cl;
27172 location_t loc_first;
27173 bool collapse_err = false;
27174 int i, collapse = 1, nbraces = 0;
27175 vec<tree, va_gc> *for_block = make_tree_vector ();
27177 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
27178 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
27179 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
27181 gcc_assert (collapse >= 1);
27183 declv = make_tree_vec (collapse);
27184 initv = make_tree_vec (collapse);
27185 condv = make_tree_vec (collapse);
27186 incrv = make_tree_vec (collapse);
27188 loc_first = cp_lexer_peek_token (parser->lexer)->location;
27190 for (i = 0; i < collapse; i++)
27192 int bracecount = 0;
27193 bool add_private_clause = false;
27194 location_t loc;
27196 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27198 cp_parser_error (parser, "for statement expected");
27199 return NULL;
27201 loc = cp_lexer_consume_token (parser->lexer)->location;
27203 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27204 return NULL;
27206 init = decl = real_decl = NULL;
27207 this_pre_body = push_stmt_list ();
27208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27210 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
27212 init-expr:
27213 var = lb
27214 integer-type var = lb
27215 random-access-iterator-type var = lb
27216 pointer-type var = lb
27218 cp_decl_specifier_seq type_specifiers;
27220 /* First, try to parse as an initialized declaration. See
27221 cp_parser_condition, from whence the bulk of this is copied. */
27223 cp_parser_parse_tentatively (parser);
27224 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
27225 /*is_trailing_return=*/false,
27226 &type_specifiers);
27227 if (cp_parser_parse_definitely (parser))
27229 /* If parsing a type specifier seq succeeded, then this
27230 MUST be a initialized declaration. */
27231 tree asm_specification, attributes;
27232 cp_declarator *declarator;
27234 declarator = cp_parser_declarator (parser,
27235 CP_PARSER_DECLARATOR_NAMED,
27236 /*ctor_dtor_or_conv_p=*/NULL,
27237 /*parenthesized_p=*/NULL,
27238 /*member_p=*/false);
27239 attributes = cp_parser_attributes_opt (parser);
27240 asm_specification = cp_parser_asm_specification_opt (parser);
27242 if (declarator == cp_error_declarator)
27243 cp_parser_skip_to_end_of_statement (parser);
27245 else
27247 tree pushed_scope, auto_node;
27249 decl = start_decl (declarator, &type_specifiers,
27250 SD_INITIALIZED, attributes,
27251 /*prefix_attributes=*/NULL_TREE,
27252 &pushed_scope);
27254 auto_node = type_uses_auto (TREE_TYPE (decl));
27255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27257 if (cp_lexer_next_token_is (parser->lexer,
27258 CPP_OPEN_PAREN))
27259 error ("parenthesized initialization is not allowed in "
27260 "OpenMP %<for%> loop");
27261 else
27262 /* Trigger an error. */
27263 cp_parser_require (parser, CPP_EQ, RT_EQ);
27265 init = error_mark_node;
27266 cp_parser_skip_to_end_of_statement (parser);
27268 else if (CLASS_TYPE_P (TREE_TYPE (decl))
27269 || type_dependent_expression_p (decl)
27270 || auto_node)
27272 bool is_direct_init, is_non_constant_init;
27274 init = cp_parser_initializer (parser,
27275 &is_direct_init,
27276 &is_non_constant_init);
27278 if (auto_node)
27280 TREE_TYPE (decl)
27281 = do_auto_deduction (TREE_TYPE (decl), init,
27282 auto_node);
27284 if (!CLASS_TYPE_P (TREE_TYPE (decl))
27285 && !type_dependent_expression_p (decl))
27286 goto non_class;
27289 cp_finish_decl (decl, init, !is_non_constant_init,
27290 asm_specification,
27291 LOOKUP_ONLYCONVERTING);
27292 if (CLASS_TYPE_P (TREE_TYPE (decl)))
27294 vec_safe_push (for_block, this_pre_body);
27295 init = NULL_TREE;
27297 else
27298 init = pop_stmt_list (this_pre_body);
27299 this_pre_body = NULL_TREE;
27301 else
27303 /* Consume '='. */
27304 cp_lexer_consume_token (parser->lexer);
27305 init = cp_parser_assignment_expression (parser, false, NULL);
27307 non_class:
27308 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27309 init = error_mark_node;
27310 else
27311 cp_finish_decl (decl, NULL_TREE,
27312 /*init_const_expr_p=*/false,
27313 asm_specification,
27314 LOOKUP_ONLYCONVERTING);
27317 if (pushed_scope)
27318 pop_scope (pushed_scope);
27321 else
27323 cp_id_kind idk;
27324 /* If parsing a type specifier sequence failed, then
27325 this MUST be a simple expression. */
27326 cp_parser_parse_tentatively (parser);
27327 decl = cp_parser_primary_expression (parser, false, false,
27328 false, &idk);
27329 if (!cp_parser_error_occurred (parser)
27330 && decl
27331 && DECL_P (decl)
27332 && CLASS_TYPE_P (TREE_TYPE (decl)))
27334 tree rhs;
27336 cp_parser_parse_definitely (parser);
27337 cp_parser_require (parser, CPP_EQ, RT_EQ);
27338 rhs = cp_parser_assignment_expression (parser, false, NULL);
27339 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27340 decl, NOP_EXPR,
27341 rhs,
27342 tf_warning_or_error));
27343 add_private_clause = true;
27345 else
27347 decl = NULL;
27348 cp_parser_abort_tentative_parse (parser);
27349 init = cp_parser_expression (parser, false, NULL);
27350 if (init)
27352 if (TREE_CODE (init) == MODIFY_EXPR
27353 || TREE_CODE (init) == MODOP_EXPR)
27354 real_decl = TREE_OPERAND (init, 0);
27359 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27360 if (this_pre_body)
27362 this_pre_body = pop_stmt_list (this_pre_body);
27363 if (pre_body)
27365 tree t = pre_body;
27366 pre_body = push_stmt_list ();
27367 add_stmt (t);
27368 add_stmt (this_pre_body);
27369 pre_body = pop_stmt_list (pre_body);
27371 else
27372 pre_body = this_pre_body;
27375 if (decl)
27376 real_decl = decl;
27377 if (par_clauses != NULL && real_decl != NULL_TREE)
27379 tree *c;
27380 for (c = par_clauses; *c ; )
27381 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27382 && OMP_CLAUSE_DECL (*c) == real_decl)
27384 error_at (loc, "iteration variable %qD"
27385 " should not be firstprivate", real_decl);
27386 *c = OMP_CLAUSE_CHAIN (*c);
27388 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27389 && OMP_CLAUSE_DECL (*c) == real_decl)
27391 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27392 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27393 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27394 OMP_CLAUSE_DECL (l) = real_decl;
27395 OMP_CLAUSE_CHAIN (l) = clauses;
27396 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27397 clauses = l;
27398 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27399 CP_OMP_CLAUSE_INFO (*c) = NULL;
27400 add_private_clause = false;
27402 else
27404 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27405 && OMP_CLAUSE_DECL (*c) == real_decl)
27406 add_private_clause = false;
27407 c = &OMP_CLAUSE_CHAIN (*c);
27411 if (add_private_clause)
27413 tree c;
27414 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27416 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27417 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27418 && OMP_CLAUSE_DECL (c) == decl)
27419 break;
27420 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27421 && OMP_CLAUSE_DECL (c) == decl)
27422 error_at (loc, "iteration variable %qD "
27423 "should not be firstprivate",
27424 decl);
27425 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27426 && OMP_CLAUSE_DECL (c) == decl)
27427 error_at (loc, "iteration variable %qD should not be reduction",
27428 decl);
27430 if (c == NULL)
27432 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27433 OMP_CLAUSE_DECL (c) = decl;
27434 c = finish_omp_clauses (c);
27435 if (c)
27437 OMP_CLAUSE_CHAIN (c) = clauses;
27438 clauses = c;
27443 cond = NULL;
27444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27445 cond = cp_parser_omp_for_cond (parser, decl);
27446 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27448 incr = NULL;
27449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27451 /* If decl is an iterator, preserve the operator on decl
27452 until finish_omp_for. */
27453 if (real_decl
27454 && ((processing_template_decl
27455 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27456 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27457 incr = cp_parser_omp_for_incr (parser, real_decl);
27458 else
27459 incr = cp_parser_expression (parser, false, NULL);
27460 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27461 SET_EXPR_LOCATION (incr, input_location);
27464 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27465 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27466 /*or_comma=*/false,
27467 /*consume_paren=*/true);
27469 TREE_VEC_ELT (declv, i) = decl;
27470 TREE_VEC_ELT (initv, i) = init;
27471 TREE_VEC_ELT (condv, i) = cond;
27472 TREE_VEC_ELT (incrv, i) = incr;
27474 if (i == collapse - 1)
27475 break;
27477 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27478 in between the collapsed for loops to be still considered perfectly
27479 nested. Hopefully the final version clarifies this.
27480 For now handle (multiple) {'s and empty statements. */
27481 cp_parser_parse_tentatively (parser);
27484 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27485 break;
27486 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27488 cp_lexer_consume_token (parser->lexer);
27489 bracecount++;
27491 else if (bracecount
27492 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27493 cp_lexer_consume_token (parser->lexer);
27494 else
27496 loc = cp_lexer_peek_token (parser->lexer)->location;
27497 error_at (loc, "not enough collapsed for loops");
27498 collapse_err = true;
27499 cp_parser_abort_tentative_parse (parser);
27500 declv = NULL_TREE;
27501 break;
27504 while (1);
27506 if (declv)
27508 cp_parser_parse_definitely (parser);
27509 nbraces += bracecount;
27513 /* Note that we saved the original contents of this flag when we entered
27514 the structured block, and so we don't need to re-save it here. */
27515 parser->in_statement = IN_OMP_FOR;
27517 /* Note that the grammar doesn't call for a structured block here,
27518 though the loop as a whole is a structured block. */
27519 body = push_stmt_list ();
27520 cp_parser_statement (parser, NULL_TREE, false, NULL);
27521 body = pop_stmt_list (body);
27523 if (declv == NULL_TREE)
27524 ret = NULL_TREE;
27525 else
27526 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27527 pre_body, clauses);
27529 while (nbraces)
27531 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27533 cp_lexer_consume_token (parser->lexer);
27534 nbraces--;
27536 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27537 cp_lexer_consume_token (parser->lexer);
27538 else
27540 if (!collapse_err)
27542 error_at (cp_lexer_peek_token (parser->lexer)->location,
27543 "collapsed loops not perfectly nested");
27545 collapse_err = true;
27546 cp_parser_statement_seq_opt (parser, NULL);
27547 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27548 break;
27552 while (!for_block->is_empty ())
27553 add_stmt (pop_stmt_list (for_block->pop ()));
27554 release_tree_vector (for_block);
27556 return ret;
27559 /* OpenMP 2.5:
27560 #pragma omp for for-clause[optseq] new-line
27561 for-loop */
27563 #define OMP_FOR_CLAUSE_MASK \
27564 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27565 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27566 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27567 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27568 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27569 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27570 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27571 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27573 static tree
27574 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27576 tree clauses, sb, ret;
27577 unsigned int save;
27579 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27580 "#pragma omp for", pragma_tok);
27582 sb = begin_omp_structured_block ();
27583 save = cp_parser_begin_omp_structured_block (parser);
27585 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27587 cp_parser_end_omp_structured_block (parser, save);
27588 add_stmt (finish_omp_structured_block (sb));
27590 return ret;
27593 /* OpenMP 2.5:
27594 # pragma omp master new-line
27595 structured-block */
27597 static tree
27598 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27600 cp_parser_require_pragma_eol (parser, pragma_tok);
27601 return c_finish_omp_master (input_location,
27602 cp_parser_omp_structured_block (parser));
27605 /* OpenMP 2.5:
27606 # pragma omp ordered new-line
27607 structured-block */
27609 static tree
27610 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27612 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27613 cp_parser_require_pragma_eol (parser, pragma_tok);
27614 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27617 /* OpenMP 2.5:
27619 section-scope:
27620 { section-sequence }
27622 section-sequence:
27623 section-directive[opt] structured-block
27624 section-sequence section-directive structured-block */
27626 static tree
27627 cp_parser_omp_sections_scope (cp_parser *parser)
27629 tree stmt, substmt;
27630 bool error_suppress = false;
27631 cp_token *tok;
27633 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27634 return NULL_TREE;
27636 stmt = push_stmt_list ();
27638 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27640 unsigned save;
27642 substmt = begin_omp_structured_block ();
27643 save = cp_parser_begin_omp_structured_block (parser);
27645 while (1)
27647 cp_parser_statement (parser, NULL_TREE, false, NULL);
27649 tok = cp_lexer_peek_token (parser->lexer);
27650 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27651 break;
27652 if (tok->type == CPP_CLOSE_BRACE)
27653 break;
27654 if (tok->type == CPP_EOF)
27655 break;
27658 cp_parser_end_omp_structured_block (parser, save);
27659 substmt = finish_omp_structured_block (substmt);
27660 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27661 add_stmt (substmt);
27664 while (1)
27666 tok = cp_lexer_peek_token (parser->lexer);
27667 if (tok->type == CPP_CLOSE_BRACE)
27668 break;
27669 if (tok->type == CPP_EOF)
27670 break;
27672 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27674 cp_lexer_consume_token (parser->lexer);
27675 cp_parser_require_pragma_eol (parser, tok);
27676 error_suppress = false;
27678 else if (!error_suppress)
27680 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27681 error_suppress = true;
27684 substmt = cp_parser_omp_structured_block (parser);
27685 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27686 add_stmt (substmt);
27688 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27690 substmt = pop_stmt_list (stmt);
27692 stmt = make_node (OMP_SECTIONS);
27693 TREE_TYPE (stmt) = void_type_node;
27694 OMP_SECTIONS_BODY (stmt) = substmt;
27696 add_stmt (stmt);
27697 return stmt;
27700 /* OpenMP 2.5:
27701 # pragma omp sections sections-clause[optseq] newline
27702 sections-scope */
27704 #define OMP_SECTIONS_CLAUSE_MASK \
27705 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27706 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27707 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27708 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27709 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27711 static tree
27712 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27714 tree clauses, ret;
27716 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27717 "#pragma omp sections", pragma_tok);
27719 ret = cp_parser_omp_sections_scope (parser);
27720 if (ret)
27721 OMP_SECTIONS_CLAUSES (ret) = clauses;
27723 return ret;
27726 /* OpenMP 2.5:
27727 # pragma parallel parallel-clause new-line
27728 # pragma parallel for parallel-for-clause new-line
27729 # pragma parallel sections parallel-sections-clause new-line */
27731 #define OMP_PARALLEL_CLAUSE_MASK \
27732 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27733 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27734 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27735 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27736 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27737 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27738 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27739 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27741 static tree
27742 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27744 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27745 const char *p_name = "#pragma omp parallel";
27746 tree stmt, clauses, par_clause, ws_clause, block;
27747 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27748 unsigned int save;
27749 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27751 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27753 cp_lexer_consume_token (parser->lexer);
27754 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27755 p_name = "#pragma omp parallel for";
27756 mask |= OMP_FOR_CLAUSE_MASK;
27757 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27759 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27761 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27762 const char *p = IDENTIFIER_POINTER (id);
27763 if (strcmp (p, "sections") == 0)
27765 cp_lexer_consume_token (parser->lexer);
27766 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27767 p_name = "#pragma omp parallel sections";
27768 mask |= OMP_SECTIONS_CLAUSE_MASK;
27769 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27773 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27774 block = begin_omp_parallel ();
27775 save = cp_parser_begin_omp_structured_block (parser);
27777 switch (p_kind)
27779 case PRAGMA_OMP_PARALLEL:
27780 cp_parser_statement (parser, NULL_TREE, false, NULL);
27781 par_clause = clauses;
27782 break;
27784 case PRAGMA_OMP_PARALLEL_FOR:
27785 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27786 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27787 break;
27789 case PRAGMA_OMP_PARALLEL_SECTIONS:
27790 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27791 stmt = cp_parser_omp_sections_scope (parser);
27792 if (stmt)
27793 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27794 break;
27796 default:
27797 gcc_unreachable ();
27800 cp_parser_end_omp_structured_block (parser, save);
27801 stmt = finish_omp_parallel (par_clause, block);
27802 if (p_kind != PRAGMA_OMP_PARALLEL)
27803 OMP_PARALLEL_COMBINED (stmt) = 1;
27804 return stmt;
27807 /* OpenMP 2.5:
27808 # pragma omp single single-clause[optseq] new-line
27809 structured-block */
27811 #define OMP_SINGLE_CLAUSE_MASK \
27812 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27813 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27814 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27815 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27817 static tree
27818 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27820 tree stmt = make_node (OMP_SINGLE);
27821 TREE_TYPE (stmt) = void_type_node;
27823 OMP_SINGLE_CLAUSES (stmt)
27824 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27825 "#pragma omp single", pragma_tok);
27826 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27828 return add_stmt (stmt);
27831 /* OpenMP 3.0:
27832 # pragma omp task task-clause[optseq] new-line
27833 structured-block */
27835 #define OMP_TASK_CLAUSE_MASK \
27836 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27837 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27838 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27839 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27840 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27841 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27842 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27843 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27845 static tree
27846 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27848 tree clauses, block;
27849 unsigned int save;
27851 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27852 "#pragma omp task", pragma_tok);
27853 block = begin_omp_task ();
27854 save = cp_parser_begin_omp_structured_block (parser);
27855 cp_parser_statement (parser, NULL_TREE, false, NULL);
27856 cp_parser_end_omp_structured_block (parser, save);
27857 return finish_omp_task (clauses, block);
27860 /* OpenMP 3.0:
27861 # pragma omp taskwait new-line */
27863 static void
27864 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27866 cp_parser_require_pragma_eol (parser, pragma_tok);
27867 finish_omp_taskwait ();
27870 /* OpenMP 3.1:
27871 # pragma omp taskyield new-line */
27873 static void
27874 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27876 cp_parser_require_pragma_eol (parser, pragma_tok);
27877 finish_omp_taskyield ();
27880 /* OpenMP 2.5:
27881 # pragma omp threadprivate (variable-list) */
27883 static void
27884 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27886 tree vars;
27888 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27889 cp_parser_require_pragma_eol (parser, pragma_tok);
27891 finish_omp_threadprivate (vars);
27894 /* Main entry point to OpenMP statement pragmas. */
27896 static void
27897 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27899 tree stmt;
27901 switch (pragma_tok->pragma_kind)
27903 case PRAGMA_OMP_ATOMIC:
27904 cp_parser_omp_atomic (parser, pragma_tok);
27905 return;
27906 case PRAGMA_OMP_CRITICAL:
27907 stmt = cp_parser_omp_critical (parser, pragma_tok);
27908 break;
27909 case PRAGMA_OMP_FOR:
27910 stmt = cp_parser_omp_for (parser, pragma_tok);
27911 break;
27912 case PRAGMA_OMP_MASTER:
27913 stmt = cp_parser_omp_master (parser, pragma_tok);
27914 break;
27915 case PRAGMA_OMP_ORDERED:
27916 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27917 break;
27918 case PRAGMA_OMP_PARALLEL:
27919 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27920 break;
27921 case PRAGMA_OMP_SECTIONS:
27922 stmt = cp_parser_omp_sections (parser, pragma_tok);
27923 break;
27924 case PRAGMA_OMP_SINGLE:
27925 stmt = cp_parser_omp_single (parser, pragma_tok);
27926 break;
27927 case PRAGMA_OMP_TASK:
27928 stmt = cp_parser_omp_task (parser, pragma_tok);
27929 break;
27930 default:
27931 gcc_unreachable ();
27934 if (stmt)
27935 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27938 /* Transactional Memory parsing routines. */
27940 /* Parse a transaction attribute.
27942 txn-attribute:
27943 attribute
27944 [ [ identifier ] ]
27946 ??? Simplify this when C++0x bracket attributes are
27947 implemented properly. */
27949 static tree
27950 cp_parser_txn_attribute_opt (cp_parser *parser)
27952 cp_token *token;
27953 tree attr_name, attr = NULL;
27955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27956 return cp_parser_attributes_opt (parser);
27958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27959 return NULL_TREE;
27960 cp_lexer_consume_token (parser->lexer);
27961 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27962 goto error1;
27964 token = cp_lexer_peek_token (parser->lexer);
27965 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27967 token = cp_lexer_consume_token (parser->lexer);
27969 attr_name = (token->type == CPP_KEYWORD
27970 /* For keywords, use the canonical spelling,
27971 not the parsed identifier. */
27972 ? ridpointers[(int) token->keyword]
27973 : token->u.value);
27974 attr = build_tree_list (attr_name, NULL_TREE);
27976 else
27977 cp_parser_error (parser, "expected identifier");
27979 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27980 error1:
27981 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27982 return attr;
27985 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27987 transaction-statement:
27988 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27989 compound-statement
27990 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27993 static tree
27994 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27996 unsigned char old_in = parser->in_transaction;
27997 unsigned char this_in = 1, new_in;
27998 cp_token *token;
27999 tree stmt, attrs, noex;
28001 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28002 || keyword == RID_TRANSACTION_RELAXED);
28003 token = cp_parser_require_keyword (parser, keyword,
28004 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28005 : RT_TRANSACTION_RELAXED));
28006 gcc_assert (token != NULL);
28008 if (keyword == RID_TRANSACTION_RELAXED)
28009 this_in |= TM_STMT_ATTR_RELAXED;
28010 else
28012 attrs = cp_parser_txn_attribute_opt (parser);
28013 if (attrs)
28014 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28017 /* Parse a noexcept specification. */
28018 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
28020 /* Keep track if we're in the lexical scope of an outer transaction. */
28021 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
28023 stmt = begin_transaction_stmt (token->location, NULL, this_in);
28025 parser->in_transaction = new_in;
28026 cp_parser_compound_statement (parser, NULL, false, false);
28027 parser->in_transaction = old_in;
28029 finish_transaction_stmt (stmt, NULL, this_in, noex);
28031 return stmt;
28034 /* Parse a __transaction_atomic or __transaction_relaxed expression.
28036 transaction-expression:
28037 __transaction_atomic txn-noexcept-spec[opt] ( expression )
28038 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
28041 static tree
28042 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
28044 unsigned char old_in = parser->in_transaction;
28045 unsigned char this_in = 1;
28046 cp_token *token;
28047 tree expr, noex;
28048 bool noex_expr;
28050 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28051 || keyword == RID_TRANSACTION_RELAXED);
28053 if (!flag_tm)
28054 error (keyword == RID_TRANSACTION_RELAXED
28055 ? G_("%<__transaction_relaxed%> without transactional memory "
28056 "support enabled")
28057 : G_("%<__transaction_atomic%> without transactional memory "
28058 "support enabled"));
28060 token = cp_parser_require_keyword (parser, keyword,
28061 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28062 : RT_TRANSACTION_RELAXED));
28063 gcc_assert (token != NULL);
28065 if (keyword == RID_TRANSACTION_RELAXED)
28066 this_in |= TM_STMT_ATTR_RELAXED;
28068 /* Set this early. This might mean that we allow transaction_cancel in
28069 an expression that we find out later actually has to be a constexpr.
28070 However, we expect that cxx_constant_value will be able to deal with
28071 this; also, if the noexcept has no constexpr, then what we parse next
28072 really is a transaction's body. */
28073 parser->in_transaction = this_in;
28075 /* Parse a noexcept specification. */
28076 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
28077 true);
28079 if (!noex || !noex_expr
28080 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28082 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28084 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28085 expr = finish_parenthesized_expr (expr);
28087 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28089 else
28091 /* The only expression that is available got parsed for the noexcept
28092 already. noexcept is true then. */
28093 expr = noex;
28094 noex = boolean_true_node;
28097 expr = build_transaction_expr (token->location, expr, this_in, noex);
28098 parser->in_transaction = old_in;
28100 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
28101 return error_mark_node;
28103 return (flag_tm ? expr : error_mark_node);
28106 /* Parse a function-transaction-block.
28108 function-transaction-block:
28109 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
28110 function-body
28111 __transaction_atomic txn-attribute[opt] function-try-block
28112 __transaction_relaxed ctor-initializer[opt] function-body
28113 __transaction_relaxed function-try-block
28116 static bool
28117 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
28119 unsigned char old_in = parser->in_transaction;
28120 unsigned char new_in = 1;
28121 tree compound_stmt, stmt, attrs;
28122 bool ctor_initializer_p;
28123 cp_token *token;
28125 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28126 || keyword == RID_TRANSACTION_RELAXED);
28127 token = cp_parser_require_keyword (parser, keyword,
28128 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28129 : RT_TRANSACTION_RELAXED));
28130 gcc_assert (token != NULL);
28132 if (keyword == RID_TRANSACTION_RELAXED)
28133 new_in |= TM_STMT_ATTR_RELAXED;
28134 else
28136 attrs = cp_parser_txn_attribute_opt (parser);
28137 if (attrs)
28138 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28141 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
28143 parser->in_transaction = new_in;
28145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28146 ctor_initializer_p = cp_parser_function_try_block (parser);
28147 else
28148 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
28149 (parser, /*in_function_try_block=*/false);
28151 parser->in_transaction = old_in;
28153 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
28155 return ctor_initializer_p;
28158 /* Parse a __transaction_cancel statement.
28160 cancel-statement:
28161 __transaction_cancel txn-attribute[opt] ;
28162 __transaction_cancel txn-attribute[opt] throw-expression ;
28164 ??? Cancel and throw is not yet implemented. */
28166 static tree
28167 cp_parser_transaction_cancel (cp_parser *parser)
28169 cp_token *token;
28170 bool is_outer = false;
28171 tree stmt, attrs;
28173 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
28174 RT_TRANSACTION_CANCEL);
28175 gcc_assert (token != NULL);
28177 attrs = cp_parser_txn_attribute_opt (parser);
28178 if (attrs)
28179 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
28181 /* ??? Parse cancel-and-throw here. */
28183 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28185 if (!flag_tm)
28187 error_at (token->location, "%<__transaction_cancel%> without "
28188 "transactional memory support enabled");
28189 return error_mark_node;
28191 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
28193 error_at (token->location, "%<__transaction_cancel%> within a "
28194 "%<__transaction_relaxed%>");
28195 return error_mark_node;
28197 else if (is_outer)
28199 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
28200 && !is_tm_may_cancel_outer (current_function_decl))
28202 error_at (token->location, "outer %<__transaction_cancel%> not "
28203 "within outer %<__transaction_atomic%>");
28204 error_at (token->location,
28205 " or a %<transaction_may_cancel_outer%> function");
28206 return error_mark_node;
28209 else if (parser->in_transaction == 0)
28211 error_at (token->location, "%<__transaction_cancel%> not within "
28212 "%<__transaction_atomic%>");
28213 return error_mark_node;
28216 stmt = build_tm_abort_call (token->location, is_outer);
28217 add_stmt (stmt);
28218 finish_stmt ();
28220 return stmt;
28223 /* The parser. */
28225 static GTY (()) cp_parser *the_parser;
28228 /* Special handling for the first token or line in the file. The first
28229 thing in the file might be #pragma GCC pch_preprocess, which loads a
28230 PCH file, which is a GC collection point. So we need to handle this
28231 first pragma without benefit of an existing lexer structure.
28233 Always returns one token to the caller in *FIRST_TOKEN. This is
28234 either the true first token of the file, or the first token after
28235 the initial pragma. */
28237 static void
28238 cp_parser_initial_pragma (cp_token *first_token)
28240 tree name = NULL;
28242 cp_lexer_get_preprocessor_token (NULL, first_token);
28243 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
28244 return;
28246 cp_lexer_get_preprocessor_token (NULL, first_token);
28247 if (first_token->type == CPP_STRING)
28249 name = first_token->u.value;
28251 cp_lexer_get_preprocessor_token (NULL, first_token);
28252 if (first_token->type != CPP_PRAGMA_EOL)
28253 error_at (first_token->location,
28254 "junk at end of %<#pragma GCC pch_preprocess%>");
28256 else
28257 error_at (first_token->location, "expected string literal");
28259 /* Skip to the end of the pragma. */
28260 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
28261 cp_lexer_get_preprocessor_token (NULL, first_token);
28263 /* Now actually load the PCH file. */
28264 if (name)
28265 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
28267 /* Read one more token to return to our caller. We have to do this
28268 after reading the PCH file in, since its pointers have to be
28269 live. */
28270 cp_lexer_get_preprocessor_token (NULL, first_token);
28273 /* Normal parsing of a pragma token. Here we can (and must) use the
28274 regular lexer. */
28276 static bool
28277 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
28279 cp_token *pragma_tok;
28280 unsigned int id;
28282 pragma_tok = cp_lexer_consume_token (parser->lexer);
28283 gcc_assert (pragma_tok->type == CPP_PRAGMA);
28284 parser->lexer->in_pragma = true;
28286 id = pragma_tok->pragma_kind;
28287 switch (id)
28289 case PRAGMA_GCC_PCH_PREPROCESS:
28290 error_at (pragma_tok->location,
28291 "%<#pragma GCC pch_preprocess%> must be first");
28292 break;
28294 case PRAGMA_OMP_BARRIER:
28295 switch (context)
28297 case pragma_compound:
28298 cp_parser_omp_barrier (parser, pragma_tok);
28299 return false;
28300 case pragma_stmt:
28301 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28302 "used in compound statements");
28303 break;
28304 default:
28305 goto bad_stmt;
28307 break;
28309 case PRAGMA_OMP_FLUSH:
28310 switch (context)
28312 case pragma_compound:
28313 cp_parser_omp_flush (parser, pragma_tok);
28314 return false;
28315 case pragma_stmt:
28316 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28317 "used in compound statements");
28318 break;
28319 default:
28320 goto bad_stmt;
28322 break;
28324 case PRAGMA_OMP_TASKWAIT:
28325 switch (context)
28327 case pragma_compound:
28328 cp_parser_omp_taskwait (parser, pragma_tok);
28329 return false;
28330 case pragma_stmt:
28331 error_at (pragma_tok->location,
28332 "%<#pragma omp taskwait%> may only be "
28333 "used in compound statements");
28334 break;
28335 default:
28336 goto bad_stmt;
28338 break;
28340 case PRAGMA_OMP_TASKYIELD:
28341 switch (context)
28343 case pragma_compound:
28344 cp_parser_omp_taskyield (parser, pragma_tok);
28345 return false;
28346 case pragma_stmt:
28347 error_at (pragma_tok->location,
28348 "%<#pragma omp taskyield%> may only be "
28349 "used in compound statements");
28350 break;
28351 default:
28352 goto bad_stmt;
28354 break;
28356 case PRAGMA_OMP_THREADPRIVATE:
28357 cp_parser_omp_threadprivate (parser, pragma_tok);
28358 return false;
28360 case PRAGMA_OMP_ATOMIC:
28361 case PRAGMA_OMP_CRITICAL:
28362 case PRAGMA_OMP_FOR:
28363 case PRAGMA_OMP_MASTER:
28364 case PRAGMA_OMP_ORDERED:
28365 case PRAGMA_OMP_PARALLEL:
28366 case PRAGMA_OMP_SECTIONS:
28367 case PRAGMA_OMP_SINGLE:
28368 case PRAGMA_OMP_TASK:
28369 if (context == pragma_external)
28370 goto bad_stmt;
28371 cp_parser_omp_construct (parser, pragma_tok);
28372 return true;
28374 case PRAGMA_OMP_SECTION:
28375 error_at (pragma_tok->location,
28376 "%<#pragma omp section%> may only be used in "
28377 "%<#pragma omp sections%> construct");
28378 break;
28380 default:
28381 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28382 c_invoke_pragma_handler (id);
28383 break;
28385 bad_stmt:
28386 cp_parser_error (parser, "expected declaration specifiers");
28387 break;
28390 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28391 return false;
28394 /* The interface the pragma parsers have to the lexer. */
28396 enum cpp_ttype
28397 pragma_lex (tree *value)
28399 cp_token *tok;
28400 enum cpp_ttype ret;
28402 tok = cp_lexer_peek_token (the_parser->lexer);
28404 ret = tok->type;
28405 *value = tok->u.value;
28407 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28408 ret = CPP_EOF;
28409 else if (ret == CPP_STRING)
28410 *value = cp_parser_string_literal (the_parser, false, false);
28411 else
28413 cp_lexer_consume_token (the_parser->lexer);
28414 if (ret == CPP_KEYWORD)
28415 ret = CPP_NAME;
28418 return ret;
28422 /* External interface. */
28424 /* Parse one entire translation unit. */
28426 void
28427 c_parse_file (void)
28429 static bool already_called = false;
28431 if (already_called)
28433 sorry ("inter-module optimizations not implemented for C++");
28434 return;
28436 already_called = true;
28438 the_parser = cp_parser_new ();
28439 push_deferring_access_checks (flag_access_control
28440 ? dk_no_deferred : dk_no_check);
28441 cp_parser_translation_unit (the_parser);
28442 the_parser = NULL;
28445 #include "gt-cp-parser.h"