* xvasprintf.c: New file.
[official-gcc.git] / gcc / cp / parser.c
blob48dd64a4c1703e5d62cec0e7ff21f2e1e1380943
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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 "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "hash-map.h"
40 #include "is-a.h"
41 #include "plugin-api.h"
42 #include "vec.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "machmode.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
53 #include "plugin.h"
54 #include "tree-pretty-print.h"
55 #include "parser.h"
56 #include "type-utils.h"
57 #include "omp-low.h"
60 /* The lexer. */
62 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
63 and c-lex.c) and the C++ parser. */
65 static cp_token eof_token =
67 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
70 /* The various kinds of non integral constant we encounter. */
71 typedef enum non_integral_constant {
72 NIC_NONE,
73 /* floating-point literal */
74 NIC_FLOAT,
75 /* %<this%> */
76 NIC_THIS,
77 /* %<__FUNCTION__%> */
78 NIC_FUNC_NAME,
79 /* %<__PRETTY_FUNCTION__%> */
80 NIC_PRETTY_FUNC,
81 /* %<__func__%> */
82 NIC_C99_FUNC,
83 /* "%<va_arg%> */
84 NIC_VA_ARG,
85 /* a cast */
86 NIC_CAST,
87 /* %<typeid%> operator */
88 NIC_TYPEID,
89 /* non-constant compound literals */
90 NIC_NCC,
91 /* a function call */
92 NIC_FUNC_CALL,
93 /* an increment */
94 NIC_INC,
95 /* an decrement */
96 NIC_DEC,
97 /* an array reference */
98 NIC_ARRAY_REF,
99 /* %<->%> */
100 NIC_ARROW,
101 /* %<.%> */
102 NIC_POINT,
103 /* the address of a label */
104 NIC_ADDR_LABEL,
105 /* %<*%> */
106 NIC_STAR,
107 /* %<&%> */
108 NIC_ADDR,
109 /* %<++%> */
110 NIC_PREINCREMENT,
111 /* %<--%> */
112 NIC_PREDECREMENT,
113 /* %<new%> */
114 NIC_NEW,
115 /* %<delete%> */
116 NIC_DEL,
117 /* calls to overloaded operators */
118 NIC_OVERLOADED,
119 /* an assignment */
120 NIC_ASSIGNMENT,
121 /* a comma operator */
122 NIC_COMMA,
123 /* a call to a constructor */
124 NIC_CONSTRUCTOR,
125 /* a transaction expression */
126 NIC_TRANSACTION
127 } non_integral_constant;
129 /* The various kinds of errors about name-lookup failing. */
130 typedef enum name_lookup_error {
131 /* NULL */
132 NLE_NULL,
133 /* is not a type */
134 NLE_TYPE,
135 /* is not a class or namespace */
136 NLE_CXX98,
137 /* is not a class, namespace, or enumeration */
138 NLE_NOT_CXX98
139 } name_lookup_error;
141 /* The various kinds of required token */
142 typedef enum required_token {
143 RT_NONE,
144 RT_SEMICOLON, /* ';' */
145 RT_OPEN_PAREN, /* '(' */
146 RT_CLOSE_BRACE, /* '}' */
147 RT_OPEN_BRACE, /* '{' */
148 RT_CLOSE_SQUARE, /* ']' */
149 RT_OPEN_SQUARE, /* '[' */
150 RT_COMMA, /* ',' */
151 RT_SCOPE, /* '::' */
152 RT_LESS, /* '<' */
153 RT_GREATER, /* '>' */
154 RT_EQ, /* '=' */
155 RT_ELLIPSIS, /* '...' */
156 RT_MULT, /* '*' */
157 RT_COMPL, /* '~' */
158 RT_COLON, /* ':' */
159 RT_COLON_SCOPE, /* ':' or '::' */
160 RT_CLOSE_PAREN, /* ')' */
161 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
162 RT_PRAGMA_EOL, /* end of line */
163 RT_NAME, /* identifier */
165 /* The type is CPP_KEYWORD */
166 RT_NEW, /* new */
167 RT_DELETE, /* delete */
168 RT_RETURN, /* return */
169 RT_WHILE, /* while */
170 RT_EXTERN, /* extern */
171 RT_STATIC_ASSERT, /* static_assert */
172 RT_DECLTYPE, /* decltype */
173 RT_OPERATOR, /* operator */
174 RT_CLASS, /* class */
175 RT_TEMPLATE, /* template */
176 RT_NAMESPACE, /* namespace */
177 RT_USING, /* using */
178 RT_ASM, /* asm */
179 RT_TRY, /* try */
180 RT_CATCH, /* catch */
181 RT_THROW, /* throw */
182 RT_LABEL, /* __label__ */
183 RT_AT_TRY, /* @try */
184 RT_AT_SYNCHRONIZED, /* @synchronized */
185 RT_AT_THROW, /* @throw */
187 RT_SELECT, /* selection-statement */
188 RT_INTERATION, /* iteration-statement */
189 RT_JUMP, /* jump-statement */
190 RT_CLASS_KEY, /* class-key */
191 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
192 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
193 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
194 RT_TRANSACTION_CANCEL /* __transaction_cancel */
195 } required_token;
197 /* Prototypes. */
199 static cp_lexer *cp_lexer_new_main
200 (void);
201 static cp_lexer *cp_lexer_new_from_tokens
202 (cp_token_cache *tokens);
203 static void cp_lexer_destroy
204 (cp_lexer *);
205 static int cp_lexer_saving_tokens
206 (const cp_lexer *);
207 static cp_token *cp_lexer_token_at
208 (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
212 (cp_lexer *);
213 static cp_token *cp_lexer_peek_nth_token
214 (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
222 (cp_lexer *);
223 static void cp_lexer_purge_token
224 (cp_lexer *);
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
228 (cp_lexer *);
229 static void cp_lexer_commit_tokens
230 (cp_lexer *);
231 static void cp_lexer_rollback_tokens
232 (cp_lexer *);
233 static void cp_lexer_print_token
234 (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
236 (cp_lexer *);
237 static void cp_lexer_start_debugging
238 (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static cp_token_cache *cp_token_cache_new
243 (cp_token *, cp_token *);
245 static void cp_parser_initial_pragma
246 (cp_token *);
248 static tree cp_literal_operator_id
249 (const char *);
251 static void cp_parser_cilk_simd
252 (cp_parser *, cp_token *);
253 static tree cp_parser_cilk_for
254 (cp_parser *, tree);
255 static bool cp_parser_omp_declare_reduction_exprs
256 (tree, cp_parser *);
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser *, tree, bool);
260 /* Manifest constants. */
261 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
262 #define CP_SAVED_TOKEN_STACK 5
264 /* Variables. */
266 /* The stream to which debugging output should be written. */
267 static FILE *cp_lexer_debug_stream;
269 /* Nonzero if we are parsing an unevaluated operand: an operand to
270 sizeof, typeof, or alignof. */
271 int cp_unevaluated_operand;
273 /* Dump up to NUM tokens in BUFFER to FILE starting with token
274 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
275 first token in BUFFER. If NUM is 0, dump all the tokens. If
276 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
277 highlighted by surrounding it in [[ ]]. */
279 static void
280 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
281 cp_token *start_token, unsigned num,
282 cp_token *curr_token)
284 unsigned i, nprinted;
285 cp_token *token;
286 bool do_print;
288 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
290 if (buffer == NULL)
291 return;
293 if (num == 0)
294 num = buffer->length ();
296 if (start_token == NULL)
297 start_token = buffer->address ();
299 if (start_token > buffer->address ())
301 cp_lexer_print_token (file, &(*buffer)[0]);
302 fprintf (file, " ... ");
305 do_print = false;
306 nprinted = 0;
307 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
309 if (token == start_token)
310 do_print = true;
312 if (!do_print)
313 continue;
315 nprinted++;
316 if (token == curr_token)
317 fprintf (file, "[[");
319 cp_lexer_print_token (file, token);
321 if (token == curr_token)
322 fprintf (file, "]]");
324 switch (token->type)
326 case CPP_SEMICOLON:
327 case CPP_OPEN_BRACE:
328 case CPP_CLOSE_BRACE:
329 case CPP_EOF:
330 fputc ('\n', file);
331 break;
333 default:
334 fputc (' ', file);
338 if (i == num && i < buffer->length ())
340 fprintf (file, " ... ");
341 cp_lexer_print_token (file, &buffer->last ());
344 fprintf (file, "\n");
348 /* Dump all tokens in BUFFER to stderr. */
350 void
351 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
353 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> &ref)
359 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> *ptr)
365 if (ptr)
366 debug (*ptr);
367 else
368 fprintf (stderr, "<nil>\n");
372 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
373 description for T. */
375 static void
376 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
378 if (t)
380 fprintf (file, "%s: ", desc);
381 print_node_brief (file, "", t, 0);
386 /* Dump parser context C to FILE. */
388 static void
389 cp_debug_print_context (FILE *file, cp_parser_context *c)
391 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
392 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
393 print_node_brief (file, "", c->object_type, 0);
394 fprintf (file, "}\n");
398 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 static void
401 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
403 unsigned i;
404 cp_parser_context *c;
406 fprintf (file, "Parsing context stack:\n");
407 for (i = 0, c = first; c; c = c->next, i++)
409 fprintf (file, "\t#%u: ", i);
410 cp_debug_print_context (file, c);
415 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 static void
418 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
420 if (flag)
421 fprintf (file, "%s: true\n", desc);
425 /* Print an unparsed function entry UF to FILE. */
427 static void
428 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
430 unsigned i;
431 cp_default_arg_entry *default_arg_fn;
432 tree fn;
434 fprintf (file, "\tFunctions with default args:\n");
435 for (i = 0;
436 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
437 i++)
439 fprintf (file, "\t\tClass type: ");
440 print_node_brief (file, "", default_arg_fn->class_type, 0);
441 fprintf (file, "\t\tDeclaration: ");
442 print_node_brief (file, "", default_arg_fn->decl, 0);
443 fprintf (file, "\n");
446 fprintf (file, "\n\tFunctions with definitions that require "
447 "post-processing\n\t\t");
448 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
450 print_node_brief (file, "", fn, 0);
451 fprintf (file, " ");
453 fprintf (file, "\n");
455 fprintf (file, "\n\tNon-static data members with initializers that require "
456 "post-processing\n\t\t");
457 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
459 print_node_brief (file, "", fn, 0);
460 fprintf (file, " ");
462 fprintf (file, "\n");
466 /* Print the stack of unparsed member functions S to FILE. */
468 static void
469 cp_debug_print_unparsed_queues (FILE *file,
470 vec<cp_unparsed_functions_entry, va_gc> *s)
472 unsigned i;
473 cp_unparsed_functions_entry *uf;
475 fprintf (file, "Unparsed functions\n");
476 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
478 fprintf (file, "#%u:\n", i);
479 cp_debug_print_unparsed_function (file, uf);
484 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
485 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 static void
488 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
490 cp_token *next_token, *first_token, *start_token;
492 if (file == NULL)
493 file = stderr;
495 next_token = parser->lexer->next_token;
496 first_token = parser->lexer->buffer->address ();
497 start_token = (next_token > first_token + window_size / 2)
498 ? next_token - window_size / 2
499 : first_token;
500 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
501 next_token);
505 /* Dump debugging information for the given PARSER. If FILE is NULL,
506 the output is printed on stderr. */
508 void
509 cp_debug_parser (FILE *file, cp_parser *parser)
511 const size_t window_size = 20;
512 cp_token *token;
513 expanded_location eloc;
515 if (file == NULL)
516 file = stderr;
518 fprintf (file, "Parser state\n\n");
519 fprintf (file, "Number of tokens: %u\n",
520 vec_safe_length (parser->lexer->buffer));
521 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
522 cp_debug_print_tree_if_set (file, "Object scope",
523 parser->object_scope);
524 cp_debug_print_tree_if_set (file, "Qualifying scope",
525 parser->qualifying_scope);
526 cp_debug_print_context_stack (file, parser->context);
527 cp_debug_print_flag (file, "Allow GNU extensions",
528 parser->allow_gnu_extensions_p);
529 cp_debug_print_flag (file, "'>' token is greater-than",
530 parser->greater_than_is_operator_p);
531 cp_debug_print_flag (file, "Default args allowed in current "
532 "parameter list", parser->default_arg_ok_p);
533 cp_debug_print_flag (file, "Parsing integral constant-expression",
534 parser->integral_constant_expression_p);
535 cp_debug_print_flag (file, "Allow non-constant expression in current "
536 "constant-expression",
537 parser->allow_non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Seen non-constant expression",
539 parser->non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
541 "current context",
542 parser->local_variables_forbidden_p);
543 cp_debug_print_flag (file, "In unbraced linkage specification",
544 parser->in_unbraced_linkage_specification_p);
545 cp_debug_print_flag (file, "Parsing a declarator",
546 parser->in_declarator_p);
547 cp_debug_print_flag (file, "In template argument list",
548 parser->in_template_argument_list_p);
549 cp_debug_print_flag (file, "Parsing an iteration statement",
550 parser->in_statement & IN_ITERATION_STMT);
551 cp_debug_print_flag (file, "Parsing a switch statement",
552 parser->in_statement & IN_SWITCH_STMT);
553 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
554 parser->in_statement & IN_OMP_BLOCK);
555 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
556 parser->in_statement & IN_CILK_SIMD_FOR);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
564 parser->implicit_extern_c);
565 cp_debug_print_flag (file, "String expressions should be translated "
566 "to execution character set",
567 parser->translate_strings_p);
568 cp_debug_print_flag (file, "Parsing function body outside of a "
569 "local class", parser->in_function_body);
570 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
571 parser->colon_corrects_to_scope_p);
572 cp_debug_print_flag (file, "Colon doesn't start a class definition",
573 parser->colon_doesnt_start_class_def_p);
574 if (parser->type_definition_forbidden_message)
575 fprintf (file, "Error message for forbidden type definitions: %s\n",
576 parser->type_definition_forbidden_message);
577 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
578 fprintf (file, "Number of class definitions in progress: %u\n",
579 parser->num_classes_being_defined);
580 fprintf (file, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser->num_template_parameter_lists);
582 cp_debug_parser_tokens (file, parser, window_size);
583 token = parser->lexer->next_token;
584 fprintf (file, "Next token to parse:\n");
585 fprintf (file, "\tToken: ");
586 cp_lexer_print_token (file, token);
587 eloc = expand_location (token->location);
588 fprintf (file, "\n\tFile: %s\n", eloc.file);
589 fprintf (file, "\tLine: %d\n", eloc.line);
590 fprintf (file, "\tColumn: %d\n", eloc.column);
593 DEBUG_FUNCTION void
594 debug (cp_parser &ref)
596 cp_debug_parser (stderr, &ref);
599 DEBUG_FUNCTION void
600 debug (cp_parser *ptr)
602 if (ptr)
603 debug (*ptr);
604 else
605 fprintf (stderr, "<nil>\n");
608 /* Allocate memory for a new lexer object and return it. */
610 static cp_lexer *
611 cp_lexer_alloc (void)
613 cp_lexer *lexer;
615 c_common_no_more_pch ();
617 /* Allocate the memory. */
618 lexer = ggc_cleared_alloc<cp_lexer> ();
620 /* Initially we are not debugging. */
621 lexer->debugging_p = false;
623 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
625 /* Create the buffer. */
626 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
628 return lexer;
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
635 static cp_lexer *
636 cp_lexer_new_main (void)
638 cp_lexer *lexer;
639 cp_token token;
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token);
646 lexer = cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 lexer->buffer->quick_push (token);
651 /* Get the remaining tokens from the preprocessor. */
652 while (token.type != CPP_EOF)
654 cp_lexer_get_preprocessor_token (lexer, &token);
655 vec_safe_push (lexer->buffer, token);
658 lexer->last_token = lexer->buffer->address ()
659 + lexer->buffer->length ()
660 - 1;
661 lexer->next_token = lexer->buffer->length ()
662 ? lexer->buffer->address ()
663 : &eof_token;
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
667 done_lexing = true;
669 gcc_assert (!lexer->next_token->purged_p);
670 return lexer;
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
676 static cp_lexer *
677 cp_lexer_new_from_tokens (cp_token_cache *cache)
679 cp_token *first = cache->first;
680 cp_token *last = cache->last;
681 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
683 /* We do not own the buffer. */
684 lexer->buffer = NULL;
685 lexer->next_token = first == last ? &eof_token : first;
686 lexer->last_token = last;
688 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
690 /* Initially we are not debugging. */
691 lexer->debugging_p = false;
693 gcc_assert (!lexer->next_token->purged_p);
694 return lexer;
697 /* Frees all resources associated with LEXER. */
699 static void
700 cp_lexer_destroy (cp_lexer *lexer)
702 vec_free (lexer->buffer);
703 lexer->saved_tokens.release ();
704 ggc_free (lexer);
707 /* Returns nonzero if debugging information should be output. */
709 static inline bool
710 cp_lexer_debugging_p (cp_lexer *lexer)
712 return lexer->debugging_p;
716 static inline cp_token_position
717 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
719 gcc_assert (!previous_p || lexer->next_token != &eof_token);
721 return lexer->next_token - previous_p;
724 static inline cp_token *
725 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
727 return pos;
730 static inline void
731 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
733 lexer->next_token = cp_lexer_token_at (lexer, pos);
736 static inline cp_token_position
737 cp_lexer_previous_token_position (cp_lexer *lexer)
739 if (lexer->next_token == &eof_token)
740 return lexer->last_token - 1;
741 else
742 return cp_lexer_token_position (lexer, true);
745 static inline cp_token *
746 cp_lexer_previous_token (cp_lexer *lexer)
748 cp_token_position tp = cp_lexer_previous_token_position (lexer);
750 return cp_lexer_token_at (lexer, tp);
753 /* nonzero if we are presently saving tokens. */
755 static inline int
756 cp_lexer_saving_tokens (const cp_lexer* lexer)
758 return lexer->saved_tokens.length () != 0;
761 /* Store the next token from the preprocessor in *TOKEN. Return true
762 if we reach EOF. If LEXER is NULL, assume we are handling an
763 initial #pragma pch_preprocess, and thus want the lexer to return
764 processed strings. */
766 static void
767 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
769 static int is_extern_c = 0;
771 /* Get a new token from the preprocessor. */
772 token->type
773 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
774 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
775 token->keyword = RID_MAX;
776 token->pragma_kind = PRAGMA_NONE;
777 token->purged_p = false;
778 token->error_reported = false;
780 /* On some systems, some header files are surrounded by an
781 implicit extern "C" block. Set a flag in the token if it
782 comes from such a header. */
783 is_extern_c += pending_lang_change;
784 pending_lang_change = 0;
785 token->implicit_extern_c = is_extern_c > 0;
787 /* Check to see if this token is a keyword. */
788 if (token->type == CPP_NAME)
790 if (C_IS_RESERVED_WORD (token->u.value))
792 /* Mark this token as a keyword. */
793 token->type = CPP_KEYWORD;
794 /* Record which keyword. */
795 token->keyword = C_RID_CODE (token->u.value);
797 else
799 if (warn_cxx0x_compat
800 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
801 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
803 /* Warn about the C++0x keyword (but still treat it as
804 an identifier). */
805 warning (OPT_Wc__0x_compat,
806 "identifier %qE is a keyword in C++11",
807 token->u.value);
809 /* Clear out the C_RID_CODE so we don't warn about this
810 particular identifier-turned-keyword again. */
811 C_SET_RID_CODE (token->u.value, RID_MAX);
814 token->keyword = RID_MAX;
817 else if (token->type == CPP_AT_NAME)
819 /* This only happens in Objective-C++; it must be a keyword. */
820 token->type = CPP_KEYWORD;
821 switch (C_RID_CODE (token->u.value))
823 /* Replace 'class' with '@class', 'private' with '@private',
824 etc. This prevents confusion with the C++ keyword
825 'class', and makes the tokens consistent with other
826 Objective-C 'AT' keywords. For example '@class' is
827 reported as RID_AT_CLASS which is consistent with
828 '@synchronized', which is reported as
829 RID_AT_SYNCHRONIZED.
831 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
832 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
833 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
834 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
835 case RID_THROW: token->keyword = RID_AT_THROW; break;
836 case RID_TRY: token->keyword = RID_AT_TRY; break;
837 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
838 default: token->keyword = C_RID_CODE (token->u.value);
841 else if (token->type == CPP_PRAGMA)
843 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
844 token->pragma_kind = ((enum pragma_kind)
845 TREE_INT_CST_LOW (token->u.value));
846 token->u.value = NULL_TREE;
850 /* Update the globals input_location and the input file stack from TOKEN. */
851 static inline void
852 cp_lexer_set_source_position_from_token (cp_token *token)
854 if (token->type != CPP_EOF)
856 input_location = token->location;
860 /* Update the globals input_location and the input file stack from LEXER. */
861 static inline void
862 cp_lexer_set_source_position (cp_lexer *lexer)
864 cp_token *token = cp_lexer_peek_token (lexer);
865 cp_lexer_set_source_position_from_token (token);
868 /* Return a pointer to the next token in the token stream, but do not
869 consume it. */
871 static inline cp_token *
872 cp_lexer_peek_token (cp_lexer *lexer)
874 if (cp_lexer_debugging_p (lexer))
876 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
877 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
878 putc ('\n', cp_lexer_debug_stream);
880 return lexer->next_token;
883 /* Return true if the next token has the indicated TYPE. */
885 static inline bool
886 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
888 return cp_lexer_peek_token (lexer)->type == type;
891 /* Return true if the next token does not have the indicated TYPE. */
893 static inline bool
894 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
896 return !cp_lexer_next_token_is (lexer, type);
899 /* Return true if the next token is the indicated KEYWORD. */
901 static inline bool
902 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
904 return cp_lexer_peek_token (lexer)->keyword == keyword;
907 static inline bool
908 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
910 return cp_lexer_peek_nth_token (lexer, n)->type == type;
913 static inline bool
914 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
916 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
919 /* Return true if the next token is not the indicated KEYWORD. */
921 static inline bool
922 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
924 return cp_lexer_peek_token (lexer)->keyword != keyword;
927 /* Return true if the next token is a keyword for a decl-specifier. */
929 static bool
930 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
932 cp_token *token;
934 token = cp_lexer_peek_token (lexer);
935 switch (token->keyword)
937 /* auto specifier: storage-class-specifier in C++,
938 simple-type-specifier in C++0x. */
939 case RID_AUTO:
940 /* Storage classes. */
941 case RID_REGISTER:
942 case RID_STATIC:
943 case RID_EXTERN:
944 case RID_MUTABLE:
945 case RID_THREAD:
946 /* Elaborated type specifiers. */
947 case RID_ENUM:
948 case RID_CLASS:
949 case RID_STRUCT:
950 case RID_UNION:
951 case RID_TYPENAME:
952 /* Simple type specifiers. */
953 case RID_CHAR:
954 case RID_CHAR16:
955 case RID_CHAR32:
956 case RID_WCHAR:
957 case RID_BOOL:
958 case RID_SHORT:
959 case RID_INT:
960 case RID_LONG:
961 case RID_SIGNED:
962 case RID_UNSIGNED:
963 case RID_FLOAT:
964 case RID_DOUBLE:
965 case RID_VOID:
966 /* GNU extensions. */
967 case RID_ATTRIBUTE:
968 case RID_TYPEOF:
969 /* C++0x extensions. */
970 case RID_DECLTYPE:
971 case RID_UNDERLYING_TYPE:
972 return true;
974 default:
975 if (token->keyword >= RID_FIRST_INT_N
976 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
977 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
978 return true;
979 return false;
983 /* Returns TRUE iff the token T begins a decltype type. */
985 static bool
986 token_is_decltype (cp_token *t)
988 return (t->keyword == RID_DECLTYPE
989 || t->type == CPP_DECLTYPE);
992 /* Returns TRUE iff the next token begins a decltype type. */
994 static bool
995 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
997 cp_token *t = cp_lexer_peek_token (lexer);
998 return token_is_decltype (t);
1001 /* Return a pointer to the Nth token in the token stream. If N is 1,
1002 then this is precisely equivalent to cp_lexer_peek_token (except
1003 that it is not inline). One would like to disallow that case, but
1004 there is one case (cp_parser_nth_token_starts_template_id) where
1005 the caller passes a variable for N and it might be 1. */
1007 static cp_token *
1008 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1010 cp_token *token;
1012 /* N is 1-based, not zero-based. */
1013 gcc_assert (n > 0);
1015 if (cp_lexer_debugging_p (lexer))
1016 fprintf (cp_lexer_debug_stream,
1017 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1019 --n;
1020 token = lexer->next_token;
1021 gcc_assert (!n || token != &eof_token);
1022 while (n != 0)
1024 ++token;
1025 if (token == lexer->last_token)
1027 token = &eof_token;
1028 break;
1031 if (!token->purged_p)
1032 --n;
1035 if (cp_lexer_debugging_p (lexer))
1037 cp_lexer_print_token (cp_lexer_debug_stream, token);
1038 putc ('\n', cp_lexer_debug_stream);
1041 return token;
1044 /* Return the next token, and advance the lexer's next_token pointer
1045 to point to the next non-purged token. */
1047 static cp_token *
1048 cp_lexer_consume_token (cp_lexer* lexer)
1050 cp_token *token = lexer->next_token;
1052 gcc_assert (token != &eof_token);
1053 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1057 lexer->next_token++;
1058 if (lexer->next_token == lexer->last_token)
1060 lexer->next_token = &eof_token;
1061 break;
1065 while (lexer->next_token->purged_p);
1067 cp_lexer_set_source_position_from_token (token);
1069 /* Provide debugging output. */
1070 if (cp_lexer_debugging_p (lexer))
1072 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1073 cp_lexer_print_token (cp_lexer_debug_stream, token);
1074 putc ('\n', cp_lexer_debug_stream);
1077 return token;
1080 /* Permanently remove the next token from the token stream, and
1081 advance the next_token pointer to refer to the next non-purged
1082 token. */
1084 static void
1085 cp_lexer_purge_token (cp_lexer *lexer)
1087 cp_token *tok = lexer->next_token;
1089 gcc_assert (tok != &eof_token);
1090 tok->purged_p = true;
1091 tok->location = UNKNOWN_LOCATION;
1092 tok->u.value = NULL_TREE;
1093 tok->keyword = RID_MAX;
1097 tok++;
1098 if (tok == lexer->last_token)
1100 tok = &eof_token;
1101 break;
1104 while (tok->purged_p);
1105 lexer->next_token = tok;
1108 /* Permanently remove all tokens after TOK, up to, but not
1109 including, the token that will be returned next by
1110 cp_lexer_peek_token. */
1112 static void
1113 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1115 cp_token *peek = lexer->next_token;
1117 if (peek == &eof_token)
1118 peek = lexer->last_token;
1120 gcc_assert (tok < peek);
1122 for ( tok += 1; tok != peek; tok += 1)
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1131 /* Begin saving tokens. All tokens consumed after this point will be
1132 preserved. */
1134 static void
1135 cp_lexer_save_tokens (cp_lexer* lexer)
1137 /* Provide debugging output. */
1138 if (cp_lexer_debugging_p (lexer))
1139 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1141 lexer->saved_tokens.safe_push (lexer->next_token);
1144 /* Commit to the portion of the token stream most recently saved. */
1146 static void
1147 cp_lexer_commit_tokens (cp_lexer* lexer)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer))
1151 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1153 lexer->saved_tokens.pop ();
1156 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1157 to the token stream. Stop saving tokens. */
1159 static void
1160 cp_lexer_rollback_tokens (cp_lexer* lexer)
1162 /* Provide debugging output. */
1163 if (cp_lexer_debugging_p (lexer))
1164 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1166 lexer->next_token = lexer->saved_tokens.pop ();
1169 /* RAII wrapper around the above functions, with sanity checking. Creating
1170 a variable saves tokens, which are committed when the variable is
1171 destroyed unless they are explicitly rolled back by calling the rollback
1172 member function. */
1174 struct saved_token_sentinel
1176 cp_lexer *lexer;
1177 unsigned len;
1178 bool commit;
1179 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1181 len = lexer->saved_tokens.length ();
1182 cp_lexer_save_tokens (lexer);
1184 void rollback ()
1186 cp_lexer_rollback_tokens (lexer);
1187 commit = false;
1189 ~saved_token_sentinel()
1191 if (commit)
1192 cp_lexer_commit_tokens (lexer);
1193 gcc_assert (lexer->saved_tokens.length () == len);
1197 /* Print a representation of the TOKEN on the STREAM. */
1199 static void
1200 cp_lexer_print_token (FILE * stream, cp_token *token)
1202 /* We don't use cpp_type2name here because the parser defines
1203 a few tokens of its own. */
1204 static const char *const token_names[] = {
1205 /* cpplib-defined token types */
1206 #define OP(e, s) #e,
1207 #define TK(e, s) #e,
1208 TTYPE_TABLE
1209 #undef OP
1210 #undef TK
1211 /* C++ parser token types - see "Manifest constants", above. */
1212 "KEYWORD",
1213 "TEMPLATE_ID",
1214 "NESTED_NAME_SPECIFIER",
1217 /* For some tokens, print the associated data. */
1218 switch (token->type)
1220 case CPP_KEYWORD:
1221 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1222 For example, `struct' is mapped to an INTEGER_CST. */
1223 if (!identifier_p (token->u.value))
1224 break;
1225 /* else fall through */
1226 case CPP_NAME:
1227 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1228 break;
1230 case CPP_STRING:
1231 case CPP_STRING16:
1232 case CPP_STRING32:
1233 case CPP_WSTRING:
1234 case CPP_UTF8STRING:
1235 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1236 break;
1238 case CPP_NUMBER:
1239 print_generic_expr (stream, token->u.value, 0);
1240 break;
1242 default:
1243 /* If we have a name for the token, print it out. Otherwise, we
1244 simply give the numeric code. */
1245 if (token->type < ARRAY_SIZE(token_names))
1246 fputs (token_names[token->type], stream);
1247 else
1248 fprintf (stream, "[%d]", token->type);
1249 break;
1253 DEBUG_FUNCTION void
1254 debug (cp_token &ref)
1256 cp_lexer_print_token (stderr, &ref);
1257 fprintf (stderr, "\n");
1260 DEBUG_FUNCTION void
1261 debug (cp_token *ptr)
1263 if (ptr)
1264 debug (*ptr);
1265 else
1266 fprintf (stderr, "<nil>\n");
1270 /* Start emitting debugging information. */
1272 static void
1273 cp_lexer_start_debugging (cp_lexer* lexer)
1275 lexer->debugging_p = true;
1276 cp_lexer_debug_stream = stderr;
1279 /* Stop emitting debugging information. */
1281 static void
1282 cp_lexer_stop_debugging (cp_lexer* lexer)
1284 lexer->debugging_p = false;
1285 cp_lexer_debug_stream = NULL;
1288 /* Create a new cp_token_cache, representing a range of tokens. */
1290 static cp_token_cache *
1291 cp_token_cache_new (cp_token *first, cp_token *last)
1293 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1294 cache->first = first;
1295 cache->last = last;
1296 return cache;
1299 /* Diagnose if #pragma omp declare simd isn't followed immediately
1300 by function declaration or definition. */
1302 static inline void
1303 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1305 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1307 error ("%<#pragma omp declare simd%> not immediately followed by "
1308 "function declaration or definition");
1309 parser->omp_declare_simd = NULL;
1313 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1314 and put that into "omp declare simd" attribute. */
1316 static inline void
1317 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1319 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1321 if (fndecl == error_mark_node)
1323 parser->omp_declare_simd = NULL;
1324 return;
1326 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1328 cp_ensure_no_omp_declare_simd (parser);
1329 return;
1334 /* Decl-specifiers. */
1336 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1338 static void
1339 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1341 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1344 /* Declarators. */
1346 /* Nothing other than the parser should be creating declarators;
1347 declarators are a semi-syntactic representation of C++ entities.
1348 Other parts of the front end that need to create entities (like
1349 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1351 static cp_declarator *make_call_declarator
1352 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1353 static cp_declarator *make_array_declarator
1354 (cp_declarator *, tree);
1355 static cp_declarator *make_pointer_declarator
1356 (cp_cv_quals, cp_declarator *, tree);
1357 static cp_declarator *make_reference_declarator
1358 (cp_cv_quals, cp_declarator *, bool, tree);
1359 static cp_parameter_declarator *make_parameter_declarator
1360 (cp_decl_specifier_seq *, cp_declarator *, tree);
1361 static cp_declarator *make_ptrmem_declarator
1362 (cp_cv_quals, tree, cp_declarator *, tree);
1364 /* An erroneous declarator. */
1365 static cp_declarator *cp_error_declarator;
1367 /* The obstack on which declarators and related data structures are
1368 allocated. */
1369 static struct obstack declarator_obstack;
1371 /* Alloc BYTES from the declarator memory pool. */
1373 static inline void *
1374 alloc_declarator (size_t bytes)
1376 return obstack_alloc (&declarator_obstack, bytes);
1379 /* Allocate a declarator of the indicated KIND. Clear fields that are
1380 common to all declarators. */
1382 static cp_declarator *
1383 make_declarator (cp_declarator_kind kind)
1385 cp_declarator *declarator;
1387 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1388 declarator->kind = kind;
1389 declarator->attributes = NULL_TREE;
1390 declarator->std_attributes = NULL_TREE;
1391 declarator->declarator = NULL;
1392 declarator->parameter_pack_p = false;
1393 declarator->id_loc = UNKNOWN_LOCATION;
1395 return declarator;
1398 /* Make a declarator for a generalized identifier. If
1399 QUALIFYING_SCOPE is non-NULL, the identifier is
1400 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1401 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1402 is, if any. */
1404 static cp_declarator *
1405 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1406 special_function_kind sfk)
1408 cp_declarator *declarator;
1410 /* It is valid to write:
1412 class C { void f(); };
1413 typedef C D;
1414 void D::f();
1416 The standard is not clear about whether `typedef const C D' is
1417 legal; as of 2002-09-15 the committee is considering that
1418 question. EDG 3.0 allows that syntax. Therefore, we do as
1419 well. */
1420 if (qualifying_scope && TYPE_P (qualifying_scope))
1421 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1423 gcc_assert (identifier_p (unqualified_name)
1424 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1425 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1427 declarator = make_declarator (cdk_id);
1428 declarator->u.id.qualifying_scope = qualifying_scope;
1429 declarator->u.id.unqualified_name = unqualified_name;
1430 declarator->u.id.sfk = sfk;
1432 return declarator;
1435 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1436 of modifiers such as const or volatile to apply to the pointer
1437 type, represented as identifiers. ATTRIBUTES represent the attributes that
1438 appertain to the pointer or reference. */
1440 cp_declarator *
1441 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1442 tree attributes)
1444 cp_declarator *declarator;
1446 declarator = make_declarator (cdk_pointer);
1447 declarator->declarator = target;
1448 declarator->u.pointer.qualifiers = cv_qualifiers;
1449 declarator->u.pointer.class_type = NULL_TREE;
1450 if (target)
1452 declarator->id_loc = target->id_loc;
1453 declarator->parameter_pack_p = target->parameter_pack_p;
1454 target->parameter_pack_p = false;
1456 else
1457 declarator->parameter_pack_p = false;
1459 declarator->std_attributes = attributes;
1461 return declarator;
1464 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1465 represent the attributes that appertain to the pointer or
1466 reference. */
1468 cp_declarator *
1469 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1470 bool rvalue_ref, tree attributes)
1472 cp_declarator *declarator;
1474 declarator = make_declarator (cdk_reference);
1475 declarator->declarator = target;
1476 declarator->u.reference.qualifiers = cv_qualifiers;
1477 declarator->u.reference.rvalue_ref = rvalue_ref;
1478 if (target)
1480 declarator->id_loc = target->id_loc;
1481 declarator->parameter_pack_p = target->parameter_pack_p;
1482 target->parameter_pack_p = false;
1484 else
1485 declarator->parameter_pack_p = false;
1487 declarator->std_attributes = attributes;
1489 return declarator;
1492 /* Like make_pointer_declarator -- but for a pointer to a non-static
1493 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1494 appertain to the pointer or reference. */
1496 cp_declarator *
1497 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1498 cp_declarator *pointee,
1499 tree attributes)
1501 cp_declarator *declarator;
1503 declarator = make_declarator (cdk_ptrmem);
1504 declarator->declarator = pointee;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = class_type;
1508 if (pointee)
1510 declarator->parameter_pack_p = pointee->parameter_pack_p;
1511 pointee->parameter_pack_p = false;
1513 else
1514 declarator->parameter_pack_p = false;
1516 declarator->std_attributes = attributes;
1518 return declarator;
1521 /* Make a declarator for the function given by TARGET, with the
1522 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1523 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1524 indicates what exceptions can be thrown. */
1526 cp_declarator *
1527 make_call_declarator (cp_declarator *target,
1528 tree parms,
1529 cp_cv_quals cv_qualifiers,
1530 cp_virt_specifiers virt_specifiers,
1531 cp_ref_qualifier ref_qualifier,
1532 tree exception_specification,
1533 tree late_return_type)
1535 cp_declarator *declarator;
1537 declarator = make_declarator (cdk_function);
1538 declarator->declarator = target;
1539 declarator->u.function.parameters = parms;
1540 declarator->u.function.qualifiers = cv_qualifiers;
1541 declarator->u.function.virt_specifiers = virt_specifiers;
1542 declarator->u.function.ref_qualifier = ref_qualifier;
1543 declarator->u.function.exception_specification = exception_specification;
1544 declarator->u.function.late_return_type = late_return_type;
1545 if (target)
1547 declarator->id_loc = target->id_loc;
1548 declarator->parameter_pack_p = target->parameter_pack_p;
1549 target->parameter_pack_p = false;
1551 else
1552 declarator->parameter_pack_p = false;
1554 return declarator;
1557 /* Make a declarator for an array of BOUNDS elements, each of which is
1558 defined by ELEMENT. */
1560 cp_declarator *
1561 make_array_declarator (cp_declarator *element, tree bounds)
1563 cp_declarator *declarator;
1565 declarator = make_declarator (cdk_array);
1566 declarator->declarator = element;
1567 declarator->u.array.bounds = bounds;
1568 if (element)
1570 declarator->id_loc = element->id_loc;
1571 declarator->parameter_pack_p = element->parameter_pack_p;
1572 element->parameter_pack_p = false;
1574 else
1575 declarator->parameter_pack_p = false;
1577 return declarator;
1580 /* Determine whether the declarator we've seen so far can be a
1581 parameter pack, when followed by an ellipsis. */
1582 static bool
1583 declarator_can_be_parameter_pack (cp_declarator *declarator)
1585 /* Search for a declarator name, or any other declarator that goes
1586 after the point where the ellipsis could appear in a parameter
1587 pack. If we find any of these, then this declarator can not be
1588 made into a parameter pack. */
1589 bool found = false;
1590 while (declarator && !found)
1592 switch ((int)declarator->kind)
1594 case cdk_id:
1595 case cdk_array:
1596 found = true;
1597 break;
1599 case cdk_error:
1600 return true;
1602 default:
1603 declarator = declarator->declarator;
1604 break;
1608 return !found;
1611 cp_parameter_declarator *no_parameters;
1613 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1614 DECLARATOR and DEFAULT_ARGUMENT. */
1616 cp_parameter_declarator *
1617 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1618 cp_declarator *declarator,
1619 tree default_argument)
1621 cp_parameter_declarator *parameter;
1623 parameter = ((cp_parameter_declarator *)
1624 alloc_declarator (sizeof (cp_parameter_declarator)));
1625 parameter->next = NULL;
1626 if (decl_specifiers)
1627 parameter->decl_specifiers = *decl_specifiers;
1628 else
1629 clear_decl_specs (&parameter->decl_specifiers);
1630 parameter->declarator = declarator;
1631 parameter->default_argument = default_argument;
1632 parameter->ellipsis_p = false;
1634 return parameter;
1637 /* Returns true iff DECLARATOR is a declaration for a function. */
1639 static bool
1640 function_declarator_p (const cp_declarator *declarator)
1642 while (declarator)
1644 if (declarator->kind == cdk_function
1645 && declarator->declarator->kind == cdk_id)
1646 return true;
1647 if (declarator->kind == cdk_id
1648 || declarator->kind == cdk_error)
1649 return false;
1650 declarator = declarator->declarator;
1652 return false;
1655 /* The parser. */
1657 /* Overview
1658 --------
1660 A cp_parser parses the token stream as specified by the C++
1661 grammar. Its job is purely parsing, not semantic analysis. For
1662 example, the parser breaks the token stream into declarators,
1663 expressions, statements, and other similar syntactic constructs.
1664 It does not check that the types of the expressions on either side
1665 of an assignment-statement are compatible, or that a function is
1666 not declared with a parameter of type `void'.
1668 The parser invokes routines elsewhere in the compiler to perform
1669 semantic analysis and to build up the abstract syntax tree for the
1670 code processed.
1672 The parser (and the template instantiation code, which is, in a
1673 way, a close relative of parsing) are the only parts of the
1674 compiler that should be calling push_scope and pop_scope, or
1675 related functions. The parser (and template instantiation code)
1676 keeps track of what scope is presently active; everything else
1677 should simply honor that. (The code that generates static
1678 initializers may also need to set the scope, in order to check
1679 access control correctly when emitting the initializers.)
1681 Methodology
1682 -----------
1684 The parser is of the standard recursive-descent variety. Upcoming
1685 tokens in the token stream are examined in order to determine which
1686 production to use when parsing a non-terminal. Some C++ constructs
1687 require arbitrary look ahead to disambiguate. For example, it is
1688 impossible, in the general case, to tell whether a statement is an
1689 expression or declaration without scanning the entire statement.
1690 Therefore, the parser is capable of "parsing tentatively." When the
1691 parser is not sure what construct comes next, it enters this mode.
1692 Then, while we attempt to parse the construct, the parser queues up
1693 error messages, rather than issuing them immediately, and saves the
1694 tokens it consumes. If the construct is parsed successfully, the
1695 parser "commits", i.e., it issues any queued error messages and
1696 the tokens that were being preserved are permanently discarded.
1697 If, however, the construct is not parsed successfully, the parser
1698 rolls back its state completely so that it can resume parsing using
1699 a different alternative.
1701 Future Improvements
1702 -------------------
1704 The performance of the parser could probably be improved substantially.
1705 We could often eliminate the need to parse tentatively by looking ahead
1706 a little bit. In some places, this approach might not entirely eliminate
1707 the need to parse tentatively, but it might still speed up the average
1708 case. */
1710 /* Flags that are passed to some parsing functions. These values can
1711 be bitwise-ored together. */
1713 enum
1715 /* No flags. */
1716 CP_PARSER_FLAGS_NONE = 0x0,
1717 /* The construct is optional. If it is not present, then no error
1718 should be issued. */
1719 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1720 /* When parsing a type-specifier, treat user-defined type-names
1721 as non-type identifiers. */
1722 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1723 /* When parsing a type-specifier, do not try to parse a class-specifier
1724 or enum-specifier. */
1725 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1726 /* When parsing a decl-specifier-seq, only allow type-specifier or
1727 constexpr. */
1728 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1731 /* This type is used for parameters and variables which hold
1732 combinations of the above flags. */
1733 typedef int cp_parser_flags;
1735 /* The different kinds of declarators we want to parse. */
1737 typedef enum cp_parser_declarator_kind
1739 /* We want an abstract declarator. */
1740 CP_PARSER_DECLARATOR_ABSTRACT,
1741 /* We want a named declarator. */
1742 CP_PARSER_DECLARATOR_NAMED,
1743 /* We don't mind, but the name must be an unqualified-id. */
1744 CP_PARSER_DECLARATOR_EITHER
1745 } cp_parser_declarator_kind;
1747 /* The precedence values used to parse binary expressions. The minimum value
1748 of PREC must be 1, because zero is reserved to quickly discriminate
1749 binary operators from other tokens. */
1751 enum cp_parser_prec
1753 PREC_NOT_OPERATOR,
1754 PREC_LOGICAL_OR_EXPRESSION,
1755 PREC_LOGICAL_AND_EXPRESSION,
1756 PREC_INCLUSIVE_OR_EXPRESSION,
1757 PREC_EXCLUSIVE_OR_EXPRESSION,
1758 PREC_AND_EXPRESSION,
1759 PREC_EQUALITY_EXPRESSION,
1760 PREC_RELATIONAL_EXPRESSION,
1761 PREC_SHIFT_EXPRESSION,
1762 PREC_ADDITIVE_EXPRESSION,
1763 PREC_MULTIPLICATIVE_EXPRESSION,
1764 PREC_PM_EXPRESSION,
1765 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1768 /* A mapping from a token type to a corresponding tree node type, with a
1769 precedence value. */
1771 typedef struct cp_parser_binary_operations_map_node
1773 /* The token type. */
1774 enum cpp_ttype token_type;
1775 /* The corresponding tree code. */
1776 enum tree_code tree_type;
1777 /* The precedence of this operator. */
1778 enum cp_parser_prec prec;
1779 } cp_parser_binary_operations_map_node;
1781 typedef struct cp_parser_expression_stack_entry
1783 /* Left hand side of the binary operation we are currently
1784 parsing. */
1785 tree lhs;
1786 /* Original tree code for left hand side, if it was a binary
1787 expression itself (used for -Wparentheses). */
1788 enum tree_code lhs_type;
1789 /* Tree code for the binary operation we are parsing. */
1790 enum tree_code tree_type;
1791 /* Precedence of the binary operation we are parsing. */
1792 enum cp_parser_prec prec;
1793 /* Location of the binary operation we are parsing. */
1794 location_t loc;
1795 } cp_parser_expression_stack_entry;
1797 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1798 entries because precedence levels on the stack are monotonically
1799 increasing. */
1800 typedef struct cp_parser_expression_stack_entry
1801 cp_parser_expression_stack[NUM_PREC_VALUES];
1803 /* Prototypes. */
1805 /* Constructors and destructors. */
1807 static cp_parser_context *cp_parser_context_new
1808 (cp_parser_context *);
1810 /* Class variables. */
1812 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1814 /* The operator-precedence table used by cp_parser_binary_expression.
1815 Transformed into an associative array (binops_by_token) by
1816 cp_parser_new. */
1818 static const cp_parser_binary_operations_map_node binops[] = {
1819 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1820 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1822 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1823 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1824 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1826 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1827 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1829 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1830 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1832 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1833 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1834 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1835 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1837 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1838 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1840 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1842 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1844 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1846 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1848 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1851 /* The same as binops, but initialized by cp_parser_new so that
1852 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1853 for speed. */
1854 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1856 /* Constructors and destructors. */
1858 /* Construct a new context. The context below this one on the stack
1859 is given by NEXT. */
1861 static cp_parser_context *
1862 cp_parser_context_new (cp_parser_context* next)
1864 cp_parser_context *context;
1866 /* Allocate the storage. */
1867 if (cp_parser_context_free_list != NULL)
1869 /* Pull the first entry from the free list. */
1870 context = cp_parser_context_free_list;
1871 cp_parser_context_free_list = context->next;
1872 memset (context, 0, sizeof (*context));
1874 else
1875 context = ggc_cleared_alloc<cp_parser_context> ();
1877 /* No errors have occurred yet in this context. */
1878 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1879 /* If this is not the bottommost context, copy information that we
1880 need from the previous context. */
1881 if (next)
1883 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1884 expression, then we are parsing one in this context, too. */
1885 context->object_type = next->object_type;
1886 /* Thread the stack. */
1887 context->next = next;
1890 return context;
1893 /* Managing the unparsed function queues. */
1895 #define unparsed_funs_with_default_args \
1896 parser->unparsed_queues->last ().funs_with_default_args
1897 #define unparsed_funs_with_definitions \
1898 parser->unparsed_queues->last ().funs_with_definitions
1899 #define unparsed_nsdmis \
1900 parser->unparsed_queues->last ().nsdmis
1901 #define unparsed_classes \
1902 parser->unparsed_queues->last ().classes
1904 static void
1905 push_unparsed_function_queues (cp_parser *parser)
1907 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1908 vec_safe_push (parser->unparsed_queues, e);
1911 static void
1912 pop_unparsed_function_queues (cp_parser *parser)
1914 release_tree_vector (unparsed_funs_with_definitions);
1915 parser->unparsed_queues->pop ();
1918 /* Prototypes. */
1920 /* Constructors and destructors. */
1922 static cp_parser *cp_parser_new
1923 (void);
1925 /* Routines to parse various constructs.
1927 Those that return `tree' will return the error_mark_node (rather
1928 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1929 Sometimes, they will return an ordinary node if error-recovery was
1930 attempted, even though a parse error occurred. So, to check
1931 whether or not a parse error occurred, you should always use
1932 cp_parser_error_occurred. If the construct is optional (indicated
1933 either by an `_opt' in the name of the function that does the
1934 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1935 the construct is not present. */
1937 /* Lexical conventions [gram.lex] */
1939 static tree cp_parser_identifier
1940 (cp_parser *);
1941 static tree cp_parser_string_literal
1942 (cp_parser *, bool, bool, bool);
1943 static tree cp_parser_userdef_char_literal
1944 (cp_parser *);
1945 static tree cp_parser_userdef_string_literal
1946 (tree);
1947 static tree cp_parser_userdef_numeric_literal
1948 (cp_parser *);
1950 /* Basic concepts [gram.basic] */
1952 static bool cp_parser_translation_unit
1953 (cp_parser *);
1955 /* Expressions [gram.expr] */
1957 static tree cp_parser_primary_expression
1958 (cp_parser *, bool, bool, bool, cp_id_kind *);
1959 static tree cp_parser_id_expression
1960 (cp_parser *, bool, bool, bool *, bool, bool);
1961 static tree cp_parser_unqualified_id
1962 (cp_parser *, bool, bool, bool, bool);
1963 static tree cp_parser_nested_name_specifier_opt
1964 (cp_parser *, bool, bool, bool, bool);
1965 static tree cp_parser_nested_name_specifier
1966 (cp_parser *, bool, bool, bool, bool);
1967 static tree cp_parser_qualifying_entity
1968 (cp_parser *, bool, bool, bool, bool, bool);
1969 static tree cp_parser_postfix_expression
1970 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1971 static tree cp_parser_postfix_open_square_expression
1972 (cp_parser *, tree, bool, bool);
1973 static tree cp_parser_postfix_dot_deref_expression
1974 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1975 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1976 (cp_parser *, int, bool, bool, bool *, bool = false);
1977 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1978 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1979 static void cp_parser_pseudo_destructor_name
1980 (cp_parser *, tree, tree *, tree *);
1981 static tree cp_parser_unary_expression
1982 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1983 static enum tree_code cp_parser_unary_operator
1984 (cp_token *);
1985 static tree cp_parser_new_expression
1986 (cp_parser *);
1987 static vec<tree, va_gc> *cp_parser_new_placement
1988 (cp_parser *);
1989 static tree cp_parser_new_type_id
1990 (cp_parser *, tree *);
1991 static cp_declarator *cp_parser_new_declarator_opt
1992 (cp_parser *);
1993 static cp_declarator *cp_parser_direct_new_declarator
1994 (cp_parser *);
1995 static vec<tree, va_gc> *cp_parser_new_initializer
1996 (cp_parser *);
1997 static tree cp_parser_delete_expression
1998 (cp_parser *);
1999 static tree cp_parser_cast_expression
2000 (cp_parser *, bool, bool, bool, cp_id_kind *);
2001 static tree cp_parser_binary_expression
2002 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2003 static tree cp_parser_question_colon_clause
2004 (cp_parser *, tree);
2005 static tree cp_parser_assignment_expression
2006 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2007 static enum tree_code cp_parser_assignment_operator_opt
2008 (cp_parser *);
2009 static tree cp_parser_expression
2010 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2011 static tree cp_parser_constant_expression
2012 (cp_parser *, bool = false, bool * = NULL);
2013 static tree cp_parser_builtin_offsetof
2014 (cp_parser *);
2015 static tree cp_parser_lambda_expression
2016 (cp_parser *);
2017 static void cp_parser_lambda_introducer
2018 (cp_parser *, tree);
2019 static bool cp_parser_lambda_declarator_opt
2020 (cp_parser *, tree);
2021 static void cp_parser_lambda_body
2022 (cp_parser *, tree);
2024 /* Statements [gram.stmt.stmt] */
2026 static void cp_parser_statement
2027 (cp_parser *, tree, bool, bool *);
2028 static void cp_parser_label_for_labeled_statement
2029 (cp_parser *, tree);
2030 static tree cp_parser_expression_statement
2031 (cp_parser *, tree);
2032 static tree cp_parser_compound_statement
2033 (cp_parser *, tree, bool, bool);
2034 static void cp_parser_statement_seq_opt
2035 (cp_parser *, tree);
2036 static tree cp_parser_selection_statement
2037 (cp_parser *, bool *);
2038 static tree cp_parser_condition
2039 (cp_parser *);
2040 static tree cp_parser_iteration_statement
2041 (cp_parser *, bool);
2042 static bool cp_parser_for_init_statement
2043 (cp_parser *, tree *decl);
2044 static tree cp_parser_for
2045 (cp_parser *, bool);
2046 static tree cp_parser_c_for
2047 (cp_parser *, tree, tree, bool);
2048 static tree cp_parser_range_for
2049 (cp_parser *, tree, tree, tree, bool);
2050 static void do_range_for_auto_deduction
2051 (tree, tree);
2052 static tree cp_parser_perform_range_for_lookup
2053 (tree, tree *, tree *);
2054 static tree cp_parser_range_for_member_function
2055 (tree, tree);
2056 static tree cp_parser_jump_statement
2057 (cp_parser *);
2058 static void cp_parser_declaration_statement
2059 (cp_parser *);
2061 static tree cp_parser_implicitly_scoped_statement
2062 (cp_parser *, bool *);
2063 static void cp_parser_already_scoped_statement
2064 (cp_parser *);
2066 /* Declarations [gram.dcl.dcl] */
2068 static void cp_parser_declaration_seq_opt
2069 (cp_parser *);
2070 static void cp_parser_declaration
2071 (cp_parser *);
2072 static void cp_parser_block_declaration
2073 (cp_parser *, bool);
2074 static void cp_parser_simple_declaration
2075 (cp_parser *, bool, tree *);
2076 static void cp_parser_decl_specifier_seq
2077 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2078 static tree cp_parser_storage_class_specifier_opt
2079 (cp_parser *);
2080 static tree cp_parser_function_specifier_opt
2081 (cp_parser *, cp_decl_specifier_seq *);
2082 static tree cp_parser_type_specifier
2083 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2084 int *, bool *);
2085 static tree cp_parser_simple_type_specifier
2086 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2087 static tree cp_parser_type_name
2088 (cp_parser *);
2089 static tree cp_parser_nonclass_name
2090 (cp_parser* parser);
2091 static tree cp_parser_elaborated_type_specifier
2092 (cp_parser *, bool, bool);
2093 static tree cp_parser_enum_specifier
2094 (cp_parser *);
2095 static void cp_parser_enumerator_list
2096 (cp_parser *, tree);
2097 static void cp_parser_enumerator_definition
2098 (cp_parser *, tree);
2099 static tree cp_parser_namespace_name
2100 (cp_parser *);
2101 static void cp_parser_namespace_definition
2102 (cp_parser *);
2103 static void cp_parser_namespace_body
2104 (cp_parser *);
2105 static tree cp_parser_qualified_namespace_specifier
2106 (cp_parser *);
2107 static void cp_parser_namespace_alias_definition
2108 (cp_parser *);
2109 static bool cp_parser_using_declaration
2110 (cp_parser *, bool);
2111 static void cp_parser_using_directive
2112 (cp_parser *);
2113 static tree cp_parser_alias_declaration
2114 (cp_parser *);
2115 static void cp_parser_asm_definition
2116 (cp_parser *);
2117 static void cp_parser_linkage_specification
2118 (cp_parser *);
2119 static void cp_parser_static_assert
2120 (cp_parser *, bool);
2121 static tree cp_parser_decltype
2122 (cp_parser *);
2124 /* Declarators [gram.dcl.decl] */
2126 static tree cp_parser_init_declarator
2127 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2128 static cp_declarator *cp_parser_declarator
2129 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2130 static cp_declarator *cp_parser_direct_declarator
2131 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2132 static enum tree_code cp_parser_ptr_operator
2133 (cp_parser *, tree *, cp_cv_quals *, tree *);
2134 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2135 (cp_parser *);
2136 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2137 (cp_parser *);
2138 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2139 (cp_parser *);
2140 static tree cp_parser_late_return_type_opt
2141 (cp_parser *, cp_declarator *, cp_cv_quals);
2142 static tree cp_parser_declarator_id
2143 (cp_parser *, bool);
2144 static tree cp_parser_type_id
2145 (cp_parser *);
2146 static tree cp_parser_template_type_arg
2147 (cp_parser *);
2148 static tree cp_parser_trailing_type_id (cp_parser *);
2149 static tree cp_parser_type_id_1
2150 (cp_parser *, bool, bool);
2151 static void cp_parser_type_specifier_seq
2152 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2153 static tree cp_parser_parameter_declaration_clause
2154 (cp_parser *);
2155 static tree cp_parser_parameter_declaration_list
2156 (cp_parser *, bool *);
2157 static cp_parameter_declarator *cp_parser_parameter_declaration
2158 (cp_parser *, bool, bool *);
2159 static tree cp_parser_default_argument
2160 (cp_parser *, bool);
2161 static void cp_parser_function_body
2162 (cp_parser *, bool);
2163 static tree cp_parser_initializer
2164 (cp_parser *, bool *, bool *);
2165 static tree cp_parser_initializer_clause
2166 (cp_parser *, bool *);
2167 static tree cp_parser_braced_list
2168 (cp_parser*, bool*);
2169 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2170 (cp_parser *, bool *);
2172 static bool cp_parser_ctor_initializer_opt_and_function_body
2173 (cp_parser *, bool);
2175 static tree cp_parser_late_parsing_omp_declare_simd
2176 (cp_parser *, tree);
2178 static tree cp_parser_late_parsing_cilk_simd_fn_info
2179 (cp_parser *, tree);
2181 static tree synthesize_implicit_template_parm
2182 (cp_parser *);
2183 static tree finish_fully_implicit_template
2184 (cp_parser *, tree);
2186 /* Classes [gram.class] */
2188 static tree cp_parser_class_name
2189 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2190 static tree cp_parser_class_specifier
2191 (cp_parser *);
2192 static tree cp_parser_class_head
2193 (cp_parser *, bool *);
2194 static enum tag_types cp_parser_class_key
2195 (cp_parser *);
2196 static void cp_parser_type_parameter_key
2197 (cp_parser* parser);
2198 static void cp_parser_member_specification_opt
2199 (cp_parser *);
2200 static void cp_parser_member_declaration
2201 (cp_parser *);
2202 static tree cp_parser_pure_specifier
2203 (cp_parser *);
2204 static tree cp_parser_constant_initializer
2205 (cp_parser *);
2207 /* Derived classes [gram.class.derived] */
2209 static tree cp_parser_base_clause
2210 (cp_parser *);
2211 static tree cp_parser_base_specifier
2212 (cp_parser *);
2214 /* Special member functions [gram.special] */
2216 static tree cp_parser_conversion_function_id
2217 (cp_parser *);
2218 static tree cp_parser_conversion_type_id
2219 (cp_parser *);
2220 static cp_declarator *cp_parser_conversion_declarator_opt
2221 (cp_parser *);
2222 static bool cp_parser_ctor_initializer_opt
2223 (cp_parser *);
2224 static void cp_parser_mem_initializer_list
2225 (cp_parser *);
2226 static tree cp_parser_mem_initializer
2227 (cp_parser *);
2228 static tree cp_parser_mem_initializer_id
2229 (cp_parser *);
2231 /* Overloading [gram.over] */
2233 static tree cp_parser_operator_function_id
2234 (cp_parser *);
2235 static tree cp_parser_operator
2236 (cp_parser *);
2238 /* Templates [gram.temp] */
2240 static void cp_parser_template_declaration
2241 (cp_parser *, bool);
2242 static tree cp_parser_template_parameter_list
2243 (cp_parser *);
2244 static tree cp_parser_template_parameter
2245 (cp_parser *, bool *, bool *);
2246 static tree cp_parser_type_parameter
2247 (cp_parser *, bool *);
2248 static tree cp_parser_template_id
2249 (cp_parser *, bool, bool, enum tag_types, bool);
2250 static tree cp_parser_template_name
2251 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2252 static tree cp_parser_template_argument_list
2253 (cp_parser *);
2254 static tree cp_parser_template_argument
2255 (cp_parser *);
2256 static void cp_parser_explicit_instantiation
2257 (cp_parser *);
2258 static void cp_parser_explicit_specialization
2259 (cp_parser *);
2261 /* Exception handling [gram.exception] */
2263 static tree cp_parser_try_block
2264 (cp_parser *);
2265 static bool cp_parser_function_try_block
2266 (cp_parser *);
2267 static void cp_parser_handler_seq
2268 (cp_parser *);
2269 static void cp_parser_handler
2270 (cp_parser *);
2271 static tree cp_parser_exception_declaration
2272 (cp_parser *);
2273 static tree cp_parser_throw_expression
2274 (cp_parser *);
2275 static tree cp_parser_exception_specification_opt
2276 (cp_parser *);
2277 static tree cp_parser_type_id_list
2278 (cp_parser *);
2280 /* GNU Extensions */
2282 static tree cp_parser_asm_specification_opt
2283 (cp_parser *);
2284 static tree cp_parser_asm_operand_list
2285 (cp_parser *);
2286 static tree cp_parser_asm_clobber_list
2287 (cp_parser *);
2288 static tree cp_parser_asm_label_list
2289 (cp_parser *);
2290 static bool cp_next_tokens_can_be_attribute_p
2291 (cp_parser *);
2292 static bool cp_next_tokens_can_be_gnu_attribute_p
2293 (cp_parser *);
2294 static bool cp_next_tokens_can_be_std_attribute_p
2295 (cp_parser *);
2296 static bool cp_nth_tokens_can_be_std_attribute_p
2297 (cp_parser *, size_t);
2298 static bool cp_nth_tokens_can_be_gnu_attribute_p
2299 (cp_parser *, size_t);
2300 static bool cp_nth_tokens_can_be_attribute_p
2301 (cp_parser *, size_t);
2302 static tree cp_parser_attributes_opt
2303 (cp_parser *);
2304 static tree cp_parser_gnu_attributes_opt
2305 (cp_parser *);
2306 static tree cp_parser_gnu_attribute_list
2307 (cp_parser *);
2308 static tree cp_parser_std_attribute
2309 (cp_parser *);
2310 static tree cp_parser_std_attribute_spec
2311 (cp_parser *);
2312 static tree cp_parser_std_attribute_spec_seq
2313 (cp_parser *);
2314 static bool cp_parser_extension_opt
2315 (cp_parser *, int *);
2316 static void cp_parser_label_declaration
2317 (cp_parser *);
2319 /* Transactional Memory Extensions */
2321 static tree cp_parser_transaction
2322 (cp_parser *, enum rid);
2323 static tree cp_parser_transaction_expression
2324 (cp_parser *, enum rid);
2325 static bool cp_parser_function_transaction
2326 (cp_parser *, enum rid);
2327 static tree cp_parser_transaction_cancel
2328 (cp_parser *);
2330 enum pragma_context {
2331 pragma_external,
2332 pragma_member,
2333 pragma_objc_icode,
2334 pragma_stmt,
2335 pragma_compound
2337 static bool cp_parser_pragma
2338 (cp_parser *, enum pragma_context);
2340 /* Objective-C++ Productions */
2342 static tree cp_parser_objc_message_receiver
2343 (cp_parser *);
2344 static tree cp_parser_objc_message_args
2345 (cp_parser *);
2346 static tree cp_parser_objc_message_expression
2347 (cp_parser *);
2348 static tree cp_parser_objc_encode_expression
2349 (cp_parser *);
2350 static tree cp_parser_objc_defs_expression
2351 (cp_parser *);
2352 static tree cp_parser_objc_protocol_expression
2353 (cp_parser *);
2354 static tree cp_parser_objc_selector_expression
2355 (cp_parser *);
2356 static tree cp_parser_objc_expression
2357 (cp_parser *);
2358 static bool cp_parser_objc_selector_p
2359 (enum cpp_ttype);
2360 static tree cp_parser_objc_selector
2361 (cp_parser *);
2362 static tree cp_parser_objc_protocol_refs_opt
2363 (cp_parser *);
2364 static void cp_parser_objc_declaration
2365 (cp_parser *, tree);
2366 static tree cp_parser_objc_statement
2367 (cp_parser *);
2368 static bool cp_parser_objc_valid_prefix_attributes
2369 (cp_parser *, tree *);
2370 static void cp_parser_objc_at_property_declaration
2371 (cp_parser *) ;
2372 static void cp_parser_objc_at_synthesize_declaration
2373 (cp_parser *) ;
2374 static void cp_parser_objc_at_dynamic_declaration
2375 (cp_parser *) ;
2376 static tree cp_parser_objc_struct_declaration
2377 (cp_parser *) ;
2379 /* Utility Routines */
2381 static tree cp_parser_lookup_name
2382 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2383 static tree cp_parser_lookup_name_simple
2384 (cp_parser *, tree, location_t);
2385 static tree cp_parser_maybe_treat_template_as_class
2386 (tree, bool);
2387 static bool cp_parser_check_declarator_template_parameters
2388 (cp_parser *, cp_declarator *, location_t);
2389 static bool cp_parser_check_template_parameters
2390 (cp_parser *, unsigned, location_t, cp_declarator *);
2391 static tree cp_parser_simple_cast_expression
2392 (cp_parser *);
2393 static tree cp_parser_global_scope_opt
2394 (cp_parser *, bool);
2395 static bool cp_parser_constructor_declarator_p
2396 (cp_parser *, bool);
2397 static tree cp_parser_function_definition_from_specifiers_and_declarator
2398 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2399 static tree cp_parser_function_definition_after_declarator
2400 (cp_parser *, bool);
2401 static void cp_parser_template_declaration_after_export
2402 (cp_parser *, bool);
2403 static void cp_parser_perform_template_parameter_access_checks
2404 (vec<deferred_access_check, va_gc> *);
2405 static tree cp_parser_single_declaration
2406 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2407 static tree cp_parser_functional_cast
2408 (cp_parser *, tree);
2409 static tree cp_parser_save_member_function_body
2410 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2411 static tree cp_parser_save_nsdmi
2412 (cp_parser *);
2413 static tree cp_parser_enclosed_template_argument_list
2414 (cp_parser *);
2415 static void cp_parser_save_default_args
2416 (cp_parser *, tree);
2417 static void cp_parser_late_parsing_for_member
2418 (cp_parser *, tree);
2419 static tree cp_parser_late_parse_one_default_arg
2420 (cp_parser *, tree, tree, tree);
2421 static void cp_parser_late_parsing_nsdmi
2422 (cp_parser *, tree);
2423 static void cp_parser_late_parsing_default_args
2424 (cp_parser *, tree);
2425 static tree cp_parser_sizeof_operand
2426 (cp_parser *, enum rid);
2427 static tree cp_parser_trait_expr
2428 (cp_parser *, enum rid);
2429 static bool cp_parser_declares_only_class_p
2430 (cp_parser *);
2431 static void cp_parser_set_storage_class
2432 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2433 static void cp_parser_set_decl_spec_type
2434 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2435 static void set_and_check_decl_spec_loc
2436 (cp_decl_specifier_seq *decl_specs,
2437 cp_decl_spec ds, cp_token *);
2438 static bool cp_parser_friend_p
2439 (const cp_decl_specifier_seq *);
2440 static void cp_parser_required_error
2441 (cp_parser *, required_token, bool);
2442 static cp_token *cp_parser_require
2443 (cp_parser *, enum cpp_ttype, required_token);
2444 static cp_token *cp_parser_require_keyword
2445 (cp_parser *, enum rid, required_token);
2446 static bool cp_parser_token_starts_function_definition_p
2447 (cp_token *);
2448 static bool cp_parser_next_token_starts_class_definition_p
2449 (cp_parser *);
2450 static bool cp_parser_next_token_ends_template_argument_p
2451 (cp_parser *);
2452 static bool cp_parser_nth_token_starts_template_argument_list_p
2453 (cp_parser *, size_t);
2454 static enum tag_types cp_parser_token_is_class_key
2455 (cp_token *);
2456 static enum tag_types cp_parser_token_is_type_parameter_key
2457 (cp_token *);
2458 static void cp_parser_check_class_key
2459 (enum tag_types, tree type);
2460 static void cp_parser_check_access_in_redeclaration
2461 (tree type, location_t location);
2462 static bool cp_parser_optional_template_keyword
2463 (cp_parser *);
2464 static void cp_parser_pre_parsed_nested_name_specifier
2465 (cp_parser *);
2466 static bool cp_parser_cache_group
2467 (cp_parser *, enum cpp_ttype, unsigned);
2468 static tree cp_parser_cache_defarg
2469 (cp_parser *parser, bool nsdmi);
2470 static void cp_parser_parse_tentatively
2471 (cp_parser *);
2472 static void cp_parser_commit_to_tentative_parse
2473 (cp_parser *);
2474 static void cp_parser_commit_to_topmost_tentative_parse
2475 (cp_parser *);
2476 static void cp_parser_abort_tentative_parse
2477 (cp_parser *);
2478 static bool cp_parser_parse_definitely
2479 (cp_parser *);
2480 static inline bool cp_parser_parsing_tentatively
2481 (cp_parser *);
2482 static bool cp_parser_uncommitted_to_tentative_parse_p
2483 (cp_parser *);
2484 static void cp_parser_error
2485 (cp_parser *, const char *);
2486 static void cp_parser_name_lookup_error
2487 (cp_parser *, tree, tree, name_lookup_error, location_t);
2488 static bool cp_parser_simulate_error
2489 (cp_parser *);
2490 static bool cp_parser_check_type_definition
2491 (cp_parser *);
2492 static void cp_parser_check_for_definition_in_return_type
2493 (cp_declarator *, tree, location_t type_location);
2494 static void cp_parser_check_for_invalid_template_id
2495 (cp_parser *, tree, enum tag_types, location_t location);
2496 static bool cp_parser_non_integral_constant_expression
2497 (cp_parser *, non_integral_constant);
2498 static void cp_parser_diagnose_invalid_type_name
2499 (cp_parser *, tree, location_t);
2500 static bool cp_parser_parse_and_diagnose_invalid_type_name
2501 (cp_parser *);
2502 static int cp_parser_skip_to_closing_parenthesis
2503 (cp_parser *, bool, bool, bool);
2504 static void cp_parser_skip_to_end_of_statement
2505 (cp_parser *);
2506 static void cp_parser_consume_semicolon_at_end_of_statement
2507 (cp_parser *);
2508 static void cp_parser_skip_to_end_of_block_or_statement
2509 (cp_parser *);
2510 static bool cp_parser_skip_to_closing_brace
2511 (cp_parser *);
2512 static void cp_parser_skip_to_end_of_template_parameter_list
2513 (cp_parser *);
2514 static void cp_parser_skip_to_pragma_eol
2515 (cp_parser*, cp_token *);
2516 static bool cp_parser_error_occurred
2517 (cp_parser *);
2518 static bool cp_parser_allow_gnu_extensions_p
2519 (cp_parser *);
2520 static bool cp_parser_is_pure_string_literal
2521 (cp_token *);
2522 static bool cp_parser_is_string_literal
2523 (cp_token *);
2524 static bool cp_parser_is_keyword
2525 (cp_token *, enum rid);
2526 static tree cp_parser_make_typename_type
2527 (cp_parser *, tree, location_t location);
2528 static cp_declarator * cp_parser_make_indirect_declarator
2529 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2530 static bool cp_parser_compound_literal_p
2531 (cp_parser *);
2532 static bool cp_parser_array_designator_p
2533 (cp_parser *);
2534 static bool cp_parser_skip_to_closing_square_bracket
2535 (cp_parser *);
2537 /* Returns nonzero if we are parsing tentatively. */
2539 static inline bool
2540 cp_parser_parsing_tentatively (cp_parser* parser)
2542 return parser->context->next != NULL;
2545 /* Returns nonzero if TOKEN is a string literal. */
2547 static bool
2548 cp_parser_is_pure_string_literal (cp_token* token)
2550 return (token->type == CPP_STRING ||
2551 token->type == CPP_STRING16 ||
2552 token->type == CPP_STRING32 ||
2553 token->type == CPP_WSTRING ||
2554 token->type == CPP_UTF8STRING);
2557 /* Returns nonzero if TOKEN is a string literal
2558 of a user-defined string literal. */
2560 static bool
2561 cp_parser_is_string_literal (cp_token* token)
2563 return (cp_parser_is_pure_string_literal (token) ||
2564 token->type == CPP_STRING_USERDEF ||
2565 token->type == CPP_STRING16_USERDEF ||
2566 token->type == CPP_STRING32_USERDEF ||
2567 token->type == CPP_WSTRING_USERDEF ||
2568 token->type == CPP_UTF8STRING_USERDEF);
2571 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2573 static bool
2574 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2576 return token->keyword == keyword;
2579 /* If not parsing tentatively, issue a diagnostic of the form
2580 FILE:LINE: MESSAGE before TOKEN
2581 where TOKEN is the next token in the input stream. MESSAGE
2582 (specified by the caller) is usually of the form "expected
2583 OTHER-TOKEN". */
2585 static void
2586 cp_parser_error (cp_parser* parser, const char* gmsgid)
2588 if (!cp_parser_simulate_error (parser))
2590 cp_token *token = cp_lexer_peek_token (parser->lexer);
2591 /* This diagnostic makes more sense if it is tagged to the line
2592 of the token we just peeked at. */
2593 cp_lexer_set_source_position_from_token (token);
2595 if (token->type == CPP_PRAGMA)
2597 error_at (token->location,
2598 "%<#pragma%> is not allowed here");
2599 cp_parser_skip_to_pragma_eol (parser, token);
2600 return;
2603 c_parse_error (gmsgid,
2604 /* Because c_parser_error does not understand
2605 CPP_KEYWORD, keywords are treated like
2606 identifiers. */
2607 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2608 token->u.value, token->flags);
2612 /* Issue an error about name-lookup failing. NAME is the
2613 IDENTIFIER_NODE DECL is the result of
2614 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2615 the thing that we hoped to find. */
2617 static void
2618 cp_parser_name_lookup_error (cp_parser* parser,
2619 tree name,
2620 tree decl,
2621 name_lookup_error desired,
2622 location_t location)
2624 /* If name lookup completely failed, tell the user that NAME was not
2625 declared. */
2626 if (decl == error_mark_node)
2628 if (parser->scope && parser->scope != global_namespace)
2629 error_at (location, "%<%E::%E%> has not been declared",
2630 parser->scope, name);
2631 else if (parser->scope == global_namespace)
2632 error_at (location, "%<::%E%> has not been declared", name);
2633 else if (parser->object_scope
2634 && !CLASS_TYPE_P (parser->object_scope))
2635 error_at (location, "request for member %qE in non-class type %qT",
2636 name, parser->object_scope);
2637 else if (parser->object_scope)
2638 error_at (location, "%<%T::%E%> has not been declared",
2639 parser->object_scope, name);
2640 else
2641 error_at (location, "%qE has not been declared", name);
2643 else if (parser->scope && parser->scope != global_namespace)
2645 switch (desired)
2647 case NLE_TYPE:
2648 error_at (location, "%<%E::%E%> is not a type",
2649 parser->scope, name);
2650 break;
2651 case NLE_CXX98:
2652 error_at (location, "%<%E::%E%> is not a class or namespace",
2653 parser->scope, name);
2654 break;
2655 case NLE_NOT_CXX98:
2656 error_at (location,
2657 "%<%E::%E%> is not a class, namespace, or enumeration",
2658 parser->scope, name);
2659 break;
2660 default:
2661 gcc_unreachable ();
2665 else if (parser->scope == global_namespace)
2667 switch (desired)
2669 case NLE_TYPE:
2670 error_at (location, "%<::%E%> is not a type", name);
2671 break;
2672 case NLE_CXX98:
2673 error_at (location, "%<::%E%> is not a class or namespace", name);
2674 break;
2675 case NLE_NOT_CXX98:
2676 error_at (location,
2677 "%<::%E%> is not a class, namespace, or enumeration",
2678 name);
2679 break;
2680 default:
2681 gcc_unreachable ();
2684 else
2686 switch (desired)
2688 case NLE_TYPE:
2689 error_at (location, "%qE is not a type", name);
2690 break;
2691 case NLE_CXX98:
2692 error_at (location, "%qE is not a class or namespace", name);
2693 break;
2694 case NLE_NOT_CXX98:
2695 error_at (location,
2696 "%qE is not a class, namespace, or enumeration", name);
2697 break;
2698 default:
2699 gcc_unreachable ();
2704 /* If we are parsing tentatively, remember that an error has occurred
2705 during this tentative parse. Returns true if the error was
2706 simulated; false if a message should be issued by the caller. */
2708 static bool
2709 cp_parser_simulate_error (cp_parser* parser)
2711 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2713 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2714 return true;
2716 return false;
2719 /* This function is called when a type is defined. If type
2720 definitions are forbidden at this point, an error message is
2721 issued. */
2723 static bool
2724 cp_parser_check_type_definition (cp_parser* parser)
2726 /* If types are forbidden here, issue a message. */
2727 if (parser->type_definition_forbidden_message)
2729 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2730 in the message need to be interpreted. */
2731 error (parser->type_definition_forbidden_message);
2732 return false;
2734 return true;
2737 /* This function is called when the DECLARATOR is processed. The TYPE
2738 was a type defined in the decl-specifiers. If it is invalid to
2739 define a type in the decl-specifiers for DECLARATOR, an error is
2740 issued. TYPE_LOCATION is the location of TYPE and is used
2741 for error reporting. */
2743 static void
2744 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2745 tree type, location_t type_location)
2747 /* [dcl.fct] forbids type definitions in return types.
2748 Unfortunately, it's not easy to know whether or not we are
2749 processing a return type until after the fact. */
2750 while (declarator
2751 && (declarator->kind == cdk_pointer
2752 || declarator->kind == cdk_reference
2753 || declarator->kind == cdk_ptrmem))
2754 declarator = declarator->declarator;
2755 if (declarator
2756 && declarator->kind == cdk_function)
2758 error_at (type_location,
2759 "new types may not be defined in a return type");
2760 inform (type_location,
2761 "(perhaps a semicolon is missing after the definition of %qT)",
2762 type);
2766 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2767 "<" in any valid C++ program. If the next token is indeed "<",
2768 issue a message warning the user about what appears to be an
2769 invalid attempt to form a template-id. LOCATION is the location
2770 of the type-specifier (TYPE) */
2772 static void
2773 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2774 tree type,
2775 enum tag_types tag_type,
2776 location_t location)
2778 cp_token_position start = 0;
2780 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2782 if (TYPE_P (type))
2783 error_at (location, "%qT is not a template", type);
2784 else if (identifier_p (type))
2786 if (tag_type != none_type)
2787 error_at (location, "%qE is not a class template", type);
2788 else
2789 error_at (location, "%qE is not a template", type);
2791 else
2792 error_at (location, "invalid template-id");
2793 /* Remember the location of the invalid "<". */
2794 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2795 start = cp_lexer_token_position (parser->lexer, true);
2796 /* Consume the "<". */
2797 cp_lexer_consume_token (parser->lexer);
2798 /* Parse the template arguments. */
2799 cp_parser_enclosed_template_argument_list (parser);
2800 /* Permanently remove the invalid template arguments so that
2801 this error message is not issued again. */
2802 if (start)
2803 cp_lexer_purge_tokens_after (parser->lexer, start);
2807 /* If parsing an integral constant-expression, issue an error message
2808 about the fact that THING appeared and return true. Otherwise,
2809 return false. In either case, set
2810 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2812 static bool
2813 cp_parser_non_integral_constant_expression (cp_parser *parser,
2814 non_integral_constant thing)
2816 parser->non_integral_constant_expression_p = true;
2817 if (parser->integral_constant_expression_p)
2819 if (!parser->allow_non_integral_constant_expression_p)
2821 const char *msg = NULL;
2822 switch (thing)
2824 case NIC_FLOAT:
2825 error ("floating-point literal "
2826 "cannot appear in a constant-expression");
2827 return true;
2828 case NIC_CAST:
2829 error ("a cast to a type other than an integral or "
2830 "enumeration type cannot appear in a "
2831 "constant-expression");
2832 return true;
2833 case NIC_TYPEID:
2834 error ("%<typeid%> operator "
2835 "cannot appear in a constant-expression");
2836 return true;
2837 case NIC_NCC:
2838 error ("non-constant compound literals "
2839 "cannot appear in a constant-expression");
2840 return true;
2841 case NIC_FUNC_CALL:
2842 error ("a function call "
2843 "cannot appear in a constant-expression");
2844 return true;
2845 case NIC_INC:
2846 error ("an increment "
2847 "cannot appear in a constant-expression");
2848 return true;
2849 case NIC_DEC:
2850 error ("an decrement "
2851 "cannot appear in a constant-expression");
2852 return true;
2853 case NIC_ARRAY_REF:
2854 error ("an array reference "
2855 "cannot appear in a constant-expression");
2856 return true;
2857 case NIC_ADDR_LABEL:
2858 error ("the address of a label "
2859 "cannot appear in a constant-expression");
2860 return true;
2861 case NIC_OVERLOADED:
2862 error ("calls to overloaded operators "
2863 "cannot appear in a constant-expression");
2864 return true;
2865 case NIC_ASSIGNMENT:
2866 error ("an assignment cannot appear in a constant-expression");
2867 return true;
2868 case NIC_COMMA:
2869 error ("a comma operator "
2870 "cannot appear in a constant-expression");
2871 return true;
2872 case NIC_CONSTRUCTOR:
2873 error ("a call to a constructor "
2874 "cannot appear in a constant-expression");
2875 return true;
2876 case NIC_TRANSACTION:
2877 error ("a transaction expression "
2878 "cannot appear in a constant-expression");
2879 return true;
2880 case NIC_THIS:
2881 msg = "this";
2882 break;
2883 case NIC_FUNC_NAME:
2884 msg = "__FUNCTION__";
2885 break;
2886 case NIC_PRETTY_FUNC:
2887 msg = "__PRETTY_FUNCTION__";
2888 break;
2889 case NIC_C99_FUNC:
2890 msg = "__func__";
2891 break;
2892 case NIC_VA_ARG:
2893 msg = "va_arg";
2894 break;
2895 case NIC_ARROW:
2896 msg = "->";
2897 break;
2898 case NIC_POINT:
2899 msg = ".";
2900 break;
2901 case NIC_STAR:
2902 msg = "*";
2903 break;
2904 case NIC_ADDR:
2905 msg = "&";
2906 break;
2907 case NIC_PREINCREMENT:
2908 msg = "++";
2909 break;
2910 case NIC_PREDECREMENT:
2911 msg = "--";
2912 break;
2913 case NIC_NEW:
2914 msg = "new";
2915 break;
2916 case NIC_DEL:
2917 msg = "delete";
2918 break;
2919 default:
2920 gcc_unreachable ();
2922 if (msg)
2923 error ("%qs cannot appear in a constant-expression", msg);
2924 return true;
2927 return false;
2930 /* Emit a diagnostic for an invalid type name. This function commits
2931 to the current active tentative parse, if any. (Otherwise, the
2932 problematic construct might be encountered again later, resulting
2933 in duplicate error messages.) LOCATION is the location of ID. */
2935 static void
2936 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2937 location_t location)
2939 tree decl, ambiguous_decls;
2940 cp_parser_commit_to_tentative_parse (parser);
2941 /* Try to lookup the identifier. */
2942 decl = cp_parser_lookup_name (parser, id, none_type,
2943 /*is_template=*/false,
2944 /*is_namespace=*/false,
2945 /*check_dependency=*/true,
2946 &ambiguous_decls, location);
2947 if (ambiguous_decls)
2948 /* If the lookup was ambiguous, an error will already have
2949 been issued. */
2950 return;
2951 /* If the lookup found a template-name, it means that the user forgot
2952 to specify an argument list. Emit a useful error message. */
2953 if (TREE_CODE (decl) == TEMPLATE_DECL)
2954 error_at (location,
2955 "invalid use of template-name %qE without an argument list",
2956 decl);
2957 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2958 error_at (location, "invalid use of destructor %qD as a type", id);
2959 else if (TREE_CODE (decl) == TYPE_DECL)
2960 /* Something like 'unsigned A a;' */
2961 error_at (location, "invalid combination of multiple type-specifiers");
2962 else if (!parser->scope)
2964 /* Issue an error message. */
2965 error_at (location, "%qE does not name a type", id);
2966 /* If we're in a template class, it's possible that the user was
2967 referring to a type from a base class. For example:
2969 template <typename T> struct A { typedef T X; };
2970 template <typename T> struct B : public A<T> { X x; };
2972 The user should have said "typename A<T>::X". */
2973 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2974 inform (location, "C++11 %<constexpr%> only available with "
2975 "-std=c++11 or -std=gnu++11");
2976 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2977 inform (location, "C++11 %<noexcept%> only available with "
2978 "-std=c++11 or -std=gnu++11");
2979 else if (cxx_dialect < cxx11
2980 && TREE_CODE (id) == IDENTIFIER_NODE
2981 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2982 inform (location, "C++11 %<thread_local%> only available with "
2983 "-std=c++11 or -std=gnu++11");
2984 else if (processing_template_decl && current_class_type
2985 && TYPE_BINFO (current_class_type))
2987 tree b;
2989 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2991 b = TREE_CHAIN (b))
2993 tree base_type = BINFO_TYPE (b);
2994 if (CLASS_TYPE_P (base_type)
2995 && dependent_type_p (base_type))
2997 tree field;
2998 /* Go from a particular instantiation of the
2999 template (which will have an empty TYPE_FIELDs),
3000 to the main version. */
3001 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3002 for (field = TYPE_FIELDS (base_type);
3003 field;
3004 field = DECL_CHAIN (field))
3005 if (TREE_CODE (field) == TYPE_DECL
3006 && DECL_NAME (field) == id)
3008 inform (location,
3009 "(perhaps %<typename %T::%E%> was intended)",
3010 BINFO_TYPE (b), id);
3011 break;
3013 if (field)
3014 break;
3019 /* Here we diagnose qualified-ids where the scope is actually correct,
3020 but the identifier does not resolve to a valid type name. */
3021 else if (parser->scope != error_mark_node)
3023 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3025 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3026 error_at (location_of (id),
3027 "%qE in namespace %qE does not name a template type",
3028 id, parser->scope);
3029 else
3030 error_at (location_of (id),
3031 "%qE in namespace %qE does not name a type",
3032 id, parser->scope);
3034 else if (CLASS_TYPE_P (parser->scope)
3035 && constructor_name_p (id, parser->scope))
3037 /* A<T>::A<T>() */
3038 error_at (location, "%<%T::%E%> names the constructor, not"
3039 " the type", parser->scope, id);
3040 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3041 error_at (location, "and %qT has no template constructors",
3042 parser->scope);
3044 else if (TYPE_P (parser->scope)
3045 && dependent_scope_p (parser->scope))
3046 error_at (location, "need %<typename%> before %<%T::%E%> because "
3047 "%qT is a dependent scope",
3048 parser->scope, id, parser->scope);
3049 else if (TYPE_P (parser->scope))
3051 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3052 error_at (location_of (id),
3053 "%qE in %q#T does not name a template type",
3054 id, parser->scope);
3055 else
3056 error_at (location_of (id),
3057 "%qE in %q#T does not name a type",
3058 id, parser->scope);
3060 else
3061 gcc_unreachable ();
3065 /* Check for a common situation where a type-name should be present,
3066 but is not, and issue a sensible error message. Returns true if an
3067 invalid type-name was detected.
3069 The situation handled by this function are variable declarations of the
3070 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3071 Usually, `ID' should name a type, but if we got here it means that it
3072 does not. We try to emit the best possible error message depending on
3073 how exactly the id-expression looks like. */
3075 static bool
3076 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3078 tree id;
3079 cp_token *token = cp_lexer_peek_token (parser->lexer);
3081 /* Avoid duplicate error about ambiguous lookup. */
3082 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3084 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3085 if (next->type == CPP_NAME && next->error_reported)
3086 goto out;
3089 cp_parser_parse_tentatively (parser);
3090 id = cp_parser_id_expression (parser,
3091 /*template_keyword_p=*/false,
3092 /*check_dependency_p=*/true,
3093 /*template_p=*/NULL,
3094 /*declarator_p=*/true,
3095 /*optional_p=*/false);
3096 /* If the next token is a (, this is a function with no explicit return
3097 type, i.e. constructor, destructor or conversion op. */
3098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3099 || TREE_CODE (id) == TYPE_DECL)
3101 cp_parser_abort_tentative_parse (parser);
3102 return false;
3104 if (!cp_parser_parse_definitely (parser))
3105 return false;
3107 /* Emit a diagnostic for the invalid type. */
3108 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3109 out:
3110 /* If we aren't in the middle of a declarator (i.e. in a
3111 parameter-declaration-clause), skip to the end of the declaration;
3112 there's no point in trying to process it. */
3113 if (!parser->in_declarator_p)
3114 cp_parser_skip_to_end_of_block_or_statement (parser);
3115 return true;
3118 /* Consume tokens up to, and including, the next non-nested closing `)'.
3119 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3120 are doing error recovery. Returns -1 if OR_COMMA is true and we
3121 found an unnested comma. */
3123 static int
3124 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3125 bool recovering,
3126 bool or_comma,
3127 bool consume_paren)
3129 unsigned paren_depth = 0;
3130 unsigned brace_depth = 0;
3131 unsigned square_depth = 0;
3133 if (recovering && !or_comma
3134 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3135 return 0;
3137 while (true)
3139 cp_token * token = cp_lexer_peek_token (parser->lexer);
3141 switch (token->type)
3143 case CPP_EOF:
3144 case CPP_PRAGMA_EOL:
3145 /* If we've run out of tokens, then there is no closing `)'. */
3146 return 0;
3148 /* This is good for lambda expression capture-lists. */
3149 case CPP_OPEN_SQUARE:
3150 ++square_depth;
3151 break;
3152 case CPP_CLOSE_SQUARE:
3153 if (!square_depth--)
3154 return 0;
3155 break;
3157 case CPP_SEMICOLON:
3158 /* This matches the processing in skip_to_end_of_statement. */
3159 if (!brace_depth)
3160 return 0;
3161 break;
3163 case CPP_OPEN_BRACE:
3164 ++brace_depth;
3165 break;
3166 case CPP_CLOSE_BRACE:
3167 if (!brace_depth--)
3168 return 0;
3169 break;
3171 case CPP_COMMA:
3172 if (recovering && or_comma && !brace_depth && !paren_depth
3173 && !square_depth)
3174 return -1;
3175 break;
3177 case CPP_OPEN_PAREN:
3178 if (!brace_depth)
3179 ++paren_depth;
3180 break;
3182 case CPP_CLOSE_PAREN:
3183 if (!brace_depth && !paren_depth--)
3185 if (consume_paren)
3186 cp_lexer_consume_token (parser->lexer);
3187 return 1;
3189 break;
3191 default:
3192 break;
3195 /* Consume the token. */
3196 cp_lexer_consume_token (parser->lexer);
3200 /* Consume tokens until we reach the end of the current statement.
3201 Normally, that will be just before consuming a `;'. However, if a
3202 non-nested `}' comes first, then we stop before consuming that. */
3204 static void
3205 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3207 unsigned nesting_depth = 0;
3209 /* Unwind generic function template scope if necessary. */
3210 if (parser->fully_implicit_function_template_p)
3211 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3213 while (true)
3215 cp_token *token = cp_lexer_peek_token (parser->lexer);
3217 switch (token->type)
3219 case CPP_EOF:
3220 case CPP_PRAGMA_EOL:
3221 /* If we've run out of tokens, stop. */
3222 return;
3224 case CPP_SEMICOLON:
3225 /* If the next token is a `;', we have reached the end of the
3226 statement. */
3227 if (!nesting_depth)
3228 return;
3229 break;
3231 case CPP_CLOSE_BRACE:
3232 /* If this is a non-nested '}', stop before consuming it.
3233 That way, when confronted with something like:
3235 { 3 + }
3237 we stop before consuming the closing '}', even though we
3238 have not yet reached a `;'. */
3239 if (nesting_depth == 0)
3240 return;
3242 /* If it is the closing '}' for a block that we have
3243 scanned, stop -- but only after consuming the token.
3244 That way given:
3246 void f g () { ... }
3247 typedef int I;
3249 we will stop after the body of the erroneously declared
3250 function, but before consuming the following `typedef'
3251 declaration. */
3252 if (--nesting_depth == 0)
3254 cp_lexer_consume_token (parser->lexer);
3255 return;
3258 case CPP_OPEN_BRACE:
3259 ++nesting_depth;
3260 break;
3262 default:
3263 break;
3266 /* Consume the token. */
3267 cp_lexer_consume_token (parser->lexer);
3271 /* This function is called at the end of a statement or declaration.
3272 If the next token is a semicolon, it is consumed; otherwise, error
3273 recovery is attempted. */
3275 static void
3276 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3278 /* Look for the trailing `;'. */
3279 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3281 /* If there is additional (erroneous) input, skip to the end of
3282 the statement. */
3283 cp_parser_skip_to_end_of_statement (parser);
3284 /* If the next token is now a `;', consume it. */
3285 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3286 cp_lexer_consume_token (parser->lexer);
3290 /* Skip tokens until we have consumed an entire block, or until we
3291 have consumed a non-nested `;'. */
3293 static void
3294 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3296 int nesting_depth = 0;
3298 /* Unwind generic function template scope if necessary. */
3299 if (parser->fully_implicit_function_template_p)
3300 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3302 while (nesting_depth >= 0)
3304 cp_token *token = cp_lexer_peek_token (parser->lexer);
3306 switch (token->type)
3308 case CPP_EOF:
3309 case CPP_PRAGMA_EOL:
3310 /* If we've run out of tokens, stop. */
3311 return;
3313 case CPP_SEMICOLON:
3314 /* Stop if this is an unnested ';'. */
3315 if (!nesting_depth)
3316 nesting_depth = -1;
3317 break;
3319 case CPP_CLOSE_BRACE:
3320 /* Stop if this is an unnested '}', or closes the outermost
3321 nesting level. */
3322 nesting_depth--;
3323 if (nesting_depth < 0)
3324 return;
3325 if (!nesting_depth)
3326 nesting_depth = -1;
3327 break;
3329 case CPP_OPEN_BRACE:
3330 /* Nest. */
3331 nesting_depth++;
3332 break;
3334 default:
3335 break;
3338 /* Consume the token. */
3339 cp_lexer_consume_token (parser->lexer);
3343 /* Skip tokens until a non-nested closing curly brace is the next
3344 token, or there are no more tokens. Return true in the first case,
3345 false otherwise. */
3347 static bool
3348 cp_parser_skip_to_closing_brace (cp_parser *parser)
3350 unsigned nesting_depth = 0;
3352 while (true)
3354 cp_token *token = cp_lexer_peek_token (parser->lexer);
3356 switch (token->type)
3358 case CPP_EOF:
3359 case CPP_PRAGMA_EOL:
3360 /* If we've run out of tokens, stop. */
3361 return false;
3363 case CPP_CLOSE_BRACE:
3364 /* If the next token is a non-nested `}', then we have reached
3365 the end of the current block. */
3366 if (nesting_depth-- == 0)
3367 return true;
3368 break;
3370 case CPP_OPEN_BRACE:
3371 /* If it the next token is a `{', then we are entering a new
3372 block. Consume the entire block. */
3373 ++nesting_depth;
3374 break;
3376 default:
3377 break;
3380 /* Consume the token. */
3381 cp_lexer_consume_token (parser->lexer);
3385 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3386 parameter is the PRAGMA token, allowing us to purge the entire pragma
3387 sequence. */
3389 static void
3390 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3392 cp_token *token;
3394 parser->lexer->in_pragma = false;
3397 token = cp_lexer_consume_token (parser->lexer);
3398 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3400 /* Ensure that the pragma is not parsed again. */
3401 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3404 /* Require pragma end of line, resyncing with it as necessary. The
3405 arguments are as for cp_parser_skip_to_pragma_eol. */
3407 static void
3408 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3410 parser->lexer->in_pragma = false;
3411 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3412 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3415 /* This is a simple wrapper around make_typename_type. When the id is
3416 an unresolved identifier node, we can provide a superior diagnostic
3417 using cp_parser_diagnose_invalid_type_name. */
3419 static tree
3420 cp_parser_make_typename_type (cp_parser *parser, tree id,
3421 location_t id_location)
3423 tree result;
3424 if (identifier_p (id))
3426 result = make_typename_type (parser->scope, id, typename_type,
3427 /*complain=*/tf_none);
3428 if (result == error_mark_node)
3429 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3430 return result;
3432 return make_typename_type (parser->scope, id, typename_type, tf_error);
3435 /* This is a wrapper around the
3436 make_{pointer,ptrmem,reference}_declarator functions that decides
3437 which one to call based on the CODE and CLASS_TYPE arguments. The
3438 CODE argument should be one of the values returned by
3439 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3440 appertain to the pointer or reference. */
3442 static cp_declarator *
3443 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3444 cp_cv_quals cv_qualifiers,
3445 cp_declarator *target,
3446 tree attributes)
3448 if (code == ERROR_MARK)
3449 return cp_error_declarator;
3451 if (code == INDIRECT_REF)
3452 if (class_type == NULL_TREE)
3453 return make_pointer_declarator (cv_qualifiers, target, attributes);
3454 else
3455 return make_ptrmem_declarator (cv_qualifiers, class_type,
3456 target, attributes);
3457 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3458 return make_reference_declarator (cv_qualifiers, target,
3459 false, attributes);
3460 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3461 return make_reference_declarator (cv_qualifiers, target,
3462 true, attributes);
3463 gcc_unreachable ();
3466 /* Create a new C++ parser. */
3468 static cp_parser *
3469 cp_parser_new (void)
3471 cp_parser *parser;
3472 cp_lexer *lexer;
3473 unsigned i;
3475 /* cp_lexer_new_main is called before doing GC allocation because
3476 cp_lexer_new_main might load a PCH file. */
3477 lexer = cp_lexer_new_main ();
3479 /* Initialize the binops_by_token so that we can get the tree
3480 directly from the token. */
3481 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3482 binops_by_token[binops[i].token_type] = binops[i];
3484 parser = ggc_cleared_alloc<cp_parser> ();
3485 parser->lexer = lexer;
3486 parser->context = cp_parser_context_new (NULL);
3488 /* For now, we always accept GNU extensions. */
3489 parser->allow_gnu_extensions_p = 1;
3491 /* The `>' token is a greater-than operator, not the end of a
3492 template-id. */
3493 parser->greater_than_is_operator_p = true;
3495 parser->default_arg_ok_p = true;
3497 /* We are not parsing a constant-expression. */
3498 parser->integral_constant_expression_p = false;
3499 parser->allow_non_integral_constant_expression_p = false;
3500 parser->non_integral_constant_expression_p = false;
3502 /* Local variable names are not forbidden. */
3503 parser->local_variables_forbidden_p = false;
3505 /* We are not processing an `extern "C"' declaration. */
3506 parser->in_unbraced_linkage_specification_p = false;
3508 /* We are not processing a declarator. */
3509 parser->in_declarator_p = false;
3511 /* We are not processing a template-argument-list. */
3512 parser->in_template_argument_list_p = false;
3514 /* We are not in an iteration statement. */
3515 parser->in_statement = 0;
3517 /* We are not in a switch statement. */
3518 parser->in_switch_statement_p = false;
3520 /* We are not parsing a type-id inside an expression. */
3521 parser->in_type_id_in_expr_p = false;
3523 /* Declarations aren't implicitly extern "C". */
3524 parser->implicit_extern_c = false;
3526 /* String literals should be translated to the execution character set. */
3527 parser->translate_strings_p = true;
3529 /* We are not parsing a function body. */
3530 parser->in_function_body = false;
3532 /* We can correct until told otherwise. */
3533 parser->colon_corrects_to_scope_p = true;
3535 /* The unparsed function queue is empty. */
3536 push_unparsed_function_queues (parser);
3538 /* There are no classes being defined. */
3539 parser->num_classes_being_defined = 0;
3541 /* No template parameters apply. */
3542 parser->num_template_parameter_lists = 0;
3544 /* Not declaring an implicit function template. */
3545 parser->auto_is_implicit_function_template_parm_p = false;
3546 parser->fully_implicit_function_template_p = false;
3547 parser->implicit_template_parms = 0;
3548 parser->implicit_template_scope = 0;
3550 return parser;
3553 /* Create a cp_lexer structure which will emit the tokens in CACHE
3554 and push it onto the parser's lexer stack. This is used for delayed
3555 parsing of in-class method bodies and default arguments, and should
3556 not be confused with tentative parsing. */
3557 static void
3558 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3560 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3561 lexer->next = parser->lexer;
3562 parser->lexer = lexer;
3564 /* Move the current source position to that of the first token in the
3565 new lexer. */
3566 cp_lexer_set_source_position_from_token (lexer->next_token);
3569 /* Pop the top lexer off the parser stack. This is never used for the
3570 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3571 static void
3572 cp_parser_pop_lexer (cp_parser *parser)
3574 cp_lexer *lexer = parser->lexer;
3575 parser->lexer = lexer->next;
3576 cp_lexer_destroy (lexer);
3578 /* Put the current source position back where it was before this
3579 lexer was pushed. */
3580 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3583 /* Lexical conventions [gram.lex] */
3585 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3586 identifier. */
3588 static tree
3589 cp_parser_identifier (cp_parser* parser)
3591 cp_token *token;
3593 /* Look for the identifier. */
3594 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3595 /* Return the value. */
3596 return token ? token->u.value : error_mark_node;
3599 /* Parse a sequence of adjacent string constants. Returns a
3600 TREE_STRING representing the combined, nul-terminated string
3601 constant. If TRANSLATE is true, translate the string to the
3602 execution character set. If WIDE_OK is true, a wide string is
3603 invalid here.
3605 C++98 [lex.string] says that if a narrow string literal token is
3606 adjacent to a wide string literal token, the behavior is undefined.
3607 However, C99 6.4.5p4 says that this results in a wide string literal.
3608 We follow C99 here, for consistency with the C front end.
3610 This code is largely lifted from lex_string() in c-lex.c.
3612 FUTURE: ObjC++ will need to handle @-strings here. */
3613 static tree
3614 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3615 bool lookup_udlit = true)
3617 tree value;
3618 size_t count;
3619 struct obstack str_ob;
3620 cpp_string str, istr, *strs;
3621 cp_token *tok;
3622 enum cpp_ttype type, curr_type;
3623 int have_suffix_p = 0;
3624 tree string_tree;
3625 tree suffix_id = NULL_TREE;
3626 bool curr_tok_is_userdef_p = false;
3628 tok = cp_lexer_peek_token (parser->lexer);
3629 if (!cp_parser_is_string_literal (tok))
3631 cp_parser_error (parser, "expected string-literal");
3632 return error_mark_node;
3635 if (cpp_userdef_string_p (tok->type))
3637 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3638 curr_type = cpp_userdef_string_remove_type (tok->type);
3639 curr_tok_is_userdef_p = true;
3641 else
3643 string_tree = tok->u.value;
3644 curr_type = tok->type;
3646 type = curr_type;
3648 /* Try to avoid the overhead of creating and destroying an obstack
3649 for the common case of just one string. */
3650 if (!cp_parser_is_string_literal
3651 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3653 cp_lexer_consume_token (parser->lexer);
3655 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3656 str.len = TREE_STRING_LENGTH (string_tree);
3657 count = 1;
3659 if (curr_tok_is_userdef_p)
3661 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3662 have_suffix_p = 1;
3663 curr_type = cpp_userdef_string_remove_type (tok->type);
3665 else
3666 curr_type = tok->type;
3668 strs = &str;
3670 else
3672 gcc_obstack_init (&str_ob);
3673 count = 0;
3677 cp_lexer_consume_token (parser->lexer);
3678 count++;
3679 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3680 str.len = TREE_STRING_LENGTH (string_tree);
3682 if (curr_tok_is_userdef_p)
3684 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3685 if (have_suffix_p == 0)
3687 suffix_id = curr_suffix_id;
3688 have_suffix_p = 1;
3690 else if (have_suffix_p == 1
3691 && curr_suffix_id != suffix_id)
3693 error ("inconsistent user-defined literal suffixes"
3694 " %qD and %qD in string literal",
3695 suffix_id, curr_suffix_id);
3696 have_suffix_p = -1;
3698 curr_type = cpp_userdef_string_remove_type (tok->type);
3700 else
3701 curr_type = tok->type;
3703 if (type != curr_type)
3705 if (type == CPP_STRING)
3706 type = curr_type;
3707 else if (curr_type != CPP_STRING)
3708 error_at (tok->location,
3709 "unsupported non-standard concatenation "
3710 "of string literals");
3713 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3715 tok = cp_lexer_peek_token (parser->lexer);
3716 if (cpp_userdef_string_p (tok->type))
3718 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3719 curr_type = cpp_userdef_string_remove_type (tok->type);
3720 curr_tok_is_userdef_p = true;
3722 else
3724 string_tree = tok->u.value;
3725 curr_type = tok->type;
3726 curr_tok_is_userdef_p = false;
3729 while (cp_parser_is_string_literal (tok));
3731 strs = (cpp_string *) obstack_finish (&str_ob);
3734 if (type != CPP_STRING && !wide_ok)
3736 cp_parser_error (parser, "a wide string is invalid in this context");
3737 type = CPP_STRING;
3740 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3741 (parse_in, strs, count, &istr, type))
3743 value = build_string (istr.len, (const char *)istr.text);
3744 free (CONST_CAST (unsigned char *, istr.text));
3746 switch (type)
3748 default:
3749 case CPP_STRING:
3750 case CPP_UTF8STRING:
3751 TREE_TYPE (value) = char_array_type_node;
3752 break;
3753 case CPP_STRING16:
3754 TREE_TYPE (value) = char16_array_type_node;
3755 break;
3756 case CPP_STRING32:
3757 TREE_TYPE (value) = char32_array_type_node;
3758 break;
3759 case CPP_WSTRING:
3760 TREE_TYPE (value) = wchar_array_type_node;
3761 break;
3764 value = fix_string_type (value);
3766 if (have_suffix_p)
3768 tree literal = build_userdef_literal (suffix_id, value,
3769 OT_NONE, NULL_TREE);
3770 if (lookup_udlit)
3771 value = cp_parser_userdef_string_literal (literal);
3772 else
3773 value = literal;
3776 else
3777 /* cpp_interpret_string has issued an error. */
3778 value = error_mark_node;
3780 if (count > 1)
3781 obstack_free (&str_ob, 0);
3783 return value;
3786 /* Look up a literal operator with the name and the exact arguments. */
3788 static tree
3789 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3791 tree decl, fns;
3792 decl = lookup_name (name);
3793 if (!decl || !is_overloaded_fn (decl))
3794 return error_mark_node;
3796 for (fns = decl; fns; fns = OVL_NEXT (fns))
3798 unsigned int ix;
3799 bool found = true;
3800 tree fn = OVL_CURRENT (fns);
3801 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3802 if (parmtypes != NULL_TREE)
3804 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3805 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3807 tree tparm = TREE_VALUE (parmtypes);
3808 tree targ = TREE_TYPE ((*args)[ix]);
3809 bool ptr = TYPE_PTR_P (tparm);
3810 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3811 if ((ptr || arr || !same_type_p (tparm, targ))
3812 && (!ptr || !arr
3813 || !same_type_p (TREE_TYPE (tparm),
3814 TREE_TYPE (targ))))
3815 found = false;
3817 if (found
3818 && ix == vec_safe_length (args)
3819 /* May be this should be sufficient_parms_p instead,
3820 depending on how exactly should user-defined literals
3821 work in presence of default arguments on the literal
3822 operator parameters. */
3823 && parmtypes == void_list_node)
3824 return fn;
3828 return error_mark_node;
3831 /* Parse a user-defined char constant. Returns a call to a user-defined
3832 literal operator taking the character as an argument. */
3834 static tree
3835 cp_parser_userdef_char_literal (cp_parser *parser)
3837 cp_token *token = cp_lexer_consume_token (parser->lexer);
3838 tree literal = token->u.value;
3839 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3840 tree value = USERDEF_LITERAL_VALUE (literal);
3841 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3842 tree decl, result;
3844 /* Build up a call to the user-defined operator */
3845 /* Lookup the name we got back from the id-expression. */
3846 vec<tree, va_gc> *args = make_tree_vector ();
3847 vec_safe_push (args, value);
3848 decl = lookup_literal_operator (name, args);
3849 if (!decl || decl == error_mark_node)
3851 error ("unable to find character literal operator %qD with %qT argument",
3852 name, TREE_TYPE (value));
3853 release_tree_vector (args);
3854 return error_mark_node;
3856 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3857 release_tree_vector (args);
3858 if (result != error_mark_node)
3859 return result;
3861 error ("unable to find character literal operator %qD with %qT argument",
3862 name, TREE_TYPE (value));
3863 return error_mark_node;
3866 /* A subroutine of cp_parser_userdef_numeric_literal to
3867 create a char... template parameter pack from a string node. */
3869 static tree
3870 make_char_string_pack (tree value)
3872 tree charvec;
3873 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3874 const char *str = TREE_STRING_POINTER (value);
3875 int i, len = TREE_STRING_LENGTH (value) - 1;
3876 tree argvec = make_tree_vec (1);
3878 /* Fill in CHARVEC with all of the parameters. */
3879 charvec = make_tree_vec (len);
3880 for (i = 0; i < len; ++i)
3881 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3883 /* Build the argument packs. */
3884 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3885 TREE_TYPE (argpack) = char_type_node;
3887 TREE_VEC_ELT (argvec, 0) = argpack;
3889 return argvec;
3892 /* A subroutine of cp_parser_userdef_numeric_literal to
3893 create a char... template parameter pack from a string node. */
3895 static tree
3896 make_string_pack (tree value)
3898 tree charvec;
3899 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3900 const unsigned char *str
3901 = (const unsigned char *) TREE_STRING_POINTER (value);
3902 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3903 int len = TREE_STRING_LENGTH (value) / sz - 1;
3904 tree argvec = make_tree_vec (2);
3906 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3907 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3909 /* First template parm is character type. */
3910 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3912 /* Fill in CHARVEC with all of the parameters. */
3913 charvec = make_tree_vec (len);
3914 for (int i = 0; i < len; ++i)
3915 TREE_VEC_ELT (charvec, i)
3916 = double_int_to_tree (str_char_type_node,
3917 double_int::from_buffer (str + i * sz, sz));
3919 /* Build the argument packs. */
3920 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3921 TREE_TYPE (argpack) = str_char_type_node;
3923 TREE_VEC_ELT (argvec, 1) = argpack;
3925 return argvec;
3928 /* Parse a user-defined numeric constant. returns a call to a user-defined
3929 literal operator. */
3931 static tree
3932 cp_parser_userdef_numeric_literal (cp_parser *parser)
3934 cp_token *token = cp_lexer_consume_token (parser->lexer);
3935 tree literal = token->u.value;
3936 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3937 tree value = USERDEF_LITERAL_VALUE (literal);
3938 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3939 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3940 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3941 tree decl, result;
3942 vec<tree, va_gc> *args;
3944 /* Look for a literal operator taking the exact type of numeric argument
3945 as the literal value. */
3946 args = make_tree_vector ();
3947 vec_safe_push (args, value);
3948 decl = lookup_literal_operator (name, args);
3949 if (decl && decl != error_mark_node)
3951 result = finish_call_expr (decl, &args, false, true, tf_none);
3952 if (result != error_mark_node)
3954 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3955 warning_at (token->location, OPT_Woverflow,
3956 "integer literal exceeds range of %qT type",
3957 long_long_unsigned_type_node);
3958 else
3960 if (overflow > 0)
3961 warning_at (token->location, OPT_Woverflow,
3962 "floating literal exceeds range of %qT type",
3963 long_double_type_node);
3964 else if (overflow < 0)
3965 warning_at (token->location, OPT_Woverflow,
3966 "floating literal truncated to zero");
3968 release_tree_vector (args);
3969 return result;
3972 release_tree_vector (args);
3974 /* If the numeric argument didn't work, look for a raw literal
3975 operator taking a const char* argument consisting of the number
3976 in string format. */
3977 args = make_tree_vector ();
3978 vec_safe_push (args, num_string);
3979 decl = lookup_literal_operator (name, args);
3980 if (decl && decl != error_mark_node)
3982 result = finish_call_expr (decl, &args, false, true, tf_none);
3983 if (result != error_mark_node)
3985 release_tree_vector (args);
3986 return result;
3989 release_tree_vector (args);
3991 /* If the raw literal didn't work, look for a non-type template
3992 function with parameter pack char.... Call the function with
3993 template parameter characters representing the number. */
3994 args = make_tree_vector ();
3995 decl = lookup_literal_operator (name, args);
3996 if (decl && decl != error_mark_node)
3998 tree tmpl_args = make_char_string_pack (num_string);
3999 decl = lookup_template_function (decl, tmpl_args);
4000 result = finish_call_expr (decl, &args, false, true, tf_none);
4001 if (result != error_mark_node)
4003 release_tree_vector (args);
4004 return result;
4007 release_tree_vector (args);
4009 error ("unable to find numeric literal operator %qD", name);
4010 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4011 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4012 "to enable more built-in suffixes");
4013 return error_mark_node;
4016 /* Parse a user-defined string constant. Returns a call to a user-defined
4017 literal operator taking a character pointer and the length of the string
4018 as arguments. */
4020 static tree
4021 cp_parser_userdef_string_literal (tree literal)
4023 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4024 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4025 tree value = USERDEF_LITERAL_VALUE (literal);
4026 int len = TREE_STRING_LENGTH (value)
4027 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4028 tree decl, result;
4029 vec<tree, va_gc> *args;
4031 /* Look for a template function with typename parameter CharT
4032 and parameter pack CharT... Call the function with
4033 template parameter characters representing the string. */
4034 args = make_tree_vector ();
4035 decl = lookup_literal_operator (name, args);
4036 if (decl && decl != error_mark_node)
4038 tree tmpl_args = make_string_pack (value);
4039 decl = lookup_template_function (decl, tmpl_args);
4040 result = finish_call_expr (decl, &args, false, true, tf_none);
4041 if (result != error_mark_node)
4043 release_tree_vector (args);
4044 return result;
4047 release_tree_vector (args);
4049 /* Build up a call to the user-defined operator */
4050 /* Lookup the name we got back from the id-expression. */
4051 args = make_tree_vector ();
4052 vec_safe_push (args, value);
4053 vec_safe_push (args, build_int_cst (size_type_node, len));
4054 decl = lookup_name (name);
4055 if (!decl || decl == error_mark_node)
4057 error ("unable to find string literal operator %qD", name);
4058 release_tree_vector (args);
4059 return error_mark_node;
4061 result = finish_call_expr (decl, &args, false, true, tf_none);
4062 release_tree_vector (args);
4063 if (result != error_mark_node)
4064 return result;
4066 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4067 name, TREE_TYPE (value), size_type_node);
4068 return error_mark_node;
4072 /* Basic concepts [gram.basic] */
4074 /* Parse a translation-unit.
4076 translation-unit:
4077 declaration-seq [opt]
4079 Returns TRUE if all went well. */
4081 static bool
4082 cp_parser_translation_unit (cp_parser* parser)
4084 /* The address of the first non-permanent object on the declarator
4085 obstack. */
4086 static void *declarator_obstack_base;
4088 bool success;
4090 /* Create the declarator obstack, if necessary. */
4091 if (!cp_error_declarator)
4093 gcc_obstack_init (&declarator_obstack);
4094 /* Create the error declarator. */
4095 cp_error_declarator = make_declarator (cdk_error);
4096 /* Create the empty parameter list. */
4097 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4098 /* Remember where the base of the declarator obstack lies. */
4099 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4102 cp_parser_declaration_seq_opt (parser);
4104 /* If there are no tokens left then all went well. */
4105 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4107 /* Get rid of the token array; we don't need it any more. */
4108 cp_lexer_destroy (parser->lexer);
4109 parser->lexer = NULL;
4111 /* This file might have been a context that's implicitly extern
4112 "C". If so, pop the lang context. (Only relevant for PCH.) */
4113 if (parser->implicit_extern_c)
4115 pop_lang_context ();
4116 parser->implicit_extern_c = false;
4119 /* Finish up. */
4120 finish_translation_unit ();
4122 success = true;
4124 else
4126 cp_parser_error (parser, "expected declaration");
4127 success = false;
4130 /* Make sure the declarator obstack was fully cleaned up. */
4131 gcc_assert (obstack_next_free (&declarator_obstack)
4132 == declarator_obstack_base);
4134 /* All went well. */
4135 return success;
4138 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4139 decltype context. */
4141 static inline tsubst_flags_t
4142 complain_flags (bool decltype_p)
4144 tsubst_flags_t complain = tf_warning_or_error;
4145 if (decltype_p)
4146 complain |= tf_decltype;
4147 return complain;
4150 /* We're about to parse a collection of statements. If we're currently
4151 parsing tentatively, set up a firewall so that any nested
4152 cp_parser_commit_to_tentative_parse won't affect the current context. */
4154 static cp_token_position
4155 cp_parser_start_tentative_firewall (cp_parser *parser)
4157 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4158 return 0;
4160 cp_parser_parse_tentatively (parser);
4161 cp_parser_commit_to_topmost_tentative_parse (parser);
4162 return cp_lexer_token_position (parser->lexer, false);
4165 /* We've finished parsing the collection of statements. Wrap up the
4166 firewall and replace the relevant tokens with the parsed form. */
4168 static void
4169 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4170 tree expr)
4172 if (!start)
4173 return;
4175 /* Finish the firewall level. */
4176 cp_parser_parse_definitely (parser);
4177 /* And remember the result of the parse for when we try again. */
4178 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4179 token->type = CPP_PREPARSED_EXPR;
4180 token->u.value = expr;
4181 token->keyword = RID_MAX;
4182 cp_lexer_purge_tokens_after (parser->lexer, start);
4185 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4186 enclosing parentheses. */
4188 static tree
4189 cp_parser_statement_expr (cp_parser *parser)
4191 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4193 /* Consume the '('. */
4194 cp_lexer_consume_token (parser->lexer);
4195 /* Start the statement-expression. */
4196 tree expr = begin_stmt_expr ();
4197 /* Parse the compound-statement. */
4198 cp_parser_compound_statement (parser, expr, false, false);
4199 /* Finish up. */
4200 expr = finish_stmt_expr (expr, false);
4201 /* Consume the ')'. */
4202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4203 cp_parser_skip_to_end_of_statement (parser);
4205 cp_parser_end_tentative_firewall (parser, start, expr);
4206 return expr;
4209 /* Expressions [gram.expr] */
4211 /* Parse a primary-expression.
4213 primary-expression:
4214 literal
4215 this
4216 ( expression )
4217 id-expression
4218 lambda-expression (C++11)
4220 GNU Extensions:
4222 primary-expression:
4223 ( compound-statement )
4224 __builtin_va_arg ( assignment-expression , type-id )
4225 __builtin_offsetof ( type-id , offsetof-expression )
4227 C++ Extensions:
4228 __has_nothrow_assign ( type-id )
4229 __has_nothrow_constructor ( type-id )
4230 __has_nothrow_copy ( type-id )
4231 __has_trivial_assign ( type-id )
4232 __has_trivial_constructor ( type-id )
4233 __has_trivial_copy ( type-id )
4234 __has_trivial_destructor ( type-id )
4235 __has_virtual_destructor ( type-id )
4236 __is_abstract ( type-id )
4237 __is_base_of ( type-id , type-id )
4238 __is_class ( type-id )
4239 __is_empty ( type-id )
4240 __is_enum ( type-id )
4241 __is_final ( type-id )
4242 __is_literal_type ( type-id )
4243 __is_pod ( type-id )
4244 __is_polymorphic ( type-id )
4245 __is_std_layout ( type-id )
4246 __is_trivial ( type-id )
4247 __is_union ( type-id )
4249 Objective-C++ Extension:
4251 primary-expression:
4252 objc-expression
4254 literal:
4255 __null
4257 ADDRESS_P is true iff this expression was immediately preceded by
4258 "&" and therefore might denote a pointer-to-member. CAST_P is true
4259 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4260 true iff this expression is a template argument.
4262 Returns a representation of the expression. Upon return, *IDK
4263 indicates what kind of id-expression (if any) was present. */
4265 static tree
4266 cp_parser_primary_expression (cp_parser *parser,
4267 bool address_p,
4268 bool cast_p,
4269 bool template_arg_p,
4270 bool decltype_p,
4271 cp_id_kind *idk)
4273 cp_token *token = NULL;
4275 /* Assume the primary expression is not an id-expression. */
4276 *idk = CP_ID_KIND_NONE;
4278 /* Peek at the next token. */
4279 token = cp_lexer_peek_token (parser->lexer);
4280 switch ((int) token->type)
4282 /* literal:
4283 integer-literal
4284 character-literal
4285 floating-literal
4286 string-literal
4287 boolean-literal
4288 pointer-literal
4289 user-defined-literal */
4290 case CPP_CHAR:
4291 case CPP_CHAR16:
4292 case CPP_CHAR32:
4293 case CPP_WCHAR:
4294 case CPP_NUMBER:
4295 case CPP_PREPARSED_EXPR:
4296 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4297 return cp_parser_userdef_numeric_literal (parser);
4298 token = cp_lexer_consume_token (parser->lexer);
4299 if (TREE_CODE (token->u.value) == FIXED_CST)
4301 error_at (token->location,
4302 "fixed-point types not supported in C++");
4303 return error_mark_node;
4305 /* Floating-point literals are only allowed in an integral
4306 constant expression if they are cast to an integral or
4307 enumeration type. */
4308 if (TREE_CODE (token->u.value) == REAL_CST
4309 && parser->integral_constant_expression_p
4310 && pedantic)
4312 /* CAST_P will be set even in invalid code like "int(2.7 +
4313 ...)". Therefore, we have to check that the next token
4314 is sure to end the cast. */
4315 if (cast_p)
4317 cp_token *next_token;
4319 next_token = cp_lexer_peek_token (parser->lexer);
4320 if (/* The comma at the end of an
4321 enumerator-definition. */
4322 next_token->type != CPP_COMMA
4323 /* The curly brace at the end of an enum-specifier. */
4324 && next_token->type != CPP_CLOSE_BRACE
4325 /* The end of a statement. */
4326 && next_token->type != CPP_SEMICOLON
4327 /* The end of the cast-expression. */
4328 && next_token->type != CPP_CLOSE_PAREN
4329 /* The end of an array bound. */
4330 && next_token->type != CPP_CLOSE_SQUARE
4331 /* The closing ">" in a template-argument-list. */
4332 && (next_token->type != CPP_GREATER
4333 || parser->greater_than_is_operator_p)
4334 /* C++0x only: A ">>" treated like two ">" tokens,
4335 in a template-argument-list. */
4336 && (next_token->type != CPP_RSHIFT
4337 || (cxx_dialect == cxx98)
4338 || parser->greater_than_is_operator_p))
4339 cast_p = false;
4342 /* If we are within a cast, then the constraint that the
4343 cast is to an integral or enumeration type will be
4344 checked at that point. If we are not within a cast, then
4345 this code is invalid. */
4346 if (!cast_p)
4347 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4349 return token->u.value;
4351 case CPP_CHAR_USERDEF:
4352 case CPP_CHAR16_USERDEF:
4353 case CPP_CHAR32_USERDEF:
4354 case CPP_WCHAR_USERDEF:
4355 return cp_parser_userdef_char_literal (parser);
4357 case CPP_STRING:
4358 case CPP_STRING16:
4359 case CPP_STRING32:
4360 case CPP_WSTRING:
4361 case CPP_UTF8STRING:
4362 case CPP_STRING_USERDEF:
4363 case CPP_STRING16_USERDEF:
4364 case CPP_STRING32_USERDEF:
4365 case CPP_WSTRING_USERDEF:
4366 case CPP_UTF8STRING_USERDEF:
4367 /* ??? Should wide strings be allowed when parser->translate_strings_p
4368 is false (i.e. in attributes)? If not, we can kill the third
4369 argument to cp_parser_string_literal. */
4370 return cp_parser_string_literal (parser,
4371 parser->translate_strings_p,
4372 true);
4374 case CPP_OPEN_PAREN:
4375 /* If we see `( { ' then we are looking at the beginning of
4376 a GNU statement-expression. */
4377 if (cp_parser_allow_gnu_extensions_p (parser)
4378 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4380 /* Statement-expressions are not allowed by the standard. */
4381 pedwarn (token->location, OPT_Wpedantic,
4382 "ISO C++ forbids braced-groups within expressions");
4384 /* And they're not allowed outside of a function-body; you
4385 cannot, for example, write:
4387 int i = ({ int j = 3; j + 1; });
4389 at class or namespace scope. */
4390 if (!parser->in_function_body
4391 || parser->in_template_argument_list_p)
4393 error_at (token->location,
4394 "statement-expressions are not allowed outside "
4395 "functions nor in template-argument lists");
4396 cp_parser_skip_to_end_of_block_or_statement (parser);
4397 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4398 cp_lexer_consume_token (parser->lexer);
4399 return error_mark_node;
4401 else
4402 return cp_parser_statement_expr (parser);
4404 /* Otherwise it's a normal parenthesized expression. */
4406 tree expr;
4407 bool saved_greater_than_is_operator_p;
4409 /* Consume the `('. */
4410 cp_lexer_consume_token (parser->lexer);
4411 /* Within a parenthesized expression, a `>' token is always
4412 the greater-than operator. */
4413 saved_greater_than_is_operator_p
4414 = parser->greater_than_is_operator_p;
4415 parser->greater_than_is_operator_p = true;
4417 /* Parse the parenthesized expression. */
4418 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4419 /* Let the front end know that this expression was
4420 enclosed in parentheses. This matters in case, for
4421 example, the expression is of the form `A::B', since
4422 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4423 not. */
4424 expr = finish_parenthesized_expr (expr);
4425 /* DR 705: Wrapping an unqualified name in parentheses
4426 suppresses arg-dependent lookup. We want to pass back
4427 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4428 (c++/37862), but none of the others. */
4429 if (*idk != CP_ID_KIND_QUALIFIED)
4430 *idk = CP_ID_KIND_NONE;
4432 /* The `>' token might be the end of a template-id or
4433 template-parameter-list now. */
4434 parser->greater_than_is_operator_p
4435 = saved_greater_than_is_operator_p;
4436 /* Consume the `)'. */
4437 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4438 cp_parser_skip_to_end_of_statement (parser);
4440 return expr;
4443 case CPP_OPEN_SQUARE:
4444 if (c_dialect_objc ())
4445 /* We have an Objective-C++ message. */
4446 return cp_parser_objc_expression (parser);
4448 tree lam = cp_parser_lambda_expression (parser);
4449 /* Don't warn about a failed tentative parse. */
4450 if (cp_parser_error_occurred (parser))
4451 return error_mark_node;
4452 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4453 return lam;
4456 case CPP_OBJC_STRING:
4457 if (c_dialect_objc ())
4458 /* We have an Objective-C++ string literal. */
4459 return cp_parser_objc_expression (parser);
4460 cp_parser_error (parser, "expected primary-expression");
4461 return error_mark_node;
4463 case CPP_KEYWORD:
4464 switch (token->keyword)
4466 /* These two are the boolean literals. */
4467 case RID_TRUE:
4468 cp_lexer_consume_token (parser->lexer);
4469 return boolean_true_node;
4470 case RID_FALSE:
4471 cp_lexer_consume_token (parser->lexer);
4472 return boolean_false_node;
4474 /* The `__null' literal. */
4475 case RID_NULL:
4476 cp_lexer_consume_token (parser->lexer);
4477 return null_node;
4479 /* The `nullptr' literal. */
4480 case RID_NULLPTR:
4481 cp_lexer_consume_token (parser->lexer);
4482 return nullptr_node;
4484 /* Recognize the `this' keyword. */
4485 case RID_THIS:
4486 cp_lexer_consume_token (parser->lexer);
4487 if (parser->local_variables_forbidden_p)
4489 error_at (token->location,
4490 "%<this%> may not be used in this context");
4491 return error_mark_node;
4493 /* Pointers cannot appear in constant-expressions. */
4494 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4495 return error_mark_node;
4496 return finish_this_expr ();
4498 /* The `operator' keyword can be the beginning of an
4499 id-expression. */
4500 case RID_OPERATOR:
4501 goto id_expression;
4503 case RID_FUNCTION_NAME:
4504 case RID_PRETTY_FUNCTION_NAME:
4505 case RID_C99_FUNCTION_NAME:
4506 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4507 __func__ are the names of variables. */
4508 goto id_expression;
4510 case RID_VA_ARG:
4512 tree expression;
4513 tree type;
4514 source_location type_location;
4516 /* The `__builtin_va_arg' construct is used to handle
4517 `va_arg'. Consume the `__builtin_va_arg' token. */
4518 cp_lexer_consume_token (parser->lexer);
4519 /* Look for the opening `('. */
4520 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4521 /* Now, parse the assignment-expression. */
4522 expression = cp_parser_assignment_expression (parser);
4523 /* Look for the `,'. */
4524 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4525 type_location = cp_lexer_peek_token (parser->lexer)->location;
4526 /* Parse the type-id. */
4527 type = cp_parser_type_id (parser);
4528 /* Look for the closing `)'. */
4529 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4530 /* Using `va_arg' in a constant-expression is not
4531 allowed. */
4532 if (cp_parser_non_integral_constant_expression (parser,
4533 NIC_VA_ARG))
4534 return error_mark_node;
4535 return build_x_va_arg (type_location, expression, type);
4538 case RID_OFFSETOF:
4539 return cp_parser_builtin_offsetof (parser);
4541 case RID_HAS_NOTHROW_ASSIGN:
4542 case RID_HAS_NOTHROW_CONSTRUCTOR:
4543 case RID_HAS_NOTHROW_COPY:
4544 case RID_HAS_TRIVIAL_ASSIGN:
4545 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4546 case RID_HAS_TRIVIAL_COPY:
4547 case RID_HAS_TRIVIAL_DESTRUCTOR:
4548 case RID_HAS_VIRTUAL_DESTRUCTOR:
4549 case RID_IS_ABSTRACT:
4550 case RID_IS_BASE_OF:
4551 case RID_IS_CLASS:
4552 case RID_IS_EMPTY:
4553 case RID_IS_ENUM:
4554 case RID_IS_FINAL:
4555 case RID_IS_LITERAL_TYPE:
4556 case RID_IS_POD:
4557 case RID_IS_POLYMORPHIC:
4558 case RID_IS_STD_LAYOUT:
4559 case RID_IS_TRIVIAL:
4560 case RID_IS_TRIVIALLY_ASSIGNABLE:
4561 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4562 case RID_IS_TRIVIALLY_COPYABLE:
4563 case RID_IS_UNION:
4564 return cp_parser_trait_expr (parser, token->keyword);
4566 /* Objective-C++ expressions. */
4567 case RID_AT_ENCODE:
4568 case RID_AT_PROTOCOL:
4569 case RID_AT_SELECTOR:
4570 return cp_parser_objc_expression (parser);
4572 case RID_TEMPLATE:
4573 if (parser->in_function_body
4574 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4575 == CPP_LESS))
4577 error_at (token->location,
4578 "a template declaration cannot appear at block scope");
4579 cp_parser_skip_to_end_of_block_or_statement (parser);
4580 return error_mark_node;
4582 default:
4583 cp_parser_error (parser, "expected primary-expression");
4584 return error_mark_node;
4587 /* An id-expression can start with either an identifier, a
4588 `::' as the beginning of a qualified-id, or the "operator"
4589 keyword. */
4590 case CPP_NAME:
4591 case CPP_SCOPE:
4592 case CPP_TEMPLATE_ID:
4593 case CPP_NESTED_NAME_SPECIFIER:
4595 tree id_expression;
4596 tree decl;
4597 const char *error_msg;
4598 bool template_p;
4599 bool done;
4600 cp_token *id_expr_token;
4602 id_expression:
4603 /* Parse the id-expression. */
4604 id_expression
4605 = cp_parser_id_expression (parser,
4606 /*template_keyword_p=*/false,
4607 /*check_dependency_p=*/true,
4608 &template_p,
4609 /*declarator_p=*/false,
4610 /*optional_p=*/false);
4611 if (id_expression == error_mark_node)
4612 return error_mark_node;
4613 id_expr_token = token;
4614 token = cp_lexer_peek_token (parser->lexer);
4615 done = (token->type != CPP_OPEN_SQUARE
4616 && token->type != CPP_OPEN_PAREN
4617 && token->type != CPP_DOT
4618 && token->type != CPP_DEREF
4619 && token->type != CPP_PLUS_PLUS
4620 && token->type != CPP_MINUS_MINUS);
4621 /* If we have a template-id, then no further lookup is
4622 required. If the template-id was for a template-class, we
4623 will sometimes have a TYPE_DECL at this point. */
4624 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4625 || TREE_CODE (id_expression) == TYPE_DECL)
4626 decl = id_expression;
4627 /* Look up the name. */
4628 else
4630 tree ambiguous_decls;
4632 /* If we already know that this lookup is ambiguous, then
4633 we've already issued an error message; there's no reason
4634 to check again. */
4635 if (id_expr_token->type == CPP_NAME
4636 && id_expr_token->error_reported)
4638 cp_parser_simulate_error (parser);
4639 return error_mark_node;
4642 decl = cp_parser_lookup_name (parser, id_expression,
4643 none_type,
4644 template_p,
4645 /*is_namespace=*/false,
4646 /*check_dependency=*/true,
4647 &ambiguous_decls,
4648 id_expr_token->location);
4649 /* If the lookup was ambiguous, an error will already have
4650 been issued. */
4651 if (ambiguous_decls)
4652 return error_mark_node;
4654 /* In Objective-C++, we may have an Objective-C 2.0
4655 dot-syntax for classes here. */
4656 if (c_dialect_objc ()
4657 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4658 && TREE_CODE (decl) == TYPE_DECL
4659 && objc_is_class_name (decl))
4661 tree component;
4662 cp_lexer_consume_token (parser->lexer);
4663 component = cp_parser_identifier (parser);
4664 if (component == error_mark_node)
4665 return error_mark_node;
4667 return objc_build_class_component_ref (id_expression, component);
4670 /* In Objective-C++, an instance variable (ivar) may be preferred
4671 to whatever cp_parser_lookup_name() found. */
4672 decl = objc_lookup_ivar (decl, id_expression);
4674 /* If name lookup gives us a SCOPE_REF, then the
4675 qualifying scope was dependent. */
4676 if (TREE_CODE (decl) == SCOPE_REF)
4678 /* At this point, we do not know if DECL is a valid
4679 integral constant expression. We assume that it is
4680 in fact such an expression, so that code like:
4682 template <int N> struct A {
4683 int a[B<N>::i];
4686 is accepted. At template-instantiation time, we
4687 will check that B<N>::i is actually a constant. */
4688 return decl;
4690 /* Check to see if DECL is a local variable in a context
4691 where that is forbidden. */
4692 if (parser->local_variables_forbidden_p
4693 && local_variable_p (decl))
4695 /* It might be that we only found DECL because we are
4696 trying to be generous with pre-ISO scoping rules.
4697 For example, consider:
4699 int i;
4700 void g() {
4701 for (int i = 0; i < 10; ++i) {}
4702 extern void f(int j = i);
4705 Here, name look up will originally find the out
4706 of scope `i'. We need to issue a warning message,
4707 but then use the global `i'. */
4708 decl = check_for_out_of_scope_variable (decl);
4709 if (local_variable_p (decl))
4711 error_at (id_expr_token->location,
4712 "local variable %qD may not appear in this context",
4713 decl);
4714 return error_mark_node;
4719 decl = (finish_id_expression
4720 (id_expression, decl, parser->scope,
4721 idk,
4722 parser->integral_constant_expression_p,
4723 parser->allow_non_integral_constant_expression_p,
4724 &parser->non_integral_constant_expression_p,
4725 template_p, done, address_p,
4726 template_arg_p,
4727 &error_msg,
4728 id_expr_token->location));
4729 if (error_msg)
4730 cp_parser_error (parser, error_msg);
4731 return decl;
4734 /* Anything else is an error. */
4735 default:
4736 cp_parser_error (parser, "expected primary-expression");
4737 return error_mark_node;
4741 static inline tree
4742 cp_parser_primary_expression (cp_parser *parser,
4743 bool address_p,
4744 bool cast_p,
4745 bool template_arg_p,
4746 cp_id_kind *idk)
4748 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4749 /*decltype*/false, idk);
4752 /* Parse an id-expression.
4754 id-expression:
4755 unqualified-id
4756 qualified-id
4758 qualified-id:
4759 :: [opt] nested-name-specifier template [opt] unqualified-id
4760 :: identifier
4761 :: operator-function-id
4762 :: template-id
4764 Return a representation of the unqualified portion of the
4765 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4766 a `::' or nested-name-specifier.
4768 Often, if the id-expression was a qualified-id, the caller will
4769 want to make a SCOPE_REF to represent the qualified-id. This
4770 function does not do this in order to avoid wastefully creating
4771 SCOPE_REFs when they are not required.
4773 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4774 `template' keyword.
4776 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4777 uninstantiated templates.
4779 If *TEMPLATE_P is non-NULL, it is set to true iff the
4780 `template' keyword is used to explicitly indicate that the entity
4781 named is a template.
4783 If DECLARATOR_P is true, the id-expression is appearing as part of
4784 a declarator, rather than as part of an expression. */
4786 static tree
4787 cp_parser_id_expression (cp_parser *parser,
4788 bool template_keyword_p,
4789 bool check_dependency_p,
4790 bool *template_p,
4791 bool declarator_p,
4792 bool optional_p)
4794 bool global_scope_p;
4795 bool nested_name_specifier_p;
4797 /* Assume the `template' keyword was not used. */
4798 if (template_p)
4799 *template_p = template_keyword_p;
4801 /* Look for the optional `::' operator. */
4802 global_scope_p
4803 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4804 != NULL_TREE);
4805 /* Look for the optional nested-name-specifier. */
4806 nested_name_specifier_p
4807 = (cp_parser_nested_name_specifier_opt (parser,
4808 /*typename_keyword_p=*/false,
4809 check_dependency_p,
4810 /*type_p=*/false,
4811 declarator_p)
4812 != NULL_TREE);
4813 /* If there is a nested-name-specifier, then we are looking at
4814 the first qualified-id production. */
4815 if (nested_name_specifier_p)
4817 tree saved_scope;
4818 tree saved_object_scope;
4819 tree saved_qualifying_scope;
4820 tree unqualified_id;
4821 bool is_template;
4823 /* See if the next token is the `template' keyword. */
4824 if (!template_p)
4825 template_p = &is_template;
4826 *template_p = cp_parser_optional_template_keyword (parser);
4827 /* Name lookup we do during the processing of the
4828 unqualified-id might obliterate SCOPE. */
4829 saved_scope = parser->scope;
4830 saved_object_scope = parser->object_scope;
4831 saved_qualifying_scope = parser->qualifying_scope;
4832 /* Process the final unqualified-id. */
4833 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4834 check_dependency_p,
4835 declarator_p,
4836 /*optional_p=*/false);
4837 /* Restore the SAVED_SCOPE for our caller. */
4838 parser->scope = saved_scope;
4839 parser->object_scope = saved_object_scope;
4840 parser->qualifying_scope = saved_qualifying_scope;
4842 return unqualified_id;
4844 /* Otherwise, if we are in global scope, then we are looking at one
4845 of the other qualified-id productions. */
4846 else if (global_scope_p)
4848 cp_token *token;
4849 tree id;
4851 /* Peek at the next token. */
4852 token = cp_lexer_peek_token (parser->lexer);
4854 /* If it's an identifier, and the next token is not a "<", then
4855 we can avoid the template-id case. This is an optimization
4856 for this common case. */
4857 if (token->type == CPP_NAME
4858 && !cp_parser_nth_token_starts_template_argument_list_p
4859 (parser, 2))
4860 return cp_parser_identifier (parser);
4862 cp_parser_parse_tentatively (parser);
4863 /* Try a template-id. */
4864 id = cp_parser_template_id (parser,
4865 /*template_keyword_p=*/false,
4866 /*check_dependency_p=*/true,
4867 none_type,
4868 declarator_p);
4869 /* If that worked, we're done. */
4870 if (cp_parser_parse_definitely (parser))
4871 return id;
4873 /* Peek at the next token. (Changes in the token buffer may
4874 have invalidated the pointer obtained above.) */
4875 token = cp_lexer_peek_token (parser->lexer);
4877 switch (token->type)
4879 case CPP_NAME:
4880 return cp_parser_identifier (parser);
4882 case CPP_KEYWORD:
4883 if (token->keyword == RID_OPERATOR)
4884 return cp_parser_operator_function_id (parser);
4885 /* Fall through. */
4887 default:
4888 cp_parser_error (parser, "expected id-expression");
4889 return error_mark_node;
4892 else
4893 return cp_parser_unqualified_id (parser, template_keyword_p,
4894 /*check_dependency_p=*/true,
4895 declarator_p,
4896 optional_p);
4899 /* Parse an unqualified-id.
4901 unqualified-id:
4902 identifier
4903 operator-function-id
4904 conversion-function-id
4905 ~ class-name
4906 template-id
4908 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4909 keyword, in a construct like `A::template ...'.
4911 Returns a representation of unqualified-id. For the `identifier'
4912 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4913 production a BIT_NOT_EXPR is returned; the operand of the
4914 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4915 other productions, see the documentation accompanying the
4916 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4917 names are looked up in uninstantiated templates. If DECLARATOR_P
4918 is true, the unqualified-id is appearing as part of a declarator,
4919 rather than as part of an expression. */
4921 static tree
4922 cp_parser_unqualified_id (cp_parser* parser,
4923 bool template_keyword_p,
4924 bool check_dependency_p,
4925 bool declarator_p,
4926 bool optional_p)
4928 cp_token *token;
4929 tree id;
4931 /* Peek at the next token. */
4932 token = cp_lexer_peek_token (parser->lexer);
4934 switch ((int) token->type)
4936 case CPP_NAME:
4938 /* We don't know yet whether or not this will be a
4939 template-id. */
4940 cp_parser_parse_tentatively (parser);
4941 /* Try a template-id. */
4942 id = cp_parser_template_id (parser, template_keyword_p,
4943 check_dependency_p,
4944 none_type,
4945 declarator_p);
4946 /* If it worked, we're done. */
4947 if (cp_parser_parse_definitely (parser))
4948 return id;
4949 /* Otherwise, it's an ordinary identifier. */
4950 return cp_parser_identifier (parser);
4953 case CPP_TEMPLATE_ID:
4954 return cp_parser_template_id (parser, template_keyword_p,
4955 check_dependency_p,
4956 none_type,
4957 declarator_p);
4959 case CPP_COMPL:
4961 tree type_decl;
4962 tree qualifying_scope;
4963 tree object_scope;
4964 tree scope;
4965 bool done;
4967 /* Consume the `~' token. */
4968 cp_lexer_consume_token (parser->lexer);
4969 /* Parse the class-name. The standard, as written, seems to
4970 say that:
4972 template <typename T> struct S { ~S (); };
4973 template <typename T> S<T>::~S() {}
4975 is invalid, since `~' must be followed by a class-name, but
4976 `S<T>' is dependent, and so not known to be a class.
4977 That's not right; we need to look in uninstantiated
4978 templates. A further complication arises from:
4980 template <typename T> void f(T t) {
4981 t.T::~T();
4984 Here, it is not possible to look up `T' in the scope of `T'
4985 itself. We must look in both the current scope, and the
4986 scope of the containing complete expression.
4988 Yet another issue is:
4990 struct S {
4991 int S;
4992 ~S();
4995 S::~S() {}
4997 The standard does not seem to say that the `S' in `~S'
4998 should refer to the type `S' and not the data member
4999 `S::S'. */
5001 /* DR 244 says that we look up the name after the "~" in the
5002 same scope as we looked up the qualifying name. That idea
5003 isn't fully worked out; it's more complicated than that. */
5004 scope = parser->scope;
5005 object_scope = parser->object_scope;
5006 qualifying_scope = parser->qualifying_scope;
5008 /* Check for invalid scopes. */
5009 if (scope == error_mark_node)
5011 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5012 cp_lexer_consume_token (parser->lexer);
5013 return error_mark_node;
5015 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5017 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5018 error_at (token->location,
5019 "scope %qT before %<~%> is not a class-name",
5020 scope);
5021 cp_parser_simulate_error (parser);
5022 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5023 cp_lexer_consume_token (parser->lexer);
5024 return error_mark_node;
5026 gcc_assert (!scope || TYPE_P (scope));
5028 /* If the name is of the form "X::~X" it's OK even if X is a
5029 typedef. */
5030 token = cp_lexer_peek_token (parser->lexer);
5031 if (scope
5032 && token->type == CPP_NAME
5033 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5034 != CPP_LESS)
5035 && (token->u.value == TYPE_IDENTIFIER (scope)
5036 || (CLASS_TYPE_P (scope)
5037 && constructor_name_p (token->u.value, scope))))
5039 cp_lexer_consume_token (parser->lexer);
5040 return build_nt (BIT_NOT_EXPR, scope);
5043 /* ~auto means the destructor of whatever the object is. */
5044 if (cp_parser_is_keyword (token, RID_AUTO))
5046 if (cxx_dialect < cxx14)
5047 pedwarn (input_location, 0,
5048 "%<~auto%> only available with "
5049 "-std=c++14 or -std=gnu++14");
5050 cp_lexer_consume_token (parser->lexer);
5051 return build_nt (BIT_NOT_EXPR, make_auto ());
5054 /* If there was an explicit qualification (S::~T), first look
5055 in the scope given by the qualification (i.e., S).
5057 Note: in the calls to cp_parser_class_name below we pass
5058 typename_type so that lookup finds the injected-class-name
5059 rather than the constructor. */
5060 done = false;
5061 type_decl = NULL_TREE;
5062 if (scope)
5064 cp_parser_parse_tentatively (parser);
5065 type_decl = cp_parser_class_name (parser,
5066 /*typename_keyword_p=*/false,
5067 /*template_keyword_p=*/false,
5068 typename_type,
5069 /*check_dependency=*/false,
5070 /*class_head_p=*/false,
5071 declarator_p);
5072 if (cp_parser_parse_definitely (parser))
5073 done = true;
5075 /* In "N::S::~S", look in "N" as well. */
5076 if (!done && scope && qualifying_scope)
5078 cp_parser_parse_tentatively (parser);
5079 parser->scope = qualifying_scope;
5080 parser->object_scope = NULL_TREE;
5081 parser->qualifying_scope = NULL_TREE;
5082 type_decl
5083 = cp_parser_class_name (parser,
5084 /*typename_keyword_p=*/false,
5085 /*template_keyword_p=*/false,
5086 typename_type,
5087 /*check_dependency=*/false,
5088 /*class_head_p=*/false,
5089 declarator_p);
5090 if (cp_parser_parse_definitely (parser))
5091 done = true;
5093 /* In "p->S::~T", look in the scope given by "*p" as well. */
5094 else if (!done && object_scope)
5096 cp_parser_parse_tentatively (parser);
5097 parser->scope = object_scope;
5098 parser->object_scope = NULL_TREE;
5099 parser->qualifying_scope = NULL_TREE;
5100 type_decl
5101 = cp_parser_class_name (parser,
5102 /*typename_keyword_p=*/false,
5103 /*template_keyword_p=*/false,
5104 typename_type,
5105 /*check_dependency=*/false,
5106 /*class_head_p=*/false,
5107 declarator_p);
5108 if (cp_parser_parse_definitely (parser))
5109 done = true;
5111 /* Look in the surrounding context. */
5112 if (!done)
5114 parser->scope = NULL_TREE;
5115 parser->object_scope = NULL_TREE;
5116 parser->qualifying_scope = NULL_TREE;
5117 if (processing_template_decl)
5118 cp_parser_parse_tentatively (parser);
5119 type_decl
5120 = cp_parser_class_name (parser,
5121 /*typename_keyword_p=*/false,
5122 /*template_keyword_p=*/false,
5123 typename_type,
5124 /*check_dependency=*/false,
5125 /*class_head_p=*/false,
5126 declarator_p);
5127 if (processing_template_decl
5128 && ! cp_parser_parse_definitely (parser))
5130 /* We couldn't find a type with this name, so just accept
5131 it and check for a match at instantiation time. */
5132 type_decl = cp_parser_identifier (parser);
5133 if (type_decl != error_mark_node)
5134 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5135 return type_decl;
5138 /* If an error occurred, assume that the name of the
5139 destructor is the same as the name of the qualifying
5140 class. That allows us to keep parsing after running
5141 into ill-formed destructor names. */
5142 if (type_decl == error_mark_node && scope)
5143 return build_nt (BIT_NOT_EXPR, scope);
5144 else if (type_decl == error_mark_node)
5145 return error_mark_node;
5147 /* Check that destructor name and scope match. */
5148 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5150 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5151 error_at (token->location,
5152 "declaration of %<~%T%> as member of %qT",
5153 type_decl, scope);
5154 cp_parser_simulate_error (parser);
5155 return error_mark_node;
5158 /* [class.dtor]
5160 A typedef-name that names a class shall not be used as the
5161 identifier in the declarator for a destructor declaration. */
5162 if (declarator_p
5163 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5164 && !DECL_SELF_REFERENCE_P (type_decl)
5165 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5166 error_at (token->location,
5167 "typedef-name %qD used as destructor declarator",
5168 type_decl);
5170 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5173 case CPP_KEYWORD:
5174 switch (token->keyword)
5176 case RID_OPERATOR:
5177 /* This could be a template-id, so we try that first. */
5178 cp_parser_parse_tentatively (parser);
5179 /* Try a template-id. */
5180 id = cp_parser_template_id (parser, template_keyword_p,
5181 /*check_dependency_p=*/true,
5182 none_type,
5183 declarator_p);
5184 /* If that worked, we're done. */
5185 if (cp_parser_parse_definitely (parser))
5186 return id;
5187 /* We still don't know whether we're looking at an
5188 operator-function-id or a conversion-function-id. */
5189 cp_parser_parse_tentatively (parser);
5190 /* Try an operator-function-id. */
5191 id = cp_parser_operator_function_id (parser);
5192 /* If that didn't work, try a conversion-function-id. */
5193 if (!cp_parser_parse_definitely (parser))
5194 id = cp_parser_conversion_function_id (parser);
5195 else if (UDLIT_OPER_P (id))
5197 /* 17.6.3.3.5 */
5198 const char *name = UDLIT_OP_SUFFIX (id);
5199 if (name[0] != '_' && !in_system_header_at (input_location)
5200 && declarator_p)
5201 warning (0, "literal operator suffixes not preceded by %<_%>"
5202 " are reserved for future standardization");
5205 return id;
5207 case RID_FUNCTION_NAME:
5208 case RID_PRETTY_FUNCTION_NAME:
5209 case RID_C99_FUNCTION_NAME:
5210 cp_lexer_consume_token (parser->lexer);
5211 /* Don't try to declare this while tentatively parsing a function
5212 declarator, as cp_make_fname_decl will fail. */
5213 if (current_binding_level->kind != sk_function_parms)
5214 finish_fname (token->u.value);
5215 return token->u.value;
5217 default:
5218 break;
5220 /* Fall through. */
5222 default:
5223 if (optional_p)
5224 return NULL_TREE;
5225 cp_parser_error (parser, "expected unqualified-id");
5226 return error_mark_node;
5230 /* Parse an (optional) nested-name-specifier.
5232 nested-name-specifier: [C++98]
5233 class-or-namespace-name :: nested-name-specifier [opt]
5234 class-or-namespace-name :: template nested-name-specifier [opt]
5236 nested-name-specifier: [C++0x]
5237 type-name ::
5238 namespace-name ::
5239 nested-name-specifier identifier ::
5240 nested-name-specifier template [opt] simple-template-id ::
5242 PARSER->SCOPE should be set appropriately before this function is
5243 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5244 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5245 in name lookups.
5247 Sets PARSER->SCOPE to the class (TYPE) or namespace
5248 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5249 it unchanged if there is no nested-name-specifier. Returns the new
5250 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5252 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5253 part of a declaration and/or decl-specifier. */
5255 static tree
5256 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5257 bool typename_keyword_p,
5258 bool check_dependency_p,
5259 bool type_p,
5260 bool is_declaration)
5262 bool success = false;
5263 cp_token_position start = 0;
5264 cp_token *token;
5266 /* Remember where the nested-name-specifier starts. */
5267 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5269 start = cp_lexer_token_position (parser->lexer, false);
5270 push_deferring_access_checks (dk_deferred);
5273 while (true)
5275 tree new_scope;
5276 tree old_scope;
5277 tree saved_qualifying_scope;
5278 bool template_keyword_p;
5280 /* Spot cases that cannot be the beginning of a
5281 nested-name-specifier. */
5282 token = cp_lexer_peek_token (parser->lexer);
5284 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5285 the already parsed nested-name-specifier. */
5286 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5288 /* Grab the nested-name-specifier and continue the loop. */
5289 cp_parser_pre_parsed_nested_name_specifier (parser);
5290 /* If we originally encountered this nested-name-specifier
5291 with IS_DECLARATION set to false, we will not have
5292 resolved TYPENAME_TYPEs, so we must do so here. */
5293 if (is_declaration
5294 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5296 new_scope = resolve_typename_type (parser->scope,
5297 /*only_current_p=*/false);
5298 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5299 parser->scope = new_scope;
5301 success = true;
5302 continue;
5305 /* Spot cases that cannot be the beginning of a
5306 nested-name-specifier. On the second and subsequent times
5307 through the loop, we look for the `template' keyword. */
5308 if (success && token->keyword == RID_TEMPLATE)
5310 /* A template-id can start a nested-name-specifier. */
5311 else if (token->type == CPP_TEMPLATE_ID)
5313 /* DR 743: decltype can be used in a nested-name-specifier. */
5314 else if (token_is_decltype (token))
5316 else
5318 /* If the next token is not an identifier, then it is
5319 definitely not a type-name or namespace-name. */
5320 if (token->type != CPP_NAME)
5321 break;
5322 /* If the following token is neither a `<' (to begin a
5323 template-id), nor a `::', then we are not looking at a
5324 nested-name-specifier. */
5325 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5327 if (token->type == CPP_COLON
5328 && parser->colon_corrects_to_scope_p
5329 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5331 error_at (token->location,
5332 "found %<:%> in nested-name-specifier, expected %<::%>");
5333 token->type = CPP_SCOPE;
5336 if (token->type != CPP_SCOPE
5337 && !cp_parser_nth_token_starts_template_argument_list_p
5338 (parser, 2))
5339 break;
5342 /* The nested-name-specifier is optional, so we parse
5343 tentatively. */
5344 cp_parser_parse_tentatively (parser);
5346 /* Look for the optional `template' keyword, if this isn't the
5347 first time through the loop. */
5348 if (success)
5349 template_keyword_p = cp_parser_optional_template_keyword (parser);
5350 else
5351 template_keyword_p = false;
5353 /* Save the old scope since the name lookup we are about to do
5354 might destroy it. */
5355 old_scope = parser->scope;
5356 saved_qualifying_scope = parser->qualifying_scope;
5357 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5358 look up names in "X<T>::I" in order to determine that "Y" is
5359 a template. So, if we have a typename at this point, we make
5360 an effort to look through it. */
5361 if (is_declaration
5362 && !typename_keyword_p
5363 && parser->scope
5364 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5365 parser->scope = resolve_typename_type (parser->scope,
5366 /*only_current_p=*/false);
5367 /* Parse the qualifying entity. */
5368 new_scope
5369 = cp_parser_qualifying_entity (parser,
5370 typename_keyword_p,
5371 template_keyword_p,
5372 check_dependency_p,
5373 type_p,
5374 is_declaration);
5375 /* Look for the `::' token. */
5376 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5378 /* If we found what we wanted, we keep going; otherwise, we're
5379 done. */
5380 if (!cp_parser_parse_definitely (parser))
5382 bool error_p = false;
5384 /* Restore the OLD_SCOPE since it was valid before the
5385 failed attempt at finding the last
5386 class-or-namespace-name. */
5387 parser->scope = old_scope;
5388 parser->qualifying_scope = saved_qualifying_scope;
5390 /* If the next token is a decltype, and the one after that is a
5391 `::', then the decltype has failed to resolve to a class or
5392 enumeration type. Give this error even when parsing
5393 tentatively since it can't possibly be valid--and we're going
5394 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5395 won't get another chance.*/
5396 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5397 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5398 == CPP_SCOPE))
5400 token = cp_lexer_consume_token (parser->lexer);
5401 error_at (token->location, "decltype evaluates to %qT, "
5402 "which is not a class or enumeration type",
5403 token->u.value);
5404 parser->scope = error_mark_node;
5405 error_p = true;
5406 /* As below. */
5407 success = true;
5408 cp_lexer_consume_token (parser->lexer);
5411 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5412 break;
5413 /* If the next token is an identifier, and the one after
5414 that is a `::', then any valid interpretation would have
5415 found a class-or-namespace-name. */
5416 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5417 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5418 == CPP_SCOPE)
5419 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5420 != CPP_COMPL))
5422 token = cp_lexer_consume_token (parser->lexer);
5423 if (!error_p)
5425 if (!token->error_reported)
5427 tree decl;
5428 tree ambiguous_decls;
5430 decl = cp_parser_lookup_name (parser, token->u.value,
5431 none_type,
5432 /*is_template=*/false,
5433 /*is_namespace=*/false,
5434 /*check_dependency=*/true,
5435 &ambiguous_decls,
5436 token->location);
5437 if (TREE_CODE (decl) == TEMPLATE_DECL)
5438 error_at (token->location,
5439 "%qD used without template parameters",
5440 decl);
5441 else if (ambiguous_decls)
5443 // cp_parser_lookup_name has the same diagnostic,
5444 // thus make sure to emit it at most once.
5445 if (cp_parser_uncommitted_to_tentative_parse_p
5446 (parser))
5448 error_at (token->location,
5449 "reference to %qD is ambiguous",
5450 token->u.value);
5451 print_candidates (ambiguous_decls);
5453 decl = error_mark_node;
5455 else
5457 if (cxx_dialect != cxx98)
5458 cp_parser_name_lookup_error
5459 (parser, token->u.value, decl, NLE_NOT_CXX98,
5460 token->location);
5461 else
5462 cp_parser_name_lookup_error
5463 (parser, token->u.value, decl, NLE_CXX98,
5464 token->location);
5467 parser->scope = error_mark_node;
5468 error_p = true;
5469 /* Treat this as a successful nested-name-specifier
5470 due to:
5472 [basic.lookup.qual]
5474 If the name found is not a class-name (clause
5475 _class_) or namespace-name (_namespace.def_), the
5476 program is ill-formed. */
5477 success = true;
5479 cp_lexer_consume_token (parser->lexer);
5481 break;
5483 /* We've found one valid nested-name-specifier. */
5484 success = true;
5485 /* Name lookup always gives us a DECL. */
5486 if (TREE_CODE (new_scope) == TYPE_DECL)
5487 new_scope = TREE_TYPE (new_scope);
5488 /* Uses of "template" must be followed by actual templates. */
5489 if (template_keyword_p
5490 && !(CLASS_TYPE_P (new_scope)
5491 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5492 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5493 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5494 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5495 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5496 == TEMPLATE_ID_EXPR)))
5497 permerror (input_location, TYPE_P (new_scope)
5498 ? G_("%qT is not a template")
5499 : G_("%qD is not a template"),
5500 new_scope);
5501 /* If it is a class scope, try to complete it; we are about to
5502 be looking up names inside the class. */
5503 if (TYPE_P (new_scope)
5504 /* Since checking types for dependency can be expensive,
5505 avoid doing it if the type is already complete. */
5506 && !COMPLETE_TYPE_P (new_scope)
5507 /* Do not try to complete dependent types. */
5508 && !dependent_type_p (new_scope))
5510 new_scope = complete_type (new_scope);
5511 /* If it is a typedef to current class, use the current
5512 class instead, as the typedef won't have any names inside
5513 it yet. */
5514 if (!COMPLETE_TYPE_P (new_scope)
5515 && currently_open_class (new_scope))
5516 new_scope = TYPE_MAIN_VARIANT (new_scope);
5518 /* Make sure we look in the right scope the next time through
5519 the loop. */
5520 parser->scope = new_scope;
5523 /* If parsing tentatively, replace the sequence of tokens that makes
5524 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5525 token. That way, should we re-parse the token stream, we will
5526 not have to repeat the effort required to do the parse, nor will
5527 we issue duplicate error messages. */
5528 if (success && start)
5530 cp_token *token;
5532 token = cp_lexer_token_at (parser->lexer, start);
5533 /* Reset the contents of the START token. */
5534 token->type = CPP_NESTED_NAME_SPECIFIER;
5535 /* Retrieve any deferred checks. Do not pop this access checks yet
5536 so the memory will not be reclaimed during token replacing below. */
5537 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5538 token->u.tree_check_value->value = parser->scope;
5539 token->u.tree_check_value->checks = get_deferred_access_checks ();
5540 token->u.tree_check_value->qualifying_scope =
5541 parser->qualifying_scope;
5542 token->keyword = RID_MAX;
5544 /* Purge all subsequent tokens. */
5545 cp_lexer_purge_tokens_after (parser->lexer, start);
5548 if (start)
5549 pop_to_parent_deferring_access_checks ();
5551 return success ? parser->scope : NULL_TREE;
5554 /* Parse a nested-name-specifier. See
5555 cp_parser_nested_name_specifier_opt for details. This function
5556 behaves identically, except that it will an issue an error if no
5557 nested-name-specifier is present. */
5559 static tree
5560 cp_parser_nested_name_specifier (cp_parser *parser,
5561 bool typename_keyword_p,
5562 bool check_dependency_p,
5563 bool type_p,
5564 bool is_declaration)
5566 tree scope;
5568 /* Look for the nested-name-specifier. */
5569 scope = cp_parser_nested_name_specifier_opt (parser,
5570 typename_keyword_p,
5571 check_dependency_p,
5572 type_p,
5573 is_declaration);
5574 /* If it was not present, issue an error message. */
5575 if (!scope)
5577 cp_parser_error (parser, "expected nested-name-specifier");
5578 parser->scope = NULL_TREE;
5581 return scope;
5584 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5585 this is either a class-name or a namespace-name (which corresponds
5586 to the class-or-namespace-name production in the grammar). For
5587 C++0x, it can also be a type-name that refers to an enumeration
5588 type or a simple-template-id.
5590 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5591 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5592 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5593 TYPE_P is TRUE iff the next name should be taken as a class-name,
5594 even the same name is declared to be another entity in the same
5595 scope.
5597 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5598 specified by the class-or-namespace-name. If neither is found the
5599 ERROR_MARK_NODE is returned. */
5601 static tree
5602 cp_parser_qualifying_entity (cp_parser *parser,
5603 bool typename_keyword_p,
5604 bool template_keyword_p,
5605 bool check_dependency_p,
5606 bool type_p,
5607 bool is_declaration)
5609 tree saved_scope;
5610 tree saved_qualifying_scope;
5611 tree saved_object_scope;
5612 tree scope;
5613 bool only_class_p;
5614 bool successful_parse_p;
5616 /* DR 743: decltype can appear in a nested-name-specifier. */
5617 if (cp_lexer_next_token_is_decltype (parser->lexer))
5619 scope = cp_parser_decltype (parser);
5620 if (TREE_CODE (scope) != ENUMERAL_TYPE
5621 && !MAYBE_CLASS_TYPE_P (scope))
5623 cp_parser_simulate_error (parser);
5624 return error_mark_node;
5626 if (TYPE_NAME (scope))
5627 scope = TYPE_NAME (scope);
5628 return scope;
5631 /* Before we try to parse the class-name, we must save away the
5632 current PARSER->SCOPE since cp_parser_class_name will destroy
5633 it. */
5634 saved_scope = parser->scope;
5635 saved_qualifying_scope = parser->qualifying_scope;
5636 saved_object_scope = parser->object_scope;
5637 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5638 there is no need to look for a namespace-name. */
5639 only_class_p = template_keyword_p
5640 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5641 if (!only_class_p)
5642 cp_parser_parse_tentatively (parser);
5643 scope = cp_parser_class_name (parser,
5644 typename_keyword_p,
5645 template_keyword_p,
5646 type_p ? class_type : none_type,
5647 check_dependency_p,
5648 /*class_head_p=*/false,
5649 is_declaration);
5650 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5651 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5652 if (!only_class_p
5653 && cxx_dialect != cxx98
5654 && !successful_parse_p)
5656 /* Restore the saved scope. */
5657 parser->scope = saved_scope;
5658 parser->qualifying_scope = saved_qualifying_scope;
5659 parser->object_scope = saved_object_scope;
5661 /* Parse tentatively. */
5662 cp_parser_parse_tentatively (parser);
5664 /* Parse a type-name */
5665 scope = cp_parser_type_name (parser);
5667 /* "If the name found does not designate a namespace or a class,
5668 enumeration, or dependent type, the program is ill-formed."
5670 We cover classes and dependent types above and namespaces below,
5671 so this code is only looking for enums. */
5672 if (!scope || TREE_CODE (scope) != TYPE_DECL
5673 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5674 cp_parser_simulate_error (parser);
5676 successful_parse_p = cp_parser_parse_definitely (parser);
5678 /* If that didn't work, try for a namespace-name. */
5679 if (!only_class_p && !successful_parse_p)
5681 /* Restore the saved scope. */
5682 parser->scope = saved_scope;
5683 parser->qualifying_scope = saved_qualifying_scope;
5684 parser->object_scope = saved_object_scope;
5685 /* If we are not looking at an identifier followed by the scope
5686 resolution operator, then this is not part of a
5687 nested-name-specifier. (Note that this function is only used
5688 to parse the components of a nested-name-specifier.) */
5689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5690 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5691 return error_mark_node;
5692 scope = cp_parser_namespace_name (parser);
5695 return scope;
5698 /* Return true if we are looking at a compound-literal, false otherwise. */
5700 static bool
5701 cp_parser_compound_literal_p (cp_parser *parser)
5703 /* Consume the `('. */
5704 cp_lexer_consume_token (parser->lexer);
5706 cp_lexer_save_tokens (parser->lexer);
5708 /* Skip tokens until the next token is a closing parenthesis.
5709 If we find the closing `)', and the next token is a `{', then
5710 we are looking at a compound-literal. */
5711 bool compound_literal_p
5712 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5713 /*consume_paren=*/true)
5714 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5716 /* Roll back the tokens we skipped. */
5717 cp_lexer_rollback_tokens (parser->lexer);
5719 return compound_literal_p;
5722 /* Parse a postfix-expression.
5724 postfix-expression:
5725 primary-expression
5726 postfix-expression [ expression ]
5727 postfix-expression ( expression-list [opt] )
5728 simple-type-specifier ( expression-list [opt] )
5729 typename :: [opt] nested-name-specifier identifier
5730 ( expression-list [opt] )
5731 typename :: [opt] nested-name-specifier template [opt] template-id
5732 ( expression-list [opt] )
5733 postfix-expression . template [opt] id-expression
5734 postfix-expression -> template [opt] id-expression
5735 postfix-expression . pseudo-destructor-name
5736 postfix-expression -> pseudo-destructor-name
5737 postfix-expression ++
5738 postfix-expression --
5739 dynamic_cast < type-id > ( expression )
5740 static_cast < type-id > ( expression )
5741 reinterpret_cast < type-id > ( expression )
5742 const_cast < type-id > ( expression )
5743 typeid ( expression )
5744 typeid ( type-id )
5746 GNU Extension:
5748 postfix-expression:
5749 ( type-id ) { initializer-list , [opt] }
5751 This extension is a GNU version of the C99 compound-literal
5752 construct. (The C99 grammar uses `type-name' instead of `type-id',
5753 but they are essentially the same concept.)
5755 If ADDRESS_P is true, the postfix expression is the operand of the
5756 `&' operator. CAST_P is true if this expression is the target of a
5757 cast.
5759 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5760 class member access expressions [expr.ref].
5762 Returns a representation of the expression. */
5764 static tree
5765 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5766 bool member_access_only_p, bool decltype_p,
5767 cp_id_kind * pidk_return)
5769 cp_token *token;
5770 location_t loc;
5771 enum rid keyword;
5772 cp_id_kind idk = CP_ID_KIND_NONE;
5773 tree postfix_expression = NULL_TREE;
5774 bool is_member_access = false;
5775 int saved_in_statement = -1;
5777 /* Peek at the next token. */
5778 token = cp_lexer_peek_token (parser->lexer);
5779 loc = token->location;
5780 /* Some of the productions are determined by keywords. */
5781 keyword = token->keyword;
5782 switch (keyword)
5784 case RID_DYNCAST:
5785 case RID_STATCAST:
5786 case RID_REINTCAST:
5787 case RID_CONSTCAST:
5789 tree type;
5790 tree expression;
5791 const char *saved_message;
5792 bool saved_in_type_id_in_expr_p;
5794 /* All of these can be handled in the same way from the point
5795 of view of parsing. Begin by consuming the token
5796 identifying the cast. */
5797 cp_lexer_consume_token (parser->lexer);
5799 /* New types cannot be defined in the cast. */
5800 saved_message = parser->type_definition_forbidden_message;
5801 parser->type_definition_forbidden_message
5802 = G_("types may not be defined in casts");
5804 /* Look for the opening `<'. */
5805 cp_parser_require (parser, CPP_LESS, RT_LESS);
5806 /* Parse the type to which we are casting. */
5807 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5808 parser->in_type_id_in_expr_p = true;
5809 type = cp_parser_type_id (parser);
5810 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5811 /* Look for the closing `>'. */
5812 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5813 /* Restore the old message. */
5814 parser->type_definition_forbidden_message = saved_message;
5816 bool saved_greater_than_is_operator_p
5817 = parser->greater_than_is_operator_p;
5818 parser->greater_than_is_operator_p = true;
5820 /* And the expression which is being cast. */
5821 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5822 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5823 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5825 parser->greater_than_is_operator_p
5826 = saved_greater_than_is_operator_p;
5828 /* Only type conversions to integral or enumeration types
5829 can be used in constant-expressions. */
5830 if (!cast_valid_in_integral_constant_expression_p (type)
5831 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5832 return error_mark_node;
5834 switch (keyword)
5836 case RID_DYNCAST:
5837 postfix_expression
5838 = build_dynamic_cast (type, expression, tf_warning_or_error);
5839 break;
5840 case RID_STATCAST:
5841 postfix_expression
5842 = build_static_cast (type, expression, tf_warning_or_error);
5843 break;
5844 case RID_REINTCAST:
5845 postfix_expression
5846 = build_reinterpret_cast (type, expression,
5847 tf_warning_or_error);
5848 break;
5849 case RID_CONSTCAST:
5850 postfix_expression
5851 = build_const_cast (type, expression, tf_warning_or_error);
5852 break;
5853 default:
5854 gcc_unreachable ();
5857 break;
5859 case RID_TYPEID:
5861 tree type;
5862 const char *saved_message;
5863 bool saved_in_type_id_in_expr_p;
5865 /* Consume the `typeid' token. */
5866 cp_lexer_consume_token (parser->lexer);
5867 /* Look for the `(' token. */
5868 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5869 /* Types cannot be defined in a `typeid' expression. */
5870 saved_message = parser->type_definition_forbidden_message;
5871 parser->type_definition_forbidden_message
5872 = G_("types may not be defined in a %<typeid%> expression");
5873 /* We can't be sure yet whether we're looking at a type-id or an
5874 expression. */
5875 cp_parser_parse_tentatively (parser);
5876 /* Try a type-id first. */
5877 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5878 parser->in_type_id_in_expr_p = true;
5879 type = cp_parser_type_id (parser);
5880 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5881 /* Look for the `)' token. Otherwise, we can't be sure that
5882 we're not looking at an expression: consider `typeid (int
5883 (3))', for example. */
5884 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5885 /* If all went well, simply lookup the type-id. */
5886 if (cp_parser_parse_definitely (parser))
5887 postfix_expression = get_typeid (type, tf_warning_or_error);
5888 /* Otherwise, fall back to the expression variant. */
5889 else
5891 tree expression;
5893 /* Look for an expression. */
5894 expression = cp_parser_expression (parser, & idk);
5895 /* Compute its typeid. */
5896 postfix_expression = build_typeid (expression, tf_warning_or_error);
5897 /* Look for the `)' token. */
5898 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5900 /* Restore the saved message. */
5901 parser->type_definition_forbidden_message = saved_message;
5902 /* `typeid' may not appear in an integral constant expression. */
5903 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5904 return error_mark_node;
5906 break;
5908 case RID_TYPENAME:
5910 tree type;
5911 /* The syntax permitted here is the same permitted for an
5912 elaborated-type-specifier. */
5913 type = cp_parser_elaborated_type_specifier (parser,
5914 /*is_friend=*/false,
5915 /*is_declaration=*/false);
5916 postfix_expression = cp_parser_functional_cast (parser, type);
5918 break;
5920 case RID_CILK_SPAWN:
5922 cp_lexer_consume_token (parser->lexer);
5923 token = cp_lexer_peek_token (parser->lexer);
5924 if (token->type == CPP_SEMICOLON)
5926 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5927 "an expression");
5928 postfix_expression = error_mark_node;
5929 break;
5931 else if (!current_function_decl)
5933 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5934 "inside a function");
5935 postfix_expression = error_mark_node;
5936 break;
5938 else
5940 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5941 saved_in_statement = parser->in_statement;
5942 parser->in_statement |= IN_CILK_SPAWN;
5944 cfun->calls_cilk_spawn = 1;
5945 postfix_expression =
5946 cp_parser_postfix_expression (parser, false, false,
5947 false, false, &idk);
5948 if (!flag_cilkplus)
5950 error_at (token->location, "-fcilkplus must be enabled to use"
5951 " %<_Cilk_spawn%>");
5952 cfun->calls_cilk_spawn = 0;
5954 else if (saved_in_statement & IN_CILK_SPAWN)
5956 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5957 "are not permitted");
5958 postfix_expression = error_mark_node;
5959 cfun->calls_cilk_spawn = 0;
5961 else
5963 postfix_expression = build_cilk_spawn (token->location,
5964 postfix_expression);
5965 if (postfix_expression != error_mark_node)
5966 SET_EXPR_LOCATION (postfix_expression, input_location);
5967 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5969 break;
5972 case RID_BUILTIN_SHUFFLE:
5974 vec<tree, va_gc> *vec;
5975 unsigned int i;
5976 tree p;
5978 cp_lexer_consume_token (parser->lexer);
5979 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5980 /*cast_p=*/false, /*allow_expansion_p=*/true,
5981 /*non_constant_p=*/NULL);
5982 if (vec == NULL)
5983 return error_mark_node;
5985 FOR_EACH_VEC_ELT (*vec, i, p)
5986 mark_exp_read (p);
5988 if (vec->length () == 2)
5989 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5990 tf_warning_or_error);
5991 else if (vec->length () == 3)
5992 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5993 tf_warning_or_error);
5994 else
5996 error_at (loc, "wrong number of arguments to "
5997 "%<__builtin_shuffle%>");
5998 return error_mark_node;
6000 break;
6003 default:
6005 tree type;
6007 /* If the next thing is a simple-type-specifier, we may be
6008 looking at a functional cast. We could also be looking at
6009 an id-expression. So, we try the functional cast, and if
6010 that doesn't work we fall back to the primary-expression. */
6011 cp_parser_parse_tentatively (parser);
6012 /* Look for the simple-type-specifier. */
6013 type = cp_parser_simple_type_specifier (parser,
6014 /*decl_specs=*/NULL,
6015 CP_PARSER_FLAGS_NONE);
6016 /* Parse the cast itself. */
6017 if (!cp_parser_error_occurred (parser))
6018 postfix_expression
6019 = cp_parser_functional_cast (parser, type);
6020 /* If that worked, we're done. */
6021 if (cp_parser_parse_definitely (parser))
6022 break;
6024 /* If the functional-cast didn't work out, try a
6025 compound-literal. */
6026 if (cp_parser_allow_gnu_extensions_p (parser)
6027 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6029 tree initializer = NULL_TREE;
6031 cp_parser_parse_tentatively (parser);
6033 /* Avoid calling cp_parser_type_id pointlessly, see comment
6034 in cp_parser_cast_expression about c++/29234. */
6035 if (!cp_parser_compound_literal_p (parser))
6036 cp_parser_simulate_error (parser);
6037 else
6039 /* Parse the type. */
6040 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6041 parser->in_type_id_in_expr_p = true;
6042 type = cp_parser_type_id (parser);
6043 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6044 /* Look for the `)'. */
6045 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6048 /* If things aren't going well, there's no need to
6049 keep going. */
6050 if (!cp_parser_error_occurred (parser))
6052 bool non_constant_p;
6053 /* Parse the brace-enclosed initializer list. */
6054 initializer = cp_parser_braced_list (parser,
6055 &non_constant_p);
6057 /* If that worked, we're definitely looking at a
6058 compound-literal expression. */
6059 if (cp_parser_parse_definitely (parser))
6061 /* Warn the user that a compound literal is not
6062 allowed in standard C++. */
6063 pedwarn (input_location, OPT_Wpedantic,
6064 "ISO C++ forbids compound-literals");
6065 /* For simplicity, we disallow compound literals in
6066 constant-expressions. We could
6067 allow compound literals of integer type, whose
6068 initializer was a constant, in constant
6069 expressions. Permitting that usage, as a further
6070 extension, would not change the meaning of any
6071 currently accepted programs. (Of course, as
6072 compound literals are not part of ISO C++, the
6073 standard has nothing to say.) */
6074 if (cp_parser_non_integral_constant_expression (parser,
6075 NIC_NCC))
6077 postfix_expression = error_mark_node;
6078 break;
6080 /* Form the representation of the compound-literal. */
6081 postfix_expression
6082 = finish_compound_literal (type, initializer,
6083 tf_warning_or_error);
6084 break;
6088 /* It must be a primary-expression. */
6089 postfix_expression
6090 = cp_parser_primary_expression (parser, address_p, cast_p,
6091 /*template_arg_p=*/false,
6092 decltype_p,
6093 &idk);
6095 break;
6098 /* Note that we don't need to worry about calling build_cplus_new on a
6099 class-valued CALL_EXPR in decltype when it isn't the end of the
6100 postfix-expression; unary_complex_lvalue will take care of that for
6101 all these cases. */
6103 /* Keep looping until the postfix-expression is complete. */
6104 while (true)
6106 if (idk == CP_ID_KIND_UNQUALIFIED
6107 && identifier_p (postfix_expression)
6108 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6109 /* It is not a Koenig lookup function call. */
6110 postfix_expression
6111 = unqualified_name_lookup_error (postfix_expression);
6113 /* Peek at the next token. */
6114 token = cp_lexer_peek_token (parser->lexer);
6116 switch (token->type)
6118 case CPP_OPEN_SQUARE:
6119 if (cp_next_tokens_can_be_std_attribute_p (parser))
6121 cp_parser_error (parser,
6122 "two consecutive %<[%> shall "
6123 "only introduce an attribute");
6124 return error_mark_node;
6126 postfix_expression
6127 = cp_parser_postfix_open_square_expression (parser,
6128 postfix_expression,
6129 false,
6130 decltype_p);
6131 idk = CP_ID_KIND_NONE;
6132 is_member_access = false;
6133 break;
6135 case CPP_OPEN_PAREN:
6136 /* postfix-expression ( expression-list [opt] ) */
6138 bool koenig_p;
6139 bool is_builtin_constant_p;
6140 bool saved_integral_constant_expression_p = false;
6141 bool saved_non_integral_constant_expression_p = false;
6142 tsubst_flags_t complain = complain_flags (decltype_p);
6143 vec<tree, va_gc> *args;
6145 is_member_access = false;
6147 is_builtin_constant_p
6148 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6149 if (is_builtin_constant_p)
6151 /* The whole point of __builtin_constant_p is to allow
6152 non-constant expressions to appear as arguments. */
6153 saved_integral_constant_expression_p
6154 = parser->integral_constant_expression_p;
6155 saved_non_integral_constant_expression_p
6156 = parser->non_integral_constant_expression_p;
6157 parser->integral_constant_expression_p = false;
6159 args = (cp_parser_parenthesized_expression_list
6160 (parser, non_attr,
6161 /*cast_p=*/false, /*allow_expansion_p=*/true,
6162 /*non_constant_p=*/NULL,
6163 /*want_literal_zero_p=*/warn_memset_transposed_args));
6164 if (is_builtin_constant_p)
6166 parser->integral_constant_expression_p
6167 = saved_integral_constant_expression_p;
6168 parser->non_integral_constant_expression_p
6169 = saved_non_integral_constant_expression_p;
6172 if (args == NULL)
6174 postfix_expression = error_mark_node;
6175 break;
6178 /* Function calls are not permitted in
6179 constant-expressions. */
6180 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6181 && cp_parser_non_integral_constant_expression (parser,
6182 NIC_FUNC_CALL))
6184 postfix_expression = error_mark_node;
6185 release_tree_vector (args);
6186 break;
6189 koenig_p = false;
6190 if (idk == CP_ID_KIND_UNQUALIFIED
6191 || idk == CP_ID_KIND_TEMPLATE_ID)
6193 if (identifier_p (postfix_expression))
6195 if (!args->is_empty ())
6197 koenig_p = true;
6198 if (!any_type_dependent_arguments_p (args))
6199 postfix_expression
6200 = perform_koenig_lookup (postfix_expression, args,
6201 complain);
6203 else
6204 postfix_expression
6205 = unqualified_fn_lookup_error (postfix_expression);
6207 /* We do not perform argument-dependent lookup if
6208 normal lookup finds a non-function, in accordance
6209 with the expected resolution of DR 218. */
6210 else if (!args->is_empty ()
6211 && is_overloaded_fn (postfix_expression))
6213 tree fn = get_first_fn (postfix_expression);
6214 fn = STRIP_TEMPLATE (fn);
6216 /* Do not do argument dependent lookup if regular
6217 lookup finds a member function or a block-scope
6218 function declaration. [basic.lookup.argdep]/3 */
6219 if (!DECL_FUNCTION_MEMBER_P (fn)
6220 && !DECL_LOCAL_FUNCTION_P (fn))
6222 koenig_p = true;
6223 if (!any_type_dependent_arguments_p (args))
6224 postfix_expression
6225 = perform_koenig_lookup (postfix_expression, args,
6226 complain);
6231 if (warn_memset_transposed_args)
6233 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6234 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6235 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6236 && vec_safe_length (args) == 3
6237 && integer_zerop ((*args)[2])
6238 && LITERAL_ZERO_P ((*args)[2])
6239 && !(integer_zerop ((*args)[1])
6240 && LITERAL_ZERO_P ((*args)[1])))
6241 warning (OPT_Wmemset_transposed_args,
6242 "%<memset%> used with constant zero length "
6243 "parameter; this could be due to transposed "
6244 "parameters");
6246 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6247 to avoid leaking those into folder and middle-end. */
6248 unsigned int i;
6249 tree arg;
6250 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6251 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6252 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6255 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6257 tree instance = TREE_OPERAND (postfix_expression, 0);
6258 tree fn = TREE_OPERAND (postfix_expression, 1);
6260 if (processing_template_decl
6261 && (type_dependent_expression_p (instance)
6262 || (!BASELINK_P (fn)
6263 && TREE_CODE (fn) != FIELD_DECL)
6264 || type_dependent_expression_p (fn)
6265 || any_type_dependent_arguments_p (args)))
6267 postfix_expression
6268 = build_nt_call_vec (postfix_expression, args);
6269 release_tree_vector (args);
6270 break;
6273 if (BASELINK_P (fn))
6275 postfix_expression
6276 = (build_new_method_call
6277 (instance, fn, &args, NULL_TREE,
6278 (idk == CP_ID_KIND_QUALIFIED
6279 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6280 : LOOKUP_NORMAL),
6281 /*fn_p=*/NULL,
6282 complain));
6284 else
6285 postfix_expression
6286 = finish_call_expr (postfix_expression, &args,
6287 /*disallow_virtual=*/false,
6288 /*koenig_p=*/false,
6289 complain);
6291 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6292 || TREE_CODE (postfix_expression) == MEMBER_REF
6293 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6294 postfix_expression = (build_offset_ref_call_from_tree
6295 (postfix_expression, &args,
6296 complain));
6297 else if (idk == CP_ID_KIND_QUALIFIED)
6298 /* A call to a static class member, or a namespace-scope
6299 function. */
6300 postfix_expression
6301 = finish_call_expr (postfix_expression, &args,
6302 /*disallow_virtual=*/true,
6303 koenig_p,
6304 complain);
6305 else
6306 /* All other function calls. */
6307 postfix_expression
6308 = finish_call_expr (postfix_expression, &args,
6309 /*disallow_virtual=*/false,
6310 koenig_p,
6311 complain);
6313 protected_set_expr_location (postfix_expression, token->location);
6315 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6316 idk = CP_ID_KIND_NONE;
6318 release_tree_vector (args);
6320 break;
6322 case CPP_DOT:
6323 case CPP_DEREF:
6324 /* postfix-expression . template [opt] id-expression
6325 postfix-expression . pseudo-destructor-name
6326 postfix-expression -> template [opt] id-expression
6327 postfix-expression -> pseudo-destructor-name */
6329 /* Consume the `.' or `->' operator. */
6330 cp_lexer_consume_token (parser->lexer);
6332 postfix_expression
6333 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6334 postfix_expression,
6335 false, &idk, loc);
6337 is_member_access = true;
6338 break;
6340 case CPP_PLUS_PLUS:
6341 /* postfix-expression ++ */
6342 /* Consume the `++' token. */
6343 cp_lexer_consume_token (parser->lexer);
6344 /* Generate a representation for the complete expression. */
6345 postfix_expression
6346 = finish_increment_expr (postfix_expression,
6347 POSTINCREMENT_EXPR);
6348 /* Increments may not appear in constant-expressions. */
6349 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6350 postfix_expression = error_mark_node;
6351 idk = CP_ID_KIND_NONE;
6352 is_member_access = false;
6353 break;
6355 case CPP_MINUS_MINUS:
6356 /* postfix-expression -- */
6357 /* Consume the `--' token. */
6358 cp_lexer_consume_token (parser->lexer);
6359 /* Generate a representation for the complete expression. */
6360 postfix_expression
6361 = finish_increment_expr (postfix_expression,
6362 POSTDECREMENT_EXPR);
6363 /* Decrements may not appear in constant-expressions. */
6364 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6365 postfix_expression = error_mark_node;
6366 idk = CP_ID_KIND_NONE;
6367 is_member_access = false;
6368 break;
6370 default:
6371 if (pidk_return != NULL)
6372 * pidk_return = idk;
6373 if (member_access_only_p)
6374 return is_member_access? postfix_expression : error_mark_node;
6375 else
6376 return postfix_expression;
6380 /* We should never get here. */
6381 gcc_unreachable ();
6382 return error_mark_node;
6385 /* This function parses Cilk Plus array notations. If a normal array expr. is
6386 parsed then the array index is passed back to the caller through *INIT_INDEX
6387 and the function returns a NULL_TREE. If array notation expr. is parsed,
6388 then *INIT_INDEX is ignored by the caller and the function returns
6389 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6390 error_mark_node. */
6392 static tree
6393 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6394 tree array_value)
6396 cp_token *token = NULL;
6397 tree length_index, stride = NULL_TREE, value_tree, array_type;
6398 if (!array_value || array_value == error_mark_node)
6400 cp_parser_skip_to_end_of_statement (parser);
6401 return error_mark_node;
6404 array_type = TREE_TYPE (array_value);
6406 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6407 parser->colon_corrects_to_scope_p = false;
6408 token = cp_lexer_peek_token (parser->lexer);
6410 if (!token)
6412 cp_parser_error (parser, "expected %<:%> or numeral");
6413 return error_mark_node;
6415 else if (token->type == CPP_COLON)
6417 /* Consume the ':'. */
6418 cp_lexer_consume_token (parser->lexer);
6420 /* If we are here, then we have a case like this A[:]. */
6421 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6423 cp_parser_error (parser, "expected %<]%>");
6424 cp_parser_skip_to_end_of_statement (parser);
6425 return error_mark_node;
6427 *init_index = NULL_TREE;
6428 stride = NULL_TREE;
6429 length_index = NULL_TREE;
6431 else
6433 /* If we are here, then there are three valid possibilities:
6434 1. ARRAY [ EXP ]
6435 2. ARRAY [ EXP : EXP ]
6436 3. ARRAY [ EXP : EXP : EXP ] */
6438 *init_index = cp_parser_expression (parser);
6439 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6441 /* This indicates that we have a normal array expression. */
6442 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6443 return NULL_TREE;
6446 /* Consume the ':'. */
6447 cp_lexer_consume_token (parser->lexer);
6448 length_index = cp_parser_expression (parser);
6449 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6451 cp_lexer_consume_token (parser->lexer);
6452 stride = cp_parser_expression (parser);
6455 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6457 if (*init_index == error_mark_node || length_index == error_mark_node
6458 || stride == error_mark_node || array_type == error_mark_node)
6460 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6461 cp_lexer_consume_token (parser->lexer);
6462 return error_mark_node;
6464 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6466 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6467 length_index, stride, array_type);
6468 return value_tree;
6471 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6472 by cp_parser_builtin_offsetof. We're looking for
6474 postfix-expression [ expression ]
6475 postfix-expression [ braced-init-list ] (C++11)
6477 FOR_OFFSETOF is set if we're being called in that context, which
6478 changes how we deal with integer constant expressions. */
6480 static tree
6481 cp_parser_postfix_open_square_expression (cp_parser *parser,
6482 tree postfix_expression,
6483 bool for_offsetof,
6484 bool decltype_p)
6486 tree index = NULL_TREE;
6487 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6488 bool saved_greater_than_is_operator_p;
6490 /* Consume the `[' token. */
6491 cp_lexer_consume_token (parser->lexer);
6493 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6494 parser->greater_than_is_operator_p = true;
6496 /* Parse the index expression. */
6497 /* ??? For offsetof, there is a question of what to allow here. If
6498 offsetof is not being used in an integral constant expression context,
6499 then we *could* get the right answer by computing the value at runtime.
6500 If we are in an integral constant expression context, then we might
6501 could accept any constant expression; hard to say without analysis.
6502 Rather than open the barn door too wide right away, allow only integer
6503 constant expressions here. */
6504 if (for_offsetof)
6505 index = cp_parser_constant_expression (parser);
6506 else
6508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6510 bool expr_nonconst_p;
6511 cp_lexer_set_source_position (parser->lexer);
6512 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6513 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6514 if (flag_cilkplus
6515 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6517 error_at (cp_lexer_peek_token (parser->lexer)->location,
6518 "braced list index is not allowed with array "
6519 "notation");
6520 cp_parser_skip_to_end_of_statement (parser);
6521 return error_mark_node;
6524 else if (flag_cilkplus)
6526 /* Here are have these two options:
6527 ARRAY[EXP : EXP] - Array notation expr with default
6528 stride of 1.
6529 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6530 stride. */
6531 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6532 postfix_expression);
6533 if (an_exp)
6534 return an_exp;
6536 else
6537 index = cp_parser_expression (parser);
6540 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6542 /* Look for the closing `]'. */
6543 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6545 /* Build the ARRAY_REF. */
6546 postfix_expression = grok_array_decl (loc, postfix_expression,
6547 index, decltype_p);
6549 /* When not doing offsetof, array references are not permitted in
6550 constant-expressions. */
6551 if (!for_offsetof
6552 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6553 postfix_expression = error_mark_node;
6555 return postfix_expression;
6558 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6559 by cp_parser_builtin_offsetof. We're looking for
6561 postfix-expression . template [opt] id-expression
6562 postfix-expression . pseudo-destructor-name
6563 postfix-expression -> template [opt] id-expression
6564 postfix-expression -> pseudo-destructor-name
6566 FOR_OFFSETOF is set if we're being called in that context. That sorta
6567 limits what of the above we'll actually accept, but nevermind.
6568 TOKEN_TYPE is the "." or "->" token, which will already have been
6569 removed from the stream. */
6571 static tree
6572 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6573 enum cpp_ttype token_type,
6574 tree postfix_expression,
6575 bool for_offsetof, cp_id_kind *idk,
6576 location_t location)
6578 tree name;
6579 bool dependent_p;
6580 bool pseudo_destructor_p;
6581 tree scope = NULL_TREE;
6583 /* If this is a `->' operator, dereference the pointer. */
6584 if (token_type == CPP_DEREF)
6585 postfix_expression = build_x_arrow (location, postfix_expression,
6586 tf_warning_or_error);
6587 /* Check to see whether or not the expression is type-dependent. */
6588 dependent_p = type_dependent_expression_p (postfix_expression);
6589 /* The identifier following the `->' or `.' is not qualified. */
6590 parser->scope = NULL_TREE;
6591 parser->qualifying_scope = NULL_TREE;
6592 parser->object_scope = NULL_TREE;
6593 *idk = CP_ID_KIND_NONE;
6595 /* Enter the scope corresponding to the type of the object
6596 given by the POSTFIX_EXPRESSION. */
6597 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6599 scope = TREE_TYPE (postfix_expression);
6600 /* According to the standard, no expression should ever have
6601 reference type. Unfortunately, we do not currently match
6602 the standard in this respect in that our internal representation
6603 of an expression may have reference type even when the standard
6604 says it does not. Therefore, we have to manually obtain the
6605 underlying type here. */
6606 scope = non_reference (scope);
6607 /* The type of the POSTFIX_EXPRESSION must be complete. */
6608 if (scope == unknown_type_node)
6610 error_at (location, "%qE does not have class type",
6611 postfix_expression);
6612 scope = NULL_TREE;
6614 /* Unlike the object expression in other contexts, *this is not
6615 required to be of complete type for purposes of class member
6616 access (5.2.5) outside the member function body. */
6617 else if (postfix_expression != current_class_ref
6618 && !(processing_template_decl && scope == current_class_type))
6619 scope = complete_type_or_else (scope, NULL_TREE);
6620 /* Let the name lookup machinery know that we are processing a
6621 class member access expression. */
6622 parser->context->object_type = scope;
6623 /* If something went wrong, we want to be able to discern that case,
6624 as opposed to the case where there was no SCOPE due to the type
6625 of expression being dependent. */
6626 if (!scope)
6627 scope = error_mark_node;
6628 /* If the SCOPE was erroneous, make the various semantic analysis
6629 functions exit quickly -- and without issuing additional error
6630 messages. */
6631 if (scope == error_mark_node)
6632 postfix_expression = error_mark_node;
6635 /* Assume this expression is not a pseudo-destructor access. */
6636 pseudo_destructor_p = false;
6638 /* If the SCOPE is a scalar type, then, if this is a valid program,
6639 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6640 is type dependent, it can be pseudo-destructor-name or something else.
6641 Try to parse it as pseudo-destructor-name first. */
6642 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6644 tree s;
6645 tree type;
6647 cp_parser_parse_tentatively (parser);
6648 /* Parse the pseudo-destructor-name. */
6649 s = NULL_TREE;
6650 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6651 &s, &type);
6652 if (dependent_p
6653 && (cp_parser_error_occurred (parser)
6654 || !SCALAR_TYPE_P (type)))
6655 cp_parser_abort_tentative_parse (parser);
6656 else if (cp_parser_parse_definitely (parser))
6658 pseudo_destructor_p = true;
6659 postfix_expression
6660 = finish_pseudo_destructor_expr (postfix_expression,
6661 s, type, location);
6665 if (!pseudo_destructor_p)
6667 /* If the SCOPE is not a scalar type, we are looking at an
6668 ordinary class member access expression, rather than a
6669 pseudo-destructor-name. */
6670 bool template_p;
6671 cp_token *token = cp_lexer_peek_token (parser->lexer);
6672 /* Parse the id-expression. */
6673 name = (cp_parser_id_expression
6674 (parser,
6675 cp_parser_optional_template_keyword (parser),
6676 /*check_dependency_p=*/true,
6677 &template_p,
6678 /*declarator_p=*/false,
6679 /*optional_p=*/false));
6680 /* In general, build a SCOPE_REF if the member name is qualified.
6681 However, if the name was not dependent and has already been
6682 resolved; there is no need to build the SCOPE_REF. For example;
6684 struct X { void f(); };
6685 template <typename T> void f(T* t) { t->X::f(); }
6687 Even though "t" is dependent, "X::f" is not and has been resolved
6688 to a BASELINK; there is no need to include scope information. */
6690 /* But we do need to remember that there was an explicit scope for
6691 virtual function calls. */
6692 if (parser->scope)
6693 *idk = CP_ID_KIND_QUALIFIED;
6695 /* If the name is a template-id that names a type, we will get a
6696 TYPE_DECL here. That is invalid code. */
6697 if (TREE_CODE (name) == TYPE_DECL)
6699 error_at (token->location, "invalid use of %qD", name);
6700 postfix_expression = error_mark_node;
6702 else
6704 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6706 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6708 error_at (token->location, "%<%D::%D%> is not a class member",
6709 parser->scope, name);
6710 postfix_expression = error_mark_node;
6712 else
6713 name = build_qualified_name (/*type=*/NULL_TREE,
6714 parser->scope,
6715 name,
6716 template_p);
6717 parser->scope = NULL_TREE;
6718 parser->qualifying_scope = NULL_TREE;
6719 parser->object_scope = NULL_TREE;
6721 if (parser->scope && name && BASELINK_P (name))
6722 adjust_result_of_qualified_name_lookup
6723 (name, parser->scope, scope);
6724 postfix_expression
6725 = finish_class_member_access_expr (postfix_expression, name,
6726 template_p,
6727 tf_warning_or_error);
6731 /* We no longer need to look up names in the scope of the object on
6732 the left-hand side of the `.' or `->' operator. */
6733 parser->context->object_type = NULL_TREE;
6735 /* Outside of offsetof, these operators may not appear in
6736 constant-expressions. */
6737 if (!for_offsetof
6738 && (cp_parser_non_integral_constant_expression
6739 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6740 postfix_expression = error_mark_node;
6742 return postfix_expression;
6745 /* Cache of LITERAL_ZERO_P constants. */
6747 static GTY(()) tree literal_zeros[itk_none];
6749 /* Parse a parenthesized expression-list.
6751 expression-list:
6752 assignment-expression
6753 expression-list, assignment-expression
6755 attribute-list:
6756 expression-list
6757 identifier
6758 identifier, expression-list
6760 CAST_P is true if this expression is the target of a cast.
6762 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6763 argument pack.
6765 Returns a vector of trees. Each element is a representation of an
6766 assignment-expression. NULL is returned if the ( and or ) are
6767 missing. An empty, but allocated, vector is returned on no
6768 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6769 if we are parsing an attribute list for an attribute that wants a
6770 plain identifier argument, normal_attr for an attribute that wants
6771 an expression, or non_attr if we aren't parsing an attribute list. If
6772 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6773 not all of the expressions in the list were constant.
6774 WANT_LITERAL_ZERO_P is true if the caller is interested in
6775 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6776 immediately, this can be removed. */
6778 static vec<tree, va_gc> *
6779 cp_parser_parenthesized_expression_list (cp_parser* parser,
6780 int is_attribute_list,
6781 bool cast_p,
6782 bool allow_expansion_p,
6783 bool *non_constant_p,
6784 bool want_literal_zero_p)
6786 vec<tree, va_gc> *expression_list;
6787 bool fold_expr_p = is_attribute_list != non_attr;
6788 tree identifier = NULL_TREE;
6789 bool saved_greater_than_is_operator_p;
6791 /* Assume all the expressions will be constant. */
6792 if (non_constant_p)
6793 *non_constant_p = false;
6795 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6796 return NULL;
6798 expression_list = make_tree_vector ();
6800 /* Within a parenthesized expression, a `>' token is always
6801 the greater-than operator. */
6802 saved_greater_than_is_operator_p
6803 = parser->greater_than_is_operator_p;
6804 parser->greater_than_is_operator_p = true;
6806 /* Consume expressions until there are no more. */
6807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6808 while (true)
6810 tree expr;
6812 /* At the beginning of attribute lists, check to see if the
6813 next token is an identifier. */
6814 if (is_attribute_list == id_attr
6815 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6817 cp_token *token;
6819 /* Consume the identifier. */
6820 token = cp_lexer_consume_token (parser->lexer);
6821 /* Save the identifier. */
6822 identifier = token->u.value;
6824 else
6826 bool expr_non_constant_p;
6828 /* Parse the next assignment-expression. */
6829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6831 /* A braced-init-list. */
6832 cp_lexer_set_source_position (parser->lexer);
6833 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6834 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6835 if (non_constant_p && expr_non_constant_p)
6836 *non_constant_p = true;
6838 else if (non_constant_p)
6840 expr = (cp_parser_constant_expression
6841 (parser, /*allow_non_constant_p=*/true,
6842 &expr_non_constant_p));
6843 if (expr_non_constant_p)
6844 *non_constant_p = true;
6846 else
6848 expr = NULL_TREE;
6849 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6850 switch (tok->type)
6852 case CPP_NUMBER:
6853 case CPP_CHAR:
6854 case CPP_WCHAR:
6855 case CPP_CHAR16:
6856 case CPP_CHAR32:
6857 /* If a parameter is literal zero alone, remember it
6858 for -Wmemset-transposed-args warning. */
6859 if (integer_zerop (tok->u.value)
6860 && !TREE_OVERFLOW (tok->u.value)
6861 && want_literal_zero_p
6862 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6863 == CPP_COMMA
6864 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6865 == CPP_CLOSE_PAREN))
6867 unsigned int i;
6868 for (i = 0; i < itk_none; ++i)
6869 if (TREE_TYPE (tok->u.value) == integer_types[i])
6870 break;
6871 if (i < itk_none && literal_zeros[i])
6872 expr = literal_zeros[i];
6873 else
6875 expr = copy_node (tok->u.value);
6876 LITERAL_ZERO_P (expr) = 1;
6877 if (i < itk_none)
6878 literal_zeros[i] = expr;
6880 /* Consume the 0 token (or '\0', 0LL etc.). */
6881 cp_lexer_consume_token (parser->lexer);
6883 break;
6884 default:
6885 break;
6887 if (expr == NULL_TREE)
6888 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6889 cast_p);
6892 if (fold_expr_p)
6893 expr = instantiate_non_dependent_expr (expr);
6895 /* If we have an ellipsis, then this is an expression
6896 expansion. */
6897 if (allow_expansion_p
6898 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6900 /* Consume the `...'. */
6901 cp_lexer_consume_token (parser->lexer);
6903 /* Build the argument pack. */
6904 expr = make_pack_expansion (expr);
6907 /* Add it to the list. We add error_mark_node
6908 expressions to the list, so that we can still tell if
6909 the correct form for a parenthesized expression-list
6910 is found. That gives better errors. */
6911 vec_safe_push (expression_list, expr);
6913 if (expr == error_mark_node)
6914 goto skip_comma;
6917 /* After the first item, attribute lists look the same as
6918 expression lists. */
6919 is_attribute_list = non_attr;
6921 get_comma:;
6922 /* If the next token isn't a `,', then we are done. */
6923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6924 break;
6926 /* Otherwise, consume the `,' and keep going. */
6927 cp_lexer_consume_token (parser->lexer);
6930 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6932 int ending;
6934 skip_comma:;
6935 /* We try and resync to an unnested comma, as that will give the
6936 user better diagnostics. */
6937 ending = cp_parser_skip_to_closing_parenthesis (parser,
6938 /*recovering=*/true,
6939 /*or_comma=*/true,
6940 /*consume_paren=*/true);
6941 if (ending < 0)
6942 goto get_comma;
6943 if (!ending)
6945 parser->greater_than_is_operator_p
6946 = saved_greater_than_is_operator_p;
6947 return NULL;
6951 parser->greater_than_is_operator_p
6952 = saved_greater_than_is_operator_p;
6954 if (identifier)
6955 vec_safe_insert (expression_list, 0, identifier);
6957 return expression_list;
6960 /* Parse a pseudo-destructor-name.
6962 pseudo-destructor-name:
6963 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6964 :: [opt] nested-name-specifier template template-id :: ~ type-name
6965 :: [opt] nested-name-specifier [opt] ~ type-name
6967 If either of the first two productions is used, sets *SCOPE to the
6968 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6969 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6970 or ERROR_MARK_NODE if the parse fails. */
6972 static void
6973 cp_parser_pseudo_destructor_name (cp_parser* parser,
6974 tree object,
6975 tree* scope,
6976 tree* type)
6978 bool nested_name_specifier_p;
6980 /* Handle ~auto. */
6981 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6982 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6983 && !type_dependent_expression_p (object))
6985 if (cxx_dialect < cxx14)
6986 pedwarn (input_location, 0,
6987 "%<~auto%> only available with "
6988 "-std=c++14 or -std=gnu++14");
6989 cp_lexer_consume_token (parser->lexer);
6990 cp_lexer_consume_token (parser->lexer);
6991 *scope = NULL_TREE;
6992 *type = TREE_TYPE (object);
6993 return;
6996 /* Assume that things will not work out. */
6997 *type = error_mark_node;
6999 /* Look for the optional `::' operator. */
7000 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7001 /* Look for the optional nested-name-specifier. */
7002 nested_name_specifier_p
7003 = (cp_parser_nested_name_specifier_opt (parser,
7004 /*typename_keyword_p=*/false,
7005 /*check_dependency_p=*/true,
7006 /*type_p=*/false,
7007 /*is_declaration=*/false)
7008 != NULL_TREE);
7009 /* Now, if we saw a nested-name-specifier, we might be doing the
7010 second production. */
7011 if (nested_name_specifier_p
7012 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7014 /* Consume the `template' keyword. */
7015 cp_lexer_consume_token (parser->lexer);
7016 /* Parse the template-id. */
7017 cp_parser_template_id (parser,
7018 /*template_keyword_p=*/true,
7019 /*check_dependency_p=*/false,
7020 class_type,
7021 /*is_declaration=*/true);
7022 /* Look for the `::' token. */
7023 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7025 /* If the next token is not a `~', then there might be some
7026 additional qualification. */
7027 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7029 /* At this point, we're looking for "type-name :: ~". The type-name
7030 must not be a class-name, since this is a pseudo-destructor. So,
7031 it must be either an enum-name, or a typedef-name -- both of which
7032 are just identifiers. So, we peek ahead to check that the "::"
7033 and "~" tokens are present; if they are not, then we can avoid
7034 calling type_name. */
7035 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7036 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7037 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7039 cp_parser_error (parser, "non-scalar type");
7040 return;
7043 /* Look for the type-name. */
7044 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7045 if (*scope == error_mark_node)
7046 return;
7048 /* Look for the `::' token. */
7049 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7051 else
7052 *scope = NULL_TREE;
7054 /* Look for the `~'. */
7055 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7057 /* Once we see the ~, this has to be a pseudo-destructor. */
7058 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7059 cp_parser_commit_to_topmost_tentative_parse (parser);
7061 /* Look for the type-name again. We are not responsible for
7062 checking that it matches the first type-name. */
7063 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7066 /* Parse a unary-expression.
7068 unary-expression:
7069 postfix-expression
7070 ++ cast-expression
7071 -- cast-expression
7072 unary-operator cast-expression
7073 sizeof unary-expression
7074 sizeof ( type-id )
7075 alignof ( type-id ) [C++0x]
7076 new-expression
7077 delete-expression
7079 GNU Extensions:
7081 unary-expression:
7082 __extension__ cast-expression
7083 __alignof__ unary-expression
7084 __alignof__ ( type-id )
7085 alignof unary-expression [C++0x]
7086 __real__ cast-expression
7087 __imag__ cast-expression
7088 && identifier
7089 sizeof ( type-id ) { initializer-list , [opt] }
7090 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7091 __alignof__ ( type-id ) { initializer-list , [opt] }
7093 ADDRESS_P is true iff the unary-expression is appearing as the
7094 operand of the `&' operator. CAST_P is true if this expression is
7095 the target of a cast.
7097 Returns a representation of the expression. */
7099 static tree
7100 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7101 bool address_p, bool cast_p, bool decltype_p)
7103 cp_token *token;
7104 enum tree_code unary_operator;
7106 /* Peek at the next token. */
7107 token = cp_lexer_peek_token (parser->lexer);
7108 /* Some keywords give away the kind of expression. */
7109 if (token->type == CPP_KEYWORD)
7111 enum rid keyword = token->keyword;
7113 switch (keyword)
7115 case RID_ALIGNOF:
7116 case RID_SIZEOF:
7118 tree operand, ret;
7119 enum tree_code op;
7120 location_t first_loc;
7122 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7123 /* Consume the token. */
7124 cp_lexer_consume_token (parser->lexer);
7125 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7126 /* Parse the operand. */
7127 operand = cp_parser_sizeof_operand (parser, keyword);
7129 if (TYPE_P (operand))
7130 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7131 else
7133 /* ISO C++ defines alignof only with types, not with
7134 expressions. So pedwarn if alignof is used with a non-
7135 type expression. However, __alignof__ is ok. */
7136 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7137 pedwarn (token->location, OPT_Wpedantic,
7138 "ISO C++ does not allow %<alignof%> "
7139 "with a non-type");
7141 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7143 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7144 SIZEOF_EXPR with the original operand. */
7145 if (op == SIZEOF_EXPR && ret != error_mark_node)
7147 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7149 if (!processing_template_decl && TYPE_P (operand))
7151 ret = build_min (SIZEOF_EXPR, size_type_node,
7152 build1 (NOP_EXPR, operand,
7153 error_mark_node));
7154 SIZEOF_EXPR_TYPE_P (ret) = 1;
7156 else
7157 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7158 TREE_SIDE_EFFECTS (ret) = 0;
7159 TREE_READONLY (ret) = 1;
7161 SET_EXPR_LOCATION (ret, first_loc);
7163 return ret;
7166 case RID_NEW:
7167 return cp_parser_new_expression (parser);
7169 case RID_DELETE:
7170 return cp_parser_delete_expression (parser);
7172 case RID_EXTENSION:
7174 /* The saved value of the PEDANTIC flag. */
7175 int saved_pedantic;
7176 tree expr;
7178 /* Save away the PEDANTIC flag. */
7179 cp_parser_extension_opt (parser, &saved_pedantic);
7180 /* Parse the cast-expression. */
7181 expr = cp_parser_simple_cast_expression (parser);
7182 /* Restore the PEDANTIC flag. */
7183 pedantic = saved_pedantic;
7185 return expr;
7188 case RID_REALPART:
7189 case RID_IMAGPART:
7191 tree expression;
7193 /* Consume the `__real__' or `__imag__' token. */
7194 cp_lexer_consume_token (parser->lexer);
7195 /* Parse the cast-expression. */
7196 expression = cp_parser_simple_cast_expression (parser);
7197 /* Create the complete representation. */
7198 return build_x_unary_op (token->location,
7199 (keyword == RID_REALPART
7200 ? REALPART_EXPR : IMAGPART_EXPR),
7201 expression,
7202 tf_warning_or_error);
7204 break;
7206 case RID_TRANSACTION_ATOMIC:
7207 case RID_TRANSACTION_RELAXED:
7208 return cp_parser_transaction_expression (parser, keyword);
7210 case RID_NOEXCEPT:
7212 tree expr;
7213 const char *saved_message;
7214 bool saved_integral_constant_expression_p;
7215 bool saved_non_integral_constant_expression_p;
7216 bool saved_greater_than_is_operator_p;
7218 cp_lexer_consume_token (parser->lexer);
7219 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7221 saved_message = parser->type_definition_forbidden_message;
7222 parser->type_definition_forbidden_message
7223 = G_("types may not be defined in %<noexcept%> expressions");
7225 saved_integral_constant_expression_p
7226 = parser->integral_constant_expression_p;
7227 saved_non_integral_constant_expression_p
7228 = parser->non_integral_constant_expression_p;
7229 parser->integral_constant_expression_p = false;
7231 saved_greater_than_is_operator_p
7232 = parser->greater_than_is_operator_p;
7233 parser->greater_than_is_operator_p = true;
7235 ++cp_unevaluated_operand;
7236 ++c_inhibit_evaluation_warnings;
7237 ++cp_noexcept_operand;
7238 expr = cp_parser_expression (parser);
7239 --cp_noexcept_operand;
7240 --c_inhibit_evaluation_warnings;
7241 --cp_unevaluated_operand;
7243 parser->greater_than_is_operator_p
7244 = saved_greater_than_is_operator_p;
7246 parser->integral_constant_expression_p
7247 = saved_integral_constant_expression_p;
7248 parser->non_integral_constant_expression_p
7249 = saved_non_integral_constant_expression_p;
7251 parser->type_definition_forbidden_message = saved_message;
7253 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7254 return finish_noexcept_expr (expr, tf_warning_or_error);
7257 default:
7258 break;
7262 /* Look for the `:: new' and `:: delete', which also signal the
7263 beginning of a new-expression, or delete-expression,
7264 respectively. If the next token is `::', then it might be one of
7265 these. */
7266 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7268 enum rid keyword;
7270 /* See if the token after the `::' is one of the keywords in
7271 which we're interested. */
7272 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7273 /* If it's `new', we have a new-expression. */
7274 if (keyword == RID_NEW)
7275 return cp_parser_new_expression (parser);
7276 /* Similarly, for `delete'. */
7277 else if (keyword == RID_DELETE)
7278 return cp_parser_delete_expression (parser);
7281 /* Look for a unary operator. */
7282 unary_operator = cp_parser_unary_operator (token);
7283 /* The `++' and `--' operators can be handled similarly, even though
7284 they are not technically unary-operators in the grammar. */
7285 if (unary_operator == ERROR_MARK)
7287 if (token->type == CPP_PLUS_PLUS)
7288 unary_operator = PREINCREMENT_EXPR;
7289 else if (token->type == CPP_MINUS_MINUS)
7290 unary_operator = PREDECREMENT_EXPR;
7291 /* Handle the GNU address-of-label extension. */
7292 else if (cp_parser_allow_gnu_extensions_p (parser)
7293 && token->type == CPP_AND_AND)
7295 tree identifier;
7296 tree expression;
7297 location_t loc = token->location;
7299 /* Consume the '&&' token. */
7300 cp_lexer_consume_token (parser->lexer);
7301 /* Look for the identifier. */
7302 identifier = cp_parser_identifier (parser);
7303 /* Create an expression representing the address. */
7304 expression = finish_label_address_expr (identifier, loc);
7305 if (cp_parser_non_integral_constant_expression (parser,
7306 NIC_ADDR_LABEL))
7307 expression = error_mark_node;
7308 return expression;
7311 if (unary_operator != ERROR_MARK)
7313 tree cast_expression;
7314 tree expression = error_mark_node;
7315 non_integral_constant non_constant_p = NIC_NONE;
7316 location_t loc = token->location;
7317 tsubst_flags_t complain = complain_flags (decltype_p);
7319 /* Consume the operator token. */
7320 token = cp_lexer_consume_token (parser->lexer);
7321 /* Parse the cast-expression. */
7322 cast_expression
7323 = cp_parser_cast_expression (parser,
7324 unary_operator == ADDR_EXPR,
7325 /*cast_p=*/false,
7326 /*decltype*/false,
7327 pidk);
7328 /* Now, build an appropriate representation. */
7329 switch (unary_operator)
7331 case INDIRECT_REF:
7332 non_constant_p = NIC_STAR;
7333 expression = build_x_indirect_ref (loc, cast_expression,
7334 RO_UNARY_STAR,
7335 complain);
7336 break;
7338 case ADDR_EXPR:
7339 non_constant_p = NIC_ADDR;
7340 /* Fall through. */
7341 case BIT_NOT_EXPR:
7342 expression = build_x_unary_op (loc, unary_operator,
7343 cast_expression,
7344 complain);
7345 break;
7347 case PREINCREMENT_EXPR:
7348 case PREDECREMENT_EXPR:
7349 non_constant_p = unary_operator == PREINCREMENT_EXPR
7350 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7351 /* Fall through. */
7352 case UNARY_PLUS_EXPR:
7353 case NEGATE_EXPR:
7354 case TRUTH_NOT_EXPR:
7355 expression = finish_unary_op_expr (loc, unary_operator,
7356 cast_expression, complain);
7357 break;
7359 default:
7360 gcc_unreachable ();
7363 if (non_constant_p != NIC_NONE
7364 && cp_parser_non_integral_constant_expression (parser,
7365 non_constant_p))
7366 expression = error_mark_node;
7368 return expression;
7371 return cp_parser_postfix_expression (parser, address_p, cast_p,
7372 /*member_access_only_p=*/false,
7373 decltype_p,
7374 pidk);
7377 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7378 unary-operator, the corresponding tree code is returned. */
7380 static enum tree_code
7381 cp_parser_unary_operator (cp_token* token)
7383 switch (token->type)
7385 case CPP_MULT:
7386 return INDIRECT_REF;
7388 case CPP_AND:
7389 return ADDR_EXPR;
7391 case CPP_PLUS:
7392 return UNARY_PLUS_EXPR;
7394 case CPP_MINUS:
7395 return NEGATE_EXPR;
7397 case CPP_NOT:
7398 return TRUTH_NOT_EXPR;
7400 case CPP_COMPL:
7401 return BIT_NOT_EXPR;
7403 default:
7404 return ERROR_MARK;
7408 /* Parse a new-expression.
7410 new-expression:
7411 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7412 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7414 Returns a representation of the expression. */
7416 static tree
7417 cp_parser_new_expression (cp_parser* parser)
7419 bool global_scope_p;
7420 vec<tree, va_gc> *placement;
7421 tree type;
7422 vec<tree, va_gc> *initializer;
7423 tree nelts = NULL_TREE;
7424 tree ret;
7426 /* Look for the optional `::' operator. */
7427 global_scope_p
7428 = (cp_parser_global_scope_opt (parser,
7429 /*current_scope_valid_p=*/false)
7430 != NULL_TREE);
7431 /* Look for the `new' operator. */
7432 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7433 /* There's no easy way to tell a new-placement from the
7434 `( type-id )' construct. */
7435 cp_parser_parse_tentatively (parser);
7436 /* Look for a new-placement. */
7437 placement = cp_parser_new_placement (parser);
7438 /* If that didn't work out, there's no new-placement. */
7439 if (!cp_parser_parse_definitely (parser))
7441 if (placement != NULL)
7442 release_tree_vector (placement);
7443 placement = NULL;
7446 /* If the next token is a `(', then we have a parenthesized
7447 type-id. */
7448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7450 cp_token *token;
7451 const char *saved_message = parser->type_definition_forbidden_message;
7453 /* Consume the `('. */
7454 cp_lexer_consume_token (parser->lexer);
7456 /* Parse the type-id. */
7457 parser->type_definition_forbidden_message
7458 = G_("types may not be defined in a new-expression");
7459 type = cp_parser_type_id (parser);
7460 parser->type_definition_forbidden_message = saved_message;
7462 /* Look for the closing `)'. */
7463 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7464 token = cp_lexer_peek_token (parser->lexer);
7465 /* There should not be a direct-new-declarator in this production,
7466 but GCC used to allowed this, so we check and emit a sensible error
7467 message for this case. */
7468 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7470 error_at (token->location,
7471 "array bound forbidden after parenthesized type-id");
7472 inform (token->location,
7473 "try removing the parentheses around the type-id");
7474 cp_parser_direct_new_declarator (parser);
7477 /* Otherwise, there must be a new-type-id. */
7478 else
7479 type = cp_parser_new_type_id (parser, &nelts);
7481 /* If the next token is a `(' or '{', then we have a new-initializer. */
7482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7483 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7484 initializer = cp_parser_new_initializer (parser);
7485 else
7486 initializer = NULL;
7488 /* A new-expression may not appear in an integral constant
7489 expression. */
7490 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7491 ret = error_mark_node;
7492 else
7494 /* Create a representation of the new-expression. */
7495 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7496 tf_warning_or_error);
7499 if (placement != NULL)
7500 release_tree_vector (placement);
7501 if (initializer != NULL)
7502 release_tree_vector (initializer);
7504 return ret;
7507 /* Parse a new-placement.
7509 new-placement:
7510 ( expression-list )
7512 Returns the same representation as for an expression-list. */
7514 static vec<tree, va_gc> *
7515 cp_parser_new_placement (cp_parser* parser)
7517 vec<tree, va_gc> *expression_list;
7519 /* Parse the expression-list. */
7520 expression_list = (cp_parser_parenthesized_expression_list
7521 (parser, non_attr, /*cast_p=*/false,
7522 /*allow_expansion_p=*/true,
7523 /*non_constant_p=*/NULL));
7525 return expression_list;
7528 /* Parse a new-type-id.
7530 new-type-id:
7531 type-specifier-seq new-declarator [opt]
7533 Returns the TYPE allocated. If the new-type-id indicates an array
7534 type, *NELTS is set to the number of elements in the last array
7535 bound; the TYPE will not include the last array bound. */
7537 static tree
7538 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7540 cp_decl_specifier_seq type_specifier_seq;
7541 cp_declarator *new_declarator;
7542 cp_declarator *declarator;
7543 cp_declarator *outer_declarator;
7544 const char *saved_message;
7546 /* The type-specifier sequence must not contain type definitions.
7547 (It cannot contain declarations of new types either, but if they
7548 are not definitions we will catch that because they are not
7549 complete.) */
7550 saved_message = parser->type_definition_forbidden_message;
7551 parser->type_definition_forbidden_message
7552 = G_("types may not be defined in a new-type-id");
7553 /* Parse the type-specifier-seq. */
7554 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7555 /*is_trailing_return=*/false,
7556 &type_specifier_seq);
7557 /* Restore the old message. */
7558 parser->type_definition_forbidden_message = saved_message;
7560 if (type_specifier_seq.type == error_mark_node)
7561 return error_mark_node;
7563 /* Parse the new-declarator. */
7564 new_declarator = cp_parser_new_declarator_opt (parser);
7566 /* Determine the number of elements in the last array dimension, if
7567 any. */
7568 *nelts = NULL_TREE;
7569 /* Skip down to the last array dimension. */
7570 declarator = new_declarator;
7571 outer_declarator = NULL;
7572 while (declarator && (declarator->kind == cdk_pointer
7573 || declarator->kind == cdk_ptrmem))
7575 outer_declarator = declarator;
7576 declarator = declarator->declarator;
7578 while (declarator
7579 && declarator->kind == cdk_array
7580 && declarator->declarator
7581 && declarator->declarator->kind == cdk_array)
7583 outer_declarator = declarator;
7584 declarator = declarator->declarator;
7587 if (declarator && declarator->kind == cdk_array)
7589 *nelts = declarator->u.array.bounds;
7590 if (*nelts == error_mark_node)
7591 *nelts = integer_one_node;
7593 if (outer_declarator)
7594 outer_declarator->declarator = declarator->declarator;
7595 else
7596 new_declarator = NULL;
7599 return groktypename (&type_specifier_seq, new_declarator, false);
7602 /* Parse an (optional) new-declarator.
7604 new-declarator:
7605 ptr-operator new-declarator [opt]
7606 direct-new-declarator
7608 Returns the declarator. */
7610 static cp_declarator *
7611 cp_parser_new_declarator_opt (cp_parser* parser)
7613 enum tree_code code;
7614 tree type, std_attributes = NULL_TREE;
7615 cp_cv_quals cv_quals;
7617 /* We don't know if there's a ptr-operator next, or not. */
7618 cp_parser_parse_tentatively (parser);
7619 /* Look for a ptr-operator. */
7620 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7621 /* If that worked, look for more new-declarators. */
7622 if (cp_parser_parse_definitely (parser))
7624 cp_declarator *declarator;
7626 /* Parse another optional declarator. */
7627 declarator = cp_parser_new_declarator_opt (parser);
7629 declarator = cp_parser_make_indirect_declarator
7630 (code, type, cv_quals, declarator, std_attributes);
7632 return declarator;
7635 /* If the next token is a `[', there is a direct-new-declarator. */
7636 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7637 return cp_parser_direct_new_declarator (parser);
7639 return NULL;
7642 /* Parse a direct-new-declarator.
7644 direct-new-declarator:
7645 [ expression ]
7646 direct-new-declarator [constant-expression]
7650 static cp_declarator *
7651 cp_parser_direct_new_declarator (cp_parser* parser)
7653 cp_declarator *declarator = NULL;
7655 while (true)
7657 tree expression;
7658 cp_token *token;
7660 /* Look for the opening `['. */
7661 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7663 token = cp_lexer_peek_token (parser->lexer);
7664 expression = cp_parser_expression (parser);
7665 /* The standard requires that the expression have integral
7666 type. DR 74 adds enumeration types. We believe that the
7667 real intent is that these expressions be handled like the
7668 expression in a `switch' condition, which also allows
7669 classes with a single conversion to integral or
7670 enumeration type. */
7671 if (!processing_template_decl)
7673 expression
7674 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7675 expression,
7676 /*complain=*/true);
7677 if (!expression)
7679 error_at (token->location,
7680 "expression in new-declarator must have integral "
7681 "or enumeration type");
7682 expression = error_mark_node;
7686 /* Look for the closing `]'. */
7687 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7689 /* Add this bound to the declarator. */
7690 declarator = make_array_declarator (declarator, expression);
7692 /* If the next token is not a `[', then there are no more
7693 bounds. */
7694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7695 break;
7698 return declarator;
7701 /* Parse a new-initializer.
7703 new-initializer:
7704 ( expression-list [opt] )
7705 braced-init-list
7707 Returns a representation of the expression-list. */
7709 static vec<tree, va_gc> *
7710 cp_parser_new_initializer (cp_parser* parser)
7712 vec<tree, va_gc> *expression_list;
7714 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7716 tree t;
7717 bool expr_non_constant_p;
7718 cp_lexer_set_source_position (parser->lexer);
7719 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7720 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7721 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7722 expression_list = make_tree_vector_single (t);
7724 else
7725 expression_list = (cp_parser_parenthesized_expression_list
7726 (parser, non_attr, /*cast_p=*/false,
7727 /*allow_expansion_p=*/true,
7728 /*non_constant_p=*/NULL));
7730 return expression_list;
7733 /* Parse a delete-expression.
7735 delete-expression:
7736 :: [opt] delete cast-expression
7737 :: [opt] delete [ ] cast-expression
7739 Returns a representation of the expression. */
7741 static tree
7742 cp_parser_delete_expression (cp_parser* parser)
7744 bool global_scope_p;
7745 bool array_p;
7746 tree expression;
7748 /* Look for the optional `::' operator. */
7749 global_scope_p
7750 = (cp_parser_global_scope_opt (parser,
7751 /*current_scope_valid_p=*/false)
7752 != NULL_TREE);
7753 /* Look for the `delete' keyword. */
7754 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7755 /* See if the array syntax is in use. */
7756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7758 /* Consume the `[' token. */
7759 cp_lexer_consume_token (parser->lexer);
7760 /* Look for the `]' token. */
7761 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7762 /* Remember that this is the `[]' construct. */
7763 array_p = true;
7765 else
7766 array_p = false;
7768 /* Parse the cast-expression. */
7769 expression = cp_parser_simple_cast_expression (parser);
7771 /* A delete-expression may not appear in an integral constant
7772 expression. */
7773 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7774 return error_mark_node;
7776 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7777 tf_warning_or_error);
7780 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7781 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7782 0 otherwise. */
7784 static int
7785 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7787 cp_token *token = cp_lexer_peek_token (parser->lexer);
7788 switch (token->type)
7790 case CPP_COMMA:
7791 case CPP_SEMICOLON:
7792 case CPP_QUERY:
7793 case CPP_COLON:
7794 case CPP_CLOSE_SQUARE:
7795 case CPP_CLOSE_PAREN:
7796 case CPP_CLOSE_BRACE:
7797 case CPP_OPEN_BRACE:
7798 case CPP_DOT:
7799 case CPP_DOT_STAR:
7800 case CPP_DEREF:
7801 case CPP_DEREF_STAR:
7802 case CPP_DIV:
7803 case CPP_MOD:
7804 case CPP_LSHIFT:
7805 case CPP_RSHIFT:
7806 case CPP_LESS:
7807 case CPP_GREATER:
7808 case CPP_LESS_EQ:
7809 case CPP_GREATER_EQ:
7810 case CPP_EQ_EQ:
7811 case CPP_NOT_EQ:
7812 case CPP_EQ:
7813 case CPP_MULT_EQ:
7814 case CPP_DIV_EQ:
7815 case CPP_MOD_EQ:
7816 case CPP_PLUS_EQ:
7817 case CPP_MINUS_EQ:
7818 case CPP_RSHIFT_EQ:
7819 case CPP_LSHIFT_EQ:
7820 case CPP_AND_EQ:
7821 case CPP_XOR_EQ:
7822 case CPP_OR_EQ:
7823 case CPP_XOR:
7824 case CPP_OR:
7825 case CPP_OR_OR:
7826 case CPP_EOF:
7827 case CPP_ELLIPSIS:
7828 return 0;
7830 case CPP_OPEN_PAREN:
7831 /* In ((type ()) () the last () isn't a valid cast-expression,
7832 so the whole must be parsed as postfix-expression. */
7833 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7834 != CPP_CLOSE_PAREN;
7836 case CPP_OPEN_SQUARE:
7837 /* '[' may start a primary-expression in obj-c++ and in C++11,
7838 as a lambda-expression, eg, '(void)[]{}'. */
7839 if (cxx_dialect >= cxx11)
7840 return -1;
7841 return c_dialect_objc ();
7843 case CPP_PLUS_PLUS:
7844 case CPP_MINUS_MINUS:
7845 /* '++' and '--' may or may not start a cast-expression:
7847 struct T { void operator++(int); };
7848 void f() { (T())++; }
7852 int a;
7853 (int)++a; */
7854 return -1;
7856 default:
7857 return 1;
7861 /* Parse a cast-expression.
7863 cast-expression:
7864 unary-expression
7865 ( type-id ) cast-expression
7867 ADDRESS_P is true iff the unary-expression is appearing as the
7868 operand of the `&' operator. CAST_P is true if this expression is
7869 the target of a cast.
7871 Returns a representation of the expression. */
7873 static tree
7874 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7875 bool decltype_p, cp_id_kind * pidk)
7877 /* If it's a `(', then we might be looking at a cast. */
7878 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7880 tree type = NULL_TREE;
7881 tree expr = NULL_TREE;
7882 int cast_expression = 0;
7883 const char *saved_message;
7885 /* There's no way to know yet whether or not this is a cast.
7886 For example, `(int (3))' is a unary-expression, while `(int)
7887 3' is a cast. So, we resort to parsing tentatively. */
7888 cp_parser_parse_tentatively (parser);
7889 /* Types may not be defined in a cast. */
7890 saved_message = parser->type_definition_forbidden_message;
7891 parser->type_definition_forbidden_message
7892 = G_("types may not be defined in casts");
7893 /* Consume the `('. */
7894 cp_lexer_consume_token (parser->lexer);
7895 /* A very tricky bit is that `(struct S) { 3 }' is a
7896 compound-literal (which we permit in C++ as an extension).
7897 But, that construct is not a cast-expression -- it is a
7898 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7899 is legal; if the compound-literal were a cast-expression,
7900 you'd need an extra set of parentheses.) But, if we parse
7901 the type-id, and it happens to be a class-specifier, then we
7902 will commit to the parse at that point, because we cannot
7903 undo the action that is done when creating a new class. So,
7904 then we cannot back up and do a postfix-expression.
7906 Another tricky case is the following (c++/29234):
7908 struct S { void operator () (); };
7910 void foo ()
7912 ( S()() );
7915 As a type-id we parse the parenthesized S()() as a function
7916 returning a function, groktypename complains and we cannot
7917 back up in this case either.
7919 Therefore, we scan ahead to the closing `)', and check to see
7920 if the tokens after the `)' can start a cast-expression. Otherwise
7921 we are dealing with an unary-expression, a postfix-expression
7922 or something else.
7924 Yet another tricky case, in C++11, is the following (c++/54891):
7926 (void)[]{};
7928 The issue is that usually, besides the case of lambda-expressions,
7929 the parenthesized type-id cannot be followed by '[', and, eg, we
7930 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7931 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7932 we don't commit, we try a cast-expression, then an unary-expression.
7934 Save tokens so that we can put them back. */
7935 cp_lexer_save_tokens (parser->lexer);
7937 /* We may be looking at a cast-expression. */
7938 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7939 /*consume_paren=*/true))
7940 cast_expression
7941 = cp_parser_tokens_start_cast_expression (parser);
7943 /* Roll back the tokens we skipped. */
7944 cp_lexer_rollback_tokens (parser->lexer);
7945 /* If we aren't looking at a cast-expression, simulate an error so
7946 that the call to cp_parser_error_occurred below returns true. */
7947 if (!cast_expression)
7948 cp_parser_simulate_error (parser);
7949 else
7951 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7952 parser->in_type_id_in_expr_p = true;
7953 /* Look for the type-id. */
7954 type = cp_parser_type_id (parser);
7955 /* Look for the closing `)'. */
7956 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7957 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7960 /* Restore the saved message. */
7961 parser->type_definition_forbidden_message = saved_message;
7963 /* At this point this can only be either a cast or a
7964 parenthesized ctor such as `(T ())' that looks like a cast to
7965 function returning T. */
7966 if (!cp_parser_error_occurred (parser))
7968 /* Only commit if the cast-expression doesn't start with
7969 '++', '--', or '[' in C++11. */
7970 if (cast_expression > 0)
7971 cp_parser_commit_to_topmost_tentative_parse (parser);
7973 expr = cp_parser_cast_expression (parser,
7974 /*address_p=*/false,
7975 /*cast_p=*/true,
7976 /*decltype_p=*/false,
7977 pidk);
7979 if (cp_parser_parse_definitely (parser))
7981 /* Warn about old-style casts, if so requested. */
7982 if (warn_old_style_cast
7983 && !in_system_header_at (input_location)
7984 && !VOID_TYPE_P (type)
7985 && current_lang_name != lang_name_c)
7986 warning (OPT_Wold_style_cast, "use of old-style cast");
7988 /* Only type conversions to integral or enumeration types
7989 can be used in constant-expressions. */
7990 if (!cast_valid_in_integral_constant_expression_p (type)
7991 && cp_parser_non_integral_constant_expression (parser,
7992 NIC_CAST))
7993 return error_mark_node;
7995 /* Perform the cast. */
7996 expr = build_c_cast (input_location, type, expr);
7997 return expr;
8000 else
8001 cp_parser_abort_tentative_parse (parser);
8004 /* If we get here, then it's not a cast, so it must be a
8005 unary-expression. */
8006 return cp_parser_unary_expression (parser, pidk, address_p,
8007 cast_p, decltype_p);
8010 /* Parse a binary expression of the general form:
8012 pm-expression:
8013 cast-expression
8014 pm-expression .* cast-expression
8015 pm-expression ->* cast-expression
8017 multiplicative-expression:
8018 pm-expression
8019 multiplicative-expression * pm-expression
8020 multiplicative-expression / pm-expression
8021 multiplicative-expression % pm-expression
8023 additive-expression:
8024 multiplicative-expression
8025 additive-expression + multiplicative-expression
8026 additive-expression - multiplicative-expression
8028 shift-expression:
8029 additive-expression
8030 shift-expression << additive-expression
8031 shift-expression >> additive-expression
8033 relational-expression:
8034 shift-expression
8035 relational-expression < shift-expression
8036 relational-expression > shift-expression
8037 relational-expression <= shift-expression
8038 relational-expression >= shift-expression
8040 GNU Extension:
8042 relational-expression:
8043 relational-expression <? shift-expression
8044 relational-expression >? shift-expression
8046 equality-expression:
8047 relational-expression
8048 equality-expression == relational-expression
8049 equality-expression != relational-expression
8051 and-expression:
8052 equality-expression
8053 and-expression & equality-expression
8055 exclusive-or-expression:
8056 and-expression
8057 exclusive-or-expression ^ and-expression
8059 inclusive-or-expression:
8060 exclusive-or-expression
8061 inclusive-or-expression | exclusive-or-expression
8063 logical-and-expression:
8064 inclusive-or-expression
8065 logical-and-expression && inclusive-or-expression
8067 logical-or-expression:
8068 logical-and-expression
8069 logical-or-expression || logical-and-expression
8071 All these are implemented with a single function like:
8073 binary-expression:
8074 simple-cast-expression
8075 binary-expression <token> binary-expression
8077 CAST_P is true if this expression is the target of a cast.
8079 The binops_by_token map is used to get the tree codes for each <token> type.
8080 binary-expressions are associated according to a precedence table. */
8082 #define TOKEN_PRECEDENCE(token) \
8083 (((token->type == CPP_GREATER \
8084 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8085 && !parser->greater_than_is_operator_p) \
8086 ? PREC_NOT_OPERATOR \
8087 : binops_by_token[token->type].prec)
8089 static tree
8090 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8091 bool no_toplevel_fold_p,
8092 bool decltype_p,
8093 enum cp_parser_prec prec,
8094 cp_id_kind * pidk)
8096 cp_parser_expression_stack stack;
8097 cp_parser_expression_stack_entry *sp = &stack[0];
8098 cp_parser_expression_stack_entry current;
8099 tree rhs;
8100 cp_token *token;
8101 enum tree_code rhs_type;
8102 enum cp_parser_prec new_prec, lookahead_prec;
8103 tree overload;
8105 /* Parse the first expression. */
8106 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8107 ? TRUTH_NOT_EXPR : ERROR_MARK);
8108 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8109 cast_p, decltype_p, pidk);
8110 current.prec = prec;
8112 if (cp_parser_error_occurred (parser))
8113 return error_mark_node;
8115 for (;;)
8117 /* Get an operator token. */
8118 token = cp_lexer_peek_token (parser->lexer);
8120 if (warn_cxx0x_compat
8121 && token->type == CPP_RSHIFT
8122 && !parser->greater_than_is_operator_p)
8124 if (warning_at (token->location, OPT_Wc__0x_compat,
8125 "%<>>%> operator is treated"
8126 " as two right angle brackets in C++11"))
8127 inform (token->location,
8128 "suggest parentheses around %<>>%> expression");
8131 new_prec = TOKEN_PRECEDENCE (token);
8133 /* Popping an entry off the stack means we completed a subexpression:
8134 - either we found a token which is not an operator (`>' where it is not
8135 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8136 will happen repeatedly;
8137 - or, we found an operator which has lower priority. This is the case
8138 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8139 parsing `3 * 4'. */
8140 if (new_prec <= current.prec)
8142 if (sp == stack)
8143 break;
8144 else
8145 goto pop;
8148 get_rhs:
8149 current.tree_type = binops_by_token[token->type].tree_type;
8150 current.loc = token->location;
8152 /* We used the operator token. */
8153 cp_lexer_consume_token (parser->lexer);
8155 /* For "false && x" or "true || x", x will never be executed;
8156 disable warnings while evaluating it. */
8157 if (current.tree_type == TRUTH_ANDIF_EXPR)
8158 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8159 else if (current.tree_type == TRUTH_ORIF_EXPR)
8160 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8162 /* Extract another operand. It may be the RHS of this expression
8163 or the LHS of a new, higher priority expression. */
8164 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8165 ? TRUTH_NOT_EXPR : ERROR_MARK);
8166 rhs = cp_parser_simple_cast_expression (parser);
8168 /* Get another operator token. Look up its precedence to avoid
8169 building a useless (immediately popped) stack entry for common
8170 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8171 token = cp_lexer_peek_token (parser->lexer);
8172 lookahead_prec = TOKEN_PRECEDENCE (token);
8173 if (lookahead_prec > new_prec)
8175 /* ... and prepare to parse the RHS of the new, higher priority
8176 expression. Since precedence levels on the stack are
8177 monotonically increasing, we do not have to care about
8178 stack overflows. */
8179 *sp = current;
8180 ++sp;
8181 current.lhs = rhs;
8182 current.lhs_type = rhs_type;
8183 current.prec = new_prec;
8184 new_prec = lookahead_prec;
8185 goto get_rhs;
8187 pop:
8188 lookahead_prec = new_prec;
8189 /* If the stack is not empty, we have parsed into LHS the right side
8190 (`4' in the example above) of an expression we had suspended.
8191 We can use the information on the stack to recover the LHS (`3')
8192 from the stack together with the tree code (`MULT_EXPR'), and
8193 the precedence of the higher level subexpression
8194 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8195 which will be used to actually build the additive expression. */
8196 rhs = current.lhs;
8197 rhs_type = current.lhs_type;
8198 --sp;
8199 current = *sp;
8202 /* Undo the disabling of warnings done above. */
8203 if (current.tree_type == TRUTH_ANDIF_EXPR)
8204 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8205 else if (current.tree_type == TRUTH_ORIF_EXPR)
8206 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8208 if (warn_logical_not_paren
8209 && current.lhs_type == TRUTH_NOT_EXPR)
8210 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8212 overload = NULL;
8213 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8214 ERROR_MARK for everything that is not a binary expression.
8215 This makes warn_about_parentheses miss some warnings that
8216 involve unary operators. For unary expressions we should
8217 pass the correct tree_code unless the unary expression was
8218 surrounded by parentheses.
8220 if (no_toplevel_fold_p
8221 && lookahead_prec <= current.prec
8222 && sp == stack)
8223 current.lhs = build2 (current.tree_type,
8224 TREE_CODE_CLASS (current.tree_type)
8225 == tcc_comparison
8226 ? boolean_type_node : TREE_TYPE (current.lhs),
8227 current.lhs, rhs);
8228 else
8229 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8230 current.lhs, current.lhs_type,
8231 rhs, rhs_type, &overload,
8232 complain_flags (decltype_p));
8233 current.lhs_type = current.tree_type;
8234 if (EXPR_P (current.lhs))
8235 SET_EXPR_LOCATION (current.lhs, current.loc);
8237 /* If the binary operator required the use of an overloaded operator,
8238 then this expression cannot be an integral constant-expression.
8239 An overloaded operator can be used even if both operands are
8240 otherwise permissible in an integral constant-expression if at
8241 least one of the operands is of enumeration type. */
8243 if (overload
8244 && cp_parser_non_integral_constant_expression (parser,
8245 NIC_OVERLOADED))
8246 return error_mark_node;
8249 return current.lhs;
8252 static tree
8253 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8254 bool no_toplevel_fold_p,
8255 enum cp_parser_prec prec,
8256 cp_id_kind * pidk)
8258 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8259 /*decltype*/false, prec, pidk);
8262 /* Parse the `? expression : assignment-expression' part of a
8263 conditional-expression. The LOGICAL_OR_EXPR is the
8264 logical-or-expression that started the conditional-expression.
8265 Returns a representation of the entire conditional-expression.
8267 This routine is used by cp_parser_assignment_expression.
8269 ? expression : assignment-expression
8271 GNU Extensions:
8273 ? : assignment-expression */
8275 static tree
8276 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8278 tree expr;
8279 tree assignment_expr;
8280 struct cp_token *token;
8281 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8283 /* Consume the `?' token. */
8284 cp_lexer_consume_token (parser->lexer);
8285 token = cp_lexer_peek_token (parser->lexer);
8286 if (cp_parser_allow_gnu_extensions_p (parser)
8287 && token->type == CPP_COLON)
8289 pedwarn (token->location, OPT_Wpedantic,
8290 "ISO C++ does not allow ?: with omitted middle operand");
8291 /* Implicit true clause. */
8292 expr = NULL_TREE;
8293 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8294 warn_for_omitted_condop (token->location, logical_or_expr);
8296 else
8298 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8299 parser->colon_corrects_to_scope_p = false;
8300 /* Parse the expression. */
8301 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8302 expr = cp_parser_expression (parser);
8303 c_inhibit_evaluation_warnings +=
8304 ((logical_or_expr == truthvalue_true_node)
8305 - (logical_or_expr == truthvalue_false_node));
8306 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8309 /* The next token should be a `:'. */
8310 cp_parser_require (parser, CPP_COLON, RT_COLON);
8311 /* Parse the assignment-expression. */
8312 assignment_expr = cp_parser_assignment_expression (parser);
8313 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8315 /* Build the conditional-expression. */
8316 return build_x_conditional_expr (loc, logical_or_expr,
8317 expr,
8318 assignment_expr,
8319 tf_warning_or_error);
8322 /* Parse an assignment-expression.
8324 assignment-expression:
8325 conditional-expression
8326 logical-or-expression assignment-operator assignment_expression
8327 throw-expression
8329 CAST_P is true if this expression is the target of a cast.
8330 DECLTYPE_P is true if this expression is the operand of decltype.
8332 Returns a representation for the expression. */
8334 static tree
8335 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8336 bool cast_p, bool decltype_p)
8338 tree expr;
8340 /* If the next token is the `throw' keyword, then we're looking at
8341 a throw-expression. */
8342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8343 expr = cp_parser_throw_expression (parser);
8344 /* Otherwise, it must be that we are looking at a
8345 logical-or-expression. */
8346 else
8348 /* Parse the binary expressions (logical-or-expression). */
8349 expr = cp_parser_binary_expression (parser, cast_p, false,
8350 decltype_p,
8351 PREC_NOT_OPERATOR, pidk);
8352 /* If the next token is a `?' then we're actually looking at a
8353 conditional-expression. */
8354 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8355 return cp_parser_question_colon_clause (parser, expr);
8356 else
8358 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8360 /* If it's an assignment-operator, we're using the second
8361 production. */
8362 enum tree_code assignment_operator
8363 = cp_parser_assignment_operator_opt (parser);
8364 if (assignment_operator != ERROR_MARK)
8366 bool non_constant_p;
8367 location_t saved_input_location;
8369 /* Parse the right-hand side of the assignment. */
8370 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8372 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8373 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8375 /* An assignment may not appear in a
8376 constant-expression. */
8377 if (cp_parser_non_integral_constant_expression (parser,
8378 NIC_ASSIGNMENT))
8379 return error_mark_node;
8380 /* Build the assignment expression. Its default
8381 location is the location of the '=' token. */
8382 saved_input_location = input_location;
8383 input_location = loc;
8384 expr = build_x_modify_expr (loc, expr,
8385 assignment_operator,
8386 rhs,
8387 complain_flags (decltype_p));
8388 input_location = saved_input_location;
8393 return expr;
8396 /* Parse an (optional) assignment-operator.
8398 assignment-operator: one of
8399 = *= /= %= += -= >>= <<= &= ^= |=
8401 GNU Extension:
8403 assignment-operator: one of
8404 <?= >?=
8406 If the next token is an assignment operator, the corresponding tree
8407 code is returned, and the token is consumed. For example, for
8408 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8409 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8410 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8411 operator, ERROR_MARK is returned. */
8413 static enum tree_code
8414 cp_parser_assignment_operator_opt (cp_parser* parser)
8416 enum tree_code op;
8417 cp_token *token;
8419 /* Peek at the next token. */
8420 token = cp_lexer_peek_token (parser->lexer);
8422 switch (token->type)
8424 case CPP_EQ:
8425 op = NOP_EXPR;
8426 break;
8428 case CPP_MULT_EQ:
8429 op = MULT_EXPR;
8430 break;
8432 case CPP_DIV_EQ:
8433 op = TRUNC_DIV_EXPR;
8434 break;
8436 case CPP_MOD_EQ:
8437 op = TRUNC_MOD_EXPR;
8438 break;
8440 case CPP_PLUS_EQ:
8441 op = PLUS_EXPR;
8442 break;
8444 case CPP_MINUS_EQ:
8445 op = MINUS_EXPR;
8446 break;
8448 case CPP_RSHIFT_EQ:
8449 op = RSHIFT_EXPR;
8450 break;
8452 case CPP_LSHIFT_EQ:
8453 op = LSHIFT_EXPR;
8454 break;
8456 case CPP_AND_EQ:
8457 op = BIT_AND_EXPR;
8458 break;
8460 case CPP_XOR_EQ:
8461 op = BIT_XOR_EXPR;
8462 break;
8464 case CPP_OR_EQ:
8465 op = BIT_IOR_EXPR;
8466 break;
8468 default:
8469 /* Nothing else is an assignment operator. */
8470 op = ERROR_MARK;
8473 /* If it was an assignment operator, consume it. */
8474 if (op != ERROR_MARK)
8475 cp_lexer_consume_token (parser->lexer);
8477 return op;
8480 /* Parse an expression.
8482 expression:
8483 assignment-expression
8484 expression , assignment-expression
8486 CAST_P is true if this expression is the target of a cast.
8487 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8488 except possibly parenthesized or on the RHS of a comma (N3276).
8490 Returns a representation of the expression. */
8492 static tree
8493 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8494 bool cast_p, bool decltype_p)
8496 tree expression = NULL_TREE;
8497 location_t loc = UNKNOWN_LOCATION;
8499 while (true)
8501 tree assignment_expression;
8503 /* Parse the next assignment-expression. */
8504 assignment_expression
8505 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8507 /* We don't create a temporary for a call that is the immediate operand
8508 of decltype or on the RHS of a comma. But when we see a comma, we
8509 need to create a temporary for a call on the LHS. */
8510 if (decltype_p && !processing_template_decl
8511 && TREE_CODE (assignment_expression) == CALL_EXPR
8512 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8513 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8514 assignment_expression
8515 = build_cplus_new (TREE_TYPE (assignment_expression),
8516 assignment_expression, tf_warning_or_error);
8518 /* If this is the first assignment-expression, we can just
8519 save it away. */
8520 if (!expression)
8521 expression = assignment_expression;
8522 else
8523 expression = build_x_compound_expr (loc, expression,
8524 assignment_expression,
8525 complain_flags (decltype_p));
8526 /* If the next token is not a comma, then we are done with the
8527 expression. */
8528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8529 break;
8530 /* Consume the `,'. */
8531 loc = cp_lexer_peek_token (parser->lexer)->location;
8532 cp_lexer_consume_token (parser->lexer);
8533 /* A comma operator cannot appear in a constant-expression. */
8534 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8535 expression = error_mark_node;
8538 return expression;
8541 /* Parse a constant-expression.
8543 constant-expression:
8544 conditional-expression
8546 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8547 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8548 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8549 is false, NON_CONSTANT_P should be NULL. */
8551 static tree
8552 cp_parser_constant_expression (cp_parser* parser,
8553 bool allow_non_constant_p,
8554 bool *non_constant_p)
8556 bool saved_integral_constant_expression_p;
8557 bool saved_allow_non_integral_constant_expression_p;
8558 bool saved_non_integral_constant_expression_p;
8559 tree expression;
8561 /* It might seem that we could simply parse the
8562 conditional-expression, and then check to see if it were
8563 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8564 one that the compiler can figure out is constant, possibly after
8565 doing some simplifications or optimizations. The standard has a
8566 precise definition of constant-expression, and we must honor
8567 that, even though it is somewhat more restrictive.
8569 For example:
8571 int i[(2, 3)];
8573 is not a legal declaration, because `(2, 3)' is not a
8574 constant-expression. The `,' operator is forbidden in a
8575 constant-expression. However, GCC's constant-folding machinery
8576 will fold this operation to an INTEGER_CST for `3'. */
8578 /* Save the old settings. */
8579 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8580 saved_allow_non_integral_constant_expression_p
8581 = parser->allow_non_integral_constant_expression_p;
8582 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8583 /* We are now parsing a constant-expression. */
8584 parser->integral_constant_expression_p = true;
8585 parser->allow_non_integral_constant_expression_p
8586 = (allow_non_constant_p || cxx_dialect >= cxx11);
8587 parser->non_integral_constant_expression_p = false;
8588 /* Although the grammar says "conditional-expression", we parse an
8589 "assignment-expression", which also permits "throw-expression"
8590 and the use of assignment operators. In the case that
8591 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8592 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8593 actually essential that we look for an assignment-expression.
8594 For example, cp_parser_initializer_clauses uses this function to
8595 determine whether a particular assignment-expression is in fact
8596 constant. */
8597 expression = cp_parser_assignment_expression (parser);
8598 /* Restore the old settings. */
8599 parser->integral_constant_expression_p
8600 = saved_integral_constant_expression_p;
8601 parser->allow_non_integral_constant_expression_p
8602 = saved_allow_non_integral_constant_expression_p;
8603 if (cxx_dialect >= cxx11)
8605 /* Require an rvalue constant expression here; that's what our
8606 callers expect. Reference constant expressions are handled
8607 separately in e.g. cp_parser_template_argument. */
8608 bool is_const = potential_rvalue_constant_expression (expression);
8609 parser->non_integral_constant_expression_p = !is_const;
8610 if (!is_const && !allow_non_constant_p)
8611 require_potential_rvalue_constant_expression (expression);
8613 if (allow_non_constant_p)
8614 *non_constant_p = parser->non_integral_constant_expression_p;
8615 parser->non_integral_constant_expression_p
8616 = saved_non_integral_constant_expression_p;
8618 return expression;
8621 /* Parse __builtin_offsetof.
8623 offsetof-expression:
8624 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8626 offsetof-member-designator:
8627 id-expression
8628 | offsetof-member-designator "." id-expression
8629 | offsetof-member-designator "[" expression "]"
8630 | offsetof-member-designator "->" id-expression */
8632 static tree
8633 cp_parser_builtin_offsetof (cp_parser *parser)
8635 int save_ice_p, save_non_ice_p;
8636 tree type, expr;
8637 cp_id_kind dummy;
8638 cp_token *token;
8640 /* We're about to accept non-integral-constant things, but will
8641 definitely yield an integral constant expression. Save and
8642 restore these values around our local parsing. */
8643 save_ice_p = parser->integral_constant_expression_p;
8644 save_non_ice_p = parser->non_integral_constant_expression_p;
8646 /* Consume the "__builtin_offsetof" token. */
8647 cp_lexer_consume_token (parser->lexer);
8648 /* Consume the opening `('. */
8649 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8650 /* Parse the type-id. */
8651 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8652 type = cp_parser_type_id (parser);
8653 /* Look for the `,'. */
8654 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8655 token = cp_lexer_peek_token (parser->lexer);
8657 /* Build the (type *)null that begins the traditional offsetof macro. */
8658 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8659 tf_warning_or_error);
8661 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8662 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8663 true, &dummy, token->location);
8664 while (true)
8666 token = cp_lexer_peek_token (parser->lexer);
8667 switch (token->type)
8669 case CPP_OPEN_SQUARE:
8670 /* offsetof-member-designator "[" expression "]" */
8671 expr = cp_parser_postfix_open_square_expression (parser, expr,
8672 true, false);
8673 break;
8675 case CPP_DEREF:
8676 /* offsetof-member-designator "->" identifier */
8677 expr = grok_array_decl (token->location, expr,
8678 integer_zero_node, false);
8679 /* FALLTHRU */
8681 case CPP_DOT:
8682 /* offsetof-member-designator "." identifier */
8683 cp_lexer_consume_token (parser->lexer);
8684 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8685 expr, true, &dummy,
8686 token->location);
8687 break;
8689 case CPP_CLOSE_PAREN:
8690 /* Consume the ")" token. */
8691 cp_lexer_consume_token (parser->lexer);
8692 goto success;
8694 default:
8695 /* Error. We know the following require will fail, but
8696 that gives the proper error message. */
8697 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8698 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8699 expr = error_mark_node;
8700 goto failure;
8704 success:
8705 /* If we're processing a template, we can't finish the semantics yet.
8706 Otherwise we can fold the entire expression now. */
8707 if (processing_template_decl)
8709 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8710 SET_EXPR_LOCATION (expr, loc);
8712 else
8713 expr = finish_offsetof (expr, loc);
8715 failure:
8716 parser->integral_constant_expression_p = save_ice_p;
8717 parser->non_integral_constant_expression_p = save_non_ice_p;
8719 return expr;
8722 /* Parse a trait expression.
8724 Returns a representation of the expression, the underlying type
8725 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8727 static tree
8728 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8730 cp_trait_kind kind;
8731 tree type1, type2 = NULL_TREE;
8732 bool binary = false;
8733 bool variadic = false;
8735 switch (keyword)
8737 case RID_HAS_NOTHROW_ASSIGN:
8738 kind = CPTK_HAS_NOTHROW_ASSIGN;
8739 break;
8740 case RID_HAS_NOTHROW_CONSTRUCTOR:
8741 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8742 break;
8743 case RID_HAS_NOTHROW_COPY:
8744 kind = CPTK_HAS_NOTHROW_COPY;
8745 break;
8746 case RID_HAS_TRIVIAL_ASSIGN:
8747 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8748 break;
8749 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8750 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8751 break;
8752 case RID_HAS_TRIVIAL_COPY:
8753 kind = CPTK_HAS_TRIVIAL_COPY;
8754 break;
8755 case RID_HAS_TRIVIAL_DESTRUCTOR:
8756 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8757 break;
8758 case RID_HAS_VIRTUAL_DESTRUCTOR:
8759 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8760 break;
8761 case RID_IS_ABSTRACT:
8762 kind = CPTK_IS_ABSTRACT;
8763 break;
8764 case RID_IS_BASE_OF:
8765 kind = CPTK_IS_BASE_OF;
8766 binary = true;
8767 break;
8768 case RID_IS_CLASS:
8769 kind = CPTK_IS_CLASS;
8770 break;
8771 case RID_IS_EMPTY:
8772 kind = CPTK_IS_EMPTY;
8773 break;
8774 case RID_IS_ENUM:
8775 kind = CPTK_IS_ENUM;
8776 break;
8777 case RID_IS_FINAL:
8778 kind = CPTK_IS_FINAL;
8779 break;
8780 case RID_IS_LITERAL_TYPE:
8781 kind = CPTK_IS_LITERAL_TYPE;
8782 break;
8783 case RID_IS_POD:
8784 kind = CPTK_IS_POD;
8785 break;
8786 case RID_IS_POLYMORPHIC:
8787 kind = CPTK_IS_POLYMORPHIC;
8788 break;
8789 case RID_IS_STD_LAYOUT:
8790 kind = CPTK_IS_STD_LAYOUT;
8791 break;
8792 case RID_IS_TRIVIAL:
8793 kind = CPTK_IS_TRIVIAL;
8794 break;
8795 case RID_IS_TRIVIALLY_ASSIGNABLE:
8796 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8797 binary = true;
8798 break;
8799 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8800 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8801 variadic = true;
8802 break;
8803 case RID_IS_TRIVIALLY_COPYABLE:
8804 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8805 break;
8806 case RID_IS_UNION:
8807 kind = CPTK_IS_UNION;
8808 break;
8809 case RID_UNDERLYING_TYPE:
8810 kind = CPTK_UNDERLYING_TYPE;
8811 break;
8812 case RID_BASES:
8813 kind = CPTK_BASES;
8814 break;
8815 case RID_DIRECT_BASES:
8816 kind = CPTK_DIRECT_BASES;
8817 break;
8818 default:
8819 gcc_unreachable ();
8822 /* Consume the token. */
8823 cp_lexer_consume_token (parser->lexer);
8825 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8827 type1 = cp_parser_type_id (parser);
8829 if (type1 == error_mark_node)
8830 return error_mark_node;
8832 if (binary)
8834 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8836 type2 = cp_parser_type_id (parser);
8838 if (type2 == error_mark_node)
8839 return error_mark_node;
8841 else if (variadic)
8843 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8845 cp_lexer_consume_token (parser->lexer);
8846 tree elt = cp_parser_type_id (parser);
8847 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8849 cp_lexer_consume_token (parser->lexer);
8850 elt = make_pack_expansion (elt);
8852 if (elt == error_mark_node)
8853 return error_mark_node;
8854 type2 = tree_cons (NULL_TREE, elt, type2);
8858 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8860 /* Complete the trait expression, which may mean either processing
8861 the trait expr now or saving it for template instantiation. */
8862 switch(kind)
8864 case CPTK_UNDERLYING_TYPE:
8865 return finish_underlying_type (type1);
8866 case CPTK_BASES:
8867 return finish_bases (type1, false);
8868 case CPTK_DIRECT_BASES:
8869 return finish_bases (type1, true);
8870 default:
8871 return finish_trait_expr (kind, type1, type2);
8875 /* Lambdas that appear in variable initializer or default argument scope
8876 get that in their mangling, so we need to record it. We might as well
8877 use the count for function and namespace scopes as well. */
8878 static GTY(()) tree lambda_scope;
8879 static GTY(()) int lambda_count;
8880 typedef struct GTY(()) tree_int
8882 tree t;
8883 int i;
8884 } tree_int;
8885 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8887 static void
8888 start_lambda_scope (tree decl)
8890 tree_int ti;
8891 gcc_assert (decl);
8892 /* Once we're inside a function, we ignore other scopes and just push
8893 the function again so that popping works properly. */
8894 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8895 decl = current_function_decl;
8896 ti.t = lambda_scope;
8897 ti.i = lambda_count;
8898 vec_safe_push (lambda_scope_stack, ti);
8899 if (lambda_scope != decl)
8901 /* Don't reset the count if we're still in the same function. */
8902 lambda_scope = decl;
8903 lambda_count = 0;
8907 static void
8908 record_lambda_scope (tree lambda)
8910 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8911 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8914 static void
8915 finish_lambda_scope (void)
8917 tree_int *p = &lambda_scope_stack->last ();
8918 if (lambda_scope != p->t)
8920 lambda_scope = p->t;
8921 lambda_count = p->i;
8923 lambda_scope_stack->pop ();
8926 /* Parse a lambda expression.
8928 lambda-expression:
8929 lambda-introducer lambda-declarator [opt] compound-statement
8931 Returns a representation of the expression. */
8933 static tree
8934 cp_parser_lambda_expression (cp_parser* parser)
8936 tree lambda_expr = build_lambda_expr ();
8937 tree type;
8938 bool ok = true;
8939 cp_token *token = cp_lexer_peek_token (parser->lexer);
8940 cp_token_position start = 0;
8942 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8944 if (cp_unevaluated_operand)
8946 if (!token->error_reported)
8948 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8949 "lambda-expression in unevaluated context");
8950 token->error_reported = true;
8952 ok = false;
8954 else if (parser->in_template_argument_list_p)
8956 if (!token->error_reported)
8958 error_at (token->location, "lambda-expression in template-argument");
8959 token->error_reported = true;
8961 ok = false;
8964 /* We may be in the middle of deferred access check. Disable
8965 it now. */
8966 push_deferring_access_checks (dk_no_deferred);
8968 cp_parser_lambda_introducer (parser, lambda_expr);
8970 type = begin_lambda_type (lambda_expr);
8971 if (type == error_mark_node)
8972 return error_mark_node;
8974 record_lambda_scope (lambda_expr);
8976 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8977 determine_visibility (TYPE_NAME (type));
8979 /* Now that we've started the type, add the capture fields for any
8980 explicit captures. */
8981 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8984 /* Inside the class, surrounding template-parameter-lists do not apply. */
8985 unsigned int saved_num_template_parameter_lists
8986 = parser->num_template_parameter_lists;
8987 unsigned char in_statement = parser->in_statement;
8988 bool in_switch_statement_p = parser->in_switch_statement_p;
8989 bool fully_implicit_function_template_p
8990 = parser->fully_implicit_function_template_p;
8991 tree implicit_template_parms = parser->implicit_template_parms;
8992 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8993 bool auto_is_implicit_function_template_parm_p
8994 = parser->auto_is_implicit_function_template_parm_p;
8996 parser->num_template_parameter_lists = 0;
8997 parser->in_statement = 0;
8998 parser->in_switch_statement_p = false;
8999 parser->fully_implicit_function_template_p = false;
9000 parser->implicit_template_parms = 0;
9001 parser->implicit_template_scope = 0;
9002 parser->auto_is_implicit_function_template_parm_p = false;
9004 /* By virtue of defining a local class, a lambda expression has access to
9005 the private variables of enclosing classes. */
9007 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9009 if (ok)
9011 if (!cp_parser_error_occurred (parser)
9012 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9013 && cp_parser_start_tentative_firewall (parser))
9014 start = token;
9015 cp_parser_lambda_body (parser, lambda_expr);
9017 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9019 if (cp_parser_skip_to_closing_brace (parser))
9020 cp_lexer_consume_token (parser->lexer);
9023 /* The capture list was built up in reverse order; fix that now. */
9024 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9025 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9027 if (ok)
9028 maybe_add_lambda_conv_op (type);
9030 type = finish_struct (type, /*attributes=*/NULL_TREE);
9032 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9033 parser->in_statement = in_statement;
9034 parser->in_switch_statement_p = in_switch_statement_p;
9035 parser->fully_implicit_function_template_p
9036 = fully_implicit_function_template_p;
9037 parser->implicit_template_parms = implicit_template_parms;
9038 parser->implicit_template_scope = implicit_template_scope;
9039 parser->auto_is_implicit_function_template_parm_p
9040 = auto_is_implicit_function_template_parm_p;
9043 pop_deferring_access_checks ();
9045 /* This field is only used during parsing of the lambda. */
9046 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9048 /* This lambda shouldn't have any proxies left at this point. */
9049 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9050 /* And now that we're done, push proxies for an enclosing lambda. */
9051 insert_pending_capture_proxies ();
9053 if (ok)
9054 lambda_expr = build_lambda_object (lambda_expr);
9055 else
9056 lambda_expr = error_mark_node;
9058 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9060 return lambda_expr;
9063 /* Parse the beginning of a lambda expression.
9065 lambda-introducer:
9066 [ lambda-capture [opt] ]
9068 LAMBDA_EXPR is the current representation of the lambda expression. */
9070 static void
9071 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9073 /* Need commas after the first capture. */
9074 bool first = true;
9076 /* Eat the leading `['. */
9077 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9079 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9080 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9081 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9082 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9083 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9084 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9086 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9088 cp_lexer_consume_token (parser->lexer);
9089 first = false;
9092 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9094 cp_token* capture_token;
9095 tree capture_id;
9096 tree capture_init_expr;
9097 cp_id_kind idk = CP_ID_KIND_NONE;
9098 bool explicit_init_p = false;
9100 enum capture_kind_type
9102 BY_COPY,
9103 BY_REFERENCE
9105 enum capture_kind_type capture_kind = BY_COPY;
9107 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9109 error ("expected end of capture-list");
9110 return;
9113 if (first)
9114 first = false;
9115 else
9116 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9118 /* Possibly capture `this'. */
9119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9122 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9123 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9124 "with by-copy capture default");
9125 cp_lexer_consume_token (parser->lexer);
9126 add_capture (lambda_expr,
9127 /*id=*/this_identifier,
9128 /*initializer=*/finish_this_expr(),
9129 /*by_reference_p=*/false,
9130 explicit_init_p);
9131 continue;
9134 /* Remember whether we want to capture as a reference or not. */
9135 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9137 capture_kind = BY_REFERENCE;
9138 cp_lexer_consume_token (parser->lexer);
9141 /* Get the identifier. */
9142 capture_token = cp_lexer_peek_token (parser->lexer);
9143 capture_id = cp_parser_identifier (parser);
9145 if (capture_id == error_mark_node)
9146 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9147 delimiters, but I modified this to stop on unnested ']' as well. It
9148 was already changed to stop on unnested '}', so the
9149 "closing_parenthesis" name is no more misleading with my change. */
9151 cp_parser_skip_to_closing_parenthesis (parser,
9152 /*recovering=*/true,
9153 /*or_comma=*/true,
9154 /*consume_paren=*/true);
9155 break;
9158 /* Find the initializer for this capture. */
9159 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9160 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9161 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9163 bool direct, non_constant;
9164 /* An explicit initializer exists. */
9165 if (cxx_dialect < cxx14)
9166 pedwarn (input_location, 0,
9167 "lambda capture initializers "
9168 "only available with -std=c++14 or -std=gnu++14");
9169 capture_init_expr = cp_parser_initializer (parser, &direct,
9170 &non_constant);
9171 explicit_init_p = true;
9172 if (capture_init_expr == NULL_TREE)
9174 error ("empty initializer for lambda init-capture");
9175 capture_init_expr = error_mark_node;
9178 else
9180 const char* error_msg;
9182 /* Turn the identifier into an id-expression. */
9183 capture_init_expr
9184 = cp_parser_lookup_name_simple (parser, capture_id,
9185 capture_token->location);
9187 if (capture_init_expr == error_mark_node)
9189 unqualified_name_lookup_error (capture_id);
9190 continue;
9192 else if (DECL_P (capture_init_expr)
9193 && (!VAR_P (capture_init_expr)
9194 && TREE_CODE (capture_init_expr) != PARM_DECL))
9196 error_at (capture_token->location,
9197 "capture of non-variable %qD ",
9198 capture_init_expr);
9199 inform (0, "%q+#D declared here", capture_init_expr);
9200 continue;
9202 if (VAR_P (capture_init_expr)
9203 && decl_storage_duration (capture_init_expr) != dk_auto)
9205 if (pedwarn (capture_token->location, 0, "capture of variable "
9206 "%qD with non-automatic storage duration",
9207 capture_init_expr))
9208 inform (0, "%q+#D declared here", capture_init_expr);
9209 continue;
9212 capture_init_expr
9213 = finish_id_expression
9214 (capture_id,
9215 capture_init_expr,
9216 parser->scope,
9217 &idk,
9218 /*integral_constant_expression_p=*/false,
9219 /*allow_non_integral_constant_expression_p=*/false,
9220 /*non_integral_constant_expression_p=*/NULL,
9221 /*template_p=*/false,
9222 /*done=*/true,
9223 /*address_p=*/false,
9224 /*template_arg_p=*/false,
9225 &error_msg,
9226 capture_token->location);
9228 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9230 cp_lexer_consume_token (parser->lexer);
9231 capture_init_expr = make_pack_expansion (capture_init_expr);
9233 else
9234 check_for_bare_parameter_packs (capture_init_expr);
9237 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9238 && !explicit_init_p)
9240 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9241 && capture_kind == BY_COPY)
9242 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9243 "of %qD redundant with by-copy capture default",
9244 capture_id);
9245 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9246 && capture_kind == BY_REFERENCE)
9247 pedwarn (capture_token->location, 0, "explicit by-reference "
9248 "capture of %qD redundant with by-reference capture "
9249 "default", capture_id);
9252 add_capture (lambda_expr,
9253 capture_id,
9254 capture_init_expr,
9255 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9256 explicit_init_p);
9259 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9262 /* Parse the (optional) middle of a lambda expression.
9264 lambda-declarator:
9265 < template-parameter-list [opt] >
9266 ( parameter-declaration-clause [opt] )
9267 attribute-specifier [opt]
9268 mutable [opt]
9269 exception-specification [opt]
9270 lambda-return-type-clause [opt]
9272 LAMBDA_EXPR is the current representation of the lambda expression. */
9274 static bool
9275 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9277 /* 5.1.1.4 of the standard says:
9278 If a lambda-expression does not include a lambda-declarator, it is as if
9279 the lambda-declarator were ().
9280 This means an empty parameter list, no attributes, and no exception
9281 specification. */
9282 tree param_list = void_list_node;
9283 tree attributes = NULL_TREE;
9284 tree exception_spec = NULL_TREE;
9285 tree template_param_list = NULL_TREE;
9287 /* The template-parameter-list is optional, but must begin with
9288 an opening angle if present. */
9289 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9291 if (cxx_dialect < cxx14)
9292 pedwarn (parser->lexer->next_token->location, 0,
9293 "lambda templates are only available with "
9294 "-std=c++14 or -std=gnu++14");
9296 cp_lexer_consume_token (parser->lexer);
9298 template_param_list = cp_parser_template_parameter_list (parser);
9300 cp_parser_skip_to_end_of_template_parameter_list (parser);
9302 /* We just processed one more parameter list. */
9303 ++parser->num_template_parameter_lists;
9306 /* The parameter-declaration-clause is optional (unless
9307 template-parameter-list was given), but must begin with an
9308 opening parenthesis if present. */
9309 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9311 cp_lexer_consume_token (parser->lexer);
9313 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9315 /* Parse parameters. */
9316 param_list = cp_parser_parameter_declaration_clause (parser);
9318 /* Default arguments shall not be specified in the
9319 parameter-declaration-clause of a lambda-declarator. */
9320 for (tree t = param_list; t; t = TREE_CHAIN (t))
9321 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9322 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9323 "default argument specified for lambda parameter");
9325 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9327 attributes = cp_parser_attributes_opt (parser);
9329 /* Parse optional `mutable' keyword. */
9330 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9332 cp_lexer_consume_token (parser->lexer);
9333 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9336 /* Parse optional exception specification. */
9337 exception_spec = cp_parser_exception_specification_opt (parser);
9339 /* Parse optional trailing return type. */
9340 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9342 cp_lexer_consume_token (parser->lexer);
9343 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9344 = cp_parser_trailing_type_id (parser);
9347 /* The function parameters must be in scope all the way until after the
9348 trailing-return-type in case of decltype. */
9349 pop_bindings_and_leave_scope ();
9351 else if (template_param_list != NULL_TREE) // generate diagnostic
9352 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9354 /* Create the function call operator.
9356 Messing with declarators like this is no uglier than building up the
9357 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9358 other code. */
9360 cp_decl_specifier_seq return_type_specs;
9361 cp_declarator* declarator;
9362 tree fco;
9363 int quals;
9364 void *p;
9366 clear_decl_specs (&return_type_specs);
9367 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9368 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9369 else
9370 /* Maybe we will deduce the return type later. */
9371 return_type_specs.type = make_auto ();
9373 p = obstack_alloc (&declarator_obstack, 0);
9375 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9376 sfk_none);
9378 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9379 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9380 declarator = make_call_declarator (declarator, param_list, quals,
9381 VIRT_SPEC_UNSPECIFIED,
9382 REF_QUAL_NONE,
9383 exception_spec,
9384 /*late_return_type=*/NULL_TREE);
9385 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9387 fco = grokmethod (&return_type_specs,
9388 declarator,
9389 attributes);
9390 if (fco != error_mark_node)
9392 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9393 DECL_ARTIFICIAL (fco) = 1;
9394 /* Give the object parameter a different name. */
9395 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9396 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9397 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9399 if (template_param_list)
9401 fco = finish_member_template_decl (fco);
9402 finish_template_decl (template_param_list);
9403 --parser->num_template_parameter_lists;
9405 else if (parser->fully_implicit_function_template_p)
9406 fco = finish_fully_implicit_template (parser, fco);
9408 finish_member_declaration (fco);
9410 obstack_free (&declarator_obstack, p);
9412 return (fco != error_mark_node);
9416 /* Parse the body of a lambda expression, which is simply
9418 compound-statement
9420 but which requires special handling.
9421 LAMBDA_EXPR is the current representation of the lambda expression. */
9423 static void
9424 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9426 bool nested = (current_function_decl != NULL_TREE);
9427 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9428 if (nested)
9429 push_function_context ();
9430 else
9431 /* Still increment function_depth so that we don't GC in the
9432 middle of an expression. */
9433 ++function_depth;
9434 /* Clear this in case we're in the middle of a default argument. */
9435 parser->local_variables_forbidden_p = false;
9437 /* Finish the function call operator
9438 - class_specifier
9439 + late_parsing_for_member
9440 + function_definition_after_declarator
9441 + ctor_initializer_opt_and_function_body */
9443 tree fco = lambda_function (lambda_expr);
9444 tree body;
9445 bool done = false;
9446 tree compound_stmt;
9447 tree cap;
9449 /* Let the front end know that we are going to be defining this
9450 function. */
9451 start_preparsed_function (fco,
9452 NULL_TREE,
9453 SF_PRE_PARSED | SF_INCLASS_INLINE);
9455 start_lambda_scope (fco);
9456 body = begin_function_body ();
9458 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9459 goto out;
9461 /* Push the proxies for any explicit captures. */
9462 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9463 cap = TREE_CHAIN (cap))
9464 build_capture_proxy (TREE_PURPOSE (cap));
9466 compound_stmt = begin_compound_stmt (0);
9468 /* 5.1.1.4 of the standard says:
9469 If a lambda-expression does not include a trailing-return-type, it
9470 is as if the trailing-return-type denotes the following type:
9471 * if the compound-statement is of the form
9472 { return attribute-specifier [opt] expression ; }
9473 the type of the returned expression after lvalue-to-rvalue
9474 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9475 (_conv.array_ 4.2), and function-to-pointer conversion
9476 (_conv.func_ 4.3);
9477 * otherwise, void. */
9479 /* In a lambda that has neither a lambda-return-type-clause
9480 nor a deducible form, errors should be reported for return statements
9481 in the body. Since we used void as the placeholder return type, parsing
9482 the body as usual will give such desired behavior. */
9483 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9484 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9485 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9487 tree expr = NULL_TREE;
9488 cp_id_kind idk = CP_ID_KIND_NONE;
9490 /* Parse tentatively in case there's more after the initial return
9491 statement. */
9492 cp_parser_parse_tentatively (parser);
9494 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9496 expr = cp_parser_expression (parser, &idk);
9498 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9499 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9501 if (cp_parser_parse_definitely (parser))
9503 if (!processing_template_decl)
9504 apply_deduced_return_type (fco, lambda_return_type (expr));
9506 /* Will get error here if type not deduced yet. */
9507 finish_return_stmt (expr);
9509 done = true;
9513 if (!done)
9515 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9516 cp_parser_label_declaration (parser);
9517 cp_parser_statement_seq_opt (parser, NULL_TREE);
9518 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9521 finish_compound_stmt (compound_stmt);
9523 out:
9524 finish_function_body (body);
9525 finish_lambda_scope ();
9527 /* Finish the function and generate code for it if necessary. */
9528 tree fn = finish_function (/*inline*/2);
9530 /* Only expand if the call op is not a template. */
9531 if (!DECL_TEMPLATE_INFO (fco))
9532 expand_or_defer_fn (fn);
9535 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9536 if (nested)
9537 pop_function_context();
9538 else
9539 --function_depth;
9542 /* Statements [gram.stmt.stmt] */
9544 /* Parse a statement.
9546 statement:
9547 labeled-statement
9548 expression-statement
9549 compound-statement
9550 selection-statement
9551 iteration-statement
9552 jump-statement
9553 declaration-statement
9554 try-block
9556 C++11:
9558 statement:
9559 labeled-statement
9560 attribute-specifier-seq (opt) expression-statement
9561 attribute-specifier-seq (opt) compound-statement
9562 attribute-specifier-seq (opt) selection-statement
9563 attribute-specifier-seq (opt) iteration-statement
9564 attribute-specifier-seq (opt) jump-statement
9565 declaration-statement
9566 attribute-specifier-seq (opt) try-block
9568 TM Extension:
9570 statement:
9571 atomic-statement
9573 IN_COMPOUND is true when the statement is nested inside a
9574 cp_parser_compound_statement; this matters for certain pragmas.
9576 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9577 is a (possibly labeled) if statement which is not enclosed in braces
9578 and has an else clause. This is used to implement -Wparentheses. */
9580 static void
9581 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9582 bool in_compound, bool *if_p)
9584 tree statement, std_attrs = NULL_TREE;
9585 cp_token *token;
9586 location_t statement_location, attrs_location;
9588 restart:
9589 if (if_p != NULL)
9590 *if_p = false;
9591 /* There is no statement yet. */
9592 statement = NULL_TREE;
9594 saved_token_sentinel saved_tokens (parser->lexer);
9595 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9596 if (c_dialect_objc ())
9597 /* In obj-c++, seeing '[[' might be the either the beginning of
9598 c++11 attributes, or a nested objc-message-expression. So
9599 let's parse the c++11 attributes tentatively. */
9600 cp_parser_parse_tentatively (parser);
9601 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9602 if (c_dialect_objc ())
9604 if (!cp_parser_parse_definitely (parser))
9605 std_attrs = NULL_TREE;
9608 /* Peek at the next token. */
9609 token = cp_lexer_peek_token (parser->lexer);
9610 /* Remember the location of the first token in the statement. */
9611 statement_location = token->location;
9612 /* If this is a keyword, then that will often determine what kind of
9613 statement we have. */
9614 if (token->type == CPP_KEYWORD)
9616 enum rid keyword = token->keyword;
9618 switch (keyword)
9620 case RID_CASE:
9621 case RID_DEFAULT:
9622 /* Looks like a labeled-statement with a case label.
9623 Parse the label, and then use tail recursion to parse
9624 the statement. */
9625 cp_parser_label_for_labeled_statement (parser, std_attrs);
9626 goto restart;
9628 case RID_IF:
9629 case RID_SWITCH:
9630 statement = cp_parser_selection_statement (parser, if_p);
9631 break;
9633 case RID_WHILE:
9634 case RID_DO:
9635 case RID_FOR:
9636 statement = cp_parser_iteration_statement (parser, false);
9637 break;
9639 case RID_CILK_FOR:
9640 if (!flag_cilkplus)
9642 error_at (cp_lexer_peek_token (parser->lexer)->location,
9643 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9644 cp_lexer_consume_token (parser->lexer);
9645 statement = error_mark_node;
9647 else
9648 statement = cp_parser_cilk_for (parser, integer_zero_node);
9649 break;
9651 case RID_BREAK:
9652 case RID_CONTINUE:
9653 case RID_RETURN:
9654 case RID_GOTO:
9655 statement = cp_parser_jump_statement (parser);
9656 break;
9658 case RID_CILK_SYNC:
9659 cp_lexer_consume_token (parser->lexer);
9660 if (flag_cilkplus)
9662 tree sync_expr = build_cilk_sync ();
9663 SET_EXPR_LOCATION (sync_expr,
9664 token->location);
9665 statement = finish_expr_stmt (sync_expr);
9667 else
9669 error_at (token->location, "-fcilkplus must be enabled to use"
9670 " %<_Cilk_sync%>");
9671 statement = error_mark_node;
9673 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9674 break;
9676 /* Objective-C++ exception-handling constructs. */
9677 case RID_AT_TRY:
9678 case RID_AT_CATCH:
9679 case RID_AT_FINALLY:
9680 case RID_AT_SYNCHRONIZED:
9681 case RID_AT_THROW:
9682 statement = cp_parser_objc_statement (parser);
9683 break;
9685 case RID_TRY:
9686 statement = cp_parser_try_block (parser);
9687 break;
9689 case RID_NAMESPACE:
9690 /* This must be a namespace alias definition. */
9691 cp_parser_declaration_statement (parser);
9692 return;
9694 case RID_TRANSACTION_ATOMIC:
9695 case RID_TRANSACTION_RELAXED:
9696 statement = cp_parser_transaction (parser, keyword);
9697 break;
9698 case RID_TRANSACTION_CANCEL:
9699 statement = cp_parser_transaction_cancel (parser);
9700 break;
9702 default:
9703 /* It might be a keyword like `int' that can start a
9704 declaration-statement. */
9705 break;
9708 else if (token->type == CPP_NAME)
9710 /* If the next token is a `:', then we are looking at a
9711 labeled-statement. */
9712 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9713 if (token->type == CPP_COLON)
9715 /* Looks like a labeled-statement with an ordinary label.
9716 Parse the label, and then use tail recursion to parse
9717 the statement. */
9719 cp_parser_label_for_labeled_statement (parser, std_attrs);
9720 goto restart;
9723 /* Anything that starts with a `{' must be a compound-statement. */
9724 else if (token->type == CPP_OPEN_BRACE)
9725 statement = cp_parser_compound_statement (parser, NULL, false, false);
9726 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9727 a statement all its own. */
9728 else if (token->type == CPP_PRAGMA)
9730 /* Only certain OpenMP pragmas are attached to statements, and thus
9731 are considered statements themselves. All others are not. In
9732 the context of a compound, accept the pragma as a "statement" and
9733 return so that we can check for a close brace. Otherwise we
9734 require a real statement and must go back and read one. */
9735 if (in_compound)
9736 cp_parser_pragma (parser, pragma_compound);
9737 else if (!cp_parser_pragma (parser, pragma_stmt))
9738 goto restart;
9739 return;
9741 else if (token->type == CPP_EOF)
9743 cp_parser_error (parser, "expected statement");
9744 return;
9747 /* Everything else must be a declaration-statement or an
9748 expression-statement. Try for the declaration-statement
9749 first, unless we are looking at a `;', in which case we know that
9750 we have an expression-statement. */
9751 if (!statement)
9753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9755 if (std_attrs != NULL_TREE)
9757 /* Attributes should be parsed as part of the the
9758 declaration, so let's un-parse them. */
9759 saved_tokens.rollback();
9760 std_attrs = NULL_TREE;
9763 cp_parser_parse_tentatively (parser);
9764 /* Try to parse the declaration-statement. */
9765 cp_parser_declaration_statement (parser);
9766 /* If that worked, we're done. */
9767 if (cp_parser_parse_definitely (parser))
9768 return;
9770 /* Look for an expression-statement instead. */
9771 statement = cp_parser_expression_statement (parser, in_statement_expr);
9774 /* Set the line number for the statement. */
9775 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9776 SET_EXPR_LOCATION (statement, statement_location);
9778 /* Note that for now, we don't do anything with c++11 statements
9779 parsed at this level. */
9780 if (std_attrs != NULL_TREE)
9781 warning_at (attrs_location,
9782 OPT_Wattributes,
9783 "attributes at the beginning of statement are ignored");
9786 /* Parse the label for a labeled-statement, i.e.
9788 identifier :
9789 case constant-expression :
9790 default :
9792 GNU Extension:
9793 case constant-expression ... constant-expression : statement
9795 When a label is parsed without errors, the label is added to the
9796 parse tree by the finish_* functions, so this function doesn't
9797 have to return the label. */
9799 static void
9800 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9802 cp_token *token;
9803 tree label = NULL_TREE;
9804 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9806 /* The next token should be an identifier. */
9807 token = cp_lexer_peek_token (parser->lexer);
9808 if (token->type != CPP_NAME
9809 && token->type != CPP_KEYWORD)
9811 cp_parser_error (parser, "expected labeled-statement");
9812 return;
9815 parser->colon_corrects_to_scope_p = false;
9816 switch (token->keyword)
9818 case RID_CASE:
9820 tree expr, expr_hi;
9821 cp_token *ellipsis;
9823 /* Consume the `case' token. */
9824 cp_lexer_consume_token (parser->lexer);
9825 /* Parse the constant-expression. */
9826 expr = cp_parser_constant_expression (parser);
9827 if (check_for_bare_parameter_packs (expr))
9828 expr = error_mark_node;
9830 ellipsis = cp_lexer_peek_token (parser->lexer);
9831 if (ellipsis->type == CPP_ELLIPSIS)
9833 /* Consume the `...' token. */
9834 cp_lexer_consume_token (parser->lexer);
9835 expr_hi = cp_parser_constant_expression (parser);
9836 if (check_for_bare_parameter_packs (expr_hi))
9837 expr_hi = error_mark_node;
9839 /* We don't need to emit warnings here, as the common code
9840 will do this for us. */
9842 else
9843 expr_hi = NULL_TREE;
9845 if (parser->in_switch_statement_p)
9846 finish_case_label (token->location, expr, expr_hi);
9847 else
9848 error_at (token->location,
9849 "case label %qE not within a switch statement",
9850 expr);
9852 break;
9854 case RID_DEFAULT:
9855 /* Consume the `default' token. */
9856 cp_lexer_consume_token (parser->lexer);
9858 if (parser->in_switch_statement_p)
9859 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9860 else
9861 error_at (token->location, "case label not within a switch statement");
9862 break;
9864 default:
9865 /* Anything else must be an ordinary label. */
9866 label = finish_label_stmt (cp_parser_identifier (parser));
9867 break;
9870 /* Require the `:' token. */
9871 cp_parser_require (parser, CPP_COLON, RT_COLON);
9873 /* An ordinary label may optionally be followed by attributes.
9874 However, this is only permitted if the attributes are then
9875 followed by a semicolon. This is because, for backward
9876 compatibility, when parsing
9877 lab: __attribute__ ((unused)) int i;
9878 we want the attribute to attach to "i", not "lab". */
9879 if (label != NULL_TREE
9880 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9882 tree attrs;
9883 cp_parser_parse_tentatively (parser);
9884 attrs = cp_parser_gnu_attributes_opt (parser);
9885 if (attrs == NULL_TREE
9886 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9887 cp_parser_abort_tentative_parse (parser);
9888 else if (!cp_parser_parse_definitely (parser))
9890 else
9891 attributes = chainon (attributes, attrs);
9894 if (attributes != NULL_TREE)
9895 cplus_decl_attributes (&label, attributes, 0);
9897 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9900 /* Parse an expression-statement.
9902 expression-statement:
9903 expression [opt] ;
9905 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9906 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9907 indicates whether this expression-statement is part of an
9908 expression statement. */
9910 static tree
9911 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9913 tree statement = NULL_TREE;
9914 cp_token *token = cp_lexer_peek_token (parser->lexer);
9916 /* If the next token is a ';', then there is no expression
9917 statement. */
9918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9920 statement = cp_parser_expression (parser);
9921 if (statement == error_mark_node
9922 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9924 cp_parser_skip_to_end_of_block_or_statement (parser);
9925 return error_mark_node;
9929 /* Give a helpful message for "A<T>::type t;" and the like. */
9930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9931 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9933 if (TREE_CODE (statement) == SCOPE_REF)
9934 error_at (token->location, "need %<typename%> before %qE because "
9935 "%qT is a dependent scope",
9936 statement, TREE_OPERAND (statement, 0));
9937 else if (is_overloaded_fn (statement)
9938 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9940 /* A::A a; */
9941 tree fn = get_first_fn (statement);
9942 error_at (token->location,
9943 "%<%T::%D%> names the constructor, not the type",
9944 DECL_CONTEXT (fn), DECL_NAME (fn));
9948 /* Consume the final `;'. */
9949 cp_parser_consume_semicolon_at_end_of_statement (parser);
9951 if (in_statement_expr
9952 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9953 /* This is the final expression statement of a statement
9954 expression. */
9955 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9956 else if (statement)
9957 statement = finish_expr_stmt (statement);
9959 return statement;
9962 /* Parse a compound-statement.
9964 compound-statement:
9965 { statement-seq [opt] }
9967 GNU extension:
9969 compound-statement:
9970 { label-declaration-seq [opt] statement-seq [opt] }
9972 label-declaration-seq:
9973 label-declaration
9974 label-declaration-seq label-declaration
9976 Returns a tree representing the statement. */
9978 static tree
9979 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9980 bool in_try, bool function_body)
9982 tree compound_stmt;
9984 /* Consume the `{'. */
9985 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9986 return error_mark_node;
9987 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9988 && !function_body && cxx_dialect < cxx14)
9989 pedwarn (input_location, OPT_Wpedantic,
9990 "compound-statement in constexpr function");
9991 /* Begin the compound-statement. */
9992 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9993 /* If the next keyword is `__label__' we have a label declaration. */
9994 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9995 cp_parser_label_declaration (parser);
9996 /* Parse an (optional) statement-seq. */
9997 cp_parser_statement_seq_opt (parser, in_statement_expr);
9998 /* Finish the compound-statement. */
9999 finish_compound_stmt (compound_stmt);
10000 /* Consume the `}'. */
10001 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10003 return compound_stmt;
10006 /* Parse an (optional) statement-seq.
10008 statement-seq:
10009 statement
10010 statement-seq [opt] statement */
10012 static void
10013 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10015 /* Scan statements until there aren't any more. */
10016 while (true)
10018 cp_token *token = cp_lexer_peek_token (parser->lexer);
10020 /* If we are looking at a `}', then we have run out of
10021 statements; the same is true if we have reached the end
10022 of file, or have stumbled upon a stray '@end'. */
10023 if (token->type == CPP_CLOSE_BRACE
10024 || token->type == CPP_EOF
10025 || token->type == CPP_PRAGMA_EOL
10026 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10027 break;
10029 /* If we are in a compound statement and find 'else' then
10030 something went wrong. */
10031 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10033 if (parser->in_statement & IN_IF_STMT)
10034 break;
10035 else
10037 token = cp_lexer_consume_token (parser->lexer);
10038 error_at (token->location, "%<else%> without a previous %<if%>");
10042 /* Parse the statement. */
10043 cp_parser_statement (parser, in_statement_expr, true, NULL);
10047 /* Parse a selection-statement.
10049 selection-statement:
10050 if ( condition ) statement
10051 if ( condition ) statement else statement
10052 switch ( condition ) statement
10054 Returns the new IF_STMT or SWITCH_STMT.
10056 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10057 is a (possibly labeled) if statement which is not enclosed in
10058 braces and has an else clause. This is used to implement
10059 -Wparentheses. */
10061 static tree
10062 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10064 cp_token *token;
10065 enum rid keyword;
10067 if (if_p != NULL)
10068 *if_p = false;
10070 /* Peek at the next token. */
10071 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10073 /* See what kind of keyword it is. */
10074 keyword = token->keyword;
10075 switch (keyword)
10077 case RID_IF:
10078 case RID_SWITCH:
10080 tree statement;
10081 tree condition;
10083 /* Look for the `('. */
10084 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10086 cp_parser_skip_to_end_of_statement (parser);
10087 return error_mark_node;
10090 /* Begin the selection-statement. */
10091 if (keyword == RID_IF)
10092 statement = begin_if_stmt ();
10093 else
10094 statement = begin_switch_stmt ();
10096 /* Parse the condition. */
10097 condition = cp_parser_condition (parser);
10098 /* Look for the `)'. */
10099 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10100 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10101 /*consume_paren=*/true);
10103 if (keyword == RID_IF)
10105 bool nested_if;
10106 unsigned char in_statement;
10108 /* Add the condition. */
10109 finish_if_stmt_cond (condition, statement);
10111 /* Parse the then-clause. */
10112 in_statement = parser->in_statement;
10113 parser->in_statement |= IN_IF_STMT;
10114 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10116 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10117 add_stmt (build_empty_stmt (loc));
10118 cp_lexer_consume_token (parser->lexer);
10119 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10120 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10121 "empty body in an %<if%> statement");
10122 nested_if = false;
10124 else
10125 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10126 parser->in_statement = in_statement;
10128 finish_then_clause (statement);
10130 /* If the next token is `else', parse the else-clause. */
10131 if (cp_lexer_next_token_is_keyword (parser->lexer,
10132 RID_ELSE))
10134 /* Consume the `else' keyword. */
10135 cp_lexer_consume_token (parser->lexer);
10136 begin_else_clause (statement);
10137 /* Parse the else-clause. */
10138 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10140 location_t loc;
10141 loc = cp_lexer_peek_token (parser->lexer)->location;
10142 warning_at (loc,
10143 OPT_Wempty_body, "suggest braces around "
10144 "empty body in an %<else%> statement");
10145 add_stmt (build_empty_stmt (loc));
10146 cp_lexer_consume_token (parser->lexer);
10148 else
10149 cp_parser_implicitly_scoped_statement (parser, NULL);
10151 finish_else_clause (statement);
10153 /* If we are currently parsing a then-clause, then
10154 IF_P will not be NULL. We set it to true to
10155 indicate that this if statement has an else clause.
10156 This may trigger the Wparentheses warning below
10157 when we get back up to the parent if statement. */
10158 if (if_p != NULL)
10159 *if_p = true;
10161 else
10163 /* This if statement does not have an else clause. If
10164 NESTED_IF is true, then the then-clause is an if
10165 statement which does have an else clause. We warn
10166 about the potential ambiguity. */
10167 if (nested_if)
10168 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10169 "suggest explicit braces to avoid ambiguous"
10170 " %<else%>");
10173 /* Now we're all done with the if-statement. */
10174 finish_if_stmt (statement);
10176 else
10178 bool in_switch_statement_p;
10179 unsigned char in_statement;
10181 /* Add the condition. */
10182 finish_switch_cond (condition, statement);
10184 /* Parse the body of the switch-statement. */
10185 in_switch_statement_p = parser->in_switch_statement_p;
10186 in_statement = parser->in_statement;
10187 parser->in_switch_statement_p = true;
10188 parser->in_statement |= IN_SWITCH_STMT;
10189 cp_parser_implicitly_scoped_statement (parser, NULL);
10190 parser->in_switch_statement_p = in_switch_statement_p;
10191 parser->in_statement = in_statement;
10193 /* Now we're all done with the switch-statement. */
10194 finish_switch_stmt (statement);
10197 return statement;
10199 break;
10201 default:
10202 cp_parser_error (parser, "expected selection-statement");
10203 return error_mark_node;
10207 /* Parse a condition.
10209 condition:
10210 expression
10211 type-specifier-seq declarator = initializer-clause
10212 type-specifier-seq declarator braced-init-list
10214 GNU Extension:
10216 condition:
10217 type-specifier-seq declarator asm-specification [opt]
10218 attributes [opt] = assignment-expression
10220 Returns the expression that should be tested. */
10222 static tree
10223 cp_parser_condition (cp_parser* parser)
10225 cp_decl_specifier_seq type_specifiers;
10226 const char *saved_message;
10227 int declares_class_or_enum;
10229 /* Try the declaration first. */
10230 cp_parser_parse_tentatively (parser);
10231 /* New types are not allowed in the type-specifier-seq for a
10232 condition. */
10233 saved_message = parser->type_definition_forbidden_message;
10234 parser->type_definition_forbidden_message
10235 = G_("types may not be defined in conditions");
10236 /* Parse the type-specifier-seq. */
10237 cp_parser_decl_specifier_seq (parser,
10238 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10239 &type_specifiers,
10240 &declares_class_or_enum);
10241 /* Restore the saved message. */
10242 parser->type_definition_forbidden_message = saved_message;
10243 /* If all is well, we might be looking at a declaration. */
10244 if (!cp_parser_error_occurred (parser))
10246 tree decl;
10247 tree asm_specification;
10248 tree attributes;
10249 cp_declarator *declarator;
10250 tree initializer = NULL_TREE;
10252 /* Parse the declarator. */
10253 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10254 /*ctor_dtor_or_conv_p=*/NULL,
10255 /*parenthesized_p=*/NULL,
10256 /*member_p=*/false,
10257 /*friend_p=*/false);
10258 /* Parse the attributes. */
10259 attributes = cp_parser_attributes_opt (parser);
10260 /* Parse the asm-specification. */
10261 asm_specification = cp_parser_asm_specification_opt (parser);
10262 /* If the next token is not an `=' or '{', then we might still be
10263 looking at an expression. For example:
10265 if (A(a).x)
10267 looks like a decl-specifier-seq and a declarator -- but then
10268 there is no `=', so this is an expression. */
10269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10270 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10271 cp_parser_simulate_error (parser);
10273 /* If we did see an `=' or '{', then we are looking at a declaration
10274 for sure. */
10275 if (cp_parser_parse_definitely (parser))
10277 tree pushed_scope;
10278 bool non_constant_p;
10279 bool flags = LOOKUP_ONLYCONVERTING;
10281 /* Create the declaration. */
10282 decl = start_decl (declarator, &type_specifiers,
10283 /*initialized_p=*/true,
10284 attributes, /*prefix_attributes=*/NULL_TREE,
10285 &pushed_scope);
10287 /* Parse the initializer. */
10288 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10290 initializer = cp_parser_braced_list (parser, &non_constant_p);
10291 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10292 flags = 0;
10294 else
10296 /* Consume the `='. */
10297 cp_parser_require (parser, CPP_EQ, RT_EQ);
10298 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10300 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10301 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10303 /* Process the initializer. */
10304 cp_finish_decl (decl,
10305 initializer, !non_constant_p,
10306 asm_specification,
10307 flags);
10309 if (pushed_scope)
10310 pop_scope (pushed_scope);
10312 return convert_from_reference (decl);
10315 /* If we didn't even get past the declarator successfully, we are
10316 definitely not looking at a declaration. */
10317 else
10318 cp_parser_abort_tentative_parse (parser);
10320 /* Otherwise, we are looking at an expression. */
10321 return cp_parser_expression (parser);
10324 /* Parses a for-statement or range-for-statement until the closing ')',
10325 not included. */
10327 static tree
10328 cp_parser_for (cp_parser *parser, bool ivdep)
10330 tree init, scope, decl;
10331 bool is_range_for;
10333 /* Begin the for-statement. */
10334 scope = begin_for_scope (&init);
10336 /* Parse the initialization. */
10337 is_range_for = cp_parser_for_init_statement (parser, &decl);
10339 if (is_range_for)
10340 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10341 else
10342 return cp_parser_c_for (parser, scope, init, ivdep);
10345 static tree
10346 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10348 /* Normal for loop */
10349 tree condition = NULL_TREE;
10350 tree expression = NULL_TREE;
10351 tree stmt;
10353 stmt = begin_for_stmt (scope, init);
10354 /* The for-init-statement has already been parsed in
10355 cp_parser_for_init_statement, so no work is needed here. */
10356 finish_for_init_stmt (stmt);
10358 /* If there's a condition, process it. */
10359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10360 condition = cp_parser_condition (parser);
10361 else if (ivdep)
10363 cp_parser_error (parser, "missing loop condition in loop with "
10364 "%<GCC ivdep%> pragma");
10365 condition = error_mark_node;
10367 finish_for_cond (condition, stmt, ivdep);
10368 /* Look for the `;'. */
10369 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10371 /* If there's an expression, process it. */
10372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10373 expression = cp_parser_expression (parser);
10374 finish_for_expr (expression, stmt);
10376 return stmt;
10379 /* Tries to parse a range-based for-statement:
10381 range-based-for:
10382 decl-specifier-seq declarator : expression
10384 The decl-specifier-seq declarator and the `:' are already parsed by
10385 cp_parser_for_init_statement. If processing_template_decl it returns a
10386 newly created RANGE_FOR_STMT; if not, it is converted to a
10387 regular FOR_STMT. */
10389 static tree
10390 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10391 bool ivdep)
10393 tree stmt, range_expr;
10395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10397 bool expr_non_constant_p;
10398 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10400 else
10401 range_expr = cp_parser_expression (parser);
10403 /* If in template, STMT is converted to a normal for-statement
10404 at instantiation. If not, it is done just ahead. */
10405 if (processing_template_decl)
10407 if (check_for_bare_parameter_packs (range_expr))
10408 range_expr = error_mark_node;
10409 stmt = begin_range_for_stmt (scope, init);
10410 if (ivdep)
10411 RANGE_FOR_IVDEP (stmt) = 1;
10412 finish_range_for_decl (stmt, range_decl, range_expr);
10413 if (!type_dependent_expression_p (range_expr)
10414 /* do_auto_deduction doesn't mess with template init-lists. */
10415 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10416 do_range_for_auto_deduction (range_decl, range_expr);
10418 else
10420 stmt = begin_for_stmt (scope, init);
10421 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10423 return stmt;
10426 /* Subroutine of cp_convert_range_for: given the initializer expression,
10427 builds up the range temporary. */
10429 static tree
10430 build_range_temp (tree range_expr)
10432 tree range_type, range_temp;
10434 /* Find out the type deduced by the declaration
10435 `auto &&__range = range_expr'. */
10436 range_type = cp_build_reference_type (make_auto (), true);
10437 range_type = do_auto_deduction (range_type, range_expr,
10438 type_uses_auto (range_type));
10440 /* Create the __range variable. */
10441 range_temp = build_decl (input_location, VAR_DECL,
10442 get_identifier ("__for_range"), range_type);
10443 TREE_USED (range_temp) = 1;
10444 DECL_ARTIFICIAL (range_temp) = 1;
10446 return range_temp;
10449 /* Used by cp_parser_range_for in template context: we aren't going to
10450 do a full conversion yet, but we still need to resolve auto in the
10451 type of the for-range-declaration if present. This is basically
10452 a shortcut version of cp_convert_range_for. */
10454 static void
10455 do_range_for_auto_deduction (tree decl, tree range_expr)
10457 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10458 if (auto_node)
10460 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10461 range_temp = convert_from_reference (build_range_temp (range_expr));
10462 iter_type = (cp_parser_perform_range_for_lookup
10463 (range_temp, &begin_dummy, &end_dummy));
10464 if (iter_type)
10466 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10467 iter_type);
10468 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10469 tf_warning_or_error);
10470 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10471 iter_decl, auto_node);
10476 /* Converts a range-based for-statement into a normal
10477 for-statement, as per the definition.
10479 for (RANGE_DECL : RANGE_EXPR)
10480 BLOCK
10482 should be equivalent to:
10485 auto &&__range = RANGE_EXPR;
10486 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10487 __begin != __end;
10488 ++__begin)
10490 RANGE_DECL = *__begin;
10491 BLOCK
10495 If RANGE_EXPR is an array:
10496 BEGIN_EXPR = __range
10497 END_EXPR = __range + ARRAY_SIZE(__range)
10498 Else if RANGE_EXPR has a member 'begin' or 'end':
10499 BEGIN_EXPR = __range.begin()
10500 END_EXPR = __range.end()
10501 Else:
10502 BEGIN_EXPR = begin(__range)
10503 END_EXPR = end(__range);
10505 If __range has a member 'begin' but not 'end', or vice versa, we must
10506 still use the second alternative (it will surely fail, however).
10507 When calling begin()/end() in the third alternative we must use
10508 argument dependent lookup, but always considering 'std' as an associated
10509 namespace. */
10511 tree
10512 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10513 bool ivdep)
10515 tree begin, end;
10516 tree iter_type, begin_expr, end_expr;
10517 tree condition, expression;
10519 if (range_decl == error_mark_node || range_expr == error_mark_node)
10520 /* If an error happened previously do nothing or else a lot of
10521 unhelpful errors would be issued. */
10522 begin_expr = end_expr = iter_type = error_mark_node;
10523 else
10525 tree range_temp;
10527 if (TREE_CODE (range_expr) == VAR_DECL
10528 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10529 /* Can't bind a reference to an array of runtime bound. */
10530 range_temp = range_expr;
10531 else
10533 range_temp = build_range_temp (range_expr);
10534 pushdecl (range_temp);
10535 cp_finish_decl (range_temp, range_expr,
10536 /*is_constant_init*/false, NULL_TREE,
10537 LOOKUP_ONLYCONVERTING);
10538 range_temp = convert_from_reference (range_temp);
10540 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10541 &begin_expr, &end_expr);
10544 /* The new for initialization statement. */
10545 begin = build_decl (input_location, VAR_DECL,
10546 get_identifier ("__for_begin"), iter_type);
10547 TREE_USED (begin) = 1;
10548 DECL_ARTIFICIAL (begin) = 1;
10549 pushdecl (begin);
10550 cp_finish_decl (begin, begin_expr,
10551 /*is_constant_init*/false, NULL_TREE,
10552 LOOKUP_ONLYCONVERTING);
10554 end = build_decl (input_location, VAR_DECL,
10555 get_identifier ("__for_end"), iter_type);
10556 TREE_USED (end) = 1;
10557 DECL_ARTIFICIAL (end) = 1;
10558 pushdecl (end);
10559 cp_finish_decl (end, end_expr,
10560 /*is_constant_init*/false, NULL_TREE,
10561 LOOKUP_ONLYCONVERTING);
10563 finish_for_init_stmt (statement);
10565 /* The new for condition. */
10566 condition = build_x_binary_op (input_location, NE_EXPR,
10567 begin, ERROR_MARK,
10568 end, ERROR_MARK,
10569 NULL, tf_warning_or_error);
10570 finish_for_cond (condition, statement, ivdep);
10572 /* The new increment expression. */
10573 expression = finish_unary_op_expr (input_location,
10574 PREINCREMENT_EXPR, begin,
10575 tf_warning_or_error);
10576 finish_for_expr (expression, statement);
10578 /* The declaration is initialized with *__begin inside the loop body. */
10579 cp_finish_decl (range_decl,
10580 build_x_indirect_ref (input_location, begin, RO_NULL,
10581 tf_warning_or_error),
10582 /*is_constant_init*/false, NULL_TREE,
10583 LOOKUP_ONLYCONVERTING);
10585 return statement;
10588 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10589 We need to solve both at the same time because the method used
10590 depends on the existence of members begin or end.
10591 Returns the type deduced for the iterator expression. */
10593 static tree
10594 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10596 if (error_operand_p (range))
10598 *begin = *end = error_mark_node;
10599 return error_mark_node;
10602 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10604 error ("range-based %<for%> expression of type %qT "
10605 "has incomplete type", TREE_TYPE (range));
10606 *begin = *end = error_mark_node;
10607 return error_mark_node;
10609 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10611 /* If RANGE is an array, we will use pointer arithmetic. */
10612 *begin = range;
10613 *end = build_binary_op (input_location, PLUS_EXPR,
10614 range,
10615 array_type_nelts_top (TREE_TYPE (range)),
10617 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10619 else
10621 /* If it is not an array, we must do a bit of magic. */
10622 tree id_begin, id_end;
10623 tree member_begin, member_end;
10625 *begin = *end = error_mark_node;
10627 id_begin = get_identifier ("begin");
10628 id_end = get_identifier ("end");
10629 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10630 /*protect=*/2, /*want_type=*/false,
10631 tf_warning_or_error);
10632 member_end = lookup_member (TREE_TYPE (range), id_end,
10633 /*protect=*/2, /*want_type=*/false,
10634 tf_warning_or_error);
10636 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10638 /* Use the member functions. */
10639 if (member_begin != NULL_TREE)
10640 *begin = cp_parser_range_for_member_function (range, id_begin);
10641 else
10642 error ("range-based %<for%> expression of type %qT has an "
10643 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10645 if (member_end != NULL_TREE)
10646 *end = cp_parser_range_for_member_function (range, id_end);
10647 else
10648 error ("range-based %<for%> expression of type %qT has a "
10649 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10651 else
10653 /* Use global functions with ADL. */
10654 vec<tree, va_gc> *vec;
10655 vec = make_tree_vector ();
10657 vec_safe_push (vec, range);
10659 member_begin = perform_koenig_lookup (id_begin, vec,
10660 tf_warning_or_error);
10661 *begin = finish_call_expr (member_begin, &vec, false, true,
10662 tf_warning_or_error);
10663 member_end = perform_koenig_lookup (id_end, vec,
10664 tf_warning_or_error);
10665 *end = finish_call_expr (member_end, &vec, false, true,
10666 tf_warning_or_error);
10668 release_tree_vector (vec);
10671 /* Last common checks. */
10672 if (*begin == error_mark_node || *end == error_mark_node)
10674 /* If one of the expressions is an error do no more checks. */
10675 *begin = *end = error_mark_node;
10676 return error_mark_node;
10678 else if (type_dependent_expression_p (*begin)
10679 || type_dependent_expression_p (*end))
10680 /* Can happen, when, eg, in a template context, Koenig lookup
10681 can't resolve begin/end (c++/58503). */
10682 return NULL_TREE;
10683 else
10685 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10686 /* The unqualified type of the __begin and __end temporaries should
10687 be the same, as required by the multiple auto declaration. */
10688 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10689 error ("inconsistent begin/end types in range-based %<for%> "
10690 "statement: %qT and %qT",
10691 TREE_TYPE (*begin), TREE_TYPE (*end));
10692 return iter_type;
10697 /* Helper function for cp_parser_perform_range_for_lookup.
10698 Builds a tree for RANGE.IDENTIFIER(). */
10700 static tree
10701 cp_parser_range_for_member_function (tree range, tree identifier)
10703 tree member, res;
10704 vec<tree, va_gc> *vec;
10706 member = finish_class_member_access_expr (range, identifier,
10707 false, tf_warning_or_error);
10708 if (member == error_mark_node)
10709 return error_mark_node;
10711 vec = make_tree_vector ();
10712 res = finish_call_expr (member, &vec,
10713 /*disallow_virtual=*/false,
10714 /*koenig_p=*/false,
10715 tf_warning_or_error);
10716 release_tree_vector (vec);
10717 return res;
10720 /* Parse an iteration-statement.
10722 iteration-statement:
10723 while ( condition ) statement
10724 do statement while ( expression ) ;
10725 for ( for-init-statement condition [opt] ; expression [opt] )
10726 statement
10728 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10730 static tree
10731 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10733 cp_token *token;
10734 enum rid keyword;
10735 tree statement;
10736 unsigned char in_statement;
10738 /* Peek at the next token. */
10739 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10740 if (!token)
10741 return error_mark_node;
10743 /* Remember whether or not we are already within an iteration
10744 statement. */
10745 in_statement = parser->in_statement;
10747 /* See what kind of keyword it is. */
10748 keyword = token->keyword;
10749 switch (keyword)
10751 case RID_WHILE:
10753 tree condition;
10755 /* Begin the while-statement. */
10756 statement = begin_while_stmt ();
10757 /* Look for the `('. */
10758 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10759 /* Parse the condition. */
10760 condition = cp_parser_condition (parser);
10761 finish_while_stmt_cond (condition, statement, ivdep);
10762 /* Look for the `)'. */
10763 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10764 /* Parse the dependent statement. */
10765 parser->in_statement = IN_ITERATION_STMT;
10766 cp_parser_already_scoped_statement (parser);
10767 parser->in_statement = in_statement;
10768 /* We're done with the while-statement. */
10769 finish_while_stmt (statement);
10771 break;
10773 case RID_DO:
10775 tree expression;
10777 /* Begin the do-statement. */
10778 statement = begin_do_stmt ();
10779 /* Parse the body of the do-statement. */
10780 parser->in_statement = IN_ITERATION_STMT;
10781 cp_parser_implicitly_scoped_statement (parser, NULL);
10782 parser->in_statement = in_statement;
10783 finish_do_body (statement);
10784 /* Look for the `while' keyword. */
10785 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10786 /* Look for the `('. */
10787 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10788 /* Parse the expression. */
10789 expression = cp_parser_expression (parser);
10790 /* We're done with the do-statement. */
10791 finish_do_stmt (expression, statement, ivdep);
10792 /* Look for the `)'. */
10793 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10794 /* Look for the `;'. */
10795 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10797 break;
10799 case RID_FOR:
10801 /* Look for the `('. */
10802 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10804 statement = cp_parser_for (parser, ivdep);
10806 /* Look for the `)'. */
10807 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10809 /* Parse the body of the for-statement. */
10810 parser->in_statement = IN_ITERATION_STMT;
10811 cp_parser_already_scoped_statement (parser);
10812 parser->in_statement = in_statement;
10814 /* We're done with the for-statement. */
10815 finish_for_stmt (statement);
10817 break;
10819 default:
10820 cp_parser_error (parser, "expected iteration-statement");
10821 statement = error_mark_node;
10822 break;
10825 return statement;
10828 /* Parse a for-init-statement or the declarator of a range-based-for.
10829 Returns true if a range-based-for declaration is seen.
10831 for-init-statement:
10832 expression-statement
10833 simple-declaration */
10835 static bool
10836 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10838 /* If the next token is a `;', then we have an empty
10839 expression-statement. Grammatically, this is also a
10840 simple-declaration, but an invalid one, because it does not
10841 declare anything. Therefore, if we did not handle this case
10842 specially, we would issue an error message about an invalid
10843 declaration. */
10844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10846 bool is_range_for = false;
10847 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10849 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10850 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10852 /* N3994 -- for (id : init) ... */
10853 if (cxx_dialect < cxx1z)
10854 pedwarn (input_location, 0, "range-based for loop without a "
10855 "type-specifier only available with "
10856 "-std=c++1z or -std=gnu++1z");
10857 tree name = cp_parser_identifier (parser);
10858 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10859 *decl = build_decl (input_location, VAR_DECL, name, type);
10860 pushdecl (*decl);
10861 cp_lexer_consume_token (parser->lexer);
10862 return true;
10865 /* A colon is used in range-based for. */
10866 parser->colon_corrects_to_scope_p = false;
10868 /* We're going to speculatively look for a declaration, falling back
10869 to an expression, if necessary. */
10870 cp_parser_parse_tentatively (parser);
10871 /* Parse the declaration. */
10872 cp_parser_simple_declaration (parser,
10873 /*function_definition_allowed_p=*/false,
10874 decl);
10875 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10876 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10878 /* It is a range-for, consume the ':' */
10879 cp_lexer_consume_token (parser->lexer);
10880 is_range_for = true;
10881 if (cxx_dialect < cxx11)
10883 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10884 "range-based %<for%> loops only available with "
10885 "-std=c++11 or -std=gnu++11");
10886 *decl = error_mark_node;
10889 else
10890 /* The ';' is not consumed yet because we told
10891 cp_parser_simple_declaration not to. */
10892 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10894 if (cp_parser_parse_definitely (parser))
10895 return is_range_for;
10896 /* If the tentative parse failed, then we shall need to look for an
10897 expression-statement. */
10899 /* If we are here, it is an expression-statement. */
10900 cp_parser_expression_statement (parser, NULL_TREE);
10901 return false;
10904 /* Parse a jump-statement.
10906 jump-statement:
10907 break ;
10908 continue ;
10909 return expression [opt] ;
10910 return braced-init-list ;
10911 goto identifier ;
10913 GNU extension:
10915 jump-statement:
10916 goto * expression ;
10918 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10920 static tree
10921 cp_parser_jump_statement (cp_parser* parser)
10923 tree statement = error_mark_node;
10924 cp_token *token;
10925 enum rid keyword;
10926 unsigned char in_statement;
10928 /* Peek at the next token. */
10929 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10930 if (!token)
10931 return error_mark_node;
10933 /* See what kind of keyword it is. */
10934 keyword = token->keyword;
10935 switch (keyword)
10937 case RID_BREAK:
10938 in_statement = parser->in_statement & ~IN_IF_STMT;
10939 switch (in_statement)
10941 case 0:
10942 error_at (token->location, "break statement not within loop or switch");
10943 break;
10944 default:
10945 gcc_assert ((in_statement & IN_SWITCH_STMT)
10946 || in_statement == IN_ITERATION_STMT);
10947 statement = finish_break_stmt ();
10948 if (in_statement == IN_ITERATION_STMT)
10949 break_maybe_infinite_loop ();
10950 break;
10951 case IN_OMP_BLOCK:
10952 error_at (token->location, "invalid exit from OpenMP structured block");
10953 break;
10954 case IN_OMP_FOR:
10955 error_at (token->location, "break statement used with OpenMP for loop");
10956 break;
10957 case IN_CILK_SIMD_FOR:
10958 error_at (token->location, "break statement used with Cilk Plus for loop");
10959 break;
10961 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10962 break;
10964 case RID_CONTINUE:
10965 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10967 case 0:
10968 error_at (token->location, "continue statement not within a loop");
10969 break;
10970 case IN_CILK_SIMD_FOR:
10971 error_at (token->location,
10972 "continue statement within %<#pragma simd%> loop body");
10973 /* Fall through. */
10974 case IN_ITERATION_STMT:
10975 case IN_OMP_FOR:
10976 statement = finish_continue_stmt ();
10977 break;
10978 case IN_OMP_BLOCK:
10979 error_at (token->location, "invalid exit from OpenMP structured block");
10980 break;
10981 default:
10982 gcc_unreachable ();
10984 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10985 break;
10987 case RID_RETURN:
10989 tree expr;
10990 bool expr_non_constant_p;
10992 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10994 cp_lexer_set_source_position (parser->lexer);
10995 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10996 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10998 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10999 expr = cp_parser_expression (parser);
11000 else
11001 /* If the next token is a `;', then there is no
11002 expression. */
11003 expr = NULL_TREE;
11004 /* Build the return-statement. */
11005 statement = finish_return_stmt (expr);
11006 /* Look for the final `;'. */
11007 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11009 break;
11011 case RID_GOTO:
11012 if (parser->in_function_body
11013 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11015 error ("%<goto%> in %<constexpr%> function");
11016 cp_function_chain->invalid_constexpr = true;
11019 /* Create the goto-statement. */
11020 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11022 /* Issue a warning about this use of a GNU extension. */
11023 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11024 /* Consume the '*' token. */
11025 cp_lexer_consume_token (parser->lexer);
11026 /* Parse the dependent expression. */
11027 finish_goto_stmt (cp_parser_expression (parser));
11029 else
11030 finish_goto_stmt (cp_parser_identifier (parser));
11031 /* Look for the final `;'. */
11032 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11033 break;
11035 default:
11036 cp_parser_error (parser, "expected jump-statement");
11037 break;
11040 return statement;
11043 /* Parse a declaration-statement.
11045 declaration-statement:
11046 block-declaration */
11048 static void
11049 cp_parser_declaration_statement (cp_parser* parser)
11051 void *p;
11053 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11054 p = obstack_alloc (&declarator_obstack, 0);
11056 /* Parse the block-declaration. */
11057 cp_parser_block_declaration (parser, /*statement_p=*/true);
11059 /* Free any declarators allocated. */
11060 obstack_free (&declarator_obstack, p);
11063 /* Some dependent statements (like `if (cond) statement'), are
11064 implicitly in their own scope. In other words, if the statement is
11065 a single statement (as opposed to a compound-statement), it is
11066 none-the-less treated as if it were enclosed in braces. Any
11067 declarations appearing in the dependent statement are out of scope
11068 after control passes that point. This function parses a statement,
11069 but ensures that is in its own scope, even if it is not a
11070 compound-statement.
11072 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11073 is a (possibly labeled) if statement which is not enclosed in
11074 braces and has an else clause. This is used to implement
11075 -Wparentheses.
11077 Returns the new statement. */
11079 static tree
11080 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11082 tree statement;
11084 if (if_p != NULL)
11085 *if_p = false;
11087 /* Mark if () ; with a special NOP_EXPR. */
11088 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11090 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11091 cp_lexer_consume_token (parser->lexer);
11092 statement = add_stmt (build_empty_stmt (loc));
11094 /* if a compound is opened, we simply parse the statement directly. */
11095 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11096 statement = cp_parser_compound_statement (parser, NULL, false, false);
11097 /* If the token is not a `{', then we must take special action. */
11098 else
11100 /* Create a compound-statement. */
11101 statement = begin_compound_stmt (0);
11102 /* Parse the dependent-statement. */
11103 cp_parser_statement (parser, NULL_TREE, false, if_p);
11104 /* Finish the dummy compound-statement. */
11105 finish_compound_stmt (statement);
11108 /* Return the statement. */
11109 return statement;
11112 /* For some dependent statements (like `while (cond) statement'), we
11113 have already created a scope. Therefore, even if the dependent
11114 statement is a compound-statement, we do not want to create another
11115 scope. */
11117 static void
11118 cp_parser_already_scoped_statement (cp_parser* parser)
11120 /* If the token is a `{', then we must take special action. */
11121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11122 cp_parser_statement (parser, NULL_TREE, false, NULL);
11123 else
11125 /* Avoid calling cp_parser_compound_statement, so that we
11126 don't create a new scope. Do everything else by hand. */
11127 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11128 /* If the next keyword is `__label__' we have a label declaration. */
11129 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11130 cp_parser_label_declaration (parser);
11131 /* Parse an (optional) statement-seq. */
11132 cp_parser_statement_seq_opt (parser, NULL_TREE);
11133 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11137 /* Declarations [gram.dcl.dcl] */
11139 /* Parse an optional declaration-sequence.
11141 declaration-seq:
11142 declaration
11143 declaration-seq declaration */
11145 static void
11146 cp_parser_declaration_seq_opt (cp_parser* parser)
11148 while (true)
11150 cp_token *token;
11152 token = cp_lexer_peek_token (parser->lexer);
11154 if (token->type == CPP_CLOSE_BRACE
11155 || token->type == CPP_EOF
11156 || token->type == CPP_PRAGMA_EOL)
11157 break;
11159 if (token->type == CPP_SEMICOLON)
11161 /* A declaration consisting of a single semicolon is
11162 invalid. Allow it unless we're being pedantic. */
11163 cp_lexer_consume_token (parser->lexer);
11164 if (!in_system_header_at (input_location))
11165 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11166 continue;
11169 /* If we're entering or exiting a region that's implicitly
11170 extern "C", modify the lang context appropriately. */
11171 if (!parser->implicit_extern_c && token->implicit_extern_c)
11173 push_lang_context (lang_name_c);
11174 parser->implicit_extern_c = true;
11176 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11178 pop_lang_context ();
11179 parser->implicit_extern_c = false;
11182 if (token->type == CPP_PRAGMA)
11184 /* A top-level declaration can consist solely of a #pragma.
11185 A nested declaration cannot, so this is done here and not
11186 in cp_parser_declaration. (A #pragma at block scope is
11187 handled in cp_parser_statement.) */
11188 cp_parser_pragma (parser, pragma_external);
11189 continue;
11192 /* Parse the declaration itself. */
11193 cp_parser_declaration (parser);
11197 /* Parse a declaration.
11199 declaration:
11200 block-declaration
11201 function-definition
11202 template-declaration
11203 explicit-instantiation
11204 explicit-specialization
11205 linkage-specification
11206 namespace-definition
11208 GNU extension:
11210 declaration:
11211 __extension__ declaration */
11213 static void
11214 cp_parser_declaration (cp_parser* parser)
11216 cp_token token1;
11217 cp_token token2;
11218 int saved_pedantic;
11219 void *p;
11220 tree attributes = NULL_TREE;
11222 /* Check for the `__extension__' keyword. */
11223 if (cp_parser_extension_opt (parser, &saved_pedantic))
11225 /* Parse the qualified declaration. */
11226 cp_parser_declaration (parser);
11227 /* Restore the PEDANTIC flag. */
11228 pedantic = saved_pedantic;
11230 return;
11233 /* Try to figure out what kind of declaration is present. */
11234 token1 = *cp_lexer_peek_token (parser->lexer);
11236 if (token1.type != CPP_EOF)
11237 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11238 else
11240 token2.type = CPP_EOF;
11241 token2.keyword = RID_MAX;
11244 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11245 p = obstack_alloc (&declarator_obstack, 0);
11247 /* If the next token is `extern' and the following token is a string
11248 literal, then we have a linkage specification. */
11249 if (token1.keyword == RID_EXTERN
11250 && cp_parser_is_pure_string_literal (&token2))
11251 cp_parser_linkage_specification (parser);
11252 /* If the next token is `template', then we have either a template
11253 declaration, an explicit instantiation, or an explicit
11254 specialization. */
11255 else if (token1.keyword == RID_TEMPLATE)
11257 /* `template <>' indicates a template specialization. */
11258 if (token2.type == CPP_LESS
11259 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11260 cp_parser_explicit_specialization (parser);
11261 /* `template <' indicates a template declaration. */
11262 else if (token2.type == CPP_LESS)
11263 cp_parser_template_declaration (parser, /*member_p=*/false);
11264 /* Anything else must be an explicit instantiation. */
11265 else
11266 cp_parser_explicit_instantiation (parser);
11268 /* If the next token is `export', then we have a template
11269 declaration. */
11270 else if (token1.keyword == RID_EXPORT)
11271 cp_parser_template_declaration (parser, /*member_p=*/false);
11272 /* If the next token is `extern', 'static' or 'inline' and the one
11273 after that is `template', we have a GNU extended explicit
11274 instantiation directive. */
11275 else if (cp_parser_allow_gnu_extensions_p (parser)
11276 && (token1.keyword == RID_EXTERN
11277 || token1.keyword == RID_STATIC
11278 || token1.keyword == RID_INLINE)
11279 && token2.keyword == RID_TEMPLATE)
11280 cp_parser_explicit_instantiation (parser);
11281 /* If the next token is `namespace', check for a named or unnamed
11282 namespace definition. */
11283 else if (token1.keyword == RID_NAMESPACE
11284 && (/* A named namespace definition. */
11285 (token2.type == CPP_NAME
11286 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11287 != CPP_EQ))
11288 /* An unnamed namespace definition. */
11289 || token2.type == CPP_OPEN_BRACE
11290 || token2.keyword == RID_ATTRIBUTE))
11291 cp_parser_namespace_definition (parser);
11292 /* An inline (associated) namespace definition. */
11293 else if (token1.keyword == RID_INLINE
11294 && token2.keyword == RID_NAMESPACE)
11295 cp_parser_namespace_definition (parser);
11296 /* Objective-C++ declaration/definition. */
11297 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11298 cp_parser_objc_declaration (parser, NULL_TREE);
11299 else if (c_dialect_objc ()
11300 && token1.keyword == RID_ATTRIBUTE
11301 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11302 cp_parser_objc_declaration (parser, attributes);
11303 /* We must have either a block declaration or a function
11304 definition. */
11305 else
11306 /* Try to parse a block-declaration, or a function-definition. */
11307 cp_parser_block_declaration (parser, /*statement_p=*/false);
11309 /* Free any declarators allocated. */
11310 obstack_free (&declarator_obstack, p);
11313 /* Parse a block-declaration.
11315 block-declaration:
11316 simple-declaration
11317 asm-definition
11318 namespace-alias-definition
11319 using-declaration
11320 using-directive
11322 GNU Extension:
11324 block-declaration:
11325 __extension__ block-declaration
11327 C++0x Extension:
11329 block-declaration:
11330 static_assert-declaration
11332 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11333 part of a declaration-statement. */
11335 static void
11336 cp_parser_block_declaration (cp_parser *parser,
11337 bool statement_p)
11339 cp_token *token1;
11340 int saved_pedantic;
11342 /* Check for the `__extension__' keyword. */
11343 if (cp_parser_extension_opt (parser, &saved_pedantic))
11345 /* Parse the qualified declaration. */
11346 cp_parser_block_declaration (parser, statement_p);
11347 /* Restore the PEDANTIC flag. */
11348 pedantic = saved_pedantic;
11350 return;
11353 /* Peek at the next token to figure out which kind of declaration is
11354 present. */
11355 token1 = cp_lexer_peek_token (parser->lexer);
11357 /* If the next keyword is `asm', we have an asm-definition. */
11358 if (token1->keyword == RID_ASM)
11360 if (statement_p)
11361 cp_parser_commit_to_tentative_parse (parser);
11362 cp_parser_asm_definition (parser);
11364 /* If the next keyword is `namespace', we have a
11365 namespace-alias-definition. */
11366 else if (token1->keyword == RID_NAMESPACE)
11367 cp_parser_namespace_alias_definition (parser);
11368 /* If the next keyword is `using', we have a
11369 using-declaration, a using-directive, or an alias-declaration. */
11370 else if (token1->keyword == RID_USING)
11372 cp_token *token2;
11374 if (statement_p)
11375 cp_parser_commit_to_tentative_parse (parser);
11376 /* If the token after `using' is `namespace', then we have a
11377 using-directive. */
11378 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11379 if (token2->keyword == RID_NAMESPACE)
11380 cp_parser_using_directive (parser);
11381 /* If the second token after 'using' is '=', then we have an
11382 alias-declaration. */
11383 else if (cxx_dialect >= cxx11
11384 && token2->type == CPP_NAME
11385 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11386 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11387 cp_parser_alias_declaration (parser);
11388 /* Otherwise, it's a using-declaration. */
11389 else
11390 cp_parser_using_declaration (parser,
11391 /*access_declaration_p=*/false);
11393 /* If the next keyword is `__label__' we have a misplaced label
11394 declaration. */
11395 else if (token1->keyword == RID_LABEL)
11397 cp_lexer_consume_token (parser->lexer);
11398 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11399 cp_parser_skip_to_end_of_statement (parser);
11400 /* If the next token is now a `;', consume it. */
11401 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11402 cp_lexer_consume_token (parser->lexer);
11404 /* If the next token is `static_assert' we have a static assertion. */
11405 else if (token1->keyword == RID_STATIC_ASSERT)
11406 cp_parser_static_assert (parser, /*member_p=*/false);
11407 /* Anything else must be a simple-declaration. */
11408 else
11409 cp_parser_simple_declaration (parser, !statement_p,
11410 /*maybe_range_for_decl*/NULL);
11413 /* Parse a simple-declaration.
11415 simple-declaration:
11416 decl-specifier-seq [opt] init-declarator-list [opt] ;
11418 init-declarator-list:
11419 init-declarator
11420 init-declarator-list , init-declarator
11422 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11423 function-definition as a simple-declaration.
11425 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11426 parsed declaration if it is an uninitialized single declarator not followed
11427 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11428 if present, will not be consumed. */
11430 static void
11431 cp_parser_simple_declaration (cp_parser* parser,
11432 bool function_definition_allowed_p,
11433 tree *maybe_range_for_decl)
11435 cp_decl_specifier_seq decl_specifiers;
11436 int declares_class_or_enum;
11437 bool saw_declarator;
11439 if (maybe_range_for_decl)
11440 *maybe_range_for_decl = NULL_TREE;
11442 /* Defer access checks until we know what is being declared; the
11443 checks for names appearing in the decl-specifier-seq should be
11444 done as if we were in the scope of the thing being declared. */
11445 push_deferring_access_checks (dk_deferred);
11447 /* Parse the decl-specifier-seq. We have to keep track of whether
11448 or not the decl-specifier-seq declares a named class or
11449 enumeration type, since that is the only case in which the
11450 init-declarator-list is allowed to be empty.
11452 [dcl.dcl]
11454 In a simple-declaration, the optional init-declarator-list can be
11455 omitted only when declaring a class or enumeration, that is when
11456 the decl-specifier-seq contains either a class-specifier, an
11457 elaborated-type-specifier, or an enum-specifier. */
11458 cp_parser_decl_specifier_seq (parser,
11459 CP_PARSER_FLAGS_OPTIONAL,
11460 &decl_specifiers,
11461 &declares_class_or_enum);
11462 /* We no longer need to defer access checks. */
11463 stop_deferring_access_checks ();
11465 /* In a block scope, a valid declaration must always have a
11466 decl-specifier-seq. By not trying to parse declarators, we can
11467 resolve the declaration/expression ambiguity more quickly. */
11468 if (!function_definition_allowed_p
11469 && !decl_specifiers.any_specifiers_p)
11471 cp_parser_error (parser, "expected declaration");
11472 goto done;
11475 /* If the next two tokens are both identifiers, the code is
11476 erroneous. The usual cause of this situation is code like:
11478 T t;
11480 where "T" should name a type -- but does not. */
11481 if (!decl_specifiers.any_type_specifiers_p
11482 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11484 /* If parsing tentatively, we should commit; we really are
11485 looking at a declaration. */
11486 cp_parser_commit_to_tentative_parse (parser);
11487 /* Give up. */
11488 goto done;
11491 /* If we have seen at least one decl-specifier, and the next token
11492 is not a parenthesis, then we must be looking at a declaration.
11493 (After "int (" we might be looking at a functional cast.) */
11494 if (decl_specifiers.any_specifiers_p
11495 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11496 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11497 && !cp_parser_error_occurred (parser))
11498 cp_parser_commit_to_tentative_parse (parser);
11500 /* Keep going until we hit the `;' at the end of the simple
11501 declaration. */
11502 saw_declarator = false;
11503 while (cp_lexer_next_token_is_not (parser->lexer,
11504 CPP_SEMICOLON))
11506 cp_token *token;
11507 bool function_definition_p;
11508 tree decl;
11510 if (saw_declarator)
11512 /* If we are processing next declarator, coma is expected */
11513 token = cp_lexer_peek_token (parser->lexer);
11514 gcc_assert (token->type == CPP_COMMA);
11515 cp_lexer_consume_token (parser->lexer);
11516 if (maybe_range_for_decl)
11517 *maybe_range_for_decl = error_mark_node;
11519 else
11520 saw_declarator = true;
11522 /* Parse the init-declarator. */
11523 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11524 /*checks=*/NULL,
11525 function_definition_allowed_p,
11526 /*member_p=*/false,
11527 declares_class_or_enum,
11528 &function_definition_p,
11529 maybe_range_for_decl);
11530 /* If an error occurred while parsing tentatively, exit quickly.
11531 (That usually happens when in the body of a function; each
11532 statement is treated as a declaration-statement until proven
11533 otherwise.) */
11534 if (cp_parser_error_occurred (parser))
11535 goto done;
11536 /* Handle function definitions specially. */
11537 if (function_definition_p)
11539 /* If the next token is a `,', then we are probably
11540 processing something like:
11542 void f() {}, *p;
11544 which is erroneous. */
11545 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11547 cp_token *token = cp_lexer_peek_token (parser->lexer);
11548 error_at (token->location,
11549 "mixing"
11550 " declarations and function-definitions is forbidden");
11552 /* Otherwise, we're done with the list of declarators. */
11553 else
11555 pop_deferring_access_checks ();
11556 return;
11559 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11560 *maybe_range_for_decl = decl;
11561 /* The next token should be either a `,' or a `;'. */
11562 token = cp_lexer_peek_token (parser->lexer);
11563 /* If it's a `,', there are more declarators to come. */
11564 if (token->type == CPP_COMMA)
11565 /* will be consumed next time around */;
11566 /* If it's a `;', we are done. */
11567 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11568 break;
11569 /* Anything else is an error. */
11570 else
11572 /* If we have already issued an error message we don't need
11573 to issue another one. */
11574 if (decl != error_mark_node
11575 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11576 cp_parser_error (parser, "expected %<,%> or %<;%>");
11577 /* Skip tokens until we reach the end of the statement. */
11578 cp_parser_skip_to_end_of_statement (parser);
11579 /* If the next token is now a `;', consume it. */
11580 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11581 cp_lexer_consume_token (parser->lexer);
11582 goto done;
11584 /* After the first time around, a function-definition is not
11585 allowed -- even if it was OK at first. For example:
11587 int i, f() {}
11589 is not valid. */
11590 function_definition_allowed_p = false;
11593 /* Issue an error message if no declarators are present, and the
11594 decl-specifier-seq does not itself declare a class or
11595 enumeration: [dcl.dcl]/3. */
11596 if (!saw_declarator)
11598 if (cp_parser_declares_only_class_p (parser))
11600 if (!declares_class_or_enum
11601 && decl_specifiers.type
11602 && OVERLOAD_TYPE_P (decl_specifiers.type))
11603 /* Ensure an error is issued anyway when finish_decltype_type,
11604 called via cp_parser_decl_specifier_seq, returns a class or
11605 an enumeration (c++/51786). */
11606 decl_specifiers.type = NULL_TREE;
11607 shadow_tag (&decl_specifiers);
11609 /* Perform any deferred access checks. */
11610 perform_deferred_access_checks (tf_warning_or_error);
11613 /* Consume the `;'. */
11614 if (!maybe_range_for_decl)
11615 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11617 done:
11618 pop_deferring_access_checks ();
11621 /* Parse a decl-specifier-seq.
11623 decl-specifier-seq:
11624 decl-specifier-seq [opt] decl-specifier
11625 decl-specifier attribute-specifier-seq [opt] (C++11)
11627 decl-specifier:
11628 storage-class-specifier
11629 type-specifier
11630 function-specifier
11631 friend
11632 typedef
11634 GNU Extension:
11636 decl-specifier:
11637 attributes
11639 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11641 The parser flags FLAGS is used to control type-specifier parsing.
11643 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11644 flags:
11646 1: one of the decl-specifiers is an elaborated-type-specifier
11647 (i.e., a type declaration)
11648 2: one of the decl-specifiers is an enum-specifier or a
11649 class-specifier (i.e., a type definition)
11653 static void
11654 cp_parser_decl_specifier_seq (cp_parser* parser,
11655 cp_parser_flags flags,
11656 cp_decl_specifier_seq *decl_specs,
11657 int* declares_class_or_enum)
11659 bool constructor_possible_p = !parser->in_declarator_p;
11660 bool found_decl_spec = false;
11661 cp_token *start_token = NULL;
11662 cp_decl_spec ds;
11664 /* Clear DECL_SPECS. */
11665 clear_decl_specs (decl_specs);
11667 /* Assume no class or enumeration type is declared. */
11668 *declares_class_or_enum = 0;
11670 /* Keep reading specifiers until there are no more to read. */
11671 while (true)
11673 bool constructor_p;
11674 cp_token *token;
11675 ds = ds_last;
11677 /* Peek at the next token. */
11678 token = cp_lexer_peek_token (parser->lexer);
11680 /* Save the first token of the decl spec list for error
11681 reporting. */
11682 if (!start_token)
11683 start_token = token;
11684 /* Handle attributes. */
11685 if (cp_next_tokens_can_be_attribute_p (parser))
11687 /* Parse the attributes. */
11688 tree attrs = cp_parser_attributes_opt (parser);
11690 /* In a sequence of declaration specifiers, c++11 attributes
11691 appertain to the type that precede them. In that case
11692 [dcl.spec]/1 says:
11694 The attribute-specifier-seq affects the type only for
11695 the declaration it appears in, not other declarations
11696 involving the same type.
11698 But for now let's force the user to position the
11699 attribute either at the beginning of the declaration or
11700 after the declarator-id, which would clearly mean that it
11701 applies to the declarator. */
11702 if (cxx11_attribute_p (attrs))
11704 if (!found_decl_spec)
11705 /* The c++11 attribute is at the beginning of the
11706 declaration. It appertains to the entity being
11707 declared. */;
11708 else
11710 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11712 /* This is an attribute following a
11713 class-specifier. */
11714 if (decl_specs->type_definition_p)
11715 warn_misplaced_attr_for_class_type (token->location,
11716 decl_specs->type);
11717 attrs = NULL_TREE;
11719 else
11721 decl_specs->std_attributes
11722 = chainon (decl_specs->std_attributes,
11723 attrs);
11724 if (decl_specs->locations[ds_std_attribute] == 0)
11725 decl_specs->locations[ds_std_attribute] = token->location;
11727 continue;
11731 decl_specs->attributes
11732 = chainon (decl_specs->attributes,
11733 attrs);
11734 if (decl_specs->locations[ds_attribute] == 0)
11735 decl_specs->locations[ds_attribute] = token->location;
11736 continue;
11738 /* Assume we will find a decl-specifier keyword. */
11739 found_decl_spec = true;
11740 /* If the next token is an appropriate keyword, we can simply
11741 add it to the list. */
11742 switch (token->keyword)
11744 /* decl-specifier:
11745 friend
11746 constexpr */
11747 case RID_FRIEND:
11748 if (!at_class_scope_p ())
11750 error_at (token->location, "%<friend%> used outside of class");
11751 cp_lexer_purge_token (parser->lexer);
11753 else
11755 ds = ds_friend;
11756 /* Consume the token. */
11757 cp_lexer_consume_token (parser->lexer);
11759 break;
11761 case RID_CONSTEXPR:
11762 ds = ds_constexpr;
11763 cp_lexer_consume_token (parser->lexer);
11764 break;
11766 /* function-specifier:
11767 inline
11768 virtual
11769 explicit */
11770 case RID_INLINE:
11771 case RID_VIRTUAL:
11772 case RID_EXPLICIT:
11773 cp_parser_function_specifier_opt (parser, decl_specs);
11774 break;
11776 /* decl-specifier:
11777 typedef */
11778 case RID_TYPEDEF:
11779 ds = ds_typedef;
11780 /* Consume the token. */
11781 cp_lexer_consume_token (parser->lexer);
11782 /* A constructor declarator cannot appear in a typedef. */
11783 constructor_possible_p = false;
11784 /* The "typedef" keyword can only occur in a declaration; we
11785 may as well commit at this point. */
11786 cp_parser_commit_to_tentative_parse (parser);
11788 if (decl_specs->storage_class != sc_none)
11789 decl_specs->conflicting_specifiers_p = true;
11790 break;
11792 /* storage-class-specifier:
11793 auto
11794 register
11795 static
11796 extern
11797 mutable
11799 GNU Extension:
11800 thread */
11801 case RID_AUTO:
11802 if (cxx_dialect == cxx98)
11804 /* Consume the token. */
11805 cp_lexer_consume_token (parser->lexer);
11807 /* Complain about `auto' as a storage specifier, if
11808 we're complaining about C++0x compatibility. */
11809 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11810 " changes meaning in C++11; please remove it");
11812 /* Set the storage class anyway. */
11813 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11814 token);
11816 else
11817 /* C++0x auto type-specifier. */
11818 found_decl_spec = false;
11819 break;
11821 case RID_REGISTER:
11822 case RID_STATIC:
11823 case RID_EXTERN:
11824 case RID_MUTABLE:
11825 /* Consume the token. */
11826 cp_lexer_consume_token (parser->lexer);
11827 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11828 token);
11829 break;
11830 case RID_THREAD:
11831 /* Consume the token. */
11832 ds = ds_thread;
11833 cp_lexer_consume_token (parser->lexer);
11834 break;
11836 default:
11837 /* We did not yet find a decl-specifier yet. */
11838 found_decl_spec = false;
11839 break;
11842 if (found_decl_spec
11843 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11844 && token->keyword != RID_CONSTEXPR)
11845 error ("decl-specifier invalid in condition");
11847 if (ds != ds_last)
11848 set_and_check_decl_spec_loc (decl_specs, ds, token);
11850 /* Constructors are a special case. The `S' in `S()' is not a
11851 decl-specifier; it is the beginning of the declarator. */
11852 constructor_p
11853 = (!found_decl_spec
11854 && constructor_possible_p
11855 && (cp_parser_constructor_declarator_p
11856 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11858 /* If we don't have a DECL_SPEC yet, then we must be looking at
11859 a type-specifier. */
11860 if (!found_decl_spec && !constructor_p)
11862 int decl_spec_declares_class_or_enum;
11863 bool is_cv_qualifier;
11864 tree type_spec;
11866 type_spec
11867 = cp_parser_type_specifier (parser, flags,
11868 decl_specs,
11869 /*is_declaration=*/true,
11870 &decl_spec_declares_class_or_enum,
11871 &is_cv_qualifier);
11872 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11874 /* If this type-specifier referenced a user-defined type
11875 (a typedef, class-name, etc.), then we can't allow any
11876 more such type-specifiers henceforth.
11878 [dcl.spec]
11880 The longest sequence of decl-specifiers that could
11881 possibly be a type name is taken as the
11882 decl-specifier-seq of a declaration. The sequence shall
11883 be self-consistent as described below.
11885 [dcl.type]
11887 As a general rule, at most one type-specifier is allowed
11888 in the complete decl-specifier-seq of a declaration. The
11889 only exceptions are the following:
11891 -- const or volatile can be combined with any other
11892 type-specifier.
11894 -- signed or unsigned can be combined with char, long,
11895 short, or int.
11897 -- ..
11899 Example:
11901 typedef char* Pc;
11902 void g (const int Pc);
11904 Here, Pc is *not* part of the decl-specifier seq; it's
11905 the declarator. Therefore, once we see a type-specifier
11906 (other than a cv-qualifier), we forbid any additional
11907 user-defined types. We *do* still allow things like `int
11908 int' to be considered a decl-specifier-seq, and issue the
11909 error message later. */
11910 if (type_spec && !is_cv_qualifier)
11911 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11912 /* A constructor declarator cannot follow a type-specifier. */
11913 if (type_spec)
11915 constructor_possible_p = false;
11916 found_decl_spec = true;
11917 if (!is_cv_qualifier)
11918 decl_specs->any_type_specifiers_p = true;
11922 /* If we still do not have a DECL_SPEC, then there are no more
11923 decl-specifiers. */
11924 if (!found_decl_spec)
11925 break;
11927 decl_specs->any_specifiers_p = true;
11928 /* After we see one decl-specifier, further decl-specifiers are
11929 always optional. */
11930 flags |= CP_PARSER_FLAGS_OPTIONAL;
11933 /* Don't allow a friend specifier with a class definition. */
11934 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11935 && (*declares_class_or_enum & 2))
11936 error_at (decl_specs->locations[ds_friend],
11937 "class definition may not be declared a friend");
11940 /* Parse an (optional) storage-class-specifier.
11942 storage-class-specifier:
11943 auto
11944 register
11945 static
11946 extern
11947 mutable
11949 GNU Extension:
11951 storage-class-specifier:
11952 thread
11954 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11956 static tree
11957 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11959 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11961 case RID_AUTO:
11962 if (cxx_dialect != cxx98)
11963 return NULL_TREE;
11964 /* Fall through for C++98. */
11966 case RID_REGISTER:
11967 case RID_STATIC:
11968 case RID_EXTERN:
11969 case RID_MUTABLE:
11970 case RID_THREAD:
11971 /* Consume the token. */
11972 return cp_lexer_consume_token (parser->lexer)->u.value;
11974 default:
11975 return NULL_TREE;
11979 /* Parse an (optional) function-specifier.
11981 function-specifier:
11982 inline
11983 virtual
11984 explicit
11986 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11987 Updates DECL_SPECS, if it is non-NULL. */
11989 static tree
11990 cp_parser_function_specifier_opt (cp_parser* parser,
11991 cp_decl_specifier_seq *decl_specs)
11993 cp_token *token = cp_lexer_peek_token (parser->lexer);
11994 switch (token->keyword)
11996 case RID_INLINE:
11997 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11998 break;
12000 case RID_VIRTUAL:
12001 /* 14.5.2.3 [temp.mem]
12003 A member function template shall not be virtual. */
12004 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12005 error_at (token->location, "templates may not be %<virtual%>");
12006 else
12007 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12008 break;
12010 case RID_EXPLICIT:
12011 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12012 break;
12014 default:
12015 return NULL_TREE;
12018 /* Consume the token. */
12019 return cp_lexer_consume_token (parser->lexer)->u.value;
12022 /* Parse a linkage-specification.
12024 linkage-specification:
12025 extern string-literal { declaration-seq [opt] }
12026 extern string-literal declaration */
12028 static void
12029 cp_parser_linkage_specification (cp_parser* parser)
12031 tree linkage;
12033 /* Look for the `extern' keyword. */
12034 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12036 /* Look for the string-literal. */
12037 linkage = cp_parser_string_literal (parser, false, false);
12039 /* Transform the literal into an identifier. If the literal is a
12040 wide-character string, or contains embedded NULs, then we can't
12041 handle it as the user wants. */
12042 if (strlen (TREE_STRING_POINTER (linkage))
12043 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12045 cp_parser_error (parser, "invalid linkage-specification");
12046 /* Assume C++ linkage. */
12047 linkage = lang_name_cplusplus;
12049 else
12050 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12052 /* We're now using the new linkage. */
12053 push_lang_context (linkage);
12055 /* If the next token is a `{', then we're using the first
12056 production. */
12057 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12059 cp_ensure_no_omp_declare_simd (parser);
12061 /* Consume the `{' token. */
12062 cp_lexer_consume_token (parser->lexer);
12063 /* Parse the declarations. */
12064 cp_parser_declaration_seq_opt (parser);
12065 /* Look for the closing `}'. */
12066 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12068 /* Otherwise, there's just one declaration. */
12069 else
12071 bool saved_in_unbraced_linkage_specification_p;
12073 saved_in_unbraced_linkage_specification_p
12074 = parser->in_unbraced_linkage_specification_p;
12075 parser->in_unbraced_linkage_specification_p = true;
12076 cp_parser_declaration (parser);
12077 parser->in_unbraced_linkage_specification_p
12078 = saved_in_unbraced_linkage_specification_p;
12081 /* We're done with the linkage-specification. */
12082 pop_lang_context ();
12085 /* Parse a static_assert-declaration.
12087 static_assert-declaration:
12088 static_assert ( constant-expression , string-literal ) ;
12090 If MEMBER_P, this static_assert is a class member. */
12092 static void
12093 cp_parser_static_assert(cp_parser *parser, bool member_p)
12095 tree condition;
12096 tree message;
12097 cp_token *token;
12098 location_t saved_loc;
12099 bool dummy;
12101 /* Peek at the `static_assert' token so we can keep track of exactly
12102 where the static assertion started. */
12103 token = cp_lexer_peek_token (parser->lexer);
12104 saved_loc = token->location;
12106 /* Look for the `static_assert' keyword. */
12107 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12108 RT_STATIC_ASSERT))
12109 return;
12111 /* We know we are in a static assertion; commit to any tentative
12112 parse. */
12113 if (cp_parser_parsing_tentatively (parser))
12114 cp_parser_commit_to_tentative_parse (parser);
12116 /* Parse the `(' starting the static assertion condition. */
12117 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12119 /* Parse the constant-expression. Allow a non-constant expression
12120 here in order to give better diagnostics in finish_static_assert. */
12121 condition =
12122 cp_parser_constant_expression (parser,
12123 /*allow_non_constant_p=*/true,
12124 /*non_constant_p=*/&dummy);
12126 /* Parse the separating `,'. */
12127 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12129 /* Parse the string-literal message. */
12130 message = cp_parser_string_literal (parser,
12131 /*translate=*/false,
12132 /*wide_ok=*/true);
12134 /* A `)' completes the static assertion. */
12135 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12136 cp_parser_skip_to_closing_parenthesis (parser,
12137 /*recovering=*/true,
12138 /*or_comma=*/false,
12139 /*consume_paren=*/true);
12141 /* A semicolon terminates the declaration. */
12142 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12144 /* Complete the static assertion, which may mean either processing
12145 the static assert now or saving it for template instantiation. */
12146 finish_static_assert (condition, message, saved_loc, member_p);
12149 /* Parse the expression in decltype ( expression ). */
12151 static tree
12152 cp_parser_decltype_expr (cp_parser *parser,
12153 bool &id_expression_or_member_access_p)
12155 cp_token *id_expr_start_token;
12156 tree expr;
12158 /* First, try parsing an id-expression. */
12159 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12160 cp_parser_parse_tentatively (parser);
12161 expr = cp_parser_id_expression (parser,
12162 /*template_keyword_p=*/false,
12163 /*check_dependency_p=*/true,
12164 /*template_p=*/NULL,
12165 /*declarator_p=*/false,
12166 /*optional_p=*/false);
12168 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12170 bool non_integral_constant_expression_p = false;
12171 tree id_expression = expr;
12172 cp_id_kind idk;
12173 const char *error_msg;
12175 if (identifier_p (expr))
12176 /* Lookup the name we got back from the id-expression. */
12177 expr = cp_parser_lookup_name_simple (parser, expr,
12178 id_expr_start_token->location);
12180 if (expr
12181 && expr != error_mark_node
12182 && TREE_CODE (expr) != TYPE_DECL
12183 && (TREE_CODE (expr) != BIT_NOT_EXPR
12184 || !TYPE_P (TREE_OPERAND (expr, 0)))
12185 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12187 /* Complete lookup of the id-expression. */
12188 expr = (finish_id_expression
12189 (id_expression, expr, parser->scope, &idk,
12190 /*integral_constant_expression_p=*/false,
12191 /*allow_non_integral_constant_expression_p=*/true,
12192 &non_integral_constant_expression_p,
12193 /*template_p=*/false,
12194 /*done=*/true,
12195 /*address_p=*/false,
12196 /*template_arg_p=*/false,
12197 &error_msg,
12198 id_expr_start_token->location));
12200 if (expr == error_mark_node)
12201 /* We found an id-expression, but it was something that we
12202 should not have found. This is an error, not something
12203 we can recover from, so note that we found an
12204 id-expression and we'll recover as gracefully as
12205 possible. */
12206 id_expression_or_member_access_p = true;
12209 if (expr
12210 && expr != error_mark_node
12211 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12212 /* We have an id-expression. */
12213 id_expression_or_member_access_p = true;
12216 if (!id_expression_or_member_access_p)
12218 /* Abort the id-expression parse. */
12219 cp_parser_abort_tentative_parse (parser);
12221 /* Parsing tentatively, again. */
12222 cp_parser_parse_tentatively (parser);
12224 /* Parse a class member access. */
12225 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12226 /*cast_p=*/false, /*decltype*/true,
12227 /*member_access_only_p=*/true, NULL);
12229 if (expr
12230 && expr != error_mark_node
12231 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12232 /* We have an id-expression. */
12233 id_expression_or_member_access_p = true;
12236 if (id_expression_or_member_access_p)
12237 /* We have parsed the complete id-expression or member access. */
12238 cp_parser_parse_definitely (parser);
12239 else
12241 /* Abort our attempt to parse an id-expression or member access
12242 expression. */
12243 cp_parser_abort_tentative_parse (parser);
12245 /* Parse a full expression. */
12246 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12247 /*decltype_p=*/true);
12250 return expr;
12253 /* Parse a `decltype' type. Returns the type.
12255 simple-type-specifier:
12256 decltype ( expression )
12257 C++14 proposal:
12258 decltype ( auto ) */
12260 static tree
12261 cp_parser_decltype (cp_parser *parser)
12263 tree expr;
12264 bool id_expression_or_member_access_p = false;
12265 const char *saved_message;
12266 bool saved_integral_constant_expression_p;
12267 bool saved_non_integral_constant_expression_p;
12268 bool saved_greater_than_is_operator_p;
12269 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12271 if (start_token->type == CPP_DECLTYPE)
12273 /* Already parsed. */
12274 cp_lexer_consume_token (parser->lexer);
12275 return start_token->u.value;
12278 /* Look for the `decltype' token. */
12279 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12280 return error_mark_node;
12282 /* Parse the opening `('. */
12283 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12284 return error_mark_node;
12286 /* decltype (auto) */
12287 if (cxx_dialect >= cxx14
12288 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12290 cp_lexer_consume_token (parser->lexer);
12291 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12292 return error_mark_node;
12293 expr = make_decltype_auto ();
12294 AUTO_IS_DECLTYPE (expr) = true;
12295 goto rewrite;
12298 /* Types cannot be defined in a `decltype' expression. Save away the
12299 old message. */
12300 saved_message = parser->type_definition_forbidden_message;
12302 /* And create the new one. */
12303 parser->type_definition_forbidden_message
12304 = G_("types may not be defined in %<decltype%> expressions");
12306 /* The restrictions on constant-expressions do not apply inside
12307 decltype expressions. */
12308 saved_integral_constant_expression_p
12309 = parser->integral_constant_expression_p;
12310 saved_non_integral_constant_expression_p
12311 = parser->non_integral_constant_expression_p;
12312 parser->integral_constant_expression_p = false;
12314 /* Within a parenthesized expression, a `>' token is always
12315 the greater-than operator. */
12316 saved_greater_than_is_operator_p
12317 = parser->greater_than_is_operator_p;
12318 parser->greater_than_is_operator_p = true;
12320 /* Do not actually evaluate the expression. */
12321 ++cp_unevaluated_operand;
12323 /* Do not warn about problems with the expression. */
12324 ++c_inhibit_evaluation_warnings;
12326 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12328 /* Go back to evaluating expressions. */
12329 --cp_unevaluated_operand;
12330 --c_inhibit_evaluation_warnings;
12332 /* The `>' token might be the end of a template-id or
12333 template-parameter-list now. */
12334 parser->greater_than_is_operator_p
12335 = saved_greater_than_is_operator_p;
12337 /* Restore the old message and the integral constant expression
12338 flags. */
12339 parser->type_definition_forbidden_message = saved_message;
12340 parser->integral_constant_expression_p
12341 = saved_integral_constant_expression_p;
12342 parser->non_integral_constant_expression_p
12343 = saved_non_integral_constant_expression_p;
12345 /* Parse to the closing `)'. */
12346 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12348 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12349 /*consume_paren=*/true);
12350 return error_mark_node;
12353 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12354 tf_warning_or_error);
12356 rewrite:
12357 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12358 it again. */
12359 start_token->type = CPP_DECLTYPE;
12360 start_token->u.value = expr;
12361 start_token->keyword = RID_MAX;
12362 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12364 return expr;
12367 /* Special member functions [gram.special] */
12369 /* Parse a conversion-function-id.
12371 conversion-function-id:
12372 operator conversion-type-id
12374 Returns an IDENTIFIER_NODE representing the operator. */
12376 static tree
12377 cp_parser_conversion_function_id (cp_parser* parser)
12379 tree type;
12380 tree saved_scope;
12381 tree saved_qualifying_scope;
12382 tree saved_object_scope;
12383 tree pushed_scope = NULL_TREE;
12385 /* Look for the `operator' token. */
12386 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12387 return error_mark_node;
12388 /* When we parse the conversion-type-id, the current scope will be
12389 reset. However, we need that information in able to look up the
12390 conversion function later, so we save it here. */
12391 saved_scope = parser->scope;
12392 saved_qualifying_scope = parser->qualifying_scope;
12393 saved_object_scope = parser->object_scope;
12394 /* We must enter the scope of the class so that the names of
12395 entities declared within the class are available in the
12396 conversion-type-id. For example, consider:
12398 struct S {
12399 typedef int I;
12400 operator I();
12403 S::operator I() { ... }
12405 In order to see that `I' is a type-name in the definition, we
12406 must be in the scope of `S'. */
12407 if (saved_scope)
12408 pushed_scope = push_scope (saved_scope);
12409 /* Parse the conversion-type-id. */
12410 type = cp_parser_conversion_type_id (parser);
12411 /* Leave the scope of the class, if any. */
12412 if (pushed_scope)
12413 pop_scope (pushed_scope);
12414 /* Restore the saved scope. */
12415 parser->scope = saved_scope;
12416 parser->qualifying_scope = saved_qualifying_scope;
12417 parser->object_scope = saved_object_scope;
12418 /* If the TYPE is invalid, indicate failure. */
12419 if (type == error_mark_node)
12420 return error_mark_node;
12421 return mangle_conv_op_name_for_type (type);
12424 /* Parse a conversion-type-id:
12426 conversion-type-id:
12427 type-specifier-seq conversion-declarator [opt]
12429 Returns the TYPE specified. */
12431 static tree
12432 cp_parser_conversion_type_id (cp_parser* parser)
12434 tree attributes;
12435 cp_decl_specifier_seq type_specifiers;
12436 cp_declarator *declarator;
12437 tree type_specified;
12438 const char *saved_message;
12440 /* Parse the attributes. */
12441 attributes = cp_parser_attributes_opt (parser);
12443 saved_message = parser->type_definition_forbidden_message;
12444 parser->type_definition_forbidden_message
12445 = G_("types may not be defined in a conversion-type-id");
12447 /* Parse the type-specifiers. */
12448 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12449 /*is_trailing_return=*/false,
12450 &type_specifiers);
12452 parser->type_definition_forbidden_message = saved_message;
12454 /* If that didn't work, stop. */
12455 if (type_specifiers.type == error_mark_node)
12456 return error_mark_node;
12457 /* Parse the conversion-declarator. */
12458 declarator = cp_parser_conversion_declarator_opt (parser);
12460 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12461 /*initialized=*/0, &attributes);
12462 if (attributes)
12463 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12465 /* Don't give this error when parsing tentatively. This happens to
12466 work because we always parse this definitively once. */
12467 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12468 && type_uses_auto (type_specified))
12470 if (cxx_dialect < cxx14)
12472 error ("invalid use of %<auto%> in conversion operator");
12473 return error_mark_node;
12475 else if (template_parm_scope_p ())
12476 warning (0, "use of %<auto%> in member template "
12477 "conversion operator can never be deduced");
12480 return type_specified;
12483 /* Parse an (optional) conversion-declarator.
12485 conversion-declarator:
12486 ptr-operator conversion-declarator [opt]
12490 static cp_declarator *
12491 cp_parser_conversion_declarator_opt (cp_parser* parser)
12493 enum tree_code code;
12494 tree class_type, std_attributes = NULL_TREE;
12495 cp_cv_quals cv_quals;
12497 /* We don't know if there's a ptr-operator next, or not. */
12498 cp_parser_parse_tentatively (parser);
12499 /* Try the ptr-operator. */
12500 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12501 &std_attributes);
12502 /* If it worked, look for more conversion-declarators. */
12503 if (cp_parser_parse_definitely (parser))
12505 cp_declarator *declarator;
12507 /* Parse another optional declarator. */
12508 declarator = cp_parser_conversion_declarator_opt (parser);
12510 declarator = cp_parser_make_indirect_declarator
12511 (code, class_type, cv_quals, declarator, std_attributes);
12513 return declarator;
12516 return NULL;
12519 /* Parse an (optional) ctor-initializer.
12521 ctor-initializer:
12522 : mem-initializer-list
12524 Returns TRUE iff the ctor-initializer was actually present. */
12526 static bool
12527 cp_parser_ctor_initializer_opt (cp_parser* parser)
12529 /* If the next token is not a `:', then there is no
12530 ctor-initializer. */
12531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12533 /* Do default initialization of any bases and members. */
12534 if (DECL_CONSTRUCTOR_P (current_function_decl))
12535 finish_mem_initializers (NULL_TREE);
12537 return false;
12540 /* Consume the `:' token. */
12541 cp_lexer_consume_token (parser->lexer);
12542 /* And the mem-initializer-list. */
12543 cp_parser_mem_initializer_list (parser);
12545 return true;
12548 /* Parse a mem-initializer-list.
12550 mem-initializer-list:
12551 mem-initializer ... [opt]
12552 mem-initializer ... [opt] , mem-initializer-list */
12554 static void
12555 cp_parser_mem_initializer_list (cp_parser* parser)
12557 tree mem_initializer_list = NULL_TREE;
12558 tree target_ctor = error_mark_node;
12559 cp_token *token = cp_lexer_peek_token (parser->lexer);
12561 /* Let the semantic analysis code know that we are starting the
12562 mem-initializer-list. */
12563 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12564 error_at (token->location,
12565 "only constructors take member initializers");
12567 /* Loop through the list. */
12568 while (true)
12570 tree mem_initializer;
12572 token = cp_lexer_peek_token (parser->lexer);
12573 /* Parse the mem-initializer. */
12574 mem_initializer = cp_parser_mem_initializer (parser);
12575 /* If the next token is a `...', we're expanding member initializers. */
12576 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12578 /* Consume the `...'. */
12579 cp_lexer_consume_token (parser->lexer);
12581 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12582 can be expanded but members cannot. */
12583 if (mem_initializer != error_mark_node
12584 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12586 error_at (token->location,
12587 "cannot expand initializer for member %<%D%>",
12588 TREE_PURPOSE (mem_initializer));
12589 mem_initializer = error_mark_node;
12592 /* Construct the pack expansion type. */
12593 if (mem_initializer != error_mark_node)
12594 mem_initializer = make_pack_expansion (mem_initializer);
12596 if (target_ctor != error_mark_node
12597 && mem_initializer != error_mark_node)
12599 error ("mem-initializer for %qD follows constructor delegation",
12600 TREE_PURPOSE (mem_initializer));
12601 mem_initializer = error_mark_node;
12603 /* Look for a target constructor. */
12604 if (mem_initializer != error_mark_node
12605 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12606 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12608 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12609 if (mem_initializer_list)
12611 error ("constructor delegation follows mem-initializer for %qD",
12612 TREE_PURPOSE (mem_initializer_list));
12613 mem_initializer = error_mark_node;
12615 target_ctor = mem_initializer;
12617 /* Add it to the list, unless it was erroneous. */
12618 if (mem_initializer != error_mark_node)
12620 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12621 mem_initializer_list = mem_initializer;
12623 /* If the next token is not a `,', we're done. */
12624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12625 break;
12626 /* Consume the `,' token. */
12627 cp_lexer_consume_token (parser->lexer);
12630 /* Perform semantic analysis. */
12631 if (DECL_CONSTRUCTOR_P (current_function_decl))
12632 finish_mem_initializers (mem_initializer_list);
12635 /* Parse a mem-initializer.
12637 mem-initializer:
12638 mem-initializer-id ( expression-list [opt] )
12639 mem-initializer-id braced-init-list
12641 GNU extension:
12643 mem-initializer:
12644 ( expression-list [opt] )
12646 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12647 class) or FIELD_DECL (for a non-static data member) to initialize;
12648 the TREE_VALUE is the expression-list. An empty initialization
12649 list is represented by void_list_node. */
12651 static tree
12652 cp_parser_mem_initializer (cp_parser* parser)
12654 tree mem_initializer_id;
12655 tree expression_list;
12656 tree member;
12657 cp_token *token = cp_lexer_peek_token (parser->lexer);
12659 /* Find out what is being initialized. */
12660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12662 permerror (token->location,
12663 "anachronistic old-style base class initializer");
12664 mem_initializer_id = NULL_TREE;
12666 else
12668 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12669 if (mem_initializer_id == error_mark_node)
12670 return mem_initializer_id;
12672 member = expand_member_init (mem_initializer_id);
12673 if (member && !DECL_P (member))
12674 in_base_initializer = 1;
12676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12678 bool expr_non_constant_p;
12679 cp_lexer_set_source_position (parser->lexer);
12680 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12681 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12682 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12683 expression_list = build_tree_list (NULL_TREE, expression_list);
12685 else
12687 vec<tree, va_gc> *vec;
12688 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12689 /*cast_p=*/false,
12690 /*allow_expansion_p=*/true,
12691 /*non_constant_p=*/NULL);
12692 if (vec == NULL)
12693 return error_mark_node;
12694 expression_list = build_tree_list_vec (vec);
12695 release_tree_vector (vec);
12698 if (expression_list == error_mark_node)
12699 return error_mark_node;
12700 if (!expression_list)
12701 expression_list = void_type_node;
12703 in_base_initializer = 0;
12705 return member ? build_tree_list (member, expression_list) : error_mark_node;
12708 /* Parse a mem-initializer-id.
12710 mem-initializer-id:
12711 :: [opt] nested-name-specifier [opt] class-name
12712 identifier
12714 Returns a TYPE indicating the class to be initializer for the first
12715 production. Returns an IDENTIFIER_NODE indicating the data member
12716 to be initialized for the second production. */
12718 static tree
12719 cp_parser_mem_initializer_id (cp_parser* parser)
12721 bool global_scope_p;
12722 bool nested_name_specifier_p;
12723 bool template_p = false;
12724 tree id;
12726 cp_token *token = cp_lexer_peek_token (parser->lexer);
12728 /* `typename' is not allowed in this context ([temp.res]). */
12729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12731 error_at (token->location,
12732 "keyword %<typename%> not allowed in this context (a qualified "
12733 "member initializer is implicitly a type)");
12734 cp_lexer_consume_token (parser->lexer);
12736 /* Look for the optional `::' operator. */
12737 global_scope_p
12738 = (cp_parser_global_scope_opt (parser,
12739 /*current_scope_valid_p=*/false)
12740 != NULL_TREE);
12741 /* Look for the optional nested-name-specifier. The simplest way to
12742 implement:
12744 [temp.res]
12746 The keyword `typename' is not permitted in a base-specifier or
12747 mem-initializer; in these contexts a qualified name that
12748 depends on a template-parameter is implicitly assumed to be a
12749 type name.
12751 is to assume that we have seen the `typename' keyword at this
12752 point. */
12753 nested_name_specifier_p
12754 = (cp_parser_nested_name_specifier_opt (parser,
12755 /*typename_keyword_p=*/true,
12756 /*check_dependency_p=*/true,
12757 /*type_p=*/true,
12758 /*is_declaration=*/true)
12759 != NULL_TREE);
12760 if (nested_name_specifier_p)
12761 template_p = cp_parser_optional_template_keyword (parser);
12762 /* If there is a `::' operator or a nested-name-specifier, then we
12763 are definitely looking for a class-name. */
12764 if (global_scope_p || nested_name_specifier_p)
12765 return cp_parser_class_name (parser,
12766 /*typename_keyword_p=*/true,
12767 /*template_keyword_p=*/template_p,
12768 typename_type,
12769 /*check_dependency_p=*/true,
12770 /*class_head_p=*/false,
12771 /*is_declaration=*/true);
12772 /* Otherwise, we could also be looking for an ordinary identifier. */
12773 cp_parser_parse_tentatively (parser);
12774 /* Try a class-name. */
12775 id = cp_parser_class_name (parser,
12776 /*typename_keyword_p=*/true,
12777 /*template_keyword_p=*/false,
12778 none_type,
12779 /*check_dependency_p=*/true,
12780 /*class_head_p=*/false,
12781 /*is_declaration=*/true);
12782 /* If we found one, we're done. */
12783 if (cp_parser_parse_definitely (parser))
12784 return id;
12785 /* Otherwise, look for an ordinary identifier. */
12786 return cp_parser_identifier (parser);
12789 /* Overloading [gram.over] */
12791 /* Parse an operator-function-id.
12793 operator-function-id:
12794 operator operator
12796 Returns an IDENTIFIER_NODE for the operator which is a
12797 human-readable spelling of the identifier, e.g., `operator +'. */
12799 static tree
12800 cp_parser_operator_function_id (cp_parser* parser)
12802 /* Look for the `operator' keyword. */
12803 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12804 return error_mark_node;
12805 /* And then the name of the operator itself. */
12806 return cp_parser_operator (parser);
12809 /* Return an identifier node for a user-defined literal operator.
12810 The suffix identifier is chained to the operator name identifier. */
12812 static tree
12813 cp_literal_operator_id (const char* name)
12815 tree identifier;
12816 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12817 + strlen (name) + 10);
12818 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12819 identifier = get_identifier (buffer);
12821 return identifier;
12824 /* Parse an operator.
12826 operator:
12827 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12828 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12829 || ++ -- , ->* -> () []
12831 GNU Extensions:
12833 operator:
12834 <? >? <?= >?=
12836 Returns an IDENTIFIER_NODE for the operator which is a
12837 human-readable spelling of the identifier, e.g., `operator +'. */
12839 static tree
12840 cp_parser_operator (cp_parser* parser)
12842 tree id = NULL_TREE;
12843 cp_token *token;
12844 bool utf8 = false;
12846 /* Peek at the next token. */
12847 token = cp_lexer_peek_token (parser->lexer);
12848 /* Figure out which operator we have. */
12849 switch (token->type)
12851 case CPP_KEYWORD:
12853 enum tree_code op;
12855 /* The keyword should be either `new' or `delete'. */
12856 if (token->keyword == RID_NEW)
12857 op = NEW_EXPR;
12858 else if (token->keyword == RID_DELETE)
12859 op = DELETE_EXPR;
12860 else
12861 break;
12863 /* Consume the `new' or `delete' token. */
12864 cp_lexer_consume_token (parser->lexer);
12866 /* Peek at the next token. */
12867 token = cp_lexer_peek_token (parser->lexer);
12868 /* If it's a `[' token then this is the array variant of the
12869 operator. */
12870 if (token->type == CPP_OPEN_SQUARE)
12872 /* Consume the `[' token. */
12873 cp_lexer_consume_token (parser->lexer);
12874 /* Look for the `]' token. */
12875 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12876 id = ansi_opname (op == NEW_EXPR
12877 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12879 /* Otherwise, we have the non-array variant. */
12880 else
12881 id = ansi_opname (op);
12883 return id;
12886 case CPP_PLUS:
12887 id = ansi_opname (PLUS_EXPR);
12888 break;
12890 case CPP_MINUS:
12891 id = ansi_opname (MINUS_EXPR);
12892 break;
12894 case CPP_MULT:
12895 id = ansi_opname (MULT_EXPR);
12896 break;
12898 case CPP_DIV:
12899 id = ansi_opname (TRUNC_DIV_EXPR);
12900 break;
12902 case CPP_MOD:
12903 id = ansi_opname (TRUNC_MOD_EXPR);
12904 break;
12906 case CPP_XOR:
12907 id = ansi_opname (BIT_XOR_EXPR);
12908 break;
12910 case CPP_AND:
12911 id = ansi_opname (BIT_AND_EXPR);
12912 break;
12914 case CPP_OR:
12915 id = ansi_opname (BIT_IOR_EXPR);
12916 break;
12918 case CPP_COMPL:
12919 id = ansi_opname (BIT_NOT_EXPR);
12920 break;
12922 case CPP_NOT:
12923 id = ansi_opname (TRUTH_NOT_EXPR);
12924 break;
12926 case CPP_EQ:
12927 id = ansi_assopname (NOP_EXPR);
12928 break;
12930 case CPP_LESS:
12931 id = ansi_opname (LT_EXPR);
12932 break;
12934 case CPP_GREATER:
12935 id = ansi_opname (GT_EXPR);
12936 break;
12938 case CPP_PLUS_EQ:
12939 id = ansi_assopname (PLUS_EXPR);
12940 break;
12942 case CPP_MINUS_EQ:
12943 id = ansi_assopname (MINUS_EXPR);
12944 break;
12946 case CPP_MULT_EQ:
12947 id = ansi_assopname (MULT_EXPR);
12948 break;
12950 case CPP_DIV_EQ:
12951 id = ansi_assopname (TRUNC_DIV_EXPR);
12952 break;
12954 case CPP_MOD_EQ:
12955 id = ansi_assopname (TRUNC_MOD_EXPR);
12956 break;
12958 case CPP_XOR_EQ:
12959 id = ansi_assopname (BIT_XOR_EXPR);
12960 break;
12962 case CPP_AND_EQ:
12963 id = ansi_assopname (BIT_AND_EXPR);
12964 break;
12966 case CPP_OR_EQ:
12967 id = ansi_assopname (BIT_IOR_EXPR);
12968 break;
12970 case CPP_LSHIFT:
12971 id = ansi_opname (LSHIFT_EXPR);
12972 break;
12974 case CPP_RSHIFT:
12975 id = ansi_opname (RSHIFT_EXPR);
12976 break;
12978 case CPP_LSHIFT_EQ:
12979 id = ansi_assopname (LSHIFT_EXPR);
12980 break;
12982 case CPP_RSHIFT_EQ:
12983 id = ansi_assopname (RSHIFT_EXPR);
12984 break;
12986 case CPP_EQ_EQ:
12987 id = ansi_opname (EQ_EXPR);
12988 break;
12990 case CPP_NOT_EQ:
12991 id = ansi_opname (NE_EXPR);
12992 break;
12994 case CPP_LESS_EQ:
12995 id = ansi_opname (LE_EXPR);
12996 break;
12998 case CPP_GREATER_EQ:
12999 id = ansi_opname (GE_EXPR);
13000 break;
13002 case CPP_AND_AND:
13003 id = ansi_opname (TRUTH_ANDIF_EXPR);
13004 break;
13006 case CPP_OR_OR:
13007 id = ansi_opname (TRUTH_ORIF_EXPR);
13008 break;
13010 case CPP_PLUS_PLUS:
13011 id = ansi_opname (POSTINCREMENT_EXPR);
13012 break;
13014 case CPP_MINUS_MINUS:
13015 id = ansi_opname (PREDECREMENT_EXPR);
13016 break;
13018 case CPP_COMMA:
13019 id = ansi_opname (COMPOUND_EXPR);
13020 break;
13022 case CPP_DEREF_STAR:
13023 id = ansi_opname (MEMBER_REF);
13024 break;
13026 case CPP_DEREF:
13027 id = ansi_opname (COMPONENT_REF);
13028 break;
13030 case CPP_OPEN_PAREN:
13031 /* Consume the `('. */
13032 cp_lexer_consume_token (parser->lexer);
13033 /* Look for the matching `)'. */
13034 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13035 return ansi_opname (CALL_EXPR);
13037 case CPP_OPEN_SQUARE:
13038 /* Consume the `['. */
13039 cp_lexer_consume_token (parser->lexer);
13040 /* Look for the matching `]'. */
13041 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13042 return ansi_opname (ARRAY_REF);
13044 case CPP_UTF8STRING:
13045 case CPP_UTF8STRING_USERDEF:
13046 utf8 = true;
13047 case CPP_STRING:
13048 case CPP_WSTRING:
13049 case CPP_STRING16:
13050 case CPP_STRING32:
13051 case CPP_STRING_USERDEF:
13052 case CPP_WSTRING_USERDEF:
13053 case CPP_STRING16_USERDEF:
13054 case CPP_STRING32_USERDEF:
13056 tree str, string_tree;
13057 int sz, len;
13059 if (cxx_dialect == cxx98)
13060 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13062 /* Consume the string. */
13063 str = cp_parser_string_literal (parser, /*translate=*/true,
13064 /*wide_ok=*/true, /*lookup_udlit=*/false);
13065 if (str == error_mark_node)
13066 return error_mark_node;
13067 else if (TREE_CODE (str) == USERDEF_LITERAL)
13069 string_tree = USERDEF_LITERAL_VALUE (str);
13070 id = USERDEF_LITERAL_SUFFIX_ID (str);
13072 else
13074 string_tree = str;
13075 /* Look for the suffix identifier. */
13076 token = cp_lexer_peek_token (parser->lexer);
13077 if (token->type == CPP_NAME)
13078 id = cp_parser_identifier (parser);
13079 else if (token->type == CPP_KEYWORD)
13081 error ("unexpected keyword;"
13082 " remove space between quotes and suffix identifier");
13083 return error_mark_node;
13085 else
13087 error ("expected suffix identifier");
13088 return error_mark_node;
13091 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13092 (TREE_TYPE (TREE_TYPE (string_tree))));
13093 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13094 if (len != 0)
13096 error ("expected empty string after %<operator%> keyword");
13097 return error_mark_node;
13099 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13100 != char_type_node)
13102 error ("invalid encoding prefix in literal operator");
13103 return error_mark_node;
13105 if (id != error_mark_node)
13107 const char *name = IDENTIFIER_POINTER (id);
13108 id = cp_literal_operator_id (name);
13110 return id;
13113 default:
13114 /* Anything else is an error. */
13115 break;
13118 /* If we have selected an identifier, we need to consume the
13119 operator token. */
13120 if (id)
13121 cp_lexer_consume_token (parser->lexer);
13122 /* Otherwise, no valid operator name was present. */
13123 else
13125 cp_parser_error (parser, "expected operator");
13126 id = error_mark_node;
13129 return id;
13132 /* Parse a template-declaration.
13134 template-declaration:
13135 export [opt] template < template-parameter-list > declaration
13137 If MEMBER_P is TRUE, this template-declaration occurs within a
13138 class-specifier.
13140 The grammar rule given by the standard isn't correct. What
13141 is really meant is:
13143 template-declaration:
13144 export [opt] template-parameter-list-seq
13145 decl-specifier-seq [opt] init-declarator [opt] ;
13146 export [opt] template-parameter-list-seq
13147 function-definition
13149 template-parameter-list-seq:
13150 template-parameter-list-seq [opt]
13151 template < template-parameter-list > */
13153 static void
13154 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13156 /* Check for `export'. */
13157 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13159 /* Consume the `export' token. */
13160 cp_lexer_consume_token (parser->lexer);
13161 /* Warn that we do not support `export'. */
13162 warning (0, "keyword %<export%> not implemented, and will be ignored");
13165 cp_parser_template_declaration_after_export (parser, member_p);
13168 /* Parse a template-parameter-list.
13170 template-parameter-list:
13171 template-parameter
13172 template-parameter-list , template-parameter
13174 Returns a TREE_LIST. Each node represents a template parameter.
13175 The nodes are connected via their TREE_CHAINs. */
13177 static tree
13178 cp_parser_template_parameter_list (cp_parser* parser)
13180 tree parameter_list = NULL_TREE;
13182 begin_template_parm_list ();
13184 /* The loop below parses the template parms. We first need to know
13185 the total number of template parms to be able to compute proper
13186 canonical types of each dependent type. So after the loop, when
13187 we know the total number of template parms,
13188 end_template_parm_list computes the proper canonical types and
13189 fixes up the dependent types accordingly. */
13190 while (true)
13192 tree parameter;
13193 bool is_non_type;
13194 bool is_parameter_pack;
13195 location_t parm_loc;
13197 /* Parse the template-parameter. */
13198 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13199 parameter = cp_parser_template_parameter (parser,
13200 &is_non_type,
13201 &is_parameter_pack);
13202 /* Add it to the list. */
13203 if (parameter != error_mark_node)
13204 parameter_list = process_template_parm (parameter_list,
13205 parm_loc,
13206 parameter,
13207 is_non_type,
13208 is_parameter_pack);
13209 else
13211 tree err_parm = build_tree_list (parameter, parameter);
13212 parameter_list = chainon (parameter_list, err_parm);
13215 /* If the next token is not a `,', we're done. */
13216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13217 break;
13218 /* Otherwise, consume the `,' token. */
13219 cp_lexer_consume_token (parser->lexer);
13222 return end_template_parm_list (parameter_list);
13225 /* Parse a template-parameter.
13227 template-parameter:
13228 type-parameter
13229 parameter-declaration
13231 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13232 the parameter. The TREE_PURPOSE is the default value, if any.
13233 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13234 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13235 set to true iff this parameter is a parameter pack. */
13237 static tree
13238 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13239 bool *is_parameter_pack)
13241 cp_token *token;
13242 cp_parameter_declarator *parameter_declarator;
13243 cp_declarator *id_declarator;
13244 tree parm;
13246 /* Assume it is a type parameter or a template parameter. */
13247 *is_non_type = false;
13248 /* Assume it not a parameter pack. */
13249 *is_parameter_pack = false;
13250 /* Peek at the next token. */
13251 token = cp_lexer_peek_token (parser->lexer);
13252 /* If it is `class' or `template', we have a type-parameter. */
13253 if (token->keyword == RID_TEMPLATE)
13254 return cp_parser_type_parameter (parser, is_parameter_pack);
13255 /* If it is `class' or `typename' we do not know yet whether it is a
13256 type parameter or a non-type parameter. Consider:
13258 template <typename T, typename T::X X> ...
13262 template <class C, class D*> ...
13264 Here, the first parameter is a type parameter, and the second is
13265 a non-type parameter. We can tell by looking at the token after
13266 the identifier -- if it is a `,', `=', or `>' then we have a type
13267 parameter. */
13268 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13270 /* Peek at the token after `class' or `typename'. */
13271 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13272 /* If it's an ellipsis, we have a template type parameter
13273 pack. */
13274 if (token->type == CPP_ELLIPSIS)
13275 return cp_parser_type_parameter (parser, is_parameter_pack);
13276 /* If it's an identifier, skip it. */
13277 if (token->type == CPP_NAME)
13278 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13279 /* Now, see if the token looks like the end of a template
13280 parameter. */
13281 if (token->type == CPP_COMMA
13282 || token->type == CPP_EQ
13283 || token->type == CPP_GREATER)
13284 return cp_parser_type_parameter (parser, is_parameter_pack);
13287 /* Otherwise, it is a non-type parameter.
13289 [temp.param]
13291 When parsing a default template-argument for a non-type
13292 template-parameter, the first non-nested `>' is taken as the end
13293 of the template parameter-list rather than a greater-than
13294 operator. */
13295 *is_non_type = true;
13296 parameter_declarator
13297 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13298 /*parenthesized_p=*/NULL);
13300 if (!parameter_declarator)
13301 return error_mark_node;
13303 /* If the parameter declaration is marked as a parameter pack, set
13304 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13305 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13306 grokdeclarator. */
13307 if (parameter_declarator->declarator
13308 && parameter_declarator->declarator->parameter_pack_p)
13310 *is_parameter_pack = true;
13311 parameter_declarator->declarator->parameter_pack_p = false;
13314 if (parameter_declarator->default_argument)
13316 /* Can happen in some cases of erroneous input (c++/34892). */
13317 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13318 /* Consume the `...' for better error recovery. */
13319 cp_lexer_consume_token (parser->lexer);
13321 /* If the next token is an ellipsis, and we don't already have it
13322 marked as a parameter pack, then we have a parameter pack (that
13323 has no declarator). */
13324 else if (!*is_parameter_pack
13325 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13326 && (declarator_can_be_parameter_pack
13327 (parameter_declarator->declarator)))
13329 /* Consume the `...'. */
13330 cp_lexer_consume_token (parser->lexer);
13331 maybe_warn_variadic_templates ();
13333 *is_parameter_pack = true;
13335 /* We might end up with a pack expansion as the type of the non-type
13336 template parameter, in which case this is a non-type template
13337 parameter pack. */
13338 else if (parameter_declarator->decl_specifiers.type
13339 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13341 *is_parameter_pack = true;
13342 parameter_declarator->decl_specifiers.type =
13343 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13346 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13348 /* Parameter packs cannot have default arguments. However, a
13349 user may try to do so, so we'll parse them and give an
13350 appropriate diagnostic here. */
13352 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13354 /* Find the name of the parameter pack. */
13355 id_declarator = parameter_declarator->declarator;
13356 while (id_declarator && id_declarator->kind != cdk_id)
13357 id_declarator = id_declarator->declarator;
13359 if (id_declarator && id_declarator->kind == cdk_id)
13360 error_at (start_token->location,
13361 "template parameter pack %qD cannot have a default argument",
13362 id_declarator->u.id.unqualified_name);
13363 else
13364 error_at (start_token->location,
13365 "template parameter pack cannot have a default argument");
13367 /* Parse the default argument, but throw away the result. */
13368 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13371 parm = grokdeclarator (parameter_declarator->declarator,
13372 &parameter_declarator->decl_specifiers,
13373 TPARM, /*initialized=*/0,
13374 /*attrlist=*/NULL);
13375 if (parm == error_mark_node)
13376 return error_mark_node;
13378 return build_tree_list (parameter_declarator->default_argument, parm);
13381 /* Parse a type-parameter.
13383 type-parameter:
13384 class identifier [opt]
13385 class identifier [opt] = type-id
13386 typename identifier [opt]
13387 typename identifier [opt] = type-id
13388 template < template-parameter-list > class identifier [opt]
13389 template < template-parameter-list > class identifier [opt]
13390 = id-expression
13392 GNU Extension (variadic templates):
13394 type-parameter:
13395 class ... identifier [opt]
13396 typename ... identifier [opt]
13398 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13399 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13400 the declaration of the parameter.
13402 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13404 static tree
13405 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13407 cp_token *token;
13408 tree parameter;
13410 /* Look for a keyword to tell us what kind of parameter this is. */
13411 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13412 if (!token)
13413 return error_mark_node;
13415 switch (token->keyword)
13417 case RID_CLASS:
13418 case RID_TYPENAME:
13420 tree identifier;
13421 tree default_argument;
13423 /* If the next token is an ellipsis, we have a template
13424 argument pack. */
13425 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13427 /* Consume the `...' token. */
13428 cp_lexer_consume_token (parser->lexer);
13429 maybe_warn_variadic_templates ();
13431 *is_parameter_pack = true;
13434 /* If the next token is an identifier, then it names the
13435 parameter. */
13436 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13437 identifier = cp_parser_identifier (parser);
13438 else
13439 identifier = NULL_TREE;
13441 /* Create the parameter. */
13442 parameter = finish_template_type_parm (class_type_node, identifier);
13444 /* If the next token is an `=', we have a default argument. */
13445 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13447 /* Consume the `=' token. */
13448 cp_lexer_consume_token (parser->lexer);
13449 /* Parse the default-argument. */
13450 push_deferring_access_checks (dk_no_deferred);
13451 default_argument = cp_parser_type_id (parser);
13453 /* Template parameter packs cannot have default
13454 arguments. */
13455 if (*is_parameter_pack)
13457 if (identifier)
13458 error_at (token->location,
13459 "template parameter pack %qD cannot have a "
13460 "default argument", identifier);
13461 else
13462 error_at (token->location,
13463 "template parameter packs cannot have "
13464 "default arguments");
13465 default_argument = NULL_TREE;
13467 pop_deferring_access_checks ();
13469 else
13470 default_argument = NULL_TREE;
13472 /* Create the combined representation of the parameter and the
13473 default argument. */
13474 parameter = build_tree_list (default_argument, parameter);
13476 break;
13478 case RID_TEMPLATE:
13480 tree identifier;
13481 tree default_argument;
13483 /* Look for the `<'. */
13484 cp_parser_require (parser, CPP_LESS, RT_LESS);
13485 /* Parse the template-parameter-list. */
13486 cp_parser_template_parameter_list (parser);
13487 /* Look for the `>'. */
13488 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13489 /* Look for the `class' or 'typename' keywords. */
13490 cp_parser_type_parameter_key (parser);
13491 /* If the next token is an ellipsis, we have a template
13492 argument pack. */
13493 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13495 /* Consume the `...' token. */
13496 cp_lexer_consume_token (parser->lexer);
13497 maybe_warn_variadic_templates ();
13499 *is_parameter_pack = true;
13501 /* If the next token is an `=', then there is a
13502 default-argument. If the next token is a `>', we are at
13503 the end of the parameter-list. If the next token is a `,',
13504 then we are at the end of this parameter. */
13505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13506 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13507 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13509 identifier = cp_parser_identifier (parser);
13510 /* Treat invalid names as if the parameter were nameless. */
13511 if (identifier == error_mark_node)
13512 identifier = NULL_TREE;
13514 else
13515 identifier = NULL_TREE;
13517 /* Create the template parameter. */
13518 parameter = finish_template_template_parm (class_type_node,
13519 identifier);
13521 /* If the next token is an `=', then there is a
13522 default-argument. */
13523 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13525 bool is_template;
13527 /* Consume the `='. */
13528 cp_lexer_consume_token (parser->lexer);
13529 /* Parse the id-expression. */
13530 push_deferring_access_checks (dk_no_deferred);
13531 /* save token before parsing the id-expression, for error
13532 reporting */
13533 token = cp_lexer_peek_token (parser->lexer);
13534 default_argument
13535 = cp_parser_id_expression (parser,
13536 /*template_keyword_p=*/false,
13537 /*check_dependency_p=*/true,
13538 /*template_p=*/&is_template,
13539 /*declarator_p=*/false,
13540 /*optional_p=*/false);
13541 if (TREE_CODE (default_argument) == TYPE_DECL)
13542 /* If the id-expression was a template-id that refers to
13543 a template-class, we already have the declaration here,
13544 so no further lookup is needed. */
13546 else
13547 /* Look up the name. */
13548 default_argument
13549 = cp_parser_lookup_name (parser, default_argument,
13550 none_type,
13551 /*is_template=*/is_template,
13552 /*is_namespace=*/false,
13553 /*check_dependency=*/true,
13554 /*ambiguous_decls=*/NULL,
13555 token->location);
13556 /* See if the default argument is valid. */
13557 default_argument
13558 = check_template_template_default_arg (default_argument);
13560 /* Template parameter packs cannot have default
13561 arguments. */
13562 if (*is_parameter_pack)
13564 if (identifier)
13565 error_at (token->location,
13566 "template parameter pack %qD cannot "
13567 "have a default argument",
13568 identifier);
13569 else
13570 error_at (token->location, "template parameter packs cannot "
13571 "have default arguments");
13572 default_argument = NULL_TREE;
13574 pop_deferring_access_checks ();
13576 else
13577 default_argument = NULL_TREE;
13579 /* Create the combined representation of the parameter and the
13580 default argument. */
13581 parameter = build_tree_list (default_argument, parameter);
13583 break;
13585 default:
13586 gcc_unreachable ();
13587 break;
13590 return parameter;
13593 /* Parse a template-id.
13595 template-id:
13596 template-name < template-argument-list [opt] >
13598 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13599 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13600 returned. Otherwise, if the template-name names a function, or set
13601 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13602 names a class, returns a TYPE_DECL for the specialization.
13604 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13605 uninstantiated templates. */
13607 static tree
13608 cp_parser_template_id (cp_parser *parser,
13609 bool template_keyword_p,
13610 bool check_dependency_p,
13611 enum tag_types tag_type,
13612 bool is_declaration)
13614 int i;
13615 tree templ;
13616 tree arguments;
13617 tree template_id;
13618 cp_token_position start_of_id = 0;
13619 deferred_access_check *chk;
13620 vec<deferred_access_check, va_gc> *access_check;
13621 cp_token *next_token = NULL, *next_token_2 = NULL;
13622 bool is_identifier;
13624 /* If the next token corresponds to a template-id, there is no need
13625 to reparse it. */
13626 next_token = cp_lexer_peek_token (parser->lexer);
13627 if (next_token->type == CPP_TEMPLATE_ID)
13629 struct tree_check *check_value;
13631 /* Get the stored value. */
13632 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13633 /* Perform any access checks that were deferred. */
13634 access_check = check_value->checks;
13635 if (access_check)
13637 FOR_EACH_VEC_ELT (*access_check, i, chk)
13638 perform_or_defer_access_check (chk->binfo,
13639 chk->decl,
13640 chk->diag_decl,
13641 tf_warning_or_error);
13643 /* Return the stored value. */
13644 return check_value->value;
13647 /* Avoid performing name lookup if there is no possibility of
13648 finding a template-id. */
13649 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13650 || (next_token->type == CPP_NAME
13651 && !cp_parser_nth_token_starts_template_argument_list_p
13652 (parser, 2)))
13654 cp_parser_error (parser, "expected template-id");
13655 return error_mark_node;
13658 /* Remember where the template-id starts. */
13659 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13660 start_of_id = cp_lexer_token_position (parser->lexer, false);
13662 push_deferring_access_checks (dk_deferred);
13664 /* Parse the template-name. */
13665 is_identifier = false;
13666 templ = cp_parser_template_name (parser, template_keyword_p,
13667 check_dependency_p,
13668 is_declaration,
13669 tag_type,
13670 &is_identifier);
13671 if (templ == error_mark_node || is_identifier)
13673 pop_deferring_access_checks ();
13674 return templ;
13677 /* If we find the sequence `[:' after a template-name, it's probably
13678 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13679 parse correctly the argument list. */
13680 next_token = cp_lexer_peek_token (parser->lexer);
13681 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13682 if (next_token->type == CPP_OPEN_SQUARE
13683 && next_token->flags & DIGRAPH
13684 && next_token_2->type == CPP_COLON
13685 && !(next_token_2->flags & PREV_WHITE))
13687 cp_parser_parse_tentatively (parser);
13688 /* Change `:' into `::'. */
13689 next_token_2->type = CPP_SCOPE;
13690 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13691 CPP_LESS. */
13692 cp_lexer_consume_token (parser->lexer);
13694 /* Parse the arguments. */
13695 arguments = cp_parser_enclosed_template_argument_list (parser);
13696 if (!cp_parser_parse_definitely (parser))
13698 /* If we couldn't parse an argument list, then we revert our changes
13699 and return simply an error. Maybe this is not a template-id
13700 after all. */
13701 next_token_2->type = CPP_COLON;
13702 cp_parser_error (parser, "expected %<<%>");
13703 pop_deferring_access_checks ();
13704 return error_mark_node;
13706 /* Otherwise, emit an error about the invalid digraph, but continue
13707 parsing because we got our argument list. */
13708 if (permerror (next_token->location,
13709 "%<<::%> cannot begin a template-argument list"))
13711 static bool hint = false;
13712 inform (next_token->location,
13713 "%<<:%> is an alternate spelling for %<[%>."
13714 " Insert whitespace between %<<%> and %<::%>");
13715 if (!hint && !flag_permissive)
13717 inform (next_token->location, "(if you use %<-fpermissive%> "
13718 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13719 "accept your code)");
13720 hint = true;
13724 else
13726 /* Look for the `<' that starts the template-argument-list. */
13727 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13729 pop_deferring_access_checks ();
13730 return error_mark_node;
13732 /* Parse the arguments. */
13733 arguments = cp_parser_enclosed_template_argument_list (parser);
13736 /* Build a representation of the specialization. */
13737 if (identifier_p (templ))
13738 template_id = build_min_nt_loc (next_token->location,
13739 TEMPLATE_ID_EXPR,
13740 templ, arguments);
13741 else if (DECL_TYPE_TEMPLATE_P (templ)
13742 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13744 bool entering_scope;
13745 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13746 template (rather than some instantiation thereof) only if
13747 is not nested within some other construct. For example, in
13748 "template <typename T> void f(T) { A<T>::", A<T> is just an
13749 instantiation of A. */
13750 entering_scope = (template_parm_scope_p ()
13751 && cp_lexer_next_token_is (parser->lexer,
13752 CPP_SCOPE));
13753 template_id
13754 = finish_template_type (templ, arguments, entering_scope);
13756 else if (variable_template_p (templ))
13758 template_id = lookup_template_variable (templ, arguments);
13760 else
13762 /* If it's not a class-template or a template-template, it should be
13763 a function-template. */
13764 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13765 || TREE_CODE (templ) == OVERLOAD
13766 || BASELINK_P (templ)));
13768 template_id = lookup_template_function (templ, arguments);
13771 /* If parsing tentatively, replace the sequence of tokens that makes
13772 up the template-id with a CPP_TEMPLATE_ID token. That way,
13773 should we re-parse the token stream, we will not have to repeat
13774 the effort required to do the parse, nor will we issue duplicate
13775 error messages about problems during instantiation of the
13776 template. */
13777 if (start_of_id
13778 /* Don't do this if we had a parse error in a declarator; re-parsing
13779 might succeed if a name changes meaning (60361). */
13780 && !(cp_parser_error_occurred (parser)
13781 && cp_parser_parsing_tentatively (parser)
13782 && parser->in_declarator_p))
13784 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13786 /* Reset the contents of the START_OF_ID token. */
13787 token->type = CPP_TEMPLATE_ID;
13788 /* Retrieve any deferred checks. Do not pop this access checks yet
13789 so the memory will not be reclaimed during token replacing below. */
13790 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13791 token->u.tree_check_value->value = template_id;
13792 token->u.tree_check_value->checks = get_deferred_access_checks ();
13793 token->keyword = RID_MAX;
13795 /* Purge all subsequent tokens. */
13796 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13798 /* ??? Can we actually assume that, if template_id ==
13799 error_mark_node, we will have issued a diagnostic to the
13800 user, as opposed to simply marking the tentative parse as
13801 failed? */
13802 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13803 error_at (token->location, "parse error in template argument list");
13806 pop_to_parent_deferring_access_checks ();
13807 return template_id;
13810 /* Parse a template-name.
13812 template-name:
13813 identifier
13815 The standard should actually say:
13817 template-name:
13818 identifier
13819 operator-function-id
13821 A defect report has been filed about this issue.
13823 A conversion-function-id cannot be a template name because they cannot
13824 be part of a template-id. In fact, looking at this code:
13826 a.operator K<int>()
13828 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13829 It is impossible to call a templated conversion-function-id with an
13830 explicit argument list, since the only allowed template parameter is
13831 the type to which it is converting.
13833 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13834 `template' keyword, in a construction like:
13836 T::template f<3>()
13838 In that case `f' is taken to be a template-name, even though there
13839 is no way of knowing for sure.
13841 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13842 name refers to a set of overloaded functions, at least one of which
13843 is a template, or an IDENTIFIER_NODE with the name of the template,
13844 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13845 names are looked up inside uninstantiated templates. */
13847 static tree
13848 cp_parser_template_name (cp_parser* parser,
13849 bool template_keyword_p,
13850 bool check_dependency_p,
13851 bool is_declaration,
13852 enum tag_types tag_type,
13853 bool *is_identifier)
13855 tree identifier;
13856 tree decl;
13857 tree fns;
13858 cp_token *token = cp_lexer_peek_token (parser->lexer);
13860 /* If the next token is `operator', then we have either an
13861 operator-function-id or a conversion-function-id. */
13862 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13864 /* We don't know whether we're looking at an
13865 operator-function-id or a conversion-function-id. */
13866 cp_parser_parse_tentatively (parser);
13867 /* Try an operator-function-id. */
13868 identifier = cp_parser_operator_function_id (parser);
13869 /* If that didn't work, try a conversion-function-id. */
13870 if (!cp_parser_parse_definitely (parser))
13872 cp_parser_error (parser, "expected template-name");
13873 return error_mark_node;
13876 /* Look for the identifier. */
13877 else
13878 identifier = cp_parser_identifier (parser);
13880 /* If we didn't find an identifier, we don't have a template-id. */
13881 if (identifier == error_mark_node)
13882 return error_mark_node;
13884 /* If the name immediately followed the `template' keyword, then it
13885 is a template-name. However, if the next token is not `<', then
13886 we do not treat it as a template-name, since it is not being used
13887 as part of a template-id. This enables us to handle constructs
13888 like:
13890 template <typename T> struct S { S(); };
13891 template <typename T> S<T>::S();
13893 correctly. We would treat `S' as a template -- if it were `S<T>'
13894 -- but we do not if there is no `<'. */
13896 if (processing_template_decl
13897 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13899 /* In a declaration, in a dependent context, we pretend that the
13900 "template" keyword was present in order to improve error
13901 recovery. For example, given:
13903 template <typename T> void f(T::X<int>);
13905 we want to treat "X<int>" as a template-id. */
13906 if (is_declaration
13907 && !template_keyword_p
13908 && parser->scope && TYPE_P (parser->scope)
13909 && check_dependency_p
13910 && dependent_scope_p (parser->scope)
13911 /* Do not do this for dtors (or ctors), since they never
13912 need the template keyword before their name. */
13913 && !constructor_name_p (identifier, parser->scope))
13915 cp_token_position start = 0;
13917 /* Explain what went wrong. */
13918 error_at (token->location, "non-template %qD used as template",
13919 identifier);
13920 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13921 parser->scope, identifier);
13922 /* If parsing tentatively, find the location of the "<" token. */
13923 if (cp_parser_simulate_error (parser))
13924 start = cp_lexer_token_position (parser->lexer, true);
13925 /* Parse the template arguments so that we can issue error
13926 messages about them. */
13927 cp_lexer_consume_token (parser->lexer);
13928 cp_parser_enclosed_template_argument_list (parser);
13929 /* Skip tokens until we find a good place from which to
13930 continue parsing. */
13931 cp_parser_skip_to_closing_parenthesis (parser,
13932 /*recovering=*/true,
13933 /*or_comma=*/true,
13934 /*consume_paren=*/false);
13935 /* If parsing tentatively, permanently remove the
13936 template argument list. That will prevent duplicate
13937 error messages from being issued about the missing
13938 "template" keyword. */
13939 if (start)
13940 cp_lexer_purge_tokens_after (parser->lexer, start);
13941 if (is_identifier)
13942 *is_identifier = true;
13943 return identifier;
13946 /* If the "template" keyword is present, then there is generally
13947 no point in doing name-lookup, so we just return IDENTIFIER.
13948 But, if the qualifying scope is non-dependent then we can
13949 (and must) do name-lookup normally. */
13950 if (template_keyword_p
13951 && (!parser->scope
13952 || (TYPE_P (parser->scope)
13953 && dependent_type_p (parser->scope))))
13954 return identifier;
13957 /* Look up the name. */
13958 decl = cp_parser_lookup_name (parser, identifier,
13959 tag_type,
13960 /*is_template=*/true,
13961 /*is_namespace=*/false,
13962 check_dependency_p,
13963 /*ambiguous_decls=*/NULL,
13964 token->location);
13966 /* If DECL is a template, then the name was a template-name. */
13967 if (TREE_CODE (decl) == TEMPLATE_DECL)
13969 if (TREE_DEPRECATED (decl)
13970 && deprecated_state != DEPRECATED_SUPPRESS)
13971 warn_deprecated_use (decl, NULL_TREE);
13973 else
13975 tree fn = NULL_TREE;
13977 /* The standard does not explicitly indicate whether a name that
13978 names a set of overloaded declarations, some of which are
13979 templates, is a template-name. However, such a name should
13980 be a template-name; otherwise, there is no way to form a
13981 template-id for the overloaded templates. */
13982 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13983 if (TREE_CODE (fns) == OVERLOAD)
13984 for (fn = fns; fn; fn = OVL_NEXT (fn))
13985 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13986 break;
13988 if (!fn)
13990 /* The name does not name a template. */
13991 cp_parser_error (parser, "expected template-name");
13992 return error_mark_node;
13996 /* If DECL is dependent, and refers to a function, then just return
13997 its name; we will look it up again during template instantiation. */
13998 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14000 tree scope = ovl_scope (decl);
14001 if (TYPE_P (scope) && dependent_type_p (scope))
14002 return identifier;
14005 return decl;
14008 /* Parse a template-argument-list.
14010 template-argument-list:
14011 template-argument ... [opt]
14012 template-argument-list , template-argument ... [opt]
14014 Returns a TREE_VEC containing the arguments. */
14016 static tree
14017 cp_parser_template_argument_list (cp_parser* parser)
14019 tree fixed_args[10];
14020 unsigned n_args = 0;
14021 unsigned alloced = 10;
14022 tree *arg_ary = fixed_args;
14023 tree vec;
14024 bool saved_in_template_argument_list_p;
14025 bool saved_ice_p;
14026 bool saved_non_ice_p;
14028 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14029 parser->in_template_argument_list_p = true;
14030 /* Even if the template-id appears in an integral
14031 constant-expression, the contents of the argument list do
14032 not. */
14033 saved_ice_p = parser->integral_constant_expression_p;
14034 parser->integral_constant_expression_p = false;
14035 saved_non_ice_p = parser->non_integral_constant_expression_p;
14036 parser->non_integral_constant_expression_p = false;
14038 /* Parse the arguments. */
14041 tree argument;
14043 if (n_args)
14044 /* Consume the comma. */
14045 cp_lexer_consume_token (parser->lexer);
14047 /* Parse the template-argument. */
14048 argument = cp_parser_template_argument (parser);
14050 /* If the next token is an ellipsis, we're expanding a template
14051 argument pack. */
14052 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14054 if (argument == error_mark_node)
14056 cp_token *token = cp_lexer_peek_token (parser->lexer);
14057 error_at (token->location,
14058 "expected parameter pack before %<...%>");
14060 /* Consume the `...' token. */
14061 cp_lexer_consume_token (parser->lexer);
14063 /* Make the argument into a TYPE_PACK_EXPANSION or
14064 EXPR_PACK_EXPANSION. */
14065 argument = make_pack_expansion (argument);
14068 if (n_args == alloced)
14070 alloced *= 2;
14072 if (arg_ary == fixed_args)
14074 arg_ary = XNEWVEC (tree, alloced);
14075 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14077 else
14078 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14080 arg_ary[n_args++] = argument;
14082 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14084 vec = make_tree_vec (n_args);
14086 while (n_args--)
14087 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14089 if (arg_ary != fixed_args)
14090 free (arg_ary);
14091 parser->non_integral_constant_expression_p = saved_non_ice_p;
14092 parser->integral_constant_expression_p = saved_ice_p;
14093 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14094 #ifdef ENABLE_CHECKING
14095 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14096 #endif
14097 return vec;
14100 /* Parse a template-argument.
14102 template-argument:
14103 assignment-expression
14104 type-id
14105 id-expression
14107 The representation is that of an assignment-expression, type-id, or
14108 id-expression -- except that the qualified id-expression is
14109 evaluated, so that the value returned is either a DECL or an
14110 OVERLOAD.
14112 Although the standard says "assignment-expression", it forbids
14113 throw-expressions or assignments in the template argument.
14114 Therefore, we use "conditional-expression" instead. */
14116 static tree
14117 cp_parser_template_argument (cp_parser* parser)
14119 tree argument;
14120 bool template_p;
14121 bool address_p;
14122 bool maybe_type_id = false;
14123 cp_token *token = NULL, *argument_start_token = NULL;
14124 location_t loc = 0;
14125 cp_id_kind idk;
14127 /* There's really no way to know what we're looking at, so we just
14128 try each alternative in order.
14130 [temp.arg]
14132 In a template-argument, an ambiguity between a type-id and an
14133 expression is resolved to a type-id, regardless of the form of
14134 the corresponding template-parameter.
14136 Therefore, we try a type-id first. */
14137 cp_parser_parse_tentatively (parser);
14138 argument = cp_parser_template_type_arg (parser);
14139 /* If there was no error parsing the type-id but the next token is a
14140 '>>', our behavior depends on which dialect of C++ we're
14141 parsing. In C++98, we probably found a typo for '> >'. But there
14142 are type-id which are also valid expressions. For instance:
14144 struct X { int operator >> (int); };
14145 template <int V> struct Foo {};
14146 Foo<X () >> 5> r;
14148 Here 'X()' is a valid type-id of a function type, but the user just
14149 wanted to write the expression "X() >> 5". Thus, we remember that we
14150 found a valid type-id, but we still try to parse the argument as an
14151 expression to see what happens.
14153 In C++0x, the '>>' will be considered two separate '>'
14154 tokens. */
14155 if (!cp_parser_error_occurred (parser)
14156 && cxx_dialect == cxx98
14157 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14159 maybe_type_id = true;
14160 cp_parser_abort_tentative_parse (parser);
14162 else
14164 /* If the next token isn't a `,' or a `>', then this argument wasn't
14165 really finished. This means that the argument is not a valid
14166 type-id. */
14167 if (!cp_parser_next_token_ends_template_argument_p (parser))
14168 cp_parser_error (parser, "expected template-argument");
14169 /* If that worked, we're done. */
14170 if (cp_parser_parse_definitely (parser))
14171 return argument;
14173 /* We're still not sure what the argument will be. */
14174 cp_parser_parse_tentatively (parser);
14175 /* Try a template. */
14176 argument_start_token = cp_lexer_peek_token (parser->lexer);
14177 argument = cp_parser_id_expression (parser,
14178 /*template_keyword_p=*/false,
14179 /*check_dependency_p=*/true,
14180 &template_p,
14181 /*declarator_p=*/false,
14182 /*optional_p=*/false);
14183 /* If the next token isn't a `,' or a `>', then this argument wasn't
14184 really finished. */
14185 if (!cp_parser_next_token_ends_template_argument_p (parser))
14186 cp_parser_error (parser, "expected template-argument");
14187 if (!cp_parser_error_occurred (parser))
14189 /* Figure out what is being referred to. If the id-expression
14190 was for a class template specialization, then we will have a
14191 TYPE_DECL at this point. There is no need to do name lookup
14192 at this point in that case. */
14193 if (TREE_CODE (argument) != TYPE_DECL)
14194 argument = cp_parser_lookup_name (parser, argument,
14195 none_type,
14196 /*is_template=*/template_p,
14197 /*is_namespace=*/false,
14198 /*check_dependency=*/true,
14199 /*ambiguous_decls=*/NULL,
14200 argument_start_token->location);
14201 if (TREE_CODE (argument) != TEMPLATE_DECL
14202 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14203 cp_parser_error (parser, "expected template-name");
14205 if (cp_parser_parse_definitely (parser))
14207 if (TREE_DEPRECATED (argument))
14208 warn_deprecated_use (argument, NULL_TREE);
14209 return argument;
14211 /* It must be a non-type argument. There permitted cases are given
14212 in [temp.arg.nontype]:
14214 -- an integral constant-expression of integral or enumeration
14215 type; or
14217 -- the name of a non-type template-parameter; or
14219 -- the name of an object or function with external linkage...
14221 -- the address of an object or function with external linkage...
14223 -- a pointer to member... */
14224 /* Look for a non-type template parameter. */
14225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14227 cp_parser_parse_tentatively (parser);
14228 argument = cp_parser_primary_expression (parser,
14229 /*address_p=*/false,
14230 /*cast_p=*/false,
14231 /*template_arg_p=*/true,
14232 &idk);
14233 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14234 || !cp_parser_next_token_ends_template_argument_p (parser))
14235 cp_parser_simulate_error (parser);
14236 if (cp_parser_parse_definitely (parser))
14237 return argument;
14240 /* If the next token is "&", the argument must be the address of an
14241 object or function with external linkage. */
14242 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14243 if (address_p)
14245 loc = cp_lexer_peek_token (parser->lexer)->location;
14246 cp_lexer_consume_token (parser->lexer);
14248 /* See if we might have an id-expression. */
14249 token = cp_lexer_peek_token (parser->lexer);
14250 if (token->type == CPP_NAME
14251 || token->keyword == RID_OPERATOR
14252 || token->type == CPP_SCOPE
14253 || token->type == CPP_TEMPLATE_ID
14254 || token->type == CPP_NESTED_NAME_SPECIFIER)
14256 cp_parser_parse_tentatively (parser);
14257 argument = cp_parser_primary_expression (parser,
14258 address_p,
14259 /*cast_p=*/false,
14260 /*template_arg_p=*/true,
14261 &idk);
14262 if (cp_parser_error_occurred (parser)
14263 || !cp_parser_next_token_ends_template_argument_p (parser))
14264 cp_parser_abort_tentative_parse (parser);
14265 else
14267 tree probe;
14269 if (INDIRECT_REF_P (argument))
14271 /* Strip the dereference temporarily. */
14272 gcc_assert (REFERENCE_REF_P (argument));
14273 argument = TREE_OPERAND (argument, 0);
14276 /* If we're in a template, we represent a qualified-id referring
14277 to a static data member as a SCOPE_REF even if the scope isn't
14278 dependent so that we can check access control later. */
14279 probe = argument;
14280 if (TREE_CODE (probe) == SCOPE_REF)
14281 probe = TREE_OPERAND (probe, 1);
14282 if (VAR_P (probe))
14284 /* A variable without external linkage might still be a
14285 valid constant-expression, so no error is issued here
14286 if the external-linkage check fails. */
14287 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14288 cp_parser_simulate_error (parser);
14290 else if (is_overloaded_fn (argument))
14291 /* All overloaded functions are allowed; if the external
14292 linkage test does not pass, an error will be issued
14293 later. */
14295 else if (address_p
14296 && (TREE_CODE (argument) == OFFSET_REF
14297 || TREE_CODE (argument) == SCOPE_REF))
14298 /* A pointer-to-member. */
14300 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14302 else
14303 cp_parser_simulate_error (parser);
14305 if (cp_parser_parse_definitely (parser))
14307 if (address_p)
14308 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14309 tf_warning_or_error);
14310 else
14311 argument = convert_from_reference (argument);
14312 return argument;
14316 /* If the argument started with "&", there are no other valid
14317 alternatives at this point. */
14318 if (address_p)
14320 cp_parser_error (parser, "invalid non-type template argument");
14321 return error_mark_node;
14324 /* If the argument wasn't successfully parsed as a type-id followed
14325 by '>>', the argument can only be a constant expression now.
14326 Otherwise, we try parsing the constant-expression tentatively,
14327 because the argument could really be a type-id. */
14328 if (maybe_type_id)
14329 cp_parser_parse_tentatively (parser);
14330 argument = cp_parser_constant_expression (parser);
14332 if (!maybe_type_id)
14333 return argument;
14334 if (!cp_parser_next_token_ends_template_argument_p (parser))
14335 cp_parser_error (parser, "expected template-argument");
14336 if (cp_parser_parse_definitely (parser))
14337 return argument;
14338 /* We did our best to parse the argument as a non type-id, but that
14339 was the only alternative that matched (albeit with a '>' after
14340 it). We can assume it's just a typo from the user, and a
14341 diagnostic will then be issued. */
14342 return cp_parser_template_type_arg (parser);
14345 /* Parse an explicit-instantiation.
14347 explicit-instantiation:
14348 template declaration
14350 Although the standard says `declaration', what it really means is:
14352 explicit-instantiation:
14353 template decl-specifier-seq [opt] declarator [opt] ;
14355 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14356 supposed to be allowed. A defect report has been filed about this
14357 issue.
14359 GNU Extension:
14361 explicit-instantiation:
14362 storage-class-specifier template
14363 decl-specifier-seq [opt] declarator [opt] ;
14364 function-specifier template
14365 decl-specifier-seq [opt] declarator [opt] ; */
14367 static void
14368 cp_parser_explicit_instantiation (cp_parser* parser)
14370 int declares_class_or_enum;
14371 cp_decl_specifier_seq decl_specifiers;
14372 tree extension_specifier = NULL_TREE;
14374 timevar_push (TV_TEMPLATE_INST);
14376 /* Look for an (optional) storage-class-specifier or
14377 function-specifier. */
14378 if (cp_parser_allow_gnu_extensions_p (parser))
14380 extension_specifier
14381 = cp_parser_storage_class_specifier_opt (parser);
14382 if (!extension_specifier)
14383 extension_specifier
14384 = cp_parser_function_specifier_opt (parser,
14385 /*decl_specs=*/NULL);
14388 /* Look for the `template' keyword. */
14389 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14390 /* Let the front end know that we are processing an explicit
14391 instantiation. */
14392 begin_explicit_instantiation ();
14393 /* [temp.explicit] says that we are supposed to ignore access
14394 control while processing explicit instantiation directives. */
14395 push_deferring_access_checks (dk_no_check);
14396 /* Parse a decl-specifier-seq. */
14397 cp_parser_decl_specifier_seq (parser,
14398 CP_PARSER_FLAGS_OPTIONAL,
14399 &decl_specifiers,
14400 &declares_class_or_enum);
14401 /* If there was exactly one decl-specifier, and it declared a class,
14402 and there's no declarator, then we have an explicit type
14403 instantiation. */
14404 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14406 tree type;
14408 type = check_tag_decl (&decl_specifiers,
14409 /*explicit_type_instantiation_p=*/true);
14410 /* Turn access control back on for names used during
14411 template instantiation. */
14412 pop_deferring_access_checks ();
14413 if (type)
14414 do_type_instantiation (type, extension_specifier,
14415 /*complain=*/tf_error);
14417 else
14419 cp_declarator *declarator;
14420 tree decl;
14422 /* Parse the declarator. */
14423 declarator
14424 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14425 /*ctor_dtor_or_conv_p=*/NULL,
14426 /*parenthesized_p=*/NULL,
14427 /*member_p=*/false,
14428 /*friend_p=*/false);
14429 if (declares_class_or_enum & 2)
14430 cp_parser_check_for_definition_in_return_type (declarator,
14431 decl_specifiers.type,
14432 decl_specifiers.locations[ds_type_spec]);
14433 if (declarator != cp_error_declarator)
14435 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14436 permerror (decl_specifiers.locations[ds_inline],
14437 "explicit instantiation shall not use"
14438 " %<inline%> specifier");
14439 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14440 permerror (decl_specifiers.locations[ds_constexpr],
14441 "explicit instantiation shall not use"
14442 " %<constexpr%> specifier");
14444 decl = grokdeclarator (declarator, &decl_specifiers,
14445 NORMAL, 0, &decl_specifiers.attributes);
14446 /* Turn access control back on for names used during
14447 template instantiation. */
14448 pop_deferring_access_checks ();
14449 /* Do the explicit instantiation. */
14450 do_decl_instantiation (decl, extension_specifier);
14452 else
14454 pop_deferring_access_checks ();
14455 /* Skip the body of the explicit instantiation. */
14456 cp_parser_skip_to_end_of_statement (parser);
14459 /* We're done with the instantiation. */
14460 end_explicit_instantiation ();
14462 cp_parser_consume_semicolon_at_end_of_statement (parser);
14464 timevar_pop (TV_TEMPLATE_INST);
14467 /* Parse an explicit-specialization.
14469 explicit-specialization:
14470 template < > declaration
14472 Although the standard says `declaration', what it really means is:
14474 explicit-specialization:
14475 template <> decl-specifier [opt] init-declarator [opt] ;
14476 template <> function-definition
14477 template <> explicit-specialization
14478 template <> template-declaration */
14480 static void
14481 cp_parser_explicit_specialization (cp_parser* parser)
14483 bool need_lang_pop;
14484 cp_token *token = cp_lexer_peek_token (parser->lexer);
14486 /* Look for the `template' keyword. */
14487 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14488 /* Look for the `<'. */
14489 cp_parser_require (parser, CPP_LESS, RT_LESS);
14490 /* Look for the `>'. */
14491 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14492 /* We have processed another parameter list. */
14493 ++parser->num_template_parameter_lists;
14494 /* [temp]
14496 A template ... explicit specialization ... shall not have C
14497 linkage. */
14498 if (current_lang_name == lang_name_c)
14500 error_at (token->location, "template specialization with C linkage");
14501 /* Give it C++ linkage to avoid confusing other parts of the
14502 front end. */
14503 push_lang_context (lang_name_cplusplus);
14504 need_lang_pop = true;
14506 else
14507 need_lang_pop = false;
14508 /* Let the front end know that we are beginning a specialization. */
14509 if (!begin_specialization ())
14511 end_specialization ();
14512 return;
14515 /* If the next keyword is `template', we need to figure out whether
14516 or not we're looking a template-declaration. */
14517 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14519 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14520 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14521 cp_parser_template_declaration_after_export (parser,
14522 /*member_p=*/false);
14523 else
14524 cp_parser_explicit_specialization (parser);
14526 else
14527 /* Parse the dependent declaration. */
14528 cp_parser_single_declaration (parser,
14529 /*checks=*/NULL,
14530 /*member_p=*/false,
14531 /*explicit_specialization_p=*/true,
14532 /*friend_p=*/NULL);
14533 /* We're done with the specialization. */
14534 end_specialization ();
14535 /* For the erroneous case of a template with C linkage, we pushed an
14536 implicit C++ linkage scope; exit that scope now. */
14537 if (need_lang_pop)
14538 pop_lang_context ();
14539 /* We're done with this parameter list. */
14540 --parser->num_template_parameter_lists;
14543 /* Parse a type-specifier.
14545 type-specifier:
14546 simple-type-specifier
14547 class-specifier
14548 enum-specifier
14549 elaborated-type-specifier
14550 cv-qualifier
14552 GNU Extension:
14554 type-specifier:
14555 __complex__
14557 Returns a representation of the type-specifier. For a
14558 class-specifier, enum-specifier, or elaborated-type-specifier, a
14559 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14561 The parser flags FLAGS is used to control type-specifier parsing.
14563 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14564 in a decl-specifier-seq.
14566 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14567 class-specifier, enum-specifier, or elaborated-type-specifier, then
14568 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14569 if a type is declared; 2 if it is defined. Otherwise, it is set to
14570 zero.
14572 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14573 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14574 is set to FALSE. */
14576 static tree
14577 cp_parser_type_specifier (cp_parser* parser,
14578 cp_parser_flags flags,
14579 cp_decl_specifier_seq *decl_specs,
14580 bool is_declaration,
14581 int* declares_class_or_enum,
14582 bool* is_cv_qualifier)
14584 tree type_spec = NULL_TREE;
14585 cp_token *token;
14586 enum rid keyword;
14587 cp_decl_spec ds = ds_last;
14589 /* Assume this type-specifier does not declare a new type. */
14590 if (declares_class_or_enum)
14591 *declares_class_or_enum = 0;
14592 /* And that it does not specify a cv-qualifier. */
14593 if (is_cv_qualifier)
14594 *is_cv_qualifier = false;
14595 /* Peek at the next token. */
14596 token = cp_lexer_peek_token (parser->lexer);
14598 /* If we're looking at a keyword, we can use that to guide the
14599 production we choose. */
14600 keyword = token->keyword;
14601 switch (keyword)
14603 case RID_ENUM:
14604 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14605 goto elaborated_type_specifier;
14607 /* Look for the enum-specifier. */
14608 type_spec = cp_parser_enum_specifier (parser);
14609 /* If that worked, we're done. */
14610 if (type_spec)
14612 if (declares_class_or_enum)
14613 *declares_class_or_enum = 2;
14614 if (decl_specs)
14615 cp_parser_set_decl_spec_type (decl_specs,
14616 type_spec,
14617 token,
14618 /*type_definition_p=*/true);
14619 return type_spec;
14621 else
14622 goto elaborated_type_specifier;
14624 /* Any of these indicate either a class-specifier, or an
14625 elaborated-type-specifier. */
14626 case RID_CLASS:
14627 case RID_STRUCT:
14628 case RID_UNION:
14629 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14630 goto elaborated_type_specifier;
14632 /* Parse tentatively so that we can back up if we don't find a
14633 class-specifier. */
14634 cp_parser_parse_tentatively (parser);
14635 /* Look for the class-specifier. */
14636 type_spec = cp_parser_class_specifier (parser);
14637 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14638 /* If that worked, we're done. */
14639 if (cp_parser_parse_definitely (parser))
14641 if (declares_class_or_enum)
14642 *declares_class_or_enum = 2;
14643 if (decl_specs)
14644 cp_parser_set_decl_spec_type (decl_specs,
14645 type_spec,
14646 token,
14647 /*type_definition_p=*/true);
14648 return type_spec;
14651 /* Fall through. */
14652 elaborated_type_specifier:
14653 /* We're declaring (not defining) a class or enum. */
14654 if (declares_class_or_enum)
14655 *declares_class_or_enum = 1;
14657 /* Fall through. */
14658 case RID_TYPENAME:
14659 /* Look for an elaborated-type-specifier. */
14660 type_spec
14661 = (cp_parser_elaborated_type_specifier
14662 (parser,
14663 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14664 is_declaration));
14665 if (decl_specs)
14666 cp_parser_set_decl_spec_type (decl_specs,
14667 type_spec,
14668 token,
14669 /*type_definition_p=*/false);
14670 return type_spec;
14672 case RID_CONST:
14673 ds = ds_const;
14674 if (is_cv_qualifier)
14675 *is_cv_qualifier = true;
14676 break;
14678 case RID_VOLATILE:
14679 ds = ds_volatile;
14680 if (is_cv_qualifier)
14681 *is_cv_qualifier = true;
14682 break;
14684 case RID_RESTRICT:
14685 ds = ds_restrict;
14686 if (is_cv_qualifier)
14687 *is_cv_qualifier = true;
14688 break;
14690 case RID_COMPLEX:
14691 /* The `__complex__' keyword is a GNU extension. */
14692 ds = ds_complex;
14693 break;
14695 default:
14696 break;
14699 /* Handle simple keywords. */
14700 if (ds != ds_last)
14702 if (decl_specs)
14704 set_and_check_decl_spec_loc (decl_specs, ds, token);
14705 decl_specs->any_specifiers_p = true;
14707 return cp_lexer_consume_token (parser->lexer)->u.value;
14710 /* If we do not already have a type-specifier, assume we are looking
14711 at a simple-type-specifier. */
14712 type_spec = cp_parser_simple_type_specifier (parser,
14713 decl_specs,
14714 flags);
14716 /* If we didn't find a type-specifier, and a type-specifier was not
14717 optional in this context, issue an error message. */
14718 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14720 cp_parser_error (parser, "expected type specifier");
14721 return error_mark_node;
14724 return type_spec;
14727 /* Parse a simple-type-specifier.
14729 simple-type-specifier:
14730 :: [opt] nested-name-specifier [opt] type-name
14731 :: [opt] nested-name-specifier template template-id
14732 char
14733 wchar_t
14734 bool
14735 short
14737 long
14738 signed
14739 unsigned
14740 float
14741 double
14742 void
14744 C++0x Extension:
14746 simple-type-specifier:
14747 auto
14748 decltype ( expression )
14749 char16_t
14750 char32_t
14751 __underlying_type ( type-id )
14753 GNU Extension:
14755 simple-type-specifier:
14756 __int128
14757 __typeof__ unary-expression
14758 __typeof__ ( type-id )
14759 __typeof__ ( type-id ) { initializer-list , [opt] }
14761 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14762 appropriately updated. */
14764 static tree
14765 cp_parser_simple_type_specifier (cp_parser* parser,
14766 cp_decl_specifier_seq *decl_specs,
14767 cp_parser_flags flags)
14769 tree type = NULL_TREE;
14770 cp_token *token;
14771 int idx;
14773 /* Peek at the next token. */
14774 token = cp_lexer_peek_token (parser->lexer);
14776 /* If we're looking at a keyword, things are easy. */
14777 switch (token->keyword)
14779 case RID_CHAR:
14780 if (decl_specs)
14781 decl_specs->explicit_char_p = true;
14782 type = char_type_node;
14783 break;
14784 case RID_CHAR16:
14785 type = char16_type_node;
14786 break;
14787 case RID_CHAR32:
14788 type = char32_type_node;
14789 break;
14790 case RID_WCHAR:
14791 type = wchar_type_node;
14792 break;
14793 case RID_BOOL:
14794 type = boolean_type_node;
14795 break;
14796 case RID_SHORT:
14797 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14798 type = short_integer_type_node;
14799 break;
14800 case RID_INT:
14801 if (decl_specs)
14802 decl_specs->explicit_int_p = true;
14803 type = integer_type_node;
14804 break;
14805 case RID_INT_N_0:
14806 case RID_INT_N_1:
14807 case RID_INT_N_2:
14808 case RID_INT_N_3:
14809 idx = token->keyword - RID_INT_N_0;
14810 if (! int_n_enabled_p [idx])
14811 break;
14812 if (decl_specs)
14814 decl_specs->explicit_intN_p = true;
14815 decl_specs->int_n_idx = idx;
14817 type = int_n_trees [idx].signed_type;
14818 break;
14819 case RID_LONG:
14820 if (decl_specs)
14821 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14822 type = long_integer_type_node;
14823 break;
14824 case RID_SIGNED:
14825 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14826 type = integer_type_node;
14827 break;
14828 case RID_UNSIGNED:
14829 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14830 type = unsigned_type_node;
14831 break;
14832 case RID_FLOAT:
14833 type = float_type_node;
14834 break;
14835 case RID_DOUBLE:
14836 type = double_type_node;
14837 break;
14838 case RID_VOID:
14839 type = void_type_node;
14840 break;
14842 case RID_AUTO:
14843 maybe_warn_cpp0x (CPP0X_AUTO);
14844 if (parser->auto_is_implicit_function_template_parm_p)
14846 type = synthesize_implicit_template_parm (parser);
14848 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14850 if (cxx_dialect < cxx14)
14851 pedwarn (location_of (type), 0,
14852 "use of %<auto%> in lambda parameter declaration "
14853 "only available with "
14854 "-std=c++14 or -std=gnu++14");
14856 else if (cxx_dialect < cxx14)
14857 pedwarn (location_of (type), 0,
14858 "use of %<auto%> in parameter declaration "
14859 "only available with "
14860 "-std=c++14 or -std=gnu++14");
14861 else
14862 pedwarn (location_of (type), OPT_Wpedantic,
14863 "ISO C++ forbids use of %<auto%> in parameter "
14864 "declaration");
14866 else
14867 type = make_auto ();
14868 break;
14870 case RID_DECLTYPE:
14871 /* Since DR 743, decltype can either be a simple-type-specifier by
14872 itself or begin a nested-name-specifier. Parsing it will replace
14873 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14874 handling below decide what to do. */
14875 cp_parser_decltype (parser);
14876 cp_lexer_set_token_position (parser->lexer, token);
14877 break;
14879 case RID_TYPEOF:
14880 /* Consume the `typeof' token. */
14881 cp_lexer_consume_token (parser->lexer);
14882 /* Parse the operand to `typeof'. */
14883 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14884 /* If it is not already a TYPE, take its type. */
14885 if (!TYPE_P (type))
14886 type = finish_typeof (type);
14888 if (decl_specs)
14889 cp_parser_set_decl_spec_type (decl_specs, type,
14890 token,
14891 /*type_definition_p=*/false);
14893 return type;
14895 case RID_UNDERLYING_TYPE:
14896 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14897 if (decl_specs)
14898 cp_parser_set_decl_spec_type (decl_specs, type,
14899 token,
14900 /*type_definition_p=*/false);
14902 return type;
14904 case RID_BASES:
14905 case RID_DIRECT_BASES:
14906 type = cp_parser_trait_expr (parser, token->keyword);
14907 if (decl_specs)
14908 cp_parser_set_decl_spec_type (decl_specs, type,
14909 token,
14910 /*type_definition_p=*/false);
14911 return type;
14912 default:
14913 break;
14916 /* If token is an already-parsed decltype not followed by ::,
14917 it's a simple-type-specifier. */
14918 if (token->type == CPP_DECLTYPE
14919 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14921 type = token->u.value;
14922 if (decl_specs)
14924 cp_parser_set_decl_spec_type (decl_specs, type,
14925 token,
14926 /*type_definition_p=*/false);
14927 /* Remember that we are handling a decltype in order to
14928 implement the resolution of DR 1510 when the argument
14929 isn't instantiation dependent. */
14930 decl_specs->decltype_p = true;
14932 cp_lexer_consume_token (parser->lexer);
14933 return type;
14936 /* If the type-specifier was for a built-in type, we're done. */
14937 if (type)
14939 /* Record the type. */
14940 if (decl_specs
14941 && (token->keyword != RID_SIGNED
14942 && token->keyword != RID_UNSIGNED
14943 && token->keyword != RID_SHORT
14944 && token->keyword != RID_LONG))
14945 cp_parser_set_decl_spec_type (decl_specs,
14946 type,
14947 token,
14948 /*type_definition_p=*/false);
14949 if (decl_specs)
14950 decl_specs->any_specifiers_p = true;
14952 /* Consume the token. */
14953 cp_lexer_consume_token (parser->lexer);
14955 /* There is no valid C++ program where a non-template type is
14956 followed by a "<". That usually indicates that the user thought
14957 that the type was a template. */
14958 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14959 token->location);
14961 return TYPE_NAME (type);
14964 /* The type-specifier must be a user-defined type. */
14965 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14967 bool qualified_p;
14968 bool global_p;
14970 /* Don't gobble tokens or issue error messages if this is an
14971 optional type-specifier. */
14972 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14973 cp_parser_parse_tentatively (parser);
14975 /* Look for the optional `::' operator. */
14976 global_p
14977 = (cp_parser_global_scope_opt (parser,
14978 /*current_scope_valid_p=*/false)
14979 != NULL_TREE);
14980 /* Look for the nested-name specifier. */
14981 qualified_p
14982 = (cp_parser_nested_name_specifier_opt (parser,
14983 /*typename_keyword_p=*/false,
14984 /*check_dependency_p=*/true,
14985 /*type_p=*/false,
14986 /*is_declaration=*/false)
14987 != NULL_TREE);
14988 token = cp_lexer_peek_token (parser->lexer);
14989 /* If we have seen a nested-name-specifier, and the next token
14990 is `template', then we are using the template-id production. */
14991 if (parser->scope
14992 && cp_parser_optional_template_keyword (parser))
14994 /* Look for the template-id. */
14995 type = cp_parser_template_id (parser,
14996 /*template_keyword_p=*/true,
14997 /*check_dependency_p=*/true,
14998 none_type,
14999 /*is_declaration=*/false);
15000 /* If the template-id did not name a type, we are out of
15001 luck. */
15002 if (TREE_CODE (type) != TYPE_DECL)
15004 cp_parser_error (parser, "expected template-id for type");
15005 type = NULL_TREE;
15008 /* Otherwise, look for a type-name. */
15009 else
15010 type = cp_parser_type_name (parser);
15011 /* Keep track of all name-lookups performed in class scopes. */
15012 if (type
15013 && !global_p
15014 && !qualified_p
15015 && TREE_CODE (type) == TYPE_DECL
15016 && identifier_p (DECL_NAME (type)))
15017 maybe_note_name_used_in_class (DECL_NAME (type), type);
15018 /* If it didn't work out, we don't have a TYPE. */
15019 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15020 && !cp_parser_parse_definitely (parser))
15021 type = NULL_TREE;
15022 if (type && decl_specs)
15023 cp_parser_set_decl_spec_type (decl_specs, type,
15024 token,
15025 /*type_definition_p=*/false);
15028 /* If we didn't get a type-name, issue an error message. */
15029 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15031 cp_parser_error (parser, "expected type-name");
15032 return error_mark_node;
15035 if (type && type != error_mark_node)
15037 /* See if TYPE is an Objective-C type, and if so, parse and
15038 accept any protocol references following it. Do this before
15039 the cp_parser_check_for_invalid_template_id() call, because
15040 Objective-C types can be followed by '<...>' which would
15041 enclose protocol names rather than template arguments, and so
15042 everything is fine. */
15043 if (c_dialect_objc () && !parser->scope
15044 && (objc_is_id (type) || objc_is_class_name (type)))
15046 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15047 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15049 /* Clobber the "unqualified" type previously entered into
15050 DECL_SPECS with the new, improved protocol-qualified version. */
15051 if (decl_specs)
15052 decl_specs->type = qual_type;
15054 return qual_type;
15057 /* There is no valid C++ program where a non-template type is
15058 followed by a "<". That usually indicates that the user
15059 thought that the type was a template. */
15060 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15061 none_type,
15062 token->location);
15065 return type;
15068 /* Parse a type-name.
15070 type-name:
15071 class-name
15072 enum-name
15073 typedef-name
15074 simple-template-id [in c++0x]
15076 enum-name:
15077 identifier
15079 typedef-name:
15080 identifier
15082 Returns a TYPE_DECL for the type. */
15084 static tree
15085 cp_parser_type_name (cp_parser* parser)
15087 tree type_decl;
15089 /* We can't know yet whether it is a class-name or not. */
15090 cp_parser_parse_tentatively (parser);
15091 /* Try a class-name. */
15092 type_decl = cp_parser_class_name (parser,
15093 /*typename_keyword_p=*/false,
15094 /*template_keyword_p=*/false,
15095 none_type,
15096 /*check_dependency_p=*/true,
15097 /*class_head_p=*/false,
15098 /*is_declaration=*/false);
15099 /* If it's not a class-name, keep looking. */
15100 if (!cp_parser_parse_definitely (parser))
15102 if (cxx_dialect < cxx11)
15103 /* It must be a typedef-name or an enum-name. */
15104 return cp_parser_nonclass_name (parser);
15106 cp_parser_parse_tentatively (parser);
15107 /* It is either a simple-template-id representing an
15108 instantiation of an alias template... */
15109 type_decl = cp_parser_template_id (parser,
15110 /*template_keyword_p=*/false,
15111 /*check_dependency_p=*/true,
15112 none_type,
15113 /*is_declaration=*/false);
15114 /* Note that this must be an instantiation of an alias template
15115 because [temp.names]/6 says:
15117 A template-id that names an alias template specialization
15118 is a type-name.
15120 Whereas [temp.names]/7 says:
15122 A simple-template-id that names a class template
15123 specialization is a class-name. */
15124 if (type_decl != NULL_TREE
15125 && TREE_CODE (type_decl) == TYPE_DECL
15126 && TYPE_DECL_ALIAS_P (type_decl))
15127 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15128 else
15129 cp_parser_simulate_error (parser);
15131 if (!cp_parser_parse_definitely (parser))
15132 /* ... Or a typedef-name or an enum-name. */
15133 return cp_parser_nonclass_name (parser);
15136 return type_decl;
15139 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15141 enum-name:
15142 identifier
15144 typedef-name:
15145 identifier
15147 Returns a TYPE_DECL for the type. */
15149 static tree
15150 cp_parser_nonclass_name (cp_parser* parser)
15152 tree type_decl;
15153 tree identifier;
15155 cp_token *token = cp_lexer_peek_token (parser->lexer);
15156 identifier = cp_parser_identifier (parser);
15157 if (identifier == error_mark_node)
15158 return error_mark_node;
15160 /* Look up the type-name. */
15161 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15163 type_decl = strip_using_decl (type_decl);
15165 if (TREE_CODE (type_decl) != TYPE_DECL
15166 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15168 /* See if this is an Objective-C type. */
15169 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15170 tree type = objc_get_protocol_qualified_type (identifier, protos);
15171 if (type)
15172 type_decl = TYPE_NAME (type);
15175 /* Issue an error if we did not find a type-name. */
15176 if (TREE_CODE (type_decl) != TYPE_DECL
15177 /* In Objective-C, we have the complication that class names are
15178 normally type names and start declarations (eg, the
15179 "NSObject" in "NSObject *object;"), but can be used in an
15180 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15181 is an expression. So, a classname followed by a dot is not a
15182 valid type-name. */
15183 || (objc_is_class_name (TREE_TYPE (type_decl))
15184 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15186 if (!cp_parser_simulate_error (parser))
15187 cp_parser_name_lookup_error (parser, identifier, type_decl,
15188 NLE_TYPE, token->location);
15189 return error_mark_node;
15191 /* Remember that the name was used in the definition of the
15192 current class so that we can check later to see if the
15193 meaning would have been different after the class was
15194 entirely defined. */
15195 else if (type_decl != error_mark_node
15196 && !parser->scope)
15197 maybe_note_name_used_in_class (identifier, type_decl);
15199 return type_decl;
15202 /* Parse an elaborated-type-specifier. Note that the grammar given
15203 here incorporates the resolution to DR68.
15205 elaborated-type-specifier:
15206 class-key :: [opt] nested-name-specifier [opt] identifier
15207 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15208 enum-key :: [opt] nested-name-specifier [opt] identifier
15209 typename :: [opt] nested-name-specifier identifier
15210 typename :: [opt] nested-name-specifier template [opt]
15211 template-id
15213 GNU extension:
15215 elaborated-type-specifier:
15216 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15217 class-key attributes :: [opt] nested-name-specifier [opt]
15218 template [opt] template-id
15219 enum attributes :: [opt] nested-name-specifier [opt] identifier
15221 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15222 declared `friend'. If IS_DECLARATION is TRUE, then this
15223 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15224 something is being declared.
15226 Returns the TYPE specified. */
15228 static tree
15229 cp_parser_elaborated_type_specifier (cp_parser* parser,
15230 bool is_friend,
15231 bool is_declaration)
15233 enum tag_types tag_type;
15234 tree identifier;
15235 tree type = NULL_TREE;
15236 tree attributes = NULL_TREE;
15237 tree globalscope;
15238 cp_token *token = NULL;
15240 /* See if we're looking at the `enum' keyword. */
15241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15243 /* Consume the `enum' token. */
15244 cp_lexer_consume_token (parser->lexer);
15245 /* Remember that it's an enumeration type. */
15246 tag_type = enum_type;
15247 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15248 enums) is used here. */
15249 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15250 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15252 pedwarn (input_location, 0, "elaborated-type-specifier "
15253 "for a scoped enum must not use the %<%D%> keyword",
15254 cp_lexer_peek_token (parser->lexer)->u.value);
15255 /* Consume the `struct' or `class' and parse it anyway. */
15256 cp_lexer_consume_token (parser->lexer);
15258 /* Parse the attributes. */
15259 attributes = cp_parser_attributes_opt (parser);
15261 /* Or, it might be `typename'. */
15262 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15263 RID_TYPENAME))
15265 /* Consume the `typename' token. */
15266 cp_lexer_consume_token (parser->lexer);
15267 /* Remember that it's a `typename' type. */
15268 tag_type = typename_type;
15270 /* Otherwise it must be a class-key. */
15271 else
15273 tag_type = cp_parser_class_key (parser);
15274 if (tag_type == none_type)
15275 return error_mark_node;
15276 /* Parse the attributes. */
15277 attributes = cp_parser_attributes_opt (parser);
15280 /* Look for the `::' operator. */
15281 globalscope = cp_parser_global_scope_opt (parser,
15282 /*current_scope_valid_p=*/false);
15283 /* Look for the nested-name-specifier. */
15284 if (tag_type == typename_type && !globalscope)
15286 if (!cp_parser_nested_name_specifier (parser,
15287 /*typename_keyword_p=*/true,
15288 /*check_dependency_p=*/true,
15289 /*type_p=*/true,
15290 is_declaration))
15291 return error_mark_node;
15293 else
15294 /* Even though `typename' is not present, the proposed resolution
15295 to Core Issue 180 says that in `class A<T>::B', `B' should be
15296 considered a type-name, even if `A<T>' is dependent. */
15297 cp_parser_nested_name_specifier_opt (parser,
15298 /*typename_keyword_p=*/true,
15299 /*check_dependency_p=*/true,
15300 /*type_p=*/true,
15301 is_declaration);
15302 /* For everything but enumeration types, consider a template-id.
15303 For an enumeration type, consider only a plain identifier. */
15304 if (tag_type != enum_type)
15306 bool template_p = false;
15307 tree decl;
15309 /* Allow the `template' keyword. */
15310 template_p = cp_parser_optional_template_keyword (parser);
15311 /* If we didn't see `template', we don't know if there's a
15312 template-id or not. */
15313 if (!template_p)
15314 cp_parser_parse_tentatively (parser);
15315 /* Parse the template-id. */
15316 token = cp_lexer_peek_token (parser->lexer);
15317 decl = cp_parser_template_id (parser, template_p,
15318 /*check_dependency_p=*/true,
15319 tag_type,
15320 is_declaration);
15321 /* If we didn't find a template-id, look for an ordinary
15322 identifier. */
15323 if (!template_p && !cp_parser_parse_definitely (parser))
15325 /* We can get here when cp_parser_template_id, called by
15326 cp_parser_class_name with tag_type == none_type, succeeds
15327 and caches a BASELINK. Then, when called again here,
15328 instead of failing and returning an error_mark_node
15329 returns it (see template/typename17.C in C++11).
15330 ??? Could we diagnose this earlier? */
15331 else if (tag_type == typename_type && BASELINK_P (decl))
15333 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15334 type = error_mark_node;
15336 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15337 in effect, then we must assume that, upon instantiation, the
15338 template will correspond to a class. */
15339 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15340 && tag_type == typename_type)
15341 type = make_typename_type (parser->scope, decl,
15342 typename_type,
15343 /*complain=*/tf_error);
15344 /* If the `typename' keyword is in effect and DECL is not a type
15345 decl, then type is non existent. */
15346 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15348 else if (TREE_CODE (decl) == TYPE_DECL)
15349 type = check_elaborated_type_specifier (tag_type, decl,
15350 /*allow_template_p=*/true);
15351 else if (decl == error_mark_node)
15352 type = error_mark_node;
15355 if (!type)
15357 token = cp_lexer_peek_token (parser->lexer);
15358 identifier = cp_parser_identifier (parser);
15360 if (identifier == error_mark_node)
15362 parser->scope = NULL_TREE;
15363 return error_mark_node;
15366 /* For a `typename', we needn't call xref_tag. */
15367 if (tag_type == typename_type
15368 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15369 return cp_parser_make_typename_type (parser, identifier,
15370 token->location);
15372 /* Template parameter lists apply only if we are not within a
15373 function parameter list. */
15374 bool template_parm_lists_apply
15375 = parser->num_template_parameter_lists;
15376 if (template_parm_lists_apply)
15377 for (cp_binding_level *s = current_binding_level;
15378 s && s->kind != sk_template_parms;
15379 s = s->level_chain)
15380 if (s->kind == sk_function_parms)
15381 template_parm_lists_apply = false;
15383 /* Look up a qualified name in the usual way. */
15384 if (parser->scope)
15386 tree decl;
15387 tree ambiguous_decls;
15389 decl = cp_parser_lookup_name (parser, identifier,
15390 tag_type,
15391 /*is_template=*/false,
15392 /*is_namespace=*/false,
15393 /*check_dependency=*/true,
15394 &ambiguous_decls,
15395 token->location);
15397 /* If the lookup was ambiguous, an error will already have been
15398 issued. */
15399 if (ambiguous_decls)
15400 return error_mark_node;
15402 /* If we are parsing friend declaration, DECL may be a
15403 TEMPLATE_DECL tree node here. However, we need to check
15404 whether this TEMPLATE_DECL results in valid code. Consider
15405 the following example:
15407 namespace N {
15408 template <class T> class C {};
15410 class X {
15411 template <class T> friend class N::C; // #1, valid code
15413 template <class T> class Y {
15414 friend class N::C; // #2, invalid code
15417 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15418 name lookup of `N::C'. We see that friend declaration must
15419 be template for the code to be valid. Note that
15420 processing_template_decl does not work here since it is
15421 always 1 for the above two cases. */
15423 decl = (cp_parser_maybe_treat_template_as_class
15424 (decl, /*tag_name_p=*/is_friend
15425 && template_parm_lists_apply));
15427 if (TREE_CODE (decl) != TYPE_DECL)
15429 cp_parser_diagnose_invalid_type_name (parser,
15430 identifier,
15431 token->location);
15432 return error_mark_node;
15435 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15437 bool allow_template = (template_parm_lists_apply
15438 || DECL_SELF_REFERENCE_P (decl));
15439 type = check_elaborated_type_specifier (tag_type, decl,
15440 allow_template);
15442 if (type == error_mark_node)
15443 return error_mark_node;
15446 /* Forward declarations of nested types, such as
15448 class C1::C2;
15449 class C1::C2::C3;
15451 are invalid unless all components preceding the final '::'
15452 are complete. If all enclosing types are complete, these
15453 declarations become merely pointless.
15455 Invalid forward declarations of nested types are errors
15456 caught elsewhere in parsing. Those that are pointless arrive
15457 here. */
15459 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15460 && !is_friend && !processing_explicit_instantiation)
15461 warning (0, "declaration %qD does not declare anything", decl);
15463 type = TREE_TYPE (decl);
15465 else
15467 /* An elaborated-type-specifier sometimes introduces a new type and
15468 sometimes names an existing type. Normally, the rule is that it
15469 introduces a new type only if there is not an existing type of
15470 the same name already in scope. For example, given:
15472 struct S {};
15473 void f() { struct S s; }
15475 the `struct S' in the body of `f' is the same `struct S' as in
15476 the global scope; the existing definition is used. However, if
15477 there were no global declaration, this would introduce a new
15478 local class named `S'.
15480 An exception to this rule applies to the following code:
15482 namespace N { struct S; }
15484 Here, the elaborated-type-specifier names a new type
15485 unconditionally; even if there is already an `S' in the
15486 containing scope this declaration names a new type.
15487 This exception only applies if the elaborated-type-specifier
15488 forms the complete declaration:
15490 [class.name]
15492 A declaration consisting solely of `class-key identifier ;' is
15493 either a redeclaration of the name in the current scope or a
15494 forward declaration of the identifier as a class name. It
15495 introduces the name into the current scope.
15497 We are in this situation precisely when the next token is a `;'.
15499 An exception to the exception is that a `friend' declaration does
15500 *not* name a new type; i.e., given:
15502 struct S { friend struct T; };
15504 `T' is not a new type in the scope of `S'.
15506 Also, `new struct S' or `sizeof (struct S)' never results in the
15507 definition of a new type; a new type can only be declared in a
15508 declaration context. */
15510 tag_scope ts;
15511 bool template_p;
15513 if (is_friend)
15514 /* Friends have special name lookup rules. */
15515 ts = ts_within_enclosing_non_class;
15516 else if (is_declaration
15517 && cp_lexer_next_token_is (parser->lexer,
15518 CPP_SEMICOLON))
15519 /* This is a `class-key identifier ;' */
15520 ts = ts_current;
15521 else
15522 ts = ts_global;
15524 template_p =
15525 (template_parm_lists_apply
15526 && (cp_parser_next_token_starts_class_definition_p (parser)
15527 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15528 /* An unqualified name was used to reference this type, so
15529 there were no qualifying templates. */
15530 if (template_parm_lists_apply
15531 && !cp_parser_check_template_parameters (parser,
15532 /*num_templates=*/0,
15533 token->location,
15534 /*declarator=*/NULL))
15535 return error_mark_node;
15536 type = xref_tag (tag_type, identifier, ts, template_p);
15540 if (type == error_mark_node)
15541 return error_mark_node;
15543 /* Allow attributes on forward declarations of classes. */
15544 if (attributes)
15546 if (TREE_CODE (type) == TYPENAME_TYPE)
15547 warning (OPT_Wattributes,
15548 "attributes ignored on uninstantiated type");
15549 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15550 && ! processing_explicit_instantiation)
15551 warning (OPT_Wattributes,
15552 "attributes ignored on template instantiation");
15553 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15554 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15555 else
15556 warning (OPT_Wattributes,
15557 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15560 if (tag_type != enum_type)
15562 /* Indicate whether this class was declared as a `class' or as a
15563 `struct'. */
15564 if (TREE_CODE (type) == RECORD_TYPE)
15565 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15566 cp_parser_check_class_key (tag_type, type);
15569 /* A "<" cannot follow an elaborated type specifier. If that
15570 happens, the user was probably trying to form a template-id. */
15571 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15572 token->location);
15574 return type;
15577 /* Parse an enum-specifier.
15579 enum-specifier:
15580 enum-head { enumerator-list [opt] }
15581 enum-head { enumerator-list , } [C++0x]
15583 enum-head:
15584 enum-key identifier [opt] enum-base [opt]
15585 enum-key nested-name-specifier identifier enum-base [opt]
15587 enum-key:
15588 enum
15589 enum class [C++0x]
15590 enum struct [C++0x]
15592 enum-base: [C++0x]
15593 : type-specifier-seq
15595 opaque-enum-specifier:
15596 enum-key identifier enum-base [opt] ;
15598 GNU Extensions:
15599 enum-key attributes[opt] identifier [opt] enum-base [opt]
15600 { enumerator-list [opt] }attributes[opt]
15601 enum-key attributes[opt] identifier [opt] enum-base [opt]
15602 { enumerator-list, }attributes[opt] [C++0x]
15604 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15605 if the token stream isn't an enum-specifier after all. */
15607 static tree
15608 cp_parser_enum_specifier (cp_parser* parser)
15610 tree identifier;
15611 tree type = NULL_TREE;
15612 tree prev_scope;
15613 tree nested_name_specifier = NULL_TREE;
15614 tree attributes;
15615 bool scoped_enum_p = false;
15616 bool has_underlying_type = false;
15617 bool nested_being_defined = false;
15618 bool new_value_list = false;
15619 bool is_new_type = false;
15620 bool is_anonymous = false;
15621 tree underlying_type = NULL_TREE;
15622 cp_token *type_start_token = NULL;
15623 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15625 parser->colon_corrects_to_scope_p = false;
15627 /* Parse tentatively so that we can back up if we don't find a
15628 enum-specifier. */
15629 cp_parser_parse_tentatively (parser);
15631 /* Caller guarantees that the current token is 'enum', an identifier
15632 possibly follows, and the token after that is an opening brace.
15633 If we don't have an identifier, fabricate an anonymous name for
15634 the enumeration being defined. */
15635 cp_lexer_consume_token (parser->lexer);
15637 /* Parse the "class" or "struct", which indicates a scoped
15638 enumeration type in C++0x. */
15639 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15640 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15642 if (cxx_dialect < cxx11)
15643 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15645 /* Consume the `struct' or `class' token. */
15646 cp_lexer_consume_token (parser->lexer);
15648 scoped_enum_p = true;
15651 attributes = cp_parser_attributes_opt (parser);
15653 /* Clear the qualification. */
15654 parser->scope = NULL_TREE;
15655 parser->qualifying_scope = NULL_TREE;
15656 parser->object_scope = NULL_TREE;
15658 /* Figure out in what scope the declaration is being placed. */
15659 prev_scope = current_scope ();
15661 type_start_token = cp_lexer_peek_token (parser->lexer);
15663 push_deferring_access_checks (dk_no_check);
15664 nested_name_specifier
15665 = cp_parser_nested_name_specifier_opt (parser,
15666 /*typename_keyword_p=*/true,
15667 /*check_dependency_p=*/false,
15668 /*type_p=*/false,
15669 /*is_declaration=*/false);
15671 if (nested_name_specifier)
15673 tree name;
15675 identifier = cp_parser_identifier (parser);
15676 name = cp_parser_lookup_name (parser, identifier,
15677 enum_type,
15678 /*is_template=*/false,
15679 /*is_namespace=*/false,
15680 /*check_dependency=*/true,
15681 /*ambiguous_decls=*/NULL,
15682 input_location);
15683 if (name && name != error_mark_node)
15685 type = TREE_TYPE (name);
15686 if (TREE_CODE (type) == TYPENAME_TYPE)
15688 /* Are template enums allowed in ISO? */
15689 if (template_parm_scope_p ())
15690 pedwarn (type_start_token->location, OPT_Wpedantic,
15691 "%qD is an enumeration template", name);
15692 /* ignore a typename reference, for it will be solved by name
15693 in start_enum. */
15694 type = NULL_TREE;
15697 else if (nested_name_specifier == error_mark_node)
15698 /* We already issued an error. */;
15699 else
15700 error_at (type_start_token->location,
15701 "%qD is not an enumerator-name", identifier);
15703 else
15705 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15706 identifier = cp_parser_identifier (parser);
15707 else
15709 identifier = make_anon_name ();
15710 is_anonymous = true;
15711 if (scoped_enum_p)
15712 error_at (type_start_token->location,
15713 "anonymous scoped enum is not allowed");
15716 pop_deferring_access_checks ();
15718 /* Check for the `:' that denotes a specified underlying type in C++0x.
15719 Note that a ':' could also indicate a bitfield width, however. */
15720 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15722 cp_decl_specifier_seq type_specifiers;
15724 /* Consume the `:'. */
15725 cp_lexer_consume_token (parser->lexer);
15727 /* Parse the type-specifier-seq. */
15728 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15729 /*is_trailing_return=*/false,
15730 &type_specifiers);
15732 /* At this point this is surely not elaborated type specifier. */
15733 if (!cp_parser_parse_definitely (parser))
15734 return NULL_TREE;
15736 if (cxx_dialect < cxx11)
15737 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15739 has_underlying_type = true;
15741 /* If that didn't work, stop. */
15742 if (type_specifiers.type != error_mark_node)
15744 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15745 /*initialized=*/0, NULL);
15746 if (underlying_type == error_mark_node
15747 || check_for_bare_parameter_packs (underlying_type))
15748 underlying_type = NULL_TREE;
15752 /* Look for the `{' but don't consume it yet. */
15753 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15755 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15757 cp_parser_error (parser, "expected %<{%>");
15758 if (has_underlying_type)
15760 type = NULL_TREE;
15761 goto out;
15764 /* An opaque-enum-specifier must have a ';' here. */
15765 if ((scoped_enum_p || underlying_type)
15766 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15768 cp_parser_error (parser, "expected %<;%> or %<{%>");
15769 if (has_underlying_type)
15771 type = NULL_TREE;
15772 goto out;
15777 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15778 return NULL_TREE;
15780 if (nested_name_specifier)
15782 if (CLASS_TYPE_P (nested_name_specifier))
15784 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15785 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15786 push_scope (nested_name_specifier);
15788 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15790 push_nested_namespace (nested_name_specifier);
15794 /* Issue an error message if type-definitions are forbidden here. */
15795 if (!cp_parser_check_type_definition (parser))
15796 type = error_mark_node;
15797 else
15798 /* Create the new type. We do this before consuming the opening
15799 brace so the enum will be recorded as being on the line of its
15800 tag (or the 'enum' keyword, if there is no tag). */
15801 type = start_enum (identifier, type, underlying_type,
15802 scoped_enum_p, &is_new_type);
15804 /* If the next token is not '{' it is an opaque-enum-specifier or an
15805 elaborated-type-specifier. */
15806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15808 timevar_push (TV_PARSE_ENUM);
15809 if (nested_name_specifier
15810 && nested_name_specifier != error_mark_node)
15812 /* The following catches invalid code such as:
15813 enum class S<int>::E { A, B, C }; */
15814 if (!processing_specialization
15815 && CLASS_TYPE_P (nested_name_specifier)
15816 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15817 error_at (type_start_token->location, "cannot add an enumerator "
15818 "list to a template instantiation");
15820 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15822 error_at (type_start_token->location,
15823 "%<%T::%E%> has not been declared",
15824 TYPE_CONTEXT (nested_name_specifier),
15825 nested_name_specifier);
15826 type = error_mark_node;
15828 /* If that scope does not contain the scope in which the
15829 class was originally declared, the program is invalid. */
15830 else if (prev_scope && !is_ancestor (prev_scope,
15831 nested_name_specifier))
15833 if (at_namespace_scope_p ())
15834 error_at (type_start_token->location,
15835 "declaration of %qD in namespace %qD which does not "
15836 "enclose %qD",
15837 type, prev_scope, nested_name_specifier);
15838 else
15839 error_at (type_start_token->location,
15840 "declaration of %qD in %qD which does not "
15841 "enclose %qD",
15842 type, prev_scope, nested_name_specifier);
15843 type = error_mark_node;
15847 if (scoped_enum_p)
15848 begin_scope (sk_scoped_enum, type);
15850 /* Consume the opening brace. */
15851 cp_lexer_consume_token (parser->lexer);
15853 if (type == error_mark_node)
15854 ; /* Nothing to add */
15855 else if (OPAQUE_ENUM_P (type)
15856 || (cxx_dialect > cxx98 && processing_specialization))
15858 new_value_list = true;
15859 SET_OPAQUE_ENUM_P (type, false);
15860 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15862 else
15864 error_at (type_start_token->location,
15865 "multiple definition of %q#T", type);
15866 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15867 "previous definition here");
15868 type = error_mark_node;
15871 if (type == error_mark_node)
15872 cp_parser_skip_to_end_of_block_or_statement (parser);
15873 /* If the next token is not '}', then there are some enumerators. */
15874 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15876 if (is_anonymous && !scoped_enum_p)
15877 pedwarn (type_start_token->location, OPT_Wpedantic,
15878 "ISO C++ forbids empty anonymous enum");
15880 else
15881 cp_parser_enumerator_list (parser, type);
15883 /* Consume the final '}'. */
15884 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15886 if (scoped_enum_p)
15887 finish_scope ();
15888 timevar_pop (TV_PARSE_ENUM);
15890 else
15892 /* If a ';' follows, then it is an opaque-enum-specifier
15893 and additional restrictions apply. */
15894 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15896 if (is_anonymous)
15897 error_at (type_start_token->location,
15898 "opaque-enum-specifier without name");
15899 else if (nested_name_specifier)
15900 error_at (type_start_token->location,
15901 "opaque-enum-specifier must use a simple identifier");
15905 /* Look for trailing attributes to apply to this enumeration, and
15906 apply them if appropriate. */
15907 if (cp_parser_allow_gnu_extensions_p (parser))
15909 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15910 trailing_attr = chainon (trailing_attr, attributes);
15911 cplus_decl_attributes (&type,
15912 trailing_attr,
15913 (int) ATTR_FLAG_TYPE_IN_PLACE);
15916 /* Finish up the enumeration. */
15917 if (type != error_mark_node)
15919 if (new_value_list)
15920 finish_enum_value_list (type);
15921 if (is_new_type)
15922 finish_enum (type);
15925 if (nested_name_specifier)
15927 if (CLASS_TYPE_P (nested_name_specifier))
15929 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15930 pop_scope (nested_name_specifier);
15932 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15934 pop_nested_namespace (nested_name_specifier);
15937 out:
15938 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15939 return type;
15942 /* Parse an enumerator-list. The enumerators all have the indicated
15943 TYPE.
15945 enumerator-list:
15946 enumerator-definition
15947 enumerator-list , enumerator-definition */
15949 static void
15950 cp_parser_enumerator_list (cp_parser* parser, tree type)
15952 while (true)
15954 /* Parse an enumerator-definition. */
15955 cp_parser_enumerator_definition (parser, type);
15957 /* If the next token is not a ',', we've reached the end of
15958 the list. */
15959 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15960 break;
15961 /* Otherwise, consume the `,' and keep going. */
15962 cp_lexer_consume_token (parser->lexer);
15963 /* If the next token is a `}', there is a trailing comma. */
15964 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15966 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15967 pedwarn (input_location, OPT_Wpedantic,
15968 "comma at end of enumerator list");
15969 break;
15974 /* Parse an enumerator-definition. The enumerator has the indicated
15975 TYPE.
15977 enumerator-definition:
15978 enumerator
15979 enumerator = constant-expression
15981 enumerator:
15982 identifier */
15984 static void
15985 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15987 tree identifier;
15988 tree value;
15989 location_t loc;
15991 /* Save the input location because we are interested in the location
15992 of the identifier and not the location of the explicit value. */
15993 loc = cp_lexer_peek_token (parser->lexer)->location;
15995 /* Look for the identifier. */
15996 identifier = cp_parser_identifier (parser);
15997 if (identifier == error_mark_node)
15998 return;
16000 /* If the next token is an '=', then there is an explicit value. */
16001 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16003 /* Consume the `=' token. */
16004 cp_lexer_consume_token (parser->lexer);
16005 /* Parse the value. */
16006 value = cp_parser_constant_expression (parser);
16008 else
16009 value = NULL_TREE;
16011 /* If we are processing a template, make sure the initializer of the
16012 enumerator doesn't contain any bare template parameter pack. */
16013 if (check_for_bare_parameter_packs (value))
16014 value = error_mark_node;
16016 /* Create the enumerator. */
16017 build_enumerator (identifier, value, type, loc);
16020 /* Parse a namespace-name.
16022 namespace-name:
16023 original-namespace-name
16024 namespace-alias
16026 Returns the NAMESPACE_DECL for the namespace. */
16028 static tree
16029 cp_parser_namespace_name (cp_parser* parser)
16031 tree identifier;
16032 tree namespace_decl;
16034 cp_token *token = cp_lexer_peek_token (parser->lexer);
16036 /* Get the name of the namespace. */
16037 identifier = cp_parser_identifier (parser);
16038 if (identifier == error_mark_node)
16039 return error_mark_node;
16041 /* Look up the identifier in the currently active scope. Look only
16042 for namespaces, due to:
16044 [basic.lookup.udir]
16046 When looking up a namespace-name in a using-directive or alias
16047 definition, only namespace names are considered.
16049 And:
16051 [basic.lookup.qual]
16053 During the lookup of a name preceding the :: scope resolution
16054 operator, object, function, and enumerator names are ignored.
16056 (Note that cp_parser_qualifying_entity only calls this
16057 function if the token after the name is the scope resolution
16058 operator.) */
16059 namespace_decl = cp_parser_lookup_name (parser, identifier,
16060 none_type,
16061 /*is_template=*/false,
16062 /*is_namespace=*/true,
16063 /*check_dependency=*/true,
16064 /*ambiguous_decls=*/NULL,
16065 token->location);
16066 /* If it's not a namespace, issue an error. */
16067 if (namespace_decl == error_mark_node
16068 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16070 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16071 error_at (token->location, "%qD is not a namespace-name", identifier);
16072 cp_parser_error (parser, "expected namespace-name");
16073 namespace_decl = error_mark_node;
16076 return namespace_decl;
16079 /* Parse a namespace-definition.
16081 namespace-definition:
16082 named-namespace-definition
16083 unnamed-namespace-definition
16085 named-namespace-definition:
16086 original-namespace-definition
16087 extension-namespace-definition
16089 original-namespace-definition:
16090 namespace identifier { namespace-body }
16092 extension-namespace-definition:
16093 namespace original-namespace-name { namespace-body }
16095 unnamed-namespace-definition:
16096 namespace { namespace-body } */
16098 static void
16099 cp_parser_namespace_definition (cp_parser* parser)
16101 tree identifier, attribs;
16102 bool has_visibility;
16103 bool is_inline;
16105 cp_ensure_no_omp_declare_simd (parser);
16106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16108 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16109 is_inline = true;
16110 cp_lexer_consume_token (parser->lexer);
16112 else
16113 is_inline = false;
16115 /* Look for the `namespace' keyword. */
16116 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16118 /* Get the name of the namespace. We do not attempt to distinguish
16119 between an original-namespace-definition and an
16120 extension-namespace-definition at this point. The semantic
16121 analysis routines are responsible for that. */
16122 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16123 identifier = cp_parser_identifier (parser);
16124 else
16125 identifier = NULL_TREE;
16127 /* Parse any specified attributes. */
16128 attribs = cp_parser_attributes_opt (parser);
16130 /* Look for the `{' to start the namespace. */
16131 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16132 /* Start the namespace. */
16133 push_namespace (identifier);
16135 /* "inline namespace" is equivalent to a stub namespace definition
16136 followed by a strong using directive. */
16137 if (is_inline)
16139 tree name_space = current_namespace;
16140 /* Set up namespace association. */
16141 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16142 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16143 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16144 /* Import the contents of the inline namespace. */
16145 pop_namespace ();
16146 do_using_directive (name_space);
16147 push_namespace (identifier);
16150 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16152 /* Parse the body of the namespace. */
16153 cp_parser_namespace_body (parser);
16155 if (has_visibility)
16156 pop_visibility (1);
16158 /* Finish the namespace. */
16159 pop_namespace ();
16160 /* Look for the final `}'. */
16161 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16164 /* Parse a namespace-body.
16166 namespace-body:
16167 declaration-seq [opt] */
16169 static void
16170 cp_parser_namespace_body (cp_parser* parser)
16172 cp_parser_declaration_seq_opt (parser);
16175 /* Parse a namespace-alias-definition.
16177 namespace-alias-definition:
16178 namespace identifier = qualified-namespace-specifier ; */
16180 static void
16181 cp_parser_namespace_alias_definition (cp_parser* parser)
16183 tree identifier;
16184 tree namespace_specifier;
16186 cp_token *token = cp_lexer_peek_token (parser->lexer);
16188 /* Look for the `namespace' keyword. */
16189 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16190 /* Look for the identifier. */
16191 identifier = cp_parser_identifier (parser);
16192 if (identifier == error_mark_node)
16193 return;
16194 /* Look for the `=' token. */
16195 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16196 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16198 error_at (token->location, "%<namespace%> definition is not allowed here");
16199 /* Skip the definition. */
16200 cp_lexer_consume_token (parser->lexer);
16201 if (cp_parser_skip_to_closing_brace (parser))
16202 cp_lexer_consume_token (parser->lexer);
16203 return;
16205 cp_parser_require (parser, CPP_EQ, RT_EQ);
16206 /* Look for the qualified-namespace-specifier. */
16207 namespace_specifier
16208 = cp_parser_qualified_namespace_specifier (parser);
16209 /* Look for the `;' token. */
16210 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16212 /* Register the alias in the symbol table. */
16213 do_namespace_alias (identifier, namespace_specifier);
16216 /* Parse a qualified-namespace-specifier.
16218 qualified-namespace-specifier:
16219 :: [opt] nested-name-specifier [opt] namespace-name
16221 Returns a NAMESPACE_DECL corresponding to the specified
16222 namespace. */
16224 static tree
16225 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16227 /* Look for the optional `::'. */
16228 cp_parser_global_scope_opt (parser,
16229 /*current_scope_valid_p=*/false);
16231 /* Look for the optional nested-name-specifier. */
16232 cp_parser_nested_name_specifier_opt (parser,
16233 /*typename_keyword_p=*/false,
16234 /*check_dependency_p=*/true,
16235 /*type_p=*/false,
16236 /*is_declaration=*/true);
16238 return cp_parser_namespace_name (parser);
16241 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16242 access declaration.
16244 using-declaration:
16245 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16246 using :: unqualified-id ;
16248 access-declaration:
16249 qualified-id ;
16253 static bool
16254 cp_parser_using_declaration (cp_parser* parser,
16255 bool access_declaration_p)
16257 cp_token *token;
16258 bool typename_p = false;
16259 bool global_scope_p;
16260 tree decl;
16261 tree identifier;
16262 tree qscope;
16263 int oldcount = errorcount;
16264 cp_token *diag_token = NULL;
16266 if (access_declaration_p)
16268 diag_token = cp_lexer_peek_token (parser->lexer);
16269 cp_parser_parse_tentatively (parser);
16271 else
16273 /* Look for the `using' keyword. */
16274 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16276 /* Peek at the next token. */
16277 token = cp_lexer_peek_token (parser->lexer);
16278 /* See if it's `typename'. */
16279 if (token->keyword == RID_TYPENAME)
16281 /* Remember that we've seen it. */
16282 typename_p = true;
16283 /* Consume the `typename' token. */
16284 cp_lexer_consume_token (parser->lexer);
16288 /* Look for the optional global scope qualification. */
16289 global_scope_p
16290 = (cp_parser_global_scope_opt (parser,
16291 /*current_scope_valid_p=*/false)
16292 != NULL_TREE);
16294 /* If we saw `typename', or didn't see `::', then there must be a
16295 nested-name-specifier present. */
16296 if (typename_p || !global_scope_p)
16298 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16299 /*check_dependency_p=*/true,
16300 /*type_p=*/false,
16301 /*is_declaration=*/true);
16302 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16304 cp_parser_skip_to_end_of_block_or_statement (parser);
16305 return false;
16308 /* Otherwise, we could be in either of the two productions. In that
16309 case, treat the nested-name-specifier as optional. */
16310 else
16311 qscope = cp_parser_nested_name_specifier_opt (parser,
16312 /*typename_keyword_p=*/false,
16313 /*check_dependency_p=*/true,
16314 /*type_p=*/false,
16315 /*is_declaration=*/true);
16316 if (!qscope)
16317 qscope = global_namespace;
16318 else if (UNSCOPED_ENUM_P (qscope))
16319 qscope = CP_TYPE_CONTEXT (qscope);
16321 if (access_declaration_p && cp_parser_error_occurred (parser))
16322 /* Something has already gone wrong; there's no need to parse
16323 further. Since an error has occurred, the return value of
16324 cp_parser_parse_definitely will be false, as required. */
16325 return cp_parser_parse_definitely (parser);
16327 token = cp_lexer_peek_token (parser->lexer);
16328 /* Parse the unqualified-id. */
16329 identifier = cp_parser_unqualified_id (parser,
16330 /*template_keyword_p=*/false,
16331 /*check_dependency_p=*/true,
16332 /*declarator_p=*/true,
16333 /*optional_p=*/false);
16335 if (access_declaration_p)
16337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16338 cp_parser_simulate_error (parser);
16339 if (!cp_parser_parse_definitely (parser))
16340 return false;
16343 /* The function we call to handle a using-declaration is different
16344 depending on what scope we are in. */
16345 if (qscope == error_mark_node || identifier == error_mark_node)
16347 else if (!identifier_p (identifier)
16348 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16349 /* [namespace.udecl]
16351 A using declaration shall not name a template-id. */
16352 error_at (token->location,
16353 "a template-id may not appear in a using-declaration");
16354 else
16356 if (at_class_scope_p ())
16358 /* Create the USING_DECL. */
16359 decl = do_class_using_decl (parser->scope, identifier);
16361 if (decl && typename_p)
16362 USING_DECL_TYPENAME_P (decl) = 1;
16364 if (check_for_bare_parameter_packs (decl))
16366 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16367 return false;
16369 else
16370 /* Add it to the list of members in this class. */
16371 finish_member_declaration (decl);
16373 else
16375 decl = cp_parser_lookup_name_simple (parser,
16376 identifier,
16377 token->location);
16378 if (decl == error_mark_node)
16379 cp_parser_name_lookup_error (parser, identifier,
16380 decl, NLE_NULL,
16381 token->location);
16382 else if (check_for_bare_parameter_packs (decl))
16384 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16385 return false;
16387 else if (!at_namespace_scope_p ())
16388 do_local_using_decl (decl, qscope, identifier);
16389 else
16390 do_toplevel_using_decl (decl, qscope, identifier);
16394 /* Look for the final `;'. */
16395 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16397 if (access_declaration_p && errorcount == oldcount)
16398 warning_at (diag_token->location, OPT_Wdeprecated,
16399 "access declarations are deprecated "
16400 "in favour of using-declarations; "
16401 "suggestion: add the %<using%> keyword");
16403 return true;
16406 /* Parse an alias-declaration.
16408 alias-declaration:
16409 using identifier attribute-specifier-seq [opt] = type-id */
16411 static tree
16412 cp_parser_alias_declaration (cp_parser* parser)
16414 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16415 location_t id_location;
16416 cp_declarator *declarator;
16417 cp_decl_specifier_seq decl_specs;
16418 bool member_p;
16419 const char *saved_message = NULL;
16421 /* Look for the `using' keyword. */
16422 cp_token *using_token
16423 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16424 if (using_token == NULL)
16425 return error_mark_node;
16427 id_location = cp_lexer_peek_token (parser->lexer)->location;
16428 id = cp_parser_identifier (parser);
16429 if (id == error_mark_node)
16430 return error_mark_node;
16432 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16433 attributes = cp_parser_attributes_opt (parser);
16434 if (attributes == error_mark_node)
16435 return error_mark_node;
16437 cp_parser_require (parser, CPP_EQ, RT_EQ);
16439 if (cp_parser_error_occurred (parser))
16440 return error_mark_node;
16442 cp_parser_commit_to_tentative_parse (parser);
16444 /* Now we are going to parse the type-id of the declaration. */
16447 [dcl.type]/3 says:
16449 "A type-specifier-seq shall not define a class or enumeration
16450 unless it appears in the type-id of an alias-declaration (7.1.3) that
16451 is not the declaration of a template-declaration."
16453 In other words, if we currently are in an alias template, the
16454 type-id should not define a type.
16456 So let's set parser->type_definition_forbidden_message in that
16457 case; cp_parser_check_type_definition (called by
16458 cp_parser_class_specifier) will then emit an error if a type is
16459 defined in the type-id. */
16460 if (parser->num_template_parameter_lists)
16462 saved_message = parser->type_definition_forbidden_message;
16463 parser->type_definition_forbidden_message =
16464 G_("types may not be defined in alias template declarations");
16467 type = cp_parser_type_id (parser);
16469 /* Restore the error message if need be. */
16470 if (parser->num_template_parameter_lists)
16471 parser->type_definition_forbidden_message = saved_message;
16473 if (type == error_mark_node
16474 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16476 cp_parser_skip_to_end_of_block_or_statement (parser);
16477 return error_mark_node;
16480 /* A typedef-name can also be introduced by an alias-declaration. The
16481 identifier following the using keyword becomes a typedef-name. It has
16482 the same semantics as if it were introduced by the typedef
16483 specifier. In particular, it does not define a new type and it shall
16484 not appear in the type-id. */
16486 clear_decl_specs (&decl_specs);
16487 decl_specs.type = type;
16488 if (attributes != NULL_TREE)
16490 decl_specs.attributes = attributes;
16491 set_and_check_decl_spec_loc (&decl_specs,
16492 ds_attribute,
16493 attrs_token);
16495 set_and_check_decl_spec_loc (&decl_specs,
16496 ds_typedef,
16497 using_token);
16498 set_and_check_decl_spec_loc (&decl_specs,
16499 ds_alias,
16500 using_token);
16502 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16503 declarator->id_loc = id_location;
16505 member_p = at_class_scope_p ();
16506 if (member_p)
16507 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16508 NULL_TREE, attributes);
16509 else
16510 decl = start_decl (declarator, &decl_specs, 0,
16511 attributes, NULL_TREE, &pushed_scope);
16512 if (decl == error_mark_node)
16513 return decl;
16515 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16517 if (pushed_scope)
16518 pop_scope (pushed_scope);
16520 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16521 added into the symbol table; otherwise, return the TYPE_DECL. */
16522 if (DECL_LANG_SPECIFIC (decl)
16523 && DECL_TEMPLATE_INFO (decl)
16524 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16526 decl = DECL_TI_TEMPLATE (decl);
16527 if (member_p)
16528 check_member_template (decl);
16531 return decl;
16534 /* Parse a using-directive.
16536 using-directive:
16537 using namespace :: [opt] nested-name-specifier [opt]
16538 namespace-name ; */
16540 static void
16541 cp_parser_using_directive (cp_parser* parser)
16543 tree namespace_decl;
16544 tree attribs;
16546 /* Look for the `using' keyword. */
16547 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16548 /* And the `namespace' keyword. */
16549 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16550 /* Look for the optional `::' operator. */
16551 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16552 /* And the optional nested-name-specifier. */
16553 cp_parser_nested_name_specifier_opt (parser,
16554 /*typename_keyword_p=*/false,
16555 /*check_dependency_p=*/true,
16556 /*type_p=*/false,
16557 /*is_declaration=*/true);
16558 /* Get the namespace being used. */
16559 namespace_decl = cp_parser_namespace_name (parser);
16560 /* And any specified attributes. */
16561 attribs = cp_parser_attributes_opt (parser);
16562 /* Update the symbol table. */
16563 parse_using_directive (namespace_decl, attribs);
16564 /* Look for the final `;'. */
16565 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16568 /* Parse an asm-definition.
16570 asm-definition:
16571 asm ( string-literal ) ;
16573 GNU Extension:
16575 asm-definition:
16576 asm volatile [opt] ( string-literal ) ;
16577 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16578 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16579 : asm-operand-list [opt] ) ;
16580 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16581 : asm-operand-list [opt]
16582 : asm-clobber-list [opt] ) ;
16583 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16584 : asm-clobber-list [opt]
16585 : asm-goto-list ) ; */
16587 static void
16588 cp_parser_asm_definition (cp_parser* parser)
16590 tree string;
16591 tree outputs = NULL_TREE;
16592 tree inputs = NULL_TREE;
16593 tree clobbers = NULL_TREE;
16594 tree labels = NULL_TREE;
16595 tree asm_stmt;
16596 bool volatile_p = false;
16597 bool extended_p = false;
16598 bool invalid_inputs_p = false;
16599 bool invalid_outputs_p = false;
16600 bool goto_p = false;
16601 required_token missing = RT_NONE;
16603 /* Look for the `asm' keyword. */
16604 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16606 if (parser->in_function_body
16607 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16609 error ("%<asm%> in %<constexpr%> function");
16610 cp_function_chain->invalid_constexpr = true;
16613 /* See if the next token is `volatile'. */
16614 if (cp_parser_allow_gnu_extensions_p (parser)
16615 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16617 /* Remember that we saw the `volatile' keyword. */
16618 volatile_p = true;
16619 /* Consume the token. */
16620 cp_lexer_consume_token (parser->lexer);
16622 if (cp_parser_allow_gnu_extensions_p (parser)
16623 && parser->in_function_body
16624 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16626 /* Remember that we saw the `goto' keyword. */
16627 goto_p = true;
16628 /* Consume the token. */
16629 cp_lexer_consume_token (parser->lexer);
16631 /* Look for the opening `('. */
16632 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16633 return;
16634 /* Look for the string. */
16635 string = cp_parser_string_literal (parser, false, false);
16636 if (string == error_mark_node)
16638 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16639 /*consume_paren=*/true);
16640 return;
16643 /* If we're allowing GNU extensions, check for the extended assembly
16644 syntax. Unfortunately, the `:' tokens need not be separated by
16645 a space in C, and so, for compatibility, we tolerate that here
16646 too. Doing that means that we have to treat the `::' operator as
16647 two `:' tokens. */
16648 if (cp_parser_allow_gnu_extensions_p (parser)
16649 && parser->in_function_body
16650 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16651 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16653 bool inputs_p = false;
16654 bool clobbers_p = false;
16655 bool labels_p = false;
16657 /* The extended syntax was used. */
16658 extended_p = true;
16660 /* Look for outputs. */
16661 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16663 /* Consume the `:'. */
16664 cp_lexer_consume_token (parser->lexer);
16665 /* Parse the output-operands. */
16666 if (cp_lexer_next_token_is_not (parser->lexer,
16667 CPP_COLON)
16668 && cp_lexer_next_token_is_not (parser->lexer,
16669 CPP_SCOPE)
16670 && cp_lexer_next_token_is_not (parser->lexer,
16671 CPP_CLOSE_PAREN)
16672 && !goto_p)
16673 outputs = cp_parser_asm_operand_list (parser);
16675 if (outputs == error_mark_node)
16676 invalid_outputs_p = true;
16678 /* If the next token is `::', there are no outputs, and the
16679 next token is the beginning of the inputs. */
16680 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16681 /* The inputs are coming next. */
16682 inputs_p = true;
16684 /* Look for inputs. */
16685 if (inputs_p
16686 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16688 /* Consume the `:' or `::'. */
16689 cp_lexer_consume_token (parser->lexer);
16690 /* Parse the output-operands. */
16691 if (cp_lexer_next_token_is_not (parser->lexer,
16692 CPP_COLON)
16693 && cp_lexer_next_token_is_not (parser->lexer,
16694 CPP_SCOPE)
16695 && cp_lexer_next_token_is_not (parser->lexer,
16696 CPP_CLOSE_PAREN))
16697 inputs = cp_parser_asm_operand_list (parser);
16699 if (inputs == error_mark_node)
16700 invalid_inputs_p = true;
16702 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16703 /* The clobbers are coming next. */
16704 clobbers_p = true;
16706 /* Look for clobbers. */
16707 if (clobbers_p
16708 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16710 clobbers_p = true;
16711 /* Consume the `:' or `::'. */
16712 cp_lexer_consume_token (parser->lexer);
16713 /* Parse the clobbers. */
16714 if (cp_lexer_next_token_is_not (parser->lexer,
16715 CPP_COLON)
16716 && cp_lexer_next_token_is_not (parser->lexer,
16717 CPP_CLOSE_PAREN))
16718 clobbers = cp_parser_asm_clobber_list (parser);
16720 else if (goto_p
16721 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16722 /* The labels are coming next. */
16723 labels_p = true;
16725 /* Look for labels. */
16726 if (labels_p
16727 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16729 labels_p = true;
16730 /* Consume the `:' or `::'. */
16731 cp_lexer_consume_token (parser->lexer);
16732 /* Parse the labels. */
16733 labels = cp_parser_asm_label_list (parser);
16736 if (goto_p && !labels_p)
16737 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16739 else if (goto_p)
16740 missing = RT_COLON_SCOPE;
16742 /* Look for the closing `)'. */
16743 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16744 missing ? missing : RT_CLOSE_PAREN))
16745 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16746 /*consume_paren=*/true);
16747 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16749 if (!invalid_inputs_p && !invalid_outputs_p)
16751 /* Create the ASM_EXPR. */
16752 if (parser->in_function_body)
16754 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16755 inputs, clobbers, labels);
16756 /* If the extended syntax was not used, mark the ASM_EXPR. */
16757 if (!extended_p)
16759 tree temp = asm_stmt;
16760 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16761 temp = TREE_OPERAND (temp, 0);
16763 ASM_INPUT_P (temp) = 1;
16766 else
16767 symtab->finalize_toplevel_asm (string);
16771 /* Declarators [gram.dcl.decl] */
16773 /* Parse an init-declarator.
16775 init-declarator:
16776 declarator initializer [opt]
16778 GNU Extension:
16780 init-declarator:
16781 declarator asm-specification [opt] attributes [opt] initializer [opt]
16783 function-definition:
16784 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16785 function-body
16786 decl-specifier-seq [opt] declarator function-try-block
16788 GNU Extension:
16790 function-definition:
16791 __extension__ function-definition
16793 TM Extension:
16795 function-definition:
16796 decl-specifier-seq [opt] declarator function-transaction-block
16798 The DECL_SPECIFIERS apply to this declarator. Returns a
16799 representation of the entity declared. If MEMBER_P is TRUE, then
16800 this declarator appears in a class scope. The new DECL created by
16801 this declarator is returned.
16803 The CHECKS are access checks that should be performed once we know
16804 what entity is being declared (and, therefore, what classes have
16805 befriended it).
16807 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16808 for a function-definition here as well. If the declarator is a
16809 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16810 be TRUE upon return. By that point, the function-definition will
16811 have been completely parsed.
16813 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16814 is FALSE.
16816 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16817 parsed declaration if it is an uninitialized single declarator not followed
16818 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16819 if present, will not be consumed. If returned, this declarator will be
16820 created with SD_INITIALIZED but will not call cp_finish_decl. */
16822 static tree
16823 cp_parser_init_declarator (cp_parser* parser,
16824 cp_decl_specifier_seq *decl_specifiers,
16825 vec<deferred_access_check, va_gc> *checks,
16826 bool function_definition_allowed_p,
16827 bool member_p,
16828 int declares_class_or_enum,
16829 bool* function_definition_p,
16830 tree* maybe_range_for_decl)
16832 cp_token *token = NULL, *asm_spec_start_token = NULL,
16833 *attributes_start_token = NULL;
16834 cp_declarator *declarator;
16835 tree prefix_attributes;
16836 tree attributes = NULL;
16837 tree asm_specification;
16838 tree initializer;
16839 tree decl = NULL_TREE;
16840 tree scope;
16841 int is_initialized;
16842 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16843 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16844 "(...)". */
16845 enum cpp_ttype initialization_kind;
16846 bool is_direct_init = false;
16847 bool is_non_constant_init;
16848 int ctor_dtor_or_conv_p;
16849 bool friend_p = cp_parser_friend_p (decl_specifiers);
16850 tree pushed_scope = NULL_TREE;
16851 bool range_for_decl_p = false;
16852 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16854 /* Gather the attributes that were provided with the
16855 decl-specifiers. */
16856 prefix_attributes = decl_specifiers->attributes;
16858 /* Assume that this is not the declarator for a function
16859 definition. */
16860 if (function_definition_p)
16861 *function_definition_p = false;
16863 /* Default arguments are only permitted for function parameters. */
16864 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16865 parser->default_arg_ok_p = false;
16867 /* Defer access checks while parsing the declarator; we cannot know
16868 what names are accessible until we know what is being
16869 declared. */
16870 resume_deferring_access_checks ();
16872 /* Parse the declarator. */
16873 token = cp_lexer_peek_token (parser->lexer);
16874 declarator
16875 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16876 &ctor_dtor_or_conv_p,
16877 /*parenthesized_p=*/NULL,
16878 member_p, friend_p);
16879 /* Gather up the deferred checks. */
16880 stop_deferring_access_checks ();
16882 parser->default_arg_ok_p = saved_default_arg_ok_p;
16884 /* If the DECLARATOR was erroneous, there's no need to go
16885 further. */
16886 if (declarator == cp_error_declarator)
16887 return error_mark_node;
16889 /* Check that the number of template-parameter-lists is OK. */
16890 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16891 token->location))
16892 return error_mark_node;
16894 if (declares_class_or_enum & 2)
16895 cp_parser_check_for_definition_in_return_type (declarator,
16896 decl_specifiers->type,
16897 decl_specifiers->locations[ds_type_spec]);
16899 /* Figure out what scope the entity declared by the DECLARATOR is
16900 located in. `grokdeclarator' sometimes changes the scope, so
16901 we compute it now. */
16902 scope = get_scope_of_declarator (declarator);
16904 /* Perform any lookups in the declared type which were thought to be
16905 dependent, but are not in the scope of the declarator. */
16906 decl_specifiers->type
16907 = maybe_update_decl_type (decl_specifiers->type, scope);
16909 /* If we're allowing GNU extensions, look for an
16910 asm-specification. */
16911 if (cp_parser_allow_gnu_extensions_p (parser))
16913 /* Look for an asm-specification. */
16914 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16915 asm_specification = cp_parser_asm_specification_opt (parser);
16917 else
16918 asm_specification = NULL_TREE;
16920 /* Look for attributes. */
16921 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16922 attributes = cp_parser_attributes_opt (parser);
16924 /* Peek at the next token. */
16925 token = cp_lexer_peek_token (parser->lexer);
16927 bool bogus_implicit_tmpl = false;
16929 if (function_declarator_p (declarator))
16931 /* Check to see if the token indicates the start of a
16932 function-definition. */
16933 if (cp_parser_token_starts_function_definition_p (token))
16935 if (!function_definition_allowed_p)
16937 /* If a function-definition should not appear here, issue an
16938 error message. */
16939 cp_parser_error (parser,
16940 "a function-definition is not allowed here");
16941 return error_mark_node;
16944 location_t func_brace_location
16945 = cp_lexer_peek_token (parser->lexer)->location;
16947 /* Neither attributes nor an asm-specification are allowed
16948 on a function-definition. */
16949 if (asm_specification)
16950 error_at (asm_spec_start_token->location,
16951 "an asm-specification is not allowed "
16952 "on a function-definition");
16953 if (attributes)
16954 error_at (attributes_start_token->location,
16955 "attributes are not allowed "
16956 "on a function-definition");
16957 /* This is a function-definition. */
16958 *function_definition_p = true;
16960 /* Parse the function definition. */
16961 if (member_p)
16962 decl = cp_parser_save_member_function_body (parser,
16963 decl_specifiers,
16964 declarator,
16965 prefix_attributes);
16966 else
16967 decl =
16968 (cp_parser_function_definition_from_specifiers_and_declarator
16969 (parser, decl_specifiers, prefix_attributes, declarator));
16971 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16973 /* This is where the prologue starts... */
16974 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16975 = func_brace_location;
16978 return decl;
16981 else if (parser->fully_implicit_function_template_p)
16983 /* A non-template declaration involving a function parameter list
16984 containing an implicit template parameter will be made into a
16985 template. If the resulting declaration is not going to be an
16986 actual function then finish the template scope here to prevent it.
16987 An error message will be issued once we have a decl to talk about.
16989 FIXME probably we should do type deduction rather than create an
16990 implicit template, but the standard currently doesn't allow it. */
16991 bogus_implicit_tmpl = true;
16992 finish_fully_implicit_template (parser, NULL_TREE);
16995 /* [dcl.dcl]
16997 Only in function declarations for constructors, destructors, and
16998 type conversions can the decl-specifier-seq be omitted.
17000 We explicitly postpone this check past the point where we handle
17001 function-definitions because we tolerate function-definitions
17002 that are missing their return types in some modes. */
17003 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17005 cp_parser_error (parser,
17006 "expected constructor, destructor, or type conversion");
17007 return error_mark_node;
17010 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17011 if (token->type == CPP_EQ
17012 || token->type == CPP_OPEN_PAREN
17013 || token->type == CPP_OPEN_BRACE)
17015 is_initialized = SD_INITIALIZED;
17016 initialization_kind = token->type;
17017 if (maybe_range_for_decl)
17018 *maybe_range_for_decl = error_mark_node;
17020 if (token->type == CPP_EQ
17021 && function_declarator_p (declarator))
17023 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17024 if (t2->keyword == RID_DEFAULT)
17025 is_initialized = SD_DEFAULTED;
17026 else if (t2->keyword == RID_DELETE)
17027 is_initialized = SD_DELETED;
17030 else
17032 /* If the init-declarator isn't initialized and isn't followed by a
17033 `,' or `;', it's not a valid init-declarator. */
17034 if (token->type != CPP_COMMA
17035 && token->type != CPP_SEMICOLON)
17037 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17038 range_for_decl_p = true;
17039 else
17041 cp_parser_error (parser, "expected initializer");
17042 return error_mark_node;
17045 is_initialized = SD_UNINITIALIZED;
17046 initialization_kind = CPP_EOF;
17049 /* Because start_decl has side-effects, we should only call it if we
17050 know we're going ahead. By this point, we know that we cannot
17051 possibly be looking at any other construct. */
17052 cp_parser_commit_to_tentative_parse (parser);
17054 /* Enter the newly declared entry in the symbol table. If we're
17055 processing a declaration in a class-specifier, we wait until
17056 after processing the initializer. */
17057 if (!member_p)
17059 if (parser->in_unbraced_linkage_specification_p)
17060 decl_specifiers->storage_class = sc_extern;
17061 decl = start_decl (declarator, decl_specifiers,
17062 range_for_decl_p? SD_INITIALIZED : is_initialized,
17063 attributes, prefix_attributes, &pushed_scope);
17064 cp_finalize_omp_declare_simd (parser, decl);
17065 /* Adjust location of decl if declarator->id_loc is more appropriate:
17066 set, and decl wasn't merged with another decl, in which case its
17067 location would be different from input_location, and more accurate. */
17068 if (DECL_P (decl)
17069 && declarator->id_loc != UNKNOWN_LOCATION
17070 && DECL_SOURCE_LOCATION (decl) == input_location)
17071 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17073 else if (scope)
17074 /* Enter the SCOPE. That way unqualified names appearing in the
17075 initializer will be looked up in SCOPE. */
17076 pushed_scope = push_scope (scope);
17078 /* Perform deferred access control checks, now that we know in which
17079 SCOPE the declared entity resides. */
17080 if (!member_p && decl)
17082 tree saved_current_function_decl = NULL_TREE;
17084 /* If the entity being declared is a function, pretend that we
17085 are in its scope. If it is a `friend', it may have access to
17086 things that would not otherwise be accessible. */
17087 if (TREE_CODE (decl) == FUNCTION_DECL)
17089 saved_current_function_decl = current_function_decl;
17090 current_function_decl = decl;
17093 /* Perform access checks for template parameters. */
17094 cp_parser_perform_template_parameter_access_checks (checks);
17096 /* Perform the access control checks for the declarator and the
17097 decl-specifiers. */
17098 perform_deferred_access_checks (tf_warning_or_error);
17100 /* Restore the saved value. */
17101 if (TREE_CODE (decl) == FUNCTION_DECL)
17102 current_function_decl = saved_current_function_decl;
17105 /* Parse the initializer. */
17106 initializer = NULL_TREE;
17107 is_direct_init = false;
17108 is_non_constant_init = true;
17109 if (is_initialized)
17111 if (function_declarator_p (declarator))
17113 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17114 if (initialization_kind == CPP_EQ)
17115 initializer = cp_parser_pure_specifier (parser);
17116 else
17118 /* If the declaration was erroneous, we don't really
17119 know what the user intended, so just silently
17120 consume the initializer. */
17121 if (decl != error_mark_node)
17122 error_at (initializer_start_token->location,
17123 "initializer provided for function");
17124 cp_parser_skip_to_closing_parenthesis (parser,
17125 /*recovering=*/true,
17126 /*or_comma=*/false,
17127 /*consume_paren=*/true);
17130 else
17132 /* We want to record the extra mangling scope for in-class
17133 initializers of class members and initializers of static data
17134 member templates. The former involves deferring
17135 parsing of the initializer until end of class as with default
17136 arguments. So right here we only handle the latter. */
17137 if (!member_p && processing_template_decl)
17138 start_lambda_scope (decl);
17139 initializer = cp_parser_initializer (parser,
17140 &is_direct_init,
17141 &is_non_constant_init);
17142 if (!member_p && processing_template_decl)
17143 finish_lambda_scope ();
17144 if (initializer == error_mark_node)
17145 cp_parser_skip_to_end_of_statement (parser);
17149 /* The old parser allows attributes to appear after a parenthesized
17150 initializer. Mark Mitchell proposed removing this functionality
17151 on the GCC mailing lists on 2002-08-13. This parser accepts the
17152 attributes -- but ignores them. */
17153 if (cp_parser_allow_gnu_extensions_p (parser)
17154 && initialization_kind == CPP_OPEN_PAREN)
17155 if (cp_parser_attributes_opt (parser))
17156 warning (OPT_Wattributes,
17157 "attributes after parenthesized initializer ignored");
17159 /* And now complain about a non-function implicit template. */
17160 if (bogus_implicit_tmpl)
17161 error_at (DECL_SOURCE_LOCATION (decl),
17162 "non-function %qD declared as implicit template", decl);
17164 /* For an in-class declaration, use `grokfield' to create the
17165 declaration. */
17166 if (member_p)
17168 if (pushed_scope)
17170 pop_scope (pushed_scope);
17171 pushed_scope = NULL_TREE;
17173 decl = grokfield (declarator, decl_specifiers,
17174 initializer, !is_non_constant_init,
17175 /*asmspec=*/NULL_TREE,
17176 chainon (attributes, prefix_attributes));
17177 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17178 cp_parser_save_default_args (parser, decl);
17179 cp_finalize_omp_declare_simd (parser, decl);
17182 /* Finish processing the declaration. But, skip member
17183 declarations. */
17184 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17186 cp_finish_decl (decl,
17187 initializer, !is_non_constant_init,
17188 asm_specification,
17189 /* If the initializer is in parentheses, then this is
17190 a direct-initialization, which means that an
17191 `explicit' constructor is OK. Otherwise, an
17192 `explicit' constructor cannot be used. */
17193 ((is_direct_init || !is_initialized)
17194 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17196 else if ((cxx_dialect != cxx98) && friend_p
17197 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17198 /* Core issue #226 (C++0x only): A default template-argument
17199 shall not be specified in a friend class template
17200 declaration. */
17201 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17202 /*is_partial=*/false, /*is_friend_decl=*/1);
17204 if (!friend_p && pushed_scope)
17205 pop_scope (pushed_scope);
17207 if (function_declarator_p (declarator)
17208 && parser->fully_implicit_function_template_p)
17210 if (member_p)
17211 decl = finish_fully_implicit_template (parser, decl);
17212 else
17213 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17216 return decl;
17219 /* Parse a declarator.
17221 declarator:
17222 direct-declarator
17223 ptr-operator declarator
17225 abstract-declarator:
17226 ptr-operator abstract-declarator [opt]
17227 direct-abstract-declarator
17229 GNU Extensions:
17231 declarator:
17232 attributes [opt] direct-declarator
17233 attributes [opt] ptr-operator declarator
17235 abstract-declarator:
17236 attributes [opt] ptr-operator abstract-declarator [opt]
17237 attributes [opt] direct-abstract-declarator
17239 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17240 detect constructor, destructor or conversion operators. It is set
17241 to -1 if the declarator is a name, and +1 if it is a
17242 function. Otherwise it is set to zero. Usually you just want to
17243 test for >0, but internally the negative value is used.
17245 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17246 a decl-specifier-seq unless it declares a constructor, destructor,
17247 or conversion. It might seem that we could check this condition in
17248 semantic analysis, rather than parsing, but that makes it difficult
17249 to handle something like `f()'. We want to notice that there are
17250 no decl-specifiers, and therefore realize that this is an
17251 expression, not a declaration.)
17253 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17254 the declarator is a direct-declarator of the form "(...)".
17256 MEMBER_P is true iff this declarator is a member-declarator.
17258 FRIEND_P is true iff this declarator is a friend. */
17260 static cp_declarator *
17261 cp_parser_declarator (cp_parser* parser,
17262 cp_parser_declarator_kind dcl_kind,
17263 int* ctor_dtor_or_conv_p,
17264 bool* parenthesized_p,
17265 bool member_p, bool friend_p)
17267 cp_declarator *declarator;
17268 enum tree_code code;
17269 cp_cv_quals cv_quals;
17270 tree class_type;
17271 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17273 /* Assume this is not a constructor, destructor, or type-conversion
17274 operator. */
17275 if (ctor_dtor_or_conv_p)
17276 *ctor_dtor_or_conv_p = 0;
17278 if (cp_parser_allow_gnu_extensions_p (parser))
17279 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17281 /* Check for the ptr-operator production. */
17282 cp_parser_parse_tentatively (parser);
17283 /* Parse the ptr-operator. */
17284 code = cp_parser_ptr_operator (parser,
17285 &class_type,
17286 &cv_quals,
17287 &std_attributes);
17289 /* If that worked, then we have a ptr-operator. */
17290 if (cp_parser_parse_definitely (parser))
17292 /* If a ptr-operator was found, then this declarator was not
17293 parenthesized. */
17294 if (parenthesized_p)
17295 *parenthesized_p = true;
17296 /* The dependent declarator is optional if we are parsing an
17297 abstract-declarator. */
17298 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17299 cp_parser_parse_tentatively (parser);
17301 /* Parse the dependent declarator. */
17302 declarator = cp_parser_declarator (parser, dcl_kind,
17303 /*ctor_dtor_or_conv_p=*/NULL,
17304 /*parenthesized_p=*/NULL,
17305 /*member_p=*/false,
17306 friend_p);
17308 /* If we are parsing an abstract-declarator, we must handle the
17309 case where the dependent declarator is absent. */
17310 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17311 && !cp_parser_parse_definitely (parser))
17312 declarator = NULL;
17314 declarator = cp_parser_make_indirect_declarator
17315 (code, class_type, cv_quals, declarator, std_attributes);
17317 /* Everything else is a direct-declarator. */
17318 else
17320 if (parenthesized_p)
17321 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17322 CPP_OPEN_PAREN);
17323 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17324 ctor_dtor_or_conv_p,
17325 member_p, friend_p);
17328 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17329 declarator->attributes = gnu_attributes;
17330 return declarator;
17333 /* Parse a direct-declarator or direct-abstract-declarator.
17335 direct-declarator:
17336 declarator-id
17337 direct-declarator ( parameter-declaration-clause )
17338 cv-qualifier-seq [opt]
17339 ref-qualifier [opt]
17340 exception-specification [opt]
17341 direct-declarator [ constant-expression [opt] ]
17342 ( declarator )
17344 direct-abstract-declarator:
17345 direct-abstract-declarator [opt]
17346 ( parameter-declaration-clause )
17347 cv-qualifier-seq [opt]
17348 ref-qualifier [opt]
17349 exception-specification [opt]
17350 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17351 ( abstract-declarator )
17353 Returns a representation of the declarator. DCL_KIND is
17354 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17355 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17356 we are parsing a direct-declarator. It is
17357 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17358 of ambiguity we prefer an abstract declarator, as per
17359 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17360 as for cp_parser_declarator. */
17362 static cp_declarator *
17363 cp_parser_direct_declarator (cp_parser* parser,
17364 cp_parser_declarator_kind dcl_kind,
17365 int* ctor_dtor_or_conv_p,
17366 bool member_p, bool friend_p)
17368 cp_token *token;
17369 cp_declarator *declarator = NULL;
17370 tree scope = NULL_TREE;
17371 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17372 bool saved_in_declarator_p = parser->in_declarator_p;
17373 bool first = true;
17374 tree pushed_scope = NULL_TREE;
17376 while (true)
17378 /* Peek at the next token. */
17379 token = cp_lexer_peek_token (parser->lexer);
17380 if (token->type == CPP_OPEN_PAREN)
17382 /* This is either a parameter-declaration-clause, or a
17383 parenthesized declarator. When we know we are parsing a
17384 named declarator, it must be a parenthesized declarator
17385 if FIRST is true. For instance, `(int)' is a
17386 parameter-declaration-clause, with an omitted
17387 direct-abstract-declarator. But `((*))', is a
17388 parenthesized abstract declarator. Finally, when T is a
17389 template parameter `(T)' is a
17390 parameter-declaration-clause, and not a parenthesized
17391 named declarator.
17393 We first try and parse a parameter-declaration-clause,
17394 and then try a nested declarator (if FIRST is true).
17396 It is not an error for it not to be a
17397 parameter-declaration-clause, even when FIRST is
17398 false. Consider,
17400 int i (int);
17401 int i (3);
17403 The first is the declaration of a function while the
17404 second is the definition of a variable, including its
17405 initializer.
17407 Having seen only the parenthesis, we cannot know which of
17408 these two alternatives should be selected. Even more
17409 complex are examples like:
17411 int i (int (a));
17412 int i (int (3));
17414 The former is a function-declaration; the latter is a
17415 variable initialization.
17417 Thus again, we try a parameter-declaration-clause, and if
17418 that fails, we back out and return. */
17420 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17422 tree params;
17423 bool is_declarator = false;
17425 /* In a member-declarator, the only valid interpretation
17426 of a parenthesis is the start of a
17427 parameter-declaration-clause. (It is invalid to
17428 initialize a static data member with a parenthesized
17429 initializer; only the "=" form of initialization is
17430 permitted.) */
17431 if (!member_p)
17432 cp_parser_parse_tentatively (parser);
17434 /* Consume the `('. */
17435 cp_lexer_consume_token (parser->lexer);
17436 if (first)
17438 /* If this is going to be an abstract declarator, we're
17439 in a declarator and we can't have default args. */
17440 parser->default_arg_ok_p = false;
17441 parser->in_declarator_p = true;
17444 begin_scope (sk_function_parms, NULL_TREE);
17446 /* Parse the parameter-declaration-clause. */
17447 params = cp_parser_parameter_declaration_clause (parser);
17449 /* Consume the `)'. */
17450 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17452 /* If all went well, parse the cv-qualifier-seq,
17453 ref-qualifier and the exception-specification. */
17454 if (member_p || cp_parser_parse_definitely (parser))
17456 cp_cv_quals cv_quals;
17457 cp_virt_specifiers virt_specifiers;
17458 cp_ref_qualifier ref_qual;
17459 tree exception_specification;
17460 tree late_return;
17461 tree attrs;
17462 bool memfn = (member_p || (pushed_scope
17463 && CLASS_TYPE_P (pushed_scope)));
17465 is_declarator = true;
17467 if (ctor_dtor_or_conv_p)
17468 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17469 first = false;
17471 /* Parse the cv-qualifier-seq. */
17472 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17473 /* Parse the ref-qualifier. */
17474 ref_qual = cp_parser_ref_qualifier_opt (parser);
17475 /* And the exception-specification. */
17476 exception_specification
17477 = cp_parser_exception_specification_opt (parser);
17479 attrs = cp_parser_std_attribute_spec_seq (parser);
17481 /* In here, we handle cases where attribute is used after
17482 the function declaration. For example:
17483 void func (int x) __attribute__((vector(..))); */
17484 if (flag_cilkplus
17485 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17487 cp_parser_parse_tentatively (parser);
17488 tree attr = cp_parser_gnu_attributes_opt (parser);
17489 if (cp_lexer_next_token_is_not (parser->lexer,
17490 CPP_SEMICOLON)
17491 && cp_lexer_next_token_is_not (parser->lexer,
17492 CPP_OPEN_BRACE))
17493 cp_parser_abort_tentative_parse (parser);
17494 else if (!cp_parser_parse_definitely (parser))
17496 else
17497 attrs = chainon (attr, attrs);
17499 late_return = (cp_parser_late_return_type_opt
17500 (parser, declarator,
17501 memfn ? cv_quals : -1));
17504 /* Parse the virt-specifier-seq. */
17505 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17507 /* Create the function-declarator. */
17508 declarator = make_call_declarator (declarator,
17509 params,
17510 cv_quals,
17511 virt_specifiers,
17512 ref_qual,
17513 exception_specification,
17514 late_return);
17515 declarator->std_attributes = attrs;
17516 /* Any subsequent parameter lists are to do with
17517 return type, so are not those of the declared
17518 function. */
17519 parser->default_arg_ok_p = false;
17522 /* Remove the function parms from scope. */
17523 pop_bindings_and_leave_scope ();
17525 if (is_declarator)
17526 /* Repeat the main loop. */
17527 continue;
17530 /* If this is the first, we can try a parenthesized
17531 declarator. */
17532 if (first)
17534 bool saved_in_type_id_in_expr_p;
17536 parser->default_arg_ok_p = saved_default_arg_ok_p;
17537 parser->in_declarator_p = saved_in_declarator_p;
17539 /* Consume the `('. */
17540 cp_lexer_consume_token (parser->lexer);
17541 /* Parse the nested declarator. */
17542 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17543 parser->in_type_id_in_expr_p = true;
17544 declarator
17545 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17546 /*parenthesized_p=*/NULL,
17547 member_p, friend_p);
17548 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17549 first = false;
17550 /* Expect a `)'. */
17551 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17552 declarator = cp_error_declarator;
17553 if (declarator == cp_error_declarator)
17554 break;
17556 goto handle_declarator;
17558 /* Otherwise, we must be done. */
17559 else
17560 break;
17562 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17563 && token->type == CPP_OPEN_SQUARE
17564 && !cp_next_tokens_can_be_attribute_p (parser))
17566 /* Parse an array-declarator. */
17567 tree bounds, attrs;
17569 if (ctor_dtor_or_conv_p)
17570 *ctor_dtor_or_conv_p = 0;
17572 first = false;
17573 parser->default_arg_ok_p = false;
17574 parser->in_declarator_p = true;
17575 /* Consume the `['. */
17576 cp_lexer_consume_token (parser->lexer);
17577 /* Peek at the next token. */
17578 token = cp_lexer_peek_token (parser->lexer);
17579 /* If the next token is `]', then there is no
17580 constant-expression. */
17581 if (token->type != CPP_CLOSE_SQUARE)
17583 bool non_constant_p;
17584 bounds
17585 = cp_parser_constant_expression (parser,
17586 /*allow_non_constant=*/true,
17587 &non_constant_p);
17588 if (!non_constant_p)
17589 /* OK */;
17590 else if (error_operand_p (bounds))
17591 /* Already gave an error. */;
17592 else if (!parser->in_function_body
17593 || current_binding_level->kind == sk_function_parms)
17595 /* Normally, the array bound must be an integral constant
17596 expression. However, as an extension, we allow VLAs
17597 in function scopes as long as they aren't part of a
17598 parameter declaration. */
17599 cp_parser_error (parser,
17600 "array bound is not an integer constant");
17601 bounds = error_mark_node;
17603 else if (processing_template_decl
17604 && !type_dependent_expression_p (bounds))
17606 /* Remember this wasn't a constant-expression. */
17607 bounds = build_nop (TREE_TYPE (bounds), bounds);
17608 TREE_SIDE_EFFECTS (bounds) = 1;
17611 else
17612 bounds = NULL_TREE;
17613 /* Look for the closing `]'. */
17614 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17616 declarator = cp_error_declarator;
17617 break;
17620 attrs = cp_parser_std_attribute_spec_seq (parser);
17621 declarator = make_array_declarator (declarator, bounds);
17622 declarator->std_attributes = attrs;
17624 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17627 tree qualifying_scope;
17628 tree unqualified_name;
17629 tree attrs;
17630 special_function_kind sfk;
17631 bool abstract_ok;
17632 bool pack_expansion_p = false;
17633 cp_token *declarator_id_start_token;
17635 /* Parse a declarator-id */
17636 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17637 if (abstract_ok)
17639 cp_parser_parse_tentatively (parser);
17641 /* If we see an ellipsis, we should be looking at a
17642 parameter pack. */
17643 if (token->type == CPP_ELLIPSIS)
17645 /* Consume the `...' */
17646 cp_lexer_consume_token (parser->lexer);
17648 pack_expansion_p = true;
17652 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17653 unqualified_name
17654 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17655 qualifying_scope = parser->scope;
17656 if (abstract_ok)
17658 bool okay = false;
17660 if (!unqualified_name && pack_expansion_p)
17662 /* Check whether an error occurred. */
17663 okay = !cp_parser_error_occurred (parser);
17665 /* We already consumed the ellipsis to mark a
17666 parameter pack, but we have no way to report it,
17667 so abort the tentative parse. We will be exiting
17668 immediately anyway. */
17669 cp_parser_abort_tentative_parse (parser);
17671 else
17672 okay = cp_parser_parse_definitely (parser);
17674 if (!okay)
17675 unqualified_name = error_mark_node;
17676 else if (unqualified_name
17677 && (qualifying_scope
17678 || (!identifier_p (unqualified_name))))
17680 cp_parser_error (parser, "expected unqualified-id");
17681 unqualified_name = error_mark_node;
17685 if (!unqualified_name)
17686 return NULL;
17687 if (unqualified_name == error_mark_node)
17689 declarator = cp_error_declarator;
17690 pack_expansion_p = false;
17691 declarator->parameter_pack_p = false;
17692 break;
17695 attrs = cp_parser_std_attribute_spec_seq (parser);
17697 if (qualifying_scope && at_namespace_scope_p ()
17698 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17700 /* In the declaration of a member of a template class
17701 outside of the class itself, the SCOPE will sometimes
17702 be a TYPENAME_TYPE. For example, given:
17704 template <typename T>
17705 int S<T>::R::i = 3;
17707 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17708 this context, we must resolve S<T>::R to an ordinary
17709 type, rather than a typename type.
17711 The reason we normally avoid resolving TYPENAME_TYPEs
17712 is that a specialization of `S' might render
17713 `S<T>::R' not a type. However, if `S' is
17714 specialized, then this `i' will not be used, so there
17715 is no harm in resolving the types here. */
17716 tree type;
17718 /* Resolve the TYPENAME_TYPE. */
17719 type = resolve_typename_type (qualifying_scope,
17720 /*only_current_p=*/false);
17721 /* If that failed, the declarator is invalid. */
17722 if (TREE_CODE (type) == TYPENAME_TYPE)
17724 if (typedef_variant_p (type))
17725 error_at (declarator_id_start_token->location,
17726 "cannot define member of dependent typedef "
17727 "%qT", type);
17728 else
17729 error_at (declarator_id_start_token->location,
17730 "%<%T::%E%> is not a type",
17731 TYPE_CONTEXT (qualifying_scope),
17732 TYPE_IDENTIFIER (qualifying_scope));
17734 qualifying_scope = type;
17737 sfk = sfk_none;
17739 if (unqualified_name)
17741 tree class_type;
17743 if (qualifying_scope
17744 && CLASS_TYPE_P (qualifying_scope))
17745 class_type = qualifying_scope;
17746 else
17747 class_type = current_class_type;
17749 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17751 tree name_type = TREE_TYPE (unqualified_name);
17752 if (class_type && same_type_p (name_type, class_type))
17754 if (qualifying_scope
17755 && CLASSTYPE_USE_TEMPLATE (name_type))
17757 error_at (declarator_id_start_token->location,
17758 "invalid use of constructor as a template");
17759 inform (declarator_id_start_token->location,
17760 "use %<%T::%D%> instead of %<%T::%D%> to "
17761 "name the constructor in a qualified name",
17762 class_type,
17763 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17764 class_type, name_type);
17765 declarator = cp_error_declarator;
17766 break;
17768 else
17769 unqualified_name = constructor_name (class_type);
17771 else
17773 /* We do not attempt to print the declarator
17774 here because we do not have enough
17775 information about its original syntactic
17776 form. */
17777 cp_parser_error (parser, "invalid declarator");
17778 declarator = cp_error_declarator;
17779 break;
17783 if (class_type)
17785 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17786 sfk = sfk_destructor;
17787 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17788 sfk = sfk_conversion;
17789 else if (/* There's no way to declare a constructor
17790 for an anonymous type, even if the type
17791 got a name for linkage purposes. */
17792 !TYPE_WAS_ANONYMOUS (class_type)
17793 /* Handle correctly (c++/19200):
17795 struct S {
17796 struct T{};
17797 friend void S(T);
17800 and also:
17802 namespace N {
17803 void S();
17806 struct S {
17807 friend void N::S();
17808 }; */
17809 && !(friend_p
17810 && class_type != qualifying_scope)
17811 && constructor_name_p (unqualified_name,
17812 class_type))
17814 unqualified_name = constructor_name (class_type);
17815 sfk = sfk_constructor;
17817 else if (is_overloaded_fn (unqualified_name)
17818 && DECL_CONSTRUCTOR_P (get_first_fn
17819 (unqualified_name)))
17820 sfk = sfk_constructor;
17822 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17823 *ctor_dtor_or_conv_p = -1;
17826 declarator = make_id_declarator (qualifying_scope,
17827 unqualified_name,
17828 sfk);
17829 declarator->std_attributes = attrs;
17830 declarator->id_loc = token->location;
17831 declarator->parameter_pack_p = pack_expansion_p;
17833 if (pack_expansion_p)
17834 maybe_warn_variadic_templates ();
17837 handle_declarator:;
17838 scope = get_scope_of_declarator (declarator);
17839 if (scope)
17841 /* Any names that appear after the declarator-id for a
17842 member are looked up in the containing scope. */
17843 if (at_function_scope_p ())
17845 /* But declarations with qualified-ids can't appear in a
17846 function. */
17847 cp_parser_error (parser, "qualified-id in declaration");
17848 declarator = cp_error_declarator;
17849 break;
17851 pushed_scope = push_scope (scope);
17853 parser->in_declarator_p = true;
17854 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17855 || (declarator && declarator->kind == cdk_id))
17856 /* Default args are only allowed on function
17857 declarations. */
17858 parser->default_arg_ok_p = saved_default_arg_ok_p;
17859 else
17860 parser->default_arg_ok_p = false;
17862 first = false;
17864 /* We're done. */
17865 else
17866 break;
17869 /* For an abstract declarator, we might wind up with nothing at this
17870 point. That's an error; the declarator is not optional. */
17871 if (!declarator)
17872 cp_parser_error (parser, "expected declarator");
17874 /* If we entered a scope, we must exit it now. */
17875 if (pushed_scope)
17876 pop_scope (pushed_scope);
17878 parser->default_arg_ok_p = saved_default_arg_ok_p;
17879 parser->in_declarator_p = saved_in_declarator_p;
17881 return declarator;
17884 /* Parse a ptr-operator.
17886 ptr-operator:
17887 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17888 * cv-qualifier-seq [opt]
17890 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17891 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17893 GNU Extension:
17895 ptr-operator:
17896 & cv-qualifier-seq [opt]
17898 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17899 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17900 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17901 filled in with the TYPE containing the member. *CV_QUALS is
17902 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17903 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17904 Note that the tree codes returned by this function have nothing
17905 to do with the types of trees that will be eventually be created
17906 to represent the pointer or reference type being parsed. They are
17907 just constants with suggestive names. */
17908 static enum tree_code
17909 cp_parser_ptr_operator (cp_parser* parser,
17910 tree* type,
17911 cp_cv_quals *cv_quals,
17912 tree *attributes)
17914 enum tree_code code = ERROR_MARK;
17915 cp_token *token;
17916 tree attrs = NULL_TREE;
17918 /* Assume that it's not a pointer-to-member. */
17919 *type = NULL_TREE;
17920 /* And that there are no cv-qualifiers. */
17921 *cv_quals = TYPE_UNQUALIFIED;
17923 /* Peek at the next token. */
17924 token = cp_lexer_peek_token (parser->lexer);
17926 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17927 if (token->type == CPP_MULT)
17928 code = INDIRECT_REF;
17929 else if (token->type == CPP_AND)
17930 code = ADDR_EXPR;
17931 else if ((cxx_dialect != cxx98) &&
17932 token->type == CPP_AND_AND) /* C++0x only */
17933 code = NON_LVALUE_EXPR;
17935 if (code != ERROR_MARK)
17937 /* Consume the `*', `&' or `&&'. */
17938 cp_lexer_consume_token (parser->lexer);
17940 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17941 `&', if we are allowing GNU extensions. (The only qualifier
17942 that can legally appear after `&' is `restrict', but that is
17943 enforced during semantic analysis. */
17944 if (code == INDIRECT_REF
17945 || cp_parser_allow_gnu_extensions_p (parser))
17946 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17948 attrs = cp_parser_std_attribute_spec_seq (parser);
17949 if (attributes != NULL)
17950 *attributes = attrs;
17952 else
17954 /* Try the pointer-to-member case. */
17955 cp_parser_parse_tentatively (parser);
17956 /* Look for the optional `::' operator. */
17957 cp_parser_global_scope_opt (parser,
17958 /*current_scope_valid_p=*/false);
17959 /* Look for the nested-name specifier. */
17960 token = cp_lexer_peek_token (parser->lexer);
17961 cp_parser_nested_name_specifier (parser,
17962 /*typename_keyword_p=*/false,
17963 /*check_dependency_p=*/true,
17964 /*type_p=*/false,
17965 /*is_declaration=*/false);
17966 /* If we found it, and the next token is a `*', then we are
17967 indeed looking at a pointer-to-member operator. */
17968 if (!cp_parser_error_occurred (parser)
17969 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17971 /* Indicate that the `*' operator was used. */
17972 code = INDIRECT_REF;
17974 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17975 error_at (token->location, "%qD is a namespace", parser->scope);
17976 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17977 error_at (token->location, "cannot form pointer to member of "
17978 "non-class %q#T", parser->scope);
17979 else
17981 /* The type of which the member is a member is given by the
17982 current SCOPE. */
17983 *type = parser->scope;
17984 /* The next name will not be qualified. */
17985 parser->scope = NULL_TREE;
17986 parser->qualifying_scope = NULL_TREE;
17987 parser->object_scope = NULL_TREE;
17988 /* Look for optional c++11 attributes. */
17989 attrs = cp_parser_std_attribute_spec_seq (parser);
17990 if (attributes != NULL)
17991 *attributes = attrs;
17992 /* Look for the optional cv-qualifier-seq. */
17993 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17996 /* If that didn't work we don't have a ptr-operator. */
17997 if (!cp_parser_parse_definitely (parser))
17998 cp_parser_error (parser, "expected ptr-operator");
18001 return code;
18004 /* Parse an (optional) cv-qualifier-seq.
18006 cv-qualifier-seq:
18007 cv-qualifier cv-qualifier-seq [opt]
18009 cv-qualifier:
18010 const
18011 volatile
18013 GNU Extension:
18015 cv-qualifier:
18016 __restrict__
18018 Returns a bitmask representing the cv-qualifiers. */
18020 static cp_cv_quals
18021 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18023 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18025 while (true)
18027 cp_token *token;
18028 cp_cv_quals cv_qualifier;
18030 /* Peek at the next token. */
18031 token = cp_lexer_peek_token (parser->lexer);
18032 /* See if it's a cv-qualifier. */
18033 switch (token->keyword)
18035 case RID_CONST:
18036 cv_qualifier = TYPE_QUAL_CONST;
18037 break;
18039 case RID_VOLATILE:
18040 cv_qualifier = TYPE_QUAL_VOLATILE;
18041 break;
18043 case RID_RESTRICT:
18044 cv_qualifier = TYPE_QUAL_RESTRICT;
18045 break;
18047 default:
18048 cv_qualifier = TYPE_UNQUALIFIED;
18049 break;
18052 if (!cv_qualifier)
18053 break;
18055 if (cv_quals & cv_qualifier)
18057 error_at (token->location, "duplicate cv-qualifier");
18058 cp_lexer_purge_token (parser->lexer);
18060 else
18062 cp_lexer_consume_token (parser->lexer);
18063 cv_quals |= cv_qualifier;
18067 return cv_quals;
18070 /* Parse an (optional) ref-qualifier
18072 ref-qualifier:
18076 Returns cp_ref_qualifier representing ref-qualifier. */
18078 static cp_ref_qualifier
18079 cp_parser_ref_qualifier_opt (cp_parser* parser)
18081 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18083 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18084 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18085 return ref_qual;
18087 while (true)
18089 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18090 cp_token *token = cp_lexer_peek_token (parser->lexer);
18092 switch (token->type)
18094 case CPP_AND:
18095 curr_ref_qual = REF_QUAL_LVALUE;
18096 break;
18098 case CPP_AND_AND:
18099 curr_ref_qual = REF_QUAL_RVALUE;
18100 break;
18102 default:
18103 curr_ref_qual = REF_QUAL_NONE;
18104 break;
18107 if (!curr_ref_qual)
18108 break;
18109 else if (ref_qual)
18111 error_at (token->location, "multiple ref-qualifiers");
18112 cp_lexer_purge_token (parser->lexer);
18114 else
18116 ref_qual = curr_ref_qual;
18117 cp_lexer_consume_token (parser->lexer);
18121 return ref_qual;
18124 /* Parse an (optional) virt-specifier-seq.
18126 virt-specifier-seq:
18127 virt-specifier virt-specifier-seq [opt]
18129 virt-specifier:
18130 override
18131 final
18133 Returns a bitmask representing the virt-specifiers. */
18135 static cp_virt_specifiers
18136 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18138 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18140 while (true)
18142 cp_token *token;
18143 cp_virt_specifiers virt_specifier;
18145 /* Peek at the next token. */
18146 token = cp_lexer_peek_token (parser->lexer);
18147 /* See if it's a virt-specifier-qualifier. */
18148 if (token->type != CPP_NAME)
18149 break;
18150 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18152 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18153 virt_specifier = VIRT_SPEC_OVERRIDE;
18155 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18157 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18158 virt_specifier = VIRT_SPEC_FINAL;
18160 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18162 virt_specifier = VIRT_SPEC_FINAL;
18164 else
18165 break;
18167 if (virt_specifiers & virt_specifier)
18169 error_at (token->location, "duplicate virt-specifier");
18170 cp_lexer_purge_token (parser->lexer);
18172 else
18174 cp_lexer_consume_token (parser->lexer);
18175 virt_specifiers |= virt_specifier;
18178 return virt_specifiers;
18181 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18182 is in scope even though it isn't real. */
18184 void
18185 inject_this_parameter (tree ctype, cp_cv_quals quals)
18187 tree this_parm;
18189 if (current_class_ptr)
18191 /* We don't clear this between NSDMIs. Is it already what we want? */
18192 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18193 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18194 && cp_type_quals (type) == quals)
18195 return;
18198 this_parm = build_this_parm (ctype, quals);
18199 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18200 current_class_ptr = NULL_TREE;
18201 current_class_ref
18202 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18203 current_class_ptr = this_parm;
18206 /* Return true iff our current scope is a non-static data member
18207 initializer. */
18209 bool
18210 parsing_nsdmi (void)
18212 /* We recognize NSDMI context by the context-less 'this' pointer set up
18213 by the function above. */
18214 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18215 return true;
18216 return false;
18219 /* Parse a late-specified return type, if any. This is not a separate
18220 non-terminal, but part of a function declarator, which looks like
18222 -> trailing-type-specifier-seq abstract-declarator(opt)
18224 Returns the type indicated by the type-id.
18226 In addition to this this parses any queued up omp declare simd
18227 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18229 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18230 function. */
18232 static tree
18233 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18234 cp_cv_quals quals)
18236 cp_token *token;
18237 tree type = NULL_TREE;
18238 bool declare_simd_p = (parser->omp_declare_simd
18239 && declarator
18240 && declarator->kind == cdk_id);
18242 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18243 && declarator && declarator->kind == cdk_id);
18245 /* Peek at the next token. */
18246 token = cp_lexer_peek_token (parser->lexer);
18247 /* A late-specified return type is indicated by an initial '->'. */
18248 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18249 return NULL_TREE;
18251 tree save_ccp = current_class_ptr;
18252 tree save_ccr = current_class_ref;
18253 if (quals >= 0)
18255 /* DR 1207: 'this' is in scope in the trailing return type. */
18256 inject_this_parameter (current_class_type, quals);
18259 if (token->type == CPP_DEREF)
18261 /* Consume the ->. */
18262 cp_lexer_consume_token (parser->lexer);
18264 type = cp_parser_trailing_type_id (parser);
18267 if (cilk_simd_fn_vector_p)
18268 declarator->std_attributes
18269 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18270 declarator->std_attributes);
18271 if (declare_simd_p)
18272 declarator->std_attributes
18273 = cp_parser_late_parsing_omp_declare_simd (parser,
18274 declarator->std_attributes);
18276 if (quals >= 0)
18278 current_class_ptr = save_ccp;
18279 current_class_ref = save_ccr;
18282 return type;
18285 /* Parse a declarator-id.
18287 declarator-id:
18288 id-expression
18289 :: [opt] nested-name-specifier [opt] type-name
18291 In the `id-expression' case, the value returned is as for
18292 cp_parser_id_expression if the id-expression was an unqualified-id.
18293 If the id-expression was a qualified-id, then a SCOPE_REF is
18294 returned. The first operand is the scope (either a NAMESPACE_DECL
18295 or TREE_TYPE), but the second is still just a representation of an
18296 unqualified-id. */
18298 static tree
18299 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18301 tree id;
18302 /* The expression must be an id-expression. Assume that qualified
18303 names are the names of types so that:
18305 template <class T>
18306 int S<T>::R::i = 3;
18308 will work; we must treat `S<T>::R' as the name of a type.
18309 Similarly, assume that qualified names are templates, where
18310 required, so that:
18312 template <class T>
18313 int S<T>::R<T>::i = 3;
18315 will work, too. */
18316 id = cp_parser_id_expression (parser,
18317 /*template_keyword_p=*/false,
18318 /*check_dependency_p=*/false,
18319 /*template_p=*/NULL,
18320 /*declarator_p=*/true,
18321 optional_p);
18322 if (id && BASELINK_P (id))
18323 id = BASELINK_FUNCTIONS (id);
18324 return id;
18327 /* Parse a type-id.
18329 type-id:
18330 type-specifier-seq abstract-declarator [opt]
18332 Returns the TYPE specified. */
18334 static tree
18335 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18336 bool is_trailing_return)
18338 cp_decl_specifier_seq type_specifier_seq;
18339 cp_declarator *abstract_declarator;
18341 /* Parse the type-specifier-seq. */
18342 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18343 is_trailing_return,
18344 &type_specifier_seq);
18345 if (type_specifier_seq.type == error_mark_node)
18346 return error_mark_node;
18348 /* There might or might not be an abstract declarator. */
18349 cp_parser_parse_tentatively (parser);
18350 /* Look for the declarator. */
18351 abstract_declarator
18352 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18353 /*parenthesized_p=*/NULL,
18354 /*member_p=*/false,
18355 /*friend_p=*/false);
18356 /* Check to see if there really was a declarator. */
18357 if (!cp_parser_parse_definitely (parser))
18358 abstract_declarator = NULL;
18360 if (type_specifier_seq.type
18361 /* None of the valid uses of 'auto' in C++14 involve the type-id
18362 nonterminal, but it is valid in a trailing-return-type. */
18363 && !(cxx_dialect >= cxx14 && is_trailing_return)
18364 && type_uses_auto (type_specifier_seq.type))
18366 /* A type-id with type 'auto' is only ok if the abstract declarator
18367 is a function declarator with a late-specified return type. */
18368 if (abstract_declarator
18369 && abstract_declarator->kind == cdk_function
18370 && abstract_declarator->u.function.late_return_type)
18371 /* OK */;
18372 else
18374 error ("invalid use of %<auto%>");
18375 return error_mark_node;
18379 return groktypename (&type_specifier_seq, abstract_declarator,
18380 is_template_arg);
18383 static tree cp_parser_type_id (cp_parser *parser)
18385 return cp_parser_type_id_1 (parser, false, false);
18388 static tree cp_parser_template_type_arg (cp_parser *parser)
18390 tree r;
18391 const char *saved_message = parser->type_definition_forbidden_message;
18392 parser->type_definition_forbidden_message
18393 = G_("types may not be defined in template arguments");
18394 r = cp_parser_type_id_1 (parser, true, false);
18395 parser->type_definition_forbidden_message = saved_message;
18396 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18398 error ("invalid use of %<auto%> in template argument");
18399 r = error_mark_node;
18401 return r;
18404 static tree cp_parser_trailing_type_id (cp_parser *parser)
18406 return cp_parser_type_id_1 (parser, false, true);
18409 /* Parse a type-specifier-seq.
18411 type-specifier-seq:
18412 type-specifier type-specifier-seq [opt]
18414 GNU extension:
18416 type-specifier-seq:
18417 attributes type-specifier-seq [opt]
18419 If IS_DECLARATION is true, we are at the start of a "condition" or
18420 exception-declaration, so we might be followed by a declarator-id.
18422 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18423 i.e. we've just seen "->".
18425 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18427 static void
18428 cp_parser_type_specifier_seq (cp_parser* parser,
18429 bool is_declaration,
18430 bool is_trailing_return,
18431 cp_decl_specifier_seq *type_specifier_seq)
18433 bool seen_type_specifier = false;
18434 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18435 cp_token *start_token = NULL;
18437 /* Clear the TYPE_SPECIFIER_SEQ. */
18438 clear_decl_specs (type_specifier_seq);
18440 /* In the context of a trailing return type, enum E { } is an
18441 elaborated-type-specifier followed by a function-body, not an
18442 enum-specifier. */
18443 if (is_trailing_return)
18444 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18446 /* Parse the type-specifiers and attributes. */
18447 while (true)
18449 tree type_specifier;
18450 bool is_cv_qualifier;
18452 /* Check for attributes first. */
18453 if (cp_next_tokens_can_be_attribute_p (parser))
18455 type_specifier_seq->attributes =
18456 chainon (type_specifier_seq->attributes,
18457 cp_parser_attributes_opt (parser));
18458 continue;
18461 /* record the token of the beginning of the type specifier seq,
18462 for error reporting purposes*/
18463 if (!start_token)
18464 start_token = cp_lexer_peek_token (parser->lexer);
18466 /* Look for the type-specifier. */
18467 type_specifier = cp_parser_type_specifier (parser,
18468 flags,
18469 type_specifier_seq,
18470 /*is_declaration=*/false,
18471 NULL,
18472 &is_cv_qualifier);
18473 if (!type_specifier)
18475 /* If the first type-specifier could not be found, this is not a
18476 type-specifier-seq at all. */
18477 if (!seen_type_specifier)
18479 /* Set in_declarator_p to avoid skipping to the semicolon. */
18480 int in_decl = parser->in_declarator_p;
18481 parser->in_declarator_p = true;
18483 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18484 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18485 cp_parser_error (parser, "expected type-specifier");
18487 parser->in_declarator_p = in_decl;
18489 type_specifier_seq->type = error_mark_node;
18490 return;
18492 /* If subsequent type-specifiers could not be found, the
18493 type-specifier-seq is complete. */
18494 break;
18497 seen_type_specifier = true;
18498 /* The standard says that a condition can be:
18500 type-specifier-seq declarator = assignment-expression
18502 However, given:
18504 struct S {};
18505 if (int S = ...)
18507 we should treat the "S" as a declarator, not as a
18508 type-specifier. The standard doesn't say that explicitly for
18509 type-specifier-seq, but it does say that for
18510 decl-specifier-seq in an ordinary declaration. Perhaps it
18511 would be clearer just to allow a decl-specifier-seq here, and
18512 then add a semantic restriction that if any decl-specifiers
18513 that are not type-specifiers appear, the program is invalid. */
18514 if (is_declaration && !is_cv_qualifier)
18515 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18519 /* Return whether the function currently being declared has an associated
18520 template parameter list. */
18522 static bool
18523 function_being_declared_is_template_p (cp_parser* parser)
18525 if (!current_template_parms || processing_template_parmlist)
18526 return false;
18528 if (parser->implicit_template_scope)
18529 return true;
18531 if (at_class_scope_p ()
18532 && TYPE_BEING_DEFINED (current_class_type))
18533 return parser->num_template_parameter_lists != 0;
18535 return ((int) parser->num_template_parameter_lists > template_class_depth
18536 (current_class_type));
18539 /* Parse a parameter-declaration-clause.
18541 parameter-declaration-clause:
18542 parameter-declaration-list [opt] ... [opt]
18543 parameter-declaration-list , ...
18545 Returns a representation for the parameter declarations. A return
18546 value of NULL indicates a parameter-declaration-clause consisting
18547 only of an ellipsis. */
18549 static tree
18550 cp_parser_parameter_declaration_clause (cp_parser* parser)
18552 tree parameters;
18553 cp_token *token;
18554 bool ellipsis_p;
18555 bool is_error;
18557 struct cleanup {
18558 cp_parser* parser;
18559 int auto_is_implicit_function_template_parm_p;
18560 ~cleanup() {
18561 parser->auto_is_implicit_function_template_parm_p
18562 = auto_is_implicit_function_template_parm_p;
18564 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18566 (void) cleanup;
18568 if (!processing_specialization
18569 && !processing_template_parmlist
18570 && !processing_explicit_instantiation)
18571 if (!current_function_decl
18572 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18573 parser->auto_is_implicit_function_template_parm_p = true;
18575 /* Peek at the next token. */
18576 token = cp_lexer_peek_token (parser->lexer);
18577 /* Check for trivial parameter-declaration-clauses. */
18578 if (token->type == CPP_ELLIPSIS)
18580 /* Consume the `...' token. */
18581 cp_lexer_consume_token (parser->lexer);
18582 return NULL_TREE;
18584 else if (token->type == CPP_CLOSE_PAREN)
18585 /* There are no parameters. */
18587 #ifndef NO_IMPLICIT_EXTERN_C
18588 if (in_system_header_at (input_location)
18589 && current_class_type == NULL
18590 && current_lang_name == lang_name_c)
18591 return NULL_TREE;
18592 else
18593 #endif
18594 return void_list_node;
18596 /* Check for `(void)', too, which is a special case. */
18597 else if (token->keyword == RID_VOID
18598 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18599 == CPP_CLOSE_PAREN))
18601 /* Consume the `void' token. */
18602 cp_lexer_consume_token (parser->lexer);
18603 /* There are no parameters. */
18604 return void_list_node;
18607 /* Parse the parameter-declaration-list. */
18608 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18609 /* If a parse error occurred while parsing the
18610 parameter-declaration-list, then the entire
18611 parameter-declaration-clause is erroneous. */
18612 if (is_error)
18613 return NULL;
18615 /* Peek at the next token. */
18616 token = cp_lexer_peek_token (parser->lexer);
18617 /* If it's a `,', the clause should terminate with an ellipsis. */
18618 if (token->type == CPP_COMMA)
18620 /* Consume the `,'. */
18621 cp_lexer_consume_token (parser->lexer);
18622 /* Expect an ellipsis. */
18623 ellipsis_p
18624 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18626 /* It might also be `...' if the optional trailing `,' was
18627 omitted. */
18628 else if (token->type == CPP_ELLIPSIS)
18630 /* Consume the `...' token. */
18631 cp_lexer_consume_token (parser->lexer);
18632 /* And remember that we saw it. */
18633 ellipsis_p = true;
18635 else
18636 ellipsis_p = false;
18638 /* Finish the parameter list. */
18639 if (!ellipsis_p)
18640 parameters = chainon (parameters, void_list_node);
18642 return parameters;
18645 /* Parse a parameter-declaration-list.
18647 parameter-declaration-list:
18648 parameter-declaration
18649 parameter-declaration-list , parameter-declaration
18651 Returns a representation of the parameter-declaration-list, as for
18652 cp_parser_parameter_declaration_clause. However, the
18653 `void_list_node' is never appended to the list. Upon return,
18654 *IS_ERROR will be true iff an error occurred. */
18656 static tree
18657 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18659 tree parameters = NULL_TREE;
18660 tree *tail = &parameters;
18661 bool saved_in_unbraced_linkage_specification_p;
18662 int index = 0;
18664 /* Assume all will go well. */
18665 *is_error = false;
18666 /* The special considerations that apply to a function within an
18667 unbraced linkage specifications do not apply to the parameters
18668 to the function. */
18669 saved_in_unbraced_linkage_specification_p
18670 = parser->in_unbraced_linkage_specification_p;
18671 parser->in_unbraced_linkage_specification_p = false;
18673 /* Look for more parameters. */
18674 while (true)
18676 cp_parameter_declarator *parameter;
18677 tree decl = error_mark_node;
18678 bool parenthesized_p = false;
18679 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18680 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18681 (current_template_parms)) : 0);
18683 /* Parse the parameter. */
18684 parameter
18685 = cp_parser_parameter_declaration (parser,
18686 /*template_parm_p=*/false,
18687 &parenthesized_p);
18689 /* We don't know yet if the enclosing context is deprecated, so wait
18690 and warn in grokparms if appropriate. */
18691 deprecated_state = DEPRECATED_SUPPRESS;
18693 if (parameter)
18695 /* If a function parameter pack was specified and an implicit template
18696 parameter was introduced during cp_parser_parameter_declaration,
18697 change any implicit parameters introduced into packs. */
18698 if (parser->implicit_template_parms
18699 && parameter->declarator
18700 && parameter->declarator->parameter_pack_p)
18702 int latest_template_parm_idx = TREE_VEC_LENGTH
18703 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18705 if (latest_template_parm_idx != template_parm_idx)
18706 parameter->decl_specifiers.type = convert_generic_types_to_packs
18707 (parameter->decl_specifiers.type,
18708 template_parm_idx, latest_template_parm_idx);
18711 decl = grokdeclarator (parameter->declarator,
18712 &parameter->decl_specifiers,
18713 PARM,
18714 parameter->default_argument != NULL_TREE,
18715 &parameter->decl_specifiers.attributes);
18718 deprecated_state = DEPRECATED_NORMAL;
18720 /* If a parse error occurred parsing the parameter declaration,
18721 then the entire parameter-declaration-list is erroneous. */
18722 if (decl == error_mark_node)
18724 *is_error = true;
18725 parameters = error_mark_node;
18726 break;
18729 if (parameter->decl_specifiers.attributes)
18730 cplus_decl_attributes (&decl,
18731 parameter->decl_specifiers.attributes,
18733 if (DECL_NAME (decl))
18734 decl = pushdecl (decl);
18736 if (decl != error_mark_node)
18738 retrofit_lang_decl (decl);
18739 DECL_PARM_INDEX (decl) = ++index;
18740 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18743 /* Add the new parameter to the list. */
18744 *tail = build_tree_list (parameter->default_argument, decl);
18745 tail = &TREE_CHAIN (*tail);
18747 /* Peek at the next token. */
18748 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18749 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18750 /* These are for Objective-C++ */
18751 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18752 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18753 /* The parameter-declaration-list is complete. */
18754 break;
18755 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18757 cp_token *token;
18759 /* Peek at the next token. */
18760 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18761 /* If it's an ellipsis, then the list is complete. */
18762 if (token->type == CPP_ELLIPSIS)
18763 break;
18764 /* Otherwise, there must be more parameters. Consume the
18765 `,'. */
18766 cp_lexer_consume_token (parser->lexer);
18767 /* When parsing something like:
18769 int i(float f, double d)
18771 we can tell after seeing the declaration for "f" that we
18772 are not looking at an initialization of a variable "i",
18773 but rather at the declaration of a function "i".
18775 Due to the fact that the parsing of template arguments
18776 (as specified to a template-id) requires backtracking we
18777 cannot use this technique when inside a template argument
18778 list. */
18779 if (!parser->in_template_argument_list_p
18780 && !parser->in_type_id_in_expr_p
18781 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18782 /* However, a parameter-declaration of the form
18783 "float(f)" (which is a valid declaration of a
18784 parameter "f") can also be interpreted as an
18785 expression (the conversion of "f" to "float"). */
18786 && !parenthesized_p)
18787 cp_parser_commit_to_tentative_parse (parser);
18789 else
18791 cp_parser_error (parser, "expected %<,%> or %<...%>");
18792 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18793 cp_parser_skip_to_closing_parenthesis (parser,
18794 /*recovering=*/true,
18795 /*or_comma=*/false,
18796 /*consume_paren=*/false);
18797 break;
18801 parser->in_unbraced_linkage_specification_p
18802 = saved_in_unbraced_linkage_specification_p;
18804 /* Reset implicit_template_scope if we are about to leave the function
18805 parameter list that introduced it. Note that for out-of-line member
18806 definitions, there will be one or more class scopes before we get to
18807 the template parameter scope. */
18809 if (cp_binding_level *its = parser->implicit_template_scope)
18810 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18812 while (maybe_its->kind == sk_class)
18813 maybe_its = maybe_its->level_chain;
18814 if (maybe_its == its)
18816 parser->implicit_template_parms = 0;
18817 parser->implicit_template_scope = 0;
18821 return parameters;
18824 /* Parse a parameter declaration.
18826 parameter-declaration:
18827 decl-specifier-seq ... [opt] declarator
18828 decl-specifier-seq declarator = assignment-expression
18829 decl-specifier-seq ... [opt] abstract-declarator [opt]
18830 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18832 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18833 declares a template parameter. (In that case, a non-nested `>'
18834 token encountered during the parsing of the assignment-expression
18835 is not interpreted as a greater-than operator.)
18837 Returns a representation of the parameter, or NULL if an error
18838 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18839 true iff the declarator is of the form "(p)". */
18841 static cp_parameter_declarator *
18842 cp_parser_parameter_declaration (cp_parser *parser,
18843 bool template_parm_p,
18844 bool *parenthesized_p)
18846 int declares_class_or_enum;
18847 cp_decl_specifier_seq decl_specifiers;
18848 cp_declarator *declarator;
18849 tree default_argument;
18850 cp_token *token = NULL, *declarator_token_start = NULL;
18851 const char *saved_message;
18853 /* In a template parameter, `>' is not an operator.
18855 [temp.param]
18857 When parsing a default template-argument for a non-type
18858 template-parameter, the first non-nested `>' is taken as the end
18859 of the template parameter-list rather than a greater-than
18860 operator. */
18862 /* Type definitions may not appear in parameter types. */
18863 saved_message = parser->type_definition_forbidden_message;
18864 parser->type_definition_forbidden_message
18865 = G_("types may not be defined in parameter types");
18867 /* Parse the declaration-specifiers. */
18868 cp_parser_decl_specifier_seq (parser,
18869 CP_PARSER_FLAGS_NONE,
18870 &decl_specifiers,
18871 &declares_class_or_enum);
18873 /* Complain about missing 'typename' or other invalid type names. */
18874 if (!decl_specifiers.any_type_specifiers_p
18875 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18876 decl_specifiers.type = error_mark_node;
18878 /* If an error occurred, there's no reason to attempt to parse the
18879 rest of the declaration. */
18880 if (cp_parser_error_occurred (parser))
18882 parser->type_definition_forbidden_message = saved_message;
18883 return NULL;
18886 /* Peek at the next token. */
18887 token = cp_lexer_peek_token (parser->lexer);
18889 /* If the next token is a `)', `,', `=', `>', or `...', then there
18890 is no declarator. However, when variadic templates are enabled,
18891 there may be a declarator following `...'. */
18892 if (token->type == CPP_CLOSE_PAREN
18893 || token->type == CPP_COMMA
18894 || token->type == CPP_EQ
18895 || token->type == CPP_GREATER)
18897 declarator = NULL;
18898 if (parenthesized_p)
18899 *parenthesized_p = false;
18901 /* Otherwise, there should be a declarator. */
18902 else
18904 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18905 parser->default_arg_ok_p = false;
18907 /* After seeing a decl-specifier-seq, if the next token is not a
18908 "(", there is no possibility that the code is a valid
18909 expression. Therefore, if parsing tentatively, we commit at
18910 this point. */
18911 if (!parser->in_template_argument_list_p
18912 /* In an expression context, having seen:
18914 (int((char ...
18916 we cannot be sure whether we are looking at a
18917 function-type (taking a "char" as a parameter) or a cast
18918 of some object of type "char" to "int". */
18919 && !parser->in_type_id_in_expr_p
18920 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18921 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18922 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18923 cp_parser_commit_to_tentative_parse (parser);
18924 /* Parse the declarator. */
18925 declarator_token_start = token;
18926 declarator = cp_parser_declarator (parser,
18927 CP_PARSER_DECLARATOR_EITHER,
18928 /*ctor_dtor_or_conv_p=*/NULL,
18929 parenthesized_p,
18930 /*member_p=*/false,
18931 /*friend_p=*/false);
18932 parser->default_arg_ok_p = saved_default_arg_ok_p;
18933 /* After the declarator, allow more attributes. */
18934 decl_specifiers.attributes
18935 = chainon (decl_specifiers.attributes,
18936 cp_parser_attributes_opt (parser));
18939 /* If the next token is an ellipsis, and we have not seen a
18940 declarator name, and the type of the declarator contains parameter
18941 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18942 a parameter pack expansion expression. Otherwise, leave the
18943 ellipsis for a C-style variadic function. */
18944 token = cp_lexer_peek_token (parser->lexer);
18945 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18947 tree type = decl_specifiers.type;
18949 if (type && DECL_P (type))
18950 type = TREE_TYPE (type);
18952 if (type
18953 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18954 && declarator_can_be_parameter_pack (declarator)
18955 && (!declarator || !declarator->parameter_pack_p)
18956 && uses_parameter_packs (type))
18958 /* Consume the `...'. */
18959 cp_lexer_consume_token (parser->lexer);
18960 maybe_warn_variadic_templates ();
18962 /* Build a pack expansion type */
18963 if (declarator)
18964 declarator->parameter_pack_p = true;
18965 else
18966 decl_specifiers.type = make_pack_expansion (type);
18970 /* The restriction on defining new types applies only to the type
18971 of the parameter, not to the default argument. */
18972 parser->type_definition_forbidden_message = saved_message;
18974 /* If the next token is `=', then process a default argument. */
18975 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18977 token = cp_lexer_peek_token (parser->lexer);
18978 /* If we are defining a class, then the tokens that make up the
18979 default argument must be saved and processed later. */
18980 if (!template_parm_p && at_class_scope_p ()
18981 && TYPE_BEING_DEFINED (current_class_type)
18982 && !LAMBDA_TYPE_P (current_class_type))
18983 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18984 /* Outside of a class definition, we can just parse the
18985 assignment-expression. */
18986 else
18987 default_argument
18988 = cp_parser_default_argument (parser, template_parm_p);
18990 if (!parser->default_arg_ok_p)
18992 if (flag_permissive)
18993 warning (0, "deprecated use of default argument for parameter of non-function");
18994 else
18996 error_at (token->location,
18997 "default arguments are only "
18998 "permitted for function parameters");
18999 default_argument = NULL_TREE;
19002 else if ((declarator && declarator->parameter_pack_p)
19003 || (decl_specifiers.type
19004 && PACK_EXPANSION_P (decl_specifiers.type)))
19006 /* Find the name of the parameter pack. */
19007 cp_declarator *id_declarator = declarator;
19008 while (id_declarator && id_declarator->kind != cdk_id)
19009 id_declarator = id_declarator->declarator;
19011 if (id_declarator && id_declarator->kind == cdk_id)
19012 error_at (declarator_token_start->location,
19013 template_parm_p
19014 ? G_("template parameter pack %qD "
19015 "cannot have a default argument")
19016 : G_("parameter pack %qD cannot have "
19017 "a default argument"),
19018 id_declarator->u.id.unqualified_name);
19019 else
19020 error_at (declarator_token_start->location,
19021 template_parm_p
19022 ? G_("template parameter pack cannot have "
19023 "a default argument")
19024 : G_("parameter pack cannot have a "
19025 "default argument"));
19027 default_argument = NULL_TREE;
19030 else
19031 default_argument = NULL_TREE;
19033 return make_parameter_declarator (&decl_specifiers,
19034 declarator,
19035 default_argument);
19038 /* Parse a default argument and return it.
19040 TEMPLATE_PARM_P is true if this is a default argument for a
19041 non-type template parameter. */
19042 static tree
19043 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19045 tree default_argument = NULL_TREE;
19046 bool saved_greater_than_is_operator_p;
19047 bool saved_local_variables_forbidden_p;
19048 bool non_constant_p, is_direct_init;
19050 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19051 set correctly. */
19052 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19053 parser->greater_than_is_operator_p = !template_parm_p;
19054 /* Local variable names (and the `this' keyword) may not
19055 appear in a default argument. */
19056 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19057 parser->local_variables_forbidden_p = true;
19058 /* Parse the assignment-expression. */
19059 if (template_parm_p)
19060 push_deferring_access_checks (dk_no_deferred);
19061 tree saved_class_ptr = NULL_TREE;
19062 tree saved_class_ref = NULL_TREE;
19063 /* The "this" pointer is not valid in a default argument. */
19064 if (cfun)
19066 saved_class_ptr = current_class_ptr;
19067 cp_function_chain->x_current_class_ptr = NULL_TREE;
19068 saved_class_ref = current_class_ref;
19069 cp_function_chain->x_current_class_ref = NULL_TREE;
19071 default_argument
19072 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19073 /* Restore the "this" pointer. */
19074 if (cfun)
19076 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19077 cp_function_chain->x_current_class_ref = saved_class_ref;
19079 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19080 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19081 if (template_parm_p)
19082 pop_deferring_access_checks ();
19083 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19084 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19086 return default_argument;
19089 /* Parse a function-body.
19091 function-body:
19092 compound_statement */
19094 static void
19095 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19097 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19100 /* Parse a ctor-initializer-opt followed by a function-body. Return
19101 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19102 is true we are parsing a function-try-block. */
19104 static bool
19105 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19106 bool in_function_try_block)
19108 tree body, list;
19109 bool ctor_initializer_p;
19110 const bool check_body_p =
19111 DECL_CONSTRUCTOR_P (current_function_decl)
19112 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19113 tree last = NULL;
19115 /* Begin the function body. */
19116 body = begin_function_body ();
19117 /* Parse the optional ctor-initializer. */
19118 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19120 /* If we're parsing a constexpr constructor definition, we need
19121 to check that the constructor body is indeed empty. However,
19122 before we get to cp_parser_function_body lot of junk has been
19123 generated, so we can't just check that we have an empty block.
19124 Rather we take a snapshot of the outermost block, and check whether
19125 cp_parser_function_body changed its state. */
19126 if (check_body_p)
19128 list = cur_stmt_list;
19129 if (STATEMENT_LIST_TAIL (list))
19130 last = STATEMENT_LIST_TAIL (list)->stmt;
19132 /* Parse the function-body. */
19133 cp_parser_function_body (parser, in_function_try_block);
19134 if (check_body_p)
19135 check_constexpr_ctor_body (last, list, /*complain=*/true);
19136 /* Finish the function body. */
19137 finish_function_body (body);
19139 return ctor_initializer_p;
19142 /* Parse an initializer.
19144 initializer:
19145 = initializer-clause
19146 ( expression-list )
19148 Returns an expression representing the initializer. If no
19149 initializer is present, NULL_TREE is returned.
19151 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19152 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19153 set to TRUE if there is no initializer present. If there is an
19154 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19155 is set to true; otherwise it is set to false. */
19157 static tree
19158 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19159 bool* non_constant_p)
19161 cp_token *token;
19162 tree init;
19164 /* Peek at the next token. */
19165 token = cp_lexer_peek_token (parser->lexer);
19167 /* Let our caller know whether or not this initializer was
19168 parenthesized. */
19169 *is_direct_init = (token->type != CPP_EQ);
19170 /* Assume that the initializer is constant. */
19171 *non_constant_p = false;
19173 if (token->type == CPP_EQ)
19175 /* Consume the `='. */
19176 cp_lexer_consume_token (parser->lexer);
19177 /* Parse the initializer-clause. */
19178 init = cp_parser_initializer_clause (parser, non_constant_p);
19180 else if (token->type == CPP_OPEN_PAREN)
19182 vec<tree, va_gc> *vec;
19183 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19184 /*cast_p=*/false,
19185 /*allow_expansion_p=*/true,
19186 non_constant_p);
19187 if (vec == NULL)
19188 return error_mark_node;
19189 init = build_tree_list_vec (vec);
19190 release_tree_vector (vec);
19192 else if (token->type == CPP_OPEN_BRACE)
19194 cp_lexer_set_source_position (parser->lexer);
19195 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19196 init = cp_parser_braced_list (parser, non_constant_p);
19197 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19199 else
19201 /* Anything else is an error. */
19202 cp_parser_error (parser, "expected initializer");
19203 init = error_mark_node;
19206 return init;
19209 /* Parse an initializer-clause.
19211 initializer-clause:
19212 assignment-expression
19213 braced-init-list
19215 Returns an expression representing the initializer.
19217 If the `assignment-expression' production is used the value
19218 returned is simply a representation for the expression.
19220 Otherwise, calls cp_parser_braced_list. */
19222 static tree
19223 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19225 tree initializer;
19227 /* Assume the expression is constant. */
19228 *non_constant_p = false;
19230 /* If it is not a `{', then we are looking at an
19231 assignment-expression. */
19232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19234 initializer
19235 = cp_parser_constant_expression (parser,
19236 /*allow_non_constant_p=*/true,
19237 non_constant_p);
19239 else
19240 initializer = cp_parser_braced_list (parser, non_constant_p);
19242 return initializer;
19245 /* Parse a brace-enclosed initializer list.
19247 braced-init-list:
19248 { initializer-list , [opt] }
19251 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19252 the elements of the initializer-list (or NULL, if the last
19253 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19254 NULL_TREE. There is no way to detect whether or not the optional
19255 trailing `,' was provided. NON_CONSTANT_P is as for
19256 cp_parser_initializer. */
19258 static tree
19259 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19261 tree initializer;
19263 /* Consume the `{' token. */
19264 cp_lexer_consume_token (parser->lexer);
19265 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19266 initializer = make_node (CONSTRUCTOR);
19267 /* If it's not a `}', then there is a non-trivial initializer. */
19268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19270 /* Parse the initializer list. */
19271 CONSTRUCTOR_ELTS (initializer)
19272 = cp_parser_initializer_list (parser, non_constant_p);
19273 /* A trailing `,' token is allowed. */
19274 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19275 cp_lexer_consume_token (parser->lexer);
19277 else
19278 *non_constant_p = false;
19279 /* Now, there should be a trailing `}'. */
19280 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19281 TREE_TYPE (initializer) = init_list_type_node;
19282 return initializer;
19285 /* Consume tokens up to, and including, the next non-nested closing `]'.
19286 Returns true iff we found a closing `]'. */
19288 static bool
19289 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19291 unsigned square_depth = 0;
19293 while (true)
19295 cp_token * token = cp_lexer_peek_token (parser->lexer);
19297 switch (token->type)
19299 case CPP_EOF:
19300 case CPP_PRAGMA_EOL:
19301 /* If we've run out of tokens, then there is no closing `]'. */
19302 return false;
19304 case CPP_OPEN_SQUARE:
19305 ++square_depth;
19306 break;
19308 case CPP_CLOSE_SQUARE:
19309 if (!square_depth--)
19311 cp_lexer_consume_token (parser->lexer);
19312 return true;
19314 break;
19316 default:
19317 break;
19320 /* Consume the token. */
19321 cp_lexer_consume_token (parser->lexer);
19325 /* Return true if we are looking at an array-designator, false otherwise. */
19327 static bool
19328 cp_parser_array_designator_p (cp_parser *parser)
19330 /* Consume the `['. */
19331 cp_lexer_consume_token (parser->lexer);
19333 cp_lexer_save_tokens (parser->lexer);
19335 /* Skip tokens until the next token is a closing square bracket.
19336 If we find the closing `]', and the next token is a `=', then
19337 we are looking at an array designator. */
19338 bool array_designator_p
19339 = (cp_parser_skip_to_closing_square_bracket (parser)
19340 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19342 /* Roll back the tokens we skipped. */
19343 cp_lexer_rollback_tokens (parser->lexer);
19345 return array_designator_p;
19348 /* Parse an initializer-list.
19350 initializer-list:
19351 initializer-clause ... [opt]
19352 initializer-list , initializer-clause ... [opt]
19354 GNU Extension:
19356 initializer-list:
19357 designation initializer-clause ...[opt]
19358 initializer-list , designation initializer-clause ...[opt]
19360 designation:
19361 . identifier =
19362 identifier :
19363 [ constant-expression ] =
19365 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19366 for the initializer. If the INDEX of the elt is non-NULL, it is the
19367 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19368 as for cp_parser_initializer. */
19370 static vec<constructor_elt, va_gc> *
19371 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19373 vec<constructor_elt, va_gc> *v = NULL;
19375 /* Assume all of the expressions are constant. */
19376 *non_constant_p = false;
19378 /* Parse the rest of the list. */
19379 while (true)
19381 cp_token *token;
19382 tree designator;
19383 tree initializer;
19384 bool clause_non_constant_p;
19386 /* If the next token is an identifier and the following one is a
19387 colon, we are looking at the GNU designated-initializer
19388 syntax. */
19389 if (cp_parser_allow_gnu_extensions_p (parser)
19390 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19391 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19393 /* Warn the user that they are using an extension. */
19394 pedwarn (input_location, OPT_Wpedantic,
19395 "ISO C++ does not allow designated initializers");
19396 /* Consume the identifier. */
19397 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19398 /* Consume the `:'. */
19399 cp_lexer_consume_token (parser->lexer);
19401 /* Also handle the C99 syntax, '. id ='. */
19402 else if (cp_parser_allow_gnu_extensions_p (parser)
19403 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19404 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19405 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19407 /* Warn the user that they are using an extension. */
19408 pedwarn (input_location, OPT_Wpedantic,
19409 "ISO C++ does not allow C99 designated initializers");
19410 /* Consume the `.'. */
19411 cp_lexer_consume_token (parser->lexer);
19412 /* Consume the identifier. */
19413 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19414 /* Consume the `='. */
19415 cp_lexer_consume_token (parser->lexer);
19417 /* Also handle C99 array designators, '[ const ] ='. */
19418 else if (cp_parser_allow_gnu_extensions_p (parser)
19419 && !c_dialect_objc ()
19420 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19422 /* In C++11, [ could start a lambda-introducer. */
19423 bool non_const = false;
19425 cp_parser_parse_tentatively (parser);
19427 if (!cp_parser_array_designator_p (parser))
19429 cp_parser_simulate_error (parser);
19430 designator = NULL_TREE;
19432 else
19434 designator = cp_parser_constant_expression (parser, true,
19435 &non_const);
19436 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19437 cp_parser_require (parser, CPP_EQ, RT_EQ);
19440 if (!cp_parser_parse_definitely (parser))
19441 designator = NULL_TREE;
19442 else if (non_const)
19443 require_potential_rvalue_constant_expression (designator);
19445 else
19446 designator = NULL_TREE;
19448 /* Parse the initializer. */
19449 initializer = cp_parser_initializer_clause (parser,
19450 &clause_non_constant_p);
19451 /* If any clause is non-constant, so is the entire initializer. */
19452 if (clause_non_constant_p)
19453 *non_constant_p = true;
19455 /* If we have an ellipsis, this is an initializer pack
19456 expansion. */
19457 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19459 /* Consume the `...'. */
19460 cp_lexer_consume_token (parser->lexer);
19462 /* Turn the initializer into an initializer expansion. */
19463 initializer = make_pack_expansion (initializer);
19466 /* Add it to the vector. */
19467 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19469 /* If the next token is not a comma, we have reached the end of
19470 the list. */
19471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19472 break;
19474 /* Peek at the next token. */
19475 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19476 /* If the next token is a `}', then we're still done. An
19477 initializer-clause can have a trailing `,' after the
19478 initializer-list and before the closing `}'. */
19479 if (token->type == CPP_CLOSE_BRACE)
19480 break;
19482 /* Consume the `,' token. */
19483 cp_lexer_consume_token (parser->lexer);
19486 return v;
19489 /* Classes [gram.class] */
19491 /* Parse a class-name.
19493 class-name:
19494 identifier
19495 template-id
19497 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19498 to indicate that names looked up in dependent types should be
19499 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19500 keyword has been used to indicate that the name that appears next
19501 is a template. TAG_TYPE indicates the explicit tag given before
19502 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19503 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19504 is the class being defined in a class-head.
19506 Returns the TYPE_DECL representing the class. */
19508 static tree
19509 cp_parser_class_name (cp_parser *parser,
19510 bool typename_keyword_p,
19511 bool template_keyword_p,
19512 enum tag_types tag_type,
19513 bool check_dependency_p,
19514 bool class_head_p,
19515 bool is_declaration)
19517 tree decl;
19518 tree scope;
19519 bool typename_p;
19520 cp_token *token;
19521 tree identifier = NULL_TREE;
19523 /* All class-names start with an identifier. */
19524 token = cp_lexer_peek_token (parser->lexer);
19525 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19527 cp_parser_error (parser, "expected class-name");
19528 return error_mark_node;
19531 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19532 to a template-id, so we save it here. */
19533 scope = parser->scope;
19534 if (scope == error_mark_node)
19535 return error_mark_node;
19537 /* Any name names a type if we're following the `typename' keyword
19538 in a qualified name where the enclosing scope is type-dependent. */
19539 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19540 && dependent_type_p (scope));
19541 /* Handle the common case (an identifier, but not a template-id)
19542 efficiently. */
19543 if (token->type == CPP_NAME
19544 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19546 cp_token *identifier_token;
19547 bool ambiguous_p;
19549 /* Look for the identifier. */
19550 identifier_token = cp_lexer_peek_token (parser->lexer);
19551 ambiguous_p = identifier_token->error_reported;
19552 identifier = cp_parser_identifier (parser);
19553 /* If the next token isn't an identifier, we are certainly not
19554 looking at a class-name. */
19555 if (identifier == error_mark_node)
19556 decl = error_mark_node;
19557 /* If we know this is a type-name, there's no need to look it
19558 up. */
19559 else if (typename_p)
19560 decl = identifier;
19561 else
19563 tree ambiguous_decls;
19564 /* If we already know that this lookup is ambiguous, then
19565 we've already issued an error message; there's no reason
19566 to check again. */
19567 if (ambiguous_p)
19569 cp_parser_simulate_error (parser);
19570 return error_mark_node;
19572 /* If the next token is a `::', then the name must be a type
19573 name.
19575 [basic.lookup.qual]
19577 During the lookup for a name preceding the :: scope
19578 resolution operator, object, function, and enumerator
19579 names are ignored. */
19580 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19581 tag_type = typename_type;
19582 /* Look up the name. */
19583 decl = cp_parser_lookup_name (parser, identifier,
19584 tag_type,
19585 /*is_template=*/false,
19586 /*is_namespace=*/false,
19587 check_dependency_p,
19588 &ambiguous_decls,
19589 identifier_token->location);
19590 if (ambiguous_decls)
19592 if (cp_parser_parsing_tentatively (parser))
19593 cp_parser_simulate_error (parser);
19594 return error_mark_node;
19598 else
19600 /* Try a template-id. */
19601 decl = cp_parser_template_id (parser, template_keyword_p,
19602 check_dependency_p,
19603 tag_type,
19604 is_declaration);
19605 if (decl == error_mark_node)
19606 return error_mark_node;
19609 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19611 /* If this is a typename, create a TYPENAME_TYPE. */
19612 if (typename_p && decl != error_mark_node)
19614 decl = make_typename_type (scope, decl, typename_type,
19615 /*complain=*/tf_error);
19616 if (decl != error_mark_node)
19617 decl = TYPE_NAME (decl);
19620 decl = strip_using_decl (decl);
19622 /* Check to see that it is really the name of a class. */
19623 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19624 && identifier_p (TREE_OPERAND (decl, 0))
19625 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19626 /* Situations like this:
19628 template <typename T> struct A {
19629 typename T::template X<int>::I i;
19632 are problematic. Is `T::template X<int>' a class-name? The
19633 standard does not seem to be definitive, but there is no other
19634 valid interpretation of the following `::'. Therefore, those
19635 names are considered class-names. */
19637 decl = make_typename_type (scope, decl, tag_type, tf_error);
19638 if (decl != error_mark_node)
19639 decl = TYPE_NAME (decl);
19641 else if (TREE_CODE (decl) != TYPE_DECL
19642 || TREE_TYPE (decl) == error_mark_node
19643 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19644 /* In Objective-C 2.0, a classname followed by '.' starts a
19645 dot-syntax expression, and it's not a type-name. */
19646 || (c_dialect_objc ()
19647 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19648 && objc_is_class_name (decl)))
19649 decl = error_mark_node;
19651 if (decl == error_mark_node)
19652 cp_parser_error (parser, "expected class-name");
19653 else if (identifier && !parser->scope)
19654 maybe_note_name_used_in_class (identifier, decl);
19656 return decl;
19659 /* Parse a class-specifier.
19661 class-specifier:
19662 class-head { member-specification [opt] }
19664 Returns the TREE_TYPE representing the class. */
19666 static tree
19667 cp_parser_class_specifier_1 (cp_parser* parser)
19669 tree type;
19670 tree attributes = NULL_TREE;
19671 bool nested_name_specifier_p;
19672 unsigned saved_num_template_parameter_lists;
19673 bool saved_in_function_body;
19674 unsigned char in_statement;
19675 bool in_switch_statement_p;
19676 bool saved_in_unbraced_linkage_specification_p;
19677 tree old_scope = NULL_TREE;
19678 tree scope = NULL_TREE;
19679 cp_token *closing_brace;
19681 push_deferring_access_checks (dk_no_deferred);
19683 /* Parse the class-head. */
19684 type = cp_parser_class_head (parser,
19685 &nested_name_specifier_p);
19686 /* If the class-head was a semantic disaster, skip the entire body
19687 of the class. */
19688 if (!type)
19690 cp_parser_skip_to_end_of_block_or_statement (parser);
19691 pop_deferring_access_checks ();
19692 return error_mark_node;
19695 /* Look for the `{'. */
19696 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19698 pop_deferring_access_checks ();
19699 return error_mark_node;
19702 cp_ensure_no_omp_declare_simd (parser);
19704 /* Issue an error message if type-definitions are forbidden here. */
19705 cp_parser_check_type_definition (parser);
19706 /* Remember that we are defining one more class. */
19707 ++parser->num_classes_being_defined;
19708 /* Inside the class, surrounding template-parameter-lists do not
19709 apply. */
19710 saved_num_template_parameter_lists
19711 = parser->num_template_parameter_lists;
19712 parser->num_template_parameter_lists = 0;
19713 /* We are not in a function body. */
19714 saved_in_function_body = parser->in_function_body;
19715 parser->in_function_body = false;
19716 /* Or in a loop. */
19717 in_statement = parser->in_statement;
19718 parser->in_statement = 0;
19719 /* Or in a switch. */
19720 in_switch_statement_p = parser->in_switch_statement_p;
19721 parser->in_switch_statement_p = false;
19722 /* We are not immediately inside an extern "lang" block. */
19723 saved_in_unbraced_linkage_specification_p
19724 = parser->in_unbraced_linkage_specification_p;
19725 parser->in_unbraced_linkage_specification_p = false;
19727 /* Start the class. */
19728 if (nested_name_specifier_p)
19730 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19731 old_scope = push_inner_scope (scope);
19733 type = begin_class_definition (type);
19735 if (type == error_mark_node)
19736 /* If the type is erroneous, skip the entire body of the class. */
19737 cp_parser_skip_to_closing_brace (parser);
19738 else
19739 /* Parse the member-specification. */
19740 cp_parser_member_specification_opt (parser);
19742 /* Look for the trailing `}'. */
19743 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19744 /* Look for trailing attributes to apply to this class. */
19745 if (cp_parser_allow_gnu_extensions_p (parser))
19746 attributes = cp_parser_gnu_attributes_opt (parser);
19747 if (type != error_mark_node)
19748 type = finish_struct (type, attributes);
19749 if (nested_name_specifier_p)
19750 pop_inner_scope (old_scope, scope);
19752 /* We've finished a type definition. Check for the common syntax
19753 error of forgetting a semicolon after the definition. We need to
19754 be careful, as we can't just check for not-a-semicolon and be done
19755 with it; the user might have typed:
19757 class X { } c = ...;
19758 class X { } *p = ...;
19760 and so forth. Instead, enumerate all the possible tokens that
19761 might follow this production; if we don't see one of them, then
19762 complain and silently insert the semicolon. */
19764 cp_token *token = cp_lexer_peek_token (parser->lexer);
19765 bool want_semicolon = true;
19767 if (cp_next_tokens_can_be_std_attribute_p (parser))
19768 /* Don't try to parse c++11 attributes here. As per the
19769 grammar, that should be a task for
19770 cp_parser_decl_specifier_seq. */
19771 want_semicolon = false;
19773 switch (token->type)
19775 case CPP_NAME:
19776 case CPP_SEMICOLON:
19777 case CPP_MULT:
19778 case CPP_AND:
19779 case CPP_OPEN_PAREN:
19780 case CPP_CLOSE_PAREN:
19781 case CPP_COMMA:
19782 want_semicolon = false;
19783 break;
19785 /* While it's legal for type qualifiers and storage class
19786 specifiers to follow type definitions in the grammar, only
19787 compiler testsuites contain code like that. Assume that if
19788 we see such code, then what we're really seeing is a case
19789 like:
19791 class X { }
19792 const <type> var = ...;
19796 class Y { }
19797 static <type> func (...) ...
19799 i.e. the qualifier or specifier applies to the next
19800 declaration. To do so, however, we need to look ahead one
19801 more token to see if *that* token is a type specifier.
19803 This code could be improved to handle:
19805 class Z { }
19806 static const <type> var = ...; */
19807 case CPP_KEYWORD:
19808 if (keyword_is_decl_specifier (token->keyword))
19810 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19812 /* Handling user-defined types here would be nice, but very
19813 tricky. */
19814 want_semicolon
19815 = (lookahead->type == CPP_KEYWORD
19816 && keyword_begins_type_specifier (lookahead->keyword));
19818 break;
19819 default:
19820 break;
19823 /* If we don't have a type, then something is very wrong and we
19824 shouldn't try to do anything clever. Likewise for not seeing the
19825 closing brace. */
19826 if (closing_brace && TYPE_P (type) && want_semicolon)
19828 cp_token_position prev
19829 = cp_lexer_previous_token_position (parser->lexer);
19830 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19831 location_t loc = prev_token->location;
19833 if (CLASSTYPE_DECLARED_CLASS (type))
19834 error_at (loc, "expected %<;%> after class definition");
19835 else if (TREE_CODE (type) == RECORD_TYPE)
19836 error_at (loc, "expected %<;%> after struct definition");
19837 else if (TREE_CODE (type) == UNION_TYPE)
19838 error_at (loc, "expected %<;%> after union definition");
19839 else
19840 gcc_unreachable ();
19842 /* Unget one token and smash it to look as though we encountered
19843 a semicolon in the input stream. */
19844 cp_lexer_set_token_position (parser->lexer, prev);
19845 token = cp_lexer_peek_token (parser->lexer);
19846 token->type = CPP_SEMICOLON;
19847 token->keyword = RID_MAX;
19851 /* If this class is not itself within the scope of another class,
19852 then we need to parse the bodies of all of the queued function
19853 definitions. Note that the queued functions defined in a class
19854 are not always processed immediately following the
19855 class-specifier for that class. Consider:
19857 struct A {
19858 struct B { void f() { sizeof (A); } };
19861 If `f' were processed before the processing of `A' were
19862 completed, there would be no way to compute the size of `A'.
19863 Note that the nesting we are interested in here is lexical --
19864 not the semantic nesting given by TYPE_CONTEXT. In particular,
19865 for:
19867 struct A { struct B; };
19868 struct A::B { void f() { } };
19870 there is no need to delay the parsing of `A::B::f'. */
19871 if (--parser->num_classes_being_defined == 0)
19873 tree decl;
19874 tree class_type = NULL_TREE;
19875 tree pushed_scope = NULL_TREE;
19876 unsigned ix;
19877 cp_default_arg_entry *e;
19878 tree save_ccp, save_ccr;
19880 /* In a first pass, parse default arguments to the functions.
19881 Then, in a second pass, parse the bodies of the functions.
19882 This two-phased approach handles cases like:
19884 struct S {
19885 void f() { g(); }
19886 void g(int i = 3);
19890 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19892 decl = e->decl;
19893 /* If there are default arguments that have not yet been processed,
19894 take care of them now. */
19895 if (class_type != e->class_type)
19897 if (pushed_scope)
19898 pop_scope (pushed_scope);
19899 class_type = e->class_type;
19900 pushed_scope = push_scope (class_type);
19902 /* Make sure that any template parameters are in scope. */
19903 maybe_begin_member_template_processing (decl);
19904 /* Parse the default argument expressions. */
19905 cp_parser_late_parsing_default_args (parser, decl);
19906 /* Remove any template parameters from the symbol table. */
19907 maybe_end_member_template_processing ();
19909 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19910 /* Now parse any NSDMIs. */
19911 save_ccp = current_class_ptr;
19912 save_ccr = current_class_ref;
19913 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19915 if (class_type != DECL_CONTEXT (decl))
19917 if (pushed_scope)
19918 pop_scope (pushed_scope);
19919 class_type = DECL_CONTEXT (decl);
19920 pushed_scope = push_scope (class_type);
19922 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19923 cp_parser_late_parsing_nsdmi (parser, decl);
19925 vec_safe_truncate (unparsed_nsdmis, 0);
19926 current_class_ptr = save_ccp;
19927 current_class_ref = save_ccr;
19928 if (pushed_scope)
19929 pop_scope (pushed_scope);
19931 /* Now do some post-NSDMI bookkeeping. */
19932 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19933 after_nsdmi_defaulted_late_checks (class_type);
19934 vec_safe_truncate (unparsed_classes, 0);
19935 after_nsdmi_defaulted_late_checks (type);
19937 /* Now parse the body of the functions. */
19938 if (flag_openmp)
19940 /* OpenMP UDRs need to be parsed before all other functions. */
19941 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19942 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19943 cp_parser_late_parsing_for_member (parser, decl);
19944 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19945 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19946 cp_parser_late_parsing_for_member (parser, decl);
19948 else
19949 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19950 cp_parser_late_parsing_for_member (parser, decl);
19951 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19953 else
19954 vec_safe_push (unparsed_classes, type);
19956 /* Put back any saved access checks. */
19957 pop_deferring_access_checks ();
19959 /* Restore saved state. */
19960 parser->in_switch_statement_p = in_switch_statement_p;
19961 parser->in_statement = in_statement;
19962 parser->in_function_body = saved_in_function_body;
19963 parser->num_template_parameter_lists
19964 = saved_num_template_parameter_lists;
19965 parser->in_unbraced_linkage_specification_p
19966 = saved_in_unbraced_linkage_specification_p;
19968 return type;
19971 static tree
19972 cp_parser_class_specifier (cp_parser* parser)
19974 tree ret;
19975 timevar_push (TV_PARSE_STRUCT);
19976 ret = cp_parser_class_specifier_1 (parser);
19977 timevar_pop (TV_PARSE_STRUCT);
19978 return ret;
19981 /* Parse a class-head.
19983 class-head:
19984 class-key identifier [opt] base-clause [opt]
19985 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19986 class-key nested-name-specifier [opt] template-id
19987 base-clause [opt]
19989 class-virt-specifier:
19990 final
19992 GNU Extensions:
19993 class-key attributes identifier [opt] base-clause [opt]
19994 class-key attributes nested-name-specifier identifier base-clause [opt]
19995 class-key attributes nested-name-specifier [opt] template-id
19996 base-clause [opt]
19998 Upon return BASES is initialized to the list of base classes (or
19999 NULL, if there are none) in the same form returned by
20000 cp_parser_base_clause.
20002 Returns the TYPE of the indicated class. Sets
20003 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20004 involving a nested-name-specifier was used, and FALSE otherwise.
20006 Returns error_mark_node if this is not a class-head.
20008 Returns NULL_TREE if the class-head is syntactically valid, but
20009 semantically invalid in a way that means we should skip the entire
20010 body of the class. */
20012 static tree
20013 cp_parser_class_head (cp_parser* parser,
20014 bool* nested_name_specifier_p)
20016 tree nested_name_specifier;
20017 enum tag_types class_key;
20018 tree id = NULL_TREE;
20019 tree type = NULL_TREE;
20020 tree attributes;
20021 tree bases;
20022 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20023 bool template_id_p = false;
20024 bool qualified_p = false;
20025 bool invalid_nested_name_p = false;
20026 bool invalid_explicit_specialization_p = false;
20027 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20028 tree pushed_scope = NULL_TREE;
20029 unsigned num_templates;
20030 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20031 /* Assume no nested-name-specifier will be present. */
20032 *nested_name_specifier_p = false;
20033 /* Assume no template parameter lists will be used in defining the
20034 type. */
20035 num_templates = 0;
20036 parser->colon_corrects_to_scope_p = false;
20038 /* Look for the class-key. */
20039 class_key = cp_parser_class_key (parser);
20040 if (class_key == none_type)
20041 return error_mark_node;
20043 /* Parse the attributes. */
20044 attributes = cp_parser_attributes_opt (parser);
20046 /* If the next token is `::', that is invalid -- but sometimes
20047 people do try to write:
20049 struct ::S {};
20051 Handle this gracefully by accepting the extra qualifier, and then
20052 issuing an error about it later if this really is a
20053 class-head. If it turns out just to be an elaborated type
20054 specifier, remain silent. */
20055 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20056 qualified_p = true;
20058 push_deferring_access_checks (dk_no_check);
20060 /* Determine the name of the class. Begin by looking for an
20061 optional nested-name-specifier. */
20062 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20063 nested_name_specifier
20064 = cp_parser_nested_name_specifier_opt (parser,
20065 /*typename_keyword_p=*/false,
20066 /*check_dependency_p=*/false,
20067 /*type_p=*/true,
20068 /*is_declaration=*/false);
20069 /* If there was a nested-name-specifier, then there *must* be an
20070 identifier. */
20071 if (nested_name_specifier)
20073 type_start_token = cp_lexer_peek_token (parser->lexer);
20074 /* Although the grammar says `identifier', it really means
20075 `class-name' or `template-name'. You are only allowed to
20076 define a class that has already been declared with this
20077 syntax.
20079 The proposed resolution for Core Issue 180 says that wherever
20080 you see `class T::X' you should treat `X' as a type-name.
20082 It is OK to define an inaccessible class; for example:
20084 class A { class B; };
20085 class A::B {};
20087 We do not know if we will see a class-name, or a
20088 template-name. We look for a class-name first, in case the
20089 class-name is a template-id; if we looked for the
20090 template-name first we would stop after the template-name. */
20091 cp_parser_parse_tentatively (parser);
20092 type = cp_parser_class_name (parser,
20093 /*typename_keyword_p=*/false,
20094 /*template_keyword_p=*/false,
20095 class_type,
20096 /*check_dependency_p=*/false,
20097 /*class_head_p=*/true,
20098 /*is_declaration=*/false);
20099 /* If that didn't work, ignore the nested-name-specifier. */
20100 if (!cp_parser_parse_definitely (parser))
20102 invalid_nested_name_p = true;
20103 type_start_token = cp_lexer_peek_token (parser->lexer);
20104 id = cp_parser_identifier (parser);
20105 if (id == error_mark_node)
20106 id = NULL_TREE;
20108 /* If we could not find a corresponding TYPE, treat this
20109 declaration like an unqualified declaration. */
20110 if (type == error_mark_node)
20111 nested_name_specifier = NULL_TREE;
20112 /* Otherwise, count the number of templates used in TYPE and its
20113 containing scopes. */
20114 else
20116 tree scope;
20118 for (scope = TREE_TYPE (type);
20119 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20120 scope = get_containing_scope (scope))
20121 if (TYPE_P (scope)
20122 && CLASS_TYPE_P (scope)
20123 && CLASSTYPE_TEMPLATE_INFO (scope)
20124 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20125 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20126 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20127 ++num_templates;
20130 /* Otherwise, the identifier is optional. */
20131 else
20133 /* We don't know whether what comes next is a template-id,
20134 an identifier, or nothing at all. */
20135 cp_parser_parse_tentatively (parser);
20136 /* Check for a template-id. */
20137 type_start_token = cp_lexer_peek_token (parser->lexer);
20138 id = cp_parser_template_id (parser,
20139 /*template_keyword_p=*/false,
20140 /*check_dependency_p=*/true,
20141 class_key,
20142 /*is_declaration=*/true);
20143 /* If that didn't work, it could still be an identifier. */
20144 if (!cp_parser_parse_definitely (parser))
20146 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20148 type_start_token = cp_lexer_peek_token (parser->lexer);
20149 id = cp_parser_identifier (parser);
20151 else
20152 id = NULL_TREE;
20154 else
20156 template_id_p = true;
20157 ++num_templates;
20161 pop_deferring_access_checks ();
20163 if (id)
20165 cp_parser_check_for_invalid_template_id (parser, id,
20166 class_key,
20167 type_start_token->location);
20169 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20171 /* If it's not a `:' or a `{' then we can't really be looking at a
20172 class-head, since a class-head only appears as part of a
20173 class-specifier. We have to detect this situation before calling
20174 xref_tag, since that has irreversible side-effects. */
20175 if (!cp_parser_next_token_starts_class_definition_p (parser))
20177 cp_parser_error (parser, "expected %<{%> or %<:%>");
20178 type = error_mark_node;
20179 goto out;
20182 /* At this point, we're going ahead with the class-specifier, even
20183 if some other problem occurs. */
20184 cp_parser_commit_to_tentative_parse (parser);
20185 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20187 cp_parser_error (parser,
20188 "cannot specify %<override%> for a class");
20189 type = error_mark_node;
20190 goto out;
20192 /* Issue the error about the overly-qualified name now. */
20193 if (qualified_p)
20195 cp_parser_error (parser,
20196 "global qualification of class name is invalid");
20197 type = error_mark_node;
20198 goto out;
20200 else if (invalid_nested_name_p)
20202 cp_parser_error (parser,
20203 "qualified name does not name a class");
20204 type = error_mark_node;
20205 goto out;
20207 else if (nested_name_specifier)
20209 tree scope;
20211 /* Reject typedef-names in class heads. */
20212 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20214 error_at (type_start_token->location,
20215 "invalid class name in declaration of %qD",
20216 type);
20217 type = NULL_TREE;
20218 goto done;
20221 /* Figure out in what scope the declaration is being placed. */
20222 scope = current_scope ();
20223 /* If that scope does not contain the scope in which the
20224 class was originally declared, the program is invalid. */
20225 if (scope && !is_ancestor (scope, nested_name_specifier))
20227 if (at_namespace_scope_p ())
20228 error_at (type_start_token->location,
20229 "declaration of %qD in namespace %qD which does not "
20230 "enclose %qD",
20231 type, scope, nested_name_specifier);
20232 else
20233 error_at (type_start_token->location,
20234 "declaration of %qD in %qD which does not enclose %qD",
20235 type, scope, nested_name_specifier);
20236 type = NULL_TREE;
20237 goto done;
20239 /* [dcl.meaning]
20241 A declarator-id shall not be qualified except for the
20242 definition of a ... nested class outside of its class
20243 ... [or] the definition or explicit instantiation of a
20244 class member of a namespace outside of its namespace. */
20245 if (scope == nested_name_specifier)
20247 permerror (nested_name_specifier_token_start->location,
20248 "extra qualification not allowed");
20249 nested_name_specifier = NULL_TREE;
20250 num_templates = 0;
20253 /* An explicit-specialization must be preceded by "template <>". If
20254 it is not, try to recover gracefully. */
20255 if (at_namespace_scope_p ()
20256 && parser->num_template_parameter_lists == 0
20257 && template_id_p)
20259 error_at (type_start_token->location,
20260 "an explicit specialization must be preceded by %<template <>%>");
20261 invalid_explicit_specialization_p = true;
20262 /* Take the same action that would have been taken by
20263 cp_parser_explicit_specialization. */
20264 ++parser->num_template_parameter_lists;
20265 begin_specialization ();
20267 /* There must be no "return" statements between this point and the
20268 end of this function; set "type "to the correct return value and
20269 use "goto done;" to return. */
20270 /* Make sure that the right number of template parameters were
20271 present. */
20272 if (!cp_parser_check_template_parameters (parser, num_templates,
20273 type_start_token->location,
20274 /*declarator=*/NULL))
20276 /* If something went wrong, there is no point in even trying to
20277 process the class-definition. */
20278 type = NULL_TREE;
20279 goto done;
20282 /* Look up the type. */
20283 if (template_id_p)
20285 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20286 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20287 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20289 error_at (type_start_token->location,
20290 "function template %qD redeclared as a class template", id);
20291 type = error_mark_node;
20293 else
20295 type = TREE_TYPE (id);
20296 type = maybe_process_partial_specialization (type);
20298 if (nested_name_specifier)
20299 pushed_scope = push_scope (nested_name_specifier);
20301 else if (nested_name_specifier)
20303 tree class_type;
20305 /* Given:
20307 template <typename T> struct S { struct T };
20308 template <typename T> struct S<T>::T { };
20310 we will get a TYPENAME_TYPE when processing the definition of
20311 `S::T'. We need to resolve it to the actual type before we
20312 try to define it. */
20313 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20315 class_type = resolve_typename_type (TREE_TYPE (type),
20316 /*only_current_p=*/false);
20317 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20318 type = TYPE_NAME (class_type);
20319 else
20321 cp_parser_error (parser, "could not resolve typename type");
20322 type = error_mark_node;
20326 if (maybe_process_partial_specialization (TREE_TYPE (type))
20327 == error_mark_node)
20329 type = NULL_TREE;
20330 goto done;
20333 class_type = current_class_type;
20334 /* Enter the scope indicated by the nested-name-specifier. */
20335 pushed_scope = push_scope (nested_name_specifier);
20336 /* Get the canonical version of this type. */
20337 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20338 /* Call push_template_decl if it seems like we should be defining a
20339 template either from the template headers or the type we're
20340 defining, so that we diagnose both extra and missing headers. */
20341 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20342 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20343 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20345 type = push_template_decl (type);
20346 if (type == error_mark_node)
20348 type = NULL_TREE;
20349 goto done;
20353 type = TREE_TYPE (type);
20354 *nested_name_specifier_p = true;
20356 else /* The name is not a nested name. */
20358 /* If the class was unnamed, create a dummy name. */
20359 if (!id)
20360 id = make_anon_name ();
20361 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20362 parser->num_template_parameter_lists);
20365 /* Indicate whether this class was declared as a `class' or as a
20366 `struct'. */
20367 if (TREE_CODE (type) == RECORD_TYPE)
20368 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20369 cp_parser_check_class_key (class_key, type);
20371 /* If this type was already complete, and we see another definition,
20372 that's an error. */
20373 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20375 error_at (type_start_token->location, "redefinition of %q#T",
20376 type);
20377 error_at (type_start_token->location, "previous definition of %q+#T",
20378 type);
20379 type = NULL_TREE;
20380 goto done;
20382 else if (type == error_mark_node)
20383 type = NULL_TREE;
20385 if (type)
20387 /* Apply attributes now, before any use of the class as a template
20388 argument in its base list. */
20389 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20390 fixup_attribute_variants (type);
20393 /* We will have entered the scope containing the class; the names of
20394 base classes should be looked up in that context. For example:
20396 struct A { struct B {}; struct C; };
20397 struct A::C : B {};
20399 is valid. */
20401 /* Get the list of base-classes, if there is one. */
20402 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20404 /* PR59482: enter the class scope so that base-specifiers are looked
20405 up correctly. */
20406 if (type)
20407 pushclass (type);
20408 bases = cp_parser_base_clause (parser);
20409 /* PR59482: get out of the previously pushed class scope so that the
20410 subsequent pops pop the right thing. */
20411 if (type)
20412 popclass ();
20414 else
20415 bases = NULL_TREE;
20417 /* If we're really defining a class, process the base classes.
20418 If they're invalid, fail. */
20419 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20420 && !xref_basetypes (type, bases))
20421 type = NULL_TREE;
20423 done:
20424 /* Leave the scope given by the nested-name-specifier. We will
20425 enter the class scope itself while processing the members. */
20426 if (pushed_scope)
20427 pop_scope (pushed_scope);
20429 if (invalid_explicit_specialization_p)
20431 end_specialization ();
20432 --parser->num_template_parameter_lists;
20435 if (type)
20436 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20437 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20438 CLASSTYPE_FINAL (type) = 1;
20439 out:
20440 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20441 return type;
20444 /* Parse a class-key.
20446 class-key:
20447 class
20448 struct
20449 union
20451 Returns the kind of class-key specified, or none_type to indicate
20452 error. */
20454 static enum tag_types
20455 cp_parser_class_key (cp_parser* parser)
20457 cp_token *token;
20458 enum tag_types tag_type;
20460 /* Look for the class-key. */
20461 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20462 if (!token)
20463 return none_type;
20465 /* Check to see if the TOKEN is a class-key. */
20466 tag_type = cp_parser_token_is_class_key (token);
20467 if (!tag_type)
20468 cp_parser_error (parser, "expected class-key");
20469 return tag_type;
20472 /* Parse a type-parameter-key.
20474 type-parameter-key:
20475 class
20476 typename
20479 static void
20480 cp_parser_type_parameter_key (cp_parser* parser)
20482 /* Look for the type-parameter-key. */
20483 enum tag_types tag_type = none_type;
20484 cp_token *token = cp_lexer_peek_token (parser->lexer);
20485 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20487 cp_lexer_consume_token (parser->lexer);
20488 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20489 /* typename is not allowed in a template template parameter
20490 by the standard until C++1Z. */
20491 pedwarn (token->location, OPT_Wpedantic,
20492 "ISO C++ forbids typename key in template template parameter;"
20493 " use -std=c++1z or -std=gnu++1z");
20495 else
20496 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20498 return;
20501 /* Parse an (optional) member-specification.
20503 member-specification:
20504 member-declaration member-specification [opt]
20505 access-specifier : member-specification [opt] */
20507 static void
20508 cp_parser_member_specification_opt (cp_parser* parser)
20510 while (true)
20512 cp_token *token;
20513 enum rid keyword;
20515 /* Peek at the next token. */
20516 token = cp_lexer_peek_token (parser->lexer);
20517 /* If it's a `}', or EOF then we've seen all the members. */
20518 if (token->type == CPP_CLOSE_BRACE
20519 || token->type == CPP_EOF
20520 || token->type == CPP_PRAGMA_EOL)
20521 break;
20523 /* See if this token is a keyword. */
20524 keyword = token->keyword;
20525 switch (keyword)
20527 case RID_PUBLIC:
20528 case RID_PROTECTED:
20529 case RID_PRIVATE:
20530 /* Consume the access-specifier. */
20531 cp_lexer_consume_token (parser->lexer);
20532 /* Remember which access-specifier is active. */
20533 current_access_specifier = token->u.value;
20534 /* Look for the `:'. */
20535 cp_parser_require (parser, CPP_COLON, RT_COLON);
20536 break;
20538 default:
20539 /* Accept #pragmas at class scope. */
20540 if (token->type == CPP_PRAGMA)
20542 cp_parser_pragma (parser, pragma_member);
20543 break;
20546 /* Otherwise, the next construction must be a
20547 member-declaration. */
20548 cp_parser_member_declaration (parser);
20553 /* Parse a member-declaration.
20555 member-declaration:
20556 decl-specifier-seq [opt] member-declarator-list [opt] ;
20557 function-definition ; [opt]
20558 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20559 using-declaration
20560 template-declaration
20561 alias-declaration
20563 member-declarator-list:
20564 member-declarator
20565 member-declarator-list , member-declarator
20567 member-declarator:
20568 declarator pure-specifier [opt]
20569 declarator constant-initializer [opt]
20570 identifier [opt] : constant-expression
20572 GNU Extensions:
20574 member-declaration:
20575 __extension__ member-declaration
20577 member-declarator:
20578 declarator attributes [opt] pure-specifier [opt]
20579 declarator attributes [opt] constant-initializer [opt]
20580 identifier [opt] attributes [opt] : constant-expression
20582 C++0x Extensions:
20584 member-declaration:
20585 static_assert-declaration */
20587 static void
20588 cp_parser_member_declaration (cp_parser* parser)
20590 cp_decl_specifier_seq decl_specifiers;
20591 tree prefix_attributes;
20592 tree decl;
20593 int declares_class_or_enum;
20594 bool friend_p;
20595 cp_token *token = NULL;
20596 cp_token *decl_spec_token_start = NULL;
20597 cp_token *initializer_token_start = NULL;
20598 int saved_pedantic;
20599 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20601 /* Check for the `__extension__' keyword. */
20602 if (cp_parser_extension_opt (parser, &saved_pedantic))
20604 /* Recurse. */
20605 cp_parser_member_declaration (parser);
20606 /* Restore the old value of the PEDANTIC flag. */
20607 pedantic = saved_pedantic;
20609 return;
20612 /* Check for a template-declaration. */
20613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20615 /* An explicit specialization here is an error condition, and we
20616 expect the specialization handler to detect and report this. */
20617 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20618 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20619 cp_parser_explicit_specialization (parser);
20620 else
20621 cp_parser_template_declaration (parser, /*member_p=*/true);
20623 return;
20626 /* Check for a using-declaration. */
20627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20629 if (cxx_dialect < cxx11)
20631 /* Parse the using-declaration. */
20632 cp_parser_using_declaration (parser,
20633 /*access_declaration_p=*/false);
20634 return;
20636 else
20638 tree decl;
20639 bool alias_decl_expected;
20640 cp_parser_parse_tentatively (parser);
20641 decl = cp_parser_alias_declaration (parser);
20642 /* Note that if we actually see the '=' token after the
20643 identifier, cp_parser_alias_declaration commits the
20644 tentative parse. In that case, we really expects an
20645 alias-declaration. Otherwise, we expect a using
20646 declaration. */
20647 alias_decl_expected =
20648 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20649 cp_parser_parse_definitely (parser);
20651 if (alias_decl_expected)
20652 finish_member_declaration (decl);
20653 else
20654 cp_parser_using_declaration (parser,
20655 /*access_declaration_p=*/false);
20656 return;
20660 /* Check for @defs. */
20661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20663 tree ivar, member;
20664 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20665 ivar = ivar_chains;
20666 while (ivar)
20668 member = ivar;
20669 ivar = TREE_CHAIN (member);
20670 TREE_CHAIN (member) = NULL_TREE;
20671 finish_member_declaration (member);
20673 return;
20676 /* If the next token is `static_assert' we have a static assertion. */
20677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20679 cp_parser_static_assert (parser, /*member_p=*/true);
20680 return;
20683 parser->colon_corrects_to_scope_p = false;
20685 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20686 goto out;
20688 /* Parse the decl-specifier-seq. */
20689 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20690 cp_parser_decl_specifier_seq (parser,
20691 CP_PARSER_FLAGS_OPTIONAL,
20692 &decl_specifiers,
20693 &declares_class_or_enum);
20694 /* Check for an invalid type-name. */
20695 if (!decl_specifiers.any_type_specifiers_p
20696 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20697 goto out;
20698 /* If there is no declarator, then the decl-specifier-seq should
20699 specify a type. */
20700 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20702 /* If there was no decl-specifier-seq, and the next token is a
20703 `;', then we have something like:
20705 struct S { ; };
20707 [class.mem]
20709 Each member-declaration shall declare at least one member
20710 name of the class. */
20711 if (!decl_specifiers.any_specifiers_p)
20713 cp_token *token = cp_lexer_peek_token (parser->lexer);
20714 if (!in_system_header_at (token->location))
20715 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20717 else
20719 tree type;
20721 /* See if this declaration is a friend. */
20722 friend_p = cp_parser_friend_p (&decl_specifiers);
20723 /* If there were decl-specifiers, check to see if there was
20724 a class-declaration. */
20725 type = check_tag_decl (&decl_specifiers,
20726 /*explicit_type_instantiation_p=*/false);
20727 /* Nested classes have already been added to the class, but
20728 a `friend' needs to be explicitly registered. */
20729 if (friend_p)
20731 /* If the `friend' keyword was present, the friend must
20732 be introduced with a class-key. */
20733 if (!declares_class_or_enum && cxx_dialect < cxx11)
20734 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20735 "in C++03 a class-key must be used "
20736 "when declaring a friend");
20737 /* In this case:
20739 template <typename T> struct A {
20740 friend struct A<T>::B;
20743 A<T>::B will be represented by a TYPENAME_TYPE, and
20744 therefore not recognized by check_tag_decl. */
20745 if (!type)
20747 type = decl_specifiers.type;
20748 if (type && TREE_CODE (type) == TYPE_DECL)
20749 type = TREE_TYPE (type);
20751 if (!type || !TYPE_P (type))
20752 error_at (decl_spec_token_start->location,
20753 "friend declaration does not name a class or "
20754 "function");
20755 else
20756 make_friend_class (current_class_type, type,
20757 /*complain=*/true);
20759 /* If there is no TYPE, an error message will already have
20760 been issued. */
20761 else if (!type || type == error_mark_node)
20763 /* An anonymous aggregate has to be handled specially; such
20764 a declaration really declares a data member (with a
20765 particular type), as opposed to a nested class. */
20766 else if (ANON_AGGR_TYPE_P (type))
20768 /* C++11 9.5/6. */
20769 if (decl_specifiers.storage_class != sc_none)
20770 error_at (decl_spec_token_start->location,
20771 "a storage class on an anonymous aggregate "
20772 "in class scope is not allowed");
20774 /* Remove constructors and such from TYPE, now that we
20775 know it is an anonymous aggregate. */
20776 fixup_anonymous_aggr (type);
20777 /* And make the corresponding data member. */
20778 decl = build_decl (decl_spec_token_start->location,
20779 FIELD_DECL, NULL_TREE, type);
20780 /* Add it to the class. */
20781 finish_member_declaration (decl);
20783 else
20784 cp_parser_check_access_in_redeclaration
20785 (TYPE_NAME (type),
20786 decl_spec_token_start->location);
20789 else
20791 bool assume_semicolon = false;
20793 /* Clear attributes from the decl_specifiers but keep them
20794 around as prefix attributes that apply them to the entity
20795 being declared. */
20796 prefix_attributes = decl_specifiers.attributes;
20797 decl_specifiers.attributes = NULL_TREE;
20799 /* See if these declarations will be friends. */
20800 friend_p = cp_parser_friend_p (&decl_specifiers);
20802 /* Keep going until we hit the `;' at the end of the
20803 declaration. */
20804 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20806 tree attributes = NULL_TREE;
20807 tree first_attribute;
20809 /* Peek at the next token. */
20810 token = cp_lexer_peek_token (parser->lexer);
20812 /* Check for a bitfield declaration. */
20813 if (token->type == CPP_COLON
20814 || (token->type == CPP_NAME
20815 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20816 == CPP_COLON))
20818 tree identifier;
20819 tree width;
20821 /* Get the name of the bitfield. Note that we cannot just
20822 check TOKEN here because it may have been invalidated by
20823 the call to cp_lexer_peek_nth_token above. */
20824 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20825 identifier = cp_parser_identifier (parser);
20826 else
20827 identifier = NULL_TREE;
20829 /* Consume the `:' token. */
20830 cp_lexer_consume_token (parser->lexer);
20831 /* Get the width of the bitfield. */
20832 width
20833 = cp_parser_constant_expression (parser);
20835 /* Look for attributes that apply to the bitfield. */
20836 attributes = cp_parser_attributes_opt (parser);
20837 /* Remember which attributes are prefix attributes and
20838 which are not. */
20839 first_attribute = attributes;
20840 /* Combine the attributes. */
20841 attributes = chainon (prefix_attributes, attributes);
20843 /* Create the bitfield declaration. */
20844 decl = grokbitfield (identifier
20845 ? make_id_declarator (NULL_TREE,
20846 identifier,
20847 sfk_none)
20848 : NULL,
20849 &decl_specifiers,
20850 width,
20851 attributes);
20853 else
20855 cp_declarator *declarator;
20856 tree initializer;
20857 tree asm_specification;
20858 int ctor_dtor_or_conv_p;
20860 /* Parse the declarator. */
20861 declarator
20862 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20863 &ctor_dtor_or_conv_p,
20864 /*parenthesized_p=*/NULL,
20865 /*member_p=*/true,
20866 friend_p);
20868 /* If something went wrong parsing the declarator, make sure
20869 that we at least consume some tokens. */
20870 if (declarator == cp_error_declarator)
20872 /* Skip to the end of the statement. */
20873 cp_parser_skip_to_end_of_statement (parser);
20874 /* If the next token is not a semicolon, that is
20875 probably because we just skipped over the body of
20876 a function. So, we consume a semicolon if
20877 present, but do not issue an error message if it
20878 is not present. */
20879 if (cp_lexer_next_token_is (parser->lexer,
20880 CPP_SEMICOLON))
20881 cp_lexer_consume_token (parser->lexer);
20882 goto out;
20885 if (declares_class_or_enum & 2)
20886 cp_parser_check_for_definition_in_return_type
20887 (declarator, decl_specifiers.type,
20888 decl_specifiers.locations[ds_type_spec]);
20890 /* Look for an asm-specification. */
20891 asm_specification = cp_parser_asm_specification_opt (parser);
20892 /* Look for attributes that apply to the declaration. */
20893 attributes = cp_parser_attributes_opt (parser);
20894 /* Remember which attributes are prefix attributes and
20895 which are not. */
20896 first_attribute = attributes;
20897 /* Combine the attributes. */
20898 attributes = chainon (prefix_attributes, attributes);
20900 /* If it's an `=', then we have a constant-initializer or a
20901 pure-specifier. It is not correct to parse the
20902 initializer before registering the member declaration
20903 since the member declaration should be in scope while
20904 its initializer is processed. However, the rest of the
20905 front end does not yet provide an interface that allows
20906 us to handle this correctly. */
20907 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20909 /* In [class.mem]:
20911 A pure-specifier shall be used only in the declaration of
20912 a virtual function.
20914 A member-declarator can contain a constant-initializer
20915 only if it declares a static member of integral or
20916 enumeration type.
20918 Therefore, if the DECLARATOR is for a function, we look
20919 for a pure-specifier; otherwise, we look for a
20920 constant-initializer. When we call `grokfield', it will
20921 perform more stringent semantics checks. */
20922 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20923 if (function_declarator_p (declarator)
20924 || (decl_specifiers.type
20925 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20926 && declarator->kind == cdk_id
20927 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20928 == FUNCTION_TYPE)))
20929 initializer = cp_parser_pure_specifier (parser);
20930 else if (decl_specifiers.storage_class != sc_static)
20931 initializer = cp_parser_save_nsdmi (parser);
20932 else if (cxx_dialect >= cxx11)
20934 bool nonconst;
20935 /* Don't require a constant rvalue in C++11, since we
20936 might want a reference constant. We'll enforce
20937 constancy later. */
20938 cp_lexer_consume_token (parser->lexer);
20939 /* Parse the initializer. */
20940 initializer = cp_parser_initializer_clause (parser,
20941 &nonconst);
20943 else
20944 /* Parse the initializer. */
20945 initializer = cp_parser_constant_initializer (parser);
20947 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20948 && !function_declarator_p (declarator))
20950 bool x;
20951 if (decl_specifiers.storage_class != sc_static)
20952 initializer = cp_parser_save_nsdmi (parser);
20953 else
20954 initializer = cp_parser_initializer (parser, &x, &x);
20956 /* Otherwise, there is no initializer. */
20957 else
20958 initializer = NULL_TREE;
20960 /* See if we are probably looking at a function
20961 definition. We are certainly not looking at a
20962 member-declarator. Calling `grokfield' has
20963 side-effects, so we must not do it unless we are sure
20964 that we are looking at a member-declarator. */
20965 if (cp_parser_token_starts_function_definition_p
20966 (cp_lexer_peek_token (parser->lexer)))
20968 /* The grammar does not allow a pure-specifier to be
20969 used when a member function is defined. (It is
20970 possible that this fact is an oversight in the
20971 standard, since a pure function may be defined
20972 outside of the class-specifier. */
20973 if (initializer && initializer_token_start)
20974 error_at (initializer_token_start->location,
20975 "pure-specifier on function-definition");
20976 decl = cp_parser_save_member_function_body (parser,
20977 &decl_specifiers,
20978 declarator,
20979 attributes);
20980 if (parser->fully_implicit_function_template_p)
20981 decl = finish_fully_implicit_template (parser, decl);
20982 /* If the member was not a friend, declare it here. */
20983 if (!friend_p)
20984 finish_member_declaration (decl);
20985 /* Peek at the next token. */
20986 token = cp_lexer_peek_token (parser->lexer);
20987 /* If the next token is a semicolon, consume it. */
20988 if (token->type == CPP_SEMICOLON)
20989 cp_lexer_consume_token (parser->lexer);
20990 goto out;
20992 else
20993 if (declarator->kind == cdk_function)
20994 declarator->id_loc = token->location;
20995 /* Create the declaration. */
20996 decl = grokfield (declarator, &decl_specifiers,
20997 initializer, /*init_const_expr_p=*/true,
20998 asm_specification, attributes);
20999 if (parser->fully_implicit_function_template_p)
21001 if (friend_p)
21002 finish_fully_implicit_template (parser, 0);
21003 else
21004 decl = finish_fully_implicit_template (parser, decl);
21008 cp_finalize_omp_declare_simd (parser, decl);
21010 /* Reset PREFIX_ATTRIBUTES. */
21011 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21012 attributes = TREE_CHAIN (attributes);
21013 if (attributes)
21014 TREE_CHAIN (attributes) = NULL_TREE;
21016 /* If there is any qualification still in effect, clear it
21017 now; we will be starting fresh with the next declarator. */
21018 parser->scope = NULL_TREE;
21019 parser->qualifying_scope = NULL_TREE;
21020 parser->object_scope = NULL_TREE;
21021 /* If it's a `,', then there are more declarators. */
21022 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21024 cp_lexer_consume_token (parser->lexer);
21025 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21027 cp_token *token = cp_lexer_previous_token (parser->lexer);
21028 error_at (token->location,
21029 "stray %<,%> at end of member declaration");
21032 /* If the next token isn't a `;', then we have a parse error. */
21033 else if (cp_lexer_next_token_is_not (parser->lexer,
21034 CPP_SEMICOLON))
21036 /* The next token might be a ways away from where the
21037 actual semicolon is missing. Find the previous token
21038 and use that for our error position. */
21039 cp_token *token = cp_lexer_previous_token (parser->lexer);
21040 error_at (token->location,
21041 "expected %<;%> at end of member declaration");
21043 /* Assume that the user meant to provide a semicolon. If
21044 we were to cp_parser_skip_to_end_of_statement, we might
21045 skip to a semicolon inside a member function definition
21046 and issue nonsensical error messages. */
21047 assume_semicolon = true;
21050 if (decl)
21052 /* Add DECL to the list of members. */
21053 if (!friend_p)
21054 finish_member_declaration (decl);
21056 if (TREE_CODE (decl) == FUNCTION_DECL)
21057 cp_parser_save_default_args (parser, decl);
21058 else if (TREE_CODE (decl) == FIELD_DECL
21059 && !DECL_C_BIT_FIELD (decl)
21060 && DECL_INITIAL (decl))
21061 /* Add DECL to the queue of NSDMI to be parsed later. */
21062 vec_safe_push (unparsed_nsdmis, decl);
21065 if (assume_semicolon)
21066 goto out;
21070 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21071 out:
21072 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21075 /* Parse a pure-specifier.
21077 pure-specifier:
21080 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21081 Otherwise, ERROR_MARK_NODE is returned. */
21083 static tree
21084 cp_parser_pure_specifier (cp_parser* parser)
21086 cp_token *token;
21088 /* Look for the `=' token. */
21089 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21090 return error_mark_node;
21091 /* Look for the `0' token. */
21092 token = cp_lexer_peek_token (parser->lexer);
21094 if (token->type == CPP_EOF
21095 || token->type == CPP_PRAGMA_EOL)
21096 return error_mark_node;
21098 cp_lexer_consume_token (parser->lexer);
21100 /* Accept = default or = delete in c++0x mode. */
21101 if (token->keyword == RID_DEFAULT
21102 || token->keyword == RID_DELETE)
21104 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21105 return token->u.value;
21108 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21109 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21111 cp_parser_error (parser,
21112 "invalid pure specifier (only %<= 0%> is allowed)");
21113 cp_parser_skip_to_end_of_statement (parser);
21114 return error_mark_node;
21116 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21118 error_at (token->location, "templates may not be %<virtual%>");
21119 return error_mark_node;
21122 return integer_zero_node;
21125 /* Parse a constant-initializer.
21127 constant-initializer:
21128 = constant-expression
21130 Returns a representation of the constant-expression. */
21132 static tree
21133 cp_parser_constant_initializer (cp_parser* parser)
21135 /* Look for the `=' token. */
21136 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21137 return error_mark_node;
21139 /* It is invalid to write:
21141 struct S { static const int i = { 7 }; };
21144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21146 cp_parser_error (parser,
21147 "a brace-enclosed initializer is not allowed here");
21148 /* Consume the opening brace. */
21149 cp_lexer_consume_token (parser->lexer);
21150 /* Skip the initializer. */
21151 cp_parser_skip_to_closing_brace (parser);
21152 /* Look for the trailing `}'. */
21153 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21155 return error_mark_node;
21158 return cp_parser_constant_expression (parser);
21161 /* Derived classes [gram.class.derived] */
21163 /* Parse a base-clause.
21165 base-clause:
21166 : base-specifier-list
21168 base-specifier-list:
21169 base-specifier ... [opt]
21170 base-specifier-list , base-specifier ... [opt]
21172 Returns a TREE_LIST representing the base-classes, in the order in
21173 which they were declared. The representation of each node is as
21174 described by cp_parser_base_specifier.
21176 In the case that no bases are specified, this function will return
21177 NULL_TREE, not ERROR_MARK_NODE. */
21179 static tree
21180 cp_parser_base_clause (cp_parser* parser)
21182 tree bases = NULL_TREE;
21184 /* Look for the `:' that begins the list. */
21185 cp_parser_require (parser, CPP_COLON, RT_COLON);
21187 /* Scan the base-specifier-list. */
21188 while (true)
21190 cp_token *token;
21191 tree base;
21192 bool pack_expansion_p = false;
21194 /* Look for the base-specifier. */
21195 base = cp_parser_base_specifier (parser);
21196 /* Look for the (optional) ellipsis. */
21197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21199 /* Consume the `...'. */
21200 cp_lexer_consume_token (parser->lexer);
21202 pack_expansion_p = true;
21205 /* Add BASE to the front of the list. */
21206 if (base && base != error_mark_node)
21208 if (pack_expansion_p)
21209 /* Make this a pack expansion type. */
21210 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21212 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21214 TREE_CHAIN (base) = bases;
21215 bases = base;
21218 /* Peek at the next token. */
21219 token = cp_lexer_peek_token (parser->lexer);
21220 /* If it's not a comma, then the list is complete. */
21221 if (token->type != CPP_COMMA)
21222 break;
21223 /* Consume the `,'. */
21224 cp_lexer_consume_token (parser->lexer);
21227 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21228 base class had a qualified name. However, the next name that
21229 appears is certainly not qualified. */
21230 parser->scope = NULL_TREE;
21231 parser->qualifying_scope = NULL_TREE;
21232 parser->object_scope = NULL_TREE;
21234 return nreverse (bases);
21237 /* Parse a base-specifier.
21239 base-specifier:
21240 :: [opt] nested-name-specifier [opt] class-name
21241 virtual access-specifier [opt] :: [opt] nested-name-specifier
21242 [opt] class-name
21243 access-specifier virtual [opt] :: [opt] nested-name-specifier
21244 [opt] class-name
21246 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21247 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21248 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21249 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21251 static tree
21252 cp_parser_base_specifier (cp_parser* parser)
21254 cp_token *token;
21255 bool done = false;
21256 bool virtual_p = false;
21257 bool duplicate_virtual_error_issued_p = false;
21258 bool duplicate_access_error_issued_p = false;
21259 bool class_scope_p, template_p;
21260 tree access = access_default_node;
21261 tree type;
21263 /* Process the optional `virtual' and `access-specifier'. */
21264 while (!done)
21266 /* Peek at the next token. */
21267 token = cp_lexer_peek_token (parser->lexer);
21268 /* Process `virtual'. */
21269 switch (token->keyword)
21271 case RID_VIRTUAL:
21272 /* If `virtual' appears more than once, issue an error. */
21273 if (virtual_p && !duplicate_virtual_error_issued_p)
21275 cp_parser_error (parser,
21276 "%<virtual%> specified more than once in base-specified");
21277 duplicate_virtual_error_issued_p = true;
21280 virtual_p = true;
21282 /* Consume the `virtual' token. */
21283 cp_lexer_consume_token (parser->lexer);
21285 break;
21287 case RID_PUBLIC:
21288 case RID_PROTECTED:
21289 case RID_PRIVATE:
21290 /* If more than one access specifier appears, issue an
21291 error. */
21292 if (access != access_default_node
21293 && !duplicate_access_error_issued_p)
21295 cp_parser_error (parser,
21296 "more than one access specifier in base-specified");
21297 duplicate_access_error_issued_p = true;
21300 access = ridpointers[(int) token->keyword];
21302 /* Consume the access-specifier. */
21303 cp_lexer_consume_token (parser->lexer);
21305 break;
21307 default:
21308 done = true;
21309 break;
21312 /* It is not uncommon to see programs mechanically, erroneously, use
21313 the 'typename' keyword to denote (dependent) qualified types
21314 as base classes. */
21315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21317 token = cp_lexer_peek_token (parser->lexer);
21318 if (!processing_template_decl)
21319 error_at (token->location,
21320 "keyword %<typename%> not allowed outside of templates");
21321 else
21322 error_at (token->location,
21323 "keyword %<typename%> not allowed in this context "
21324 "(the base class is implicitly a type)");
21325 cp_lexer_consume_token (parser->lexer);
21328 /* Look for the optional `::' operator. */
21329 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21330 /* Look for the nested-name-specifier. The simplest way to
21331 implement:
21333 [temp.res]
21335 The keyword `typename' is not permitted in a base-specifier or
21336 mem-initializer; in these contexts a qualified name that
21337 depends on a template-parameter is implicitly assumed to be a
21338 type name.
21340 is to pretend that we have seen the `typename' keyword at this
21341 point. */
21342 cp_parser_nested_name_specifier_opt (parser,
21343 /*typename_keyword_p=*/true,
21344 /*check_dependency_p=*/true,
21345 typename_type,
21346 /*is_declaration=*/true);
21347 /* If the base class is given by a qualified name, assume that names
21348 we see are type names or templates, as appropriate. */
21349 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21350 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21352 if (!parser->scope
21353 && cp_lexer_next_token_is_decltype (parser->lexer))
21354 /* DR 950 allows decltype as a base-specifier. */
21355 type = cp_parser_decltype (parser);
21356 else
21358 /* Otherwise, look for the class-name. */
21359 type = cp_parser_class_name (parser,
21360 class_scope_p,
21361 template_p,
21362 typename_type,
21363 /*check_dependency_p=*/true,
21364 /*class_head_p=*/false,
21365 /*is_declaration=*/true);
21366 type = TREE_TYPE (type);
21369 if (type == error_mark_node)
21370 return error_mark_node;
21372 return finish_base_specifier (type, access, virtual_p);
21375 /* Exception handling [gram.exception] */
21377 /* Parse an (optional) noexcept-specification.
21379 noexcept-specification:
21380 noexcept ( constant-expression ) [opt]
21382 If no noexcept-specification is present, returns NULL_TREE.
21383 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21384 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21385 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21386 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21387 in which case a boolean condition is returned instead. */
21389 static tree
21390 cp_parser_noexcept_specification_opt (cp_parser* parser,
21391 bool require_constexpr,
21392 bool* consumed_expr,
21393 bool return_cond)
21395 cp_token *token;
21396 const char *saved_message;
21398 /* Peek at the next token. */
21399 token = cp_lexer_peek_token (parser->lexer);
21401 /* Is it a noexcept-specification? */
21402 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21404 tree expr;
21405 cp_lexer_consume_token (parser->lexer);
21407 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21409 cp_lexer_consume_token (parser->lexer);
21411 if (require_constexpr)
21413 /* Types may not be defined in an exception-specification. */
21414 saved_message = parser->type_definition_forbidden_message;
21415 parser->type_definition_forbidden_message
21416 = G_("types may not be defined in an exception-specification");
21418 expr = cp_parser_constant_expression (parser);
21420 /* Restore the saved message. */
21421 parser->type_definition_forbidden_message = saved_message;
21423 else
21425 expr = cp_parser_expression (parser);
21426 *consumed_expr = true;
21429 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21431 else
21433 expr = boolean_true_node;
21434 if (!require_constexpr)
21435 *consumed_expr = false;
21438 /* We cannot build a noexcept-spec right away because this will check
21439 that expr is a constexpr. */
21440 if (!return_cond)
21441 return build_noexcept_spec (expr, tf_warning_or_error);
21442 else
21443 return expr;
21445 else
21446 return NULL_TREE;
21449 /* Parse an (optional) exception-specification.
21451 exception-specification:
21452 throw ( type-id-list [opt] )
21454 Returns a TREE_LIST representing the exception-specification. The
21455 TREE_VALUE of each node is a type. */
21457 static tree
21458 cp_parser_exception_specification_opt (cp_parser* parser)
21460 cp_token *token;
21461 tree type_id_list;
21462 const char *saved_message;
21464 /* Peek at the next token. */
21465 token = cp_lexer_peek_token (parser->lexer);
21467 /* Is it a noexcept-specification? */
21468 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21469 false);
21470 if (type_id_list != NULL_TREE)
21471 return type_id_list;
21473 /* If it's not `throw', then there's no exception-specification. */
21474 if (!cp_parser_is_keyword (token, RID_THROW))
21475 return NULL_TREE;
21477 #if 0
21478 /* Enable this once a lot of code has transitioned to noexcept? */
21479 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21480 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21481 "deprecated in C++0x; use %<noexcept%> instead");
21482 #endif
21484 /* Consume the `throw'. */
21485 cp_lexer_consume_token (parser->lexer);
21487 /* Look for the `('. */
21488 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21490 /* Peek at the next token. */
21491 token = cp_lexer_peek_token (parser->lexer);
21492 /* If it's not a `)', then there is a type-id-list. */
21493 if (token->type != CPP_CLOSE_PAREN)
21495 /* Types may not be defined in an exception-specification. */
21496 saved_message = parser->type_definition_forbidden_message;
21497 parser->type_definition_forbidden_message
21498 = G_("types may not be defined in an exception-specification");
21499 /* Parse the type-id-list. */
21500 type_id_list = cp_parser_type_id_list (parser);
21501 /* Restore the saved message. */
21502 parser->type_definition_forbidden_message = saved_message;
21504 else
21505 type_id_list = empty_except_spec;
21507 /* Look for the `)'. */
21508 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21510 return type_id_list;
21513 /* Parse an (optional) type-id-list.
21515 type-id-list:
21516 type-id ... [opt]
21517 type-id-list , type-id ... [opt]
21519 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21520 in the order that the types were presented. */
21522 static tree
21523 cp_parser_type_id_list (cp_parser* parser)
21525 tree types = NULL_TREE;
21527 while (true)
21529 cp_token *token;
21530 tree type;
21532 /* Get the next type-id. */
21533 type = cp_parser_type_id (parser);
21534 /* Parse the optional ellipsis. */
21535 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21537 /* Consume the `...'. */
21538 cp_lexer_consume_token (parser->lexer);
21540 /* Turn the type into a pack expansion expression. */
21541 type = make_pack_expansion (type);
21543 /* Add it to the list. */
21544 types = add_exception_specifier (types, type, /*complain=*/1);
21545 /* Peek at the next token. */
21546 token = cp_lexer_peek_token (parser->lexer);
21547 /* If it is not a `,', we are done. */
21548 if (token->type != CPP_COMMA)
21549 break;
21550 /* Consume the `,'. */
21551 cp_lexer_consume_token (parser->lexer);
21554 return nreverse (types);
21557 /* Parse a try-block.
21559 try-block:
21560 try compound-statement handler-seq */
21562 static tree
21563 cp_parser_try_block (cp_parser* parser)
21565 tree try_block;
21567 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21568 if (parser->in_function_body
21569 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21570 error ("%<try%> in %<constexpr%> function");
21572 try_block = begin_try_block ();
21573 cp_parser_compound_statement (parser, NULL, true, false);
21574 finish_try_block (try_block);
21575 cp_parser_handler_seq (parser);
21576 finish_handler_sequence (try_block);
21578 return try_block;
21581 /* Parse a function-try-block.
21583 function-try-block:
21584 try ctor-initializer [opt] function-body handler-seq */
21586 static bool
21587 cp_parser_function_try_block (cp_parser* parser)
21589 tree compound_stmt;
21590 tree try_block;
21591 bool ctor_initializer_p;
21593 /* Look for the `try' keyword. */
21594 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21595 return false;
21596 /* Let the rest of the front end know where we are. */
21597 try_block = begin_function_try_block (&compound_stmt);
21598 /* Parse the function-body. */
21599 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21600 (parser, /*in_function_try_block=*/true);
21601 /* We're done with the `try' part. */
21602 finish_function_try_block (try_block);
21603 /* Parse the handlers. */
21604 cp_parser_handler_seq (parser);
21605 /* We're done with the handlers. */
21606 finish_function_handler_sequence (try_block, compound_stmt);
21608 return ctor_initializer_p;
21611 /* Parse a handler-seq.
21613 handler-seq:
21614 handler handler-seq [opt] */
21616 static void
21617 cp_parser_handler_seq (cp_parser* parser)
21619 while (true)
21621 cp_token *token;
21623 /* Parse the handler. */
21624 cp_parser_handler (parser);
21625 /* Peek at the next token. */
21626 token = cp_lexer_peek_token (parser->lexer);
21627 /* If it's not `catch' then there are no more handlers. */
21628 if (!cp_parser_is_keyword (token, RID_CATCH))
21629 break;
21633 /* Parse a handler.
21635 handler:
21636 catch ( exception-declaration ) compound-statement */
21638 static void
21639 cp_parser_handler (cp_parser* parser)
21641 tree handler;
21642 tree declaration;
21644 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21645 handler = begin_handler ();
21646 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21647 declaration = cp_parser_exception_declaration (parser);
21648 finish_handler_parms (declaration, handler);
21649 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21650 cp_parser_compound_statement (parser, NULL, false, false);
21651 finish_handler (handler);
21654 /* Parse an exception-declaration.
21656 exception-declaration:
21657 type-specifier-seq declarator
21658 type-specifier-seq abstract-declarator
21659 type-specifier-seq
21662 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21663 ellipsis variant is used. */
21665 static tree
21666 cp_parser_exception_declaration (cp_parser* parser)
21668 cp_decl_specifier_seq type_specifiers;
21669 cp_declarator *declarator;
21670 const char *saved_message;
21672 /* If it's an ellipsis, it's easy to handle. */
21673 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21675 /* Consume the `...' token. */
21676 cp_lexer_consume_token (parser->lexer);
21677 return NULL_TREE;
21680 /* Types may not be defined in exception-declarations. */
21681 saved_message = parser->type_definition_forbidden_message;
21682 parser->type_definition_forbidden_message
21683 = G_("types may not be defined in exception-declarations");
21685 /* Parse the type-specifier-seq. */
21686 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21687 /*is_trailing_return=*/false,
21688 &type_specifiers);
21689 /* If it's a `)', then there is no declarator. */
21690 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21691 declarator = NULL;
21692 else
21693 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21694 /*ctor_dtor_or_conv_p=*/NULL,
21695 /*parenthesized_p=*/NULL,
21696 /*member_p=*/false,
21697 /*friend_p=*/false);
21699 /* Restore the saved message. */
21700 parser->type_definition_forbidden_message = saved_message;
21702 if (!type_specifiers.any_specifiers_p)
21703 return error_mark_node;
21705 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21708 /* Parse a throw-expression.
21710 throw-expression:
21711 throw assignment-expression [opt]
21713 Returns a THROW_EXPR representing the throw-expression. */
21715 static tree
21716 cp_parser_throw_expression (cp_parser* parser)
21718 tree expression;
21719 cp_token* token;
21721 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21722 token = cp_lexer_peek_token (parser->lexer);
21723 /* Figure out whether or not there is an assignment-expression
21724 following the "throw" keyword. */
21725 if (token->type == CPP_COMMA
21726 || token->type == CPP_SEMICOLON
21727 || token->type == CPP_CLOSE_PAREN
21728 || token->type == CPP_CLOSE_SQUARE
21729 || token->type == CPP_CLOSE_BRACE
21730 || token->type == CPP_COLON)
21731 expression = NULL_TREE;
21732 else
21733 expression = cp_parser_assignment_expression (parser);
21735 return build_throw (expression);
21738 /* GNU Extensions */
21740 /* Parse an (optional) asm-specification.
21742 asm-specification:
21743 asm ( string-literal )
21745 If the asm-specification is present, returns a STRING_CST
21746 corresponding to the string-literal. Otherwise, returns
21747 NULL_TREE. */
21749 static tree
21750 cp_parser_asm_specification_opt (cp_parser* parser)
21752 cp_token *token;
21753 tree asm_specification;
21755 /* Peek at the next token. */
21756 token = cp_lexer_peek_token (parser->lexer);
21757 /* If the next token isn't the `asm' keyword, then there's no
21758 asm-specification. */
21759 if (!cp_parser_is_keyword (token, RID_ASM))
21760 return NULL_TREE;
21762 /* Consume the `asm' token. */
21763 cp_lexer_consume_token (parser->lexer);
21764 /* Look for the `('. */
21765 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21767 /* Look for the string-literal. */
21768 asm_specification = cp_parser_string_literal (parser, false, false);
21770 /* Look for the `)'. */
21771 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21773 return asm_specification;
21776 /* Parse an asm-operand-list.
21778 asm-operand-list:
21779 asm-operand
21780 asm-operand-list , asm-operand
21782 asm-operand:
21783 string-literal ( expression )
21784 [ string-literal ] string-literal ( expression )
21786 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21787 each node is the expression. The TREE_PURPOSE is itself a
21788 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21789 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21790 is a STRING_CST for the string literal before the parenthesis. Returns
21791 ERROR_MARK_NODE if any of the operands are invalid. */
21793 static tree
21794 cp_parser_asm_operand_list (cp_parser* parser)
21796 tree asm_operands = NULL_TREE;
21797 bool invalid_operands = false;
21799 while (true)
21801 tree string_literal;
21802 tree expression;
21803 tree name;
21805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21807 /* Consume the `[' token. */
21808 cp_lexer_consume_token (parser->lexer);
21809 /* Read the operand name. */
21810 name = cp_parser_identifier (parser);
21811 if (name != error_mark_node)
21812 name = build_string (IDENTIFIER_LENGTH (name),
21813 IDENTIFIER_POINTER (name));
21814 /* Look for the closing `]'. */
21815 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21817 else
21818 name = NULL_TREE;
21819 /* Look for the string-literal. */
21820 string_literal = cp_parser_string_literal (parser, false, false);
21822 /* Look for the `('. */
21823 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21824 /* Parse the expression. */
21825 expression = cp_parser_expression (parser);
21826 /* Look for the `)'. */
21827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21829 if (name == error_mark_node
21830 || string_literal == error_mark_node
21831 || expression == error_mark_node)
21832 invalid_operands = true;
21834 /* Add this operand to the list. */
21835 asm_operands = tree_cons (build_tree_list (name, string_literal),
21836 expression,
21837 asm_operands);
21838 /* If the next token is not a `,', there are no more
21839 operands. */
21840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21841 break;
21842 /* Consume the `,'. */
21843 cp_lexer_consume_token (parser->lexer);
21846 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21849 /* Parse an asm-clobber-list.
21851 asm-clobber-list:
21852 string-literal
21853 asm-clobber-list , string-literal
21855 Returns a TREE_LIST, indicating the clobbers in the order that they
21856 appeared. The TREE_VALUE of each node is a STRING_CST. */
21858 static tree
21859 cp_parser_asm_clobber_list (cp_parser* parser)
21861 tree clobbers = NULL_TREE;
21863 while (true)
21865 tree string_literal;
21867 /* Look for the string literal. */
21868 string_literal = cp_parser_string_literal (parser, false, false);
21869 /* Add it to the list. */
21870 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21871 /* If the next token is not a `,', then the list is
21872 complete. */
21873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21874 break;
21875 /* Consume the `,' token. */
21876 cp_lexer_consume_token (parser->lexer);
21879 return clobbers;
21882 /* Parse an asm-label-list.
21884 asm-label-list:
21885 identifier
21886 asm-label-list , identifier
21888 Returns a TREE_LIST, indicating the labels in the order that they
21889 appeared. The TREE_VALUE of each node is a label. */
21891 static tree
21892 cp_parser_asm_label_list (cp_parser* parser)
21894 tree labels = NULL_TREE;
21896 while (true)
21898 tree identifier, label, name;
21900 /* Look for the identifier. */
21901 identifier = cp_parser_identifier (parser);
21902 if (!error_operand_p (identifier))
21904 label = lookup_label (identifier);
21905 if (TREE_CODE (label) == LABEL_DECL)
21907 TREE_USED (label) = 1;
21908 check_goto (label);
21909 name = build_string (IDENTIFIER_LENGTH (identifier),
21910 IDENTIFIER_POINTER (identifier));
21911 labels = tree_cons (name, label, labels);
21914 /* If the next token is not a `,', then the list is
21915 complete. */
21916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21917 break;
21918 /* Consume the `,' token. */
21919 cp_lexer_consume_token (parser->lexer);
21922 return nreverse (labels);
21925 /* Return TRUE iff the next tokens in the stream are possibly the
21926 beginning of a GNU extension attribute. */
21928 static bool
21929 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21931 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21934 /* Return TRUE iff the next tokens in the stream are possibly the
21935 beginning of a standard C++-11 attribute specifier. */
21937 static bool
21938 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21940 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21943 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21944 beginning of a standard C++-11 attribute specifier. */
21946 static bool
21947 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21949 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21951 return (cxx_dialect >= cxx11
21952 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21953 || (token->type == CPP_OPEN_SQUARE
21954 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21955 && token->type == CPP_OPEN_SQUARE)));
21958 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21959 beginning of a GNU extension attribute. */
21961 static bool
21962 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21964 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21966 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21969 /* Return true iff the next tokens can be the beginning of either a
21970 GNU attribute list, or a standard C++11 attribute sequence. */
21972 static bool
21973 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21975 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21976 || cp_next_tokens_can_be_std_attribute_p (parser));
21979 /* Return true iff the next Nth tokens can be the beginning of either
21980 a GNU attribute list, or a standard C++11 attribute sequence. */
21982 static bool
21983 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21985 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21986 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21989 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21990 of GNU attributes, or return NULL. */
21992 static tree
21993 cp_parser_attributes_opt (cp_parser *parser)
21995 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21996 return cp_parser_gnu_attributes_opt (parser);
21997 return cp_parser_std_attribute_spec_seq (parser);
22000 #define CILK_SIMD_FN_CLAUSE_MASK \
22001 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22002 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22003 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22004 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22005 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22007 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22008 vector [(<clauses>)] */
22010 static void
22011 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22013 bool first_p = parser->cilk_simd_fn_info == NULL;
22014 cp_token *token = v_token;
22015 if (first_p)
22017 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22018 parser->cilk_simd_fn_info->error_seen = false;
22019 parser->cilk_simd_fn_info->fndecl_seen = false;
22020 parser->cilk_simd_fn_info->tokens = vNULL;
22022 int paren_scope = 0;
22023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22025 cp_lexer_consume_token (parser->lexer);
22026 v_token = cp_lexer_peek_token (parser->lexer);
22027 paren_scope++;
22029 while (paren_scope > 0)
22031 token = cp_lexer_peek_token (parser->lexer);
22032 if (token->type == CPP_OPEN_PAREN)
22033 paren_scope++;
22034 else if (token->type == CPP_CLOSE_PAREN)
22035 paren_scope--;
22036 /* Do not push the last ')' */
22037 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22038 cp_lexer_consume_token (parser->lexer);
22041 token->type = CPP_PRAGMA_EOL;
22042 parser->lexer->next_token = token;
22043 cp_lexer_consume_token (parser->lexer);
22045 struct cp_token_cache *cp
22046 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22047 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22050 /* Parse an (optional) series of attributes.
22052 attributes:
22053 attributes attribute
22055 attribute:
22056 __attribute__ (( attribute-list [opt] ))
22058 The return value is as for cp_parser_gnu_attribute_list. */
22060 static tree
22061 cp_parser_gnu_attributes_opt (cp_parser* parser)
22063 tree attributes = NULL_TREE;
22065 while (true)
22067 cp_token *token;
22068 tree attribute_list;
22069 bool ok = true;
22071 /* Peek at the next token. */
22072 token = cp_lexer_peek_token (parser->lexer);
22073 /* If it's not `__attribute__', then we're done. */
22074 if (token->keyword != RID_ATTRIBUTE)
22075 break;
22077 /* Consume the `__attribute__' keyword. */
22078 cp_lexer_consume_token (parser->lexer);
22079 /* Look for the two `(' tokens. */
22080 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22081 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22083 /* Peek at the next token. */
22084 token = cp_lexer_peek_token (parser->lexer);
22085 if (token->type != CPP_CLOSE_PAREN)
22086 /* Parse the attribute-list. */
22087 attribute_list = cp_parser_gnu_attribute_list (parser);
22088 else
22089 /* If the next token is a `)', then there is no attribute
22090 list. */
22091 attribute_list = NULL;
22093 /* Look for the two `)' tokens. */
22094 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22095 ok = false;
22096 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22097 ok = false;
22098 if (!ok)
22099 cp_parser_skip_to_end_of_statement (parser);
22101 /* Add these new attributes to the list. */
22102 attributes = chainon (attributes, attribute_list);
22105 return attributes;
22108 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22109 "__vector" or "__vector__." */
22111 static inline bool
22112 is_cilkplus_vector_p (tree name)
22114 if (flag_cilkplus && is_attribute_p ("vector", name))
22115 return true;
22116 return false;
22119 /* Parse a GNU attribute-list.
22121 attribute-list:
22122 attribute
22123 attribute-list , attribute
22125 attribute:
22126 identifier
22127 identifier ( identifier )
22128 identifier ( identifier , expression-list )
22129 identifier ( expression-list )
22131 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22132 to an attribute. The TREE_PURPOSE of each node is the identifier
22133 indicating which attribute is in use. The TREE_VALUE represents
22134 the arguments, if any. */
22136 static tree
22137 cp_parser_gnu_attribute_list (cp_parser* parser)
22139 tree attribute_list = NULL_TREE;
22140 bool save_translate_strings_p = parser->translate_strings_p;
22142 parser->translate_strings_p = false;
22143 while (true)
22145 cp_token *token;
22146 tree identifier;
22147 tree attribute;
22149 /* Look for the identifier. We also allow keywords here; for
22150 example `__attribute__ ((const))' is legal. */
22151 token = cp_lexer_peek_token (parser->lexer);
22152 if (token->type == CPP_NAME
22153 || token->type == CPP_KEYWORD)
22155 tree arguments = NULL_TREE;
22157 /* Consume the token, but save it since we need it for the
22158 SIMD enabled function parsing. */
22159 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22161 /* Save away the identifier that indicates which attribute
22162 this is. */
22163 identifier = (token->type == CPP_KEYWORD)
22164 /* For keywords, use the canonical spelling, not the
22165 parsed identifier. */
22166 ? ridpointers[(int) token->keyword]
22167 : id_token->u.value;
22169 attribute = build_tree_list (identifier, NULL_TREE);
22171 /* Peek at the next token. */
22172 token = cp_lexer_peek_token (parser->lexer);
22173 /* If it's an `(', then parse the attribute arguments. */
22174 if (token->type == CPP_OPEN_PAREN)
22176 vec<tree, va_gc> *vec;
22177 int attr_flag = (attribute_takes_identifier_p (identifier)
22178 ? id_attr : normal_attr);
22179 if (is_cilkplus_vector_p (identifier))
22181 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22182 continue;
22184 else
22185 vec = cp_parser_parenthesized_expression_list
22186 (parser, attr_flag, /*cast_p=*/false,
22187 /*allow_expansion_p=*/false,
22188 /*non_constant_p=*/NULL);
22189 if (vec == NULL)
22190 arguments = error_mark_node;
22191 else
22193 arguments = build_tree_list_vec (vec);
22194 release_tree_vector (vec);
22196 /* Save the arguments away. */
22197 TREE_VALUE (attribute) = arguments;
22199 else if (is_cilkplus_vector_p (identifier))
22201 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22202 continue;
22205 if (arguments != error_mark_node)
22207 /* Add this attribute to the list. */
22208 TREE_CHAIN (attribute) = attribute_list;
22209 attribute_list = attribute;
22212 token = cp_lexer_peek_token (parser->lexer);
22214 /* Now, look for more attributes. If the next token isn't a
22215 `,', we're done. */
22216 if (token->type != CPP_COMMA)
22217 break;
22219 /* Consume the comma and keep going. */
22220 cp_lexer_consume_token (parser->lexer);
22222 parser->translate_strings_p = save_translate_strings_p;
22224 /* We built up the list in reverse order. */
22225 return nreverse (attribute_list);
22228 /* Parse a standard C++11 attribute.
22230 The returned representation is a TREE_LIST which TREE_PURPOSE is
22231 the scoped name of the attribute, and the TREE_VALUE is its
22232 arguments list.
22234 Note that the scoped name of the attribute is itself a TREE_LIST
22235 which TREE_PURPOSE is the namespace of the attribute, and
22236 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22237 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22238 and which TREE_PURPOSE is directly the attribute name.
22240 Clients of the attribute code should use get_attribute_namespace
22241 and get_attribute_name to get the actual namespace and name of
22242 attributes, regardless of their being GNU or C++11 attributes.
22244 attribute:
22245 attribute-token attribute-argument-clause [opt]
22247 attribute-token:
22248 identifier
22249 attribute-scoped-token
22251 attribute-scoped-token:
22252 attribute-namespace :: identifier
22254 attribute-namespace:
22255 identifier
22257 attribute-argument-clause:
22258 ( balanced-token-seq )
22260 balanced-token-seq:
22261 balanced-token [opt]
22262 balanced-token-seq balanced-token
22264 balanced-token:
22265 ( balanced-token-seq )
22266 [ balanced-token-seq ]
22267 { balanced-token-seq }. */
22269 static tree
22270 cp_parser_std_attribute (cp_parser *parser)
22272 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22273 cp_token *token;
22275 /* First, parse name of the the attribute, a.k.a
22276 attribute-token. */
22278 token = cp_lexer_peek_token (parser->lexer);
22279 if (token->type == CPP_NAME)
22280 attr_id = token->u.value;
22281 else if (token->type == CPP_KEYWORD)
22282 attr_id = ridpointers[(int) token->keyword];
22283 else if (token->flags & NAMED_OP)
22284 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22286 if (attr_id == NULL_TREE)
22287 return NULL_TREE;
22289 cp_lexer_consume_token (parser->lexer);
22291 token = cp_lexer_peek_token (parser->lexer);
22292 if (token->type == CPP_SCOPE)
22294 /* We are seeing a scoped attribute token. */
22296 cp_lexer_consume_token (parser->lexer);
22297 attr_ns = attr_id;
22299 token = cp_lexer_consume_token (parser->lexer);
22300 if (token->type == CPP_NAME)
22301 attr_id = token->u.value;
22302 else if (token->type == CPP_KEYWORD)
22303 attr_id = ridpointers[(int) token->keyword];
22304 else
22306 error_at (token->location,
22307 "expected an identifier for the attribute name");
22308 return error_mark_node;
22310 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22311 NULL_TREE);
22312 token = cp_lexer_peek_token (parser->lexer);
22314 else
22316 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22317 NULL_TREE);
22318 /* C++11 noreturn attribute is equivalent to GNU's. */
22319 if (is_attribute_p ("noreturn", attr_id))
22320 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22321 /* C++14 deprecated attribute is equivalent to GNU's. */
22322 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22324 if (cxx_dialect == cxx11)
22325 pedwarn (token->location, OPT_Wpedantic,
22326 "%<deprecated%> is a C++14 feature;"
22327 " use %<gnu::deprecated%>");
22328 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22332 /* Now parse the optional argument clause of the attribute. */
22334 if (token->type != CPP_OPEN_PAREN)
22335 return attribute;
22338 vec<tree, va_gc> *vec;
22339 int attr_flag = normal_attr;
22341 if (attr_ns == get_identifier ("gnu")
22342 && attribute_takes_identifier_p (attr_id))
22343 /* A GNU attribute that takes an identifier in parameter. */
22344 attr_flag = id_attr;
22346 vec = cp_parser_parenthesized_expression_list
22347 (parser, attr_flag, /*cast_p=*/false,
22348 /*allow_expansion_p=*/true,
22349 /*non_constant_p=*/NULL);
22350 if (vec == NULL)
22351 arguments = error_mark_node;
22352 else
22354 arguments = build_tree_list_vec (vec);
22355 release_tree_vector (vec);
22358 if (arguments == error_mark_node)
22359 attribute = error_mark_node;
22360 else
22361 TREE_VALUE (attribute) = arguments;
22364 return attribute;
22367 /* Parse a list of standard C++-11 attributes.
22369 attribute-list:
22370 attribute [opt]
22371 attribute-list , attribute[opt]
22372 attribute ...
22373 attribute-list , attribute ...
22376 static tree
22377 cp_parser_std_attribute_list (cp_parser *parser)
22379 tree attributes = NULL_TREE, attribute = NULL_TREE;
22380 cp_token *token = NULL;
22382 while (true)
22384 attribute = cp_parser_std_attribute (parser);
22385 if (attribute == error_mark_node)
22386 break;
22387 if (attribute != NULL_TREE)
22389 TREE_CHAIN (attribute) = attributes;
22390 attributes = attribute;
22392 token = cp_lexer_peek_token (parser->lexer);
22393 if (token->type != CPP_COMMA)
22394 break;
22395 cp_lexer_consume_token (parser->lexer);
22397 attributes = nreverse (attributes);
22398 return attributes;
22401 /* Parse a standard C++-11 attribute specifier.
22403 attribute-specifier:
22404 [ [ attribute-list ] ]
22405 alignment-specifier
22407 alignment-specifier:
22408 alignas ( type-id ... [opt] )
22409 alignas ( alignment-expression ... [opt] ). */
22411 static tree
22412 cp_parser_std_attribute_spec (cp_parser *parser)
22414 tree attributes = NULL_TREE;
22415 cp_token *token = cp_lexer_peek_token (parser->lexer);
22417 if (token->type == CPP_OPEN_SQUARE
22418 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22420 cp_lexer_consume_token (parser->lexer);
22421 cp_lexer_consume_token (parser->lexer);
22423 attributes = cp_parser_std_attribute_list (parser);
22425 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22426 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22427 cp_parser_skip_to_end_of_statement (parser);
22428 else
22429 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22430 when we are sure that we have actually parsed them. */
22431 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22433 else
22435 tree alignas_expr;
22437 /* Look for an alignment-specifier. */
22439 token = cp_lexer_peek_token (parser->lexer);
22441 if (token->type != CPP_KEYWORD
22442 || token->keyword != RID_ALIGNAS)
22443 return NULL_TREE;
22445 cp_lexer_consume_token (parser->lexer);
22446 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22448 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22450 cp_parser_error (parser, "expected %<(%>");
22451 return error_mark_node;
22454 cp_parser_parse_tentatively (parser);
22455 alignas_expr = cp_parser_type_id (parser);
22457 if (!cp_parser_parse_definitely (parser))
22459 gcc_assert (alignas_expr == error_mark_node
22460 || alignas_expr == NULL_TREE);
22462 alignas_expr =
22463 cp_parser_assignment_expression (parser);
22464 if (alignas_expr == error_mark_node)
22465 cp_parser_skip_to_end_of_statement (parser);
22466 if (alignas_expr == NULL_TREE
22467 || alignas_expr == error_mark_node)
22468 return alignas_expr;
22471 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22473 cp_parser_error (parser, "expected %<)%>");
22474 return error_mark_node;
22477 alignas_expr = cxx_alignas_expr (alignas_expr);
22479 /* Build the C++-11 representation of an 'aligned'
22480 attribute. */
22481 attributes =
22482 build_tree_list (build_tree_list (get_identifier ("gnu"),
22483 get_identifier ("aligned")),
22484 build_tree_list (NULL_TREE, alignas_expr));
22487 return attributes;
22490 /* Parse a standard C++-11 attribute-specifier-seq.
22492 attribute-specifier-seq:
22493 attribute-specifier-seq [opt] attribute-specifier
22496 static tree
22497 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22499 tree attr_specs = NULL;
22501 while (true)
22503 tree attr_spec = cp_parser_std_attribute_spec (parser);
22504 if (attr_spec == NULL_TREE)
22505 break;
22506 if (attr_spec == error_mark_node)
22507 return error_mark_node;
22509 TREE_CHAIN (attr_spec) = attr_specs;
22510 attr_specs = attr_spec;
22513 attr_specs = nreverse (attr_specs);
22514 return attr_specs;
22517 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22518 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22519 current value of the PEDANTIC flag, regardless of whether or not
22520 the `__extension__' keyword is present. The caller is responsible
22521 for restoring the value of the PEDANTIC flag. */
22523 static bool
22524 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22526 /* Save the old value of the PEDANTIC flag. */
22527 *saved_pedantic = pedantic;
22529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22531 /* Consume the `__extension__' token. */
22532 cp_lexer_consume_token (parser->lexer);
22533 /* We're not being pedantic while the `__extension__' keyword is
22534 in effect. */
22535 pedantic = 0;
22537 return true;
22540 return false;
22543 /* Parse a label declaration.
22545 label-declaration:
22546 __label__ label-declarator-seq ;
22548 label-declarator-seq:
22549 identifier , label-declarator-seq
22550 identifier */
22552 static void
22553 cp_parser_label_declaration (cp_parser* parser)
22555 /* Look for the `__label__' keyword. */
22556 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22558 while (true)
22560 tree identifier;
22562 /* Look for an identifier. */
22563 identifier = cp_parser_identifier (parser);
22564 /* If we failed, stop. */
22565 if (identifier == error_mark_node)
22566 break;
22567 /* Declare it as a label. */
22568 finish_label_decl (identifier);
22569 /* If the next token is a `;', stop. */
22570 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22571 break;
22572 /* Look for the `,' separating the label declarations. */
22573 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22576 /* Look for the final `;'. */
22577 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22580 /* Support Functions */
22582 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22583 NAME should have one of the representations used for an
22584 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22585 is returned. If PARSER->SCOPE is a dependent type, then a
22586 SCOPE_REF is returned.
22588 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22589 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22590 was formed. Abstractly, such entities should not be passed to this
22591 function, because they do not need to be looked up, but it is
22592 simpler to check for this special case here, rather than at the
22593 call-sites.
22595 In cases not explicitly covered above, this function returns a
22596 DECL, OVERLOAD, or baselink representing the result of the lookup.
22597 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22598 is returned.
22600 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22601 (e.g., "struct") that was used. In that case bindings that do not
22602 refer to types are ignored.
22604 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22605 ignored.
22607 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22608 are ignored.
22610 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22611 types.
22613 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22614 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22615 NULL_TREE otherwise. */
22617 static tree
22618 cp_parser_lookup_name (cp_parser *parser, tree name,
22619 enum tag_types tag_type,
22620 bool is_template,
22621 bool is_namespace,
22622 bool check_dependency,
22623 tree *ambiguous_decls,
22624 location_t name_location)
22626 tree decl;
22627 tree object_type = parser->context->object_type;
22629 /* Assume that the lookup will be unambiguous. */
22630 if (ambiguous_decls)
22631 *ambiguous_decls = NULL_TREE;
22633 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22634 no longer valid. Note that if we are parsing tentatively, and
22635 the parse fails, OBJECT_TYPE will be automatically restored. */
22636 parser->context->object_type = NULL_TREE;
22638 if (name == error_mark_node)
22639 return error_mark_node;
22641 /* A template-id has already been resolved; there is no lookup to
22642 do. */
22643 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22644 return name;
22645 if (BASELINK_P (name))
22647 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22648 == TEMPLATE_ID_EXPR);
22649 return name;
22652 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22653 it should already have been checked to make sure that the name
22654 used matches the type being destroyed. */
22655 if (TREE_CODE (name) == BIT_NOT_EXPR)
22657 tree type;
22659 /* Figure out to which type this destructor applies. */
22660 if (parser->scope)
22661 type = parser->scope;
22662 else if (object_type)
22663 type = object_type;
22664 else
22665 type = current_class_type;
22666 /* If that's not a class type, there is no destructor. */
22667 if (!type || !CLASS_TYPE_P (type))
22668 return error_mark_node;
22669 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22670 lazily_declare_fn (sfk_destructor, type);
22671 if (!CLASSTYPE_DESTRUCTORS (type))
22672 return error_mark_node;
22673 /* If it was a class type, return the destructor. */
22674 return CLASSTYPE_DESTRUCTORS (type);
22677 /* By this point, the NAME should be an ordinary identifier. If
22678 the id-expression was a qualified name, the qualifying scope is
22679 stored in PARSER->SCOPE at this point. */
22680 gcc_assert (identifier_p (name));
22682 /* Perform the lookup. */
22683 if (parser->scope)
22685 bool dependent_p;
22687 if (parser->scope == error_mark_node)
22688 return error_mark_node;
22690 /* If the SCOPE is dependent, the lookup must be deferred until
22691 the template is instantiated -- unless we are explicitly
22692 looking up names in uninstantiated templates. Even then, we
22693 cannot look up the name if the scope is not a class type; it
22694 might, for example, be a template type parameter. */
22695 dependent_p = (TYPE_P (parser->scope)
22696 && dependent_scope_p (parser->scope));
22697 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22698 && dependent_p)
22699 /* Defer lookup. */
22700 decl = error_mark_node;
22701 else
22703 tree pushed_scope = NULL_TREE;
22705 /* If PARSER->SCOPE is a dependent type, then it must be a
22706 class type, and we must not be checking dependencies;
22707 otherwise, we would have processed this lookup above. So
22708 that PARSER->SCOPE is not considered a dependent base by
22709 lookup_member, we must enter the scope here. */
22710 if (dependent_p)
22711 pushed_scope = push_scope (parser->scope);
22713 /* If the PARSER->SCOPE is a template specialization, it
22714 may be instantiated during name lookup. In that case,
22715 errors may be issued. Even if we rollback the current
22716 tentative parse, those errors are valid. */
22717 decl = lookup_qualified_name (parser->scope, name,
22718 tag_type != none_type,
22719 /*complain=*/true);
22721 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22722 lookup result and the nested-name-specifier nominates a class C:
22723 * if the name specified after the nested-name-specifier, when
22724 looked up in C, is the injected-class-name of C (Clause 9), or
22725 * if the name specified after the nested-name-specifier is the
22726 same as the identifier or the simple-template-id's template-
22727 name in the last component of the nested-name-specifier,
22728 the name is instead considered to name the constructor of
22729 class C. [ Note: for example, the constructor is not an
22730 acceptable lookup result in an elaborated-type-specifier so
22731 the constructor would not be used in place of the
22732 injected-class-name. --end note ] Such a constructor name
22733 shall be used only in the declarator-id of a declaration that
22734 names a constructor or in a using-declaration. */
22735 if (tag_type == none_type
22736 && DECL_SELF_REFERENCE_P (decl)
22737 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22738 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22739 tag_type != none_type,
22740 /*complain=*/true);
22742 /* If we have a single function from a using decl, pull it out. */
22743 if (TREE_CODE (decl) == OVERLOAD
22744 && !really_overloaded_fn (decl))
22745 decl = OVL_FUNCTION (decl);
22747 if (pushed_scope)
22748 pop_scope (pushed_scope);
22751 /* If the scope is a dependent type and either we deferred lookup or
22752 we did lookup but didn't find the name, rememeber the name. */
22753 if (decl == error_mark_node && TYPE_P (parser->scope)
22754 && dependent_type_p (parser->scope))
22756 if (tag_type)
22758 tree type;
22760 /* The resolution to Core Issue 180 says that `struct
22761 A::B' should be considered a type-name, even if `A'
22762 is dependent. */
22763 type = make_typename_type (parser->scope, name, tag_type,
22764 /*complain=*/tf_error);
22765 if (type != error_mark_node)
22766 decl = TYPE_NAME (type);
22768 else if (is_template
22769 && (cp_parser_next_token_ends_template_argument_p (parser)
22770 || cp_lexer_next_token_is (parser->lexer,
22771 CPP_CLOSE_PAREN)))
22772 decl = make_unbound_class_template (parser->scope,
22773 name, NULL_TREE,
22774 /*complain=*/tf_error);
22775 else
22776 decl = build_qualified_name (/*type=*/NULL_TREE,
22777 parser->scope, name,
22778 is_template);
22780 parser->qualifying_scope = parser->scope;
22781 parser->object_scope = NULL_TREE;
22783 else if (object_type)
22785 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22786 OBJECT_TYPE is not a class. */
22787 if (CLASS_TYPE_P (object_type))
22788 /* If the OBJECT_TYPE is a template specialization, it may
22789 be instantiated during name lookup. In that case, errors
22790 may be issued. Even if we rollback the current tentative
22791 parse, those errors are valid. */
22792 decl = lookup_member (object_type,
22793 name,
22794 /*protect=*/0,
22795 tag_type != none_type,
22796 tf_warning_or_error);
22797 else
22798 decl = NULL_TREE;
22800 if (!decl)
22801 /* Look it up in the enclosing context. */
22802 decl = lookup_name_real (name, tag_type != none_type,
22803 /*nonclass=*/0,
22804 /*block_p=*/true, is_namespace, 0);
22805 parser->object_scope = object_type;
22806 parser->qualifying_scope = NULL_TREE;
22808 else
22810 decl = lookup_name_real (name, tag_type != none_type,
22811 /*nonclass=*/0,
22812 /*block_p=*/true, is_namespace, 0);
22813 parser->qualifying_scope = NULL_TREE;
22814 parser->object_scope = NULL_TREE;
22817 /* If the lookup failed, let our caller know. */
22818 if (!decl || decl == error_mark_node)
22819 return error_mark_node;
22821 /* Pull out the template from an injected-class-name (or multiple). */
22822 if (is_template)
22823 decl = maybe_get_template_decl_from_type_decl (decl);
22825 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22826 if (TREE_CODE (decl) == TREE_LIST)
22828 if (ambiguous_decls)
22829 *ambiguous_decls = decl;
22830 /* The error message we have to print is too complicated for
22831 cp_parser_error, so we incorporate its actions directly. */
22832 if (!cp_parser_simulate_error (parser))
22834 error_at (name_location, "reference to %qD is ambiguous",
22835 name);
22836 print_candidates (decl);
22838 return error_mark_node;
22841 gcc_assert (DECL_P (decl)
22842 || TREE_CODE (decl) == OVERLOAD
22843 || TREE_CODE (decl) == SCOPE_REF
22844 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22845 || BASELINK_P (decl));
22847 /* If we have resolved the name of a member declaration, check to
22848 see if the declaration is accessible. When the name resolves to
22849 set of overloaded functions, accessibility is checked when
22850 overload resolution is done.
22852 During an explicit instantiation, access is not checked at all,
22853 as per [temp.explicit]. */
22854 if (DECL_P (decl))
22855 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22857 maybe_record_typedef_use (decl);
22859 return decl;
22862 /* Like cp_parser_lookup_name, but for use in the typical case where
22863 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22864 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22866 static tree
22867 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22869 return cp_parser_lookup_name (parser, name,
22870 none_type,
22871 /*is_template=*/false,
22872 /*is_namespace=*/false,
22873 /*check_dependency=*/true,
22874 /*ambiguous_decls=*/NULL,
22875 location);
22878 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22879 the current context, return the TYPE_DECL. If TAG_NAME_P is
22880 true, the DECL indicates the class being defined in a class-head,
22881 or declared in an elaborated-type-specifier.
22883 Otherwise, return DECL. */
22885 static tree
22886 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22888 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22889 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22891 struct A {
22892 template <typename T> struct B;
22895 template <typename T> struct A::B {};
22897 Similarly, in an elaborated-type-specifier:
22899 namespace N { struct X{}; }
22901 struct A {
22902 template <typename T> friend struct N::X;
22905 However, if the DECL refers to a class type, and we are in
22906 the scope of the class, then the name lookup automatically
22907 finds the TYPE_DECL created by build_self_reference rather
22908 than a TEMPLATE_DECL. For example, in:
22910 template <class T> struct S {
22911 S s;
22914 there is no need to handle such case. */
22916 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22917 return DECL_TEMPLATE_RESULT (decl);
22919 return decl;
22922 /* If too many, or too few, template-parameter lists apply to the
22923 declarator, issue an error message. Returns TRUE if all went well,
22924 and FALSE otherwise. */
22926 static bool
22927 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22928 cp_declarator *declarator,
22929 location_t declarator_location)
22931 switch (declarator->kind)
22933 case cdk_id:
22935 unsigned num_templates = 0;
22936 tree scope = declarator->u.id.qualifying_scope;
22938 if (scope)
22939 num_templates = num_template_headers_for_class (scope);
22940 else if (TREE_CODE (declarator->u.id.unqualified_name)
22941 == TEMPLATE_ID_EXPR)
22942 /* If the DECLARATOR has the form `X<y>' then it uses one
22943 additional level of template parameters. */
22944 ++num_templates;
22946 return cp_parser_check_template_parameters
22947 (parser, num_templates, declarator_location, declarator);
22950 case cdk_function:
22951 case cdk_array:
22952 case cdk_pointer:
22953 case cdk_reference:
22954 case cdk_ptrmem:
22955 return (cp_parser_check_declarator_template_parameters
22956 (parser, declarator->declarator, declarator_location));
22958 case cdk_error:
22959 return true;
22961 default:
22962 gcc_unreachable ();
22964 return false;
22967 /* NUM_TEMPLATES were used in the current declaration. If that is
22968 invalid, return FALSE and issue an error messages. Otherwise,
22969 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22970 declarator and we can print more accurate diagnostics. */
22972 static bool
22973 cp_parser_check_template_parameters (cp_parser* parser,
22974 unsigned num_templates,
22975 location_t location,
22976 cp_declarator *declarator)
22978 /* If there are the same number of template classes and parameter
22979 lists, that's OK. */
22980 if (parser->num_template_parameter_lists == num_templates)
22981 return true;
22982 /* If there are more, but only one more, then we are referring to a
22983 member template. That's OK too. */
22984 if (parser->num_template_parameter_lists == num_templates + 1)
22985 return true;
22986 /* If there are more template classes than parameter lists, we have
22987 something like:
22989 template <class T> void S<T>::R<T>::f (); */
22990 if (parser->num_template_parameter_lists < num_templates)
22992 if (declarator && !current_function_decl)
22993 error_at (location, "specializing member %<%T::%E%> "
22994 "requires %<template<>%> syntax",
22995 declarator->u.id.qualifying_scope,
22996 declarator->u.id.unqualified_name);
22997 else if (declarator)
22998 error_at (location, "invalid declaration of %<%T::%E%>",
22999 declarator->u.id.qualifying_scope,
23000 declarator->u.id.unqualified_name);
23001 else
23002 error_at (location, "too few template-parameter-lists");
23003 return false;
23005 /* Otherwise, there are too many template parameter lists. We have
23006 something like:
23008 template <class T> template <class U> void S::f(); */
23009 error_at (location, "too many template-parameter-lists");
23010 return false;
23013 /* Parse an optional `::' token indicating that the following name is
23014 from the global namespace. If so, PARSER->SCOPE is set to the
23015 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23016 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23017 Returns the new value of PARSER->SCOPE, if the `::' token is
23018 present, and NULL_TREE otherwise. */
23020 static tree
23021 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23023 cp_token *token;
23025 /* Peek at the next token. */
23026 token = cp_lexer_peek_token (parser->lexer);
23027 /* If we're looking at a `::' token then we're starting from the
23028 global namespace, not our current location. */
23029 if (token->type == CPP_SCOPE)
23031 /* Consume the `::' token. */
23032 cp_lexer_consume_token (parser->lexer);
23033 /* Set the SCOPE so that we know where to start the lookup. */
23034 parser->scope = global_namespace;
23035 parser->qualifying_scope = global_namespace;
23036 parser->object_scope = NULL_TREE;
23038 return parser->scope;
23040 else if (!current_scope_valid_p)
23042 parser->scope = NULL_TREE;
23043 parser->qualifying_scope = NULL_TREE;
23044 parser->object_scope = NULL_TREE;
23047 return NULL_TREE;
23050 /* Returns TRUE if the upcoming token sequence is the start of a
23051 constructor declarator. If FRIEND_P is true, the declarator is
23052 preceded by the `friend' specifier. */
23054 static bool
23055 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23057 bool constructor_p;
23058 bool outside_class_specifier_p;
23059 tree nested_name_specifier;
23060 cp_token *next_token;
23062 /* The common case is that this is not a constructor declarator, so
23063 try to avoid doing lots of work if at all possible. It's not
23064 valid declare a constructor at function scope. */
23065 if (parser->in_function_body)
23066 return false;
23067 /* And only certain tokens can begin a constructor declarator. */
23068 next_token = cp_lexer_peek_token (parser->lexer);
23069 if (next_token->type != CPP_NAME
23070 && next_token->type != CPP_SCOPE
23071 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23072 && next_token->type != CPP_TEMPLATE_ID)
23073 return false;
23075 /* Parse tentatively; we are going to roll back all of the tokens
23076 consumed here. */
23077 cp_parser_parse_tentatively (parser);
23078 /* Assume that we are looking at a constructor declarator. */
23079 constructor_p = true;
23081 /* Look for the optional `::' operator. */
23082 cp_parser_global_scope_opt (parser,
23083 /*current_scope_valid_p=*/false);
23084 /* Look for the nested-name-specifier. */
23085 nested_name_specifier
23086 = (cp_parser_nested_name_specifier_opt (parser,
23087 /*typename_keyword_p=*/false,
23088 /*check_dependency_p=*/false,
23089 /*type_p=*/false,
23090 /*is_declaration=*/false));
23092 outside_class_specifier_p = (!at_class_scope_p ()
23093 || !TYPE_BEING_DEFINED (current_class_type)
23094 || friend_p);
23096 /* Outside of a class-specifier, there must be a
23097 nested-name-specifier. */
23098 if (!nested_name_specifier && outside_class_specifier_p)
23099 constructor_p = false;
23100 else if (nested_name_specifier == error_mark_node)
23101 constructor_p = false;
23103 /* If we have a class scope, this is easy; DR 147 says that S::S always
23104 names the constructor, and no other qualified name could. */
23105 if (constructor_p && nested_name_specifier
23106 && CLASS_TYPE_P (nested_name_specifier))
23108 tree id = cp_parser_unqualified_id (parser,
23109 /*template_keyword_p=*/false,
23110 /*check_dependency_p=*/false,
23111 /*declarator_p=*/true,
23112 /*optional_p=*/false);
23113 if (is_overloaded_fn (id))
23114 id = DECL_NAME (get_first_fn (id));
23115 if (!constructor_name_p (id, nested_name_specifier))
23116 constructor_p = false;
23118 /* If we still think that this might be a constructor-declarator,
23119 look for a class-name. */
23120 else if (constructor_p)
23122 /* If we have:
23124 template <typename T> struct S {
23125 S();
23128 we must recognize that the nested `S' names a class. */
23129 tree type_decl;
23130 type_decl = cp_parser_class_name (parser,
23131 /*typename_keyword_p=*/false,
23132 /*template_keyword_p=*/false,
23133 none_type,
23134 /*check_dependency_p=*/false,
23135 /*class_head_p=*/false,
23136 /*is_declaration=*/false);
23137 /* If there was no class-name, then this is not a constructor.
23138 Otherwise, if we are in a class-specifier and we aren't
23139 handling a friend declaration, check that its type matches
23140 current_class_type (c++/38313). Note: error_mark_node
23141 is left alone for error recovery purposes. */
23142 constructor_p = (!cp_parser_error_occurred (parser)
23143 && (outside_class_specifier_p
23144 || type_decl == error_mark_node
23145 || same_type_p (current_class_type,
23146 TREE_TYPE (type_decl))));
23148 /* If we're still considering a constructor, we have to see a `(',
23149 to begin the parameter-declaration-clause, followed by either a
23150 `)', an `...', or a decl-specifier. We need to check for a
23151 type-specifier to avoid being fooled into thinking that:
23153 S (f) (int);
23155 is a constructor. (It is actually a function named `f' that
23156 takes one parameter (of type `int') and returns a value of type
23157 `S'. */
23158 if (constructor_p
23159 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23160 constructor_p = false;
23162 if (constructor_p
23163 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23164 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23165 /* A parameter declaration begins with a decl-specifier,
23166 which is either the "attribute" keyword, a storage class
23167 specifier, or (usually) a type-specifier. */
23168 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23170 tree type;
23171 tree pushed_scope = NULL_TREE;
23172 unsigned saved_num_template_parameter_lists;
23174 /* Names appearing in the type-specifier should be looked up
23175 in the scope of the class. */
23176 if (current_class_type)
23177 type = NULL_TREE;
23178 else
23180 type = TREE_TYPE (type_decl);
23181 if (TREE_CODE (type) == TYPENAME_TYPE)
23183 type = resolve_typename_type (type,
23184 /*only_current_p=*/false);
23185 if (TREE_CODE (type) == TYPENAME_TYPE)
23187 cp_parser_abort_tentative_parse (parser);
23188 return false;
23191 pushed_scope = push_scope (type);
23194 /* Inside the constructor parameter list, surrounding
23195 template-parameter-lists do not apply. */
23196 saved_num_template_parameter_lists
23197 = parser->num_template_parameter_lists;
23198 parser->num_template_parameter_lists = 0;
23200 /* Look for the type-specifier. */
23201 cp_parser_type_specifier (parser,
23202 CP_PARSER_FLAGS_NONE,
23203 /*decl_specs=*/NULL,
23204 /*is_declarator=*/true,
23205 /*declares_class_or_enum=*/NULL,
23206 /*is_cv_qualifier=*/NULL);
23208 parser->num_template_parameter_lists
23209 = saved_num_template_parameter_lists;
23211 /* Leave the scope of the class. */
23212 if (pushed_scope)
23213 pop_scope (pushed_scope);
23215 constructor_p = !cp_parser_error_occurred (parser);
23219 /* We did not really want to consume any tokens. */
23220 cp_parser_abort_tentative_parse (parser);
23222 return constructor_p;
23225 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23226 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23227 they must be performed once we are in the scope of the function.
23229 Returns the function defined. */
23231 static tree
23232 cp_parser_function_definition_from_specifiers_and_declarator
23233 (cp_parser* parser,
23234 cp_decl_specifier_seq *decl_specifiers,
23235 tree attributes,
23236 const cp_declarator *declarator)
23238 tree fn;
23239 bool success_p;
23241 /* Begin the function-definition. */
23242 success_p = start_function (decl_specifiers, declarator, attributes);
23244 /* The things we're about to see are not directly qualified by any
23245 template headers we've seen thus far. */
23246 reset_specialization ();
23248 /* If there were names looked up in the decl-specifier-seq that we
23249 did not check, check them now. We must wait until we are in the
23250 scope of the function to perform the checks, since the function
23251 might be a friend. */
23252 perform_deferred_access_checks (tf_warning_or_error);
23254 if (success_p)
23256 cp_finalize_omp_declare_simd (parser, current_function_decl);
23257 parser->omp_declare_simd = NULL;
23260 if (!success_p)
23262 /* Skip the entire function. */
23263 cp_parser_skip_to_end_of_block_or_statement (parser);
23264 fn = error_mark_node;
23266 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23268 /* Seen already, skip it. An error message has already been output. */
23269 cp_parser_skip_to_end_of_block_or_statement (parser);
23270 fn = current_function_decl;
23271 current_function_decl = NULL_TREE;
23272 /* If this is a function from a class, pop the nested class. */
23273 if (current_class_name)
23274 pop_nested_class ();
23276 else
23278 timevar_id_t tv;
23279 if (DECL_DECLARED_INLINE_P (current_function_decl))
23280 tv = TV_PARSE_INLINE;
23281 else
23282 tv = TV_PARSE_FUNC;
23283 timevar_push (tv);
23284 fn = cp_parser_function_definition_after_declarator (parser,
23285 /*inline_p=*/false);
23286 timevar_pop (tv);
23289 return fn;
23292 /* Parse the part of a function-definition that follows the
23293 declarator. INLINE_P is TRUE iff this function is an inline
23294 function defined within a class-specifier.
23296 Returns the function defined. */
23298 static tree
23299 cp_parser_function_definition_after_declarator (cp_parser* parser,
23300 bool inline_p)
23302 tree fn;
23303 bool ctor_initializer_p = false;
23304 bool saved_in_unbraced_linkage_specification_p;
23305 bool saved_in_function_body;
23306 unsigned saved_num_template_parameter_lists;
23307 cp_token *token;
23308 bool fully_implicit_function_template_p
23309 = parser->fully_implicit_function_template_p;
23310 parser->fully_implicit_function_template_p = false;
23311 tree implicit_template_parms
23312 = parser->implicit_template_parms;
23313 parser->implicit_template_parms = 0;
23314 cp_binding_level* implicit_template_scope
23315 = parser->implicit_template_scope;
23316 parser->implicit_template_scope = 0;
23318 saved_in_function_body = parser->in_function_body;
23319 parser->in_function_body = true;
23320 /* If the next token is `return', then the code may be trying to
23321 make use of the "named return value" extension that G++ used to
23322 support. */
23323 token = cp_lexer_peek_token (parser->lexer);
23324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23326 /* Consume the `return' keyword. */
23327 cp_lexer_consume_token (parser->lexer);
23328 /* Look for the identifier that indicates what value is to be
23329 returned. */
23330 cp_parser_identifier (parser);
23331 /* Issue an error message. */
23332 error_at (token->location,
23333 "named return values are no longer supported");
23334 /* Skip tokens until we reach the start of the function body. */
23335 while (true)
23337 cp_token *token = cp_lexer_peek_token (parser->lexer);
23338 if (token->type == CPP_OPEN_BRACE
23339 || token->type == CPP_EOF
23340 || token->type == CPP_PRAGMA_EOL)
23341 break;
23342 cp_lexer_consume_token (parser->lexer);
23345 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23346 anything declared inside `f'. */
23347 saved_in_unbraced_linkage_specification_p
23348 = parser->in_unbraced_linkage_specification_p;
23349 parser->in_unbraced_linkage_specification_p = false;
23350 /* Inside the function, surrounding template-parameter-lists do not
23351 apply. */
23352 saved_num_template_parameter_lists
23353 = parser->num_template_parameter_lists;
23354 parser->num_template_parameter_lists = 0;
23356 start_lambda_scope (current_function_decl);
23358 /* If the next token is `try', `__transaction_atomic', or
23359 `__transaction_relaxed`, then we are looking at either function-try-block
23360 or function-transaction-block. Note that all of these include the
23361 function-body. */
23362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23363 ctor_initializer_p = cp_parser_function_transaction (parser,
23364 RID_TRANSACTION_ATOMIC);
23365 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23366 RID_TRANSACTION_RELAXED))
23367 ctor_initializer_p = cp_parser_function_transaction (parser,
23368 RID_TRANSACTION_RELAXED);
23369 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23370 ctor_initializer_p = cp_parser_function_try_block (parser);
23371 else
23372 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23373 (parser, /*in_function_try_block=*/false);
23375 finish_lambda_scope ();
23377 /* Finish the function. */
23378 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23379 (inline_p ? 2 : 0));
23380 /* Generate code for it, if necessary. */
23381 expand_or_defer_fn (fn);
23382 /* Restore the saved values. */
23383 parser->in_unbraced_linkage_specification_p
23384 = saved_in_unbraced_linkage_specification_p;
23385 parser->num_template_parameter_lists
23386 = saved_num_template_parameter_lists;
23387 parser->in_function_body = saved_in_function_body;
23389 parser->fully_implicit_function_template_p
23390 = fully_implicit_function_template_p;
23391 parser->implicit_template_parms
23392 = implicit_template_parms;
23393 parser->implicit_template_scope
23394 = implicit_template_scope;
23396 if (parser->fully_implicit_function_template_p)
23397 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23399 return fn;
23402 /* Parse a template-declaration, assuming that the `export' (and
23403 `extern') keywords, if present, has already been scanned. MEMBER_P
23404 is as for cp_parser_template_declaration. */
23406 static void
23407 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23409 tree decl = NULL_TREE;
23410 vec<deferred_access_check, va_gc> *checks;
23411 tree parameter_list;
23412 bool friend_p = false;
23413 bool need_lang_pop;
23414 cp_token *token;
23416 /* Look for the `template' keyword. */
23417 token = cp_lexer_peek_token (parser->lexer);
23418 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23419 return;
23421 /* And the `<'. */
23422 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23423 return;
23424 if (at_class_scope_p () && current_function_decl)
23426 /* 14.5.2.2 [temp.mem]
23428 A local class shall not have member templates. */
23429 error_at (token->location,
23430 "invalid declaration of member template in local class");
23431 cp_parser_skip_to_end_of_block_or_statement (parser);
23432 return;
23434 /* [temp]
23436 A template ... shall not have C linkage. */
23437 if (current_lang_name == lang_name_c)
23439 error_at (token->location, "template with C linkage");
23440 /* Give it C++ linkage to avoid confusing other parts of the
23441 front end. */
23442 push_lang_context (lang_name_cplusplus);
23443 need_lang_pop = true;
23445 else
23446 need_lang_pop = false;
23448 /* We cannot perform access checks on the template parameter
23449 declarations until we know what is being declared, just as we
23450 cannot check the decl-specifier list. */
23451 push_deferring_access_checks (dk_deferred);
23453 /* If the next token is `>', then we have an invalid
23454 specialization. Rather than complain about an invalid template
23455 parameter, issue an error message here. */
23456 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23458 cp_parser_error (parser, "invalid explicit specialization");
23459 begin_specialization ();
23460 parameter_list = NULL_TREE;
23462 else
23464 /* Parse the template parameters. */
23465 parameter_list = cp_parser_template_parameter_list (parser);
23468 /* Get the deferred access checks from the parameter list. These
23469 will be checked once we know what is being declared, as for a
23470 member template the checks must be performed in the scope of the
23471 class containing the member. */
23472 checks = get_deferred_access_checks ();
23474 /* Look for the `>'. */
23475 cp_parser_skip_to_end_of_template_parameter_list (parser);
23476 /* We just processed one more parameter list. */
23477 ++parser->num_template_parameter_lists;
23478 /* If the next token is `template', there are more template
23479 parameters. */
23480 if (cp_lexer_next_token_is_keyword (parser->lexer,
23481 RID_TEMPLATE))
23482 cp_parser_template_declaration_after_export (parser, member_p);
23483 else if (cxx_dialect >= cxx11
23484 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23485 decl = cp_parser_alias_declaration (parser);
23486 else
23488 /* There are no access checks when parsing a template, as we do not
23489 know if a specialization will be a friend. */
23490 push_deferring_access_checks (dk_no_check);
23491 token = cp_lexer_peek_token (parser->lexer);
23492 decl = cp_parser_single_declaration (parser,
23493 checks,
23494 member_p,
23495 /*explicit_specialization_p=*/false,
23496 &friend_p);
23497 pop_deferring_access_checks ();
23499 /* If this is a member template declaration, let the front
23500 end know. */
23501 if (member_p && !friend_p && decl)
23503 if (TREE_CODE (decl) == TYPE_DECL)
23504 cp_parser_check_access_in_redeclaration (decl, token->location);
23506 decl = finish_member_template_decl (decl);
23508 else if (friend_p && decl
23509 && DECL_DECLARES_TYPE_P (decl))
23510 make_friend_class (current_class_type, TREE_TYPE (decl),
23511 /*complain=*/true);
23513 /* We are done with the current parameter list. */
23514 --parser->num_template_parameter_lists;
23516 pop_deferring_access_checks ();
23518 /* Finish up. */
23519 finish_template_decl (parameter_list);
23521 /* Check the template arguments for a literal operator template. */
23522 if (decl
23523 && DECL_DECLARES_FUNCTION_P (decl)
23524 && UDLIT_OPER_P (DECL_NAME (decl)))
23526 bool ok = true;
23527 if (parameter_list == NULL_TREE)
23528 ok = false;
23529 else
23531 int num_parms = TREE_VEC_LENGTH (parameter_list);
23532 if (num_parms == 1)
23534 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23535 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23536 if (TREE_TYPE (parm) != char_type_node
23537 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23538 ok = false;
23540 else if (num_parms == 2 && cxx_dialect >= cxx14)
23542 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23543 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23544 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23545 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23546 if (TREE_TYPE (parm) != TREE_TYPE (type)
23547 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23548 ok = false;
23550 else
23551 ok = false;
23553 if (!ok)
23555 if (cxx_dialect >= cxx14)
23556 error ("literal operator template %qD has invalid parameter list."
23557 " Expected non-type template argument pack <char...>"
23558 " or <typename CharT, CharT...>",
23559 decl);
23560 else
23561 error ("literal operator template %qD has invalid parameter list."
23562 " Expected non-type template argument pack <char...>",
23563 decl);
23566 /* Register member declarations. */
23567 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23568 finish_member_declaration (decl);
23569 /* For the erroneous case of a template with C linkage, we pushed an
23570 implicit C++ linkage scope; exit that scope now. */
23571 if (need_lang_pop)
23572 pop_lang_context ();
23573 /* If DECL is a function template, we must return to parse it later.
23574 (Even though there is no definition, there might be default
23575 arguments that need handling.) */
23576 if (member_p && decl
23577 && DECL_DECLARES_FUNCTION_P (decl))
23578 vec_safe_push (unparsed_funs_with_definitions, decl);
23581 /* Perform the deferred access checks from a template-parameter-list.
23582 CHECKS is a TREE_LIST of access checks, as returned by
23583 get_deferred_access_checks. */
23585 static void
23586 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23588 ++processing_template_parmlist;
23589 perform_access_checks (checks, tf_warning_or_error);
23590 --processing_template_parmlist;
23593 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23594 `function-definition' sequence that follows a template header.
23595 If MEMBER_P is true, this declaration appears in a class scope.
23597 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23598 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23600 static tree
23601 cp_parser_single_declaration (cp_parser* parser,
23602 vec<deferred_access_check, va_gc> *checks,
23603 bool member_p,
23604 bool explicit_specialization_p,
23605 bool* friend_p)
23607 int declares_class_or_enum;
23608 tree decl = NULL_TREE;
23609 cp_decl_specifier_seq decl_specifiers;
23610 bool function_definition_p = false;
23611 cp_token *decl_spec_token_start;
23613 /* This function is only used when processing a template
23614 declaration. */
23615 gcc_assert (innermost_scope_kind () == sk_template_parms
23616 || innermost_scope_kind () == sk_template_spec);
23618 /* Defer access checks until we know what is being declared. */
23619 push_deferring_access_checks (dk_deferred);
23621 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23622 alternative. */
23623 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23624 cp_parser_decl_specifier_seq (parser,
23625 CP_PARSER_FLAGS_OPTIONAL,
23626 &decl_specifiers,
23627 &declares_class_or_enum);
23628 if (friend_p)
23629 *friend_p = cp_parser_friend_p (&decl_specifiers);
23631 /* There are no template typedefs. */
23632 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23634 error_at (decl_spec_token_start->location,
23635 "template declaration of %<typedef%>");
23636 decl = error_mark_node;
23639 /* Gather up the access checks that occurred the
23640 decl-specifier-seq. */
23641 stop_deferring_access_checks ();
23643 /* Check for the declaration of a template class. */
23644 if (declares_class_or_enum)
23646 if (cp_parser_declares_only_class_p (parser))
23648 decl = shadow_tag (&decl_specifiers);
23650 /* In this case:
23652 struct C {
23653 friend template <typename T> struct A<T>::B;
23656 A<T>::B will be represented by a TYPENAME_TYPE, and
23657 therefore not recognized by shadow_tag. */
23658 if (friend_p && *friend_p
23659 && !decl
23660 && decl_specifiers.type
23661 && TYPE_P (decl_specifiers.type))
23662 decl = decl_specifiers.type;
23664 if (decl && decl != error_mark_node)
23665 decl = TYPE_NAME (decl);
23666 else
23667 decl = error_mark_node;
23669 /* Perform access checks for template parameters. */
23670 cp_parser_perform_template_parameter_access_checks (checks);
23674 /* Complain about missing 'typename' or other invalid type names. */
23675 if (!decl_specifiers.any_type_specifiers_p
23676 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23678 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23679 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23680 the rest of this declaration. */
23681 decl = error_mark_node;
23682 goto out;
23685 /* If it's not a template class, try for a template function. If
23686 the next token is a `;', then this declaration does not declare
23687 anything. But, if there were errors in the decl-specifiers, then
23688 the error might well have come from an attempted class-specifier.
23689 In that case, there's no need to warn about a missing declarator. */
23690 if (!decl
23691 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23692 || decl_specifiers.type != error_mark_node))
23694 decl = cp_parser_init_declarator (parser,
23695 &decl_specifiers,
23696 checks,
23697 /*function_definition_allowed_p=*/true,
23698 member_p,
23699 declares_class_or_enum,
23700 &function_definition_p,
23701 NULL);
23703 /* 7.1.1-1 [dcl.stc]
23705 A storage-class-specifier shall not be specified in an explicit
23706 specialization... */
23707 if (decl
23708 && explicit_specialization_p
23709 && decl_specifiers.storage_class != sc_none)
23711 error_at (decl_spec_token_start->location,
23712 "explicit template specialization cannot have a storage class");
23713 decl = error_mark_node;
23716 if (decl && VAR_P (decl))
23717 check_template_variable (decl);
23720 /* Look for a trailing `;' after the declaration. */
23721 if (!function_definition_p
23722 && (decl == error_mark_node
23723 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23724 cp_parser_skip_to_end_of_block_or_statement (parser);
23726 out:
23727 pop_deferring_access_checks ();
23729 /* Clear any current qualification; whatever comes next is the start
23730 of something new. */
23731 parser->scope = NULL_TREE;
23732 parser->qualifying_scope = NULL_TREE;
23733 parser->object_scope = NULL_TREE;
23735 return decl;
23738 /* Parse a cast-expression that is not the operand of a unary "&". */
23740 static tree
23741 cp_parser_simple_cast_expression (cp_parser *parser)
23743 return cp_parser_cast_expression (parser, /*address_p=*/false,
23744 /*cast_p=*/false, /*decltype*/false, NULL);
23747 /* Parse a functional cast to TYPE. Returns an expression
23748 representing the cast. */
23750 static tree
23751 cp_parser_functional_cast (cp_parser* parser, tree type)
23753 vec<tree, va_gc> *vec;
23754 tree expression_list;
23755 tree cast;
23756 bool nonconst_p;
23758 if (!type)
23759 type = error_mark_node;
23761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23763 cp_lexer_set_source_position (parser->lexer);
23764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23765 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23766 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23767 if (TREE_CODE (type) == TYPE_DECL)
23768 type = TREE_TYPE (type);
23769 return finish_compound_literal (type, expression_list,
23770 tf_warning_or_error);
23774 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23775 /*cast_p=*/true,
23776 /*allow_expansion_p=*/true,
23777 /*non_constant_p=*/NULL);
23778 if (vec == NULL)
23779 expression_list = error_mark_node;
23780 else
23782 expression_list = build_tree_list_vec (vec);
23783 release_tree_vector (vec);
23786 cast = build_functional_cast (type, expression_list,
23787 tf_warning_or_error);
23788 /* [expr.const]/1: In an integral constant expression "only type
23789 conversions to integral or enumeration type can be used". */
23790 if (TREE_CODE (type) == TYPE_DECL)
23791 type = TREE_TYPE (type);
23792 if (cast != error_mark_node
23793 && !cast_valid_in_integral_constant_expression_p (type)
23794 && cp_parser_non_integral_constant_expression (parser,
23795 NIC_CONSTRUCTOR))
23796 return error_mark_node;
23797 return cast;
23800 /* Save the tokens that make up the body of a member function defined
23801 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23802 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23803 specifiers applied to the declaration. Returns the FUNCTION_DECL
23804 for the member function. */
23806 static tree
23807 cp_parser_save_member_function_body (cp_parser* parser,
23808 cp_decl_specifier_seq *decl_specifiers,
23809 cp_declarator *declarator,
23810 tree attributes)
23812 cp_token *first;
23813 cp_token *last;
23814 tree fn;
23816 /* Create the FUNCTION_DECL. */
23817 fn = grokmethod (decl_specifiers, declarator, attributes);
23818 cp_finalize_omp_declare_simd (parser, fn);
23819 /* If something went badly wrong, bail out now. */
23820 if (fn == error_mark_node)
23822 /* If there's a function-body, skip it. */
23823 if (cp_parser_token_starts_function_definition_p
23824 (cp_lexer_peek_token (parser->lexer)))
23825 cp_parser_skip_to_end_of_block_or_statement (parser);
23826 return error_mark_node;
23829 /* Remember it, if there default args to post process. */
23830 cp_parser_save_default_args (parser, fn);
23832 /* Save away the tokens that make up the body of the
23833 function. */
23834 first = parser->lexer->next_token;
23835 /* Handle function try blocks. */
23836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23837 cp_lexer_consume_token (parser->lexer);
23838 /* We can have braced-init-list mem-initializers before the fn body. */
23839 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23841 cp_lexer_consume_token (parser->lexer);
23842 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23844 /* cache_group will stop after an un-nested { } pair, too. */
23845 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23846 break;
23848 /* variadic mem-inits have ... after the ')'. */
23849 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23850 cp_lexer_consume_token (parser->lexer);
23853 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23854 /* Handle function try blocks. */
23855 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23856 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23857 last = parser->lexer->next_token;
23859 /* Save away the inline definition; we will process it when the
23860 class is complete. */
23861 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23862 DECL_PENDING_INLINE_P (fn) = 1;
23864 /* We need to know that this was defined in the class, so that
23865 friend templates are handled correctly. */
23866 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23868 /* Add FN to the queue of functions to be parsed later. */
23869 vec_safe_push (unparsed_funs_with_definitions, fn);
23871 return fn;
23874 /* Save the tokens that make up the in-class initializer for a non-static
23875 data member. Returns a DEFAULT_ARG. */
23877 static tree
23878 cp_parser_save_nsdmi (cp_parser* parser)
23880 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23883 /* Parse a template-argument-list, as well as the trailing ">" (but
23884 not the opening "<"). See cp_parser_template_argument_list for the
23885 return value. */
23887 static tree
23888 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23890 tree arguments;
23891 tree saved_scope;
23892 tree saved_qualifying_scope;
23893 tree saved_object_scope;
23894 bool saved_greater_than_is_operator_p;
23895 int saved_unevaluated_operand;
23896 int saved_inhibit_evaluation_warnings;
23898 /* [temp.names]
23900 When parsing a template-id, the first non-nested `>' is taken as
23901 the end of the template-argument-list rather than a greater-than
23902 operator. */
23903 saved_greater_than_is_operator_p
23904 = parser->greater_than_is_operator_p;
23905 parser->greater_than_is_operator_p = false;
23906 /* Parsing the argument list may modify SCOPE, so we save it
23907 here. */
23908 saved_scope = parser->scope;
23909 saved_qualifying_scope = parser->qualifying_scope;
23910 saved_object_scope = parser->object_scope;
23911 /* We need to evaluate the template arguments, even though this
23912 template-id may be nested within a "sizeof". */
23913 saved_unevaluated_operand = cp_unevaluated_operand;
23914 cp_unevaluated_operand = 0;
23915 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23916 c_inhibit_evaluation_warnings = 0;
23917 /* Parse the template-argument-list itself. */
23918 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23919 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23920 arguments = NULL_TREE;
23921 else
23922 arguments = cp_parser_template_argument_list (parser);
23923 /* Look for the `>' that ends the template-argument-list. If we find
23924 a '>>' instead, it's probably just a typo. */
23925 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23927 if (cxx_dialect != cxx98)
23929 /* In C++0x, a `>>' in a template argument list or cast
23930 expression is considered to be two separate `>'
23931 tokens. So, change the current token to a `>', but don't
23932 consume it: it will be consumed later when the outer
23933 template argument list (or cast expression) is parsed.
23934 Note that this replacement of `>' for `>>' is necessary
23935 even if we are parsing tentatively: in the tentative
23936 case, after calling
23937 cp_parser_enclosed_template_argument_list we will always
23938 throw away all of the template arguments and the first
23939 closing `>', either because the template argument list
23940 was erroneous or because we are replacing those tokens
23941 with a CPP_TEMPLATE_ID token. The second `>' (which will
23942 not have been thrown away) is needed either to close an
23943 outer template argument list or to complete a new-style
23944 cast. */
23945 cp_token *token = cp_lexer_peek_token (parser->lexer);
23946 token->type = CPP_GREATER;
23948 else if (!saved_greater_than_is_operator_p)
23950 /* If we're in a nested template argument list, the '>>' has
23951 to be a typo for '> >'. We emit the error message, but we
23952 continue parsing and we push a '>' as next token, so that
23953 the argument list will be parsed correctly. Note that the
23954 global source location is still on the token before the
23955 '>>', so we need to say explicitly where we want it. */
23956 cp_token *token = cp_lexer_peek_token (parser->lexer);
23957 error_at (token->location, "%<>>%> should be %<> >%> "
23958 "within a nested template argument list");
23960 token->type = CPP_GREATER;
23962 else
23964 /* If this is not a nested template argument list, the '>>'
23965 is a typo for '>'. Emit an error message and continue.
23966 Same deal about the token location, but here we can get it
23967 right by consuming the '>>' before issuing the diagnostic. */
23968 cp_token *token = cp_lexer_consume_token (parser->lexer);
23969 error_at (token->location,
23970 "spurious %<>>%>, use %<>%> to terminate "
23971 "a template argument list");
23974 else
23975 cp_parser_skip_to_end_of_template_parameter_list (parser);
23976 /* The `>' token might be a greater-than operator again now. */
23977 parser->greater_than_is_operator_p
23978 = saved_greater_than_is_operator_p;
23979 /* Restore the SAVED_SCOPE. */
23980 parser->scope = saved_scope;
23981 parser->qualifying_scope = saved_qualifying_scope;
23982 parser->object_scope = saved_object_scope;
23983 cp_unevaluated_operand = saved_unevaluated_operand;
23984 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23986 return arguments;
23989 /* MEMBER_FUNCTION is a member function, or a friend. If default
23990 arguments, or the body of the function have not yet been parsed,
23991 parse them now. */
23993 static void
23994 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23996 timevar_push (TV_PARSE_INMETH);
23997 /* If this member is a template, get the underlying
23998 FUNCTION_DECL. */
23999 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24000 member_function = DECL_TEMPLATE_RESULT (member_function);
24002 /* There should not be any class definitions in progress at this
24003 point; the bodies of members are only parsed outside of all class
24004 definitions. */
24005 gcc_assert (parser->num_classes_being_defined == 0);
24006 /* While we're parsing the member functions we might encounter more
24007 classes. We want to handle them right away, but we don't want
24008 them getting mixed up with functions that are currently in the
24009 queue. */
24010 push_unparsed_function_queues (parser);
24012 /* Make sure that any template parameters are in scope. */
24013 maybe_begin_member_template_processing (member_function);
24015 /* If the body of the function has not yet been parsed, parse it
24016 now. */
24017 if (DECL_PENDING_INLINE_P (member_function))
24019 tree function_scope;
24020 cp_token_cache *tokens;
24022 /* The function is no longer pending; we are processing it. */
24023 tokens = DECL_PENDING_INLINE_INFO (member_function);
24024 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24025 DECL_PENDING_INLINE_P (member_function) = 0;
24027 /* If this is a local class, enter the scope of the containing
24028 function. */
24029 function_scope = current_function_decl;
24030 if (function_scope)
24031 push_function_context ();
24033 /* Push the body of the function onto the lexer stack. */
24034 cp_parser_push_lexer_for_tokens (parser, tokens);
24036 /* Let the front end know that we going to be defining this
24037 function. */
24038 start_preparsed_function (member_function, NULL_TREE,
24039 SF_PRE_PARSED | SF_INCLASS_INLINE);
24041 /* Don't do access checking if it is a templated function. */
24042 if (processing_template_decl)
24043 push_deferring_access_checks (dk_no_check);
24045 /* #pragma omp declare reduction needs special parsing. */
24046 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24048 parser->lexer->in_pragma = true;
24049 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24050 finish_function (/*inline*/2);
24051 cp_check_omp_declare_reduction (member_function);
24053 else
24054 /* Now, parse the body of the function. */
24055 cp_parser_function_definition_after_declarator (parser,
24056 /*inline_p=*/true);
24058 if (processing_template_decl)
24059 pop_deferring_access_checks ();
24061 /* Leave the scope of the containing function. */
24062 if (function_scope)
24063 pop_function_context ();
24064 cp_parser_pop_lexer (parser);
24067 /* Remove any template parameters from the symbol table. */
24068 maybe_end_member_template_processing ();
24070 /* Restore the queue. */
24071 pop_unparsed_function_queues (parser);
24072 timevar_pop (TV_PARSE_INMETH);
24075 /* If DECL contains any default args, remember it on the unparsed
24076 functions queue. */
24078 static void
24079 cp_parser_save_default_args (cp_parser* parser, tree decl)
24081 tree probe;
24083 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24084 probe;
24085 probe = TREE_CHAIN (probe))
24086 if (TREE_PURPOSE (probe))
24088 cp_default_arg_entry entry = {current_class_type, decl};
24089 vec_safe_push (unparsed_funs_with_default_args, entry);
24090 break;
24094 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24095 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24096 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24097 from the parameter-type-list. */
24099 static tree
24100 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24101 tree default_arg, tree parmtype)
24103 cp_token_cache *tokens;
24104 tree parsed_arg;
24105 bool dummy;
24107 if (default_arg == error_mark_node)
24108 return error_mark_node;
24110 /* Push the saved tokens for the default argument onto the parser's
24111 lexer stack. */
24112 tokens = DEFARG_TOKENS (default_arg);
24113 cp_parser_push_lexer_for_tokens (parser, tokens);
24115 start_lambda_scope (decl);
24117 /* Parse the default argument. */
24118 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24119 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24120 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24122 finish_lambda_scope ();
24124 if (parsed_arg == error_mark_node)
24125 cp_parser_skip_to_end_of_statement (parser);
24127 if (!processing_template_decl)
24129 /* In a non-template class, check conversions now. In a template,
24130 we'll wait and instantiate these as needed. */
24131 if (TREE_CODE (decl) == PARM_DECL)
24132 parsed_arg = check_default_argument (parmtype, parsed_arg,
24133 tf_warning_or_error);
24134 else
24135 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24138 /* If the token stream has not been completely used up, then
24139 there was extra junk after the end of the default
24140 argument. */
24141 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24143 if (TREE_CODE (decl) == PARM_DECL)
24144 cp_parser_error (parser, "expected %<,%>");
24145 else
24146 cp_parser_error (parser, "expected %<;%>");
24149 /* Revert to the main lexer. */
24150 cp_parser_pop_lexer (parser);
24152 return parsed_arg;
24155 /* FIELD is a non-static data member with an initializer which we saved for
24156 later; parse it now. */
24158 static void
24159 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24161 tree def;
24163 maybe_begin_member_template_processing (field);
24165 push_unparsed_function_queues (parser);
24166 def = cp_parser_late_parse_one_default_arg (parser, field,
24167 DECL_INITIAL (field),
24168 NULL_TREE);
24169 pop_unparsed_function_queues (parser);
24171 maybe_end_member_template_processing ();
24173 DECL_INITIAL (field) = def;
24176 /* FN is a FUNCTION_DECL which may contains a parameter with an
24177 unparsed DEFAULT_ARG. Parse the default args now. This function
24178 assumes that the current scope is the scope in which the default
24179 argument should be processed. */
24181 static void
24182 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24184 bool saved_local_variables_forbidden_p;
24185 tree parm, parmdecl;
24187 /* While we're parsing the default args, we might (due to the
24188 statement expression extension) encounter more classes. We want
24189 to handle them right away, but we don't want them getting mixed
24190 up with default args that are currently in the queue. */
24191 push_unparsed_function_queues (parser);
24193 /* Local variable names (and the `this' keyword) may not appear
24194 in a default argument. */
24195 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24196 parser->local_variables_forbidden_p = true;
24198 push_defarg_context (fn);
24200 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24201 parmdecl = DECL_ARGUMENTS (fn);
24202 parm && parm != void_list_node;
24203 parm = TREE_CHAIN (parm),
24204 parmdecl = DECL_CHAIN (parmdecl))
24206 tree default_arg = TREE_PURPOSE (parm);
24207 tree parsed_arg;
24208 vec<tree, va_gc> *insts;
24209 tree copy;
24210 unsigned ix;
24212 if (!default_arg)
24213 continue;
24215 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24216 /* This can happen for a friend declaration for a function
24217 already declared with default arguments. */
24218 continue;
24220 parsed_arg
24221 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24222 default_arg,
24223 TREE_VALUE (parm));
24224 if (parsed_arg == error_mark_node)
24226 continue;
24229 TREE_PURPOSE (parm) = parsed_arg;
24231 /* Update any instantiations we've already created. */
24232 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24233 vec_safe_iterate (insts, ix, &copy); ix++)
24234 TREE_PURPOSE (copy) = parsed_arg;
24237 pop_defarg_context ();
24239 /* Make sure no default arg is missing. */
24240 check_default_args (fn);
24242 /* Restore the state of local_variables_forbidden_p. */
24243 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24245 /* Restore the queue. */
24246 pop_unparsed_function_queues (parser);
24249 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24251 sizeof ... ( identifier )
24253 where the 'sizeof' token has already been consumed. */
24255 static tree
24256 cp_parser_sizeof_pack (cp_parser *parser)
24258 /* Consume the `...'. */
24259 cp_lexer_consume_token (parser->lexer);
24260 maybe_warn_variadic_templates ();
24262 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24263 if (paren)
24264 cp_lexer_consume_token (parser->lexer);
24265 else
24266 permerror (cp_lexer_peek_token (parser->lexer)->location,
24267 "%<sizeof...%> argument must be surrounded by parentheses");
24269 cp_token *token = cp_lexer_peek_token (parser->lexer);
24270 tree name = cp_parser_identifier (parser);
24271 if (name == error_mark_node)
24272 return error_mark_node;
24273 /* The name is not qualified. */
24274 parser->scope = NULL_TREE;
24275 parser->qualifying_scope = NULL_TREE;
24276 parser->object_scope = NULL_TREE;
24277 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24278 if (expr == error_mark_node)
24279 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24280 token->location);
24281 if (TREE_CODE (expr) == TYPE_DECL)
24282 expr = TREE_TYPE (expr);
24283 else if (TREE_CODE (expr) == CONST_DECL)
24284 expr = DECL_INITIAL (expr);
24285 expr = make_pack_expansion (expr);
24287 if (paren)
24288 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24290 return expr;
24293 /* Parse the operand of `sizeof' (or a similar operator). Returns
24294 either a TYPE or an expression, depending on the form of the
24295 input. The KEYWORD indicates which kind of expression we have
24296 encountered. */
24298 static tree
24299 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24301 tree expr = NULL_TREE;
24302 const char *saved_message;
24303 char *tmp;
24304 bool saved_integral_constant_expression_p;
24305 bool saved_non_integral_constant_expression_p;
24307 /* If it's a `...', then we are computing the length of a parameter
24308 pack. */
24309 if (keyword == RID_SIZEOF
24310 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24311 return cp_parser_sizeof_pack (parser);
24313 /* Types cannot be defined in a `sizeof' expression. Save away the
24314 old message. */
24315 saved_message = parser->type_definition_forbidden_message;
24316 /* And create the new one. */
24317 tmp = concat ("types may not be defined in %<",
24318 IDENTIFIER_POINTER (ridpointers[keyword]),
24319 "%> expressions", NULL);
24320 parser->type_definition_forbidden_message = tmp;
24322 /* The restrictions on constant-expressions do not apply inside
24323 sizeof expressions. */
24324 saved_integral_constant_expression_p
24325 = parser->integral_constant_expression_p;
24326 saved_non_integral_constant_expression_p
24327 = parser->non_integral_constant_expression_p;
24328 parser->integral_constant_expression_p = false;
24330 /* Do not actually evaluate the expression. */
24331 ++cp_unevaluated_operand;
24332 ++c_inhibit_evaluation_warnings;
24333 /* If it's a `(', then we might be looking at the type-id
24334 construction. */
24335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24337 tree type = NULL_TREE;
24339 /* We can't be sure yet whether we're looking at a type-id or an
24340 expression. */
24341 cp_parser_parse_tentatively (parser);
24342 /* Note: as a GNU Extension, compound literals are considered
24343 postfix-expressions as they are in C99, so they are valid
24344 arguments to sizeof. See comment in cp_parser_cast_expression
24345 for details. */
24346 if (cp_parser_compound_literal_p (parser))
24347 cp_parser_simulate_error (parser);
24348 else
24350 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24351 parser->in_type_id_in_expr_p = true;
24352 /* Look for the type-id. */
24353 type = cp_parser_type_id (parser);
24354 /* Look for the closing `)'. */
24355 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24356 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24359 /* If all went well, then we're done. */
24360 if (cp_parser_parse_definitely (parser))
24362 cp_decl_specifier_seq decl_specs;
24364 /* Build a trivial decl-specifier-seq. */
24365 clear_decl_specs (&decl_specs);
24366 decl_specs.type = type;
24368 /* Call grokdeclarator to figure out what type this is. */
24369 expr = grokdeclarator (NULL,
24370 &decl_specs,
24371 TYPENAME,
24372 /*initialized=*/0,
24373 /*attrlist=*/NULL);
24377 /* If the type-id production did not work out, then we must be
24378 looking at the unary-expression production. */
24379 if (!expr)
24380 expr = cp_parser_unary_expression (parser);
24382 /* Go back to evaluating expressions. */
24383 --cp_unevaluated_operand;
24384 --c_inhibit_evaluation_warnings;
24386 /* Free the message we created. */
24387 free (tmp);
24388 /* And restore the old one. */
24389 parser->type_definition_forbidden_message = saved_message;
24390 parser->integral_constant_expression_p
24391 = saved_integral_constant_expression_p;
24392 parser->non_integral_constant_expression_p
24393 = saved_non_integral_constant_expression_p;
24395 return expr;
24398 /* If the current declaration has no declarator, return true. */
24400 static bool
24401 cp_parser_declares_only_class_p (cp_parser *parser)
24403 /* If the next token is a `;' or a `,' then there is no
24404 declarator. */
24405 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24406 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24409 /* Update the DECL_SPECS to reflect the storage class indicated by
24410 KEYWORD. */
24412 static void
24413 cp_parser_set_storage_class (cp_parser *parser,
24414 cp_decl_specifier_seq *decl_specs,
24415 enum rid keyword,
24416 cp_token *token)
24418 cp_storage_class storage_class;
24420 if (parser->in_unbraced_linkage_specification_p)
24422 error_at (token->location, "invalid use of %qD in linkage specification",
24423 ridpointers[keyword]);
24424 return;
24426 else if (decl_specs->storage_class != sc_none)
24428 decl_specs->conflicting_specifiers_p = true;
24429 return;
24432 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24433 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24434 && decl_specs->gnu_thread_keyword_p)
24436 pedwarn (decl_specs->locations[ds_thread], 0,
24437 "%<__thread%> before %qD", ridpointers[keyword]);
24440 switch (keyword)
24442 case RID_AUTO:
24443 storage_class = sc_auto;
24444 break;
24445 case RID_REGISTER:
24446 storage_class = sc_register;
24447 break;
24448 case RID_STATIC:
24449 storage_class = sc_static;
24450 break;
24451 case RID_EXTERN:
24452 storage_class = sc_extern;
24453 break;
24454 case RID_MUTABLE:
24455 storage_class = sc_mutable;
24456 break;
24457 default:
24458 gcc_unreachable ();
24460 decl_specs->storage_class = storage_class;
24461 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24463 /* A storage class specifier cannot be applied alongside a typedef
24464 specifier. If there is a typedef specifier present then set
24465 conflicting_specifiers_p which will trigger an error later
24466 on in grokdeclarator. */
24467 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24468 decl_specs->conflicting_specifiers_p = true;
24471 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24472 is true, the type is a class or enum definition. */
24474 static void
24475 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24476 tree type_spec,
24477 cp_token *token,
24478 bool type_definition_p)
24480 decl_specs->any_specifiers_p = true;
24482 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24483 (with, for example, in "typedef int wchar_t;") we remember that
24484 this is what happened. In system headers, we ignore these
24485 declarations so that G++ can work with system headers that are not
24486 C++-safe. */
24487 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24488 && !type_definition_p
24489 && (type_spec == boolean_type_node
24490 || type_spec == char16_type_node
24491 || type_spec == char32_type_node
24492 || type_spec == wchar_type_node)
24493 && (decl_specs->type
24494 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24495 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24496 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24497 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24499 decl_specs->redefined_builtin_type = type_spec;
24500 set_and_check_decl_spec_loc (decl_specs,
24501 ds_redefined_builtin_type_spec,
24502 token);
24503 if (!decl_specs->type)
24505 decl_specs->type = type_spec;
24506 decl_specs->type_definition_p = false;
24507 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24510 else if (decl_specs->type)
24511 decl_specs->multiple_types_p = true;
24512 else
24514 decl_specs->type = type_spec;
24515 decl_specs->type_definition_p = type_definition_p;
24516 decl_specs->redefined_builtin_type = NULL_TREE;
24517 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24521 /* True iff TOKEN is the GNU keyword __thread. */
24523 static bool
24524 token_is__thread (cp_token *token)
24526 gcc_assert (token->keyword == RID_THREAD);
24527 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24530 /* Set the location for a declarator specifier and check if it is
24531 duplicated.
24533 DECL_SPECS is the sequence of declarator specifiers onto which to
24534 set the location.
24536 DS is the single declarator specifier to set which location is to
24537 be set onto the existing sequence of declarators.
24539 LOCATION is the location for the declarator specifier to
24540 consider. */
24542 static void
24543 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24544 cp_decl_spec ds, cp_token *token)
24546 gcc_assert (ds < ds_last);
24548 if (decl_specs == NULL)
24549 return;
24551 source_location location = token->location;
24553 if (decl_specs->locations[ds] == 0)
24555 decl_specs->locations[ds] = location;
24556 if (ds == ds_thread)
24557 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24559 else
24561 if (ds == ds_long)
24563 if (decl_specs->locations[ds_long_long] != 0)
24564 error_at (location,
24565 "%<long long long%> is too long for GCC");
24566 else
24568 decl_specs->locations[ds_long_long] = location;
24569 pedwarn_cxx98 (location,
24570 OPT_Wlong_long,
24571 "ISO C++ 1998 does not support %<long long%>");
24574 else if (ds == ds_thread)
24576 bool gnu = token_is__thread (token);
24577 if (gnu != decl_specs->gnu_thread_keyword_p)
24578 error_at (location,
24579 "both %<__thread%> and %<thread_local%> specified");
24580 else
24581 error_at (location, "duplicate %qD", token->u.value);
24583 else
24585 static const char *const decl_spec_names[] = {
24586 "signed",
24587 "unsigned",
24588 "short",
24589 "long",
24590 "const",
24591 "volatile",
24592 "restrict",
24593 "inline",
24594 "virtual",
24595 "explicit",
24596 "friend",
24597 "typedef",
24598 "using",
24599 "constexpr",
24600 "__complex"
24602 error_at (location,
24603 "duplicate %qs", decl_spec_names[ds]);
24608 /* Return true iff the declarator specifier DS is present in the
24609 sequence of declarator specifiers DECL_SPECS. */
24611 bool
24612 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24613 cp_decl_spec ds)
24615 gcc_assert (ds < ds_last);
24617 if (decl_specs == NULL)
24618 return false;
24620 return decl_specs->locations[ds] != 0;
24623 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24624 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24626 static bool
24627 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24629 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24632 /* Issue an error message indicating that TOKEN_DESC was expected.
24633 If KEYWORD is true, it indicated this function is called by
24634 cp_parser_require_keword and the required token can only be
24635 a indicated keyword. */
24637 static void
24638 cp_parser_required_error (cp_parser *parser,
24639 required_token token_desc,
24640 bool keyword)
24642 switch (token_desc)
24644 case RT_NEW:
24645 cp_parser_error (parser, "expected %<new%>");
24646 return;
24647 case RT_DELETE:
24648 cp_parser_error (parser, "expected %<delete%>");
24649 return;
24650 case RT_RETURN:
24651 cp_parser_error (parser, "expected %<return%>");
24652 return;
24653 case RT_WHILE:
24654 cp_parser_error (parser, "expected %<while%>");
24655 return;
24656 case RT_EXTERN:
24657 cp_parser_error (parser, "expected %<extern%>");
24658 return;
24659 case RT_STATIC_ASSERT:
24660 cp_parser_error (parser, "expected %<static_assert%>");
24661 return;
24662 case RT_DECLTYPE:
24663 cp_parser_error (parser, "expected %<decltype%>");
24664 return;
24665 case RT_OPERATOR:
24666 cp_parser_error (parser, "expected %<operator%>");
24667 return;
24668 case RT_CLASS:
24669 cp_parser_error (parser, "expected %<class%>");
24670 return;
24671 case RT_TEMPLATE:
24672 cp_parser_error (parser, "expected %<template%>");
24673 return;
24674 case RT_NAMESPACE:
24675 cp_parser_error (parser, "expected %<namespace%>");
24676 return;
24677 case RT_USING:
24678 cp_parser_error (parser, "expected %<using%>");
24679 return;
24680 case RT_ASM:
24681 cp_parser_error (parser, "expected %<asm%>");
24682 return;
24683 case RT_TRY:
24684 cp_parser_error (parser, "expected %<try%>");
24685 return;
24686 case RT_CATCH:
24687 cp_parser_error (parser, "expected %<catch%>");
24688 return;
24689 case RT_THROW:
24690 cp_parser_error (parser, "expected %<throw%>");
24691 return;
24692 case RT_LABEL:
24693 cp_parser_error (parser, "expected %<__label__%>");
24694 return;
24695 case RT_AT_TRY:
24696 cp_parser_error (parser, "expected %<@try%>");
24697 return;
24698 case RT_AT_SYNCHRONIZED:
24699 cp_parser_error (parser, "expected %<@synchronized%>");
24700 return;
24701 case RT_AT_THROW:
24702 cp_parser_error (parser, "expected %<@throw%>");
24703 return;
24704 case RT_TRANSACTION_ATOMIC:
24705 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24706 return;
24707 case RT_TRANSACTION_RELAXED:
24708 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24709 return;
24710 default:
24711 break;
24713 if (!keyword)
24715 switch (token_desc)
24717 case RT_SEMICOLON:
24718 cp_parser_error (parser, "expected %<;%>");
24719 return;
24720 case RT_OPEN_PAREN:
24721 cp_parser_error (parser, "expected %<(%>");
24722 return;
24723 case RT_CLOSE_BRACE:
24724 cp_parser_error (parser, "expected %<}%>");
24725 return;
24726 case RT_OPEN_BRACE:
24727 cp_parser_error (parser, "expected %<{%>");
24728 return;
24729 case RT_CLOSE_SQUARE:
24730 cp_parser_error (parser, "expected %<]%>");
24731 return;
24732 case RT_OPEN_SQUARE:
24733 cp_parser_error (parser, "expected %<[%>");
24734 return;
24735 case RT_COMMA:
24736 cp_parser_error (parser, "expected %<,%>");
24737 return;
24738 case RT_SCOPE:
24739 cp_parser_error (parser, "expected %<::%>");
24740 return;
24741 case RT_LESS:
24742 cp_parser_error (parser, "expected %<<%>");
24743 return;
24744 case RT_GREATER:
24745 cp_parser_error (parser, "expected %<>%>");
24746 return;
24747 case RT_EQ:
24748 cp_parser_error (parser, "expected %<=%>");
24749 return;
24750 case RT_ELLIPSIS:
24751 cp_parser_error (parser, "expected %<...%>");
24752 return;
24753 case RT_MULT:
24754 cp_parser_error (parser, "expected %<*%>");
24755 return;
24756 case RT_COMPL:
24757 cp_parser_error (parser, "expected %<~%>");
24758 return;
24759 case RT_COLON:
24760 cp_parser_error (parser, "expected %<:%>");
24761 return;
24762 case RT_COLON_SCOPE:
24763 cp_parser_error (parser, "expected %<:%> or %<::%>");
24764 return;
24765 case RT_CLOSE_PAREN:
24766 cp_parser_error (parser, "expected %<)%>");
24767 return;
24768 case RT_COMMA_CLOSE_PAREN:
24769 cp_parser_error (parser, "expected %<,%> or %<)%>");
24770 return;
24771 case RT_PRAGMA_EOL:
24772 cp_parser_error (parser, "expected end of line");
24773 return;
24774 case RT_NAME:
24775 cp_parser_error (parser, "expected identifier");
24776 return;
24777 case RT_SELECT:
24778 cp_parser_error (parser, "expected selection-statement");
24779 return;
24780 case RT_INTERATION:
24781 cp_parser_error (parser, "expected iteration-statement");
24782 return;
24783 case RT_JUMP:
24784 cp_parser_error (parser, "expected jump-statement");
24785 return;
24786 case RT_CLASS_KEY:
24787 cp_parser_error (parser, "expected class-key");
24788 return;
24789 case RT_CLASS_TYPENAME_TEMPLATE:
24790 cp_parser_error (parser,
24791 "expected %<class%>, %<typename%>, or %<template%>");
24792 return;
24793 default:
24794 gcc_unreachable ();
24797 else
24798 gcc_unreachable ();
24803 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24804 issue an error message indicating that TOKEN_DESC was expected.
24806 Returns the token consumed, if the token had the appropriate type.
24807 Otherwise, returns NULL. */
24809 static cp_token *
24810 cp_parser_require (cp_parser* parser,
24811 enum cpp_ttype type,
24812 required_token token_desc)
24814 if (cp_lexer_next_token_is (parser->lexer, type))
24815 return cp_lexer_consume_token (parser->lexer);
24816 else
24818 /* Output the MESSAGE -- unless we're parsing tentatively. */
24819 if (!cp_parser_simulate_error (parser))
24820 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24821 return NULL;
24825 /* An error message is produced if the next token is not '>'.
24826 All further tokens are skipped until the desired token is
24827 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24829 static void
24830 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24832 /* Current level of '< ... >'. */
24833 unsigned level = 0;
24834 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24835 unsigned nesting_depth = 0;
24837 /* Are we ready, yet? If not, issue error message. */
24838 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24839 return;
24841 /* Skip tokens until the desired token is found. */
24842 while (true)
24844 /* Peek at the next token. */
24845 switch (cp_lexer_peek_token (parser->lexer)->type)
24847 case CPP_LESS:
24848 if (!nesting_depth)
24849 ++level;
24850 break;
24852 case CPP_RSHIFT:
24853 if (cxx_dialect == cxx98)
24854 /* C++0x views the `>>' operator as two `>' tokens, but
24855 C++98 does not. */
24856 break;
24857 else if (!nesting_depth && level-- == 0)
24859 /* We've hit a `>>' where the first `>' closes the
24860 template argument list, and the second `>' is
24861 spurious. Just consume the `>>' and stop; we've
24862 already produced at least one error. */
24863 cp_lexer_consume_token (parser->lexer);
24864 return;
24866 /* Fall through for C++0x, so we handle the second `>' in
24867 the `>>'. */
24869 case CPP_GREATER:
24870 if (!nesting_depth && level-- == 0)
24872 /* We've reached the token we want, consume it and stop. */
24873 cp_lexer_consume_token (parser->lexer);
24874 return;
24876 break;
24878 case CPP_OPEN_PAREN:
24879 case CPP_OPEN_SQUARE:
24880 ++nesting_depth;
24881 break;
24883 case CPP_CLOSE_PAREN:
24884 case CPP_CLOSE_SQUARE:
24885 if (nesting_depth-- == 0)
24886 return;
24887 break;
24889 case CPP_EOF:
24890 case CPP_PRAGMA_EOL:
24891 case CPP_SEMICOLON:
24892 case CPP_OPEN_BRACE:
24893 case CPP_CLOSE_BRACE:
24894 /* The '>' was probably forgotten, don't look further. */
24895 return;
24897 default:
24898 break;
24901 /* Consume this token. */
24902 cp_lexer_consume_token (parser->lexer);
24906 /* If the next token is the indicated keyword, consume it. Otherwise,
24907 issue an error message indicating that TOKEN_DESC was expected.
24909 Returns the token consumed, if the token had the appropriate type.
24910 Otherwise, returns NULL. */
24912 static cp_token *
24913 cp_parser_require_keyword (cp_parser* parser,
24914 enum rid keyword,
24915 required_token token_desc)
24917 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24919 if (token && token->keyword != keyword)
24921 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24922 return NULL;
24925 return token;
24928 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24929 function-definition. */
24931 static bool
24932 cp_parser_token_starts_function_definition_p (cp_token* token)
24934 return (/* An ordinary function-body begins with an `{'. */
24935 token->type == CPP_OPEN_BRACE
24936 /* A ctor-initializer begins with a `:'. */
24937 || token->type == CPP_COLON
24938 /* A function-try-block begins with `try'. */
24939 || token->keyword == RID_TRY
24940 /* A function-transaction-block begins with `__transaction_atomic'
24941 or `__transaction_relaxed'. */
24942 || token->keyword == RID_TRANSACTION_ATOMIC
24943 || token->keyword == RID_TRANSACTION_RELAXED
24944 /* The named return value extension begins with `return'. */
24945 || token->keyword == RID_RETURN);
24948 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24949 definition. */
24951 static bool
24952 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24954 cp_token *token;
24956 token = cp_lexer_peek_token (parser->lexer);
24957 return (token->type == CPP_OPEN_BRACE
24958 || (token->type == CPP_COLON
24959 && !parser->colon_doesnt_start_class_def_p));
24962 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24963 C++0x) ending a template-argument. */
24965 static bool
24966 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24968 cp_token *token;
24970 token = cp_lexer_peek_token (parser->lexer);
24971 return (token->type == CPP_COMMA
24972 || token->type == CPP_GREATER
24973 || token->type == CPP_ELLIPSIS
24974 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24977 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24978 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24980 static bool
24981 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24982 size_t n)
24984 cp_token *token;
24986 token = cp_lexer_peek_nth_token (parser->lexer, n);
24987 if (token->type == CPP_LESS)
24988 return true;
24989 /* Check for the sequence `<::' in the original code. It would be lexed as
24990 `[:', where `[' is a digraph, and there is no whitespace before
24991 `:'. */
24992 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24994 cp_token *token2;
24995 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24996 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24997 return true;
24999 return false;
25002 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25003 or none_type otherwise. */
25005 static enum tag_types
25006 cp_parser_token_is_class_key (cp_token* token)
25008 switch (token->keyword)
25010 case RID_CLASS:
25011 return class_type;
25012 case RID_STRUCT:
25013 return record_type;
25014 case RID_UNION:
25015 return union_type;
25017 default:
25018 return none_type;
25022 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25023 or none_type otherwise or if the token is null. */
25025 static enum tag_types
25026 cp_parser_token_is_type_parameter_key (cp_token* token)
25028 if (!token)
25029 return none_type;
25031 switch (token->keyword)
25033 case RID_CLASS:
25034 return class_type;
25035 case RID_TYPENAME:
25036 return typename_type;
25038 default:
25039 return none_type;
25043 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25045 static void
25046 cp_parser_check_class_key (enum tag_types class_key, tree type)
25048 if (type == error_mark_node)
25049 return;
25050 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25052 if (permerror (input_location, "%qs tag used in naming %q#T",
25053 class_key == union_type ? "union"
25054 : class_key == record_type ? "struct" : "class",
25055 type))
25056 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25057 "%q#T was previously declared here", type);
25061 /* Issue an error message if DECL is redeclared with different
25062 access than its original declaration [class.access.spec/3].
25063 This applies to nested classes and nested class templates.
25064 [class.mem/1]. */
25066 static void
25067 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25069 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25070 return;
25072 if ((TREE_PRIVATE (decl)
25073 != (current_access_specifier == access_private_node))
25074 || (TREE_PROTECTED (decl)
25075 != (current_access_specifier == access_protected_node)))
25076 error_at (location, "%qD redeclared with different access", decl);
25079 /* Look for the `template' keyword, as a syntactic disambiguator.
25080 Return TRUE iff it is present, in which case it will be
25081 consumed. */
25083 static bool
25084 cp_parser_optional_template_keyword (cp_parser *parser)
25086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25088 /* In C++98 the `template' keyword can only be used within templates;
25089 outside templates the parser can always figure out what is a
25090 template and what is not. In C++11, per the resolution of DR 468,
25091 `template' is allowed in cases where it is not strictly necessary. */
25092 if (!processing_template_decl
25093 && pedantic && cxx_dialect == cxx98)
25095 cp_token *token = cp_lexer_peek_token (parser->lexer);
25096 pedwarn (token->location, OPT_Wpedantic,
25097 "in C++98 %<template%> (as a disambiguator) is only "
25098 "allowed within templates");
25099 /* If this part of the token stream is rescanned, the same
25100 error message would be generated. So, we purge the token
25101 from the stream. */
25102 cp_lexer_purge_token (parser->lexer);
25103 return false;
25105 else
25107 /* Consume the `template' keyword. */
25108 cp_lexer_consume_token (parser->lexer);
25109 return true;
25112 return false;
25115 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25116 set PARSER->SCOPE, and perform other related actions. */
25118 static void
25119 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25121 int i;
25122 struct tree_check *check_value;
25123 deferred_access_check *chk;
25124 vec<deferred_access_check, va_gc> *checks;
25126 /* Get the stored value. */
25127 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25128 /* Perform any access checks that were deferred. */
25129 checks = check_value->checks;
25130 if (checks)
25132 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25133 perform_or_defer_access_check (chk->binfo,
25134 chk->decl,
25135 chk->diag_decl, tf_warning_or_error);
25137 /* Set the scope from the stored value. */
25138 parser->scope = check_value->value;
25139 parser->qualifying_scope = check_value->qualifying_scope;
25140 parser->object_scope = NULL_TREE;
25143 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25144 encounter the end of a block before what we were looking for. */
25146 static bool
25147 cp_parser_cache_group (cp_parser *parser,
25148 enum cpp_ttype end,
25149 unsigned depth)
25151 while (true)
25153 cp_token *token = cp_lexer_peek_token (parser->lexer);
25155 /* Abort a parenthesized expression if we encounter a semicolon. */
25156 if ((end == CPP_CLOSE_PAREN || depth == 0)
25157 && token->type == CPP_SEMICOLON)
25158 return true;
25159 /* If we've reached the end of the file, stop. */
25160 if (token->type == CPP_EOF
25161 || (end != CPP_PRAGMA_EOL
25162 && token->type == CPP_PRAGMA_EOL))
25163 return true;
25164 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25165 /* We've hit the end of an enclosing block, so there's been some
25166 kind of syntax error. */
25167 return true;
25169 /* Consume the token. */
25170 cp_lexer_consume_token (parser->lexer);
25171 /* See if it starts a new group. */
25172 if (token->type == CPP_OPEN_BRACE)
25174 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25175 /* In theory this should probably check end == '}', but
25176 cp_parser_save_member_function_body needs it to exit
25177 after either '}' or ')' when called with ')'. */
25178 if (depth == 0)
25179 return false;
25181 else if (token->type == CPP_OPEN_PAREN)
25183 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25184 if (depth == 0 && end == CPP_CLOSE_PAREN)
25185 return false;
25187 else if (token->type == CPP_PRAGMA)
25188 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25189 else if (token->type == end)
25190 return false;
25194 /* Like above, for caching a default argument or NSDMI. Both of these are
25195 terminated by a non-nested comma, but it can be unclear whether or not a
25196 comma is nested in a template argument list unless we do more parsing.
25197 In order to handle this ambiguity, when we encounter a ',' after a '<'
25198 we try to parse what follows as a parameter-declaration-list (in the
25199 case of a default argument) or a member-declarator (in the case of an
25200 NSDMI). If that succeeds, then we stop caching. */
25202 static tree
25203 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25205 unsigned depth = 0;
25206 int maybe_template_id = 0;
25207 cp_token *first_token;
25208 cp_token *token;
25209 tree default_argument;
25211 /* Add tokens until we have processed the entire default
25212 argument. We add the range [first_token, token). */
25213 first_token = cp_lexer_peek_token (parser->lexer);
25214 if (first_token->type == CPP_OPEN_BRACE)
25216 /* For list-initialization, this is straightforward. */
25217 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25218 token = cp_lexer_peek_token (parser->lexer);
25220 else while (true)
25222 bool done = false;
25224 /* Peek at the next token. */
25225 token = cp_lexer_peek_token (parser->lexer);
25226 /* What we do depends on what token we have. */
25227 switch (token->type)
25229 /* In valid code, a default argument must be
25230 immediately followed by a `,' `)', or `...'. */
25231 case CPP_COMMA:
25232 if (depth == 0 && maybe_template_id)
25234 /* If we've seen a '<', we might be in a
25235 template-argument-list. Until Core issue 325 is
25236 resolved, we don't know how this situation ought
25237 to be handled, so try to DTRT. We check whether
25238 what comes after the comma is a valid parameter
25239 declaration list. If it is, then the comma ends
25240 the default argument; otherwise the default
25241 argument continues. */
25242 bool error = false;
25244 /* Set ITALP so cp_parser_parameter_declaration_list
25245 doesn't decide to commit to this parse. */
25246 bool saved_italp = parser->in_template_argument_list_p;
25247 parser->in_template_argument_list_p = true;
25249 cp_parser_parse_tentatively (parser);
25250 cp_lexer_consume_token (parser->lexer);
25252 if (nsdmi)
25254 int ctor_dtor_or_conv_p;
25255 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25256 &ctor_dtor_or_conv_p,
25257 /*parenthesized_p=*/NULL,
25258 /*member_p=*/true,
25259 /*friend_p=*/false);
25261 else
25263 begin_scope (sk_function_parms, NULL_TREE);
25264 cp_parser_parameter_declaration_list (parser, &error);
25265 pop_bindings_and_leave_scope ();
25267 if (!cp_parser_error_occurred (parser) && !error)
25268 done = true;
25269 cp_parser_abort_tentative_parse (parser);
25271 parser->in_template_argument_list_p = saved_italp;
25272 break;
25274 case CPP_CLOSE_PAREN:
25275 case CPP_ELLIPSIS:
25276 /* If we run into a non-nested `;', `}', or `]',
25277 then the code is invalid -- but the default
25278 argument is certainly over. */
25279 case CPP_SEMICOLON:
25280 case CPP_CLOSE_BRACE:
25281 case CPP_CLOSE_SQUARE:
25282 if (depth == 0
25283 /* Handle correctly int n = sizeof ... ( p ); */
25284 && token->type != CPP_ELLIPSIS)
25285 done = true;
25286 /* Update DEPTH, if necessary. */
25287 else if (token->type == CPP_CLOSE_PAREN
25288 || token->type == CPP_CLOSE_BRACE
25289 || token->type == CPP_CLOSE_SQUARE)
25290 --depth;
25291 break;
25293 case CPP_OPEN_PAREN:
25294 case CPP_OPEN_SQUARE:
25295 case CPP_OPEN_BRACE:
25296 ++depth;
25297 break;
25299 case CPP_LESS:
25300 if (depth == 0)
25301 /* This might be the comparison operator, or it might
25302 start a template argument list. */
25303 ++maybe_template_id;
25304 break;
25306 case CPP_RSHIFT:
25307 if (cxx_dialect == cxx98)
25308 break;
25309 /* Fall through for C++0x, which treats the `>>'
25310 operator like two `>' tokens in certain
25311 cases. */
25313 case CPP_GREATER:
25314 if (depth == 0)
25316 /* This might be an operator, or it might close a
25317 template argument list. But if a previous '<'
25318 started a template argument list, this will have
25319 closed it, so we can't be in one anymore. */
25320 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25321 if (maybe_template_id < 0)
25322 maybe_template_id = 0;
25324 break;
25326 /* If we run out of tokens, issue an error message. */
25327 case CPP_EOF:
25328 case CPP_PRAGMA_EOL:
25329 error_at (token->location, "file ends in default argument");
25330 done = true;
25331 break;
25333 case CPP_NAME:
25334 case CPP_SCOPE:
25335 /* In these cases, we should look for template-ids.
25336 For example, if the default argument is
25337 `X<int, double>()', we need to do name lookup to
25338 figure out whether or not `X' is a template; if
25339 so, the `,' does not end the default argument.
25341 That is not yet done. */
25342 break;
25344 default:
25345 break;
25348 /* If we've reached the end, stop. */
25349 if (done)
25350 break;
25352 /* Add the token to the token block. */
25353 token = cp_lexer_consume_token (parser->lexer);
25356 /* Create a DEFAULT_ARG to represent the unparsed default
25357 argument. */
25358 default_argument = make_node (DEFAULT_ARG);
25359 DEFARG_TOKENS (default_argument)
25360 = cp_token_cache_new (first_token, token);
25361 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25363 return default_argument;
25366 /* Begin parsing tentatively. We always save tokens while parsing
25367 tentatively so that if the tentative parsing fails we can restore the
25368 tokens. */
25370 static void
25371 cp_parser_parse_tentatively (cp_parser* parser)
25373 /* Enter a new parsing context. */
25374 parser->context = cp_parser_context_new (parser->context);
25375 /* Begin saving tokens. */
25376 cp_lexer_save_tokens (parser->lexer);
25377 /* In order to avoid repetitive access control error messages,
25378 access checks are queued up until we are no longer parsing
25379 tentatively. */
25380 push_deferring_access_checks (dk_deferred);
25383 /* Commit to the currently active tentative parse. */
25385 static void
25386 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25388 cp_parser_context *context;
25389 cp_lexer *lexer;
25391 /* Mark all of the levels as committed. */
25392 lexer = parser->lexer;
25393 for (context = parser->context; context->next; context = context->next)
25395 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25396 break;
25397 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25398 while (!cp_lexer_saving_tokens (lexer))
25399 lexer = lexer->next;
25400 cp_lexer_commit_tokens (lexer);
25404 /* Commit to the topmost currently active tentative parse.
25406 Note that this function shouldn't be called when there are
25407 irreversible side-effects while in a tentative state. For
25408 example, we shouldn't create a permanent entry in the symbol
25409 table, or issue an error message that might not apply if the
25410 tentative parse is aborted. */
25412 static void
25413 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25415 cp_parser_context *context = parser->context;
25416 cp_lexer *lexer = parser->lexer;
25418 if (context)
25420 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25421 return;
25422 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25424 while (!cp_lexer_saving_tokens (lexer))
25425 lexer = lexer->next;
25426 cp_lexer_commit_tokens (lexer);
25430 /* Abort the currently active tentative parse. All consumed tokens
25431 will be rolled back, and no diagnostics will be issued. */
25433 static void
25434 cp_parser_abort_tentative_parse (cp_parser* parser)
25436 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25437 || errorcount > 0);
25438 cp_parser_simulate_error (parser);
25439 /* Now, pretend that we want to see if the construct was
25440 successfully parsed. */
25441 cp_parser_parse_definitely (parser);
25444 /* Stop parsing tentatively. If a parse error has occurred, restore the
25445 token stream. Otherwise, commit to the tokens we have consumed.
25446 Returns true if no error occurred; false otherwise. */
25448 static bool
25449 cp_parser_parse_definitely (cp_parser* parser)
25451 bool error_occurred;
25452 cp_parser_context *context;
25454 /* Remember whether or not an error occurred, since we are about to
25455 destroy that information. */
25456 error_occurred = cp_parser_error_occurred (parser);
25457 /* Remove the topmost context from the stack. */
25458 context = parser->context;
25459 parser->context = context->next;
25460 /* If no parse errors occurred, commit to the tentative parse. */
25461 if (!error_occurred)
25463 /* Commit to the tokens read tentatively, unless that was
25464 already done. */
25465 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25466 cp_lexer_commit_tokens (parser->lexer);
25468 pop_to_parent_deferring_access_checks ();
25470 /* Otherwise, if errors occurred, roll back our state so that things
25471 are just as they were before we began the tentative parse. */
25472 else
25474 cp_lexer_rollback_tokens (parser->lexer);
25475 pop_deferring_access_checks ();
25477 /* Add the context to the front of the free list. */
25478 context->next = cp_parser_context_free_list;
25479 cp_parser_context_free_list = context;
25481 return !error_occurred;
25484 /* Returns true if we are parsing tentatively and are not committed to
25485 this tentative parse. */
25487 static bool
25488 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25490 return (cp_parser_parsing_tentatively (parser)
25491 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25494 /* Returns nonzero iff an error has occurred during the most recent
25495 tentative parse. */
25497 static bool
25498 cp_parser_error_occurred (cp_parser* parser)
25500 return (cp_parser_parsing_tentatively (parser)
25501 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25504 /* Returns nonzero if GNU extensions are allowed. */
25506 static bool
25507 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25509 return parser->allow_gnu_extensions_p;
25512 /* Objective-C++ Productions */
25515 /* Parse an Objective-C expression, which feeds into a primary-expression
25516 above.
25518 objc-expression:
25519 objc-message-expression
25520 objc-string-literal
25521 objc-encode-expression
25522 objc-protocol-expression
25523 objc-selector-expression
25525 Returns a tree representation of the expression. */
25527 static tree
25528 cp_parser_objc_expression (cp_parser* parser)
25530 /* Try to figure out what kind of declaration is present. */
25531 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25533 switch (kwd->type)
25535 case CPP_OPEN_SQUARE:
25536 return cp_parser_objc_message_expression (parser);
25538 case CPP_OBJC_STRING:
25539 kwd = cp_lexer_consume_token (parser->lexer);
25540 return objc_build_string_object (kwd->u.value);
25542 case CPP_KEYWORD:
25543 switch (kwd->keyword)
25545 case RID_AT_ENCODE:
25546 return cp_parser_objc_encode_expression (parser);
25548 case RID_AT_PROTOCOL:
25549 return cp_parser_objc_protocol_expression (parser);
25551 case RID_AT_SELECTOR:
25552 return cp_parser_objc_selector_expression (parser);
25554 default:
25555 break;
25557 default:
25558 error_at (kwd->location,
25559 "misplaced %<@%D%> Objective-C++ construct",
25560 kwd->u.value);
25561 cp_parser_skip_to_end_of_block_or_statement (parser);
25564 return error_mark_node;
25567 /* Parse an Objective-C message expression.
25569 objc-message-expression:
25570 [ objc-message-receiver objc-message-args ]
25572 Returns a representation of an Objective-C message. */
25574 static tree
25575 cp_parser_objc_message_expression (cp_parser* parser)
25577 tree receiver, messageargs;
25579 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25580 receiver = cp_parser_objc_message_receiver (parser);
25581 messageargs = cp_parser_objc_message_args (parser);
25582 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25584 return objc_build_message_expr (receiver, messageargs);
25587 /* Parse an objc-message-receiver.
25589 objc-message-receiver:
25590 expression
25591 simple-type-specifier
25593 Returns a representation of the type or expression. */
25595 static tree
25596 cp_parser_objc_message_receiver (cp_parser* parser)
25598 tree rcv;
25600 /* An Objective-C message receiver may be either (1) a type
25601 or (2) an expression. */
25602 cp_parser_parse_tentatively (parser);
25603 rcv = cp_parser_expression (parser);
25605 if (cp_parser_parse_definitely (parser))
25606 return rcv;
25608 rcv = cp_parser_simple_type_specifier (parser,
25609 /*decl_specs=*/NULL,
25610 CP_PARSER_FLAGS_NONE);
25612 return objc_get_class_reference (rcv);
25615 /* Parse the arguments and selectors comprising an Objective-C message.
25617 objc-message-args:
25618 objc-selector
25619 objc-selector-args
25620 objc-selector-args , objc-comma-args
25622 objc-selector-args:
25623 objc-selector [opt] : assignment-expression
25624 objc-selector-args objc-selector [opt] : assignment-expression
25626 objc-comma-args:
25627 assignment-expression
25628 objc-comma-args , assignment-expression
25630 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25631 selector arguments and TREE_VALUE containing a list of comma
25632 arguments. */
25634 static tree
25635 cp_parser_objc_message_args (cp_parser* parser)
25637 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25638 bool maybe_unary_selector_p = true;
25639 cp_token *token = cp_lexer_peek_token (parser->lexer);
25641 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25643 tree selector = NULL_TREE, arg;
25645 if (token->type != CPP_COLON)
25646 selector = cp_parser_objc_selector (parser);
25648 /* Detect if we have a unary selector. */
25649 if (maybe_unary_selector_p
25650 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25651 return build_tree_list (selector, NULL_TREE);
25653 maybe_unary_selector_p = false;
25654 cp_parser_require (parser, CPP_COLON, RT_COLON);
25655 arg = cp_parser_assignment_expression (parser);
25657 sel_args
25658 = chainon (sel_args,
25659 build_tree_list (selector, arg));
25661 token = cp_lexer_peek_token (parser->lexer);
25664 /* Handle non-selector arguments, if any. */
25665 while (token->type == CPP_COMMA)
25667 tree arg;
25669 cp_lexer_consume_token (parser->lexer);
25670 arg = cp_parser_assignment_expression (parser);
25672 addl_args
25673 = chainon (addl_args,
25674 build_tree_list (NULL_TREE, arg));
25676 token = cp_lexer_peek_token (parser->lexer);
25679 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25681 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25682 return build_tree_list (error_mark_node, error_mark_node);
25685 return build_tree_list (sel_args, addl_args);
25688 /* Parse an Objective-C encode expression.
25690 objc-encode-expression:
25691 @encode objc-typename
25693 Returns an encoded representation of the type argument. */
25695 static tree
25696 cp_parser_objc_encode_expression (cp_parser* parser)
25698 tree type;
25699 cp_token *token;
25701 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25702 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25703 token = cp_lexer_peek_token (parser->lexer);
25704 type = complete_type (cp_parser_type_id (parser));
25705 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25707 if (!type)
25709 error_at (token->location,
25710 "%<@encode%> must specify a type as an argument");
25711 return error_mark_node;
25714 /* This happens if we find @encode(T) (where T is a template
25715 typename or something dependent on a template typename) when
25716 parsing a template. In that case, we can't compile it
25717 immediately, but we rather create an AT_ENCODE_EXPR which will
25718 need to be instantiated when the template is used.
25720 if (dependent_type_p (type))
25722 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25723 TREE_READONLY (value) = 1;
25724 return value;
25727 return objc_build_encode_expr (type);
25730 /* Parse an Objective-C @defs expression. */
25732 static tree
25733 cp_parser_objc_defs_expression (cp_parser *parser)
25735 tree name;
25737 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25738 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25739 name = cp_parser_identifier (parser);
25740 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25742 return objc_get_class_ivars (name);
25745 /* Parse an Objective-C protocol expression.
25747 objc-protocol-expression:
25748 @protocol ( identifier )
25750 Returns a representation of the protocol expression. */
25752 static tree
25753 cp_parser_objc_protocol_expression (cp_parser* parser)
25755 tree proto;
25757 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25758 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25759 proto = cp_parser_identifier (parser);
25760 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25762 return objc_build_protocol_expr (proto);
25765 /* Parse an Objective-C selector expression.
25767 objc-selector-expression:
25768 @selector ( objc-method-signature )
25770 objc-method-signature:
25771 objc-selector
25772 objc-selector-seq
25774 objc-selector-seq:
25775 objc-selector :
25776 objc-selector-seq objc-selector :
25778 Returns a representation of the method selector. */
25780 static tree
25781 cp_parser_objc_selector_expression (cp_parser* parser)
25783 tree sel_seq = NULL_TREE;
25784 bool maybe_unary_selector_p = true;
25785 cp_token *token;
25786 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25788 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25789 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25790 token = cp_lexer_peek_token (parser->lexer);
25792 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25793 || token->type == CPP_SCOPE)
25795 tree selector = NULL_TREE;
25797 if (token->type != CPP_COLON
25798 || token->type == CPP_SCOPE)
25799 selector = cp_parser_objc_selector (parser);
25801 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25802 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25804 /* Detect if we have a unary selector. */
25805 if (maybe_unary_selector_p)
25807 sel_seq = selector;
25808 goto finish_selector;
25810 else
25812 cp_parser_error (parser, "expected %<:%>");
25815 maybe_unary_selector_p = false;
25816 token = cp_lexer_consume_token (parser->lexer);
25818 if (token->type == CPP_SCOPE)
25820 sel_seq
25821 = chainon (sel_seq,
25822 build_tree_list (selector, NULL_TREE));
25823 sel_seq
25824 = chainon (sel_seq,
25825 build_tree_list (NULL_TREE, NULL_TREE));
25827 else
25828 sel_seq
25829 = chainon (sel_seq,
25830 build_tree_list (selector, NULL_TREE));
25832 token = cp_lexer_peek_token (parser->lexer);
25835 finish_selector:
25836 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25838 return objc_build_selector_expr (loc, sel_seq);
25841 /* Parse a list of identifiers.
25843 objc-identifier-list:
25844 identifier
25845 objc-identifier-list , identifier
25847 Returns a TREE_LIST of identifier nodes. */
25849 static tree
25850 cp_parser_objc_identifier_list (cp_parser* parser)
25852 tree identifier;
25853 tree list;
25854 cp_token *sep;
25856 identifier = cp_parser_identifier (parser);
25857 if (identifier == error_mark_node)
25858 return error_mark_node;
25860 list = build_tree_list (NULL_TREE, identifier);
25861 sep = cp_lexer_peek_token (parser->lexer);
25863 while (sep->type == CPP_COMMA)
25865 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25866 identifier = cp_parser_identifier (parser);
25867 if (identifier == error_mark_node)
25868 return list;
25870 list = chainon (list, build_tree_list (NULL_TREE,
25871 identifier));
25872 sep = cp_lexer_peek_token (parser->lexer);
25875 return list;
25878 /* Parse an Objective-C alias declaration.
25880 objc-alias-declaration:
25881 @compatibility_alias identifier identifier ;
25883 This function registers the alias mapping with the Objective-C front end.
25884 It returns nothing. */
25886 static void
25887 cp_parser_objc_alias_declaration (cp_parser* parser)
25889 tree alias, orig;
25891 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25892 alias = cp_parser_identifier (parser);
25893 orig = cp_parser_identifier (parser);
25894 objc_declare_alias (alias, orig);
25895 cp_parser_consume_semicolon_at_end_of_statement (parser);
25898 /* Parse an Objective-C class forward-declaration.
25900 objc-class-declaration:
25901 @class objc-identifier-list ;
25903 The function registers the forward declarations with the Objective-C
25904 front end. It returns nothing. */
25906 static void
25907 cp_parser_objc_class_declaration (cp_parser* parser)
25909 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25910 while (true)
25912 tree id;
25914 id = cp_parser_identifier (parser);
25915 if (id == error_mark_node)
25916 break;
25918 objc_declare_class (id);
25920 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25921 cp_lexer_consume_token (parser->lexer);
25922 else
25923 break;
25925 cp_parser_consume_semicolon_at_end_of_statement (parser);
25928 /* Parse a list of Objective-C protocol references.
25930 objc-protocol-refs-opt:
25931 objc-protocol-refs [opt]
25933 objc-protocol-refs:
25934 < objc-identifier-list >
25936 Returns a TREE_LIST of identifiers, if any. */
25938 static tree
25939 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25941 tree protorefs = NULL_TREE;
25943 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25945 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25946 protorefs = cp_parser_objc_identifier_list (parser);
25947 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25950 return protorefs;
25953 /* Parse a Objective-C visibility specification. */
25955 static void
25956 cp_parser_objc_visibility_spec (cp_parser* parser)
25958 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25960 switch (vis->keyword)
25962 case RID_AT_PRIVATE:
25963 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25964 break;
25965 case RID_AT_PROTECTED:
25966 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25967 break;
25968 case RID_AT_PUBLIC:
25969 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25970 break;
25971 case RID_AT_PACKAGE:
25972 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25973 break;
25974 default:
25975 return;
25978 /* Eat '@private'/'@protected'/'@public'. */
25979 cp_lexer_consume_token (parser->lexer);
25982 /* Parse an Objective-C method type. Return 'true' if it is a class
25983 (+) method, and 'false' if it is an instance (-) method. */
25985 static inline bool
25986 cp_parser_objc_method_type (cp_parser* parser)
25988 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25989 return true;
25990 else
25991 return false;
25994 /* Parse an Objective-C protocol qualifier. */
25996 static tree
25997 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25999 tree quals = NULL_TREE, node;
26000 cp_token *token = cp_lexer_peek_token (parser->lexer);
26002 node = token->u.value;
26004 while (node && identifier_p (node)
26005 && (node == ridpointers [(int) RID_IN]
26006 || node == ridpointers [(int) RID_OUT]
26007 || node == ridpointers [(int) RID_INOUT]
26008 || node == ridpointers [(int) RID_BYCOPY]
26009 || node == ridpointers [(int) RID_BYREF]
26010 || node == ridpointers [(int) RID_ONEWAY]))
26012 quals = tree_cons (NULL_TREE, node, quals);
26013 cp_lexer_consume_token (parser->lexer);
26014 token = cp_lexer_peek_token (parser->lexer);
26015 node = token->u.value;
26018 return quals;
26021 /* Parse an Objective-C typename. */
26023 static tree
26024 cp_parser_objc_typename (cp_parser* parser)
26026 tree type_name = NULL_TREE;
26028 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26030 tree proto_quals, cp_type = NULL_TREE;
26032 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26033 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26035 /* An ObjC type name may consist of just protocol qualifiers, in which
26036 case the type shall default to 'id'. */
26037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26039 cp_type = cp_parser_type_id (parser);
26041 /* If the type could not be parsed, an error has already
26042 been produced. For error recovery, behave as if it had
26043 not been specified, which will use the default type
26044 'id'. */
26045 if (cp_type == error_mark_node)
26047 cp_type = NULL_TREE;
26048 /* We need to skip to the closing parenthesis as
26049 cp_parser_type_id() does not seem to do it for
26050 us. */
26051 cp_parser_skip_to_closing_parenthesis (parser,
26052 /*recovering=*/true,
26053 /*or_comma=*/false,
26054 /*consume_paren=*/false);
26058 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26059 type_name = build_tree_list (proto_quals, cp_type);
26062 return type_name;
26065 /* Check to see if TYPE refers to an Objective-C selector name. */
26067 static bool
26068 cp_parser_objc_selector_p (enum cpp_ttype type)
26070 return (type == CPP_NAME || type == CPP_KEYWORD
26071 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26072 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26073 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26074 || type == CPP_XOR || type == CPP_XOR_EQ);
26077 /* Parse an Objective-C selector. */
26079 static tree
26080 cp_parser_objc_selector (cp_parser* parser)
26082 cp_token *token = cp_lexer_consume_token (parser->lexer);
26084 if (!cp_parser_objc_selector_p (token->type))
26086 error_at (token->location, "invalid Objective-C++ selector name");
26087 return error_mark_node;
26090 /* C++ operator names are allowed to appear in ObjC selectors. */
26091 switch (token->type)
26093 case CPP_AND_AND: return get_identifier ("and");
26094 case CPP_AND_EQ: return get_identifier ("and_eq");
26095 case CPP_AND: return get_identifier ("bitand");
26096 case CPP_OR: return get_identifier ("bitor");
26097 case CPP_COMPL: return get_identifier ("compl");
26098 case CPP_NOT: return get_identifier ("not");
26099 case CPP_NOT_EQ: return get_identifier ("not_eq");
26100 case CPP_OR_OR: return get_identifier ("or");
26101 case CPP_OR_EQ: return get_identifier ("or_eq");
26102 case CPP_XOR: return get_identifier ("xor");
26103 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26104 default: return token->u.value;
26108 /* Parse an Objective-C params list. */
26110 static tree
26111 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26113 tree params = NULL_TREE;
26114 bool maybe_unary_selector_p = true;
26115 cp_token *token = cp_lexer_peek_token (parser->lexer);
26117 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26119 tree selector = NULL_TREE, type_name, identifier;
26120 tree parm_attr = NULL_TREE;
26122 if (token->keyword == RID_ATTRIBUTE)
26123 break;
26125 if (token->type != CPP_COLON)
26126 selector = cp_parser_objc_selector (parser);
26128 /* Detect if we have a unary selector. */
26129 if (maybe_unary_selector_p
26130 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26132 params = selector; /* Might be followed by attributes. */
26133 break;
26136 maybe_unary_selector_p = false;
26137 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26139 /* Something went quite wrong. There should be a colon
26140 here, but there is not. Stop parsing parameters. */
26141 break;
26143 type_name = cp_parser_objc_typename (parser);
26144 /* New ObjC allows attributes on parameters too. */
26145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26146 parm_attr = cp_parser_attributes_opt (parser);
26147 identifier = cp_parser_identifier (parser);
26149 params
26150 = chainon (params,
26151 objc_build_keyword_decl (selector,
26152 type_name,
26153 identifier,
26154 parm_attr));
26156 token = cp_lexer_peek_token (parser->lexer);
26159 if (params == NULL_TREE)
26161 cp_parser_error (parser, "objective-c++ method declaration is expected");
26162 return error_mark_node;
26165 /* We allow tail attributes for the method. */
26166 if (token->keyword == RID_ATTRIBUTE)
26168 *attributes = cp_parser_attributes_opt (parser);
26169 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26170 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26171 return params;
26172 cp_parser_error (parser,
26173 "method attributes must be specified at the end");
26174 return error_mark_node;
26177 if (params == NULL_TREE)
26179 cp_parser_error (parser, "objective-c++ method declaration is expected");
26180 return error_mark_node;
26182 return params;
26185 /* Parse the non-keyword Objective-C params. */
26187 static tree
26188 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26189 tree* attributes)
26191 tree params = make_node (TREE_LIST);
26192 cp_token *token = cp_lexer_peek_token (parser->lexer);
26193 *ellipsisp = false; /* Initially, assume no ellipsis. */
26195 while (token->type == CPP_COMMA)
26197 cp_parameter_declarator *parmdecl;
26198 tree parm;
26200 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26201 token = cp_lexer_peek_token (parser->lexer);
26203 if (token->type == CPP_ELLIPSIS)
26205 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26206 *ellipsisp = true;
26207 token = cp_lexer_peek_token (parser->lexer);
26208 break;
26211 /* TODO: parse attributes for tail parameters. */
26212 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26213 parm = grokdeclarator (parmdecl->declarator,
26214 &parmdecl->decl_specifiers,
26215 PARM, /*initialized=*/0,
26216 /*attrlist=*/NULL);
26218 chainon (params, build_tree_list (NULL_TREE, parm));
26219 token = cp_lexer_peek_token (parser->lexer);
26222 /* We allow tail attributes for the method. */
26223 if (token->keyword == RID_ATTRIBUTE)
26225 if (*attributes == NULL_TREE)
26227 *attributes = cp_parser_attributes_opt (parser);
26228 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26229 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26230 return params;
26232 else
26233 /* We have an error, but parse the attributes, so that we can
26234 carry on. */
26235 *attributes = cp_parser_attributes_opt (parser);
26237 cp_parser_error (parser,
26238 "method attributes must be specified at the end");
26239 return error_mark_node;
26242 return params;
26245 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26247 static void
26248 cp_parser_objc_interstitial_code (cp_parser* parser)
26250 cp_token *token = cp_lexer_peek_token (parser->lexer);
26252 /* If the next token is `extern' and the following token is a string
26253 literal, then we have a linkage specification. */
26254 if (token->keyword == RID_EXTERN
26255 && cp_parser_is_pure_string_literal
26256 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26257 cp_parser_linkage_specification (parser);
26258 /* Handle #pragma, if any. */
26259 else if (token->type == CPP_PRAGMA)
26260 cp_parser_pragma (parser, pragma_objc_icode);
26261 /* Allow stray semicolons. */
26262 else if (token->type == CPP_SEMICOLON)
26263 cp_lexer_consume_token (parser->lexer);
26264 /* Mark methods as optional or required, when building protocols. */
26265 else if (token->keyword == RID_AT_OPTIONAL)
26267 cp_lexer_consume_token (parser->lexer);
26268 objc_set_method_opt (true);
26270 else if (token->keyword == RID_AT_REQUIRED)
26272 cp_lexer_consume_token (parser->lexer);
26273 objc_set_method_opt (false);
26275 else if (token->keyword == RID_NAMESPACE)
26276 cp_parser_namespace_definition (parser);
26277 /* Other stray characters must generate errors. */
26278 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26280 cp_lexer_consume_token (parser->lexer);
26281 error ("stray %qs between Objective-C++ methods",
26282 token->type == CPP_OPEN_BRACE ? "{" : "}");
26284 /* Finally, try to parse a block-declaration, or a function-definition. */
26285 else
26286 cp_parser_block_declaration (parser, /*statement_p=*/false);
26289 /* Parse a method signature. */
26291 static tree
26292 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26294 tree rettype, kwdparms, optparms;
26295 bool ellipsis = false;
26296 bool is_class_method;
26298 is_class_method = cp_parser_objc_method_type (parser);
26299 rettype = cp_parser_objc_typename (parser);
26300 *attributes = NULL_TREE;
26301 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26302 if (kwdparms == error_mark_node)
26303 return error_mark_node;
26304 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26305 if (optparms == error_mark_node)
26306 return error_mark_node;
26308 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26311 static bool
26312 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26314 tree tattr;
26315 cp_lexer_save_tokens (parser->lexer);
26316 tattr = cp_parser_attributes_opt (parser);
26317 gcc_assert (tattr) ;
26319 /* If the attributes are followed by a method introducer, this is not allowed.
26320 Dump the attributes and flag the situation. */
26321 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26322 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26323 return true;
26325 /* Otherwise, the attributes introduce some interstitial code, possibly so
26326 rewind to allow that check. */
26327 cp_lexer_rollback_tokens (parser->lexer);
26328 return false;
26331 /* Parse an Objective-C method prototype list. */
26333 static void
26334 cp_parser_objc_method_prototype_list (cp_parser* parser)
26336 cp_token *token = cp_lexer_peek_token (parser->lexer);
26338 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26340 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26342 tree attributes, sig;
26343 bool is_class_method;
26344 if (token->type == CPP_PLUS)
26345 is_class_method = true;
26346 else
26347 is_class_method = false;
26348 sig = cp_parser_objc_method_signature (parser, &attributes);
26349 if (sig == error_mark_node)
26351 cp_parser_skip_to_end_of_block_or_statement (parser);
26352 token = cp_lexer_peek_token (parser->lexer);
26353 continue;
26355 objc_add_method_declaration (is_class_method, sig, attributes);
26356 cp_parser_consume_semicolon_at_end_of_statement (parser);
26358 else if (token->keyword == RID_AT_PROPERTY)
26359 cp_parser_objc_at_property_declaration (parser);
26360 else if (token->keyword == RID_ATTRIBUTE
26361 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26362 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26363 OPT_Wattributes,
26364 "prefix attributes are ignored for methods");
26365 else
26366 /* Allow for interspersed non-ObjC++ code. */
26367 cp_parser_objc_interstitial_code (parser);
26369 token = cp_lexer_peek_token (parser->lexer);
26372 if (token->type != CPP_EOF)
26373 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26374 else
26375 cp_parser_error (parser, "expected %<@end%>");
26377 objc_finish_interface ();
26380 /* Parse an Objective-C method definition list. */
26382 static void
26383 cp_parser_objc_method_definition_list (cp_parser* parser)
26385 cp_token *token = cp_lexer_peek_token (parser->lexer);
26387 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26389 tree meth;
26391 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26393 cp_token *ptk;
26394 tree sig, attribute;
26395 bool is_class_method;
26396 if (token->type == CPP_PLUS)
26397 is_class_method = true;
26398 else
26399 is_class_method = false;
26400 push_deferring_access_checks (dk_deferred);
26401 sig = cp_parser_objc_method_signature (parser, &attribute);
26402 if (sig == error_mark_node)
26404 cp_parser_skip_to_end_of_block_or_statement (parser);
26405 token = cp_lexer_peek_token (parser->lexer);
26406 continue;
26408 objc_start_method_definition (is_class_method, sig, attribute,
26409 NULL_TREE);
26411 /* For historical reasons, we accept an optional semicolon. */
26412 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26413 cp_lexer_consume_token (parser->lexer);
26415 ptk = cp_lexer_peek_token (parser->lexer);
26416 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26417 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26419 perform_deferred_access_checks (tf_warning_or_error);
26420 stop_deferring_access_checks ();
26421 meth = cp_parser_function_definition_after_declarator (parser,
26422 false);
26423 pop_deferring_access_checks ();
26424 objc_finish_method_definition (meth);
26427 /* The following case will be removed once @synthesize is
26428 completely implemented. */
26429 else if (token->keyword == RID_AT_PROPERTY)
26430 cp_parser_objc_at_property_declaration (parser);
26431 else if (token->keyword == RID_AT_SYNTHESIZE)
26432 cp_parser_objc_at_synthesize_declaration (parser);
26433 else if (token->keyword == RID_AT_DYNAMIC)
26434 cp_parser_objc_at_dynamic_declaration (parser);
26435 else if (token->keyword == RID_ATTRIBUTE
26436 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26437 warning_at (token->location, OPT_Wattributes,
26438 "prefix attributes are ignored for methods");
26439 else
26440 /* Allow for interspersed non-ObjC++ code. */
26441 cp_parser_objc_interstitial_code (parser);
26443 token = cp_lexer_peek_token (parser->lexer);
26446 if (token->type != CPP_EOF)
26447 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26448 else
26449 cp_parser_error (parser, "expected %<@end%>");
26451 objc_finish_implementation ();
26454 /* Parse Objective-C ivars. */
26456 static void
26457 cp_parser_objc_class_ivars (cp_parser* parser)
26459 cp_token *token = cp_lexer_peek_token (parser->lexer);
26461 if (token->type != CPP_OPEN_BRACE)
26462 return; /* No ivars specified. */
26464 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26465 token = cp_lexer_peek_token (parser->lexer);
26467 while (token->type != CPP_CLOSE_BRACE
26468 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26470 cp_decl_specifier_seq declspecs;
26471 int decl_class_or_enum_p;
26472 tree prefix_attributes;
26474 cp_parser_objc_visibility_spec (parser);
26476 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26477 break;
26479 cp_parser_decl_specifier_seq (parser,
26480 CP_PARSER_FLAGS_OPTIONAL,
26481 &declspecs,
26482 &decl_class_or_enum_p);
26484 /* auto, register, static, extern, mutable. */
26485 if (declspecs.storage_class != sc_none)
26487 cp_parser_error (parser, "invalid type for instance variable");
26488 declspecs.storage_class = sc_none;
26491 /* thread_local. */
26492 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26494 cp_parser_error (parser, "invalid type for instance variable");
26495 declspecs.locations[ds_thread] = 0;
26498 /* typedef. */
26499 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26501 cp_parser_error (parser, "invalid type for instance variable");
26502 declspecs.locations[ds_typedef] = 0;
26505 prefix_attributes = declspecs.attributes;
26506 declspecs.attributes = NULL_TREE;
26508 /* Keep going until we hit the `;' at the end of the
26509 declaration. */
26510 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26512 tree width = NULL_TREE, attributes, first_attribute, decl;
26513 cp_declarator *declarator = NULL;
26514 int ctor_dtor_or_conv_p;
26516 /* Check for a (possibly unnamed) bitfield declaration. */
26517 token = cp_lexer_peek_token (parser->lexer);
26518 if (token->type == CPP_COLON)
26519 goto eat_colon;
26521 if (token->type == CPP_NAME
26522 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26523 == CPP_COLON))
26525 /* Get the name of the bitfield. */
26526 declarator = make_id_declarator (NULL_TREE,
26527 cp_parser_identifier (parser),
26528 sfk_none);
26530 eat_colon:
26531 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26532 /* Get the width of the bitfield. */
26533 width
26534 = cp_parser_constant_expression (parser);
26536 else
26538 /* Parse the declarator. */
26539 declarator
26540 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26541 &ctor_dtor_or_conv_p,
26542 /*parenthesized_p=*/NULL,
26543 /*member_p=*/false,
26544 /*friend_p=*/false);
26547 /* Look for attributes that apply to the ivar. */
26548 attributes = cp_parser_attributes_opt (parser);
26549 /* Remember which attributes are prefix attributes and
26550 which are not. */
26551 first_attribute = attributes;
26552 /* Combine the attributes. */
26553 attributes = chainon (prefix_attributes, attributes);
26555 if (width)
26556 /* Create the bitfield declaration. */
26557 decl = grokbitfield (declarator, &declspecs,
26558 width,
26559 attributes);
26560 else
26561 decl = grokfield (declarator, &declspecs,
26562 NULL_TREE, /*init_const_expr_p=*/false,
26563 NULL_TREE, attributes);
26565 /* Add the instance variable. */
26566 if (decl != error_mark_node && decl != NULL_TREE)
26567 objc_add_instance_variable (decl);
26569 /* Reset PREFIX_ATTRIBUTES. */
26570 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26571 attributes = TREE_CHAIN (attributes);
26572 if (attributes)
26573 TREE_CHAIN (attributes) = NULL_TREE;
26575 token = cp_lexer_peek_token (parser->lexer);
26577 if (token->type == CPP_COMMA)
26579 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26580 continue;
26582 break;
26585 cp_parser_consume_semicolon_at_end_of_statement (parser);
26586 token = cp_lexer_peek_token (parser->lexer);
26589 if (token->keyword == RID_AT_END)
26590 cp_parser_error (parser, "expected %<}%>");
26592 /* Do not consume the RID_AT_END, so it will be read again as terminating
26593 the @interface of @implementation. */
26594 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26595 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26597 /* For historical reasons, we accept an optional semicolon. */
26598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26599 cp_lexer_consume_token (parser->lexer);
26602 /* Parse an Objective-C protocol declaration. */
26604 static void
26605 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26607 tree proto, protorefs;
26608 cp_token *tok;
26610 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26613 tok = cp_lexer_peek_token (parser->lexer);
26614 error_at (tok->location, "identifier expected after %<@protocol%>");
26615 cp_parser_consume_semicolon_at_end_of_statement (parser);
26616 return;
26619 /* See if we have a forward declaration or a definition. */
26620 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26622 /* Try a forward declaration first. */
26623 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26625 while (true)
26627 tree id;
26629 id = cp_parser_identifier (parser);
26630 if (id == error_mark_node)
26631 break;
26633 objc_declare_protocol (id, attributes);
26635 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26636 cp_lexer_consume_token (parser->lexer);
26637 else
26638 break;
26640 cp_parser_consume_semicolon_at_end_of_statement (parser);
26643 /* Ok, we got a full-fledged definition (or at least should). */
26644 else
26646 proto = cp_parser_identifier (parser);
26647 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26648 objc_start_protocol (proto, protorefs, attributes);
26649 cp_parser_objc_method_prototype_list (parser);
26653 /* Parse an Objective-C superclass or category. */
26655 static void
26656 cp_parser_objc_superclass_or_category (cp_parser *parser,
26657 bool iface_p,
26658 tree *super,
26659 tree *categ, bool *is_class_extension)
26661 cp_token *next = cp_lexer_peek_token (parser->lexer);
26663 *super = *categ = NULL_TREE;
26664 *is_class_extension = false;
26665 if (next->type == CPP_COLON)
26667 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26668 *super = cp_parser_identifier (parser);
26670 else if (next->type == CPP_OPEN_PAREN)
26672 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26674 /* If there is no category name, and this is an @interface, we
26675 have a class extension. */
26676 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26678 *categ = NULL_TREE;
26679 *is_class_extension = true;
26681 else
26682 *categ = cp_parser_identifier (parser);
26684 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26688 /* Parse an Objective-C class interface. */
26690 static void
26691 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26693 tree name, super, categ, protos;
26694 bool is_class_extension;
26696 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26697 name = cp_parser_identifier (parser);
26698 if (name == error_mark_node)
26700 /* It's hard to recover because even if valid @interface stuff
26701 is to follow, we can't compile it (or validate it) if we
26702 don't even know which class it refers to. Let's assume this
26703 was a stray '@interface' token in the stream and skip it.
26705 return;
26707 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26708 &is_class_extension);
26709 protos = cp_parser_objc_protocol_refs_opt (parser);
26711 /* We have either a class or a category on our hands. */
26712 if (categ || is_class_extension)
26713 objc_start_category_interface (name, categ, protos, attributes);
26714 else
26716 objc_start_class_interface (name, super, protos, attributes);
26717 /* Handle instance variable declarations, if any. */
26718 cp_parser_objc_class_ivars (parser);
26719 objc_continue_interface ();
26722 cp_parser_objc_method_prototype_list (parser);
26725 /* Parse an Objective-C class implementation. */
26727 static void
26728 cp_parser_objc_class_implementation (cp_parser* parser)
26730 tree name, super, categ;
26731 bool is_class_extension;
26733 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26734 name = cp_parser_identifier (parser);
26735 if (name == error_mark_node)
26737 /* It's hard to recover because even if valid @implementation
26738 stuff is to follow, we can't compile it (or validate it) if
26739 we don't even know which class it refers to. Let's assume
26740 this was a stray '@implementation' token in the stream and
26741 skip it.
26743 return;
26745 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26746 &is_class_extension);
26748 /* We have either a class or a category on our hands. */
26749 if (categ)
26750 objc_start_category_implementation (name, categ);
26751 else
26753 objc_start_class_implementation (name, super);
26754 /* Handle instance variable declarations, if any. */
26755 cp_parser_objc_class_ivars (parser);
26756 objc_continue_implementation ();
26759 cp_parser_objc_method_definition_list (parser);
26762 /* Consume the @end token and finish off the implementation. */
26764 static void
26765 cp_parser_objc_end_implementation (cp_parser* parser)
26767 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26768 objc_finish_implementation ();
26771 /* Parse an Objective-C declaration. */
26773 static void
26774 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26776 /* Try to figure out what kind of declaration is present. */
26777 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26779 if (attributes)
26780 switch (kwd->keyword)
26782 case RID_AT_ALIAS:
26783 case RID_AT_CLASS:
26784 case RID_AT_END:
26785 error_at (kwd->location, "attributes may not be specified before"
26786 " the %<@%D%> Objective-C++ keyword",
26787 kwd->u.value);
26788 attributes = NULL;
26789 break;
26790 case RID_AT_IMPLEMENTATION:
26791 warning_at (kwd->location, OPT_Wattributes,
26792 "prefix attributes are ignored before %<@%D%>",
26793 kwd->u.value);
26794 attributes = NULL;
26795 default:
26796 break;
26799 switch (kwd->keyword)
26801 case RID_AT_ALIAS:
26802 cp_parser_objc_alias_declaration (parser);
26803 break;
26804 case RID_AT_CLASS:
26805 cp_parser_objc_class_declaration (parser);
26806 break;
26807 case RID_AT_PROTOCOL:
26808 cp_parser_objc_protocol_declaration (parser, attributes);
26809 break;
26810 case RID_AT_INTERFACE:
26811 cp_parser_objc_class_interface (parser, attributes);
26812 break;
26813 case RID_AT_IMPLEMENTATION:
26814 cp_parser_objc_class_implementation (parser);
26815 break;
26816 case RID_AT_END:
26817 cp_parser_objc_end_implementation (parser);
26818 break;
26819 default:
26820 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26821 kwd->u.value);
26822 cp_parser_skip_to_end_of_block_or_statement (parser);
26826 /* Parse an Objective-C try-catch-finally statement.
26828 objc-try-catch-finally-stmt:
26829 @try compound-statement objc-catch-clause-seq [opt]
26830 objc-finally-clause [opt]
26832 objc-catch-clause-seq:
26833 objc-catch-clause objc-catch-clause-seq [opt]
26835 objc-catch-clause:
26836 @catch ( objc-exception-declaration ) compound-statement
26838 objc-finally-clause:
26839 @finally compound-statement
26841 objc-exception-declaration:
26842 parameter-declaration
26843 '...'
26845 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26847 Returns NULL_TREE.
26849 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26850 for C. Keep them in sync. */
26852 static tree
26853 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26855 location_t location;
26856 tree stmt;
26858 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26859 location = cp_lexer_peek_token (parser->lexer)->location;
26860 objc_maybe_warn_exceptions (location);
26861 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26862 node, lest it get absorbed into the surrounding block. */
26863 stmt = push_stmt_list ();
26864 cp_parser_compound_statement (parser, NULL, false, false);
26865 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26867 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26869 cp_parameter_declarator *parm;
26870 tree parameter_declaration = error_mark_node;
26871 bool seen_open_paren = false;
26873 cp_lexer_consume_token (parser->lexer);
26874 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26875 seen_open_paren = true;
26876 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26878 /* We have "@catch (...)" (where the '...' are literally
26879 what is in the code). Skip the '...'.
26880 parameter_declaration is set to NULL_TREE, and
26881 objc_being_catch_clauses() knows that that means
26882 '...'. */
26883 cp_lexer_consume_token (parser->lexer);
26884 parameter_declaration = NULL_TREE;
26886 else
26888 /* We have "@catch (NSException *exception)" or something
26889 like that. Parse the parameter declaration. */
26890 parm = cp_parser_parameter_declaration (parser, false, NULL);
26891 if (parm == NULL)
26892 parameter_declaration = error_mark_node;
26893 else
26894 parameter_declaration = grokdeclarator (parm->declarator,
26895 &parm->decl_specifiers,
26896 PARM, /*initialized=*/0,
26897 /*attrlist=*/NULL);
26899 if (seen_open_paren)
26900 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26901 else
26903 /* If there was no open parenthesis, we are recovering from
26904 an error, and we are trying to figure out what mistake
26905 the user has made. */
26907 /* If there is an immediate closing parenthesis, the user
26908 probably forgot the opening one (ie, they typed "@catch
26909 NSException *e)". Parse the closing parenthesis and keep
26910 going. */
26911 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26912 cp_lexer_consume_token (parser->lexer);
26914 /* If these is no immediate closing parenthesis, the user
26915 probably doesn't know that parenthesis are required at
26916 all (ie, they typed "@catch NSException *e"). So, just
26917 forget about the closing parenthesis and keep going. */
26919 objc_begin_catch_clause (parameter_declaration);
26920 cp_parser_compound_statement (parser, NULL, false, false);
26921 objc_finish_catch_clause ();
26923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26925 cp_lexer_consume_token (parser->lexer);
26926 location = cp_lexer_peek_token (parser->lexer)->location;
26927 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26928 node, lest it get absorbed into the surrounding block. */
26929 stmt = push_stmt_list ();
26930 cp_parser_compound_statement (parser, NULL, false, false);
26931 objc_build_finally_clause (location, pop_stmt_list (stmt));
26934 return objc_finish_try_stmt ();
26937 /* Parse an Objective-C synchronized statement.
26939 objc-synchronized-stmt:
26940 @synchronized ( expression ) compound-statement
26942 Returns NULL_TREE. */
26944 static tree
26945 cp_parser_objc_synchronized_statement (cp_parser *parser)
26947 location_t location;
26948 tree lock, stmt;
26950 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26952 location = cp_lexer_peek_token (parser->lexer)->location;
26953 objc_maybe_warn_exceptions (location);
26954 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26955 lock = cp_parser_expression (parser);
26956 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26958 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26959 node, lest it get absorbed into the surrounding block. */
26960 stmt = push_stmt_list ();
26961 cp_parser_compound_statement (parser, NULL, false, false);
26963 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26966 /* Parse an Objective-C throw statement.
26968 objc-throw-stmt:
26969 @throw assignment-expression [opt] ;
26971 Returns a constructed '@throw' statement. */
26973 static tree
26974 cp_parser_objc_throw_statement (cp_parser *parser)
26976 tree expr = NULL_TREE;
26977 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26979 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26982 expr = cp_parser_expression (parser);
26984 cp_parser_consume_semicolon_at_end_of_statement (parser);
26986 return objc_build_throw_stmt (loc, expr);
26989 /* Parse an Objective-C statement. */
26991 static tree
26992 cp_parser_objc_statement (cp_parser * parser)
26994 /* Try to figure out what kind of declaration is present. */
26995 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26997 switch (kwd->keyword)
26999 case RID_AT_TRY:
27000 return cp_parser_objc_try_catch_finally_statement (parser);
27001 case RID_AT_SYNCHRONIZED:
27002 return cp_parser_objc_synchronized_statement (parser);
27003 case RID_AT_THROW:
27004 return cp_parser_objc_throw_statement (parser);
27005 default:
27006 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27007 kwd->u.value);
27008 cp_parser_skip_to_end_of_block_or_statement (parser);
27011 return error_mark_node;
27014 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27015 look ahead to see if an objc keyword follows the attributes. This
27016 is to detect the use of prefix attributes on ObjC @interface and
27017 @protocol. */
27019 static bool
27020 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27022 cp_lexer_save_tokens (parser->lexer);
27023 *attrib = cp_parser_attributes_opt (parser);
27024 gcc_assert (*attrib);
27025 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27027 cp_lexer_commit_tokens (parser->lexer);
27028 return true;
27030 cp_lexer_rollback_tokens (parser->lexer);
27031 return false;
27034 /* This routine is a minimal replacement for
27035 c_parser_struct_declaration () used when parsing the list of
27036 types/names or ObjC++ properties. For example, when parsing the
27037 code
27039 @property (readonly) int a, b, c;
27041 this function is responsible for parsing "int a, int b, int c" and
27042 returning the declarations as CHAIN of DECLs.
27044 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27045 similar parsing. */
27046 static tree
27047 cp_parser_objc_struct_declaration (cp_parser *parser)
27049 tree decls = NULL_TREE;
27050 cp_decl_specifier_seq declspecs;
27051 int decl_class_or_enum_p;
27052 tree prefix_attributes;
27054 cp_parser_decl_specifier_seq (parser,
27055 CP_PARSER_FLAGS_NONE,
27056 &declspecs,
27057 &decl_class_or_enum_p);
27059 if (declspecs.type == error_mark_node)
27060 return error_mark_node;
27062 /* auto, register, static, extern, mutable. */
27063 if (declspecs.storage_class != sc_none)
27065 cp_parser_error (parser, "invalid type for property");
27066 declspecs.storage_class = sc_none;
27069 /* thread_local. */
27070 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27072 cp_parser_error (parser, "invalid type for property");
27073 declspecs.locations[ds_thread] = 0;
27076 /* typedef. */
27077 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27079 cp_parser_error (parser, "invalid type for property");
27080 declspecs.locations[ds_typedef] = 0;
27083 prefix_attributes = declspecs.attributes;
27084 declspecs.attributes = NULL_TREE;
27086 /* Keep going until we hit the `;' at the end of the declaration. */
27087 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27089 tree attributes, first_attribute, decl;
27090 cp_declarator *declarator;
27091 cp_token *token;
27093 /* Parse the declarator. */
27094 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27095 NULL, NULL, false, false);
27097 /* Look for attributes that apply to the ivar. */
27098 attributes = cp_parser_attributes_opt (parser);
27099 /* Remember which attributes are prefix attributes and
27100 which are not. */
27101 first_attribute = attributes;
27102 /* Combine the attributes. */
27103 attributes = chainon (prefix_attributes, attributes);
27105 decl = grokfield (declarator, &declspecs,
27106 NULL_TREE, /*init_const_expr_p=*/false,
27107 NULL_TREE, attributes);
27109 if (decl == error_mark_node || decl == NULL_TREE)
27110 return error_mark_node;
27112 /* Reset PREFIX_ATTRIBUTES. */
27113 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27114 attributes = TREE_CHAIN (attributes);
27115 if (attributes)
27116 TREE_CHAIN (attributes) = NULL_TREE;
27118 DECL_CHAIN (decl) = decls;
27119 decls = decl;
27121 token = cp_lexer_peek_token (parser->lexer);
27122 if (token->type == CPP_COMMA)
27124 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27125 continue;
27127 else
27128 break;
27130 return decls;
27133 /* Parse an Objective-C @property declaration. The syntax is:
27135 objc-property-declaration:
27136 '@property' objc-property-attributes[opt] struct-declaration ;
27138 objc-property-attributes:
27139 '(' objc-property-attribute-list ')'
27141 objc-property-attribute-list:
27142 objc-property-attribute
27143 objc-property-attribute-list, objc-property-attribute
27145 objc-property-attribute
27146 'getter' = identifier
27147 'setter' = identifier
27148 'readonly'
27149 'readwrite'
27150 'assign'
27151 'retain'
27152 'copy'
27153 'nonatomic'
27155 For example:
27156 @property NSString *name;
27157 @property (readonly) id object;
27158 @property (retain, nonatomic, getter=getTheName) id name;
27159 @property int a, b, c;
27161 PS: This function is identical to
27162 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27163 static void
27164 cp_parser_objc_at_property_declaration (cp_parser *parser)
27166 /* The following variables hold the attributes of the properties as
27167 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27168 seen. When we see an attribute, we set them to 'true' (if they
27169 are boolean properties) or to the identifier (if they have an
27170 argument, ie, for getter and setter). Note that here we only
27171 parse the list of attributes, check the syntax and accumulate the
27172 attributes that we find. objc_add_property_declaration() will
27173 then process the information. */
27174 bool property_assign = false;
27175 bool property_copy = false;
27176 tree property_getter_ident = NULL_TREE;
27177 bool property_nonatomic = false;
27178 bool property_readonly = false;
27179 bool property_readwrite = false;
27180 bool property_retain = false;
27181 tree property_setter_ident = NULL_TREE;
27183 /* 'properties' is the list of properties that we read. Usually a
27184 single one, but maybe more (eg, in "@property int a, b, c;" there
27185 are three). */
27186 tree properties;
27187 location_t loc;
27189 loc = cp_lexer_peek_token (parser->lexer)->location;
27191 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27193 /* Parse the optional attribute list... */
27194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27196 /* Eat the '('. */
27197 cp_lexer_consume_token (parser->lexer);
27199 while (true)
27201 bool syntax_error = false;
27202 cp_token *token = cp_lexer_peek_token (parser->lexer);
27203 enum rid keyword;
27205 if (token->type != CPP_NAME)
27207 cp_parser_error (parser, "expected identifier");
27208 break;
27210 keyword = C_RID_CODE (token->u.value);
27211 cp_lexer_consume_token (parser->lexer);
27212 switch (keyword)
27214 case RID_ASSIGN: property_assign = true; break;
27215 case RID_COPY: property_copy = true; break;
27216 case RID_NONATOMIC: property_nonatomic = true; break;
27217 case RID_READONLY: property_readonly = true; break;
27218 case RID_READWRITE: property_readwrite = true; break;
27219 case RID_RETAIN: property_retain = true; break;
27221 case RID_GETTER:
27222 case RID_SETTER:
27223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27225 if (keyword == RID_GETTER)
27226 cp_parser_error (parser,
27227 "missing %<=%> (after %<getter%> attribute)");
27228 else
27229 cp_parser_error (parser,
27230 "missing %<=%> (after %<setter%> attribute)");
27231 syntax_error = true;
27232 break;
27234 cp_lexer_consume_token (parser->lexer); /* eat the = */
27235 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27237 cp_parser_error (parser, "expected identifier");
27238 syntax_error = true;
27239 break;
27241 if (keyword == RID_SETTER)
27243 if (property_setter_ident != NULL_TREE)
27245 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27246 cp_lexer_consume_token (parser->lexer);
27248 else
27249 property_setter_ident = cp_parser_objc_selector (parser);
27250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27251 cp_parser_error (parser, "setter name must terminate with %<:%>");
27252 else
27253 cp_lexer_consume_token (parser->lexer);
27255 else
27257 if (property_getter_ident != NULL_TREE)
27259 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27260 cp_lexer_consume_token (parser->lexer);
27262 else
27263 property_getter_ident = cp_parser_objc_selector (parser);
27265 break;
27266 default:
27267 cp_parser_error (parser, "unknown property attribute");
27268 syntax_error = true;
27269 break;
27272 if (syntax_error)
27273 break;
27275 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27276 cp_lexer_consume_token (parser->lexer);
27277 else
27278 break;
27281 /* FIXME: "@property (setter, assign);" will generate a spurious
27282 "error: expected ‘)’ before ‘,’ token". This is because
27283 cp_parser_require, unlike the C counterpart, will produce an
27284 error even if we are in error recovery. */
27285 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27287 cp_parser_skip_to_closing_parenthesis (parser,
27288 /*recovering=*/true,
27289 /*or_comma=*/false,
27290 /*consume_paren=*/true);
27294 /* ... and the property declaration(s). */
27295 properties = cp_parser_objc_struct_declaration (parser);
27297 if (properties == error_mark_node)
27299 cp_parser_skip_to_end_of_statement (parser);
27300 /* If the next token is now a `;', consume it. */
27301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27302 cp_lexer_consume_token (parser->lexer);
27303 return;
27306 if (properties == NULL_TREE)
27307 cp_parser_error (parser, "expected identifier");
27308 else
27310 /* Comma-separated properties are chained together in
27311 reverse order; add them one by one. */
27312 properties = nreverse (properties);
27314 for (; properties; properties = TREE_CHAIN (properties))
27315 objc_add_property_declaration (loc, copy_node (properties),
27316 property_readonly, property_readwrite,
27317 property_assign, property_retain,
27318 property_copy, property_nonatomic,
27319 property_getter_ident, property_setter_ident);
27322 cp_parser_consume_semicolon_at_end_of_statement (parser);
27325 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27327 objc-synthesize-declaration:
27328 @synthesize objc-synthesize-identifier-list ;
27330 objc-synthesize-identifier-list:
27331 objc-synthesize-identifier
27332 objc-synthesize-identifier-list, objc-synthesize-identifier
27334 objc-synthesize-identifier
27335 identifier
27336 identifier = identifier
27338 For example:
27339 @synthesize MyProperty;
27340 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27342 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27343 for C. Keep them in sync.
27345 static void
27346 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27348 tree list = NULL_TREE;
27349 location_t loc;
27350 loc = cp_lexer_peek_token (parser->lexer)->location;
27352 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27353 while (true)
27355 tree property, ivar;
27356 property = cp_parser_identifier (parser);
27357 if (property == error_mark_node)
27359 cp_parser_consume_semicolon_at_end_of_statement (parser);
27360 return;
27362 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27364 cp_lexer_consume_token (parser->lexer);
27365 ivar = cp_parser_identifier (parser);
27366 if (ivar == error_mark_node)
27368 cp_parser_consume_semicolon_at_end_of_statement (parser);
27369 return;
27372 else
27373 ivar = NULL_TREE;
27374 list = chainon (list, build_tree_list (ivar, property));
27375 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27376 cp_lexer_consume_token (parser->lexer);
27377 else
27378 break;
27380 cp_parser_consume_semicolon_at_end_of_statement (parser);
27381 objc_add_synthesize_declaration (loc, list);
27384 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27386 objc-dynamic-declaration:
27387 @dynamic identifier-list ;
27389 For example:
27390 @dynamic MyProperty;
27391 @dynamic MyProperty, AnotherProperty;
27393 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27394 for C. Keep them in sync.
27396 static void
27397 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27399 tree list = NULL_TREE;
27400 location_t loc;
27401 loc = cp_lexer_peek_token (parser->lexer)->location;
27403 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27404 while (true)
27406 tree property;
27407 property = cp_parser_identifier (parser);
27408 if (property == error_mark_node)
27410 cp_parser_consume_semicolon_at_end_of_statement (parser);
27411 return;
27413 list = chainon (list, build_tree_list (NULL, property));
27414 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27415 cp_lexer_consume_token (parser->lexer);
27416 else
27417 break;
27419 cp_parser_consume_semicolon_at_end_of_statement (parser);
27420 objc_add_dynamic_declaration (loc, list);
27424 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27426 /* Returns name of the next clause.
27427 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27428 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27429 returned and the token is consumed. */
27431 static pragma_omp_clause
27432 cp_parser_omp_clause_name (cp_parser *parser)
27434 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27436 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27437 result = PRAGMA_OMP_CLAUSE_IF;
27438 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27439 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27440 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27441 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27442 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27443 result = PRAGMA_OMP_CLAUSE_FOR;
27444 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27446 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27447 const char *p = IDENTIFIER_POINTER (id);
27449 switch (p[0])
27451 case 'a':
27452 if (!strcmp ("aligned", p))
27453 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27454 break;
27455 case 'c':
27456 if (!strcmp ("collapse", p))
27457 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27458 else if (!strcmp ("copyin", p))
27459 result = PRAGMA_OMP_CLAUSE_COPYIN;
27460 else if (!strcmp ("copyprivate", p))
27461 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27462 break;
27463 case 'd':
27464 if (!strcmp ("depend", p))
27465 result = PRAGMA_OMP_CLAUSE_DEPEND;
27466 else if (!strcmp ("device", p))
27467 result = PRAGMA_OMP_CLAUSE_DEVICE;
27468 else if (!strcmp ("dist_schedule", p))
27469 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27470 break;
27471 case 'f':
27472 if (!strcmp ("final", p))
27473 result = PRAGMA_OMP_CLAUSE_FINAL;
27474 else if (!strcmp ("firstprivate", p))
27475 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27476 else if (!strcmp ("from", p))
27477 result = PRAGMA_OMP_CLAUSE_FROM;
27478 break;
27479 case 'i':
27480 if (!strcmp ("inbranch", p))
27481 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27482 break;
27483 case 'l':
27484 if (!strcmp ("lastprivate", p))
27485 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27486 else if (!strcmp ("linear", p))
27487 result = PRAGMA_OMP_CLAUSE_LINEAR;
27488 break;
27489 case 'm':
27490 if (!strcmp ("map", p))
27491 result = PRAGMA_OMP_CLAUSE_MAP;
27492 else if (!strcmp ("mergeable", p))
27493 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27494 else if (flag_cilkplus && !strcmp ("mask", p))
27495 result = PRAGMA_CILK_CLAUSE_MASK;
27496 break;
27497 case 'n':
27498 if (!strcmp ("notinbranch", p))
27499 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27500 else if (!strcmp ("nowait", p))
27501 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27502 else if (flag_cilkplus && !strcmp ("nomask", p))
27503 result = PRAGMA_CILK_CLAUSE_NOMASK;
27504 else if (!strcmp ("num_teams", p))
27505 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27506 else if (!strcmp ("num_threads", p))
27507 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27508 break;
27509 case 'o':
27510 if (!strcmp ("ordered", p))
27511 result = PRAGMA_OMP_CLAUSE_ORDERED;
27512 break;
27513 case 'p':
27514 if (!strcmp ("parallel", p))
27515 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27516 else if (!strcmp ("proc_bind", p))
27517 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27518 break;
27519 case 'r':
27520 if (!strcmp ("reduction", p))
27521 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27522 break;
27523 case 's':
27524 if (!strcmp ("safelen", p))
27525 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27526 else if (!strcmp ("schedule", p))
27527 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27528 else if (!strcmp ("sections", p))
27529 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27530 else if (!strcmp ("shared", p))
27531 result = PRAGMA_OMP_CLAUSE_SHARED;
27532 else if (!strcmp ("simdlen", p))
27533 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27534 break;
27535 case 't':
27536 if (!strcmp ("taskgroup", p))
27537 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27538 else if (!strcmp ("thread_limit", p))
27539 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27540 else if (!strcmp ("to", p))
27541 result = PRAGMA_OMP_CLAUSE_TO;
27542 break;
27543 case 'u':
27544 if (!strcmp ("uniform", p))
27545 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27546 else if (!strcmp ("untied", p))
27547 result = PRAGMA_OMP_CLAUSE_UNTIED;
27548 break;
27549 case 'v':
27550 if (flag_cilkplus && !strcmp ("vectorlength", p))
27551 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27552 break;
27556 if (result != PRAGMA_OMP_CLAUSE_NONE)
27557 cp_lexer_consume_token (parser->lexer);
27559 return result;
27562 /* Validate that a clause of the given type does not already exist. */
27564 static void
27565 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27566 const char *name, location_t location)
27568 tree c;
27570 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27571 if (OMP_CLAUSE_CODE (c) == code)
27573 error_at (location, "too many %qs clauses", name);
27574 break;
27578 /* OpenMP 2.5:
27579 variable-list:
27580 identifier
27581 variable-list , identifier
27583 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27584 colon). An opening parenthesis will have been consumed by the caller.
27586 If KIND is nonzero, create the appropriate node and install the decl
27587 in OMP_CLAUSE_DECL and add the node to the head of the list.
27589 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27590 return the list created.
27592 COLON can be NULL if only closing parenthesis should end the list,
27593 or pointer to bool which will receive false if the list is terminated
27594 by closing parenthesis or true if the list is terminated by colon. */
27596 static tree
27597 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27598 tree list, bool *colon)
27600 cp_token *token;
27601 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27602 if (colon)
27604 parser->colon_corrects_to_scope_p = false;
27605 *colon = false;
27607 while (1)
27609 tree name, decl;
27611 token = cp_lexer_peek_token (parser->lexer);
27612 name = cp_parser_id_expression (parser, /*template_p=*/false,
27613 /*check_dependency_p=*/true,
27614 /*template_p=*/NULL,
27615 /*declarator_p=*/false,
27616 /*optional_p=*/false);
27617 if (name == error_mark_node)
27618 goto skip_comma;
27620 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27621 if (decl == error_mark_node)
27622 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27623 token->location);
27624 else if (kind != 0)
27626 switch (kind)
27628 case OMP_CLAUSE_MAP:
27629 case OMP_CLAUSE_FROM:
27630 case OMP_CLAUSE_TO:
27631 case OMP_CLAUSE_DEPEND:
27632 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27634 tree low_bound = NULL_TREE, length = NULL_TREE;
27636 parser->colon_corrects_to_scope_p = false;
27637 cp_lexer_consume_token (parser->lexer);
27638 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27639 low_bound = cp_parser_expression (parser);
27640 if (!colon)
27641 parser->colon_corrects_to_scope_p
27642 = saved_colon_corrects_to_scope_p;
27643 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27644 length = integer_one_node;
27645 else
27647 /* Look for `:'. */
27648 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27649 goto skip_comma;
27650 if (!cp_lexer_next_token_is (parser->lexer,
27651 CPP_CLOSE_SQUARE))
27652 length = cp_parser_expression (parser);
27654 /* Look for the closing `]'. */
27655 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27656 RT_CLOSE_SQUARE))
27657 goto skip_comma;
27658 decl = tree_cons (low_bound, length, decl);
27660 break;
27661 default:
27662 break;
27665 tree u = build_omp_clause (token->location, kind);
27666 OMP_CLAUSE_DECL (u) = decl;
27667 OMP_CLAUSE_CHAIN (u) = list;
27668 list = u;
27670 else
27671 list = tree_cons (decl, NULL_TREE, list);
27673 get_comma:
27674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27675 break;
27676 cp_lexer_consume_token (parser->lexer);
27679 if (colon)
27680 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27682 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27684 *colon = true;
27685 cp_parser_require (parser, CPP_COLON, RT_COLON);
27686 return list;
27689 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27691 int ending;
27693 /* Try to resync to an unnested comma. Copied from
27694 cp_parser_parenthesized_expression_list. */
27695 skip_comma:
27696 if (colon)
27697 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27698 ending = cp_parser_skip_to_closing_parenthesis (parser,
27699 /*recovering=*/true,
27700 /*or_comma=*/true,
27701 /*consume_paren=*/true);
27702 if (ending < 0)
27703 goto get_comma;
27706 return list;
27709 /* Similarly, but expect leading and trailing parenthesis. This is a very
27710 common case for omp clauses. */
27712 static tree
27713 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27715 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27716 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27717 return list;
27720 /* OpenMP 3.0:
27721 collapse ( constant-expression ) */
27723 static tree
27724 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27726 tree c, num;
27727 location_t loc;
27728 HOST_WIDE_INT n;
27730 loc = cp_lexer_peek_token (parser->lexer)->location;
27731 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27732 return list;
27734 num = cp_parser_constant_expression (parser);
27736 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27737 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27738 /*or_comma=*/false,
27739 /*consume_paren=*/true);
27741 if (num == error_mark_node)
27742 return list;
27743 num = fold_non_dependent_expr (num);
27744 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27745 || !tree_fits_shwi_p (num)
27746 || (n = tree_to_shwi (num)) <= 0
27747 || (int) n != n)
27749 error_at (loc, "collapse argument needs positive constant integer expression");
27750 return list;
27753 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27754 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27755 OMP_CLAUSE_CHAIN (c) = list;
27756 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27758 return c;
27761 /* OpenMP 2.5:
27762 default ( shared | none ) */
27764 static tree
27765 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27767 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27768 tree c;
27770 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27771 return list;
27772 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27774 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27775 const char *p = IDENTIFIER_POINTER (id);
27777 switch (p[0])
27779 case 'n':
27780 if (strcmp ("none", p) != 0)
27781 goto invalid_kind;
27782 kind = OMP_CLAUSE_DEFAULT_NONE;
27783 break;
27785 case 's':
27786 if (strcmp ("shared", p) != 0)
27787 goto invalid_kind;
27788 kind = OMP_CLAUSE_DEFAULT_SHARED;
27789 break;
27791 default:
27792 goto invalid_kind;
27795 cp_lexer_consume_token (parser->lexer);
27797 else
27799 invalid_kind:
27800 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27803 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27804 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27805 /*or_comma=*/false,
27806 /*consume_paren=*/true);
27808 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27809 return list;
27811 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27812 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27813 OMP_CLAUSE_CHAIN (c) = list;
27814 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27816 return c;
27819 /* OpenMP 3.1:
27820 final ( expression ) */
27822 static tree
27823 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27825 tree t, c;
27827 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27828 return list;
27830 t = cp_parser_condition (parser);
27832 if (t == error_mark_node
27833 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27835 /*or_comma=*/false,
27836 /*consume_paren=*/true);
27838 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27840 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27841 OMP_CLAUSE_FINAL_EXPR (c) = t;
27842 OMP_CLAUSE_CHAIN (c) = list;
27844 return c;
27847 /* OpenMP 2.5:
27848 if ( expression ) */
27850 static tree
27851 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27853 tree t, c;
27855 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27856 return list;
27858 t = cp_parser_condition (parser);
27860 if (t == error_mark_node
27861 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27862 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27863 /*or_comma=*/false,
27864 /*consume_paren=*/true);
27866 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27868 c = build_omp_clause (location, OMP_CLAUSE_IF);
27869 OMP_CLAUSE_IF_EXPR (c) = t;
27870 OMP_CLAUSE_CHAIN (c) = list;
27872 return c;
27875 /* OpenMP 3.1:
27876 mergeable */
27878 static tree
27879 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27880 tree list, location_t location)
27882 tree c;
27884 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27885 location);
27887 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27888 OMP_CLAUSE_CHAIN (c) = list;
27889 return c;
27892 /* OpenMP 2.5:
27893 nowait */
27895 static tree
27896 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27897 tree list, location_t location)
27899 tree c;
27901 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27903 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27904 OMP_CLAUSE_CHAIN (c) = list;
27905 return c;
27908 /* OpenMP 2.5:
27909 num_threads ( expression ) */
27911 static tree
27912 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27913 location_t location)
27915 tree t, c;
27917 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27918 return list;
27920 t = cp_parser_expression (parser);
27922 if (t == error_mark_node
27923 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27924 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27925 /*or_comma=*/false,
27926 /*consume_paren=*/true);
27928 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27929 "num_threads", location);
27931 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27932 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27933 OMP_CLAUSE_CHAIN (c) = list;
27935 return c;
27938 /* OpenMP 2.5:
27939 ordered */
27941 static tree
27942 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27943 tree list, location_t location)
27945 tree c;
27947 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27948 "ordered", location);
27950 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27951 OMP_CLAUSE_CHAIN (c) = list;
27952 return c;
27955 /* OpenMP 2.5:
27956 reduction ( reduction-operator : variable-list )
27958 reduction-operator:
27959 One of: + * - & ^ | && ||
27961 OpenMP 3.1:
27963 reduction-operator:
27964 One of: + * - & ^ | && || min max
27966 OpenMP 4.0:
27968 reduction-operator:
27969 One of: + * - & ^ | && ||
27970 id-expression */
27972 static tree
27973 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27975 enum tree_code code = ERROR_MARK;
27976 tree nlist, c, id = NULL_TREE;
27978 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27979 return list;
27981 switch (cp_lexer_peek_token (parser->lexer)->type)
27983 case CPP_PLUS: code = PLUS_EXPR; break;
27984 case CPP_MULT: code = MULT_EXPR; break;
27985 case CPP_MINUS: code = MINUS_EXPR; break;
27986 case CPP_AND: code = BIT_AND_EXPR; break;
27987 case CPP_XOR: code = BIT_XOR_EXPR; break;
27988 case CPP_OR: code = BIT_IOR_EXPR; break;
27989 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27990 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27991 default: break;
27994 if (code != ERROR_MARK)
27995 cp_lexer_consume_token (parser->lexer);
27996 else
27998 bool saved_colon_corrects_to_scope_p;
27999 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28000 parser->colon_corrects_to_scope_p = false;
28001 id = cp_parser_id_expression (parser, /*template_p=*/false,
28002 /*check_dependency_p=*/true,
28003 /*template_p=*/NULL,
28004 /*declarator_p=*/false,
28005 /*optional_p=*/false);
28006 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28007 if (identifier_p (id))
28009 const char *p = IDENTIFIER_POINTER (id);
28011 if (strcmp (p, "min") == 0)
28012 code = MIN_EXPR;
28013 else if (strcmp (p, "max") == 0)
28014 code = MAX_EXPR;
28015 else if (id == ansi_opname (PLUS_EXPR))
28016 code = PLUS_EXPR;
28017 else if (id == ansi_opname (MULT_EXPR))
28018 code = MULT_EXPR;
28019 else if (id == ansi_opname (MINUS_EXPR))
28020 code = MINUS_EXPR;
28021 else if (id == ansi_opname (BIT_AND_EXPR))
28022 code = BIT_AND_EXPR;
28023 else if (id == ansi_opname (BIT_IOR_EXPR))
28024 code = BIT_IOR_EXPR;
28025 else if (id == ansi_opname (BIT_XOR_EXPR))
28026 code = BIT_XOR_EXPR;
28027 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28028 code = TRUTH_ANDIF_EXPR;
28029 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28030 code = TRUTH_ORIF_EXPR;
28031 id = omp_reduction_id (code, id, NULL_TREE);
28032 tree scope = parser->scope;
28033 if (scope)
28034 id = build_qualified_name (NULL_TREE, scope, id, false);
28035 parser->scope = NULL_TREE;
28036 parser->qualifying_scope = NULL_TREE;
28037 parser->object_scope = NULL_TREE;
28039 else
28041 error ("invalid reduction-identifier");
28042 resync_fail:
28043 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28044 /*or_comma=*/false,
28045 /*consume_paren=*/true);
28046 return list;
28050 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28051 goto resync_fail;
28053 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28054 NULL);
28055 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28057 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28058 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28061 return nlist;
28064 /* OpenMP 2.5:
28065 schedule ( schedule-kind )
28066 schedule ( schedule-kind , expression )
28068 schedule-kind:
28069 static | dynamic | guided | runtime | auto */
28071 static tree
28072 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28074 tree c, t;
28076 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28077 return list;
28079 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28081 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28083 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28084 const char *p = IDENTIFIER_POINTER (id);
28086 switch (p[0])
28088 case 'd':
28089 if (strcmp ("dynamic", p) != 0)
28090 goto invalid_kind;
28091 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28092 break;
28094 case 'g':
28095 if (strcmp ("guided", p) != 0)
28096 goto invalid_kind;
28097 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28098 break;
28100 case 'r':
28101 if (strcmp ("runtime", p) != 0)
28102 goto invalid_kind;
28103 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28104 break;
28106 default:
28107 goto invalid_kind;
28110 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28111 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28112 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28113 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28114 else
28115 goto invalid_kind;
28116 cp_lexer_consume_token (parser->lexer);
28118 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28120 cp_token *token;
28121 cp_lexer_consume_token (parser->lexer);
28123 token = cp_lexer_peek_token (parser->lexer);
28124 t = cp_parser_assignment_expression (parser);
28126 if (t == error_mark_node)
28127 goto resync_fail;
28128 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28129 error_at (token->location, "schedule %<runtime%> does not take "
28130 "a %<chunk_size%> parameter");
28131 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28132 error_at (token->location, "schedule %<auto%> does not take "
28133 "a %<chunk_size%> parameter");
28134 else
28135 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28137 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28138 goto resync_fail;
28140 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28141 goto resync_fail;
28143 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28144 OMP_CLAUSE_CHAIN (c) = list;
28145 return c;
28147 invalid_kind:
28148 cp_parser_error (parser, "invalid schedule kind");
28149 resync_fail:
28150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28151 /*or_comma=*/false,
28152 /*consume_paren=*/true);
28153 return list;
28156 /* OpenMP 3.0:
28157 untied */
28159 static tree
28160 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28161 tree list, location_t location)
28163 tree c;
28165 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28167 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28168 OMP_CLAUSE_CHAIN (c) = list;
28169 return c;
28172 /* OpenMP 4.0:
28173 inbranch
28174 notinbranch */
28176 static tree
28177 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28178 tree list, location_t location)
28180 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28181 tree c = build_omp_clause (location, code);
28182 OMP_CLAUSE_CHAIN (c) = list;
28183 return c;
28186 /* OpenMP 4.0:
28187 parallel
28189 sections
28190 taskgroup */
28192 static tree
28193 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28194 enum omp_clause_code code,
28195 tree list, location_t location)
28197 tree c = build_omp_clause (location, code);
28198 OMP_CLAUSE_CHAIN (c) = list;
28199 return c;
28202 /* OpenMP 4.0:
28203 num_teams ( expression ) */
28205 static tree
28206 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28207 location_t location)
28209 tree t, c;
28211 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28212 return list;
28214 t = cp_parser_expression (parser);
28216 if (t == error_mark_node
28217 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28218 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28219 /*or_comma=*/false,
28220 /*consume_paren=*/true);
28222 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28223 "num_teams", location);
28225 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28226 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28227 OMP_CLAUSE_CHAIN (c) = list;
28229 return c;
28232 /* OpenMP 4.0:
28233 thread_limit ( expression ) */
28235 static tree
28236 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28237 location_t location)
28239 tree t, c;
28241 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28242 return list;
28244 t = cp_parser_expression (parser);
28246 if (t == error_mark_node
28247 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28249 /*or_comma=*/false,
28250 /*consume_paren=*/true);
28252 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28253 "thread_limit", location);
28255 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28256 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28257 OMP_CLAUSE_CHAIN (c) = list;
28259 return c;
28262 /* OpenMP 4.0:
28263 aligned ( variable-list )
28264 aligned ( variable-list : constant-expression ) */
28266 static tree
28267 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28269 tree nlist, c, alignment = NULL_TREE;
28270 bool colon;
28272 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28273 return list;
28275 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28276 &colon);
28278 if (colon)
28280 alignment = cp_parser_constant_expression (parser);
28282 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28283 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28284 /*or_comma=*/false,
28285 /*consume_paren=*/true);
28287 if (alignment == error_mark_node)
28288 alignment = NULL_TREE;
28291 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28292 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28294 return nlist;
28297 /* OpenMP 4.0:
28298 linear ( variable-list )
28299 linear ( variable-list : expression ) */
28301 static tree
28302 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28303 bool is_cilk_simd_fn)
28305 tree nlist, c, step = integer_one_node;
28306 bool colon;
28308 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28309 return list;
28311 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28312 &colon);
28314 if (colon)
28316 step = cp_parser_expression (parser);
28318 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28320 sorry ("using parameters for %<linear%> step is not supported yet");
28321 step = integer_one_node;
28323 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28324 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28325 /*or_comma=*/false,
28326 /*consume_paren=*/true);
28328 if (step == error_mark_node)
28329 return list;
28332 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28333 OMP_CLAUSE_LINEAR_STEP (c) = step;
28335 return nlist;
28338 /* OpenMP 4.0:
28339 safelen ( constant-expression ) */
28341 static tree
28342 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28343 location_t location)
28345 tree t, c;
28347 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28348 return list;
28350 t = cp_parser_constant_expression (parser);
28352 if (t == error_mark_node
28353 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28354 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28355 /*or_comma=*/false,
28356 /*consume_paren=*/true);
28358 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28360 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28361 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28362 OMP_CLAUSE_CHAIN (c) = list;
28364 return c;
28367 /* OpenMP 4.0:
28368 simdlen ( constant-expression ) */
28370 static tree
28371 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28372 location_t location)
28374 tree t, c;
28376 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28377 return list;
28379 t = cp_parser_constant_expression (parser);
28381 if (t == error_mark_node
28382 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28383 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28384 /*or_comma=*/false,
28385 /*consume_paren=*/true);
28387 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28389 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28390 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28391 OMP_CLAUSE_CHAIN (c) = list;
28393 return c;
28396 /* OpenMP 4.0:
28397 depend ( depend-kind : variable-list )
28399 depend-kind:
28400 in | out | inout */
28402 static tree
28403 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28405 tree nlist, c;
28406 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28408 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28409 return list;
28411 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28413 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28414 const char *p = IDENTIFIER_POINTER (id);
28416 if (strcmp ("in", p) == 0)
28417 kind = OMP_CLAUSE_DEPEND_IN;
28418 else if (strcmp ("inout", p) == 0)
28419 kind = OMP_CLAUSE_DEPEND_INOUT;
28420 else if (strcmp ("out", p) == 0)
28421 kind = OMP_CLAUSE_DEPEND_OUT;
28422 else
28423 goto invalid_kind;
28425 else
28426 goto invalid_kind;
28428 cp_lexer_consume_token (parser->lexer);
28429 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28430 goto resync_fail;
28432 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28433 NULL);
28435 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28436 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28438 return nlist;
28440 invalid_kind:
28441 cp_parser_error (parser, "invalid depend kind");
28442 resync_fail:
28443 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28444 /*or_comma=*/false,
28445 /*consume_paren=*/true);
28446 return list;
28449 /* OpenMP 4.0:
28450 map ( map-kind : variable-list )
28451 map ( variable-list )
28453 map-kind:
28454 alloc | to | from | tofrom */
28456 static tree
28457 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28459 tree nlist, c;
28460 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28462 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28463 return list;
28465 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28466 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28468 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28469 const char *p = IDENTIFIER_POINTER (id);
28471 if (strcmp ("alloc", p) == 0)
28472 kind = OMP_CLAUSE_MAP_ALLOC;
28473 else if (strcmp ("to", p) == 0)
28474 kind = OMP_CLAUSE_MAP_TO;
28475 else if (strcmp ("from", p) == 0)
28476 kind = OMP_CLAUSE_MAP_FROM;
28477 else if (strcmp ("tofrom", p) == 0)
28478 kind = OMP_CLAUSE_MAP_TOFROM;
28479 else
28481 cp_parser_error (parser, "invalid map kind");
28482 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28483 /*or_comma=*/false,
28484 /*consume_paren=*/true);
28485 return list;
28487 cp_lexer_consume_token (parser->lexer);
28488 cp_lexer_consume_token (parser->lexer);
28491 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28492 NULL);
28494 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28495 OMP_CLAUSE_MAP_KIND (c) = kind;
28497 return nlist;
28500 /* OpenMP 4.0:
28501 device ( expression ) */
28503 static tree
28504 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28505 location_t location)
28507 tree t, c;
28509 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28510 return list;
28512 t = cp_parser_expression (parser);
28514 if (t == error_mark_node
28515 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28516 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28517 /*or_comma=*/false,
28518 /*consume_paren=*/true);
28520 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28521 "device", location);
28523 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28524 OMP_CLAUSE_DEVICE_ID (c) = t;
28525 OMP_CLAUSE_CHAIN (c) = list;
28527 return c;
28530 /* OpenMP 4.0:
28531 dist_schedule ( static )
28532 dist_schedule ( static , expression ) */
28534 static tree
28535 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28536 location_t location)
28538 tree c, t;
28540 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28541 return list;
28543 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28545 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28546 goto invalid_kind;
28547 cp_lexer_consume_token (parser->lexer);
28549 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28551 cp_lexer_consume_token (parser->lexer);
28553 t = cp_parser_assignment_expression (parser);
28555 if (t == error_mark_node)
28556 goto resync_fail;
28557 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28559 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28560 goto resync_fail;
28562 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28563 goto resync_fail;
28565 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28566 location);
28567 OMP_CLAUSE_CHAIN (c) = list;
28568 return c;
28570 invalid_kind:
28571 cp_parser_error (parser, "invalid dist_schedule kind");
28572 resync_fail:
28573 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28574 /*or_comma=*/false,
28575 /*consume_paren=*/true);
28576 return list;
28579 /* OpenMP 4.0:
28580 proc_bind ( proc-bind-kind )
28582 proc-bind-kind:
28583 master | close | spread */
28585 static tree
28586 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28587 location_t location)
28589 tree c;
28590 enum omp_clause_proc_bind_kind kind;
28592 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28593 return list;
28595 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28597 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28598 const char *p = IDENTIFIER_POINTER (id);
28600 if (strcmp ("master", p) == 0)
28601 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28602 else if (strcmp ("close", p) == 0)
28603 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28604 else if (strcmp ("spread", p) == 0)
28605 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28606 else
28607 goto invalid_kind;
28609 else
28610 goto invalid_kind;
28612 cp_lexer_consume_token (parser->lexer);
28613 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28614 goto resync_fail;
28616 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28617 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28618 location);
28619 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28620 OMP_CLAUSE_CHAIN (c) = list;
28621 return c;
28623 invalid_kind:
28624 cp_parser_error (parser, "invalid depend kind");
28625 resync_fail:
28626 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28627 /*or_comma=*/false,
28628 /*consume_paren=*/true);
28629 return list;
28632 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28633 is a bitmask in MASK. Return the list of clauses found; the result
28634 of clause default goes in *pdefault. */
28636 static tree
28637 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28638 const char *where, cp_token *pragma_tok,
28639 bool finish_p = true)
28641 tree clauses = NULL;
28642 bool first = true;
28643 cp_token *token = NULL;
28644 bool cilk_simd_fn = false;
28646 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28648 pragma_omp_clause c_kind;
28649 const char *c_name;
28650 tree prev = clauses;
28652 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28653 cp_lexer_consume_token (parser->lexer);
28655 token = cp_lexer_peek_token (parser->lexer);
28656 c_kind = cp_parser_omp_clause_name (parser);
28658 switch (c_kind)
28660 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28661 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28662 token->location);
28663 c_name = "collapse";
28664 break;
28665 case PRAGMA_OMP_CLAUSE_COPYIN:
28666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28667 c_name = "copyin";
28668 break;
28669 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28670 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28671 clauses);
28672 c_name = "copyprivate";
28673 break;
28674 case PRAGMA_OMP_CLAUSE_DEFAULT:
28675 clauses = cp_parser_omp_clause_default (parser, clauses,
28676 token->location);
28677 c_name = "default";
28678 break;
28679 case PRAGMA_OMP_CLAUSE_FINAL:
28680 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28681 c_name = "final";
28682 break;
28683 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28684 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28685 clauses);
28686 c_name = "firstprivate";
28687 break;
28688 case PRAGMA_OMP_CLAUSE_IF:
28689 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28690 c_name = "if";
28691 break;
28692 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28693 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28694 clauses);
28695 c_name = "lastprivate";
28696 break;
28697 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28698 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28699 token->location);
28700 c_name = "mergeable";
28701 break;
28702 case PRAGMA_OMP_CLAUSE_NOWAIT:
28703 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28704 c_name = "nowait";
28705 break;
28706 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28707 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28708 token->location);
28709 c_name = "num_threads";
28710 break;
28711 case PRAGMA_OMP_CLAUSE_ORDERED:
28712 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28713 token->location);
28714 c_name = "ordered";
28715 break;
28716 case PRAGMA_OMP_CLAUSE_PRIVATE:
28717 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28718 clauses);
28719 c_name = "private";
28720 break;
28721 case PRAGMA_OMP_CLAUSE_REDUCTION:
28722 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28723 c_name = "reduction";
28724 break;
28725 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28726 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28727 token->location);
28728 c_name = "schedule";
28729 break;
28730 case PRAGMA_OMP_CLAUSE_SHARED:
28731 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28732 clauses);
28733 c_name = "shared";
28734 break;
28735 case PRAGMA_OMP_CLAUSE_UNTIED:
28736 clauses = cp_parser_omp_clause_untied (parser, clauses,
28737 token->location);
28738 c_name = "untied";
28739 break;
28740 case PRAGMA_OMP_CLAUSE_INBRANCH:
28741 case PRAGMA_CILK_CLAUSE_MASK:
28742 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28743 clauses, token->location);
28744 c_name = "inbranch";
28745 break;
28746 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28747 case PRAGMA_CILK_CLAUSE_NOMASK:
28748 clauses = cp_parser_omp_clause_branch (parser,
28749 OMP_CLAUSE_NOTINBRANCH,
28750 clauses, token->location);
28751 c_name = "notinbranch";
28752 break;
28753 case PRAGMA_OMP_CLAUSE_PARALLEL:
28754 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28755 clauses, token->location);
28756 c_name = "parallel";
28757 if (!first)
28759 clause_not_first:
28760 error_at (token->location, "%qs must be the first clause of %qs",
28761 c_name, where);
28762 clauses = prev;
28764 break;
28765 case PRAGMA_OMP_CLAUSE_FOR:
28766 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28767 clauses, token->location);
28768 c_name = "for";
28769 if (!first)
28770 goto clause_not_first;
28771 break;
28772 case PRAGMA_OMP_CLAUSE_SECTIONS:
28773 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28774 clauses, token->location);
28775 c_name = "sections";
28776 if (!first)
28777 goto clause_not_first;
28778 break;
28779 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28780 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28781 clauses, token->location);
28782 c_name = "taskgroup";
28783 if (!first)
28784 goto clause_not_first;
28785 break;
28786 case PRAGMA_OMP_CLAUSE_TO:
28787 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28788 clauses);
28789 c_name = "to";
28790 break;
28791 case PRAGMA_OMP_CLAUSE_FROM:
28792 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28793 clauses);
28794 c_name = "from";
28795 break;
28796 case PRAGMA_OMP_CLAUSE_UNIFORM:
28797 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28798 clauses);
28799 c_name = "uniform";
28800 break;
28801 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28802 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28803 token->location);
28804 c_name = "num_teams";
28805 break;
28806 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28807 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28808 token->location);
28809 c_name = "thread_limit";
28810 break;
28811 case PRAGMA_OMP_CLAUSE_ALIGNED:
28812 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28813 c_name = "aligned";
28814 break;
28815 case PRAGMA_OMP_CLAUSE_LINEAR:
28816 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28817 cilk_simd_fn = true;
28818 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28819 c_name = "linear";
28820 break;
28821 case PRAGMA_OMP_CLAUSE_DEPEND:
28822 clauses = cp_parser_omp_clause_depend (parser, clauses);
28823 c_name = "depend";
28824 break;
28825 case PRAGMA_OMP_CLAUSE_MAP:
28826 clauses = cp_parser_omp_clause_map (parser, clauses);
28827 c_name = "map";
28828 break;
28829 case PRAGMA_OMP_CLAUSE_DEVICE:
28830 clauses = cp_parser_omp_clause_device (parser, clauses,
28831 token->location);
28832 c_name = "device";
28833 break;
28834 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28835 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28836 token->location);
28837 c_name = "dist_schedule";
28838 break;
28839 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28840 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28841 token->location);
28842 c_name = "proc_bind";
28843 break;
28844 case PRAGMA_OMP_CLAUSE_SAFELEN:
28845 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28846 token->location);
28847 c_name = "safelen";
28848 break;
28849 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28850 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28851 token->location);
28852 c_name = "simdlen";
28853 break;
28854 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28855 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28856 c_name = "simdlen";
28857 break;
28858 default:
28859 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28860 goto saw_error;
28863 first = false;
28865 if (((mask >> c_kind) & 1) == 0)
28867 /* Remove the invalid clause(s) from the list to avoid
28868 confusing the rest of the compiler. */
28869 clauses = prev;
28870 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28873 saw_error:
28874 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28875 no reason to skip to the end. */
28876 if (!(flag_cilkplus && pragma_tok == NULL))
28877 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28878 if (finish_p)
28879 return finish_omp_clauses (clauses);
28880 return clauses;
28883 /* OpenMP 2.5:
28884 structured-block:
28885 statement
28887 In practice, we're also interested in adding the statement to an
28888 outer node. So it is convenient if we work around the fact that
28889 cp_parser_statement calls add_stmt. */
28891 static unsigned
28892 cp_parser_begin_omp_structured_block (cp_parser *parser)
28894 unsigned save = parser->in_statement;
28896 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28897 This preserves the "not within loop or switch" style error messages
28898 for nonsense cases like
28899 void foo() {
28900 #pragma omp single
28901 break;
28904 if (parser->in_statement)
28905 parser->in_statement = IN_OMP_BLOCK;
28907 return save;
28910 static void
28911 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28913 parser->in_statement = save;
28916 static tree
28917 cp_parser_omp_structured_block (cp_parser *parser)
28919 tree stmt = begin_omp_structured_block ();
28920 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28922 cp_parser_statement (parser, NULL_TREE, false, NULL);
28924 cp_parser_end_omp_structured_block (parser, save);
28925 return finish_omp_structured_block (stmt);
28928 /* OpenMP 2.5:
28929 # pragma omp atomic new-line
28930 expression-stmt
28932 expression-stmt:
28933 x binop= expr | x++ | ++x | x-- | --x
28934 binop:
28935 +, *, -, /, &, ^, |, <<, >>
28937 where x is an lvalue expression with scalar type.
28939 OpenMP 3.1:
28940 # pragma omp atomic new-line
28941 update-stmt
28943 # pragma omp atomic read new-line
28944 read-stmt
28946 # pragma omp atomic write new-line
28947 write-stmt
28949 # pragma omp atomic update new-line
28950 update-stmt
28952 # pragma omp atomic capture new-line
28953 capture-stmt
28955 # pragma omp atomic capture new-line
28956 capture-block
28958 read-stmt:
28959 v = x
28960 write-stmt:
28961 x = expr
28962 update-stmt:
28963 expression-stmt | x = x binop expr
28964 capture-stmt:
28965 v = expression-stmt
28966 capture-block:
28967 { v = x; update-stmt; } | { update-stmt; v = x; }
28969 OpenMP 4.0:
28970 update-stmt:
28971 expression-stmt | x = x binop expr | x = expr binop x
28972 capture-stmt:
28973 v = update-stmt
28974 capture-block:
28975 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28977 where x and v are lvalue expressions with scalar type. */
28979 static void
28980 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28982 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28983 tree rhs1 = NULL_TREE, orig_lhs;
28984 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28985 bool structured_block = false;
28986 bool seq_cst = false;
28988 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28990 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28991 const char *p = IDENTIFIER_POINTER (id);
28993 if (!strcmp (p, "seq_cst"))
28995 seq_cst = true;
28996 cp_lexer_consume_token (parser->lexer);
28997 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28998 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28999 cp_lexer_consume_token (parser->lexer);
29002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29004 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29005 const char *p = IDENTIFIER_POINTER (id);
29007 if (!strcmp (p, "read"))
29008 code = OMP_ATOMIC_READ;
29009 else if (!strcmp (p, "write"))
29010 code = NOP_EXPR;
29011 else if (!strcmp (p, "update"))
29012 code = OMP_ATOMIC;
29013 else if (!strcmp (p, "capture"))
29014 code = OMP_ATOMIC_CAPTURE_NEW;
29015 else
29016 p = NULL;
29017 if (p)
29018 cp_lexer_consume_token (parser->lexer);
29020 if (!seq_cst)
29022 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29023 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29024 cp_lexer_consume_token (parser->lexer);
29026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29028 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29029 const char *p = IDENTIFIER_POINTER (id);
29031 if (!strcmp (p, "seq_cst"))
29033 seq_cst = true;
29034 cp_lexer_consume_token (parser->lexer);
29038 cp_parser_require_pragma_eol (parser, pragma_tok);
29040 switch (code)
29042 case OMP_ATOMIC_READ:
29043 case NOP_EXPR: /* atomic write */
29044 v = cp_parser_unary_expression (parser);
29045 if (v == error_mark_node)
29046 goto saw_error;
29047 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29048 goto saw_error;
29049 if (code == NOP_EXPR)
29050 lhs = cp_parser_expression (parser);
29051 else
29052 lhs = cp_parser_unary_expression (parser);
29053 if (lhs == error_mark_node)
29054 goto saw_error;
29055 if (code == NOP_EXPR)
29057 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29058 opcode. */
29059 code = OMP_ATOMIC;
29060 rhs = lhs;
29061 lhs = v;
29062 v = NULL_TREE;
29064 goto done;
29065 case OMP_ATOMIC_CAPTURE_NEW:
29066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29068 cp_lexer_consume_token (parser->lexer);
29069 structured_block = true;
29071 else
29073 v = cp_parser_unary_expression (parser);
29074 if (v == error_mark_node)
29075 goto saw_error;
29076 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29077 goto saw_error;
29079 default:
29080 break;
29083 restart:
29084 lhs = cp_parser_unary_expression (parser);
29085 orig_lhs = lhs;
29086 switch (TREE_CODE (lhs))
29088 case ERROR_MARK:
29089 goto saw_error;
29091 case POSTINCREMENT_EXPR:
29092 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29093 code = OMP_ATOMIC_CAPTURE_OLD;
29094 /* FALLTHROUGH */
29095 case PREINCREMENT_EXPR:
29096 lhs = TREE_OPERAND (lhs, 0);
29097 opcode = PLUS_EXPR;
29098 rhs = integer_one_node;
29099 break;
29101 case POSTDECREMENT_EXPR:
29102 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29103 code = OMP_ATOMIC_CAPTURE_OLD;
29104 /* FALLTHROUGH */
29105 case PREDECREMENT_EXPR:
29106 lhs = TREE_OPERAND (lhs, 0);
29107 opcode = MINUS_EXPR;
29108 rhs = integer_one_node;
29109 break;
29111 case COMPOUND_EXPR:
29112 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29113 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29114 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29115 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29116 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29117 (TREE_OPERAND (lhs, 1), 0), 0)))
29118 == BOOLEAN_TYPE)
29119 /* Undo effects of boolean_increment for post {in,de}crement. */
29120 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29121 /* FALLTHRU */
29122 case MODIFY_EXPR:
29123 if (TREE_CODE (lhs) == MODIFY_EXPR
29124 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29126 /* Undo effects of boolean_increment. */
29127 if (integer_onep (TREE_OPERAND (lhs, 1)))
29129 /* This is pre or post increment. */
29130 rhs = TREE_OPERAND (lhs, 1);
29131 lhs = TREE_OPERAND (lhs, 0);
29132 opcode = NOP_EXPR;
29133 if (code == OMP_ATOMIC_CAPTURE_NEW
29134 && !structured_block
29135 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29136 code = OMP_ATOMIC_CAPTURE_OLD;
29137 break;
29140 /* FALLTHRU */
29141 default:
29142 switch (cp_lexer_peek_token (parser->lexer)->type)
29144 case CPP_MULT_EQ:
29145 opcode = MULT_EXPR;
29146 break;
29147 case CPP_DIV_EQ:
29148 opcode = TRUNC_DIV_EXPR;
29149 break;
29150 case CPP_PLUS_EQ:
29151 opcode = PLUS_EXPR;
29152 break;
29153 case CPP_MINUS_EQ:
29154 opcode = MINUS_EXPR;
29155 break;
29156 case CPP_LSHIFT_EQ:
29157 opcode = LSHIFT_EXPR;
29158 break;
29159 case CPP_RSHIFT_EQ:
29160 opcode = RSHIFT_EXPR;
29161 break;
29162 case CPP_AND_EQ:
29163 opcode = BIT_AND_EXPR;
29164 break;
29165 case CPP_OR_EQ:
29166 opcode = BIT_IOR_EXPR;
29167 break;
29168 case CPP_XOR_EQ:
29169 opcode = BIT_XOR_EXPR;
29170 break;
29171 case CPP_EQ:
29172 enum cp_parser_prec oprec;
29173 cp_token *token;
29174 cp_lexer_consume_token (parser->lexer);
29175 cp_parser_parse_tentatively (parser);
29176 rhs1 = cp_parser_simple_cast_expression (parser);
29177 if (rhs1 == error_mark_node)
29179 cp_parser_abort_tentative_parse (parser);
29180 cp_parser_simple_cast_expression (parser);
29181 goto saw_error;
29183 token = cp_lexer_peek_token (parser->lexer);
29184 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29186 cp_parser_abort_tentative_parse (parser);
29187 cp_parser_parse_tentatively (parser);
29188 rhs = cp_parser_binary_expression (parser, false, true,
29189 PREC_NOT_OPERATOR, NULL);
29190 if (rhs == error_mark_node)
29192 cp_parser_abort_tentative_parse (parser);
29193 cp_parser_binary_expression (parser, false, true,
29194 PREC_NOT_OPERATOR, NULL);
29195 goto saw_error;
29197 switch (TREE_CODE (rhs))
29199 case MULT_EXPR:
29200 case TRUNC_DIV_EXPR:
29201 case PLUS_EXPR:
29202 case MINUS_EXPR:
29203 case LSHIFT_EXPR:
29204 case RSHIFT_EXPR:
29205 case BIT_AND_EXPR:
29206 case BIT_IOR_EXPR:
29207 case BIT_XOR_EXPR:
29208 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29210 if (cp_parser_parse_definitely (parser))
29212 opcode = TREE_CODE (rhs);
29213 rhs1 = TREE_OPERAND (rhs, 0);
29214 rhs = TREE_OPERAND (rhs, 1);
29215 goto stmt_done;
29217 else
29218 goto saw_error;
29220 break;
29221 default:
29222 break;
29224 cp_parser_abort_tentative_parse (parser);
29225 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29227 rhs = cp_parser_expression (parser);
29228 if (rhs == error_mark_node)
29229 goto saw_error;
29230 opcode = NOP_EXPR;
29231 rhs1 = NULL_TREE;
29232 goto stmt_done;
29234 cp_parser_error (parser,
29235 "invalid form of %<#pragma omp atomic%>");
29236 goto saw_error;
29238 if (!cp_parser_parse_definitely (parser))
29239 goto saw_error;
29240 switch (token->type)
29242 case CPP_SEMICOLON:
29243 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29245 code = OMP_ATOMIC_CAPTURE_OLD;
29246 v = lhs;
29247 lhs = NULL_TREE;
29248 lhs1 = rhs1;
29249 rhs1 = NULL_TREE;
29250 cp_lexer_consume_token (parser->lexer);
29251 goto restart;
29253 else if (structured_block)
29255 opcode = NOP_EXPR;
29256 rhs = rhs1;
29257 rhs1 = NULL_TREE;
29258 goto stmt_done;
29260 cp_parser_error (parser,
29261 "invalid form of %<#pragma omp atomic%>");
29262 goto saw_error;
29263 case CPP_MULT:
29264 opcode = MULT_EXPR;
29265 break;
29266 case CPP_DIV:
29267 opcode = TRUNC_DIV_EXPR;
29268 break;
29269 case CPP_PLUS:
29270 opcode = PLUS_EXPR;
29271 break;
29272 case CPP_MINUS:
29273 opcode = MINUS_EXPR;
29274 break;
29275 case CPP_LSHIFT:
29276 opcode = LSHIFT_EXPR;
29277 break;
29278 case CPP_RSHIFT:
29279 opcode = RSHIFT_EXPR;
29280 break;
29281 case CPP_AND:
29282 opcode = BIT_AND_EXPR;
29283 break;
29284 case CPP_OR:
29285 opcode = BIT_IOR_EXPR;
29286 break;
29287 case CPP_XOR:
29288 opcode = BIT_XOR_EXPR;
29289 break;
29290 default:
29291 cp_parser_error (parser,
29292 "invalid operator for %<#pragma omp atomic%>");
29293 goto saw_error;
29295 oprec = TOKEN_PRECEDENCE (token);
29296 gcc_assert (oprec != PREC_NOT_OPERATOR);
29297 if (commutative_tree_code (opcode))
29298 oprec = (enum cp_parser_prec) (oprec - 1);
29299 cp_lexer_consume_token (parser->lexer);
29300 rhs = cp_parser_binary_expression (parser, false, false,
29301 oprec, NULL);
29302 if (rhs == error_mark_node)
29303 goto saw_error;
29304 goto stmt_done;
29305 /* FALLTHROUGH */
29306 default:
29307 cp_parser_error (parser,
29308 "invalid operator for %<#pragma omp atomic%>");
29309 goto saw_error;
29311 cp_lexer_consume_token (parser->lexer);
29313 rhs = cp_parser_expression (parser);
29314 if (rhs == error_mark_node)
29315 goto saw_error;
29316 break;
29318 stmt_done:
29319 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29321 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29322 goto saw_error;
29323 v = cp_parser_unary_expression (parser);
29324 if (v == error_mark_node)
29325 goto saw_error;
29326 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29327 goto saw_error;
29328 lhs1 = cp_parser_unary_expression (parser);
29329 if (lhs1 == error_mark_node)
29330 goto saw_error;
29332 if (structured_block)
29334 cp_parser_consume_semicolon_at_end_of_statement (parser);
29335 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29337 done:
29338 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29339 if (!structured_block)
29340 cp_parser_consume_semicolon_at_end_of_statement (parser);
29341 return;
29343 saw_error:
29344 cp_parser_skip_to_end_of_block_or_statement (parser);
29345 if (structured_block)
29347 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29348 cp_lexer_consume_token (parser->lexer);
29349 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29351 cp_parser_skip_to_end_of_block_or_statement (parser);
29352 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29353 cp_lexer_consume_token (parser->lexer);
29359 /* OpenMP 2.5:
29360 # pragma omp barrier new-line */
29362 static void
29363 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29365 cp_parser_require_pragma_eol (parser, pragma_tok);
29366 finish_omp_barrier ();
29369 /* OpenMP 2.5:
29370 # pragma omp critical [(name)] new-line
29371 structured-block */
29373 static tree
29374 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29376 tree stmt, name = NULL;
29378 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29380 cp_lexer_consume_token (parser->lexer);
29382 name = cp_parser_identifier (parser);
29384 if (name == error_mark_node
29385 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29386 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29387 /*or_comma=*/false,
29388 /*consume_paren=*/true);
29389 if (name == error_mark_node)
29390 name = NULL;
29392 cp_parser_require_pragma_eol (parser, pragma_tok);
29394 stmt = cp_parser_omp_structured_block (parser);
29395 return c_finish_omp_critical (input_location, stmt, name);
29398 /* OpenMP 2.5:
29399 # pragma omp flush flush-vars[opt] new-line
29401 flush-vars:
29402 ( variable-list ) */
29404 static void
29405 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29407 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29408 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29409 cp_parser_require_pragma_eol (parser, pragma_tok);
29411 finish_omp_flush ();
29414 /* Helper function, to parse omp for increment expression. */
29416 static tree
29417 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29419 tree cond = cp_parser_binary_expression (parser, false, true,
29420 PREC_NOT_OPERATOR, NULL);
29421 if (cond == error_mark_node
29422 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29424 cp_parser_skip_to_end_of_statement (parser);
29425 return error_mark_node;
29428 switch (TREE_CODE (cond))
29430 case GT_EXPR:
29431 case GE_EXPR:
29432 case LT_EXPR:
29433 case LE_EXPR:
29434 break;
29435 case NE_EXPR:
29436 if (code == CILK_SIMD || code == CILK_FOR)
29437 break;
29438 /* Fall through: OpenMP disallows NE_EXPR. */
29439 default:
29440 return error_mark_node;
29443 /* If decl is an iterator, preserve LHS and RHS of the relational
29444 expr until finish_omp_for. */
29445 if (decl
29446 && (type_dependent_expression_p (decl)
29447 || CLASS_TYPE_P (TREE_TYPE (decl))))
29448 return cond;
29450 return build_x_binary_op (input_location, TREE_CODE (cond),
29451 TREE_OPERAND (cond, 0), ERROR_MARK,
29452 TREE_OPERAND (cond, 1), ERROR_MARK,
29453 /*overload=*/NULL, tf_warning_or_error);
29456 /* Helper function, to parse omp for increment expression. */
29458 static tree
29459 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29461 cp_token *token = cp_lexer_peek_token (parser->lexer);
29462 enum tree_code op;
29463 tree lhs, rhs;
29464 cp_id_kind idk;
29465 bool decl_first;
29467 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29469 op = (token->type == CPP_PLUS_PLUS
29470 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29471 cp_lexer_consume_token (parser->lexer);
29472 lhs = cp_parser_simple_cast_expression (parser);
29473 if (lhs != decl)
29474 return error_mark_node;
29475 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29478 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29479 if (lhs != decl)
29480 return error_mark_node;
29482 token = cp_lexer_peek_token (parser->lexer);
29483 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29485 op = (token->type == CPP_PLUS_PLUS
29486 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29487 cp_lexer_consume_token (parser->lexer);
29488 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29491 op = cp_parser_assignment_operator_opt (parser);
29492 if (op == ERROR_MARK)
29493 return error_mark_node;
29495 if (op != NOP_EXPR)
29497 rhs = cp_parser_assignment_expression (parser);
29498 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29499 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29502 lhs = cp_parser_binary_expression (parser, false, false,
29503 PREC_ADDITIVE_EXPRESSION, NULL);
29504 token = cp_lexer_peek_token (parser->lexer);
29505 decl_first = lhs == decl;
29506 if (decl_first)
29507 lhs = NULL_TREE;
29508 if (token->type != CPP_PLUS
29509 && token->type != CPP_MINUS)
29510 return error_mark_node;
29514 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29515 cp_lexer_consume_token (parser->lexer);
29516 rhs = cp_parser_binary_expression (parser, false, false,
29517 PREC_ADDITIVE_EXPRESSION, NULL);
29518 token = cp_lexer_peek_token (parser->lexer);
29519 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29521 if (lhs == NULL_TREE)
29523 if (op == PLUS_EXPR)
29524 lhs = rhs;
29525 else
29526 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29527 tf_warning_or_error);
29529 else
29530 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29531 ERROR_MARK, NULL, tf_warning_or_error);
29534 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29536 if (!decl_first)
29538 if (rhs != decl || op == MINUS_EXPR)
29539 return error_mark_node;
29540 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29542 else
29543 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29545 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29548 /* Parse the initialization statement of either an OpenMP for loop or
29549 a Cilk Plus for loop.
29551 Return true if the resulting construct should have an
29552 OMP_CLAUSE_PRIVATE added to it. */
29554 static bool
29555 cp_parser_omp_for_loop_init (cp_parser *parser,
29556 enum tree_code code,
29557 tree &this_pre_body,
29558 vec<tree, va_gc> *for_block,
29559 tree &init,
29560 tree &decl,
29561 tree &real_decl)
29563 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29564 return false;
29566 bool add_private_clause = false;
29568 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29570 init-expr:
29571 var = lb
29572 integer-type var = lb
29573 random-access-iterator-type var = lb
29574 pointer-type var = lb
29576 cp_decl_specifier_seq type_specifiers;
29578 /* First, try to parse as an initialized declaration. See
29579 cp_parser_condition, from whence the bulk of this is copied. */
29581 cp_parser_parse_tentatively (parser);
29582 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29583 /*is_trailing_return=*/false,
29584 &type_specifiers);
29585 if (cp_parser_parse_definitely (parser))
29587 /* If parsing a type specifier seq succeeded, then this
29588 MUST be a initialized declaration. */
29589 tree asm_specification, attributes;
29590 cp_declarator *declarator;
29592 declarator = cp_parser_declarator (parser,
29593 CP_PARSER_DECLARATOR_NAMED,
29594 /*ctor_dtor_or_conv_p=*/NULL,
29595 /*parenthesized_p=*/NULL,
29596 /*member_p=*/false,
29597 /*friend_p=*/false);
29598 attributes = cp_parser_attributes_opt (parser);
29599 asm_specification = cp_parser_asm_specification_opt (parser);
29601 if (declarator == cp_error_declarator)
29602 cp_parser_skip_to_end_of_statement (parser);
29604 else
29606 tree pushed_scope, auto_node;
29608 decl = start_decl (declarator, &type_specifiers,
29609 SD_INITIALIZED, attributes,
29610 /*prefix_attributes=*/NULL_TREE,
29611 &pushed_scope);
29613 auto_node = type_uses_auto (TREE_TYPE (decl));
29614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29616 if (cp_lexer_next_token_is (parser->lexer,
29617 CPP_OPEN_PAREN))
29619 if (code != CILK_SIMD && code != CILK_FOR)
29620 error ("parenthesized initialization is not allowed in "
29621 "OpenMP %<for%> loop");
29622 else
29623 error ("parenthesized initialization is "
29624 "not allowed in for-loop");
29626 else
29627 /* Trigger an error. */
29628 cp_parser_require (parser, CPP_EQ, RT_EQ);
29630 init = error_mark_node;
29631 cp_parser_skip_to_end_of_statement (parser);
29633 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29634 || type_dependent_expression_p (decl)
29635 || auto_node)
29637 bool is_direct_init, is_non_constant_init;
29639 init = cp_parser_initializer (parser,
29640 &is_direct_init,
29641 &is_non_constant_init);
29643 if (auto_node)
29645 TREE_TYPE (decl)
29646 = do_auto_deduction (TREE_TYPE (decl), init,
29647 auto_node);
29649 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29650 && !type_dependent_expression_p (decl))
29651 goto non_class;
29654 cp_finish_decl (decl, init, !is_non_constant_init,
29655 asm_specification,
29656 LOOKUP_ONLYCONVERTING);
29657 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29659 vec_safe_push (for_block, this_pre_body);
29660 init = NULL_TREE;
29662 else
29663 init = pop_stmt_list (this_pre_body);
29664 this_pre_body = NULL_TREE;
29666 else
29668 /* Consume '='. */
29669 cp_lexer_consume_token (parser->lexer);
29670 init = cp_parser_assignment_expression (parser);
29672 non_class:
29673 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29674 init = error_mark_node;
29675 else
29676 cp_finish_decl (decl, NULL_TREE,
29677 /*init_const_expr_p=*/false,
29678 asm_specification,
29679 LOOKUP_ONLYCONVERTING);
29682 if (pushed_scope)
29683 pop_scope (pushed_scope);
29686 else
29688 cp_id_kind idk;
29689 /* If parsing a type specifier sequence failed, then
29690 this MUST be a simple expression. */
29691 if (code == CILK_FOR)
29692 error ("%<_Cilk_for%> allows expression instead of declaration only "
29693 "in C, not in C++");
29694 cp_parser_parse_tentatively (parser);
29695 decl = cp_parser_primary_expression (parser, false, false,
29696 false, &idk);
29697 if (!cp_parser_error_occurred (parser)
29698 && decl
29699 && DECL_P (decl)
29700 && CLASS_TYPE_P (TREE_TYPE (decl)))
29702 tree rhs;
29704 cp_parser_parse_definitely (parser);
29705 cp_parser_require (parser, CPP_EQ, RT_EQ);
29706 rhs = cp_parser_assignment_expression (parser);
29707 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29708 decl, NOP_EXPR,
29709 rhs,
29710 tf_warning_or_error));
29711 add_private_clause = true;
29713 else
29715 decl = NULL;
29716 cp_parser_abort_tentative_parse (parser);
29717 init = cp_parser_expression (parser);
29718 if (init)
29720 if (TREE_CODE (init) == MODIFY_EXPR
29721 || TREE_CODE (init) == MODOP_EXPR)
29722 real_decl = TREE_OPERAND (init, 0);
29726 return add_private_clause;
29729 /* Parse the restricted form of the for statement allowed by OpenMP. */
29731 static tree
29732 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29733 tree *cclauses)
29735 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29736 tree real_decl, initv, condv, incrv, declv;
29737 tree this_pre_body, cl;
29738 location_t loc_first;
29739 bool collapse_err = false;
29740 int i, collapse = 1, nbraces = 0;
29741 vec<tree, va_gc> *for_block = make_tree_vector ();
29743 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29744 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29745 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29747 gcc_assert (collapse >= 1);
29749 declv = make_tree_vec (collapse);
29750 initv = make_tree_vec (collapse);
29751 condv = make_tree_vec (collapse);
29752 incrv = make_tree_vec (collapse);
29754 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29756 for (i = 0; i < collapse; i++)
29758 int bracecount = 0;
29759 bool add_private_clause = false;
29760 location_t loc;
29762 if (code != CILK_FOR
29763 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29765 cp_parser_error (parser, "for statement expected");
29766 return NULL;
29768 if (code == CILK_FOR
29769 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29771 cp_parser_error (parser, "_Cilk_for statement expected");
29772 return NULL;
29774 loc = cp_lexer_consume_token (parser->lexer)->location;
29776 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29777 return NULL;
29779 init = decl = real_decl = NULL;
29780 this_pre_body = push_stmt_list ();
29782 add_private_clause
29783 |= cp_parser_omp_for_loop_init (parser, code,
29784 this_pre_body, for_block,
29785 init, decl, real_decl);
29787 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29788 if (this_pre_body)
29790 this_pre_body = pop_stmt_list (this_pre_body);
29791 if (pre_body)
29793 tree t = pre_body;
29794 pre_body = push_stmt_list ();
29795 add_stmt (t);
29796 add_stmt (this_pre_body);
29797 pre_body = pop_stmt_list (pre_body);
29799 else
29800 pre_body = this_pre_body;
29803 if (decl)
29804 real_decl = decl;
29805 if (cclauses != NULL
29806 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29807 && real_decl != NULL_TREE)
29809 tree *c;
29810 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29811 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29812 && OMP_CLAUSE_DECL (*c) == real_decl)
29814 error_at (loc, "iteration variable %qD"
29815 " should not be firstprivate", real_decl);
29816 *c = OMP_CLAUSE_CHAIN (*c);
29818 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29819 && OMP_CLAUSE_DECL (*c) == real_decl)
29821 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29822 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29823 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29824 OMP_CLAUSE_DECL (l) = real_decl;
29825 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29826 if (code == OMP_SIMD)
29828 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29829 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29831 else
29833 OMP_CLAUSE_CHAIN (l) = clauses;
29834 clauses = l;
29836 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29837 CP_OMP_CLAUSE_INFO (*c) = NULL;
29838 add_private_clause = false;
29840 else
29842 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29843 && OMP_CLAUSE_DECL (*c) == real_decl)
29844 add_private_clause = false;
29845 c = &OMP_CLAUSE_CHAIN (*c);
29849 if (add_private_clause)
29851 tree c;
29852 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29854 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29855 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29856 && OMP_CLAUSE_DECL (c) == decl)
29857 break;
29858 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29859 && OMP_CLAUSE_DECL (c) == decl)
29860 error_at (loc, "iteration variable %qD "
29861 "should not be firstprivate",
29862 decl);
29863 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29864 && OMP_CLAUSE_DECL (c) == decl)
29865 error_at (loc, "iteration variable %qD should not be reduction",
29866 decl);
29868 if (c == NULL)
29870 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29871 OMP_CLAUSE_DECL (c) = decl;
29872 c = finish_omp_clauses (c);
29873 if (c)
29875 OMP_CLAUSE_CHAIN (c) = clauses;
29876 clauses = c;
29881 cond = NULL;
29882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29883 cond = cp_parser_omp_for_cond (parser, decl, code);
29884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29886 incr = NULL;
29887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29889 /* If decl is an iterator, preserve the operator on decl
29890 until finish_omp_for. */
29891 if (real_decl
29892 && ((processing_template_decl
29893 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29894 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29895 incr = cp_parser_omp_for_incr (parser, real_decl);
29896 else
29897 incr = cp_parser_expression (parser);
29898 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29899 SET_EXPR_LOCATION (incr, input_location);
29902 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29903 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29904 /*or_comma=*/false,
29905 /*consume_paren=*/true);
29907 TREE_VEC_ELT (declv, i) = decl;
29908 TREE_VEC_ELT (initv, i) = init;
29909 TREE_VEC_ELT (condv, i) = cond;
29910 TREE_VEC_ELT (incrv, i) = incr;
29912 if (i == collapse - 1)
29913 break;
29915 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29916 in between the collapsed for loops to be still considered perfectly
29917 nested. Hopefully the final version clarifies this.
29918 For now handle (multiple) {'s and empty statements. */
29919 cp_parser_parse_tentatively (parser);
29922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29923 break;
29924 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29926 cp_lexer_consume_token (parser->lexer);
29927 bracecount++;
29929 else if (bracecount
29930 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29931 cp_lexer_consume_token (parser->lexer);
29932 else
29934 loc = cp_lexer_peek_token (parser->lexer)->location;
29935 error_at (loc, "not enough collapsed for loops");
29936 collapse_err = true;
29937 cp_parser_abort_tentative_parse (parser);
29938 declv = NULL_TREE;
29939 break;
29942 while (1);
29944 if (declv)
29946 cp_parser_parse_definitely (parser);
29947 nbraces += bracecount;
29951 /* Note that we saved the original contents of this flag when we entered
29952 the structured block, and so we don't need to re-save it here. */
29953 if (code == CILK_SIMD || code == CILK_FOR)
29954 parser->in_statement = IN_CILK_SIMD_FOR;
29955 else
29956 parser->in_statement = IN_OMP_FOR;
29958 /* Note that the grammar doesn't call for a structured block here,
29959 though the loop as a whole is a structured block. */
29960 body = push_stmt_list ();
29961 cp_parser_statement (parser, NULL_TREE, false, NULL);
29962 body = pop_stmt_list (body);
29964 if (declv == NULL_TREE)
29965 ret = NULL_TREE;
29966 else
29967 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29968 pre_body, clauses);
29970 while (nbraces)
29972 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29974 cp_lexer_consume_token (parser->lexer);
29975 nbraces--;
29977 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29978 cp_lexer_consume_token (parser->lexer);
29979 else
29981 if (!collapse_err)
29983 error_at (cp_lexer_peek_token (parser->lexer)->location,
29984 "collapsed loops not perfectly nested");
29986 collapse_err = true;
29987 cp_parser_statement_seq_opt (parser, NULL);
29988 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29989 break;
29993 while (!for_block->is_empty ())
29994 add_stmt (pop_stmt_list (for_block->pop ()));
29995 release_tree_vector (for_block);
29997 return ret;
30000 /* Helper function for OpenMP parsing, split clauses and call
30001 finish_omp_clauses on each of the set of clauses afterwards. */
30003 static void
30004 cp_omp_split_clauses (location_t loc, enum tree_code code,
30005 omp_clause_mask mask, tree clauses, tree *cclauses)
30007 int i;
30008 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30009 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30010 if (cclauses[i])
30011 cclauses[i] = finish_omp_clauses (cclauses[i]);
30014 /* OpenMP 4.0:
30015 #pragma omp simd simd-clause[optseq] new-line
30016 for-loop */
30018 #define OMP_SIMD_CLAUSE_MASK \
30019 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30027 static tree
30028 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30029 char *p_name, omp_clause_mask mask, tree *cclauses)
30031 tree clauses, sb, ret;
30032 unsigned int save;
30033 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30035 strcat (p_name, " simd");
30036 mask |= OMP_SIMD_CLAUSE_MASK;
30037 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30039 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30040 cclauses == NULL);
30041 if (cclauses)
30043 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30044 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30047 sb = begin_omp_structured_block ();
30048 save = cp_parser_begin_omp_structured_block (parser);
30050 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30052 cp_parser_end_omp_structured_block (parser, save);
30053 add_stmt (finish_omp_structured_block (sb));
30055 return ret;
30058 /* OpenMP 2.5:
30059 #pragma omp for for-clause[optseq] new-line
30060 for-loop
30062 OpenMP 4.0:
30063 #pragma omp for simd for-simd-clause[optseq] new-line
30064 for-loop */
30066 #define OMP_FOR_CLAUSE_MASK \
30067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30076 static tree
30077 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30078 char *p_name, omp_clause_mask mask, tree *cclauses)
30080 tree clauses, sb, ret;
30081 unsigned int save;
30082 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30084 strcat (p_name, " for");
30085 mask |= OMP_FOR_CLAUSE_MASK;
30086 if (cclauses)
30087 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30089 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30091 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30092 const char *p = IDENTIFIER_POINTER (id);
30094 if (strcmp (p, "simd") == 0)
30096 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30097 if (cclauses == NULL)
30098 cclauses = cclauses_buf;
30100 cp_lexer_consume_token (parser->lexer);
30101 if (!flag_openmp) /* flag_openmp_simd */
30102 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30103 cclauses);
30104 sb = begin_omp_structured_block ();
30105 save = cp_parser_begin_omp_structured_block (parser);
30106 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30107 cclauses);
30108 cp_parser_end_omp_structured_block (parser, save);
30109 tree body = finish_omp_structured_block (sb);
30110 if (ret == NULL)
30111 return ret;
30112 ret = make_node (OMP_FOR);
30113 TREE_TYPE (ret) = void_type_node;
30114 OMP_FOR_BODY (ret) = body;
30115 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30116 SET_EXPR_LOCATION (ret, loc);
30117 add_stmt (ret);
30118 return ret;
30121 if (!flag_openmp) /* flag_openmp_simd */
30123 cp_parser_require_pragma_eol (parser, pragma_tok);
30124 return NULL_TREE;
30127 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30128 cclauses == NULL);
30129 if (cclauses)
30131 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30132 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30135 sb = begin_omp_structured_block ();
30136 save = cp_parser_begin_omp_structured_block (parser);
30138 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30140 cp_parser_end_omp_structured_block (parser, save);
30141 add_stmt (finish_omp_structured_block (sb));
30143 return ret;
30146 /* OpenMP 2.5:
30147 # pragma omp master new-line
30148 structured-block */
30150 static tree
30151 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30153 cp_parser_require_pragma_eol (parser, pragma_tok);
30154 return c_finish_omp_master (input_location,
30155 cp_parser_omp_structured_block (parser));
30158 /* OpenMP 2.5:
30159 # pragma omp ordered new-line
30160 structured-block */
30162 static tree
30163 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30165 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30166 cp_parser_require_pragma_eol (parser, pragma_tok);
30167 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30170 /* OpenMP 2.5:
30172 section-scope:
30173 { section-sequence }
30175 section-sequence:
30176 section-directive[opt] structured-block
30177 section-sequence section-directive structured-block */
30179 static tree
30180 cp_parser_omp_sections_scope (cp_parser *parser)
30182 tree stmt, substmt;
30183 bool error_suppress = false;
30184 cp_token *tok;
30186 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30187 return NULL_TREE;
30189 stmt = push_stmt_list ();
30191 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30193 substmt = cp_parser_omp_structured_block (parser);
30194 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30195 add_stmt (substmt);
30198 while (1)
30200 tok = cp_lexer_peek_token (parser->lexer);
30201 if (tok->type == CPP_CLOSE_BRACE)
30202 break;
30203 if (tok->type == CPP_EOF)
30204 break;
30206 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30208 cp_lexer_consume_token (parser->lexer);
30209 cp_parser_require_pragma_eol (parser, tok);
30210 error_suppress = false;
30212 else if (!error_suppress)
30214 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30215 error_suppress = true;
30218 substmt = cp_parser_omp_structured_block (parser);
30219 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30220 add_stmt (substmt);
30222 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30224 substmt = pop_stmt_list (stmt);
30226 stmt = make_node (OMP_SECTIONS);
30227 TREE_TYPE (stmt) = void_type_node;
30228 OMP_SECTIONS_BODY (stmt) = substmt;
30230 add_stmt (stmt);
30231 return stmt;
30234 /* OpenMP 2.5:
30235 # pragma omp sections sections-clause[optseq] newline
30236 sections-scope */
30238 #define OMP_SECTIONS_CLAUSE_MASK \
30239 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30245 static tree
30246 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30247 char *p_name, omp_clause_mask mask, tree *cclauses)
30249 tree clauses, ret;
30250 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30252 strcat (p_name, " sections");
30253 mask |= OMP_SECTIONS_CLAUSE_MASK;
30254 if (cclauses)
30255 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30257 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30258 cclauses == NULL);
30259 if (cclauses)
30261 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30262 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30265 ret = cp_parser_omp_sections_scope (parser);
30266 if (ret)
30267 OMP_SECTIONS_CLAUSES (ret) = clauses;
30269 return ret;
30272 /* OpenMP 2.5:
30273 # pragma omp parallel parallel-clause[optseq] new-line
30274 structured-block
30275 # pragma omp parallel for parallel-for-clause[optseq] new-line
30276 structured-block
30277 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30278 structured-block
30280 OpenMP 4.0:
30281 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30282 structured-block */
30284 #define OMP_PARALLEL_CLAUSE_MASK \
30285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30295 static tree
30296 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30297 char *p_name, omp_clause_mask mask, tree *cclauses)
30299 tree stmt, clauses, block;
30300 unsigned int save;
30301 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30303 strcat (p_name, " parallel");
30304 mask |= OMP_PARALLEL_CLAUSE_MASK;
30306 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30308 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30309 if (cclauses == NULL)
30310 cclauses = cclauses_buf;
30312 cp_lexer_consume_token (parser->lexer);
30313 if (!flag_openmp) /* flag_openmp_simd */
30314 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30315 block = begin_omp_parallel ();
30316 save = cp_parser_begin_omp_structured_block (parser);
30317 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30318 cp_parser_end_omp_structured_block (parser, save);
30319 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30320 block);
30321 if (ret == NULL_TREE)
30322 return ret;
30323 OMP_PARALLEL_COMBINED (stmt) = 1;
30324 return stmt;
30326 else if (cclauses)
30328 error_at (loc, "expected %<for%> after %qs", p_name);
30329 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30330 return NULL_TREE;
30332 else if (!flag_openmp) /* flag_openmp_simd */
30334 cp_parser_require_pragma_eol (parser, pragma_tok);
30335 return NULL_TREE;
30337 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30339 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30340 const char *p = IDENTIFIER_POINTER (id);
30341 if (strcmp (p, "sections") == 0)
30343 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30344 cclauses = cclauses_buf;
30346 cp_lexer_consume_token (parser->lexer);
30347 block = begin_omp_parallel ();
30348 save = cp_parser_begin_omp_structured_block (parser);
30349 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30350 cp_parser_end_omp_structured_block (parser, save);
30351 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30352 block);
30353 OMP_PARALLEL_COMBINED (stmt) = 1;
30354 return stmt;
30358 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30360 block = begin_omp_parallel ();
30361 save = cp_parser_begin_omp_structured_block (parser);
30362 cp_parser_statement (parser, NULL_TREE, false, NULL);
30363 cp_parser_end_omp_structured_block (parser, save);
30364 stmt = finish_omp_parallel (clauses, block);
30365 return stmt;
30368 /* OpenMP 2.5:
30369 # pragma omp single single-clause[optseq] new-line
30370 structured-block */
30372 #define OMP_SINGLE_CLAUSE_MASK \
30373 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30378 static tree
30379 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30381 tree stmt = make_node (OMP_SINGLE);
30382 TREE_TYPE (stmt) = void_type_node;
30384 OMP_SINGLE_CLAUSES (stmt)
30385 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30386 "#pragma omp single", pragma_tok);
30387 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30389 return add_stmt (stmt);
30392 /* OpenMP 3.0:
30393 # pragma omp task task-clause[optseq] new-line
30394 structured-block */
30396 #define OMP_TASK_CLAUSE_MASK \
30397 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30407 static tree
30408 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30410 tree clauses, block;
30411 unsigned int save;
30413 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30414 "#pragma omp task", pragma_tok);
30415 block = begin_omp_task ();
30416 save = cp_parser_begin_omp_structured_block (parser);
30417 cp_parser_statement (parser, NULL_TREE, false, NULL);
30418 cp_parser_end_omp_structured_block (parser, save);
30419 return finish_omp_task (clauses, block);
30422 /* OpenMP 3.0:
30423 # pragma omp taskwait new-line */
30425 static void
30426 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30428 cp_parser_require_pragma_eol (parser, pragma_tok);
30429 finish_omp_taskwait ();
30432 /* OpenMP 3.1:
30433 # pragma omp taskyield new-line */
30435 static void
30436 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30438 cp_parser_require_pragma_eol (parser, pragma_tok);
30439 finish_omp_taskyield ();
30442 /* OpenMP 4.0:
30443 # pragma omp taskgroup new-line
30444 structured-block */
30446 static tree
30447 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30449 cp_parser_require_pragma_eol (parser, pragma_tok);
30450 return c_finish_omp_taskgroup (input_location,
30451 cp_parser_omp_structured_block (parser));
30455 /* OpenMP 2.5:
30456 # pragma omp threadprivate (variable-list) */
30458 static void
30459 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30461 tree vars;
30463 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30464 cp_parser_require_pragma_eol (parser, pragma_tok);
30466 finish_omp_threadprivate (vars);
30469 /* OpenMP 4.0:
30470 # pragma omp cancel cancel-clause[optseq] new-line */
30472 #define OMP_CANCEL_CLAUSE_MASK \
30473 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30479 static void
30480 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30482 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30483 "#pragma omp cancel", pragma_tok);
30484 finish_omp_cancel (clauses);
30487 /* OpenMP 4.0:
30488 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30490 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30496 static void
30497 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30499 tree clauses;
30500 bool point_seen = false;
30502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30504 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30505 const char *p = IDENTIFIER_POINTER (id);
30507 if (strcmp (p, "point") == 0)
30509 cp_lexer_consume_token (parser->lexer);
30510 point_seen = true;
30513 if (!point_seen)
30515 cp_parser_error (parser, "expected %<point%>");
30516 cp_parser_require_pragma_eol (parser, pragma_tok);
30517 return;
30520 clauses = cp_parser_omp_all_clauses (parser,
30521 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30522 "#pragma omp cancellation point",
30523 pragma_tok);
30524 finish_omp_cancellation_point (clauses);
30527 /* OpenMP 4.0:
30528 #pragma omp distribute distribute-clause[optseq] new-line
30529 for-loop */
30531 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30532 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30537 static tree
30538 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30539 char *p_name, omp_clause_mask mask, tree *cclauses)
30541 tree clauses, sb, ret;
30542 unsigned int save;
30543 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30545 strcat (p_name, " distribute");
30546 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30551 const char *p = IDENTIFIER_POINTER (id);
30552 bool simd = false;
30553 bool parallel = false;
30555 if (strcmp (p, "simd") == 0)
30556 simd = true;
30557 else
30558 parallel = strcmp (p, "parallel") == 0;
30559 if (parallel || simd)
30561 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30562 if (cclauses == NULL)
30563 cclauses = cclauses_buf;
30564 cp_lexer_consume_token (parser->lexer);
30565 if (!flag_openmp) /* flag_openmp_simd */
30567 if (simd)
30568 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30569 cclauses);
30570 else
30571 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30572 cclauses);
30574 sb = begin_omp_structured_block ();
30575 save = cp_parser_begin_omp_structured_block (parser);
30576 if (simd)
30577 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30578 cclauses);
30579 else
30580 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30581 cclauses);
30582 cp_parser_end_omp_structured_block (parser, save);
30583 tree body = finish_omp_structured_block (sb);
30584 if (ret == NULL)
30585 return ret;
30586 ret = make_node (OMP_DISTRIBUTE);
30587 TREE_TYPE (ret) = void_type_node;
30588 OMP_FOR_BODY (ret) = body;
30589 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30590 SET_EXPR_LOCATION (ret, loc);
30591 add_stmt (ret);
30592 return ret;
30595 if (!flag_openmp) /* flag_openmp_simd */
30597 cp_parser_require_pragma_eol (parser, pragma_tok);
30598 return NULL_TREE;
30601 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30602 cclauses == NULL);
30603 if (cclauses)
30605 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30606 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30609 sb = begin_omp_structured_block ();
30610 save = cp_parser_begin_omp_structured_block (parser);
30612 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30614 cp_parser_end_omp_structured_block (parser, save);
30615 add_stmt (finish_omp_structured_block (sb));
30617 return ret;
30620 /* OpenMP 4.0:
30621 # pragma omp teams teams-clause[optseq] new-line
30622 structured-block */
30624 #define OMP_TEAMS_CLAUSE_MASK \
30625 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30633 static tree
30634 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30635 char *p_name, omp_clause_mask mask, tree *cclauses)
30637 tree clauses, sb, ret;
30638 unsigned int save;
30639 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30641 strcat (p_name, " teams");
30642 mask |= OMP_TEAMS_CLAUSE_MASK;
30644 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30646 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30647 const char *p = IDENTIFIER_POINTER (id);
30648 if (strcmp (p, "distribute") == 0)
30650 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30651 if (cclauses == NULL)
30652 cclauses = cclauses_buf;
30654 cp_lexer_consume_token (parser->lexer);
30655 if (!flag_openmp) /* flag_openmp_simd */
30656 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30657 cclauses);
30658 sb = begin_omp_structured_block ();
30659 save = cp_parser_begin_omp_structured_block (parser);
30660 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30661 cclauses);
30662 cp_parser_end_omp_structured_block (parser, save);
30663 tree body = finish_omp_structured_block (sb);
30664 if (ret == NULL)
30665 return ret;
30666 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30667 ret = make_node (OMP_TEAMS);
30668 TREE_TYPE (ret) = void_type_node;
30669 OMP_TEAMS_CLAUSES (ret) = clauses;
30670 OMP_TEAMS_BODY (ret) = body;
30671 return add_stmt (ret);
30674 if (!flag_openmp) /* flag_openmp_simd */
30676 cp_parser_require_pragma_eol (parser, pragma_tok);
30677 return NULL_TREE;
30680 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30681 cclauses == NULL);
30682 if (cclauses)
30684 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30685 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30688 tree stmt = make_node (OMP_TEAMS);
30689 TREE_TYPE (stmt) = void_type_node;
30690 OMP_TEAMS_CLAUSES (stmt) = clauses;
30691 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30693 return add_stmt (stmt);
30696 /* OpenMP 4.0:
30697 # pragma omp target data target-data-clause[optseq] new-line
30698 structured-block */
30700 #define OMP_TARGET_DATA_CLAUSE_MASK \
30701 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30705 static tree
30706 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30708 tree stmt = make_node (OMP_TARGET_DATA);
30709 TREE_TYPE (stmt) = void_type_node;
30711 OMP_TARGET_DATA_CLAUSES (stmt)
30712 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30713 "#pragma omp target data", pragma_tok);
30714 keep_next_level (true);
30715 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30717 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30718 return add_stmt (stmt);
30721 /* OpenMP 4.0:
30722 # pragma omp target update target-update-clause[optseq] new-line */
30724 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30730 static bool
30731 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30732 enum pragma_context context)
30734 if (context == pragma_stmt)
30736 error_at (pragma_tok->location,
30737 "%<#pragma omp target update%> may only be "
30738 "used in compound statements");
30739 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30740 return false;
30743 tree clauses
30744 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30745 "#pragma omp target update", pragma_tok);
30746 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30747 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30749 error_at (pragma_tok->location,
30750 "%<#pragma omp target update must contain at least one "
30751 "%<from%> or %<to%> clauses");
30752 return false;
30755 tree stmt = make_node (OMP_TARGET_UPDATE);
30756 TREE_TYPE (stmt) = void_type_node;
30757 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30758 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30759 add_stmt (stmt);
30760 return false;
30763 /* OpenMP 4.0:
30764 # pragma omp target target-clause[optseq] new-line
30765 structured-block */
30767 #define OMP_TARGET_CLAUSE_MASK \
30768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30772 static bool
30773 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30774 enum pragma_context context)
30776 if (context != pragma_stmt && context != pragma_compound)
30778 cp_parser_error (parser, "expected declaration specifiers");
30779 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30780 return false;
30783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30785 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30786 const char *p = IDENTIFIER_POINTER (id);
30788 if (strcmp (p, "teams") == 0)
30790 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30791 char p_name[sizeof ("#pragma omp target teams distribute "
30792 "parallel for simd")];
30794 cp_lexer_consume_token (parser->lexer);
30795 strcpy (p_name, "#pragma omp target");
30796 if (!flag_openmp) /* flag_openmp_simd */
30798 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30799 OMP_TARGET_CLAUSE_MASK,
30800 cclauses);
30801 return stmt != NULL_TREE;
30803 keep_next_level (true);
30804 tree sb = begin_omp_structured_block ();
30805 unsigned save = cp_parser_begin_omp_structured_block (parser);
30806 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30807 OMP_TARGET_CLAUSE_MASK, cclauses);
30808 cp_parser_end_omp_structured_block (parser, save);
30809 tree body = finish_omp_structured_block (sb);
30810 if (ret == NULL_TREE)
30811 return false;
30812 tree stmt = make_node (OMP_TARGET);
30813 TREE_TYPE (stmt) = void_type_node;
30814 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30815 OMP_TARGET_BODY (stmt) = body;
30816 add_stmt (stmt);
30817 return true;
30819 else if (!flag_openmp) /* flag_openmp_simd */
30821 cp_parser_require_pragma_eol (parser, pragma_tok);
30822 return false;
30824 else if (strcmp (p, "data") == 0)
30826 cp_lexer_consume_token (parser->lexer);
30827 cp_parser_omp_target_data (parser, pragma_tok);
30828 return true;
30830 else if (strcmp (p, "update") == 0)
30832 cp_lexer_consume_token (parser->lexer);
30833 return cp_parser_omp_target_update (parser, pragma_tok, context);
30837 tree stmt = make_node (OMP_TARGET);
30838 TREE_TYPE (stmt) = void_type_node;
30840 OMP_TARGET_CLAUSES (stmt)
30841 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30842 "#pragma omp target", pragma_tok);
30843 keep_next_level (true);
30844 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30846 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30847 add_stmt (stmt);
30848 return true;
30851 /* OpenMP 4.0:
30852 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30854 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30855 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30862 static void
30863 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30864 enum pragma_context context)
30866 bool first_p = parser->omp_declare_simd == NULL;
30867 cp_omp_declare_simd_data data;
30868 if (first_p)
30870 data.error_seen = false;
30871 data.fndecl_seen = false;
30872 data.tokens = vNULL;
30873 parser->omp_declare_simd = &data;
30875 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30876 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30877 cp_lexer_consume_token (parser->lexer);
30878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30879 parser->omp_declare_simd->error_seen = true;
30880 cp_parser_require_pragma_eol (parser, pragma_tok);
30881 struct cp_token_cache *cp
30882 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30883 parser->omp_declare_simd->tokens.safe_push (cp);
30884 if (first_p)
30886 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30887 cp_parser_pragma (parser, context);
30888 switch (context)
30890 case pragma_external:
30891 cp_parser_declaration (parser);
30892 break;
30893 case pragma_member:
30894 cp_parser_member_declaration (parser);
30895 break;
30896 case pragma_objc_icode:
30897 cp_parser_block_declaration (parser, /*statement_p=*/false);
30898 break;
30899 default:
30900 cp_parser_declaration_statement (parser);
30901 break;
30903 if (parser->omp_declare_simd
30904 && !parser->omp_declare_simd->error_seen
30905 && !parser->omp_declare_simd->fndecl_seen)
30906 error_at (pragma_tok->location,
30907 "%<#pragma omp declare simd%> not immediately followed by "
30908 "function declaration or definition");
30909 data.tokens.release ();
30910 parser->omp_declare_simd = NULL;
30914 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30915 This function is modelled similar to the late parsing of omp declare
30916 simd. */
30918 static tree
30919 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30921 struct cp_token_cache *ce;
30922 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30923 int ii = 0;
30925 if (parser->omp_declare_simd != NULL)
30927 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30928 " marked as a Cilk Plus SIMD-enabled function");
30929 XDELETE (parser->cilk_simd_fn_info);
30930 parser->cilk_simd_fn_info = NULL;
30931 return attrs;
30933 if (!info->error_seen && info->fndecl_seen)
30935 error ("vector attribute not immediately followed by a single function"
30936 " declaration or definition");
30937 info->error_seen = true;
30939 if (info->error_seen)
30940 return attrs;
30942 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30944 tree c, cl;
30946 cp_parser_push_lexer_for_tokens (parser, ce);
30947 parser->lexer->in_pragma = true;
30948 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30949 "SIMD-enabled functions attribute",
30950 NULL);
30951 cp_parser_pop_lexer (parser);
30952 if (cl)
30953 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30955 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30956 TREE_CHAIN (c) = attrs;
30957 attrs = c;
30959 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30960 TREE_CHAIN (c) = attrs;
30961 if (processing_template_decl)
30962 ATTR_IS_DEPENDENT (c) = 1;
30963 attrs = c;
30965 info->fndecl_seen = true;
30966 XDELETE (parser->cilk_simd_fn_info);
30967 parser->cilk_simd_fn_info = NULL;
30968 return attrs;
30971 /* Finalize #pragma omp declare simd clauses after direct declarator has
30972 been parsed, and put that into "omp declare simd" attribute. */
30974 static tree
30975 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30977 struct cp_token_cache *ce;
30978 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30979 int i;
30981 if (!data->error_seen && data->fndecl_seen)
30983 error ("%<#pragma omp declare simd%> not immediately followed by "
30984 "a single function declaration or definition");
30985 data->error_seen = true;
30986 return attrs;
30988 if (data->error_seen)
30989 return attrs;
30991 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30993 tree c, cl;
30995 cp_parser_push_lexer_for_tokens (parser, ce);
30996 parser->lexer->in_pragma = true;
30997 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30998 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30999 cp_lexer_consume_token (parser->lexer);
31000 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31001 "#pragma omp declare simd", pragma_tok);
31002 cp_parser_pop_lexer (parser);
31003 if (cl)
31004 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31005 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31006 TREE_CHAIN (c) = attrs;
31007 if (processing_template_decl)
31008 ATTR_IS_DEPENDENT (c) = 1;
31009 attrs = c;
31012 data->fndecl_seen = true;
31013 return attrs;
31017 /* OpenMP 4.0:
31018 # pragma omp declare target new-line
31019 declarations and definitions
31020 # pragma omp end declare target new-line */
31022 static void
31023 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31025 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31026 scope_chain->omp_declare_target_attribute++;
31029 static void
31030 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31032 const char *p = "";
31033 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31035 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31036 p = IDENTIFIER_POINTER (id);
31038 if (strcmp (p, "declare") == 0)
31040 cp_lexer_consume_token (parser->lexer);
31041 p = "";
31042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31044 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31045 p = IDENTIFIER_POINTER (id);
31047 if (strcmp (p, "target") == 0)
31048 cp_lexer_consume_token (parser->lexer);
31049 else
31051 cp_parser_error (parser, "expected %<target%>");
31052 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31053 return;
31056 else
31058 cp_parser_error (parser, "expected %<declare%>");
31059 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31060 return;
31062 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31063 if (!scope_chain->omp_declare_target_attribute)
31064 error_at (pragma_tok->location,
31065 "%<#pragma omp end declare target%> without corresponding "
31066 "%<#pragma omp declare target%>");
31067 else
31068 scope_chain->omp_declare_target_attribute--;
31071 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31072 expression and optional initializer clause of
31073 #pragma omp declare reduction. We store the expression(s) as
31074 either 3, 6 or 7 special statements inside of the artificial function's
31075 body. The first two statements are DECL_EXPRs for the artificial
31076 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31077 expression that uses those variables.
31078 If there was any INITIALIZER clause, this is followed by further statements,
31079 the fourth and fifth statements are DECL_EXPRs for the artificial
31080 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31081 constructor variant (first token after open paren is not omp_priv),
31082 then the sixth statement is a statement with the function call expression
31083 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31084 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31085 to initialize the OMP_PRIV artificial variable and there is seventh
31086 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31088 static bool
31089 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31091 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31092 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31093 type = TREE_TYPE (type);
31094 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31095 DECL_ARTIFICIAL (omp_out) = 1;
31096 pushdecl (omp_out);
31097 add_decl_expr (omp_out);
31098 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31099 DECL_ARTIFICIAL (omp_in) = 1;
31100 pushdecl (omp_in);
31101 add_decl_expr (omp_in);
31102 tree combiner;
31103 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31105 keep_next_level (true);
31106 tree block = begin_omp_structured_block ();
31107 combiner = cp_parser_expression (parser);
31108 finish_expr_stmt (combiner);
31109 block = finish_omp_structured_block (block);
31110 add_stmt (block);
31112 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31113 return false;
31115 const char *p = "";
31116 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31118 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31119 p = IDENTIFIER_POINTER (id);
31122 if (strcmp (p, "initializer") == 0)
31124 cp_lexer_consume_token (parser->lexer);
31125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31126 return false;
31128 p = "";
31129 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31131 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31132 p = IDENTIFIER_POINTER (id);
31135 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31136 DECL_ARTIFICIAL (omp_priv) = 1;
31137 pushdecl (omp_priv);
31138 add_decl_expr (omp_priv);
31139 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31140 DECL_ARTIFICIAL (omp_orig) = 1;
31141 pushdecl (omp_orig);
31142 add_decl_expr (omp_orig);
31144 keep_next_level (true);
31145 block = begin_omp_structured_block ();
31147 bool ctor = false;
31148 if (strcmp (p, "omp_priv") == 0)
31150 bool is_direct_init, is_non_constant_init;
31151 ctor = true;
31152 cp_lexer_consume_token (parser->lexer);
31153 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31154 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31155 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31156 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31157 == CPP_CLOSE_PAREN
31158 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31159 == CPP_CLOSE_PAREN))
31161 finish_omp_structured_block (block);
31162 error ("invalid initializer clause");
31163 return false;
31165 initializer = cp_parser_initializer (parser, &is_direct_init,
31166 &is_non_constant_init);
31167 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31168 NULL_TREE, LOOKUP_ONLYCONVERTING);
31170 else
31172 cp_parser_parse_tentatively (parser);
31173 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31174 /*check_dependency_p=*/true,
31175 /*template_p=*/NULL,
31176 /*declarator_p=*/false,
31177 /*optional_p=*/false);
31178 vec<tree, va_gc> *args;
31179 if (fn_name == error_mark_node
31180 || cp_parser_error_occurred (parser)
31181 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31182 || ((args = cp_parser_parenthesized_expression_list
31183 (parser, non_attr, /*cast_p=*/false,
31184 /*allow_expansion_p=*/true,
31185 /*non_constant_p=*/NULL)),
31186 cp_parser_error_occurred (parser)))
31188 finish_omp_structured_block (block);
31189 cp_parser_abort_tentative_parse (parser);
31190 cp_parser_error (parser, "expected id-expression (arguments)");
31191 return false;
31193 unsigned int i;
31194 tree arg;
31195 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31196 if (arg == omp_priv
31197 || (TREE_CODE (arg) == ADDR_EXPR
31198 && TREE_OPERAND (arg, 0) == omp_priv))
31199 break;
31200 cp_parser_abort_tentative_parse (parser);
31201 if (arg == NULL_TREE)
31202 error ("one of the initializer call arguments should be %<omp_priv%>"
31203 " or %<&omp_priv%>");
31204 initializer = cp_parser_postfix_expression (parser, false, false, false,
31205 false, NULL);
31206 finish_expr_stmt (initializer);
31209 block = finish_omp_structured_block (block);
31210 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31211 add_stmt (block);
31213 if (ctor)
31214 add_decl_expr (omp_orig);
31216 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31217 return false;
31220 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31221 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31223 return true;
31226 /* OpenMP 4.0
31227 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31228 initializer-clause[opt] new-line
31230 initializer-clause:
31231 initializer (omp_priv initializer)
31232 initializer (function-name (argument-list)) */
31234 static void
31235 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31236 enum pragma_context)
31238 auto_vec<tree> types;
31239 enum tree_code reduc_code = ERROR_MARK;
31240 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31241 unsigned int i;
31242 cp_token *first_token;
31243 cp_token_cache *cp;
31244 int errs;
31245 void *p;
31247 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31248 p = obstack_alloc (&declarator_obstack, 0);
31250 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31251 goto fail;
31253 switch (cp_lexer_peek_token (parser->lexer)->type)
31255 case CPP_PLUS:
31256 reduc_code = PLUS_EXPR;
31257 break;
31258 case CPP_MULT:
31259 reduc_code = MULT_EXPR;
31260 break;
31261 case CPP_MINUS:
31262 reduc_code = MINUS_EXPR;
31263 break;
31264 case CPP_AND:
31265 reduc_code = BIT_AND_EXPR;
31266 break;
31267 case CPP_XOR:
31268 reduc_code = BIT_XOR_EXPR;
31269 break;
31270 case CPP_OR:
31271 reduc_code = BIT_IOR_EXPR;
31272 break;
31273 case CPP_AND_AND:
31274 reduc_code = TRUTH_ANDIF_EXPR;
31275 break;
31276 case CPP_OR_OR:
31277 reduc_code = TRUTH_ORIF_EXPR;
31278 break;
31279 case CPP_NAME:
31280 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31281 break;
31282 default:
31283 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31284 "%<|%>, %<&&%>, %<||%> or identifier");
31285 goto fail;
31288 if (reduc_code != ERROR_MARK)
31289 cp_lexer_consume_token (parser->lexer);
31291 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31292 if (reduc_id == error_mark_node)
31293 goto fail;
31295 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31296 goto fail;
31298 /* Types may not be defined in declare reduction type list. */
31299 const char *saved_message;
31300 saved_message = parser->type_definition_forbidden_message;
31301 parser->type_definition_forbidden_message
31302 = G_("types may not be defined in declare reduction type list");
31303 bool saved_colon_corrects_to_scope_p;
31304 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31305 parser->colon_corrects_to_scope_p = false;
31306 bool saved_colon_doesnt_start_class_def_p;
31307 saved_colon_doesnt_start_class_def_p
31308 = parser->colon_doesnt_start_class_def_p;
31309 parser->colon_doesnt_start_class_def_p = true;
31311 while (true)
31313 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31314 type = cp_parser_type_id (parser);
31315 if (type == error_mark_node)
31317 else if (ARITHMETIC_TYPE_P (type)
31318 && (orig_reduc_id == NULL_TREE
31319 || (TREE_CODE (type) != COMPLEX_TYPE
31320 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31321 "min") == 0
31322 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31323 "max") == 0))))
31324 error_at (loc, "predeclared arithmetic type %qT in "
31325 "%<#pragma omp declare reduction%>", type);
31326 else if (TREE_CODE (type) == FUNCTION_TYPE
31327 || TREE_CODE (type) == METHOD_TYPE
31328 || TREE_CODE (type) == ARRAY_TYPE)
31329 error_at (loc, "function or array type %qT in "
31330 "%<#pragma omp declare reduction%>", type);
31331 else if (TREE_CODE (type) == REFERENCE_TYPE)
31332 error_at (loc, "reference type %qT in "
31333 "%<#pragma omp declare reduction%>", type);
31334 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31335 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31336 "%<#pragma omp declare reduction%>", type);
31337 else
31338 types.safe_push (type);
31340 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31341 cp_lexer_consume_token (parser->lexer);
31342 else
31343 break;
31346 /* Restore the saved message. */
31347 parser->type_definition_forbidden_message = saved_message;
31348 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31349 parser->colon_doesnt_start_class_def_p
31350 = saved_colon_doesnt_start_class_def_p;
31352 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31353 || types.is_empty ())
31355 fail:
31356 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31357 goto done;
31360 first_token = cp_lexer_peek_token (parser->lexer);
31361 cp = NULL;
31362 errs = errorcount;
31363 FOR_EACH_VEC_ELT (types, i, type)
31365 tree fntype
31366 = build_function_type_list (void_type_node,
31367 cp_build_reference_type (type, false),
31368 NULL_TREE);
31369 tree this_reduc_id = reduc_id;
31370 if (!dependent_type_p (type))
31371 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31372 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31373 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31374 DECL_ARTIFICIAL (fndecl) = 1;
31375 DECL_EXTERNAL (fndecl) = 1;
31376 DECL_DECLARED_INLINE_P (fndecl) = 1;
31377 DECL_IGNORED_P (fndecl) = 1;
31378 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31379 DECL_ATTRIBUTES (fndecl)
31380 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31381 DECL_ATTRIBUTES (fndecl));
31382 if (processing_template_decl)
31383 fndecl = push_template_decl (fndecl);
31384 bool block_scope = false;
31385 tree block = NULL_TREE;
31386 if (current_function_decl)
31388 block_scope = true;
31389 DECL_CONTEXT (fndecl) = global_namespace;
31390 if (!processing_template_decl)
31391 pushdecl (fndecl);
31393 else if (current_class_type)
31395 if (cp == NULL)
31397 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31398 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31399 cp_lexer_consume_token (parser->lexer);
31400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31401 goto fail;
31402 cp = cp_token_cache_new (first_token,
31403 cp_lexer_peek_nth_token (parser->lexer,
31404 2));
31406 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31407 finish_member_declaration (fndecl);
31408 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31409 DECL_PENDING_INLINE_P (fndecl) = 1;
31410 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31411 continue;
31413 else
31415 DECL_CONTEXT (fndecl) = current_namespace;
31416 pushdecl (fndecl);
31418 if (!block_scope)
31419 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31420 else
31421 block = begin_omp_structured_block ();
31422 if (cp)
31424 cp_parser_push_lexer_for_tokens (parser, cp);
31425 parser->lexer->in_pragma = true;
31427 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31429 if (!block_scope)
31430 finish_function (0);
31431 else
31432 DECL_CONTEXT (fndecl) = current_function_decl;
31433 if (cp)
31434 cp_parser_pop_lexer (parser);
31435 goto fail;
31437 if (cp)
31438 cp_parser_pop_lexer (parser);
31439 if (!block_scope)
31440 finish_function (0);
31441 else
31443 DECL_CONTEXT (fndecl) = current_function_decl;
31444 block = finish_omp_structured_block (block);
31445 if (TREE_CODE (block) == BIND_EXPR)
31446 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31447 else if (TREE_CODE (block) == STATEMENT_LIST)
31448 DECL_SAVED_TREE (fndecl) = block;
31449 if (processing_template_decl)
31450 add_decl_expr (fndecl);
31452 cp_check_omp_declare_reduction (fndecl);
31453 if (cp == NULL && types.length () > 1)
31454 cp = cp_token_cache_new (first_token,
31455 cp_lexer_peek_nth_token (parser->lexer, 2));
31456 if (errs != errorcount)
31457 break;
31460 cp_parser_require_pragma_eol (parser, pragma_tok);
31462 done:
31463 /* Free any declarators allocated. */
31464 obstack_free (&declarator_obstack, p);
31467 /* OpenMP 4.0
31468 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31469 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31470 initializer-clause[opt] new-line
31471 #pragma omp declare target new-line */
31473 static void
31474 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31475 enum pragma_context context)
31477 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31479 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31480 const char *p = IDENTIFIER_POINTER (id);
31482 if (strcmp (p, "simd") == 0)
31484 cp_lexer_consume_token (parser->lexer);
31485 cp_parser_omp_declare_simd (parser, pragma_tok,
31486 context);
31487 return;
31489 cp_ensure_no_omp_declare_simd (parser);
31490 if (strcmp (p, "reduction") == 0)
31492 cp_lexer_consume_token (parser->lexer);
31493 cp_parser_omp_declare_reduction (parser, pragma_tok,
31494 context);
31495 return;
31497 if (!flag_openmp) /* flag_openmp_simd */
31499 cp_parser_require_pragma_eol (parser, pragma_tok);
31500 return;
31502 if (strcmp (p, "target") == 0)
31504 cp_lexer_consume_token (parser->lexer);
31505 cp_parser_omp_declare_target (parser, pragma_tok);
31506 return;
31509 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31510 "or %<target%>");
31511 cp_parser_require_pragma_eol (parser, pragma_tok);
31514 /* Main entry point to OpenMP statement pragmas. */
31516 static void
31517 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31519 tree stmt;
31520 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31521 omp_clause_mask mask (0);
31523 switch (pragma_tok->pragma_kind)
31525 case PRAGMA_OMP_ATOMIC:
31526 cp_parser_omp_atomic (parser, pragma_tok);
31527 return;
31528 case PRAGMA_OMP_CRITICAL:
31529 stmt = cp_parser_omp_critical (parser, pragma_tok);
31530 break;
31531 case PRAGMA_OMP_DISTRIBUTE:
31532 strcpy (p_name, "#pragma omp");
31533 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31534 break;
31535 case PRAGMA_OMP_FOR:
31536 strcpy (p_name, "#pragma omp");
31537 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31538 break;
31539 case PRAGMA_OMP_MASTER:
31540 stmt = cp_parser_omp_master (parser, pragma_tok);
31541 break;
31542 case PRAGMA_OMP_ORDERED:
31543 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31544 break;
31545 case PRAGMA_OMP_PARALLEL:
31546 strcpy (p_name, "#pragma omp");
31547 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31548 break;
31549 case PRAGMA_OMP_SECTIONS:
31550 strcpy (p_name, "#pragma omp");
31551 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31552 break;
31553 case PRAGMA_OMP_SIMD:
31554 strcpy (p_name, "#pragma omp");
31555 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31556 break;
31557 case PRAGMA_OMP_SINGLE:
31558 stmt = cp_parser_omp_single (parser, pragma_tok);
31559 break;
31560 case PRAGMA_OMP_TASK:
31561 stmt = cp_parser_omp_task (parser, pragma_tok);
31562 break;
31563 case PRAGMA_OMP_TASKGROUP:
31564 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31565 break;
31566 case PRAGMA_OMP_TEAMS:
31567 strcpy (p_name, "#pragma omp");
31568 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31569 break;
31570 default:
31571 gcc_unreachable ();
31574 if (stmt)
31575 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31578 /* Transactional Memory parsing routines. */
31580 /* Parse a transaction attribute.
31582 txn-attribute:
31583 attribute
31584 [ [ identifier ] ]
31586 ??? Simplify this when C++0x bracket attributes are
31587 implemented properly. */
31589 static tree
31590 cp_parser_txn_attribute_opt (cp_parser *parser)
31592 cp_token *token;
31593 tree attr_name, attr = NULL;
31595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31596 return cp_parser_attributes_opt (parser);
31598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31599 return NULL_TREE;
31600 cp_lexer_consume_token (parser->lexer);
31601 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31602 goto error1;
31604 token = cp_lexer_peek_token (parser->lexer);
31605 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31607 token = cp_lexer_consume_token (parser->lexer);
31609 attr_name = (token->type == CPP_KEYWORD
31610 /* For keywords, use the canonical spelling,
31611 not the parsed identifier. */
31612 ? ridpointers[(int) token->keyword]
31613 : token->u.value);
31614 attr = build_tree_list (attr_name, NULL_TREE);
31616 else
31617 cp_parser_error (parser, "expected identifier");
31619 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31620 error1:
31621 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31622 return attr;
31625 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31627 transaction-statement:
31628 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31629 compound-statement
31630 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31633 static tree
31634 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31636 unsigned char old_in = parser->in_transaction;
31637 unsigned char this_in = 1, new_in;
31638 cp_token *token;
31639 tree stmt, attrs, noex;
31641 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31642 || keyword == RID_TRANSACTION_RELAXED);
31643 token = cp_parser_require_keyword (parser, keyword,
31644 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31645 : RT_TRANSACTION_RELAXED));
31646 gcc_assert (token != NULL);
31648 if (keyword == RID_TRANSACTION_RELAXED)
31649 this_in |= TM_STMT_ATTR_RELAXED;
31650 else
31652 attrs = cp_parser_txn_attribute_opt (parser);
31653 if (attrs)
31654 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31657 /* Parse a noexcept specification. */
31658 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31660 /* Keep track if we're in the lexical scope of an outer transaction. */
31661 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31663 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31665 parser->in_transaction = new_in;
31666 cp_parser_compound_statement (parser, NULL, false, false);
31667 parser->in_transaction = old_in;
31669 finish_transaction_stmt (stmt, NULL, this_in, noex);
31671 return stmt;
31674 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31676 transaction-expression:
31677 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31678 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31681 static tree
31682 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31684 unsigned char old_in = parser->in_transaction;
31685 unsigned char this_in = 1;
31686 cp_token *token;
31687 tree expr, noex;
31688 bool noex_expr;
31690 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31691 || keyword == RID_TRANSACTION_RELAXED);
31693 if (!flag_tm)
31694 error (keyword == RID_TRANSACTION_RELAXED
31695 ? G_("%<__transaction_relaxed%> without transactional memory "
31696 "support enabled")
31697 : G_("%<__transaction_atomic%> without transactional memory "
31698 "support enabled"));
31700 token = cp_parser_require_keyword (parser, keyword,
31701 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31702 : RT_TRANSACTION_RELAXED));
31703 gcc_assert (token != NULL);
31705 if (keyword == RID_TRANSACTION_RELAXED)
31706 this_in |= TM_STMT_ATTR_RELAXED;
31708 /* Set this early. This might mean that we allow transaction_cancel in
31709 an expression that we find out later actually has to be a constexpr.
31710 However, we expect that cxx_constant_value will be able to deal with
31711 this; also, if the noexcept has no constexpr, then what we parse next
31712 really is a transaction's body. */
31713 parser->in_transaction = this_in;
31715 /* Parse a noexcept specification. */
31716 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31717 true);
31719 if (!noex || !noex_expr
31720 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31722 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31724 expr = cp_parser_expression (parser);
31725 expr = finish_parenthesized_expr (expr);
31727 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31729 else
31731 /* The only expression that is available got parsed for the noexcept
31732 already. noexcept is true then. */
31733 expr = noex;
31734 noex = boolean_true_node;
31737 expr = build_transaction_expr (token->location, expr, this_in, noex);
31738 parser->in_transaction = old_in;
31740 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31741 return error_mark_node;
31743 return (flag_tm ? expr : error_mark_node);
31746 /* Parse a function-transaction-block.
31748 function-transaction-block:
31749 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31750 function-body
31751 __transaction_atomic txn-attribute[opt] function-try-block
31752 __transaction_relaxed ctor-initializer[opt] function-body
31753 __transaction_relaxed function-try-block
31756 static bool
31757 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31759 unsigned char old_in = parser->in_transaction;
31760 unsigned char new_in = 1;
31761 tree compound_stmt, stmt, attrs;
31762 bool ctor_initializer_p;
31763 cp_token *token;
31765 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31766 || keyword == RID_TRANSACTION_RELAXED);
31767 token = cp_parser_require_keyword (parser, keyword,
31768 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31769 : RT_TRANSACTION_RELAXED));
31770 gcc_assert (token != NULL);
31772 if (keyword == RID_TRANSACTION_RELAXED)
31773 new_in |= TM_STMT_ATTR_RELAXED;
31774 else
31776 attrs = cp_parser_txn_attribute_opt (parser);
31777 if (attrs)
31778 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31781 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31783 parser->in_transaction = new_in;
31785 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31786 ctor_initializer_p = cp_parser_function_try_block (parser);
31787 else
31788 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31789 (parser, /*in_function_try_block=*/false);
31791 parser->in_transaction = old_in;
31793 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31795 return ctor_initializer_p;
31798 /* Parse a __transaction_cancel statement.
31800 cancel-statement:
31801 __transaction_cancel txn-attribute[opt] ;
31802 __transaction_cancel txn-attribute[opt] throw-expression ;
31804 ??? Cancel and throw is not yet implemented. */
31806 static tree
31807 cp_parser_transaction_cancel (cp_parser *parser)
31809 cp_token *token;
31810 bool is_outer = false;
31811 tree stmt, attrs;
31813 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31814 RT_TRANSACTION_CANCEL);
31815 gcc_assert (token != NULL);
31817 attrs = cp_parser_txn_attribute_opt (parser);
31818 if (attrs)
31819 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31821 /* ??? Parse cancel-and-throw here. */
31823 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31825 if (!flag_tm)
31827 error_at (token->location, "%<__transaction_cancel%> without "
31828 "transactional memory support enabled");
31829 return error_mark_node;
31831 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31833 error_at (token->location, "%<__transaction_cancel%> within a "
31834 "%<__transaction_relaxed%>");
31835 return error_mark_node;
31837 else if (is_outer)
31839 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31840 && !is_tm_may_cancel_outer (current_function_decl))
31842 error_at (token->location, "outer %<__transaction_cancel%> not "
31843 "within outer %<__transaction_atomic%>");
31844 error_at (token->location,
31845 " or a %<transaction_may_cancel_outer%> function");
31846 return error_mark_node;
31849 else if (parser->in_transaction == 0)
31851 error_at (token->location, "%<__transaction_cancel%> not within "
31852 "%<__transaction_atomic%>");
31853 return error_mark_node;
31856 stmt = build_tm_abort_call (token->location, is_outer);
31857 add_stmt (stmt);
31859 return stmt;
31862 /* The parser. */
31864 static GTY (()) cp_parser *the_parser;
31867 /* Special handling for the first token or line in the file. The first
31868 thing in the file might be #pragma GCC pch_preprocess, which loads a
31869 PCH file, which is a GC collection point. So we need to handle this
31870 first pragma without benefit of an existing lexer structure.
31872 Always returns one token to the caller in *FIRST_TOKEN. This is
31873 either the true first token of the file, or the first token after
31874 the initial pragma. */
31876 static void
31877 cp_parser_initial_pragma (cp_token *first_token)
31879 tree name = NULL;
31881 cp_lexer_get_preprocessor_token (NULL, first_token);
31882 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31883 return;
31885 cp_lexer_get_preprocessor_token (NULL, first_token);
31886 if (first_token->type == CPP_STRING)
31888 name = first_token->u.value;
31890 cp_lexer_get_preprocessor_token (NULL, first_token);
31891 if (first_token->type != CPP_PRAGMA_EOL)
31892 error_at (first_token->location,
31893 "junk at end of %<#pragma GCC pch_preprocess%>");
31895 else
31896 error_at (first_token->location, "expected string literal");
31898 /* Skip to the end of the pragma. */
31899 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31900 cp_lexer_get_preprocessor_token (NULL, first_token);
31902 /* Now actually load the PCH file. */
31903 if (name)
31904 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31906 /* Read one more token to return to our caller. We have to do this
31907 after reading the PCH file in, since its pointers have to be
31908 live. */
31909 cp_lexer_get_preprocessor_token (NULL, first_token);
31912 /* Parses the grainsize pragma for the _Cilk_for statement.
31913 Syntax:
31914 #pragma cilk grainsize = <VALUE>. */
31916 static void
31917 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31919 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31921 tree exp = cp_parser_binary_expression (parser, false, false,
31922 PREC_NOT_OPERATOR, NULL);
31923 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31924 if (!exp || exp == error_mark_node)
31926 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31927 return;
31930 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31932 cp_parser_cilk_for (parser, exp);
31933 else
31934 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31935 "%<#pragma cilk grainsize%> is not followed by "
31936 "%<_Cilk_for%>");
31937 return;
31939 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31942 /* Normal parsing of a pragma token. Here we can (and must) use the
31943 regular lexer. */
31945 static bool
31946 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31948 cp_token *pragma_tok;
31949 unsigned int id;
31951 pragma_tok = cp_lexer_consume_token (parser->lexer);
31952 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31953 parser->lexer->in_pragma = true;
31955 id = pragma_tok->pragma_kind;
31956 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31957 cp_ensure_no_omp_declare_simd (parser);
31958 switch (id)
31960 case PRAGMA_GCC_PCH_PREPROCESS:
31961 error_at (pragma_tok->location,
31962 "%<#pragma GCC pch_preprocess%> must be first");
31963 break;
31965 case PRAGMA_OMP_BARRIER:
31966 switch (context)
31968 case pragma_compound:
31969 cp_parser_omp_barrier (parser, pragma_tok);
31970 return false;
31971 case pragma_stmt:
31972 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31973 "used in compound statements");
31974 break;
31975 default:
31976 goto bad_stmt;
31978 break;
31980 case PRAGMA_OMP_FLUSH:
31981 switch (context)
31983 case pragma_compound:
31984 cp_parser_omp_flush (parser, pragma_tok);
31985 return false;
31986 case pragma_stmt:
31987 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31988 "used in compound statements");
31989 break;
31990 default:
31991 goto bad_stmt;
31993 break;
31995 case PRAGMA_OMP_TASKWAIT:
31996 switch (context)
31998 case pragma_compound:
31999 cp_parser_omp_taskwait (parser, pragma_tok);
32000 return false;
32001 case pragma_stmt:
32002 error_at (pragma_tok->location,
32003 "%<#pragma omp taskwait%> may only be "
32004 "used in compound statements");
32005 break;
32006 default:
32007 goto bad_stmt;
32009 break;
32011 case PRAGMA_OMP_TASKYIELD:
32012 switch (context)
32014 case pragma_compound:
32015 cp_parser_omp_taskyield (parser, pragma_tok);
32016 return false;
32017 case pragma_stmt:
32018 error_at (pragma_tok->location,
32019 "%<#pragma omp taskyield%> may only be "
32020 "used in compound statements");
32021 break;
32022 default:
32023 goto bad_stmt;
32025 break;
32027 case PRAGMA_OMP_CANCEL:
32028 switch (context)
32030 case pragma_compound:
32031 cp_parser_omp_cancel (parser, pragma_tok);
32032 return false;
32033 case pragma_stmt:
32034 error_at (pragma_tok->location,
32035 "%<#pragma omp cancel%> may only be "
32036 "used in compound statements");
32037 break;
32038 default:
32039 goto bad_stmt;
32041 break;
32043 case PRAGMA_OMP_CANCELLATION_POINT:
32044 switch (context)
32046 case pragma_compound:
32047 cp_parser_omp_cancellation_point (parser, pragma_tok);
32048 return false;
32049 case pragma_stmt:
32050 error_at (pragma_tok->location,
32051 "%<#pragma omp cancellation point%> may only be "
32052 "used in compound statements");
32053 break;
32054 default:
32055 goto bad_stmt;
32057 break;
32059 case PRAGMA_OMP_THREADPRIVATE:
32060 cp_parser_omp_threadprivate (parser, pragma_tok);
32061 return false;
32063 case PRAGMA_OMP_DECLARE_REDUCTION:
32064 cp_parser_omp_declare (parser, pragma_tok, context);
32065 return false;
32067 case PRAGMA_OMP_ATOMIC:
32068 case PRAGMA_OMP_CRITICAL:
32069 case PRAGMA_OMP_DISTRIBUTE:
32070 case PRAGMA_OMP_FOR:
32071 case PRAGMA_OMP_MASTER:
32072 case PRAGMA_OMP_ORDERED:
32073 case PRAGMA_OMP_PARALLEL:
32074 case PRAGMA_OMP_SECTIONS:
32075 case PRAGMA_OMP_SIMD:
32076 case PRAGMA_OMP_SINGLE:
32077 case PRAGMA_OMP_TASK:
32078 case PRAGMA_OMP_TASKGROUP:
32079 case PRAGMA_OMP_TEAMS:
32080 if (context != pragma_stmt && context != pragma_compound)
32081 goto bad_stmt;
32082 cp_parser_omp_construct (parser, pragma_tok);
32083 return true;
32085 case PRAGMA_OMP_TARGET:
32086 return cp_parser_omp_target (parser, pragma_tok, context);
32088 case PRAGMA_OMP_END_DECLARE_TARGET:
32089 cp_parser_omp_end_declare_target (parser, pragma_tok);
32090 return false;
32092 case PRAGMA_OMP_SECTION:
32093 error_at (pragma_tok->location,
32094 "%<#pragma omp section%> may only be used in "
32095 "%<#pragma omp sections%> construct");
32096 break;
32098 case PRAGMA_IVDEP:
32100 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32101 cp_token *tok;
32102 tok = cp_lexer_peek_token (the_parser->lexer);
32103 if (tok->type != CPP_KEYWORD
32104 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32105 && tok->keyword != RID_DO))
32107 cp_parser_error (parser, "for, while or do statement expected");
32108 return false;
32110 cp_parser_iteration_statement (parser, true);
32111 return true;
32114 case PRAGMA_CILK_SIMD:
32115 if (context == pragma_external)
32117 error_at (pragma_tok->location,
32118 "%<#pragma simd%> must be inside a function");
32119 break;
32121 cp_parser_cilk_simd (parser, pragma_tok);
32122 return true;
32124 case PRAGMA_CILK_GRAINSIZE:
32125 if (context == pragma_external)
32127 error_at (pragma_tok->location,
32128 "%<#pragma cilk grainsize%> must be inside a function");
32129 break;
32132 /* Ignore the pragma if Cilk Plus is not enabled. */
32133 if (flag_cilkplus)
32135 cp_parser_cilk_grainsize (parser, pragma_tok);
32136 return true;
32138 else
32140 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32141 "%<#pragma cilk grainsize%>");
32142 break;
32145 default:
32146 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32147 c_invoke_pragma_handler (id);
32148 break;
32150 bad_stmt:
32151 cp_parser_error (parser, "expected declaration specifiers");
32152 break;
32155 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32156 return false;
32159 /* The interface the pragma parsers have to the lexer. */
32161 enum cpp_ttype
32162 pragma_lex (tree *value)
32164 cp_token *tok;
32165 enum cpp_ttype ret;
32167 tok = cp_lexer_peek_token (the_parser->lexer);
32169 ret = tok->type;
32170 *value = tok->u.value;
32172 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32173 ret = CPP_EOF;
32174 else if (ret == CPP_STRING)
32175 *value = cp_parser_string_literal (the_parser, false, false);
32176 else
32178 cp_lexer_consume_token (the_parser->lexer);
32179 if (ret == CPP_KEYWORD)
32180 ret = CPP_NAME;
32183 return ret;
32187 /* External interface. */
32189 /* Parse one entire translation unit. */
32191 void
32192 c_parse_file (void)
32194 static bool already_called = false;
32196 if (already_called)
32197 fatal_error ("inter-module optimizations not implemented for C++");
32198 already_called = true;
32200 the_parser = cp_parser_new ();
32201 push_deferring_access_checks (flag_access_control
32202 ? dk_no_deferred : dk_no_check);
32203 cp_parser_translation_unit (the_parser);
32204 the_parser = NULL;
32207 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32208 vectorlength clause:
32209 Syntax:
32210 vectorlength ( constant-expression ) */
32212 static tree
32213 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32214 bool is_simd_fn)
32216 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32217 tree expr;
32218 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32219 safelen clause. Thus, vectorlength is represented as OMP 4.0
32220 safelen. For SIMD-enabled function it is represented by OMP 4.0
32221 simdlen. */
32222 if (!is_simd_fn)
32223 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32224 loc);
32225 else
32226 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32227 loc);
32229 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32230 return error_mark_node;
32232 expr = cp_parser_constant_expression (parser);
32233 expr = maybe_constant_value (expr);
32235 /* If expr == error_mark_node, then don't emit any errors nor
32236 create a clause. if any of the above functions returns
32237 error mark node then they would have emitted an error message. */
32238 if (expr == error_mark_node)
32240 else if (!TREE_TYPE (expr)
32241 || !TREE_CONSTANT (expr)
32242 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32243 error_at (loc, "vectorlength must be an integer constant");
32244 else if (TREE_CONSTANT (expr)
32245 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32246 error_at (loc, "vectorlength must be a power of 2");
32247 else
32249 tree c;
32250 if (!is_simd_fn)
32252 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32253 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32254 OMP_CLAUSE_CHAIN (c) = clauses;
32255 clauses = c;
32257 else
32259 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32260 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32261 OMP_CLAUSE_CHAIN (c) = clauses;
32262 clauses = c;
32266 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32267 return error_mark_node;
32268 return clauses;
32271 /* Handles the Cilk Plus #pragma simd linear clause.
32272 Syntax:
32273 linear ( simd-linear-variable-list )
32275 simd-linear-variable-list:
32276 simd-linear-variable
32277 simd-linear-variable-list , simd-linear-variable
32279 simd-linear-variable:
32280 id-expression
32281 id-expression : simd-linear-step
32283 simd-linear-step:
32284 conditional-expression */
32286 static tree
32287 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32289 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32292 return clauses;
32293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32295 cp_parser_error (parser, "expected identifier");
32296 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32297 return error_mark_node;
32300 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32301 parser->colon_corrects_to_scope_p = false;
32302 while (1)
32304 cp_token *token = cp_lexer_peek_token (parser->lexer);
32305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32307 cp_parser_error (parser, "expected variable-name");
32308 clauses = error_mark_node;
32309 break;
32312 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32313 false, false);
32314 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32315 token->location);
32316 if (decl == error_mark_node)
32318 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32319 token->location);
32320 clauses = error_mark_node;
32322 else
32324 tree e = NULL_TREE;
32325 tree step_size = integer_one_node;
32327 /* If present, parse the linear step. Otherwise, assume the default
32328 value of 1. */
32329 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32331 cp_lexer_consume_token (parser->lexer);
32333 e = cp_parser_assignment_expression (parser);
32334 e = maybe_constant_value (e);
32336 if (e == error_mark_node)
32338 /* If an error has occurred, then the whole pragma is
32339 considered ill-formed. Thus, no reason to keep
32340 parsing. */
32341 clauses = error_mark_node;
32342 break;
32344 else if (type_dependent_expression_p (e)
32345 || value_dependent_expression_p (e)
32346 || (TREE_TYPE (e)
32347 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32348 && (TREE_CONSTANT (e)
32349 || DECL_P (e))))
32350 step_size = e;
32351 else
32352 cp_parser_error (parser,
32353 "step size must be an integer constant "
32354 "expression or an integer variable");
32357 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32358 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32359 OMP_CLAUSE_DECL (l) = decl;
32360 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32361 OMP_CLAUSE_CHAIN (l) = clauses;
32362 clauses = l;
32364 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32365 cp_lexer_consume_token (parser->lexer);
32366 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32367 break;
32368 else
32370 error_at (cp_lexer_peek_token (parser->lexer)->location,
32371 "expected %<,%> or %<)%> after %qE", decl);
32372 clauses = error_mark_node;
32373 break;
32376 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32377 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32378 return clauses;
32381 /* Returns the name of the next clause. If the clause is not
32382 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32383 token is not consumed. Otherwise, the appropriate enum from the
32384 pragma_simd_clause is returned and the token is consumed. */
32386 static pragma_omp_clause
32387 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32389 pragma_omp_clause clause_type;
32390 cp_token *token = cp_lexer_peek_token (parser->lexer);
32392 if (token->keyword == RID_PRIVATE)
32393 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32394 else if (!token->u.value || token->type != CPP_NAME)
32395 return PRAGMA_CILK_CLAUSE_NONE;
32396 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32397 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32398 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32399 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32400 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32401 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32402 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32403 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32404 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32405 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32406 else
32407 return PRAGMA_CILK_CLAUSE_NONE;
32409 cp_lexer_consume_token (parser->lexer);
32410 return clause_type;
32413 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32415 static tree
32416 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32418 tree clauses = NULL_TREE;
32420 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32421 && clauses != error_mark_node)
32423 pragma_omp_clause c_kind;
32424 c_kind = cp_parser_cilk_simd_clause_name (parser);
32425 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32426 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32427 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32428 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32429 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32430 /* Use the OpenMP 4.0 equivalent function. */
32431 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32432 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32433 /* Use the OpenMP 4.0 equivalent function. */
32434 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32435 clauses);
32436 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32437 /* Use the OMP 4.0 equivalent function. */
32438 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32439 clauses);
32440 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32441 /* Use the OMP 4.0 equivalent function. */
32442 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32443 else
32445 clauses = error_mark_node;
32446 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32447 break;
32451 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32453 if (clauses == error_mark_node)
32454 return error_mark_node;
32455 else
32456 return c_finish_cilk_clauses (clauses);
32459 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32461 static void
32462 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32464 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32466 if (clauses == error_mark_node)
32467 return;
32469 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32471 error_at (cp_lexer_peek_token (parser->lexer)->location,
32472 "for statement expected");
32473 return;
32476 tree sb = begin_omp_structured_block ();
32477 int save = cp_parser_begin_omp_structured_block (parser);
32478 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32479 if (ret)
32480 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32481 cp_parser_end_omp_structured_block (parser, save);
32482 add_stmt (finish_omp_structured_block (sb));
32485 /* Main entry-point for parsing Cilk Plus _Cilk_for
32486 loops. The return value is error_mark_node
32487 when errors happen and CILK_FOR tree on success. */
32489 static tree
32490 cp_parser_cilk_for (cp_parser *parser, tree grain)
32492 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32493 gcc_unreachable ();
32495 tree sb = begin_omp_structured_block ();
32496 int save = cp_parser_begin_omp_structured_block (parser);
32498 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32499 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32500 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32501 clauses = finish_omp_clauses (clauses);
32503 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32504 if (ret)
32505 cpp_validate_cilk_plus_loop (ret);
32506 else
32507 ret = error_mark_node;
32509 cp_parser_end_omp_structured_block (parser, save);
32510 add_stmt (finish_omp_structured_block (sb));
32511 return ret;
32514 /* Create an identifier for a generic parameter type (a synthesized
32515 template parameter implied by `auto' or a concept identifier). */
32517 static GTY(()) int generic_parm_count;
32518 static tree
32519 make_generic_type_name ()
32521 char buf[32];
32522 sprintf (buf, "auto:%d", ++generic_parm_count);
32523 return get_identifier (buf);
32526 /* Predicate that behaves as is_auto_or_concept but matches the parent
32527 node of the generic type rather than the generic type itself. This
32528 allows for type transformation in add_implicit_template_parms. */
32530 static inline bool
32531 tree_type_is_auto_or_concept (const_tree t)
32533 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32536 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32537 (creating a new template parameter list if necessary). Returns the newly
32538 created template type parm. */
32540 tree
32541 synthesize_implicit_template_parm (cp_parser *parser)
32543 gcc_assert (current_binding_level->kind == sk_function_parms);
32545 /* We are either continuing a function template that already contains implicit
32546 template parameters, creating a new fully-implicit function template, or
32547 extending an existing explicit function template with implicit template
32548 parameters. */
32550 cp_binding_level *const entry_scope = current_binding_level;
32552 bool become_template = false;
32553 cp_binding_level *parent_scope = 0;
32555 if (parser->implicit_template_scope)
32557 gcc_assert (parser->implicit_template_parms);
32559 current_binding_level = parser->implicit_template_scope;
32561 else
32563 /* Roll back to the existing template parameter scope (in the case of
32564 extending an explicit function template) or introduce a new template
32565 parameter scope ahead of the function parameter scope (or class scope
32566 in the case of out-of-line member definitions). The function scope is
32567 added back after template parameter synthesis below. */
32569 cp_binding_level *scope = entry_scope;
32571 while (scope->kind == sk_function_parms)
32573 parent_scope = scope;
32574 scope = scope->level_chain;
32576 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32578 /* If not defining a class, then any class scope is a scope level in
32579 an out-of-line member definition. In this case simply wind back
32580 beyond the first such scope to inject the template parameter list.
32581 Otherwise wind back to the class being defined. The latter can
32582 occur in class member friend declarations such as:
32584 class A {
32585 void foo (auto);
32587 class B {
32588 friend void A::foo (auto);
32591 The template parameter list synthesized for the friend declaration
32592 must be injected in the scope of 'B'. This can also occur in
32593 erroneous cases such as:
32595 struct A {
32596 struct B {
32597 void foo (auto);
32599 void B::foo (auto) {}
32602 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32603 but, nevertheless, the template parameter list synthesized for the
32604 declarator should be injected into the scope of 'A' as if the
32605 ill-formed template was specified explicitly. */
32607 while (scope->kind == sk_class && !scope->defining_class_p)
32609 parent_scope = scope;
32610 scope = scope->level_chain;
32614 current_binding_level = scope;
32616 if (scope->kind != sk_template_parms
32617 || !function_being_declared_is_template_p (parser))
32619 /* Introduce a new template parameter list for implicit template
32620 parameters. */
32622 become_template = true;
32624 parser->implicit_template_scope
32625 = begin_scope (sk_template_parms, NULL);
32627 ++processing_template_decl;
32629 parser->fully_implicit_function_template_p = true;
32630 ++parser->num_template_parameter_lists;
32632 else
32634 /* Synthesize implicit template parameters at the end of the explicit
32635 template parameter list. */
32637 gcc_assert (current_template_parms);
32639 parser->implicit_template_scope = scope;
32641 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32642 parser->implicit_template_parms
32643 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32647 /* Synthesize a new template parameter and track the current template
32648 parameter chain with implicit_template_parms. */
32650 tree synth_id = make_generic_type_name ();
32651 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32652 synth_id);
32653 tree new_parm
32654 = process_template_parm (parser->implicit_template_parms,
32655 input_location,
32656 build_tree_list (NULL_TREE, synth_tmpl_parm),
32657 /*non_type=*/false,
32658 /*param_pack=*/false);
32661 if (parser->implicit_template_parms)
32662 parser->implicit_template_parms
32663 = TREE_CHAIN (parser->implicit_template_parms);
32664 else
32665 parser->implicit_template_parms = new_parm;
32667 tree new_type = TREE_TYPE (getdecls ());
32669 /* If creating a fully implicit function template, start the new implicit
32670 template parameter list with this synthesized type, otherwise grow the
32671 current template parameter list. */
32673 if (become_template)
32675 parent_scope->level_chain = current_binding_level;
32677 tree new_parms = make_tree_vec (1);
32678 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32679 current_template_parms = tree_cons (size_int (processing_template_decl),
32680 new_parms, current_template_parms);
32682 else
32684 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32685 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32686 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32687 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32690 current_binding_level = entry_scope;
32692 return new_type;
32695 /* Finish the declaration of a fully implicit function template. Such a
32696 template has no explicit template parameter list so has not been through the
32697 normal template head and tail processing. synthesize_implicit_template_parm
32698 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32699 provided if the declaration is a class member such that its template
32700 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32701 form is returned. Otherwise NULL_TREE is returned. */
32703 tree
32704 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32706 gcc_assert (parser->fully_implicit_function_template_p);
32708 if (member_decl_opt && member_decl_opt != error_mark_node
32709 && DECL_VIRTUAL_P (member_decl_opt))
32711 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32712 "implicit templates may not be %<virtual%>");
32713 DECL_VIRTUAL_P (member_decl_opt) = false;
32716 if (member_decl_opt)
32717 member_decl_opt = finish_member_template_decl (member_decl_opt);
32718 end_template_decl ();
32720 parser->fully_implicit_function_template_p = false;
32721 --parser->num_template_parameter_lists;
32723 return member_decl_opt;
32726 #include "gt-cp-parser.h"